Repository navigation
Expand file tree
/
Copy pathexternal.php
More file actions
214 lines (194 loc) · 7.23 KB
/
Copy pathexternal.php
File metadata and controls
214 lines (194 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?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/>.
/**
* Opencast external API
*
* @package tool_opencast
* @category external
* @copyright 2018 Tobias Reischmann <tobias.reischmann@wi.uni-muenster.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.2
*/
defined('MOODLE_INTERNAL') || die;
require_once($CFG->libdir . '/externallib.php');
require_once($CFG->libdir . '/authlib.php');
/**
* Opencast external API
*
* @package tool_opencast
* @category external
* @copyright 2018 Tobias Reischmann <tobias.reischmann@wi.uni-muenster.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_opencast_external extends external_api {
/**
* Describes the parameters for getting courses for a opencast instructor.
*
* @return external_function_parameters
* @throws coding_exception
*/
public static function get_courses_for_instructor_parameters() {
return new external_function_parameters(
array(
'username' => new external_value(core_user::get_property_type('username'), 'User Name'),
)
);
}
/**
* Describes the parameters for getting courses for a opencast learner.
*
* @return external_function_parameters
* @throws coding_exception
*/
public static function get_courses_for_learner_parameters() {
return new external_function_parameters(
array(
'username' => new external_value(core_user::get_property_type('username'), 'User Name'),
)
);
}
/**
* Describes the parameters for getting groups for a opencast user.
*
* @return external_function_parameters
* @throws coding_exception
*/
public static function get_groups_for_learner_parameters() {
return new external_function_parameters(
array(
'username' => new external_value(core_user::get_property_type('username'), 'User Name'),
)
);
}
/**
* Get all courses for a user, in which he has the capabilities of a instructor.
*
* @param string $username user name
* @return array list of course ids
* @throws coding_exception
* @throws dml_exception
* @throws invalid_parameter_exception
* @throws required_capability_exception
*/
public static function get_courses_for_instructor($username) {
self::validate_parameters(self::get_courses_for_instructor_parameters(), array('username' => $username));
return self::get_courses_with_capability($username, 'tool/opencast:instructor');
}
/**
* Get all courses for a user, in which he has the capabilities of a learner.
*
* @param string $username user name
* @return array list of course ids
* @throws coding_exception
* @throws dml_exception
* @throws invalid_parameter_exception
* @throws required_capability_exception
*/
public static function get_courses_for_learner($username) {
self::validate_parameters(self::get_courses_for_learner_parameters(), array('username' => $username));
return self::get_courses_with_capability($username, 'tool/opencast:learner');
}
/**
* Get all courses for a user, in which he has the capabilities of a learner.
*
* @param string $username user name
* @return array list of course ids
* @throws coding_exception
* @throws dml_exception
* @throws invalid_parameter_exception
* @throws required_capability_exception
*/
public static function get_groups_for_learner($username) {
self::validate_parameters(self::get_groups_for_learner_parameters(), array('username' => $username));
$context = context_system::instance();
if (!has_capability('tool/opencast:externalapi', $context)) {
throw new required_capability_exception($context, 'tool/opencast:externalapi', 'nopermissions', '');
}
if (!has_capability('moodle/site:accessallgroups', $context)) {
throw new required_capability_exception($context, 'moodle/site:accessallgroups', 'nopermissions', '');
}
global $DB;
$user = core_user::get_user_by_username($username);
return $DB->get_records('groups_members', array('userid' => $user->id), '', 'groupid as id');
}
/**
* Returns all course ids where the user has the specific capability in.
* @param $username
* @param $capability
* @return array
* @throws coding_exception
* @throws dml_exception
* @throws required_capability_exception
*/
private static function get_courses_with_capability($username, $capability) {
$result = array();
$context = context_system::instance();
if (!has_capability('tool/opencast:externalapi', $context)) {
throw new required_capability_exception($context, 'tool/opencast:externalapi', 'nopermissions', '');
}
$user = core_user::get_user_by_username($username);
$courses = enrol_get_all_users_courses($user->id);
foreach ($courses as $course) {
$context = context_course::instance($course->id);
if (has_capability($capability, $context, $user)) {
$result [] = array('id' => $course->id);
}
}
return $result;
}
/**
* Describes the confirm_user return value.
*
* @return external_multiple_structure array of course ids
*/
public static function get_courses_for_instructor_returns() {
return new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'id of course'),
)
)
);
}
/**
* Describes the confirm_user return value.
*
* @return external_multiple_structure array of course ids
*/
public static function get_courses_for_learner_returns() {
return new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'id of course'),
)
)
);
}
/**
* Describes the confirm_user return value.
*
* @return external_multiple_structure array of course ids
*/
public static function get_groups_for_learner_returns() {
return new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'id of group'),
)
)
);
}
}