MDL-63352 block_timeline: Persist user preference on timeline
[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
57 /**
58 * main constructor.
59 *
60 * @param string $order Constant sort value from ../timeline/lib.php
61 * @param string $filter Constant sort value from ../timeline/lib.php
62 */
63 public function __construct($order, $filter) {
64 $this->order = $order ? $order : BLOCK_TIMELINE_SORT_BY_DATES;
65 $this->filter = $filter ? $filter : BLOCK_TIMELINE_FILTER_BY_7_DAYS;
66 }
67
68 /**
69 * Test the available filters with the current user preference and return an array with
70 * bool flags corresponding to which is active
71 *
72 * @return array
73 */
74 protected function get_filters_as_booleans() {
75 $filters = [
76 BLOCK_TIMELINE_FILTER_BY_NONE => false,
77 BLOCK_TIMELINE_FILTER_BY_OVERDUE => false,
78 BLOCK_TIMELINE_FILTER_BY_7_DAYS => false,
79 BLOCK_TIMELINE_FILTER_BY_30_DAYS => false,
80 BLOCK_TIMELINE_FILTER_BY_3_MONTHS => false,
81 BLOCK_TIMELINE_FILTER_BY_6_MONTHS => false
82 ];
83
84 // Set the selected filter to true.
85 $filters[$this->filter] = true;
86
87 return $filters;
88 }
89
90 /**
91 * Get the offset/limit values corresponding to $this->filter
92 * which are used to send through to the context as default values
93 *
94 * @return array
95 */
96 private function get_filter_offsets() {
97
98 $limit = false;
99 if (in_array($this->filter, [BLOCK_TIMELINE_FILTER_BY_NONE, BLOCK_TIMELINE_FILTER_BY_OVERDUE])) {
100 $offset = -14;
101 if ($this->filter == BLOCK_TIMELINE_FILTER_BY_OVERDUE) {
102 $limit = 0;
103 }
104 } else {
105 $offset = 0;
106 $limit = 7;
107
108 switch($this->filter) {
109 case BLOCK_TIMELINE_FILTER_BY_30_DAYS:
110 $limit = 30;
111 break;
112 case BLOCK_TIMELINE_FILTER_BY_3_MONTHS:
113 $limit = 90;
114 break;
115 case BLOCK_TIMELINE_FILTER_BY_6_MONTHS:
116 $limit = 180;
117 break;
118 }
119 }
120
121 return [
122 'daysoffset' => $offset,
123 'dayslimit' => $limit
124 ];
125 }
126
1e44de35
RW
127 /**
128 * Export this data so it can be used as the context for a mustache template.
129 *
130 * @param \renderer_base $output
131 * @return stdClass
132 */
133 public function export_for_template(renderer_base $output) {
134
135 $nocoursesurl = $output->image_url('courses', 'block_timeline')->out();
136 $noeventsurl = $output->image_url('activities', 'block_timeline')->out();
137
138 $requiredproperties = course_summary_exporter::define_properties();
139 $fields = join(',', array_keys($requiredproperties));
140 $courses = course_get_enrolled_courses_for_logged_in_user(0, 0, null, $fields);
141 list($inprogresscourses, $processedcount) = course_filter_courses_by_timeline_classification(
142 $courses,
143 COURSE_TIMELINE_INPROGRESS,
144 self::COURSES_PER_PAGE
145 );
146 $formattedcourses = array_map(function($course) use ($output) {
147 \context_helper::preload_from_record($course);
148 $context = \context_course::instance($course->id);
149 $exporter = new course_summary_exporter($course, ['context' => $context]);
150 return $exporter->export($output);
151 }, $inprogresscourses);
152
9e95193e
P
153 $filters = $this->get_filters_as_booleans();
154 $offsets = $this->get_filter_offsets();
155 $contextvariables = [
1e44de35
RW
156 'midnight' => usergetmidnight(time()),
157 'coursepages' => [$formattedcourses],
158 'urls' => [
159 'nocourses' => $nocoursesurl,
160 'noevents' => $noeventsurl
9e95193e
P
161 ],
162 'sorttimelinedates' => $this->order == BLOCK_TIMELINE_SORT_BY_DATES,
163 'sorttimelinecourses' => $this->order == BLOCK_TIMELINE_SORT_BY_COURSES,
164 'selectedfilter' => $this->filter,
165 'hasdaysoffset' => true,
166 'hasdayslimit' => $offsets['dayslimit'] !== false ,
167 'nodayslimit' => $offsets['dayslimit'] === false ,
1e44de35 168 ];
9e95193e 169 return array_merge($contextvariables, $filters, $offsets);
1e44de35
RW
170 }
171}