Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
/my/ -> index.php (source)

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

   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   * - each user can currently have their own page (cloned from system and then customised)
  21   * - only the user can see their own dashboard
  22   * - users can add any blocks they want
  23   * - the administrators can define a default site dashboard for users who have
  24   *   not created their own dashboard
  25   *
  26   * This script implements the user's view of the dashboard, and allows editing
  27   * of the dashboard.
  28   *
  29   * @package    moodlecore
  30   * @subpackage my
  31   * @copyright  2010 Remote-Learner.net
  32   * @author     Hubert Chathi <hubert@remote-learner.net>
  33   * @author     Olav Jordan <olav.jordan@remote-learner.net>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  
  37  require_once(__DIR__ . '/../config.php');
  38  require_once($CFG->dirroot . '/my/lib.php');
  39  
  40  redirect_if_major_upgrade_required();
  41  
  42  // TODO Add sesskey check to edit
  43  $edit   = optional_param('edit', null, PARAM_BOOL);    // Turn editing on and off
  44  $reset  = optional_param('reset', null, PARAM_BOOL);
  45  
  46  require_login();
  47  
  48  $hassiteconfig = has_capability('moodle/site:config', context_system::instance());
  49  if ($hassiteconfig && moodle_needs_upgrading()) {
  50      redirect(new moodle_url('/admin/index.php'));
  51  }
  52  
  53  $strmymoodle = get_string('myhome');
  54  
  55  if (empty($CFG->enabledashboard)) {
  56      // Dashboard is disabled, so the /my page shouldn't be displayed.
  57      $defaultpage = get_default_home_page();
  58      if ($defaultpage == HOMEPAGE_MYCOURSES) {
  59          // If default page is set to "My courses", redirect to it.
  60          redirect(new moodle_url('/my/courses.php'));
  61      } else {
  62          // Otherwise, raise an exception to inform the dashboard is disabled.
  63          throw new moodle_exception('error:dashboardisdisabled', 'my');
  64      }
  65  }
  66  
  67  if (isguestuser()) {  // Force them to see system default, no editing allowed
  68      // If guests are not allowed my moodle, send them to front page.
  69      if (empty($CFG->allowguestmymoodle)) {
  70          redirect(new moodle_url('/', array('redirect' => 0)));
  71      }
  72  
  73      $userid = null;
  74      $USER->editing = $edit = 0;  // Just in case
  75      $context = context_system::instance();
  76      $PAGE->set_blocks_editing_capability('moodle/my:configsyspages');  // unlikely :)
  77      $strguest = get_string('guest');
  78      $pagetitle = "$strmymoodle ($strguest)";
  79  
  80  } else {        // We are trying to view or edit our own My Moodle page
  81      $userid = $USER->id;  // Owner of the page
  82      $context = context_user::instance($USER->id);
  83      $PAGE->set_blocks_editing_capability('moodle/my:manageblocks');
  84      $pagetitle = $strmymoodle;
  85  }
  86  
  87  // Get the My Moodle page info.  Should always return something unless the database is broken.
  88  if (!$currentpage = my_get_page($userid, MY_PAGE_PRIVATE)) {
  89      throw new \moodle_exception('mymoodlesetup');
  90  }
  91  
  92  // Start setting up the page
  93  $params = array();
  94  $PAGE->set_context($context);
  95  $PAGE->set_url('/my/index.php', $params);
  96  $PAGE->set_pagelayout('mydashboard');
  97  $PAGE->add_body_class('limitedwidth');
  98  $PAGE->set_pagetype('my-index');
  99  $PAGE->blocks->add_region('content');
 100  $PAGE->set_subpage($currentpage->id);
 101  $PAGE->set_title($pagetitle);
 102  $PAGE->set_heading($pagetitle);
 103  
 104  if (!isguestuser()) {   // Skip default home page for guests
 105      if (get_home_page() != HOMEPAGE_MY) {
 106          if (optional_param('setdefaulthome', false, PARAM_BOOL)) {
 107              set_user_preference('user_home_page_preference', HOMEPAGE_MY);
 108          } else if (!empty($CFG->defaulthomepage) && $CFG->defaulthomepage == HOMEPAGE_USER) {
 109              $frontpagenode = $PAGE->settingsnav->add(get_string('frontpagesettings'), null, navigation_node::TYPE_SETTING, null);
 110              $frontpagenode->force_open();
 111              $frontpagenode->add(get_string('makethismyhome'), new moodle_url('/my/', array('setdefaulthome' => true)),
 112                      navigation_node::TYPE_SETTING);
 113          }
 114      }
 115  }
 116  
 117  // Toggle the editing state and switches
 118  if (empty($CFG->forcedefaultmymoodle) && $PAGE->user_allowed_editing()) {
 119      if ($reset !== null) {
 120          if (!is_null($userid)) {
 121              require_sesskey();
 122              if (!$currentpage = my_reset_page($userid, MY_PAGE_PRIVATE)) {
 123                  throw new \moodle_exception('reseterror', 'my');
 124              }
 125              redirect(new moodle_url('/my'));
 126          }
 127      } else if ($edit !== null) {             // Editing state was specified
 128          $USER->editing = $edit;       // Change editing state
 129      } else {                          // Editing state is in session
 130          if ($currentpage->userid) {   // It's a page we can edit, so load from session
 131              if (!empty($USER->editing)) {
 132                  $edit = 1;
 133              } else {
 134                  $edit = 0;
 135              }
 136          } else {
 137              // For the page to display properly with the user context header the page blocks need to
 138              // be copied over to the user context.
 139              if (!$currentpage = my_copy_page($USER->id, MY_PAGE_PRIVATE)) {
 140                  throw new \moodle_exception('mymoodlesetup');
 141              }
 142              $context = context_user::instance($USER->id);
 143              $PAGE->set_context($context);
 144              $PAGE->set_subpage($currentpage->id);
 145              // It's a system page and they are not allowed to edit system pages
 146              $USER->editing = $edit = 0;          // Disable editing completely, just to be safe
 147          }
 148      }
 149  
 150      // Add button for editing page
 151      $params = array('edit' => !$edit);
 152  
 153      $resetbutton = '';
 154      $resetstring = get_string('resetpage', 'my');
 155      $reseturl = new moodle_url("$CFG->wwwroot/my/index.php", array('edit' => 1, 'reset' => 1));
 156  
 157      if (!$currentpage->userid) {
 158          // viewing a system page -- let the user customise it
 159          $editstring = get_string('updatemymoodleon');
 160          $params['edit'] = 1;
 161      } else if (empty($edit)) {
 162          $editstring = get_string('updatemymoodleon');
 163      } else {
 164          $editstring = get_string('updatemymoodleoff');
 165          $resetbutton = $OUTPUT->single_button($reseturl, $resetstring);
 166      }
 167  
 168      $url = new moodle_url("$CFG->wwwroot/my/index.php", $params);
 169      $button = '';
 170      if (!$PAGE->theme->haseditswitch) {
 171          $button = $OUTPUT->single_button($url, $editstring);
 172      }
 173      $PAGE->set_button($resetbutton . $button);
 174  
 175  } else {
 176      $USER->editing = $edit = 0;
 177  }
 178  
 179  echo $OUTPUT->header();
 180  
 181  if (core_userfeedback::should_display_reminder()) {
 182      core_userfeedback::print_reminder_block();
 183  }
 184  
 185  echo $OUTPUT->addblockbutton('content');
 186  
 187  echo $OUTPUT->custom_block_region('content');
 188  
 189  echo $OUTPUT->footer();
 190  
 191  // Trigger dashboard has been viewed event.
 192  $eventparams = array('context' => $context);
 193  $event = \core\event\dashboard_viewed::create($eventparams);
 194  $event->trigger();