[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/group/ -> autogroup_form.php (source)

   1  <?php // $Id: autogroup_form.php,v 1.1.2.3 2007/12/20 16:10:03 poltawski Exp $
   2  
   3  require_once($CFG->dirroot.'/lib/formslib.php');
   4  
   5  /// get url variables
   6  class autogroup_form extends moodleform {
   7  
   8      // Define the form
   9      function definition() {
  10          global $CFG, $COURSE;
  11  
  12          $mform =& $this->_form;
  13  
  14          $mform->addElement('header', 'autogroup', get_string('autocreategroups', 'group'));
  15  
  16          $options = array(0=>get_string('all'));
  17          $options += $this->_customdata['roles'];
  18          $mform->addElement('select', 'roleid', get_string('selectfromrole', 'group'), $options);
  19          if (!empty($COURSE->defaultrole) and array_key_exists($COURSE->defaultrole, $options)) {
  20              $mform->setDefault('roleid', $COURSE->defaultrole);
  21          } else if (!empty($CFG->defaultcourseroleid) and array_key_exists($CFG->defaultcourseroleid, $options)) {
  22              $mform->setDefault('roleid', $CFG->defaultcourseroleid);
  23          }
  24  
  25          $options = array('groups' => get_string('numgroups', 'group'),
  26                           'members' => get_string('nummembers', 'group'));
  27          $mform->addElement('select', 'groupby', get_string('groupby', 'group'), $options);
  28  
  29          $mform->addElement('text', 'number', get_string('number', 'group'),'maxlength="4" size="4"');
  30          $mform->setType('number', PARAM_INT);
  31          $mform->addRule('number', null, 'numeric', null, 'client');
  32          $mform->addRule('number', get_string('required'), 'required', null, 'client');
  33  
  34          $mform->addElement('checkbox', 'nosmallgroups', get_string('nosmallgroups', 'group'));
  35          $mform->disabledIf('nosmallgroups', 'groupby', 'noteq', 'members');
  36          $mform->setAdvanced('nosmallgroups');
  37  
  38          $options = array('no'        => get_string('noallocation', 'group'),
  39                           'random'    => get_string('random', 'group'),
  40                           'firstname' => get_string('byfirstname', 'group'),
  41                           'lastname'  => get_string('bylastname', 'group'),
  42                           'idnumber'  => get_string('byidnumber', 'group'));
  43          $mform->addElement('select', 'allocateby', get_string('allocateby', 'group'), $options);
  44          $mform->setDefault('allocateby', 'random');
  45          $mform->setAdvanced('allocateby');
  46  
  47          $mform->addElement('text', 'namingscheme', get_string('namingscheme', 'group'));
  48          $mform->setHelpButton('namingscheme', array(false, get_string('namingschemehelp', 'group'),
  49                  false, true, false, get_string('namingschemehelp', 'group')));
  50          $mform->addRule('namingscheme', get_string('required'), 'required', null, 'client');
  51          $mform->setType('namingscheme', PARAM_MULTILANG);
  52          // there must not be duplicate group names in course
  53          $template = get_string('grouptemplate', 'group');
  54          $gname = groups_parse_name($template, 0);
  55          if (!groups_get_group_by_name($COURSE->id, $gname)) {
  56              $mform->setDefault('namingscheme', $template);
  57          }
  58  
  59          if (!empty($CFG->enablegroupings)) {
  60              $options = array('0' => get_string('no'),
  61                               '-1'=> get_string('newgrouping', 'group'));
  62              if ($groupings = groups_get_all_groupings($COURSE->id)) {
  63                  foreach ($groupings as $grouping) {
  64                      $options[$grouping->id] = strip_tags(format_string($grouping->name));
  65                  }
  66              }
  67              $mform->addElement('select', 'grouping', get_string('createingrouping', 'group'), $options);
  68              if ($groupings) {
  69                  $mform->setDefault('grouping', '-1');
  70              }
  71  
  72              $mform->addElement('text', 'groupingname', get_string('groupingname', 'group'), $options);
  73              $mform->setType('groupingname', PARAM_MULTILANG);
  74              $mform->disabledIf('groupingname', 'grouping', 'noteq', '-1');
  75          }
  76  
  77          $mform->addElement('hidden','courseid');
  78          $mform->setType('courseid', PARAM_INT);
  79  
  80          $mform->addElement('hidden','seed');
  81          $mform->setType('seed', PARAM_INT);
  82  
  83          $buttonarray = array();
  84          $buttonarray[] = &$mform->createElement('submit', 'preview', get_string('preview'), 'xx');
  85          $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('submit'));
  86          $buttonarray[] = &$mform->createElement('cancel');
  87          $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
  88          $mform->closeHeaderBefore('buttonar');
  89      }
  90  
  91  
  92      function validation($data, $files) {
  93          global $CFG, $COURSE;
  94          $errors = parent::validation($data, $files);
  95  
  96          if ($data['allocateby'] != 'no') {
  97              if (!$users = groups_get_potential_members($data['courseid'], $data['roleid'])) {
  98                  $errors['roleid'] = get_string('nousersinrole', 'group');
  99              }
 100  
 101             /// Check the number entered is sane
 102              if ($data['groupby'] == 'groups') {
 103                  $usercnt = count($users);
 104  
 105                  if ($data['number'] > $usercnt || $data['number'] < 1) {
 106                      $errors['number'] = get_string('toomanygroups', 'group', $usercnt);
 107                  }
 108              }
 109          }
 110  
 111          //try to detect group name duplicates
 112          $name = groups_parse_name(stripslashes(trim($data['namingscheme'])), 0);
 113          if (groups_get_group_by_name($COURSE->id, $name)) {
 114              $errors['namingscheme'] = get_string('groupnameexists', 'group', $name);
 115          }
 116  
 117          // check grouping name duplicates
 118          if ( isset($data['grouping']) && $data['grouping'] == '-1') {
 119              $name = trim(stripslashes($data['groupingname']));
 120              if (empty($name)) {
 121                  $errors['groupingname'] = get_string('required');
 122              } else if (groups_get_grouping_by_name($COURSE->id, $name)) {
 123                  $errors['groupingname'] = get_string('groupingnameexists', 'group', $name);
 124              }
 125          }
 126  
 127         /// Check the naming scheme
 128          $matchcnt = preg_match_all('/[#@]{1,1}/', $data['namingscheme'], $matches);
 129  
 130          if ($matchcnt != 1) {
 131              $errors['namingscheme'] = get_string('badnamingscheme', 'group');
 132          }
 133  
 134          return $errors;
 135      }
 136  }
 137  
 138  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7