index.php 6.5 KB

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