<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

use Zizaco\Entrust\HasRole;

class User extends Eloquent implements UserInterface, RemindableInterface
{

	use UserTrait, RemindableTrait, HasRole;

	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'users';

	/**
	 * The attributes excluded from the model's JSON form.
	 *
	 * @var array
	 */
	protected $hidden = array();

	/**
	 * The attributes that can be massively assigned
	 */
	protected $fillable = array('ssn', 'first_name', 'surnames', 'email', 'role', 'school_id', 'has_access', 'cell_phone', 'office_phone', 'office_extension');

	/**
	 * Searchable rules.
	 *
	 * @var array
	 */
	protected $searchable = [
		'columns' => [
			'first_name' => 10,
			'surnames' => 10,
			'email' => 2,
		],
	];

	public function scopeFromSchool($query, $school_id)
	{
		return $query->join('program_user', 'program_user.user_id', '=', 'users.id')
			->join('programs', 'program_user.program_id', '=', 'programs.id')
			->where('programs.school_id', $school_id)
			->select('users.*');
	}

	public function scoreFromPrograms($query, $program_ids)
	{
		return $query->join("program_user", 'users.id', '=', 'program_user.user_id')
			->whereIn('program_user.program_id', $program_ids)
			->select('users.*');
	}

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

	public function school()
	{
		return $this->belongsTo('School');
	}

	public function courses()
	{
		return $this->hasMany('Course')->whereIn('semester_id', Session::get('semesters_ids'))->orderBy('code')->orderBy('number')->orderBy('section')->orderBy('semester_id');
	}

	public function rubrics()
	{
		return $this->hasMany('Rubric')->orderBy('name');
	}

	// return the programs the user is a pcoord of
	public function programs()
	{
		return $this->belongsToMany('Program')->select('programs.id', 'programs.name', 'programs.school_id', 'programs.is_graduate');
	}
}