Merge branch '68137-master-prevent-removing-all-dots-in-filename' of https://github...
[moodle.git] / my / index.php
CommitLineData
1d422980 1<?php
03d9401e
MD
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
1fcf0ca8 37require_once(__DIR__ . '/../config.php');
03d9401e
MD
38require_once($CFG->dirroot . '/my/lib.php');
39
40redirect_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
41dcfbf1 44$reset = optional_param('reset', null, PARAM_BOOL);
03d9401e
MD
45
46require_login();
47
94e6f38f
FM
48$hassiteconfig = has_capability('moodle/site:config', context_system::instance());
49if ($hassiteconfig && moodle_needs_upgrading()) {
50 redirect(new moodle_url('/admin/index.php'));
51}
52
03d9401e
MD
53$strmymoodle = get_string('myhome');
54
55if (isguestuser()) { // Force them to see system default, no editing allowed
110831ce
IT
56 // If guests are not allowed my moodle, send them to front page.
57 if (empty($CFG->allowguestmymoodle)) {
58 redirect(new moodle_url('/', array('redirect' => 0)));
59 }
60
41dcfbf1 61 $userid = null;
03d9401e 62 $USER->editing = $edit = 0; // Just in case
bf0f06b1 63 $context = context_system::instance();
03d9401e
MD
64 $PAGE->set_blocks_editing_capability('moodle/my:configsyspages'); // unlikely :)
65 $header = "$SITE->shortname: $strmymoodle (GUEST)";
26a50547 66 $pagetitle = $header;
03d9401e
MD
67
68} else { // We are trying to view or edit our own My Moodle page
69 $userid = $USER->id; // Owner of the page
bf0f06b1 70 $context = context_user::instance($USER->id);
cfcfb9f3 71 $PAGE->set_blocks_editing_capability('moodle/my:manageblocks');
880c5073 72 $header = fullname($USER);
26a50547 73 $pagetitle = $strmymoodle;
03d9401e
MD
74}
75
76// Get the My Moodle page info. Should always return something unless the database is broken.
77if (!$currentpage = my_get_page($userid, MY_PAGE_PRIVATE)) {
78 print_error('mymoodlesetup');
79}
80
03d9401e
MD
81// Start setting up the page
82$params = array();
83$PAGE->set_context($context);
84$PAGE->set_url('/my/index.php', $params);
85$PAGE->set_pagelayout('mydashboard');
86$PAGE->set_pagetype('my-index');
87$PAGE->blocks->add_region('content');
88$PAGE->set_subpage($currentpage->id);
26a50547 89$PAGE->set_title($pagetitle);
03d9401e
MD
90$PAGE->set_heading($header);
91
613921c6
IT
92if (!isguestuser()) { // Skip default home page for guests
93 if (get_home_page() != HOMEPAGE_MY) {
94 if (optional_param('setdefaulthome', false, PARAM_BOOL)) {
95 set_user_preference('user_home_page_preference', HOMEPAGE_MY);
96 } else if (!empty($CFG->defaulthomepage) && $CFG->defaulthomepage == HOMEPAGE_USER) {
d9926e86
AG
97 $frontpagenode = $PAGE->settingsnav->add(get_string('frontpagesettings'), null, navigation_node::TYPE_SETTING, null);
98 $frontpagenode->force_open();
99 $frontpagenode->add(get_string('makethismyhome'), new moodle_url('/my/', array('setdefaulthome' => true)),
100 navigation_node::TYPE_SETTING);
613921c6 101 }
4766a50c
SH
102 }
103}
104
03d9401e 105// Toggle the editing state and switches
8930d282 106if (empty($CFG->forcedefaultmymoodle) && $PAGE->user_allowed_editing()) {
41dcfbf1
MA
107 if ($reset !== null) {
108 if (!is_null($userid)) {
ff77d601 109 require_sesskey();
56af0fbd 110 if (!$currentpage = my_reset_page($userid, MY_PAGE_PRIVATE)) {
41dcfbf1
MA
111 print_error('reseterror', 'my');
112 }
113 redirect(new moodle_url('/my'));
114 }
115 } else if ($edit !== null) { // Editing state was specified
03d9401e 116 $USER->editing = $edit; // Change editing state
03d9401e
MD
117 } else { // Editing state is in session
118 if ($currentpage->userid) { // It's a page we can edit, so load from session
119 if (!empty($USER->editing)) {
120 $edit = 1;
121 } else {
122 $edit = 0;
123 }
880c5073
AG
124 } else {
125 // For the page to display properly with the user context header the page blocks need to
126 // be copied over to the user context.
127 if (!$currentpage = my_copy_page($USER->id, MY_PAGE_PRIVATE)) {
128 print_error('mymoodlesetup');
129 }
130 $context = context_user::instance($USER->id);
131 $PAGE->set_context($context);
132 $PAGE->set_subpage($currentpage->id);
133 // It's a system page and they are not allowed to edit system pages
03d9401e
MD
134 $USER->editing = $edit = 0; // Disable editing completely, just to be safe
135 }
0d6b9d4f 136 }
137
03d9401e
MD
138 // Add button for editing page
139 $params = array('edit' => !$edit);
140
41dcfbf1
MA
141 $resetbutton = '';
142 $resetstring = get_string('resetpage', 'my');
143 $reseturl = new moodle_url("$CFG->wwwroot/my/index.php", array('edit' => 1, 'reset' => 1));
144
03d9401e
MD
145 if (!$currentpage->userid) {
146 // viewing a system page -- let the user customise it
147 $editstring = get_string('updatemymoodleon');
148 $params['edit'] = 1;
149 } else if (empty($edit)) {
150 $editstring = get_string('updatemymoodleon');
7527a2f0 151 } else {
03d9401e 152 $editstring = get_string('updatemymoodleoff');
41dcfbf1 153 $resetbutton = $OUTPUT->single_button($reseturl, $resetstring);
7527a2f0 154 }
155
03d9401e
MD
156 $url = new moodle_url("$CFG->wwwroot/my/index.php", $params);
157 $button = $OUTPUT->single_button($url, $editstring);
41dcfbf1 158 $PAGE->set_button($resetbutton . $button);
e84a97b8 159
03d9401e
MD
160} else {
161 $USER->editing = $edit = 0;
162}
e84a97b8 163
03d9401e 164echo $OUTPUT->header();
0d6b9d4f 165
225c418f 166echo $OUTPUT->custom_block_region('content');
0d6b9d4f 167
03d9401e 168echo $OUTPUT->footer();
94c7e9f1
SB
169
170// Trigger dashboard has been viewed event.
171$eventparams = array('context' => $context);
172$event = \core\event\dashboard_viewed::create($eventparams);
173$event->trigger();