MDL-62358 Question: Improve number function

Support all 'question numbers' that might be needed
This commit is contained in:
Huong Nguyen
2018-06-14 19:43:55 +07:00
parent 9e7c397889
commit 628b4c86af
3 changed files with 41 additions and 6 deletions
+6 -6
View File
@@ -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'));
}
+19
View File
@@ -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'));
}
}