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 402] [Versions 401 and 403]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  
  19  /**
  20   * This file is used to manage repositories
  21   *
  22   * @since Moodle 2.0
  23   * @package    core
  24   * @subpackage repository
  25   * @copyright  2009 Dongsheng Cai <dongsheng@moodle.com>
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  
  29  require_once(__DIR__ . '/../config.php');
  30  require_once($CFG->dirroot . '/repository/lib.php');
  31  
  32  $edit    = optional_param('edit', 0, PARAM_INT);
  33  $new     = optional_param('new', '', PARAM_ALPHANUMEXT);
  34  $delete  = optional_param('delete', 0, PARAM_INT);
  35  $sure    = optional_param('sure', '', PARAM_ALPHA);
  36  $contextid = optional_param('contextid', 0, PARAM_INT);
  37  $usercourseid = optional_param('usercourseid', SITEID, PARAM_INT);  // Extra: used for user context only
  38  
  39  $url = new moodle_url('/repository/manage_instances.php');
  40  
  41  $baseurl = new moodle_url('/repository/manage_instances.php');
  42  $baseurl->param('sesskey', sesskey());
  43  
  44  if ($edit){
  45      $url->param('edit', $edit);
  46      $pagename = 'repositoryinstanceedit';
  47  } else if ($delete) {
  48      $url->param('delete', $delete);
  49      $pagename = 'repositorydelete';
  50  } else if ($new) {
  51      $url->param('new', $new);
  52      $pagename = 'repositoryinstancenew';
  53  } else {
  54      $pagename = 'repositorylist';
  55  }
  56  
  57  if ($sure !== '') {
  58      $url->param('sure', $sure);
  59  }
  60  if ($contextid !== 0) {
  61      $url->param('contextid', $contextid);
  62      $baseurl->param('contextid', $contextid);
  63  }
  64  if ($usercourseid != SITEID) {
  65      $url->param('usercourseid', $usercourseid);
  66  }
  67  
  68  $context = context::instance_by_id($contextid);
  69  
  70  $PAGE->set_url($url);
  71  $PAGE->set_context($context);
  72  $PAGE->set_pagelayout('standard');
  73  
  74  /// Security: make sure we're allowed to do this operation
  75  if ($context->contextlevel == CONTEXT_COURSE) {
  76      $pagename = get_string("repositorycourse",'repository');
  77  
  78      if ( !$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
  79          throw new \moodle_exception('invalidcourseid');
  80      }
  81      require_login($course, false);
  82      // If the user is allowed to edit this course, he's allowed to edit list of repository instances
  83      require_capability('moodle/course:update',  $context);
  84  
  85  
  86  } else if ($context->contextlevel == CONTEXT_USER) {
  87      require_login();
  88      $pagename = get_string('manageinstances', 'repository');
  89      //is the user looking at its own repository instances
  90      if ($USER->id != $context->instanceid){
  91          throw new \moodle_exception('notyourinstances', 'repository');
  92      }
  93      $user = $USER;
  94  } else {
  95      throw new \moodle_exception('invalidcontext');
  96  }
  97  
  98  /// Security: we cannot perform any action if the type is not visible or if the context has been disabled
  99  if (!empty($new) && empty($edit)){
 100      $type = repository::get_type_by_typename($new);
 101  } else if (!empty($edit)){
 102      $instance = repository::get_repository_by_id($edit, $context->id);
 103      $type = repository::get_type_by_id($instance->options['typeid']);
 104  } else if (!empty($delete)){
 105      $instance = repository::get_repository_by_id($delete, $context->id);
 106      $type = repository::get_type_by_id($instance->options['typeid']);
 107  }
 108  
 109  if (isset($type)) {
 110      if (!$type->get_visible()) {
 111          throw new \moodle_exception('typenotvisible', 'repository', $baseurl);
 112      }
 113      // Prevents the user from creating/editing an instance if the repository is not visible in
 114      // this context OR if the user does not have the capability to view this repository in this context.
 115      $canviewrepository = has_capability('repository/'.$type->get_typename().':view', $context);
 116      if (!$type->get_contextvisibility($context) || !$canviewrepository) {
 117          throw new \moodle_exception('usercontextrepositorydisabled', 'repository', $baseurl);
 118      }
 119  }
 120  
 121  // We have an instance when we are going to edit, or delete. Several checks need to be done!
 122  if (!empty($instance)) {
 123      // The context passed MUST match the context of the repository. And as both have to be
 124      // similar, this also ensures that the context is either a user one, or a course one.
 125      if ($instance->instance->contextid != $context->id) {
 126          throw new \moodle_exception('invalidcontext');
 127      }
 128      if ($instance->readonly) {
 129          // Cannot edit, or delete a readonly instance.
 130          throw new repository_exception('readonlyinstance', 'repository');
 131      } else if (!$instance->can_be_edited_by_user()) {
 132          // The user has to have the right to edit the instance.
 133          throw new repository_exception('nopermissiontoaccess', 'repository');
 134      }
 135  }
 136  
 137  // Create navigation links.
 138  if (!empty($course)) {
 139      $pageheading = $course->fullname;
 140  } else {
 141      $pageheading = $pagename;
 142  }
 143  
 144  // Display page header.
 145  $PAGE->set_title($pagename);
 146  $PAGE->set_heading($pageheading);
 147  
 148  $return = true;
 149  if (!empty($edit) || !empty($new)) {
 150      if (!empty($edit)) {
 151          $instancetype = repository::get_type_by_id($instance->options['typeid']);
 152          $classname = 'repository_' . $instancetype->get_typename();
 153          $configs  = $instance->get_instance_option_names();
 154          $plugin = $instancetype->get_typename();
 155          $typeid = $instance->options['typeid'];
 156      } else {
 157          $plugin = $new;
 158          $typeid = $new;
 159          $instance = null;
 160      }
 161  
 162  /// Create edit form for this instance
 163      $mform = new repository_instance_form('', array('plugin' => $plugin, 'typeid' => $typeid,'instance' => $instance, 'contextid' => $contextid));
 164  
 165  /// Process the form data if any, or display
 166      if ($mform->is_cancelled()){
 167          redirect($baseurl);
 168          exit;
 169  
 170      } else if ($fromform = $mform->get_data()){
 171          if (!confirm_sesskey()) {
 172              throw new \moodle_exception('confirmsesskeybad', '', $baseurl);
 173          }
 174          if ($edit) {
 175              $settings = array();
 176              $settings['name'] = $fromform->name;
 177              foreach($configs as $config) {
 178                  $settings[$config] = isset($fromform->$config) ? $fromform->$config : null;
 179              }
 180              $success = $instance->set_option($settings);
 181          } else {
 182              $success = repository::static_function($plugin, 'create', $plugin, 0, context::instance_by_id($contextid), $fromform);
 183              $data = data_submitted();
 184          }
 185          if ($success) {
 186              $savedstr = get_string('configsaved', 'repository');
 187              redirect($baseurl);
 188          } else {
 189              throw new \moodle_exception('instancenotsaved', 'repository', $baseurl);
 190          }
 191          exit;
 192      } else {     // Display the form
 193          echo $OUTPUT->header();
 194          echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin));
 195          $OUTPUT->box_start();
 196          $mform->display();
 197          $OUTPUT->box_end();
 198          $return = false;
 199      }
 200  } else if (!empty($delete)) {
 201      if ($sure) {
 202          if (!confirm_sesskey()) {
 203              throw new \moodle_exception('confirmsesskeybad', '', $baseurl);
 204          }
 205          if ($instance->delete()) {
 206              $deletedstr = get_string('instancedeleted', 'repository');
 207              redirect($baseurl, $deletedstr, 3);
 208          } else {
 209              throw new \moodle_exception('instancenotdeleted', 'repository', $baseurl);
 210          }
 211          exit;
 212      }
 213      echo $OUTPUT->header();
 214      $formcontinue = new single_button(new moodle_url($baseurl, array('delete' => $delete, 'sure' => 'yes')), get_string('yes'));
 215      $formcancel = new single_button($baseurl, get_string('no'));
 216      echo $OUTPUT->confirm(get_string('confirmdelete', 'repository', $instance->name), $formcontinue, $formcancel);
 217      $return = false;
 218  } else {
 219      echo $OUTPUT->header();
 220      repository::display_instances_list($context);
 221      $return = false;
 222  }
 223  
 224  if (!empty($return)) {
 225      redirect($baseurl);
 226  }
 227  
 228  echo $OUTPUT->footer();