public function test_matrix()
 {
     $students = array(2 => 'Vineet Naik', 3 => 'Jimit Modi', 4 => 'Dharmesh Dhakan', 20 => 'Axansh Sheth');
     $exams = array(3 => 'Discrete Maths Monthly test', 6 => 'Simple Php test');
     $results = array_fill_keys(array_keys($students), array_fill_keys(array_keys($exams), '-'));
     $results[2][3] = 0;
     $results[3][3] = 12;
     $results[4][6] = 15;
     $results[20][6] = 24;
     $matrix = Examresult_Csv::matrix($students, $exams, $results);
     $expected = array(array('Student Id', 'Student Name', 'Discrete Maths Monthly test', 'Simple Php test'), array(2, 'Vineet Naik', 0, '-'), array(3, 'Jimit Modi', 12, '-'), array(4, 'Dharmesh Dhakan', '-', 15), array(20, 'Axansh Sheth', '-', 24));
     $this->assertEquals($expected, $matrix);
 }
 /**
  * @action download csv
  * @view None!
  * Create the csv for entering the results for the examgroup 
  * and force download of the csv file.
  * @param GET int examgroup_id
  */
 public function action_download_csv()
 {
     $examgroup_id = $this->request->param('examgroup_id');
     $examresult = new Examgroup_Examresult($examgroup_id);
     $examgroup = $examresult->examgroup();
     // get all the exams in this exam group
     $exams = $examresult->exams();
     $exams_arr = $examresult->exams_arr();
     // if no exams found, redirect to nil exams page
     if (!count($exams_arr)) {
         Session::instance()->set('examgroup_nil_exams', array('examgroup_id' => $examgroup_id, 'back_url' => Url::site('examresult/upload')));
         Request::current()->redirect('examgroup/nil_exams');
         exit;
     }
     // get all students in this examgroup
     $students = $examresult->students();
     // get saved results
     $results = $examresult->results();
     // get an array of default marks for a user-exam combination
     $csv_default_values = $this->csv_default_values($examresult);
     // create a final results array by merging the default array and saved data
     foreach ($csv_default_values as $user_id => $marks) {
         $results[$user_id] = Arr::get($results, $user_id, array()) + $marks;
     }
     // get the matrix array
     $matrix = Examresult_Csv::matrix($students, $exams_arr, $results);
     // var_dump($matrix); exit;
     $filename = Inflector::underscore($examgroup->name) . '_results.csv';
     header('Content-Type: text/csv');
     header('Content-Disposition: attachment;filename=' . $filename);
     $fp = fopen('php://output', 'w');
     foreach ($matrix as $row) {
         fputcsv($fp, $row);
     }
     fclose($fp);
     exit;
 }