<?php

class StudentsController extends \BaseController {

  public function show($semester_id, $course_identifier, $number)
  {

  	$student = Student::where('number', '=', $number)->first();
  	$code = substr($course_identifier, 0, 4);
  	$number = substr($course_identifier, 4, 4);
  	$section = substr($course_identifier, 9, 4);
  	$course = Course::where('code', $code)->where('number', $number)->where('section', $section)->where('semester_id', $semester_id)->first();
  	$title = $student->name;

  	$activities = $course->assessedActivities;
  	$activitiesArray = array();
  	foreach ($activities as $activity)
  	{
  		$activitiesArray[]=$activity->id;
  	}

    // If the student has be assessed
    if(!$activities->isEmpty())
    {
      // Get the assessments
      $assessments = DB::table('assessments')->whereIn('activity_id', $activitiesArray)->where('student_id', '=', $student->id)->orderBy('created_at')->get();

      // foreach ($assessments as $assessed_activity)
      // {
      //   $outcomes_achieved = array_fill(1, Outcome::all()->count(), 0);
      //   $outcomes_attempted = array_fill(1, Outcome::all()->count(), 0);

      //   $single_activity_scores = json_decode($assessed_activity->scores);
      //   foreach($single_activity_scores as $criterion_id => $criterion_score)
      //   {
      //       // Find corresponding learning outcome;
      //       $criterion = Criterion::withTrashed()->find($criterion_id);
      //       $outcome = Outcome::find($criterion->outcome_id);

      //       // If criterion is achieved (1), add 1 to all arrays
      //       if($criterion_score >= Rubric::find($activity->rubric_id)->expected_points)
      //       {
      //           $outcomes_attempted[$outcome->id]+=1;
      //           $outcomes_achieved[$outcome->id]+=1;;
      //       }
      //       // Else, only add to the attempted outcomes array
      //       else
      //       {
      //           $outcomes_attempted[$outcome->id]+=1;
      //       }
      //   }

      //   $outcomes_per_activity = array_fill(1, Outcome::all()->count(), 0);
      //   foreach ($outcomes_attempted as $index=>$outcome_attempted)
      //   {
      //     if($outcomes_attempted[$index]!=0)
      //     {
      //       // For each outcome in the activity, calculate and save the percentage
      //       $outcomes_per_activity[$index] = (float)$outcomes_achieved[$index]/$outcomes_attempted[$index]*100;
      //     }
      //   }

      //   //Save to activity array
      //   $activitiesArray[]=$outcomes_per_activity;
      // }
    }
    else
      $assessments = NULL;

    return View::make('local.professors.student', compact('student', 'course', 'title', 'assessments'));
  }

  public function printStudentReport($semester_id, $course_identifier, $number)
  {

    $student = Student::where('number', '=', $number)->first();
    $code = substr($course_identifier, 0, 4);
    $number = substr($course_identifier, 4, 4);
    $section = substr($course_identifier, 9, 4);
    $course = Course::where('code', $code)->where('number', $number)->where('section', $section)->where('semester_id', $semester_id)->first();
    $title = $student->name;

    $activities = $course->assessedActivities;
    $activitiesArray = array();
    foreach ($activities as $activity)
    {
      $activitiesArray[]=$activity->id;
    }

    if(!$activities->isEmpty())
    {
      $assessments = DB::table('assessments')->whereIn('activity_id', $activitiesArray)->where('student_id', '=', $student->id)->get();

      foreach ($assessments as $assessed_activity)
      {
        $outcomes_achieved = array_fill(1, Outcome::all()->count(), 0);
        $outcomes_attempted = array_fill(1, Outcome::all()->count(), 0);

        $single_activity_scores = json_decode($assessed_activity->scores);
        foreach($single_activity_scores as $criterion_id=>$criterion_score)
        {
            // Find corresponding learning outcome;
            $criterion = Criterion::withTrashed()->find($criterion_id);
            $outcome = Outcome::find($criterion->outcome_id);

            // If criterion is achieved (1), add 1 to all arrays
            if($criterion_score >= Rubric::find($activity->rubric_id)->expected_points)
            {
                $outcomes_attempted[$outcome->id]+=1;
                $outcomes_achieved[$outcome->id]+=1;;
            }
            // Else, only add to the attempted outcomes array
            else
            {
                $outcomes_attempted[$outcome->id]+=1;
            }
        }

        $outcomes_per_activity = array_fill(1, Outcome::all()->count(), 0);
        foreach ($outcomes_attempted as $index=>$outcome_attempted)
        {
          if($outcomes_attempted[$index]!=0)
          {
            // For each outcome in the activity, calculate and save the percentage
            $outcomes_per_activity[$index] = (float)$outcomes_achieved[$index]/$outcomes_attempted[$index]*100;
          }
        }

        //Save to activity array
        $activitiesArray[]=$outcomes_per_activity;
      }
    }
    else
      $assessments = NULL;

    return View::make('local.professors.print_student_report', compact('student', 'course', 'title', 'assessments', 'activitiesArray'));
  }
}