Skip to content

Commit cc95a16

Browse files
committed
fix changing completionsettings after users already completing the activity
1 parent ce4c177 commit cc95a16

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

locallib.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,9 @@ private function process_default() {
11221122
// $output .= $renderer->reports_group($this->ratingallocateid, $this->coursemodule->id, $status, $this->context);
11231123
}
11241124

1125+
$completion = new completion_info($this->course);
1126+
$completion->set_module_viewed($this->coursemodule);
1127+
11251128
// Logging.
11261129
$event = \mod_ratingallocate\event\ratingallocate_viewed::create_simple(
11271130
context_module::instance($this->coursemodule->id), $this->ratingallocateid);

mod_form.php

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,16 @@ private function add_settings_field($stratfieldid, array $value, $strategyid, Mo
202202
$mform->hideIf($stratfieldid, 'strategy', 'neq', $strategyid);
203203
}
204204

205+
205206
// Override if you need to setup the form depending on current values.
206207
public function definition_after_data() {
207-
parent::definition_after_data();
208+
208209
$mform = &$this->_form;
209210

210211
$data = $this->current;
211212

212213
if ($this->is_submitted()) {
213-
$subdata = $this->get_submitted_data();
214+
$subdata = $this->get_data();
214215
$allstrategyoptions = $subdata->{self::STRATEGY_OPTIONS};
215216
} else if (isset($data->setting)) {
216217
$allstrategyoptions = json_decode($data->setting, true);
@@ -245,8 +246,12 @@ public function definition_after_data() {
245246
}
246247
$mform->removeElement($strategyplaceholder);
247248
}
249+
250+
// Call parent function after, in order to have completiontracking working properly.
251+
parent::definition_after_data();
248252
}
249253

254+
250255
/**
251256
* Checks that accesstimestart is before accesstimestop
252257
*/
@@ -321,7 +326,7 @@ public function add_completion_rules() {
321326
}
322327

323328
protected function get_suffixed_name(string $fieldname): string {
324-
return 'completion' . $fieldname;
329+
return 'completion' . $fieldname . $this->get_suffix();
325330
}
326331

327332
/**
@@ -334,4 +339,44 @@ public function completion_rule_enabled($data) {
334339
return ($data[$this->get_suffixed_name('vote')] == 1 || $data[$this->get_suffixed_name('allocation')] == 1);
335340
}
336341

342+
/**
343+
* Allows module to modify data returned by get_moduleinfo_data() or prepare_new_moduleinfo_data() before calling set_data().
344+
* This method is also called in the bulk activity completion form.
345+
* Only available on moodleform_mod.
346+
*
347+
* @param $default_values
348+
* @return void
349+
*/
350+
function data_preprocessing(&$default_values){
351+
if(empty($default_values[$this->get_suffixed_name('vote')])) {
352+
$default_values[$this->get_suffixed_name('vote')] = 0;
353+
}
354+
if(empty($default_values[$this->get_suffixed_name('allocation')])) {
355+
$default_values[$this->get_suffixed_name('allocation')] = 0;
356+
}
357+
}
358+
359+
/**
360+
* Allows module to modify the data returned by form get_data().
361+
* This method is also called in the bulk activity completion form.
362+
*
363+
* Only available on moodleform_mod.
364+
*
365+
* @param stdClass $data the form data to be modified.
366+
*/
367+
public function data_postprocessing($data) {
368+
parent::data_postprocessing($data);
369+
// Turn off completion settings if the checkboxes aren't ticked.
370+
if (!empty($data->completionunlocked)) {
371+
$completion = $data->{'completion' . $this->get_suffix()};
372+
$autocompletion = !empty($completion) && $completion == COMPLETION_TRACKING_AUTOMATIC;
373+
if (empty($data->{$this->get_suffixed_name('vote')}) || !$autocompletion) {
374+
$data->{$this->get_suffixed_name('vote')} = 0;
375+
}
376+
if (empty($data->{$this->get_suffixed_name('allocation')}) || !$autocompletion) {
377+
$data->{$this->get_suffixed_name('allocation')} = 0;
378+
}
379+
}
380+
}
381+
337382
}

0 commit comments

Comments
 (0)