Skip to content
Permalink
Browse files

Merge branch 'MDL-6074-master' of git://github.com/mihailges/moodle

  • Loading branch information...
andrewnicols committed Jun 13, 2018
2 parents a9f6dd0 + 5226999 commit c3f13f534d0aca28d93a42309f7a4f5613fabfcc

Some generated files are not rendered by default. Learn more.

@@ -0,0 +1,171 @@
// 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/>.

/**
* A javascript module that handles the change of the user's visibility in the
* online users block.
*
* @module block_online_users/change_user_visibility
* @package block_online_users
* @copyright 2018 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['jquery', 'core/ajax', 'core/str', 'core/notification'],
function($, Ajax, Str, Notification) {

/**
* Selectors.
*
* @access private
* @type {Object}
*/
var SELECTORS = {
CHANGE_VISIBILITY_LINK: '#change-user-visibility',
CHANGE_VISIBILITY_ICON: '#change-user-visibility .icon'
};

/**
* Change user visibility in the online users block.
*
* @method changeVisibility
* @param {String} action
* @param {String} userid
* @private
*/
var changeVisibility = function(action, userid) {

var value = action == "show" ? 1 : 0;
var preferences = [{
'name': 'block_online_users_uservisibility',
'value': value,
'userid': userid
}];

var request = {
methodname: 'core_user_set_user_preferences',
args: {
preferences: preferences
}
};
Ajax.call([request])[0].then(function(data) {
if (data.saved) {
var newAction = oppositeAction(action);
changeVisibilityLinkAttr(newAction);
changeVisibilityIconAttr(newAction);
}
return;
}).catch(Notification.exception);
};

/**
* Get the opposite action.
*
* @method oppositeAction
* @param {String} action
* @return {String}
* @private
*/
var oppositeAction = function(action) {
return action == 'show' ? 'hide' : 'show';
};

/**
* Change the attribute values of the user visibility link in the online users block.
*
* @method changeVisibilityLinkAttr
* @param {String} action
* @private
*/
var changeVisibilityLinkAttr = function(action) {
getTitle(action).then(function(title) {
$(SELECTORS.CHANGE_VISIBILITY_LINK).attr({
'data-action': action,
'title': title
});
return;
}).catch(Notification.exception);
};

/**
* Change the attribute values of the user visibility icon in the online users block.
*
* @method changeVisibilityIconAttr
* @param {String} action
* @private
*/
var changeVisibilityIconAttr = function(action) {
var icon = $(SELECTORS.CHANGE_VISIBILITY_ICON);
getTitle(action).then(function(title) {
// Add the proper title to the icon.
$(icon).attr({
'title': title,
'aria-label': title
});
// If the icon is an image.
if (icon.is("img")) {
$(icon).attr({
'src': M.util.image_url('t/' + action),
'alt': title
});
} else {
// Add the new icon class and remove the old one.
$(icon).addClass(getIconClass(action));
$(icon).removeClass(getIconClass(oppositeAction(action)));
}
return;
}).catch(Notification.exception);
};

/**
* Get the proper class for the user visibility icon in the online users block.
*
* @method getIconClass
* @param {String} action
* @return {String}
* @private
*/
var getIconClass = function(action) {
return action == 'show' ? 'fa-eye-slash' : 'fa-eye';
};

/**
* Get the title description of the user visibility link in the online users block.
*
* @method getTitle
* @param {String} action
* @return {object} jQuery promise
* @private
*/
var getTitle = function(action) {
return Str.get_string('online_status:' + action, 'block_online_users');
};

