Skip to content

Commit

Permalink
quiz-data - Fill in defaults, match import and export
Browse files Browse the repository at this point in the history
  • Loading branch information
EJMFarrow committed Oct 17, 2024
1 parent e06ecf0 commit 132a509
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
17 changes: 8 additions & 9 deletions classes/external/export_quiz_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ public static function execute_returns():external_single_structure {
'name' => new external_value(PARAM_TEXT, 'context level description'),
'intro' => new external_value(PARAM_RAW, 'course category name (course category context)'),
'introformat' => new external_value(PARAM_SEQUENCE, 'id of course category, course or module'),
'preferredbehaviour' => new external_value(PARAM_TEXT, 'preferred behaviour'),
'grade' => new external_value(PARAM_SEQUENCE, 'maximum grade'),
'questionsperpage' => new external_value(PARAM_SEQUENCE, 'default questions per page'),
'shuffleanswers' => new external_value(PARAM_BOOL, 'shuffle answers if question allows?'),
'grade' => new external_value(PARAM_TEXT, 'maximum grade'),
'navmethod' => new external_value(PARAM_TEXT, 'navigation method'),
]),
'sections' => new external_multiple_structure(
Expand Down Expand Up @@ -121,14 +119,14 @@ public static function execute(?string $moduleid = null, ?string $quizname = nul
$response->quiz = new \stdClass();
$response->sections = [];
$response->questions = [];
$instanceid = (int) $contextinfo->moduleid;
$quizid = (int) $contextinfo->quizid;

$quiz = $DB->get_record('quiz', ['id' => $instanceid], 'intro, introformat');
$response->quiz->name = $contextinfo->modulename;
$response->quiz->intro = $quiz->intro;
$response->quiz->introformat = $quiz->introformat;
$quiz = $DB->get_record('quiz', ['id' => $quizid], 'intro, introformat, questionsperpage, grade, navmethod');
$quiz->name = $contextinfo->modulename;
$response->quiz = $quiz;

$response->sections = $DB->get_records('quiz_sections', ['quizid' => $quizid], null, 'firstslot, heading, shufflequestions');

$response->sections = $DB->get_records('quiz_sections', ['quizid' => $instanceid], null, 'firstslot, heading, shufflequestions');
$response->questions = $DB->get_records_sql("
SELECT qr.questionbankentryid, qs.slot, qs.page, qs.requireprevious, qs.maxmark
FROM {quiz_slots} qs
Expand All @@ -137,6 +135,7 @@ public static function execute(?string $moduleid = null, ?string $quizname = nul
AND qr.questionarea = 'slot'",
['contextid' => $contextinfo->context->id]);

$response->feedback = $DB->get_records('quiz_feedback', ['quizid' => $quizid], null, 'feedbacktext, feedbacktextformat, mingrade, maxgrade');
return $response;
}
}
29 changes: 22 additions & 7 deletions classes/external/import_quiz_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ public static function execute_parameters() {
'introformat' => new external_value(PARAM_SEQUENCE, 'id of course category, course or module'),
'coursename' => new external_value(PARAM_TEXT, 'course to import quiz into'),
'courseid' => new external_value(PARAM_SEQUENCE, 'course to import quiz into'),
'preferredbehaviour' => new external_value(PARAM_TEXT, 'preferred behaviour'),
'grade' => new external_value(PARAM_SEQUENCE, 'maximum grade'),
'questionsperpage' => new external_value(PARAM_SEQUENCE, 'default questions per page'),
'shuffleanswers' => new external_value(PARAM_BOOL, 'shuffle answers if question allows?'),
'grade' => new external_value(PARAM_SEQUENCE, 'maximum grade'),
'navmethod' => new external_value(PARAM_TEXT, 'navigation method'),
]),
'sections' => new external_multiple_structure(
Expand Down Expand Up @@ -133,12 +131,23 @@ public static function execute(array $quiz, array $sections, array $questions, a
$moduleinfo->quizpassword = '';
$moduleinfo->visible = true;
$moduleinfo->introeditor = ['text' => $params['quiz']['intro'], 'format' => $params['quiz']['introformat']];
$moduleinfo->preferredbehaviour = $params['quiz']['preferredbehaviour'];
$moduleinfo->preferredbehaviour = 'deferredfeedback';
$moduleinfo->grade = $params['quiz']['grade'];
$moduleinfo->questionsperpage = $params['quiz']['questionsperpage'];
$moduleinfo->shuffleanswers = $params['quiz']['shuffleanswers'];
$moduleinfo->shuffleanswers = true;
$moduleinfo->navmethod = $params['quiz']['navmethod'];
$moduleinfo = create_module($moduleinfo);
$reviewchoice = [];
$reviewchoice['reviewattempt'] = 69888;
$reviewchoice['reviewcorrectness'] = 4352;
$reviewchoice['reviewmarks'] = 4352;
$reviewchoice['reviewspecificfeedback'] = 4352;
$reviewchoice['reviewgeneralfeedback'] = 4352;
$reviewchoice['reviewrightanswer'] = 4352;
$reviewchoice['reviewoverallfeedback'] = 4352;
$reviewchoice['id'] = $moduleinfo->instance;

$DB->update_record('quiz', $reviewchoice);

foreach ($params['sections'] as $section) {
$section['quizid'] = $moduleinfo->instance;
Expand Down Expand Up @@ -176,14 +185,20 @@ public static function execute(array $quiz, array $sections, array $questions, a
quiz_add_quiz_question($qdata->questionid, $module, $params['question']['page'], $params['question']['maxmark']);
if ($question['requireprevious']) {
$quizcontext = get_context(\CONTEXT_MODULE, null, null, null, $moduleinfo->coursemodule);
$x = $quizcontext->context->id;
$itemid = $DB->get_field('question_references', 'itemid',
['usingcontextid' => $quizcontext->context->id, 'questionbankentryid' => $question['questionbankentryid']]);
$DB->set_field('quiz_slots', 'requireprevious', 1, ['id' => $itemid]);
}
}
quiz_update_sumgrades($module);

if ($params['feedback']) {
// Delete auto created feedback if we have something to replace it with.
$DB->delete_records('quiz_feedback', ['id' => $moduleinfo->instance]);
}
foreach ($params['feedback'] as $feedback) {
$feedback['quizid'] = $moduleinfo->instance;
$DB->insert_record('quiz_feedback', $feedback);
}
$response = new \stdClass();
$response->success = true;
return $response;
Expand Down

0 comments on commit 132a509

Please sign in to comment.