MDL-49689 navigation: Allow the user to set their homepage.
[moodle.git] / my / index.php
CommitLineData
1d422980 1<?php
0d6b9d4f 2
03d9401e
MD
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * My Moodle -- a user's personal dashboard
20 *
21 * - each user can currently have their own page (cloned from system and then customised)
22 * - only the user can see their own dashboard
23 * - users can add any blocks they want
24 * - the administrators can define a default site dashboard for users who have
25 * not created their own dashboard
26 *
27 * This script implements the user's view of the dashboard, and allows editing
28 * of the dashboard.
29 *
30 * @package moodlecore
31 * @subpackage my
32 * @copyright 2010 Remote-Learner.net
33 * @author Hubert Chathi <hubert@remote-learner.net>
34 * @author Olav Jordan <olav.jordan@remote-learner.net>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37
38require_once(dirname(__FILE__) . '/../config.php');
39require_once($CFG->dirroot . '/my/lib.php');
40
41redirect_if_major_upgrade_required();
42
43// TODO Add sesskey check to edit
44$edit = optional_param('edit', null, PARAM_BOOL); // Turn editing on and off
41dcfbf1 45$reset = optional_param('reset', null, PARAM_BOOL);
03d9401e
MD
46
47require_login();
48
49$strmymoodle = get_string('myhome');
50
51if (isguestuser()) { // Force them to see system default, no editing allowed
110831ce
IT
52 // If guests are not allowed my moodle, send them to front page.
53 if (empty($CFG->allowguestmymoodle)) {
54 redirect(new moodle_url('/', array('redirect' => 0)));
55 }
56
41dcfbf1 57 $userid = null;
03d9401e 58 $USER->editing = $edit = 0; // Just in case
bf0f06b1 59 $context = context_system::instance();
03d9401e
MD
60 $PAGE->set_blocks_editing_capability('moodle/my:configsyspages'); // unlikely :)
61 $header = "$SITE->shortname: $strmymoodle (GUEST)";
26a50547 62 $pagetitle = $header;
03d9401e
MD
63
64} else { // We are trying to view or edit our own My Moodle page
65 $userid = $USER->id; // Owner of the page
bf0f06b1 66 $context = context_user::instance($USER->id);
cfcfb9f3 67 $PAGE->set_blocks_editing_capability('moodle/my:manageblocks');
880c5073 68 $header = fullname($USER);
26a50547 69 $pagetitle = $strmymoodle;
03d9401e
MD
70}
71
72// Get the My Moodle page info. Should always return something unless the database is broken.
73if (!$currentpage = my_get_page($userid, MY_PAGE_PRIVATE)) {
74 print_error('mymoodlesetup');
75}
76
03d9401e
MD
77// Start setting up the page
78$params = array();
79$PAGE->set_context($context);
80$PAGE->set_url('/my/index.php', $params);
81$PAGE->set_pagelayout('mydashboard');
82$PAGE->set_pagetype('my-index');
83$PAGE->blocks->add_region('content');
84$PAGE->set_subpage($currentpage->id);
26a50547 85$PAGE->set_title($pagetitle);
03d9401e
MD
86$PAGE->set_heading($header);
87
613921c6
IT
88if (!isguestuser()) { // Skip default home page for guests
89 if (get_home_page() != HOMEPAGE_MY) {
90 if (optional_param('setdefaulthome', false, PARAM_BOOL)) {
91 set_user_preference('user_home_page_preference', HOMEPAGE_MY);
92 } else if (!empty($CFG->defaulthomepage) && $CFG->defaulthomepage == HOMEPAGE_USER) {
d9926e86
AG
93 $frontpagenode = $PAGE->settingsnav->add(get_string('frontpagesettings'), null, navigation_node::TYPE_SETTING, null);
94 $frontpagenode->force_open();
95 $frontpagenode->add(get_string('makethismyhome'), new moodle_url('/my/', array('setdefaulthome' => true)),
96 navigation_node::TYPE_SETTING);
613921c6 97 }
4766a50c
SH
98 }
99}
100
03d9401e
MD
101// Toggle the editing state and switches
102if ($PAGE->user_allowed_editing()) {
41dcfbf1
MA
103 if ($reset !== null) {
104 if (!is_null($userid)) {
ff77d601 105 require_sesskey();
41dcfbf1
MA
106 if(!$currentpage = my_reset_page($userid, MY_PAGE_PRIVATE)){
107 print_error('reseterror', 'my');
108 }
109 redirect(new moodle_url('/my'));
110 }
111 } else if ($edit !== null) { // Editing state was specified
03d9401e 112 $USER->editing = $edit; // Change editing state
03d9401e
MD
113 } else { // Editing state is in session
114 if ($currentpage->userid) { // It's a page we can edit, so load from session
115 if (!empty($USER->editing)) {
116 $edit = 1;
117 } else {
118 $edit = 0;
119 }
880c5073
AG
120 } else {
121 // For the page to display properly with the user context header the page blocks need to
122 // be copied over to the user context.
123 if (!$currentpage = my_copy_page($USER->id, MY_PAGE_PRIVATE)) {
124 print_error('mymoodlesetup');
125 }
126 $context = context_user::instance($USER->id);
127 $PAGE->set_context($context);
128 $PAGE->set_subpage($currentpage->id);
129 // It's a system page and they are not allowed to edit system pages
03d9401e
MD
130 $USER->editing = $edit = 0; // Disable editing completely, just to be safe
131 }
0d6b9d4f 132 }
133
03d9401e
MD
134 // Add button for editing page
135 $params = array('edit' => !$edit);
136
41dcfbf1
MA
137 $resetbutton = '';
138 $resetstring = get_string('resetpage', 'my');
139 $reseturl = new moodle_url("$CFG->wwwroot/my/index.php", array('edit' => 1, 'reset' => 1));
140
03d9401e
MD
141 if (!$currentpage->userid) {
142 // viewing a system page -- let the user customise it
143 $editstring = get_string('updatemymoodleon');
144 $params['edit'] = 1;
145 } else if (empty($edit)) {
146 $editstring = get_string('updatemymoodleon');
7527a2f0 147 } else {
03d9401e 148 $editstring = get_string('updatemymoodleoff');
41dcfbf1 149 $resetbutton = $OUTPUT->single_button($reseturl, $resetstring);
7527a2f0 150 }
151
03d9401e
MD
152 $url = new moodle_url("$CFG->wwwroot/my/index.php", $params);
153 $button = $OUTPUT->single_button($url, $editstring);
41dcfbf1 154 $PAGE->set_button($resetbutton . $button);
e84a97b8 155
03d9401e
MD
156} else {
157 $USER->editing = $edit = 0;
158}
e84a97b8 159
03d9401e 160echo $OUTPUT->header();
0d6b9d4f 161
225c418f 162echo $OUTPUT->custom_block_region('content');
0d6b9d4f 163
03d9401e 164echo $OUTPUT->footer();