Completion state never updated for vote and submit actions
Kemmotar83 opened this issue · 11 comments
Hi,
in Moodle 3.11 completion doesn't seem to work very well. If students do the action required by the completion criteria (ie. vote or post questions), the completion state doesn't change. I haven't done a test with Moodle 4.0 yet, but I think it's the same.
Checking the code I see that completion state is never updated when the action is performed.
Next you can see an example code that updates the state correctly when the vote action is performed (it is a working example for vote action but it could be written better).
locallib.php
/**
* Update completion state for vote action.
*
* @param int $voter
* @return void
*/
private function update_completion_state_on_vote(int $voter): void {
global $DB;
$completion = new completion_info($this->course);
if (!$completion->is_enabled($this->cm)) {
return;
}
$votes = $DB->count_records_sql('SELECT count(HQV.id) FROM {hotquestion_votes} HQV
JOIN {hotquestion_questions} HQQ ON (HQQ.id = HQV.question)
WHERE HQV.voter = :voter AND HQQ.hotquestion = :hotquestion;', ['voter' => $voter, 'hotquestion' => $this->instance->id] );
if ($votes > $this->instance->completionvote) {
$completion->update_state($this->cm, COMPLETION_COMPLETE);
} else {
$completion->update_state($this->cm, COMPLETION_INCOMPLETE);
}
}
and then add the following row in vote_on function and remove_vote function (just before the if statement closing).
$this->update_completion_state_on_vote($USER->id);
What do you think about it?
Regards,
Giorgio
p.s. I can provide you a pull request if needed.
There's another case to analyze about completion.
When the "Require heat" condition is defined, what will happen to completions when a question with votes is removed by the teacher?
Should we invalidate and re-evaluate completions for those students who voted the removed questions, perhaps requiring them to vote again other questions to complete the activity? My personal answer is yes, but I'm no one to decide.
Giorgio
Thanks for your interest in improving the HotQuestion plugin, and especially thanks for the code sample. A pull request would be great and the quickest way to get this implemented. Otherwise, I will add this to my TODO list and try to implement the needed changes for the upcomping Moodle 4.1 release. Right at the moment, I am trying to participate and do a little QA Testing for that upcoming Moodle 4.1 version release. I also am right in the middle of a huge capability update to the Diary plugin and it seems that every time I make one bug fix, I find another item that needs fixing, too.
Hi Al,
thanks for the reply. I'll do my best to implement the fix and submit the requests.
What do you think about the "delete question" case? Since the question related votes will be deleted as well, I think the best and consistent solution is to re-evaluate completions and, if necessary, change the state back to COMPLETION_INCOMPLETE.
Giorgio
Hi Al,
while working on this fix, I've noticed another bug on function get_question_voters on locallib.php.
The following db call doesn't return the voter column but the question column, since the latter is the second. Then the function returns an array of question ids and not user ids.
$voters = $DB->get_records_menu('hotquestion_votes', ['question' => $questionid], 'id, voter');
The fix is very simple, you just need to add the empty string for sort parameter and move 'id,voter' string to field parameter:
$voters = $DB->get_records_menu('hotquestion_votes', ['question' => $questionid], '', 'id, voter');
Do you need a specific issue and pull request or can I include this fix inside the current fix?
Regards,
Giorgio
Thanks for your continued interest in and support of the Hot Question plugin. Sorry to take so long to reply, but I had to spend about six hours yesterday trying to pickup a medicine refill. Due to being closed two days and changing software, it caused a backlog of people needing refills out at the Fort Rucker pharmacy. When I arrived there was 47 people ahead of me, and about 150 behind me when I left over five hours later. No telling when the last one was served.
Anyway, you can add it to your same pull request.
I realized I had a couple of other changes I have made to Hot Question, so I went ahead and made the voter ID change and pushed a new version to master. So, before you make a pull request for the completion changes you are working on you will need to take this into account.
Ok, thanks for the update. I've already merged your commits.
Giorgio
Hi Al,
I've made the pull request #70 for this issue.
I hope it is all ok and you could merge it without any problem.
Giorgio
Hi Giorgio,
I have completed the pull request, and also merged some code I added yesterday, that lets anyone show/hide unapproved questions. Those also are automatically sorted to the bottom of the question table now. The changes all appear to be working correctly on Moodle 3.11, Moodle 4.0, and the code for the upcoming and soon to be released Moodle 4.1.
You can see and try it out at https://www.drachels.com/moodle311/course/view.php?id=5
Log in info is posted on the log in page. You are welcome to add more HotQuestion activities to the course to try things the way you normally do them.
Hi Al,
I've done some tests and everything works as expected.
Giorgio
Just saw this as I was closing down for the day, so I will look at it as soon as I can. Got a new version of MooTyper that needs just a couple more verification tests, before a new release, so I will need to do those tests and hopefully the new release of MooTyper, before I can get to this.