MDL-64901 block_myoverview: admin settings to control available layouts
[moodle.git] / blocks / myoverview / classes / output / main.php
CommitLineData
9ac2e865
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 my overview block.
19 *
20 * @package block_myoverview
21 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24namespace block_myoverview\output;
25defined('MOODLE_INTERNAL') || die();
26
27use renderable;
28use renderer_base;
29use templatable;
5f59a4c0 30use stdClass;
3f0c421b 31
3cfff885 32require_once($CFG->dirroot . '/blocks/myoverview/lib.php');
c5515a34 33
9ac2e865
RW
34/**
35 * Class containing data for my overview block.
36 *
e4b4b9e7 37 * @copyright 2018 Bas Brands <bas@moodle.com>
9ac2e865
RW
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class main implements renderable, templatable {
3cfff885
BB
41
42 /**
5f59a4c0 43 * Store the grouping preference.
3cfff885
BB
44 *
45 * @var string String matching the grouping constants defined in myoverview/lib.php
46 */
47 private $grouping;
48
49 /**
5f59a4c0 50 * Store the sort preference.
3cfff885
BB
51 *
52 * @var string String matching the sort constants defined in myoverview/lib.php
53 */
54 private $sort;
55
56 /**
5f59a4c0 57 * Store the view preference.
3cfff885
BB
58 *
59 * @var string String matching the view/display constants defined in myoverview/lib.php
60 */
61 private $view;
62
11988d74 63 /**
5f59a4c0 64 * Store the paging preference.
11988d74
P
65 *
66 * @var string String matching the paging constants defined in myoverview/lib.php
67 */
68 private $paging;
69
5ae4db88 70 /**
5f59a4c0 71 * Store the display categories config setting.
5ae4db88
TD
72 *
73 * @var boolean
74 */
75 private $displaycategories;
76
5f59a4c0
TD
77 /**
78 * Store the configuration values for the myoverview block.
79 *
80 * @var array Array of available layouts matching view/display constants defined in myoverview/lib.php
81 */
82 private $layouts;
83
3cfff885
BB
84 /**
85 * main constructor.
86 * Initialize the user preferences
87 *
88 * @param string $grouping Grouping user preference
89 * @param string $sort Sort user preference
90 * @param string $view Display user preference
5f59a4c0
TD
91 *
92 * @throws \dml_exception
3cfff885 93 */
11988d74 94 public function __construct($grouping, $sort, $view, $paging) {
3cfff885
BB
95 $this->grouping = $grouping ? $grouping : BLOCK_MYOVERVIEW_GROUPING_ALL;
96 $this->sort = $sort ? $sort : BLOCK_MYOVERVIEW_SORTING_TITLE;
11988d74 97 $this->paging = $paging ? $paging : BLOCK_MYOVERVIEW_PAGING_12;
5f59a4c0 98
5ae4db88
TD
99 $config = get_config('block_myoverview');
100 if (!$config->displaycategories) {
101 $this->displaycategories = BLOCK_MYOVERVIEW_DISPLAY_CATEGORIES_OFF;
102 } else {
103 $this->displaycategories = BLOCK_MYOVERVIEW_DISPLAY_CATEGORIES_ON;
104 }
5f59a4c0
TD
105
106 $this->set_available_layouts();
107 $this->view = $view ? $view : reset($this->layouts);
3cfff885
BB
108 }
109
5f59a4c0 110
3cfff885 111 /**
5f59a4c0
TD
112 * Set the available layouts based on the config table settings,
113 * if none are available, defaults to the cards view.
114 *
115 * @throws \dml_exception
116 *
117 */
118 public function set_available_layouts() {
119
120 if ($config = get_config('block_myoverview', 'layouts')) {
121 $this->layouts = explode(',', $config);
122 } else {
123 $this->layouts = array(BLOCK_MYOVERVIEW_VIEW_CARD);
124 }
125 }
126
127 /**
128 * Get the user preferences as an array to figure out what has been selected.
3cfff885
BB
129 *
130 * @return array $preferences Array with the pref as key and value set to true
131 */
132 public function get_preferences_as_booleans() {
133 $preferences = [];
3cfff885
BB
134 $preferences[$this->sort] = true;
135 $preferences[$this->grouping] = true;
5f59a4c0
TD
136 // Only use the user view/display preference if it is in available layouts.
137 if (in_array($this->view, $this->layouts)) {
138 $preferences[$this->view] = true;
139 } else {
140 $preferences[reset($this->layouts)] = true;
141 }
3cfff885
BB
142
143 return $preferences;
144 }
145
5f59a4c0
TD
146 /**
147 * Format a layout into an object for export as a Context variable to template.
148 *
149 * @param string $layoutname
150 *
151 * @return \stdClass $layout an object representation of a layout
152 * @throws \coding_exception
153 */
154 public function format_layout_for_export($layoutname) {
155 $layout = new stdClass();
156
157 $layout->id = $layoutname;
158 $layout->name = get_string($layoutname, 'block_myoverview');
159 $layout->active = $this->view == $layoutname ? true : false;
160 $layout->arialabel = get_string('aria:' . $layoutname, 'block_myoverview');
161
162 return $layout;
163 }
164
165 /**
166 * Get the available layouts formatted for export.
167 *
168 * @return array an array of objects representing available layouts
169 */
170 public function get_formatted_available_layouts_for_export() {
171
172 return array_map(array($this, 'format_layout_for_export'), $this->layouts);
173
174 }
175
9ac2e865
RW
176 /**
177 * Export this data so it can be used as the context for a mustache template.
178 *
179 * @param \renderer_base $output
3cfff885 180 * @return array Context variables for the template
9ac2e865
RW
181 */
182 public function export_for_template(renderer_base $output) {
4a995a1f 183
01a95b86 184 $nocoursesurl = $output->image_url('courses', 'block_myoverview')->out();
9ac2e865 185
5f59a4c0
TD
186 $preferences = $this->get_preferences_as_booleans();
187 $availablelayouts = $this->get_formatted_available_layouts_for_export();
188
3cfff885
BB
189 $defaultvariables = [
190 'nocoursesimg' => $nocoursesurl,
191 'grouping' => $this->grouping,
192 'sort' => $this->sort == BLOCK_MYOVERVIEW_SORTING_TITLE ? 'fullname' : 'ul.timeaccess desc',
5f59a4c0
TD
193 // If the user preference display option is not available, default to first available layout.
194 'view' => in_array($this->view, $this->layouts) ? $this->view : reset($this->layouts),
5ae4db88 195 'paging' => $this->paging,
5f59a4c0 196 'layouts' => $availablelayouts,
5ae4db88 197 'displaycategories' => $this->displaycategories,
5f59a4c0 198 'displaydropdown' => (count($availablelayouts) > 1) ? true : false,
9ac2e865 199 ];
3cfff885
BB
200 return array_merge($defaultvariables, $preferences);
201
9ac2e865 202 }
3cfff885 203}