Permalink
Browse files

MDL-25249 Tidied up calendar variable passing and removed session abuse

  • Loading branch information...
Sam Hemelryk
Sam Hemelryk committed Jun 29, 2011
1 parent 7fde489 commit 797cedc7cf777f307624f1f17e1d36c1cc4e5585
@@ -19,8 +19,6 @@ function get_content() {
if ($this->content !== NULL) {
return $this->content;
}
- // Reset the session variables
- calendar_session_vars($this->page->course);
$this->content = new stdClass;
$this->content->text = '';
@@ -29,52 +27,30 @@ function get_content() {
// [pj] To me it looks like this if would never be needed, but Penny added it
// when committing the /my/ stuff. Reminder to discuss and learn what it's about.
// It definitely needs SOME comment here!
- $courseshown = $this->page->course->id;
+ $courseid = $this->page->course->id;
+ $issite = ($courseid == SITEID);
- if ($courseshown == SITEID) {
+ if ($issite) {
// Being displayed at site level. This will cause the filter to fall back to auto-detecting
// the list of courses it will be grabbing events from.
- $filtercourse = NULL;
- $groupeventsfrom = NULL;
- $SESSION->cal_courses_shown = calendar_get_default_courses(true);
- calendar_set_referring_course(0);
-
+ $filtercourse = calendar_get_default_courses();
} else {
// Forcibly filter events to include only those from the particular course we are in.
- $filtercourse = array($courseshown => $this->page->course);
- $groupeventsfrom = array($courseshown => 1);
- }
-
- // We 'll need this later
- calendar_set_referring_course($courseshown);
-
- // MDL-9059, set to show this course when admins go into a course, then unset it.
- if ($this->page->course->id != SITEID && !isset($SESSION->cal_courses_shown[$this->page->course->id]) && has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_SYSTEM))) {
- $courseset = true;
- $SESSION->cal_courses_shown[$this->page->course->id] = $this->page->course;
+ $filtercourse = array($courseid => $this->page->course);
}
- // Be VERY careful with the format for default courses arguments!
- // Correct formatting is [courseid] => 1 to be concise with moodlelib.php functions.
- calendar_set_filters($courses, $group, $user, $filtercourse, $groupeventsfrom, false);
- if ($courseshown == SITEID) {
+ list($courses, $group, $user) = calendar_set_filters($filtercourse);
+ if ($issite) {
// For the front page
- $this->content->text .= calendar_top_controls('frontpage', array('id' => $courseshown, 'm' => $cal_m, 'y' => $cal_y));
+ $this->content->text .= calendar_top_controls('frontpage', array('id' => $courseid, 'm' => $cal_m, 'y' => $cal_y));
$this->content->text .= calendar_get_mini($courses, $group, $user, $cal_m, $cal_y);
// No filters for now
-
} else {
// For any other course
- $this->content->text .= calendar_top_controls('course', array('id' => $courseshown, 'm' => $cal_m, 'y' => $cal_y));
+ $this->content->text .= calendar_top_controls('course', array('id' => $courseid, 'm' => $cal_m, 'y' => $cal_y));
$this->content->text .= calendar_get_mini($courses, $group, $user, $cal_m, $cal_y);
$this->content->text .= '<h3 class="eventskey">'.get_string('eventskey', 'calendar').'</h3>';
- $this->content->text .= '<div class="filters">'.calendar_filter_controls('course', '', $this->page->course).'</div>';
-
- }
-
- // MDL-9059, unset this so that it doesn't stay in session
- if (!empty($courseset)) {
- unset($SESSION->cal_courses_shown[$this->page->course->id]);
+ $this->content->text .= '<div class="filters">'.calendar_filter_controls($this->page->url).'</div>';
}
return $this->content;
@@ -15,14 +15,12 @@ function get_content() {
if ($this->content !== NULL) {
return $this->content;
}
- // Reset the session variables
- calendar_session_vars($this->page->course);
$this->content = new stdClass;
$this->content->text = '';
+ $filtercourse = array();
if (empty($this->instance)) { // Overrides: use no course at all
$courseshown = false;
- $filtercourse = array();
$this->content->footer = '';
} else {
@@ -31,45 +29,42 @@ function get_content() {
'/calendar/view.php?view=upcoming&amp;course='.$courseshown.'">'.
get_string('gotocalendar', 'calendar').'</a>...</div>';
$context = get_context_instance(CONTEXT_COURSE, $courseshown);
- if (has_capability('moodle/calendar:manageentries', $context) ||
- has_capability('moodle/calendar:manageownentries', $context)) {
+ if (has_any_capability(array('moodle/calendar:manageentries', 'moodle/calendar:manageownentries'), $context)) {
$this->content->footer .= '<div class="newevent"><a href="'.$CFG->wwwroot.
'/calendar/event.php?action=new&amp;course='.$courseshown.'">'.
get_string('newevent', 'calendar').'</a>...</div>';
}
if ($courseshown == SITEID) {
// Being displayed at site level. This will cause the filter to fall back to auto-detecting
// the list of courses it will be grabbing events from.
- $filtercourse = NULL;
- $groupeventsfrom = NULL;
- $SESSION->cal_courses_shown = calendar_get_default_courses(true);
- calendar_set_referring_course(0);
+ $filtercourse = calendar_get_default_courses();
} else {
// Forcibly filter events to include only those from the particular course we are in.
- $filtercourse = array($courseshown => $this->page->course);
- $groupeventsfrom = array($courseshown => 1);
+ $filtercourse = array($courseshown => $this->page->course);
}
}
- // We 'll need this later
- calendar_set_referring_course($courseshown);
+ list($courses, $group, $user) = calendar_set_filters($filtercourse);
- // Be VERY careful with the format for default courses arguments!
- // Correct formatting is [courseid] => 1 to be concise with moodlelib.php functions.
+ $defaultlookahead = CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD;
+ if (isset($CFG->calendar_lookahead)) {
+ $defaultlookahead = intval($CFG->calendar_lookahead);
+ }
+ $lookahead = get_user_preferences('calendar_lookahead', $defaultlookahead);
- calendar_set_filters($courses, $group, $user, $filtercourse, $groupeventsfrom, false);
- $events = calendar_get_upcoming($courses, $group, $user,
- get_user_preferences('calendar_lookahead', CALENDAR_UPCOMING_DAYS),
- get_user_preferences('calendar_maxevents', CALENDAR_UPCOMING_MAXEVENTS));
+ $defaultmaxevents = CALENDAR_DEFAULT_UPCOMING_MAXEVENTS;
+ if (isset($CFG->calendar_maxevents)) {
+ $defaultmaxevents = intval($CFG->calendar_maxevents);
+ }
+ $maxevents = get_user_preferences('calendar_maxevents', $defaultmaxevents);
+ $events = calendar_get_upcoming($courses, $group, $user, $lookahead, $maxevents);
if (!empty($this->instance)) {
- $this->content->text = calendar_get_block_upcoming($events,
- 'view.php?view=day&amp;course='.$courseshown.'&amp;');
+ $this->content->text = calendar_get_block_upcoming($events, 'view.php?view=day&amp;course='.$courseshown.'&amp;');
}
if (empty($this->content->text)) {
- $this->content->text = '<div class="post">'.
- get_string('noupcomingevents', 'calendar').'</div>';
+ $this->content->text = '<div class="post">'. get_string('noupcomingevents', 'calendar').'</div>';
}
return $this->content;
View
@@ -55,83 +55,54 @@
$action = optional_param('action', 'new', PARAM_ALPHA);
$eventid = optional_param('id', 0, PARAM_INT);
-$courseid = optional_param('courseid', 0, PARAM_INT);
+$courseid = optional_param('courseid', SITEID, PARAM_INT);
+$courseid = optional_param('course', $courseid, PARAM_INT);
$cal_y = optional_param('cal_y', 0, PARAM_INT);
$cal_m = optional_param('cal_m', 0, PARAM_INT);
$cal_d = optional_param('cal_d', 0, PARAM_INT);
-if ($courseid === 0) {
- $courseid = optional_param('course', 0, PARAM_INT);
+$url = new moodle_url('/calendar/event.php', array('action' => $action));
+if ($eventid != 0) {
+ $url->param('id', $eventid);
+}
+if ($courseid != SITEID) {
+ $url->param('course', $courseid);
+}
+if ($cal_y !== 0) {
+ $url->param('cal_y', $cal_y);
+}
+if ($cal_m !== 0) {
+ $url->param('cal_m', $cal_m);
+}
+if ($cal_d !== 0) {
+ $url->param('cal_d', $cal_d);
}
-
-$url = new moodle_url('/calendar/event.php', array('action'=>$action));
-if ($eventid !== 0) $url->param('id', $eventid);
-if ($courseid !== 0) $url->param('course', $courseid);
-if ($cal_y !== 0) $url->param('cal_y', $cal_y);
-if ($cal_m !== 0) $url->param('cal_m', $cal_m);
-if ($cal_d !== 0) $url->param('cal_d', $cal_d);
$PAGE->set_url($url);
-$PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
$PAGE->set_pagelayout('standard');
-if ($action === 'delete' && $eventid>0) {
+if ($courseid != SITEID && !empty($courseid)) {
+ $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
+ $courses = array($course->id => $course);
+ $issite = false;
+} else {
+ $course = get_site();
+ $courses = calendar_get_default_courses();
+ $issite = true;
+}
+require_login($course, false);
+
+if ($action === 'delete' && $eventid > 0) {
$deleteurl = new moodle_url('/calendar/delete.php', array('id'=>$eventid));
if ($courseid > 0) {
$deleteurl->param('course', $courseid);
}
redirect($deleteurl);
}
-$viewcalendarurl = new moodle_url(CALENDAR_URL.'view.php');
-$viewcalendarurl->params($PAGE->url->params());
-$viewcalendarurl->remove_params(array('id','action'));
-
-$now = usergetdate(time());
-
-if (isguestuser()) {
- // Guests cannot do anything with events
- redirect(new moodle_url(CALENDAR_URL.'view.php', array('view'=>'upcoming', 'course'=>$courseid)));
-}
-
-$focus = '';
-
-$site = get_site();
-
-calendar_session_vars();
-
-// If a course has been supplied in the URL, change the filters to show that one
-$courseexists = false;
-if ($courseid > 0) {
- if ($courseid == SITEID) {
- // If coming from the site page, show all courses
- $SESSION->cal_courses_shown = calendar_get_default_courses(true);
- calendar_set_referring_course(0);
- } else if ($DB->record_exists('course', array('id'=>$courseid))) {
- $courseexists = true;
- // Otherwise show just this one
- $SESSION->cal_courses_shown = $courseid;
- calendar_set_referring_course($SESSION->cal_courses_shown);
- }
-}
-
-if (!empty($SESSION->cal_course_referer)) {
- // TODO: This is part of the Great $course Hack in Moodle. Replace it at some point.
- $course = $DB->get_record('course', array('id'=>$SESSION->cal_course_referer));
-} else {
- $course = $site;
-}
-
-require_login($course, false);
-
$calendar = new calendar_information($cal_d, $cal_m, $cal_y);
-$calendar->courseid = $courseid;
-
-$strcalendar = get_string('calendar', 'calendar');
-$link = clone($viewcalendarurl);
-$link->param('view', 'upcoming');
+$calendar->prepare_for_view($course, $courses);
$formoptions = new stdClass;
-
if ($eventid !== 0) {
$title = get_string('editevent', 'calendar');
$event = calendar_event::load($eventid);
@@ -148,13 +119,13 @@
}
} else {
$title = get_string('newevent', 'calendar');
- calendar_get_allowed_types($formoptions->eventtypes, $USER->id);
+ calendar_get_allowed_types($formoptions->eventtypes, $course);
$event = new stdClass();
$event->action = $action;
$event->course = $courseid;
$event->timeduration = 0;
if ($formoptions->eventtypes->courses) {
- if ($courseexists) {
+ if (!$issite) {
$event->courseid = $courseid;
$event->eventtype = 'course';
} else {
@@ -165,6 +136,7 @@
if($cal_y && $cal_m && $cal_d && checkdate($cal_m, $cal_d, $cal_y)) {
$event->timestart = make_timestamp($cal_y, $cal_m, $cal_d, 0, 0, 0);
} else if($cal_y && $cal_m && checkdate($cal_m, 1, $cal_y)) {
+ $now = usergetdate(time());
if($cal_y == $now['year'] && $cal_m == $now['mon']) {
$event->timestart = make_timestamp($cal_y, $cal_m, $now['mday'], 0, 0, 0);
} else {
@@ -193,23 +165,31 @@
}
$event->update($data);
- $eventurl = new moodle_url(CALENDAR_URL.'view.php', array('view'=>'day'));
- if (!empty($event->courseid)) {
+
+ $params = array(
+ 'view' => 'day',
+ 'cal_d' => date('j', $event->timestart),
+ 'cal_m' => date('n', $event->timestart),
+ 'cal_y' => date('y', $event->timestart),
+ );
+ $eventurl = new moodle_url('/calendar/view.php', $params);
+ if (!empty($event->courseid) && $event->courseid != SITEID) {
$eventurl->param('course', $event->courseid);
}
- $eventurl->param('cal_d', date('j', $event->timestart));
- $eventurl->param('cal_m', date('n', $event->timestart));
- $eventurl->param('cal_y', date('Y', $event->timestart));
$eventurl->set_anchor('event_'.$event->id);
redirect($eventurl);
}
-$PAGE->navbar->add($strcalendar, $link);
+$viewcalendarurl = new moodle_url(CALENDAR_URL.'view.php', $PAGE->url->params());
+$viewcalendarurl->remove_params(array('id', 'action'));
+$viewcalendarurl->param('view', 'upcoming');
+$strcalendar = get_string('calendar', 'calendar');
+
+$PAGE->navbar->add($strcalendar, $viewcalendarurl);
$PAGE->navbar->add($title);
-$PAGE->set_title($site->shortname.': '.$strcalendar.': '.$title);
-$PAGE->set_heading($COURSE->fullname);
+$PAGE->set_title($course->shortname.': '.$strcalendar.': '.$title);
+$PAGE->set_heading($course->fullname);
-calendar_set_filters($calendar->courses, $calendar->groups, $calendar->users);
$renderer = $PAGE->get_renderer('core_calendar');
$calendar->add_sidecalendar_blocks($renderer);
Oops, something went wrong.

0 comments on commit 797cedc

Please sign in to comment.