first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+173
View File
@@ -0,0 +1,173 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_quiz\external;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/externallib.php');
require_once($CFG->dirroot . '/question/editlib.php');
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
use external_function_parameters;
use external_single_structure;
use external_value;
use external_api;
use mod_quiz\question\bank\filter\custom_category_condition;
use mod_quiz\quiz_settings;
use mod_quiz\structure;
/**
* Add random questions to a quiz.
*
* @package mod_quiz
* @copyright 2022 Catalyst IT Australia Pty Ltd
* @author Nathan Nguyen <nathannguyen@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class add_random_questions extends external_api {
/**
* Parameters for the web service function
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters (
[
'cmid' => new external_value(PARAM_INT, 'The cmid of the quiz'),
'addonpage' => new external_value(PARAM_INT, 'The page where random questions will be added to'),
'randomcount' => new external_value(PARAM_INT, 'Number of random questions'),
'filtercondition' => new external_value(
PARAM_TEXT,
'(Optional) The filter condition used when adding random questions from an existing category.
Not required if adding random questions from a new category.',
VALUE_DEFAULT,
'',
),
'newcategory' => new external_value(
PARAM_TEXT,
'(Optional) The name of a new question category to create and use for the random questions.',
VALUE_DEFAULT,
'',
),
'parentcategory' => new external_value(
PARAM_TEXT,
'(Optional) The parent of the new question category, if creating one.',
VALUE_DEFAULT,
0,
),
]
);
}
/**
* Add random questions.
*
* @param int $cmid The cmid of the quiz
* @param int $addonpage The page where random questions will be added to
* @param int $randomcount Number of random questions
* @param string $filtercondition Filter condition
* @param string $newcategory add new category
* @param string $parentcategory parent category of new category
* @return array result
*/
public static function execute(
int $cmid,
int $addonpage,
int $randomcount,
string $filtercondition = '',
string $newcategory = '',
string $parentcategory = '',
): array {
[
'cmid' => $cmid,
'addonpage' => $addonpage,
'randomcount' => $randomcount,
'filtercondition' => $filtercondition,
'newcategory' => $newcategory,
'parentcategory' => $parentcategory,
] = self::validate_parameters(self::execute_parameters(), [
'cmid' => $cmid,
'addonpage' => $addonpage,
'randomcount' => $randomcount,
'filtercondition' => $filtercondition,
'newcategory' => $newcategory,
'parentcategory' => $parentcategory,
]);
// Validate context.
$thiscontext = \context_module::instance($cmid);
self::validate_context($thiscontext);
require_capability('mod/quiz:manage', $thiscontext);
// If filtercondition is not empty, decode it. Otherwise, set it to empty array.
$filtercondition = !empty($filtercondition) ? json_decode($filtercondition, true) : [];
// Create new category.
if (!empty($newcategory)) {
$contexts = new \core_question\local\bank\question_edit_contexts($thiscontext);
$defaultcategoryobj = question_make_default_categories($contexts->all());
$defaultcategory = $defaultcategoryobj->id . ',' . $defaultcategoryobj->contextid;
$qcobject = new \qbank_managecategories\question_category_object(
null,
new \moodle_url('/'),
$contexts->having_one_edit_tab_cap('categories'),
$defaultcategoryobj->id,
$defaultcategory,
null,
$contexts->having_cap('moodle/question:add'));
$categoryid = $qcobject->add_category($parentcategory, $newcategory, '', true);
$filter = [
'category' => [
'jointype' => custom_category_condition::JOINTYPE_DEFAULT,
'values' => [$categoryid],
'filteroptions' => ['includesubcategories' => false],
]
];
// Generate default filter condition for the random question to be added in the new category.
$filtercondition = [
'qpage' => 0,
'cat' => "{$categoryid},{$thiscontext->id}",
'qperpage' => DEFAULT_QUESTIONS_PER_PAGE,
'tabname' => 'questions',
'sortdata' => [],
'filter' => $filter,
];
}
// Add random question to the quiz.
[$quiz, ] = get_module_from_cmid($cmid);
$settings = quiz_settings::create_for_cmid($cmid);
$structure = structure::create_for_quiz($settings);
$structure->add_random_questions($addonpage, $randomcount, $filtercondition);
quiz_delete_previews($quiz);
quiz_settings::create($quiz->id)->get_grade_calculator()->recompute_quiz_sumgrades();
return ['message' => get_string('addarandomquestion_success', 'mod_quiz')];
}
/**
* Returns description of method result value.
*
* @return external_value
*/
public static function execute_returns() {
return new external_single_structure([
'message' => new external_value(PARAM_TEXT, 'Message', VALUE_OPTIONAL)
]);
}
}
@@ -0,0 +1,122 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_quiz\external;
use coding_exception;
use core_external\external_api;
use core_external\external_description;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use mod_quiz\quiz_attempt;
use mod_quiz\quiz_settings;
use moodle_exception;
use stdClass;
/**
* For a quiz with no grade items yet, create a grade item for each section.
*
* And, assign the questions in each section to the corresponding grade item.
*
* The user must have the 'mod/quiz:manage' capability for the quiz.
*
* @package mod_quiz
* @copyright 2024 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class create_grade_item_per_section extends external_api {
/**
* Declare the method parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'quizid' => new external_value(PARAM_INT, 'The quiz to update slots for.'),
]);
}
/**
* For a quiz with no grade items yet, create a grade item for each section.
*
* And, assign the questions in each section to the corresponding grade item.
*
* The user must have the 'mod/quiz:manage' capability for the quiz.
*
* @param int $quizid the id of the quiz to setup grade items for.
*/
public static function execute(int $quizid): void {
global $DB;
[
'quizid' => $quizid,
] = self::validate_parameters(self::execute_parameters(), [
'quizid' => $quizid,
]);
// Check the request is valid.
$quizobj = quiz_settings::create($quizid);
require_capability('mod/quiz:manage', $quizobj->get_context());
self::validate_context($quizobj->get_context());
$structure = $quizobj->get_structure();
if ($structure->get_grade_items()) {
throw new coding_exception('Cannot use create_grade_item_per_section for a quiz ' .
'that already has grade items.');
}
$transaction = $DB->start_delegated_transaction();
$gradeitemsids = [];
foreach ($structure->get_sections() as $section) {
// Only create a grade item for sections that contain at least one real question (not description).
$hasrealquestion = false;
foreach ($structure->get_slots_in_section($section->id) as $slot) {
$hasrealquestion = $hasrealquestion || $structure->is_real_question($slot);
}
if (!$hasrealquestion) {
continue;
}
// Grade item required. Create it.
$gradeitem = new stdClass();
$gradeitem->quizid = $quizid;
$gradeitem->name = $section->heading;
$structure->create_grade_item($gradeitem);
$gradeitemsids[$section->id] = $gradeitem->id;
}
foreach ($structure->get_slots() as $slot) {
if ($structure->is_real_question($slot->slot)) {
$structure->update_slot_grade_item($slot, $gradeitemsids[$slot->section->id]);
}
}
$transaction->allow_commit();
}
/**
* Define the webservice response.
*
* @return external_description|null always null.
*/
public static function execute_returns(): ?external_description {
return null;
}
}
+103
View File
@@ -0,0 +1,103 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_quiz\external;
use core_external\external_api;
use core_external\external_description;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use mod_quiz\quiz_attempt;
use mod_quiz\quiz_settings;
use moodle_exception;
/**
* Web service method to create quiz grade items for a quiz.
*
* The user must have the 'mod/quiz:manage' capability for the quiz.
*
* The new grade items are added in order, after all the existing ones.
*
* @package mod_quiz
* @copyright 2023 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class create_grade_items extends external_api {
/**
* Declare the method parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'quizid' => new external_value(PARAM_INT, 'The quiz to update slots for.'),
'quizgradeitems' => new external_multiple_structure(
new external_single_structure([
'name' => new external_value(PARAM_TEXT,
'The name for the grade item to create. If empty string, a sensible default is used.'),
])
),
]);
}
/**
* Add new grade items to this quiz.
*
* New items are added in order, at the end of the sort order.
*
* @param int $quizid the id of the quiz to add the grade items to.
* @param array $gradeitems list of grade items to create. (Each one must have property name.)
*/
public static function execute(int $quizid, array $gradeitems): void {
global $DB;
[
'quizid' => $quizid,
'quizgradeitems' => $gradeitems,
] = self::validate_parameters(self::execute_parameters(), [
'quizid' => $quizid,
'quizgradeitems' => $gradeitems,
]);
// Check the request is valid.
$quizobj = quiz_settings::create($quizid);
require_capability('mod/quiz:manage', $quizobj->get_context());
self::validate_context($quizobj->get_context());
$transaction = $DB->start_delegated_transaction();
$structure = $quizobj->get_structure();
foreach ($gradeitems as $gradeitemdata) {
$gradeitem = (object) $gradeitemdata;
$gradeitem->quizid = $quizid;
$structure->create_grade_item($gradeitem);
}
$transaction->allow_commit();
}
/**
* Define the webservice response.
*
* @return external_description|null always null.
*/
public static function execute_returns(): ?external_description {
return null;
}
}
+99
View File
@@ -0,0 +1,99 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_quiz\external;
use core_external\external_api;
use core_external\external_description;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use mod_quiz\quiz_attempt;
use mod_quiz\quiz_settings;
use moodle_exception;
/**
* Web service method to delete quiz grade items.
*
* The user must have the 'mod/quiz:manage' capability for the quiz.
*
* The grade items to be deleted must all belong to the same quiz,
* and must not be referred to by any slot.
*
* @package mod_quiz
* @copyright 2023 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class delete_grade_items extends external_api {
/**
* Declare the method parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'quizid' => new external_value(PARAM_INT, 'The quiz to update slots for.'),
'quizgradeitems' => new external_multiple_structure(
new external_single_structure([
'id' => new external_value(PARAM_INT, 'id of the quiz grade item'),
])
),
]);
}
/**
* Delete quiz grade items, if they are unused.
*
* @param int $quizid the id of the quiz from which to dlete grade items.
* @param array $gradeitems list of grade items to delete. (They must belong to this quiz.)
*/
public static function execute(int $quizid, array $gradeitems): void {
global $DB;
[
'quizid' => $quizid,
'quizgradeitems' => $gradeitems,
] = self::validate_parameters(self::execute_parameters(), [
'quizid' => $quizid,
'quizgradeitems' => $gradeitems,
]);
// Check the request is valid.
$quizobj = quiz_settings::create($quizid);
require_capability('mod/quiz:manage', $quizobj->get_context());
self::validate_context($quizobj->get_context());
$transaction = $DB->start_delegated_transaction();
$structure = $quizobj->get_structure();
foreach ($gradeitems as $gradeitemdata) {
$structure->delete_grade_item($gradeitemdata['id']);
}
$transaction->allow_commit();
}
/**
* Define the webservice response.
*
* @return external_description|null always null.
*/
public static function execute_returns(): ?external_description {
return null;
}
}
+77
View File
@@ -0,0 +1,77 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_quiz\external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use mod_quiz\quiz_settings;
/**
* Webservice for deleting quiz overrides.
*
* @package mod_quiz
* @copyright 2024 Matthew Hilton <matthewhilton@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class delete_overrides extends external_api {
/**
* Defines parameters
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
// This must be nested in a single structure, because the ids structure does not play nicely at the top level.
'data' => new external_single_structure([
'quizid' => new external_value(PARAM_INT, "ID of quiz to delete overrides in"),
'ids' => new external_multiple_structure(new external_value(PARAM_INT, 'ID of override to delete')),
]),
]);
}
/**
* Executes webservice function, deleting given overrides.
*
* @param array $params array of override parameters
* @return array with ids key, which contains the ids of the overrides successfully deleted.
*/
public static function execute($params): array {
$params = self::validate_parameters(self::execute_parameters(), ['data' => $params])['data'];
$quizsettings = quiz_settings::create($params['quizid']);
$manager = $quizsettings->get_override_manager();
self::validate_context($manager->context);
$manager->require_manage_capability();
$manager->delete_overrides_by_id($params['ids']);
return ['ids' => $params['ids']];
}
/**
* Defines return type
*
* @return external_single_structure
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'ids' => new external_multiple_structure(new external_value(PARAM_INT, 'ID of deleted override')),
]);
}
}
@@ -0,0 +1,89 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_quiz\external;
use core_external\external_api;
use core_external\external_description;
use core_external\external_function_parameters;
use core_external\external_value;
use Exception;
use html_writer;
use mod_quiz\output\edit_grading_page;
use mod_quiz\quiz_attempt;
use mod_quiz\quiz_settings;
use moodle_exception;
/**
* Web service to get the data required o re-render the Quiz grading setup page.
*
* The use must have the 'mod/quiz:manage' capability.
*
* @package mod_quiz
* @copyright 2023 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_edit_grading_page_data extends external_api {
/**
* Declare the method parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'quizid' => new external_value(PARAM_INT, 'The quiz for which to return the data.'),
]);
}
/**
* Check a quiz attempt state, and return a confirmation message method implementation.
*
* @param int $quizid the quiz for which to return the data.
* @return string a suitable confirmation message (HTML), if the attempt is suitable to be reopened.
* @throws Exception an appropriate exception if the attempt cannot be reopened now.
*/
public static function execute(int $quizid): string {
global $PAGE;
[
'quizid' => $quizid,
] = self::validate_parameters(self::execute_parameters(), [
'quizid' => $quizid,
]);
// Check the request is valid.
$quizobj = quiz_settings::create($quizid);
require_capability('mod/quiz:manage', $quizobj->get_context());
self::validate_context($quizobj->get_context());
// Set dummy URL to stop debugging in the renderer (TODO: remove as part of MDL-76728).
$PAGE->set_url('/');
$structure = $quizobj->get_structure();
$editpage = new edit_grading_page($structure);
return json_encode($editpage->export_for_template($PAGE->get_renderer('core')));
}
/**
* Define the webservice response.
*
* @return external_description
*/
public static function execute_returns(): external_description {
return new external_value(PARAM_RAW, 'JSON-encoded data required to render the mod_quiz/edit_grading_page template.');
}
}
+94
View File
@@ -0,0 +1,94 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_quiz\external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use mod_quiz\quiz_settings;
/**
* Webservice for searching overrides.
*
* @package mod_quiz
* @copyright 2024 Matthew Hilton <matthewhilton@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_overrides extends external_api {
/**
* Defines parameters for getting quiz overrides.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'quizid' => new external_value(PARAM_INT, 'ID of quiz to get overrides for'),
]);
}
/**
* Executes webservice function, returning quiz overrides.
*
* @param int $quizid
* @return array with overrides key which contains the overrides for the given quiz.
*/
public static function execute($quizid): array {
$params = self::validate_parameters(self::execute_parameters(), ['quizid' => $quizid]);
$quizsettings = quiz_settings::create($params['quizid']);
$manager = $quizsettings->get_override_manager();
self::validate_context($manager->context);
$manager->require_read_capability();
// Filter for those overrides user can access.
$overrides = array_filter(
$manager->get_all_overrides(),
fn(\stdClass $override) => $manager->can_view_override(
$override,
$quizsettings->get_course(),
$quizsettings->get_cm(),
),
);
return ['overrides' => $overrides];
}
/**
* Defines return type
*
* @return external_single_structure
*/
public static function execute_returns(): external_single_structure {
$overridedatastructure = new external_single_structure([
'id' => new external_value(PARAM_INT, 'Override ID'),
'quiz' => new external_value(PARAM_INT, 'Quiz ID'),
'userid' => new external_value(PARAM_INT, 'User ID', VALUE_DEFAULT, null),
'groupid' => new external_value(PARAM_INT, 'Group ID', VALUE_DEFAULT, null),
'timeopen' => new external_value(PARAM_INT, 'Override time open value', VALUE_DEFAULT, null),
'timeclose' => new external_value(PARAM_INT, 'Override time close value', VALUE_DEFAULT, null),
'timelimit' => new external_value(PARAM_INT, 'Override time limit value', VALUE_DEFAULT, null),
'attempts' => new external_value(PARAM_INT, 'Override attempts value', VALUE_DEFAULT, null),
'password' => new external_value(PARAM_TEXT, 'Override password', VALUE_DEFAULT, null),
]);
return new external_single_structure([
'overrides' => new external_multiple_structure($overridedatastructure),
]);
}
}
@@ -0,0 +1,98 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_quiz\external;
use core_external\external_api;
use core_external\external_description;
use core_external\external_function_parameters;
use core_external\external_value;
use Exception;
use html_writer;
use mod_quiz\quiz_attempt;
use moodle_exception;
/**
* Web service to check a quiz attempt state, and return a confirmation message if it can be reopened now.
*
* The use must have the 'mod/quiz:reopenattempts' capability and the attempt
* must (at least for now) be in the 'Never submitted' state (quiz_attempt::ABANDONED).
*
* @package mod_quiz
* @copyright 2023 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_reopen_attempt_confirmation extends external_api {
/**
* Declare the method parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'attemptid' => new external_value(PARAM_INT, 'The id of the attempt to reopen'),
]);
}
/**
* Check a quiz attempt state, and return a confirmation message method implementation.
*
* @param int $attemptid the id of the attempt to reopen.
* @return string a suitable confirmation message (HTML), if the attempt is suitable to be reopened.
* @throws Exception an appropriate exception if the attempt cannot be reopened now.
*/
public static function execute(int $attemptid): string {
global $DB;
['attemptid' => $attemptid] = self::validate_parameters(
self::execute_parameters(), ['attemptid' => $attemptid]);
// Check the request is valid.
$attemptobj = quiz_attempt::create($attemptid);
require_capability('mod/quiz:reopenattempts', $attemptobj->get_context());
self::validate_context($attemptobj->get_context());
if ($attemptobj->get_state() != quiz_attempt::ABANDONED) {
throw new moodle_exception('reopenattemptwrongstate', 'quiz', '',
['attemptid' => $attemptid, 'state' => quiz_attempt_state_name($attemptobj->get_state())]);
}
// Work out what the affect or re-opening will be.
$timestamp = time();
$timeclose = $attemptobj->get_access_manager(time())->get_end_time($attemptobj->get_attempt());
if ($timeclose && $timestamp > $timeclose) {
$expectedoutcome = get_string('reopenedattemptwillbesubmitted', 'quiz');
} else if ($timeclose) {
$expectedoutcome = get_string('reopenedattemptwillbeinprogressuntil', 'quiz', userdate($timeclose));
} else {
$expectedoutcome = get_string('reopenedattemptwillbeinprogress', 'quiz');
}
// Return the required message.
$user = $DB->get_record('user', ['id' => $attemptobj->get_userid()], '*', MUST_EXIST);
return html_writer::tag('p', get_string('reopenattemptareyousuremessage', 'quiz',
['attemptnumber' => $attemptobj->get_attempt_number(), 'attemptuser' => s(fullname($user))])) .
html_writer::tag('p', $expectedoutcome);
}
/**
* Define the webservice response.
*
* @return external_description
*/
public static function execute_returns(): external_description {
return new external_value(PARAM_RAW, 'Confirmation to show the user before the attempt is reopened.');
}
}
+79
View File
@@ -0,0 +1,79 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_quiz\external;
use core_external\external_api;
use core_external\external_description;
use core_external\external_function_parameters;
use core_external\external_value;
use mod_quiz\quiz_attempt;
use moodle_exception;
/**
* Web service method for re-opening a quiz attempt.
*
* The use must have the 'mod/quiz:reopenattempts' capability and the attempt
* must (at least for now) be in the 'Never submitted' state (quiz_attempt::ABANDONED).
*
* @package mod_quiz
* @copyright 2023 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class reopen_attempt extends external_api {
/**
* Declare the method parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'attemptid' => new external_value(PARAM_INT, 'The id of the attempt to reopen'),
]);
}
/**
* Re-opening a submitted attempt method implementation.
*
* @param int $attemptid the id of the attempt to reopen.
*/
public static function execute(int $attemptid): void {
['attemptid' => $attemptid] = self::validate_parameters(
self::execute_parameters(), ['attemptid' => $attemptid]);
// Check the request is valid.
$attemptobj = quiz_attempt::create($attemptid);
require_capability('mod/quiz:reopenattempts', $attemptobj->get_context());
self::validate_context($attemptobj->get_context());
if ($attemptobj->get_state() != quiz_attempt::ABANDONED) {
throw new moodle_exception('reopenattemptwrongstate', 'quiz', '',
['attemptid' => $attemptid, 'state' => quiz_attempt_state_name($attemptobj->get_state())]);
}
// Re-open the attempt.
$attemptobj->process_reopen_abandoned(time());
}
/**
* Define the webservice response.
*
* @return external_description|null always null.
*/
public static function execute_returns(): ?external_description {
return null;
}
}
+100
View File
@@ -0,0 +1,100 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_quiz\external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use mod_quiz\quiz_settings;
/**
* Webservice for upserting quiz overrides.
*
* @package mod_quiz
* @copyright 2024 Matthew Hilton <matthewhilton@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class save_overrides extends external_api {
/**
* Defines parameters
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
$overridestructure = new external_single_structure([
'id' => new external_value(PARAM_INT, 'ID of existing override (if updating)', VALUE_DEFAULT, null),
'groupid' => new external_value(PARAM_INT, 'ID of group', VALUE_DEFAULT, null),
'userid' => new external_value(PARAM_INT, 'ID of user', VALUE_DEFAULT, null),
'timeopen' => new external_value(PARAM_INT, 'Quiz override opening timestamp', VALUE_DEFAULT, null),
'timeclose' => new external_value(PARAM_INT, 'Quiz override closing timestamp', VALUE_OPTIONAL, null),
'timelimit' => new external_value(PARAM_INT, 'Quiz override time limit', VALUE_DEFAULT, null),
'attempts' => new external_value(PARAM_INT, 'Quiz override attempt count', VALUE_DEFAULT, null),
'password' => new external_value(PARAM_TEXT, 'Quiz override password', VALUE_DEFAULT, null),
]);
return new external_function_parameters([
// This must be nested in a single structure, because the overrides structure does not play nicely at the top level.
'data' => new external_single_structure([
'quizid' => new external_value(PARAM_INT, 'ID of quiz to save overrides to'),
'overrides' => new external_multiple_structure($overridestructure),
]),
]);
}
/**
* Executes webservice function, saving the requested overrides.
*
* @param array $data array with quizid key and overrides key containing list of overrides to save.
* @return array with ids key which contains ids of created/updated overrides.
*/
public static function execute($data): array {
$params = self::validate_parameters(self::execute_parameters(), ['data' => $data])['data'];
$quizsettings = quiz_settings::create($params['quizid']);
$manager = $quizsettings->get_override_manager();
self::validate_context($manager->context);
$manager->require_manage_capability();
// Filter for those overrides user can access.
$overrides = array_filter(
$params['overrides'],
fn(array $override) => $manager->can_view_override(
(object) $override,
$quizsettings->get_course(),
$quizsettings->get_cm(),
),
);
// Iterate over and save all overrides.
$ids = array_map(fn($override) => $manager->save_override($override), $overrides);
return ['ids' => $ids];
}
/**
* Defines return type
*
* @return external_single_structure
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'ids' => new external_multiple_structure(new external_value(PARAM_INT, 'ID of created/updated override')),
]);
}
}
+101
View File
@@ -0,0 +1,101 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_quiz\external;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/question/engine/lib.php');
require_once($CFG->dirroot . '/question/engine/datalib.php');
require_once($CFG->libdir . '/questionlib.php');
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_single_structure;
use core_external\external_value;
use stdClass;
/**
* External api for changing the question version in the quiz.
*
* @package mod_quiz
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class submit_question_version extends external_api {
/**
* Parameters for the submit_question_version.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'slotid' => new external_value(PARAM_INT, ''),
'newversion' => new external_value(PARAM_INT, '')
]);
}
/**
* Set the questions slot parameters to display the question template.
*
* @param int $slotid Slot id to display.
* @param int $newversion the version to set. 0 means 'always latest'.
* @return array
*/
public static function execute(int $slotid, int $newversion): array {
global $DB;
$params = [
'slotid' => $slotid,
'newversion' => $newversion
];
$params = self::validate_parameters(self::execute_parameters(), $params);
$response = [];
// Get the required data.
$referencedata = $DB->get_record('question_references',
['itemid' => $params['slotid'], 'component' => 'mod_quiz', 'questionarea' => 'slot']);
$slotdata = $DB->get_record('quiz_slots', ['id' => $slotid]);
// Capability check.
[, $cm] = get_course_and_cm_from_instance($slotdata->quizid, 'quiz');
$context = \context_module::instance($cm->id);
self::validate_context($context);
require_capability('mod/quiz:manage', $context);
$reference = new stdClass();
$reference->id = $referencedata->id;
if ($params['newversion'] === 0) {
$reference->version = null;
} else {
$reference->version = $params['newversion'];
}
$response['result'] = $DB->update_record('question_references', $reference);
return $response;
}
/**
* Define the webservice response.
*
* @return \core_external\external_description
*/
public static function execute_returns() {
return new external_single_structure([
'result' => new external_value(PARAM_BOOL, '')
]);
}
}
+104
View File
@@ -0,0 +1,104 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_quiz\external;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/externallib.php');
require_once($CFG->dirroot . '/question/editlib.php');
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
use external_function_parameters;
use external_single_structure;
use external_value;
use external_api;
/**
* Update the filter condition for a random question.
*
* @package mod_quiz
* @copyright 2022 Catalyst IT Australia Pty Ltd
* @author Nathan Nguyen <nathannguyen@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class update_filter_condition extends external_api {
/**
* Parameters for the web service function
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters ([
'cmid' => new external_value(PARAM_INT, 'The cmid of the quiz'),
'slotid' => new external_value(PARAM_INT, 'The quiz slot ID for the random question.'),
'filtercondition' => new external_value(PARAM_TEXT, 'Filter condition'),
]);
}
/**
* Add random questions.
*
* @param int $cmid course module id
* @param int $slotid The quiz slot id
* @param string $filtercondition
* @return array result
*/
public static function execute(
int $cmid,
int $slotid,
string $filtercondition,
): array {
global $DB;
[
'cmid' => $cmid,
'slotid' => $slotid,
'filtercondition' => $filtercondition,
] = self::validate_parameters(self::execute_parameters(), [
'cmid' => $cmid,
'slotid' => $slotid,
'filtercondition' => $filtercondition,
]);
// Validate context.
$thiscontext = \context_module::instance($cmid);
self::validate_context($thiscontext);
require_capability('mod/quiz:manage', $thiscontext);
// Update filter condition.
$setparams = [
'itemid' => $slotid,
'questionarea' => 'slot',
'component' => 'mod_quiz',
];
$DB->set_field('question_set_references', 'filtercondition', $filtercondition, $setparams);
return ['message' => get_string('updatefilterconditon_success', 'mod_quiz')];
}
/**
* Returns description of method result value.
*
* @return external_value
*/
public static function execute_returns() {
return new external_single_structure([
'message' => new external_value(PARAM_TEXT, 'Message', VALUE_OPTIONAL)
]);
}
}
+105
View File
@@ -0,0 +1,105 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_quiz\external;
use core_external\external_api;
use core_external\external_description;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use mod_quiz\quiz_attempt;
use mod_quiz\quiz_settings;
use moodle_exception;
/**
* Web service method to update the properties of quiz grade items.
*
* The user must have the 'mod/quiz:manage' capability for the quiz.
*
* All the properties that can be set are optional. Only the ones passed are changed.
*
* @package mod_quiz
* @copyright 2023 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class update_grade_items extends external_api {
/**
* Declare the method parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'quizid' => new external_value(PARAM_INT, 'The quiz to update slots for.'),
'quizgradeitems' => new external_multiple_structure(
new external_single_structure([
'id' => new external_value(PARAM_INT, 'id of the quiz grade item'),
'name' => new external_value(
PARAM_TEXT,
'If passed, new name to set. Null, or not specified, to leave unchanged.',
VALUE_OPTIONAL
),
])
),
]);
}
/**
* Update grade items to this quiz.
*
* @param int $quizid the id of the quiz from which to dlete grade items.
* @param array $gradeitems list of grade items to update. Must have properties id and name
*/
public static function execute(int $quizid, array $gradeitems): void {
global $DB;
[
'quizid' => $quizid,
'quizgradeitems' => $gradeitems,
] = self::validate_parameters(self::execute_parameters(), [
'quizid' => $quizid,
'quizgradeitems' => $gradeitems,
]);
// Check the request is valid.
$quizobj = quiz_settings::create($quizid);
require_capability('mod/quiz:manage', $quizobj->get_context());
self::validate_context($quizobj->get_context());
$transaction = $DB->start_delegated_transaction();
$structure = $quizobj->get_structure();
foreach ($gradeitems as $gradeitemdata) {
if ($gradeitemdata['name'] !== null) {
$structure->update_grade_item((object) $gradeitemdata);
}
}
$transaction->allow_commit();
}
/**
* Define the webservice response.
*
* @return external_description|null always null.
*/
public static function execute_returns(): ?external_description {
return null;
}
}
+148
View File
@@ -0,0 +1,148 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_quiz\external;
use core_external\external_api;
use core_external\external_description;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use mod_quiz\quiz_attempt;
use mod_quiz\quiz_settings;
use moodle_exception;
/**
* Web service method to update the properties of one or more slots in a quiz.
*
* The user must have the 'mod/quiz:manage' capability for the quiz.
*
* All the properties that can be set are optional. Only the ones passed are changed.
* The full properties of the updated slot are returned.
*
* @package mod_quiz
* @copyright 2023 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class update_slots extends external_api {
/**
* Declare the method parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'quizid' => new external_value(PARAM_INT, 'The quiz to update slots for.'),
'slots' => new external_multiple_structure(
new external_single_structure([
'id' => new external_value(PARAM_INT, 'id of the slot'),
'displaynumber' => new external_value(
PARAM_TEXT,
'If passed, new customised question number. Empty string to clear customisation. ' .
'Null, or not specified, to leave unchanged.',
VALUE_OPTIONAL
),
'requireprevious' => new external_value(
PARAM_BOOL,
'Whether to make this slot dependent on the previous one. Null, or not specified, to leave unchanged.',
VALUE_OPTIONAL
),
'maxmark' => new external_value(
PARAM_FLOAT,
'Mark that this questions is out of. Null, or not specified, to leave unchanged.',
VALUE_OPTIONAL
),
'quizgradeitemid' => new external_value(
PARAM_INT,
'For quizzes with multiple grades, which grade this slot contributes to (quiz_grade_id). ' .
'0 to set to nothing. Null, or not specified, to leave unchanged.',
VALUE_OPTIONAL
),
])
),
]);
}
/**
* Update the properties of one or more slots in a quiz.
*
* @param int $quizid the id of the quiz to update slots in.
* @param array $slotsdata list of slots update. Must have properties id, any any other properties to change.
*/
public static function execute(int $quizid, array $slotsdata): void {
global $DB;
[
'quizid' => $quizid,
'slots' => $slotsdata,
] = self::validate_parameters(self::execute_parameters(), [
'quizid' => $quizid,
'slots' => $slotsdata,
]);
// Check the request is valid.
$quizobj = quiz_settings::create($quizid);
require_capability('mod/quiz:manage', $quizobj->get_context());
self::validate_context($quizobj->get_context());
$transaction = $DB->start_delegated_transaction();
$structure = $quizobj->get_structure();
$gradingsetupchanged = false;
foreach ($slotsdata as $slotdata) {
// Check this slot exists in this quiz.
$slot = $structure->get_slot_by_id($slotdata['id']);
if (isset($slotdata['displaynumber'])) {
$structure->update_slot_display_number($slot->id, $slotdata['displaynumber']);
}
if (isset($slotdata['requireprevious'])) {
$structure->update_question_dependency($slot->id, $slotdata['requireprevious']);
}
if (isset($slotdata['maxmark'])) {
$gradingsetupchanged = $structure->update_slot_maxmark($slot, $slotdata['maxmark'])
|| $gradingsetupchanged;
}
if (array_key_exists('quizgradeitemid', $slotdata)) {
$gradingsetupchanged = $structure->update_slot_grade_item($slot, $slotdata['quizgradeitemid'])
|| $gradingsetupchanged;
}
}
// If the grade setup has canged, recompute things.
if ($gradingsetupchanged) {
$gradecalculator = $quizobj->get_grade_calculator();
quiz_delete_previews($quizobj->get_quiz());
$gradecalculator->recompute_quiz_sumgrades();
$gradecalculator->recompute_all_attempt_sumgrades();
$gradecalculator->recompute_all_final_grades();
quiz_update_grades($quizobj->get_quiz(), 0, true);
}
$transaction->allow_commit();
}
/**
* Define the webservice response.
*
* @return external_description|null always null.
*/
public static function execute_returns(): ?external_description {
return null;
}
}