コード例 #1
0
 public function validation($data, $files)
 {
     global $COURSE, $CFG, $DB;
     $errors = parent::validation($data, $files);
     if (isset($data['limitgroup']['maxpostsblock']) && !preg_match('/^[0-9]{1,9}$/', $data['limitgroup']['maxpostsblock'])) {
         $errors['limitgroup'] = get_string('err_numeric', 'form');
     }
     if (!empty($data['reportingemail']) && !$this->validate_emails($data['reportingemail'])) {
         $errors['reportingemail'] = get_string('invalidemail', 'forumng');
     }
     // If old discussions are set to be moved to another forum...
     $targetforumngid = isset($data['removeto']) ? $data['removeto'] : 0;
     $removeafter = isset($data['removeafter']) ? $data['removeafter'] : 0;
     if ($removeafter && $targetforumngid > 0) {
         $modinfo = get_fast_modinfo($COURSE);
         // Look for target forum
         if (!array_key_exists($targetforumngid, $modinfo->instances['forumng'])) {
             $errors['removeto'] = get_string('errorinvalidforum', 'forumng');
         }
     }
     // If sharing is turned on, check requirements
     if (!empty($data['shared'])) {
         if (!empty($data['groupmode'])) {
             $errors['groupmode'] = get_string('error_notwhensharing', 'forumng');
         }
         if (!empty($data['grading'])) {
             $errors['grading'] = get_string('error_notwhensharing', 'forumng');
         }
         if (empty($data['cmidnumber'])) {
             $errors['cmidnumber'] = get_string('error_sharingrequiresidnumber', 'forumng');
         } else {
             // Check it's unique
             $cmid = isset($data['coursemodule']) ? (int) $data['coursemodule'] : 0;
             if ($DB->count_records_select('course_modules', "idnumber = ? AND id <> ?", array($data['cmidnumber'], $cmid))) {
                 $errors['cmidnumber'] = get_string('error_sharingrequiresidnumber', 'forumng');
             }
         }
     } else {
         if (isset($data['shared'])) {
             // They are trying to turn sharing off. You aren't allowed to do
             // this if there are existing references.
             $cmid = isset($data['coursemodule']) ? (int) $data['coursemodule'] : -1;
             if ($DB->count_records('forumng', array('originalcmid' => $cmid))) {
                 $errors['shared'] = get_string('error_sharinginuse', 'forumng');
             }
         }
     }
     if (!empty($data['usesharedgroup']['useshared'])) {
         if (empty($data['usesharedgroup']['originalcmidnumber'])) {
             $errors['usesharedgroup'] = get_string('error_sharingidnumbernotfound', 'forumng');
         } else {
             // Check we can find it
             if (!mod_forumng::get_shared_cm_from_idnumber($data['usesharedgroup']['originalcmidnumber'])) {
                 $errors['usesharedgroup'] = get_string('error_sharingidnumbernotfound', 'forumng');
             }
         }
     }
     // If grading is set to ratings and ratings not enabled.
     if (!empty($data['grading'])) {
         if ($data['grading'] > 0 && $data['grading'] < 6 && empty($data['enableratings'])) {
             // If grading between 1 and 5 (not = 6 and not = 0) and enableratings is empty (not checked).
             $errors['enableratings'] = get_string('error_ratingrequired', 'forumng');
         }
     }
     return $errors;
 }