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 400 and 402]

   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   * Displays a list of remote courses offered by a given host for our students
  20   *
  21   * By default the courses information is cached in our local DB table. Parameter
  22   * $usecache can be used to force re-fetching up to date state from remote
  23   * hosts (session key required in such case).
  24   *
  25   * @package    mnetservice
  26   * @subpackage enrol
  27   * @copyright  2010 David Mudrak <david@moodle.com>
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  
  31  require(__DIR__.'/../../../config.php');
  32  require_once($CFG->libdir.'/adminlib.php');
  33  require_once($CFG->dirroot.'/mnet/service/enrol/locallib.php');
  34  
  35  $hostid   = required_param('id', PARAM_INT); // remote host id
  36  $usecache = optional_param('usecache', true, PARAM_BOOL); // use cached list of courses
  37  
  38  admin_externalpage_setup('mnetenrol', '', array('id'=>$hostid, 'usecache'=>1),
  39          new moodle_url('/mnet/service/enrol/host.php'));
  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      throw new \moodle_exception('wearenotsubscribedtothishost', 'mnetservice_enrol');
  53  }
  54  $host = $hosts[$hostid];
  55  
  56  if (!$usecache) {
  57      // our local database will be changed
  58      require_sesskey();
  59  }
  60  $courses = $service->get_remote_courses($host->id, $usecache);
  61  if (is_string($courses)) {
  62      throw new \moodle_exception('fetchingcourses', 'mnetservice_enrol', '', null, $service->format_error_message($courses));
  63  }
  64  
  65  echo $OUTPUT->header();
  66  echo $OUTPUT->heading(get_string('availablecourseson', 'mnetservice_enrol', s($host->hostname)));
  67  if (empty($courses)) {
  68      $a = (object)array('hostname' => s($host->hostname), 'hosturl' => s($host->hosturl));
  69      echo $OUTPUT->box(get_string('availablecoursesonnone','mnetservice_enrol', $a), 'noticebox');
  70      if ($usecache) {
  71          echo $OUTPUT->single_button(new moodle_url($PAGE->url, array('usecache'=>0, 'sesskey'=>sesskey())),
  72                                      get_string('refetch', 'mnetservice_enrol'), 'get');
  73      }
  74      echo $OUTPUT->footer();
  75      die();
  76  }
  77  
  78  $table = new html_table();
  79  $table->head = array(
  80      get_string('shortnamecourse'),
  81      get_string('fullnamecourse'),
  82      get_string('role'),
  83      get_string('action')
  84  );
  85  $table->attributes['class'] = 'generaltable remotecourses';
  86  $icon = $OUTPUT->pix_icon('i/course', get_string('category'));
  87  $prevcat = null;
  88  foreach ($courses as $course) {
  89      $course = (object)$course;
  90      if ($prevcat !== $course->categoryid) {
  91          $row = new html_table_row();
  92          $cell = new html_table_cell($icon . s($course->categoryname));
  93          $cell->header = true;
  94          $cell->attributes['class'] = 'categoryname';
  95          $cell->colspan = 4;
  96          $row->cells = array($cell);
  97          $table->data[] = $row;
  98          $prevcat = $course->categoryid;
  99      }
 100      $editbtn = $OUTPUT->single_button(new moodle_url('/mnet/service/enrol/course.php',
 101                                        array('host'=>$host->id, 'course'=>$course->id, 'usecache'=>0, 'sesskey'=>sesskey())),
 102                                        get_string('editenrolments', 'mnetservice_enrol'), 'get');
 103      $row = new html_table_row();
 104      $row->cells = array(
 105          s($course->shortname),
 106          s($course->fullname),
 107          s($course->rolename),
 108          $editbtn
 109      );
 110      $table->data[] = $row;
 111  }
 112  echo html_writer::table($table);
 113  
 114  if ($usecache) {
 115      echo $OUTPUT->single_button(new moodle_url($PAGE->url, array('usecache'=>0, 'sesskey'=>sesskey())),
 116                                  get_string('refetch', 'mnetservice_enrol'), 'get');
 117  }
 118  
 119  echo $OUTPUT->footer();