/** Score page */ public function sc($test_code, $token = NULL) { $test = $this->get_test_or_die($test_code); $data['page_title'] = $test->name . ' - ' . lang('scores'); $data = $this->set_test_data($data, $test); $data = $this->set_token_data($data, $token); if ($data['valid_token'] && substr($test->code, 0, 4) === 'ncdi') { // Calculate the scores $testinvite = $data['testinvite']; $scores = create_ncdi_score_array($test, $testinvite); $data['ncdi_table'] = create_ncdi_table($scores); // Find any existing previous scores for this participant $participant = $this->participantModel->get_participant_by_id($data['participant_id']); $prev_testinvites = $this->testInviteModel->get_previous_testinvites($participant, $testinvite); $data['has_prev_results'] = FALSE; // Add some comments on the results $comments = $this->add_comments_to_score($scores, $participant); $data['ncdi_text'] = ul($comments); // Loop over the scores and add them to the page (without comments) $prev_tables = array(); $prev_descs = array(); foreach ($prev_testinvites as $prev_testinvite) { $scores = create_ncdi_score_array($test, $prev_testinvite); $date = output_date($prev_testinvite->datecompleted); $gender = gender_child($participant->gender); $age = age_in_months($participant, $prev_testinvite->datecompleted); $data['has_prev_results'] = TRUE; array_push($prev_tables, create_ncdi_table($scores)); array_push($prev_descs, sprintf('Resultaten van %s. Uw %s was toen <strong>%s maanden</strong> oud.', $date, $gender, $age)); } $data['ncdi_prev_tables'] = $prev_tables; $data['ncdi_prev_descs'] = $prev_descs; } $this->load->view($this->view_code($test, 'header'), $data); $this->load->view($this->view_code($test, 'scores'), $data); $this->load->view('templates/footer'); }
/** Creates a .csv-file from a table of scores (testinvite_id -> score) */ function scores_to_csv($test_code, $score_table, $experiment_id = NULL) { $CI =& get_instance(); // Retrieve the headers $headers = array(lang('reference_number'), lang('gender'), lang('age'), lang('age_md'), lang('dyslexicparent'), lang('multilingual')); if ($experiment_id) { array_unshift($headers, lang('part_number')); } // Add test categories $test = $CI->testModel->get_test_by_code($test_code); $testcats = $CI->testCatModel->get_testcats_by_test($test->id, FALSE, TRUE); foreach ($testcats as $testcat) { $headers[] = $testcat->code . ' - ' . $testcat->name; } // For N-CDI: add parent test categories if ($test_code == 'ncdi_wz') { $parent_testcats = $CI->testCatModel->get_testcats_by_test($test->id, TRUE); foreach ($parent_testcats as $parent) { $headers[] = $parent->name . ' - ' . lang('raw_score'); $headers[] = $parent->name . ' - ' . lang('percentile'); $headers[] = $parent->name . ' - ' . lang('language_age'); } } // Add headers to the csv array (later used in fputscsv) $csv_array = array(); $csv_array[] = $headers; // Generate array for each row and put in total array foreach ($score_table as $testinvite_id => $scores) { $testinvite = $CI->testInviteModel->get_testinvite_by_id($testinvite_id); $participant = $CI->testInviteModel->get_participant_by_testinvite($testinvite); // Add participant data $refnr = reference_number($participant); $g = $participant->gender; $age = age_in_months($participant, $testinvite->datecompleted); $agemd = age_in_months_and_days($participant->dateofbirth, $testinvite->datecompleted); $d = $participant->dyslexicparent ? $participant->dyslexicparent : lang('no'); $m = $participant->multilingual ? lang('yes') : lang('no'); $csv_row = array($refnr, $g, $age, $agemd, $d, $m); if ($experiment_id) { $participation = $CI->participationModel->get_participation($experiment_id, $participant->id); array_unshift($csv_row, $participation->part_number); } // Add score data foreach ($testcats as $testcat) { $score = isset($scores[$testcat->id]) ? $scores[$testcat->id] : ''; array_push($csv_row, $score); } // For N-CDI: total score data if ($test_code == 'ncdi_wz') { $totals = create_ncdi_score_array($test, $testinvite, TRUE); foreach ($totals as $total) { array_push($csv_row, $total['score'], $total['percentile'], $total['age']); } } // Add row to csv array $csv_array[] = $csv_row; } // Create a new output stream and capture the result in a new object $fp = fopen('php://output', 'w'); ob_start(); // Create a new row in the CSV file for every in the array foreach ($csv_array as $row) { fputcsv($fp, $row, ';'); } // Capture the output as a string $csv = ob_get_contents(); // Close the object and the stream ob_end_clean(); fclose($fp); return $csv; }