Merge branch 'MDL-58945_401' of https://github.com/timhunt/moodle into MOODLE_401_STABLE

This commit is contained in:
Andrew Nicols 2023-03-01 10:55:38 +08:00
commit 576afc3ba6
10 changed files with 85 additions and 44 deletions

View File

@ -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';

View File

@ -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';
}

View File

@ -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');

View File

@ -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 {
'<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

View File

@ -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 = '&#160;';
}

View File

@ -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"

View File

@ -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';
}

View File

@ -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) {

View File

@ -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]);
}
/**

View File

@ -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>