MDL-51853 calendar: Whitespace fix
[moodle.git] / cohort / locallib.php
blob62dcfdfe90c3558c6efe81fd2bd65535f3d18f00
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Cohort UI related functions and classes.
20 * @package core_cohort
21 * @copyright 2012 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->dirroot . '/cohort/lib.php');
28 require_once($CFG->dirroot . '/user/selector/lib.php');
31 /**
32 * Cohort assignment candidates
34 class cohort_candidate_selector extends user_selector_base {
35 protected $cohortid;
37 public function __construct($name, $options) {
38 $this->cohortid = $options['cohortid'];
39 parent::__construct($name, $options);
42 /**
43 * Candidate users
44 * @param string $search
45 * @return array
47 public function find_users($search) {
48 global $DB;
49 // By default wherecondition retrieves all users except the deleted, not confirmed and guest.
50 list($wherecondition, $params) = $this->search_sql($search, 'u');
51 $params['cohortid'] = $this->cohortid;
53 $fields = 'SELECT ' . $this->required_fields_sql('u');
54 $countfields = 'SELECT COUNT(1)';
56 $sql = " FROM {user} u
57 LEFT JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
58 WHERE cm.id IS NULL AND $wherecondition";
60 list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
61 $order = ' ORDER BY ' . $sort;
63 if (!$this->is_validating()) {
64 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
65 if ($potentialmemberscount > $this->maxusersperpage) {
66 return $this->too_many_results($search, $potentialmemberscount);
70 $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
72 if (empty($availableusers)) {
73 return array();
77 if ($search) {
78 $groupname = get_string('potusersmatching', 'cohort', $search);
79 } else {
80 $groupname = get_string('potusers', 'cohort');
83 return array($groupname => $availableusers);
86 protected function get_options() {
87 $options = parent::get_options();
88 $options['cohortid'] = $this->cohortid;
89 $options['file'] = 'cohort/locallib.php';
90 return $options;
95 /**
96 * Cohort assignment candidates
98 class cohort_existing_selector extends user_selector_base {
99 protected $cohortid;
101 public function __construct($name, $options) {
102 $this->cohortid = $options['cohortid'];
103 parent::__construct($name, $options);
107 * Candidate users
108 * @param string $search
109 * @return array
111 public function find_users($search) {
112 global $DB;
113 // By default wherecondition retrieves all users except the deleted, not confirmed and guest.
114 list($wherecondition, $params) = $this->search_sql($search, 'u');
115 $params['cohortid'] = $this->cohortid;
117 $fields = 'SELECT ' . $this->required_fields_sql('u');
118 $countfields = 'SELECT COUNT(1)';
120 $sql = " FROM {user} u
121 JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
122 WHERE $wherecondition";
124 list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
125 $order = ' ORDER BY ' . $sort;
127 if (!$this->is_validating()) {
128 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
129 if ($potentialmemberscount > $this->maxusersperpage) {
130 return $this->too_many_results($search, $potentialmemberscount);
134 $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
136 if (empty($availableusers)) {
137 return array();
141 if ($search) {
142 $groupname = get_string('currentusersmatching', 'cohort', $search);
143 } else {
144 $groupname = get_string('currentusers', 'cohort');
147 return array($groupname => $availableusers);
150 protected function get_options() {
151 $options = parent::get_options();
152 $options['cohortid'] = $this->cohortid;
153 $options['file'] = 'cohort/locallib.php';
154 return $options;