-
Notifications
You must be signed in to change notification settings - Fork 9
/
block_messageteacher.php
171 lines (155 loc) · 6.9 KB
/
block_messageteacher.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
<?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/>.
/**
* Defines the class for the Message My Teacher block
*
* @package block_messageteacher
* @author Mark Johnson <[email protected]>
* @copyright 2010-2012 Tauntons College, UK. 2012 Onwards Mark Johnson
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Class definition for the Message My Teacher block
*
* @copyright 2010-2012 Tauntons College, UK. 2012 Onwards Mark Johnson
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_messageteacher extends block_base {
/**
* Initialise the block title.
*/
public function init() {
$this->title = get_string('pluginname', 'block_messageteacher');
}
/**
* Set applicable formats, any page except My Moodle.
*/
public function applicable_formats() {
return array('all' => true, 'my' => false);
}
/**
* Enable block config.
*
* @return true
*/
public function has_config() {
return true;
}
/**
* Gets a list of "teachers" with the defined role, and displays a link to message each
*
* @return stdClass
*/
public function get_content() {
global $COURSE, $USER, $DB, $OUTPUT, $PAGE;
if ($this->content !== null) {
return $this->content;
}
$this->content = new stdClass;
$this->content->text = '';
$this->content->footer = '';
$usegroups = get_config('block_messageteacher', 'groups');
$coursehasgroups = groups_get_all_groups($COURSE->id);
if (get_config('block_messageteacher', 'roles')) {
$roles = explode(',', get_config('block_messageteacher', 'roles'));
list($usql, $uparams) = $DB->get_in_or_equal($roles);
$params = array($COURSE->id, CONTEXT_COURSE);
$select = 'SELECT DISTINCT u.id, u.firstname, u.lastname, u.firstnamephonetic,
u.lastnamephonetic, u.middlename, u.alternatename, u.picture, u.imagealt, u.email ';
$from = 'FROM {role_assignments} ra
JOIN {context} c ON ra.contextid = c.id
JOIN {user} u ON u.id = ra.userid ';
$where = 'WHERE ((c.instanceid = ? AND c.contextlevel = ?)';
if (get_config('block_messageteacher', 'includecoursecat')) {
$params = array_merge($params, array($COURSE->category, CONTEXT_COURSECAT));
$where .= ' OR (c.instanceid = ? AND c.contextlevel = ?))';
} else {
$where .= ')';
}
$params = array_merge($params, array($USER->id), $uparams);
$where .= ' AND userid != ? AND roleid '.$usql;
$order = ' ORDER BY u.firstname ASC, u.lastname';
if ($teachers = $DB->get_records_sql($select.$from.$where.$order, $params)) {
if ($usegroups && $coursehasgroups) {
try {
$groupteachers = array();
$usergroupings = groups_get_user_groups($COURSE->id, $USER->id);
if (empty($usergroupings)) {
throw new Exception('nogroupmembership');
} else {
foreach ($usergroupings as $usergroups) {
if (empty($usergroups)) {
throw new Exception('nogroupmembership');
} else {
foreach ($usergroups as $usergroup) {
foreach ($teachers as $teacher) {
if (groups_is_member($usergroup, $teacher->id)) {
$groupteachers[$teacher->id] = $teacher;
}
}
}
}
}
if (empty($groupteachers)) {
throw new Exception('nogroupteachers');
} else {
$teachers = $groupteachers;
}
}
} catch (Exception $e) {
$this->content->text = get_string($e->getMessage(), 'block_messageteacher');
return $this->content;
}
}
$items = array();
foreach ($teachers as $teacher) {
$urlparams = array (
'courseid' => $COURSE->id,
'referurl' => $this->page->url->out(),
'recipientid' => $teacher->id
);
$url = new moodle_url('/blocks/messageteacher/message.php', $urlparams);
$picture = '';
if (get_config('block_messageteacher', 'showuserpictures')) {
$picture = new user_picture($teacher);
$picture->link = false;
$picture->size = 50;
$picture = $OUTPUT->render($picture);
}
$name = html_writer::tag('span', fullname($teacher));
$attrs = [
'href' => $url,
'class' => 'messageteacher_link',
'data-courseid' => $COURSE->id,
'data-referurl' => $this->page->url->out(),
'data-recipientid' => $teacher->id
];
$items[] = html_writer::tag('a', $picture.$name, $attrs);
}
$this->content->text = html_writer::alist($items, [
'data-contextid' => $this->context->id,
'data-appendurl' => get_config('block_messageteacher', 'appendurl'),
'data-referurl' => $this->page->url->out(),
]);
}
$PAGE->requires->js_call_amd('block_messageteacher/form', 'init');
} else {
$this->content->text = "No teacher role defined";
}
return $this->content;
}
}