Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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   * This file contains functions used by the outline reports
  19   *
  20   * @package    report
  21   * @subpackage outline
  22   * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die;
  27  
  28  require_once (__DIR__.'/lib.php');
  29  require_once($CFG->dirroot.'/course/lib.php');
  30  
  31  function report_outline_print_row($mod, $instance, $result) {
  32      global $OUTPUT, $CFG;
  33  
  34      $image = $OUTPUT->image_icon('icon', $mod->modfullname, $mod->modname);
  35  
  36      echo "<tr>";
  37      echo "<td valign=\"top\">$image</td>";
  38      echo "<td valign=\"top\" style=\"width:300\">";
  39      echo "   <a title=\"$mod->modfullname\"";
  40      echo "   href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">".format_string($instance->name,true)."</a></td>";
  41      echo "<td>&nbsp;&nbsp;&nbsp;</td>";
  42      echo "<td valign=\"top\">";
  43      if (isset($result->info)) {
  44          echo "$result->info";
  45      } else {
  46          echo "<p style=\"text-align:center\">-</p>";
  47      }
  48      echo "</td>";
  49      echo "<td>&nbsp;&nbsp;&nbsp;</td>";
  50      if (!empty($result->time)) {
  51          $timeago = format_time(time() - $result->time);
  52          echo "<td valign=\"top\" style=\"white-space: nowrap\">".userdate($result->time)." ($timeago)</td>";
  53      }
  54      echo "</tr>";
  55  }
  56  
  57  /**
  58   * Returns an array of the commonly used log variables by the outline report.
  59   *
  60   * @return array the array of variables used
  61   */
  62  function report_outline_get_common_log_variables() {
  63      global $DB;
  64  
  65      static $uselegacyreader;
  66      static $useinternalreader;
  67      static $minloginternalreader;
  68      static $logtable = null;
  69  
  70      if (isset($uselegacyreader) && isset($useinternalreader) && isset($minloginternalreader)) {
  71          return array($uselegacyreader, $useinternalreader, $minloginternalreader, $logtable);
  72      }
  73  
  74      $uselegacyreader = false; // Flag to determine if we should use the legacy reader.
  75      $useinternalreader = false; // Flag to determine if we should use the internal reader.
  76      $minloginternalreader = 0; // Set this to 0 for now.
  77  
  78      // Get list of readers.
  79      $logmanager = get_log_manager();
  80      $readers = $logmanager->get_readers();
  81  
  82      // Get preferred reader.
  83      if (!empty($readers)) {
  84          foreach ($readers as $readerpluginname => $reader) {
  85              // If legacy reader is preferred reader.
  86              if ($readerpluginname == 'logstore_legacy') {
  87                  $uselegacyreader = true;
  88              }
  89  
  90              // If sql_internal_table_reader is preferred reader.
  91              if ($reader instanceof \core\log\sql_internal_table_reader) {
  92                  $useinternalreader = true;
  93                  $logtable = $reader->get_internal_log_table_name();
  94                  $minloginternalreader = $DB->get_field_sql('SELECT min(timecreated) FROM {' . $logtable . '}');
  95              }
  96          }
  97      }
  98  
  99      return array($uselegacyreader, $useinternalreader, $minloginternalreader, $logtable);
 100  }
 101  
 102  /**
 103   * Return the most commonly used user outline information.
 104   *
 105   * @param int $userid the id of the user
 106   * @param int $cmid the course module id
 107   * @param string $module the name of the module (eg. 'book')
 108   * @param int $instanceid (eg. the 'id' in the 'book' table)
 109   * @return stdClass|null if any information is found then a stdClass containing
 110   *  this info is returned, else null is.
 111   */
 112  function report_outline_user_outline($userid, $cmid, $module, $instanceid) {
 113      global $DB;
 114  
 115      list($uselegacyreader, $useinternalreader, $minloginternalreader, $logtable) = report_outline_get_common_log_variables();
 116  
 117      // If using legacy log then get users from old table.
 118      if ($uselegacyreader) {
 119          // Create the params for the query.
 120          $params = array('userid' => $userid, 'module' => $module, 'action' => 'view', 'info' => $instanceid);
 121          // If we are going to use the internal (not legacy) log table, we should only get records
 122          // from the legacy table that exist before we started adding logs to the new table.
 123          $limittime = '';
 124          if (!empty($minloginternalreader)) {
 125              $limittime = ' AND time < :timeto ';
 126              $params['timeto'] = $minloginternalreader;
 127          }
 128          $select = "SELECT COUNT(id) ";
 129          $from = "FROM {log} ";
 130          $where = "WHERE userid = :userid
 131                      AND module = :module
 132                      AND action = :action
 133                      AND info = :info ";
 134          if ($legacylogcount = $DB->count_records_sql($select . $from . $where . $limittime, $params)) {
 135              $numviews = $legacylogcount;
 136  
 137              // Get the time for the last log.
 138              $select = "SELECT MAX(time) ";
 139              $lastlogtime = $DB->get_field_sql($select . $from . $where, $params);
 140  
 141              $result = new stdClass();
 142              $result->info = get_string('numviews', '', $numviews);
 143              $result->time = $lastlogtime;
 144          }
 145      }
 146  
 147      // Get record from sql_internal_table_reader and combine with the number of views from the legacy log table (if needed).
 148      if ($useinternalreader) {
 149          $params = array('userid' => $userid, 'contextlevel' => CONTEXT_MODULE, 'contextinstanceid' => $cmid, 'crud' => 'r',
 150              'edulevel1' => core\event\base::LEVEL_PARTICIPATING, 'edulevel2' => core\event\base::LEVEL_TEACHING,
 151              'edulevel3' => core\event\base::LEVEL_OTHER, 'anonymous' => 0);
 152          $select = "SELECT COUNT(*) as count ";
 153          $from = "FROM {" . $logtable . "} ";
 154          $where = "WHERE userid = :userid
 155                      AND contextlevel = :contextlevel
 156                      AND contextinstanceid = :contextinstanceid
 157                      AND crud = :crud
 158                      AND edulevel IN (:edulevel1, :edulevel2, :edulevel3)
 159                      AND anonymous = :anonymous";
 160          if ($internalreadercount = $DB->count_records_sql($select . $from . $where, $params)) {
 161              if (!empty($numviews)) {
 162                  $numviews = $numviews + $internalreadercount;
 163              } else {
 164                  $numviews = $internalreadercount;
 165              }
 166  
 167              // Get the time for the last log.
 168              $select = "SELECT MAX(timecreated) ";
 169              $lastlogtime = $DB->get_field_sql($select . $from . $where, $params);
 170  
 171              $result = new stdClass();
 172              $result->info = get_string('numviews', '', $numviews);
 173              $result->time = $lastlogtime;
 174          }
 175      }
 176  
 177      if (!empty($result)) {
 178          return $result;
 179      }
 180  
 181      return null;
 182  }
 183  
 184  /**
 185   * Display the most commonly used user complete information.
 186   *
 187   * @param int $userid the id of the user
 188   * @param int $cmid the course module id
 189   * @param string $module the name of the module (eg. 'book')
 190   * @param int $instanceid (eg. the 'id' in the 'book' table)
 191   * @return string
 192   */
 193  function report_outline_user_complete($userid, $cmid, $module, $instanceid) {
 194      global $DB;
 195  
 196      list($uselegacyreader, $useinternalreader, $minloginternalreader, $logtable) = report_outline_get_common_log_variables();
 197  
 198      // If using legacy log then get users from old table.
 199      if ($uselegacyreader) {
 200          // Create the params for the query.
 201          $params = array('userid' => $userid, 'module' => $module, 'action' => 'view', 'info' => $instanceid);
 202          // If we are going to use the internal (not legacy) log table, we should only get records
 203          // from the legacy table that exist before we started adding logs to the new table.
 204          $limittime = '';
 205          if (!empty($minloginternalreader)) {
 206              $limittime = ' AND time < :timeto ';
 207              $params['timeto'] = $minloginternalreader;
 208          }
 209          $select = "SELECT COUNT(id) ";
 210          $from = "FROM {log} ";
 211          $where = "WHERE userid = :userid
 212                      AND module = :module
 213                      AND action = :action
 214                      AND info = :info ";
 215          if ($legacylogcount = $DB->count_records_sql($select . $from . $where . $limittime, $params)) {
 216              $numviews = $legacylogcount;
 217  
 218              // Get the time for the last log.
 219              $select = "SELECT MAX(time) ";
 220              $lastlogtime = $DB->get_field_sql($select . $from . $where, $params);
 221  
 222              $strnumviews = get_string('numviews', '', $numviews);
 223          }
 224      }
 225  
 226      // Get record from sql_internal_table_reader and combine with the number of views from the legacy log table (if needed).
 227      if ($useinternalreader) {
 228          $params = array('userid' => $userid, 'contextlevel' => CONTEXT_MODULE, 'contextinstanceid' => $cmid, 'crud' => 'r',
 229              'edulevel1' => core\event\base::LEVEL_PARTICIPATING, 'edulevel2' => core\event\base::LEVEL_TEACHING,
 230              'edulevel3' => core\event\base::LEVEL_OTHER, 'anonymous' => 0);
 231          $select = "SELECT COUNT(*) as count ";
 232          $from = "FROM {" . $logtable . "} ";
 233          $where = "WHERE userid = :userid
 234                      AND contextlevel = :contextlevel
 235                      AND contextinstanceid = :contextinstanceid
 236                      AND crud = :crud
 237                      AND edulevel IN (:edulevel1, :edulevel2, :edulevel3)
 238                      AND anonymous = :anonymous";
 239          if ($internalreadercount = $DB->count_records_sql($select . $from . $where, $params)) {
 240              if (!empty($numviews)) {
 241                  $numviews = $numviews + $internalreadercount;
 242              } else {
 243                  $numviews = $internalreadercount;
 244              }
 245  
 246              // Get the time for the last log.
 247              $select = "SELECT MAX(timecreated) ";
 248              $lastlogtime = $DB->get_field_sql($select . $from . $where, $params);
 249  
 250              $strnumviews = get_string('numviews', '', $numviews);
 251          }
 252      }
 253  
 254      if (!empty($strnumviews) && (!empty($lastlogtime))) {
 255          return $strnumviews . ' - ' . get_string('mostrecently') . ' ' . userdate($lastlogtime);
 256      } else {
 257          return get_string('neverseen', 'report_outline');
 258      }
 259  }