MDL-21781 fixed coding style
[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 *
21 * @package moodlecore
22 * @subpackage cohort
23 * @copyright 2010 Petr Skoda (info@skodak.org)
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.
31 * @param object $data
32 * @return void
33 */
34function cohort_add_cohort($data) {
35 global $DB;
36 $data->timecreated = time();
37 $data->timemodified = $data->timecreated;
38 $DB->insert_record('cohort', $data);
39}
40
41/**
42 * Update existing cohort.
43 * @param object $data
44 * @return void
45 */
46function cohort_update_cohort($data) {
47 global $DB;
48 $data->timemodified = time();
49 $DB->update_record('cohort', $data);
50}
51
52/**
53 * Delete cohort.
54 * @param object $cohort
55 * @return void
56 */
57function cohort_delete_cohort($cohort) {
58 global $DB;
59
60 if ($cohort->component) {
61 // TODO: add component delete callback
62 }
63
64 $DB->delete_records('cohort_members', array('cohortid'=>$cohort->id));
65 $DB->delete_records('cohort', array('id'=>$cohort->id));
66}
67
e60ff536
PS
68/**
69 * Somehow deal with cohorts when deleting course category,
70 * we can not just delete them because they might be used in enrol
71 * plugins or referenced in external systems.
72 * @param object $category
73 * @return void
74 */
75function cohort_delete_category($category) {
76 global $DB;
77 // TODO: make sure that cohorts are really, really not used anywhere and delete, for now just move to parent or system context
78
79 $oldcontext = get_context_instance(CONTEXT_COURSECAT, $category->id, MUST_EXIST);
80
81 if ($category->parent and $parent = $DB->get_record('course_categories', array('id'=>$category->parent))) {
82 $parentcontext = get_context_instance(CONTEXT_COURSECAT, $parent->id, MUST_EXIST);
83 $sql = "UPDATE {cohort} SET contextid = :newcontext WHERE contextid = :oldcontext";
84 $params = array('oldcontext'=>$oldcontext->id, 'newcontext'=>$parentcontext->id);
85 } else {
86 $syscontext = get_context_instance(CONTEXT_SYSTEM);
87 $sql = "UPDATE {cohort} SET contextid = :newcontext WHERE contextid = :oldcontext";
88 $params = array('oldcontext'=>$oldcontext->id, 'newcontext'=>$syscontext->id);
89 }
90
91 $DB->execute($sql, $params);
92}
93
b980c56e
PS
94/**
95 * Remove cohort member
96 * @param int $cohortid
97 * @param int $userid
98 * @return void
99 */
100function cohort_add_member($cohortid, $userid) {
101 global $DB;
102 $record = new object();
103 $record->cohortid = $cohortid;
104 $record->userid = $userid;
105 $record->timeadded = time();
106 $DB->insert_record('cohort_members', $record);
107}
108
109/**
110 * Add cohort member
111 * @param int $cohortid
112 * @param int $userid
113 * @return void
114 */
115function cohort_remove_member($cohortid, $userid) {
116 global $DB;
117 $DB->delete_records('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid));
118}
119
92343cd2
PS
120/**
121 * Returns list of visible cohorts in course.
122 *
123 * @param object $course
124 * @param bool $enrolled true means include only cohorts with enrolled users
125 * @return array
126 */
127function cohort_get_visible_list($course) {
128 global $DB, $USER;
129
130 $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
131 list($esql, $params) = get_enrolled_sql($context);
132 $parentsql = get_related_contexts_string($context);
133
134 $sql = "SELECT c.id, c.name, c.idnumber, COUNT(u.id) AS cnt
135 FROM {cohort} c
136 JOIN {cohort_members} cm ON cm.cohortid = c.id
137 JOIN ($esql) u ON u.id = cm.userid
138 WHERE c.contextid $parentsql
139 GROUP BY c.id, c.name, c.idnumber
140 HAVING COUNT(u.id) > 0
141 ORDER BY c.name, c.idnumber";
142 $params['ctx'] = $context->id;
143
144 $cohorts = $DB->get_records_sql($sql, $params);
145
146 foreach ($cohorts as $cid=>$cohort) {
147 $cohorts[$cid] = format_string($cohort->name);
148 if ($cohort->idnumber) {
149 $cohorts[$cid] .= ' (' . $cohort->cnt . ')';
150 }
151 }
152
153 return $cohorts;
154}
155
b980c56e
PS
156/**
157 * Cohort assignment candidates
158 */
159class cohort_candidate_selector extends user_selector_base {
160 protected $cohortid;
161
162 public function __construct($name, $options) {
163 $this->cohortid = $options['cohortid'];
164 parent::__construct($name, $options);
165 }
166
167 /**
168 * Candidate users
169 * @param <type> $search
170 * @return array
171 */
172 public function find_users($search) {
173 global $DB;
174 //by default wherecondition retrieves all users except the deleted, not confirmed and guest
175 list($wherecondition, $params) = $this->search_sql($search, 'u');
176 $params['cohortid'] = $this->cohortid;
177
178 $fields = 'SELECT ' . $this->required_fields_sql('u');
179 $countfields = 'SELECT COUNT(1)';
180
181 $sql = " FROM {user} u
182 LEFT JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
183 WHERE cm.id IS NULL AND $wherecondition";
184
185 $order = ' ORDER BY u.lastname ASC, u.firstname ASC';
186
187 if (!$this->is_validating()) {
188 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
189 if ($potentialmemberscount > 100) {
190 return $this->too_many_results($search, $potentialmemberscount);
191 }
192 }
193
194 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
195
196 if (empty($availableusers)) {
197 return array();
198 }
199
200
201 if ($search) {
202 $groupname = get_string('potusersmatching', 'cohort', $search);
a174e4a3 203 } else {
b980c56e
PS
204 $groupname = get_string('potusers', 'cohort');
205 }
206
207 return array($groupname => $availableusers);
208 }
209
210 protected function get_options() {
211 $options = parent::get_options();
212 $options['cohortid'] = $this->cohortid;
b1e2e6b8 213 $options['file'] = 'cohort/lib.php';
b980c56e
PS
214 return $options;
215 }
216}
217
218/**
219 * Cohort assignment candidates
220 */
221class cohort_existing_selector extends user_selector_base {
222 protected $cohortid;
223
224 public function __construct($name, $options) {
225 $this->cohortid = $options['cohortid'];
226 parent::__construct($name, $options);
227 }
228
229 /**
230 * Candidate users
231 * @param <type> $search
232 * @return array
233 */
234 public function find_users($search) {
235 global $DB;
236 //by default wherecondition retrieves all users except the deleted, not confirmed and guest
237 list($wherecondition, $params) = $this->search_sql($search, 'u');
238 $params['cohortid'] = $this->cohortid;
239
240 $fields = 'SELECT ' . $this->required_fields_sql('u');
241 $countfields = 'SELECT COUNT(1)';
242
243 $sql = " FROM {user} u
244 JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
245 WHERE $wherecondition";
246
247 $order = ' ORDER BY u.lastname ASC, u.firstname ASC';
248
249 if (!$this->is_validating()) {
250 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
251 if ($potentialmemberscount > 100) {
252 return $this->too_many_results($search, $potentialmemberscount);
253 }
254 }
255
256 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
257
258 if (empty($availableusers)) {
259 return array();
260 }
261
262
263 if ($search) {
264 $groupname = get_string('currentusersmatching', 'cohort', $search);
a174e4a3 265 } else {
b980c56e
PS
266 $groupname = get_string('currentusers', 'cohort');
267 }
268
269 return array($groupname => $availableusers);
270 }
271
272 protected function get_options() {
273 $options = parent::get_options();
274 $options['cohortid'] = $this->cohortid;
b1e2e6b8 275 $options['file'] = 'cohort/lib.php';
b980c56e
PS
276 return $options;
277 }
278}