NOBUG: Fixed SVG browser compatibility
[moodle.git] / cohort / externallib.php
blob9a30893548438e6e6eafa8092a7b46742d173171
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.
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/>.
17 /**
18 * External cohort API
20 * @package core_cohort
21 * @category external
22 * @copyright MediaTouch 2000 srl
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 require_once("$CFG->libdir/externallib.php");
30 class core_cohort_external extends external_api {
32 /**
33 * Returns description of method parameters
35 * @return external_function_parameters
36 * @since Moodle 2.5
38 public static function create_cohorts_parameters() {
39 return new external_function_parameters(
40 array(
41 'cohorts' => new external_multiple_structure(
42 new external_single_structure(
43 array(
44 'categorytype' => new external_single_structure(
45 array(
46 'type' => new external_value(PARAM_TEXT, 'the name of the field: id (numeric value
47 of course category id) or idnumber (alphanumeric value of idnumber course category)
48 or system (value ignored)'),
49 'value' => new external_value(PARAM_RAW, 'the value of the categorytype')
52 'name' => new external_value(PARAM_RAW, 'cohort name'),
53 'idnumber' => new external_value(PARAM_RAW, 'cohort idnumber'),
54 'description' => new external_value(PARAM_RAW, 'cohort description', VALUE_OPTIONAL),
55 'descriptionformat' => new external_format_value('description', VALUE_DEFAULT),
56 'visible' => new external_value(PARAM_BOOL, 'cohort visible', VALUE_OPTIONAL, true),
64 /**
65 * Create one or more cohorts
67 * @param array $cohorts An array of cohorts to create.
68 * @return array An array of arrays
69 * @since Moodle 2.5
71 public static function create_cohorts($cohorts) {
72 global $CFG, $DB;
73 require_once("$CFG->dirroot/cohort/lib.php");
75 $params = self::validate_parameters(self::create_cohorts_parameters(), array('cohorts' => $cohorts));
77 $transaction = $DB->start_delegated_transaction();
79 $syscontext = context_system::instance();
80 $cohortids = array();
82 foreach ($params['cohorts'] as $cohort) {
83 $cohort = (object)$cohort;
85 // Category type (context id).
86 $categorytype = $cohort->categorytype;
87 if (!in_array($categorytype['type'], array('idnumber', 'id', 'system'))) {
88 throw new invalid_parameter_exception('category type must be id, idnumber or system:' . $categorytype['type']);
90 if ($categorytype['type'] === 'system') {
91 $cohort->contextid = $syscontext->id;
92 } else if ($catid = $DB->get_field('course_categories', 'id', array($categorytype['type'] => $categorytype['value']))) {
93 $catcontext = context_coursecat::instance($catid);
94 $cohort->contextid = $catcontext->id;
95 } else {
96 throw new invalid_parameter_exception('category not exists: category '
97 .$categorytype['type'].' = '.$categorytype['value']);
99 // Make sure that the idnumber doesn't already exist.
100 if ($DB->record_exists('cohort', array('idnumber' => $cohort->idnumber))) {
101 throw new invalid_parameter_exception('record already exists: idnumber='.$cohort->idnumber);
103 $context = context::instance_by_id($cohort->contextid, MUST_EXIST);
104 if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
105 throw new invalid_parameter_exception('Invalid context');
107 self::validate_context($context);
108 require_capability('moodle/cohort:manage', $context);
110 // Validate format.
111 $cohort->descriptionformat = external_validate_format($cohort->descriptionformat);
112 $cohort->id = cohort_add_cohort($cohort);
114 list($cohort->description, $cohort->descriptionformat) =
115 external_format_text($cohort->description, $cohort->descriptionformat,
116 $context->id, 'cohort', 'description', $cohort->id);
117 $cohortids[] = (array)$cohort;
119 $transaction->allow_commit();
121 return $cohortids;
125 * Returns description of method result value
127 * @return external_description
128 * @since Moodle 2.5
130 public static function create_cohorts_returns() {
131 return new external_multiple_structure(
132 new external_single_structure(
133 array(
134 'id' => new external_value(PARAM_INT, 'cohort id'),
135 'name' => new external_value(PARAM_RAW, 'cohort name'),
136 'idnumber' => new external_value(PARAM_RAW, 'cohort idnumber'),
137 'description' => new external_value(PARAM_RAW, 'cohort description'),
138 'descriptionformat' => new external_format_value('description'),
139 'visible' => new external_value(PARAM_BOOL, 'cohort visible'),
146 * Returns description of method parameters
148 * @return external_function_parameters
149 * @since Moodle 2.5
151 public static function delete_cohorts_parameters() {
152 return new external_function_parameters(
153 array(
154 'cohortids' => new external_multiple_structure(new external_value(PARAM_INT, 'cohort ID')),
160 * Delete cohorts
162 * @param array $cohortids
163 * @return null
164 * @since Moodle 2.5
166 public static function delete_cohorts($cohortids) {
167 global $CFG, $DB;
168 require_once("$CFG->dirroot/cohort/lib.php");
170 $params = self::validate_parameters(self::delete_cohorts_parameters(), array('cohortids' => $cohortids));
172 $transaction = $DB->start_delegated_transaction();
174 foreach ($params['cohortids'] as $cohortid) {
175 // Validate params.
176 $cohortid = validate_param($cohortid, PARAM_INT);
177 $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST);
179 // Now security checks.
180 $context = context::instance_by_id($cohort->contextid, MUST_EXIST);
181 if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
182 throw new invalid_parameter_exception('Invalid context');
184 self::validate_context($context);
185 require_capability('moodle/cohort:manage', $context);
186 cohort_delete_cohort($cohort);
188 $transaction->allow_commit();
190 return null;
194 * Returns description of method result value
196 * @return null
197 * @since Moodle 2.5
199 public static function delete_cohorts_returns() {
200 return null;
204 * Returns description of method parameters
206 * @return external_function_parameters
207 * @since Moodle 2.5
209 public static function get_cohorts_parameters() {
210 return new external_function_parameters(
211 array(
212 'cohortids' => new external_multiple_structure(new external_value(PARAM_INT, 'Cohort ID')
213 , 'List of cohort id. A cohort id is an integer.', VALUE_DEFAULT, array()),
219 * Get cohorts definition specified by ids
221 * @param array $cohortids array of cohort ids
222 * @return array of cohort objects (id, courseid, name)
223 * @since Moodle 2.5
225 public static function get_cohorts($cohortids = array()) {
226 global $DB;
228 $params = self::validate_parameters(self::get_cohorts_parameters(), array('cohortids' => $cohortids));
230 if (empty($cohortids)) {
231 $cohorts = $DB->get_records('cohort');
232 } else {
233 $cohorts = $DB->get_records_list('cohort', 'id', $params['cohortids']);
236 $cohortsinfo = array();
237 foreach ($cohorts as $cohort) {
238 // Now security checks.
239 $context = context::instance_by_id($cohort->contextid, MUST_EXIST);
240 if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
241 throw new invalid_parameter_exception('Invalid context');
243 self::validate_context($context);
244 if (!has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:view'), $context)) {
245 throw new required_capability_exception($context, 'moodle/cohort:view', 'nopermissions', '');
248 list($cohort->description, $cohort->descriptionformat) =
249 external_format_text($cohort->description, $cohort->descriptionformat,
250 $context->id, 'cohort', 'description', $cohort->id);
252 $cohortsinfo[] = (array) $cohort;
254 return $cohortsinfo;
259 * Returns description of method result value
261 * @return external_description
262 * @since Moodle 2.5
264 public static function get_cohorts_returns() {
265 return new external_multiple_structure(
266 new external_single_structure(
267 array(
268 'id' => new external_value(PARAM_INT, 'ID of the cohort'),
269 'name' => new external_value(PARAM_RAW, 'cohort name'),
270 'idnumber' => new external_value(PARAM_RAW, 'cohort idnumber'),
271 'description' => new external_value(PARAM_RAW, 'cohort description'),
272 'descriptionformat' => new external_format_value('description'),
273 'visible' => new external_value(PARAM_BOOL, 'cohort visible'),
280 * Returns description of method parameters
282 * @return external_function_parameters
283 * @since Moodle 2.5
285 public static function update_cohorts_parameters() {
286 return new external_function_parameters(
287 array(
288 'cohorts' => new external_multiple_structure(
289 new external_single_structure(
290 array(
291 'id' => new external_value(PARAM_INT, 'ID of the cohort'),
292 'categorytype' => new external_single_structure(
293 array(
294 'type' => new external_value(PARAM_TEXT, 'the name of the field: id (numeric value
295 of course category id) or idnumber (alphanumeric value of idnumber course category)
296 or system (value ignored)'),
297 'value' => new external_value(PARAM_RAW, 'the value of the categorytype')
300 'name' => new external_value(PARAM_RAW, 'cohort name'),
301 'idnumber' => new external_value(PARAM_RAW, 'cohort idnumber'),
302 'description' => new external_value(PARAM_RAW, 'cohort description', VALUE_OPTIONAL),
303 'descriptionformat' => new external_format_value('description', VALUE_DEFAULT),
304 'visible' => new external_value(PARAM_BOOL, 'cohort visible', VALUE_OPTIONAL),
313 * Update cohorts
315 * @param array $cohorts
316 * @return null
317 * @since Moodle 2.5
319 public static function update_cohorts($cohorts) {
320 global $CFG, $DB;
321 require_once("$CFG->dirroot/cohort/lib.php");
323 $params = self::validate_parameters(self::update_cohorts_parameters(), array('cohorts' => $cohorts));
325 $transaction = $DB->start_delegated_transaction();
326 $syscontext = context_system::instance();
328 foreach ($params['cohorts'] as $cohort) {
329 $cohort = (object) $cohort;
331 if (trim($cohort->name) == '') {
332 throw new invalid_parameter_exception('Invalid cohort name');
335 $oldcohort = $DB->get_record('cohort', array('id' => $cohort->id), '*', MUST_EXIST);
336 $oldcontext = context::instance_by_id($oldcohort->contextid, MUST_EXIST);
337 require_capability('moodle/cohort:manage', $oldcontext);
339 // Category type (context id).
340 $categorytype = $cohort->categorytype;
341 if (!in_array($categorytype['type'], array('idnumber', 'id', 'system'))) {
342 throw new invalid_parameter_exception('category type must be id, idnumber or system:' . $categorytype['type']);
344 if ($categorytype['type'] === 'system') {
345 $cohort->contextid = $syscontext->id;
346 } else if ($catid = $DB->get_field('course_categories', 'id', array($categorytype['type'] => $categorytype['value']))) {
347 $cohort->contextid = $DB->get_field('context', 'id', array('instanceid' => $catid,
348 'contextlevel' => CONTEXT_COURSECAT));
349 } else {
350 throw new invalid_parameter_exception('category not exists: category='.$categorytype['value']);
353 if ($cohort->contextid != $oldcohort->contextid) {
354 $context = context::instance_by_id($cohort->contextid, MUST_EXIST);
355 if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
356 throw new invalid_parameter_exception('Invalid context');
359 self::validate_context($context);
360 require_capability('moodle/cohort:manage', $context);
363 if (!empty($cohort->description)) {
364 $cohort->descriptionformat = external_validate_format($cohort->descriptionformat);
367 cohort_update_cohort($cohort);
370 $transaction->allow_commit();
372 return null;
376 * Returns description of method result value
378 * @return null
379 * @since Moodle 2.5
381 public static function update_cohorts_returns() {
382 return null;
386 * Returns description of method parameters
388 * @return external_function_parameters
389 * @since Moodle 2.5
391 public static function add_cohort_members_parameters() {
392 return new external_function_parameters (
393 array(
394 'members' => new external_multiple_structure (
395 new external_single_structure (
396 array (
397 'cohorttype' => new external_single_structure (
398 array(
399 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the field: id
400 (numeric value of cohortid) or idnumber (alphanumeric value of idnumber) '),
401 'value' => new external_value(PARAM_RAW, 'The value of the cohort')
404 'usertype' => new external_single_structure (
405 array(
406 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the field: id
407 (numeric value of id) or username (alphanumeric value of username) '),
408 'value' => new external_value(PARAM_RAW, 'The value of the cohort')
419 * Add cohort members
421 * @param array $members of arrays with keys userid, cohortid
422 * @since Moodle 2.5
424 public static function add_cohort_members($members) {
425 global $CFG, $DB;
426 require_once($CFG->dirroot."/cohort/lib.php");
428 $params = self::validate_parameters(self::add_cohort_members_parameters(), array('members' => $members));
430 $transaction = $DB->start_delegated_transaction();
431 $warnings = array();
432 foreach ($params['members'] as $member) {
433 // Cohort parameters.
434 $cohorttype = $member['cohorttype'];
435 $cohortparam = array($cohorttype['type'] => $cohorttype['value']);
436 // User parameters.
437 $usertype = $member['usertype'];
438 $userparam = array($usertype['type'] => $usertype['value']);
439 try {
440 // Check parameters.
441 if ($cohorttype['type'] != 'id' && $cohorttype['type'] != 'idnumber') {
442 $warning = array();
443 $warning['warningcode'] = '1';
444 $warning['message'] = 'invalid parameter: cohortype='.$cohorttype['type'];
445 $warnings[] = $warning;
446 continue;
448 if ($usertype['type'] != 'id' && $usertype['type'] != 'username') {
449 $warning = array();
450 $warning['warningcode'] = '1';
451 $warning['message'] = 'invalid parameter: usertype='.$usertype['type'];
452 $warnings[] = $warning;
453 continue;
455 // Extract parameters.
456 if (!$cohortid = $DB->get_field('cohort', 'id', $cohortparam)) {
457 $warning = array();
458 $warning['warningcode'] = '2';
459 $warning['message'] = 'cohort '.$cohorttype['type'].'='.$cohorttype['value'].' not exists';
460 $warnings[] = $warning;
461 continue;
463 if (!$userid = $DB->get_field('user', 'id', array_merge($userparam, array('deleted' => 0,
464 'mnethostid' => $CFG->mnet_localhost_id)))) {
465 $warning = array();
466 $warning['warningcode'] = '2';
467 $warning['message'] = 'user '.$usertype['type'].'='.$usertype['value'].' not exists';
468 $warnings[] = $warning;
469 continue;
471 if ($DB->record_exists('cohort_members', array('cohortid' => $cohortid, 'userid' => $userid))) {
472 $warning = array();
473 $warning['warningcode'] = '3';
474 $warning['message'] = 'record already exists: cohort('.$cohorttype['type'].'='.$cohorttype['value'].' '.
475 $usertype['type'].'='.$usertype['value'].')';
476 $warnings[] = $warning;
477 continue;
479 $cohort = $DB->get_record('cohort', array('id'=>$cohortid), '*', MUST_EXIST);
480 $context = context::instance_by_id($cohort->contextid, MUST_EXIST);
481 if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
482 $warning = array();
483 $warning['warningcode'] = '1';
484 $warning['message'] = 'Invalid context: '.$context->contextlevel;
485 $warnings[] = $warning;
486 continue;
488 self::validate_context($context);
489 } catch (Exception $e) {
490 throw new moodle_exception('Error', 'cohort', '', $e->getMessage());
492 if (!has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:assign'), $context)) {
493 throw new required_capability_exception($context, 'moodle/cohort:assign', 'nopermissions', '');
495 cohort_add_member($cohortid, $userid);
497 $transaction->allow_commit();
498 // Return.
499 $result = array();
500 $result['warnings'] = $warnings;
501 return $result;
505 * Returns description of method result value
507 * @return null
508 * @since Moodle 2.5
510 public static function add_cohort_members_returns() {
511 return new external_single_structure(
512 array(
513 'warnings' => new external_warnings()
519 * Returns description of method parameters
521 * @return external_function_parameters
522 * @since Moodle 2.5
524 public static function delete_cohort_members_parameters() {
525 return new external_function_parameters(
526 array(
527 'members' => new external_multiple_structure(
528 new external_single_structure(
529 array(
530 'cohortid' => new external_value(PARAM_INT, 'cohort record id'),
531 'userid' => new external_value(PARAM_INT, 'user id'),
540 * Delete cohort members
542 * @param array $members of arrays with keys userid, cohortid
543 * @since Moodle 2.5
545 public static function delete_cohort_members($members) {
546 global $CFG, $DB;
547 require_once("$CFG->dirroot/cohort/lib.php");
549 // Validate parameters.
550 $params = self::validate_parameters(self::delete_cohort_members_parameters(), array('members' => $members));
552 $transaction = $DB->start_delegated_transaction();
554 foreach ($params['members'] as $member) {
555 $cohortid = $member['cohortid'];
556 $userid = $member['userid'];
558 $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST);
559 $user = $DB->get_record('user', array('id' => $userid, 'deleted' => 0, 'mnethostid' => $CFG->mnet_localhost_id),
560 '*', MUST_EXIST);
562 // Now security checks.
563 $context = context::instance_by_id($cohort->contextid, MUST_EXIST);
564 if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
565 throw new invalid_parameter_exception('Invalid context');
567 self::validate_context($context);
568 if (!has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:assign'), $context)) {
569 throw new required_capability_exception($context, 'moodle/cohort:assign', 'nopermissions', '');
572 cohort_remove_member($cohort->id, $user->id);
574 $transaction->allow_commit();
578 * Returns description of method result value
580 * @return null
581 * @since Moodle 2.5
583 public static function delete_cohort_members_returns() {
584 return null;
588 * Returns description of method parameters
590 * @return external_function_parameters
591 * @since Moodle 2.5
593 public static function get_cohort_members_parameters() {
594 return new external_function_parameters(
595 array(
596 'cohortids' => new external_multiple_structure(new external_value(PARAM_INT, 'Cohort ID')),
602 * Return all members for a cohort
604 * @param array $cohortids array of cohort ids
605 * @return array with cohort id keys containing arrays of user ids
606 * @since Moodle 2.5
608 public static function get_cohort_members($cohortids) {
609 global $DB;
610 $params = self::validate_parameters(self::get_cohort_members_parameters(), array('cohortids' => $cohortids));
612 $members = array();
614 foreach ($params['cohortids'] as $cohortid) {
615 // Validate params.
616 $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST);
617 // Now security checks.
618 $context = context::instance_by_id($cohort->contextid, MUST_EXIST);
619 if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
620 throw new invalid_parameter_exception('Invalid context');
622 self::validate_context($context);
623 if (!has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:view'), $context)) {
624 throw new required_capability_exception($context, 'moodle/cohort:view', 'nopermissions', '');
627 $cohortmembers = $DB->get_records_sql("SELECT u.id FROM {user} u, {cohort_members} cm
628 WHERE u.id = cm.userid AND cm.cohortid = ?
629 ORDER BY lastname ASC, firstname ASC", array($cohort->id));
630 $members[] = array('cohortid' => $cohortid, 'userids' => array_keys($cohortmembers));
632 return $members;
636 * Returns description of method result value
638 * @return external_description
639 * @since Moodle 2.5
641 public static function get_cohort_members_returns() {
642 return new external_multiple_structure(
643 new external_single_structure(
644 array(
645 'cohortid' => new external_value(PARAM_INT, 'cohort record id'),
646 'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user id')),