PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/group/autogroup_form.php

http://github.com/moodle/moodle
PHP | 253 lines | 172 code | 36 blank | 45 comment | 36 complexity | c75a04a143b747f84ea058a0f074e555 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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 $USER, $COURSE;
  41. $coursecontext = context_course::instance($COURSE->id);
  42. $mform =& $this->_form;
  43. $mform->addElement('header', 'autogroup', get_string('general'));
  44. $mform->addElement('text', 'namingscheme', get_string('namingscheme', 'group'));
  45. $mform->addHelpButton('namingscheme', 'namingscheme', 'group');
  46. $mform->addRule('namingscheme', get_string('required'), 'required', null, 'client');
  47. $mform->setType('namingscheme', PARAM_TEXT);
  48. // There must not be duplicate group names in course.
  49. $template = get_string('grouptemplate', 'group');
  50. $gname = groups_parse_name($template, 0);
  51. if (!groups_get_group_by_name($COURSE->id, $gname)) {
  52. $mform->setDefault('namingscheme', $template);
  53. }
  54. $options = array('groups' => get_string('numgroups', 'group'),
  55. 'members' => get_string('nummembers', 'group'));
  56. $mform->addElement('select', 'groupby', get_string('groupby', 'group'), $options);
  57. $mform->addElement('text', 'number', get_string('number', 'group'),'maxlength="4" size="4"');
  58. $mform->setType('number', PARAM_INT);
  59. $mform->addRule('number', null, 'numeric', null, 'client');
  60. $mform->addRule('number', get_string('required'), 'required', null, 'client');
  61. // Enable group messaging for the groups to be auto-created.
  62. if (\core_message\api::can_create_group_conversation($USER->id, $coursecontext)) {
  63. $mform->addElement('selectyesno', 'enablemessaging', get_string('enablemessaging', 'group'));
  64. $mform->addHelpButton('enablemessaging', 'enablemessaging', 'group');
  65. }
  66. $mform->addElement('header', 'groupmembershdr', get_string('groupmembers', 'group'));
  67. $mform->setExpanded('groupmembershdr', true);
  68. $options = array(0=>get_string('all'));
  69. $options += $this->_customdata['roles'];
  70. $mform->addElement('select', 'roleid', get_string('selectfromrole', 'group'), $options);
  71. $student = get_archetype_roles('student');
  72. $student = reset($student);
  73. if ($student and array_key_exists($student->id, $options)) {
  74. $mform->setDefault('roleid', $student->id);
  75. }
  76. $coursecontext = context_course::instance($COURSE->id);
  77. if ($cohorts = cohort_get_available_cohorts($coursecontext, COHORT_WITH_ENROLLED_MEMBERS_ONLY, 0, 0)) {
  78. $options = array(0 => get_string('anycohort', 'cohort'));
  79. foreach ($cohorts as $c) {
  80. $options[$c->id] = format_string($c->name, true, context::instance_by_id($c->contextid));
  81. }
  82. $mform->addElement('select', 'cohortid', get_string('selectfromcohort', 'cohort'), $options);
  83. $mform->setDefault('cohortid', '0');
  84. } else {
  85. $mform->addElement('hidden','cohortid');
  86. $mform->setType('cohortid', PARAM_INT);
  87. $mform->setConstant('cohortid', '0');
  88. }
  89. if ($groupings = groups_get_all_groupings($COURSE->id)) {
  90. $options = array();
  91. $options[0] = get_string('none');
  92. foreach ($groupings as $grouping) {
  93. $options[$grouping->id] = format_string($grouping->name);
  94. }
  95. $mform->addElement('select', 'groupingid', get_string('selectfromgrouping', 'group'), $options);
  96. $mform->setDefault('groupingid', 0);
  97. $mform->disabledIf('groupingid', 'notingroup', 'checked');
  98. } else {
  99. $mform->addElement('hidden', 'groupingid');
  100. $mform->setType('groupingid', PARAM_INT);
  101. $mform->setConstant('groupingid', 0);
  102. }
  103. if ($groups = groups_get_all_groups($COURSE->id)) {
  104. $options = array();
  105. $options[0] = get_string('none');
  106. foreach ($groups as $group) {
  107. $options[$group->id] = format_string($group->name);
  108. }
  109. $mform->addElement('select', 'groupid', get_string('selectfromgroup', 'group'), $options);
  110. $mform->setDefault('groupid', 0);
  111. $mform->disabledIf('groupid', 'notingroup', 'checked');
  112. } else {
  113. $mform->addElement('hidden', 'groupid');
  114. $mform->setType('groupid', PARAM_INT);
  115. $mform->setConstant('groupid', 0);
  116. }
  117. $options = array('no' => get_string('noallocation', 'group'),
  118. 'random' => get_string('random', 'group'),
  119. 'firstname' => get_string('byfirstname', 'group'),
  120. 'lastname' => get_string('bylastname', 'group'),
  121. 'idnumber' => get_string('byidnumber', 'group'));
  122. $mform->addElement('select', 'allocateby', get_string('allocateby', 'group'), $options);
  123. $mform->setDefault('allocateby', 'random');
  124. $mform->addElement('checkbox', 'nosmallgroups', get_string('nosmallgroups', 'group'));
  125. $mform->disabledIf('nosmallgroups', 'groupby', 'noteq', 'members');
  126. $mform->addElement('checkbox', 'notingroup', get_string('notingroup', 'group'));
  127. $mform->disabledIf('notingroup', 'groupingid', 'neq', 0);
  128. $mform->disabledIf('notingroup', 'groupid', 'neq', 0);
  129. if (has_capability('moodle/course:viewsuspendedusers', $coursecontext)) {
  130. $mform->addElement('checkbox', 'includeonlyactiveenrol', get_string('includeonlyactiveenrol', 'group'), '');
  131. $mform->addHelpButton('includeonlyactiveenrol', 'includeonlyactiveenrol', 'group');
  132. $mform->setDefault('includeonlyactiveenrol', true);
  133. }
  134. $mform->addElement('header', 'groupinghdr', get_string('grouping', 'group'));
  135. $options = array('0' => get_string('nogrouping', 'group'),
  136. '-1'=> get_string('newgrouping', 'group'));
  137. if ($groupings = groups_get_all_groupings($COURSE->id)) {
  138. foreach ($groupings as $grouping) {
  139. $options[$grouping->id] = strip_tags(format_string($grouping->name));
  140. }
  141. }
  142. $mform->addElement('select', 'grouping', get_string('createingrouping', 'group'), $options);
  143. if ($groupings) {
  144. $mform->setDefault('grouping', '-1');
  145. }
  146. $mform->addElement('text', 'groupingname', get_string('groupingname', 'group'), $options);
  147. $mform->setType('groupingname', PARAM_TEXT);
  148. $mform->disabledIf('groupingname', 'grouping', 'noteq', '-1');
  149. $mform->addElement('hidden','courseid');
  150. $mform->setType('courseid', PARAM_INT);
  151. $mform->addElement('hidden','seed');
  152. $mform->setType('seed', PARAM_INT);
  153. $buttonarray = array();
  154. $buttonarray[] = &$mform->createElement('submit', 'preview', get_string('preview'));
  155. $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('submit'));
  156. $buttonarray[] = &$mform->createElement('cancel');
  157. $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
  158. $mform->closeHeaderBefore('buttonar');
  159. }
  160. /**
  161. * Performs validation of the form information
  162. *
  163. * @param array $data
  164. * @param array $files
  165. * @return array $errors An array of $errors
  166. */
  167. function validation($data, $files) {
  168. global $CFG, $COURSE;
  169. $errors = parent::validation($data, $files);
  170. if ($data['allocateby'] != 'no') {
  171. $source = array();
  172. if ($data['cohortid']) {
  173. $source['cohortid'] = $data['cohortid'];
  174. }
  175. if ($data['groupingid']) {
  176. $source['groupingid'] = $data['groupingid'];
  177. }
  178. if ($data['groupid']) {
  179. $source['groupid'] = $data['groupid'];
  180. }
  181. if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'], $source)) {
  182. $errors['roleid'] = get_string('nousersinrole', 'group');
  183. }
  184. /// Check the number entered is sane
  185. if ($data['groupby'] == 'groups') {
  186. $usercnt = count($users);
  187. if ($data['number'] > $usercnt || $data['number'] < 1) {
  188. $errors['number'] = get_string('toomanygroups', 'group', $usercnt);
  189. }
  190. }
  191. }
  192. //try to detect group name duplicates
  193. $name = groups_parse_name(trim($data['namingscheme']), 0);
  194. if (groups_get_group_by_name($COURSE->id, $name)) {
  195. $errors['namingscheme'] = get_string('groupnameexists', 'group', $name);
  196. }
  197. // check grouping name duplicates
  198. if ( isset($data['grouping']) && $data['grouping'] == '-1') {
  199. $name = trim($data['groupingname']);
  200. if (empty($name)) {
  201. $errors['groupingname'] = get_string('required');
  202. } else if (groups_get_grouping_by_name($COURSE->id, $name)) {
  203. $errors['groupingname'] = get_string('groupingnameexists', 'group', $name);
  204. }
  205. }
  206. /// Check the naming scheme
  207. if ($data['groupby'] == 'groups' and $data['number'] == 1) {
  208. // we can use the name as is because there will be only one group max
  209. } else {
  210. $matchcnt = preg_match_all('/[#@]{1,1}/', $data['namingscheme'], $matches);
  211. if ($matchcnt != 1) {
  212. $errors['namingscheme'] = get_string('badnamingscheme', 'group');
  213. }
  214. }
  215. return $errors;
  216. }
  217. }