Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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   * Create or update contents through the specific content type editor
  19   *
  20   * @package    core_contentbank
  21   * @copyright  2020 Victor Deniz <victor@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 = required_param('contextid', PARAM_INT);
  30  $pluginname = required_param('plugin', PARAM_PLUGIN);
  31  $id = optional_param('id', null, PARAM_INT);
  32  $library = optional_param('library', null, PARAM_RAW);
  33  
  34  $context = context::instance_by_id($contextid, MUST_EXIST);
  35  
  36  $cb = new \core_contentbank\contentbank();
  37  if (!$cb->is_context_allowed($context)) {
  38      print_error('contextnotallowed', 'core_contentbank');
  39  }
  40  
  41  require_capability('moodle/contentbank:access', $context);
  42  
  43  $returnurl = new \moodle_url('/contentbank/view.php', ['id' => $id]);
  44  
  45  if (!empty($id)) {
  46      $record = $DB->get_record('contentbank_content', ['id' => $id], '*', MUST_EXIST);
  47      $contentclass = "$record->contenttype\\content";
  48      $content = new $contentclass($record);
  49      // Set the heading title.
  50      $heading = $content->get_name();
  51      // The content type of the content overwrites the pluginname param value.
  52      $contenttypename = $content->get_content_type();
  53  } else {
  54      $contenttypename = "contenttype_$pluginname";
  55      $heading = get_string('addinganew', 'moodle', get_string('description', $contenttypename));
  56      $content = null;
  57  }
  58  
  59  // Check plugin is enabled.
  60  $plugin = core_plugin_manager::instance()->get_plugin_info($contenttypename);
  61  if (!$plugin || !$plugin->is_enabled()) {
  62      print_error('unsupported', 'core_contentbank', $returnurl);
  63  }
  64  
  65  // Create content type instance.
  66  $contenttypeclass = "$contenttypename\\contenttype";
  67  if (class_exists($contenttypeclass)) {
  68      $contenttype = new $contenttypeclass($context);
  69  } else {
  70      print_error('unsupported', 'core_contentbank', $returnurl);
  71  }
  72  
  73  // Checks the user can edit this content and content type.
  74  if (!$contenttype->can_edit($content)) {
  75      print_error('contenttypenoedit', 'core_contentbank', $returnurl);
  76  }
  77  
  78  $values = [
  79      'contextid' => $contextid,
  80      'plugin' => $pluginname,
  81      'id' => $id,
  82      'library' => $library
  83  ];
  84  
  85  $title = get_string('contentbank');
  86  \core_contentbank\helper::get_page_ready($context, $title, true);
  87  if ($PAGE->course) {
  88      require_login($PAGE->course->id);
  89  }
  90  
  91  $PAGE->set_url(new \moodle_url('/contentbank/edit.php', $values));
  92  $PAGE->set_context($context);
  93  $PAGE->navbar->add(get_string('edit'));
  94  $PAGE->set_title($title);
  95  
  96  $PAGE->set_heading($heading);
  97  
  98  // Instantiate the content type form.
  99  $editorclass = "$contenttypename\\form\\editor";
 100  if (!class_exists($editorclass)) {
 101      print_error('noformdesc');
 102  }
 103  
 104  $editorform = new $editorclass(null, $values);
 105  
 106  if ($editorform->is_cancelled()) {
 107      if (empty($id)) {
 108          $returnurl = new \moodle_url('/contentbank/index.php', ['contextid' => $context->id]);
 109      }
 110      redirect($returnurl);
 111  } else if ($data = $editorform->get_data()) {
 112      $id = $editorform->save_content($data);
 113      // Just in case we've created a new content.
 114      $returnurl->param('id', $id);
 115      redirect($returnurl);
 116  }
 117  
 118  echo $OUTPUT->header();
 119  $editorform->display();
 120  echo $OUTPUT->footer();