<?php

class FillNewCriterionRubric extends Seeder
{

	public function run()
	{
		$rubrics = DB::table('rubrics')->get();
		$i = 0;
		foreach ($rubrics as $rubric) {
			$datos = json_decode($rubric->contents, true);
			// 			print_r($datos);
			// 			print"\n";
			// 			print $rubric->id;print"\t";
			// 			if($rubric->id==29){var_dump($datos);}
			foreach ($datos as $dato) {
				if ($rubric->id == 29) {
					print $dato["id"];
					print "Entrando\n";
				}
				$criterion = (array)DB::table('criteria')->where('id', $dato["id"])->first();
				// 				var_dump($criterion);
				$program_id = (array)DB::table('courses')
					->join('activities', 'courses.id', '=', 'activities.course_id')
					->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
					->select('courses.program_id')
					->where('rubric_activity.rubric_id', $rubric->id)
					->first();
				if (empty($program_id)) $program_id['program_id'] = 1;
				// 			else var_dump($program_id);

				// 				var_dump($program_id);
				// 				var_dump($criterion);
				if (!empty($criterion)) {
					$existe = (array)DB::table('objectives')->where('id', $criterion["id"])->first();
					$objective_id = $criterion["id"];
					if (empty($existe)) {
						$existe4 = (array)DB::table('objectives')->where('program_id', $program_id['program_id'])->where('text', $criterion["name"])->first();
						// 					print_r($existe4);print"\n";
						if (!empty($existe4)) {
							// 						DB::table('objectives')->insert(array("id"=> $criterion["id"],"program_id"=>$program_id['program_id'],"text"=>$criterion["name"],'outcome_id'=>$criterion['outcome_id']));
							$objective_id = $existe4["id"];
						} else {
							print "A crear entrada en objectives:  $objective_id";
							// 							var_dump(array("id"=> $objective_id,"program_id"=>$program_id['program_id'],"text"=>$criterion["name"],'outcome_id'=>$criterion['outcome_id']));
							DB::table('objectives')->insert(array("id" => $objective_id, "program_id" => $program_id['program_id'], "text" => $criterion["name"], 'outcome_id' => $criterion['outcome_id']));
						}
					} else {
						$existe4 = (array)DB::table('objectives')->where('program_id', $program_id['program_id'])->where('text', $criterion["name"])->first();
						// 					print_r($existe4);print"\n";
						if (empty($existe4)) {
							$objective_id = DB::table('objectives')->insertGetId(array("program_id" => $program_id['program_id'], "text" => $criterion["name"], 'outcome_id' => $criterion['outcome_id']));
						}
					}
					$criterion["objective_id"] = $objective_id;
					unset($criterion['outcome_id']);
					// 					var_dump($criterion);
					$existe2 = (array)DB::table('criteria')->where('id', $criterion["id"])->first();
					if (empty($existe2)) {
						DB::table('criteria')->insert($criterion);
					}
					$existe3 = (array)DB::table('rubric_criterion')->where('criterion_id', $criterion["id"])->where('rubric_id', $rubric->id)->first();
					if (empty($existe3)) {
						DB::table('rubric_criterion')->insert(
							array('criterion_id' => $dato["id"], 'rubric_id' => $rubric->id)
						);
					}
				}
				// 			if($i)exit();

			}
			// 			if($rubric->id==29){exit();}
			$i++;
		}


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




	}
}
