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');
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 $publishes = optional_param('publish', null, PARAM_BOOL
); // optional_param cleans arrays too
28 $subscribes = optional_param('subscribe', null, PARAM_BOOL
); // optional_param cleans arrays too
29 $exists = optional_param('exists', null, PARAM_BOOL
); // optional_param cleans arrays too
36 $stradministration = get_string('administration');
37 $strconfiguration = get_string('configuration');
38 $strmnetsettings = get_string('mnetsettings', 'mnet');
39 $strmnetservices = get_string('mnetservices', 'mnet');
40 $strmnetthemes = get_string('mnetthemes', 'mnet');
41 $strmnetlog = get_string('mnetlog', 'mnet');
42 $strmnetedithost = get_string('reviewhostdetails', 'mnet');
43 $strmneteditservices = get_string('reviewhostservices', 'mnet');
45 $mnet_peer = new mnet_peer();
47 if (($form = data_submitted()) && confirm_sesskey()) {
48 $mnet_peer->set_id($hostid);
50 foreach($exists as $key => $value) {
51 $host2service = get_record('mnet_host2service', 'hostid', $hostid, 'serviceid', $key);
52 $publish = array_key_exists($key, $publishes) ?
$publishes[$key] : 0;
53 $subscribe = array_key_exists($key, $subscribes) ?
$subscribes[$key] : 0;
55 if ($publish != 1 && $subscribe != 1) {
56 if (false == $host2service) {
57 // We don't have or need a record - do nothing!
59 // We don't need the record - delete it
60 delete_records('mnet_host2service', 'hostid', $hostid, 'serviceid', $key);
62 } elseif (false == $host2service && ($publish == 1 ||
$subscribe == 1)) {
63 $host2service = new stdClass();
64 $host2service->hostid
= $hostid;
65 $host2service->serviceid
= $key;
67 $host2service->publish
= $publish;
68 $host2service->subscribe
= $subscribe;
70 $host2service->id
= insert_record('mnet_host2service', $host2service);
71 } elseif ($host2service->publish
!= $publish ||
$host2service->subscribe
!= $subscribe) {
72 $host2service->publish
= $publish;
73 $host2service->subscribe
= $subscribe;
74 $tf = update_record('mnet_host2service', $host2service);
79 if (is_int($hostid)) {
80 if (0 == $mnet_peer->id
) $mnet_peer->set_id($hostid);
81 $mnet_peer->nextstep
= 'verify';
83 $id_list = $mnet_peer->id
;
84 if (!empty($CFG->mnet_all_hosts_id
)) {
85 $id_list .= ', '.$CFG->mnet_all_hosts_id
;
88 $concat = sql_concat('COALESCE(h2s.id,0) ', ' \'-\' ', ' svc.id', '\'-\'', 'r.parent_type', '\'-\'', 'r.parent');
103 {$CFG->prefix}mnet_service2rpc s2r,
104 {$CFG->prefix}mnet_rpc r,
105 {$CFG->prefix}mnet_service svc
107 {$CFG->prefix}mnet_host2service h2s
109 h2s.hostid in ($id_list) AND
110 h2s.serviceid = svc.id
113 s2r.serviceid = svc.id AND
118 $resultset = get_records_sql($query);
120 if (is_array($resultset)) {
121 $resultset = array_values($resultset);
123 $resultset = array();
126 require_once $CFG->dirroot
.'/mnet/xmlrpc/client.php';
128 $remoteservices = array();
129 if ($hostid != $CFG->mnet_all_hosts_id
) {
130 // Create a new request object
131 $mnet_request = new mnet_xmlrpc_client();
133 // Tell it the path to the method that we want to execute
134 $mnet_request->set_method('system/listServices');
135 $mnet_request->send($mnet_peer);
136 if (is_array($mnet_request->response
)) {
137 foreach($mnet_request->response
as $service) {
138 $remoteservices[$service['name']][$service['apiversion']] = $service;
143 $myservices = array();
144 foreach($resultset as $result) {
145 $result->hostpublishes
= false;
146 $result->hostsubscribes
= false;
147 if (isset($remoteservices[$result->name
][$result->apiversion
])) {
148 if ($remoteservices[$result->name
][$result->apiversion
]['publish'] == 1) {
149 $result->hostpublishes
= true;
151 if ($remoteservices[$result->name
][$result->apiversion
]['subscribe'] == 1) {
152 $result->hostsubscribes
= true;
156 if (empty($myservices[$result->name
][$result->apiversion
])) {
157 $myservices[$result->name
][$result->apiversion
] = array('serviceid' => $result->serviceid
,
158 'name' => $result->name
,
159 'offer' => $result->offer
,
160 'apiversion' => $result->apiversion
,
161 'parent_type' => $result->parent_type
,
162 'parent' => $result->parent
,
163 'hostsubscribes' => $result->hostsubscribes
,
164 'hostpublishes' => $result->hostpublishes
168 // allhosts_publish allows us to tell the admin that even though he
169 // is disabling a service, it's still available to the host because
170 // he's also publishing it to 'all hosts'
171 if ($result->hostid
== $CFG->mnet_all_hosts_id
&& $CFG->mnet_all_hosts_id
!= $mnet_peer->id
) {
172 $myservices[$result->name
][$result->apiversion
]['allhosts_publish'] = $result->publish
;
173 $myservices[$result->name
][$result->apiversion
]['allhosts_subscribe'] = $result->subscribe
;
174 } elseif (!empty($result->hostid
)) {
175 $myservices[$result->name
][$result->apiversion
]['I_publish'] = $result->publish
;
176 $myservices[$result->name
][$result->apiversion
]['I_subscribe'] = $result->subscribe
;
182 redirect('peers.php', get_string('nohostid','mnet'), '5');
186 include('./mnet_services.html');