Moodle release 3.11.17
[moodle.git] / contentbank / view.php
blob25e036883b46ac47cc2643a51c21ca92539e3c93
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.
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/>.
17 /**
18 * Generic content bank visualizer.
20 * @package core_contentbank
21 * @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 use core_contentbank\content;
27 require('../config.php');
29 require_login();
31 $id = required_param('id', PARAM_INT);
32 $deletecontent = optional_param('deletecontent', null, PARAM_INT);
34 $PAGE->requires->js_call_amd('core_contentbank/actions', 'init');
36 $record = $DB->get_record('contentbank_content', ['id' => $id], '*', MUST_EXIST);
37 $context = context::instance_by_id($record->contextid, MUST_EXIST);
38 require_capability('moodle/contentbank:access', $context);
40 $statusmsg = optional_param('statusmsg', '', PARAM_ALPHANUMEXT);
41 $errormsg = optional_param('errormsg', '', PARAM_ALPHANUMEXT);
43 $returnurl = new \moodle_url('/contentbank/index.php', ['contextid' => $context->id]);
44 $plugin = core_plugin_manager::instance()->get_plugin_info($record->contenttype);
45 if (!$plugin || !$plugin->is_enabled()) {
46 print_error('unsupported', 'core_contentbank', $returnurl);
49 $title = get_string('contentbank');
50 \core_contentbank\helper::get_page_ready($context, $title, true);
51 if ($PAGE->course) {
52 require_login($PAGE->course->id);
55 $cb = new \core_contentbank\contentbank();
56 $content = $cb->get_content_from_id($record->id);
57 $contenttype = $content->get_content_type_instance();
58 $pageheading = $record->name;
60 if (!$content->is_view_allowed()) {
61 $cburl = new \moodle_url('/contentbank/index.php', ['contextid' => $context->id, 'errormsg' => 'notavailable']);
62 redirect($cburl);
65 if ($content->get_visibility() == content::VISIBILITY_UNLISTED) {
66 $pageheading = get_string('visibilitytitleunlisted', 'contentbank', $record->name);
69 $PAGE->set_url(new \moodle_url('/contentbank/view.php', ['id' => $id]));
70 $PAGE->set_context($context);
71 $PAGE->navbar->add($record->name);
72 $PAGE->set_heading($pageheading);
73 $title .= ": ".$record->name;
74 $PAGE->set_title($title);
75 $PAGE->set_pagetype('contentbank');
77 // Create the cog menu with all the secondary actions, such as delete, rename...
78 $actionmenu = new action_menu();
79 $actionmenu->set_alignment(action_menu::TR, action_menu::BR);
80 if ($contenttype->can_manage($content)) {
81 // Add the visibility item to the menu.
82 switch($content->get_visibility()) {
83 case content::VISIBILITY_UNLISTED:
84 $visibilitylabel = get_string('visibilitysetpublic', 'core_contentbank');
85 $newvisibility = content::VISIBILITY_PUBLIC;
86 $visibilityicon = 't/hide';
87 break;
88 case content::VISIBILITY_PUBLIC:
89 $visibilitylabel = get_string('visibilitysetunlisted', 'core_contentbank');
90 $newvisibility = content::VISIBILITY_UNLISTED;
91 $visibilityicon = 't/show';
92 break;
93 default:
94 print_error('contentvisibilitynotfound', 'error', $returnurl, $content->get_visibility());
95 break;
98 $attributes = [
99 'data-action' => 'setcontentvisibility',
100 'data-visibility' => $newvisibility,
101 'data-contentid' => $content->get_id(),
103 $actionmenu->add_secondary_action(new action_menu_link(
104 new moodle_url('#'),
105 new pix_icon($visibilityicon, $visibilitylabel),
106 $visibilitylabel,
107 false,
108 $attributes
111 // Add the rename content item to the menu.
112 $attributes = [
113 'data-action' => 'renamecontent',
114 'data-contentname' => $content->get_name(),
115 'data-contentid' => $content->get_id(),
117 $actionmenu->add_secondary_action(new action_menu_link(
118 new moodle_url('#'),
119 new pix_icon('e/styleparagraph', get_string('rename')),
120 get_string('rename'),
121 false,
122 $attributes
125 if ($contenttype->can_upload()) {
126 $actionmenu->add_secondary_action(new action_menu_link(
127 new moodle_url('/contentbank/view.php', ['contextid' => $context->id, 'id' => $content->get_id()]),
128 new pix_icon('i/upload', get_string('upload')),
129 get_string('replacecontent', 'contentbank'),
130 false,
131 ['data-action' => 'upload']
133 $PAGE->requires->js_call_amd(
134 'core_contentbank/upload',
135 'initModal',
136 ['[data-action=upload]', \core_contentbank\form\upload_files::class, $context->id, $content->get_id()]
140 if ($contenttype->can_download($content)) {
141 // Add the download content item to the menu.
142 $actionmenu->add_secondary_action(new action_menu_link(
143 new moodle_url($contenttype->get_download_url($content)),
144 new pix_icon('t/download', get_string('download')),
145 get_string('download'),
146 false
149 if ($contenttype->can_delete($content)) {
150 // Add the delete content item to the menu.
151 $attributes = [
152 'data-action' => 'deletecontent',
153 'data-contentname' => $content->get_name(),
154 'data-uses' => count($content->get_uses()),
155 'data-contentid' => $content->get_id(),
156 'data-contextid' => $context->id,
158 $actionmenu->add_secondary_action(new action_menu_link(
159 new moodle_url('#'),
160 new pix_icon('t/delete', get_string('delete')),
161 get_string('delete'),
162 false,
163 $attributes
167 // Add the cog menu to the header.
168 $PAGE->add_header_action(html_writer::div(
169 $OUTPUT->render($actionmenu),
170 'd-print-none',
171 ['id' => 'region-main-settings-menu']
174 echo $OUTPUT->header();
176 // If needed, display notifications.
177 if ($errormsg !== '' && get_string_manager()->string_exists($errormsg, 'core_contentbank')) {
178 $errormsg = get_string($errormsg, 'core_contentbank');
179 echo $OUTPUT->notification($errormsg);
180 } else if ($statusmsg !== '' && get_string_manager()->string_exists($statusmsg, 'core_contentbank')) {
181 if ($statusmsg == 'contentvisibilitychanged') {
182 switch ($content->get_visibility()) {
183 case content::VISIBILITY_PUBLIC:
184 $visibilitymsg = get_string('public', 'core_contentbank');
185 break;
186 case content::VISIBILITY_UNLISTED:
187 $visibilitymsg = get_string('unlisted', 'core_contentbank');
188 break;
189 default:
190 print_error('contentvisibilitynotfound', 'error', $returnurl, $content->get_visibility());
191 break;
193 $statusmsg = get_string($statusmsg, 'core_contentbank', $visibilitymsg);
194 } else {
195 $statusmsg = get_string($statusmsg, 'core_contentbank');
197 echo $OUTPUT->notification($statusmsg, 'notifysuccess');
199 if ($contenttype->can_access()) {
200 $viewcontent = new core_contentbank\output\viewcontent($contenttype, $content);
201 echo $OUTPUT->render($viewcontent);
202 } else {
203 $message = get_string('contenttypenoaccess', 'core_contentbank', $record->contenttype);
204 echo $OUTPUT->notification($message, 'error');
207 echo $OUTPUT->footer();