weekly release 3.8dev
[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;
3f0c421b 30
3cfff885 31require_once($CFG->dirroot . '/blocks/myoverview/lib.php');
c5515a34 32
9ac2e865
RW
33/**
34 * Class containing data for my overview block.
35 *
e4b4b9e7 36 * @copyright 2018 Bas Brands <bas@moodle.com>
9ac2e865
RW
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class main implements renderable, templatable {
3cfff885
BB
40
41 /**
42 * Store the grouping preference
43 *
44 * @var string String matching the grouping constants defined in myoverview/lib.php
45 */
46 private $grouping;
47
48 /**
49 * Store the sort preference
50 *
51 * @var string String matching the sort constants defined in myoverview/lib.php
52 */
53 private $sort;
54
55 /**
56 * Store the view preference
57 *
58 * @var string String matching the view/display constants defined in myoverview/lib.php
59 */
60 private $view;
61
11988d74
P
62 /**
63 * Store the paging preference
64 *
65 * @var string String matching the paging constants defined in myoverview/lib.php
66 */
67 private $paging;
68
5ae4db88
TD
69 /**
70 * Store the display categories config setting
71 *
72 * @var boolean
73 */
74 private $displaycategories;
75
3cfff885
BB
76 /**
77 * main constructor.
78 * Initialize the user preferences
79 *
80 * @param string $grouping Grouping user preference
81 * @param string $sort Sort user preference
82 * @param string $view Display user preference
83 */
11988d74 84 public function __construct($grouping, $sort, $view, $paging) {
3cfff885
BB
85 $this->grouping = $grouping ? $grouping : BLOCK_MYOVERVIEW_GROUPING_ALL;
86 $this->sort = $sort ? $sort : BLOCK_MYOVERVIEW_SORTING_TITLE;
87 $this->view = $view ? $view : BLOCK_MYOVERVIEW_VIEW_CARD;
11988d74 88 $this->paging = $paging ? $paging : BLOCK_MYOVERVIEW_PAGING_12;
5ae4db88
TD
89 $config = get_config('block_myoverview');
90 if (!$config->displaycategories) {
91 $this->displaycategories = BLOCK_MYOVERVIEW_DISPLAY_CATEGORIES_OFF;
92 } else {
93 $this->displaycategories = BLOCK_MYOVERVIEW_DISPLAY_CATEGORIES_ON;
94 }
3cfff885
BB
95 }
96
97 /**
98 * Get the user preferences as an array to figure out what has been selected
99 *
100 * @return array $preferences Array with the pref as key and value set to true
101 */
102 public function get_preferences_as_booleans() {
103 $preferences = [];
104 $preferences[$this->view] = true;
105 $preferences[$this->sort] = true;
106 $preferences[$this->grouping] = true;
107
108 return $preferences;
109 }
110
9ac2e865
RW
111 /**
112 * Export this data so it can be used as the context for a mustache template.
113 *
114 * @param \renderer_base $output
3cfff885 115 * @return array Context variables for the template
9ac2e865
RW
116 */
117 public function export_for_template(renderer_base $output) {
4a995a1f 118
01a95b86 119 $nocoursesurl = $output->image_url('courses', 'block_myoverview')->out();
9ac2e865 120
3cfff885
BB
121 $defaultvariables = [
122 'nocoursesimg' => $nocoursesurl,
123 'grouping' => $this->grouping,
124 'sort' => $this->sort == BLOCK_MYOVERVIEW_SORTING_TITLE ? 'fullname' : 'ul.timeaccess desc',
11988d74 125 'view' => $this->view,
5ae4db88
TD
126 'paging' => $this->paging,
127 'displaycategories' => $this->displaycategories,
9ac2e865 128 ];
3cfff885
BB
129
130 $preferences = $this->get_preferences_as_booleans();
131 return array_merge($defaultvariables, $preferences);
132
9ac2e865 133 }
3cfff885 134}