<?php

class Course extends Eloquent
{

  protected $fillable = array('name', 'code', 'number', 'section', 'program_id', 'user_id', 'semester_id');

  public function program()
  {
    return $this->belongsTo('Program');
  }

  public function students()
  {
  	return $this->belongsToMany('Student')->orderBy('name', 'asc')->withTimestamps();
  }

  public function user()
  {
    return $this->belongsTo('User')->orderBy('surnames', 'asc')->orderBy('first_name', 'asc');
  }

  // TODO: Remove
  public function activities()
  {
    return $this->hasMany('Activity')->orderBy('date', 'asc');
  }

  public function activity()
  {
      return $this->belongsTo('Activity');
  }

  public function assessedActivities()
  {
    return $this->hasMany('Activity')->whereNotNull('activities.outcomes_attempted')->orderBy('date', 'asc');
  }

  public function publishedActivities()
  {
    return $this->hasMany('Activity')->whereNotNull('activities.outcomes_attempted')->where('activities.draft', 0)->orderBy('date', 'asc');

  }

  public function semester()
  {
    return $this->belongsTo('Semester');
  }

  public function sectionsWithOutcome($outcome_id) 
  {
      return Course::has('activities')->with(array('activities'=>function($query) use (&$outcome_id){
        $query->where('outcomes_attempted', 'not like', '\'%"'.$outcome_id.'":0%\'');
      }))->where('code',$this->code)->where('number',$this->number)->get();

  }
}
