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
 * Online users block.
19
 *
20
 * @package    block_online_users
21
 * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
use block_online_users\fetcher;
26
 
27
/**
28
 * This block needs to be reworked.
29
 * The new roles system does away with the concepts of rigid student and
30
 * teacher roles.
31
 */
32
class block_online_users extends block_base {
33
    function init() {
34
        $this->title = get_string('pluginname','block_online_users');
35
    }
36
 
37
    function has_config() {
38
        return true;
39
    }
40
 
41
    function get_content() {
42
        global $USER, $CFG, $DB, $OUTPUT;
43
 
44
        if ($this->content !== NULL) {
45
            return $this->content;
46
        }
47
 
48
        $this->content = new stdClass;
49
        $this->content->text = '';
50
        $this->content->footer = '';
51
 
52
        if (empty($this->instance)) {
53
            return $this->content;
54
        }
55
 
56
        $timetoshowusers = 300; //Seconds default
57
        if (isset($CFG->block_online_users_timetosee)) {
58
            $timetoshowusers = $CFG->block_online_users_timetosee * 60;
59
        }
60
        $now = time();
61
 
62
        //Calculate if we are in separate groups
63
        $isseparategroups = ($this->page->course->groupmode == SEPARATEGROUPS
64
                             && $this->page->course->groupmodeforce
65
                             && !has_capability('moodle/site:accessallgroups', $this->page->context));
66
 
67
        //Get the user current group
68
        $currentgroup = $isseparategroups ? groups_get_course_group($this->page->course) : NULL;
69
 
70
        $sitelevel = $this->page->course->id == SITEID || $this->page->context->contextlevel < CONTEXT_COURSE;
71
 
72
        $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $this->page->context,
73
                $sitelevel, $this->page->course->id);
74
 
75
        //Calculate minutes
76
        $minutes  = floor($timetoshowusers/60);
77
        $periodminutes = get_string('periodnminutes', 'block_online_users', $minutes);
78
 
79
        // Count users.
80
        $usercount = $onlineusers->count_users();
81
        if ($usercount === 0) {
82
            $usercount = get_string('nouser', 'block_online_users');
83
        } else if ($usercount === 1) {
84
            $usercount = get_string('numuser', 'block_online_users', $usercount);
85
        } else {
86
            $usercount = get_string('numusers', 'block_online_users', $usercount);
87
        }
88
 
89
        $this->content->text = '<div class="info">'.$usercount.' ('.$periodminutes.')</div>';
90
 
91
        // Verify if we can see the list of users, if not just print number of users.
92
        // If the current user is not logged in OR it's a guest then don't show any users.
93
        if (!has_capability('block/online_users:viewlist', $this->page->context)
94
                || isguestuser() || !isloggedin()) {
95
            return $this->content;
96
        }
97
 
98
        $userlimit = 50; // We'll just take the most recent 50 maximum.
99
        $initialcount = 0;
100
        if ($users = $onlineusers->get_users($userlimit)) {
101
            require_once($CFG->dirroot . '/user/lib.php');
102
            $initialcount = count($users);
103
            foreach ($users as $user) {
104
                if (!user_can_view_profile($user)) {
105
                    unset($users[$user->id]);
106
                    continue;
107
                }
108
                $users[$user->id]->fullname = fullname($user);
109
            }
110
        } else {
111
            $users = array();
112
        }
113
 
114
        //Now, we have in users, the list of users to show
115
        //Because they are online
116
        if (!empty($users)) {
117
            $this->page->requires->js_call_amd('block_online_users/change_user_visibility', 'init');
118
            //Accessibility: Don't want 'Alt' text for the user picture; DO want it for the envelope/message link (existing lang string).
119
            //Accessibility: Converted <div> to <ul>, inherit existing classes & styles.
120
            $this->content->text .= "<ul class='list'>\n";
121
            if (isloggedin() && has_capability('moodle/site:sendmessage', $this->page->context)
122
                           && !empty($CFG->messaging) && !isguestuser()) {
123
                $canshowicon = true;
124
            } else {
125
                $canshowicon = false;
126
            }
127
            foreach ($users as $user) {
128
                $this->content->text .= '<li class="listentry">';
129
                $timeago = format_time($now - $user->lastaccess); //bruno to calculate correctly on frontpage
130
 
131
                if (isguestuser($user)) {
132
                    $this->content->text .= '<div class="user">'.$OUTPUT->user_picture($user, array('size'=>16, 'alttext'=>false));
133
                    $this->content->text .= get_string('guestuser').'</div>';
134
 
135
                } else { // Not a guest user.
136
                    $this->content->text .= '<div class="user">';
137
                    $this->content->text .= '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$this->page->course->id.'" title="'.$timeago.'">';
138
                    $avataroptions = [
139
                        'size' => 30,
140
                        'class' => 'userpicture align-middle',
141
                        'visibletoscreenreaders' => false,
142
                        'link' => false,
143
                    ];
144
                    $this->content->text .= $OUTPUT->user_picture($user, $avataroptions) . $user->fullname . '</a></div>';
145
 
146
                    if ($USER->id == $user->id) {
147
                        if ($CFG->block_online_users_onlinestatushiding) {
148
                            $action = ($user->uservisibility != null && $user->uservisibility == 0) ? 'show' : 'hide';
149
                            $anchortagcontents = $OUTPUT->pix_icon('t/' . $action,
150
                                get_string('online_status:' . $action, 'block_online_users'));
151
                            $anchortag = html_writer::link("", $anchortagcontents,
152
                                array('title' => get_string('online_status:' . $action, 'block_online_users'),
153
                                    'data-action' => $action, 'data-userid' => $user->id, 'id' => 'change-user-visibility'));
154
 
155
                            $this->content->text .= '<div class="uservisibility">' . $anchortag . '</div>';
156
                        }
157
                    } else {
158
                        if ($canshowicon) {  // Only when logged in and messaging active etc.
159
                            $anchortagcontents = $OUTPUT->pix_icon('t/message', get_string('messageselectadd'));
160
                            $anchorurl = new moodle_url('/message/index.php', array('id' => $user->id));
161
                            $anchortag = html_writer::link($anchorurl, $anchortagcontents,
162
                                array('title' => get_string('messageselectadd')));
163
 
164
                            $this->content->text .= '<div class="message">'.$anchortag.'</div>';
165
                        }
166
                    }
167
                }
168
                $this->content->text .= "</li>\n";
169
            }
170
            if ($initialcount - count($users) > 0) {
171
                $this->content->text .= '<li class="listentry"><div class="otherusers">';
172
                $this->content->text .= html_writer::span(
173
                    get_string('otherusers', 'block_online_users', $initialcount - count($users))
174
                );
175
                $this->content->text .= "</div>";
176
                $this->content->text .= "</li>\n";
177
            }
178
            $this->content->text .= '</ul><div class="clearer"><!-- --></div>';
179
        }
180
 
181
        return $this->content;
182
    }
183
 
184
    /**
185
     * Return the plugin config settings for external functions.
186
     *
187
     * @return stdClass the configs for both the block instance and plugin
188
     * @since Moodle 3.8
189
     */
190
    public function get_config_for_external() {
191
        global $CFG;
192
 
193
        // Return all settings for all users since it is safe (no private keys, etc..).
194
        $configs = (object) [
195
            'timetosee' => $CFG->block_online_users_timetosee,
196
            'onlinestatushiding' => $CFG->block_online_users_onlinestatushiding
197
        ];
198
 
199
        return (object) [
200
            'instance' => new stdClass(),
201
            'plugin' => $configs,
202
        ];
203
    }
204
}
205
 
206