Skip to content

Commit

Permalink
MDL-58945 qbank: showing q text should give option of plain or full
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed Feb 22, 2023
1 parent cb2fa7a commit a156684
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 44 deletions.
3 changes: 3 additions & 0 deletions lang/en/question.php
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion mod/quiz/classes/question/bank/custom_view.php
Expand Up @@ -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';
}

Expand Down
2 changes: 1 addition & 1 deletion mod/quiz/locallib.php
Expand Up @@ -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');
Expand Down
18 changes: 18 additions & 0 deletions mod/quiz/tests/locallib_test.php
Expand Up @@ -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';
Expand All @@ -115,6 +118,21 @@ public function test_quiz_question_tostring() {
'<span class="questiontext">What sort of INEQUALITY is x &lt; y[?]' . "\n" . '</span>', $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 = '<p>No emoticons here :-)</p>';
$question->questiontextformat = FORMAT_HTML;

$summary = quiz_question_tostring($question);
$this->assertEquals('<span class="questionname">The question name</span> ' .
'<span class="questiontext">No emoticons here :-)' . "\n</span>", $summary);
}

/**
* Test quiz_view
* @return void
Expand Down
28 changes: 19 additions & 9 deletions question/bank/viewquestiontext/classes/question_text_row.php
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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 = '&#160;';
}
Expand Down
Expand Up @@ -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 <span class="totestforhtml">first</span> 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"
2 changes: 1 addition & 1 deletion question/classes/local/bank/view.php
Expand Up @@ -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';
}

Expand Down
6 changes: 3 additions & 3 deletions question/editlib.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 6 additions & 3 deletions question/renderer.php
Expand Up @@ -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]);
}

/**
Expand Down
Expand Up @@ -15,19 +15,18 @@
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template core_question/showtext_checkbox
@template core_question/showtext_option
Example context (json):
{
"sortdata": [
{
"checked": "true/false"
}
]
"selected1": true
}
}}
<div>
<input type="hidden" name="qbshowtext" value="0" id="qbshowtext_off">
<input id="qbshowtext_on" class="searchoptions mr-1" type="checkbox" value="1" name="qbshowtext" {{#checked}} checked{{/checked}}>
<label for="qbshowtext_on">{{#str}} showquestiontext, question {{/str}}</label>
<label for="qbshowtext">{{#str}} showquestiontext, question {{/str}}</label>
<select id="qbshowtext" name="qbshowtext" class="searchoptions custom-select align-baseline">
<option value="0"{{#selected0}} selected{{/selected0}}>{{#str}} showquestiontext_off, question {{/str}}</option>
<option value="1"{{#selected1}} selected{{/selected1}}>{{#str}} showquestiontext_plain, question {{/str}}</option>
<option value="2"{{#selected2}} selected{{/selected2}}>{{#str}} showquestiontext_full, question {{/str}}</option>
</select>
</div>

0 comments on commit a156684

Please sign in to comment.