From 522b84be04a7551fa7de64a0db460268dc5c4a2a Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Thu, 5 Oct 2017 15:18:19 +0800 Subject: [PATCH] MDL-60092 block_calendar_upcoming: convert to template --- .../block_calendar_upcoming.php | 111 ++++-------------- calendar/lib.php | 11 +- 2 files changed, 28 insertions(+), 94 deletions(-) diff --git a/blocks/calendar_upcoming/block_calendar_upcoming.php b/blocks/calendar_upcoming/block_calendar_upcoming.php index 858a0e42baf..825868c11a2 100644 --- a/blocks/calendar_upcoming/block_calendar_upcoming.php +++ b/blocks/calendar_upcoming/block_calendar_upcoming.php @@ -46,106 +46,35 @@ class block_calendar_upcoming extends block_base { $this->content = new stdClass; $this->content->text = ''; - $filtercourse = array(); - if (empty($this->instance)) { // Overrides: use no course at all. - $courseshown = false; - $this->content->footer = ''; - + $renderer = $this->page->get_renderer('core_calendar'); + $courseid = $this->page->course->id; + $issite = ($courseid == 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. + $course = get_site(); + $courses = calendar_get_default_courses(); } else { - $courseshown = $this->page->course->id; - $this->content->footer = '
'. - get_string('gotocalendar', 'calendar').'...
'; - $context = context_course::instance($courseshown); - if (has_any_capability(array('moodle/calendar:manageentries', 'moodle/calendar:manageownentries'), $context)) { - $this->content->footer .= '
'. - get_string('newevent', 'calendar').'...
'; - } - 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 = 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); - } + // Forcibly filter events to include only those from the particular course we are in. + $course = $this->page->course; + $courses = [$course->id => $course]; } + $calendar = new calendar_information(0, 0, 0, time()); + $calendar->set_sources($course, $courses); - list($courses, $group, $user) = calendar_set_filters($filtercourse); - - $defaultlookahead = CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD; - if (isset($CFG->calendar_lookahead)) { - $defaultlookahead = intval($CFG->calendar_lookahead); - } - $lookahead = get_user_preferences('calendar_lookahead', $defaultlookahead); - - $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)) { - $link = 'view.php?view=day&course='.$courseshown.'&'; - $showcourselink = ($this->page->course->id == SITEID); - $this->content->text = self::get_upcoming_content($events, $link, $showcourselink); - } + list($data, $template) = calendar_get_view($calendar, 'upcoming_mini'); + $this->content->text .= $renderer->render_from_template($template, $data); if (empty($this->content->text)) { $this->content->text = '
'. get_string('noupcomingevents', 'calendar').'
'; } - return $this->content; - } - - /** - * Get the upcoming event block content. - * - * @param array $events list of events - * @param \moodle_url|string $linkhref link to event referer - * @param boolean $showcourselink whether links to courses should be shown - * @return string|null $content html block content - */ - public static function get_upcoming_content($events, $linkhref = null, $showcourselink = false) { - $content = ''; - $lines = count($events); - - if (!$lines) { - return $content; - } - - for ($i = 0; $i < $lines; ++$i) { - if (!isset($events[$i]->time)) { - continue; - } - $events[$i] = calendar_add_event_metadata($events[$i]); - $content .= '
' . $events[$i]->icon . ''; - if (!empty($events[$i]->referer)) { - // That's an activity event, so let's provide the hyperlink. - $content .= $events[$i]->referer; - } else { - if (!empty($linkhref)) { - $href = calendar_get_link_href(new \moodle_url(CALENDAR_URL . $linkhref), 0, 0, 0, - $events[$i]->timestart); - $href->set_anchor('event_' . $events[$i]->id); - $content .= \html_writer::link($href, $events[$i]->name); - } else { - $content .= $events[$i]->name; - } - } - $events[$i]->time = str_replace('»', '
»', $events[$i]->time); - if ($showcourselink && !empty($events[$i]->courselink)) { - $content .= \html_writer::div($events[$i]->courselink, 'course'); - } - $content .= '
' . $events[$i]->time . '
'; - if ($i < $lines - 1) { - $content .= '
'; - } - } + $this->content->footer = '
+ '. + get_string('gotocalendar', 'calendar').'...
'; - return $content; + return $this->content; } } diff --git a/calendar/lib.php b/calendar/lib.php index 48754d18399..05d6a14418f 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -3147,7 +3147,7 @@ function calendar_get_view(\calendar_information $calendar, $view, $includenavig if ($view === 'day') { $tstart = $type->convert_to_timestamp($date['year'], $date['mon'], $date['mday']); $tend = $tstart + DAYSECS - 1; - } else if ($view === 'upcoming') { + } else if ($view === 'upcoming' || $view === 'upcoming_mini') { if (isset($CFG->calendar_lookahead)) { $defaultlookahead = intval($CFG->calendar_lookahead); } else { @@ -3234,10 +3234,15 @@ function calendar_get_view(\calendar_information $calendar, $view, $includenavig $day = new \core_calendar\external\calendar_day_exporter($calendar, $related); $data = $day->export($renderer); $template = 'core_calendar/calendar_day'; - } else if ($view == "upcoming") { + } else if ($view == "upcoming" || $view == "upcoming_mini") { $upcoming = new \core_calendar\external\calendar_upcoming_exporter($calendar, $related); $data = $upcoming->export($renderer); - $template = 'core_calendar/calendar_upcoming'; + + if ($view == "upcoming") { + $template = 'core_calendar/calendar_upcoming'; + } else if ($view == "upcoming_mini") { + $template = 'core_calendar/upcoming_mini'; + } } return [$data, $template]; -- 2.43.0