Commit 64ff737a authored by Andrew Nicols's avatar Andrew Nicols Committed by Simey Lameze
Browse files

MDL-59388 calendar: Switch monthly view to use templates

parent c075a3de
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains event class for displaying a calendar event.
*
* @package core_calendar
* @copyright 2017 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_calendar\external;
defined('MOODLE_INTERNAL') || die();
use \core\external\exporter;
use \core_calendar\local\event\entities\event_interface;
use \core_calendar\local\event\entities\action_event_interface;
use \core_course\external\course_summary_exporter;
use \renderer_base;
/**
* Class for displaying a calendar event.
*
* @package core_calendar
* @copyright 2017 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class calendar_event_exporter extends event_exporter_base {
/**
* Return the list of additional properties.
*
* @return array
*/
protected static function define_other_properties() {
return [
'url' => ['type' => PARAM_URL],
'icon' => [
'type' => event_icon_exporter::read_properties_definition(),
],
'course' => [
'type' => course_summary_exporter::read_properties_definition(),
'optional' => true,
]
];
}
/**
* Get the additional values to inject while exporting.
*
* @param renderer_base $output The renderer.
* @return array Keys are the property names, values are their values.
*/
protected function get_other_values(renderer_base $output) {
$values = parent::get_other_values($output);
$eventid = $this->event->get_id();
$url = new \moodle_url($this->related['daylink'], [], "event_{$eventid}");
$values['url'] = $url->out(false);
return $values;
}
/**
* Returns a list of objects that are related.
*
* @return array
*/
protected static function define_related() {
$related = parent::define_related();
$related['daylink'] = \moodle_url::class;
return $related;
}
}
<?php
namespace core_calendar\external;
use core\external\exporter;
use renderer_base;
use moodle_url;
class day_exporter extends exporter {
protected static function define_properties() {
// These are the default properties as returned by getuserdate()
// but without the formatted month and week names.
return [
'seconds' => [
'type' => PARAM_INT,
],
'minutes' => [
'type' => PARAM_INT,
],
'hours' => [
'type' => PARAM_INT,
],
'mday' => [
'type' => PARAM_INT,
],
'wday' => [
'type' => PARAM_INT,
],
'year' => [
'type' => PARAM_INT,
],
'yday' => [
'type' => PARAM_INT,
],
];
}
protected static function define_other_properties() {
return [
'timestamp' => [
'type' => PARAM_INT,
],
'istoday' => [
'type' => PARAM_BOOL,
'default' => false,
],
'isweekend' => [
'type' => PARAM_BOOL,
'default' => false,
],
'viewdaylink' => [
'type' => PARAM_URL,
'optional' => true,
],
'events' => [
'type' => event_exporter::read_properties_definition(),
'multiple' => true,
],
'viewdaylink' => [
'type' => PARAM_URL,
],
//'viewdaylink' => $this->viewdaylink->out(false),
//'createeventlink' => $this->createeventlink,
//'viewdaylinktitle' => $this->get_title($renderer),
//'events' => $this->get_events($renderer),
//'hasevents' => !empty($this->events),
//'eventtypes' => array_unique($this->eventtypes),
//'eventcount' => count($this->events),
//'durationevents' => array_unique($this->durationevents),
];
}
protected function get_other_values(renderer_base $output) {
//$events = new events_exporter($this->related['events'], $this->related);
$return = [
'timestamp' => $this->data[0],
];
$url = new moodle_url('/calendar/view.php', [
'view' => 'day',
'time' => $this->data[0],
]);
$return['viewdaylink'] = $url->out(false);
$cache = $this->related['cache'];
$return['events'] = array_map(function($event) use ($cache, $output, $url) {
$context = $cache->get_context($event);
$course = $cache->get_course($event);
$exporter = new calendar_event_exporter($event, [
'context' => $context,
'course' => $course,
'daylink' => $url,
]);
return $exporter->export($output);
}, $this->related['events']);
return $return;
}
protected static function define_related() {
return [
'events' => '\core_calendar\local\event\entities\event_interface[]',
'cache' => '\core_calendar\external\events_related_objects_cache',
'type' => '\core_calendar\type_base',
];
}
}
<?php
namespace core_calendar\external;
use core\external\exporter;
use renderer_base;
use moodle_url;
class day_name_exporter extends exporter {
/**
* @var int $dayno The day number.
*/
protected $dayno;
/**
* @var string $shortname The formatted short name of the day.
*/
protected $shortname;
/**
* @var string $fullname The formatted full name of the day.
*/
protected $fullname;
public function __construct($dayno, $names) {
$data = $names + ['dayno' => $dayno];
parent::__construct($data, []);
}
protected static function define_properties() {
return [
'dayno' => [
'type' => PARAM_INT,
],
'shortname' => [
// Note: The calendar type class has already formatted the names.
'type' => PARAM_RAW,
],
'fullname' => [
// Note: The calendar type class has already formatted the names.
'type' => PARAM_RAW,
],
];
}
}
<?php
namespace core_calendar\external;
use core\external\exporter;
use renderer_base;
use moodle_url;
class month_exporter extends exporter {
/**
* @var \calendar_information $calendar The calendar to be rendered.
*/
protected $calendar;
/**
* @var int $firstdayofweek The first day of the week.
*/
protected $firstdayofweek;
/**
* @var moodle_url $url The URL for the events page.
*/
protected $url;
/**
* Constructor for month_exporter.
*
* @param \calendar_information $calendar The calendar being represented
* @param \core_calendar\type_base $type The calendar type (e.g. Gregorian)
* @param array $related The related information
*/
public function __construct(\calendar_information $calendar, \core_calendar\type_base $type, $related) {
$this->calendar = $calendar;
$this->firstdayofweek = $type->get_starting_weekday();
$this->url = new moodle_url('/calendar/view.php', [
'view' => 'month',
'time' => $calendar->time,
]);
if ($this->calendar->courseid) {
$this->url->param('course', $this->calendar->courseid);
}
$related['type'] = $type;
parent::__construct([], $related);
}
protected static function define_other_properties() {
return [
'courseid' => [
'type' => PARAM_INT,
],
'filter_selector' => [
'type' => PARAM_RAW,
],
'navigation' => [
'type' => PARAM_RAW,
],
'weeks' => [
'type' => week_exporter::read_properties_definition(),
'multiple' => true,
],
'daynames' => [
'type' => day_name_exporter::read_properties_definition(),
'multiple' => true,
],
'view' => [
'type' => PARAM_ALPHA,
],
'previousperiod' => [
'type' => PARAM_INT,
],
'nextperiod' => [
'type' => PARAM_INT,
],
];
}
protected function get_other_values(renderer_base $output) {
return [
'courseid' => $this->calendar->courseid,
'view' => 'month',
'previousperiod' => $this->get_previous_month_timestamp(),
'nextperiod' => $this->get_next_month_timestamp(),
'filter_selector' => $this->get_course_filter_selector($output),
'navigation' => $this->get_navigation($output),
'weeks' => $this->get_weeks($output),
'daynames' => $this->get_day_names($output),
];
}
/**
* Get the course filter selector.
* TODO Convert to new exporter?
*
* @param renderer_base $output
* return string
*/
protected function get_course_filter_selector(renderer_base $output) {
$content = '';
$content .= $output->course_filter_selector($this->url, get_string('detailedmonthviewfor', 'calendar'));
if (calendar_user_can_add_event($this->calendar->course)) {
$content .= $output->add_event_button($this->calendar->courseid, 0, 0, 0, $this->calendar->time);
}
return $content;
}
/**
* Get the course filter selector.
* TODO Convert to new exporter?
*
* @param renderer_base $output
* return string
*/
protected function get_navigation(renderer_base $output) {
return calendar_top_controls('month', [
'id' => $this->calendar->courseid,
'time' => $this->calendar->time,
]);
}
/**
* Get the list of day names for display, re-ordered from the first day
* of the week.
*
* @param renderer_base $output
* @return day_name_exporter[]
*/
protected function get_day_names(renderer_base $output) {
$weekdays = $this->related['type']->get_weekdays();
$daysinweek = count($weekdays);
$daynames = [];
for ($i = 0; $i < $daysinweek; $i++) {
// Bump the currentdayno and ensure it loops.
$dayno = ($i + $this->firstdayofweek + $daysinweek) % $daysinweek;
$dayname = new day_name_exporter($dayno, $weekdays[$dayno]);
$daynames[] = $dayname->export($output);
}
return $daynames;
}
/**
* Get the list of week days, ordered into weeks and padded according
* to the value of the first day of the week.
*
* @param renderer_base $output
* @return array
*/
protected function get_weeks(renderer_base $output) {
$weeks = [];
$alldays = $this->get_days();
$daysinweek = count($this->related['type']->get_weekdays());
// Calculate which day number is the first, and last day of the week.
$firstdayofweek = $this->firstdayofweek;
$lastdayofweek = ($firstdayofweek + $daysinweek - 1) % $daysinweek;
// The first week is special as it may have padding at the beginning.
$day = reset($alldays);
$firstdayno = $day['wday'];
$prepadding = ($firstdayno + $daysinweek - 1) % $daysinweek;
$daysinfirstweek = $daysinweek - $prepadding;
$days = array_slice($alldays, 0, $daysinfirstweek);
$week = new week_exporter($days, $prepadding, ($daysinweek - count($days) - $prepadding), $this->related);
$weeks[] = $week->export($output);
// Now chunk up the remaining day. and turn them into weeks.
$daychunks = array_chunk(array_slice($alldays, $daysinfirstweek), $daysinweek);
foreach ($daychunks as $days) {
$week = new week_exporter($days, 0, ($daysinweek - count($days)), $this->related);
$weeks[] = $week->export($output);
}
return $weeks;
}
/**
* Get the list of days with the matching date array.
*
* @return array
*/
protected function get_days() {
$date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
$monthdays = $this->related['type']->get_num_days_in_month($date['year'], $date['mon']);
$days = [];
for ($dayno = 1; $dayno <= $monthdays; $dayno++) {
// Get the gregorian representation of the day.
$timestamp = $this->related['type']->convert_to_timestamp($date['year'], $date['mon'], $dayno);
$days[] = $this->related['type']->timestamp_to_date_array($timestamp);
}
return $days;
}
protected static function define_related() {
return [
'events' => '\core_calendar\local\event\entities\event_interface[]',
'cache' => '\core_calendar\external\events_related_objects_cache',
'type' => '\core_calendar\type_base',
];
}
protected function get_previous_month_timestamp() {
$date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
$month = calendar_sub_month($date['mon'], $date['year']);
$monthtime = $this->related['type']->convert_to_gregorian($month[1], $month[0], 1);
return make_timestamp($monthtime['year'], $monthtime['month'], $monthtime['day'], $monthtime['hour'], $monthtime['minute']);
}
protected function get_next_month_timestamp() {
$date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
$month = calendar_sub_month($date['mon'], $date['year']);
$monthtime = $this->related['type']->convert_to_gregorian($month[1], $month[0], 1);
return make_timestamp($monthtime['year'], $monthtime['month'], $monthtime['day'], $monthtime['hour'], $monthtime['minute']);
}
}
<?php
namespace core_calendar\external;
use core\external\exporter;
use renderer_base;
use moodle_url;
class week_exporter extends exporter {
/**
* @var array $days An array of day_exporter objects.
*/
protected $days = [];
/**
* @var int $prepadding The number of pre-padding days at the start of
* the week.
*/
protected $prepadding = 0;
/**
* @var int $postpadding The number of post-padding days at the start of
* the week.
*/
protected $postpadding = 0;
public function __construct($days, $prepadding, $postpadding, $related) {
$this->days = $days;
$this->prepadding = $prepadding;
$this->postpadding = $postpadding;
parent::__construct([], $related);
}
protected static function define_other_properties() {
return [
'prepadding' => [
'type' => PARAM_INT,
'multiple' => true,
],
'postpadding' => [
'type' => PARAM_INT,
'multiple' => true,
],
'days' => [
'type' => day_exporter::read_properties_definition(),
'multiple' => true,
],
];
}
protected function get_other_values(renderer_base $output) {
$return = [
'prepadding' => [],
'postpadding' => [],
'days' => [],
];
for ($i = 0; $i < $this->prepadding; $i++) {
$return['prepadding'][] = $i;
}
for ($i = 0; $i < $this->postpadding; $i++) {
$return['postpadding'][] = $i;
}
$return['days'] = [];
foreach ($this->days as $daydata) {
$events = [];
foreach ($this->related['events'] as $event) {
$times = $event->get_times();
$starttime = $times->get_start_time()->getTimestamp();
$startdate = $this->related['type']->timestamp_to_date_array($starttime);
$endtime = $times->get_end_time()->getTimestamp();
$enddate = $this->related['type']->timestamp_to_date_array($endtime);
if ((($startdate['year'] * 366) + $startdate['yday']) > ($daydata['year'] * 366) + $daydata['yday']) {
// Starts after today.
continue;
}
if ((($enddate['year'] * 366) + $enddate['yday']) < ($daydata['year'] * 366) + $daydata['yday']) {
// Ends before today.
continue;
}
$events[] = $event;
}
$day = new day_exporter($daydata, [
'events' => $events,
'cache' => $this->related['cache'],
'type' => $this->related['type'],
]);
$return['days'][] = $day->export($output);
}
return $return;
}
protected static function define_related() {
return [
'events' => '\core_calendar\local\event\entities\event_interface[]',
'cache' => '\core_calendar\external\events_related_objects_cache',
'type' => '\core_calendar\type_base',
];
}
}
......@@ -3422,6 +3422,111 @@ function calendar_get_legacy_events($tstart, $tend, $users, $groups, $courses, $
}, []);
}
/**
* Get theh calendar view output.
*
* @param \calendar_information $calendar The calendar being represented
* @param string $view The type of calendar to have displayed
* @return array[array, string]
*/
function calendar_get_view(\calendar_information $calendar, $view) {
global $PAGE, $DB, $OUTPUT;
$renderer = $PAGE->get_renderer('core_calendar');
$type = \core_calendar\type_factory::get_calendar_instance();
// Calculate the bounds of the month.
$date = $type->timestamp_to_date_array($calendar->time);
$tstart = $type->convert_to_timestamp($date['year'], $date['mon'], 1);