[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/my/ -> lib.php (source)

   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   * My Moodle -- a user's personal dashboard
  19   *
  20   * This file contains common functions for the dashboard and profile pages.
  21   *
  22   * @package    moodlecore
  23   * @subpackage my
  24   * @copyright  2010 Remote-Learner.net
  25   * @author     Hubert Chathi <hubert@remote-learner.net>
  26   * @author     Olav Jordan <olav.jordan@remote-learner.net>
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  
  30  define('MY_PAGE_PUBLIC', 0);
  31  define('MY_PAGE_PRIVATE', 1);
  32  
  33  require_once("$CFG->libdir/blocklib.php");
  34  
  35  /*
  36   * For a given user, this returns the $page information for their My Moodle page
  37   *
  38   */
  39  function my_get_page($userid, $private=MY_PAGE_PRIVATE) {
  40      global $DB, $CFG;
  41  
  42      if (empty($CFG->forcedefaultmymoodle) && $userid) {  // Ignore custom My Moodle pages if admin has forced them
  43          // Does the user have their own page defined?  If so, return it.
  44          if ($customised = $DB->get_record('my_pages', array('userid' => $userid, 'private' => $private))) {
  45              return $customised;
  46          }
  47      }
  48  
  49      // Otherwise return the system default page
  50      return $DB->get_record('my_pages', array('userid' => null, 'name' => '__default', 'private' => $private));
  51  }
  52  
  53  
  54  /*
  55   * This copies a system default page to the current user
  56   *
  57   */
  58  function my_copy_page($userid, $private=MY_PAGE_PRIVATE, $pagetype='my-index') {
  59      global $DB;
  60  
  61      if ($customised = $DB->get_record('my_pages', array('userid' => $userid, 'private' => $private))) {
  62          return $customised;  // We're done!
  63      }
  64  
  65      // Get the system default page
  66      if (!$systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => $private))) {
  67          return false;  // error
  68      }
  69  
  70      // Clone the basic system page record
  71      $page = clone($systempage);
  72      unset($page->id);
  73      $page->userid = $userid;
  74      $page->id = $DB->insert_record('my_pages', $page);
  75  
  76      // Clone ALL the associated blocks as well
  77      $systemcontext = context_system::instance();
  78      $usercontext = context_user::instance($userid);
  79  
  80      $blockinstances = $DB->get_records('block_instances', array('parentcontextid' => $systemcontext->id,
  81                                                                  'pagetypepattern' => $pagetype,
  82                                                                  'subpagepattern' => $systempage->id));
  83      foreach ($blockinstances as $instance) {
  84          $originalid = $instance->id;
  85          unset($instance->id);
  86          $instance->parentcontextid = $usercontext->id;
  87          $instance->subpagepattern = $page->id;
  88          $instance->id = $DB->insert_record('block_instances', $instance);
  89          $blockcontext = context_block::instance($instance->id);  // Just creates the context record
  90          $block = block_instance($instance->blockname, $instance);
  91          if (!$block->instance_copy($originalid)) {
  92              debugging("Unable to copy block-specific data for original block instance: $originalid
  93                  to new block instance: $instance->id", DEBUG_DEVELOPER);
  94          }
  95      }
  96  
  97      // FIXME: block position overrides should be merged in with block instance
  98      //$blockpositions = $DB->get_records('block_positions', array('subpage' => $page->name));
  99      //foreach($blockpositions as $positions) {
 100      //    $positions->subpage = $page->name;
 101      //    $DB->insert_record('block_positions', $tc);
 102      //}
 103  
 104      return $page;
 105  }
 106  
 107  /*
 108   * For a given user, this deletes their My Moodle page and returns them to the system default.
 109   *
 110   * @param int $userid the id of the user whose page should be reset
 111   * @param int $private either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC
 112   * @param string $pagetype either my-index or user-profile
 113   * @return mixed system page, or false on error
 114   */
 115  function my_reset_page($userid, $private=MY_PAGE_PRIVATE, $pagetype='my-index') {
 116      global $DB, $CFG;
 117  
 118      $page = my_get_page($userid, $private);
 119      if ($page->userid == $userid) {
 120          $context = context_user::instance($userid);
 121          if ($blocks = $DB->get_records('block_instances', array('parentcontextid' => $context->id,
 122                  'pagetypepattern' => $pagetype))) {
 123              foreach ($blocks as $block) {
 124                  if (is_null($block->subpagepattern) || $block->subpagepattern == $page->id) {
 125                      blocks_delete_instance($block);
 126                  }
 127              }
 128          }
 129          $DB->delete_records('my_pages', array('id' => $page->id));
 130      }
 131  
 132      // Get the system default page
 133      if (!$systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => $private))) {
 134          return false; // error
 135      }
 136  
 137      // Trigger dashboard has been reset event.
 138      $eventparams = array('context' => context_user::instance($userid));
 139      $event = \core\event\dashboard_reset::create($eventparams);
 140      $event->trigger();
 141      return $systempage;
 142  }
 143  
 144  /**
 145   * Resets the page customisations for all users.
 146   *
 147   * @param int $private Either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC.
 148   * @param string $pagetype Either my-index or user-profile.
 149   * @return void
 150   */
 151  function my_reset_page_for_all_users($private = MY_PAGE_PRIVATE, $pagetype = 'my-index') {
 152      global $DB;
 153  
 154      // This may take a while. Raise the execution time limit.
 155      core_php_time_limit::raise();
 156  
 157      // Find all the user pages and all block instances in them.
 158      $sql = "SELECT bi.id
 159          FROM {my_pages} p
 160          JOIN {context} ctx ON ctx.instanceid = p.userid AND ctx.contextlevel = :usercontextlevel
 161          JOIN {block_instances} bi ON bi.parentcontextid = ctx.id AND
 162              bi.pagetypepattern = :pagetypepattern AND
 163              (bi.subpagepattern IS NULL OR bi.subpagepattern = " . $DB->sql_concat("''", 'p.id') . ")
 164          WHERE p.private = :private";
 165      $params = array('private' => $private,
 166          'usercontextlevel' => CONTEXT_USER,
 167          'pagetypepattern' => $pagetype);
 168      $blockids = $DB->get_fieldset_sql($sql, $params);
 169  
 170      // Wrap the SQL queries in a transaction.
 171      $transaction = $DB->start_delegated_transaction();
 172  
 173      // Delete the block instances.
 174      if (!empty($blockids)) {
 175          blocks_delete_instances($blockids);
 176      }
 177  
 178      // Finally delete the pages.
 179      $DB->delete_records_select('my_pages', 'userid IS NOT NULL AND private = :private', ['private' => $private]);
 180  
 181      // We should be good to go now.
 182      $transaction->allow_commit();
 183  
 184      // Trigger dashboard has been reset event.
 185      $eventparams = array('context' => context_system::instance());
 186      $event = \core\event\dashboards_reset::create($eventparams);
 187      $event->trigger();
 188  }
 189  
 190  class my_syspage_block_manager extends block_manager {
 191      // HACK WARNING!
 192      // TODO: figure out a better way to do this
 193      /**
 194       * Load blocks using the system context, rather than the user's context.
 195       *
 196       * This is needed because the My Moodle pages set the page context to the
 197       * user's context for access control, etc.  But the blocks for the system
 198       * pages are stored in the system context.
 199       */
 200      public function load_blocks($includeinvisible = null) {
 201          $origcontext = $this->page->context;
 202          $this->page->context = context_system::instance();
 203          parent::load_blocks($includeinvisible);
 204          $this->page->context = $origcontext;
 205      }
 206  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1