MDL-67786 core_contentbank: New content bank contenttype plugin type
[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
34$title = get_string('contentbank');
35\core_contentbank\helper::get_page_ready($context, $title);
36if ($PAGE->course) {
37 require_login($PAGE->course->id);
38}
39$PAGE->set_url('/contentbank/index.php');
40$PAGE->set_context($context);
41$PAGE->set_title($title);
42$PAGE->set_heading($title);
43$PAGE->set_pagetype('contenbank');
44
45// Get all contents managed by active plugins to render.
46$foldercontents = array();
47$contents = $DB->get_records('contentbank_content', ['contextid' => $contextid]);
48foreach ($contents as $content) {
49 $plugin = core_plugin_manager::instance()->get_plugin_info($content->contenttype);
50 if (!$plugin || !$plugin->is_enabled()) {
51 continue;
52 }
53 $contentclass = "\\$content->contenttype\\content";
54 if (class_exists($contentclass)) {
55 $contentmanager = new $contentclass($content);
56 if ($contentmanager->can_view()) {
57 $foldercontents[] = $contentmanager;
58 }
59 }
60}
61
62// Get the toolbar ready.
63$toolbar = array ();
64if (has_capability('moodle/contentbank:upload', $context)) {
65 // Don' show upload button if there's no plugin to support any file extension.
66 $cb = new \core_contentbank\contentbank();
67 $accepted = $cb->get_supported_extensions_as_string($context);
68 if (!empty($accepted)) {
69 $importurl = new moodle_url('/contentbank/upload.php', ['contextid' => $contextid]);
70 $toolbar[] = array('name' => 'Upload', 'link' => $importurl, 'icon' => 'i/upload');
71 }
72}
73
74echo $OUTPUT->header();
75echo $OUTPUT->box_start('generalbox');
76
77$folder = new \core_contentbank\output\bankcontent($foldercontents, $toolbar, $context);
78echo $OUTPUT->render($folder);
79
80echo $OUTPUT->box_end();
81echo $OUTPUT->footer();