<?php

class Rubric extends Eloquent
{

  /**
   * Searchable rules.
   *
   * @var array
   */
  protected $searchable = [
    'columns' => [
      'name' => 10,
    ],
  ];
  public function getTitles()
  {
    $titles = DB::table('titles')
      ->join('rubric_title', 'rubric_title.title_id', '=', 'titles.id')
      ->where('rubric_id', $this->id)
      ->orderBy('position', "ASC")
      ->get();
    return $titles;
  }

  public function professor()
  {
    return $this->belongsTo('User')->orderBy('created_at');
  }

  public function activities()
  {
    return $this->belongsToMany('Activity', 'rubric_activity');
  }

  public function getCriterion($id, $criterion_id)
  {
    $rubric = Rubric::findOrFail($id);

    $rubric_contents = json_decode($rubric->contents);

    foreach ($rubric_contents as $key => $criterion) {
      if ($criterion->id == $criterion_id) {
        return $criterion;
      }
    }
  }
}
