public function dailyAction()
 {
     $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;
     $skillsHelper = new SkillsHelper();
     $classHelper = new ClassHelper();
     $studentIds = $classHelper->allStudents();
     //$studentIds = ["John Logie Baird"];
     // Update skill scores for every student, and save history
     foreach ($studentIds as $studentId) {
         // TODO make this more efficient
         $escStudentId = str_replace("'", "", $studentId);
         $history = new SkillHistory();
         $history->email = $escStudentId;
         $history->time = $skillsHelper->calculateTimeScore($studentId, $raw, $debug);
         $history->activity = $skillsHelper->calculateActivityScore($studentId, $raw, $debug);
         $history->consistency = $skillsHelper->calculateConsistencyScore($studentId, $raw, $debug);
         $history->awareness = $skillsHelper->calculateAwarenessScore($studentId, $raw, $debug);
         $history->deep_learning = $skillsHelper->calculateDeepLearningScore($studentId, $raw, $debug);
         $history->persistence = $skillsHelper->calculatePersistenceScore($studentId, $raw, $debug);
         if ($history->create() == false) {
             echo "*** Error saving history for {$studentId}\n";
         } else {
             echo "    Successfully saved history for {$studentId}\n";
         }
     }
     // Print total time taken
     $endTime = microtime(true);
     echo "Execution time: " . ($endTime - $startTime) . " seconds\n";
 }
 public function time_graphAction($weeks = "all", $debug = false)
 {
     $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;
     }
     // We want to show points (0 if no data for that day), for the past 2 weeks
     $historyPoints = [];
     ##Defauts to 100 for beginning of the semester, need to write a function that will actually calculate how many days
     ##there are until the beginning of the semester.
     $email = $context->getUserName();
     $email = str_replace("'", "", $email);
     // Fetch skill history items for the current student
     $historyResults = SkillHistory::find(["email = '{$email}'", "order" => 'time_stored DESC']);
     for ($i = 1; $i <= count($historyResults); $i++) {
         $formattedDate = date('M j', strtotime("-{$i} days"));
         // Array to hold 6 scores
         $historyPoints[$formattedDate] = [$formattedDate, 0, 0, 0, 0, 0, 0];
     }
     // Go through each, and if it's in our historyPoints array, set the score.
     // Doing it this way avoids duplicate data points (if historical skill saver ran twice in a day), or empty points, since all are initialized above
     foreach ($historyResults as $day) {
         // Scores are saved at 3am, so they actually correspond to the previous day
         $formattedDate = date('M j', strtotime('-1 day', strtotime($day->time_stored)));
         if (isset($historyPoints[$formattedDate])) {
             $historyPoints[$formattedDate] = [$formattedDate, $day->time, $day->activity, $day->consistency, $day->awareness, $day->deep_learning, $day->persistence];
         }
         if ($debug) {
             echo $day->time . "\n";
             echo $day->activity . "\n";
             echo $day->consistency . "\n";
             echo $day->awareness . "\n";
             echo $day->deep_learning . "\n";
             echo $day->persistence . "\n";
             echo $day->time_stored . "\n";
             echo $formattedDate . "\n";
             echo $day->email . "\n";
         }
     }
     if ($debug) {
         print_r($historyPoints);
     }
     // Output data as csv so that we only have to send header information once
     if (!$debug) {
         header("Content-Type: text/csv");
     }
     switch ($weeks) {
         case "2":
             $historyPoints = array_slice($historyPoints, 0, 14);
             break;
         case "4":
             $historyPoints = array_slice($historyPoints, 0, 28);
             break;
         default:
             break;
     }
     $historyPoints = array_reverse($historyPoints);
     $output = fopen("php://output", "w");
     // Header row
     fputcsv($output, ["date", "Time Management", "Online Activity", "Consistency", "Knowledge Awareness", "Deep Learning", "Persistence"]);
     foreach ($historyPoints as $row) {
         fputcsv($output, $row);
         // here you can change delimiter/enclosure
     }
     fclose($output);
 }