2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
20 * @package core_cohort
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
{
33 * Returns description of method parameters
35 * @return external_function_parameters
38 public static function create_cohorts_parameters() {
39 return new external_function_parameters(
41 'cohorts' => new external_multiple_structure(
42 new external_single_structure(
44 'categorytype' => new external_single_structure(
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),
57 'theme' => new external_value(PARAM_THEME
,
58 'the cohort theme. The allowcohortthemes setting must be enabled on Moodle',
69 * Create one or more cohorts
71 * @param array $cohorts An array of cohorts to create.
72 * @return array An array of arrays
75 public static function create_cohorts($cohorts) {
77 require_once("$CFG->dirroot/cohort/lib.php");
79 $params = self
::validate_parameters(self
::create_cohorts_parameters(), array('cohorts' => $cohorts));
81 $availablethemes = cohort_get_list_of_themes();
83 $transaction = $DB->start_delegated_transaction();
85 $syscontext = context_system
::instance();
88 foreach ($params['cohorts'] as $cohort) {
89 $cohort = (object)$cohort;
91 // Category type (context id).
92 $categorytype = $cohort->categorytype
;
93 if (!in_array($categorytype['type'], array('idnumber', 'id', 'system'))) {
94 throw new invalid_parameter_exception('category type must be id, idnumber or system:' . $categorytype['type']);
96 if ($categorytype['type'] === 'system') {
97 $cohort->contextid
= $syscontext->id
;
98 } else if ($catid = $DB->get_field('course_categories', 'id', array($categorytype['type'] => $categorytype['value']))) {
99 $catcontext = context_coursecat
::instance($catid);
100 $cohort->contextid
= $catcontext->id
;
102 throw new invalid_parameter_exception('category not exists: category '
103 .$categorytype['type'].' = '.$categorytype['value']);
105 // Make sure that the idnumber doesn't already exist.
106 if ($DB->record_exists('cohort', array('idnumber' => $cohort->idnumber
))) {
107 throw new invalid_parameter_exception('record already exists: idnumber='.$cohort->idnumber
);
109 $context = context
::instance_by_id($cohort->contextid
, MUST_EXIST
);
110 if ($context->contextlevel
!= CONTEXT_COURSECAT
and $context->contextlevel
!= CONTEXT_SYSTEM
) {
111 throw new invalid_parameter_exception('Invalid context');
113 self
::validate_context($context);
114 require_capability('moodle/cohort:manage', $context);
116 // Make sure theme is valid.
117 if (isset($cohort->theme
)) {
118 if (!empty($CFG->allowcohortthemes
)) {
119 if (empty($availablethemes[$cohort->theme
])) {
120 throw new moodle_exception('errorinvalidparam', 'webservice', '', 'theme');
126 $cohort->descriptionformat
= external_validate_format($cohort->descriptionformat
);
127 $cohort->id
= cohort_add_cohort($cohort);
129 list($cohort->description
, $cohort->descriptionformat
) =
130 external_format_text($cohort->description
, $cohort->descriptionformat
,
131 $context->id
, 'cohort', 'description', $cohort->id
);
132 $cohortids[] = (array)$cohort;
134 $transaction->allow_commit();
140 * Returns description of method result value
142 * @return external_description
145 public static function create_cohorts_returns() {
146 return new external_multiple_structure(
147 new external_single_structure(
149 'id' => new external_value(PARAM_INT
, 'cohort id'),
150 'name' => new external_value(PARAM_RAW
, 'cohort name'),
151 'idnumber' => new external_value(PARAM_RAW
, 'cohort idnumber'),
152 'description' => new external_value(PARAM_RAW
, 'cohort description'),
153 'descriptionformat' => new external_format_value('description'),
154 'visible' => new external_value(PARAM_BOOL
, 'cohort visible'),
155 'theme' => new external_value(PARAM_THEME
, 'cohort theme', VALUE_OPTIONAL
),
162 * Returns description of method parameters
164 * @return external_function_parameters
167 public static function delete_cohorts_parameters() {
168 return new external_function_parameters(
170 'cohortids' => new external_multiple_structure(new external_value(PARAM_INT
, 'cohort ID')),
178 * @param array $cohortids
182 public static function delete_cohorts($cohortids) {
184 require_once("$CFG->dirroot/cohort/lib.php");
186 $params = self
::validate_parameters(self
::delete_cohorts_parameters(), array('cohortids' => $cohortids));
188 $transaction = $DB->start_delegated_transaction();
190 foreach ($params['cohortids'] as $cohortid) {
192 $cohortid = validate_param($cohortid, PARAM_INT
);
193 $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST
);
195 // Now security checks.
196 $context = context
::instance_by_id($cohort->contextid
, MUST_EXIST
);
197 if ($context->contextlevel
!= CONTEXT_COURSECAT
and $context->contextlevel
!= CONTEXT_SYSTEM
) {
198 throw new invalid_parameter_exception('Invalid context');
200 self
::validate_context($context);
201 require_capability('moodle/cohort:manage', $context);
202 cohort_delete_cohort($cohort);
204 $transaction->allow_commit();
210 * Returns description of method result value
215 public static function delete_cohorts_returns() {
220 * Returns description of method parameters
222 * @return external_function_parameters
225 public static function get_cohorts_parameters() {
226 return new external_function_parameters(
228 'cohortids' => new external_multiple_structure(new external_value(PARAM_INT
, 'Cohort ID')
229 , 'List of cohort id. A cohort id is an integer.', VALUE_DEFAULT
, array()),
235 * Get cohorts definition specified by ids
237 * @param array $cohortids array of cohort ids
238 * @return array of cohort objects (id, courseid, name)
241 public static function get_cohorts($cohortids = array()) {
244 $params = self
::validate_parameters(self
::get_cohorts_parameters(), array('cohortids' => $cohortids));
246 if (empty($cohortids)) {
247 $cohorts = $DB->get_records('cohort');
249 $cohorts = $DB->get_records_list('cohort', 'id', $params['cohortids']);
252 $cohortsinfo = array();
253 foreach ($cohorts as $cohort) {
254 // Now security checks.
255 $context = context
::instance_by_id($cohort->contextid
, MUST_EXIST
);
256 if ($context->contextlevel
!= CONTEXT_COURSECAT
and $context->contextlevel
!= CONTEXT_SYSTEM
) {
257 throw new invalid_parameter_exception('Invalid context');
259 self
::validate_context($context);
260 if (!has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:view'), $context)) {
261 throw new required_capability_exception($context, 'moodle/cohort:view', 'nopermissions', '');
264 // Only return theme when $CFG->allowcohortthemes is enabled.
265 if (!empty($cohort->theme
) && empty($CFG->allowcohortthemes
)) {
266 $cohort->theme
= null;
269 list($cohort->description
, $cohort->descriptionformat
) =
270 external_format_text($cohort->description
, $cohort->descriptionformat
,
271 $context->id
, 'cohort', 'description', $cohort->id
);
273 $cohortsinfo[] = (array) $cohort;
280 * Returns description of method result value
282 * @return external_description
285 public static function get_cohorts_returns() {
286 return new external_multiple_structure(
287 new external_single_structure(
289 'id' => new external_value(PARAM_INT
, 'ID of the cohort'),
290 'name' => new external_value(PARAM_RAW
, 'cohort name'),
291 'idnumber' => new external_value(PARAM_RAW
, 'cohort idnumber'),
292 'description' => new external_value(PARAM_RAW
, 'cohort description'),
293 'descriptionformat' => new external_format_value('description'),
294 'visible' => new external_value(PARAM_BOOL
, 'cohort visible'),
295 'theme' => new external_value(PARAM_THEME
, 'cohort theme', VALUE_OPTIONAL
),
302 * Returns the description of external function parameters.
304 * @return external_function_parameters
306 public static function search_cohorts_parameters() {
307 $query = new external_value(
311 $includes = new external_value(
313 'What other contexts to fetch the frameworks from. (all, parents, self)',
317 $limitfrom = new external_value(
319 'limitfrom we are fetching the records from',
323 $limitnum = new external_value(
325 'Number of records to fetch',
329 return new external_function_parameters(array(
331 'context' => self
::get_context_parameters(),
332 'includes' => $includes,
333 'limitfrom' => $limitfrom,
334 'limitnum' => $limitnum
341 * @param string $query
342 * @param array $context
343 * @param string $includes
344 * @param int $limitfrom
345 * @param int $limitnum
348 public static function search_cohorts($query, $context, $includes = 'parents', $limitfrom = 0, $limitnum = 25) {
350 require_once($CFG->dirroot
. '/cohort/lib.php');
352 $params = self
::validate_parameters(self
::search_cohorts_parameters(), array(
354 'context' => $context,
355 'includes' => $includes,
356 'limitfrom' => $limitfrom,
357 'limitnum' => $limitnum,
359 $query = $params['query'];
360 $includes = $params['includes'];
361 $context = self
::get_context_from_params($params['context']);
362 $limitfrom = $params['limitfrom'];
363 $limitnum = $params['limitnum'];
365 self
::validate_context($context);
367 $manager = has_capability('moodle/cohort:manage', $context);
369 require_capability('moodle/cohort:view', $context);
372 // TODO Make this more efficient.
373 if ($includes == 'self') {
374 $results = cohort_get_cohorts($context->id
, $limitfrom, $limitnum, $query);
375 $results = $results['cohorts'];
376 } else if ($includes == 'parents') {
377 $results = cohort_get_cohorts($context->id
, $limitfrom, $limitnum, $query);
378 $results = $results['cohorts'];
379 if (!$context instanceof context_system
) {
380 $results = array_merge($results, cohort_get_available_cohorts($context, COHORT_ALL
, $limitfrom, $limitnum, $query));
382 } else if ($includes == 'all') {
383 $results = cohort_get_all_cohorts($limitfrom, $limitnum, $query);
384 $results = $results['cohorts'];
386 throw new coding_exception('Invalid parameter value for \'includes\'.');
390 foreach ($results as $key => $cohort) {
391 $cohortcontext = context
::instance_by_id($cohort->contextid
);
393 // Only return theme when $CFG->allowcohortthemes is enabled.
394 if (!empty($cohort->theme
) && empty($CFG->allowcohortthemes
)) {
395 $cohort->theme
= null;
398 if (!isset($cohort->description
)) {
399 $cohort->description
= '';
401 if (!isset($cohort->descriptionformat
)) {
402 $cohort->descriptionformat
= FORMAT_PLAIN
;
405 list($cohort->description
, $cohort->descriptionformat
) =
406 external_format_text($cohort->description
, $cohort->descriptionformat
,
407 $cohortcontext->id
, 'cohort', 'description', $cohort->id
);
409 $cohorts[$key] = $cohort;
412 return array('cohorts' => $cohorts);
416 * Returns description of external function result value.
418 * @return external_description
420 public static function search_cohorts_returns() {
421 return new external_single_structure(array(
422 'cohorts' => new external_multiple_structure(
423 new external_single_structure(array(
424 'id' => new external_value(PARAM_INT
, 'ID of the cohort'),
425 'name' => new external_value(PARAM_RAW
, 'cohort name'),
426 'idnumber' => new external_value(PARAM_RAW
, 'cohort idnumber'),
427 'description' => new external_value(PARAM_RAW
, 'cohort description'),
428 'descriptionformat' => new external_format_value('description'),
429 'visible' => new external_value(PARAM_BOOL
, 'cohort visible'),
430 'theme' => new external_value(PARAM_THEME
, 'cohort theme', VALUE_OPTIONAL
),
439 * Returns description of method parameters
441 * @return external_function_parameters
444 public static function update_cohorts_parameters() {
445 return new external_function_parameters(
447 'cohorts' => new external_multiple_structure(
448 new external_single_structure(
450 'id' => new external_value(PARAM_INT
, 'ID of the cohort'),
451 'categorytype' => new external_single_structure(
453 'type' => new external_value(PARAM_TEXT
, 'the name of the field: id (numeric value
454 of course category id) or idnumber (alphanumeric value of idnumber course category)
455 or system (value ignored)'),
456 'value' => new external_value(PARAM_RAW
, 'the value of the categorytype')
459 'name' => new external_value(PARAM_RAW
, 'cohort name'),
460 'idnumber' => new external_value(PARAM_RAW
, 'cohort idnumber'),
461 'description' => new external_value(PARAM_RAW
, 'cohort description', VALUE_OPTIONAL
),
462 'descriptionformat' => new external_format_value('description', VALUE_DEFAULT
),
463 'visible' => new external_value(PARAM_BOOL
, 'cohort visible', VALUE_OPTIONAL
),
464 'theme' => new external_value(PARAM_THEME
,
465 'the cohort theme. The allowcohortthemes setting must be enabled on Moodle',
478 * @param array $cohorts
482 public static function update_cohorts($cohorts) {
484 require_once("$CFG->dirroot/cohort/lib.php");
486 $params = self
::validate_parameters(self
::update_cohorts_parameters(), array('cohorts' => $cohorts));
488 $availablethemes = cohort_get_list_of_themes();
490 $transaction = $DB->start_delegated_transaction();
491 $syscontext = context_system
::instance();
493 foreach ($params['cohorts'] as $cohort) {
494 $cohort = (object) $cohort;
496 if (trim($cohort->name
) == '') {
497 throw new invalid_parameter_exception('Invalid cohort name');
500 $oldcohort = $DB->get_record('cohort', array('id' => $cohort->id
), '*', MUST_EXIST
);
501 $oldcontext = context
::instance_by_id($oldcohort->contextid
, MUST_EXIST
);
502 require_capability('moodle/cohort:manage', $oldcontext);
504 // Category type (context id).
505 $categorytype = $cohort->categorytype
;
506 if (!in_array($categorytype['type'], array('idnumber', 'id', 'system'))) {
507 throw new invalid_parameter_exception('category type must be id, idnumber or system:' . $categorytype['type']);
509 if ($categorytype['type'] === 'system') {
510 $cohort->contextid
= $syscontext->id
;
511 } else if ($catid = $DB->get_field('course_categories', 'id', array($categorytype['type'] => $categorytype['value']))) {
512 $cohort->contextid
= $DB->get_field('context', 'id', array('instanceid' => $catid,
513 'contextlevel' => CONTEXT_COURSECAT
));
515 throw new invalid_parameter_exception('category not exists: category='.$categorytype['value']);
518 if ($cohort->contextid
!= $oldcohort->contextid
) {
519 $context = context
::instance_by_id($cohort->contextid
, MUST_EXIST
);
520 if ($context->contextlevel
!= CONTEXT_COURSECAT
and $context->contextlevel
!= CONTEXT_SYSTEM
) {
521 throw new invalid_parameter_exception('Invalid context');
524 self
::validate_context($context);
525 require_capability('moodle/cohort:manage', $context);
528 // Make sure theme is valid.
529 if (!empty($cohort->theme
) && !empty($CFG->allowcohortthemes
)) {
530 if (empty($availablethemes[$cohort->theme
])) {
531 $debuginfo = 'The following cohort theme is not installed on this site: '.$cohort->theme
;
532 throw new moodle_exception('errorinvalidparam', 'webservice', '', 'theme', $debuginfo);
536 if (!empty($cohort->description
)) {
537 $cohort->descriptionformat
= external_validate_format($cohort->descriptionformat
);
540 cohort_update_cohort($cohort);
543 $transaction->allow_commit();
549 * Returns description of method result value
554 public static function update_cohorts_returns() {
559 * Returns description of method parameters
561 * @return external_function_parameters
564 public static function add_cohort_members_parameters() {
565 return new external_function_parameters (
567 'members' => new external_multiple_structure (
568 new external_single_structure (
570 'cohorttype' => new external_single_structure (
572 'type' => new external_value(PARAM_ALPHANUMEXT
, 'The name of the field: id
573 (numeric value of cohortid) or idnumber (alphanumeric value of idnumber) '),
574 'value' => new external_value(PARAM_RAW
, 'The value of the cohort')
577 'usertype' => new external_single_structure (
579 'type' => new external_value(PARAM_ALPHANUMEXT
, 'The name of the field: id
580 (numeric value of id) or username (alphanumeric value of username) '),
581 'value' => new external_value(PARAM_RAW
, 'The value of the cohort')
594 * @param array $members of arrays with keys userid, cohortid
597 public static function add_cohort_members($members) {
599 require_once($CFG->dirroot
."/cohort/lib.php");
601 $params = self
::validate_parameters(self
::add_cohort_members_parameters(), array('members' => $members));
603 $transaction = $DB->start_delegated_transaction();
605 foreach ($params['members'] as $member) {
606 // Cohort parameters.
607 $cohorttype = $member['cohorttype'];
608 $cohortparam = array($cohorttype['type'] => $cohorttype['value']);
610 $usertype = $member['usertype'];
611 $userparam = array($usertype['type'] => $usertype['value']);
614 if ($cohorttype['type'] != 'id' && $cohorttype['type'] != 'idnumber') {
616 $warning['warningcode'] = '1';
617 $warning['message'] = 'invalid parameter: cohortype='.$cohorttype['type'];
618 $warnings[] = $warning;
621 if ($usertype['type'] != 'id' && $usertype['type'] != 'username') {
623 $warning['warningcode'] = '1';
624 $warning['message'] = 'invalid parameter: usertype='.$usertype['type'];
625 $warnings[] = $warning;
628 // Extract parameters.
629 if (!$cohortid = $DB->get_field('cohort', 'id', $cohortparam)) {
631 $warning['warningcode'] = '2';
632 $warning['message'] = 'cohort '.$cohorttype['type'].'='.$cohorttype['value'].' not exists';
633 $warnings[] = $warning;
636 if (!$userid = $DB->get_field('user', 'id', array_merge($userparam, array('deleted' => 0,
637 'mnethostid' => $CFG->mnet_localhost_id
)))) {
639 $warning['warningcode'] = '2';
640 $warning['message'] = 'user '.$usertype['type'].'='.$usertype['value'].' not exists';
641 $warnings[] = $warning;
644 if ($DB->record_exists('cohort_members', array('cohortid' => $cohortid, 'userid' => $userid))) {
646 $warning['warningcode'] = '3';
647 $warning['message'] = 'record already exists: cohort('.$cohorttype['type'].'='.$cohorttype['value'].' '.
648 $usertype['type'].'='.$usertype['value'].')';
649 $warnings[] = $warning;
652 $cohort = $DB->get_record('cohort', array('id'=>$cohortid), '*', MUST_EXIST
);
653 $context = context
::instance_by_id($cohort->contextid
, MUST_EXIST
);
654 if ($context->contextlevel
!= CONTEXT_COURSECAT
and $context->contextlevel
!= CONTEXT_SYSTEM
) {
656 $warning['warningcode'] = '1';
657 $warning['message'] = 'Invalid context: '.$context->contextlevel
;
658 $warnings[] = $warning;
661 self
::validate_context($context);
662 } catch (Exception
$e) {
663 throw new moodle_exception('Error', 'cohort', '', $e->getMessage());
665 if (!has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:assign'), $context)) {
666 throw new required_capability_exception($context, 'moodle/cohort:assign', 'nopermissions', '');
668 cohort_add_member($cohortid, $userid);
670 $transaction->allow_commit();
673 $result['warnings'] = $warnings;
678 * Returns description of method result value
683 public static function add_cohort_members_returns() {
684 return new external_single_structure(
686 'warnings' => new external_warnings()
692 * Returns description of method parameters
694 * @return external_function_parameters
697 public static function delete_cohort_members_parameters() {
698 return new external_function_parameters(
700 'members' => new external_multiple_structure(
701 new external_single_structure(
703 'cohortid' => new external_value(PARAM_INT
, 'cohort record id'),
704 'userid' => new external_value(PARAM_INT
, 'user id'),
713 * Delete cohort members
715 * @param array $members of arrays with keys userid, cohortid
718 public static function delete_cohort_members($members) {
720 require_once("$CFG->dirroot/cohort/lib.php");
722 // Validate parameters.
723 $params = self
::validate_parameters(self
::delete_cohort_members_parameters(), array('members' => $members));
725 $transaction = $DB->start_delegated_transaction();
727 foreach ($params['members'] as $member) {
728 $cohortid = $member['cohortid'];
729 $userid = $member['userid'];
731 $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST
);
732 $user = $DB->get_record('user', array('id' => $userid, 'deleted' => 0, 'mnethostid' => $CFG->mnet_localhost_id
),
735 // Now security checks.
736 $context = context
::instance_by_id($cohort->contextid
, MUST_EXIST
);
737 if ($context->contextlevel
!= CONTEXT_COURSECAT
and $context->contextlevel
!= CONTEXT_SYSTEM
) {
738 throw new invalid_parameter_exception('Invalid context');
740 self
::validate_context($context);
741 if (!has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:assign'), $context)) {
742 throw new required_capability_exception($context, 'moodle/cohort:assign', 'nopermissions', '');
745 cohort_remove_member($cohort->id
, $user->id
);
747 $transaction->allow_commit();
751 * Returns description of method result value
756 public static function delete_cohort_members_returns() {
761 * Returns description of method parameters
763 * @return external_function_parameters
766 public static function get_cohort_members_parameters() {
767 return new external_function_parameters(
769 'cohortids' => new external_multiple_structure(new external_value(PARAM_INT
, 'Cohort ID')),
775 * Return all members for a cohort
777 * @param array $cohortids array of cohort ids
778 * @return array with cohort id keys containing arrays of user ids
781 public static function get_cohort_members($cohortids) {
783 $params = self
::validate_parameters(self
::get_cohort_members_parameters(), array('cohortids' => $cohortids));
787 foreach ($params['cohortids'] as $cohortid) {
789 $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST
);
790 // Now security checks.
791 $context = context
::instance_by_id($cohort->contextid
, MUST_EXIST
);
792 if ($context->contextlevel
!= CONTEXT_COURSECAT
and $context->contextlevel
!= CONTEXT_SYSTEM
) {
793 throw new invalid_parameter_exception('Invalid context');
795 self
::validate_context($context);
796 if (!has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:view'), $context)) {
797 throw new required_capability_exception($context, 'moodle/cohort:view', 'nopermissions', '');
800 $cohortmembers = $DB->get_records_sql("SELECT u.id FROM {user} u, {cohort_members} cm
801 WHERE u.id = cm.userid AND cm.cohortid = ?
802 ORDER BY lastname ASC, firstname ASC", array($cohort->id
));
803 $members[] = array('cohortid' => $cohortid, 'userids' => array_keys($cohortmembers));
809 * Returns description of method result value
811 * @return external_description
814 public static function get_cohort_members_returns() {
815 return new external_multiple_structure(
816 new external_single_structure(
818 'cohortid' => new external_value(PARAM_INT
, 'cohort record id'),
819 'userids' => new external_multiple_structure(new external_value(PARAM_INT
, 'user id')),