PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/enrol/mnet/enrol.php

https://github.com/kpike/moodle
PHP | 356 lines | 184 code | 50 blank | 122 comment | 30 complexity | cd3ec2934dea946a50544c5abaedc5ae MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Implements the XML-RPC methods this plugin publishes to MNet peers
  18. *
  19. * This file must be named enrol.php because current MNet framework has the
  20. * filename hardcoded in XML-RPC path and we want to be compatible with
  21. * Moodle 1.x MNet clients. There is a proposal in MDL-21993 to allow
  22. * map XMP-RPC calls to whatever file, function, class or methods. Once this
  23. * is fixed, this file will be probably renamed to mnetlib.php (which could
  24. * be a common name of a plugin library containing functions/methods callable
  25. * via MNet framework.
  26. *
  27. * @package enrol
  28. * @subpackage mnet
  29. * @copyright 2010 David Mudrak <david@moodle.com>
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. defined('MOODLE_INTERNAL') || die();
  33. /**
  34. * MNet server-side methods that are part of mnetservice_enrol
  35. *
  36. * The weird name of the class tries to follow a pattern
  37. * {plugintype}_{pluginnname}_mnetservice_{servicename}
  38. *
  39. * Class methods are compatible with API 1 of the service used by Moodle 1.x
  40. * and 2.0 peers. The API version might become a part of class name but it is
  41. * not neccessary due to how xml-rcp methods are/will be mapped to php methods.
  42. */
  43. class enrol_mnet_mnetservice_enrol {
  44. /**
  45. * Returns list of courses that we offer to the caller for remote enrolment of their users
  46. *
  47. * Since Moodle 2.0, courses are made available for MNet peers by creating an instance
  48. * of enrol_mnet plugin for the course. Hidden courses are not returned. If there are two
  49. * instances - one specific for the host and one for 'All hosts', the setting of the specific
  50. * one is used. The id of the peer is kept in customint1, no other custom fields are used.
  51. *
  52. * @uses mnet_remote_client Callable via XML-RPC only
  53. * @return array
  54. */
  55. public function available_courses() {
  56. global $CFG, $DB;
  57. require_once($CFG->libdir.'/filelib.php');
  58. if (!$client = get_mnet_remote_client()) {
  59. die('Callable via XML-RPC only');
  60. }
  61. // we call our id as 'remoteid' because it will be sent to the peer
  62. // the column aliases are required by MNet protocol API for clients 1.x and 2.0
  63. $sql = "SELECT c.id AS remoteid, c.fullname, c.shortname, c.idnumber, c.summary, c.summaryformat,
  64. c.sortorder, c.startdate, cat.id AS cat_id, cat.name AS cat_name,
  65. cat.description AS cat_description, cat.descriptionformat AS cat_descriptionformat,
  66. e.cost, e.currency, e.roleid AS defaultroleid, r.name AS defaultrolename,
  67. e.customint1
  68. FROM {enrol} e
  69. INNER JOIN {course} c ON c.id = e.courseid
  70. INNER JOIN {course_categories} cat ON cat.id = c.category
  71. INNER JOIN {role} r ON r.id = e.roleid
  72. WHERE e.enrol = 'mnet'
  73. AND (e.customint1 = 0 OR e.customint1 = ?)
  74. AND c.visible = 1
  75. ORDER BY cat.sortorder, c.sortorder, c.shortname";
  76. $rs = $DB->get_recordset_sql($sql, array($client->id));
  77. $courses = array();
  78. foreach ($rs as $course) {
  79. // use the record if it does not exist yet or is host-specific
  80. if (empty($courses[$course->remoteid]) or ($course->customint1 > 0)) {
  81. unset($course->customint1); // the client does not need to know this
  82. $context = get_context_instance(CONTEXT_COURSE, $course->remoteid);
  83. // Rewrite file URLs so that they are correct
  84. $course->summary = file_rewrite_pluginfile_urls($course->summary, 'pluginfile.php', $context->id, 'course', 'summary', false);
  85. $courses[$course->remoteid] = $course;
  86. }
  87. }
  88. $rs->close();
  89. return array_values($courses); // can not use keys for backward compatibility
  90. }
  91. /**
  92. * This method has never been implemented in Moodle MNet API
  93. *
  94. * @uses mnet_remote_client Callable via XML-RPC only
  95. * @return array empty array
  96. */
  97. public function user_enrolments() {
  98. global $CFG, $DB;
  99. if (!$client = get_mnet_remote_client()) {
  100. die('Callable via XML-RPC only');
  101. }
  102. return array();
  103. }
  104. /**
  105. * Enrol remote user to our course
  106. *
  107. * If we do not have local record for the remote user in our database,
  108. * it gets created here.
  109. *
  110. * @uses mnet_remote_client Callable via XML-RPC only
  111. * @param array $userdata user details {@see mnet_fields_to_import()}
  112. * @param int $courseid our local course id
  113. * @return bool true if the enrolment has been successful, throws exception otherwise
  114. */
  115. public function enrol_user(array $userdata, $courseid) {
  116. global $CFG, $DB;
  117. require_once(dirname(__FILE__).'/lib.php');
  118. if (!$client = get_mnet_remote_client()) {
  119. die('Callable via XML-RPC only');
  120. }
  121. if (empty($userdata['username'])) {
  122. throw new mnet_server_exception(5021, 'emptyusername', 'enrol_mnet');
  123. }
  124. // do we know the remote user?
  125. $user = $DB->get_record('user', array('username'=>$userdata['username'], 'mnethostid'=>$client->id));
  126. if ($user === false) {
  127. // here we could check the setting if the enrol_mnet is allowed to auto-register
  128. // users {@link http://tracker.moodle.org/browse/MDL-21327}
  129. $user = mnet_strip_user((object)$userdata, mnet_fields_to_import($client));
  130. $user->mnethostid = $client->id;
  131. try {
  132. $user->id = $DB->insert_record('user', $user);
  133. } catch (Exception $e) {
  134. throw new mnet_server_exception(5011, 'couldnotcreateuser', 'enrol_mnet');
  135. }
  136. }
  137. if (! $course = $DB->get_record('course', array('id'=>$courseid))) {
  138. throw new mnet_server_exception(5012, 'coursenotfound', 'enrol_mnet');
  139. }
  140. $courses = $this->available_courses();
  141. $isavailable = false;
  142. foreach ($courses as $available) {
  143. if ($available->remoteid == $course->id) {
  144. $isavailable = true;
  145. break;
  146. }
  147. }
  148. if (!$isavailable) {
  149. throw new mnet_server_exception(5013, 'courseunavailable', 'enrol_mnet');
  150. }
  151. // try to load host specific enrol_mnet instance first
  152. $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'mnet', 'customint1'=>$client->id), '*', IGNORE_MISSING);
  153. if ($instance === false) {
  154. // if not found, try to load instance for all hosts
  155. $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'mnet', 'customint1'=>0), '*', IGNORE_MISSING);
  156. }
  157. if ($instance === false) {
  158. // this should not happen as the course was returned by {@see self::available_courses()}
  159. throw new mnet_server_exception(5017, 'noenrolinstance', 'enrol_mnet');
  160. }
  161. if (!$enrol = enrol_get_plugin('mnet')) {
  162. throw new mnet_server_exception(5018, 'couldnotinstantiate', 'enrol_mnet');
  163. }
  164. try {
  165. $enrol->enrol_user($instance, $user->id, $instance->roleid, time());
  166. } catch (Exception $e) {
  167. throw new mnet_server_exception(5019, 'couldnotenrol', 'enrol_mnet', $e->getMessage());
  168. }
  169. return true;
  170. }
  171. /**
  172. * Unenrol remote user from our course
  173. *
  174. * Only users enrolled via enrol_mnet plugin can be unenrolled remotely. If the
  175. * remote user is enrolled into the local course via some other enrol plugin
  176. * (enrol_manual for example), the remote host can't touch such enrolment. Please
  177. * do not report this behaviour as bug, it is a feature ;-)
  178. *
  179. * @uses mnet_remote_client Callable via XML-RPC only
  180. * @param string $username of the remote user
  181. * @param int $courseid of our local course
  182. * @return bool true if the unenrolment has been successful, throws exception otherwise
  183. */
  184. public function unenrol_user($username, $courseid) {
  185. global $CFG, $DB;
  186. if (!$client = get_mnet_remote_client()) {
  187. die('Callable via XML-RPC only');
  188. }
  189. $user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$client->id));
  190. if ($user === false) {
  191. throw new mnet_server_exception(5014, 'usernotfound', 'enrol_mnet');
  192. }
  193. if (! $course = $DB->get_record('course', array('id'=>$courseid))) {
  194. throw new mnet_server_exception(5012, 'coursenotfound', 'enrol_mnet');
  195. }
  196. $courses = $this->available_courses();
  197. $isavailable = false;
  198. foreach ($courses as $available) {
  199. if ($available->remoteid == $course->id) {
  200. $isavailable = true;
  201. break;
  202. }
  203. }
  204. if (!$isavailable) {
  205. // if they can not enrol, they can not unenrol
  206. throw new mnet_server_exception(5013, 'courseunavailable', 'enrol_mnet');
  207. }
  208. // try to load host specific enrol_mnet instance first
  209. $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'mnet', 'customint1'=>$client->id), '*', IGNORE_MISSING);
  210. if ($instance === false) {
  211. // if not found, try to load instance for all hosts
  212. $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'mnet', 'customint1'=>0), '*', IGNORE_MISSING);
  213. $instanceforall = true;
  214. }
  215. if ($instance === false) {
  216. // this should not happen as the course was returned by {@see self::available_courses()}
  217. throw new mnet_server_exception(5017, 'noenrolinstance', 'enrol_mnet');
  218. }
  219. if (!$enrol = enrol_get_plugin('mnet')) {
  220. throw new mnet_server_exception(5018, 'couldnotinstantiate', 'enrol_mnet');
  221. }
  222. if ($DB->record_exists('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$user->id))) {
  223. try {
  224. $enrol->unenrol_user($instance, $user->id);
  225. } catch (Exception $e) {
  226. throw new mnet_server_exception(5020, 'couldnotunenrol', 'enrol_mnet', $e->getMessage());
  227. }
  228. }
  229. if (empty($instanceforall)) {
  230. // if the user was enrolled via 'All hosts' instance and the specific one
  231. // was created after that, the first enrolment would be kept.
  232. $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'mnet', 'customint1'=>0), '*', IGNORE_MISSING);
  233. if ($instance) {
  234. // repeat the same procedure for 'All hosts' instance, too. Note that as the host specific
  235. // instance exists, it will be used for the future enrolments
  236. if ($DB->record_exists('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$user->id))) {
  237. try {
  238. $enrol->unenrol_user($instance, $user->id);
  239. } catch (Exception $e) {
  240. throw new mnet_server_exception(5020, 'couldnotunenrol', 'enrol_mnet', $e->getMessage());
  241. }
  242. }
  243. }
  244. }
  245. return true;
  246. }
  247. /**
  248. * Returns a list of users from the client server who are enrolled in our course
  249. *
  250. * Suitable instance of enrol_mnet must be created in the course. This method will not
  251. * return any information about the enrolments in courses that are not available for
  252. * remote enrolment, even if their users are enrolled into them via other plugin
  253. * (note the difference from {@link self::user_enrolments()}).
  254. *
  255. * This method will return enrolment information for users from hosts regardless
  256. * the enrolment plugin. It does not matter if the user was enrolled remotely by
  257. * their admin or locally. Once the course is available for remote enrolments, we
  258. * will tell them everything about their users.
  259. *
  260. * In Moodle 1.x the returned array used to be indexed by username. The side effect
  261. * of MDL-19219 fix is that we do not need to use such index and therefore we can
  262. * return all enrolment records. MNet clients 1.x will only use the last record for
  263. * the student, if she is enrolled via multiple plugins.
  264. *
  265. * @uses mnet_remote_client Callable via XML-RPC only
  266. * @param int $courseid ID of our course
  267. * @param string|array $roles comma separated list of role shortnames (or array of them)
  268. * @return array
  269. */
  270. public function course_enrolments($courseid, $roles=null) {
  271. global $DB, $CFG;
  272. if (!$client = get_mnet_remote_client()) {
  273. die('Callable via XML-RPC only');
  274. }
  275. $sql = "SELECT u.username, r.shortname, r.name, e.enrol, ue.timemodified
  276. FROM {user_enrolments} ue
  277. JOIN {user} u ON ue.userid = u.id
  278. JOIN {enrol} e ON ue.enrolid = e.id
  279. JOIN {role} r ON e.roleid = r.id
  280. WHERE u.mnethostid = :mnethostid
  281. AND e.courseid = :courseid
  282. AND u.id <> :guestid
  283. AND u.confirmed = 1
  284. AND u.deleted = 0";
  285. $params['mnethostid'] = $client->id;
  286. $params['courseid'] = $courseid;
  287. $params['guestid'] = $CFG->siteguest;
  288. if (!is_null($roles)) {
  289. if (!is_array($roles)) {
  290. $roles = explode(',', $roles);
  291. }
  292. $roles = array_map('trim', $roles);
  293. list($rsql, $rparams) = $DB->get_in_or_equal($roles, SQL_PARAMS_NAMED);
  294. $sql .= " AND r.shortname $rsql";
  295. $params = array_merge($params, $rparams);
  296. }
  297. $sql .= " ORDER BY u.lastname, u.firstname";
  298. $rs = $DB->get_recordset_sql($sql, $params);
  299. $list = array();
  300. foreach ($rs as $record) {
  301. $list[] = $record;
  302. }
  303. $rs->close();
  304. return $list;
  305. }
  306. }