Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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   * Manual user enrolment UI.
  19   *
  20   * @package    enrol_manual
  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  require_once($CFG->dirroot.'/enrol/manual/locallib.php');
  27  
  28  $enrolid      = required_param('enrolid', PARAM_INT);
  29  $roleid       = optional_param('roleid', -1, PARAM_INT);
  30  $extendperiod = optional_param('extendperiod', 0, PARAM_INT);
  31  $extendbase   = optional_param('extendbase', 0, PARAM_INT);
  32  $timeend      = optional_param_array('timeend', [], PARAM_INT);
  33  
  34  $instance = $DB->get_record('enrol', array('id'=>$enrolid, 'enrol'=>'manual'), '*', MUST_EXIST);
  35  $course = $DB->get_record('course', array('id'=>$instance->courseid), '*', MUST_EXIST);
  36  $context = context_course::instance($course->id, MUST_EXIST);
  37  
  38  require_login($course);
  39  $canenrol = has_capability('enrol/manual:enrol', $context);
  40  $canunenrol = has_capability('enrol/manual:unenrol', $context);
  41  
  42  // Note: manage capability not used here because it is used for editing
  43  // of existing enrolments which is not possible here.
  44  
  45  if (!$canenrol and !$canunenrol) {
  46      // No need to invent new error strings here...
  47      require_capability('enrol/manual:enrol', $context);
  48      require_capability('enrol/manual:unenrol', $context);
  49  }
  50  
  51  if ($roleid < 0) {
  52      $roleid = $instance->roleid;
  53  }
  54  $roles = get_assignable_roles($context);
  55  $roles = array('0'=>get_string('none')) + $roles;
  56  
  57  if (!isset($roles[$roleid])) {
  58      // Weird - security always first!
  59      $roleid = 0;
  60  }
  61  
  62  if (!$enrol_manual = enrol_get_plugin('manual')) {
  63      throw new coding_exception('Can not instantiate enrol_manual');
  64  }
  65  
  66  $instancename = $enrol_manual->get_instance_name($instance);
  67  
  68  $PAGE->set_url('/enrol/manual/manage.php', array('enrolid'=>$instance->id));
  69  $PAGE->set_pagelayout('admin');
  70  $PAGE->set_title($enrol_manual->get_instance_name($instance));
  71  $PAGE->set_heading($course->fullname);
  72  navigation_node::override_active_url(new moodle_url('/user/index.php', array('id'=>$course->id)));
  73  
  74  // Create the user selector objects.
  75  $options = array('enrolid' => $enrolid, 'accesscontext' => $context);
  76  
  77  $potentialuserselector = new enrol_manual_potential_participant('addselect', $options);
  78  $currentuserselector = new enrol_manual_current_participant('removeselect', $options);
  79  
  80  // Build the list of options for the enrolment period dropdown.
  81  $unlimitedperiod = get_string('unlimited');
  82  $periodmenu = array();
  83  for ($i=1; $i<=365; $i++) {
  84      $seconds = $i * 86400;
  85      $periodmenu[$seconds] = get_string('numdays', '', $i);
  86  }
  87  // Work out the apropriate default settings.
  88  if ($extendperiod) {
  89      $defaultperiod = $extendperiod;
  90  } else {
  91      $defaultperiod = $instance->enrolperiod;
  92  }
  93  if ($instance->enrolperiod > 0 && !isset($periodmenu[$instance->enrolperiod])) {
  94      $periodmenu[$instance->enrolperiod] = format_time($instance->enrolperiod);
  95  }
  96  if (empty($extendbase)) {
  97      if (!$extendbase = get_config('enrol_manual', 'enrolstart')) {
  98          // Default to now if there is no system setting.
  99          $extendbase = 4;
 100      }
 101  }
 102  
 103  // Build the list of options for the starting from dropdown.
 104  $now = time();
 105  $today = make_timestamp(date('Y', $now), date('m', $now), date('d', $now), 0, 0, 0);
 106  $dateformat = get_string('strftimedatefullshort');
 107  
 108  // Enrolment start.
 109  $basemenu = array();
 110  if ($course->startdate > 0) {
 111      $basemenu[2] = get_string('coursestart') . ' (' . userdate($course->startdate, $dateformat) . ')';
 112  }
 113  $basemenu[3] = get_string('today') . ' (' . userdate($today, $dateformat) . ')';
 114  $basemenu[4] = get_string('now', 'enrol_manual') . ' (' . userdate($now, get_string('strftimedatetimeshort')) . ')';
 115  
 116  // Process add and removes.
 117  if ($canenrol && optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
 118      $userstoassign = $potentialuserselector->get_selected_users();
 119      if (!empty($userstoassign)) {
 120          foreach($userstoassign as $adduser) {
 121              switch($extendbase) {
 122                  case 2:
 123                      $timestart = $course->startdate;
 124                      break;
 125                  case 4:
 126                      // We mimic get_enrolled_sql round(time(), -2) but always floor as we want users to always access their
 127                      // courses once they are enrolled.
 128                      $timestart = intval(substr($now, 0, 8) . '00') - 1;
 129                      break;
 130                  case 3:
 131                  default:
 132                      $timestart = $today;
 133                      break;
 134              }
 135  
 136              if ($timeend) {
 137                  $timeend = make_timestamp($timeend['year'], $timeend['month'], $timeend['day'], $timeend['hour'],
 138                          $timeend['minute']);
 139              } else if ($extendperiod <= 0) {
 140                  $timeend = 0;
 141              } else {
 142                  $timeend = $timestart + $extendperiod;
 143              }
 144              $enrol_manual->enrol_user($instance, $adduser->id, $roleid, $timestart, $timeend);
 145          }
 146  
 147          $potentialuserselector->invalidate_selected_users();
 148          $currentuserselector->invalidate_selected_users();
 149  
 150          //TODO: log
 151      }
 152  }
 153  
 154  // Process incoming role unassignments.
 155  if ($canunenrol && optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
 156      $userstounassign = $currentuserselector->get_selected_users();
 157      if (!empty($userstounassign)) {
 158          foreach($userstounassign as $removeuser) {
 159              $enrol_manual->unenrol_user($instance, $removeuser->id);
 160          }
 161  
 162          $potentialuserselector->invalidate_selected_users();
 163          $currentuserselector->invalidate_selected_users();
 164  
 165          //TODO: log
 166      }
 167  }
 168  
 169  
 170  echo $OUTPUT->header();
 171  echo $OUTPUT->heading($instancename);
 172  
 173  $addenabled = $canenrol ? '' : 'disabled="disabled"';
 174  $removeenabled = $canunenrol ? '' : 'disabled="disabled"';
 175  
 176  ?>
 177  <form id="assignform" method="post" action="<?php echo $PAGE->url ?>"><div>
 178    <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
 179  
 180    <table summary="" class="roleassigntable generaltable generalbox boxaligncenter" cellspacing="0">
 181      <tr>
 182        <td id="existingcell">
 183            <p><label for="removeselect"><?php print_string('enrolledusers', 'enrol'); ?></label></p>
 184            <?php $currentuserselector->display() ?>
 185        </td>
 186        <td id="buttonscell">
 187            <div id="addcontrols">
 188                <input class="btn btn-secondary" name="add" <?php echo $addenabled; ?> id="add" type="submit"
 189                       value="<?php echo $OUTPUT->larrow() . '&nbsp;' . get_string('add'); ?>"
 190                       title="<?php print_string('add'); ?>" /><br />
 191  
 192                <div class="enroloptions">
 193  
 194                <p><label for="menuroleid"><?php print_string('assignrole', 'enrol_manual') ?></label><br />
 195                <?php echo html_writer::select($roles, 'roleid', $roleid, false); ?></p>
 196  
 197                <p><label for="menuextendperiod"><?php print_string('enrolperiod', 'enrol') ?></label><br />
 198                <?php echo html_writer::select($periodmenu, 'extendperiod', $defaultperiod, $unlimitedperiod); ?></p>
 199  
 200                <p><label for="menuextendbase"><?php print_string('startingfrom') ?></label><br />
 201                <?php echo html_writer::select($basemenu, 'extendbase', $extendbase, false); ?></p>
 202  
 203                </div>
 204            </div>
 205  
 206            <div id="removecontrols">
 207                <input class="btn btn-secondary" name="remove" id="remove" <?php echo $removeenabled; ?> type="submit"
 208                       value="<?php echo get_string('remove') . '&nbsp;' . $OUTPUT->rarrow(); ?>"
 209                       title="<?php print_string('remove'); ?>" />
 210            </div>
 211        </td>
 212        <td id="potentialcell">
 213            <p><label for="addselect"><?php print_string('enrolcandidates', 'enrol'); ?></label></p>
 214            <?php $potentialuserselector->display() ?>
 215        </td>
 216      </tr>
 217    </table>
 218  </div></form>
 219  <?php
 220  
 221  
 222  echo $OUTPUT->footer();