on-demand release 4.0dev+
[moodle.git] / contentbank / classes / output / viewcontent.php
CommitLineData
613281cd
VDF
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 a content view.
19 *
20 * @package core_contentbank
21 * @copyright 2020 Victor Deniz <victor@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core_contentbank\output;
26
27use core_contentbank\content;
28use core_contentbank\contenttype;
29use moodle_url;
30use renderable;
31use renderer_base;
32use stdClass;
33use templatable;
34
35/**
36 * Class containing data for the content view.
37 *
38 * @copyright 2020 Victor Deniz <victor@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class viewcontent implements renderable, templatable {
42 /**
43 * @var contenttype Content bank content type.
44 */
45 private $contenttype;
46
47 /**
48 * @var stdClass Record of the contentbank_content table.
49 */
50 private $content;
51
52 /**
53 * Construct this renderable.
54 *
55 * @param contenttype $contenttype Content bank content type.
56 * @param content $content Record of the contentbank_content table.
57 */
58 public function __construct(contenttype $contenttype, content $content) {
59 $this->contenttype = $contenttype;
60 $this->content = $content;
61 }
62
63 /**
64 * Export this data so it can be used as the context for a mustache template.
65 *
66 * @param renderer_base $output
67 *
68 * @return stdClass
69 */
70 public function export_for_template(renderer_base $output): stdClass {
71 $data = new stdClass();
72
73 // Get the content type html.
74 $contenthtml = $this->contenttype->get_view_content($this->content);
75 $data->contenthtml = $contenthtml;
76
77 // Check if the user can edit this content type.
0633e60a 78 if ($this->contenttype->can_edit($this->content)) {
613281cd
VDF
79 $data->usercanedit = true;
80 $urlparams = [
81 'contextid' => $this->content->get_contextid(),
82 'plugin' => $this->contenttype->get_plugin_name(),
83 'id' => $this->content->get_id()
84 ];
85 $editcontenturl = new moodle_url('/contentbank/edit.php', $urlparams);
86 $data->editcontenturl = $editcontenturl->out(false);
87 }
88
89 $closeurl = new moodle_url('/contentbank/index.php', ['contextid' => $this->content->get_contextid()]);
90 $data->closeurl = $closeurl->out(false);
91
92 return $data;
93 }
94}