|
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 existing assignment selector definition |
|
19
|
|
|
* |
|
20
|
|
|
* @package local_relationship |
|
21
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
defined('MOODLE_INTERNAL') || die(); |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* relationship assignment candidates |
|
28
|
|
|
*/ |
|
29
|
|
|
class local_relationship_existing_selector extends user_selector_base { |
|
30
|
|
|
protected $relationshipgroup; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct($name, $options) { |
|
33
|
|
|
$this->relationshipgroup = $options['relationshipgroup']; |
|
34
|
|
|
parent::__construct($name, $options); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Candidate users |
|
39
|
|
|
* @param string $search |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
|
|
public function find_users($search) { |
|
43
|
|
|
global $DB; |
|
44
|
|
|
|
|
45
|
|
|
list($usercondition, $params) = users_search_sql($search, 'u', $this->searchanywhere); |
|
46
|
|
|
|
|
47
|
|
View Code Duplication |
if(!empty($this->validatinguserids)) { |
|
|
|
|
|
|
48
|
|
|
list($usertest, $userparams) = $DB->get_in_or_equal($this->validatinguserids, SQL_PARAMS_NAMED, 'val'); |
|
49
|
|
|
$usercondition .= " AND u.id*1000000+rc.id " . $usertest; |
|
|
|
|
|
|
50
|
|
|
$params = array_merge($params, $userparams); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$params['relationshipgroupid'] = $this->relationshipgroup->id; |
|
54
|
|
|
|
|
55
|
|
|
$countfields = 'SELECT COUNT(1)'; |
|
56
|
|
|
$selectfields = "SELECT u.id*1000000+rc.id as id, rc.id AS relationshipcohortid, rc.roleid, u.id AS userid, CONCAT(u.firstname, ' ', u.lastname) AS fullname"; |
|
57
|
|
|
|
|
58
|
|
|
$from = "FROM {relationship_members} rm |
|
59
|
|
|
JOIN {relationship_cohorts} rc ON (rc.id = rm.relationshipcohortid) |
|
60
|
|
|
JOIN {user} u ON (u.id = rm.userid) |
|
61
|
|
|
WHERE rm.relationshipgroupid = :relationshipgroupid |
|
62
|
|
|
AND {$usercondition}"; |
|
63
|
|
|
|
|
64
|
|
|
$orderby= "ORDER BY roleid, fullname"; |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
View Code Duplication |
if (!$this->is_validating()) { |
|
|
|
|
|
|
67
|
|
|
$sql = $countfields . "\n" . $from; |
|
|
|
|
|
|
68
|
|
|
$count = $DB->count_records_sql($sql, $params); |
|
69
|
|
|
if ($count > $this->maxusersperpage) { |
|
70
|
|
|
return $this->too_many_results($search, $count); |
|
71
|
|
|
} else if ($count == 0) { |
|
72
|
|
|
return array(); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$sql = $selectfields . "\n" . $from . "\n" . $orderby; |
|
77
|
|
|
|
|
78
|
|
|
$users = array(); |
|
|
|
|
|
|
79
|
|
|
$roleid = -1; |
|
80
|
|
|
$index = false; |
|
|
|
|
|
|
81
|
|
|
foreach($DB->get_recordset_sql($sql, $params) AS $cand) { |
|
82
|
|
|
if($cand->roleid != $roleid) { |
|
83
|
|
|
$role = $DB->get_record('role', array('id'=>$cand->roleid), '*', MUST_EXIST); |
|
|
|
|
|
|
84
|
|
|
$index = role_get_name($role); |
|
|
|
|
|
|
85
|
|
|
$users[$index] = array(); |
|
86
|
|
|
|
|
87
|
|
|
$roleid = $cand->roleid; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$users[$index][$cand->id] = $cand; |
|
91
|
|
|
} |
|
92
|
|
|
return $users; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Convert a user object to a string suitable for displaying as an option in the list box. |
|
97
|
|
|
* |
|
98
|
|
|
* @param object $user the user to display. |
|
99
|
|
|
* @return string a string representation of the user. |
|
100
|
|
|
*/ |
|
101
|
|
|
public function output_user($user) { |
|
102
|
|
|
return $user->fullname; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
protected function get_options() { |
|
106
|
|
|
$options = parent::get_options(); |
|
|
|
|
|
|
107
|
|
|
$options['relationshipgroup'] = $this->relationshipgroup; |
|
108
|
|
|
return $options; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
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.