public function conceptsAction($unit = '1')
 {
     $this->view->disable();
     // Get our context (this takes care of starting the session, too)
     $context = $this->getDI()->getShared('ltiContext');
     if (!$context->valid) {
         echo '[{"error":"Invalid lti context"}]';
         return;
     }
     $concepts = MappingHelper::conceptsInUnit($unit);
     $maxPercentage = 0;
     $conceptArray = [];
     foreach ($concepts as $concept) {
         $conceptID = $concept['Lecture Number'];
         $historicalConceptMasteryScores = ClassConceptHistory::find(["concept_id = '{$conceptID}'", "order" => 'time_stored DESC']);
         $newConcept = ["id" => $conceptID, "title" => $concept["Concept Title"], "history" => []];
         foreach ($historicalConceptMasteryScores as $score) {
             $newConcept["history"][] = ["date" => $score->time_stored, "average" => $score->average_mastery, "percent" => $score->videopercentage];
         }
         $newConcept["history"] = $newConcept["history"][0];
         if ($newConcept["history"]["percent"] > $maxPercentage) {
             $maxPercentage = $newConcept["history"]["percent"];
         }
         $conceptsArray[] = $newConcept;
     }
     $firstRow = ["max" => $maxPercentage];
     array_unshift($conceptsArray, $firstRow);
     echo json_encode($conceptsArray);
 }
 public function classConceptHistoryAction()
 {
     $config = $this->getDI()->getShared('config');
     if (!isset($_GET["p"])) {
         die("No history saver password provided.");
     }
     if ($_GET["p"] != $config->historySaverPassword) {
         die("Invalid history saver password provided.");
     }
     // We want to time this
     $startTime = microtime(true);
     $raw = false;
     $debug = false;
     $classHelper = new ClassHelper();
     $masteryHelper = new MasteryHelper();
     $allConcepts = MappingHelper::allConcepts();
     $studentIds = $classHelper->allStudents();
     //$studentIds = ["John Logie Baird"];
     foreach ($allConcepts as $concept) {
         $classHelper = new ClassHelper();
         $masteryHelper = new MasteryHelper();
         $studentIds = $classHelper->allStudents();
         $sum = 0;
         foreach ($studentIds as $student) {
             $sum += $masteryHelper->calculateUniqueVideoPercentageForConcept($student, $concept);
         }
         $avg = $sum / count($studentIds);
         echo $avg;
         $lecNum = $concept["Lecture Number"];
         $lastHistory = ClassConceptHistory::findFirst(["concept_id = {$lecNum}", "order" => "time_stored DESC"]);
         if (date("Y-m-d") == date("Y-m-d", strtotime($lastHistory->time_stored))) {
             echo "    History already saved today for concept #{$lecNum}\n";
             continue;
         }
         $scoreSum = 0;
         $scoreCount = 0;
         // Check if it's a concept in the future
         $today = strtotime("today");
         $includeZero = true;
         if (strtotime($concept["Date"]) > $today) {
             $includeZero = false;
         }
         // Calculate concept score for each student
         foreach ($studentIds as $studentId) {
             // Calculate score
             $score = $masteryHelper->calculateConceptMasteryScore($studentId, $concept["Lecture Number"], $debug);
             // Add score
             if ($score == 0) {
                 if ($includeZero) {
                     $scoreCount++;
                 }
             } else {
                 $scoreCount++;
                 $scoreSum += $score;
             }
         }
         // Calculate average
         $average = $scoreSum / $scoreCount;
         // Store concept average
         $conceptHistory = new ClassConceptHistory();
         $conceptHistory->concept_id = $concept["Lecture Number"];
         $conceptHistory->average_mastery = $average;
         $conceptHistory->videopercentage = $avg;
         if ($conceptHistory->create() == false) {
             echo "*** Error saving concept history for {$concept}\n";
         } else {
         }
     }
     // Print total time taken
     $endTime = microtime(true);
     echo "Execution time: " . ($endTime - $startTime) . " seconds\n";
 }