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 401] [Versions 310 and 402] [Versions 310 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   * Page to enrol our users into remote courses
  20   *
  21   * @package    plugintype
  22   * @subpackage pluginname
  23   * @copyright  2010 David Mudrak <david@moodle.com>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  require(__DIR__.'/../../../config.php');
  28  require_once($CFG->libdir.'/adminlib.php');
  29  require_once($CFG->dirroot.'/mnet/service/enrol/locallib.php');
  30  
  31  require_sesskey();
  32  
  33  $hostid   = required_param('host', PARAM_INT); // remote host id in our mnet_host table
  34  $courseid = required_param('course', PARAM_INT); // id of the course in our cache table
  35  $usecache = optional_param('usecache', true, PARAM_BOOL); // use cached list of enrolments
  36  
  37  admin_externalpage_setup('mnetenrol', '', array('host'=>$hostid, 'course'=>$courseid, 'usecache'=>1, 'sesskey'=>sesskey()),
  38                           new moodle_url('/mnet/service/enrol/course.php'));
  39  
  40  $service = mnetservice_enrol::get_instance();
  41  
  42  if (!$service->is_available()) {
  43      echo $OUTPUT->box(get_string('mnetdisabled','mnet'), 'noticebox');
  44      echo $OUTPUT->footer();
  45      die();
  46  }
  47  
  48  // remote hosts that may publish remote enrolment service and we are subscribed to it
  49  $hosts = $service->get_remote_publishers();
  50  
  51  if (empty($hosts[$hostid])) {
  52      print_error('wearenotsubscribedtothishost', 'mnetservice_enrol');
  53  }
  54  $host   = $hosts[$hostid];
  55  $course = $DB->get_record('mnetservice_enrol_courses', array('id'=>$courseid, 'hostid'=>$host->id), '*', MUST_EXIST);
  56  
  57  echo $OUTPUT->header();
  58  
  59  // course name
  60  $icon = $OUTPUT->pix_icon('i/course', get_string('category'));
  61  echo $OUTPUT->heading($icon . s($course->fullname));
  62  
  63  // collapsible course summary
  64  if (!empty($course->summary)) {
  65      $options = new stdClass();
  66      $options->trusted = false;
  67      $options->para    = false;
  68      $options->filter  = false;
  69      $options->noclean = false;
  70      $options->overflowdiv = true;
  71      print_collapsible_region_start('remotecourse summary', 'remotecourse-summary', get_string('coursesummary'), false, true);
  72      echo format_text($course->summary, $course->summaryformat, $options);
  73      print_collapsible_region_end();
  74  }
  75  
  76  $error = '';
  77  
  78  $lastfetchenrolments = get_config('mnetservice_enrol', 'lastfetchenrolments');
  79  if (!$usecache or empty($lastfetchenrolments) or (time()-$lastfetchenrolments > 600)) {
  80      // fetch fresh data from remote if we just came from the course selection screen
  81      // or every 10 minutes
  82      $usecache = false;
  83      $result = $service->req_course_enrolments($host->id, $course->remoteid, $usecache);
  84      if ($result !== true) {
  85          $error .= $service->format_error_message($result);
  86      }
  87  }
  88  
  89  // user selectors
  90  $currentuserselector = new mnetservice_enrol_existing_users_selector('removeselect', array('hostid'=>$host->id, 'remotecourseid'=>$course->remoteid));
  91  $potentialuserselector = new mnetservice_enrol_potential_users_selector('addselect', array('hostid'=>$host->id, 'remotecourseid'=>$course->remoteid));
  92  
  93  // process incoming enrol request
  94  if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
  95      $userstoassign = $potentialuserselector->get_selected_users();
  96      if (!empty($userstoassign)) {
  97          foreach($userstoassign as $adduser) {
  98              $user = $DB->get_record('user', array('id'=>$adduser->id));
  99              $result = $service->req_enrol_user($user, $course);
 100              if ($result !== true) {
 101                  $error .= $service->format_error_message($result);
 102              }
 103          }
 104  
 105          $potentialuserselector->invalidate_selected_users();
 106          $currentuserselector->invalidate_selected_users();
 107      }
 108  }
 109  
 110  // process incoming unenrol request
 111  if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
 112      $userstounassign = $currentuserselector->get_selected_users();
 113      if (!empty($userstounassign)) {
 114          foreach($userstounassign as $removeuser) {
 115              $user = $DB->get_record('user', array('id'=>$removeuser->id));
 116              $result = $service->req_unenrol_user($user, $course);
 117              if ($result !== true) {
 118                  $error .= $service->format_error_message($result);
 119              }
 120          }
 121  
 122          $potentialuserselector->invalidate_selected_users();
 123          $currentuserselector->invalidate_selected_users();
 124      }
 125  }
 126  
 127  if (!empty($error)) {
 128      echo $OUTPUT->box($error, 'generalbox error');
 129  }
 130  
 131  // print form to enrol our students
 132  ?>
 133  <form id="assignform" method="post" action="<?php echo $PAGE->url ?>">
 134  <div>
 135    <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
 136    <input type="hidden" name="hostid" value="<?php echo $host->id ?>" />
 137    <input type="hidden" name="courseid" value="<?php echo $course->id ?>" />
 138  
 139    <table summary="" class="roleassigntable generaltable generalbox boxaligncenter" cellspacing="0">
 140      <tr>
 141        <td id="existingcell">
 142            <p><label for="removeselect"><?php print_string('enrolledusers', 'enrol'); ?></label></p>
 143            <?php $currentuserselector->display() ?>
 144        </td>
 145        <td id="buttonscell">
 146            <div id="addcontrols">
 147                <input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>" title="<?php print_string('add'); ?>" /><br />
 148  
 149                <div class="enroloptions">
 150                    <p><?php echo get_string('assignrole', 'role') .': '. s($course->rolename); ?></p>
 151                </div>
 152  
 153            </div>
 154  
 155            <div id="removecontrols">
 156                <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>" title="<?php print_string('remove'); ?>" />
 157            </div>
 158        </td>
 159        <td id="potentialcell">
 160            <p><label for="addselect"><?php print_string('enrolcandidates', 'enrol'); ?></label></p>
 161            <?php $potentialuserselector->display() ?>
 162        </td>
 163      </tr>
 164    </table>
 165  </div>
 166  </form>
 167  <?php
 168  
 169  // eventually display other enrolments of our users (manual, self etc.) in the remote course
 170  list($sort, $params) = users_order_by_sql('u');
 171  $sql = "SELECT e.id,e.enroltype AS plugin, u.firstname, u.lastname, u.email, u.id AS userid,
 172                 e.enroltime AS timemodified, e.rolename
 173            FROM {mnetservice_enrol_enrolments} e
 174            JOIN {user} u ON u.id = e.userid
 175           WHERE e.hostid = :hostid AND e.remotecourseid = :remotecourseid AND e.enroltype != 'mnet'
 176        ORDER BY $sort";
 177  $params['hostid'] = $host->id;
 178  $params['remotecourseid'] = $course->remoteid;
 179  
 180  if ($enrolments = $DB->get_records_sql($sql, $params)) {
 181      echo $OUTPUT->heading(get_string('otherenrolledusers', 'mnetservice_enrol'), 3);
 182  
 183      $table = new html_table();
 184      $table->attributes['class'] = 'generaltable otherenrolledusers';
 185      $table->head = array(get_string('fullnameuser'), get_string('role'), get_string('plugin'));
 186      foreach ($enrolments as $enrolleduser) {
 187          $table->data[] = array(fullname($enrolleduser), s($enrolleduser->rolename), s($enrolleduser->plugin));
 188      }
 189      echo html_writer::table($table);
 190  }
 191  
 192  if ($usecache) {
 193      echo $OUTPUT->single_button(new moodle_url($PAGE->url, array('usecache'=>0, 'sesskey'=>sesskey())),
 194                                  get_string('refetch', 'mnetservice_enrol'), 'get');
 195  }
 196  
 197  echo $OUTPUT->single_button(new moodle_url('/mnet/service/enrol/host.php', array('id'=>$host->id)),
 198                              get_string('availablecourseson', 'mnetservice_enrol', s($host->hostname)), 'get');
 199  
 200  echo $OUTPUT->footer();