PageRenderTime 67ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/actions/enroltocourses/index.php

https://github.com/andreev-artem/moodle_admin_report_advuserbulk
PHP | 347 lines | 271 code | 44 blank | 32 comment | 40 complexity | 5daa93ec2989da737a48fa92903ecd1e MD5 | raw file
  1. <?php
  2. /**
  3. * user bulk action script for batch user enrolment
  4. */
  5. require_once('../../../../../config.php');
  6. require_once($CFG->libdir . '/adminlib.php');
  7. require_once($CFG->dirroot . '/' . $CFG->admin . '/report/advuserbulk/lib.php');
  8. $allcourses = optional_param('allcourses', '', PARAM_CLEAN);
  9. $selcourses = optional_param('selcourses', '', PARAM_CLEAN);
  10. $accept = optional_param('accept', 0, PARAM_BOOL);
  11. $confirm = optional_param('confirm', 0, PARAM_BOOL);
  12. $cancel = optional_param('cancel', 0, PARAM_BOOL);
  13. $searchtext = optional_param('searchtext', '', PARAM_RAW);
  14. $groups = optional_param('groups', '', PARAM_CLEAN);
  15. $roleassign = optional_param('roleassign', '', PARAM_RAW);
  16. $showall = optional_param('showall', 0, PARAM_BOOL);
  17. $listadd = optional_param('add', 0, PARAM_BOOL);
  18. $listremove = optional_param('remove', 0, PARAM_BOOL);
  19. $removeall = optional_param('removeall', 0, PARAM_BOOL);
  20. admin_externalpage_setup('reportadvuserbulk');
  21. check_action_capabilities('enroltocourses', true);
  22. $return = $CFG->wwwroot . '/' . $CFG->admin . '/report/advuserbulk/user_bulk.php';
  23. if ($showall) {
  24. $searchtext = '';
  25. }
  26. $strsearch = get_string('search');
  27. $pluginname = ACTIONS_LANG_PREFIX.'enroltocourses';
  28. if (empty($SESSION->bulk_users) || $cancel) {
  29. redirect($return);
  30. }
  31. if (!isset($SESSION->bulk_courses) || $removeall) {
  32. $SESSION->bulk_courses = array();
  33. }
  34. // course selection add/remove actions
  35. if ($listadd && !empty($allcourses)) {
  36. foreach ($allcourses as $courseid) {
  37. if (!in_array($courseid, $SESSION->bulk_courses)) {
  38. $SESSION->bulk_courses[] = $courseid;
  39. }
  40. }
  41. }
  42. if ($listremove && !empty($selcourses)) {
  43. foreach ($selcourses as $courseid) {
  44. unset($SESSION->bulk_courses[array_search($courseid, $SESSION->bulk_courses)]);
  45. }
  46. }
  47. // show the confirmation message
  48. if ($accept) {
  49. global $DB;
  50. if (empty($SESSION->bulk_courses)) {
  51. redirect($return);
  52. }
  53. // generate user name list
  54. list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
  55. $userlist = $DB->get_records_select_menu('user', "id $in", $params, 'fullname', 'id,' . $DB->sql_fullname() . ' AS fullname');
  56. $usernames = implode('<br />', $userlist);
  57. // generate course name list
  58. $courselist = array();
  59. $courses = get_courses('all', 'c.sortorder ASC', 'c.id, c.fullname');
  60. foreach ($courses as $course) {
  61. if (in_array($course->id, $SESSION->bulk_courses)) {
  62. $courselist[] = $course->fullname;
  63. }
  64. }
  65. // generate the message
  66. $confmsg = advuserbulk_get_string('confirmpart1', $pluginname) . '<b>' . $usernames . '</b>';
  67. $confmsg .= advuserbulk_get_string('confirmpart2', $pluginname);
  68. $confmsg .= '<b>' . implode('<br />', $courselist) . '</b>';
  69. if (!empty($groups)) {
  70. $confmsg .= advuserbulk_get_string('confirmpart3', $pluginname) . '<b>' . s(implode(', ', $groups), false) . '</b>';
  71. unset($SESSION->groups);
  72. foreach ($groups as $group) {
  73. $SESSION->groups[] = $group;
  74. }
  75. }
  76. // get system roles info and add the selected role to the message
  77. if ($roleassign != 0) {
  78. $role = $DB->get_record('role', array('id' => $roleassign));
  79. $rolename = $role->name;
  80. } else {
  81. $rolename = advuserbulk_get_string('default', $pluginname);
  82. }
  83. $confmsg .= advuserbulk_get_string('confirmpart4', $pluginname) . '<b>' . $rolename . '</b>?';
  84. $optionsyes['confirm'] = true;
  85. $optionsyes['roleassign'] = $roleassign;
  86. // print the message
  87. echo $OUTPUT->header();
  88. echo $OUTPUT->heading(get_string('confirmation', 'admin'));
  89. $buttonyes = new single_button(new moodle_url('index.php', $optionsyes), get_string('yes'));
  90. $buttonno = new single_button(new moodle_url($return), get_string('no'), 'get');
  91. echo $OUTPUT->confirm($confmsg, $buttonyes, $buttonno);
  92. echo $OUTPUT->footer();
  93. die;
  94. }
  95. // action confirmed, perform it
  96. if ($confirm) {
  97. require_once($CFG->dirroot . '/group/lib.php');
  98. if (empty($SESSION->bulk_courses)) {
  99. redirect($return);
  100. }
  101. // for each course, get the default role if needed and check the selected group
  102. foreach ($SESSION->bulk_courses as $courseid) {
  103. $groupids = array();
  104. foreach ($SESSION->groups as $group) {
  105. $groupid = groups_get_group_by_name($courseid, stripslashes($group));
  106. if ($groupid) {
  107. $groupids[] = $groupid;
  108. }
  109. }
  110. $courseswithoutinternalenrol = array();
  111. if ($roleassign == 0) {
  112. if ($enrol = enrol_get_plugin('manual'))
  113. $roleassign = $enrol->get_config('roleid');
  114. else {
  115. $courseswithoutinternalenrol[] = $courseid;
  116. continue;
  117. }
  118. }
  119. foreach ($SESSION->bulk_users as $userid) {
  120. if (enrol_try_internal_enrol($courseid, $userid, $roleassign)) {
  121. foreach ($groupids as $groupid) {
  122. try {
  123. groups_add_member($groupid, $userid);
  124. } catch(Exception $e) {
  125. }
  126. }
  127. }
  128. else {
  129. $courseswithoutinternalenrol[] = $courseid;
  130. }
  131. }
  132. }
  133. if (count($courseswithoutinternalenrol)) {
  134. global $OUTPUT;
  135. echo $OUTPUT->header();
  136. html_writer::tag('p', advuserbulk_get_string('nointernalenrol', $pluginname, implode(', ', $courseswithoutinternalenrol)));
  137. echo $OUTPUT->continue_button($return);
  138. echo $OUTPUT->footer();
  139. die;
  140. }
  141. redirect($return, get_string('changessaved'));
  142. }
  143. /**
  144. * This function generates the list of courses for <select> control
  145. * using the specified string filter and/or course id's filter
  146. *
  147. * @param string $strfilter The course name filter
  148. * @param array $arrayfilter Course ID's filter, NULL by default, which means not to use id filter
  149. * @return string
  150. */
  151. function gen_course_list($strfilter = '', $arrayfilter = NULL, $filtinvert = false) {
  152. $courselist = array();
  153. $catcnt = 0;
  154. // get the list of course categories
  155. $categories = get_categories();
  156. foreach ($categories as $cat) {
  157. // for each category, add the <optgroup> to the string array first
  158. $courselist[$catcnt] = '<optgroup label="' . htmlspecialchars($cat->name) . '">';
  159. // get the course list in that category
  160. $courses = get_courses($cat->id, 'c.sortorder ASC', 'c.fullname, c.id');
  161. $coursecnt = 0;
  162. // for each course, check the specified filter
  163. foreach ($courses as $course) {
  164. if ((!empty($strfilter) && strripos($course->fullname, $strfilter) === false ) || ( $arrayfilter !== NULL && in_array($course->id, $arrayfilter) === $filtinvert )) {
  165. continue;
  166. }
  167. // if we pass the filter, add the option to the current string
  168. $courselist[$catcnt] .= '<option value="' . $course->id . '">' . $course->fullname . '</option>';
  169. $coursecnt++;
  170. }
  171. // if no courses pass the filter in that category, delete the current string
  172. if ($coursecnt == 0) {
  173. unset($courselist[$catcnt]);
  174. } else {
  175. $courselist[$catcnt] .= '</optgroup>';
  176. $catcnt++;
  177. }
  178. }
  179. // return the html code with categorized courses
  180. return implode(' ', $courselist);
  181. }
  182. // generate full and selected course lists
  183. $coursenames = gen_course_list($searchtext, $SESSION->bulk_courses, true);
  184. $selcoursenames = gen_course_list('', $SESSION->bulk_courses);
  185. // generate the list of groups names from the selected courses.
  186. // groups with the same name appear only once
  187. $groupnames = array();
  188. foreach ($SESSION->bulk_courses as $courseid) {
  189. $cgroups = groups_get_all_groups($courseid);
  190. foreach ($cgroups as $cgroup) {
  191. if (!in_array($cgroup->name, $groupnames)) {
  192. $groupnames[] = $cgroup->name;
  193. }
  194. }
  195. }
  196. sort($groupnames);
  197. // generate html code for the group select control
  198. foreach ($groupnames as $key => $name) {
  199. $groupnames[$key] = '<option value="' . s($name, true) . '" >' . s($name, true) . '</option>';
  200. }
  201. $groupnames = implode(' ', $groupnames);
  202. $courseroles = get_roles_for_contextlevels(CONTEXT_COURSE);
  203. $context = get_context_instance(CONTEXT_SYSTEM);
  204. list($courseviewroles, $ignored) = get_roles_with_cap_in_context($context, 'moodle/course:view');
  205. $enrolableroles = array_diff_key(array_combine($courseroles, $courseroles), $courseviewroles);
  206. $roles = array_intersect_key(get_all_roles(), $enrolableroles);
  207. $roles[0] = (object) array('name' => advuserbulk_get_string('default', $pluginname));
  208. $rolenames = '';
  209. foreach ($roles as $key => $role) {
  210. $rolenames .= '<option value="' . $key . '"';
  211. if ($key == $roleassign) {
  212. $rolenames .= ' selected ';
  213. }
  214. $rolenames .= '>' . $role->name . '</option> ';
  215. }
  216. // print the general page
  217. echo $OUTPUT->header();
  218. ?>
  219. <div id="addmembersform">
  220. <h3 class="main"><?php echo advuserbulk_get_string('title', $pluginname) ?></h3>
  221. <form id="addform" method="post" action="index.php">
  222. <table cellpadding="6" class="selectcourses generaltable generalbox boxaligncenter" summary="">
  223. <tr>
  224. <td id="existingcell">
  225. <p>
  226. <label for="allcourses"><?php echo advuserbulk_get_string('allcourses', $pluginname) ?></label>
  227. </p>
  228. <select name="allcourses[]" size="20" id="allcourses" multiple="multiple"
  229. onfocus="document.getElementById('addform').add.disabled=false;
  230. document.getElementById('addform').remove.disabled=true;
  231. document.getElementById('addform').selcourses.selectedIndex=-1;"
  232. onclick="this.focus();">
  233. <?php echo $coursenames ?>
  234. </select>
  235. <br />
  236. <label for="searchtext" class="accesshide"><?php p($strsearch) ?></label>
  237. <input type="text" name="searchtext" id="searchtext" size="21" value="<?php p($searchtext, true) ?>"
  238. onfocus ="getElementById('addform').add.disabled=true;
  239. getElementById('addform').remove.disabled=true;
  240. getElementById('addform').allcourses.selectedIndex=-1;
  241. getElementById('addform').selcourses.selectedIndex=-1;"
  242. onkeydown = "var keyCode = event.which ? event.which : event.keyCode;
  243. if (keyCode == 13) {
  244. getElementById('addform').previoussearch.value=1;
  245. getElementById('addform').submit();
  246. } " />
  247. <input name="search" id="search" type="submit" value="<?php p($strsearch) ?>" />
  248. <?php
  249. if (!empty($searchtext)) {
  250. echo '<br /><input name="showall" id="showall" type="submit" value="' . get_string('showall') . '" />' . "\n";
  251. }
  252. ?>
  253. </td>
  254. <td id="buttonscell">
  255. <div id="addcontrols">
  256. <input name="add" id="add" type="submit" disabled value="<?php echo '&nbsp;' . $OUTPUT->rarrow() . ' &nbsp; &nbsp; ' . get_string('add'); ?>" title="<?php print_string('add'); ?>" />
  257. </div>
  258. <div id="removecontrols">
  259. <input name="remove" id="remove" type="submit" disabled value="<?php echo '&nbsp; ' . $OUTPUT->larrow() . ' &nbsp; &nbsp; ' . get_string('remove'); ?>" title="<?php print_string('remove'); ?>" />
  260. </div>
  261. </td>
  262. <td id="potentialcell">
  263. <p>
  264. <label for="selcourses"><?php echo advuserbulk_get_string('selectedcourses', $pluginname) ?></label>
  265. </p>
  266. <select name="selcourses[]" size="20" id="selcourses" multiple="multiple"
  267. onfocus="document.getElementById('addform').remove.disabled=false;
  268. document.getElementById('addform').add.disabled=true;
  269. document.getElementById('addform').allcourses.selectedIndex=-1;"
  270. onclick="this.focus();">
  271. <?php echo $selcoursenames; ?>
  272. </select>
  273. <br />
  274. <input name="removeall" id="removeall" type="submit" value="<?php echo get_string('removeall', 'bulkusers') ?>" />
  275. </td>
  276. </tr>
  277. <tr>
  278. <td align="center">
  279. <label for="roleassign"><?php echo advuserbulk_get_string('roletoset', $pluginname) ?></label>
  280. <br />
  281. <select name="roleassign" id="roleassign" size="1">
  282. <?php echo $rolenames ?>
  283. </select>
  284. </td>
  285. <td align="center">
  286. <label for="groups"><?php echo advuserbulk_get_string('autogroup', $pluginname) ?></label>
  287. <br />
  288. <select name="groups[]" id="groups" size="10" multiple="multiple" >
  289. <?php echo $groupnames; ?>
  290. </select>
  291. </td>
  292. </tr>
  293. <tr><td></td><td align="center">
  294. <p><input type="submit" name="cancel" value="<?php echo get_string('cancel') ?>" />
  295. <input type="submit" name="accept" value="<?php echo advuserbulk_get_string('accept', $pluginname) ?>" /></p>
  296. </td>
  297. </tr>
  298. </table>
  299. </form>
  300. </div>
  301. <?php
  302. echo $OUTPUT->footer();
  303. ?>