MDL-18375 calendar: added multiple calendar support
[moodle.git] / blocks / calendar_month / block_calendar_month.php
1 <?php
3 class block_calendar_month extends block_base {
4     function init() {
5         $this->title = get_string('pluginname', 'block_calendar_month');
6     }
8     function preferred_width() {
9         return 210;
10     }
12     function get_content() {
13         global $USER, $CFG, $SESSION, $COURSE, $OUTPUT;
14         $cal_m = optional_param( 'cal_m', 0, PARAM_INT );
15         $cal_y = optional_param( 'cal_y', 0, PARAM_INT );
17         require_once($CFG->dirroot.'/calendar/lib.php');
19         if ($this->content !== NULL) {
20             return $this->content;
21         }
23         $this->content = new stdClass;
24         $this->content->text = '';
25         $this->content->footer = '';
27         // [pj] To me it looks like this if would never be needed, but Penny added it
28         // when committing the /my/ stuff. Reminder to discuss and learn what it's about.
29         // It definitely needs SOME comment here!
30         $courseid = $this->page->course->id;
31         $issite = ($courseid == SITEID);
33         if ($issite) {
34             // Being displayed at site level. This will cause the filter to fall back to auto-detecting
35             // the list of courses it will be grabbing events from.
36             $filtercourse = calendar_get_default_courses();
37         } else {
38             // Forcibly filter events to include only those from the particular course we are in.
39             $filtercourse = array($courseid => $this->page->course);
40         }
42         list($courses, $group, $user) = calendar_set_filters($filtercourse);
43         if ($issite) {
44             // For the front page
45             $this->content->text .= calendar_get_mini($courses, $group, $user, $cal_m, $cal_y, 'frontpage', $courseid);
46             // No filters for now
47         } else {
48             // For any other course
49             $this->content->text .= calendar_get_mini($courses, $group, $user, $cal_m, $cal_y, 'course', $courseid);
50             $this->content->text .= '<h3 class="eventskey">'.get_string('eventskey', 'calendar').'</h3>';
51             $this->content->text .= '<div class="filters calendar_filters">'.calendar_filter_controls($this->page->url).'</div>';
52         }
54         // MDL-18375, Multi-Calendar Support
55         if (empty($COURSE->calendarsystem)) {
56             // the course has not a forced calendarsystem
57             // so user can change it.
58             $url = $CFG->wwwroot . (!empty($COURSE->id) && ($COURSE->id!= SITEID) ? "/course/view.php?id={$COURSE->id}" : '/index.php');
59             $url = new moodle_url($url);
61             $calendarselect = new single_select($url, 'calendarsystem', get_list_of_calendars(), current_calendarsystem_plugin(), false, 'choosecalendar');
62             $calendarselect->set_label('<span style="font-weight: normal;">'.get_string('system', 'calendarsystem').'</span>');
64             $this->content->text .= '
65             <div id="changecalendarlink" style="display: none; visibility: hidden;">
66                 <a style="font-weight: normal;" onclick="return toggleCalendarVisibility(document.getElementById(\'choosecalendar\'))" href="#">
67                 ' . get_string('changecalendar', 'calendarsystem') . '
68                 </a>
69             </div>
70             ' . $OUTPUT->render($calendarselect) . '
71             <script language="JavaScript">
72                 function toggleCalendarVisibility (choosecalendar) {
73                     if (choosecalendar.style.visibility != "visible") {
74                         choosecalendar.style.display = "block";
75                         choosecalendar.style.visibility = "visible";
76                     } else {
77                         choosecalendar.style.display = "none";
78                         choosecalendar.style.visibility = "hidden";
79                     }
81                     return false;
82                 }
84                 document.getElementById ( "choosecalendar" ).style.display = "none";
85                 document.getElementById ( "choosecalendar" ).style.visibility = "hidden";
87                 document.getElementById ( "changecalendarlink" ).style.display = "inline";
88                 document.getElementById ( "changecalendarlink" ).style.visibility = "visible";
89             </script>
90              ';
91         }
93         return $this->content;
94     }
95 }