MDL-3714 mod_forum: Add an inbound message handler
[moodle.git] / my / index.php
blob07a808a13c52cafcba4d075643a6977b40c9f07a
1 <?php
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.
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/>.
18 /**
19 * My Moodle -- a user's personal dashboard
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
27 * This script implements the user's view of the dashboard, and allows editing
28 * of the dashboard.
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
38 require_once(dirname(__FILE__) . '/../config.php');
39 require_once($CFG->dirroot . '/my/lib.php');
41 redirect_if_major_upgrade_required();
43 // TODO Add sesskey check to edit
44 $edit = optional_param('edit', null, PARAM_BOOL); // Turn editing on and off
45 $reset = optional_param('reset', null, PARAM_BOOL);
47 require_login();
49 $strmymoodle = get_string('myhome');
51 if (isguestuser()) { // Force them to see system default, no editing allowed
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)));
57 $userid = null;
58 $USER->editing = $edit = 0; // Just in case
59 $context = context_system::instance();
60 $PAGE->set_blocks_editing_capability('moodle/my:configsyspages'); // unlikely :)
61 $header = "$SITE->shortname: $strmymoodle (GUEST)";
63 } else { // We are trying to view or edit our own My Moodle page
64 $userid = $USER->id; // Owner of the page
65 $context = context_user::instance($USER->id);
66 $PAGE->set_blocks_editing_capability('moodle/my:manageblocks');
67 $header = "$SITE->shortname: $strmymoodle";
70 // Get the My Moodle page info. Should always return something unless the database is broken.
71 if (!$currentpage = my_get_page($userid, MY_PAGE_PRIVATE)) {
72 print_error('mymoodlesetup');
75 if (!$currentpage->userid) {
76 $context = context_system::instance(); // So we even see non-sticky blocks
79 // Start setting up the page
80 $params = array();
81 $PAGE->set_context($context);
82 $PAGE->set_url('/my/index.php', $params);
83 $PAGE->set_pagelayout('mydashboard');
84 $PAGE->set_pagetype('my-index');
85 $PAGE->blocks->add_region('content');
86 $PAGE->set_subpage($currentpage->id);
87 $PAGE->set_title($header);
88 $PAGE->set_heading($header);
90 if (!isguestuser()) { // Skip default home page for guests
91 if (get_home_page() != HOMEPAGE_MY) {
92 if (optional_param('setdefaulthome', false, PARAM_BOOL)) {
93 set_user_preference('user_home_page_preference', HOMEPAGE_MY);
94 } else if (!empty($CFG->defaulthomepage) && $CFG->defaulthomepage == HOMEPAGE_USER) {
95 $PAGE->settingsnav->get('usercurrentsettings')->add(get_string('makethismyhome'), new moodle_url('/my/', array('setdefaulthome'=>true)), navigation_node::TYPE_SETTING);
100 // Toggle the editing state and switches
101 if ($PAGE->user_allowed_editing()) {
102 if ($reset !== null) {
103 if (!is_null($userid)) {
104 require_sesskey();
105 if(!$currentpage = my_reset_page($userid, MY_PAGE_PRIVATE)){
106 print_error('reseterror', 'my');
108 redirect(new moodle_url('/my'));
110 } else if ($edit !== null) { // Editing state was specified
111 $USER->editing = $edit; // Change editing state
112 if (!$currentpage->userid && $edit) {
113 // If we are viewing a system page as ordinary user, and the user turns
114 // editing on, copy the system pages as new user pages, and get the
115 // new page record
116 if (!$currentpage = my_copy_page($USER->id, MY_PAGE_PRIVATE)) {
117 print_error('mymoodlesetup');
119 $context = context_user::instance($USER->id);
120 $PAGE->set_context($context);
121 $PAGE->set_subpage($currentpage->id);
123 } else { // Editing state is in session
124 if ($currentpage->userid) { // It's a page we can edit, so load from session
125 if (!empty($USER->editing)) {
126 $edit = 1;
127 } else {
128 $edit = 0;
130 } else { // It's a system page and they are not allowed to edit system pages
131 $USER->editing = $edit = 0; // Disable editing completely, just to be safe
135 // Add button for editing page
136 $params = array('edit' => !$edit);
138 $resetbutton = '';
139 $resetstring = get_string('resetpage', 'my');
140 $reseturl = new moodle_url("$CFG->wwwroot/my/index.php", array('edit' => 1, 'reset' => 1));
142 if (!$currentpage->userid) {
143 // viewing a system page -- let the user customise it
144 $editstring = get_string('updatemymoodleon');
145 $params['edit'] = 1;
146 } else if (empty($edit)) {
147 $editstring = get_string('updatemymoodleon');
148 } else {
149 $editstring = get_string('updatemymoodleoff');
150 $resetbutton = $OUTPUT->single_button($reseturl, $resetstring);
153 $url = new moodle_url("$CFG->wwwroot/my/index.php", $params);
154 $button = $OUTPUT->single_button($url, $editstring);
155 $PAGE->set_button($resetbutton . $button);
157 } else {
158 $USER->editing = $edit = 0;
161 // HACK WARNING! This loads up all this page's blocks in the system context
162 if ($currentpage->userid == 0) {
163 $CFG->blockmanagerclass = 'my_syspage_block_manager';
167 echo $OUTPUT->header();
169 echo $OUTPUT->custom_block_region('content');
171 echo $OUTPUT->footer();