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   * Generic content bank visualizer.
  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  use core_contentbank\content;
  26  
  27  require('../config.php');
  28  
  29  require_login();
  30  
  31  $id = required_param('id', PARAM_INT);
  32  $deletecontent = optional_param('deletecontent', null, PARAM_INT);
  33  
  34  $PAGE->requires->js_call_amd('core_contentbank/actions', 'init');
  35  
  36  $record = $DB->get_record('contentbank_content', ['id' => $id], '*', MUST_EXIST);
  37  $context = context::instance_by_id($record->contextid, MUST_EXIST);
  38  require_capability('moodle/contentbank:access', $context);
  39  
  40  $statusmsg = optional_param('statusmsg', '', PARAM_ALPHANUMEXT);
  41  $errormsg = optional_param('errormsg', '', PARAM_ALPHANUMEXT);
  42  
  43  $returnurl = new \moodle_url('/contentbank/index.php', ['contextid' => $context->id]);
  44  $plugin = core_plugin_manager::instance()->get_plugin_info($record->contenttype);
  45  if (!$plugin || !$plugin->is_enabled()) {
  46      throw new \moodle_exception('unsupported', 'core_contentbank', $returnurl);
  47  }
  48  
  49  $title = get_string('contentbank');
  50  \core_contentbank\helper::get_page_ready($context, $title, true);
  51  if ($PAGE->course) {
  52      require_login($PAGE->course->id);
  53  }
  54  
  55  $cb = new \core_contentbank\contentbank();
  56  $content = $cb->get_content_from_id($record->id);
  57  $contenttype = $content->get_content_type_instance();
  58  
  59  if (!$content->is_view_allowed()) {
  60      $cburl = new \moodle_url('/contentbank/index.php', ['contextid' => $context->id, 'errormsg' => 'notavailable']);
  61      redirect($cburl);
  62  }
  63  
  64  if ($context->contextlevel == CONTEXT_COURSECAT) {
  65      $PAGE->set_primary_active_tab('home');
  66  }
  67  
  68  $PAGE->set_url(new \moodle_url('/contentbank/view.php', ['id' => $id]));
  69  if ($context->id == \context_system::instance()->id) {
  70      $PAGE->set_context(context_course::instance($context->id));
  71  } else {
  72      $PAGE->set_context($context);
  73  }
  74  $PAGE->navbar->add($record->name);
  75  $title .= ": ".$record->name;
  76  $PAGE->set_title($title);
  77  $PAGE->set_pagetype('contentbank');
  78  $PAGE->set_pagelayout('incourse');
  79  $PAGE->set_secondary_active_tab('contentbank');
  80  
  81  echo $OUTPUT->header();
  82  
  83  // If needed, display notifications.
  84  if ($errormsg !== '' && get_string_manager()->string_exists($errormsg, 'core_contentbank')) {
  85      $errormsg = get_string($errormsg, 'core_contentbank');
  86      echo $OUTPUT->notification($errormsg);
  87  } else if ($statusmsg !== '' && get_string_manager()->string_exists($statusmsg, 'core_contentbank')) {
  88      if ($statusmsg == 'contentvisibilitychanged') {
  89          switch ($content->get_visibility()) {
  90              case content::VISIBILITY_PUBLIC:
  91                  $visibilitymsg = get_string('public', 'core_contentbank');
  92                  break;
  93              case content::VISIBILITY_UNLISTED:
  94                  $visibilitymsg = get_string('unlisted', 'core_contentbank');
  95                  break;
  96              default:
  97                  throw new \moodle_exception('contentvisibilitynotfound', 'error', $returnurl, $content->get_visibility());
  98                  break;
  99          }
 100          $statusmsg = get_string($statusmsg, 'core_contentbank', $visibilitymsg);
 101      } else {
 102          $statusmsg = get_string($statusmsg, 'core_contentbank');
 103      }
 104      echo $OUTPUT->notification($statusmsg, 'notifysuccess');
 105  }
 106  if ($contenttype->can_access()) {
 107      $viewcontent = new core_contentbank\output\viewcontent($contenttype, $content);
 108      echo $OUTPUT->render($viewcontent);
 109  } else {
 110      $message = get_string('contenttypenoaccess', 'core_contentbank', $record->contenttype);
 111      echo $OUTPUT->notification($message, 'error');
 112  }
 113  
 114  echo $OUTPUT->footer();