PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/calendar/classes/external/calendar_day_exporter.php

https://github.com/mackensen/moodle
PHP | 282 lines | 163 code | 31 blank | 88 comment | 8 complexity | 95bcf1f31eeabe39bdae6bd6b38229a2 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Contains event class for displaying the day view.
  18. *
  19. * @package core_calendar
  20. * @copyright 2017 Simey Lameze <simey@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. namespace core_calendar\external;
  24. defined('MOODLE_INTERNAL') || die();
  25. use core\external\exporter;
  26. use renderer_base;
  27. use moodle_url;
  28. use \core_calendar\local\event\container;
  29. /**
  30. * Class for displaying the day view.
  31. *
  32. * @package core_calendar
  33. * @copyright 2017 Simey Lameze <simey@moodle.com>
  34. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35. */
  36. class calendar_day_exporter extends exporter {
  37. /**
  38. * @var \calendar_information $calendar The calendar to be rendered.
  39. */
  40. protected $calendar;
  41. /**
  42. * @var moodle_url $url The URL for the day view page.
  43. */
  44. protected $url;
  45. /**
  46. * Constructor for day exporter.
  47. *
  48. * @param \calendar_information $calendar The calendar being represented.
  49. * @param array $related The related information
  50. */
  51. public function __construct(\calendar_information $calendar, $related) {
  52. $this->calendar = $calendar;
  53. parent::__construct([], $related);
  54. }
  55. /**
  56. * Return the list of additional properties.
  57. *
  58. * @return array
  59. */
  60. protected static function define_other_properties() {
  61. return [
  62. 'events' => [
  63. 'type' => calendar_event_exporter::read_properties_definition(),
  64. 'multiple' => true,
  65. ],
  66. 'defaulteventcontext' => [
  67. 'type' => PARAM_INT,
  68. 'default' => 0,
  69. ],
  70. 'filter_selector' => [
  71. 'type' => PARAM_RAW,
  72. ],
  73. 'courseid' => [
  74. 'type' => PARAM_INT,
  75. ],
  76. 'categoryid' => [
  77. 'type' => PARAM_INT,
  78. 'optional' => true,
  79. 'default' => 0,
  80. ],
  81. 'neweventtimestamp' => [
  82. 'type' => PARAM_INT,
  83. ],
  84. 'date' => [
  85. 'type' => date_exporter::read_properties_definition(),
  86. ],
  87. 'periodname' => [
  88. // Note: We must use RAW here because the calendar type returns the formatted month name based on a
  89. // calendar format.
  90. 'type' => PARAM_RAW,
  91. ],
  92. 'previousperiod' => [
  93. 'type' => date_exporter::read_properties_definition(),
  94. ],
  95. 'previousperiodlink' => [
  96. 'type' => PARAM_URL,
  97. ],
  98. 'previousperiodname' => [
  99. // Note: We must use RAW here because the calendar type returns the formatted month name based on a
  100. // calendar format.
  101. 'type' => PARAM_RAW,
  102. ],
  103. 'nextperiod' => [
  104. 'type' => date_exporter::read_properties_definition(),
  105. ],
  106. 'nextperiodname' => [
  107. // Note: We must use RAW here because the calendar type returns the formatted month name based on a
  108. // calendar format.
  109. 'type' => PARAM_RAW,
  110. ],
  111. 'nextperiodlink' => [
  112. 'type' => PARAM_URL,
  113. ],
  114. 'larrow' => [
  115. // The left arrow defined by the theme.
  116. 'type' => PARAM_RAW,
  117. ],
  118. 'rarrow' => [
  119. // The right arrow defined by the theme.
  120. 'type' => PARAM_RAW,
  121. ],
  122. ];
  123. }
  124. /**
  125. * Get the additional values to inject while exporting.
  126. *
  127. * @param renderer_base $output The renderer.
  128. * @return array Keys are the property names, values are their values.
  129. */
  130. protected function get_other_values(renderer_base $output) {
  131. $timestamp = $this->calendar->time;
  132. $cache = $this->related['cache'];
  133. $url = new moodle_url('/calendar/view.php', [
  134. 'view' => 'day',
  135. 'time' => $timestamp,
  136. ]);
  137. if ($this->calendar->course && SITEID !== $this->calendar->course->id) {
  138. $url->param('course', $this->calendar->course->id);
  139. } else if ($this->calendar->categoryid) {
  140. $url->param('category', $this->calendar->categoryid);
  141. }
  142. $this->url = $url;
  143. $return['events'] = array_map(function($event) use ($cache, $output, $url) {
  144. $context = $cache->get_context($event);
  145. $course = $cache->get_course($event);
  146. $moduleinstance = $cache->get_module_instance($event);
  147. $exporter = new calendar_event_exporter($event, [
  148. 'context' => $context,
  149. 'course' => $course,
  150. 'moduleinstance' => $moduleinstance,
  151. 'daylink' => $url,
  152. 'type' => $this->related['type'],
  153. 'today' => $this->calendar->time,
  154. ]);
  155. $data = $exporter->export($output);
  156. // We need to override default formatted time because it differs from day view.
  157. // Formatted time for day view adds a link to the day view.
  158. $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
  159. $data->formattedtime = calendar_format_event_time($legacyevent, time(), null);
  160. return $data;
  161. }, $this->related['events']);
  162. if ($context = $this->get_default_add_context()) {
  163. $return['defaulteventcontext'] = $context->id;
  164. }
  165. if ($this->calendar->categoryid) {
  166. $return['categoryid'] = $this->calendar->categoryid;
  167. }
  168. $return['filter_selector'] = $this->get_course_filter_selector($output);
  169. $return['courseid'] = $this->calendar->courseid;
  170. $previousperiod = $this->get_previous_day_data();
  171. $nextperiod = $this->get_next_day_data();
  172. $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
  173. $nextperiodlink = new moodle_url($this->url);
  174. $nextperiodlink->param('time', $nextperiod[0]);
  175. $previousperiodlink = new moodle_url($this->url);
  176. $previousperiodlink->param('time', $previousperiod[0]);
  177. $days = calendar_get_days();
  178. $return['date'] = (new date_exporter($date))->export($output);
  179. $return['periodname'] = userdate($this->calendar->time, get_string('strftimedaydate'));
  180. $return['previousperiod'] = (new date_exporter($previousperiod))->export($output);
  181. $return['previousperiodname'] = $days[$previousperiod['wday']]['fullname'];
  182. $return['previousperiodlink'] = $previousperiodlink->out(false);
  183. $return['nextperiod'] = (new date_exporter($nextperiod))->export($output);
  184. $return['nextperiodname'] = $days[$nextperiod['wday']]['fullname'];
  185. $return['nextperiodlink'] = $nextperiodlink->out(false);
  186. $return['larrow'] = $output->larrow();
  187. $return['rarrow'] = $output->rarrow();
  188. // Need to account for user's timezone.
  189. $usernow = usergetdate(time());
  190. $today = new \DateTimeImmutable();
  191. $neweventtimestamp = $today->setTimestamp($date[0])->setTime(
  192. $usernow['hours'],
  193. $usernow['minutes'],
  194. $usernow['seconds']
  195. );
  196. $return['neweventtimestamp'] = $neweventtimestamp->getTimestamp();
  197. return $return;
  198. }
  199. /**
  200. * Get the default context for use when adding a new event.
  201. *
  202. * @return null|\context
  203. */
  204. protected function get_default_add_context() {
  205. if (calendar_user_can_add_event($this->calendar->course)) {
  206. return \context_course::instance($this->calendar->course->id);
  207. }
  208. return null;
  209. }
  210. /**
  211. * Get the course filter selector.
  212. *
  213. * @param renderer_base $output
  214. * @return string The html code for the course filter selector.
  215. */
  216. protected function get_course_filter_selector(renderer_base $output) {
  217. return $output->course_filter_selector($this->url, '', $this->calendar->course->id);
  218. }
  219. /**
  220. * Returns a list of objects that are related.
  221. *
  222. * @return array
  223. */
  224. protected static function define_related() {
  225. return [
  226. 'events' => '\core_calendar\local\event\entities\event_interface[]',
  227. 'cache' => '\core_calendar\external\events_related_objects_cache',
  228. 'type' => '\core_calendar\type_base',
  229. ];
  230. }
  231. /**
  232. * Get the previous day timestamp.
  233. *
  234. * @return int The previous day timestamp.
  235. */
  236. protected function get_previous_day_data() {
  237. $type = $this->related['type'];
  238. $time = $type->get_prev_day($this->calendar->time);
  239. return $type->timestamp_to_date_array($time);
  240. }
  241. /**
  242. * Get the next day timestamp.
  243. *
  244. * @return int The next day timestamp.
  245. */
  246. protected function get_next_day_data() {
  247. $type = $this->related['type'];
  248. $time = $type->get_next_day($this->calendar->time);
  249. return $type->timestamp_to_date_array($time);
  250. }
  251. }