Merge branch 'MDL-36412-m23' of git://github.com/sammarshallou/moodle into MOODLE_23_...
authorDan Poltawski <dan@moodle.com>
Wed, 16 Jan 2013 01:57:01 +0000 (09:57 +0800)
committerDan Poltawski <dan@moodle.com>
Wed, 16 Jan 2013 01:57:01 +0000 (09:57 +0800)
1  2 
course/editsection_form.php

@@@ -54,19 -54,11 +54,19 @@@ class editsection_form extends moodlefo
                  $mform->addHelpButton('groupingid', 'groupingsection', 'group');
              }
  
 -            // Date and time conditions
 +            // Available from/to defaults to midnight because then the display
 +            // will be nicer where it tells users when they can access it (it
 +            // shows only the date and not time).
 +            $date = usergetdate(time());
 +            $midnight = make_timestamp($date['year'], $date['mon'], $date['mday']);
 +
 +            // Date and time conditions.
              $mform->addElement('date_time_selector', 'availablefrom',
 -                    get_string('availablefrom', 'condition'), array('optional' => true));
 +                    get_string('availablefrom', 'condition'),
 +                    array('optional' => true, 'defaulttime' => $midnight));
              $mform->addElement('date_time_selector', 'availableuntil',
 -                    get_string('availableuntil', 'condition'), array('optional' => true));
 +                    get_string('availableuntil', 'condition'),
 +                    array('optional' => true, 'defaulttime' => $midnight));
  
              // Conditions based on grades
              $gradeoptions = array();
                  CONDITION_STUDENTVIEW_HIDE => get_string('showavailabilitysection_hide', 'condition'));
              $mform->addElement('select', 'showavailability',
                      get_string('showavailabilitysection', 'condition'), $showhide);
 -
 -            $mform->setDefault('showavailability', $this->_customdata['showavailability']);
          }
  
          $this->add_action_buttons();
          // Conditions: Don't let them set dates which make no sense
          if (array_key_exists('availablefrom', $data) &&
                  $data['availablefrom'] && $data['availableuntil'] &&
-                 $data['availablefrom'] > $data['availableuntil']) {
+                 $data['availablefrom'] >= $data['availableuntil']) {
              $errors['availablefrom'] = get_string('badavailabledates', 'condition');
          }
  
+         // Conditions: Verify that the grade conditions are numbers, and make sense.
+         if (array_key_exists('conditiongradegroup', $data)) {
+             foreach ($data['conditiongradegroup'] as $i => $gradedata) {
+                 if ($gradedata['conditiongrademin'] !== '' &&
+                         !is_numeric(unformat_float($gradedata['conditiongrademin']))) {
+                     $errors["conditiongradegroup[{$i}]"] = get_string('gradesmustbenumeric', 'condition');
+                     continue;
+                 }
+                 if ($gradedata['conditiongrademax'] !== '' &&
+                         !is_numeric(unformat_float($gradedata['conditiongrademax']))) {
+                     $errors["conditiongradegroup[{$i}]"] = get_string('gradesmustbenumeric', 'condition');
+                     continue;
+                 }
+                 if ($gradedata['conditiongrademin'] !== '' && $gradedata['conditiongrademax'] !== '' &&
+                         unformat_float($gradedata['conditiongrademax']) <= unformat_float($gradedata['conditiongrademin'])) {
+                     $errors["conditiongradegroup[{$i}]"] = get_string('badgradelimits', 'condition');
+                     continue;
+                 }
+                 if ($gradedata['conditiongrademin'] === '' && $gradedata['conditiongrademax'] === '' &&
+                         $gradedata['conditiongradeitemid']) {
+                     $errors["conditiongradegroup[{$i}]"] = get_string('gradeitembutnolimits', 'condition');
+                     continue;
+                 }
+                 if (($gradedata['conditiongrademin'] !== '' || $gradedata['conditiongrademax'] !== '') &&
+                         !$gradedata['conditiongradeitemid']) {
+                     $errors["conditiongradegroup[{$i}]"] = get_string('gradelimitsbutnoitem', 'condition');
+                     continue;
+                 }
+             }
+         }
          return $errors;
      }
  }