<?php

use Illuminate\Database\Eloquent\SoftDeletingTrait;

class Criterion extends Eloquent
{
	use SoftDeletingTrait;
	protected $dates = ['deleted_at'];

	//     protected $table = 'new_criteria';
	protected $table = 'criteria';

	public function outcomes()
	{
		//	    return $this->belongs('Objective')->belongs('Outcome');
		//	    TODO: Changes here
		//		return $this->belongs('Outcome');
		return $this->hasManyThrough('Outcome', 'Objective');
	}

	public function objectives()
	{
		return $this->belongsToMany('Objective');
	}

	public function rubrics()
	{
		//         return $this->belongsToMany('Rubric', 'new_rubric_criterion');
		return $this->belongsToMany('Rubric', 'rubric_criterion');
	}

	/**
	 * Return the program that the criterion belongs to
	 *
	 * @return Illuminate\Database\Eloquent\Model
	 */
	public function program()
	{
		return $this->belongsTo('Program');
	}

	// 	public function activities()
	// 	{
	// 		return $this->hasManyThrough('Activity','activity_criterion');
	// 	}
	// 
	public function subcriteria()
	{
		return json_decode($this->subcriteria);
	}

	public static function students_attempted($criterion_id, $activity_id)
	{
		$students_attempted = 0;
		// 		var_dump($criterion_id);
		// 		var_dump($semester);
		// 		exit();
		$activities_criterions = DB::table('activity_criterion')
			->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
			->join('courses', 'activities.course_id', '=', 'courses.id')
			->where('activities.id', '=', $activity_id)
			->where('activity_criterion.criterion_id', '=', $criterion_id)
			->select('activity_criterion.id')
			->distinct()
			->get();

		foreach ($activities_criterions as $activity_criterion) {

			// 			 $students_attempted+=DB::table('new_assessments')
			// 				->join('activity_criterion', 'new_assessments.activity_criterion_id', '=', 'activity_criterion.id')
			// 				->where('activity_criterion.id',$activity_criterion->id)
			// 				->count();

			$students_attempted += DB::table('assessments')
				->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
				->where('activity_criterion.id', $activity_criterion->id)
				->count();
		}
		return $students_attempted;
	}

	public static function students_achieved($criterion_id, $activity_id)
	{
		$students_achieved = 0;
		// 		$activities_criterions=DB::table('activity_criterion')
		// 			->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
		// 			->join('courses', 'activities.course_id', '=', 'courses.id')
		// 			->where('activities.id','=',$activity_id)
		// 			->where('activity_criterion.criterion_id','=',$criterion_id)
		// 			->select('activity_criterion.id','expected_student_score')
		// 			->distinct()
		// 			->get();

		$activities_criterions = DB::table('activity_criterion')
			->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
			->join('courses', 'activities.course_id', '=', 'courses.id')
			->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
			->join('rubrics', 'rubric_activity.rubric_id', '=', 'rubrics.id')
			->where('activities.id', '=', $activity_id)
			->where('activity_criterion.criterion_id', '=', $criterion_id)
			->select('activity_criterion.id', 'expected_points')
			->distinct()
			->get();

		foreach ($activities_criterions as $activity_criterion) {

			// 			$expected_student_score=DB::table('new_criteria')->where('id',$criterion_id)->select('expected_student_score')->get();
			// 			 $students_achieved+=DB::table('new_assessments')
			// 				->join('activity_criterion', 'new_assessments.activity_criterion_id', '=', 'activity_criterion.id')
			// 				->where('activity_criterion.id',$activity_criterion->id)
			// 				->where('new_assessments.score','>=',$activity_criterion->expected_student_score)
			// 				->count();

			$students_achieved += DB::table('assessments')
				->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
				->where('activity_criterion.id', $activity_criterion->id)
				->where('assessments.score', '>=', $activity_criterion->expected_points)
				->count();
		}
		return $students_achieved;
	}
}