From c24cf442918b6c4fe869c0f17d482d31c3029ac7 Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Sat, 17 Jul 2010 22:35:18 +0000 Subject: [PATCH] MDL-22787 MNet: Finished client side of remote enrolment service --- mnet/service/enrol/course.php | 25 ++++++++-- mnet/service/enrol/locallib.php | 82 +++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 5 deletions(-) diff --git a/mnet/service/enrol/course.php b/mnet/service/enrol/course.php index f9c8e77a72309..0de84638fcaaa 100644 --- a/mnet/service/enrol/course.php +++ b/mnet/service/enrol/course.php @@ -72,12 +72,17 @@ print_collapsible_region_end(); } +$error = ''; + $lastfetchenrolments = get_config('mnetservice_enrol', 'lastfetchenrolments'); if (!$usecache or empty($lastfetchenrolments) or (time()-$lastfetchenrolments > 600)) { // fetch fresh data from remote if we just came from the course selection screen // or every 10 minutes - $service->req_course_enrolments($host->id, $course->remoteid, $usecache); $usecache = false; + $result = $service->req_course_enrolments($host->id, $course->remoteid, $usecache); + if ($result !== true) { + $error .= $service->format_error_message($result); + } } // user selectors @@ -89,8 +94,11 @@ $userstoassign = $potentialuserselector->get_selected_users(); if (!empty($userstoassign)) { foreach($userstoassign as $adduser) { - //$enrol_manual->enrol_user($instance, $adduser->id, $roleid, $timestart, $timeend); - add_to_log($course->id, 'course', 'enrol', '../enrol/users.php?id='.$course->id, $course->id); //there should be userid somewhere! + $user = $DB->get_record('user', array('id'=>$adduser->id)); + $result = $service->req_enrol_user($user, $course); + if ($result !== true) { + $error .= $service->format_error_message($result); + } } $potentialuserselector->invalidate_selected_users(); @@ -103,8 +111,11 @@ $userstounassign = $currentuserselector->get_selected_users(); if (!empty($userstounassign)) { foreach($userstounassign as $removeuser) { - //$enrol_manual->unenrol_user($instance, $removeuser->id); - add_to_log($course->id, 'course', 'unenrol', '../enrol/users.php?id='.$course->id, $course->id); //there should be userid somewhere! + $user = $DB->get_record('user', array('id'=>$removeuser->id)); + $result = $service->req_unenrol_user($user, $course); + if ($result !== true) { + $error .= $service->format_error_message($result); + } } $potentialuserselector->invalidate_selected_users(); @@ -112,6 +123,10 @@ } } +if (!empty($error)) { + echo $OUTPUT->box($error, 'generalbox error'); +} + // print form to enrol our students ?>
diff --git a/mnet/service/enrol/locallib.php b/mnet/service/enrol/locallib.php index 8c01a0aacf709..bbe1e76db3e7f 100644 --- a/mnet/service/enrol/locallib.php +++ b/mnet/service/enrol/locallib.php @@ -350,6 +350,88 @@ public function req_course_enrolments($mnethostid, $remotecourseid) { } } + /** + * Send request to enrol our user to the remote course + * + * Updates our remote enrolments cache if the enrolment was successful. + * + * @uses mnet_xmlrpc_client Invokes XML-RPC request + * @param object $user our user + * @param object $remotecourse record from mnetservice_enrol_courses table + * @return true|string true if success, error message from the remote host otherwise + */ + public function req_enrol_user(stdclass $user, stdclass $remotecourse) { + global $CFG, $DB; + require_once($CFG->dirroot.'/mnet/xmlrpc/client.php'); + + $peer = new mnet_peer(); + $peer->set_id($remotecourse->hostid); + + $request = new mnet_xmlrpc_client(); + $request->set_method('enrol/mnet/enrol.php/enrol_user'); + $request->add_param(mnet_strip_user((array)$user, mnet_fields_to_send($peer))); + $request->add_param($remotecourse->remoteid); + + if ($request->send($peer) === true) { + if ($request->response === true) { + // cache the enrolment information in our table + $enrolment = new stdclass(); + $enrolment->hostid = $peer->id; + $enrolment->userid = $user->id; + $enrolment->remotecourseid = $remotecourse->remoteid; + $enrolment->enroltype = 'mnet'; + // $enrolment->rolename not known now, must be re-fetched + // $enrolment->enroltime not known now, must be re-fetched + $DB->insert_record('mnetservice_enrol_enrolments', $enrolment); + return true; + + } else { + return serialize(array('invalid response: '.print_r($request->response, true))); + } + + } else { + return serialize($request->error); + } + } + + /** + * Send request to unenrol our user from the remote course + * + * Updates our remote enrolments cache if the unenrolment was successful. + * + * @uses mnet_xmlrpc_client Invokes XML-RPC request + * @param object $user our user + * @param object $remotecourse record from mnetservice_enrol_courses table + * @return true|string true if success, error message from the remote host otherwise + */ + public function req_unenrol_user(stdclass $user, stdclass $remotecourse) { + global $CFG, $DB; + require_once($CFG->dirroot.'/mnet/xmlrpc/client.php'); + + $peer = new mnet_peer(); + $peer->set_id($remotecourse->hostid); + + $request = new mnet_xmlrpc_client(); + $request->set_method('enrol/mnet/enrol.php/unenrol_user'); + $request->add_param($user->username); + $request->add_param($remotecourse->remoteid); + + if ($request->send($peer) === true) { + if ($request->response === true) { + // clear the cached information + $DB->delete_records('mnetservice_enrol_enrolments', + array('hostid'=>$peer->id, 'userid'=>$user->id, 'remotecourseid'=>$remotecourse->remoteid, 'enroltype'=>'mnet')); + return true; + + } else { + return serialize(array('invalid response: '.print_r($request->response, true))); + } + + } else { + return serialize($request->error); + } + } + /** * Prepares error messages returned by our XML-RPC requests to be send as debug info to {@link print_error()} *