MDL-31873 added mod_assign_get_grades web service function
[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
60 $order = ' ORDER BY u.lastname ASC, u.firstname ASC';
61
62 if (!$this->is_validating()) {
63 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
64 if ($potentialmemberscount > 100) {
65 return $this->too_many_results($search, $potentialmemberscount);
66 }
67 }
68
69 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
70
71 if (empty($availableusers)) {
72 return array();
73 }
74
75
76 if ($search) {
77 $groupname = get_string('potusersmatching', 'cohort', $search);
78 } else {
79 $groupname = get_string('potusers', 'cohort');
80 }
81
82 return array($groupname => $availableusers);
83 }
84
85 protected function get_options() {
86 $options = parent::get_options();
87 $options['cohortid'] = $this->cohortid;
88 $options['file'] = 'cohort/locallib.php';
89 return $options;
90 }
91}
92
93
94/**
95 * Cohort assignment candidates
96 */
97class cohort_existing_selector extends user_selector_base {
98 protected $cohortid;
99
100 public function __construct($name, $options) {
101 $this->cohortid = $options['cohortid'];
102 parent::__construct($name, $options);
103 }
104
105 /**
106 * Candidate users
6eb8bf7f 107 * @param string $search
607fdf63
PS
108 * @return array
109 */
110 public function find_users($search) {
111 global $DB;
6eb8bf7f 112 // By default wherecondition retrieves all users except the deleted, not confirmed and guest.
607fdf63
PS
113 list($wherecondition, $params) = $this->search_sql($search, 'u');
114 $params['cohortid'] = $this->cohortid;
115
116 $fields = 'SELECT ' . $this->required_fields_sql('u');
117 $countfields = 'SELECT COUNT(1)';
118
119 $sql = " FROM {user} u
120 JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
121 WHERE $wherecondition";
122
123 $order = ' ORDER BY u.lastname ASC, u.firstname ASC';
124
125 if (!$this->is_validating()) {
126 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
127 if ($potentialmemberscount > 100) {
128 return $this->too_many_results($search, $potentialmemberscount);
129 }
130 }
131
132 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
133
134 if (empty($availableusers)) {
135 return array();
136 }
137
138
139 if ($search) {
140 $groupname = get_string('currentusersmatching', 'cohort', $search);
141 } else {
142 $groupname = get_string('currentusers', 'cohort');
143 }
144
145 return array($groupname => $availableusers);
146 }
147
148 protected function get_options() {
149 $options = parent::get_options();
150 $options['cohortid'] = $this->cohortid;
151 $options['file'] = 'cohort/locallib.php';
152 return $options;
153 }
154}
155