<?php

class FillNewAssessmentActivityCriterion extends Seeder
{

	public function run()
	{

		//   DB::enableQueryLog();
		$activities = DB::table('activities')
			->join('rubric_activity', 'activities.id', '=', 'rubric_activity.activity_id')
			->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
			->join('rubric_criterion', 'rubrics.id', '=', 'rubric_criterion.rubric_id')
			->select('activities.*', 'rubrics.expected_percentage', 'rubrics.expected_points', 'rubric_criterion.criterion_id')
			->orderBy('activities.id')
			->orderBy('rubric_criterion.criterion_id')
			->get();
		//   DB::enableQueryLog();
		// 		dd(DB::getQueryLog());
		// 		exit();
		$activity_antes = -1;
		foreach ($activities as $activity) {
			$activity_id = $activity->id;
			$expected_percentage_students_achieving = $activity->expected_percentage;
			$transformative_actions = $activity->transforming_actions;
			$assessment_comments = $activity->assessment_comments;
			$expected_student_score = $activity->expected_points;
			$criterion_id = $activity->criterion_id;
			// 			$datos=json_decode($activity->criteria_achieved, true);
			// 			$i=0;
			// 			foreach($datos as $criterion_id=>$score)
			// 			{
			$existe_ya = DB::table('activity_criterion')->where('activity_id', $activity_id)->where('criterion_id', $criterion_id)->first();
			if (empty($existe_ya)) {
				if ($activity_antes == $activity_id) {
					DB::table('activity_criterion')->insert(
						array(
							"activity_id" => $activity_id,
							"criterion_id" => $criterion_id,
							"expected_percentage_students_achieving" => $expected_percentage_students_achieving,
							"expected_student_score" => $expected_student_score
						)
					);
				} else {
					$activity_antes = $activity_id;
					DB::table('activity_criterion')->insert(
						array(
							"activity_id" => $activity_id,
							"criterion_id" => $criterion_id,
							"expected_percentage_students_achieving" => $expected_percentage_students_achieving,
							"expected_student_score" => $expected_student_score,
							"transformative_actions" => $transformative_actions,
							"assessment_comments" => $assessment_comments
						)
					);
				}
				print $activity_id . ':' . $criterion_id;
				print "\n";
			}
			// 				$i++;
			// 			}
		}

		//     exit();
		$assessments = DB::table('assessments')->get();
		$i = 0;
		foreach ($assessments as $assessment) {
			// 			var_dump($assessment);exit();
			$student_id = $assessment->student_id;
			$activity_id = $assessment->activity_id;
			$datos = json_decode($assessment->scores, true);
			// 			$criterion=(array)DB::table('activity_criterion')->where('activity_id', $activity_id)->first();
			// 			var_dump($rubric);exit();
			// 			$criterion_id=$criterion["criterion_id"];
			// 			print_r($datos);
			// 			print"\n";
			// 			print $rubric->id;print"\t";
			foreach ($datos as $criterion_id => $score) {
				// 				print $dato["id"];print"\n";
				// 				var_dump($criterion);
				$activity_criterion = DB::table('activity_criterion')->where('criterion_id', $criterion_id)->where('activity_id', $activity_id)->first();
				if (empty($activity_criterion)) {
					// 					var_dump($criterion_id);
					var_dump($assessment);
					// 					$activities=DB::table('activities')->where('id', $activity_id)->first();
					// 					$rubric_criterion=DB::table('rubric_criterion')->where('criterion_id', $criterion_id)->first();
					// 					var_dump($activities);
					// 					var_dump($rubric_criterion);
					// 					exit();
					$activity_criterion_id = DB::table('activity_criterion')->insertGetId(
						array(
							"activity_id" => $activity_id,
							"criterion_id" => $criterion_id,
							"transformative_actions" => $transformative_actions,
							"assessment_comments" => $assessment_comments
						)
					);
				} else $activity_criterion_id = $activity_criterion->id;
				$existe_ya = DB::table('assessments')->where('student_id', $student_id)->where('activity_criterion_id', $activity_criterion_id)->first();
				if (empty($existe_ya)) {
					DB::table('assessments')->insert(array("student_id" => $student_id, "activity_criterion_id" => $activity_criterion_id, 'score' => $score));
					print ".";
				}
			}
			// 			print $activity_id.':'.$criterion_id;print"\n";

			$i++;
		}


		// 		$course = Rubric::where('code', $code)->where('number', $number)->where('section', $section)->where('semester_id', $semester_id)->first();




	}
}
