MDL-69773 block_section_links: Add an option to display section name
[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
9a2f6770 27require_once($CFG->dirroot . '/cohort/lib.php');
607fdf63
PS
28require_once($CFG->dirroot . '/user/selector/lib.php');
29
6eb8bf7f 30
607fdf63
PS
31/**
32 * Cohort assignment candidates
33 */
34class cohort_candidate_selector extends user_selector_base {
35 protected $cohortid;
36
37 public function __construct($name, $options) {
38 $this->cohortid = $options['cohortid'];
39 parent::__construct($name, $options);
40 }
41
42 /**
43 * Candidate users
6eb8bf7f 44 * @param string $search
607fdf63
PS
45 * @return array
46 */
47 public function find_users($search) {
48 global $DB;
6eb8bf7f 49 // By default wherecondition retrieves all users except the deleted, not confirmed and guest.
607fdf63
PS
50 list($wherecondition, $params) = $this->search_sql($search, 'u');
51 $params['cohortid'] = $this->cohortid;
52
53 $fields = 'SELECT ' . $this->required_fields_sql('u');
54 $countfields = 'SELECT COUNT(1)';
55
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";
59
9695ff81
TH
60 list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
61 $order = ' ORDER BY ' . $sort;
607fdf63
PS
62
63 if (!$this->is_validating()) {
64 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
9a51c3d9 65 if ($potentialmemberscount > $this->maxusersperpage) {
607fdf63
PS
66 return $this->too_many_results($search, $potentialmemberscount);
67 }
68 }
69
9695ff81 70 $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
607fdf63
PS
71
72 if (empty($availableusers)) {
73 return array();
74 }
75
76
77 if ($search) {
78 $groupname = get_string('potusersmatching', 'cohort', $search);
79 } else {
80 $groupname = get_string('potusers', 'cohort');
81 }
82
83 return array($groupname => $availableusers);
84 }
85
86 protected function get_options() {
87 $options = parent::get_options();
88 $options['cohortid'] = $this->cohortid;
89 $options['file'] = 'cohort/locallib.php';
90 return $options;
91 }
92}
93
94
95/**
96 * Cohort assignment candidates
97 */
98class cohort_existing_selector extends user_selector_base {
99 protected $cohortid;
100
101 public function __construct($name, $options) {
102 $this->cohortid = $options['cohortid'];
103 parent::__construct($name, $options);
104 }
105
106 /**
107 * Candidate users
6eb8bf7f 108 * @param string $search
607fdf63
PS
109 * @return array
110 */
111 public function find_users($search) {
112 global $DB;
6eb8bf7f 113 // By default wherecondition retrieves all users except the deleted, not confirmed and guest.
607fdf63
PS
114 list($wherecondition, $params) = $this->search_sql($search, 'u');
115 $params['cohortid'] = $this->cohortid;
116
117 $fields = 'SELECT ' . $this->required_fields_sql('u');
118 $countfields = 'SELECT COUNT(1)';
119
120 $sql = " FROM {user} u
121 JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
122 WHERE $wherecondition";
123
9695ff81
TH
124 list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
125 $order = ' ORDER BY ' . $sort;
607fdf63
PS
126
127 if (!$this->is_validating()) {
128 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
a266a1a0 129 if ($potentialmemberscount > $this->maxusersperpage) {
607fdf63
PS
130 return $this->too_many_results($search, $potentialmemberscount);
131 }
132 }
133
9695ff81 134 $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
607fdf63
PS
135
136 if (empty($availableusers)) {
137 return array();
138 }
139
140
141 if ($search) {
142 $groupname = get_string('currentusersmatching', 'cohort', $search);
143 } else {
144 $groupname = get_string('currentusers', 'cohort');
145 }
146
147 return array($groupname => $availableusers);
148 }
149
150 protected function get_options() {
151 $options = parent::get_options();
152 $options['cohortid'] = $this->cohortid;
153 $options['file'] = 'cohort/locallib.php';
154 return $options;
155 }
156}
157