| 1 | efrain | 1 | <?php
 | 
        
           |  |  | 2 | // This file is part of Moodle - http://moodle.org/
 | 
        
           |  |  | 3 | //
 | 
        
           |  |  | 4 | // Moodle is free software: you can redistribute it and/or modify
 | 
        
           |  |  | 5 | // it under the terms of the GNU General Public License as published by
 | 
        
           |  |  | 6 | // the Free Software Foundation, either version 3 of the License, or
 | 
        
           |  |  | 7 | // (at your option) any later version.
 | 
        
           |  |  | 8 | //
 | 
        
           |  |  | 9 | // Moodle is distributed in the hope that it will be useful,
 | 
        
           |  |  | 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
        
           |  |  | 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
        
           |  |  | 12 | // GNU General Public License for more details.
 | 
        
           |  |  | 13 | //
 | 
        
           |  |  | 14 | // You should have received a copy of the GNU General Public License
 | 
        
           |  |  | 15 | // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 | namespace mod_quiz\external;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | use coding_exception;
 | 
        
           |  |  | 20 | use core_external\external_api;
 | 
        
           |  |  | 21 | use core_external\external_description;
 | 
        
           |  |  | 22 | use core_external\external_function_parameters;
 | 
        
           |  |  | 23 | use core_external\external_multiple_structure;
 | 
        
           |  |  | 24 | use core_external\external_single_structure;
 | 
        
           |  |  | 25 | use core_external\external_value;
 | 
        
           |  |  | 26 | use mod_quiz\quiz_attempt;
 | 
        
           |  |  | 27 | use mod_quiz\quiz_settings;
 | 
        
           |  |  | 28 | use moodle_exception;
 | 
        
           |  |  | 29 | use stdClass;
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 | /**
 | 
        
           |  |  | 32 |  * For a quiz with no grade items yet, create a grade item for each section.
 | 
        
           |  |  | 33 |  *
 | 
        
           |  |  | 34 |  * And, assign the questions in each section to the corresponding grade item.
 | 
        
           |  |  | 35 |  *
 | 
        
           |  |  | 36 |  * The user must have the 'mod/quiz:manage' capability for the quiz.
 | 
        
           |  |  | 37 |  *
 | 
        
           |  |  | 38 |  * @package   mod_quiz
 | 
        
           |  |  | 39 |  * @copyright 2024 The Open University
 | 
        
           |  |  | 40 |  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 41 |  */
 | 
        
           |  |  | 42 | class create_grade_item_per_section extends external_api {
 | 
        
           |  |  | 43 |   | 
        
           |  |  | 44 |     /**
 | 
        
           |  |  | 45 |      * Declare the method parameters.
 | 
        
           |  |  | 46 |      *
 | 
        
           |  |  | 47 |      * @return external_function_parameters
 | 
        
           |  |  | 48 |      */
 | 
        
           |  |  | 49 |     public static function execute_parameters(): external_function_parameters {
 | 
        
           |  |  | 50 |         return new external_function_parameters([
 | 
        
           |  |  | 51 |             'quizid' => new external_value(PARAM_INT, 'The quiz to update slots for.'),
 | 
        
           |  |  | 52 |         ]);
 | 
        
           |  |  | 53 |     }
 | 
        
           |  |  | 54 |   | 
        
           |  |  | 55 |     /**
 | 
        
           |  |  | 56 |      * For a quiz with no grade items yet, create a grade item for each section.
 | 
        
           |  |  | 57 |      *
 | 
        
           |  |  | 58 |      * And, assign the questions in each section to the corresponding grade item.
 | 
        
           |  |  | 59 |      *
 | 
        
           |  |  | 60 |      * The user must have the 'mod/quiz:manage' capability for the quiz.
 | 
        
           |  |  | 61 |      *
 | 
        
           |  |  | 62 |      * @param int $quizid the id of the quiz to setup grade items for.
 | 
        
           |  |  | 63 |      */
 | 
        
           |  |  | 64 |     public static function execute(int $quizid): void {
 | 
        
           |  |  | 65 |         global $DB;
 | 
        
           |  |  | 66 |   | 
        
           |  |  | 67 |         [
 | 
        
           |  |  | 68 |             'quizid' => $quizid,
 | 
        
           |  |  | 69 |         ] = self::validate_parameters(self::execute_parameters(), [
 | 
        
           |  |  | 70 |             'quizid' => $quizid,
 | 
        
           |  |  | 71 |         ]);
 | 
        
           |  |  | 72 |   | 
        
           |  |  | 73 |         // Check the request is valid.
 | 
        
           |  |  | 74 |         $quizobj = quiz_settings::create($quizid);
 | 
        
           |  |  | 75 |         require_capability('mod/quiz:manage', $quizobj->get_context());
 | 
        
           |  |  | 76 |         self::validate_context($quizobj->get_context());
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 |         $structure = $quizobj->get_structure();
 | 
        
           |  |  | 79 |         if ($structure->get_grade_items()) {
 | 
        
           |  |  | 80 |             throw new coding_exception('Cannot use create_grade_item_per_section for a quiz ' .
 | 
        
           |  |  | 81 |                 'that already has grade items.');
 | 
        
           |  |  | 82 |         }
 | 
        
           |  |  | 83 |   | 
        
           |  |  | 84 |         $transaction = $DB->start_delegated_transaction();
 | 
        
           |  |  | 85 |   | 
        
           |  |  | 86 |         $gradeitemsids = [];
 | 
        
           |  |  | 87 |         foreach ($structure->get_sections() as $section) {
 | 
        
           | 11 | efrain | 88 |             // Only create a grade item for sections that contain at least one real question (not description).
 | 
        
           |  |  | 89 |             $hasrealquestion = false;
 | 
        
           |  |  | 90 |             foreach ($structure->get_slots_in_section($section->id) as $slot) {
 | 
        
           |  |  | 91 |                 $hasrealquestion = $hasrealquestion || $structure->is_real_question($slot);
 | 
        
           |  |  | 92 |             }
 | 
        
           |  |  | 93 |             if (!$hasrealquestion) {
 | 
        
           |  |  | 94 |                 continue;
 | 
        
           |  |  | 95 |             }
 | 
        
           |  |  | 96 |   | 
        
           |  |  | 97 |             // Grade item required. Create it.
 | 
        
           | 1 | efrain | 98 |             $gradeitem = new stdClass();
 | 
        
           |  |  | 99 |             $gradeitem->quizid = $quizid;
 | 
        
           |  |  | 100 |             $gradeitem->name = $section->heading;
 | 
        
           |  |  | 101 |             $structure->create_grade_item($gradeitem);
 | 
        
           |  |  | 102 |             $gradeitemsids[$section->id] = $gradeitem->id;
 | 
        
           |  |  | 103 |         }
 | 
        
           |  |  | 104 |   | 
        
           |  |  | 105 |         foreach ($structure->get_slots() as $slot) {
 | 
        
           | 11 | efrain | 106 |             if ($structure->is_real_question($slot->slot)) {
 | 
        
           |  |  | 107 |                 $structure->update_slot_grade_item($slot, $gradeitemsids[$slot->section->id]);
 | 
        
           |  |  | 108 |             }
 | 
        
           | 1 | efrain | 109 |         }
 | 
        
           |  |  | 110 |   | 
        
           |  |  | 111 |         $transaction->allow_commit();
 | 
        
           |  |  | 112 |     }
 | 
        
           |  |  | 113 |   | 
        
           |  |  | 114 |     /**
 | 
        
           |  |  | 115 |      * Define the webservice response.
 | 
        
           |  |  | 116 |      *
 | 
        
           |  |  | 117 |      * @return external_description|null always null.
 | 
        
           |  |  | 118 |      */
 | 
        
           |  |  | 119 |     public static function execute_returns(): ?external_description {
 | 
        
           |  |  | 120 |         return null;
 | 
        
           |  |  | 121 |     }
 | 
        
           |  |  | 122 | }
 |