weekly release 4.0dev
[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;
3776fbaf 31use core_contentbank\content;
33b8ca26
AA
32
33/**
34 * Class containing data for bank content
35 *
36 * @copyright 2020 Ferran Recio <ferran@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class bankcontent implements renderable, templatable {
40
41 /**
42 * @var \core_contentbank\content[] Array of content bank contents.
43 */
44 private $contents;
45
46 /**
47 * @var array $toolbar object.
48 */
49 private $toolbar;
50
51 /**
52 * @var \context Given context. Null by default.
53 */
54 private $context;
55
56 /**
57 * Construct this renderable.
58 *
59 * @param \core_contentbank\content[] $contents Array of content bank contents.
60 * @param array $toolbar List of content bank toolbar options.
61 * @param \context $context Optional context to check (default null)
62 */
63 public function __construct(array $contents, array $toolbar, \context $context = null) {
64 $this->contents = $contents;
65 $this->toolbar = $toolbar;
66 $this->context = $context;
67 }
68
69 /**
70 * Export the data.
71 *
72 * @param renderer_base $output
73 * @return stdClass
74 */
75 public function export_for_template(renderer_base $output): stdClass {
f9b6849b
SA
76 global $PAGE;
77
78 $PAGE->requires->js_call_amd('core_contentbank/search', 'init');
e695d9d0 79 $PAGE->requires->js_call_amd('core_contentbank/sort', 'init');
f9b6849b 80
33b8ca26
AA
81 $data = new stdClass();
82 $contentdata = array();
83 foreach ($this->contents as $content) {
e695d9d0
BB
84 $file = $content->get_file();
85 $filesize = $file ? $file->get_filesize() : 0;
86 $mimetype = $file ? get_mimetype_description($file) : '';
54ce66bd
SA
87 $contenttypeclass = $content->get_content_type().'\\contenttype';
88 $contenttype = new $contenttypeclass($this->context);
3776fbaf
FM
89 if ($content->get_visibility() == content::VISIBILITY_UNLISTED) {
90 $name = get_string('visibilitytitleunlisted', 'contentbank', $content->get_name());
91 } else {
92 $name = $content->get_name();
93 }
15381c0b 94 $author = \core_user::get_user($content->get_content()->usercreated);
54ce66bd
SA
95 $contentdata[] = array(
96 'name' => $name,
e695d9d0 97 'title' => strtolower($name),
f9830e45 98 'link' => $contenttype->get_view_url($content),
e695d9d0 99 'icon' => $contenttype->get_icon($content),
3810b293 100 'uses' => count($content->get_uses()),
e695d9d0
BB
101 'timemodified' => $content->get_timemodified(),
102 'bytes' => $filesize,
103 'size' => display_size($filesize),
15381c0b
SA
104 'type' => $mimetype,
105 'author' => fullname($author),
3776fbaf 106 'visibilityunlisted' => $content->get_visibility() == content::VISIBILITY_UNLISTED
54ce66bd 107 );
33b8ca26 108 }
c393d818 109 $data->viewlist = get_user_preferences('core_contentbank_view_list');
33b8ca26 110 $data->contents = $contentdata;
75f58cbf
VDF
111 // The tools are displayed in the action bar on the index page.
112 foreach ($this->toolbar as $tool) {
113 // Customize the output of a tool, like dropdowns.
7e5a3958 114 $method = 'export_tool_'.$tool['action'];
75f58cbf
VDF
115 if (method_exists($this, $method)) {
116 $this->$method($tool);
117 }
118 $data->tools[] = $tool;
119 }
120
33b8ca26
AA
121 return $data;
122 }
75f58cbf
VDF
123
124 /**
125 * Adds the content type items to display to the Add dropdown.
126 *
127 * Each content type is represented as an object with the properties:
128 * - name: the name of the content type.
129 * - baseurl: the base content type editor URL.
130 * - types: different types of the content type to display as dropdown items.
131 *
132 * @param array $tool Data for rendering the Add dropdown, including the editable content types.
133 */
134 private function export_tool_add(array &$tool) {
135 $editabletypes = $tool['contenttypes'];
136
137 $addoptions = [];
138 foreach ($editabletypes as $class => $type) {
139 $contentype = new $class($this->context);
140 // Get the creation options of each content type.
141 $types = $contentype->get_contenttype_types();
142 if ($types) {
143 // Add a text describing the content type as first option. This will be displayed in the drop down to
144 // separate the options for the different content types.
145 $contentdesc = new stdClass();
146 $contentdesc->typename = get_string('description', $contentype->get_contenttype_name());
147 array_unshift($types, $contentdesc);
148 // Context data for the template.
149 $addcontenttype = new stdClass();
150 // Content type name.
151 $addcontenttype->name = $type;
152 // Content type editor base URL.
153 $tool['link']->param('plugin', $type);
154 $addcontenttype->baseurl = $tool['link']->out();
155 // Different types of the content type.
156 $addcontenttype->types = $types;
157 $addoptions[] = $addcontenttype;
158 }
159 }
160
161 $tool['contenttypes'] = $addoptions;
162 }
33b8ca26 163}