Merge branch 'MDL-19171' of https://github.com/bostelm/moodle
[moodle.git] / blocks / calendar_upcoming / block_calendar_upcoming.php
1 <?php
3 class block_calendar_upcoming extends block_base {
4     function init() {
5         $this->title = get_string('pluginname', 'block_calendar_upcoming');
6     }
8     function get_content() {
9         global $USER, $CFG, $SESSION;
10         $cal_m = optional_param( 'cal_m', 0, PARAM_INT );
11         $cal_y = optional_param( 'cal_y', 0, PARAM_INT );
13         require_once($CFG->dirroot.'/calendar/lib.php');
15         if ($this->content !== NULL) {
16             return $this->content;
17         }
18         $this->content = new stdClass;
19         $this->content->text = '';
21         $filtercourse    = array();
22         if (empty($this->instance)) { // Overrides: use no course at all
23             $courseshown = false;
24             $this->content->footer = '';
26         } else {
27             $courseshown = $this->page->course->id;
28             $this->content->footer = '<div class="gotocal"><a href="'.$CFG->wwwroot.
29                                      '/calendar/view.php?view=upcoming&amp;course='.$courseshown.'">'.
30                                       get_string('gotocalendar', 'calendar').'</a>...</div>';
31             $context = context_course::instance($courseshown);
32             if (has_any_capability(array('moodle/calendar:manageentries', 'moodle/calendar:manageownentries'), $context)) {
33                 $this->content->footer .= '<div class="newevent"><a href="'.$CFG->wwwroot.
34                                           '/calendar/event.php?action=new&amp;course='.$courseshown.'">'.
35                                            get_string('newevent', 'calendar').'</a>...</div>';
36             }
37             if ($courseshown == SITEID) {
38                 // Being displayed at site level. This will cause the filter to fall back to auto-detecting
39                 // the list of courses it will be grabbing events from.
40                 $filtercourse = calendar_get_default_courses();
41             } else {
42                 // Forcibly filter events to include only those from the particular course we are in.
43                 $filtercourse = array($courseshown => $this->page->course);
44             }
45         }
47         list($courses, $group, $user) = calendar_set_filters($filtercourse);
49         $defaultlookahead = CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD;
50         if (isset($CFG->calendar_lookahead)) {
51             $defaultlookahead = intval($CFG->calendar_lookahead);
52         }
53         $lookahead = get_user_preferences('calendar_lookahead', $defaultlookahead);
55         $defaultmaxevents = CALENDAR_DEFAULT_UPCOMING_MAXEVENTS;
56         if (isset($CFG->calendar_maxevents)) {
57             $defaultmaxevents = intval($CFG->calendar_maxevents);
58         }
59         $maxevents = get_user_preferences('calendar_maxevents', $defaultmaxevents);
60         $events = calendar_get_upcoming($courses, $group, $user, $lookahead, $maxevents);
62         if (!empty($this->instance)) {
63             $link = 'view.php?view=day&amp;course='.$courseshown.'&amp;';
64             $showcourselink = ($this->page->course->id == SITEID);
65             $this->content->text = calendar_get_block_upcoming($events, $link, $showcourselink);
66         }
68         if (empty($this->content->text)) {
69             $this->content->text = '<div class="post">'. get_string('noupcomingevents', 'calendar').'</div>';
70         }
72         return $this->content;
73     }
74 }