MDL-68483 contentbank: improve search API
[moodle.git] / contentbank / classes / output / bankcontent.php
... / ...
CommitLineData
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 {
75 global $PAGE;
76
77 $PAGE->requires->js_call_amd('core_contentbank/search', 'init');
78
79 $data = new stdClass();
80 $contentdata = array();
81 foreach ($this->contents as $content) {
82 $record = $content->get_content();
83 $contenttypeclass = $content->get_content_type().'\\contenttype';
84 $contenttype = new $contenttypeclass($this->context);
85 $name = $content->get_name();
86 $contentdata[] = array(
87 'name' => $name,
88 'link' => $contenttype->get_view_url($record),
89 'icon' => $contenttype->get_icon($name)
90 );
91 }
92 $data->contents = $contentdata;
93 $data->tools = $this->toolbar;
94 return $data;
95 }
96}