Skip to content

Commit 94cfd4b

Browse files
committed
Fixed bug emailing all students (#531)
1 parent 0a84f18 commit 94cfd4b

File tree

2 files changed

+92
-7
lines changed

2 files changed

+92
-7
lines changed

classes/task/email_certificate_task.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public function execute() {
124124
if (!$fastmoduleinfo->visible) {
125125
continue;
126126
}
127+
127128
// Do not process an empty certificate.
128129
$sql = "SELECT ce.*
129130
FROM {customcert_elements} ce
@@ -150,7 +151,7 @@ public function execute() {
150151
$certificatename = format_string($customcert->name, true, ['context' => $context]);
151152

152153
// Used to create the email subject.
153-
$info = new \stdClass;
154+
$info = new \stdClass();
154155
$info->coursename = $courseshortname; // Added for BC, so users who have edited the string don't lose this value.
155156
$info->courseshortname = $courseshortname;
156157
$info->coursefullname = $coursefullname;
@@ -177,14 +178,10 @@ public function execute() {
177178
$userswithissue = get_users_by_capability($context, 'mod/customcert:receiveissue');
178179
// Get users with mod/customcert:view capability.
179180
$userswithview = get_users_by_capability($context, 'mod/customcert:view');
180-
// Users with both mod/customcert:view and mod/customcert:receiveissue cabapilities.
181+
// Users with both mod/customcert:view and mod/customcert:receiveissue capabilities.
181182
$userswithissueview = array_intersect_key($userswithissue, $userswithview);
182183

183-
$infomodule = new \core_availability\info_module($fastmoduleinfo);
184-
// Filter who can't access due to availability restriction, from the full list.
185-
$userscanissue = $infomodule->filter_user_list($userswithissueview);
186-
187-
foreach ($userscanissue as $enroluser) {
184+
foreach ($userswithissueview as $enroluser) {
188185
// Check if the user has already been issued.
189186
if (in_array($enroluser->id, array_keys((array)$issuedusers))) {
190187
continue;
@@ -195,6 +192,12 @@ public function execute() {
195192
continue;
196193
}
197194

195+
// Now check if the certificate is not visible to the current user.
196+
$cm = get_fast_modinfo($customcert->courseid, $enroluser->id)->instances['customcert'][$customcert->id];
197+
if (!$cm->uservisible) {
198+
continue;
199+
}
200+
198201
// Check that they have passed the required time.
199202
if (!empty($customcert->requiredtime)) {
200203
if (\mod_customcert\certificate::get_course_time($customcert->courseid,

tests/email_certificate_task_test.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
namespace mod_customcert;
2727

28+
use completion_info;
2829
use stdClass;
2930
use context_course;
3031
use advanced_testcase;
@@ -436,4 +437,85 @@ public function test_email_certificates_students_havent_met_required_time(): voi
436437
// Confirm no emails were sent.
437438
$this->assertCount(0, $emails);
438439
}
440+
441+
/**
442+
* Tests the email certificate task when the student has not met the completion criteria.
443+
*
444+
* @covers \mod_customcert\task\email_certificate_task
445+
*/
446+
public function test_email_certificates_students_havent_met_required_criteria(): void {
447+
global $CFG, $DB;
448+
449+
$CFG->enablecompletion = true;
450+
451+
// Create a course.
452+
$course = $this->getDataGenerator()->create_course();
453+
454+
// Create a user.
455+
$user1 = $this->getDataGenerator()->create_user();
456+
457+
// Enrol them in the course.
458+
$this->getDataGenerator()->enrol_user($user1->id, $course->id);
459+
460+
// Create a quiz.
461+
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
462+
463+
$quizmodule = $DB->get_record('course_modules', ['id' => $quiz->cmid]);
464+
465+
// Set completion criteria for the quiz.
466+
$quizmodule->completion = COMPLETION_TRACKING_AUTOMATIC;
467+
$quizmodule->completionview = 1; // Require view to complete.
468+
$quizmodule->completionexpected = 0;
469+
$DB->update_record('course_modules', $quizmodule);
470+
471+
// Set restrict access to the customcert activity based on the completion of the quiz.
472+
$customcert = $this->getDataGenerator()->create_module('customcert', [
473+
'course' => $course->id,
474+
'emailstudents' => 1,
475+
'availability' => json_encode(
476+
[
477+
'op' => '&',
478+
'c' => [
479+
[
480+
'type' => 'completion',
481+
'cm' => $quiz->cmid,
482+
'e' => COMPLETION_COMPLETE, // Ensure the quiz is marked as complete.
483+
],
484+
],
485+
'showc' => [
486+
false
487+
],
488+
]
489+
)
490+
]);
491+
492+
// Create template object.
493+
$template = new stdClass();
494+
$template->id = $customcert->templateid;
495+
$template->name = 'A template';
496+
$template->contextid = context_course::instance($course->id)->id;
497+
$template = new template($template);
498+
499+
// Add a page to this template.
500+
$pageid = $template->add_page();
501+
502+
// Add an element to the page.
503+
$element = new stdClass();
504+
$element->pageid = $pageid;
505+
$element->name = 'Image';
506+
$DB->insert_record('customcert_elements', $element);
507+
508+
// Run the task.
509+
$sink = $this->redirectEmails();
510+
$task = new email_certificate_task();
511+
$task->execute();
512+
$emails = $sink->get_messages();
513+
514+
// Confirm there are no issues as the user can not view the certificate.
515+
$issues = $DB->get_records('customcert_issues');
516+
$this->assertCount(0, $issues);
517+
518+
// Confirm no emails were sent.
519+
$this->assertCount(0, $emails);
520+
}
439521
}

0 commit comments

Comments
 (0)