Merge branch 'MDL-79664' of https://github.com/paulholden/moodle
[moodle.git] / contentbank / index.php
bloba2ff8174e3e89893a014000e448cc9c2c2353627
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 * List content in content bank.
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 require('../config.php');
27 require_login();
29 $contextid = optional_param('contextid', \context_system::instance()->id, PARAM_INT);
30 $search = optional_param('search', '', PARAM_CLEAN);
31 $context = context::instance_by_id($contextid, MUST_EXIST);
33 $cb = new \core_contentbank\contentbank();
34 if (!$cb->is_context_allowed($context)) {
35 throw new \moodle_exception('contextnotallowed', 'core_contentbank');
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 $title = get_string('contentbank');
47 \core_contentbank\helper::get_page_ready($context, $title);
48 if ($PAGE->course) {
49 require_login($PAGE->course->id);
51 $PAGE->set_url('/contentbank/index.php', ['contextid' => $contextid]);
52 if ($contextid == \context_system::instance()->id) {
53 $PAGE->set_context(context_course::instance($contextid));
54 } else {
55 $PAGE->set_context($context);
58 if ($context->contextlevel == CONTEXT_COURSECAT) {
59 $PAGE->set_primary_active_tab('home');
62 $PAGE->set_title($title);
63 $PAGE->add_body_class('limitedwidth');
64 $PAGE->set_pagetype('contentbank');
65 $PAGE->set_secondary_active_tab('contentbank');
67 // Get all contents managed by active plugins where the user has permission to render them.
68 $contenttypes = [];
69 $enabledcontenttypes = $cb->get_enabled_content_types();
70 foreach ($enabledcontenttypes as $contenttypename) {
71 $contenttypeclass = "\\contenttype_$contenttypename\\contenttype";
72 $contenttype = new $contenttypeclass($context);
73 if ($contenttype->can_access()) {
74 $contenttypes[] = $contenttypename;
78 $foldercontents = $cb->search_contents($search, $contextid, $contenttypes);
80 // Get the toolbar ready.
81 $toolbar = array ();
83 // Place the Add button in the toolbar.
84 if (has_capability('moodle/contentbank:useeditor', $context)) {
85 // Get the content types for which the user can use an editor.
86 $editabletypes = $cb->get_contenttypes_with_capability_feature(\core_contentbank\contenttype::CAN_EDIT, $context);
87 if (!empty($editabletypes)) {
88 // Editor base URL.
89 $editbaseurl = new moodle_url('/contentbank/edit.php', ['contextid' => $contextid]);
90 $toolbar[] = [
91 'name' => get_string('add'),
92 'link' => $editbaseurl, 'dropdown' => true,
93 'contenttypes' => $editabletypes,
94 'action' => 'add'
99 // Place the Upload button in the toolbar.
100 if (has_capability('moodle/contentbank:upload', $context)) {
101 // Don' show upload button if there's no plugin to support any file extension.
102 $accepted = $cb->get_supported_extensions_as_string($context);
103 if (!empty($accepted)) {
104 $importurl = new moodle_url('/contentbank/index.php', ['contextid' => $contextid]);
105 $toolbar[] = [
106 'name' => get_string('upload', 'contentbank'),
107 'link' => $importurl->out(false),
108 'icon' => 'i/upload',
109 'action' => 'upload'
111 $PAGE->requires->js_call_amd(
112 'core_contentbank/upload',
113 'initModal',
114 ['[data-action=upload]', \core_contentbank\form\upload_files::class, $contextid]
119 echo $OUTPUT->header();
120 echo $OUTPUT->heading($title, 2);
121 echo $OUTPUT->box_start('generalbox');
123 // If needed, display notifications.
124 if (!empty($errormsg) && get_string_manager()->string_exists($errormsg, 'core_contentbank')) {
125 $errormsg = get_string($errormsg, 'core_contentbank');
126 echo $OUTPUT->notification($errormsg);
127 } else if (!empty($statusmsg) && get_string_manager()->string_exists($statusmsg, 'core_contentbank')) {
128 $statusmsg = get_string($statusmsg, 'core_contentbank');
129 echo $OUTPUT->notification($statusmsg, 'notifysuccess');
132 // Render the contentbank contents.
133 $folder = new \core_contentbank\output\bankcontent($foldercontents, $toolbar, $context, $cb);
134 echo $OUTPUT->render($folder);
136 echo $OUTPUT->box_end();
137 echo $OUTPUT->footer();