Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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