MDL-35465 refactor cohort user selectors to locallib.php
[moodle.git] / cohort / locallib.php
CommitLineData
607fdf63
PS
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.
13//
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/>.
16
17/**
18 * Cohort UI related functions and classes.
19 *
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
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27require_once($CFG->dirroot . '/cohot/lib.php');
28require_once($CFG->dirroot . '/user/selector/lib.php');
29
30/**
31 * Cohort assignment candidates
32 */
33class cohort_candidate_selector extends user_selector_base {
34 protected $cohortid;
35
36 public function __construct($name, $options) {
37 $this->cohortid = $options['cohortid'];
38 parent::__construct($name, $options);
39 }
40
41 /**
42 * Candidate users
43 * @param <type> $search
44 * @return array
45 */
46 public function find_users($search) {
47 global $DB;
48 //by default wherecondition retrieves all users except the deleted, not confirmed and guest
49 list($wherecondition, $params) = $this->search_sql($search, 'u');
50 $params['cohortid'] = $this->cohortid;
51
52 $fields = 'SELECT ' . $this->required_fields_sql('u');
53 $countfields = 'SELECT COUNT(1)';
54
55 $sql = " FROM {user} u
56 LEFT JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
57 WHERE cm.id IS NULL AND $wherecondition";
58
59 $order = ' ORDER BY u.lastname ASC, u.firstname ASC';
60
61 if (!$this->is_validating()) {
62 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
63 if ($potentialmemberscount > 100) {
64 return $this->too_many_results($search, $potentialmemberscount);
65 }
66 }
67
68 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
69
70 if (empty($availableusers)) {
71 return array();
72 }
73
74
75 if ($search) {
76 $groupname = get_string('potusersmatching', 'cohort', $search);
77 } else {
78 $groupname = get_string('potusers', 'cohort');
79 }
80
81 return array($groupname => $availableusers);
82 }
83
84 protected function get_options() {
85 $options = parent::get_options();
86 $options['cohortid'] = $this->cohortid;
87 $options['file'] = 'cohort/locallib.php';
88 return $options;
89 }
90}
91
92
93/**
94 * Cohort assignment candidates
95 */
96class cohort_existing_selector extends user_selector_base {
97 protected $cohortid;
98
99 public function __construct($name, $options) {
100 $this->cohortid = $options['cohortid'];
101 parent::__construct($name, $options);
102 }
103
104 /**
105 * Candidate users
106 * @param <type> $search
107 * @return array
108 */
109 public function find_users($search) {
110 global $DB;
111 //by default wherecondition retrieves all users except the deleted, not confirmed and guest
112 list($wherecondition, $params) = $this->search_sql($search, 'u');
113 $params['cohortid'] = $this->cohortid;
114
115 $fields = 'SELECT ' . $this->required_fields_sql('u');
116 $countfields = 'SELECT COUNT(1)';
117
118 $sql = " FROM {user} u
119 JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
120 WHERE $wherecondition";
121
122 $order = ' ORDER BY u.lastname ASC, u.firstname ASC';
123
124 if (!$this->is_validating()) {
125 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
126 if ($potentialmemberscount > 100) {
127 return $this->too_many_results($search, $potentialmemberscount);
128 }
129 }
130
131 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
132
133 if (empty($availableusers)) {
134 return array();
135 }
136
137
138 if ($search) {
139 $groupname = get_string('currentusersmatching', 'cohort', $search);
140 } else {
141 $groupname = get_string('currentusers', 'cohort');
142 }
143
144 return array($groupname => $availableusers);
145 }
146
147 protected function get_options() {
148 $options = parent::get_options();
149 $options['cohortid'] = $this->cohortid;
150 $options['file'] = 'cohort/locallib.php';
151 return $options;
152 }
153}
154