From a15668409e50a68ac61525df69960d420bb80541 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Mon, 20 Feb 2023 22:13:12 +0000 Subject: [PATCH] MDL-58945 qbank: showing q text should give option of plain or full --- lang/en/question.php | 3 ++ .../classes/question/bank/custom_view.php | 2 +- mod/quiz/locallib.php | 2 +- mod/quiz/tests/locallib_test.php | 18 ++++++++ .../classes/question_text_row.php | 28 +++++++++---- .../behat/view_question_name_column.feature | 42 +++++++++++-------- question/classes/local/bank/view.php | 2 +- question/editlib.php | 6 +-- question/renderer.php | 9 ++-- ...kbox.mustache => showtext_option.mustache} | 17 ++++---- 10 files changed, 85 insertions(+), 44 deletions(-) rename question/templates/{showtext_checkbox.mustache => showtext_option.mustache} (54%) diff --git a/lang/en/question.php b/lang/en/question.php index 80267315f9d76..92a2bf69e4aa8 100644 --- a/lang/en/question.php +++ b/lang/en/question.php @@ -460,6 +460,9 @@ $string['showmarkandmax'] = 'Show mark and max'; $string['showmaxmarkonly'] = 'Show max mark only'; $string['showquestiontext'] = 'Show question text in the question list'; +$string['showquestiontext_full'] = 'Full display'; +$string['showquestiontext_off'] = 'No'; +$string['showquestiontext_plain'] = 'Text only'; $string['shown'] = 'Shown'; $string['shownumpartscorrect'] = 'Show the number of correct responses'; $string['shownumpartscorrectwhenfinished'] = 'Show the number of correct responses once the question has finished'; diff --git a/mod/quiz/classes/question/bank/custom_view.php b/mod/quiz/classes/question/bank/custom_view.php index 7686d3defa53c..6421318f6ceb1 100644 --- a/mod/quiz/classes/question/bank/custom_view.php +++ b/mod/quiz/classes/question/bank/custom_view.php @@ -69,7 +69,7 @@ protected function get_question_bank_plugins(): array { 'preview_action_column' ]; - if (question_get_display_preference('qbshowtext', 0, PARAM_BOOL, new \moodle_url(''))) { + if (question_get_display_preference('qbshowtext', 0, PARAM_INT, new \moodle_url(''))) { $corequestionbankcolumns[] = 'question_text_row'; } diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index b3f6de1720a67..0276937d59c7a 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -2243,7 +2243,7 @@ function quiz_question_tostring($question, $showicon = false, $showquestiontext // Question text. if ($showquestiontext) { $questiontext = question_utils::to_plain_text($question->questiontext, - $question->questiontextformat, array('noclean' => true, 'para' => false)); + $question->questiontextformat, ['noclean' => true, 'para' => false, 'filter' => false]); $questiontext = shorten_text($questiontext, 50); if ($questiontext) { $result .= ' ' . html_writer::span(s($questiontext), 'questiontext'); diff --git a/mod/quiz/tests/locallib_test.php b/mod/quiz/tests/locallib_test.php index ae6f4d1d1fee7..06e1b5b06e72c 100644 --- a/mod/quiz/tests/locallib_test.php +++ b/mod/quiz/tests/locallib_test.php @@ -103,6 +103,9 @@ public function test_quiz_attempt_state($attemptstate, $this->assertEquals($expectedstate, quiz_attempt_state($quiz, $attempt)); } + /** + * @covers ::quiz_question_tostring + */ public function test_quiz_question_tostring() { $question = new \stdClass(); $question->qtype = 'multichoice'; @@ -115,6 +118,21 @@ public function test_quiz_question_tostring() { 'What sort of INEQUALITY is x < y[?]' . "\n" . '', $summary); } + /** + * @covers ::quiz_question_tostring + */ + public function test_quiz_question_tostring_does_not_filter() { + $question = new \stdClass(); + $question->qtype = 'multichoice'; + $question->name = 'The question name'; + $question->questiontext = '

No emoticons here :-)

'; + $question->questiontextformat = FORMAT_HTML; + + $summary = quiz_question_tostring($question); + $this->assertEquals('The question name ' . + 'No emoticons here :-)' . "\n", $summary); + } + /** * Test quiz_view * @return void diff --git a/question/bank/viewquestiontext/classes/question_text_row.php b/question/bank/viewquestiontext/classes/question_text_row.php index 6078312316734..dfb3d3137c62b 100644 --- a/question/bank/viewquestiontext/classes/question_text_row.php +++ b/question/bank/viewquestiontext/classes/question_text_row.php @@ -17,6 +17,7 @@ namespace qbank_viewquestiontext; use core_question\local\bank\row_base; +use question_utils; /** * A column type for the name of the question name. @@ -28,13 +29,17 @@ */ class question_text_row extends row_base { - /** - * To initialise subclasses - * @var $formatoptions - */ + /** @var bool if true, we will show the question text reduced to plain text, else it is fully rendered. */ + protected $plain; + + /** @var \stdClass $formatoptions options used when displaying the question text as HTML. */ protected $formatoptions; protected function init(): void { + + // Cannot use $this->get_preference because of PHP type hints. + $preference = question_get_display_preference($this->get_preference_key(), 0, PARAM_INT, new \moodle_url('')); + $this->plain = 1 === (int) $preference; $this->formatoptions = new \stdClass(); $this->formatoptions->noclean = true; $this->formatoptions->para = false; @@ -49,11 +54,16 @@ public function get_title(): string { } protected function display_content($question, $rowclasses): void { - $text = question_rewrite_question_preview_urls($question->questiontext, $question->id, - $question->contextid, 'question', 'questiontext', $question->id, - $question->contextid, 'core_question'); - $text = format_text($text, $question->questiontextformat, - $this->formatoptions); + if ($this->plain) { + $text = question_utils::to_plain_text($question->questiontext, + $question->questiontextformat, ['noclean' => true, 'para' => false, 'filter' => false]); + } else { + $text = question_rewrite_question_preview_urls($question->questiontext, $question->id, + $question->contextid, 'question', 'questiontext', $question->id, + $question->contextid, 'core_question'); + $text = format_text($text, $question->questiontextformat, + $this->formatoptions); + } if ($text == '') { $text = ' '; } diff --git a/question/bank/viewquestiontext/tests/behat/view_question_name_column.feature b/question/bank/viewquestiontext/tests/behat/view_question_name_column.feature index 2c27ffc1d980b..b8b2ec6ad37af 100644 --- a/question/bank/viewquestiontext/tests/behat/view_question_name_column.feature +++ b/question/bank/viewquestiontext/tests/behat/view_question_name_column.feature @@ -11,23 +11,31 @@ Feature: Use the qbank plugin manager page for viewquestiontext | quiz | Test quiz | C1 | quiz1 | And the following "question categories" exist: | contextlevel | reference | name | - | Course | C1 | Test questions | + | Course | C1 | Test questions | And the following "questions" exist: - | questioncategory | qtype | name | questiontext | - | Test questions | truefalse | First question | Answer the first question | + | questioncategory | qtype | name | questiontext | + | Test questions | truefalse | First question | Answer the first question | @javascript - Scenario: Enable/disable viewquestiontext column from the base view - Given I log in as "admin" - And I navigate to "Plugins > Question bank plugins > Manage question bank plugins" in site administration - And I should see "View question text" - When I click on "Disable" "link" in the "View question text" "table_row" - And I am on the "Test quiz" "mod_quiz > question bank" page - And I should not see "Show question text in the question list" - Then "#categoryquestions .questiontext" "css_element" should not be visible - And I navigate to "Plugins > Question bank plugins > Manage question bank plugins" in site administration - And I click on "Enable" "link" in the "View question text" "table_row" - And I am on the "Test quiz" "mod_quiz > question bank" page - And I should see "Show question text in the question list" - And I click on "qbshowtext" "checkbox" - And I should see "Answer the first question" + Scenario: Display of plain question text can be turned on and off + When I am on the "Test quiz" "mod_quiz > question bank" page logged in as admin + And I set the field "Show question text in the question list" to "Text only" + Then I should see "Answer the first question" + And ".totestforhtml" "css_element" should not exist in the "Answer the first question" "table_row" + And I set the field "Show question text in the question list" to "No" + And I should not see "Answer the first question" + + @javascript + Scenario: Display of full question text can be turned on and off + When I am on the "Test quiz" "mod_quiz > question bank" page logged in as admin + And I set the field "Show question text in the question list" to "Full display" + Then I should see "Answer the first question" + And ".totestforhtml" "css_element" should exist in the "Answer the first question" "table_row" + And I set the field "Show question text in the question list" to "No" + And I should not see "Answer the first question" + + Scenario: Option does not show if the plugin is disabled + Given the following config values are set as admin: + | disabled | 1 | qbank_viewquestiontext | + When I am on the "Test quiz" "mod_quiz > question bank" page logged in as admin + Then I should not see "Show question text in the question list" diff --git a/question/classes/local/bank/view.php b/question/classes/local/bank/view.php index a3c8edbdb6724..a11d2f6adaac1 100644 --- a/question/classes/local/bank/view.php +++ b/question/classes/local/bank/view.php @@ -257,7 +257,7 @@ protected function get_question_bank_plugins(): array { 'creator_name_column', 'comment_count_column' ]; - if (question_get_display_preference('qbshowtext', 0, PARAM_BOOL, new \moodle_url(''))) { + if (question_get_display_preference('qbshowtext', 0, PARAM_INT, new \moodle_url(''))) { $corequestionbankcolumns[] = 'question_text_row'; } diff --git a/question/editlib.php b/question/editlib.php index 554be2ceae4c2..4ef1bb9c4d0c2 100644 --- a/question/editlib.php +++ b/question/editlib.php @@ -209,7 +209,7 @@ function question_edit_setup($edittab, $baseurl, $requirecmid = false, $unused = // Display options. $params['recurse'] = optional_param('recurse', null, PARAM_BOOL); $params['showhidden'] = optional_param('showhidden', null, PARAM_BOOL); - $params['qbshowtext'] = optional_param('qbshowtext', null, PARAM_BOOL); + $params['qbshowtext'] = optional_param('qbshowtext', null, PARAM_INT); // Category list page. $params['cpage'] = optional_param('cpage', null, PARAM_INT); $params['qtagids'] = optional_param_array('qtagids', null, PARAM_INT); @@ -241,7 +241,7 @@ function question_edit_setup($edittab, $baseurl, $requirecmid = false, $unused = * 'cpage' => PARAM_INT, * 'recurse' => PARAM_BOOL, * 'showhidden' => PARAM_BOOL, - * 'qbshowtext' => PARAM_BOOL, + * 'qbshowtext' => PARAM_INT, * 'qtagids' => [PARAM_INT], (array of integers) * 'qbs1' => PARAM_TEXT, * 'qbs2' => PARAM_TEXT, @@ -277,7 +277,7 @@ function question_build_edit_resources($edittab, $baseurl, $params) { 'cpage' => PARAM_INT, 'recurse' => PARAM_BOOL, 'showhidden' => PARAM_BOOL, - 'qbshowtext' => PARAM_BOOL + 'qbshowtext' => PARAM_INT, ]; foreach ($paramtypes as $name => $type) { diff --git a/question/renderer.php b/question/renderer.php index 83cfc413e5203..f69708c3f14a2 100644 --- a/question/renderer.php +++ b/question/renderer.php @@ -147,13 +147,16 @@ public function render_question_pagination($displaydata) { } /** - * Render question showtext checkbox. + * Render the showtext option. + * + * It's not a checkbox any more! [Name your API after the purpose, not the implementation!] * * @param array $displaydata - * @return bool|string + * @return string */ public function render_showtext_checkbox($displaydata) { - return $this->render_from_template('core_question/showtext_checkbox', $displaydata); + return $this->render_from_template('core_question/showtext_option', + ['selected' . $displaydata['checked'] => true]); } /** diff --git a/question/templates/showtext_checkbox.mustache b/question/templates/showtext_option.mustache similarity index 54% rename from question/templates/showtext_checkbox.mustache rename to question/templates/showtext_option.mustache index 0245357ef6d57..56e3bdacb15e8 100644 --- a/question/templates/showtext_checkbox.mustache +++ b/question/templates/showtext_option.mustache @@ -15,19 +15,18 @@ along with Moodle. If not, see . }} {{! - @template core_question/showtext_checkbox + @template core_question/showtext_option Example context (json): { - "sortdata": [ - { - "checked": "true/false" - } - ] + "selected1": true } }}
- - - + +