diff --git a/classes/question/question.php b/classes/question/question.php index 0ae9937a..11a12370 100644 --- a/classes/question/question.php +++ b/classes/question/question.php @@ -46,6 +46,7 @@ define('QUESFILE', 12); define('QUESPAGEBREAK', 99); define('QUESSECTIONTEXT', 100); +define('QUESRATEUNANSWERED', -999); global $idcounter, $CFG; $idcounter = 0; diff --git a/classes/question/rate.php b/classes/question/rate.php index d9ab3ced..37f1302f 100644 --- a/classes/question/rate.php +++ b/classes/question/rate.php @@ -315,7 +315,8 @@ protected function question_survey_display($response, $descendantsdata, $blankqu $num = 0; foreach ($this->choices as $cid => $choice) { - $num += (isset($response->answers[$this->id][$cid]) && ($response->answers[$this->id][$cid]->value != -999)); + $num += (isset($response->answers[$this->id][$cid]) && + $response->answers[$this->id][$cid]->value != QUESRATEUNANSWERED); } $notcomplete = false; @@ -361,13 +362,13 @@ protected function question_survey_display($response, $descendantsdata, $blankqu $title = ''; if ( $notcomplete && isset($response->answers[$this->id][$cid]) && - ($response->answers[$this->id][$cid]->value == -999) + $response->answers[$this->id][$cid]->value == QUESRATEUNANSWERED ) { $completeclass = 'notcompleted'; $title = get_string('pleasecomplete', 'questionnaire'); } - // Set value of notanswered button to -999 in order to eliminate it from form submit later on. - $colinput = ['name' => $str, 'value' => -999]; + // Set value of notanswered button in order to eliminate it from form submit later on. + $colinput = ['name' => $str, 'value' => QUESRATEUNANSWERED]; if (!empty($checked)) { $colinput['checked'] = true; } @@ -624,8 +625,8 @@ public function response_complete($responsedata) { if (isset($answers[$cid]) && !empty($answers[$cid]) && ($answers[$cid]->value == $na)) { $answers[$cid]->value = -1; } - // If choice value == -999 this is a not yet answered choice. - $num += (isset($answers[$cid]) && ($answers[$cid]->value != -999)); + // Ignore if value means this is a not yet answered choice. + $num += (isset($answers[$cid]) && $answers[$cid]->value != QUESRATEUNANSWERED); } $nbchoices -= $nameddegrees; } @@ -675,8 +676,8 @@ public function response_valid($responsedata) { if (isset($answers[$cid]) && ($answers[$cid]->value == $na)) { $answers[$cid]->value = -1; } - // If choice value == -999 this is a not yet answered choice. - $num += (isset($answers[$cid]) && ($answers[$cid]->value != -999)); + // Ignore if value means this is a not yet answered choice. + $num += (isset($answers[$cid]) && $answers[$cid]->value != QUESRATEUNANSWERED); } $nbchoices -= $nameddegrees; } diff --git a/questionnaire.class.php b/questionnaire.class.php index d9cf9539..7fa32d36 100644 --- a/questionnaire.class.php +++ b/questionnaire.class.php @@ -2450,7 +2450,8 @@ private function get_full_submission_for_export($rid) { } else { $rating = $this->responses[$rid]->answers[$question->id][$cid]->value; } - $response->answers[] = $question->choices[$cid]->content . ' = ' . $rating; + $response->answers[] = $question->choices[$cid]->content . ' = ' . + ($rating != QUESRATEUNANSWERED ? $rating : ''); } } } @@ -3473,7 +3474,8 @@ protected function process_csv_row( for ($c = $nbinfocols; $c < $numrespcols; $c++) { if (isset($row[$c])) { - $positioned[] = $row[$c]; + // Ignore if value means this is a not yet answered choice. + $positioned[] = $row[$c] != QUESRATEUNANSWERED ? $row[$c] : null; } else if (isset($questionsbyposition[$c])) { $question = $questionsbyposition[$c]; $qtype = intval($question->type_id); diff --git a/tests/behat/behat_mod_questionnaire.php b/tests/behat/behat_mod_questionnaire.php index a0d42e20..15b4f502 100644 --- a/tests/behat/behat_mod_questionnaire.php +++ b/tests/behat/behat_mod_questionnaire.php @@ -448,11 +448,11 @@ private function add_response_data($qid, $sid) { ["", "6", "13", "18", "-1"], ["", "6", "13", "19", "1"], ["", "6", "13", "20", "-1"], - ["", "7", "13", "16", "-999"], - ["", "7", "13", "17", "-999"], - ["", "7", "13", "18", "-999"], - ["", "7", "13", "19", "-999"], - ["", "7", "13", "20", "-999"], + ["", "7", "13", "16", QUESRATEUNANSWERED], + ["", "7", "13", "17", QUESRATEUNANSWERED], + ["", "7", "13", "18", QUESRATEUNANSWERED], + ["", "7", "13", "19", QUESRATEUNANSWERED], + ["", "7", "13", "20", QUESRATEUNANSWERED], ]; $this->add_data( $responserank, diff --git a/tests/csvexport_test.php b/tests/csvexport_test.php index 714f3da9..8c81ec49 100644 --- a/tests/csvexport_test.php +++ b/tests/csvexport_test.php @@ -76,17 +76,19 @@ public function test_csvexport(): void { $questionnaireinst = new \questionnaire($course, $cm, 0, $questionnaire); // Test for only complete responses. + $expectedoutput = $this->expected_complete_output(); $newoutput = $this->get_csv_text($questionnaireinst->generate_csv(0, '', '', 0, 0, 0)); - $this->assertEquals(count($newoutput), count($this->expected_complete_output())); + $this->assertEquals(count($newoutput), count($expectedoutput)); foreach ($newoutput as $key => $output) { - $this->assertEquals($this->expected_complete_output()[$key], $output); + $this->assertEquals($expectedoutput[$key], $output, "Output #$key"); } // Test for all responses. + $expectedoutput = $this->expected_incomplete_output(); $newoutput = $this->get_csv_text($questionnaireinst->generate_csv(0, '', '', 0, 0, 1)); - $this->assertEquals(count($newoutput), count($this->expected_incomplete_output())); + $this->assertEquals(count($newoutput), count($expectedoutput)); foreach ($newoutput as $key => $output) { - $this->assertEquals($this->expected_incomplete_output()[$key], $output); + $this->assertEquals($expectedoutput[$key], $output, "Output #$key"); } } } @@ -243,6 +245,6 @@ private function expected_incomplete_output() { " Test course 1 Testy Lastname4 username4 y Test answer Some header textSome paragraph text 83 " . "27/12/2017 wind three 0 0 0 0 0 0 0 0 0 1 1 2 3 4 5 1 2 3 4 5", " Test course 1 Testy Lastname5 username5 n Test answer Some header textSome paragraph text 83 " . - "27/12/2017 wind three 0 0 0 0 0 0 0 0 0 1 1 2 3 4 5 1 2 3 4 5"]; + "27/12/2017 wind three 0 0 0 0 0 0 0 0 0 1 5"]; } } diff --git a/tests/generator/lib.php b/tests/generator/lib.php index d311f589..f1362aee 100644 --- a/tests/generator/lib.php +++ b/tests/generator/lib.php @@ -661,7 +661,8 @@ public function generate_response($questionnaire, $questions, $userid, $complete case QUESRATE: $answers = []; for ($a = 0; $a < count($choices) - 1; $a++) { - $answers[] = new question_response_rank($choices[$a], (($a % 5) + 1)); + $rank = $complete ? (($a % 5) + 1) : QUESRATEUNANSWERED; + $answers[] = new question_response_rank($choices[$a], $rank); } $responses[] = new question_response($question->id, $answers); break; diff --git a/tests/privacy_provider_test.php b/tests/privacy_provider_test.php index b4aa5884..73ef5c02 100644 --- a/tests/privacy_provider_test.php +++ b/tests/privacy_provider_test.php @@ -138,8 +138,8 @@ public function test_export_user_data(): void { $this->assertEquals('7. Numeric 1004', $data->responses[0]['questions'][7]->questionname); $this->assertEquals(83, $data->responses[0]['questions'][7]->answers[0]); $this->assertEquals('22. Rate Scale 1014', $data->responses[0]['questions'][22]->questionname); - $this->assertEquals('fourteen = 1', $data->responses[0]['questions'][22]->answers[0]); - $this->assertEquals('happy = 3', $data->responses[0]['questions'][22]->answers[7]); + $this->assertEquals('fourteen = ', $data->responses[0]['questions'][22]->answers[0]); + $this->assertEquals('happy = ', $data->responses[0]['questions'][22]->answers[7]); } /**