MDL-67797 contentbank: add search_contents method
[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);
30$context = context::instance_by_id($contextid, MUST_EXIST);
31
32require_capability('moodle/contentbank:access', $context);
33
45192e9a
SA
34$statusmsg = optional_param('statusmsg', '', PARAM_RAW);
35$errormsg = optional_param('errormsg', '', PARAM_RAW);
36
33b8ca26
AA
37$title = get_string('contentbank');
38\core_contentbank\helper::get_page_ready($context, $title);
39if ($PAGE->course) {
40 require_login($PAGE->course->id);
41}
42$PAGE->set_url('/contentbank/index.php');
43$PAGE->set_context($context);
44$PAGE->set_title($title);
45$PAGE->set_heading($title);
46$PAGE->set_pagetype('contenbank');
47
48// Get all contents managed by active plugins to render.
49$foldercontents = array();
50$contents = $DB->get_records('contentbank_content', ['contextid' => $contextid]);
51foreach ($contents as $content) {
52 $plugin = core_plugin_manager::instance()->get_plugin_info($content->contenttype);
53 if (!$plugin || !$plugin->is_enabled()) {
54 continue;
55 }
56 $contentclass = "\\$content->contenttype\\content";
57 if (class_exists($contentclass)) {
58 $contentmanager = new $contentclass($content);
59 if ($contentmanager->can_view()) {
60 $foldercontents[] = $contentmanager;
61 }
62 }
63}
64
65// Get the toolbar ready.
66$toolbar = array ();
67if (has_capability('moodle/contentbank:upload', $context)) {
68 // Don' show upload button if there's no plugin to support any file extension.
69 $cb = new \core_contentbank\contentbank();
70 $accepted = $cb->get_supported_extensions_as_string($context);
71 if (!empty($accepted)) {
72 $importurl = new moodle_url('/contentbank/upload.php', ['contextid' => $contextid]);
73 $toolbar[] = array('name' => 'Upload', 'link' => $importurl, 'icon' => 'i/upload');
74 }
75}
76
77echo $OUTPUT->header();
78echo $OUTPUT->box_start('generalbox');
79
45192e9a
SA
80// If needed, display notifications.
81if ($errormsg !== '') {
82 echo $OUTPUT->notification($errormsg);
83} else if ($statusmsg !== '') {
84 echo $OUTPUT->notification($statusmsg, 'notifysuccess');
85}
86
87// Render the contentbank contents.
33b8ca26
AA
88$folder = new \core_contentbank\output\bankcontent($foldercontents, $toolbar, $context);
89echo $OUTPUT->render($folder);
90
91echo $OUTPUT->box_end();
92echo $OUTPUT->footer();