MDL-62358 Question: Improve number function
Support all 'question numbers' that might be needed
This commit is contained in:
@@ -156,15 +156,15 @@ class core_question_renderer extends plugin_renderer_base {
|
||||
* @return HTML fragment.
|
||||
*/
|
||||
protected function number($number) {
|
||||
if (trim($number) === '') {
|
||||
return '';
|
||||
}
|
||||
$numbertext = '';
|
||||
if (is_numeric($number)) {
|
||||
if (trim($number) === 'i') {
|
||||
$numbertext = get_string('information', 'question');
|
||||
} else {
|
||||
$numbertext = get_string('questionx', 'question',
|
||||
html_writer::tag('span', $number, array('class' => 'qno')));
|
||||
} else if ($number == 'i') {
|
||||
$numbertext = get_string('information', 'question');
|
||||
}
|
||||
if (!$numbertext) {
|
||||
return '';
|
||||
}
|
||||
return html_writer::tag('h3', $numbertext, array('class' => 'no'));
|
||||
}
|
||||
|
||||
@@ -1243,3 +1243,22 @@ class question_test_recordset extends moodle_recordset {
|
||||
$this->records = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper class for tests that help to test core_question_renderer.
|
||||
*
|
||||
* @copyright 2018 Huong Nguyen <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class testable_core_question_renderer extends core_question_renderer {
|
||||
|
||||
/**
|
||||
* Test the private number function.
|
||||
*
|
||||
* @param null|string $number
|
||||
* @return HTML
|
||||
*/
|
||||
public function number($number) {
|
||||
return parent::number($number);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,4 +131,20 @@ class question_engine_test extends advanced_testcase {
|
||||
public function test_is_manual_grade_in_range_ungraded() {
|
||||
$this->assertTrue(question_engine::is_manual_grade_in_range(1, 2));
|
||||
}
|
||||
|
||||
public function test_render_question_number() {
|
||||
global $PAGE;
|
||||
$renderer = new testable_core_question_renderer($PAGE, 'core_question');
|
||||
|
||||
// Test with number is i character.
|
||||
$this->assertEquals('<h3 class="no">Information</h3>', $renderer->number('i'));
|
||||
// Test with number is empty string.
|
||||
$this->assertEquals('', $renderer->number(''));
|
||||
// Test with number is 0.
|
||||
$this->assertEquals('<h3 class="no">Question <span class="qno">0</span></h3>', $renderer->number(0));
|
||||
// Test with number is numeric.
|
||||
$this->assertEquals('<h3 class="no">Question <span class="qno">1</span></h3>', $renderer->number(1));
|
||||
// Test with number is string.
|
||||
$this->assertEquals('<h3 class="no">Question <span class="qno">1 of 2</span></h3>', $renderer->number('1 of 2'));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user