MDL-21781 move cohorts away when deleting course category
[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
120/**
121 * Cohort assignment candidates
122 */
123class cohort_candidate_selector extends user_selector_base {
124 protected $cohortid;
125
126 public function __construct($name, $options) {
127 $this->cohortid = $options['cohortid'];
128 parent::__construct($name, $options);
129 }
130
131 /**
132 * Candidate users
133 * @param <type> $search
134 * @return array
135 */
136 public function find_users($search) {
137 global $DB;
138 //by default wherecondition retrieves all users except the deleted, not confirmed and guest
139 list($wherecondition, $params) = $this->search_sql($search, 'u');
140 $params['cohortid'] = $this->cohortid;
141
142 $fields = 'SELECT ' . $this->required_fields_sql('u');
143 $countfields = 'SELECT COUNT(1)';
144
145 $sql = " FROM {user} u
146 LEFT JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
147 WHERE cm.id IS NULL AND $wherecondition";
148
149 $order = ' ORDER BY u.lastname ASC, u.firstname ASC';
150
151 if (!$this->is_validating()) {
152 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
153 if ($potentialmemberscount > 100) {
154 return $this->too_many_results($search, $potentialmemberscount);
155 }
156 }
157
158 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
159
160 if (empty($availableusers)) {
161 return array();
162 }
163
164
165 if ($search) {
166 $groupname = get_string('potusersmatching', 'cohort', $search);
167 }
168 else {
169 $groupname = get_string('potusers', 'cohort');
170 }
171
172 return array($groupname => $availableusers);
173 }
174
175 protected function get_options() {
176 $options = parent::get_options();
177 $options['cohortid'] = $this->cohortid;
b1e2e6b8 178 $options['file'] = 'cohort/lib.php';
b980c56e
PS
179 return $options;
180 }
181}
182
183/**
184 * Cohort assignment candidates
185 */
186class cohort_existing_selector extends user_selector_base {
187 protected $cohortid;
188
189 public function __construct($name, $options) {
190 $this->cohortid = $options['cohortid'];
191 parent::__construct($name, $options);
192 }
193
194 /**
195 * Candidate users
196 * @param <type> $search
197 * @return array
198 */
199 public function find_users($search) {
200 global $DB;
201 //by default wherecondition retrieves all users except the deleted, not confirmed and guest
202 list($wherecondition, $params) = $this->search_sql($search, 'u');
203 $params['cohortid'] = $this->cohortid;
204
205 $fields = 'SELECT ' . $this->required_fields_sql('u');
206 $countfields = 'SELECT COUNT(1)';
207
208 $sql = " FROM {user} u
209 JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
210 WHERE $wherecondition";
211
212 $order = ' ORDER BY u.lastname ASC, u.firstname ASC';
213
214 if (!$this->is_validating()) {
215 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
216 if ($potentialmemberscount > 100) {
217 return $this->too_many_results($search, $potentialmemberscount);
218 }
219 }
220
221 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
222
223 if (empty($availableusers)) {
224 return array();
225 }
226
227
228 if ($search) {
229 $groupname = get_string('currentusersmatching', 'cohort', $search);
230 }
231 else {
232 $groupname = get_string('currentusers', 'cohort');
233 }
234
235 return array($groupname => $availableusers);
236 }
237
238 protected function get_options() {
239 $options = parent::get_options();
240 $options['cohortid'] = $this->cohortid;
b1e2e6b8 241 $options['file'] = 'cohort/lib.php';
b980c56e
PS
242 return $options;
243 }
244}