Skip to content

Commit 45e7acd

Browse files
andrewhancoxmdjnelson
authored andcommitted
Fix notifications when the instance of the activity is on site home #693
1 parent 19fd962 commit 45e7acd

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

classes/task/issue_certificates_task.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function execute() {
7171
ON c.templateid = ct.id
7272
JOIN {course} co
7373
ON c.course = co.id
74-
JOIN {course_categories} cat
74+
LEFT JOIN {course_categories} cat
7575
ON co.category = cat.id
7676
LEFT JOIN {customcert_issues} ci
7777
ON c.id = ci.customcertid
@@ -84,7 +84,7 @@ public function execute() {
8484
// Check the includeinnotvisiblecourses configuration.
8585
if (!$includeinnotvisiblecourses) {
8686
// Exclude certificates from hidden courses.
87-
$sql .= " AND co.visible = 1 AND cat.visible = 1";
87+
$sql .= " AND co.visible = 1 AND (cat.visible = 1 OR cat.id IS NULL)";
8888
}
8989

9090
// Add condition based on certificate execution period.

tests/email_certificate_task_test.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
namespace mod_customcert;
2727

2828
use completion_info;
29+
use context_module;
2930
use stdClass;
3031
use context_course;
3132
use advanced_testcase;
@@ -223,6 +224,85 @@ public function test_email_certificates_students(): void {
223224
$this->assertCount(0, $emails);
224225
}
225226

227+
/**
228+
* Tests the email certificate task for instance on the site home course.
229+
*
230+
* @covers \mod_customcert\task\issue_certificates_task
231+
* @covers \mod_customcert\task\email_certificate_task
232+
*/
233+
public function test_email_certificates_sitehome(): void {
234+
global $CFG, $DB, $SITE;
235+
236+
// Create some users.
237+
$user1 = $this->getDataGenerator()->create_user();
238+
$user2 = $this->getDataGenerator()->create_user();
239+
240+
// Create a custom certificate.
241+
$customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $SITE->id,
242+
'emailstudents' => 1]);
243+
244+
$role = $DB->get_record('role', ['archetype' => 'user']);
245+
role_change_permission($role->id, context_module::instance($customcert->cmid), 'mod/customcert:view', CAP_ALLOW);
246+
role_change_permission($role->id, context_module::instance($customcert->cmid), 'mod/customcert:receiveissue', CAP_ALLOW);
247+
248+
// Create template object.
249+
$template = new stdClass();
250+
$template->id = $customcert->templateid;
251+
$template->name = 'A template';
252+
$template->contextid = context_course::instance($SITE->id)->id;
253+
$template = new template($template);
254+
255+
// Add a page to this template.
256+
$pageid = $template->add_page();
257+
258+
// Add an element to the page.
259+
$element = new stdClass();
260+
$element->pageid = $pageid;
261+
$element->name = 'Image';
262+
$DB->insert_record('customcert_elements', $element);
263+
264+
// Ok, now issue this to one user.
265+
\mod_customcert\certificate::issue_certificate($customcert->id, $user1->id);
266+
267+
// Confirm there is only one entry in this table.
268+
$this->assertEquals(1, $DB->count_records('customcert_issues'));
269+
270+
// Run the task.
271+
$sink = $this->redirectEmails();
272+
$task = new issue_certificates_task();
273+
$task->execute();
274+
$emails = $sink->get_messages();
275+
276+
// Get the issues from the issues table now.
277+
$issues = $DB->get_records('customcert_issues');
278+
$this->assertCount(3, $issues);
279+
280+
// Confirm that it was marked as emailed and was not issued to the teacher.
281+
foreach ($issues as $issue) {
282+
$this->assertEquals(1, $issue->emailed);
283+
}
284+
285+
// Confirm that we sent out emails to the two users.
286+
$this->assertCount(3, $emails);
287+
288+
$this->assertEquals($CFG->noreplyaddress, $emails[1]->from);
289+
$this->assertEquals($user1->email, $emails[1]->to);
290+
291+
$this->assertEquals($CFG->noreplyaddress, $emails[2]->from);
292+
$this->assertEquals($user2->email, $emails[2]->to);
293+
294+
// Now, run the task again and ensure we did not issue any more certificates.
295+
$sink = $this->redirectEmails();
296+
$task = new issue_certificates_task();
297+
$task->execute();
298+
$emails = $sink->get_messages();
299+
300+
$issues = $DB->get_records('customcert_issues');
301+
302+
$this->assertCount(3, $issues);
303+
$this->assertCount(0, $emails);
304+
}
305+
226306
/**
227307
* Tests the email certificate task for teachers.
228308
*

0 commit comments

Comments
 (0)