Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 * Block "people"
19
 *
20
 * @package    block_people
21
 * @copyright  2013 Alexander Bias, Ulm University <alexander.bias@uni-ulm.de>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
/**
26
 * Class block_people
27
 *
28
 * @package    block_people
29
 * @copyright  2013 Alexander Bias, Ulm University <alexander.bias@uni-ulm.de>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
class block_people extends block_base {
33
    /**
34
     * init function
35
     * @return void
36
     */
37
    public function init() {
38
        $this->title = get_string('pluginname', 'block_people');
39
    }
40
 
41
    /**
42
     * applicable_formats function
43
     * @return array
44
     */
45
    public function applicable_formats() {
46
        return ['course-view' => true, 'site' => true];
47
    }
48
 
49
    /**
50
     * has_config function
51
     * @return bool
52
     */
53
    public function has_config() {
54
        return true;
55
    }
56
 
57
    /**
58
     * instance_allow_multiple function
59
     * @return bool
60
     */
61
    public function instance_allow_multiple() {
62
        return false;
63
    }
64
 
65
    /**
66
     * instance_can_be_hidden function
67
     * @return bool
68
     */
69
    public function instance_can_be_hidden() {
70
        // By default, instances can be hidden by the user.
71
        $hideblock = true;
72
        // If config 'hideblock' is disabled.
73
        if ((get_config('block_people', 'hideblock')) == '0') {
74
            // Set value to false, so instance cannot be hidden.
75
            $hideblock = false;
76
        }
77
        return $hideblock;
78
    }
79
 
80
    /**
81
     * get_content function
82
     * @return string
83
     */
