Merge branch 'MDL-60158-master' of git://github.com/andrewnicols/moodle
[moodle.git] / calendar / classes / external / footer_options_exporter.php
CommitLineData
86775ced
SL
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/**
18 * Class for exporting calendar footer view options data.
19 *
20 * @package core_calendar
21 * @copyright 2017 Simey Lameze <simey@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24namespace core_calendar\external;
25
26defined('MOODLE_INTERNAL') || die();
27
28use core\external\exporter;
29use renderer_base;
30use stdClass;
31use moodle_url;
32
33/**
34 * Class for exporting calendar footer view options data.
35 *
36 * @copyright 2017 Simey Lameze
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class footer_options_exporter extends exporter {
40
41 /**
42 * @var \calendar_information $calendar The calendar to be rendered.
43 */
44 protected $calendar;
45
46 /**
47 * @var int $userid The user id.
48 */
49 protected $userid;
50
51 /**
52 * @var string $token The user sha1 token.
53 */
54 protected $token;
55
56 /**
57 * Constructor for month_exporter.
58 *
59 * @param \calendar_information $calendar The calendar being represented
a4af4c96
SL
60 * @param int $userid The user id
61 * @param string $token The user sha1 token.
86775ced
SL
62 */
63 public function __construct(\calendar_information $calendar, $userid, $token) {
64 $this->calendar = $calendar;
65 $this->userid = $userid;
66 $this->token = $token;
67 }
68
69 /**
70 * Get the export calendar button.
71 *
12eef11c 72 * @return \single_button The export calendar button html.
86775ced 73 */
12eef11c 74 protected function get_export_calendar_button() {
86775ced 75 $exportcalendarurl = new moodle_url('/calendar/export.php', ['course' => $this->calendar->course->id]);
12eef11c 76 return new \single_button($exportcalendarurl, get_string('exportcalendar', 'calendar'));
86775ced
SL
77 }
78
79 /**
80 * Get the iCal url.
81 *
82 * @return string The iCal url.
83 */
84 protected function get_ical_url() {
39ad9cf3
AN
85 if ($this->token) {
86 return new moodle_url('/calendar/export_execute.php', ['preset_what' => 'all',
87 'preset_time' => 'recentupcoming', 'userid' => $this->userid, 'authtoken' => $this->token]);
88 }
86775ced
SL
89 }
90
91 /**
92 * Get manage subscription button.
93 *
86775ced
SL
94 * @return string The manage subscription button html.
95 */
1a1e8ab0 96 protected function get_manage_subscriptions_button() {
86775ced
SL
97 if (calendar_user_can_add_event($this->calendar->course)) {
98 $managesubscriptionurl = new moodle_url('/calendar/managesubscriptions.php',
99 ['course' => $this->calendar->course->id]);
1a1e8ab0 100 return new \single_button($managesubscriptionurl,
86775ced
SL
101 get_string('managesubscriptions', 'calendar'));
102 }
103 }
104
105 /**
106 * Get the additional values to inject while exporting.
107 *
108 * @param renderer_base $output The renderer.
109 * @return array Keys are the property names, values are their values.
110 */
111 protected function get_other_values(renderer_base $output) {
112 global $CFG;
113
114 $values = new stdClass();
115
116 if (!empty($CFG->enablecalendarexport)) {
39ad9cf3
AN
117 if ($exportbutton = $this->get_export_calendar_button()) {
118 $values->exportcalendarbutton = $exportbutton->export_for_template($output);
119 }
120 if ($managesubscriptionbutton = $this->get_manage_subscriptions_button()) {
121 $values->managesubscriptionbutton = $managesubscriptionbutton->export_for_template($output);
122 }
bd337603 123 $values->icalurl = $this->get_ical_url()->out(false);
86775ced
SL
124 }
125
126 return (array) $values;
127 }
128
129 /**
130 * Return the list of additional properties.
131 *
132 * @return array
133 */
134 public static function define_other_properties() {
135 return array(
136 'exportcalendarbutton' => [
137 'type' => PARAM_RAW,
39ad9cf3 138 'default' => null,
86775ced
SL
139 ],
140 'managesubscriptionbutton' => [
141 'type' => PARAM_RAW,
39ad9cf3 142 'default' => null,
86775ced
SL
143 ],
144 'icalurl' => [
145 'type' => PARAM_URL,
39ad9cf3 146 'default' => null,
86775ced
SL
147 ],
148 );
149 }
150}