Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace core_contentbank\output;
19
 
20
use context;
21
use core_contentbank\content;
22
use core_contentbank\contenttype;
23
use moodle_url;
24
use renderable;
25
use renderer_base;
26
use stdClass;
27
use templatable;
28
 
29
/**
30
 * Class containing data for the content view.
31
 *
32
 * @copyright  2020 Victor Deniz <victor@moodle.com>
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class viewcontent implements renderable, templatable {
36
    /**
37
     * @var contenttype Content bank content type.
38
     */
39
    private $contenttype;
40
 
41
    /**
42
     * @var stdClass Record of the contentbank_content table.
43
     */
44
    private $content;
45
 
46
    /**
47
     * Construct this renderable.
48
     *
49
     * @param contenttype $contenttype Content bank content type.
50
     * @param content $content Record of the contentbank_content table.
51
     */
52
    public function __construct(contenttype $contenttype, content $content) {
53
        $this->contenttype = $contenttype;
54
        $this->content = $content;
55
    }
56
 
57
    /**
58
     * Get the content of the "More" dropdown in the tertiary navigation
59
     *
60
     * @return array|null The options to be displayed in a dropdown in the tertiary navigation
61
     * @throws \moodle_exception
62
     */
63
    protected function get_edit_actions_dropdown(): ?array {
64
        global $PAGE;
65
        $options = [];
66
        if ($this->contenttype->can_manage($this->content)) {
67
            // Add the visibility item to the menu.
68
            switch($this->content->get_visibility()) {
69
                case content::VISIBILITY_UNLISTED:
70
                    $visibilitylabel = get_string('visibilitysetpublic', 'core_contentbank');
71
                    $newvisibility = content::VISIBILITY_PUBLIC;
72
                    break;
73
                case content::VISIBILITY_PUBLIC:
74
                    $visibilitylabel = get_string('visibilitysetunlisted', 'core_contentbank');
75
                    $newvisibility = content::VISIBILITY_UNLISTED;
76
                    break;
77
                default:
78
                    $url = new \moodle_url('/contentbank/index.php', ['contextid' => $this->content->get_contextid()]);
79
                    throw new moodle_exception('contentvisibilitynotfound', 'error', $url, $this->content->get_visibility());
80
            }
81
 
82
            if ($visibilitylabel) {
83
                $options[$visibilitylabel] = [
84
                    'data-action' => 'setcontentvisibility',
85
                    'data-visibility' => $newvisibility,
86
                    'data-contentid' => $this->content->get_id(),
87
                ];
88
            }
89
 
90
            // Add the rename content item to the menu.
91
            $options[get_string('rename')] = [
92
                'data-action' => 'renamecontent',
93
                'data-contentname' => $this->content->get_name(),
94
                'data-contentid' => $this->content->get_id(),
95
            ];
96
 
97
            if ($this->contenttype->can_upload()) {
98
                $options[get_string('replacecontent', 'contentbank')] = ['data-action' => 'upload'];
99
 
100
                $PAGE->requires->js_call_amd(
101
                    'core_contentbank/upload',
102
                    'initModal',
103
                    [
104
                        '[data-action=upload]',
105
                        \core_contentbank\form\upload_files::class,
106
                        $this->content->get_contextid(),
107
                        $this->content->get_id()
108
                    ]
109
                );
110
            }
111
        }
112
 
113
        if ($this->contenttype->can_download($this->content)) {
114
            $url = new moodle_url($this->contenttype->get_download_url($this->content));
115
            $options[get_string('download')] = [
116
                'url' => $url->out()
117
            ];
118
        }
119
 
120
        if ($this->contenttype->can_copy($this->content)) {
121
            // Add the copy content item to the menu.
122
            $options[get_string('copycontent', 'contentbank')] = [
123
                'data-action' => 'copycontent',
124
                'data-contentname' => get_string('copyof', 'contentbank', $this->content->get_name()),
125
                'data-contentid' => $this->content->get_id(),
126
            ];
127
        }
128
 
129
        // Add the delete content item to the menu.
130
        if ($this->contenttype->can_delete($this->content)) {
131
            $options[get_string('delete')] = [
132
                'data-action' => 'deletecontent',
133
                'data-contentname' => $this->content->get_name(),
134
                'data-uses' => count($this->content->get_uses()),
135
                'data-contentid' => $this->content->get_id(),
136
                'data-contextid' => $this->content->get_contextid(),
137
                'class' => 'text-danger',
138
                ];
139
        }
140
 
141
        $dropdown = [];
142
        if ($options) {
143
            foreach ($options as $key => $attribs) {
144
                $url = $attribs['url'] ?? '#';
145
                $extraclasses = $attribs['class'] ?? '';
146
                $dropdown['options'][] = [
147
                    'label' => $key,
148
                    'url' => $url,
149
                    'extraclasses' => $extraclasses,
150
                    'attributes' => array_map(function ($key, $value) {
151
                        return [
152
                            'name' => $key,
153
                            'value' => $value
154
                        ];
155
                    }, array_keys($attribs), $attribs)
156
                ];
157
            }
158
        }
159
 
160
        return $dropdown;
161
    }
162
 
163
    /**
164
     * Export this data so it can be used as the context for a mustache template.
165
     *
166
     * @param renderer_base $output
167
     *
168
     * @return stdClass
169
     */
170
    public function export_for_template(renderer_base $output): stdClass {
171
        $data = new stdClass();
172
 
173
        // Get the content type html.
174
        $contenthtml = $this->contenttype->get_view_content($this->content);
175
        $data->contenthtml = $contenthtml;
176
 
177
        // Check if the user can edit this content type.
178
        if ($this->contenttype->can_edit($this->content)) {
179
            $data->usercanedit = true;
180
            $urlparams = [
181
                'contextid' => $this->content->get_contextid(),
182
                'plugin' => $this->contenttype->get_plugin_name(),
183
                'id' => $this->content->get_id()
184
            ];
185
            $editcontenturl = new moodle_url('/contentbank/edit.php', $urlparams);
186
            $data->editcontenturl = $editcontenturl->out(false);
187
        }
188
 
189
        // Close/exit link for those users who can access that context.
190
        $context = context::instance_by_id($this->content->get_contextid());
191
        if (has_capability('moodle/contentbank:access', $context)) {
192
            $closeurl = new moodle_url('/contentbank/index.php', ['contextid' => $context->id]);
193
            $data->closeurl = $closeurl->out(false);
194
        }
195
 
196
        $data->actionmenu = $this->get_edit_actions_dropdown();
197
        $data->heading = $this->content->get_name();
198
        if ($this->content->get_visibility() == content::VISIBILITY_UNLISTED) {
199
            $data->heading = get_string('visibilitytitleunlisted', 'contentbank', $data->heading);
200
        }
201
 
202
        return $data;
203
    }
204
}