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; } $email = $context->getUserName(); // Fetch skill history items for the current student $historyResults = StudentMasteryHistory::find(["email = '{$email}'", "order" => 'time_stored DESC']); $historyPoints = []; 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))); $historyPoints[] = [$formattedDate, round($day->unit1 * 100) / 100, round($day->unit2 * 100) / 100, round($day->unit3 * 100) / 100, round($day->unit4 * 100) / 100]; //$historyPoints []= [$formattedDate, rand(0,100) / 10, rand(0,100) / 10]; } // In case it gets saved multiple times, eliminate duplicates $historyPoints = array_unique($historyPoints, SORT_REGULAR); // Get the amount of data requested: switch ($weeks) { case "2": $historyPoints = array_slice($historyPoints, 0, 14); break; case "4": $historyPoints = array_slice($historyPoints, 0, 28); break; default: break; } // echo json_encode($historyPoints); // Put it in correct order $historyPoints = array_reverse($historyPoints); //print_r($historyPoints); 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"); } $output = fopen("php://output", "w"); // Header row fputcsv($output, ["date", "Unit 1", "Unit 2", "Unit 3", "Unit 4"]); foreach ($historyPoints as $row) { fputcsv($output, $row); // here you can change delimiter/enclosure } fclose($output); }