MDL-16043: manage case when administrator set enrol_remotecoursefield or enrol_remote...
[moodle-linuxchix.git] / admin / mnet / mnet_services.php
blobdddff86b3396f786dedf29f5d1ff1d16c8188585
1 <?php
2 // Allows the admin to configure services for remote hosts
4 require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
5 require_once($CFG->libdir.'/adminlib.php');
6 include_once($CFG->dirroot.'/mnet/lib.php');
8 require_login();
9 admin_externalpage_setup('mnetpeers');
11 $context = get_context_instance(CONTEXT_SYSTEM);
13 require_capability('moodle/site:config', $context, $USER->id, true, "nopermissions");
15 if (!$site = get_site()) {
16 print_error('nosite', '', '', NULL, true);
19 /// Initialize variables.
21 // Step must be one of:
22 // input Parse the details of a new host and fetch its public key
23 // commit Save our changes (to a new OR existing host)
24 // force Go ahead with something we've been warned is strange
25 $step = optional_param('step', NULL, PARAM_ALPHA);
26 $hostid = optional_param('hostid', NULL, PARAM_INT);
27 $nocertstring = '';
28 $nocertmatch = '';
29 $badcert = '';
30 $certerror = '';
31 $noipmatch = '';
32 $stradministration = get_string('administration');
33 $strconfiguration = get_string('configuration');
34 $strmnetsettings = get_string('mnetsettings', 'mnet');
35 $strmnetservices = get_string('mnetservices', 'mnet');
36 $strmnetthemes = get_string('mnetthemes', 'mnet');
37 $strmnetlog = get_string('mnetlog', 'mnet');
38 $strmnetedithost = get_string('reviewhostdetails', 'mnet');
39 $strmneteditservices = get_string('reviewhostservices', 'mnet');
41 $mnet_peer = new mnet_peer();
43 if (($form = data_submitted()) && confirm_sesskey()) {
44 $mnet_peer->set_id($hostid);
45 $treevals = array();
46 foreach($_POST['exists'] as $key => $value) {
47 $host2service = get_record('mnet_host2service', 'hostid', $_POST['hostid'], 'serviceid', $key);
48 $publish = (isset($_POST['publish'][$key]) && $_POST['publish'][$key] == 'on')? 1 : 0;
49 $subscribe = (isset($_POST['subscribe'][$key]) && $_POST['subscribe'][$key] == 'on')? 1 : 0;
51 if ($publish != 1 && $subscribe != 1) {
52 if (false == $host2service) {
53 // We don't have or need a record - do nothing!
54 } else {
55 // We don't need the record - delete it
56 delete_records('mnet_host2service', 'hostid', $_POST['hostid'], 'serviceid', $key);
58 } elseif (false == $host2service && ($publish == 1 || $subscribe == 1)) {
59 $host2service = new stdClass();
60 $host2service->hostid = $_POST['hostid'];
61 $host2service->serviceid = $key;
63 $host2service->publish = $publish;
64 $host2service->subscribe = $subscribe;
66 $host2service->id = insert_record('mnet_host2service', $host2service);
67 } elseif ($host2service->publish != $publish || $host2service->subscribe != $subscribe) {
68 $host2service->publish = $publish;
69 $host2service->subscribe = $subscribe;
70 $tf = update_record('mnet_host2service', $host2service);
75 if (is_int($hostid)) {
76 if (0 == $mnet_peer->id) $mnet_peer->set_id($hostid);
77 $mnet_peer->nextstep = 'verify';
79 $id_list = $mnet_peer->id;
80 if (!empty($CFG->mnet_all_hosts_id)) {
81 $id_list .= ', '.$CFG->mnet_all_hosts_id;
84 $concat = sql_concat('COALESCE(h2s.id,0) ', ' \'-\' ', ' svc.id');
86 $query = "
87 SELECT DISTINCT
88 $concat as id,
89 svc.id as serviceid,
90 svc.name,
91 svc.offer,
92 svc.apiversion,
93 r.parent_type,
94 r.parent,
95 h2s.hostid,
96 h2s.publish,
97 h2s.subscribe
98 FROM
99 {$CFG->prefix}mnet_service2rpc s2r,
100 {$CFG->prefix}mnet_rpc r,
101 {$CFG->prefix}mnet_service svc
102 LEFT JOIN
103 {$CFG->prefix}mnet_host2service h2s
105 h2s.hostid in ($id_list) AND
106 h2s.serviceid = svc.id
107 WHERE
108 svc.offer = '1' AND
109 s2r.serviceid = svc.id AND
110 s2r.rpcid = r.id
111 ORDER BY
112 svc.name ASC";
114 $resultset = get_records_sql($query);
116 if (is_array($resultset)) {
117 $resultset = array_values($resultset);
118 } else {
119 $resultset = array();
122 require_once $CFG->dirroot.'/mnet/xmlrpc/client.php';
124 $remoteservices = array();
125 if ($hostid != $CFG->mnet_all_hosts_id) {
126 // Create a new request object
127 $mnet_request = new mnet_xmlrpc_client();
129 // Tell it the path to the method that we want to execute
130 $mnet_request->set_method('system/listServices');
131 $mnet_request->send($mnet_peer);
132 if (is_array($mnet_request->response)) {
133 foreach($mnet_request->response as $service) {
134 $remoteservices[$service['name']][$service['apiversion']] = $service;
139 $myservices = array();
140 foreach($resultset as $result) {
141 $result->hostpublishes = false;
142 $result->hostsubscribes = false;
143 if (isset($remoteservices[$result->name][$result->apiversion])) {
144 if ($remoteservices[$result->name][$result->apiversion]['publish'] == 1) {
145 $result->hostpublishes = true;
147 if ($remoteservices[$result->name][$result->apiversion]['subscribe'] == 1) {
148 $result->hostsubscribes = true;
152 if (empty($myservices[$result->name][$result->apiversion])) {
153 $myservices[$result->name][$result->apiversion] = array('serviceid' => $result->serviceid,
154 'name' => $result->name,
155 'offer' => $result->offer,
156 'apiversion' => $result->apiversion,
157 'parent_type' => $result->parent_type,
158 'parent' => $result->parent,
159 'hostsubscribes' => $result->hostsubscribes,
160 'hostpublishes' => $result->hostpublishes
164 // allhosts_publish allows us to tell the admin that even though he
165 // is disabling a service, it's still available to the host because
166 // he's also publishing it to 'all hosts'
167 if ($result->hostid == $CFG->mnet_all_hosts_id && $CFG->mnet_all_hosts_id != $mnet_peer->id) {
168 $myservices[$result->name][$result->apiversion]['allhosts_publish'] = $result->publish;
169 $myservices[$result->name][$result->apiversion]['allhosts_subscribe'] = $result->subscribe;
170 } elseif (!empty($result->hostid)) {
171 $myservices[$result->name][$result->apiversion]['I_publish'] = $result->publish;
172 $myservices[$result->name][$result->apiversion]['I_subscribe'] = $result->subscribe;
177 } else {
178 redirect('peers.php', get_string('nohostid','mnet'), '5');
179 exit;
182 include('./mnet_services.html');