|
Moodle
2.2.1
http://www.collinsharper.com
|
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 00035 defined('MOODLE_INTERNAL') || die(); 00036 00047 class enrol_mnet_mnetservice_enrol { 00048 00060 public function available_courses() { 00061 global $CFG, $DB; 00062 require_once($CFG->libdir.'/filelib.php'); 00063 00064 if (!$client = get_mnet_remote_client()) { 00065 die('Callable via XML-RPC only'); 00066 } 00067 00068 // we call our id as 'remoteid' because it will be sent to the peer 00069 // the column aliases are required by MNet protocol API for clients 1.x and 2.0 00070 $sql = "SELECT c.id AS remoteid, c.fullname, c.shortname, c.idnumber, c.summary, c.summaryformat, 00071 c.sortorder, c.startdate, cat.id AS cat_id, cat.name AS cat_name, 00072 cat.description AS cat_description, cat.descriptionformat AS cat_descriptionformat, 00073 e.cost, e.currency, e.roleid AS defaultroleid, r.name AS defaultrolename, 00074 e.customint1 00075 FROM {enrol} e 00076 INNER JOIN {course} c ON c.id = e.courseid 00077 INNER JOIN {course_categories} cat ON cat.id = c.category 00078 INNER JOIN {role} r ON r.id = e.roleid 00079 WHERE e.enrol = 'mnet' 00080 AND (e.customint1 = 0 OR e.customint1 = ?) 00081 AND c.visible = 1 00082 ORDER BY cat.sortorder, c.sortorder, c.shortname"; 00083 00084 $rs = $DB->get_recordset_sql($sql, array($client->id)); 00085 00086 $courses = array(); 00087 foreach ($rs as $course) { 00088 // use the record if it does not exist yet or is host-specific 00089 if (empty($courses[$course->remoteid]) or ($course->customint1 > 0)) { 00090 unset($course->customint1); // the client does not need to know this 00091 $context = get_context_instance(CONTEXT_COURSE, $course->remoteid); 00092 // Rewrite file URLs so that they are correct 00093 $course->summary = file_rewrite_pluginfile_urls($course->summary, 'pluginfile.php', $context->id, 'course', 'summary', false); 00094 $courses[$course->remoteid] = $course; 00095 } 00096 } 00097 $rs->close(); 00098 00099 return array_values($courses); // can not use keys for backward compatibility 00100 } 00101 00108 public function user_enrolments() { 00109 global $CFG, $DB; 00110 00111 if (!$client = get_mnet_remote_client()) { 00112 die('Callable via XML-RPC only'); 00113 } 00114 return array(); 00115 } 00116 00128 public function enrol_user(array $userdata, $courseid) { 00129 global $CFG, $DB; 00130 require_once(dirname(__FILE__).'/lib.php'); 00131 00132 if (!$client = get_mnet_remote_client()) { 00133 die('Callable via XML-RPC only'); 00134 } 00135 00136 if (empty($userdata['username'])) { 00137 throw new mnet_server_exception(5021, 'emptyusername', 'enrol_mnet'); 00138 } 00139 00140 // do we know the remote user? 00141 $user = $DB->get_record('user', array('username'=>$userdata['username'], 'mnethostid'=>$client->id)); 00142 00143 if ($user === false) { 00144 // here we could check the setting if the enrol_mnet is allowed to auto-register 00145 // users {@link http://tracker.moodle.org/browse/MDL-21327} 00146 $user = mnet_strip_user((object)$userdata, mnet_fields_to_import($client)); 00147 $user->mnethostid = $client->id; 00148 try { 00149 $user->id = $DB->insert_record('user', $user); 00150 } catch (Exception $e) { 00151 throw new mnet_server_exception(5011, 'couldnotcreateuser', 'enrol_mnet'); 00152 } 00153 } 00154 00155 if (! $course = $DB->get_record('course', array('id'=>$courseid))) { 00156 throw new mnet_server_exception(5012, 'coursenotfound', 'enrol_mnet'); 00157 } 00158 00159 $courses = $this->available_courses(); 00160 $isavailable = false; 00161 foreach ($courses as $available) { 00162 if ($available->remoteid == $course->id) { 00163 $isavailable = true; 00164 break; 00165 } 00166 } 00167 if (!$isavailable) { 00168 throw new mnet_server_exception(5013, 'courseunavailable', 'enrol_mnet'); 00169 } 00170 00171 // try to load host specific enrol_mnet instance first 00172 $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'mnet', 'customint1'=>$client->id), '*', IGNORE_MISSING); 00173 00174 if ($instance === false) { 00175 // if not found, try to load instance for all hosts 00176 $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'mnet', 'customint1'=>0), '*', IGNORE_MISSING); 00177 } 00178 00179 if ($instance === false) { 00180 // this should not happen as the course was returned by {@see self::available_courses()} 00181 throw new mnet_server_exception(5017, 'noenrolinstance', 'enrol_mnet'); 00182 } 00183 00184 if (!$enrol = enrol_get_plugin('mnet')) { 00185 throw new mnet_server_exception(5018, 'couldnotinstantiate', 'enrol_mnet'); 00186 } 00187 00188 try { 00189 $enrol->enrol_user($instance, $user->id, $instance->roleid, time()); 00190 00191 } catch (Exception $e) { 00192 throw new mnet_server_exception(5019, 'couldnotenrol', 'enrol_mnet', $e->getMessage()); 00193 } 00194 00195 return true; 00196 } 00197 00211 public function unenrol_user($username, $courseid) { 00212 global $CFG, $DB; 00213 00214 if (!$client = get_mnet_remote_client()) { 00215 die('Callable via XML-RPC only'); 00216 } 00217 00218 $user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$client->id)); 00219 00220 if ($user === false) { 00221 throw new mnet_server_exception(5014, 'usernotfound', 'enrol_mnet'); 00222 } 00223 00224 if (! $course = $DB->get_record('course', array('id'=>$courseid))) { 00225 throw new mnet_server_exception(5012, 'coursenotfound', 'enrol_mnet'); 00226 } 00227 00228 $courses = $this->available_courses(); 00229 $isavailable = false; 00230 foreach ($courses as $available) { 00231 if ($available->remoteid == $course->id) { 00232 $isavailable = true; 00233 break; 00234 } 00235 } 00236 if (!$isavailable) { 00237 // if they can not enrol, they can not unenrol 00238 throw new mnet_server_exception(5013, 'courseunavailable', 'enrol_mnet'); 00239 } 00240 00241 // try to load host specific enrol_mnet instance first 00242 $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'mnet', 'customint1'=>$client->id), '*', IGNORE_MISSING); 00243 00244 if ($instance === false) { 00245 // if not found, try to load instance for all hosts 00246 $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'mnet', 'customint1'=>0), '*', IGNORE_MISSING); 00247 $instanceforall = true; 00248 } 00249 00250 if ($instance === false) { 00251 // this should not happen as the course was returned by {@see self::available_courses()} 00252 throw new mnet_server_exception(5017, 'noenrolinstance', 'enrol_mnet'); 00253 } 00254 00255 if (!$enrol = enrol_get_plugin('mnet')) { 00256 throw new mnet_server_exception(5018, 'couldnotinstantiate', 'enrol_mnet'); 00257 } 00258 00259 if ($DB->record_exists('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$user->id))) { 00260 try { 00261 $enrol->unenrol_user($instance, $user->id); 00262 00263 } catch (Exception $e) { 00264 throw new mnet_server_exception(5020, 'couldnotunenrol', 'enrol_mnet', $e->getMessage()); 00265 } 00266 } 00267 00268 if (empty($instanceforall)) { 00269 // if the user was enrolled via 'All hosts' instance and the specific one 00270 // was created after that, the first enrolment would be kept. 00271 $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'mnet', 'customint1'=>0), '*', IGNORE_MISSING); 00272 00273 if ($instance) { 00274 // repeat the same procedure for 'All hosts' instance, too. Note that as the host specific 00275 // instance exists, it will be used for the future enrolments 00276 00277 if ($DB->record_exists('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$user->id))) { 00278 try { 00279 $enrol->unenrol_user($instance, $user->id); 00280 00281 } catch (Exception $e) { 00282 throw new mnet_server_exception(5020, 'couldnotunenrol', 'enrol_mnet', $e->getMessage()); 00283 } 00284 } 00285 } 00286 } 00287 00288 return true; 00289 } 00290 00314 public function course_enrolments($courseid, $roles=null) { 00315 global $DB, $CFG; 00316 00317 if (!$client = get_mnet_remote_client()) { 00318 die('Callable via XML-RPC only'); 00319 } 00320 00321 $sql = "SELECT u.username, r.shortname, r.name, e.enrol, ue.timemodified 00322 FROM {user_enrolments} ue 00323 JOIN {user} u ON ue.userid = u.id 00324 JOIN {enrol} e ON ue.enrolid = e.id 00325 JOIN {role} r ON e.roleid = r.id 00326 WHERE u.mnethostid = :mnethostid 00327 AND e.courseid = :courseid 00328 AND u.id <> :guestid 00329 AND u.confirmed = 1 00330 AND u.deleted = 0"; 00331 $params['mnethostid'] = $client->id; 00332 $params['courseid'] = $courseid; 00333 $params['guestid'] = $CFG->siteguest; 00334 00335 if (!is_null($roles)) { 00336 if (!is_array($roles)) { 00337 $roles = explode(',', $roles); 00338 } 00339 $roles = array_map('trim', $roles); 00340 list($rsql, $rparams) = $DB->get_in_or_equal($roles, SQL_PARAMS_NAMED); 00341 $sql .= " AND r.shortname $rsql"; 00342 $params = array_merge($params, $rparams); 00343 } 00344 00345 $sql .= " ORDER BY u.lastname, u.firstname"; 00346 00347 $rs = $DB->get_recordset_sql($sql, $params); 00348 $list = array(); 00349 foreach ($rs as $record) { 00350 $list[] = $record; 00351 } 00352 $rs->close(); 00353 00354 return $list; 00355 } 00356 }