Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]
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 /** 18 * Rest endpoint for ajax editing of quiz structure. 19 * 20 * @package mod_quiz 21 * @copyright 1999 Martin Dougiamas http://dougiamas.com 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 use mod_quiz\quiz_settings; 26 27 if (!defined('AJAX_SCRIPT')) { 28 define('AJAX_SCRIPT', true); 29 } 30 31 require_once(__DIR__ . '/../../config.php'); 32 require_once($CFG->dirroot . '/mod/quiz/locallib.php'); 33 34 // Initialise ALL the incoming parameters here, up front. 35 $quizid = required_param('quizid', PARAM_INT); 36 $class = required_param('class', PARAM_ALPHA); 37 $field = optional_param('field', '', PARAM_ALPHA); 38 $instanceid = optional_param('instanceId', 0, PARAM_INT); 39 $sectionid = optional_param('sectionId', 0, PARAM_INT); 40 $previousid = optional_param('previousid', 0, PARAM_INT); 41 $value = optional_param('value', 0, PARAM_INT); 42 $column = optional_param('column', 0, PARAM_ALPHA); 43 $id = optional_param('id', 0, PARAM_INT); 44 $summary = optional_param('summary', '', PARAM_RAW); 45 $sequence = optional_param('sequence', '', PARAM_SEQUENCE); 46 $visible = optional_param('visible', 0, PARAM_INT); 47 $pageaction = optional_param('action', '', PARAM_ALPHA); // Used to simulate a DELETE command. 48 $maxmark = optional_param('maxmark', '', PARAM_FLOAT); 49 $newheading = optional_param('newheading', '', PARAM_TEXT); 50 $shuffle = optional_param('newshuffle', 0, PARAM_INT); 51 $page = optional_param('page', '', PARAM_INT); 52 $ids = optional_param('ids', '', PARAM_SEQUENCE); 53 $PAGE->set_url('/mod/quiz/edit-rest.php', 54 ['quizid' => $quizid, 'class' => $class]); 55 56 require_sesskey(); 57 $quizobj = quiz_settings::create($quizid); 58 $quiz = $quizobj->get_quiz(); 59 $cm = $quizobj->get_cm(); 60 $course = $quizobj->get_course(); 61 require_login($course, false, $cm); 62 63 $structure = $quizobj->get_structure(); 64 $gradecalculator = $quizobj->get_grade_calculator(); 65 $modcontext = $quizobj->get_context(); 66 67 echo $OUTPUT->header(); // Send headers. 68 69 // All these AJAX actions should be logically atomic. 70 $transaction = $DB->start_delegated_transaction(); 71 72 // OK, now let's process the parameters and do stuff 73 // MDL-10221 the DELETE method is not allowed on some web servers, 74 // so we simulate it with the action URL param. 75 $requestmethod = $_SERVER['REQUEST_METHOD']; 76 if ($pageaction == 'DELETE') { 77 $requestmethod = 'DELETE'; 78 } 79 80 $result = null; 81 82 switch($requestmethod) { 83 case 'POST': 84 case 'GET': // For debugging. 85 switch ($class) { 86 case 'section': 87 $table = 'quiz_sections'; 88 $section = $structure->get_section_by_id($id); 89 switch ($field) { 90 case 'getsectiontitle': 91 require_capability('mod/quiz:manage', $modcontext); 92 $result = ['instancesection' => $section->heading]; 93 break; 94 case 'updatesectiontitle': 95 require_capability('mod/quiz:manage', $modcontext); 96 $structure->set_section_heading($id, $newheading); 97 $result = ['instancesection' => format_string($newheading)]; 98 break; 99 case 'updateshufflequestions': 100 require_capability('mod/quiz:manage', $modcontext); 101 $structure->set_section_shuffle($id, $shuffle); 102 $result = ['instanceshuffle' => $section->shufflequestions]; 103 break; 104 } 105 break; 106 107 case 'resource': 108 switch ($field) { 109 case 'move': 110 require_capability('mod/quiz:manage', $modcontext); 111 if (!$previousid) { 112 $section = $structure->get_section_by_id($sectionid); 113 if ($section->firstslot > 1) { 114 $previousid = $structure->get_slot_id_for_slot($section->firstslot - 1); 115 $page = $structure->get_page_number_for_slot($section->firstslot); 116 } 117 } 118 $structure->move_slot($id, $previousid, $page); 119 quiz_delete_previews($quiz); 120 $result = ['visible' => true]; 121 break; 122 123 case 'getmaxmark': 124 require_capability('mod/quiz:manage', $modcontext); 125 $slot = $DB->get_record('quiz_slots', ['id' => $id], '*', MUST_EXIST); 126 $result = ['instancemaxmark' => quiz_format_question_grade($quiz, $slot->maxmark)]; 127 break; 128 129 case 'updatemaxmark': 130 require_capability('mod/quiz:manage', $modcontext); 131 $slot = $structure->get_slot_by_id($id); 132 if ($structure->update_slot_maxmark($slot, $maxmark)) { 133 // Grade has really changed. 134 quiz_delete_previews($quiz); 135 $gradecalculator->recompute_quiz_sumgrades(); 136 $gradecalculator->recompute_all_attempt_sumgrades(); 137 $gradecalculator->recompute_all_final_grades(); 138 quiz_update_grades($quiz, 0, true); 139 } 140 $result = ['instancemaxmark' => quiz_format_question_grade($quiz, $maxmark), 141 'newsummarks' => quiz_format_grade($quiz, $quiz->sumgrades)]; 142 break; 143 144 case 'updatepagebreak': 145 require_capability('mod/quiz:manage', $modcontext); 146 $slots = $structure->update_page_break($id, $value); 147 $json = []; 148 foreach ($slots as $slot) { 149 $json[$slot->slot] = ['id' => $slot->id, 'slot' => $slot->slot, 150 'page' => $slot->page]; 151 } 152 $result = ['slots' => $json]; 153 break; 154 155 case 'deletemultiple': 156 require_capability('mod/quiz:manage', $modcontext); 157 158 $ids = explode(',', $ids); 159 foreach ($ids as $id) { 160 $slot = $DB->get_record('quiz_slots', ['quizid' => $quiz->id, 'id' => $id], 161 '*', MUST_EXIST); 162 if ($structure->has_use_capability($slot->slot)) { 163 $structure->remove_slot($slot->slot); 164 } 165 } 166 quiz_delete_previews($quiz); 167 $gradecalculator->recompute_quiz_sumgrades(); 168 169 $result = ['newsummarks' => quiz_format_grade($quiz, $quiz->sumgrades), 170 'deleted' => true, 'newnumquestions' => $structure->get_question_count()]; 171 break; 172 173 case 'updatedependency': 174 require_capability('mod/quiz:manage', $modcontext); 175 $slot = $structure->get_slot_by_id($id); 176 $value = (bool) $value; 177 $structure->update_question_dependency($slot->id, $value); 178 $result = ['requireprevious' => $value]; 179 break; 180 } 181 break; 182 } 183 break; 184 185 case 'DELETE': 186 switch ($class) { 187 case 'section': 188 require_capability('mod/quiz:manage', $modcontext); 189 $structure->remove_section_heading($id); 190 $result = ['deleted' => true]; 191 break; 192 193 case 'resource': 194 require_capability('mod/quiz:manage', $modcontext); 195 if (!$slot = $DB->get_record('quiz_slots', ['quizid' => $quiz->id, 'id' => $id])) { 196 throw new moodle_exception('AJAX commands.php: Bad slot ID '.$id); 197 } 198 199 if (!$structure->has_use_capability($slot->slot)) { 200 $slotdetail = $structure->get_slot_by_id($slot->id); 201 $context = context::instance_by_id($slotdetail->contextid); 202 throw new required_capability_exception($context, 203 'moodle/question:useall', 'nopermissions', ''); 204 } 205 $structure->remove_slot($slot->slot); 206 quiz_delete_previews($quiz); 207 $gradecalculator->recompute_quiz_sumgrades(); 208 $result = ['newsummarks' => quiz_format_grade($quiz, $quiz->sumgrades), 209 'deleted' => true, 'newnumquestions' => $structure->get_question_count()]; 210 break; 211 } 212 break; 213 } 214 215 $transaction->allow_commit(); 216 echo json_encode($result);
title
Description
Body
title
Description
Body
title
Description
Body
title
Body