MDL-38878 behat: Also update composer dependencies after site install/reinstall
[moodle.git] / cohort / lib.php
blob61d56c955ce324752959d29baa7588db6cfc95c7
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 * Cohort related management functions, this file needs to be included manually.
20 * @package core_cohort
21 * @copyright 2010 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Add new cohort.
30 * @param stdClass $cohort
31 * @return int new cohort id
33 function cohort_add_cohort($cohort) {
34 global $DB;
36 if (!isset($cohort->name)) {
37 throw new coding_exception('Missing cohort name in cohort_add_cohort().');
39 if (!isset($cohort->idnumber)) {
40 $cohort->idnumber = NULL;
42 if (!isset($cohort->description)) {
43 $cohort->description = '';
45 if (!isset($cohort->descriptionformat)) {
46 $cohort->descriptionformat = FORMAT_HTML;
48 if (empty($cohort->component)) {
49 $cohort->component = '';
51 if (!isset($cohort->timecreated)) {
52 $cohort->timecreated = time();
54 if (!isset($cohort->timemodified)) {
55 $cohort->timemodified = $cohort->timecreated;
58 $cohort->id = $DB->insert_record('cohort', $cohort);
60 events_trigger('cohort_added', $cohort);
62 return $cohort->id;
65 /**
66 * Update existing cohort.
67 * @param stdClass $cohort
68 * @return void
70 function cohort_update_cohort($cohort) {
71 global $DB;
72 if (property_exists($cohort, 'component') and empty($cohort->component)) {
73 // prevent NULLs
74 $cohort->component = '';
76 $cohort->timemodified = time();
77 $DB->update_record('cohort', $cohort);
79 events_trigger('cohort_updated', $cohort);
82 /**
83 * Delete cohort.
84 * @param stdClass $cohort
85 * @return void
87 function cohort_delete_cohort($cohort) {
88 global $DB;
90 if ($cohort->component) {
91 // TODO: add component delete callback
94 $DB->delete_records('cohort_members', array('cohortid'=>$cohort->id));
95 $DB->delete_records('cohort', array('id'=>$cohort->id));
97 events_trigger('cohort_deleted', $cohort);
101 * Somehow deal with cohorts when deleting course category,
102 * we can not just delete them because they might be used in enrol
103 * plugins or referenced in external systems.
104 * @param stdClass|coursecat $category
105 * @return void
107 function cohort_delete_category($category) {
108 global $DB;
109 // TODO: make sure that cohorts are really, really not used anywhere and delete, for now just move to parent or system context
111 $oldcontext = context_coursecat::instance($category->id);
113 if ($category->parent and $parent = $DB->get_record('course_categories', array('id'=>$category->parent))) {
114 $parentcontext = context_coursecat::instance($parent->id);
115 $sql = "UPDATE {cohort} SET contextid = :newcontext WHERE contextid = :oldcontext";
116 $params = array('oldcontext'=>$oldcontext->id, 'newcontext'=>$parentcontext->id);
117 } else {
118 $syscontext = context_system::instance();
119 $sql = "UPDATE {cohort} SET contextid = :newcontext WHERE contextid = :oldcontext";
120 $params = array('oldcontext'=>$oldcontext->id, 'newcontext'=>$syscontext->id);
123 $DB->execute($sql, $params);
127 * Add cohort member
128 * @param int $cohortid
129 * @param int $userid
130 * @return void
132 function cohort_add_member($cohortid, $userid) {
133 global $DB;
134 if ($DB->record_exists('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid))) {
135 // No duplicates!
136 return;
138 $record = new stdClass();
139 $record->cohortid = $cohortid;
140 $record->userid = $userid;
141 $record->timeadded = time();
142 $DB->insert_record('cohort_members', $record);
144 events_trigger('cohort_member_added', (object)array('cohortid'=>$cohortid, 'userid'=>$userid));
148 * Remove cohort member
149 * @param int $cohortid
150 * @param int $userid
151 * @return void
153 function cohort_remove_member($cohortid, $userid) {
154 global $DB;
155 $DB->delete_records('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid));
157 events_trigger('cohort_member_removed', (object)array('cohortid'=>$cohortid, 'userid'=>$userid));
161 * Is this user a cohort member?
162 * @param int $cohortid
163 * @param int $userid
164 * @return bool
166 function cohort_is_member($cohortid, $userid) {
167 global $DB;
169 return $DB->record_exists('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid));
173 * Returns list of cohorts from course parent contexts.
175 * Note: this function does not implement any capability checks,
176 * it means it may disclose existence of cohorts,
177 * make sure it is displayed to users with appropriate rights only.
179 * @param stdClass $course
180 * @param bool $onlyenrolled true means include only cohorts with enrolled users
181 * @return array of cohort names with number of enrolled users
183 function cohort_get_visible_list($course, $onlyenrolled=true) {
184 global $DB;
186 $context = context_course::instance($course->id);
187 list($esql, $params) = get_enrolled_sql($context);
188 list($parentsql, $params2) = $DB->get_in_or_equal($context->get_parent_context_ids(), SQL_PARAMS_NAMED);
189 $params = array_merge($params, $params2);
191 if ($onlyenrolled) {
192 $left = "";
193 $having = "HAVING COUNT(u.id) > 0";
194 } else {
195 $left = "LEFT";
196 $having = "";
199 $sql = "SELECT c.id, c.name, c.contextid, c.idnumber, COUNT(u.id) AS cnt
200 FROM {cohort} c
201 $left JOIN ({cohort_members} cm
202 JOIN ($esql) u ON u.id = cm.userid) ON cm.cohortid = c.id
203 WHERE c.contextid $parentsql
204 GROUP BY c.id, c.name, c.contextid, c.idnumber
205 $having
206 ORDER BY c.name, c.idnumber";
208 $cohorts = $DB->get_records_sql($sql, $params);
210 foreach ($cohorts as $cid=>$cohort) {
211 $cohorts[$cid] = format_string($cohort->name, true, array('context'=>$cohort->contextid));
212 if ($cohort->cnt) {
213 $cohorts[$cid] .= ' (' . $cohort->cnt . ')';
217 return $cohorts;
221 * Get all the cohorts defined in given context.
223 * @param int $contextid
224 * @param int $page number of the current page
225 * @param int $perpage items per page
226 * @param string $search search string
227 * @return array Array(totalcohorts => int, cohorts => array, allcohorts => int)
229 function cohort_get_cohorts($contextid, $page = 0, $perpage = 25, $search = '') {
230 global $DB;
232 // Add some additional sensible conditions
233 $tests = array('contextid = ?');
234 $params = array($contextid);
236 if (!empty($search)) {
237 $conditions = array('name', 'idnumber', 'description');
238 $searchparam = '%' . $DB->sql_like_escape($search) . '%';
239 foreach ($conditions as $key=>$condition) {
240 $conditions[$key] = $DB->sql_like($condition, "?", false);
241 $params[] = $searchparam;
243 $tests[] = '(' . implode(' OR ', $conditions) . ')';
245 $wherecondition = implode(' AND ', $tests);
247 $fields = "SELECT *";
248 $countfields = "SELECT COUNT(1)";
249 $sql = " FROM {cohort}
250 WHERE $wherecondition";
251 $order = " ORDER BY name ASC, idnumber ASC";
252 $allcohorts = $DB->count_records('cohort', array('contextid'=>$contextid));
253 $totalcohorts = $DB->count_records_sql($countfields . $sql, $params);
254 $cohorts = $DB->get_records_sql($fields . $sql . $order, $params, $page*$perpage, $perpage);
256 return array('totalcohorts' => $totalcohorts, 'cohorts' => $cohorts, 'allcohorts'=>$allcohorts);