Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 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('monologo', $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 sql_internal_table_reader is preferred reader.
  86              if ($reader instanceof \core\log\sql_internal_table_reader) {
  87                  $useinternalreader = true;
  88                  $logtable = $reader->get_internal_log_table_name();
  89                  $minloginternalreader = $DB->get_field_sql('SELECT min(timecreated) FROM {' . $logtable . '}');
  90              }
  91          }
  92      }
  93  
  94      return array($uselegacyreader, $useinternalreader, $minloginternalreader, $logtable);
  95  }
  96  
  97  /**
  98   * Return the most commonly used user outline information.
  99   *
 100   * @param int $userid the id of the user
 101   * @param int $cmid the course module id
 102   * @param string $module the name of the module (eg. 'book')
 103   * @param int $instanceid (eg. the 'id' in the 'book' table)
 104   * @return stdClass|null if any information is found then a stdClass containing
 105   *  this info is returned, else null is.
 106   */
 107  function report_outline_user_outline($userid, $cmid, $module, $instanceid) {
 108      global $DB;
 109  
 110      list($uselegacyreader, $useinternalreader, $minloginternalreader, $logtable) = report_outline_get_common_log_variables();
 111  
 112      // If using legacy log then get users from old table.
 113      if ($uselegacyreader) {
 114          // Create the params for the query.
 115          $params = array('userid' => $userid, 'module' => $module, 'action' => 'view', 'info' => $instanceid);
 116          // If we are going to use the internal (not legacy) log table, we should only get records
 117          // from the legacy table that exist before we started adding logs to the new table.
 118          $limittime = '';
 119          if (!empty($minloginternalreader)) {
 120              $limittime = ' AND time < :timeto ';
 121              $params['timeto'] = $minloginternalreader;
 122          }
 123          $select = "SELECT COUNT(id) ";
 124          $from = "FROM {log} ";
 125          $where = "WHERE userid = :userid
 126                      AND module = :module
 127                      AND action = :action
 128                      AND info = :info ";
 129          if ($legacylogcount = $DB->count_records_sql($select . $from . $where . $limittime, $params)) {
 130              $numviews = $legacylogcount;
 131  
 132              // Get the time for the last log.
 133              $select = "SELECT MAX(time) ";
 134              $lastlogtime = $DB->get_field_sql($select . $from . $where, $params);
 135  
 136              $result = new stdClass();
 137              $result->info = get_string('numviews', '', $numviews);
 138              $result->time = $lastlogtime;
 139          }
 140      }
 141  
 142      // Get record from sql_internal_table_reader and combine with the number of views from the legacy log table (if needed).
 143      if ($useinternalreader) {
 144          $params = array('userid' => $userid, 'contextlevel' => CONTEXT_MODULE, 'contextinstanceid' => $cmid, 'crud' => 'r',
 145              'edulevel1' => core\event\base::LEVEL_PARTICIPATING, 'edulevel2' => core\event\base::LEVEL_TEACHING,
 146              'edulevel3' => core\event\base::LEVEL_OTHER, 'anonymous' => 0);
 147          $select = "SELECT COUNT(*) as count ";
 148          $from = "FROM {" . $logtable . "} ";
 149          $where = "WHERE userid = :userid
 150                      AND contextlevel = :contextlevel
 151                      AND contextinstanceid = :contextinstanceid
 152                      AND crud = :crud
 153                      AND edulevel IN (:edulevel1, :edulevel2, :edulevel3)
 154                      AND anonymous = :anonymous";
 155          if ($internalreadercount = $DB->count_records_sql($select . $from . $where, $params)) {
 156              if (!empty($numviews)) {
 157                  $numviews = $numviews + $internalreadercount;
 158              } else {
 159                  $numviews = $internalreadercount;
 160              }
 161  
 162              // Get the time for the last log.
 163              $select = "SELECT MAX(timecreated) ";
 164              $lastlogtime = $DB->get_field_sql($select . $from . $where, $params);
 165  
 166              $result = new stdClass();
 167              $result->info = get_string('numviews', '', $numviews);
 168              $result->time = $lastlogtime;
 169          }
 170      }
 171  
 172      if (!empty($result)) {
 173          return $result;
 174      }
 175  
 176      return null;
 177  }
 178  
 179  /**
 180   * Display the most commonly used user complete information.
 181   *
 182   * @param int $userid the id of the user
 183   * @param int $cmid the course module id
 184   * @param string $module the name of the module (eg. 'book')
 185   * @param int $instanceid (eg. the 'id' in the 'book' table)
 186   * @return string
 187   */
 188  function report_outline_user_complete($userid, $cmid, $module, $instanceid) {
 189      global $DB;
 190  
 191      list($uselegacyreader, $useinternalreader, $minloginternalreader, $logtable) = report_outline_get_common_log_variables();
 192  
 193      // If using legacy log then get users from old table.
 194      if ($uselegacyreader) {
 195          // Create the params for the query.
 196          $params = array('userid' => $userid, 'module' => $module, 'action' => 'view', 'info' => $instanceid);
 197          // If we are going to use the internal (not legacy) log table, we should only get records
 198          // from the legacy table that exist before we started adding logs to the new table.
 199          $limittime = '';
 200          if (!empty($minloginternalreader)) {
 201              $limittime = ' AND time < :timeto ';
 202              $params['timeto'] = $minloginternalreader;
 203          }
 204          $select = "SELECT COUNT(id) ";
 205          $from = "FROM {log} ";
 206          $where = "WHERE userid = :userid
 207                      AND module = :module
 208                      AND action = :action
 209                      AND info = :info ";
 210          if ($legacylogcount = $DB->count_records_sql($select . $from . $where . $limittime, $params)) {
 211              $numviews = $legacylogcount;
 212  
 213              // Get the time for the last log.
 214              $select = "SELECT MAX(time) ";
 215              $lastlogtime = $DB->get_field_sql($select . $from . $where, $params);
 216  
 217              $strnumviews = get_string('numviews', '', $numviews);
 218          }
 219      }
 220  
 221      // Get record from sql_internal_table_reader and combine with the number of views from the legacy log table (if needed).
 222      if ($useinternalreader) {
 223          $params = array('userid' => $userid, 'contextlevel' => CONTEXT_MODULE, 'contextinstanceid' => $cmid, 'crud' => 'r',
 224              'edulevel1' => core\event\base::LEVEL_PARTICIPATING, 'edulevel2' => core\event\base::LEVEL_TEACHING,
 225              'edulevel3' => core\event\base::LEVEL_OTHER, 'anonymous' => 0);
 226          $select = "SELECT COUNT(*) as count ";
 227          $from = "FROM {" . $logtable . "} ";
 228          $where = "WHERE userid = :userid
 229                      AND contextlevel = :contextlevel
 230                      AND contextinstanceid = :contextinstanceid
 231                      AND crud = :crud
 232                      AND edulevel IN (:edulevel1, :edulevel2, :edulevel3)
 233                      AND anonymous = :anonymous";
 234          if ($internalreadercount = $DB->count_records_sql($select . $from . $where, $params)) {
 235              if (!empty($numviews)) {
 236                  $numviews = $numviews + $internalreadercount;
 237              } else {
 238                  $numviews = $internalreadercount;
 239              }
 240  
 241              // Get the time for the last log.
 242              $select = "SELECT MAX(timecreated) ";
 243              $lastlogtime = $DB->get_field_sql($select . $from . $where, $params);
 244  
 245              $strnumviews = get_string('numviews', '', $numviews);
 246          }
 247      }
 248  
 249      if (!empty($strnumviews) && (!empty($lastlogtime))) {
 250          return $strnumviews . ' - ' . get_string('mostrecently') . ' ' . userdate($lastlogtime);
 251      } else {
 252          return get_string('neverseen', 'report_outline');
 253      }
 254  }