Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 403]

   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  
  25  require('../config.php');
  26  
  27  require_login();
  28  
  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);
  32  
  33  $cb = new \core_contentbank\contentbank();
  34  if (!$cb->is_context_allowed($context)) {
  35      throw new \moodle_exception('contextnotallowed', 'core_contentbank');
  36  }
  37  
  38  require_capability('moodle/contentbank:access', $context);
  39  
  40  $statusmsg = optional_param('statusmsg', '', PARAM_ALPHANUMEXT);
  41  $errormsg = optional_param('errormsg', '', PARAM_ALPHANUMEXT);
  42  
  43  $title = get_string('contentbank');
  44  \core_contentbank\helper::get_page_ready($context, $title);
  45  if ($PAGE->course) {
  46      require_login($PAGE->course->id);
  47  }
  48  $PAGE->set_url('/contentbank/index.php', ['contextid' => $contextid]);
  49  if ($contextid == \context_system::instance()->id) {
  50      $PAGE->set_context(context_course::instance($contextid));
  51  } else {
  52      $PAGE->set_context($context);
  53  }
  54  
  55  if ($context->contextlevel == CONTEXT_COURSECAT) {
  56      $PAGE->set_primary_active_tab('home');
  57  }
  58  
  59  $PAGE->set_title($title);
  60  $PAGE->add_body_class('limitedwidth');
  61  $PAGE->set_pagetype('contentbank');
  62  $PAGE->set_secondary_active_tab('contentbank');
  63  
  64  // Get all contents managed by active plugins where the user has permission to render them.
  65  $contenttypes = [];
  66  $enabledcontenttypes = $cb->get_enabled_content_types();
  67  foreach ($enabledcontenttypes as $contenttypename) {
  68      $contenttypeclass = "\\contenttype_$contenttypename\\contenttype";
  69      $contenttype = new $contenttypeclass($context);
  70      if ($contenttype->can_access()) {
  71          $contenttypes[] = $contenttypename;
  72      }
  73  }
  74  
  75  $foldercontents = $cb->search_contents($search, $contextid, $contenttypes);
  76  
  77  // Get the toolbar ready.
  78  $toolbar = array ();
  79  
  80  // Place the Add button in the toolbar.
  81  if (has_capability('moodle/contentbank:useeditor', $context)) {
  82      // Get the content types for which the user can use an editor.
  83      $editabletypes = $cb->get_contenttypes_with_capability_feature(\core_contentbank\contenttype::CAN_EDIT, $context);
  84      if (!empty($editabletypes)) {
  85          // Editor base URL.
  86          $editbaseurl = new moodle_url('/contentbank/edit.php', ['contextid' => $contextid]);
  87          $toolbar[] = [
  88              'name' => get_string('add'),
  89              'link' => $editbaseurl, 'dropdown' => true,
  90              'contenttypes' => $editabletypes,
  91              'action' => 'add'
  92          ];
  93      }
  94  }
  95  
  96  // Place the Upload button in the toolbar.
  97  if (has_capability('moodle/contentbank:upload', $context)) {
  98      // Don' show upload button if there's no plugin to support any file extension.
  99      $accepted = $cb->get_supported_extensions_as_string($context);
 100      if (!empty($accepted)) {
 101          $importurl = new moodle_url('/contentbank/index.php', ['contextid' => $contextid]);
 102          $toolbar[] = [
 103              'name' => get_string('upload', 'contentbank'),
 104              'link' => $importurl->out(false),
 105              'icon' => 'i/upload',
 106              'action' => 'upload'
 107          ];
 108          $PAGE->requires->js_call_amd(
 109              'core_contentbank/upload',
 110              'initModal',
 111              ['[data-action=upload]', \core_contentbank\form\upload_files::class, $contextid]
 112          );
 113      }
 114  }
 115  
 116  echo $OUTPUT->header();
 117  echo $OUTPUT->heading($title, 2);
 118  echo $OUTPUT->box_start('generalbox');
 119  
 120  // If needed, display notifications.
 121  if ($errormsg !== '' && get_string_manager()->string_exists($errormsg, 'core_contentbank')) {
 122      $errormsg = get_string($errormsg, 'core_contentbank');
 123      echo $OUTPUT->notification($errormsg);
 124  } else if ($statusmsg !== '' && get_string_manager()->string_exists($statusmsg, 'core_contentbank')) {
 125      $statusmsg = get_string($statusmsg, 'core_contentbank');
 126      echo $OUTPUT->notification($statusmsg, 'notifysuccess');
 127  }
 128  
 129  // Render the contentbank contents.
 130  $folder = new \core_contentbank\output\bankcontent($foldercontents, $toolbar, $context, $cb);
 131  echo $OUTPUT->render($folder);
 132  
 133  echo $OUTPUT->box_end();
 134  echo $OUTPUT->footer();