84
    public function get_content() {
85
        global $COURSE, $CFG, $OUTPUT, $USER;
86
 
87
        if ($this->content !== null) {
88
            return $this->content;
89
        }
90
 
91
        if (empty($this->instance)) {
92
            $this->content = '';
93
            return $this->content;
94
        }
95
 
96
        // Prepare output.
97
        $this->content = new stdClass();
98
        $this->content->text = '';
99
        $this->content->footer = '';
100
 
101
        // Get context.
102
        $currentcontext = $this->page->context;
103
 
104
        // Get teachers separated by roles.
105
        $roles = get_config('block_people', 'roles');
106
        if (!empty($roles)) {
107
            $teacherroles = explode(',', $roles);
108
            $teachers = get_role_users($teacherroles,
109
                    $currentcontext,
110
                    true,
111
                    'ra.id AS raid, r.id AS roleid, r.sortorder, u.id, u.lastname, u.firstname, u.firstnamephonetic,
112
                            u.lastnamephonetic, u.middlename, u.alternatename, u.picture, u.imagealt, u.email, u.suspended',
113
                    'r.sortorder ASC, u.lastname ASC, u.firstname ASC');
114
        } else {
115
            $teachers = [];
116
        }
117
 
118
        // Get role names / aliases in course context.
119
        $rolenames = role_get_names($currentcontext, ROLENAME_ALIAS, true);
120
 
121
        // Get multiple roles config.
122
        $multipleroles = get_config('block_people', 'multipleroles');
123
 
124
        // Start teachers list.
125
        $this->content->text .= html_writer::start_tag('div', ['class' => 'teachers']);
126
 
127
        // Initialize running variables.
128
        $teacherrole = null;
129
        $displayedteachers = [];
130
 
131
        // Check every teacher.
132
        foreach ($teachers as $teacher) {
133
            // If the user is suspended, skip him.
134
            if ($teacher->suspended == true) {
135
                continue;
136
            }
137
 
138
            // If users should only be listed once.
139
            if (!$multipleroles) {
140
                // Continue if we have already shown this user.
141
                if (isset($displayedteachers[$teacher->id])) {
142
                    continue;
143
                }
144
                // Remember that we have shown this user.
145
                $displayedteachers[$teacher->id] = 1;
146
 
147
                // Otherwise.
148
            } else {
149
                // Continue if we have already shown this user.
150
                if (isset($displayedteachers[$teacher->id][$teacher->roleid])) {
151
                    continue;
152
                }
153
                // Remember that we have shown this user and his role.
154
                $displayedteachers[$teacher->id][$teacher->roleid] = 1;
155
            }
156
 
157
            // If we have to process a new role.
158
            if ($teacherrole != $teacher->roleid) {
159
                // End previous role list if necessary.
160
                if ($teacherrole != null) {
161
                    $this->content->text .= html_writer::end_tag('ul');
162
                }
163
 
164
                // Write heading and open new role list.
165
                $teacherrole = $teacher->roleid;
166
                $this->content->text .= html_writer::tag('h6', $rolenames[$teacherrole]);
167
                $this->content->text .= html_writer::start_tag('ul');
168
            }
169
 
170
            // Start output teacher.
171
            $this->content->text .= html_writer::start_tag('li');
172
 
173
            // Create user object for picture output.
174
            $user = new stdClass();
175
            $user->id = $teacher->id;
176
            $user->lastname = $teacher->lastname;
177
            $user->firstname = $teacher->firstname;
178
            $user->lastnamephonetic = $teacher->lastnamephonetic;
179
            $user->firstnamephonetic = $teacher->firstnamephonetic;
180
            $user->middlename = $teacher->middlename;
181
            $user->alternatename = $teacher->alternatename;
182
            $user->picture = $teacher->picture;
183
            $user->imagealt = $teacher->imagealt;
184
            $user->email = $teacher->email;
185
 
186
            // Teacher image.
187
            $this->content->text .= html_writer::start_tag('div', ['class' => 'image']);
188
            if (get_config('block_people', 'linkavatar') == 1 && has_capability('moodle/user:viewdetails', $currentcontext)) {
189
                $this->content->text .= $OUTPUT->user_picture($user,
190
                        ['size' => 35, 'link' => true, 'courseid' => $COURSE->id, 'includefullname' => false]);
191
            } else {
192
                $this->content->text .= $OUTPUT->user_picture($user,
193
                        ['size' => 35, 'link' => false, 'courseid' => $COURSE->id, 'includefullname' => false]);
194
            }
195
            $this->content->text .= html_writer::end_tag('div');
196
 
197
            // Teacher details.
198
            $this->content->text .= html_writer::start_tag('div', ['class' => 'details']);
199
            $this->content->text .= html_writer::start_tag('div', ['class' => 'name']);
200
            if (get_config('block_people', 'linkname') == 1 && has_capability('moodle/user:viewdetails', $currentcontext)) {
201
                $linkurl = new moodle_url('/user/view.php', ['id' => $teacher->id, 'course' => $COURSE->id]);
202
                $this->content->text .= html_writer::link($linkurl, fullname($teacher));
203
            } else {
204
                $this->content->text .= fullname($teacher);
205
            }
206
            $this->content->text .= html_writer::end_tag('div');
207
            $this->content->text .= html_writer::start_tag('div', ['class' => 'icons']);
208
            if (get_config('block_people', 'linkmessaging') == 1 &&
209
                    $CFG->messaging && has_capability('moodle/site:sendmessage', $currentcontext) && $teacher->id != $USER->id &&
210
                    \core_message\api::can_send_message($teacher->id, $USER->id)) {
211
                $this->content->text .= html_writer::start_tag('a',
212
                        ['href' => new moodle_url('/message/index.php', ['id' => $teacher->id]),
213
                              'title' => get_string('sendmessageto', 'core_message', fullname($teacher)), ]);
214
                $this->content->text .= $OUTPUT->pix_icon('t/message',
215
                        get_string('sendmessageto', 'core_message', fullname($teacher)), 'moodle');
216
                $this->content->text .= html_writer::end_tag('a');
217
            }
218
            $this->content->text .= html_writer::end_tag('div');
219
            $this->content->text .= html_writer::end_tag('div');
220
 
221
            // End output teacher.
222
            $this->content->text .= html_writer::end_tag('li');
223
 
224
        }
225
 
226
        // End role list if necessary.
227
        if ($teacherrole != null) {
228
            $this->content->text .= html_writer::end_tag('ul');
229
        }
230
 
231
        // End teachers list.
232
        $this->content->text .= html_writer::end_tag('div');
233
 
234
        // Output participants list if the setting linkparticipantspage is enabled.
235
        if ((get_config('block_people', 'linkparticipantspage')) != 0) {
236
            $this->content->text .= html_writer::start_tag('div', ['class' => 'participants']);
237
            $this->content->text .= html_writer::tag('h6', get_string('participants'));
238
 
239
            // Only if user is allow to see participants list.
240
            if (course_can_view_participants($currentcontext)) {
241
                $this->content->text .= html_writer::start_tag('a',
242
                    ['href' => new moodle_url('/user/index.php', ['contextid' => $currentcontext->id]),
243
                          'title' => get_string('participants'), ]);
244
                $this->content->text .= $OUTPUT->pix_icon('i/users',
245
                        get_string('participants', 'core'), 'moodle');
246
                $this->content->text .= get_string('participantslist', 'block_people');
247
                $this->content->text .= html_writer::end_tag('a');
248
            } else {
249
                $this->content->text .= html_writer::start_tag('span', ['class' => 'hint']);
250
                $this->content->text .= get_string('noparticipantslist', 'block_people');
251
                $this->content->text .= html_writer::end_tag('span');
252
            }
253
 
254
            $this->content->text .= html_writer::end_tag('div');
255
        }
256
 
257
        return $this->content;
258
    }
259
 
260
    /**
261
     * Return the plugin config settings for external functions.
262
     *
263
     * @return stdClass the configs for both the block instance and plugin
264
     * @since Moodle 3.8
265
     */
266
    public function get_config_for_external() {
267
 
268
        // Return all settings for all users since it is safe (no private keys, etc..).
269
        $instanceconfigs = !empty($this->config) ? $this->config : new stdClass();
270
        $pluginconfigs = get_config('block_people');
271
 
272
        return (object) [
273
                'instance' => $instanceconfigs,
274
                'plugin' => $pluginconfigs,
275
        ];
276
    }
277
}