Skip to content

Commit adccda8

Browse files
committed
Merge branch 'dev' of https://github.com/maths/moodle-qtype_stack into code-tidy
2 parents eefa3d9 + 7ad0204 commit adccda8

File tree

8 files changed

+89
-22
lines changed

8 files changed

+89
-22
lines changed

.github/workflows/moodle-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88

99
services:
1010
postgres:
11-
image: postgres:13
11+
image: postgres:14
1212
env:
1313
POSTGRES_USER: 'postgres'
1414
POSTGRES_HOST_AUTH_METHOD: 'trust'

questionlibrary.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@
4040
require_login($cm->course, false, $cm);
4141
$thiscontext = context_module::instance($cmid);
4242
$urlparams['cmid'] = $cmid;
43-
$returntext = get_string('stack_library_quiz_return', 'qtype_stack');
43+
if (strpos(optional_param('returnurl', null, PARAM_LOCALURL), 'quiz') !== false) {
44+
$returntext = get_string('stack_library_quiz_return', 'qtype_stack');
45+
} else {
46+
$returntext = get_string('stack_library_qb_return', 'qtype_stack');
47+
}
4448
} else if ($courseid = optional_param('courseid', 0, PARAM_INT)) {
4549
require_login($courseid);
4650
$thiscontext = context_course::instance($courseid);

stack/input/dropdown/dropdown.class.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ public function adapt_to_model_answer($teacheranswer) {
254254
// In case we see CASText2 values we need to postproc them.
255255
$tmp = castext2_parser_utils::string_to_list($display, true);
256256
$tmp = castext2_parser_utils::unpack_maxima_strings($tmp);
257-
$ddlvalues[$key]['display'] = castext2_parser_utils::postprocess_parsed($tmp);
257+
$holder = new castext2_placeholder_holder();
258+
$ddlvalues[$key]['display'] = castext2_parser_utils::postprocess_parsed($tmp, null, $holder);
259+
$ddlvalues[$key]['display'] = $holder->replace($ddlvalues[$key]['display']);
258260
} else {
259261
$cs = stack_ast_container::make_from_teacher_source($display);
260262
if ($cs->get_valid()) {
@@ -264,8 +266,10 @@ public function adapt_to_model_answer($teacheranswer) {
264266
}
265267
if ($ddlvalues[$key]['correct']) {
266268
if (substr($display, 0, 9) === '["%root",') {
267-
$tmp = castext2_parser_utils::unpack_maxima_strings($display);
268-
$correctanswerdisplay[] = castext2_parser_utils::postprocess_parsed($tmp);
269+
$tmp = castext2_parser_utils::string_to_list($display, true);
270+
$tmp = castext2_parser_utils::unpack_maxima_strings($tmp);
271+
$holder = new castext2_placeholder_holder();
272+
$correctanswerdisplay[] = castext2_parser_utils::postprocess_parsed($tmp, null, $holder);
269273
} else {
270274
$correctanswerdisplay[] = $display;
271275
}
@@ -336,7 +340,10 @@ public function adapt_to_model_answer($teacheranswer) {
336340
// And now we need to care about holders.
337341
$holder = new castext2_placeholder_holder();
338342
$ddlvalues[$key]['display'] = castext2_parser_utils::postprocess_mp_parsed(
339-
$at1->get_by_key('val'.$key)->get_evaluated(), $holder);
343+
$at1->get_by_key('val'.$key)->get_evaluated(),
344+
null,
345+
$holder,
346+
);
340347
$ddlvalues[$key]['display'] = $holder->replace($ddlvalues[$key]['display']);
341348
} else if (substr($display, 0, 1) == '"') {
342349
$ddlvalues[$key]['display'] = stack_utils::maxima_string_to_php_string($display);

tests/behat/library.feature

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ Feature: Test STACK library
6464
And I should see "CR-Diff-02-linearity-1.b"
6565

6666
@javascript
67-
Scenario: Import a question starting from quiz in Moodle 4.3.
67+
Scenario: Import a question starting from quiz in 4.3 <= Moodle <= 4.5.
6868
Given the site is running Moodle version 4.3 or higher
69+
Given the site is running Moodle version 4.5 or lower
6970
When I am on the "Quiz 1" "mod_quiz > Edit" page logged in as "teacher"
7071
When I open the "last" add to quiz menu
7172
And I follow "a new question"
@@ -86,3 +87,25 @@ Feature: Test STACK library
8687
And I click on "ul[id^=form_autocomplete_suggestions] li:nth-child(3)" "css_element"
8788
And I click on "Apply filters" "button"
8889
And I should see "CR-Diff-02-linearity-1.b"
90+
91+
@javascript
92+
Scenario: Import a question starting from quiz in Moodle ≥ 5.0.
93+
Given the site is running Moodle version 5.0 or higher
94+
When I am on the "Quiz 1" "mod_quiz > Edit" page logged in as "teacher"
95+
When I open the "last" add to quiz menu
96+
And I follow "a new question"
97+
And I set the field "item_qtype_stack" to "1"
98+
And I press "submitbutton"
99+
And I click on "STACK question library" "link"
100+
Then I should see "Default for Quiz 1"
101+
And I should not see "Question variables"
102+
And I click on "Calculus-Refresher" "button"
103+
And I click on "CR_Diff_02" "button"
104+
And I click on "CR-Diff-02-linearity-1-b.xml" "button"
105+
And I should see "Differentiate \[{@p@}\] with respect to {@v@}. [[input:ans1]]"
106+
And I click on "Import" "button"
107+
And I click on "Return to quiz" "link"
108+
And I open the "last" add to quiz menu
109+
And I follow "from question bank"
110+
And I should see "Default for Quiz 1 (1)"
111+
And I should see "CR-Diff-02-linearity-1.b"

tests/editform_test.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,26 @@
3838
final class editform_test_class extends \qtype_stack_edit_form {
3939

4040
// phpcs:ignore moodle.Commenting.MissingDocblock.MissingTestcaseMethodDescription
41-
public function __construct($questiontext, $specificfeedback) {
41+
public function __construct($questiontext, $specificfeedback, $quizmoduleid) {
4242
global $USER;
43-
$syscontext = \context_system::instance();
44-
$category = question_make_default_categories([$syscontext]);
43+
// ISS1325 - Use quiz context rather than system context as
44+
// question categories only allowed in modules from Moodle 5.
45+
$quizcontext = \context_module::instance($quizmoduleid);
46+
if (function_exists('question_get_default_category')) {
47+
// This function exists from Moodle 4.5 onwards but the second parameter
48+
// which creates the category if it doesn't exist is 5.0 onwards.
49+
$category = question_get_default_category($quizcontext->id, true);
50+
if (!$category) {
51+
$category = $category = question_make_default_categories([$quizcontext]);
52+
}
53+
} else {
54+
// Deprecated from 5.0.
55+
$category = $category = question_make_default_categories([$quizcontext]);
56+
}
4557
$fakequestion = new \stdClass();
4658
$fakequestion->qtype = 'stack';
4759
$fakequestion->category = $category->id;
48-
$fakequestion->contextid = $syscontext->id;
60+
$fakequestion->contextid = $quizcontext->id;
4961
$fakequestion->createdby = $USER->id;
5062
$fakequestion->modifiedby = $USER->id;
5163
$fakequestion->questiontext = $questiontext;
@@ -57,9 +69,9 @@ public function __construct($questiontext, $specificfeedback) {
5769
$fakequestion->inputs = null;
5870
// Support both Moodle 4.x and 3.x.
5971
if (class_exists('\core_question\local\bank\question_edit_contexts')) {
60-
$contexts = new \core_question\local\bank\question_edit_contexts($syscontext);
72+
$contexts = new \core_question\local\bank\question_edit_contexts($quizcontext);
6173
} else {
62-
$contexts = new \question_edit_contexts($syscontext);
74+
$contexts = new \question_edit_contexts($quizcontext);
6375
}
6476
parent::__construct(new \moodle_url('/'), $fakequestion, $category, $contexts);
6577
}
@@ -81,8 +93,12 @@ final class editform_test extends \advanced_testcase {
8193
protected function get_form($questiontext, $specificfeedback) {
8294
$this->setAdminUser();
8395
$this->resetAfterTest();
84-
85-
return new editform_test_class($questiontext, $specificfeedback);
96+
$quizgenerator = self::getDataGenerator()->get_plugin_generator('mod_quiz');
97+
$site = get_site();
98+
// Add a quiz to the site course.
99+
$quiz = $quizgenerator->create_instance(['course' => $site->id, 'grade' => 100.0, 'sumgrades' => 2, 'layout' => '1,0']);
100+
$quizmoduleid = $quiz->cmid;
101+
return new editform_test_class($questiontext, $specificfeedback, $quizmoduleid);
86102
}
87103

88104
public function test_get_input_names_from_question_text_default(): void {

tests/input_dropdown_test.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,4 +428,21 @@ public function test_decimals(): void {
428428
$expected = 'A correct answer is: <code>3,1415</code>';
429429
$this->assertEquals($expected, $el->get_teacher_answer_display(false, false));
430430
}
431+
432+
public function test_validate_student_response_castext() {
433+
$options = new stack_options();
434+
$ta = '[[1+x^2,false],[1-x^2,false],' .
435+
'["1+x^3",true,["%root","Hello world ",["smlt","\\({x^3}\\)"]]]]';
436+
$el = stack_input_factory::make('dropdown', 'ans1', $ta, $options, ['options' => '']);
437+
$el->adapt_to_model_answer($ta);
438+
$state = $el->validate_student_response(['ans1' => '2'], $options, '2', new stack_cas_security());
439+
$this->assertEquals(stack_input::SCORE, $state->status);
440+
441+
$ta = '[[1+x^2,true],[1-x^2,false],' .
442+
'["1+x^3",false,["%root","Hello world ",["smlt","\\({x^3}\\)"]]]]';
443+
$el = stack_input_factory::make('dropdown', 'ans1', $ta, $options, ['options' => '']);
444+
$el->adapt_to_model_answer($ta);
445+
$state = $el->validate_student_response(['ans1' => '2'], $options, '2', new stack_cas_security());
446+
$this->assertEquals(stack_input::SCORE, $state->status);
447+
}
431448
}

tests/library_import_test.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ public function test_not_logged_in(): void {
105105
}
106106

107107
/**
108-
* Test the library_import function fails when no webservice export capability assigned.
108+
* Test the library_import function fails when no capability to add questions assigned.
109109
*/
110-
public function test_no_webservice_access(): void {
110+
public function test_no_access(): void {
111111
global $DB;
112112
$context = context_course::instance($this->course->id);
113-
$studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student']);
114-
role_assign($studentroleid, $this->user->id, $context->id);
113+
$teacherroleid = $DB->get_field('role', 'id', ['shortname' => 'teacher']);
114+
role_assign($teacherroleid, $this->user->id, $context->id);
115115
$this->getDataGenerator()->enrol_user($this->user->id, $this->course->id);
116116
$this->expectException(required_capability_exception::class);
117117
$this->expectExceptionMessage('you do not currently have permissions to do that (Add new questions).');

tests/library_render_test.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ public function test_not_logged_in(): void {
108108
}
109109

110110
/**
111-
* Test the library_render function fails when no webservice export capability assigned.
111+
* Test the library_render function fails when no capability assigned.
112112
*/
113113
public function test_no_webservice_access(): void {
114114
global $DB;
115115
$context = context_course::instance($this->course->id);
116-
$studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student']);
117-
role_assign($studentroleid, $this->user->id, $context->id);
116+
$teacherroleid = $DB->get_field('role', 'id', ['shortname' => 'teacher']);
117+
role_assign($teacherroleid, $this->user->id, $context->id);
118118
$this->getDataGenerator()->enrol_user($this->user->id, $this->course->id);
119119
$this->expectException(required_capability_exception::class);
120120
$this->expectExceptionMessage('you do not currently have permissions to do that (Add new questions).');

0 commit comments

Comments
 (0)