autogroup_form.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. * Auto group form
  18. *
  19. * @package core_group
  20. * @copyright 2007 mattc-catalyst (http://moodle.com)
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. if (!defined('MOODLE_INTERNAL')) {
  24. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  25. }
  26. require_once($CFG->dirroot.'/lib/formslib.php');
  27. require_once($CFG->dirroot.'/cohort/lib.php');
  28. /**
  29. * Auto group form class
  30. *
  31. * @package core_group
  32. * @copyright 2007 mattc-catalyst (http://moodle.com)
  33. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34. */
  35. class autogroup_form extends moodleform {
  36. /**
  37. * Form Definition
  38. */
  39. function definition() {
  40. global $CFG, $COURSE;
  41. $mform =& $this->_form;
  42. $mform->addElement('header', 'autogroup', get_string('general'));
  43. $mform->addElement('text', 'namingscheme', get_string('namingscheme', 'group'));
  44. $mform->addHelpButton('namingscheme', 'namingscheme', 'group');
  45. $mform->addRule('namingscheme', get_string('required'), 'required', null, 'client');
  46. $mform->setType('namingscheme', PARAM_TEXT);
  47. // There must not be duplicate group names in course.
  48. $template = get_string('grouptemplate', 'group');
  49. $gname = groups_parse_name($template, 0);
  50. if (!groups_get_group_by_name($COURSE->id, $gname)) {
  51. $mform->setDefault('namingscheme', $template);
  52. }
  53. $options = array('groups' => get_string('numgroups', 'group'),
  54. 'members' => get_string('nummembers', 'group'));
  55. $mform->addElement('select', 'groupby', get_string('groupby', 'group'), $options);
  56. $mform->addElement('text', 'number', get_string('number', 'group'),'maxlength="4" size="4"');
  57. $mform->setType('number', PARAM_INT);
  58. $mform->addRule('number', null, 'numeric', null, 'client');
  59. $mform->addRule('number', get_string('required'), 'required', null, 'client');
  60. $mform->addElement('header', 'groupmembershdr', get_string('groupmembers', 'group'));
  61. $mform->setExpanded('groupmembershdr', true);
  62. $options = array(0=>get_string('all'));
  63. $options += $this->_customdata['roles'];
  64. $mform->addElement('select', 'roleid', get_string('selectfromrole', 'group'), $options);
  65. $student = get_archetype_roles('student');
  66. $student = reset($student);
  67. if ($student and array_key_exists($student->id, $options)) {
  68. $mform->setDefault('roleid', $student->id);
  69. }
  70. $coursecontext = context_course::instance($COURSE->id);
  71. if ($cohorts = cohort_get_available_cohorts($coursecontext, COHORT_WITH_ENROLLED_MEMBERS_ONLY)) {
  72. $options = array(0 => get_string('anycohort', 'cohort'));
  73. foreach ($cohorts as $c) {
  74. $options[$c->id] = format_string($c->name, true, context::instance_by_id($c->contextid));
  75. }
  76. $mform->addElement('select', 'cohortid', get_string('selectfromcohort', 'cohort'), $options);
  77. $mform->setDefault('cohortid', '0');
  78. } else {
  79. $mform->addElement('hidden','cohortid');
  80. $mform->setType('cohortid', PARAM_INT);
  81. $mform->setConstant('cohortid', '0');
  82. }
  83. if ($groupings = groups_get_all_groupings($COURSE->id)) {
  84. $options = array();
  85. $options[0] = get_string('none');
  86. foreach ($groupings as $grouping) {
  87. $options[$grouping->id] = format_string($grouping->name);
  88. }
  89. $mform->addElement('select', 'groupingid', get_string('selectfromgrouping', 'group'), $options);
  90. $mform->setDefault('groupingid', 0);
  91. $mform->disabledIf('groupingid', 'notingroup', 'checked');
  92. } else {
  93. $mform->addElement('hidden', 'groupingid');
  94. $mform->setType('groupingid', PARAM_INT);
  95. $mform->setConstant('groupingid', 0);
  96. }
  97. if ($groups = groups_get_all_groups($COURSE->id)) {
  98. $options = array();
  99. $options[0] = get_string('none');
  100. foreach ($groups as $group) {
  101. $options[$group->id] = format_string($group->name);
  102. }
  103. $mform->addElement('select', 'groupid', get_string('selectfromgroup', 'group'), $options);
  104. $mform->setDefault('groupid', 0);
  105. $mform->disabledIf('groupid', 'notingroup', 'checked');
  106. } else {
  107. $mform->addElement('hidden', 'groupid');
  108. $mform->setType('groupid', PARAM_INT);
  109. $mform->setConstant('groupid', 0);
  110. }
  111. $options = array('no' => get_string('noallocation', 'group'),
  112. 'random' => get_string('random', 'group'),
  113. 'firstname' => get_string('byfirstname', 'group'),
  114. 'lastname' => get_string('bylastname', 'group'),
  115. 'idnumber' => get_string('byidnumber', 'group'));
  116. $mform->addElement('select', 'allocateby', get_string('allocateby', 'group'), $options);
  117. $mform->setDefault('allocateby', 'random');
  118. $mform->addElement('checkbox', 'nosmallgroups', get_string('nosmallgroups', 'group'));
  119. $mform->disabledIf('nosmallgroups', 'groupby', 'noteq', 'members');
  120. $mform->addElement('checkbox', 'notingroup', get_string('notingroup', 'group'));
  121. $mform->disabledIf('notingroup', 'groupingid', 'neq', 0);
  122. $mform->disabledIf('notingroup', 'groupid', 'neq', 0);
  123. if (has_capability('moodle/course:viewsuspendedusers', $coursecontext)) {
  124. $mform->addElement('checkbox', 'includeonlyactiveenrol', get_string('includeonlyactiveenrol', 'group'), '');
  125. $mform->addHelpButton('includeonlyactiveenrol', 'includeonlyactiveenrol', 'group');
  126. $mform->setDefault('includeonlyactiveenrol', true);
  127. }
  128. $mform->addElement('header', 'groupinghdr', get_string('grouping', 'group'));
  129. $options = array('0' => get_string('nogrouping', 'group'),
  130. '-1'=> get_string('newgrouping', 'group'));
  131. if ($groupings = groups_get_all_groupings($COURSE->id)) {
  132. foreach ($groupings as $grouping) {
  133. $options[$grouping->id] = strip_tags(format_string($grouping->name));
  134. }
  135. }
  136. $mform->addElement('select', 'grouping', get_string('createingrouping', 'group'), $options);
  137. if ($groupings) {
  138. $mform->setDefault('grouping', '-1');
  139. }
  140. $mform->addElement('text', 'groupingname', get_string('groupingname', 'group'), $options);
  141. $mform->setType('groupingname', PARAM_TEXT);
  142. $mform->disabledIf('groupingname', 'grouping', 'noteq', '-1');
  143. $mform->addElement('hidden','courseid');
  144. $mform->setType('courseid', PARAM_INT);
  145. $mform->addElement('hidden','seed');
  146. $mform->setType('seed', PARAM_INT);
  147. $buttonarray = array();
  148. $buttonarray[] = &$mform->createElement('submit', 'preview', get_string('preview'));
  149. $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('submit'));
  150. $buttonarray[] = &$mform->createElement('cancel');
  151. $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
  152. $mform->closeHeaderBefore('buttonar');
  153. }
  154. /**
  155. * Performs validation of the form information
  156. *
  157. * @param array $data
  158. * @param array $files
  159. * @return array $errors An array of $errors
  160. */
  161. function validation($data, $files) {
  162. global $CFG, $COURSE;
  163. $errors = parent::validation($data, $files);
  164. if ($data['allocateby'] != 'no') {
  165. $source = array();
  166. if ($data['cohortid']) {
  167. $source['cohortid'] = $data['cohortid'];
  168. }
  169. if ($data['groupingid']) {
  170. $source['groupingid'] = $data['groupingid'];
  171. }
  172. if ($data['groupid']) {
  173. $source['groupid'] = $data['groupid'];
  174. }
  175. if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'], $source)) {
  176. $errors['roleid'] = get_string('nousersinrole', 'group');
  177. }
  178. /// Check the number entered is sane
  179. if ($data['groupby'] == 'groups') {
  180. $usercnt = count($users);
  181. if ($data['number'] > $usercnt || $data['number'] < 1) {
  182. $errors['number'] = get_string('toomanygroups', 'group', $usercnt);
  183. }
  184. }
  185. }
  186. //try to detect group name duplicates
  187. $name = groups_parse_name(trim($data['namingscheme']), 0);
  188. if (groups_get_group_by_name($COURSE->id, $name)) {
  189. $errors['namingscheme'] = get_string('groupnameexists', 'group', $name);
  190. }
  191. // check grouping name duplicates
  192. if ( isset($data['grouping']) && $data['grouping'] == '-1') {
  193. $name = trim($data['groupingname']);
  194. if (empty($name)) {
  195. $errors['groupingname'] = get_string('required');
  196. } else if (groups_get_grouping_by_name($COURSE->id, $name)) {
  197. $errors['groupingname'] = get_string('groupingnameexists', 'group', $name);
  198. }
  199. }
  200. /// Check the naming scheme
  201. if ($data['groupby'] == 'groups' and $data['number'] == 1) {
  202. // we can use the name as is because there will be only one group max
  203. } else {
  204. $matchcnt = preg_match_all('/[#@]{1,1}/', $data['namingscheme'], $matches);
  205. if ($matchcnt != 1) {
  206. $errors['namingscheme'] = get_string('badnamingscheme', 'group');
  207. }
  208. }
  209. return $errors;
  210. }
  211. }