MDL-63817 block_timeline: Persist page limits when sorting by dates
[moodle.git] / blocks / timeline / classes / output / main.php
CommitLineData
1e44de35
RW
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 containing data for timeline block.
19 *
20 * @package block_timeline
21 * @copyright 2018 Ryan Wyllie <ryan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24namespace block_timeline\output;
25defined('MOODLE_INTERNAL') || die();
26
27use renderable;
28use renderer_base;
29use templatable;
30use core_course\external\course_summary_exporter;
31
32require_once($CFG->dirroot . '/course/lib.php');
9e95193e 33require_once($CFG->dirroot . '/blocks/timeline/lib.php');
1e44de35
RW
34require_once($CFG->libdir . '/completionlib.php');
35
36/**
37 * Class containing data for timeline block.
38 *
39 * @copyright 2018 Ryan Wyllie <ryan@moodle.com>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class main implements renderable, templatable {
43
44 /** Number of courses to load per page */
45 const COURSES_PER_PAGE = 2;
46
9e95193e
P
47 /**
48 * @var string The current filter preference
49 */
50 public $filter;
51
52 /**
53 * @var string The current sort/order preference
54 */
55 public $order;
56
2044a539
P
57 /**
58 * @var string The current limit preference
59 */
60 public $limit;
61
9e95193e
P
62 /**
63 * main constructor.
64 *
65 * @param string $order Constant sort value from ../timeline/lib.php
2044a539
P
66 * @param string $filter Constant filter value from ../timeline/lib.php
67 * @param string $limit Constant limit value from ../timeline/lib.php
9e95193e 68 */
2044a539 69 public function __construct($order, $filter, $limit) {
9e95193e
P
70 $this->order = $order ? $order : BLOCK_TIMELINE_SORT_BY_DATES;
71 $this->filter = $filter ? $filter : BLOCK_TIMELINE_FILTER_BY_7_DAYS;
2044a539 72 $this->limit = $limit ? $limit : BLOCK_TIMELINE_LIMIT_DEFAULT;
9e95193e
P
73 }
74
75 /**
76 * Test the available filters with the current user preference and return an array with
77 * bool flags corresponding to which is active
78 *
79 * @return array
80 */
81 protected function get_filters_as_booleans() {
82 $filters = [
83 BLOCK_TIMELINE_FILTER_BY_NONE => false,
84 BLOCK_TIMELINE_FILTER_BY_OVERDUE => false,
85 BLOCK_TIMELINE_FILTER_BY_7_DAYS => false,
86 BLOCK_TIMELINE_FILTER_BY_30_DAYS => false,
87 BLOCK_TIMELINE_FILTER_BY_3_MONTHS => false,
88 BLOCK_TIMELINE_FILTER_BY_6_MONTHS => false
89 ];
90
91 // Set the selected filter to true.
92 $filters[$this->filter] = true;
93
94 return $filters;
95 }
96
97 /**
98 * Get the offset/limit values corresponding to $this->filter
99 * which are used to send through to the context as default values
100 *
101 * @return array
102 */
103 private function get_filter_offsets() {
104
105 $limit = false;
106 if (in_array($this->filter, [BLOCK_TIMELINE_FILTER_BY_NONE, BLOCK_TIMELINE_FILTER_BY_OVERDUE])) {
107 $offset = -14;
108 if ($this->filter == BLOCK_TIMELINE_FILTER_BY_OVERDUE) {
109 $limit = 0;
110 }
111 } else {
112 $offset = 0;
113 $limit = 7;
114
115 switch($this->filter) {
116 case BLOCK_TIMELINE_FILTER_BY_30_DAYS:
117 $limit = 30;
118 break;
119 case BLOCK_TIMELINE_FILTER_BY_3_MONTHS:
120 $limit = 90;
121 break;
122 case BLOCK_TIMELINE_FILTER_BY_6_MONTHS:
123 $limit = 180;
124 break;
125 }
126 }
127
128 return [
129 'daysoffset' => $offset,
130 'dayslimit' => $limit
131 ];
132 }
133
1e44de35
RW
134 /**
135 * Export this data so it can be used as the context for a mustache template.
136 *
137 * @param \renderer_base $output
138 * @return stdClass
139 */
140 public function export_for_template(renderer_base $output) {
141
142 $nocoursesurl = $output->image_url('courses', 'block_timeline')->out();
143 $noeventsurl = $output->image_url('activities', 'block_timeline')->out();
144
145 $requiredproperties = course_summary_exporter::define_properties();
146 $fields = join(',', array_keys($requiredproperties));
147 $courses = course_get_enrolled_courses_for_logged_in_user(0, 0, null, $fields);
148 list($inprogresscourses, $processedcount) = course_filter_courses_by_timeline_classification(
149 $courses,
150 COURSE_TIMELINE_INPROGRESS,
151 self::COURSES_PER_PAGE
152 );
153 $formattedcourses = array_map(function($course) use ($output) {
154 \context_helper::preload_from_record($course);
155 $context = \context_course::instance($course->id);
156 $exporter = new course_summary_exporter($course, ['context' => $context]);
157 return $exporter->export($output);
158 }, $inprogresscourses);
159
9e95193e
P
160 $filters = $this->get_filters_as_booleans();
161 $offsets = $this->get_filter_offsets();
162 $contextvariables = [
1e44de35
RW
163 'midnight' => usergetmidnight(time()),
164 'coursepages' => [$formattedcourses],
165 'urls' => [
166 'nocourses' => $nocoursesurl,
167 'noevents' => $noeventsurl
9e95193e
P
168 ],
169 'sorttimelinedates' => $this->order == BLOCK_TIMELINE_SORT_BY_DATES,
170 'sorttimelinecourses' => $this->order == BLOCK_TIMELINE_SORT_BY_COURSES,
171 'selectedfilter' => $this->filter,
172 'hasdaysoffset' => true,
173 'hasdayslimit' => $offsets['dayslimit'] !== false ,
174 'nodayslimit' => $offsets['dayslimit'] === false ,
2044a539 175 'limit' => $this->limit
1e44de35 176 ];
9e95193e 177 return array_merge($contextvariables, $filters, $offsets);
1e44de35
RW
178 }
179}