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 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  require_once(__DIR__ . '/../config.php');
  18  require_once($CFG->dirroot . '/repository/lib.php');
  19  require_once($CFG->libdir . '/adminlib.php');
  20  
  21  $repository       = optional_param('repos', '', PARAM_ALPHANUMEXT);
  22  $action           = optional_param('action', '', PARAM_ALPHANUMEXT);
  23  $sure             = optional_param('sure', '', PARAM_ALPHA);
  24  $downloadcontents = optional_param('downloadcontents', false, PARAM_BOOL);
  25  
  26  $display = true; // fall through to normal display
  27  
  28  $pagename = 'managerepositories';
  29  
  30  if ($action == 'edit') {
  31      $pagename = 'repositorysettings' . $repository;
  32  } else if ($action == 'delete') {
  33      $pagename = 'repositorydelete';
  34  } else if (($action == 'newon') || ($action == 'newoff')) {
  35      $pagename = 'repositorynew';
  36  }
  37  
  38  // Need to remember this for form
  39  $formaction = $action;
  40  
  41  // Check what visibility to show the new repository
  42  if ($action == 'newon') {
  43      $action = 'new';
  44      $visible = true;
  45  } else if ($action == 'newoff') {
  46      $action = 'new';
  47      $visible = false;
  48  }
  49  
  50  admin_externalpage_setup($pagename);
  51  
  52  $sesskeyurl = $CFG->wwwroot.'/'.$CFG->admin.'/repository.php?sesskey=' . sesskey();
  53  $baseurl    = $CFG->wwwroot.'/'.$CFG->admin.'/repository.php';
  54  
  55  $configstr  = get_string('manage', 'repository');
  56  
  57  $return = true;
  58  
  59  if (!empty($action)) {
  60      require_sesskey();
  61  }
  62  
  63  /**
  64   * Helper function that generates a moodle_url object
  65   * relevant to the repository
  66   */
  67  function repository_action_url($repository) {
  68      global $baseurl;
  69      return new moodle_url($baseurl, array('sesskey'=>sesskey(), 'repos'=>$repository));
  70  }
  71  
  72  if (($action == 'edit') || ($action == 'new')) {
  73      $pluginname = '';
  74      if ($action == 'edit') {
  75          $repositorytype = repository::get_type_by_typename($repository);
  76          $classname = 'repository_' . $repositorytype->get_typename();
  77          $configs = call_user_func(array($classname, 'get_type_option_names'));
  78          $plugin = $repositorytype->get_typename();
  79          // looking for instance to edit plugin name
  80          $instanceoptions = call_user_func(array($classname, 'get_instance_option_names'));
  81          if (empty($instanceoptions)) {
  82              $params = array();
  83              $params['type'] = $plugin;
  84              $instances = repository::get_instances($params);
  85              if ($instance = array_pop($instances)) {
  86                  // use the one form db record
  87                  $pluginname = $instance->instance->name;
  88              }
  89          }
  90  
  91      } else {
  92          $repositorytype = null;
  93          $plugin = $repository;
  94          $typeid = $repository;
  95      }
  96      $PAGE->set_pagetype('admin-repository-' . $plugin);
  97      // display the edit form for this instance
  98      $mform = new repository_type_form('', array('pluginname'=>$pluginname, 'plugin' => $plugin, 'instance' => $repositorytype, 'action' => $formaction));
  99      $fromform = $mform->get_data();
 100  
 101      //detect if we create a new type without config (in this case if don't want to display a setting page during creation)
 102      $nosettings = false;
 103      if ($action == 'new') {
 104          $adminconfignames = repository::static_function($repository, 'get_type_option_names');
 105          $nosettings = empty($adminconfignames);
 106      }
 107      // end setup, begin output
 108  
 109      if ($mform->is_cancelled()){
 110          redirect($baseurl);
 111      } else if (!empty($fromform) || $nosettings) {
 112          require_sesskey();
 113          if ($action == 'edit') {
 114              $settings = array();
 115              foreach($configs as $config) {
 116                  if (!empty($fromform->$config)) {
 117                      $settings[$config] = $fromform->$config;
 118                  } else {
 119                      // if the config name is not appear in $fromform
 120                      // empty this config value
 121                      $settings[$config] = '';
 122                  }
 123              }
 124              $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names');
 125              if (!empty($instanceoptionnames)) {
 126                  if (property_exists($fromform, 'enablecourseinstances')) {
 127                      $settings['enablecourseinstances'] = $fromform->enablecourseinstances;
 128                  }
 129                  else {
 130                      $settings['enablecourseinstances'] = 0;
 131                  }
 132                  if (property_exists($fromform, 'enableuserinstances')) {
 133                      $settings['enableuserinstances'] = $fromform->enableuserinstances;
 134                  }
 135                  else {
 136                      $settings['enableuserinstances'] = 0;
 137                  }
 138              }
 139              $success = $repositorytype->update_options($settings);
 140          } else {
 141              $type = new repository_type($plugin, (array)$fromform, $visible);
 142              $success = true;
 143              if (!$repoid = $type->create()) {
 144                  $success = false;
 145              }
 146              $data = data_submitted();
 147          }
 148          if ($success) {
 149              // configs saved
 150              core_plugin_manager::reset_caches();
 151              redirect($baseurl);
 152          } else {
 153              print_error('instancenotsaved', 'repository', $baseurl);
 154          }
 155          exit;
 156      } else {
 157          echo $OUTPUT->header();
 158          echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin));
 159          $displaysettingform = true;
 160          if ($action == 'edit') {
 161              $typeoptionnames = repository::static_function($repository, 'get_type_option_names');
 162              $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names');
 163              if (empty($typeoptionnames) && empty($instanceoptionnames)) {
 164                  $displaysettingform = false;
 165              }
 166          }
 167          if ($displaysettingform){
 168              $OUTPUT->box_start();
 169              $mform->display();
 170              $OUTPUT->box_end();
 171          }
 172          $return = false;
 173  
 174          // Display instances list and creation form
 175          if ($action == 'edit') {
 176              $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names');
 177              if (!empty($instanceoptionnames)) {
 178                  repository::display_instances_list(context_system::instance(), $repository);
 179              }
 180          }
 181      }
 182  } else if ($action == 'show') {
 183      if (!confirm_sesskey()) {
 184          print_error('confirmsesskeybad', '', $baseurl);
 185      }
 186      $repositorytype = repository::get_type_by_typename($repository);
 187      if (empty($repositorytype)) {
 188          print_error('invalidplugin', 'repository', '', $repository);
 189      }
 190      $repositorytype->update_visibility(true);
 191      core_plugin_manager::reset_caches();
 192      $return = true;
 193  } else if ($action == 'hide') {
 194      if (!confirm_sesskey()) {
 195          print_error('confirmsesskeybad', '', $baseurl);
 196      }
 197      $repositorytype = repository::get_type_by_typename($repository);
 198      if (empty($repositorytype)) {
 199          print_error('invalidplugin', 'repository', '', $repository);
 200      }
 201      $repositorytype->update_visibility(false);
 202      core_plugin_manager::reset_caches();
 203      $return = true;
 204  } else if ($action == 'delete') {
 205      $repositorytype = repository::get_type_by_typename($repository);
 206      if ($sure) {
 207          $PAGE->set_pagetype('admin-repository-' . $repository);
 208          if (!confirm_sesskey()) {
 209              print_error('confirmsesskeybad', '', $baseurl);
 210          }
 211  
 212          if ($repositorytype->delete($downloadcontents)) {
 213              core_plugin_manager::reset_caches();
 214              redirect($baseurl);
 215          } else {
 216              print_error('instancenotdeleted', 'repository', $baseurl);
 217          }
 218          exit;
 219      } else {
 220          echo $OUTPUT->header();
 221  
 222          $message = get_string('confirmremove', 'repository', $repositorytype->get_readablename());
 223  
 224          $output = $OUTPUT->box_start('generalbox', 'notice');
 225          $output .= html_writer::tag('p', $message);
 226  
 227          $removeurl = new moodle_url($sesskeyurl);
 228          $removeurl->params(array(
 229              'action' =>'delete',
 230              'repos' => $repository,
 231              'sure' => 'yes',
 232          ));
 233  
 234          $removeanddownloadurl = new moodle_url($sesskeyurl);
 235          $removeanddownloadurl->params(array(
 236              'action' =>'delete',
 237              'repos'=> $repository,
 238              'sure' => 'yes',
 239              'downloadcontents' => 1,
 240          ));
 241  
 242          $output .= $OUTPUT->single_button($removeurl, get_string('continueuninstall', 'repository'));
 243          $output .= $OUTPUT->single_button($removeanddownloadurl, get_string('continueuninstallanddownload', 'repository'));
 244          $output .= $OUTPUT->single_button($baseurl, get_string('cancel'));
 245          $output .= $OUTPUT->box_end();
 246  
 247          echo $output;
 248  
 249          $return = false;
 250      }
 251  } else if ($action == 'moveup') {
 252      $repositorytype = repository::get_type_by_typename($repository);
 253      $repositorytype->move_order('up');
 254  } else if ($action == 'movedown') {
 255      $repositorytype = repository::get_type_by_typename($repository);
 256      $repositorytype->move_order('down');
 257  } else {
 258      // If page is loaded directly
 259      echo $OUTPUT->header();
 260      echo $OUTPUT->heading(get_string('manage', 'repository'));
 261  
 262      // Get strings that are used
 263      $strshow = get_string('on', 'repository');
 264      $strhide = get_string('off', 'repository');
 265      $strdelete = get_string('disabled', 'repository');
 266      $struninstall = get_string('uninstallplugin', 'core_admin');
 267  
 268      $actionchoicesforexisting = array(
 269          'show' => $strshow,
 270          'hide' => $strhide,
 271          'delete' => $strdelete
 272      );
 273  
 274      $actionchoicesfornew = array(
 275          'newon' => $strshow,
 276          'newoff' => $strhide,
 277          'delete' => $strdelete
 278      );
 279  
 280      $output = '';
 281      $output .= $OUTPUT->box_start('generalbox');
 282  
 283      // Set strings that are used multiple times
 284      $settingsstr = get_string('settings');
 285      $disablestr = get_string('disable');
 286  
 287      // Table to list plug-ins
 288      $table = new html_table();
 289      $table->head = array(get_string('name'), get_string('isactive', 'repository'), get_string('order'), $settingsstr, $struninstall);
 290  
 291      $table->colclasses = array('leftalign', 'centeralign', 'centeralign', 'centeralign', 'centeralign', 'centeralign');
 292      $table->id = 'repositoriessetting';
 293      $table->data = array();
 294      $table->attributes['class'] = 'admintable generaltable';
 295  
 296      // Get list of used plug-ins
 297      $repositorytypes = repository::get_types();
 298      // Array to store plugins being used
 299      $alreadyplugins = array();
 300      if (!empty($repositorytypes)) {
 301          $totalrepositorytypes = count($repositorytypes);
 302          $updowncount = 1;
 303          foreach ($repositorytypes as $i) {
 304              $settings = '';
 305              $typename = $i->get_typename();
 306              // Display edit link only if you can config the type or if it has multiple instances (e.g. has instance config)
 307              $typeoptionnames = repository::static_function($typename, 'get_type_option_names');
 308              $instanceoptionnames = repository::static_function($typename, 'get_instance_option_names');
 309  
 310              if (!empty($typeoptionnames) || !empty($instanceoptionnames)) {
 311                  // Calculate number of instances in order to display them for the Moodle administrator
 312                  if (!empty($instanceoptionnames)) {
 313                      $params = array();
 314                      $params['context'] = array(context_system::instance());
 315                      $params['onlyvisible'] = false;
 316                      $params['type'] = $typename;
 317                      $admininstancenumber = count(repository::static_function($typename, 'get_instances', $params));
 318                      // site instances
 319                      $admininstancenumbertext = get_string('instancesforsite', 'repository', $admininstancenumber);
 320                      $params['context'] = array();
 321                      $instances = repository::static_function($typename, 'get_instances', $params);
 322                      $courseinstances = array();
 323                      $userinstances = array();
 324  
 325                      foreach ($instances as $instance) {
 326                          $repocontext = context::instance_by_id($instance->instance->contextid);
 327                          if ($repocontext->contextlevel == CONTEXT_COURSE) {
 328                              $courseinstances[] = $instance;
 329                          } else if ($repocontext->contextlevel == CONTEXT_USER) {
 330                              $userinstances[] = $instance;
 331                          }
 332                      }
 333                      // course instances
 334                      $instancenumber = count($courseinstances);
 335                      $courseinstancenumbertext = get_string('instancesforcourses', 'repository', $instancenumber);
 336  
 337                      // user private instances
 338                      $instancenumber =  count($userinstances);
 339                      $userinstancenumbertext = get_string('instancesforusers', 'repository', $instancenumber);
 340                  } else {
 341                      $admininstancenumbertext = "";
 342                      $courseinstancenumbertext = "";
 343                      $userinstancenumbertext = "";
 344                  }
 345  
 346                  $settings .= '<a href="' . $sesskeyurl . '&amp;action=edit&amp;repos=' . $typename . '">' . $settingsstr .'</a>';
 347  
 348                  $settings .= $OUTPUT->container_start('mdl-left');
 349                  $settings .= '<br/>';
 350                  $settings .= $admininstancenumbertext;
 351                  $settings .= '<br/>';
 352                  $settings .= $courseinstancenumbertext;
 353                  $settings .= '<br/>';
 354                  $settings .= $userinstancenumbertext;
 355                  $settings .= $OUTPUT->container_end();
 356              }
 357              // Get the current visibility
 358              if ($i->get_visible()) {
 359                  $currentaction = 'show';
 360              } else {
 361                  $currentaction = 'hide';
 362              }
 363  
 364              $select = new single_select(repository_action_url($typename, 'repos'), 'action', $actionchoicesforexisting, $currentaction, null, 'applyto' . basename($typename));
 365              $select->set_label(get_string('action'), array('class' => 'accesshide'));
 366              // Display up/down link
 367              $updown = '';
 368              $spacer = $OUTPUT->spacer(array('height'=>15, 'width'=>15)); // should be done with CSS instead
 369  
 370              if ($updowncount > 1) {
 371                  $updown .= "<a href=\"$sesskeyurl&amp;action=moveup&amp;repos=".$typename."\">";
 372                  $updown .= $OUTPUT->pix_icon('t/up', get_string('moveup')) . "</a>&nbsp;";
 373              }
 374              else {
 375                  $updown .= $spacer;
 376              }
 377              if ($updowncount < $totalrepositorytypes) {
 378                  $updown .= "<a href=\"$sesskeyurl&amp;action=movedown&amp;repos=".$typename."\">";
 379                  $updown .= $OUTPUT->pix_icon('t/down', get_string('movedown')) . "</a>&nbsp;";
 380              }
 381              else {
 382                  $updown .= $spacer;
 383              }
 384  
 385              $updowncount++;
 386  
 387              $uninstall = '';
 388              if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url('repository_' . $typename, 'manage')) {
 389                  $uninstall = html_writer::link($uninstallurl, $struninstall);
 390              }
 391  
 392              $table->data[] = array($i->get_readablename(), $OUTPUT->render($select), $updown, $settings, $uninstall);
 393  
 394              if (!in_array($typename, $alreadyplugins)) {
 395                  $alreadyplugins[] = $typename;
 396              }
 397          }
 398      }
 399  
 400      // Get all the plugins that exist on disk
 401      $plugins = core_component::get_plugin_list('repository');
 402      if (!empty($plugins)) {
 403          foreach ($plugins as $plugin => $dir) {
 404              // Check that it has not already been listed
 405              if (!in_array($plugin, $alreadyplugins)) {
 406                  $select = new single_select(repository_action_url($plugin, 'repos'), 'action', $actionchoicesfornew, 'delete', null, 'applyto' . basename($plugin));
 407                  $select->set_label(get_string('action'), array('class' => 'accesshide'));
 408                  $uninstall = '';
 409                  if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url('repository_' . $plugin, 'manage')) {
 410                      $uninstall = html_writer::link($uninstallurl, $struninstall);
 411                  }
 412                  $table->data[] = array(get_string('pluginname', 'repository_'.$plugin), $OUTPUT->render($select), '', '', $uninstall);
 413              }
 414          }
 415      }
 416  
 417      $output .= html_writer::table($table);
 418      $output .= $OUTPUT->box_end();
 419      print $output;
 420      $return = false;
 421  }
 422  
 423  if ($return) {
 424      redirect($baseurl);
 425  }
 426  echo $OUTPUT->footer();