Merge branch 'MDL-73939-master' of git://github.com/rezaies/moodle
[moodle.git] / contentbank / view.php
blob36db1ce0950fd9812811ad0915e02e8a025a6756
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();
59 if (!$content->is_view_allowed()) {
60 $cburl = new \moodle_url('/contentbank/index.php', ['contextid' => $context->id, 'errormsg' => 'notavailable']);
61 redirect($cburl);
64 $PAGE->set_url(new \moodle_url('/contentbank/view.php', ['id' => $id]));
65 $PAGE->set_context($context);
66 $PAGE->navbar->add($record->name);
67 $title .= ": ".$record->name;
68 $PAGE->set_title($title);
69 $PAGE->set_pagetype('contentbank');
70 $PAGE->set_pagelayout('incourse');
72 echo $OUTPUT->header();
74 // If needed, display notifications.
75 if ($errormsg !== '' && get_string_manager()->string_exists($errormsg, 'core_contentbank')) {
76 $errormsg = get_string($errormsg, 'core_contentbank');
77 echo $OUTPUT->notification($errormsg);
78 } else if ($statusmsg !== '' && get_string_manager()->string_exists($statusmsg, 'core_contentbank')) {
79 if ($statusmsg == 'contentvisibilitychanged') {
80 switch ($content->get_visibility()) {
81 case content::VISIBILITY_PUBLIC:
82 $visibilitymsg = get_string('public', 'core_contentbank');
83 break;
84 case content::VISIBILITY_UNLISTED:
85 $visibilitymsg = get_string('unlisted', 'core_contentbank');
86 break;
87 default:
88 print_error('contentvisibilitynotfound', 'error', $returnurl, $content->get_visibility());
89 break;
91 $statusmsg = get_string($statusmsg, 'core_contentbank', $visibilitymsg);
92 } else {
93 $statusmsg = get_string($statusmsg, 'core_contentbank');
95 echo $OUTPUT->notification($statusmsg, 'notifysuccess');
97 if ($contenttype->can_access()) {
98 $viewcontent = new core_contentbank\output\viewcontent($contenttype, $content);
99 echo $OUTPUT->render($viewcontent);
100 } else {
101 $message = get_string('contenttypenoaccess', 'core_contentbank', $record->contenttype);
102 echo $OUTPUT->notification($message, 'error');
105 echo $OUTPUT->footer();