Merge branch 'MDL-69672-310' of git://github.com/aanabit/moodle into MOODLE_310_STABLE
[moodle.git] / contentbank / index.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 * List content in content bank.
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$contextid = optional_param('contextid', \context_system::instance()->id, PARAM_INT);
1f8dfe79 30$search = optional_param('search', '', PARAM_CLEAN);
33b8ca26
AA
31$context = context::instance_by_id($contextid, MUST_EXIST);
32
3490f29a
AA
33$cb = new \core_contentbank\contentbank();
34if (!$cb->is_context_allowed($context)) {
35 print_error('contextnotallowed', 'core_contentbank');
36}
37
33b8ca26
AA
38require_capability('moodle/contentbank:access', $context);
39
088786d2
AA
40$statusmsg = optional_param('statusmsg', '', PARAM_ALPHANUMEXT);
41$errormsg = optional_param('errormsg', '', PARAM_ALPHANUMEXT);
45192e9a 42
33b8ca26
AA
43$title = get_string('contentbank');
44\core_contentbank\helper::get_page_ready($context, $title);
45if ($PAGE->course) {
46 require_login($PAGE->course->id);
47}
48$PAGE->set_url('/contentbank/index.php');
49$PAGE->set_context($context);
50$PAGE->set_title($title);
51$PAGE->set_heading($title);
061893d8 52$PAGE->set_pagetype('contentbank');
33b8ca26 53
54ce66bd 54// Get all contents managed by active plugins where the user has permission to render them.
54ce66bd
SA
55$contenttypes = [];
56$enabledcontenttypes = $cb->get_enabled_content_types();
57foreach ($enabledcontenttypes as $contenttypename) {
58 $contenttypeclass = "\\contenttype_$contenttypename\\contenttype";
59 $contenttype = new $contenttypeclass($context);
60 if ($contenttype->can_access()) {
61 $contenttypes[] = $contenttypename;
62 }
63}
64
65$foldercontents = $cb->search_contents($search, $contextid, $contenttypes);
33b8ca26
AA
66
67// Get the toolbar ready.
68$toolbar = array ();
75f58cbf
VDF
69
70// Place the Add button in the toolbar.
71if (has_capability('moodle/contentbank:useeditor', $context)) {
72 // Get the content types for which the user can use an editor.
73 $editabletypes = $cb->get_contenttypes_with_capability_feature(\core_contentbank\contenttype::CAN_EDIT, $context);
74 if (!empty($editabletypes)) {
75 // Editor base URL.
76 $editbaseurl = new moodle_url('/contentbank/edit.php', ['contextid' => $contextid]);
7e5a3958 77 $toolbar[] = [
78 'name' => get_string('add'),
79 'link' => $editbaseurl, 'dropdown' => true,
80 'contenttypes' => $editabletypes,
81 'action' => 'add'
82 ];
75f58cbf
VDF
83 }
84}
85
86// Place the Upload button in the toolbar.
33b8ca26
AA
87if (has_capability('moodle/contentbank:upload', $context)) {
88 // Don' show upload button if there's no plugin to support any file extension.
33b8ca26
AA
89 $accepted = $cb->get_supported_extensions_as_string($context);
90 if (!empty($accepted)) {
91 $importurl = new moodle_url('/contentbank/upload.php', ['contextid' => $contextid]);
7e5a3958 92 $toolbar[] = [
93 'name' => get_string('upload', 'contentbank'),
94 'link' => $importurl,
95 'icon' => 'i/upload',
96 'action' => 'upload'
97 ];
33b8ca26
AA
98 }
99}
100
101echo $OUTPUT->header();
102echo $OUTPUT->box_start('generalbox');
103
45192e9a 104// If needed, display notifications.
088786d2
AA
105if ($errormsg !== '' && get_string_manager()->string_exists($errormsg, 'core_contentbank')) {
106 $errormsg = get_string($errormsg, 'core_contentbank');
45192e9a 107 echo $OUTPUT->notification($errormsg);
088786d2
AA
108} else if ($statusmsg !== '' && get_string_manager()->string_exists($statusmsg, 'core_contentbank')) {
109 $statusmsg = get_string($statusmsg, 'core_contentbank');
45192e9a
SA
110 echo $OUTPUT->notification($statusmsg, 'notifysuccess');
111}
112
113// Render the contentbank contents.
33b8ca26
AA
114$folder = new \core_contentbank\output\bankcontent($foldercontents, $toolbar, $context);
115echo $OUTPUT->render($folder);
116
117echo $OUTPUT->box_end();
118echo $OUTPUT->footer();