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