Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 402 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   * Main course enrolment management UI.
  19   *
  20   * @package    core_enrol
  21   * @copyright  2010 Petr Skoda {@link http://skodak.org}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require('../config.php');
  26  
  27  $id         = required_param('id', PARAM_INT); // course id
  28  $action     = optional_param('action', '', PARAM_ALPHANUMEXT);
  29  $instanceid = optional_param('instance', 0, PARAM_INT);
  30  $confirm    = optional_param('confirm', 0, PARAM_BOOL);
  31  $confirm2   = optional_param('confirm2', 0, PARAM_BOOL);
  32  
  33  $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
  34  $context = context_course::instance($course->id, MUST_EXIST);
  35  
  36  if ($course->id == SITEID) {
  37      redirect("$CFG->wwwroot/");
  38  }
  39  
  40  require_login($course);
  41  require_capability('moodle/course:enrolreview', $context);
  42  
  43  $canconfig = has_capability('moodle/course:enrolconfig', $context);
  44  
  45  $PAGE->set_url('/enrol/instances.php', array('id'=>$course->id));
  46  $PAGE->set_pagelayout('admin');
  47  $PAGE->set_title(get_string('enrolmentinstances', 'enrol'));
  48  $PAGE->set_heading($course->fullname);
  49  
  50  $instances = enrol_get_instances($course->id, false);
  51  $plugins   = enrol_get_plugins(false);
  52  
  53  if ($canconfig and $action and confirm_sesskey()) {
  54      if (isset($instances[$instanceid]) and isset($plugins[$instances[$instanceid]->enrol])) {
  55          if ($action === 'up') {
  56              $order = array_keys($instances);
  57              $order = array_flip($order);
  58              $pos = $order[$instanceid];
  59              if ($pos > 0) {
  60                  $switch = $pos - 1;
  61                  $resorted = array_values($instances);
  62                  $temp = $resorted[$pos];
  63                  $resorted[$pos] = $resorted[$switch];
  64                  $resorted[$switch] = $temp;
  65                  // now update db sortorder
  66                  foreach ($resorted as $sortorder=>$instance) {
  67                      if ($instance->sortorder != $sortorder) {
  68                          $instance->sortorder = $sortorder;
  69                          $DB->update_record('enrol', $instance);
  70                      }
  71                  }
  72              }
  73              redirect($PAGE->url);
  74  
  75          } else if ($action === 'down') {
  76              $order = array_keys($instances);
  77              $order = array_flip($order);
  78              $pos = $order[$instanceid];
  79              if ($pos < count($instances) - 1) {
  80                  $switch = $pos + 1;
  81                  $resorted = array_values($instances);
  82                  $temp = $resorted[$pos];
  83                  $resorted[$pos] = $resorted[$switch];
  84                  $resorted[$switch] = $temp;
  85                  // now update db sortorder
  86                  foreach ($resorted as $sortorder=>$instance) {
  87                      if ($instance->sortorder != $sortorder) {
  88                          $instance->sortorder = $sortorder;
  89                          $DB->update_record('enrol', $instance);
  90                      }
  91                  }
  92              }
  93              redirect($PAGE->url);
  94  
  95          } else if ($action === 'delete') {
  96              $instance = $instances[$instanceid];
  97              $plugin = $plugins[$instance->enrol];
  98  
  99              if ($plugin->can_delete_instance($instance)) {
 100                  if ($confirm) {
 101                      if (enrol_accessing_via_instance($instance)) {
 102                          if (!$confirm2) {
 103                              $yesurl = new moodle_url('/enrol/instances.php',
 104                                                       array('id' => $course->id,
 105                                                             'action' => 'delete',
 106                                                             'instance' => $instance->id,
 107                                                             'confirm' => 1,
 108                                                             'confirm2' => 1,
 109                                                             'sesskey' => sesskey()));
 110                              $displayname = $plugin->get_instance_name($instance);
 111                              $message = markdown_to_html(get_string('deleteinstanceconfirmself',
 112                                                                     'enrol',
 113                                                                     array('name' => $displayname)));
 114                              echo $OUTPUT->header();
 115                              echo $OUTPUT->confirm($message, $yesurl, $PAGE->url);
 116                              echo $OUTPUT->footer();
 117                              die();
 118                          }
 119                      }
 120                      $plugin->delete_instance($instance);
 121                      redirect($PAGE->url);
 122                  }
 123  
 124                  echo $OUTPUT->header();
 125                  $yesurl = new moodle_url('/enrol/instances.php',
 126                                           array('id' => $course->id,
 127                                                 'action' => 'delete',
 128                                                 'instance' => $instance->id,
 129                                                 'confirm' => 1,
 130                                                 'sesskey' => sesskey()));
 131                  $displayname = $plugin->get_instance_name($instance);
 132                  $users = $DB->count_records('user_enrolments', array('enrolid' => $instance->id));
 133                  if ($users) {
 134                      $message = markdown_to_html(get_string('deleteinstanceconfirm', 'enrol',
 135                                                             array('name' => $displayname,
 136                                                                   'users' => $users)));
 137                  } else {
 138                      $message = markdown_to_html(get_string('deleteinstancenousersconfirm', 'enrol',
 139                                                             array('name' => $displayname)));
 140                  }
 141                  echo $OUTPUT->confirm($message, $yesurl, $PAGE->url);
 142                  echo $OUTPUT->footer();
 143                  die();
 144              }
 145  
 146          } else if ($action === 'disable') {
 147              $instance = $instances[$instanceid];
 148              $plugin = $plugins[$instance->enrol];
 149              if ($plugin->can_hide_show_instance($instance)) {
 150                  if ($instance->status != ENROL_INSTANCE_DISABLED) {
 151                      if (enrol_accessing_via_instance($instance)) {
 152                          if (!$confirm2) {
 153                              $yesurl = new moodle_url('/enrol/instances.php',
 154                                                       array('id' => $course->id,
 155                                                             'action' => 'disable',
 156                                                             'instance' => $instance->id,
 157                                                             'confirm2' => 1,
 158                                                             'sesskey' => sesskey()));
 159                              $displayname = $plugin->get_instance_name($instance);
 160                              $message = markdown_to_html(get_string('disableinstanceconfirmself',
 161                                                          'enrol',
 162                                                          array('name' => $displayname)));
 163                              echo $OUTPUT->header();
 164                              echo $OUTPUT->confirm($message, $yesurl, $PAGE->url);
 165                              echo $OUTPUT->footer();
 166                              die();
 167                          }
 168                      }
 169                      $plugin->update_status($instance, ENROL_INSTANCE_DISABLED);
 170                      redirect($PAGE->url);
 171                  }
 172              }
 173  
 174          } else if ($action === 'enable') {
 175              $instance = $instances[$instanceid];
 176              $plugin = $plugins[$instance->enrol];
 177              if ($plugin->can_hide_show_instance($instance)) {
 178                  if ($instance->status != ENROL_INSTANCE_ENABLED) {
 179                      $plugin->update_status($instance, ENROL_INSTANCE_ENABLED);
 180                      redirect($PAGE->url);
 181                  }
 182              }
 183          }
 184      }
 185  }
 186  
 187  
 188  echo $OUTPUT->header();
 189  echo $OUTPUT->render_participants_tertiary_nav($course);
 190  echo $OUTPUT->heading(get_string('enrolmentinstances', 'enrol'));
 191  
 192  echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
 193  
 194  // display strings
 195  $strup      = get_string('up');
 196  $strdown    = get_string('down');
 197  $strdelete  = get_string('delete');
 198  $strenable  = get_string('enable');
 199  $strdisable = get_string('disable');
 200  $strmanage  = get_string('manageinstance', 'enrol');
 201  
 202  $table = new html_table();
 203  $table->head  = array(get_string('name'), get_string('users'), $strup.'/'.$strdown, get_string('edit'));
 204  $table->align = array('left', 'center', 'center', 'center');
 205  $table->width = '100%';
 206  $table->data  = array();
 207  
 208  // iterate through enrol plugins and add to the display table
 209  $updowncount = 1;
 210  $icount = count($instances);
 211  $url = new moodle_url('/enrol/instances.php', array('sesskey'=>sesskey(), 'id'=>$course->id));
 212  foreach ($instances as $instance) {
 213      if (!isset($plugins[$instance->enrol])) {
 214          continue;
 215      }
 216      $plugin = $plugins[$instance->enrol];
 217  
 218      $displayname = $plugin->get_instance_name($instance);
 219      if (!enrol_is_enabled($instance->enrol) or $instance->status != ENROL_INSTANCE_ENABLED) {
 220          $displayname = html_writer::tag('span', $displayname, array('class'=>'dimmed_text'));
 221      }
 222  
 223      $users = $DB->count_records('user_enrolments', array('enrolid'=>$instance->id));
 224  
 225      $updown = array();
 226      $edit = array();
 227  
 228      if ($canconfig) {
 229          if ($updowncount > 1) {
 230              $aurl = new moodle_url($url, array('action'=>'up', 'instance'=>$instance->id));
 231              $updown[] = $OUTPUT->action_icon($aurl, new pix_icon('t/up', $strup, 'core', array('class' => 'iconsmall')));
 232          } else {
 233              $updown[] = $OUTPUT->spacer();
 234          }
 235          if ($updowncount < $icount) {
 236              $aurl = new moodle_url($url, array('action'=>'down', 'instance'=>$instance->id));
 237              $updown[] = $OUTPUT->action_icon($aurl, new pix_icon('t/down', $strdown, 'core', array('class' => 'iconsmall')));
 238          } else {
 239              $updown[] = $OUTPUT->spacer();
 240          }
 241          ++$updowncount;
 242  
 243          if ($plugin->can_delete_instance($instance)) {
 244              $aurl = new moodle_url($url, array('action'=>'delete', 'instance'=>$instance->id));
 245              $edit[] = $OUTPUT->action_icon($aurl, new pix_icon('t/delete', $strdelete, 'core', array('class' => 'iconsmall')));
 246          }
 247  
 248          if (enrol_is_enabled($instance->enrol) && $plugin->can_hide_show_instance($instance)) {
 249              if ($instance->status == ENROL_INSTANCE_ENABLED) {
 250                  $aurl = new moodle_url($url, array('action'=>'disable', 'instance'=>$instance->id));
 251                  $edit[] = $OUTPUT->action_icon($aurl, new pix_icon('t/hide', $strdisable, 'core', array('class' => 'iconsmall')));
 252              } else if ($instance->status == ENROL_INSTANCE_DISABLED) {
 253                  $aurl = new moodle_url($url, array('action'=>'enable', 'instance'=>$instance->id));
 254                  $edit[] = $OUTPUT->action_icon($aurl, new pix_icon('t/show', $strenable, 'core', array('class' => 'iconsmall')));
 255              } else {
 256                  // plugin specific state - do not mess with it!
 257                  $edit[] = $OUTPUT->pix_icon('t/show', get_string('show'));
 258              }
 259  
 260          }
 261      }
 262  
 263      // link to instance management
 264      if (enrol_is_enabled($instance->enrol) && $canconfig) {
 265          if ($icons = $plugin->get_action_icons($instance)) {
 266              $edit = array_merge($edit, $icons);
 267          }
 268      }
 269  
 270      // Add a row to the table.
 271      $table->data[] = array($displayname, $users, implode('', $updown), implode('', $edit));
 272  
 273  }
 274  echo html_writer::table($table);
 275  
 276  // access security is in each plugin
 277  $candidates = array();
 278  foreach (enrol_get_plugins(true) as $name=>$plugin) {
 279      if ($plugin->use_standard_editing_ui()) {
 280          if ($plugin->can_add_instance($course->id)) {
 281              // Standard add/edit UI.
 282              $params = array('type' => $name, 'courseid' => $course->id);
 283              $url = new moodle_url('/enrol/editinstance.php', $params);
 284              $link = $url->out(false);
 285              $candidates[$link] = get_string('pluginname', 'enrol_'.$name);
 286          }
 287      } else if ($url = $plugin->get_newinstance_link($course->id)) {
 288          // Old custom UI.
 289          $link = $url->out(false);
 290          $candidates[$link] = get_string('pluginname', 'enrol_'.$name);
 291      }
 292  }
 293  
 294  if ($candidates) {
 295      $select = new url_select($candidates);
 296      $select->set_label(get_string('addinstance', 'enrol'));
 297      echo $OUTPUT->render($select);
 298  }
 299  
 300  echo $OUTPUT->box_end();
 301  
 302  echo $OUTPUT->footer();