Skip to content

Commit

Permalink
delete use of _ratingallocate as suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
irinahpe committed Aug 6, 2024
1 parent f14fc2b commit 5df8598
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
21 changes: 17 additions & 4 deletions classes/completion/custom_completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace mod_ratingallocate\completion;

use context_module;
use core_completion\activity_custom_completion;

/**
Expand All @@ -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;
Expand Down
7 changes: 0 additions & 7 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5df8598

Please sign in to comment.