Skip to content

Commit

Permalink
GHI237 Allow rank choice averages to be exported with responses.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Churchward committed Jan 15, 2020
1 parent 9d4a078 commit 5f53db3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
1 change: 0 additions & 1 deletion classes/responsetype/display_support.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public static function mkresavg($counts, $total, $question, $showtotals, $sort,
$table = new html_table();

$table->align = array('', '', 'center', 'right');
$table->width = '99%';
if ($isna) {
$table->head = array('', $stravg, '⇓', $isnahead);
} else {
Expand Down
4 changes: 3 additions & 1 deletion lang/en/questionnaire.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@
$string['autonumberpages'] = 'Auto number pages';
$string['autonumberpagesandquestions'] = 'Auto number pages and questions';
$string['average'] = 'Average';
$string['averagerank'] = 'Average rank';
$string['averageposition'] = 'Average position';
$string['averagerank'] = 'Average rank';
$string['averagesrow'] = 'Averages (where applicable):';
$string['bodytext'] = 'Body text';
$string['boxesnbexact'] = 'exactly {$a} box(es).';
$string['boxesnbmax'] = 'a maximum of {$a} box(es).';
Expand Down Expand Up @@ -251,6 +252,7 @@
$string['headingtext'] = 'Heading text';
$string['horizontal'] = 'Horizontal';
$string['id'] = 'ID';
$string['includerankaverages'] = 'Include rank question averages';
$string['includechoicecodes'] = 'Include choice codes';
$string['includechoicetext'] = 'Include choice text';
$string['includeincomplete'] = "Include incomplete responses";
Expand Down
45 changes: 44 additions & 1 deletion questionnaire.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3000,7 +3000,8 @@ protected function process_csv_row(array &$row,
/* {{{ proto array survey_generate_csv(int surveyid)
Exports the results of a survey to an array.
*/
public function generate_csv($rid='', $userid='', $choicecodes=1, $choicetext=0, $currentgroupid, $showincompletes = 0) {
public function generate_csv($rid='', $userid='', $choicecodes=1, $choicetext=0, $currentgroupid, $showincompletes=0,
$rankaverages=0) {
global $DB;

raise_memory_limit('1G');
Expand Down Expand Up @@ -3236,9 +3237,23 @@ public function generate_csv($rid='', $userid='', $choicecodes=1, $choicetext=0,
$formatoptions = new stdClass();
$formatoptions->filter = false; // To prevent any filtering in CSV output.

if ($rankaverages) {
$averages = [];
$rids = [];
$allresponsesrs2 = $this->get_survey_all_responses($rid, $userid, $currentgroupid);
foreach ($allresponsesrs2 as $responserow) {
if (!isset($rids[$responserow->rid])) {
$rids[$responserow->rid] = $responserow->rid;
}
}
}

// Get textual versions of responses, add them to output at the correct col position.
$prevresprow = false; // Previous response row.
$row = [];
if ($rankaverages) {
$averagerow = [];
}
foreach ($allresponsesrs as $responserow) {
$rid = $responserow->rid;
$qid = $responserow->question_id;
Expand All @@ -3250,6 +3265,16 @@ public function generate_csv($rid='', $userid='', $choicecodes=1, $choicetext=0,

$question = $this->questions[$qid];
$qtype = intval($question->type_id);
if ($rankaverages) {
if ($qtype === QUESRATE) {
if (empty($averages[$qid])) {
$results = $this->questions[$qid]->responsetype->get_results($rids);
foreach ($results as $qresult) {
$averages[$qid][$qresult->id] = $qresult->average;
}
}
}
}
$questionobj = $this->questions[$qid];

if ($prevresprow !== false && $prevresprow->rid !== $rid) {
Expand All @@ -3263,6 +3288,9 @@ public function generate_csv($rid='', $userid='', $choicecodes=1, $choicetext=0,
$position = $questionpositions[$key];
if ($qtype === QUESRATE) {
$choicetxt = $responserow->rankvalue;
if ($rankaverages) {
$averagerow[$position] = $averages[$qid][$responserow->choice_id];
}
} else {
$content = $choicesbyqid[$qid][$responserow->choice_id]->content;
if (\mod_questionnaire\question\choice\choice::content_is_other_choice($content)) {
Expand Down Expand Up @@ -3336,6 +3364,21 @@ public function generate_csv($rid='', $userid='', $choicecodes=1, $choicetext=0,
$nbinfocols, $numrespcols, $showincompletes);
}

// Add averages row if appropriate.
if ($rankaverages) {
$summaryrow = [];
$summaryrow[0] = get_string('averagesrow', 'questionnaire');
$i = 1;
for ($i = 1; $i < $nbinfocols; $i++) {
$summaryrow[$i] = '';
}
$pos = 0;
for ($i = $nbinfocols; $i < $numrespcols; $i++) {
$summaryrow[$i] = isset($averagerow[$i]) ? $averagerow[$i] : '';
}
$output[] = $summaryrow;
}

// Change table headers to incorporate actual question numbers.
$numquestion = 0;
$oldkey = 0;
Expand Down
4 changes: 3 additions & 1 deletion report.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,11 +482,13 @@
$choicecodes = optional_param('choicecodes', '0', PARAM_INT);
$choicetext = optional_param('choicetext', '0', PARAM_INT);
$showincompletes = optional_param('complete', '0', PARAM_INT);
$rankaverages = optional_param('rankaverages', '0', PARAM_INT);
$dataformat = optional_param('downloadformat', '', PARAM_ALPHA);
$emailroles = optional_param('emailroles', 0, PARAM_INT);
$emailextra = optional_param('emailextra', '', PARAM_RAW);

$output = $questionnaire->generate_csv('', $user, $choicecodes, $choicetext, $currentgroupid, $showincompletes);
$output = $questionnaire->generate_csv('', $user, $choicecodes, $choicetext, $currentgroupid, $showincompletes,
$rankaverages);

$columns = $output[0];
unset($output[0]);
Expand Down

0 comments on commit 5f53db3

Please sign in to comment.