MDL-74955 reportbuilder: external method for listing custom reports.
[moodle.git] / cohort / locallib.php
blob79ee509416782a5883d89c0a87861a362bd6bc98
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 $options['includecustomfields'] = true;
40 parent::__construct($name, $options);
43 /**
44 * Candidate users
45 * @param string $search
46 * @return array
48 public function find_users($search) {
49 global $DB;
51 // By default wherecondition retrieves all users except the deleted, not confirmed and guest.
52 list($wherecondition, $params) = $this->search_sql($search, 'u');
53 $params = array_merge($params, $this->userfieldsparams);
55 $params['cohortid'] = $this->cohortid;
57 $fields = 'SELECT u.id, ' . $this->userfieldsselects;
58 $countfields = 'SELECT COUNT(1)';
60 $sql = " FROM {user} u
61 LEFT JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
62 $this->userfieldsjoin
63 WHERE cm.id IS NULL AND $wherecondition";
65 list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext, $this->userfieldsmappings);
66 $order = ' ORDER BY ' . $sort;
68 if (!$this->is_validating()) {
69 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
70 if ($potentialmemberscount > $this->maxusersperpage) {
71 return $this->too_many_results($search, $potentialmemberscount);
75 $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
77 if (empty($availableusers)) {
78 return array();
82 if ($search) {
83 $groupname = get_string('potusersmatching', 'cohort', $search);
84 } else {
85 $groupname = get_string('potusers', 'cohort');
88 return array($groupname => $availableusers);
91 protected function get_options() {
92 $options = parent::get_options();
93 $options['cohortid'] = $this->cohortid;
94 $options['file'] = 'cohort/locallib.php';
95 return $options;
101 * Cohort assignment candidates
103 class cohort_existing_selector extends user_selector_base {
104 protected $cohortid;
106 public function __construct($name, $options) {
107 $this->cohortid = $options['cohortid'];
108 $options['includecustomfields'] = true;
109 parent::__construct($name, $options);
113 * Candidate users
114 * @param string $search
115 * @return array
117 public function find_users($search) {
118 global $DB;
120 // By default wherecondition retrieves all users except the deleted, not confirmed and guest.
121 list($wherecondition, $params) = $this->search_sql($search, 'u');
122 $params = array_merge($params, $this->userfieldsparams);
124 $params['cohortid'] = $this->cohortid;
126 $fields = 'SELECT u.id, ' . $this->userfieldsselects;
127 $countfields = 'SELECT COUNT(1)';
129 $sql = " FROM {user} u
130 JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
131 $this->userfieldsjoin
132 WHERE $wherecondition";
134 list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext, $this->userfieldsmappings);
135 $order = ' ORDER BY ' . $sort;
137 if (!$this->is_validating()) {
138 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
139 if ($potentialmemberscount > $this->maxusersperpage) {
140 return $this->too_many_results($search, $potentialmemberscount);
144 $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
146 if (empty($availableusers)) {
147 return array();
151 if ($search) {
152 $groupname = get_string('currentusersmatching', 'cohort', $search);
153 } else {
154 $groupname = get_string('currentusers', 'cohort');
157 return array($groupname => $availableusers);
160 protected function get_options() {
161 $options = parent::get_options();
162 $options['cohortid'] = $this->cohortid;
163 $options['file'] = 'cohort/locallib.php';
164 return $options;