[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/group/ -> autogroup_form.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1