<?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 = [];

      foreach ($activities as $activity) {

        if ($activity->isStudentAssessed($student->id) == 0)
          continue;
        $assessments[$activity->id]['activity'] = $activity->name;
        $activity_criterion = DB::table('activity_criterion')
          ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
          ->where('activity_id', $activity->id)
          ->select('activity_criterion.id as ac_id', 'activity_criterion.weight')
          ->addSelect('criteria.*')
          ->get();


        $activity_comments = DB::table('activity_student')
          ->where('student_id', $student->id)
          ->where('activity_id', $activity->id)
          ->first()->comments;
        $sum_of_score = 0;
        $sum_of_weight = 0;
        $max_score = $activity->rubric[0]->max_score;
        foreach ($activity_criterion as $ac) {

          $assessment_score = DB::table('assessments')
            ->where('activity_criterion_id', $ac->ac_id)
            ->where('student_id', $student->id)
            ->first();


          if ($assessment_score) {
            Log::info($assessment_score->student_id);

            $assessments[$activity->id]["criteria"][$ac->id] = $ac;
            $sum_of_score += $ac->weight * $assessment_score->score;
            $sum_of_weight += $ac->weight;

            $assessments[$activity->id]["score"][$ac->id] = $assessment_score->score;
            $assessments[$activity->id]['comments'] = $activity_comments;

            $index_scale = ceil($assessment_score->score * $ac->num_scales / $ac->max_score) - 1;
            if ($index_scale == -1) $index_scale = 0;

            $assessments[$activity->id]["explication"][$ac->id] = DB::table('criterion_scale')
              ->join('scales', 'scales.id', '=', 'criterion_scale.scale_id')
              ->where("criterion_id", $ac->id)
              ->where("position", $index_scale)
              ->first()
              ->description;
          } else {
            $assessments[$activity->id]["criteria"][$ac->id] = $ac;
            $assessments[$activity->id]["score"][$ac->id] = "N/A";
            $assessments[$activity->id]["explication"][$ac->id] = '';
            $assessments[$activity->id]['comments'] = '';
          }
        }
        if ($sum_of_weight != 0)
          $assessments[$activity->id]['percentage'] = round((100 * ($sum_of_score / ($max_score * $sum_of_weight))), 2);
        else
          $assessments[$activity->id]['percentage'] = 0;
      }


      //$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;
    Log::info($assessments);

    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;
      */
    $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 = [];

      foreach ($activities as $activity) {

        if ($activity->isStudentAssessed($student->id) == 0)
          continue;
        $assessments[$activity->id]['activity'] = $activity->name;
        $activity_criterion = DB::table('activity_criterion')
          ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
          ->where('activity_id', $activity->id)
          ->select('activity_criterion.id as ac_id', 'activity_criterion.weight')
          ->addSelect('criteria.*')
          ->get();


        $activity_comments = DB::table('activity_student')
          ->where('student_id', $student->id)
          ->where('activity_id', $activity->id)
          ->first()->comments;
        $sum_of_score = 0;
        $sum_of_weight = 0;
        $max_score = $activity->rubric[0]->max_score;
        foreach ($activity_criterion as $ac) {

          $assessment_score = DB::table('assessments')
            ->where('activity_criterion_id', $ac->ac_id)
            ->where('student_id', $student->id)
            ->first();


          if ($assessment_score) {
            Log::info($assessment_score->student_id);

            $assessments[$activity->id]["criteria"][$ac->id] = $ac;
            $sum_of_score += $ac->weight * $assessment_score->score;
            $sum_of_weight += $ac->weight;

            $assessments[$activity->id]["score"][$ac->id] = $assessment_score->score;
            $assessments[$activity->id]['comments'] = $activity_comments;

            $index_scale = ceil($assessment_score->score * $ac->num_scales / $ac->max_score) - 1;
            if ($index_scale == -1) $index_scale = 0;

            $assessments[$activity->id]["explication"][$ac->id] = DB::table('criterion_scale')
              ->join('scales', 'scales.id', '=', 'criterion_scale.scale_id')
              ->where("criterion_id", $ac->id)
              ->where("position", $index_scale)
              ->first()
              ->description;
          } else {
            $assessments[$activity->id]["criteria"][$ac->id] = $ac;
            $assessments[$activity->id]["score"][$ac->id] = "N/A";
            $assessments[$activity->id]["explication"][$ac->id] = '';
            $assessments[$activity->id]['comments'] = '';
          }
        }
        if ($sum_of_weight != 0)
          $assessments[$activity->id]['percentage'] = round((100 * ($sum_of_score / ($max_score * $sum_of_weight))), 2);
        else
          $assessments[$activity->id]['percentage'] = 0;
      }
    } else
      $assessments = NULL;


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