/local/ctlplugins/enrol/plugin.class.php

https://github.com/dataview/Moodle2_NZ_Schools · PHP · 279 lines · 193 code · 40 blank · 46 comment · 29 complexity · b5687c7eef4afaef5abc647c6d394551 MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * @author Francois Marier <francois@catalyst.net.nz>
  5. * @version 1.0
  6. * @license http://www.gnu.org/copyleft/gpl.html GPLv3+
  7. * @package local
  8. *
  9. */
  10. if (false) $DB = new moodle_database();
  11. require_once "$CFG->dirroot/local/moodlectl_utils.php";
  12. /**
  13. * Functions related to course enrolment
  14. */
  15. class moodlectl_plugin_enrol extends moodlectl_plugin_base {
  16. function help() {
  17. return
  18. array(
  19. 'user-has-role' => " Check a user has a given role in a course:
  20. moodlectl user-has-role --userid=4 --courseid=100 --roleid=2
  21. moodlectl user-has-role --username=bob --coursename=CF101 --rolename=teacher",
  22. 'enrol-user' => " Enrol a user into a course with a given role:
  23. moodlectl enrol-user --userid=4 --courseid=100 --roleid=2
  24. moodlectl enrol-user --username=bob --coursename=CF101 --rolename=teacher",
  25. 'unenrol-user' => " Remove a user role in a course:
  26. moodlectl unenrol-user --userid=4 --courseid=100 --roleid=2
  27. moodlectl unenrol-user --username=bob --coursename=CF101 --rolename=student",
  28. 'enrol-student' => " Enrol a student into a course:
  29. moodlectl enrol-student --userid=4 --courseid=100
  30. moodlectl enrol-student --username=bob --coursename=CF101",
  31. 'unenrol-student' => " Remove a student from a course:
  32. moodlectl unenrol-student --userid=4 --courseid=100
  33. moodlectl unenrol-student --username=bob --coursename=CF101",
  34. 'enrol-teacher' => " Enrol a teacher into a course:
  35. moodlectl enrol-teacher --userid=4 --courseid=100
  36. moodlectl enrol-teacher --username=bob --coursename=CF101",
  37. 'unenrol-teacher' => " Remove a teacher from a course:
  38. moodlectl unenrol-teacher --userid=4 --courseid=100
  39. moodlectl unenrol-teacher --username=bob --coursename=CF101",
  40. );
  41. }
  42. function command_line_options() {
  43. global $CFG;
  44. return array(
  45. 'user-has-role' =>
  46. array(
  47. // Used to lookup the user
  48. array('long' => 'userid', 'required' => false, 'default' => 0, 'type' => 'int'),
  49. array('long' => 'username', 'required' => false, 'default' => ''),
  50. array('long' => 'emailaddress', 'required' => false, 'default' => ''),
  51. // Used to lookup the course
  52. array('long' => 'courseid', 'required' => false, 'default' => 0, 'type' => 'int'),
  53. array('long' => 'coursename', 'required' => false, 'default' => ''),
  54. // Used to lookup the role
  55. array('long' => 'roleid', 'required' => false, 'default' => 0, 'type' => 'int'),
  56. array('long' => 'rolename', 'required' => false, 'default' => ''),
  57. ),
  58. 'enrol-user' =>
  59. array(
  60. // Used to lookup the user
  61. array('long' => 'userid', 'required' => false, 'default' => 0, 'type' => 'int'),
  62. array('long' => 'username', 'required' => false, 'default' => ''),
  63. array('long' => 'emailaddress', 'required' => false, 'default' => ''),
  64. // Used to lookup the course
  65. array('long' => 'courseid', 'required' => false, 'default' => 0, 'type' => 'int'),
  66. array('long' => 'coursename', 'required' => false, 'default' => ''),
  67. // Used to lookup the role
  68. array('long' => 'roleid', 'required' => false, 'default' => 0, 'type' => 'int'),
  69. array('long' => 'rolename', 'required' => false, 'default' => ''),
  70. ),
  71. 'unenrol-user' =>
  72. array(
  73. // Used to lookup the user
  74. array('long' => 'userid', 'required' => false, 'default' => 0, 'type' => 'int'),
  75. array('long' => 'username', 'required' => false, 'default' => ''),
  76. array('long' => 'emailaddress', 'required' => false, 'default' => ''),
  77. // Used to lookup the course
  78. array('long' => 'courseid', 'required' => false, 'default' => 0, 'type' => 'int'),
  79. array('long' => 'coursename', 'required' => false, 'default' => ''),
  80. // Used to lookup the role
  81. array('long' => 'roleid', 'required' => false, 'default' => 0, 'type' => 'int'),
  82. array('long' => 'rolename', 'required' => false, 'default' => ''),
  83. ),
  84. 'enrol-student' =>
  85. array(
  86. // Used to lookup the user
  87. array('long' => 'userid', 'required' => false, 'default' => 0, 'type' => 'int'),
  88. array('long' => 'username', 'required' => false, 'default' => ''),
  89. array('long' => 'emailaddress', 'required' => false, 'default' => ''),
  90. // Used to lookup the course
  91. array('long' => 'courseid', 'required' => false, 'default' => 0, 'type' => 'int'),
  92. array('long' => 'coursename', 'required' => false, 'default' => ''),
  93. ),
  94. 'unenrol-student' =>
  95. array(
  96. // Used to lookup the user
  97. array('long' => 'userid', 'required' => false, 'default' => 0, 'type' => 'int'),
  98. array('long' => 'username', 'required' => false, 'default' => ''),
  99. array('long' => 'emailaddress', 'required' => false, 'default' => ''),
  100. // Used to lookup the course
  101. array('long' => 'courseid', 'required' => false, 'default' => 0, 'type' => 'int'),
  102. array('long' => 'coursename', 'required' => false, 'default' => ''),
  103. ),
  104. 'enrol-teacher' =>
  105. array(
  106. // Used to lookup the user
  107. array('long' => 'userid', 'required' => false, 'default' => 0, 'type' => 'int'),
  108. array('long' => 'username', 'required' => false, 'default' => ''),
  109. array('long' => 'emailaddress', 'required' => false, 'default' => ''),
  110. // Used to lookup the course
  111. array('long' => 'courseid', 'required' => false, 'default' => 0, 'type' => 'int'),
  112. array('long' => 'coursename', 'required' => false, 'default' => ''),
  113. ),
  114. 'unenrol-teacher' =>
  115. array(
  116. // Used to lookup the user
  117. array('long' => 'userid', 'required' => false, 'default' => 0, 'type' => 'int'),
  118. array('long' => 'username', 'required' => false, 'default' => ''),
  119. array('long' => 'emailaddress', 'required' => false, 'default' => ''),
  120. // Used to lookup the course
  121. array('long' => 'courseid', 'required' => false, 'default' => 0, 'type' => 'int'),
  122. array('long' => 'coursename', 'required' => false, 'default' => ''),
  123. ),
  124. );
  125. }
  126. function execute($action, $options, $mode, $format) {
  127. switch ($action) {
  128. case 'user-has-role':
  129. return moodlectl_plugin_enrol::user_has_role($options, false);
  130. break;
  131. case 'enrol-user':
  132. return moodlectl_plugin_enrol::enrol_user($options, $format, false);
  133. break;
  134. case 'unenrol-user':
  135. return moodlectl_plugin_enrol::enrol_user($options, $format, true);
  136. break;
  137. case 'enrol-student':
  138. return moodlectl_plugin_enrol::enrol_user($options, $format, false, 'student');
  139. break;
  140. case 'unenrol-student':
  141. return moodlectl_plugin_enrol::enrol_user($options, $format, true, 'student');
  142. break;
  143. case 'enrol-teacher':
  144. return moodlectl_plugin_enrol::enrol_user($options, $format, false, 'teacher');
  145. break;
  146. case 'unenrol-teacher':
  147. return moodlectl_plugin_enrol::enrol_user($options, $format, true, 'teacher');
  148. break;
  149. default:
  150. return new Exception(get_string('missingaction', MOODLECTL_LANG, $action));
  151. }
  152. }
  153. /**
  154. * Assign/unassign the a role to a user in a course
  155. *
  156. * @param array $options One of these options will allow us to match user, course and role records
  157. * @param string $format Format of input/output
  158. * @param boolean $unenrol True to unassign (unenrol) the user, False (the default) otherwise
  159. * @param string $rolename Shortname of the role to assign if not available in $options
  160. * @return boolean - true success | false failure | or Exception()
  161. */
  162. static function enrol_user($options, $format, $unenrol=false, $rolename='') {
  163. global $DB;
  164. $userparams = array('userid' => 'id', 'username' => 'username', 'emailaddress' => 'email');
  165. $user = find_matching_record('user', $userparams, $options);
  166. if (is_object($user) && get_class($user) == 'Exception') {
  167. return $user;
  168. }
  169. $courseparams = array('courseid' => 'id', 'coursename' => 'shortname');
  170. $course = find_matching_record('course', $courseparams, $options);
  171. if (is_object($course) && get_class($course) == 'Exception') {
  172. return $course;
  173. }
  174. if (empty($rolename)) {
  175. $roleparams = array('roleid' => 'id', 'rolename' => 'shortname');
  176. $role = find_matching_record('role', $roleparams, $options);
  177. if (is_object($role) && get_class($role) == 'Exception') {
  178. return $role;
  179. }
  180. }
  181. else {
  182. $role = $DB->get_record('role', array('shortname'=>$rolename));
  183. }
  184. $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
  185. if (!$unenrol) {
  186. if (!role_assign($role->id, $user->id, $coursecontext->id)) {
  187. return new Exception(get_string('enrolfailed', MOODLECTL_LANG, $action));
  188. }
  189. }
  190. else {
  191. if (!role_unassign($role->id, $user->id, $coursecontext->id)) {
  192. return new Exception(get_string('unenrolfailed', MOODLECTL_LANG, $action));
  193. }
  194. }
  195. # hackety hack, based on enrol/manual/manage.php code..
  196. if (!$enrol_manual = enrol_get_plugin('manual')) {
  197. throw new coding_exception('Can not instantiate enrol_manual');
  198. }
  199. $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'manual'), '*', MUST_EXIST); //slightly modified to use courseid
  200. $course = $DB->get_record('course', array('id'=>$instance->courseid), '*', MUST_EXIST);
  201. $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
  202. $enrol_manual->enrol_user($instance, $user->id, $role->id);
  203. }
  204. /**
  205. * check that a user has a role in a course
  206. *
  207. * @param array $options One of these options will allow us to match user, course and role records
  208. * @param string $rolename Shortname of the role to assign if not available in $options
  209. * @return boolean - true success | false failure | or Exception()
  210. */
  211. static function user_has_role($options, $rolename='') {
  212. global $DB;
  213. $userparams = array('userid' => 'id', 'username' => 'username', 'emailaddress' => 'email');
  214. $user = find_matching_record('user', $userparams, $options);
  215. if (is_object($user) && get_class($user) == 'Exception') {
  216. return $user;
  217. }
  218. $courseparams = array('courseid' => 'id', 'coursename' => 'shortname');
  219. $course = find_matching_record('course', $courseparams, $options);
  220. if (is_object($course) && get_class($course) == 'Exception') {
  221. return $course;
  222. }
  223. if (empty($rolename)) {
  224. $roleparams = array('roleid' => 'id', 'rolename' => 'shortname');
  225. $role = find_matching_record('role', $roleparams, $options);
  226. if (is_object($role) && get_class($role) == 'Exception') {
  227. return $role;
  228. }
  229. }
  230. else {
  231. //$role = get_record('role', 'shortname', $rolename);
  232. $role = $DB->get_record('role', array('shortname'=>$rolename));
  233. }
  234. $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
  235. $result = user_has_role_assignment( $user->id, $role->id, $coursecontext->id);
  236. return $result;
  237. }
  238. }
  239. ?>