Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
<?php namespace Modules\Exams\Entities;
   
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Pingpong\Modules\Exceptions\ModuleNotFoundException;
use Prettus\Repository\Contracts\Presentable;
use Prettus\Repository\Traits\PresentableTrait;

/**
 * Modules\Exams\Entities\Exam
 *
 * @property integer $id
 * @property string $title
 * @property boolean $is_active
 * @property integer $course_id
 * @property integer $questionCount
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property string $deleted_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\Modules\Courses\Entities\Course $course
 * @property-read \Illuminate\Database\Eloquent\Collection|\Modules\Exams\Entities\Question[] $question
 * @property-read \Illuminate\Database\Eloquent\Collection|\Modules\Exams\Entities\Answer[] $answer
 * @property-read \Illuminate\Database\Eloquent\Collection|\Modules\Students\Entities\Student[] $student
 * @property-read \Modules\Exams\Entities\Question $questionsCount
 * @property-read mixed $question_count
 * @method static \Illuminate\Database\Query\Builder|\Modules\Exams\Entities\Exam whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\Modules\Exams\Entities\Exam whereTitle($value)
 * @method static \Illuminate\Database\Query\Builder|\Modules\Exams\Entities\Exam whereIsActive($value)
 * @method static \Illuminate\Database\Query\Builder|\Modules\Exams\Entities\Exam whereCourseId($value)
 * @method static \Illuminate\Database\Query\Builder|\Modules\Exams\Entities\Exam whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\Modules\Exams\Entities\Exam whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\Modules\Exams\Entities\Exam whereDeletedAt($value)
 * @mixin \Eloquent
 */
class Exam extends Model implements Presentable {

    use PresentableTrait,
        SoftDeletes;
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'exams';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['title', 'is_active', 'course_id'];

    /**
     * Get the course for the exam
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function course()
    {
        if (\Module::has('Courses') == false) {
            throw new ModuleNotFoundException();
        }

        return $this->belongsTo('Modules\Courses\Entities\Course');

    }

    /**
     * Get the questions for the exam
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function question()
    {
        return $this->hasMany('Modules\Exams\Entities\Question');

    }

    /**
     * Get the answers for the exam
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function answer()
    {
        return $this->hasMany('Modules\Exams\Entities\Answer');

    }

    /**
     * Get the answers for the exam
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function student()
    {
        return $this->belongsToMany('Modules\Students\Entities\Student')->withPivot(['id']);
    }

    public function questionsCount()
    {
        return $this->hasOne('Modules\Exams\Entities\Question')
            ->selectRaw('exam_id, count(*) as aggregate')
            ->groupBy('exam_id');
    }

    public function getQuestionCountAttribute()
    {
        // if relation is not loaded already, let's do it first
        if ( !$this->relationLoaded('questionsCount'))
            $this->load('questionsCount');

        $related = $this->getRelation('questionsCount');

        // then return the count directly
        return ($related) ? (int) $related->aggregate : 0;
    }
}