Skip to content

Commit

Permalink
MDL-69823 mod_quiz: Return question options via WS
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Nov 10, 2020
1 parent d330035 commit caddb8f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
8 changes: 7 additions & 1 deletion mod/quiz/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,7 @@ private static function question_structure() {
It will be returned only if the user is allowed to see it.', VALUE_OPTIONAL),
'maxmark' => new external_value(PARAM_FLOAT, 'the maximum mark possible for this question attempt.
It will be returned only if the user is allowed to see it.', VALUE_OPTIONAL),
'settings' => new external_value(PARAM_RAW, 'Question settings (JSON encoded).', VALUE_OPTIONAL),
),
'The question data. Some fields may not be returned depending on the quiz display settings.'
);
Expand All @@ -927,6 +928,7 @@ private static function get_attempt_questions_data(quiz_attempt $attemptobj, $re
foreach ($attemptobj->get_slots($page) as $slot) {
$qtype = $attemptobj->get_question_type_name($slot);
$qattempt = $attemptobj->get_question_attempt($slot);
$questiondef = $qattempt->get_question(true);

// Get response files (for questions like essay that allows attachments).
$responsefileareas = [];
Expand All @@ -948,6 +950,9 @@ private static function get_attempt_questions_data(quiz_attempt $attemptobj, $re
}
}

// Check display settings for question.
$settings = $questiondef->get_question_definition_for_external_rendering($qattempt, $displayoptions);

$question = array(
'slot' => $slot,
'type' => $qtype,
Expand All @@ -957,7 +962,8 @@ private static function get_attempt_questions_data(quiz_attempt $attemptobj, $re
'responsefileareas' => $responsefileareas,
'sequencecheck' => $qattempt->get_sequence_check_count(),
'lastactiontime' => $qattempt->get_last_step()->get_timecreated(),
'hasautosavedstep' => $qattempt->has_autosaved_step()
'hasautosavedstep' => $qattempt->has_autosaved_step(),
'settings' => !empty($settings) ? json_encode($settings) : null,
);

if ($attemptobj->is_real_question($slot)) {
Expand Down
5 changes: 5 additions & 0 deletions mod/quiz/tests/external_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,11 @@ public function test_get_attempt_summary() {
$this->assertEquals(false, $result['questions'][0]['hasautosavedstep']);
$this->assertEquals(false, $result['questions'][1]['hasautosavedstep']);

// Check question options.
$this->assertNotEmpty(5, $result['questions'][0]['settings']);
// Check at least some settings returned.
$this->assertCount(4, (array) json_decode($result['questions'][0]['settings']));

// Submit a response for the first question.
$tosubmit = array(1 => array('answer' => '3.14'));
$attemptobj->process_submitted_actions(time(), false, $tosubmit);
Expand Down
6 changes: 6 additions & 0 deletions mod/quiz/upgrade.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
This files describes API changes in the quiz code.

=== 3.10.1 ===

* External functions mod_quiz_external::get_attempt_data, mod_quiz_external::get_attempt_summary
and mod_quiz_external::get_attempt_review now return a new additional optional field:
- settings: Containing the question definition settings for displaying the question in an external system.

=== 3.10 ===

* External functions mod_quiz_external::get_attempt_data, mod_quiz_external::get_attempt_summary
Expand Down
25 changes: 25 additions & 0 deletions question/type/questionbase.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,31 @@ public function check_file_access($qa, $options, $component, $filearea, $args, $
return false;
}
}

/**
* Return the question settings that define this question as structured data.
*
* This is used by external systems such as the Moodle mobile app, which want to display the question themselves,
* rather than using the renderer provided.
*
* This method should only return the data that the student is allowed to see or know, given the current state of
* the question. For example, do not include the 'General feedback' until the student has completed the question,
* and even then, only include it if the question_display_options say it should be visible.
*
* But, within those rules, it is recommended that you return all the settings for the question,
* to give maximum flexibility to the external system providing its own rendering of the question.
*
* @param question_attempt $qa the current attempt for which we are exporting the settings.
* @param question_display_options $options the question display options which say which aspects of the question
* should be visible.
* @return mixed structure representing the question settings. In web services, this will be JSON-encoded.
*/
public function get_question_definition_for_external_rendering(question_attempt $qa, question_display_options $options) {

debugging('This question does not implement the get_question_definition_for_external_rendering() method yet.',
DEBUG_DEVELOPER);
return null;
}
}


Expand Down

0 comments on commit caddb8f

Please sign in to comment.