return {
// Public variables and functions.
/**
* Initialise change user visibility function.
*
* @method init
*/
init: function() {
$(SELECTORS.CHANGE_VISIBILITY_LINK).on('click', function(e) {
e.preventDefault();
var action = ($(this).attr('data-action'));
var userid = ($(this).attr('data-userid'));
changeVisibility(action, userid);
});
}
};
});
@@ -105,6 +105,7 @@ function get_content() {
//Now, we have in users, the list of users to show
//Because they are online
if (!empty($users)) {
$this->page->requires->js_call_amd('block_online_users/change_user_visibility', 'init');
//Accessibility: Don't want 'Alt' text for the user picture; DO want it for the envelope/message link (existing lang string).
//Accessibility: Converted <div> to <ul>, inherit existing classes & styles.
$this->content->text .= "<ul class='list'>\n";
@@ -122,18 +123,30 @@ function get_content() {
$this->content->text .= '<div class="user">'.$OUTPUT->user_picture($user, array('size'=>16, 'alttext'=>false));
$this->content->text .= get_string('guestuser').'</div>';
} else {
} else { // Not a guest user.
$this->content->text .= '<div class="user">';
$this->content->text .= '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$this->page->course->id.'" title="'.$timeago.'">';
$this->content->text .= $OUTPUT->user_picture($user, array('size'=>16, 'alttext'=>false, 'link'=>false)) .$user->fullname.'</a></div>';
}
if ($canshowicon and ($USER->id != $user->id) and !isguestuser($user)) { // Only when logged in and messaging active etc
$anchortagcontents = $OUTPUT->pix_icon('t/message', get_string('messageselectadd'));
$anchorurl = new moodle_url('/message/index.php', array('id' => $user->id));
$anchortag = html_writer::link($anchorurl, $anchortagcontents,
array('title' => get_string('messageselectadd')));
$this->content->text .= '<div class="message">'.$anchortag.'</div>';
if ($USER->id == $user->id) {
$action = ($user->uservisibility != null && $user->uservisibility == 0) ? 'show' : 'hide';
$anchortagcontents = $OUTPUT->pix_icon('t/' . $action,
get_string('online_status:' . $action, 'block_online_users'));
$anchortag = html_writer::link("", $anchortagcontents,
array('title' => get_string('online_status:' . $action, 'block_online_users'),
'data-action' => $action, 'data-userid' => $user->id, 'id' => 'change-user-visibility'));
$this->content->text .= '<div class="uservisibility">' . $anchortag . '</div>';
} else {
if ($canshowicon) { // Only when logged in and messaging active etc.
$anchortagcontents = $OUTPUT->pix_icon('t/message', get_string('messageselectadd'));
$anchorurl = new moodle_url('/message/index.php', array('id' => $user->id));
$anchortag = html_writer::link($anchorurl, $anchortagcontents,
array('title' => get_string('messageselectadd')));
$this->content->text .= '<div class="message">'.$anchortag.'</div>';
}
}
}
$this->content->text .= "</li>\n";
}
@@ -67,13 +67,16 @@ public function __construct($currentgroup, $now, $timetoshowusers, $context, $si
* @param int $courseid The course id to check
*/
protected function set_sql($currentgroup, $now, $timetoshowusers, $context, $sitelevel, $courseid) {
global $USER, $DB;
$timefrom = 100 * floor(($now - $timetoshowusers) / 100); // Round to nearest 100 seconds for better query cache.
$groupmembers = "";
$groupselect = "";
$groupby = "";
$lastaccess = ", lastaccess";
$timeaccess = ", ul.timeaccess AS lastaccess";
$uservisibility = ", up.value AS uservisibility";
$params = array();
$userfields = \user_picture::fields('u', array('username'));
@@ -85,52 +88,75 @@ protected function set_sql($currentgroup, $now, $timetoshowusers, $context, $sit
$groupby = "GROUP BY $userfields";
$lastaccess = ", MAX(u.lastaccess) AS lastaccess";
$timeaccess = ", MAX(ul.timeaccess) AS lastaccess";
$uservisibility = ", MAX(up.value) AS uservisibility";
$params['currentgroup'] = $currentgroup;
}
$params['now'] = $now;
$params['timefrom'] = $timefrom;
$params['userid'] = $USER->id;
$params['name'] = 'block_online_users_uservisibility';
if ($sitelevel) {
$sql = "SELECT $userfields $lastaccess
$sql = "SELECT $userfields $lastaccess $uservisibility
FROM {user} u $groupmembers
LEFT JOIN {user_preferences} up ON up.userid = u.id
AND up.name = :name
WHERE u.lastaccess > :timefrom
AND u.lastaccess <= :now
AND u.deleted = 0
AND (" . $DB->sql_cast_char2int('up.value') . " = 1
OR up.value IS NULL
OR u.id = :userid)
$groupselect $groupby
ORDER BY lastaccess DESC ";
$csql = "SELECT COUNT(u.id)
FROM {user} u $groupmembers
WHERE u.lastaccess > :timefrom
AND u.lastaccess <= :now
AND u.deleted = 0
$groupselect";
FROM {user} u $groupmembers
LEFT JOIN {user_preferences} up ON up.userid = u.id
AND up.name = :name
WHERE u.lastaccess > :timefrom
AND u.lastaccess <= :now
AND u.deleted = 0
AND (" . $DB->sql_cast_char2int('up.value') . " = 1
OR up.value IS NULL
OR u.id = :userid)
$groupselect";
} else {
// Course level - show only enrolled users for now.
// TODO: add a new capability for viewing of all users (guests+enrolled+viewing).
list($esqljoin, $eparams) = get_enrolled_sql($context);
$params = array_merge($params, $eparams);
$sql = "SELECT $userfields $timeaccess
$sql = "SELECT $userfields $timeaccess $uservisibility
FROM {user_lastaccess} ul $groupmembers, {user} u
JOIN ($esqljoin) euj ON euj.id = u.id
LEFT JOIN {user_preferences} up ON up.userid = u.id
AND up.name = :name
WHERE ul.timeaccess > :timefrom
AND u.id = ul.userid
AND ul.courseid = :courseid
AND ul.timeaccess <= :now
AND u.deleted = 0
AND (" . $DB->sql_cast_char2int('up.value') . " = 1
OR up.value IS NULL
OR u.id = :userid)
$groupselect $groupby
ORDER BY lastaccess DESC";
$csql = "SELECT COUNT(u.id)
FROM {user_lastaccess} ul $groupmembers, {user} u
JOIN ($esqljoin) euj ON euj.id = u.id
LEFT JOIN {user_preferences} up ON up.userid = u.id
AND up.name = :name
WHERE ul.timeaccess > :timefrom
AND u.id = ul.userid
AND ul.courseid = :courseid
AND ul.timeaccess <= :now
AND u.deleted = 0
AND (" . $DB->sql_cast_char2int('up.value') . " = 1
OR up.value IS NULL
OR u.id = :userid)
$groupselect";
$params['courseid'] = $courseid;
@@ -24,23 +24,47 @@
namespace block_online_users\privacy;
use core_privacy\local\metadata\collection;
use core_privacy\local\request\writer;
use core_privacy\local\request\transform;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem for block_online_users implementing null_provider.
* Privacy Subsystem for block_online_users.
*
* @copyright 2018 Zig Tan <zig@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\user_preference_provider {
/**
* Describe all the places where this plugin stores personal data.
*
* @param collection $collection Collection of items to add metadata to.
* @return collection Collection with our added items.
*/
public static function get_metadata(collection $collection) : collection {
$collection->add_user_preference('block_online_users_uservisibility',
'privacy:metadata:preference:uservisibility');
return $collection;
}
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
* Export user preferences controlled by this plugin.
*
* @return string
* @param int $userid ID of the user we are exporting data form.
*/
public static function get_reason() : string {
return 'privacy:metadata';
public static function export_user_preferences(int $userid) {
$uservisibility = get_user_preferences('block_online_users_uservisibility', 1, $userid);
writer::export_user_preference('block_online_users',
'block_online_users_uservisibility', transform::yesno($uservisibility),
get_string('privacy:metadata:preference:uservisibility', 'block_online_users'));
}
}
@@ -30,7 +30,9 @@
$string['online_users:addinstance'] = 'Add a new online users block';
$string['online_users:myaddinstance'] = 'Add a new online users block to Dashboard';
$string['online_users:viewlist'] = 'View list of online users';
$string['online_status:hide'] = 'Hide my online status from other users';
$string['online_status:show'] = 'Show my online status to other users';
$string['periodnminutes'] = 'last {$a} minutes';
$string['pluginname'] = 'Online users';
$string['timetosee'] = 'Remove after inactivity (minutes)';
$string['privacy:metadata'] = 'The Online users block only shows data stored in other locations.';
$string['privacy:metadata:preference:uservisibility'] = 'Online status visible to other users in the Online users block.';

0 comments on commit c3f13f5

Please sign in to comment.
You can’t perform that action at this time.