Merge branch 'wip-mdl-57851-m32' of https://github.com/rajeshtaneja/moodle into MOODL...
[moodle.git] / user / calendar.php
blob486d5d63c28ca187973f8cc784d4761cd48e9565
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 * Allows you to edit a users profile
20 * @copyright 2015 Shamim Rezaie http://foodle.org
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 * @package core_user
25 require_once('../config.php');
26 require_once($CFG->dirroot.'/calendar/lib.php');
27 require_once($CFG->dirroot.'/user/editlib.php');
28 require_once($CFG->dirroot.'/user/lib.php');
30 $userid = optional_param('id', $USER->id, PARAM_INT); // User id.
32 $PAGE->set_url('/user/calendar.php', array('id' => $userid));
34 list($user, $course) = useredit_setup_preference_page($userid, SITEID);
36 $defaultlookahead = CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD;
37 if (isset($CFG->calendar_lookahead)) {
38 $defaultlookahead = intval($CFG->calendar_lookahead);
40 $defaultmaxevents = CALENDAR_DEFAULT_UPCOMING_MAXEVENTS;
41 if (isset($CFG->calendar_maxevents)) {
42 $defaultmaxevents = intval($CFG->calendar_maxevents);
45 // Create form.
46 $calendarform = new core_user\form\calendar_form(null, array('userid' => $user->id));
48 $user->timeformat = get_user_preferences('calendar_timeformat', '');
49 $user->startwday = calendar_get_starting_weekday();
50 $user->maxevents = get_user_preferences('calendar_maxevents', $defaultmaxevents);
51 $user->lookahead = get_user_preferences('calendar_lookahead', $defaultlookahead);
52 $user->persistflt = get_user_preferences('calendar_persistflt', 0);
53 $calendarform->set_data($user);
55 $redirect = new moodle_url("/user/preferences.php", array('userid' => $user->id));
56 if ($calendarform->is_cancelled()) {
57 redirect($redirect);
58 } else if ($calendarform->is_submitted() && $calendarform->is_validated() && confirm_sesskey()) {
59 $data = $calendarform->get_data();
61 // Time format.
62 if ($data->timeformat != CALENDAR_TF_12 && $data->timeformat != CALENDAR_TF_24) {
63 $data->timeformat = '';
65 set_user_preference('calendar_timeformat', $data->timeformat);
67 // Start weekday.
68 $data->startwday = intval($data->startwday);
69 if ($data->startwday < 0 || $data->startwday > 6) {
70 $data->startwday = abs($data->startwday % 7);
72 set_user_preference('calendar_startwday', $data->startwday);
74 // Calendar events.
75 if (intval($data->maxevents) >= 1) {
76 set_user_preference('calendar_maxevents', $data->maxevents);
79 // Calendar lookahead.
80 if (intval($data->lookahead) >= 1) {
81 set_user_preference('calendar_lookahead', $data->lookahead);
84 set_user_preference('calendar_persistflt', intval($data->persistflt));
86 // Calendar type.
87 $calendartype = $data->calendartype;
88 // If the specified calendar type does not exist, use the site default.
89 if (!array_key_exists($calendartype, \core_calendar\type_factory::get_list_of_calendar_types())) {
90 $calendartype = $CFG->calendartype;
93 $user->calendartype = $calendartype;
94 // Update user with new calendar type.
95 user_update_user($user, false, false);
97 // Trigger event.
98 \core\event\user_updated::create_from_userid($user->id)->trigger();
100 if ($USER->id == $user->id) {
101 $USER->calendartype = $calendartype;
104 redirect($redirect);
107 // Display page header.
108 $streditmycalendar = get_string('calendarpreferences', 'calendar');
109 $userfullname = fullname($user, true);
111 $PAGE->navbar->includesettingsbase = true;
113 $PAGE->set_title("$course->shortname: $streditmycalendar");
114 $PAGE->set_heading($userfullname);
116 echo $OUTPUT->header();
117 echo $OUTPUT->heading($streditmycalendar);
119 // Finally display THE form.
120 $calendarform->display();
122 // And proper footer.
123 echo $OUTPUT->footer();