MDL-49650 Templates: Move templates out of invalid sub directory.
[moodle.git] / completion / classes / external.php
CommitLineData
b9d17109
JL
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 * Completion external API
19 *
20 * @package core_completion
21 * @category external
22 * @copyright 2015 Juan Leyva <juan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @since Moodle 2.9
25 */
26
27defined('MOODLE_INTERNAL') || die;
28
29require_once("$CFG->libdir/externallib.php");
30require_once("$CFG->libdir/completionlib.php");
31
32/**
33 * Completion external functions
34 *
35 * @package core_completion
36 * @category external
37 * @copyright 2015 Juan Leyva <juan@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 * @since Moodle 2.9
40 */
41class core_completion_external extends external_api {
42
43 /**
44 * Describes the parameters for update_activity_completion_status_manually.
45 *
46 * @return external_external_function_parameters
47 * @since Moodle 2.9
48 */
49 public static function update_activity_completion_status_manually_parameters() {
50 return new external_function_parameters (
51 array(
52 'cmid' => new external_value(PARAM_INT, 'course module id'),
53 'completed' => new external_value(PARAM_BOOL, 'activity completed or not'),
54 )
55 );
56 }
57
58 /**
59 * Update completion status for the current user in an activity, only for activities with manual tracking.
60 * @param int $cmid Course module id
61 * @param bool $completed Activity completed or not
62 * @return array Result and possible warnings
63 * @since Moodle 2.9
64 * @throws moodle_exception
65 */
66 public static function update_activity_completion_status_manually($cmid, $completed) {
67
68 // Validate and normalize parameters.
69 $params = self::validate_parameters(self::update_activity_completion_status_manually_parameters(),
70 array('cmid' => $cmid, 'completed' => $completed));
71 $cmid = $params['cmid'];
72 $completed = $params['completed'];
73
74 $warnings = array();
75
76 $context = context_module::instance($cmid);
77 self::validate_context($context);
78
79 list($course, $cm) = get_course_and_cm_from_cmid($cmid);
80
81 // Set up completion object and check it is enabled.
82 $completion = new completion_info($course);
83 if (!$completion->is_enabled()) {
84 throw new moodle_exception('completionnotenabled', 'completion');
85 }
86
87 // Check completion state is manual.
88 if ($cm->completion != COMPLETION_TRACKING_MANUAL) {
89 throw new moodle_exception('cannotmanualctrack', 'error');
90 }
91
92 $targetstate = ($completed) ? COMPLETION_COMPLETE : COMPLETION_INCOMPLETE;
93 $completion->update_state($cm, $targetstate);
94
95 $result = array();
96 $result['status'] = true;
97 $result['warnings'] = $warnings;
98 return $result;
99 }
100
101 /**
102 * Describes the update_activity_completion_status_manually return value.
103 *
104 * @return external_single_structure
105 * @since Moodle 2.9
106 */
107 public static function update_activity_completion_status_manually_returns() {
108
109 return new external_single_structure(
110 array(
111 'status' => new external_value(PARAM_BOOL, 'status, true if success'),
112 'warnings' => new external_warnings(),
113 )
114 );
115 }
116
5f2406c9
CC
117 /**
118 * Returns description of method parameters
119 *
120 * @return external_function_parameters
121 * @since Moodle 2.9
122 */
123 public static function get_activities_completion_status_parameters() {
124 return new external_function_parameters(
125 array(
126 'courseid' => new external_value(PARAM_INT, 'Course ID'),
127 'userid' => new external_value(PARAM_INT, 'User ID'),
128 )
129 );
130 }
131
132 /**
133 * Get Activities completion status
134 *
135 * @param int $courseid ID of the Course
136 * @param int $userid ID of the User
137 * @return array of activities progress and warnings
138 * @throws moodle_exception
139 * @since Moodle 2.9
604afeff 140 * @throws moodle_exception
5f2406c9
CC
141 */
142 public static function get_activities_completion_status($courseid, $userid) {
143 global $CFG, $USER;
144 require_once($CFG->libdir . '/grouplib.php');
9cd1093c 145
5f2406c9
CC
146 $warnings = array();
147 $arrayparams = array(
148 'courseid' => $courseid,
149 'userid' => $userid,
150 );
151
152 $params = self::validate_parameters(self::get_activities_completion_status_parameters(), $arrayparams);
153
154 $course = get_course($params['courseid']);
155 $user = core_user::get_user($params['userid'], 'id', MUST_EXIST);
156
157 $context = context_course::instance($course->id);
158 self::validate_context($context);
159
160 // Check that current user have permissions to see this user's activities.
161 if ($user->id != $USER->id) {
162 require_capability('report/progress:view', $context);
9cd1093c
JL
163 if (!groups_user_groups_visible($course, $user->id)) {
164 // We are not in the same group!
165 throw new moodle_exception('accessdenied', 'admin');
5f2406c9
CC
166 }
167 }
168
169 $completion = new completion_info($course);
170 $activities = $completion->get_activities();
171 $progresses = $completion->get_progress_all();
172 $userprogress = $progresses[$user->id];
173
174 $results = array();
175 foreach ($activities as $activity) {
176
177 // Check if current user has visibility on this activity.
9cd1093c 178 if (!$activity->uservisible) {
5f2406c9
CC
179 continue;
180 }
181
182 // Get progress information and state.
183 if (array_key_exists($activity->id, $userprogress->progress)) {
184 $thisprogress = $userprogress->progress[$activity->id];
185 $state = $thisprogress->completionstate;
186 $timecompleted = $thisprogress->timemodified;
187 } else {
188 $state = COMPLETION_INCOMPLETE;
189 $timecompleted = 0;
190 }
191
192 $results[] = array(
193 'cmid' => $activity->id,
194 'modname' => $activity->modname,
195 'instance' => $activity->instance,
196 'state' => $state,
197 'timecompleted' => $timecompleted,
9cd1093c 198 'tracking' => $activity->completion
5f2406c9
CC
199 );
200 }
201
202 $results = array(
203 'statuses' => $results,
204 'warnings' => $warnings
205 );
206 return $results;
207 }
208
209 /**
210 * Returns description of method result value
211 *
212 * @return external_description
213 * @since Moodle 2.9
214 */
215 public static function get_activities_completion_status_returns() {
216 return new external_single_structure(
217 array(
218 'statuses' => new external_multiple_structure(
219 new external_single_structure(
220 array(
9cd1093c
JL
221 'cmid' => new external_value(PARAM_INT, 'comment ID'),
222 'modname' => new external_value(PARAM_PLUGIN, 'activity module name'),
223 'instance' => new external_value(PARAM_INT, 'instance ID'),
224 'state' => new external_value(PARAM_INT, 'completion state value:
225 0 means incomplete, 1 complete,
226 2 complete pass, 3 complete fail'),
227 'timecompleted' => new external_value(PARAM_INT, 'timestamp for completed activity'),
228 'tracking' => new external_value(PARAM_INT, 'type of tracking:
229 0 means none, 1 manual, 2 automatic'),
5f2406c9
CC
230 ), 'Activity'
231 ), 'List of activities status'
232 ),
233 'warnings' => new external_warnings()
234 )
235 );
236 }
90c32664
CC
237
238 /**
239 * Returns description of method parameters
240 *
241 * @return external_function_parameters
242 * @since Moodle 2.9
243 */
244 public static function get_course_completion_status_parameters() {
245 return new external_function_parameters(
246 array(
247 'courseid' => new external_value(PARAM_INT, 'Course ID'),
248 'userid' => new external_value(PARAM_INT, 'User ID'),
249 )
250 );
251 }
252 /**
253 * Get Course completion status
254 *
255 * @param int $courseid ID of the Course
256 * @param int $userid ID of the User
257 * @return array of course completion status and warnings
258 * @since Moodle 2.9
259 * @throws moodle_exception
260 */
261 public static function get_course_completion_status($courseid, $userid) {
262 global $CFG, $USER;
263 require_once($CFG->libdir . '/grouplib.php');
264
265 $warnings = array();
266 $arrayparams = array(
267 'courseid' => $courseid,
268 'userid' => $userid,
269 );
270 $params = self::validate_parameters(self::get_course_completion_status_parameters(), $arrayparams);
271
272 $course = get_course($params['courseid']);
273 $user = core_user::get_user($params['userid'], 'id', MUST_EXIST);
274 $context = context_course::instance($course->id);
275 self::validate_context($context);
276
277 // Can current user see user's course completion status?
278 // This check verifies if completion is enabled because $course is mandatory.
279 if (!completion_can_view_data($user->id, $course)) {
280 throw new moodle_exception('cannotviewreport');
281 }
282
283 // The previous function doesn't check groups.
284 if ($user->id != $USER->id) {
285 if (!groups_user_groups_visible($course, $user->id)) {
286 // We are not in the same group!
287 throw new moodle_exception('accessdenied', 'admin');
288 }
289 }
290
291 $info = new completion_info($course);
292
293 // Check this user is enroled.
294 if (!$info->is_tracked_user($user->id)) {
295 if ($USER->id == $user->id) {
296 throw new moodle_exception('notenroled', 'completion');
297 } else {
298 throw new moodle_exception('usernotenroled', 'completion');
299 }
300 }
301
302 $completions = $info->get_completions($user->id);
303 if (empty($completions)) {
304 throw new moodle_exception('err_nocriteria', 'completion');
305 }
306
307 // Load course completion.
308 $completionparams = array(
309 'userid' => $user->id,
310 'course' => $course->id,
311 );
312 $ccompletion = new completion_completion($completionparams);
313
314 $completionrows = array();
315 // Loop through course criteria.
316 foreach ($completions as $completion) {
317 $criteria = $completion->get_criteria();
318
319 $completionrow = array();
320 $completionrow['type'] = $criteria->criteriatype;
321 $completionrow['title'] = $criteria->get_title();
322 $completionrow['status'] = $completion->get_status();
323 $completionrow['complete'] = $completion->is_complete();
324 $completionrow['timecompleted'] = $completion->timecompleted;
325 $completionrow['details'] = $criteria->get_details($completion);
326 $completionrows[] = $completionrow;
327 }
328
329 $result = array(
330 'completed' => $info->is_course_complete($user->id),
331 'aggregation' => $info->get_aggregation_method(),
332 'completions' => $completionrows
333 );
334
335 $results = array(
336 'completionstatus' => $result,
337 'warnings' => $warnings
338 );
339 return $results;
340
341 }
342 /**
343 * Returns description of method result value
344 *
345 * @return external_description
346 * @since Moodle 2.9
347 */
348 public static function get_course_completion_status_returns() {
349 return new external_single_structure(
350 array(
351 'completionstatus' => new external_single_structure(
352 array(
353 'completed' => new external_value(PARAM_BOOL, 'true if the course is complete, false otherwise'),
354 'aggregation' => new external_value(PARAM_INT, 'aggregation method 1 means all, 2 means any'),
355 'completions' => new external_multiple_structure(
356 new external_single_structure(
357 array(
358 'type' => new external_value(PARAM_INT, 'Completion criteria type'),
359 'title' => new external_value(PARAM_TEXT, 'Completion criteria Title'),
360 'status' => new external_value(PARAM_NOTAGS, 'Completion status (Yes/No) a % or number'),
361 'complete' => new external_value(PARAM_BOOL, 'Completion status (true/false)'),
362 'timecompleted' => new external_value(PARAM_INT, 'Timestamp for criteria completetion'),
363 'details' => new external_single_structure(
364 array(
365 'type' => new external_value(PARAM_TEXT, 'Type description'),
366 'criteria' => new external_value(PARAM_RAW, 'Criteria description'),
367 'requirement' => new external_value(PARAM_TEXT, 'Requirement description'),
368 'status' => new external_value(PARAM_TEXT, 'Status description'),
369 ), 'details'),
370 ), 'Completions'
371 ), ''
372 )
373 ), 'Course status'
374 ),
375 'warnings' => new external_warnings()
376 ), 'Course completion status'
377 );
378 }
379
b9d17109 380}