/** * Returns the probability for sending the survey: * m;d percentage * 17;28 10% * 18;0 20% * 18;7 30% * 18;14 40% * 18;21 100% */ private function probability($participant, $m) { $age = explode(';', age_in_months_and_days($participant->dateofbirth)); $month = $age[0]; $day = $age[1]; if ($month < $m) { return $day < 28 ? 0 : 10; } else { return $day < 7 ? 20 : ($day < 14 ? 30 : ($day < 21 ? 40 : 100)); } }
/** Downloads all completed participations for a specific experiment */ public function download($experiment_id) { // Retrieve the participations and convert to .csv $participations = $this->participationModel->get_participations_by_experiment($experiment_id); // Add headers to the csv array (later used in fputscsv) $csv_array = array(); $csv_array[] = array(lang('part_number'), lang('gender'), lang('age_at_participation'), lang('appointment'), lang('dyslexic'), lang('multilingual'), lang('interrupted'), lang('excluded'), lang('completed'), lang('no_show'), lang('comment')); // Generate array for each row and put in total array foreach ($participations as $participation) { if (!$participation->completed) { continue; } $participant = $this->participationModel->get_participant_by_participation($participation->id); $csv_row = array($participation->part_number, $participant->gender, age_in_months_and_days($participant->dateofbirth, $participation->appointment), output_datetime($participation->appointment, TRUE), $participant->dyslexicparent ? $participant->dyslexicparent : lang('no'), $participant->multilingual ? lang('yes') : lang('no'), $participation->interrupted ? lang('yes') : lang('no'), $participation->excluded ? lang('yes') : lang('no'), $participation->completed ? lang('yes') : lang('no'), $participation->noshow ? lang('yes') : lang('no'), $participation->comment); // 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); // Generate filename $experiment_name = $this->experimentModel->get_experiment_by_id($experiment_id)->name; $escaped = preg_replace('/[^A-Za-z0-9_\\-]/', '_', $experiment_name); $filename = $escaped . '_' . mdate("%Y%m%d_%H%i", time()) . '.csv'; // Download the file force_download($filename, $csv); }
/** 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; }
<th><?php echo lang('appointment'); ?> </th> <td><?php echo output_datetime($participation->appointment); ?> </td> </tr> <tr> <th><?php echo lang('age'); ?> </th> <td><?php echo age_in_months_and_days($participant->dateofbirth, $participation->appointment); ?> </td> </tr> <?php } ?> <?php if (!empty($participation->completed)) { ?> <tr> <th><?php echo lang('part_number'); ?> </th> <td><?php
/** Returns the participants eligible for a (non-archived) experiment, they should be: * - activated * - not already participating in the experiment * - not have been participating in an experiment that excludes this experiment * - should have been participating in an experiment that is a prerequisite for this experiment * - not currently having an impediment * - not being a "risk" participant (depending on the sort of experiment) * - within the age range of the experiment */ public function find_participants($experiment, $weeks_ahead = WEEKS_AHEAD) { if ($experiment->archived) { return array(); } $prereqs = $this->relationModel->get_relation_ids_by_experiment($experiment->id, RelationType::Prerequisite, TRUE); $excludes = $this->relationModel->get_relation_ids_by_experiment($experiment->id, RelationType::Excludes, TRUE); $this->db->from('participant AS p', FALSE); $this->db->where('activated', TRUE); // not already participating (or cancelled) $this->db->where('NOT EXISTS (SELECT 1 FROM participation AS part WHERE part.participant_id = p.id AND (part.confirmed = 1 OR part.cancelled = 1) AND part.experiment_id = ' . $experiment->id . ')', NULL, FALSE); // should have been participating in an experiment that is a prerequisite for this experiment if ($prereqs) { $this->db->where('EXISTS (SELECT 1 FROM participation AS part WHERE part.participant_id = p.id AND part.completed = 1 AND part.experiment_id IN (' . implode(',', $prereqs) . '))', NULL, FALSE); } // not have been participating in an experiment that excludes this experiment if ($excludes) { $this->db->where('NOT EXISTS (SELECT 1 FROM participation AS part WHERE part.participant_id = p.id AND part.confirmed = 1 AND part.experiment_id IN (' . implode(',', $excludes) . '))', NULL, FALSE); } // not currently having an impediment $this->db->where('NOT EXISTS (SELECT 1 FROM impediment AS imp WHERE imp.participant_id = p.id AND (NOW() BETWEEN imp.from AND imp.to))', NULL, FALSE); // not being risk (depending on the sort of experiment) if ($experiment->dyslexic) { $this->db->where('multilingual', FALSE); } if ($experiment->multilingual) { $this->db->where('dyslexicparent IS NULL'); } if (!($experiment->dyslexic || $experiment->multilingual)) { $this->db->where('multilingual', FALSE); $this->db->where('dyslexicparent IS NULL'); } // Get the results $participants = $this->db->get()->result(); // Now check whether the participants are of correct age, from $weeks_ahead to now $result = array(); foreach ($participants as $participant) { $age_from = explode(';', age_in_months_and_days($participant->dateofbirth, input_date('+' . $weeks_ahead . ' weeks'))); $months_from = $age_from[0]; $days_from = $age_from[1]; $age_to = explode(';', age_in_months_and_days($participant->dateofbirth, input_date())); $months_to = $age_to[0]; $days_to = $age_to[1]; if ($months_from > $experiment->agefrommonths || $months_from == $experiment->agefrommonths && $days_from >= $experiment->agefromdays) { if ($months_to < $experiment->agetomonths || $months_to == $experiment->agetomonths && $days_to < $experiment->agetodays) { array_push($result, $participant); } } } return $result; }
/** Returns the age in months of the participant */ function age_in_md_by_id($participant_id, $date = 'now') { if (!$date) { return ''; } $CI =& get_instance(); $participant = $CI->participantModel->get_participant_by_id($participant_id); return age_in_months_and_days($participant->dateofbirth, $date); }
public function vs_scores($test_code) { $table = array(); $table['cols'] = array(array('label' => lang('testcat'), 'type' => 'string'), array('label' => lang('gender'), 'type' => 'string'), array('label' => lang('age'), 'type' => 'number'), array('label' => lang('score'), 'type' => 'number'), array('label' => '50e percentiel', 'type' => 'number')); $rows = array(); $testcat_ids = $this->get_testcat_ids($test_code); $nr = 0; foreach ($testcat_ids as $testcat_id) { $scores = $this->testCatModel->total_score_per_testinvite($testcat_id); foreach ($scores as $score) { if ($score->score > 0) { $testinvite = $this->testInviteModel->get_testinvite_by_id($score->testinvite_id); $participant = $this->testInviteModel->get_participant_by_testinvite($testinvite); $age = explode(';', age_in_months_and_days($participant->dateofbirth, $testinvite->datecompleted)); $age = $age[0] + $age[1] / 31; // to evenly divide over months. $testcat = $this->testCatModel->get_testcat_by_id($testcat_id); // FIXME: quick and dirty fix for percentiles without gender... $gender = in_array($testcat->code, array('w', 'z')) ? NULL : gender_sex($participant->gender); $rows[$nr][0] = array('v' => $testcat->name); $rows[$nr][1] = array('v' => $gender); $rows[$nr][2] = array('v' => $age); $rows[$nr][3] = array('v' => intval($score->score)); $rows[$nr][4] = array('v' => NULL); $nr++; } } } $percentiles = $this->percentileModel->get_percentiles_by_testcats($testcat_ids, array(50)); foreach ($percentiles as $percentile) { $testcat = $this->testCatModel->get_testcat_by_id($percentile->testcat_id)->name; $gender = !empty($percentile->gender) ? gender_sex($percentile->gender) : NULL; $rows[$nr][0] = array('v' => $testcat); $rows[$nr][1] = array('v' => $gender); $rows[$nr][2] = array('v' => $percentile->age); $rows[$nr][3] = array('v' => NULL); $rows[$nr][4] = array('v' => $percentile->score); $nr++; } //echo '<pre>' . var_dump($rows) . '</pre>';die; $table['rows'] = $this->flatten($rows); echo json_encode($table); }