From 6445f17bfcfd607f164873b486040ddd89c1a0da Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Fri, 31 Mar 2017 14:57:59 +0800 Subject: [PATCH] MDL-58408 completion: can not require grade if grading is not available Part of MDL-58138 epic --- completion/classes/bulkedit_form.php | 34 ++++++++++++++++++++++++++++ lang/en/completion.php | 1 + 2 files changed, 35 insertions(+) diff --git a/completion/classes/bulkedit_form.php b/completion/classes/bulkedit_form.php index 2d14f3f08a16e..902a0bda10b5a 100644 --- a/completion/classes/bulkedit_form.php +++ b/completion/classes/bulkedit_form.php @@ -123,4 +123,38 @@ public function definition() { $this->set_data($data); } } + + /** + * Form validation + * + * @param array $data array of ("fieldname"=>value) of submitted data + * @param array $files array of uploaded files "element_name"=>tmp_file_path + * @return array of "element_name"=>"error_description" if there are errors, + * or an empty array if everything is OK (true allowed for backwards compatibility too). + */ + public function validation($data, $files) { + global $CFG; + $errors = parent::validation($data, $files); + + // Completion: Don't let them choose automatic completion without turning + // on some conditions. + if (array_key_exists('completion', $data) && + $data['completion'] == COMPLETION_TRACKING_AUTOMATIC && !empty($data['completionusegrade'])) { + require_once($CFG->libdir.'/gradelib.php'); + $moduleswithoutgradeitem = []; + foreach ($this->cms as $cm) { + $item = grade_item::fetch(array('courseid' => $cm->course, 'itemtype' => 'mod', + 'itemmodule' => $cm->modname, 'iteminstance' => $cm->instance, + 'itemnumber' => 0)); + if (!$item) { + $moduleswithoutgradeitem[] = $cm->get_formatted_name(); + } + } + if ($moduleswithoutgradeitem) { + $errors['completionusegrade'] = get_string('nogradeitem', 'completion', join(', ', $moduleswithoutgradeitem)); + } + } + + return $errors; + } } \ No newline at end of file diff --git a/lang/en/completion.php b/lang/en/completion.php index 86ac7d8c4f7de..6b47cd21cfea6 100644 --- a/lang/en/completion.php +++ b/lang/en/completion.php @@ -164,6 +164,7 @@ $string['modifybulkactions'] = 'Modify the actions you wish to bulk edit'; $string['moredetails'] = 'More details'; $string['nocriteriaset'] = 'No completion criteria set for this course'; +$string['nogradeitem'] = 'Require grade can not be enabled for {$a} because grades are not available there'; $string['notcompleted'] = 'Not completed'; $string['notenroled'] = 'You are not enrolled in this course'; $string['nottracked'] = 'You are currently not being tracked by completion in this course';