MDL-71913 mod_h5pactivity: Add attempts report setting node
[moodle.git] / my / index.php
blob8a741243601c242891cf4e9a1766742a9ec59bce
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.
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/>.
17 /**
18 * My Moodle -- a user's personal dashboard
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
26 * This script implements the user's view of the dashboard, and allows editing
27 * of the dashboard.
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
37 require_once(__DIR__ . '/../config.php');
38 require_once($CFG->dirroot . '/my/lib.php');
40 redirect_if_major_upgrade_required();
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);
46 require_login();
48 $hassiteconfig = has_capability('moodle/site:config', context_system::instance());
49 if ($hassiteconfig && moodle_needs_upgrading()) {
50 redirect(new moodle_url('/admin/index.php'));
53 $strmymoodle = get_string('myhome');
55 if (isguestuser()) { // Force them to see system default, no editing allowed
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)));
61 $userid = null;
62 $USER->editing = $edit = 0; // Just in case
63 $context = context_system::instance();
64 $PAGE->set_blocks_editing_capability('moodle/my:configsyspages'); // unlikely :)
65 $strguest = get_string('guest');
66 $header = "$SITE->shortname: $strmymoodle ($strguest)";
67 $pagetitle = $header;
69 } else { // We are trying to view or edit our own My Moodle page
70 $userid = $USER->id; // Owner of the page
71 $context = context_user::instance($USER->id);
72 $PAGE->set_blocks_editing_capability('moodle/my:manageblocks');
73 $header = "$SITE->shortname: $strmymoodle";
74 $pagetitle = $strmymoodle;
77 // Get the My Moodle page info. Should always return something unless the database is broken.
78 if (!$currentpage = my_get_page($userid, MY_PAGE_PRIVATE)) {
79 print_error('mymoodlesetup');
82 // Start setting up the page
83 $params = array();
84 $PAGE->set_context($context);
85 $PAGE->set_url('/my/index.php', $params);
86 $PAGE->set_pagelayout('mydashboard');
87 $PAGE->set_pagetype('my-index');
88 $PAGE->blocks->add_region('content');
89 $PAGE->set_subpage($currentpage->id);
90 $PAGE->set_title($pagetitle);
91 $PAGE->set_heading($header);
92 $PAGE->has_secondary_navigation_setter(false);
94 if (!isguestuser()) { // Skip default home page for guests
95 if (get_home_page() != HOMEPAGE_MY) {
96 if (optional_param('setdefaulthome', false, PARAM_BOOL)) {
97 set_user_preference('user_home_page_preference', HOMEPAGE_MY);
98 } else if (!empty($CFG->defaulthomepage) && $CFG->defaulthomepage == HOMEPAGE_USER) {
99 $frontpagenode = $PAGE->settingsnav->add(get_string('frontpagesettings'), null, navigation_node::TYPE_SETTING, null);
100 $frontpagenode->force_open();
101 $frontpagenode->add(get_string('makethismyhome'), new moodle_url('/my/', array('setdefaulthome' => true)),
102 navigation_node::TYPE_SETTING);
107 // Toggle the editing state and switches
108 if (empty($CFG->forcedefaultmymoodle) && $PAGE->user_allowed_editing()) {
109 if ($reset !== null) {
110 if (!is_null($userid)) {
111 require_sesskey();
112 if (!$currentpage = my_reset_page($userid, MY_PAGE_PRIVATE)) {
113 print_error('reseterror', 'my');
115 redirect(new moodle_url('/my'));
117 } else if ($edit !== null) { // Editing state was specified
118 $USER->editing = $edit; // Change editing state
119 } else { // Editing state is in session
120 if ($currentpage->userid) { // It's a page we can edit, so load from session
121 if (!empty($USER->editing)) {
122 $edit = 1;
123 } else {
124 $edit = 0;
126 } else {
127 // For the page to display properly with the user context header the page blocks need to
128 // be copied over to the user context.
129 if (!$currentpage = my_copy_page($USER->id, MY_PAGE_PRIVATE)) {
130 print_error('mymoodlesetup');
132 $context = context_user::instance($USER->id);
133 $PAGE->set_context($context);
134 $PAGE->set_subpage($currentpage->id);
135 // It's a system page and they are not allowed to edit system pages
136 $USER->editing = $edit = 0; // Disable editing completely, just to be safe
140 // Add button for editing page
141 $params = array('edit' => !$edit);
143 $resetbutton = '';
144 $resetstring = get_string('resetpage', 'my');
145 $reseturl = new moodle_url("$CFG->wwwroot/my/index.php", array('edit' => 1, 'reset' => 1));
147 if (!$currentpage->userid) {
148 // viewing a system page -- let the user customise it
149 $editstring = get_string('updatemymoodleon');
150 $params['edit'] = 1;
151 } else if (empty($edit)) {
152 $editstring = get_string('updatemymoodleon');
153 } else {
154 $editstring = get_string('updatemymoodleoff');
155 $resetbutton = $OUTPUT->single_button($reseturl, $resetstring);
158 $url = new moodle_url("$CFG->wwwroot/my/index.php", $params);
159 $button = '';
160 if (!$PAGE->theme->haseditswitch) {
161 $button = $OUTPUT->single_button($url, $editstring);
163 $PAGE->set_button($resetbutton . $button);
165 } else {
166 $USER->editing = $edit = 0;
169 echo $OUTPUT->header();
171 if (core_userfeedback::should_display_reminder()) {
172 core_userfeedback::print_reminder_block();
175 echo $OUTPUT->custom_block_region('content');
177 echo $OUTPUT->footer();
179 // Trigger dashboard has been viewed event.
180 $eventparams = array('context' => $context);
181 $event = \core\event\dashboard_viewed::create($eventparams);
182 $event->trigger();