1 |
efrain |
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 |
/**
|
|
|
19 |
* Defines the class for the Message My Teacher block
|
|
|
20 |
*
|
|
|
21 |
* @package block_messageteacher
|
|
|
22 |
* @author Mark Johnson <mark@barrenfrozenwasteland.com>
|
|
|
23 |
* @copyright 2010-2012 Tauntons College, UK. 2012 Onwards Mark Johnson
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Class definition for the Message My Teacher block
|
|
|
31 |
*
|
|
|
32 |
* @copyright 2010-2012 Tauntons College, UK. 2012 Onwards Mark Johnson
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
*/
|
601 |
ariadna |
35 |
class block_messageteacher extends block_base
|
|
|
36 |
{
|
1 |
efrain |
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Initialise the block title.
|
|
|
40 |
*/
|
601 |
ariadna |
41 |
public function init()
|
|
|
42 |
{
|
1 |
efrain |
43 |
$this->title = get_string('pluginname', 'block_messageteacher');
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Set applicable formats, any page except My Moodle.
|
|
|
48 |
*/
|
601 |
ariadna |
49 |
public function applicable_formats()
|
|
|
50 |
{
|
|
|
51 |
return array('all' => true, 'my' => false);
|
1 |
efrain |
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Enable block config.
|
|
|
56 |
*
|
|
|
57 |
* @return true
|
|
|
58 |
*/
|
601 |
ariadna |
59 |
public function has_config()
|
|
|
60 |
{
|
1 |
efrain |
61 |
return true;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Gets a list of "teachers" with the defined role, and displays a link to message each
|
|
|
66 |
*
|
|
|
67 |
* @return stdClass
|
|
|
68 |
*/
|
601 |
ariadna |
69 |
public function get_content()
|
|
|
70 |
{
|
1 |
efrain |
71 |
global $COURSE, $USER, $DB, $OUTPUT, $PAGE;
|
|
|
72 |
|
|
|
73 |
if ($this->content !== null) {
|
|
|
74 |
return $this->content;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
$this->content = new stdClass;
|
|
|
78 |
$this->content->text = '';
|
|
|
79 |
$this->content->footer = '';
|
|
|
80 |
|
|
|
81 |
$usegroups = get_config('block_messageteacher', 'groups');
|
|
|
82 |
$coursehasgroups = groups_get_all_groups($COURSE->id);
|
|
|
83 |
|
|
|
84 |
if (get_config('block_messageteacher', 'roles')) {
|
|
|
85 |
$roles = explode(',', get_config('block_messageteacher', 'roles'));
|
|
|
86 |
list($usql, $uparams) = $DB->get_in_or_equal($roles);
|
|
|
87 |
$params = array($COURSE->id, CONTEXT_COURSE);
|
|
|
88 |
$select = 'SELECT DISTINCT u.id, u.firstname, u.lastname, u.firstnamephonetic,
|
|
|
89 |
u.lastnamephonetic, u.middlename, u.alternatename, u.picture, u.imagealt, u.email ';
|
|
|
90 |
$from = 'FROM {role_assignments} ra
|
|
|
91 |
JOIN {context} c ON ra.contextid = c.id
|
|
|
92 |
JOIN {user} u ON u.id = ra.userid ';
|
|
|
93 |
$where = 'WHERE ((c.instanceid = ? AND c.contextlevel = ?)';
|
|
|
94 |
if (get_config('block_messageteacher', 'includecoursecat')) {
|
|
|
95 |
$params = array_merge($params, array($COURSE->category, CONTEXT_COURSECAT));
|
|
|
96 |
$where .= ' OR (c.instanceid = ? AND c.contextlevel = ?))';
|
|
|
97 |
} else {
|
|
|
98 |
$where .= ')';
|
|
|
99 |
}
|
|
|
100 |
$params = array_merge($params, array($USER->id), $uparams);
|
601 |
ariadna |
101 |
$where .= ' AND userid != ? AND roleid ' . $usql;
|
1 |
efrain |
102 |
$order = ' ORDER BY u.firstname ASC, u.lastname';
|
|
|
103 |
|
601 |
ariadna |
104 |
if ($teachers = $DB->get_records_sql($select . $from . $where . $order, $params)) {
|
1 |
efrain |
105 |
if ($usegroups && $coursehasgroups) {
|
|
|
106 |
try {
|
|
|
107 |
$groupteachers = array();
|
|
|
108 |
$usergroupings = groups_get_user_groups($COURSE->id, $USER->id);
|
|
|
109 |
if (empty($usergroupings)) {
|
|
|
110 |
throw new Exception('nogroupmembership');
|
|
|
111 |
} else {
|
|
|
112 |
foreach ($usergroupings as $usergroups) {
|
|
|
113 |
if (empty($usergroups)) {
|
|
|
114 |
throw new Exception('nogroupmembership');
|
|
|
115 |
} else {
|
|
|
116 |
foreach ($usergroups as $usergroup) {
|
|
|
117 |
foreach ($teachers as $teacher) {
|
|
|
118 |
if (groups_is_member($usergroup, $teacher->id)) {
|
|
|
119 |
$groupteachers[$teacher->id] = $teacher;
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
if (empty($groupteachers)) {
|
|
|
126 |
throw new Exception('nogroupteachers');
|
|
|
127 |
} else {
|
|
|
128 |
$teachers = $groupteachers;
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
} catch (Exception $e) {
|
|
|
132 |
$this->content->text = get_string($e->getMessage(), 'block_messageteacher');
|
|
|
133 |
return $this->content;
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
$items = array();
|
|
|
138 |
foreach ($teachers as $teacher) {
|
601 |
ariadna |
139 |
$urlparams = array(
|
1 |
efrain |
140 |
'courseid' => $COURSE->id,
|
|
|
141 |
'referurl' => $this->page->url->out(),
|
|
|
142 |
'recipientid' => $teacher->id
|
|
|
143 |
);
|
|
|
144 |
$url = new moodle_url('/blocks/messageteacher/message.php', $urlparams);
|
|
|
145 |
$picture = '';
|
|
|
146 |
if (get_config('block_messageteacher', 'showuserpictures')) {
|
|
|
147 |
$picture = new user_picture($teacher);
|
|
|
148 |
$picture->link = false;
|
|
|
149 |
$picture->size = 50;
|
|
|
150 |
$picture = $OUTPUT->render($picture);
|
|
|
151 |
}
|
709 |
ariadna |
152 |
$text_content_name = 'Contacta acá al docente ';
|
708 |
ariadna |
153 |
$name = html_writer::tag('span', $text_content_name . fullname($teacher), ['class' => 'teacher-fullname']);
|
|
|
154 |
$emailLabel = 'o puedes enviarle un correo a ';
|
602 |
ariadna |
155 |
$email = html_writer::tag('span', $emailLabel . $teacher->email, ['class' => 'teacher-email']);
|
1 |
efrain |
156 |
$attrs = [
|
|
|
157 |
'href' => $url,
|
|
|
158 |
'class' => 'messageteacher_link',
|
|
|
159 |
'data-courseid' => $COURSE->id,
|
|
|
160 |
'data-referurl' => $this->page->url->out(),
|
|
|
161 |
'data-recipientid' => $teacher->id
|
|
|
162 |
];
|
601 |
ariadna |
163 |
$items[] = html_writer::tag('a', $picture . $name . '<br>' . $email, $attrs);
|
1 |
efrain |
164 |
}
|
|
|
165 |
$this->content->text = html_writer::alist($items, [
|
|
|
166 |
'data-contextid' => $this->context->id,
|
|
|
167 |
'data-appendurl' => get_config('block_messageteacher', 'appendurl'),
|
|
|
168 |
'data-referurl' => $this->page->url->out(),
|
|
|
169 |
]);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
$PAGE->requires->js_call_amd('block_messageteacher/form', 'init');
|
|
|
173 |
} else {
|
|
|
174 |
$this->content->text = "No teacher role defined";
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
return $this->content;
|
|
|
178 |
}
|
|
|
179 |
}
|