Merge branch 'MDL-69529-master' of git://github.com/ilyatregubov/moodle
[moodle.git] / contentbank / view.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 * Generic content bank visualizer.
19 *
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
23 */
24
25require('../config.php');
26
27require_login();
28
29$id = required_param('id', PARAM_INT);
45192e9a
SA
30$deletecontent = optional_param('deletecontent', null, PARAM_INT);
31
32$PAGE->requires->js_call_amd('core_contentbank/actions', 'init');
33
33b8ca26
AA
34$record = $DB->get_record('contentbank_content', ['id' => $id], '*', MUST_EXIST);
35$context = context::instance_by_id($record->contextid, MUST_EXIST);
36require_capability('moodle/contentbank:access', $context);
37
088786d2
AA
38$statusmsg = optional_param('statusmsg', '', PARAM_ALPHANUMEXT);
39$errormsg = optional_param('errormsg', '', PARAM_ALPHANUMEXT);
448012c6
AA
40
41$returnurl = new \moodle_url('/contentbank/index.php', ['contextid' => $context->id]);
33b8ca26
AA
42$plugin = core_plugin_manager::instance()->get_plugin_info($record->contenttype);
43if (!$plugin || !$plugin->is_enabled()) {
44 print_error('unsupported', 'core_contentbank', $returnurl);
45}
46
47$title = get_string('contentbank');
48\core_contentbank\helper::get_page_ready($context, $title, true);
49if ($PAGE->course) {
50 require_login($PAGE->course->id);
51}
33b8ca26
AA
52
53$PAGE->set_url(new \moodle_url('/contentbank/view.php', ['id' => $id]));
54$PAGE->set_context($context);
55$PAGE->navbar->add($record->name);
613281cd 56$PAGE->set_heading($record->name);
33b8ca26
AA
57$title .= ": ".$record->name;
58$PAGE->set_title($title);
061893d8 59$PAGE->set_pagetype('contentbank');
33b8ca26 60
1ec93d47
IT
61$cb = new \core_contentbank\contentbank();
62$content = $cb->get_content_from_id($record->id);
63$contenttype = $content->get_content_type_instance();
448012c6
AA
64
65// Create the cog menu with all the secondary actions, such as delete, rename...
66$actionmenu = new action_menu();
67$actionmenu->set_alignment(action_menu::TR, action_menu::BR);
68if ($contenttype->can_manage($content)) {
69 // Add the rename content item to the menu.
70 $attributes = [
71 'data-action' => 'renamecontent',
72 'data-contentname' => $content->get_name(),
73 'data-contentid' => $content->get_id(),
74 ];
75 $actionmenu->add_secondary_action(new action_menu_link(
76 new moodle_url('#'),
77 new pix_icon('e/styleparagraph', get_string('rename')),
78 get_string('rename'),
79 false,
80 $attributes
81 ));
c5a8469a
FR
82
83 if ($contenttype->can_upload()) {
84 $actionmenu->add_secondary_action(new action_menu_link(
85 new moodle_url('/contentbank/upload.php', ['contextid' => $context->id, 'id' => $content->get_id()]),
86 new pix_icon('i/upload', get_string('upload')),
87 get_string('replacecontent', 'contentbank'),
88 false
89 ));
90 }
448012c6 91}
66455e02
SA
92if ($contenttype->can_download($content)) {
93 // Add the download content item to the menu.
94 $actionmenu->add_secondary_action(new action_menu_link(
95 new moodle_url($contenttype->get_download_url($content)),
96 new pix_icon('t/download', get_string('download')),
97 get_string('download'),
98 false
99 ));
100}
45192e9a 101if ($contenttype->can_delete($content)) {
45192e9a
SA
102 // Add the delete content item to the menu.
103 $attributes = [
104 'data-action' => 'deletecontent',
105 'data-contentname' => $content->get_name(),
106 'data-contentid' => $content->get_id(),
107 'data-contextid' => $context->id,
108 ];
109 $actionmenu->add_secondary_action(new action_menu_link(
110 new moodle_url('#'),
111 new pix_icon('t/delete', get_string('delete')),
112 get_string('delete'),
113 false,
114 $attributes
115 ));
45192e9a
SA
116}
117
448012c6
AA
118// Add the cog menu to the header.
119$PAGE->add_header_action(html_writer::div(
120 $OUTPUT->render($actionmenu),
121 'd-print-none',
122 ['id' => 'region-main-settings-menu']
123));
124
33b8ca26 125echo $OUTPUT->header();
33b8ca26 126
448012c6 127// If needed, display notifications.
088786d2
AA
128if ($errormsg !== '' && get_string_manager()->string_exists($errormsg, 'core_contentbank')) {
129 $errormsg = get_string($errormsg, 'core_contentbank');
448012c6 130 echo $OUTPUT->notification($errormsg);
088786d2
AA
131} else if ($statusmsg !== '' && get_string_manager()->string_exists($statusmsg, 'core_contentbank')) {
132 $statusmsg = get_string($statusmsg, 'core_contentbank');
448012c6
AA
133 echo $OUTPUT->notification($statusmsg, 'notifysuccess');
134}
45192e9a 135if ($contenttype->can_access()) {
613281cd
VDF
136 $viewcontent = new core_contentbank\output\viewcontent($contenttype, $content);
137 echo $OUTPUT->render($viewcontent);
138} else {
139 $message = get_string('contenttypenoaccess', 'core_contentbank', $record->contenttype);
140 echo $OUTPUT->notification($message, 'error');
33b8ca26
AA
141}
142
33b8ca26 143echo $OUTPUT->footer();