on-demand release 4.0dev+
[moodle.git] / mnet / service / enrol / host.php
CommitLineData
481f0e15
DM
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 *
de5a205c
DM
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 *
481f0e15
DM
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
1fcf0ca8 31require(__DIR__.'/../../../config.php');
481f0e15
DM
32require_once($CFG->libdir.'/adminlib.php');
33require_once($CFG->dirroot.'/mnet/service/enrol/locallib.php');
34
de5a205c 35$hostid = required_param('id', PARAM_INT); // remote host id
481f0e15
DM
36$usecache = optional_param('usecache', true, PARAM_BOOL); // use cached list of courses
37
a722e491
DM
38admin_externalpage_setup('mnetenrol', '', array('id'=>$hostid, 'usecache'=>1),
39 new moodle_url('/mnet/service/enrol/host.php'));
481f0e15
DM
40$service = mnetservice_enrol::get_instance();
41
42if (!$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
51if (empty($hosts[$hostid])) {
52 print_error('wearenotsubscribedtothishost', 'mnetservice_enrol');
53}
54$host = $hosts[$hostid];
55
de5a205c
DM
56if (!$usecache) {
57 // our local database will be changed
58 require_sesskey();
59}
481f0e15
DM
60$courses = $service->get_remote_courses($host->id, $usecache);
61if (is_string($courses)) {
62 print_error('fetchingcourses', 'mnetservice_enrol', '', null, $service->format_error_message($courses));
63}
64
65echo $OUTPUT->header();
66echo $OUTPUT->heading(get_string('availablecourseson', 'mnetservice_enrol', s($host->hostname)));
67if (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');
a722e491
DM
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 }
481f0e15
DM
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';
663640f5 86$icon = $OUTPUT->pix_icon('i/course', get_string('category'));
481f0e15
DM
87$prevcat = null;
88foreach ($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 }
de5a205c 100 $editbtn = $OUTPUT->single_button(new moodle_url('/mnet/service/enrol/course.php',
ef75ea6c 101 array('host'=>$host->id, 'course'=>$course->id, 'usecache'=>0, 'sesskey'=>sesskey())),
481f0e15
DM
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}
112echo html_writer::table($table);
113
de5a205c 114if ($usecache) {
a722e491
DM
115 echo $OUTPUT->single_button(new moodle_url($PAGE->url, array('usecache'=>0, 'sesskey'=>sesskey())),
116 get_string('refetch', 'mnetservice_enrol'), 'get');
de5a205c
DM
117}
118
481f0e15 119echo $OUTPUT->footer();