Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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   * Recently accessed items helper.
  19   *
  20   * @package    block_recentlyaccesseditems
  21   * @copyright  2018 Victor Deniz <victor@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace block_recentlyaccesseditems;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Recently accessed items helper.
  31   *
  32   * @package    block_recentlyaccesseditems
  33   * @copyright  2018 Victor Deniz <victor@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class helper {
  37      /**
  38       * Returns a list of the most recently items accessed by the logged user
  39       *
  40       * @param int $limit Restrict result set to this amount
  41       * @return array List of recent items accessed by userid
  42       */
  43      public static function get_recent_items(int $limit = 0) {
  44          global $USER, $DB;
  45  
  46          $userid = $USER->id;
  47  
  48          $courses = array();
  49          $recentitems = array();
  50  
  51          if (!isloggedin() or \core\session\manager::is_loggedinas() or isguestuser()) {
  52              // No access tracking.
  53              return $recentitems;
  54          }
  55  
  56          $paramsql = array('userid' => $userid);
  57          $sql = "SELECT rai.*
  58                    FROM {block_recentlyaccesseditems} rai
  59                    JOIN {course} c ON c.id = rai.courseid
  60                   WHERE userid = :userid
  61                   ORDER BY rai.timeaccess DESC";
  62          $records = $DB->get_records_sql($sql, $paramsql);
  63          $order = 0;
  64  
  65          // Get array of items by course. Use $order index to keep sql sorted results.
  66          foreach ($records as $record) {
  67              $courses[$record->courseid][$order++] = $record;
  68          }
  69  
  70          // Group by courses to reduce get_fast_modinfo requests.
  71          foreach ($courses as $key => $items) {
  72              $modinfo = get_fast_modinfo($key);
  73              if (!can_access_course($modinfo->get_course(), null, '', true)) {
  74                  continue;
  75              }
  76              foreach ($items as $key => $item) {
  77                  // Exclude not visible items.
  78                  if (!$modinfo->cms[$item->cmid]->uservisible) {
  79                      continue;
  80                  }
  81                  $item->modname = $modinfo->cms[$item->cmid]->modname;
  82                  $item->name = $modinfo->cms[$item->cmid]->name;
  83                  $item->coursename = get_course_display_name_for_list($modinfo->get_course());
  84                  $recentitems[$key] = $item;
  85              }
  86          }
  87  
  88          ksort($recentitems);
  89  
  90          // Apply limit.
  91          if (!$limit) {
  92              $limit = count($recentitems);
  93          }
  94          $recentitems = array_slice($recentitems, 0, $limit);
  95  
  96          return $recentitems;
  97      }
  98  }