Proyectos de Subversion Moodle

Rev

Rev 601 | Ir a la última revisión | | 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
/**
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
 */
35
class block_messageteacher extends block_base {
36
 
37
    /**
38
     * Initialise the block title.
39
     */
40
    public function init() {
41
        $this->title = get_string('pluginname', 'block_messageteacher');
42
    }
43
 
44
    /**
45
     * Set applicable formats, any page except My Moodle.
46
     */
47
    public function applicable_formats() {
48
          return array('all' => true, 'my' => false);
49
    }
50
 
51
    /**
52
     * Enable block config.
53
     *
54
     * @return true
55
     */
56
    public function has_config() {
57
        return true;
58
    }
59
 
60
    /**
61
     * Gets a list of "teachers" with the defined role, and displays a link to message each
62
     *
63
     * @return stdClass
64
     */
65
    public function get_content() {
66
        global $COURSE, $USER, $DB, $OUTPUT, $PAGE;
67
 
68
        if ($this->content !== null) {
69
            return $this->content;
70
        }
71
 
72
        $this->content = new stdClass;
73
        $this->content->text = '';
74
        $this->content->footer = '';
75
 
76
        $usegroups = get_config('block_messageteacher', 'groups');
77
        $coursehasgroups = groups_get_all_groups($COURSE->id);
78
 
79
        if (get_config('block_messageteacher', 'roles')) {
80
            $roles = explode(',', get_config('block_messageteacher', 'roles'));
81
            list($usql, $uparams) = $DB->get_in_or_equal($roles);
82
            $params = array($COURSE->id, CONTEXT_COURSE);
83
            $select = 'SELECT DISTINCT u.id, u.firstname, u.lastname, u.firstnamephonetic,
84
                    u.lastnamephonetic, u.middlename, u.alternatename, u.picture, u.imagealt, u.email ';
85
            $from = 'FROM {role_assignments} ra
86
                    JOIN {context} c ON ra.contextid = c.id
87
                    JOIN {user} u ON u.id = ra.userid ';
88
            $where = 'WHERE ((c.instanceid = ? AND c.contextlevel = ?)';
89
            if (get_config('block_messageteacher', 'includecoursecat')) {
90
                $params = array_merge($params, array($COURSE->category, CONTEXT_COURSECAT));
91
                $where .= ' OR (c.instanceid = ? AND c.contextlevel = ?))';
92
            } else {
93
                $where .= ')';
94
            }
95
            $params = array_merge($params, array($USER->id), $uparams);
96
            $where .= ' AND userid != ? AND roleid '.$usql;
97
            $order = ' ORDER BY u.firstname ASC, u.lastname';
98
 
99
            if ($teachers = $DB->get_records_sql($select.$from.$where.$order, $params)) {
100
                if ($usegroups && $coursehasgroups) {
101
                    try {
102
                        $groupteachers = array();
103
                        $usergroupings = groups_get_user_groups($COURSE->id, $USER->id);
104
                        if (empty($usergroupings)) {
105
                            throw new Exception('nogroupmembership');
106
                        } else {
107
                            foreach ($usergroupings as $usergroups) {
108
                                if (empty($usergroups)) {
109
                                    throw new Exception('nogroupmembership');
110
                                } else {
111
                                    foreach ($usergroups as $usergroup) {
112
                                        foreach ($teachers as $teacher) {
113
                                            if (groups_is_member($usergroup, $teacher->id)) {
114
                                                $groupteachers[$teacher->id] = $teacher;
115
                                            }
116
                                        }
117
                                    }
118
                                }
119
                            }
120
                            if (empty($groupteachers)) {
121
                                throw new Exception('nogroupteachers');
122
                            } else {
123
                                $teachers = $groupteachers;
124
                            }
125
                        }
126
                    } catch (Exception $e) {
127
                        $this->content->text = get_string($e->getMessage(), 'block_messageteacher');
128
                        return $this->content;
129
                    }
130
                }
131
 
132
                $items = array();
133
                foreach ($teachers as $teacher) {
134
                    $urlparams = array (
135
                        'courseid' => $COURSE->id,
136
                        'referurl' => $this->page->url->out(),
137
                        'recipientid' => $teacher->id
138
                    );
139
                    $url = new moodle_url('/blocks/messageteacher/message.php', $urlparams);
140
                    $picture = '';
141
                    if (get_config('block_messageteacher', 'showuserpictures')) {
142
                        $picture = new user_picture($teacher);
143
                        $picture->link = false;
144
                        $picture->size = 50;
145
                        $picture = $OUTPUT->render($picture);
146
                    }
147
                    $name = html_writer::tag('span', fullname($teacher));
148
                    $attrs = [
149
                        'href' => $url,
150
                        'class' => 'messageteacher_link',
151
                        'data-courseid' => $COURSE->id,
152
                        'data-referurl' => $this->page->url->out(),
153
                        'data-recipientid' => $teacher->id
154
                    ];
155
                    $items[] = html_writer::tag('a', $picture.$name, $attrs);
156
                }
157
                $this->content->text = html_writer::alist($items, [
158
                    'data-contextid' => $this->context->id,
159
                    'data-appendurl' => get_config('block_messageteacher', 'appendurl'),
160
                    'data-referurl' => $this->page->url->out(),
161
                ]);
162
            }
163
 
164
            $PAGE->requires->js_call_amd('block_messageteacher/form', 'init');
165
        } else {
166
            $this->content->text = "No teacher role defined";
167
        }
168
 
169
        return $this->content;
170
    }
171
}