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 |
declare(strict_types=1);
|
|
|
18 |
|
|
|
19 |
namespace mod_edusharing\external;
|
|
|
20 |
|
|
|
21 |
defined('MOODLE_INTERNAL') || die();
|
|
|
22 |
|
|
|
23 |
// Once Moodle versions < 4.2 are out of LTS, we need to revert this to the proper namespaces.
|
|
|
24 |
global $CFG;
|
|
|
25 |
require_once($CFG->dirroot . '/lib/externallib.php');
|
|
|
26 |
|
|
|
27 |
use context_course;
|
|
|
28 |
use external_api;
|
|
|
29 |
use external_function_parameters;
|
|
|
30 |
use external_single_structure;
|
|
|
31 |
use external_value;
|
|
|
32 |
use Exception;
|
|
|
33 |
use mod_edusharing\Constants;
|
|
|
34 |
use mod_edusharing\EduSharingService;
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* class DeleteInstance
|
|
|
38 |
*
|
|
|
39 |
* @author Marian Ziegler <ziegler@edu-sharing.net>
|
|
|
40 |
* @package mod_edusharing
|
|
|
41 |
*/
|
|
|
42 |
class DeleteInstance extends external_api {
|
|
|
43 |
/**
|
|
|
44 |
* Function execute_parameters
|
|
|
45 |
*
|
|
|
46 |
* defines the structure of the parameters to be provided
|
|
|
47 |
* The end point expects json as follows:
|
|
|
48 |
*
|
|
|
49 |
* {"eduDeleteStructure": {
|
|
|
50 |
* "id": 12
|
|
|
51 |
* "courseId": 5,
|
|
|
52 |
* }
|
|
|
53 |
* }
|
|
|
54 |
*
|
|
|
55 |
* @return external_function_parameters
|
|
|
56 |
*/
|
|
|
57 |
public static function execute_parameters(): external_function_parameters {
|
|
|
58 |
$edudeletestructure = new external_single_structure([
|
|
|
59 |
'id' => new external_value(PARAM_INT, 'id'),
|
|
|
60 |
'courseId' => new external_value(PARAM_INT, 'course id'),
|
|
|
61 |
]);
|
|
|
62 |
return new external_function_parameters(['eduDeleteStructure' => $edudeletestructure]);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Function execute_returns
|
|
|
67 |
*
|
|
|
68 |
* defines the return data
|
|
|
69 |
*
|
|
|
70 |
* @return external_single_structure
|
|
|
71 |
*/
|
|
|
72 |
public static function execute_returns(): external_single_structure {
|
|
|
73 |
return new external_single_structure([
|
|
|
74 |
'success' => new external_value(PARAM_BOOL, 'Success?'),
|
|
|
75 |
]);
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Function execute
|
|
|
80 |
*
|
|
|
81 |
* handles the service call
|
|
|
82 |
*
|
|
|
83 |
* @param array $edudeletestructure
|
|
|
84 |
* @return array
|
|
|
85 |
* @throws Exception
|
|
|
86 |
*/
|
|
|
87 |
public static function execute(array $edudeletestructure): array {
|
|
|
88 |
global $DB;
|
|
|
89 |
try {
|
|
|
90 |
$context = context_course::instance($edudeletestructure['courseId']);
|
|
|
91 |
require_capability('mod/edusharing:wysiwygvisibility', $context);
|
|
|
92 |
$where = [
|
|
|
93 |
'id' => $edudeletestructure['id'],
|
|
|
94 |
'course' => $edudeletestructure['courseId'],
|
|
|
95 |
];
|
|
|
96 |
$DB->get_record(Constants::EDUSHARING_TABLE, $where, MUST_EXIST);
|
|
|
97 |
$service = new EduSharingService();
|
|
|
98 |
$service->delete_instance((string)$edudeletestructure['id']);
|
|
|
99 |
} catch (Exception $exception) {
|
|
|
100 |
debugging($exception->getMessage());
|
|
|
101 |
return ['success' => false];
|
|
|
102 |
}
|
|
|
103 |
return ['success' => true];
|
|
|
104 |
}
|
|
|
105 |
}
|