Merge branch 'MDL-77275-master' of https://github.com/laurentdavid/moodle
[moodle.git] / contentbank / view.php
blob2b9e360352d6fdc30b3022683df06f78e3f1457d
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 throw new \moodle_exception('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();
59 if (!$content->is_view_allowed()) {
60 $cburl = new \moodle_url('/contentbank/index.php', ['contextid' => $context->id, 'errormsg' => 'notavailable']);
61 redirect($cburl);
64 if ($context->contextlevel == CONTEXT_COURSECAT) {
65 $PAGE->set_primary_active_tab('home');
68 $PAGE->set_url(new \moodle_url('/contentbank/view.php', ['id' => $id]));
69 if ($context->id == \context_system::instance()->id) {
70 $PAGE->set_context(context_course::instance($context->id));
71 } else {
72 $PAGE->set_context($context);
74 $PAGE->navbar->add($record->name);
75 $title .= ": ".$record->name;
76 $PAGE->set_title($title);
77 $PAGE->set_pagetype('contentbank');
78 $PAGE->set_pagelayout('incourse');
79 $PAGE->set_secondary_active_tab('contentbank');
81 echo $OUTPUT->header();
83 // If needed, display notifications.
84 if ($errormsg !== '' && get_string_manager()->string_exists($errormsg, 'core_contentbank')) {
85 $errormsg = get_string($errormsg, 'core_contentbank');
86 echo $OUTPUT->notification($errormsg);
87 } else if ($statusmsg !== '' && get_string_manager()->string_exists($statusmsg, 'core_contentbank')) {
88 if ($statusmsg == 'contentvisibilitychanged') {
89 switch ($content->get_visibility()) {
90 case content::VISIBILITY_PUBLIC:
91 $visibilitymsg = get_string('public', 'core_contentbank');
92 break;
93 case content::VISIBILITY_UNLISTED:
94 $visibilitymsg = get_string('unlisted', 'core_contentbank');
95 break;
96 default:
97 throw new \moodle_exception('contentvisibilitynotfound', 'error', $returnurl, $content->get_visibility());
98 break;
100 $statusmsg = get_string($statusmsg, 'core_contentbank', $visibilitymsg);
101 } else {
102 $statusmsg = get_string($statusmsg, 'core_contentbank');
104 echo $OUTPUT->notification($statusmsg, 'notifysuccess');
106 if ($contenttype->can_access()) {
107 $viewcontent = new core_contentbank\output\viewcontent($contenttype, $content);
108 echo $OUTPUT->render($viewcontent);
109 } else {
110 $message = get_string('contenttypenoaccess', 'core_contentbank', $record->contenttype);
111 echo $OUTPUT->notification($message, 'error');
114 echo $OUTPUT->footer();