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.
   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   * Timeline block installation.
  19   *
  20   * @package    block_timeline
  21   * @copyright  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 timeline block to the dashboard for all users by default
  29    * when it is installed.
  30    */
  31  function xmldb_block_timeline_install() {
  32      global $DB;
  33  
  34      if ($DB->count_records('block_instances') < 1) {
  35          // Only add the timeline 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(BLOCK_POS_RIGHT);
  51      $page->blocks->add_block('timeline', BLOCK_POS_RIGHT, 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      // timeline block without the admin resetting all of the dashboards.
  56      //
  57      // Instead we'll just add the timeline block to their dashboards here. We will only
  58      // add the timeline block if they still have the myoverview block.
  59      $sql = "SELECT parentcontextid, subpagepattern
  60              FROM {block_instances}
  61              WHERE pagetypepattern = 'my-index'
  62              AND blockname = 'myoverview'
  63              AND parentcontextid != ?";
  64      $params = [$systemcontext->id];
  65      $existingrecords = $DB->get_recordset_sql($sql, $params);
  66      $blockinstances = [];
  67      $seencontexts = [];
  68      $now = time();
  69  
  70      foreach ($existingrecords as $existingrecord) {
  71          $parentcontextid = $existingrecord->parentcontextid;
  72          if (isset($seencontexts[$parentcontextid])) {
  73              // If we've seen this context already then skip it because we don't want
  74              // to add duplicate timeline blocks to the same context. This happens
  75              // if something funny is going on with the subpagepattern.
  76              continue;
  77          } else {
  78              $seencontexts[$parentcontextid] = true;
  79          }
  80  
  81          $blockinstances[] = [
  82              'blockname' => 'timeline',
  83              'parentcontextid' => $parentcontextid,
  84              'showinsubcontexts' => false,
  85              'pagetypepattern' => 'my-index',
  86              'subpagepattern' => $existingrecord->subpagepattern,
  87              'defaultregion' => BLOCK_POS_RIGHT,
  88              'defaultweight' => 0,
  89              'configdata' => '',
  90              'timecreated' => $now,
  91              'timemodified' => $now,
  92          ];
  93  
  94          if (count($blockinstances) >= 1000) {
  95              // Insert after every 1000 records so that the memory usage doesn't
  96              // get out of control.
  97              $DB->insert_records('block_instances', $blockinstances);
  98              $blockinstances = [];
  99          }
 100      }
 101  
 102      $existingrecords->close();
 103  
 104      if (!empty($blockinstances)) {
 105          // Insert what ever is left over.
 106          $DB->insert_records('block_instances', $blockinstances);
 107      }
 108  }