diff --git a/classes/notifier/models/reminder/course_grade_range_model.php b/classes/notifier/models/reminder/course_grade_range_model.php index b6c040d1..c733e8b5 100644 --- a/classes/notifier/models/reminder/course_grade_range_model.php +++ b/classes/notifier/models/reminder/course_grade_range_model.php @@ -44,6 +44,8 @@ class course_grade_range_model extends reminder_notification_model implements re * @return array */ public function get_user_ids_to_notify() { + global $CFG; + // Make sure a grade_greater_than boundary is set. if (!$greaterthan = $this->condition->get_value('grade_greater_than')) { $greaterthan = 0; @@ -58,16 +60,19 @@ public function get_user_ids_to_notify() { // Where users are in a specific course. global $DB; - $queryresults = $DB->get_records_sql('SELECT u.id + $gradebookroles = explode(',', $CFG->gradebookroles); + list($insql, $params) = $DB->get_in_or_equal($gradebookroles); + $params[] = $this->get_course_id(); + $queryresults = $DB->get_records_sql("SELECT u.id FROM {user} u INNER JOIN {user_enrolments} ue ON ue.userid = u.id INNER JOIN {enrol} e ON e.id = ue.enrolid INNER JOIN {course} c ON c.id = e.courseid INNER JOIN {role_assignments} ra ON ra.userid = u.id INNER JOIN {context} ctx ON ctx.id = ra.contextid AND ctx.instanceid = c.id - WHERE ra.roleid IN (SELECT value FROM {config} WHERE name = "gradebookroles") + WHERE ra.roleid " . $insql . " AND c.id = ? - GROUP BY u.id', [$this->get_course_id()]); + GROUP BY u.id", $params); $courseuserids = array_keys($queryresults); diff --git a/classes/notifier/models/reminder/course_non_participation_model.php b/classes/notifier/models/reminder/course_non_participation_model.php index 9e053d72..73b10f51 100644 --- a/classes/notifier/models/reminder/course_non_participation_model.php +++ b/classes/notifier/models/reminder/course_non_participation_model.php @@ -47,19 +47,23 @@ public function get_user_ids_to_notify() { // Where users are in a specific course. // And where have not accessed the course since a conditionally set increment of time before now. - global $DB; + global $DB, $CFG; - $results = $DB->get_records_sql('SELECT u.id - FROM {user} u + $gradebookroles = explode(',', $CFG->gradebookroles); + list($insql, $params) = $DB->get_in_or_equal($gradebookroles); + $params[] = $this->condition->get_offset_timestamp_from_now('before'); + $params[] = $this->get_course_id(); + $results = $DB->get_records_sql("SELECT u.id + FROM {user} u INNER JOIN {user_enrolments} ue ON ue.userid = u.id INNER JOIN {enrol} e ON e.id = ue.enrolid INNER JOIN {course} c ON c.id = e.courseid INNER JOIN {role_assignments} ra ON ra.userid = u.id INNER JOIN {context} ctx ON ctx.id = ra.contextid AND ctx.instanceid = c.id - WHERE u.id NOT IN (SELECT la.userid FROM {user_lastaccess} la WHERE la.courseid = c.id AND la.timeaccess > ?) - AND ra.roleid IN (SELECT value FROM {config} WHERE name = "gradebookroles") - AND c.id = ? - GROUP BY u.id', [$this->condition->get_offset_timestamp_from_now('before'), $this->get_course_id()]); + WHERE ra.roleid " . $insql . " + AND u.id NOT IN (SELECT la.userid FROM {user_lastaccess} la WHERE la.courseid = c.id AND la.timeaccess > ?) + AND c.id = ? + GROUP BY u.id", $params); return array_keys($results); } diff --git a/tests/unit/messenger_compose_test.php b/tests/unit/messenger_compose_test.php index 222bc8a7..0c3abef4 100644 --- a/tests/unit/messenger_compose_test.php +++ b/tests/unit/messenger_compose_test.php @@ -30,7 +30,7 @@ use block_quickmail\persistents\signature; use block_quickmail\exceptions\validation_exception; -class block_quickmail_messenger_compose_testcase extends advanced_testcase { +class messenger_compose_test extends advanced_testcase { use has_general_helpers, sets_up_courses, @@ -57,6 +57,7 @@ public function test_messenger_sends_composed_email_now() { ]); // Send an email from the teacher to the students now (not as queued adhoc tasks). + $this->setUser($userteacher); messenger::compose($userteacher, $course, $composeformdata, null, false); $this->assertEquals(4, $this->email_sink_email_count($sink)); @@ -89,6 +90,7 @@ public function test_messenger_sends_composed_email_including_mentors_now() { ]); // Send an email from the teacher to the students now (not as queued adhoc tasks). + $this->setUser($userteacher); messenger::compose($userteacher, $course, $composeformdata, null, false); // Should have been sent to 4 users + 1 mentor. @@ -117,6 +119,7 @@ public function test_messenger_does_not_send_compose_message_with_invalid_params ]); // Send an email from the teacher to the students now (not as queued adhoc tasks). + $this->setUser($userteacher); messenger::compose($userteacher, $course, $composeformdata, null, false); $this->assertEquals(0, $this->email_sink_email_count($sink)); @@ -140,6 +143,7 @@ public function test_messenger_sends_composed_message_now() { $composeformdata = $this->get_compose_message_form_submission($recipients, 'message', []); // Send a moodle message from the teacher to the students now (not as queued adhoc tasks). + $this->setUser($userteacher); messenger::compose($userteacher, $course, $composeformdata, null, false); $this->assertEquals(4, $this->message_sink_message_count($sink)); @@ -165,6 +169,7 @@ public function test_skips_invalid_user_ids_when_sending() { ]); // Send an email from the teacher to the students as queued adhoc tasks). + $this->setUser($userteacher); messenger::compose($userteacher, $course, $composeformdata, null, false); $this->assertEquals(1, $this->email_sink_email_count($sink)); @@ -193,6 +198,7 @@ public function test_messenger_does_not_send_scheduled_composed_email_now() { ]); // Schedule an email from the teacher to the students (as queued adhoc tasks). + $this->setUser($userteacher); messenger::compose($userteacher, $course, $composeformdata); \phpunit_util::run_all_adhoc_tasks(); @@ -221,6 +227,7 @@ public function test_messenger_sends_to_additional_emails() { ]); // Send an email from the teacher to the students now (not as queued adhoc tasks). + $this->setUser($userteacher); messenger::compose($userteacher, $course, $composeformdata, null, false); $this->assertEquals(7, $this->email_sink_email_count($sink)); @@ -255,6 +262,7 @@ public function test_messenger_sends_a_receipt_if_asked() { ]); // Send an email from the teacher to the students now (not as queued adhoc tasks). + $this->setUser($userteacher); messenger::compose($userteacher, $course, $composeformdata, null, false); $this->assertEquals(5, $this->email_sink_email_count($sink)); @@ -295,6 +303,7 @@ public function test_messenger_sends_with_signature_appended() { ]); // Send an email from the teacher to the students now (not as queued adhoc tasks). + $this->setUser($userteacher); messenger::compose($userteacher, $course, $composeformdata, null, false); $this->assertTrue($this->email_in_sink_body_contains($sink, 1, 'This is one fine body.')); diff --git a/tests/unit/messenger_drafting_test.php b/tests/unit/messenger_drafting_test.php index 5248431e..69dbe6fb 100644 --- a/tests/unit/messenger_drafting_test.php +++ b/tests/unit/messenger_drafting_test.php @@ -30,7 +30,7 @@ use block_quickmail\persistents\signature; use block_quickmail\exceptions\validation_exception; -class block_quickmail_messenger_drafting_testcase extends advanced_testcase { +class messenger_drafting_test extends advanced_testcase { use has_general_helpers, sets_up_courses, @@ -58,6 +58,7 @@ public function test_messenger_saves_draft_email() { ]); // Save this email message as a draft. + $this->setUser($userteacher); $message = messenger::save_compose_draft($userteacher, $course, $composeformdata); $messagerecipients = $message->get_message_recipients(); @@ -87,6 +88,7 @@ public function test_cannot_duplicate_a_draft_that_not_created_by_the_given_user ]); // Save this email message as a draft. + $this->setUser($userteacher); $draftmessage = messenger::save_compose_draft($userteacher, $course, $composeformdata); $this->expectException(validation_exception::class); @@ -114,6 +116,7 @@ public function test_duplicates_drafts() { ]); // Save this email message as a draft. + $this->setUser($userteacher); $draftmessage = messenger::save_compose_draft($userteacher, $course, $composeformdata); // Now attempt to duplicate this draft. diff --git a/tests/unit/send_all_ready_messages_task_test.php b/tests/unit/send_all_ready_messages_task_test.php index d244e120..e18dc013 100644 --- a/tests/unit/send_all_ready_messages_task_test.php +++ b/tests/unit/send_all_ready_messages_task_test.php @@ -28,7 +28,7 @@ use block_quickmail\messenger\messenger; use block_quickmail\tasks\send_all_ready_messages_task; -class block_quickmail_send_all_ready_messages_task_testcase extends advanced_testcase { +class send_all_ready_messages_task_test extends advanced_testcase { use has_general_helpers, sets_up_courses, @@ -99,6 +99,7 @@ private function create_messages($course, $userteacher, $userstudents) { ]); // Schedule an email from the teacher to the students (as queued adhoc tasks). + $this->setUser($userteacher); $message = messenger::compose($userteacher, $course, $composeformdata, null, true); $messages[] = $message; diff --git a/tests/unit/send_message_adhoc_task_test.php b/tests/unit/send_message_adhoc_task_test.php index 02237c2f..7d0839f5 100644 --- a/tests/unit/send_message_adhoc_task_test.php +++ b/tests/unit/send_message_adhoc_task_test.php @@ -29,7 +29,7 @@ use block_quickmail\tasks\send_message_adhoc_task; use core\task\manager as task_manager; -class block_quickmail_send_message_adhoc_task_testcase extends advanced_testcase { +class send_message_adhoc_task_test extends advanced_testcase { use has_general_helpers, sets_up_courses, @@ -70,6 +70,7 @@ public function test_send_message_adhoc_task_sends() { ]); // Schedule an email from the teacher to the students (as queued adhoc tasks). + $this->setUser($userteacher); $message = messenger::compose($userteacher, $course, $composeformdata, null, true); \phpunit_util::run_all_adhoc_tasks(); diff --git a/tests/unit/traits/has_general_helpers.php b/tests/unit/traits/has_general_helpers.php index 08ea5496..dee2d1e5 100644 --- a/tests/unit/traits/has_general_helpers.php +++ b/tests/unit/traits/has_general_helpers.php @@ -85,18 +85,7 @@ public function get_course_config_params(array $overrideparams = []) { } public function update_system_config_value($configname, $newvalue) { - global $DB; - - if ($record = $DB->get_record('config', ['name' => $configname])) { - $record->value = $newvalue; - - $DB->update_record('config', $record); - } else { - $DB->insert_record('config', (object)[ - 'name' => $configname, - 'value' => $newvalue, - ]); - } + set_config($configname, $newvalue); } public function override_params($values, $overrides) { diff --git a/tests/unit/traits/submits_compose_message_form.php b/tests/unit/traits/submits_compose_message_form.php index ccdc6b78..7b1ea3e6 100644 --- a/tests/unit/traits/submits_compose_message_form.php +++ b/tests/unit/traits/submits_compose_message_form.php @@ -86,7 +86,7 @@ private function get_recipients_array($recipients) { // Not sure how this ever worked with undescores. // Recipient IDs will never have been captured. $containername = $inclusiontype . 'entityids'; - $containername[] = $recipienttype . '_' . $id; + $$containername[] = $recipienttype . '_' . $id; } } }