weekly release 2.0.4+
[moodle.git] / cohort / lib.php
blob9a084eb63b7203d0365898106518be62fdc537ff
1 <?php
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.
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/>.
18 /**
19 * Cohort related management functions, this file needs to be included manually.
21 * @package core
22 * @subpackage cohort
23 * @copyright 2010 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once($CFG->dirroot . '/user/selector/lib.php');
29 /**
30 * Add new cohort.
32 * @param object $cohort
33 * @return int
35 function cohort_add_cohort($cohort) {
36 global $DB;
38 if (!isset($cohort->name)) {
39 throw new coding_exception('Missing cohort name in cohort_add_cohort().');
41 if (!isset($cohort->idnumber)) {
42 $cohort->idnumber = NULL;
44 if (!isset($cohort->description)) {
45 $cohort->description = $DB->sql_empty();
47 if (!isset($cohort->descriptionformat)) {
48 $cohort->descriptionformat = FORMAT_HTML;
50 if (empty($cohort->component)) {
51 $cohort->component = '';
53 if (!isset($cohort->timecreated)) {
54 $cohort->timecreated = time();
56 if (!isset($cohort->timemodified)) {
57 $cohort->timemodified = $cohort->timecreated;
60 $cohort->id = $DB->insert_record('cohort', $cohort);
62 events_trigger('cohort_added', $cohort);
64 return $cohort->id;
67 /**
68 * Update existing cohort.
69 * @param object $cohort
70 * @return void
72 function cohort_update_cohort($cohort) {
73 global $DB;
74 if (isset($cohort->component) and empty($cohort->component)) {
75 $cohort->component = NULL;
77 $cohort->timemodified = time();
78 $DB->update_record('cohort', $cohort);
80 events_trigger('cohort_updated', $cohort);
83 /**
84 * Delete cohort.
85 * @param object $cohort
86 * @return void
88 function cohort_delete_cohort($cohort) {
89 global $DB;
91 if ($cohort->component) {
92 // TODO: add component delete callback
95 $DB->delete_records('cohort_members', array('cohortid'=>$cohort->id));
96 $DB->delete_records('cohort', array('id'=>$cohort->id));
98 events_trigger('cohort_deleted', $cohort);
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
108 function 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
112 $oldcontext = get_context_instance(CONTEXT_COURSECAT, $category->id, MUST_EXIST);
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);
124 $DB->execute($sql, $params);
128 * Remove cohort member
129 * @param int $cohortid
130 * @param int $userid
131 * @return void
133 function cohort_add_member($cohortid, $userid) {
134 global $DB;
135 $record = new stdClass();
136 $record->cohortid = $cohortid;
137 $record->userid = $userid;
138 $record->timeadded = time();
139 $DB->insert_record('cohort_members', $record);
141 events_trigger('cohort_member_added', (object)array('cohortid'=>$cohortid, 'userid'=>$userid));
145 * Add cohort member
146 * @param int $cohortid
147 * @param int $userid
148 * @return void
150 function cohort_remove_member($cohortid, $userid) {
151 global $DB;
152 $DB->delete_records('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid));
154 events_trigger('cohort_member_removed', (object)array('cohortid'=>$cohortid, 'userid'=>$userid));
158 * Returns list of visible cohorts in course.
160 * @param object $course
161 * @param bool $enrolled true means include only cohorts with enrolled users
162 * @return array
164 function cohort_get_visible_list($course) {
165 global $DB, $USER;
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);
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;
181 $cohorts = $DB->get_records_sql($sql, $params);
183 foreach ($cohorts as $cid=>$cohort) {
184 $cohorts[$cid] = format_string($cohort->name);
185 if ($cohort->idnumber) {
186 $cohorts[$cid] .= ' (' . $cohort->cnt . ')';
190 return $cohorts;
194 * Cohort assignment candidates
196 class cohort_candidate_selector extends user_selector_base {
197 protected $cohortid;
199 public function __construct($name, $options) {
200 $this->cohortid = $options['cohortid'];
201 parent::__construct($name, $options);
205 * Candidate users
206 * @param <type> $search
207 * @return array
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;
215 $fields = 'SELECT ' . $this->required_fields_sql('u');
216 $countfields = 'SELECT COUNT(1)';
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";
222 $order = ' ORDER BY u.lastname ASC, u.firstname ASC';
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);
231 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
233 if (empty($availableusers)) {
234 return array();
238 if ($search) {
239 $groupname = get_string('potusersmatching', 'cohort', $search);
240 } else {
241 $groupname = get_string('potusers', 'cohort');
244 return array($groupname => $availableusers);
247 protected function get_options() {
248 $options = parent::get_options();
249 $options['cohortid'] = $this->cohortid;
250 $options['file'] = 'cohort/lib.php';
251 return $options;
256 * Cohort assignment candidates
258 class cohort_existing_selector extends user_selector_base {
259 protected $cohortid;
261 public function __construct($name, $options) {
262 $this->cohortid = $options['cohortid'];
263 parent::__construct($name, $options);
267 * Candidate users
268 * @param <type> $search
269 * @return array
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;
277 $fields = 'SELECT ' . $this->required_fields_sql('u');
278 $countfields = 'SELECT COUNT(1)';
280 $sql = " FROM {user} u
281 JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
282 WHERE $wherecondition";
284 $order = ' ORDER BY u.lastname ASC, u.firstname ASC';
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);
293 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
295 if (empty($availableusers)) {
296 return array();
300 if ($search) {
301 $groupname = get_string('currentusersmatching', 'cohort', $search);
302 } else {
303 $groupname = get_string('currentusers', 'cohort');
306 return array($groupname => $availableusers);
309 protected function get_options() {
310 $options = parent::get_options();
311 $options['cohortid'] = $this->cohortid;
312 $options['file'] = 'cohort/lib.php';
313 return $options;