MDL-64745 block_online_users: Option to disable users to set visibility
authorMihail Geshoski <mihail@moodle.com>
Fri, 27 Sep 2019 05:22:52 +0000 (13:22 +0800)
committerMihail Geshoski <mihail@moodle.com>
Mon, 7 Oct 2019 01:00:33 +0000 (09:00 +0800)
blocks/online_users/block_online_users.php
blocks/online_users/classes/fetcher.php
blocks/online_users/lang/en/block_online_users.php
blocks/online_users/settings.php
blocks/online_users/version.php

index 01ada95..9d1fc90 100644 (file)
@@ -129,14 +129,16 @@ class block_online_users extends block_base {
                     $this->content->text .= $OUTPUT->user_picture($user, array('size'=>16, 'alttext'=>false, 'link'=>false)) .$user->fullname.'</a></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>';
+                        if ($CFG->block_online_users_onlinestatushiding) {
+                            $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'));
index 20f706f..a18e625 100644 (file)
@@ -67,7 +67,7 @@ class fetcher {
      * @param int $courseid The course id to check
      */
     protected function set_sql($currentgroup, $now, $timetoshowusers, $context, $sitelevel, $courseid) {
-        global $USER, $DB;
+        global $USER, $DB, $CFG;
 
         $timefrom = 100 * floor(($now - $timetoshowusers) / 100); // Round to nearest 100 seconds for better query cache.
 
@@ -76,7 +76,14 @@ class fetcher {
         $groupby       = "";
         $lastaccess    = ", lastaccess";
         $timeaccess    = ", ul.timeaccess AS lastaccess";
-        $uservisibility = ", up.value AS uservisibility";
+        $uservisibility = "";
+        $uservisibilityselect = "";
+        if ($CFG->block_online_users_onlinestatushiding) {
+            $uservisibility = ", up.value AS uservisibility";
+            $uservisibilityselect = "AND (" . $DB->sql_cast_char2int('up.value') . " = 1
+                                    OR up.value IS NULL
+                                    OR u.id = :userid)";
+        }
         $params = array();
 
         $userfields = \user_picture::fields('u', array('username'));
@@ -88,7 +95,9 @@ class fetcher {
             $groupby = "GROUP BY $userfields";
             $lastaccess = ", MAX(u.lastaccess) AS lastaccess";
             $timeaccess = ", MAX(ul.timeaccess) AS lastaccess";
-            $uservisibility = ", MAX(up.value) AS uservisibility";
+            if ($CFG->block_online_users_onlinestatushiding) {
+                $uservisibility = ", MAX(up.value) AS uservisibility";
+            }
             $params['currentgroup'] = $currentgroup;
         }
 
@@ -105,9 +114,7 @@ class fetcher {
                      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)
+                           $uservisibilityselect
                            $groupselect $groupby
                   ORDER BY lastaccess DESC ";
 
@@ -118,9 +125,7 @@ class fetcher {
                       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)
+                            $uservisibilityselect
                             $groupselect";
         } else {
             // Course level - show only enrolled users for now.
@@ -138,9 +143,7 @@ class fetcher {
                            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)
+                           $uservisibilityselect
                            $groupselect $groupby
                   ORDER BY lastaccess DESC";
 
@@ -154,9 +157,7 @@ class fetcher {
                            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)
+                           $uservisibilityselect
                            $groupselect";
 
             $params['courseid'] = $courseid;
index c6bc022..f34f5d6 100644 (file)
@@ -23,7 +23,9 @@
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
+$string['onlinestatushiding_desc'] = 'If enabled, users have the option to hide their online status from other users.';
 $string['configtimetosee'] = 'Number of minutes determining the period of inactivity after which a user is no longer considered to be online.';
+$string['onlinestatushiding'] = 'Online status hiding';
 $string['nouser'] = 'No online users';
 $string['numuser'] = '{$a} online user';
 $string['numusers'] = '{$a} online users';
index b9d7d81..39d3bf0 100644 (file)
@@ -27,5 +27,9 @@ defined('MOODLE_INTERNAL') || die;
 if ($ADMIN->fulltree) {
     $settings->add(new admin_setting_configtext('block_online_users_timetosee', get_string('timetosee', 'block_online_users'),
                    get_string('configtimetosee', 'block_online_users'), 5, PARAM_INT));
+
+    $settings->add(new admin_setting_configcheckbox('block_online_users_onlinestatushiding',
+            get_string('onlinestatushiding', 'block_online_users'),
+            get_string('onlinestatushiding_desc', 'block_online_users'), 1));
 }
 
index eaf8a55..c5806d3 100644 (file)
@@ -24,6 +24,6 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2019052000;        // The current plugin version (Date: YYYYMMDDXX)
+$plugin->version   = 2019052001;        // The current plugin version (Date: YYYYMMDDXX)
 $plugin->requires  = 2019051100;        // Requires this Moodle version
 $plugin->component = 'block_online_users'; // Full name of the plugin (used for diagnostics)