3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Cohort related management functions, this file needs to be included manually.
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');
32 * @param object $cohort
35 function cohort_add_cohort($cohort) {
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);
68 * Update existing cohort.
69 * @param object $cohort
72 function cohort_update_cohort($cohort) {
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);
85 * @param object $cohort
88 function cohort_delete_cohort($cohort) {
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
108 function cohort_delete_category($category) {
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
);
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
133 function cohort_add_member($cohortid, $userid) {
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));
146 * @param int $cohortid
150 function cohort_remove_member($cohortid, $userid) {
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
164 function cohort_get_visible_list($course) {
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
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
. ')';
194 * Get all the cohorts.
196 * @global moodle_database $DB
197 * @param int $contextid
198 * @param int $page number of the current page
199 * @param int $perpage items per page
200 * @param string $search search string
201 * @return array Array(totalcohorts => int, cohorts => array)
203 function cohort_get_cohorts($contextid, $page = 0, $perpage = 25, $search = '') {
208 // Add some additional sensible conditions
209 $tests = array('contextid = ?');
210 $params = array($contextid);
212 if (!empty($search)) {
218 $searchparam = '%' . $search . '%';
219 foreach ($conditions as $key=>$condition) {
220 $conditions[$key] = $DB->sql_like($condition,"?", false);
221 $params[] = $searchparam;
223 $tests[] = '(' . implode(' OR ', $conditions) . ')';
225 $wherecondition = implode(' AND ', $tests);
227 $fields = 'SELECT *';
228 $countfields = 'SELECT COUNT(1)';
229 $sql = " FROM {cohort}
230 WHERE $wherecondition";
231 $order = ' ORDER BY name ASC';
232 $totalcohorts = $DB->count_records_sql($countfields . $sql, $params);
233 $cohorts = $DB->get_records_sql($fields . $sql . $order, $params, $page*$perpage, $perpage);
235 return array('totalcohorts' => $totalcohorts, 'cohorts' => $cohorts);
239 * Cohort assignment candidates
241 class cohort_candidate_selector
extends user_selector_base
{
244 public function __construct($name, $options) {
245 $this->cohortid
= $options['cohortid'];
246 parent
::__construct($name, $options);
251 * @param <type> $search
254 public function find_users($search) {
256 //by default wherecondition retrieves all users except the deleted, not confirmed and guest
257 list($wherecondition, $params) = $this->search_sql($search, 'u');
258 $params['cohortid'] = $this->cohortid
;
260 $fields = 'SELECT ' . $this->required_fields_sql('u');
261 $countfields = 'SELECT COUNT(1)';
263 $sql = " FROM {user} u
264 LEFT JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
265 WHERE cm.id IS NULL AND $wherecondition";
267 $order = ' ORDER BY u.lastname ASC, u.firstname ASC';
269 if (!$this->is_validating()) {
270 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
271 if ($potentialmemberscount > 100) {
272 return $this->too_many_results($search, $potentialmemberscount);
276 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
278 if (empty($availableusers)) {
284 $groupname = get_string('potusersmatching', 'cohort', $search);
286 $groupname = get_string('potusers', 'cohort');
289 return array($groupname => $availableusers);
292 protected function get_options() {
293 $options = parent
::get_options();
294 $options['cohortid'] = $this->cohortid
;
295 $options['file'] = 'cohort/lib.php';
301 * Cohort assignment candidates
303 class cohort_existing_selector
extends user_selector_base
{
306 public function __construct($name, $options) {
307 $this->cohortid
= $options['cohortid'];
308 parent
::__construct($name, $options);
313 * @param <type> $search
316 public function find_users($search) {
318 //by default wherecondition retrieves all users except the deleted, not confirmed and guest
319 list($wherecondition, $params) = $this->search_sql($search, 'u');
320 $params['cohortid'] = $this->cohortid
;
322 $fields = 'SELECT ' . $this->required_fields_sql('u');
323 $countfields = 'SELECT COUNT(1)';
325 $sql = " FROM {user} u
326 JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
327 WHERE $wherecondition";
329 $order = ' ORDER BY u.lastname ASC, u.firstname ASC';
331 if (!$this->is_validating()) {
332 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
333 if ($potentialmemberscount > 100) {
334 return $this->too_many_results($search, $potentialmemberscount);
338 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
340 if (empty($availableusers)) {
346 $groupname = get_string('currentusersmatching', 'cohort', $search);
348 $groupname = get_string('currentusers', 'cohort');
351 return array($groupname => $availableusers);
354 protected function get_options() {
355 $options = parent
::get_options();
356 $options['cohortid'] = $this->cohortid
;
357 $options['file'] = 'cohort/lib.php';