Skip to content

Commit

Permalink
fix changing completionsettings after users already completing the ac…
Browse files Browse the repository at this point in the history
…tivity
  • Loading branch information
irinahpe committed Jun 13, 2024
1 parent ce4c177 commit cc95a16
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
3 changes: 3 additions & 0 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
51 changes: 48 additions & 3 deletions mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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();
}

/**
Expand All @@ -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;
}
}
}

}

0 comments on commit cc95a16

Please sign in to comment.