forked from roelmann/moodle-block_course_contacts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_course_contacts.php
executable file
·280 lines (252 loc) · 12.1 KB
/
block_course_contacts.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Block Course_Contacts main file.
*
* @package block_course_contacts
* @author Mark Ward
* 2016 Richard Oelmann
* @copyright Mark Ward
* @credits 2016 R. Oelmann
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_course_contacts extends block_base {
public function init() {
global $USER;
$this->title = get_string('course_contacts', 'block_course_contacts');
}
public function applicable_formats() {
return array('all' => true, 'mod' => false, 'tag' => false, 'my' => false);
}
// A custom function for shortening names.
public function shorten_name($lname) {
if (strpos($lname, '-')) {
$names = explode('-', $lname);
$lname = '';
foreach ($names as $name) {
if (strlen($name) > 6) {
$name = substr($name, 0, 1);
}
$lname .= $name."-";
}
$lname = substr($lname, 0, -1);
}
if (strpos($lname, ' ')) {
$names = explode(' ', $lname);
$lname = $names[0];
}
return $lname;
}
/**
* Gets all the users assigned this role in this context or higher
*
* @param int $roleid (can also be an array of ints!)
* @param context $context
* @param bool $parent if true, get list of users assigned in higher context too
* @param string $fields fields from user (u.) , role assignment (ra) or role (r.)
* @param string $sort sort from user (u.) , role assignment (ra) or role (r.)
* @param bool $gethiddenignored use enrolments instead
* @param string $group defaults to ''
* @param mixed $limitfrom defaults to ''
* @param mixed $limitnum defaults to ''
* @param string $extrawheretest defaults to ''
* @param string|array $whereparams defaults to ''
* @return array
*/
private function get_role_users($roleid, $context, $parent = false, $fields = '',
$sort = 'u.lastname, u.firstname', $gethiddenignored = null, $group = '',
$limitfrom = '', $limitnum = '', $extrawheretest = '', $whereparams = array()) {
global $DB;
if (empty($fields)) {
$fields = 'u.id, u.confirmed, u.username, u.firstname, u.lastname, '.
'u.maildisplay, u.mailformat, u.maildigest, u.email, u.emailstop, u.city, '.
'u.country, u.picture, u.idnumber, u.department, u.institution, '.
'u.lang, u.timezone, u.lastaccess, u.mnethostid, r.name AS rolename, r.sortorder';
}
$parentcontexts = '';
if ($parent) {
$parentcontexts = substr($context->path, 1); // Kill leading slash.
$parentcontexts = str_replace('/', ',', $parentcontexts);
if ($parentcontexts !== '') {
$parentcontexts = ' OR ra.contextid IN ('.$parentcontexts.' )';
}
}
if ($roleid) {
list($rids, $params) = $DB->get_in_or_equal($roleid, SQL_PARAMS_QM);
$roleselect = "AND ra.roleid $rids";
} else {
$params = array();
$roleselect = '';
}
if ($group) {
$groupjoin = "JOIN {groups_members} gm ON gm.userid = u.id";
$groupselect = " AND gm.groupid = ? ";
$params[] = $group;
} else {
$groupjoin = '';
$groupselect = '';
}
array_unshift($params, $context->id);
if ($extrawheretest) {
$extrawheretest = ' AND ' . $extrawheretest;
$params = array_merge($params, $whereparams);
}
$sql = "SELECT $fields, ra.roleid
FROM {role_assignments} ra
JOIN {user} u ON u.id = ra.userid
JOIN {role} r ON ra.roleid = r.id
$groupjoin
WHERE (ra.contextid = ? $parentcontexts)
$roleselect
$groupselect
$extrawheretest
GROUP BY $fields, ra.roleid
ORDER BY $sort"; // Join now so that we can just use fullname() later.
return $DB->get_records_sql($sql, $params, $limitfrom, $limitnum);
}
public function get_content() {
global $CFG, $DB, $OUTPUT, $USER, $COURSE;
// If the user hasnt configured the plugin, set these as defaults.
if (empty($this->config)) {
$this->config = new stdclass();
$this->config->role_3 = 1;
$this->config->email = 1;
$this->config->message = 1;
$this->config->phone = 0;
}
$courseid = $this->page->course->id;
if ($this->content !== null) {
return $this->content;
}
$this->content = new stdClass;
$context = $this->page->context;
$content = '';
// Find the roles available on this course.
$roles = array_reverse(get_default_enrol_roles($context, null), true);
$content .= html_writer::start_tag('div', array('class' => 'box'));
// How are we going to sort the contacts?
$orderby = 'u.lastname'; // Default.
if (!empty($this->config->sortby)) {
switch($this->config->sortby) {
case 0:
$orderby = 'u.lastname, u.firstname';
break;
case 1:
$orderby = 'u.lastaccess DESC';
break;
case 2:
$orderby = 'MIN(ra.timemodified)';
break;
default:
$orderby = 'u.lastname, u.firstname';
break;
}
}
// Step through each role and check that the config is set to display.
$inherit = 0;
if (isset($this->config->inherit)) {
$inherit = $this->config->inherit;
}
$userfields = 'u.id,u.lastaccess,u.firstname,u.lastname,u.email,u.phone1,u.picture,u.imagealt,
u.firstnamephonetic,u.lastnamephonetic,u.middlename,u.alternatename';
// Debugging($context->id);
foreach ($roles as $key => $role) {
$att = 'role_'.$key;
if (!empty($this->config->$att)) {
if ($this->config->$att == 1) {
$contacts = $this->get_role_users($key, $context, $inherit, $userfields, $orderby, null, '', '', 30);
// Because the role search finds the custom name and the proper name in brackets.
if (!empty($contacts)) {
if ($shortened = strstr($role, '(', true)) {
$content .= html_writer::tag('h2', trim($shortened));
} else {
$content .= html_writer::tag('h2', $role);
}
}
// Now display each contact.
foreach ($contacts as $contact) {
$content .= html_writer::start_tag('div', array('class' => 'ccard'));
$content .= $OUTPUT->user_picture($contact, array('size' => 50));
$content .= html_writer::start_tag('div', array('class' => 'info'));
if ($contact->lastaccess > (time() - 300)) {
// Online :)!
$status = 'online';
} else {
// Offline :(!
$status = 'offline';
}
$content .= html_writer::start_tag('div', array('class' => 'name '.$status));
$content .= $this->shorten_name($contact->firstname)." ".$this->shorten_name($contact->lastname);
$content .= html_writer::end_tag('div');
$content .= html_writer::empty_tag('img', array(
'src' => $OUTPUT->pix_url($status, 'block_course_contacts'),
'title' => get_string($status, 'block_course_contacts'),
'alt' => get_string($status, 'block_course_contacts'),
'class' => 'status'));
$content .= html_writer::empty_tag('hr');
$content .= html_writer::start_tag('div', array('class' => 'comms'));
// Unless they are us.
if ($USER->id != $contact->id) {
// Should we display email?
if ($this->config->email == 1) {
// RO - removed, causing errors, retained for dev
// if ($CFG->block_co_co_simpleemail) {
// $url = new moodle_url('/blocks/course_contacts/email.php', array(
// 'touid' => $contact->id, 'cid'=>$COURSE->id));
// } else {
$url = 'mailto:'.strtolower($contact->email);
// }
$content .= html_writer::link($url, html_writer::empty_tag('img', array(
'src' => $OUTPUT->pix_url('mail', 'block_course_contacts'),
'title' => get_string('email', 'block_course_contacts').' '.$contact->firstname,
'alt' => get_string('email', 'block_course_contacts').' '.$contact->firstname)),
array('target' => '_blank'));
}
// What about messages?
if ($this->config->message == 1) {
$url = new moodle_url('/message/index.php', array('id' => $contact->id));
$content .= html_writer::link($url, html_writer::empty_tag('img', array(
'src' => $OUTPUT->pix_url('message', 'block_course_contacts'),
'title' => get_string('message', 'block_course_contacts').' '.$contact->firstname,
'alt' => get_string('message', 'block_course_contacts').' '.$contact->firstname)),
array('target' => '_blank'));
}
// And phone numbers?
if ($this->config->phone == 1 && $contact->phone1 != "") {
$url = 'tel:'.$contact->phone1;
$content .= html_writer::link($url, html_writer::empty_tag('img', array(
'src' => $OUTPUT->pix_url('phone', 'block_course_contacts'),
'title' => get_string('phone', 'block_course_contacts').' '.$contact->phone1,
'alt' => get_string('phone', 'block_course_contacts').' '.$contact->phone1)),
array());
}
}
$content .= html_writer::end_tag('div');
$content .= html_writer::end_tag('div');
$content .= html_writer::end_tag('div');
}
}
}
}
$content .= html_writer::end_tag('div');
$this->content->text = $content;
return $this->content;
}
public function instance_allow_config() {
return true;
}
}