Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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 courses block installation.
  19   *
  20   * @package    block_recentlyaccessedcourses
  21   * @copyright  2018 Victor Deniz <victor@moodle.com> based on code from 2018 Ryan Wyllie <ryan@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27   /**
  28    * Add the Recently accessed courses block to the dashboard for all users by default
  29    * when it is installed.
  30    */
  31  function xmldb_block_recentlyaccessedcourses_install() {
  32      global $DB;
  33  
  34      if ($DB->count_records('block_instances') < 1) {
  35          // Only add the recentlyaccessedcourses block if it's being installed on an existing site.
  36          // For new sites it will be added by blocks_add_default_system_blocks().
  37          return;
  38      }
  39  
  40      if ($defaultmypage = $DB->get_record('my_pages', array('userid' => null, 'name' => '__default', 'private' => 1))) {
  41          $subpagepattern = $defaultmypage->id;
  42      } else {
  43          $subpagepattern = null;
  44      }
  45  
  46      $page = new moodle_page();
  47      $systemcontext = context_system::instance();
  48      $page->set_context($systemcontext);
  49      // Add the block to the default /my.
  50      $page->blocks->add_region('content');
  51      $page->blocks->add_block('recentlyaccessedcourses', 'content', 0, false, 'my-index', $subpagepattern);
  52  
  53      // Now we need to find all users that have viewed their dashboard because it'll have
  54      // made duplicates of the default block_instances for them so they won't see the new
  55      // recentlyaccessedcourses block without the admin resetting all of the dashboards.
  56      //
  57      // Instead we'll just add the recentlyaccessedcourses block to their dashboards here.
  58      $sql = "SELECT parentcontextid, subpagepattern
  59                FROM {block_instances}
  60               WHERE pagetypepattern = 'my-index'
  61                     AND parentcontextid != ?";
  62      $params = [$systemcontext->id];
  63      $existingrecords = $DB->get_recordset_sql($sql, $params);
  64      $blockinstances = [];
  65      $seencontexts = [];
  66      $now = time();
  67  
  68      foreach ($existingrecords as $existingrecord) {
  69          $parentcontextid = $existingrecord->parentcontextid;
  70          if (isset($seencontexts[$parentcontextid])) {
  71              // If we've seen this context already then skip it because we don't want
  72              // to add duplicate recentlyaccessedcourses blocks to the same context. This happens
  73              // if something funny is going on with the subpagepattern.
  74              continue;
  75          } else {
  76              $seencontexts[$parentcontextid] = true;
  77          }
  78  
  79          $blockinstances[] = [
  80              'blockname' => 'recentlyaccessedcourses',
  81              'parentcontextid' => $parentcontextid,
  82              'showinsubcontexts' => false,
  83              'pagetypepattern' => 'my-index',
  84              'subpagepattern' => $existingrecord->subpagepattern,
  85              'defaultregion' => 'content',
  86              'defaultweight' => 0,
  87              'configdata' => '',
  88              'timecreated' => $now,
  89              'timemodified' => $now,
  90          ];
  91  
  92          if (count($blockinstances) >= 1000) {
  93              // Insert after every 1000 records so that the memory usage doesn't
  94              // get out of control.
  95              $DB->insert_records('block_instances', $blockinstances);
  96              $blockinstances = [];
  97          }
  98      }
  99  
 100      $existingrecords->close();
 101  
 102      if (!empty($blockinstances)) {
 103          // Insert what ever is left over.
 104          $DB->insert_records('block_instances', $blockinstances);
 105      }
 106  }