MDL-21781 cohort id duplicates detection
[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
68/**
69 * Remove cohort member
70 * @param int $cohortid
71 * @param int $userid
72 * @return void
73 */
74function cohort_add_member($cohortid, $userid) {
75 global $DB;
76 $record = new object();
77 $record->cohortid = $cohortid;
78 $record->userid = $userid;
79 $record->timeadded = time();
80 $DB->insert_record('cohort_members', $record);
81}
82
83/**
84 * Add cohort member
85 * @param int $cohortid
86 * @param int $userid
87 * @return void
88 */
89function cohort_remove_member($cohortid, $userid) {
90 global $DB;
91 $DB->delete_records('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid));
92}
93
94/**
95 * Cohort assignment candidates
96 */
97class cohort_candidate_selector extends user_selector_base {
98 protected $cohortid;
99
100 public function __construct($name, $options) {
101 $this->cohortid = $options['cohortid'];
102 parent::__construct($name, $options);
103 }
104
105 /**
106 * Candidate users
107 * @param <type> $search
108 * @return array
109 */
110 public function find_users($search) {
111 global $DB;
112 //by default wherecondition retrieves all users except the deleted, not confirmed and guest
113 list($wherecondition, $params) = $this->search_sql($search, 'u');
114 $params['cohortid'] = $this->cohortid;
115
116 $fields = 'SELECT ' . $this->required_fields_sql('u');
117 $countfields = 'SELECT COUNT(1)';
118
119 $sql = " FROM {user} u
120 LEFT JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
121 WHERE cm.id IS NULL AND $wherecondition";
122
123 $order = ' ORDER BY u.lastname ASC, u.firstname ASC';
124
125 if (!$this->is_validating()) {
126 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
127 if ($potentialmemberscount > 100) {
128 return $this->too_many_results($search, $potentialmemberscount);
129 }
130 }
131
132 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
133
134 if (empty($availableusers)) {
135 return array();
136 }
137
138
139 if ($search) {
140 $groupname = get_string('potusersmatching', 'cohort', $search);
141 }
142 else {
143 $groupname = get_string('potusers', 'cohort');
144 }
145
146 return array($groupname => $availableusers);
147 }
148
149 protected function get_options() {
150 $options = parent::get_options();
151 $options['cohortid'] = $this->cohortid;
152 return $options;
153 }
154}
155
156/**
157 * Cohort assignment candidates
158 */
159class cohort_existing_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 JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
183 WHERE $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('currentusersmatching', 'cohort', $search);
203 }
204 else {
205 $groupname = get_string('currentusers', 'cohort');
206 }
207
208 return array($groupname => $availableusers);
209 }
210
211 protected function get_options() {
212 $options = parent::get_options();
213 $options['cohortid'] = $this->cohortid;
214 return $options;
215 }
216}