Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mnet/service/enrol/locallib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00030 defined('MOODLE_INTERNAL') || die();
00031 
00032 require_once($CFG->dirroot . '/user/selector/lib.php');
00033 
00037 class mnetservice_enrol {
00038 
00040     protected static $singleton;
00041 
00043     protected $cachesubscribers = null;
00044 
00046     protected $cachepublishers = null;
00047 
00051     protected function __construct() {
00052     }
00053 
00057     public static function get_instance() {
00058         if (is_null(self::$singleton)) {
00059             self::$singleton = new self();
00060         }
00061         return self::$singleton;
00062     }
00063 
00074     public function is_available() {
00075         global $CFG;
00076 
00077         if (empty($CFG->mnet_dispatcher_mode) || $CFG->mnet_dispatcher_mode !== 'strict') {
00078             return false;
00079         }
00080         return true;
00081     }
00082 
00093     public function get_remote_subscribers() {
00094         global $DB;
00095 
00096         if (is_null($this->cachesubscribers)) {
00097             $sql = "SELECT DISTINCT h.id, h.name AS hostname, h.wwwroot AS hosturl,
00098                            a.display_name AS appname
00099                       FROM {mnet_host} h
00100                       JOIN {mnet_host2service} hs ON h.id = hs.hostid
00101                       JOIN {mnet_service} s ON hs.serviceid = s.id
00102                       JOIN {mnet_application} a ON h.applicationid = a.id
00103                      WHERE s.name = 'mnet_enrol'
00104                            AND h.deleted = 0
00105                            AND hs.publish = 1";
00106             $this->cachesubscribers = $DB->get_records_sql($sql);
00107         }
00108 
00109         return $this->cachesubscribers;
00110     }
00111 
00122     public function get_remote_publishers() {
00123         global $DB;
00124 
00125         if (is_null($this->cachepublishers)) {
00126             $sql = "SELECT DISTINCT h.id, h.name AS hostname, h.wwwroot AS hosturl,
00127                            a.display_name AS appname
00128                       FROM {mnet_host} h
00129                       JOIN {mnet_host2service} hs ON h.id = hs.hostid
00130                       JOIN {mnet_service} s ON hs.serviceid = s.id
00131                       JOIN {mnet_application} a ON h.applicationid = a.id
00132                      WHERE s.name = 'mnet_enrol'
00133                            AND h.deleted = 0
00134                            AND hs.subscribe = 1";
00135             $this->cachepublishers = $DB->get_records_sql($sql);
00136         }
00137 
00138         return $this->cachepublishers;
00139     }
00140 
00156     public function get_remote_courses($mnethostid, $usecache=true) {
00157         global $CFG, $DB; // $CFG needed!
00158 
00159         $lastfetchcourses = get_config('mnetservice_enrol', 'lastfetchcourses');
00160         if (empty($lastfetchcourses) or (time()-$lastfetchcourses > DAYSECS)) {
00161             $usecache = false;
00162         }
00163 
00164         if ($usecache) {
00165             return $DB->get_records('mnetservice_enrol_courses', array('hostid' => $mnethostid), 'sortorder, shortname');
00166         }
00167 
00168         // do not use cache - fetch fresh list from remote MNet host
00169         require_once $CFG->dirroot.'/mnet/xmlrpc/client.php';
00170         $peer = new mnet_peer();
00171         if (!$peer->set_id($mnethostid)) {
00172             return serialize(array('unknown mnet peer'));
00173         }
00174 
00175         $request = new mnet_xmlrpc_client();
00176         $request->set_method('enrol/mnet/enrol.php/available_courses');
00177 
00178         if ($request->send($peer)) {
00179             $list = array();
00180             $response = $request->response;
00181 
00182             // get the currently cached courses key'd on remote id - only need remoteid and id fields
00183             $cachedcourses = $DB->get_records('mnetservice_enrol_courses', array('hostid' => $mnethostid), 'remoteid', 'remoteid, id');
00184 
00185             foreach ($response as &$remote) {
00186                 $course                 = new stdclass(); // record in our local cache
00187                 $course->hostid         = $mnethostid;
00188                 $course->remoteid       = (int)$remote['remoteid'];
00189                 $course->categoryid     = (int)$remote['cat_id'];
00190                 $course->categoryname   = substr($remote['cat_name'], 0, 255);
00191                 $course->sortorder      = (int)$remote['sortorder'];
00192                 $course->fullname       = substr($remote['fullname'], 0, 254);
00193                 $course->shortname      = substr($remote['shortname'], 0, 100);
00194                 $course->idnumber       = substr($remote['idnumber'], 0, 100);
00195                 $course->summary        = $remote['summary'];
00196                 $course->summaryformat  = empty($remote['summaryformat']) ? FORMAT_MOODLE : (int)$remote['summaryformat'];
00197                 $course->startdate      = (int)$remote['startdate'];
00198                 $course->roleid         = (int)$remote['defaultroleid'];
00199                 $course->rolename       = substr($remote['defaultrolename'], 0, 255);
00200                 // We do not cache the following fields returned from peer in 2.0 any more
00201                 // not cached: cat_description
00202                 // not cached: cat_descriptionformat
00203                 // not cached: cost
00204                 // not cached: currency
00205 
00206                 if (empty($cachedcourses[$course->remoteid])) {
00207                     $course->id = $DB->insert_record('mnetservice_enrol_courses', $course);
00208                 } else {
00209                     $course->id = $cachedcourses[$course->remoteid]->id;
00210                     $DB->update_record('mnetservice_enrol_courses', $course);
00211                 }
00212 
00213                 $list[$course->remoteid] = $course;
00214             }
00215 
00216             // prune stale data from cache
00217             if (!empty($cachedcourses)) {
00218                 foreach ($cachedcourses as $cachedcourse) {
00219                     if (!empty($list[$cachedcourse->remoteid])) {
00220                         unset($cachedcourses[$cachedcourse->remoteid]);
00221                     }
00222                 }
00223                 $staleremoteids = array_keys($cachedcourses);
00224                 if (!empty($staleremoteids)) {
00225                     list($sql, $params) = $DB->get_in_or_equal($staleremoteids, SQL_PARAMS_NAMED);
00226                     $select = "hostid=:hostid AND remoteid $sql";
00227                     $params['hostid'] = $mnethostid;
00228                     $DB->delete_records_select('mnetservice_enrol_courses', $select, $params);
00229                 }
00230             }
00231 
00232             // and return the fresh data
00233             set_config('lastfetchcourses', time(), 'mnetservice_enrol');
00234             return $list;
00235 
00236         } else {
00237             return serialize($request->error);
00238         }
00239     }
00240 
00257     public function req_course_enrolments($mnethostid, $remotecourseid) {
00258         global $CFG, $DB; // $CFG needed!
00259         require_once $CFG->dirroot.'/mnet/xmlrpc/client.php';
00260 
00261         if (!$DB->record_exists('mnetservice_enrol_courses', array('hostid'=>$mnethostid, 'remoteid'=>$remotecourseid))) {
00262             return serialize(array('course not available for remote enrolments'));
00263         }
00264 
00265         $peer = new mnet_peer();
00266         if (!$peer->set_id($mnethostid)) {
00267             return serialize(array('unknown mnet peer'));
00268         }
00269 
00270         $request = new mnet_xmlrpc_client();
00271         $request->set_method('enrol/mnet/enrol.php/course_enrolments');
00272         $request->add_param($remotecourseid, 'int');
00273 
00274         if ($request->send($peer)) {
00275             $list = array();
00276             $response = $request->response;
00277 
00278             // prepare a table mapping usernames of our users to their ids
00279             $usernames = array();
00280             foreach ($response as $unused => $remote) {
00281                 if (!isset($remote['username'])) {
00282                     // see MDL-19219
00283                     return serialize(array('remote host running old version of mnet server - does not return username attribute'));
00284                 }
00285                 if ($remote['username'] == 'guest') { // we can not use $CFG->siteguest here
00286                     // do not try nasty things you bastard!
00287                     continue;
00288                 }
00289                 $usernames[$remote['username']] = $remote['username'];
00290             }
00291 
00292             if (!empty($usernames)) {
00293                 list($usql, $params) = $DB->get_in_or_equal($usernames, SQL_PARAMS_NAMED);
00294                 $params['mnetlocalhostid'] = $CFG->mnet_localhost_id;
00295                 $sql = "SELECT username,id
00296                           FROM {user}
00297                          WHERE mnethostid = :mnetlocalhostid
00298                                AND username $usql
00299                                AND deleted = 0
00300                                AND confirmed = 1
00301                       ORDER BY lastname,firstname,email";
00302                 $usersbyusername = $DB->get_records_sql($sql, $params);
00303             } else {
00304                 $usersbyusername = array();
00305             }
00306 
00307             // populate the returned list and update local cache of enrolment records
00308             foreach ($response as $remote) {
00309                 if (empty($usersbyusername[$remote['username']])) {
00310                     // we do not know this user or she is deleted or not confirmed or is 'guest'
00311                     continue;
00312                 }
00313                 $enrolment                  = new stdclass();
00314                 $enrolment->hostid          = $mnethostid;
00315                 $enrolment->userid          = $usersbyusername[$remote['username']]->id;
00316                 $enrolment->remotecourseid  = $remotecourseid;
00317                 $enrolment->rolename        = $remote['name']; // $remote['shortname'] not used
00318                 $enrolment->enroltime       = $remote['timemodified'];
00319                 $enrolment->enroltype       = $remote['enrol'];
00320 
00321                 $current = $DB->get_record('mnetservice_enrol_enrolments', array('hostid'=>$enrolment->hostid, 'userid'=>$enrolment->userid,
00322                                        'remotecourseid'=>$enrolment->remotecourseid, 'enroltype'=>$enrolment->enroltype), 'id, enroltime');
00323                 if (empty($current)) {
00324                     $enrolment->id = $DB->insert_record('mnetservice_enrol_enrolments', $enrolment);
00325                 } else {
00326                     $enrolment->id = $current->id;
00327                     if ($current->enroltime != $enrolment->enroltime) {
00328                         $DB->update_record('mnetservice_enrol_enrolments', $enrolment);
00329                     }
00330                 }
00331 
00332                 $list[$enrolment->id] = $enrolment;
00333             }
00334 
00335             // prune stale enrolment records
00336             if (empty($list)) {
00337                 $DB->delete_records('mnetservice_enrol_enrolments', array('hostid'=>$mnethostid, 'remotecourseid'=>$remotecourseid));
00338             } else {
00339                 list($isql, $params) = $DB->get_in_or_equal(array_keys($list), SQL_PARAMS_NAMED, 'param', false);
00340                 $params['hostid'] = $mnethostid;
00341                 $params['remotecourseid'] = $remotecourseid;
00342                 $select = "hostid = :hostid AND remotecourseid = :remotecourseid AND id $isql";
00343                 $DB->delete_records_select('mnetservice_enrol_enrolments', $select, $params);
00344             }
00345 
00346             // store the timestamp of the recent fetch, can be used for cache invalidate purposes
00347             set_config('lastfetchenrolments', time(), 'mnetservice_enrol');
00348             // local cache successfully updated
00349             return true;
00350 
00351         } else {
00352             return serialize($request->error);
00353         }
00354     }
00355 
00366     public function req_enrol_user(stdclass $user, stdclass $remotecourse) {
00367         global $CFG, $DB;
00368         require_once($CFG->dirroot.'/mnet/xmlrpc/client.php');
00369 
00370         $peer = new mnet_peer();
00371         $peer->set_id($remotecourse->hostid);
00372 
00373         $request = new mnet_xmlrpc_client();
00374         $request->set_method('enrol/mnet/enrol.php/enrol_user');
00375         $request->add_param(mnet_strip_user((array)$user, mnet_fields_to_send($peer)));
00376         $request->add_param($remotecourse->remoteid);
00377 
00378         if ($request->send($peer) === true) {
00379             if ($request->response === true) {
00380                 // cache the enrolment information in our table
00381                 $enrolment                  = new stdclass();
00382                 $enrolment->hostid          = $peer->id;
00383                 $enrolment->userid          = $user->id;
00384                 $enrolment->remotecourseid  = $remotecourse->remoteid;
00385                 $enrolment->enroltype       = 'mnet';
00386                 // $enrolment->rolename not known now, must be re-fetched
00387                 // $enrolment->enroltime not known now, must be re-fetched
00388                 $DB->insert_record('mnetservice_enrol_enrolments', $enrolment);
00389                 return true;
00390 
00391             } else {
00392                 return serialize(array('invalid response: '.print_r($request->response, true)));
00393             }
00394 
00395         } else {
00396             return serialize($request->error);
00397         }
00398     }
00399 
00410     public function req_unenrol_user(stdclass $user, stdclass $remotecourse) {
00411         global $CFG, $DB;
00412         require_once($CFG->dirroot.'/mnet/xmlrpc/client.php');
00413 
00414         $peer = new mnet_peer();
00415         $peer->set_id($remotecourse->hostid);
00416 
00417         $request = new mnet_xmlrpc_client();
00418         $request->set_method('enrol/mnet/enrol.php/unenrol_user');
00419         $request->add_param($user->username);
00420         $request->add_param($remotecourse->remoteid);
00421 
00422         if ($request->send($peer) === true) {
00423             if ($request->response === true) {
00424                 // clear the cached information
00425                 $DB->delete_records('mnetservice_enrol_enrolments',
00426                     array('hostid'=>$peer->id, 'userid'=>$user->id, 'remotecourseid'=>$remotecourse->remoteid, 'enroltype'=>'mnet'));
00427                 return true;
00428 
00429             } else {
00430                 return serialize(array('invalid response: '.print_r($request->response, true)));
00431             }
00432 
00433         } else {
00434             return serialize($request->error);
00435         }
00436     }
00437 
00446     public function format_error_message($errormsg) {
00447         $errors = unserialize($errormsg);
00448         $output = 'mnet_xmlrpc_client request returned errors:'."\n";
00449         foreach ($errors as $error) {
00450             $output .= "$error\n";
00451         }
00452         return $output;
00453     }
00454 }
00455 
00459 class mnetservice_enrol_existing_users_selector extends user_selector_base {
00461     protected $hostid;
00463     protected $remotecourseid;
00464 
00465     public function __construct($name, $options) {
00466         $this->hostid = $options['hostid'];
00467         $this->remotecourseid = $options['remotecourseid'];
00468         parent::__construct($name, $options);
00469     }
00470 
00477     public function find_users($search) {
00478         global $DB;
00479 
00480         list($wherecondition, $params)  = $this->search_sql($search, 'u');
00481         $params['hostid']               = $this->hostid;
00482         $params['remotecourseid']       = $this->remotecourseid;
00483 
00484         $fields      = "SELECT ".$this->required_fields_sql("u");
00485         $countfields = "SELECT COUNT(1)";
00486 
00487         $sql = "          FROM {user} u
00488                           JOIN {mnetservice_enrol_enrolments} e ON e.userid = u.id
00489                          WHERE e.hostid = :hostid AND e.remotecourseid = :remotecourseid
00490                                AND e.enroltype = 'mnet'
00491                                AND $wherecondition";
00492         $order = "    ORDER BY u.lastname ASC, u.firstname ASC";
00493 
00494         if (!$this->is_validating()) {
00495             $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
00496             if ($potentialmemberscount > 100) {
00497                 return $this->too_many_results($search, $potentialmemberscount);
00498             }
00499         }
00500 
00501         $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
00502 
00503         if (empty($availableusers)) {
00504             return array();
00505         }
00506 
00507         if ($search) {
00508             $groupname = get_string('enrolledusersmatching', 'enrol', $search);
00509         } else {
00510             $groupname = get_string('enrolledusers', 'enrol');
00511         }
00512 
00513         return array($groupname => $availableusers);
00514     }
00515 
00516     protected function get_options() {
00517         $options = parent::get_options();
00518         $options['hostid'] = $this->hostid;
00519         $options['remotecourseid'] = $this->remotecourseid;
00520         $options['file'] = 'mnet/service/enrol/locallib.php';
00521         return $options;
00522     }
00523 }
00524 
00528 class mnetservice_enrol_potential_users_selector extends user_selector_base {
00530     protected $hostid;
00532     protected $remotecourseid;
00533 
00534     public function __construct($name, $options) {
00535         $this->hostid = $options['hostid'];
00536         $this->remotecourseid = $options['remotecourseid'];
00537         parent::__construct($name, $options);
00538     }
00539 
00549     public function find_users($search) {
00550         global $CFG, $DB;
00551 
00552         $systemcontext = get_system_context();
00553         $userids = get_users_by_capability($systemcontext, 'moodle/site:mnetlogintoremote', 'u.id');
00554 
00555         if (empty($userids)) {
00556             return array();
00557         }
00558 
00559         list($usql, $uparams) = $DB->get_in_or_equal(array_keys($userids), SQL_PARAMS_NAMED, 'uid');
00560 
00561         list($wherecondition, $params) = $this->search_sql($search, 'u');
00562 
00563         $params = array_merge($params, $uparams);
00564         $params['hostid'] = $this->hostid;
00565         $params['remotecourseid'] = $this->remotecourseid;
00566         $params['mnetlocalhostid'] = $CFG->mnet_localhost_id;
00567 
00568         $fields      = "SELECT ".$this->required_fields_sql("u");
00569         $countfields = "SELECT COUNT(1)";
00570 
00571         $sql = "          FROM {user} u
00572                          WHERE $wherecondition
00573                                AND u.mnethostid = :mnetlocalhostid
00574                                AND u.id $usql
00575                                AND u.id NOT IN (SELECT e.userid
00576                                                   FROM {mnetservice_enrol_enrolments} e
00577                                                  WHERE (e.hostid = :hostid AND e.remotecourseid = :remotecourseid))";
00578 
00579         $order = "    ORDER BY u.lastname ASC, u.firstname ASC";
00580 
00581         if (!$this->is_validating()) {
00582             $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
00583             if ($potentialmemberscount > 100) {
00584                 return $this->too_many_results($search, $potentialmemberscount);
00585             }
00586         }
00587 
00588         $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
00589 
00590         if (empty($availableusers)) {
00591             return array();
00592         }
00593 
00594         if ($search) {
00595             $groupname = get_string('enrolcandidatesmatching', 'enrol', $search);
00596         } else {
00597             $groupname = get_string('enrolcandidates', 'enrol');
00598         }
00599 
00600         return array($groupname => $availableusers);
00601     }
00602 
00603     protected function get_options() {
00604         $options = parent::get_options();
00605         $options['hostid'] = $this->hostid;
00606         $options['remotecourseid'] = $this->remotecourseid;
00607         $options['file'] = 'mnet/service/enrol/locallib.php';
00608         return $options;
00609     }
00610 }
 All Data Structures Namespaces Files Functions Variables Enumerations