Differences Between: [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 namespace enrol_meta\external; 18 19 use core_external\external_api; 20 use core_external\external_function_parameters; 21 use core_external\external_multiple_structure; 22 use core_external\external_single_structure; 23 use core_external\external_value; 24 use invalid_parameter_exception; 25 use context_course; 26 use moodle_exception; 27 28 /** 29 * Web service function relating to add enrol meta instances 30 * 31 * @package enrol_meta 32 * @copyright 2021 WKS KV Bildung 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 */ 35 class delete_instances extends external_api { 36 37 /** 38 * Parameters for deleting meta enrolment instances 39 * 40 * @return external_function_parameters 41 */ 42 public static function execute_parameters(): external_function_parameters { 43 return new external_function_parameters([ 44 'instances' => new external_multiple_structure( 45 new external_single_structure( 46 [ 47 'metacourseid' => new external_value(PARAM_INT, 'ID of the course with meta enrolment.'), 48 'courseid' => new external_value(PARAM_RAW, 'ID of the course where meta enrolment is linked to.'), 49 ] 50 ), 'List of course meta enrolment instances to delete.', VALUE_DEFAULT, [] 51 ), 52 ]); 53 } 54 55 /** 56 * Deleting meta enrolment instances 57 * 58 * @param array $instances 59 * @return array 60 */ 61 public static function execute(array $instances): array { 62 global $DB; 63 // Parameter validation. 64 $params = self::validate_parameters(self::execute_parameters(), [ 65 'instances' => $instances, 66 ]); 67 68 if (!count($params['instances'])) { 69 throw new invalid_parameter_exception(get_string('wsnoinstancesspecified', 'enrol_meta')); 70 } 71 72 $result = []; 73 foreach ($params['instances'] as $instance) { 74 // Ensure the metacourse exists. 75 $metacourserecord = $DB->get_record('course', ['id' => $instance['metacourseid']], 'id,visible'); 76 if (!$metacourserecord) { 77 throw new invalid_parameter_exception(get_string('wsinvalidmetacourse', 'enrol_meta', $instance['metacourseid'])); 78 } 79 // Ensure the current user is allowed to access metacourse. 80 $contextmeta = context_course::instance($instance['metacourseid'], IGNORE_MISSING); 81 try { 82 self::validate_context($contextmeta); 83 require_all_capabilities(['moodle/course:enrolconfig', 'enrol/meta:config'], $contextmeta); 84 } catch (moodle_exception $e) { 85 throw new invalid_parameter_exception(get_string('wsinvalidmetacourse', 'enrol_meta', $instance['metacourseid'])); 86 } 87 88 // Ensure the linked course exists. 89 $courserecord = $DB->get_record('course', ['id' => $instance['courseid']], 'id,visible'); 90 if (!$courserecord) { 91 throw new invalid_parameter_exception(get_string('wsinvalidcourse', 'enrol_meta', $instance['courseid'])); 92 } 93 // It is probably needed to check if user is allowed to access linked course. 94 // but can_delete_instance does not do that, so we stop permission check here. 95 96 // Check for existing meta course link. 97 $enrolrecord = $DB->get_record('enrol', 98 ['enrol' => 'meta', 'courseid' => $instance['metacourseid'], 'customint1' => $instance['courseid']]); 99 if ($enrolrecord) { 100 // Link exists. Delete instance. 101 $enrolplugin = enrol_get_plugin('meta'); 102 $enrolplugin->delete_instance($enrolrecord); 103 104 $result[] = [ 105 'metacourseid' => $instance['metacourseid'], 106 'courseid' => $instance['courseid'], 107 'status' => true, 108 ]; 109 continue; 110 } 111 $result[] = [ 112 'metacourseid' => $instance['metacourseid'], 113 'courseid' => $instance['courseid'], 114 'status' => false, 115 ]; 116 } 117 118 return $result; 119 } 120 121 /** 122 * Return for deleting enrolment instances. 123 * 124 * @return external_multiple_structure 125 */ 126 public static function execute_returns(): external_multiple_structure { 127 return new external_multiple_structure( 128 new external_single_structure( 129 [ 130 'metacourseid' => new external_value(PARAM_INT, 'ID of the course where meta enrolment is deleted.'), 131 'courseid' => new external_value(PARAM_RAW, 'ID of the course that was meta linked.'), 132 'status' => new external_value(PARAM_BOOL, 'True on success, false if meta link did not exist.'), 133 134 ] 135 ), 'List of course meta enrolment instances that were deleted.', VALUE_DEFAULT, [] 136 ); 137 } 138 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body