jeudi 11 février 2016

Is this a new Controller, a Service Provider or none of before?

I'm programing a Quiz App on Laravel 5.1. I've got two Models

  • A Question Model
  • A User Model

I'd like to implement de game mechanics, in order to when a user answer a question some points will be added to it own count.

Please review this code, then I need your support

Question Model:

<?php

namespace Gamify;

// use section

class Question extends Model {

    protected $table = 'questions';
    protected $fillable = array(
    'question',
    'solution',
    'points'
    );

    // some more code
}

User Model:

<?php

namespace Gamify;

// use section

class User extends Model {
    protected $table = 'users';
    protected $fillable = array(
        'username',
        'password',
    );

    // User's answered questions
    public function answeredQuestions()
    {
        return $this->belongsToMany('Gamify\Question', 'users_questions', 'user_id', 'question_id')
            ->withPivot('points', 'answers');
    }
}

In order to keep points I'm using this pivot table:

Schema::create('points', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')
            ->references('id')->on('users');
        $table->integer('points')->unsigned();
        $table->string('description');
        $table->index('user_id');
        $table->timestamps();
 });

On the controller responsable for answering questions I'd like to implement this code:

QuestionController:

<?php

namespace Gamify\Http\Controllers;

use Gamify\Question;
use Illuminate\Http\Request;

use Gamify\Http\Requests;
use Illuminate\Support\Facades\Auth;

class QuestionController extends Controller
{

    public function answer(Request $request, Question $question)
    {
        // AI. Obtain how many points has its answer obtained
        $points = 0;
        if ($request->answer == $question->answer) {
            $points = $questions->points;
        }

        // AI. Add XP to user
        Gamify::give($points)

        // AI. Add notifications and return view
       return view('question.show', compact('question'));
    }
}

And now my question...

Gamify::give() will insert a record to pivot table *points.

I'm not sure if this is a new Controller, a Service Provider or what...

How must I implement Gamify::give()?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire