diff --git a/lang/en/question.php b/lang/en/question.php index 34eef1f10c7..8bfcf052f4f 100644 --- a/lang/en/question.php +++ b/lang/en/question.php @@ -458,6 +458,9 @@ $string['showhidden'] = 'Also show old questions'; $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 0df54d72b81..8dcb5726496 100644 --- a/mod/quiz/classes/question/bank/custom_view.php +++ b/mod/quiz/classes/question/bank/custom_view.php @@ -72,7 +72,7 @@ class custom_view extends \core_question\local\bank\view { '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 087492ab0fa..4c5b859e466 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 ae6f4d1d1fe..06e1b5b06e7 100644 --- a/mod/quiz/tests/locallib_test.php +++ b/mod/quiz/tests/locallib_test.php @@ -103,6 +103,9 @@ class locallib_test extends \advanced_testcase { $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 @@ class locallib_test extends \advanced_testcase { '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 60783123167..dfb3d3137c6 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 @@ use core_question\local\bank\row_base; */ 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 @@ class question_text_row extends row_base { } 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 2c27ffc1d98..b8b2ec6ad37 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 08b2df0814a..d8f76ff04e4 100644 --- a/question/classes/local/bank/view.php +++ b/question/classes/local/bank/view.php @@ -266,7 +266,7 @@ class view { '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 1bc9a64bff8..1d24c66f8b6 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, @@ -279,7 +279,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 83cfc413e52..f69708c3f14 100644 --- a/question/renderer.php +++ b/question/renderer.php @@ -147,13 +147,16 @@ class core_question_bank_renderer extends plugin_renderer_base { } /** - * 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 0245357ef6d..56e3bdacb15 100644 --- a/question/templates/showtext_checkbox.mustache +++ b/question/templates/showtext_option.mustache @@ -15,19 +15,18 @@ along with Moodle. If not, see