From cc95a1653fb62fd16a80701d9e10fb712c72ea39 Mon Sep 17 00:00:00 2001 From: Irina Hoppe Date: Thu, 13 Jun 2024 11:17:05 +0200 Subject: [PATCH] fix changing completionsettings after users already completing the activity --- locallib.php | 3 +++ mod_form.php | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/locallib.php b/locallib.php index 8a0976dd..d7fc206f 100644 --- a/locallib.php +++ b/locallib.php @@ -1122,6 +1122,9 @@ private function process_default() { // $output .= $renderer->reports_group($this->ratingallocateid, $this->coursemodule->id, $status, $this->context); } + $completion = new completion_info($this->course); + $completion->set_module_viewed($this->coursemodule); + // Logging. $event = \mod_ratingallocate\event\ratingallocate_viewed::create_simple( context_module::instance($this->coursemodule->id), $this->ratingallocateid); diff --git a/mod_form.php b/mod_form.php index f19ec783..0be58c8d 100644 --- a/mod_form.php +++ b/mod_form.php @@ -202,15 +202,16 @@ private function add_settings_field($stratfieldid, array $value, $strategyid, Mo $mform->hideIf($stratfieldid, 'strategy', 'neq', $strategyid); } + // Override if you need to setup the form depending on current values. public function definition_after_data() { - parent::definition_after_data(); + $mform = &$this->_form; $data = $this->current; if ($this->is_submitted()) { - $subdata = $this->get_submitted_data(); + $subdata = $this->get_data(); $allstrategyoptions = $subdata->{self::STRATEGY_OPTIONS}; } else if (isset($data->setting)) { $allstrategyoptions = json_decode($data->setting, true); @@ -245,8 +246,12 @@ public function definition_after_data() { } $mform->removeElement($strategyplaceholder); } + + // Call parent function after, in order to have completiontracking working properly. + parent::definition_after_data(); } + /** * Checks that accesstimestart is before accesstimestop */ @@ -321,7 +326,7 @@ public function add_completion_rules() { } protected function get_suffixed_name(string $fieldname): string { - return 'completion' . $fieldname; + return 'completion' . $fieldname . $this->get_suffix(); } /** @@ -334,4 +339,44 @@ public function completion_rule_enabled($data) { return ($data[$this->get_suffixed_name('vote')] == 1 || $data[$this->get_suffixed_name('allocation')] == 1); } + /** + * Allows module to modify data returned by get_moduleinfo_data() or prepare_new_moduleinfo_data() before calling set_data(). + * This method is also called in the bulk activity completion form. + * Only available on moodleform_mod. + * + * @param $default_values + * @return void + */ + function data_preprocessing(&$default_values){ + if(empty($default_values[$this->get_suffixed_name('vote')])) { + $default_values[$this->get_suffixed_name('vote')] = 0; + } + if(empty($default_values[$this->get_suffixed_name('allocation')])) { + $default_values[$this->get_suffixed_name('allocation')] = 0; + } + } + + /** + * Allows module to modify the data returned by form get_data(). + * This method is also called in the bulk activity completion form. + * + * Only available on moodleform_mod. + * + * @param stdClass $data the form data to be modified. + */ + 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' . $this->get_suffix()}; + $autocompletion = !empty($completion) && $completion == COMPLETION_TRACKING_AUTOMATIC; + if (empty($data->{$this->get_suffixed_name('vote')}) || !$autocompletion) { + $data->{$this->get_suffixed_name('vote')} = 0; + } + if (empty($data->{$this->get_suffixed_name('allocation')}) || !$autocompletion) { + $data->{$this->get_suffixed_name('allocation')} = 0; + } + } + } + }