<?php

class Activity extends Eloquent
{

  public function section()
  {
    return $this->belongsTo('Section');
  }

  public function rubric()
  {
    //     return $this->belongsTo('Rubric');
    return $this->belongsToMany('Rubric', 'rubric_activity');
  }

  public function course()
  {
    return $this->belongsTo('Course');
  }

  public function assessed_students()
  {
    return $this->belongsToMany('Student', 'assessments')->withPivot('scores', 'percentage')->withTimestamps();
  }
  public function amount_of_assessed_students()
  {

    $activity_criterion = DB::table('activity_criterion')->where('activity_id', '=', $this->id)->lists('id');
    $amount_of_students = DB::table('assessments')->whereIn('activity_criterion_id', $activity_criterion)->lists('student_id');

    return count($amount_of_students);
  }

  public function criteria_achieved()
  {

    $activity_criterion = DB::table('activity_criterion')
      ->where('activity_id', '=', $this->id)
      ->get();
    $criteria_achieved = [];
    foreach ($activity_criterion as $index => $single_ac) {

      $assessments_passed = count(DB::table('assessments')
        ->where('score', '>=', $this->rubric[0]->expected_points)
        ->where('activity_criterion_id', '=', $single_ac->id)
        ->lists('student_id'));
      $assessments_attempted = count(DB::table('assessments')
        ->where('activity_criterion_id', '=', $single_ac->id)
        ->lists('student_id'));

      if ((($assessments_passed / $assessments_attempted) * 100) >= $this->rubric[0]->expected_percentage) {
        $criteria_achieved[$single_ac->criterion_id] = 1;
      } else {
        $criteria_achieved[$single_ac->criterion_id] = 0;
      }
    }
    return  $criteria_achieved;
  }



  // cap_array
  public function getCapArrayAttribute()
  {
    $criteria_achieved_percentage = [];


    $criterias = DB::table('criteria')
      ->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
      ->where('activity_criterion.activity_id', '=', $this->id)
      ->select(
        'activity_criterion.id as id',
        'activity_criterion.criterion_id as criterion_id',
        'activity_criterion.activity_id as activity_id'
      )
      ->addSelect('criteria.name', 'criteria.subcriteria')
      ->get();
    Log::info($criterias);
    Log::info($this->rubric);
    Log::info("this is rubric");
    foreach ($criterias as $index => $single_crit) {


      $single_crit->outcome_id = json_encode(DB::table('criterion_objective_outcome')
        ->where('criterion_id', '=', $single_crit->criterion_id)
        ->lists('outcome_id'));
      Log::info(json_decode($single_crit->outcome_id));
      $amount_of_students = count(DB::table('assessments')
        ->where("activity_criterion_id", '=', $single_crit->id)
        ->lists('student_id'));
      Log::info($amount_of_students);
      Log::info($single_crit->id);
      Log::info('lmaoOOOO');

      $student_pass = DB::table('assessments')
        ->where("activity_criterion_id", '=', $single_crit->id)
        ->where('score', '>=', $this->rubric[0]->expected_points)
        ->lists('student_id');
      Log::info($student_pass);
      $amount_of_students_passed = count(DB::table('assessments')
        ->where("activity_criterion_id", '=', $single_crit->id)
        ->where('score', '>=', $this->rubric[0]->expected_points)
        ->lists('student_id'));
      Log::info($amount_of_students_passed);
      Log::info('es aqui');
      $single_crit->score_percentage = ($amount_of_students_passed / $amount_of_students) * 100;
      $criteria_achieved_percentage[$single_crit->criterion_id] = $single_crit;
      Log::info("?");
    }
    return $criteria_achieved_percentage;
  }

  // o_ach_array
  public function getOAchArrayAttribute($value)
  {
    /*
    $outcomes_achieved = [];
    $all_criterion = DB::table('activity_criterion')
      ->where('activity_criterion.activity_id', '=', $this->id)
      ->get();

    $attempted_criteria = 0;
    $passed_criteria = 0;

    $all_outcomes = DB::table('outcomes')->get();


    foreach ($all_criterion as $index => $activity_crit) {

      $amount_of_outcomes_attempted = DB::table('criterion_objective_outcome')
        ->join('outcomes', 'outcomes.id', '=', 'criterion_objective_outcome.outcome_id')
        ->where('criterion_id', '=', $activity_crit->criterion_id)
        ->select('criterion_objective_outcome.outcome_id')
        ->distinct()
        ->orderBy('criterion_objective_outcome.outcome_id')
        ->select('outcomes.id', 'outcomes.expected_outcome')
        ->get();
      $passed_criteria = count(DB::table('assessments')
        ->where('activity_criterion_id', '=', $activity_crit->id)
        ->where('score', '>=', $this->rubric[0]->expected_points)
        ->lists('student_id'));
      $attempted_criteria = count(DB::table('assessments')
        ->where('activity_criterion_id', '=', $activity_crit->id)
        ->lists('student_id'));
      Log::info("For activity_crit " . ($activity_crit->id));
      $percent_for_criteria = ($passed_criteria / $attempted_criteria) * 100;
      Log::info('Percent calculated' . $percent_for_criteria);


      $percent_for_criteria = ($passed_criteria / $attempted_criteria) * 100;

      foreach ($amount_of_outcomes_attempted as $index2 => $outcome) {

        if ($percent_for_criteria >= $this->rubric[0]->expected_percentage) {
          if (array_key_exists($outcome->id, $outcomes_achieved)) $outcomes_achieved[$outcome->id] += 1;
          else $outcomes_achieved[$outcome->id] = 1;
        }
      }
    }

    return $outcomes_achieved;*/

    $outcomes_achieved = [];

    $criteria_achieved = $this->criteria_achieved();


    $all_outcomes = DB::table('outcomes')->lists('id');

    foreach ($all_outcomes as $outcome_id) {
      $outcomes_achieved[$outcome_id] = 0;
    }

    foreach ($criteria_achieved as $criterion_id => $passed) {

      if ($passed == 1) {
        $outcomes_related = DB::table('criterion_objective_outcome')
          ->where("criterion_id", '=', $criterion_id)
          ->select('outcome_id')
          ->distinct()
          ->lists('outcome_id');

        foreach ($outcomes_related as $index => $outcome_id) {

          $outcomes_achieved[$outcome_id] += 1;
        }
      }
    }
    return $outcomes_achieved;
  }

  // o_att_array
  public function getOAttArrayAttribute()
  {


    $outcomes_attempted = [];

    $all_criterion = DB::table('activity_criterion')
      ->where('activity_criterion.activity_id', '=', $this->id)
      ->lists('criterion_id');

    foreach ($all_criterion as $index => $criterion_id) {

      $amount_of_outcomes = DB::table('criterion_objective_outcome')
        ->where('criterion_id', '=', $criterion_id)
        ->select('criterion_objective_outcome.outcome_id')
        ->distinct()
        ->orderBy('criterion_objective_outcome.outcome_id')
        ->lists('outcome_id');

      foreach ($amount_of_outcomes as $index2 => $outcome_id) {
        if (array_key_exists($outcome_id, $outcomes_attempted)) $outcomes_attempted[$outcome_id] += 1;
        else $outcomes_attempted[$outcome_id] = 1;
      }
    }

    //return json_decode($this->outcomes_attempted, true);
    return $outcomes_attempted;
  }
}