MDL-24321 switching to stdClass in /c*/
[moodle.git] / cohort / lib.php
CommitLineData
b980c56e
PS
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Cohort related management functions, this file needs to be included manually.
20 *
707f4136 21 * @package core
b980c56e 22 * @subpackage cohort
707f4136 23 * @copyright 2010 Petr Skoda {@link http://skodak.org}
b980c56e
PS
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27require_once($CFG->dirroot . '/user/selector/lib.php');
28
29/**
30 * Add new cohort.
df997f84
PS
31 *
32 * @param object $cohort
33 * @return int
b980c56e 34 */
df997f84 35function cohort_add_cohort($cohort) {
b980c56e 36 global $DB;
df997f84
PS
37
38 if (!isset($cohort->name)) {
13f66945 39 throw new coding_exception('Missing cohort name in cohort_add_cohort().');
df997f84
PS
40 }
41 if (!isset($cohort->idnumber)) {
42 $cohort->idnumber = NULL;
43 }
44 if (!isset($cohort->description)) {
45 $cohort->description = $DB->sql_empty();
46 }
47 if (!isset($cohort->descriptionformat)) {
48 $cohort->descriptionformat = FORMAT_HTML;
49 }
50 if (empty($cohort->component)) {
51 $cohort->component = '';
52 }
53 if (!isset($cohort->timecreated)) {
54 $cohort->timecreated = time();
55 }
56 if (!isset($cohort->timemodified)) {
57 $cohort->timemodified = $cohort->timecreated;
58 }
59
60 $cohort->id = $DB->insert_record('cohort', $cohort);
61
62 events_trigger('cohort_added', $cohort);
63
64 return $cohort->id;
b980c56e
PS
65}
66
67/**
68 * Update existing cohort.
df997f84 69 * @param object $cohort
b980c56e
PS
70 * @return void
71 */
df997f84 72function cohort_update_cohort($cohort) {
b980c56e 73 global $DB;
df997f84
PS
74 if (isset($cohort->component) and empty($cohort->component)) {
75 $cohort->component = NULL;
76 }
77 $cohort->timemodified = time();
78 $DB->update_record('cohort', $cohort);
79
80 events_trigger('cohort_updated', $cohort);
b980c56e
PS
81}
82
83/**
84 * Delete cohort.
85 * @param object $cohort
86 * @return void
87 */
88function cohort_delete_cohort($cohort) {
89 global $DB;
90
91 if ($cohort->component) {
92 // TODO: add component delete callback
93 }
94
95 $DB->delete_records('cohort_members', array('cohortid'=>$cohort->id));
96 $DB->delete_records('cohort', array('id'=>$cohort->id));
df997f84
PS
97
98 events_trigger('cohort_deleted', $cohort);
b980c56e
PS
99}
100
e60ff536
PS
101/**
102 * Somehow deal with cohorts when deleting course category,
103 * we can not just delete them because they might be used in enrol
104 * plugins or referenced in external systems.
105 * @param object $category
106 * @return void
107 */
108function cohort_delete_category($category) {
109 global $DB;
110 // TODO: make sure that cohorts are really, really not used anywhere and delete, for now just move to parent or system context
111
112 $oldcontext = get_context_instance(CONTEXT_COURSECAT, $category->id, MUST_EXIST);
113
114 if ($category->parent and $parent = $DB->get_record('course_categories', array('id'=>$category->parent))) {
115 $parentcontext = get_context_instance(CONTEXT_COURSECAT, $parent->id, MUST_EXIST);
116 $sql = "UPDATE {cohort} SET contextid = :newcontext WHERE contextid = :oldcontext";
117 $params = array('oldcontext'=>$oldcontext->id, 'newcontext'=>$parentcontext->id);
118 } else {
119 $syscontext = get_context_instance(CONTEXT_SYSTEM);
120 $sql = "UPDATE {cohort} SET contextid = :newcontext WHERE contextid = :oldcontext";
121 $params = array('oldcontext'=>$oldcontext->id, 'newcontext'=>$syscontext->id);
122 }
123
124 $DB->execute($sql, $params);
125}
126
b980c56e
PS
127/**
128 * Remove cohort member
129 * @param int $cohortid
130 * @param int $userid
131 * @return void
132 */
133function cohort_add_member($cohortid, $userid) {
134 global $DB;
fbaea88f 135 $record = new stdClass();
b980c56e
PS
136 $record->cohortid = $cohortid;
137 $record->userid = $userid;
138 $record->timeadded = time();
139 $DB->insert_record('cohort_members', $record);
df997f84
PS
140
141 events_trigger('cohort_member_added', (object)array('cohortid'=>$cohortid, 'userid'=>$userid));
b980c56e
PS
142}
143
144/**
145 * Add cohort member
146 * @param int $cohortid
147 * @param int $userid
148 * @return void
149 */
150function cohort_remove_member($cohortid, $userid) {
151 global $DB;
152 $DB->delete_records('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid));
df997f84
PS
153
154 events_trigger('cohort_member_removed', (object)array('cohortid'=>$cohortid, 'userid'=>$userid));
b980c56e
PS
155}
156
92343cd2
PS
157/**
158 * Returns list of visible cohorts in course.
159 *
160 * @param object $course
161 * @param bool $enrolled true means include only cohorts with enrolled users
162 * @return array
163 */
164function cohort_get_visible_list($course) {
165 global $DB, $USER;
166
167 $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
168 list($esql, $params) = get_enrolled_sql($context);
169 $parentsql = get_related_contexts_string($context);
170
171 $sql = "SELECT c.id, c.name, c.idnumber, COUNT(u.id) AS cnt
172 FROM {cohort} c
173 JOIN {cohort_members} cm ON cm.cohortid = c.id
174 JOIN ($esql) u ON u.id = cm.userid
175 WHERE c.contextid $parentsql
176 GROUP BY c.id, c.name, c.idnumber
177 HAVING COUNT(u.id) > 0
178 ORDER BY c.name, c.idnumber";
179 $params['ctx'] = $context->id;
180
181 $cohorts = $DB->get_records_sql($sql, $params);
182
183 foreach ($cohorts as $cid=>$cohort) {
184 $cohorts[$cid] = format_string($cohort->name);
185 if ($cohort->idnumber) {
186 $cohorts[$cid] .= ' (' . $cohort->cnt . ')';
187 }
188 }
189
190 return $cohorts;
191}
192
b980c56e
PS
193/**
194 * Cohort assignment candidates
195 */
196class cohort_candidate_selector extends user_selector_base {
197 protected $cohortid;
198
199 public function __construct($name, $options) {
200 $this->cohortid = $options['cohortid'];
201 parent::__construct($name, $options);
202 }
203
204 /**
205 * Candidate users
206 * @param <type> $search
207 * @return array
208 */
209 public function find_users($search) {
210 global $DB;
211 //by default wherecondition retrieves all users except the deleted, not confirmed and guest
212 list($wherecondition, $params) = $this->search_sql($search, 'u');
213 $params['cohortid'] = $this->cohortid;
214
215 $fields = 'SELECT ' . $this->required_fields_sql('u');
216 $countfields = 'SELECT COUNT(1)';
217
218 $sql = " FROM {user} u
219 LEFT JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
220 WHERE cm.id IS NULL AND $wherecondition";
221
222 $order = ' ORDER BY u.lastname ASC, u.firstname ASC';
223
224 if (!$this->is_validating()) {
225 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
226 if ($potentialmemberscount > 100) {
227 return $this->too_many_results($search, $potentialmemberscount);
228 }
229 }
230
231 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
232
233 if (empty($availableusers)) {
234 return array();
235 }
236
237
238 if ($search) {
239 $groupname = get_string('potusersmatching', 'cohort', $search);
a174e4a3 240 } else {
b980c56e
PS
241 $groupname = get_string('potusers', 'cohort');
242 }
243
244 return array($groupname => $availableusers);
245 }
246
247 protected function get_options() {
248 $options = parent::get_options();
249 $options['cohortid'] = $this->cohortid;
b1e2e6b8 250 $options['file'] = 'cohort/lib.php';
b980c56e
PS
251 return $options;
252 }
253}
254
255/**
256 * Cohort assignment candidates
257 */
258class cohort_existing_selector extends user_selector_base {
259 protected $cohortid;
260
261 public function __construct($name, $options) {
262 $this->cohortid = $options['cohortid'];
263 parent::__construct($name, $options);
264 }
265
266 /**
267 * Candidate users
268 * @param <type> $search
269 * @return array
270 */
271 public function find_users($search) {
272 global $DB;
273 //by default wherecondition retrieves all users except the deleted, not confirmed and guest
274 list($wherecondition, $params) = $this->search_sql($search, 'u');
275 $params['cohortid'] = $this->cohortid;
276
277 $fields = 'SELECT ' . $this->required_fields_sql('u');
278 $countfields = 'SELECT COUNT(1)';
279
280 $sql = " FROM {user} u
281 JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
282 WHERE $wherecondition";
283
284 $order = ' ORDER BY u.lastname ASC, u.firstname ASC';
285
286 if (!$this->is_validating()) {
287 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
288 if ($potentialmemberscount > 100) {
289 return $this->too_many_results($search, $potentialmemberscount);
290 }
291 }
292
293 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
294
295 if (empty($availableusers)) {
296 return array();
297 }
298
299
300 if ($search) {
301 $groupname = get_string('currentusersmatching', 'cohort', $search);
a174e4a3 302 } else {
b980c56e
PS
303 $groupname = get_string('currentusers', 'cohort');
304 }
305
306 return array($groupname => $availableusers);
307 }
df997f84 308
b980c56e
PS
309 protected function get_options() {
310 $options = parent::get_options();
311 $options['cohortid'] = $this->cohortid;
b1e2e6b8 312 $options['file'] = 'cohort/lib.php';
b980c56e
PS
313 return $options;
314 }
315}