Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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  require('../config.php');
  26  
  27  require_login();
  28  
  29  $id = required_param('id', PARAM_INT);
  30  $deletecontent = optional_param('deletecontent', null, PARAM_INT);
  31  
  32  $PAGE->requires->js_call_amd('core_contentbank/actions', 'init');
  33  
  34  $record = $DB->get_record('contentbank_content', ['id' => $id], '*', MUST_EXIST);
  35  $context = context::instance_by_id($record->contextid, MUST_EXIST);
  36  require_capability('moodle/contentbank:access', $context);
  37  
  38  $statusmsg = optional_param('statusmsg', '', PARAM_ALPHANUMEXT);
  39  $errormsg = optional_param('errormsg', '', PARAM_ALPHANUMEXT);
  40  
  41  $returnurl = new \moodle_url('/contentbank/index.php', ['contextid' => $context->id]);
  42  $plugin = core_plugin_manager::instance()->get_plugin_info($record->contenttype);
  43  if (!$plugin || !$plugin->is_enabled()) {
  44      print_error('unsupported', 'core_contentbank', $returnurl);
  45  }
  46  
  47  $title = get_string('contentbank');
  48  \core_contentbank\helper::get_page_ready($context, $title, true);
  49  if ($PAGE->course) {
  50      require_login($PAGE->course->id);
  51  }
  52  
  53  $PAGE->set_url(new \moodle_url('/contentbank/view.php', ['id' => $id]));
  54  $PAGE->set_context($context);
  55  $PAGE->navbar->add($record->name);
  56  $PAGE->set_heading($record->name);
  57  $title .= ": ".$record->name;
  58  $PAGE->set_title($title);
  59  $PAGE->set_pagetype('contentbank');
  60  
  61  $contenttypeclass = "\\$record->contenttype\\contenttype";
  62  $contentclass = "\\$record->contenttype\\content";
  63  if (!class_exists($contenttypeclass) || !class_exists($contentclass)) {
  64      print_error('contenttypenotfound', 'error', $returnurl, $record->contenttype);
  65  }
  66  $contenttype = new $contenttypeclass($context);
  67  $content = new $contentclass($record);
  68  
  69  // Create the cog menu with all the secondary actions, such as delete, rename...
  70  $actionmenu = new action_menu();
  71  $actionmenu->set_alignment(action_menu::TR, action_menu::BR);
  72  if ($contenttype->can_manage($content)) {
  73      // Add the rename content item to the menu.
  74      $attributes = [
  75          'data-action' => 'renamecontent',
  76          'data-contentname' => $content->get_name(),
  77          'data-contentid' => $content->get_id(),
  78      ];
  79      $actionmenu->add_secondary_action(new action_menu_link(
  80          new moodle_url('#'),
  81          new pix_icon('e/styleparagraph', get_string('rename')),
  82          get_string('rename'),
  83          false,
  84          $attributes
  85      ));
  86  }
  87  if ($contenttype->can_delete($content)) {
  88      // Add the delete content item to the menu.
  89      $attributes = [
  90                  'data-action' => 'deletecontent',
  91                  'data-contentname' => $content->get_name(),
  92                  'data-contentid' => $content->get_id(),
  93                  'data-contextid' => $context->id,
  94              ];
  95      $actionmenu->add_secondary_action(new action_menu_link(
  96          new moodle_url('#'),
  97          new pix_icon('t/delete', get_string('delete')),
  98          get_string('delete'),
  99          false,
 100          $attributes
 101      ));
 102  }
 103  
 104  // Add the cog menu to the header.
 105  $PAGE->add_header_action(html_writer::div(
 106      $OUTPUT->render($actionmenu),
 107      'd-print-none',
 108      ['id' => 'region-main-settings-menu']
 109  ));
 110  
 111  echo $OUTPUT->header();
 112  
 113  // If needed, display notifications.
 114  if ($errormsg !== '' && get_string_manager()->string_exists($errormsg, 'core_contentbank')) {
 115      $errormsg = get_string($errormsg, 'core_contentbank');
 116      echo $OUTPUT->notification($errormsg);
 117  } else if ($statusmsg !== '' && get_string_manager()->string_exists($statusmsg, 'core_contentbank')) {
 118      $statusmsg = get_string($statusmsg, 'core_contentbank');
 119      echo $OUTPUT->notification($statusmsg, 'notifysuccess');
 120  }
 121  if ($contenttype->can_access()) {
 122      $viewcontent = new core_contentbank\output\viewcontent($contenttype, $content);
 123      echo $OUTPUT->render($viewcontent);
 124  } else {
 125      $message = get_string('contenttypenoaccess', 'core_contentbank', $record->contenttype);
 126      echo $OUTPUT->notification($message, 'error');
 127  }
 128  
 129  echo $OUTPUT->footer();