MDL-68796 core_contentbank: Store view preferences
[moodle.git] / contentbank / classes / output / bankcontent.php
CommitLineData
33b8ca26
AA
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 * core_contentbank specific renderers
19 *
20 * @package core_contentbank
21 * @copyright 2020 Ferran Recio <ferran@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core_contentbank\output;
26
27use renderable;
28use templatable;
29use renderer_base;
30use stdClass;
31
32/**
33 * Class containing data for bank content
34 *
35 * @copyright 2020 Ferran Recio <ferran@moodle.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class bankcontent implements renderable, templatable {
39
40 /**
41 * @var \core_contentbank\content[] Array of content bank contents.
42 */
43 private $contents;
44
45 /**
46 * @var array $toolbar object.
47 */
48 private $toolbar;
49
50 /**
51 * @var \context Given context. Null by default.
52 */
53 private $context;
54
55 /**
56 * Construct this renderable.
57 *
58 * @param \core_contentbank\content[] $contents Array of content bank contents.
59 * @param array $toolbar List of content bank toolbar options.
60 * @param \context $context Optional context to check (default null)
61 */
62 public function __construct(array $contents, array $toolbar, \context $context = null) {
63 $this->contents = $contents;
64 $this->toolbar = $toolbar;
65 $this->context = $context;
66 }
67
68 /**
69 * Export the data.
70 *
71 * @param renderer_base $output
72 * @return stdClass
73 */
74 public function export_for_template(renderer_base $output): stdClass {
f9b6849b
SA
75 global $PAGE;
76
77 $PAGE->requires->js_call_amd('core_contentbank/search', 'init');
e695d9d0 78 $PAGE->requires->js_call_amd('core_contentbank/sort', 'init');
f9b6849b 79
33b8ca26
AA
80 $data = new stdClass();
81 $contentdata = array();
82 foreach ($this->contents as $content) {
e695d9d0
BB
83 $file = $content->get_file();
84 $filesize = $file ? $file->get_filesize() : 0;
85 $mimetype = $file ? get_mimetype_description($file) : '';
54ce66bd
SA
86 $contenttypeclass = $content->get_content_type().'\\contenttype';
87 $contenttype = new $contenttypeclass($this->context);
88 $name = $content->get_name();
89 $contentdata[] = array(
90 'name' => $name,
e695d9d0 91 'title' => strtolower($name),
f9830e45 92 'link' => $contenttype->get_view_url($content),
e695d9d0
BB
93 'icon' => $contenttype->get_icon($content),
94 'timemodified' => $content->get_timemodified(),
95 'bytes' => $filesize,
96 'size' => display_size($filesize),
97 'type' => $mimetype
54ce66bd 98 );
33b8ca26 99 }
c393d818 100 $data->viewlist = get_user_preferences('core_contentbank_view_list');
33b8ca26
AA
101 $data->contents = $contentdata;
102 $data->tools = $this->toolbar;
103 return $data;
104 }
105}