Merge branch 'MDL-81084-main' of https://github.com/andrewnicols/moodle
[moodle.git] / contentbank / view.php
blobdafea573db9e9f3b360af7b97efb9b131844de34
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 // If notifications had been sent we don't pay attention to message parameter.
41 if (empty($SESSION->notifications)) {
42 $statusmsg = optional_param('statusmsg', '', PARAM_ALPHANUMEXT);
43 $errormsg = optional_param('errormsg', '', PARAM_ALPHANUMEXT);
46 $returnurl = new \moodle_url('/contentbank/index.php', ['contextid' => $context->id]);
47 $plugin = core_plugin_manager::instance()->get_plugin_info($record->contenttype);
48 if (!$plugin || !$plugin->is_enabled()) {
49 throw new \moodle_exception('unsupported', 'core_contentbank', $returnurl);
52 $title = get_string('contentbank');
53 \core_contentbank\helper::get_page_ready($context, $title, true);
54 if ($PAGE->course) {
55 require_login($PAGE->course->id);
58 $cb = new \core_contentbank\contentbank();
59 $content = $cb->get_content_from_id($record->id);
60 $contenttype = $content->get_content_type_instance();
62 if (!$content->is_view_allowed()) {
63 $cburl = new \moodle_url('/contentbank/index.php', ['contextid' => $context->id, 'errormsg' => 'notavailable']);
64 redirect($cburl);
67 if ($context->contextlevel == CONTEXT_COURSECAT) {
68 $PAGE->set_primary_active_tab('home');
71 $PAGE->set_url(new \moodle_url('/contentbank/view.php', ['id' => $id]));
72 if ($context->id == \context_system::instance()->id) {
73 $PAGE->set_context(context_course::instance($context->id));
74 } else {
75 $PAGE->set_context($context);
77 $PAGE->navbar->add($record->name);
78 $title .= ": ".$record->name;
79 $PAGE->set_title($title);
80 $PAGE->set_pagetype('contentbank');
81 $PAGE->set_pagelayout('incourse');
82 $PAGE->set_secondary_active_tab('contentbank');
84 echo $OUTPUT->header();
86 // If needed, display notifications.
87 if (!empty($errormsg) && get_string_manager()->string_exists($errormsg, 'core_contentbank')) {
88 $errormsg = get_string($errormsg, 'core_contentbank');
89 echo $OUTPUT->notification($errormsg);
90 } else if (!empty($statusmsg) && get_string_manager()->string_exists($statusmsg, 'core_contentbank')) {
91 if ($statusmsg == 'contentvisibilitychanged') {
92 switch ($content->get_visibility()) {
93 case content::VISIBILITY_PUBLIC:
94 $visibilitymsg = get_string('public', 'core_contentbank');
95 break;
96 case content::VISIBILITY_UNLISTED:
97 $visibilitymsg = get_string('unlisted', 'core_contentbank');
98 break;
99 default:
100 throw new \moodle_exception('contentvisibilitynotfound', 'error', $returnurl, $content->get_visibility());
101 break;
103 $statusmsg = get_string($statusmsg, 'core_contentbank', $visibilitymsg);
104 } else {
105 $statusmsg = get_string($statusmsg, 'core_contentbank');
107 echo $OUTPUT->notification($statusmsg, 'notifysuccess');
109 if ($contenttype->can_access()) {
110 $viewcontent = new core_contentbank\output\viewcontent($contenttype, $content);
111 echo $OUTPUT->render($viewcontent);
112 } else {
113 $message = get_string('contenttypenoaccess', 'core_contentbank', $record->contenttype);
114 echo $OUTPUT->notification($message, 'error');
117 echo $OUTPUT->footer();