|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 require_once($CFG->dirroot . '/user/selector/lib.php'); 00028 00035 function cohort_add_cohort($cohort) { 00036 global $DB; 00037 00038 if (!isset($cohort->name)) { 00039 throw new coding_exception('Missing cohort name in cohort_add_cohort().'); 00040 } 00041 if (!isset($cohort->idnumber)) { 00042 $cohort->idnumber = NULL; 00043 } 00044 if (!isset($cohort->description)) { 00045 $cohort->description = $DB->sql_empty(); 00046 } 00047 if (!isset($cohort->descriptionformat)) { 00048 $cohort->descriptionformat = FORMAT_HTML; 00049 } 00050 if (empty($cohort->component)) { 00051 $cohort->component = ''; 00052 } 00053 if (!isset($cohort->timecreated)) { 00054 $cohort->timecreated = time(); 00055 } 00056 if (!isset($cohort->timemodified)) { 00057 $cohort->timemodified = $cohort->timecreated; 00058 } 00059 00060 $cohort->id = $DB->insert_record('cohort', $cohort); 00061 00062 events_trigger('cohort_added', $cohort); 00063 00064 return $cohort->id; 00065 } 00066 00072 function cohort_update_cohort($cohort) { 00073 global $DB; 00074 if (isset($cohort->component) and empty($cohort->component)) { 00075 $cohort->component = NULL; 00076 } 00077 $cohort->timemodified = time(); 00078 $DB->update_record('cohort', $cohort); 00079 00080 events_trigger('cohort_updated', $cohort); 00081 } 00082 00088 function cohort_delete_cohort($cohort) { 00089 global $DB; 00090 00091 if ($cohort->component) { 00092 // TODO: add component delete callback 00093 } 00094 00095 $DB->delete_records('cohort_members', array('cohortid'=>$cohort->id)); 00096 $DB->delete_records('cohort', array('id'=>$cohort->id)); 00097 00098 events_trigger('cohort_deleted', $cohort); 00099 } 00100 00108 function cohort_delete_category($category) { 00109 global $DB; 00110 // TODO: make sure that cohorts are really, really not used anywhere and delete, for now just move to parent or system context 00111 00112 $oldcontext = get_context_instance(CONTEXT_COURSECAT, $category->id, MUST_EXIST); 00113 00114 if ($category->parent and $parent = $DB->get_record('course_categories', array('id'=>$category->parent))) { 00115 $parentcontext = get_context_instance(CONTEXT_COURSECAT, $parent->id, MUST_EXIST); 00116 $sql = "UPDATE {cohort} SET contextid = :newcontext WHERE contextid = :oldcontext"; 00117 $params = array('oldcontext'=>$oldcontext->id, 'newcontext'=>$parentcontext->id); 00118 } else { 00119 $syscontext = get_context_instance(CONTEXT_SYSTEM); 00120 $sql = "UPDATE {cohort} SET contextid = :newcontext WHERE contextid = :oldcontext"; 00121 $params = array('oldcontext'=>$oldcontext->id, 'newcontext'=>$syscontext->id); 00122 } 00123 00124 $DB->execute($sql, $params); 00125 } 00126 00133 function cohort_add_member($cohortid, $userid) { 00134 global $DB; 00135 $record = new stdClass(); 00136 $record->cohortid = $cohortid; 00137 $record->userid = $userid; 00138 $record->timeadded = time(); 00139 $DB->insert_record('cohort_members', $record); 00140 00141 events_trigger('cohort_member_added', (object)array('cohortid'=>$cohortid, 'userid'=>$userid)); 00142 } 00143 00150 function cohort_remove_member($cohortid, $userid) { 00151 global $DB; 00152 $DB->delete_records('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid)); 00153 00154 events_trigger('cohort_member_removed', (object)array('cohortid'=>$cohortid, 'userid'=>$userid)); 00155 } 00156 00164 function cohort_get_visible_list($course) { 00165 global $DB, $USER; 00166 00167 $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST); 00168 list($esql, $params) = get_enrolled_sql($context); 00169 $parentsql = get_related_contexts_string($context); 00170 00171 $sql = "SELECT c.id, c.name, c.idnumber, COUNT(u.id) AS cnt 00172 FROM {cohort} c 00173 JOIN {cohort_members} cm ON cm.cohortid = c.id 00174 JOIN ($esql) u ON u.id = cm.userid 00175 WHERE c.contextid $parentsql 00176 GROUP BY c.id, c.name, c.idnumber 00177 HAVING COUNT(u.id) > 0 00178 ORDER BY c.name, c.idnumber"; 00179 $params['ctx'] = $context->id; 00180 00181 $cohorts = $DB->get_records_sql($sql, $params); 00182 00183 foreach ($cohorts as $cid=>$cohort) { 00184 $cohorts[$cid] = format_string($cohort->name); 00185 if ($cohort->idnumber) { 00186 $cohorts[$cid] .= ' (' . $cohort->cnt . ')'; 00187 } 00188 } 00189 00190 return $cohorts; 00191 } 00192 00203 function cohort_get_cohorts($contextid, $page = 0, $perpage = 25, $search = '') { 00204 global $DB; 00205 00206 $cohorts = array(); 00207 00208 // Add some additional sensible conditions 00209 $tests = array('contextid = ?'); 00210 $params = array($contextid); 00211 00212 if (!empty($search)) { 00213 $conditions = array( 00214 'name', 00215 'idnumber', 00216 'description', 00217 ); 00218 $searchparam = '%' . $search . '%'; 00219 foreach ($conditions as $key=>$condition) { 00220 $conditions[$key] = $DB->sql_like($condition,"?", false); 00221 $params[] = $searchparam; 00222 } 00223 $tests[] = '(' . implode(' OR ', $conditions) . ')'; 00224 } 00225 $wherecondition = implode(' AND ', $tests); 00226 00227 $fields = 'SELECT *'; 00228 $countfields = 'SELECT COUNT(1)'; 00229 $sql = " FROM {cohort} 00230 WHERE $wherecondition"; 00231 $order = ' ORDER BY name ASC'; 00232 $totalcohorts = $DB->count_records_sql($countfields . $sql, $params); 00233 $cohorts = $DB->get_records_sql($fields . $sql . $order, $params, $page*$perpage, $perpage); 00234 00235 return array('totalcohorts' => $totalcohorts, 'cohorts' => $cohorts); 00236 } 00237 00241 class cohort_candidate_selector extends user_selector_base { 00242 protected $cohortid; 00243 00244 public function __construct($name, $options) { 00245 $this->cohortid = $options['cohortid']; 00246 parent::__construct($name, $options); 00247 } 00248 00254 public function find_users($search) { 00255 global $DB; 00256 //by default wherecondition retrieves all users except the deleted, not confirmed and guest 00257 list($wherecondition, $params) = $this->search_sql($search, 'u'); 00258 $params['cohortid'] = $this->cohortid; 00259 00260 $fields = 'SELECT ' . $this->required_fields_sql('u'); 00261 $countfields = 'SELECT COUNT(1)'; 00262 00263 $sql = " FROM {user} u 00264 LEFT JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid) 00265 WHERE cm.id IS NULL AND $wherecondition"; 00266 00267 $order = ' ORDER BY u.lastname ASC, u.firstname ASC'; 00268 00269 if (!$this->is_validating()) { 00270 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params); 00271 if ($potentialmemberscount > 100) { 00272 return $this->too_many_results($search, $potentialmemberscount); 00273 } 00274 } 00275 00276 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params); 00277 00278 if (empty($availableusers)) { 00279 return array(); 00280 } 00281 00282 00283 if ($search) { 00284 $groupname = get_string('potusersmatching', 'cohort', $search); 00285 } else { 00286 $groupname = get_string('potusers', 'cohort'); 00287 } 00288 00289 return array($groupname => $availableusers); 00290 } 00291 00292 protected function get_options() { 00293 $options = parent::get_options(); 00294 $options['cohortid'] = $this->cohortid; 00295 $options['file'] = 'cohort/lib.php'; 00296 return $options; 00297 } 00298 } 00299 00303 class cohort_existing_selector extends user_selector_base { 00304 protected $cohortid; 00305 00306 public function __construct($name, $options) { 00307 $this->cohortid = $options['cohortid']; 00308 parent::__construct($name, $options); 00309 } 00310 00316 public function find_users($search) { 00317 global $DB; 00318 //by default wherecondition retrieves all users except the deleted, not confirmed and guest 00319 list($wherecondition, $params) = $this->search_sql($search, 'u'); 00320 $params['cohortid'] = $this->cohortid; 00321 00322 $fields = 'SELECT ' . $this->required_fields_sql('u'); 00323 $countfields = 'SELECT COUNT(1)'; 00324 00325 $sql = " FROM {user} u 00326 JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid) 00327 WHERE $wherecondition"; 00328 00329 $order = ' ORDER BY u.lastname ASC, u.firstname ASC'; 00330 00331 if (!$this->is_validating()) { 00332 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params); 00333 if ($potentialmemberscount > 100) { 00334 return $this->too_many_results($search, $potentialmemberscount); 00335 } 00336 } 00337 00338 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params); 00339 00340 if (empty($availableusers)) { 00341 return array(); 00342 } 00343 00344 00345 if ($search) { 00346 $groupname = get_string('currentusersmatching', 'cohort', $search); 00347 } else { 00348 $groupname = get_string('currentusers', 'cohort'); 00349 } 00350 00351 return array($groupname => $availableusers); 00352 } 00353 00354 protected function get_options() { 00355 $options = parent::get_options(); 00356 $options['cohortid'] = $this->cohortid; 00357 $options['file'] = 'cohort/lib.php'; 00358 return $options; 00359 } 00360 }