diff --git a/classes/completion/custom_completion.php b/classes/completion/custom_completion.php index 25a129a4..6cb164cd 100644 --- a/classes/completion/custom_completion.php +++ b/classes/completion/custom_completion.php @@ -18,6 +18,7 @@ namespace mod_ratingallocate\completion; +use context_module; use core_completion\activity_custom_completion; /** @@ -41,25 +42,37 @@ class custom_completion extends activity_custom_completion { */ public function get_state(string $rule): int { global $DB; - + $status = false; $this->validate_rule($rule); $userid = $this->userid; - $ratingallocateid = $this->cm->instance; + $course = $this->cm->get_course(); + $instance = $this->cm->instance; - if (!$DB->get_record('ratingallocate', ['id' => $ratingallocateid])) { - throw new \moodle_exception('Unable to find ratingallocate instance with id ' . $ratingallocateid); + if (!$ratingallocaterecord= $DB->get_record('ratingallocate', ['id' => $instance])) { + throw new \moodle_exception('Unable to find ratingallocate instance with id ' . $instance); } + $modinfo = get_fast_modinfo($course, $userid)->instances['ratingallocate'][$instance]; + $context = context_module::instance($modinfo->id); + + $ratingallocate = new \ratingallocate($ratingallocaterecord, $course, $this->cm, $context); + if ($rule == 'completionvote') { + $status = count($ratingallocate->get_rating_data_for_user($userid)) > 0; + /* $sql = "SELECT * FROM {ratingallocate_ratings} r INNER JOIN {ratingallocate_choices} c on r.choiceid=c.id " . "WHERE r.userid= :userid AND c.ratingallocateid= :ratingallocateid"; $votesofuser = $DB->get_records_sql($sql, ['userid' => $userid, 'ratingallocateid' => $ratingallocateid]); $status = count($votesofuser) > 0; + */ } else if ($rule == 'completionallocation') { + $status = count($ratingallocate->get_allocations_for_user($userid)) > 0; + /* $sql = "SELECT * FROM {ratingallocate_allocations} WHERE userid= :userid AND ratingallocateid= :ratingallocateid"; $allocationsofuser = $DB->get_records_sql($sql, ['userid' => $userid, 'ratingallocateid' => $ratingallocateid]); $status = count($allocationsofuser) > 0; + */ } return $status ? COMPLETION_COMPLETE : COMPLETION_INCOMPLETE; diff --git a/locallib.php b/locallib.php index a7078428..dea65099 100644 --- a/locallib.php +++ b/locallib.php @@ -750,13 +750,6 @@ private function process_action_manual_allocation() { $completion->update_state($this->coursemodule, COMPLETION_UNKNOWN, $rater->id); } } - $raters = $this->get_raters_in_course(); - $completion = new completion_info($this->course); - if ($completion->is_enabled($this->coursemodule)) { - foreach ($raters as $rater) { - $completion->update_state($this->coursemodule, COMPLETION_UNKNOWN, $rater->id); - } - } } $output .= $OUTPUT->heading(get_string('manual_allocation', RATINGALLOCATE_MOD_NAME), 2); diff --git a/mod_form.php b/mod_form.php index 4c854132..42510f15 100644 --- a/mod_form.php +++ b/mod_form.php @@ -358,8 +358,8 @@ public function add_completion_rules() { * @return string */ protected function get_suffixed_name(string $fieldname): string { - // Replace _ratingallocate with $this->get_suffix() for Moodle 4.3 or later. - return 'completion' . $fieldname . '_ratingallocate'; + // Counterintuitively don't use function get_suffix(), since data isn't saved correctly in DB otherwise. + return 'completion' . $fieldname; } /** @@ -401,7 +401,7 @@ public function data_postprocessing($data) { parent::data_postprocessing($data); // Turn off completion settings if the checkboxes aren't ticked. if (!empty($data->completionunlocked)) { - $completion = $data->{'completion_ratingallocate'}; + $completion = $data->completion; $autocompletion = !empty($completion) && $completion == COMPLETION_TRACKING_AUTOMATIC; if (empty($data->{$this->get_suffixed_name('vote')}) || !$autocompletion) { $data->{$this->get_suffixed_name('vote')} = 0;