|
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
|
|
|
* Relationship assignment candidate selector definition |
|
19
|
|
|
* |
|
20
|
|
|
* @package local_relationship |
|
21
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
|
22
|
|
|
*/ |
|
23
|
|
|
defined('MOODLE_INTERNAL') || die(); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* relationship assignment candidates |
|
27
|
|
|
*/ |
|
28
|
|
|
class local_relationship_candidate_selector extends user_selector_base { |
|
29
|
|
|
protected $relationshipgroup; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct($name, $options) { |
|
32
|
|
|
$this->relationshipgroup = $options['relationshipgroup']; |
|
33
|
|
|
parent::__construct($name, $options); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function find_users($search) { |
|
37
|
|
|
global $DB; |
|
38
|
|
|
|
|
39
|
|
|
list($usercondition, $params) = users_search_sql($search, 'u', $this->searchanywhere); |
|
40
|
|
|
|
|
41
|
|
View Code Duplication |
if(!empty($this->validatinguserids)) { |
|
|
|
|
|
|
42
|
|
|
list($usertest, $userparams) = $DB->get_in_or_equal($this->validatinguserids, SQL_PARAMS_NAMED, 'val'); |
|
43
|
|
|
$usercondition .= " AND u.id*1000000+rc.id " . $usertest; |
|
|
|
|
|
|
44
|
|
|
$params = array_merge($params, $userparams); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$params['relationshipid'] = $this->relationshipgroup->relationshipid; |
|
|
|
|
|
|
48
|
|
|
$params['relationshipgroupid'] = $this->relationshipgroup->id; |
|
49
|
|
|
|
|
50
|
|
|
$countfields = 'SELECT COUNT(DISTINCT u.id)'; |
|
51
|
|
|
$selectfields = "SELECT DISTINCT u.id*1000000+rc.id as id, rc.id AS relationshipcohortid, rc.roleid, rc.allowdupsingroups, |
|
52
|
|
|
u.id AS userid, CONCAT(u.firstname, ' ', u.lastname) AS fullname, |
|
53
|
|
|
CASE WHEN rm.id IS NULL THEN 0 ELSE 1 END AS in_group"; |
|
54
|
|
|
|
|
55
|
|
|
$from = "FROM {relationship_cohorts} rc |
|
56
|
|
|
JOIN {cohort_members} cm ON (cm.cohortid = rc.cohortid) |
|
57
|
|
|
JOIN {user} u ON (u.id = cm.userid) |
|
58
|
|
|
LEFT JOIN {relationship_members} rm ON (rm.relationshipcohortid = rc.id AND rm.userid = cm.userid) |
|
59
|
|
|
WHERE rc.relationshipid = :relationshipid |
|
60
|
|
|
AND NOT EXISTS (SELECT 1 |
|
61
|
|
|
FROM {relationship_members} rm |
|
62
|
|
|
WHERE rm.relationshipcohortid = rc.id |
|
63
|
|
|
AND rm.userid = cm.userid |
|
64
|
|
|
AND rm.relationshipgroupid = :relationshipgroupid) |
|
65
|
|
|
AND (rc.allowdupsingroups = 1 |
|
66
|
|
|
OR (rc.allowdupsingroups = 0 AND rm.id IS NULL)) |
|
67
|
|
|
AND {$usercondition}"; |
|
68
|
|
|
|
|
69
|
|
|
$orderby= "ORDER BY roleid, in_group, fullname"; |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
View Code Duplication |
if (!$this->is_validating()) { |
|
|
|
|
|
|
72
|
|
|
$sql = $countfields . "\n" . $from; |
|
|
|
|
|
|
73
|
|
|
$count = $DB->count_records_sql($sql, $params); |
|
74
|
|
|
if ($count > $this->maxusersperpage) { |
|
75
|
|
|
return $this->too_many_results($search, $count); |
|
76
|
|
|
} else if ($count == 0) { |
|
77
|
|
|
return array(); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$sql = $selectfields . "\n" . $from . "\n" . $orderby; |
|
82
|
|
|
|
|
83
|
|
|
$users = array(); |
|
|
|
|
|
|
84
|
|
|
$roleid = -1; |
|
|
|
|
|
|
85
|
|
|
$in_group = -1; |
|
86
|
|
|
$index = false; |
|
|
|
|
|
|
87
|
|
|
foreach($DB->get_recordset_sql($sql, $params) AS $cand) { |
|
88
|
|
|
if($cand->roleid != $roleid || $cand->in_group != $in_group) { |
|
89
|
|
|
$role = $DB->get_record('role', array('id'=>$cand->roleid), '*', MUST_EXIST); |
|
|
|
|
|
|
90
|
|
|
$role_name = role_get_name($role); |
|
91
|
|
|
|
|
92
|
|
|
$str_alloc = $cand->in_group == 1 ? get_string('allocated', 'local_relationship') : get_string('notallocated', 'local_relationship'); |
|
|
|
|
|
|
93
|
|
|
$index = $role_name . $str_alloc; |
|
|
|
|
|
|
94
|
|
|
$users[$index] = array(); |
|
95
|
|
|
|
|
96
|
|
|
$roleid = $cand->roleid; |
|
|
|
|
|
|
97
|
|
|
$in_group = $cand->in_group; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$users[$index][$cand->id] = $cand; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $users; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Convert a user object to a string suitable for displaying as an option in the list box. |
|
108
|
|
|
* |
|
109
|
|
|
* @param object $user the user to display. |
|
110
|
|
|
* @return string a string representation of the user. |
|
111
|
|
|
*/ |
|
112
|
|
|
public function output_user($user) { |
|
113
|
|
|
return $user->fullname; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
protected function get_options() { |
|
117
|
|
|
$options = parent::get_options(); |
|
|
|
|
|
|
118
|
|
|
$options['relationshipgroup'] = $this->relationshipgroup; |
|
119
|
|
|
return $options; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.