MDL-29670 add more timeout resets
[moodle.git] / cohort / lib.php
blobe33f77aed031db3f942ed9a816c1fce002599b72
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 * 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 = '') {
204 global $DB;
206 $cohorts = array();
208 // Add some additional sensible conditions
209 $tests = array('contextid = ?');
210 $params = array($contextid);
212 if (!empty($search)) {
213 $conditions = array(
214 'name',
215 'idnumber',
216 'description',
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 {
242 protected $cohortid;
244 public function __construct($name, $options) {
245 $this->cohortid = $options['cohortid'];
246 parent::__construct($name, $options);
250 * Candidate users
251 * @param <type> $search
252 * @return array
254 public function find_users($search) {
255 global $DB;
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)) {
279 return array();
283 if ($search) {
284 $groupname = get_string('potusersmatching', 'cohort', $search);
285 } else {
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';
296 return $options;
301 * Cohort assignment candidates
303 class cohort_existing_selector extends user_selector_base {
304 protected $cohortid;
306 public function __construct($name, $options) {
307 $this->cohortid = $options['cohortid'];
308 parent::__construct($name, $options);
312 * Candidate users
313 * @param <type> $search
314 * @return array
316 public function find_users($search) {
317 global $DB;
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)) {
341 return array();
345 if ($search) {
346 $groupname = get_string('currentusersmatching', 'cohort', $search);
347 } else {
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';
358 return $options;