From 895c9d0fdb4155a9f5f20d12f2d9add822e93670 Mon Sep 17 00:00:00 2001 From: Mario Wehr Date: Wed, 26 Sep 2018 14:23:39 +0200 Subject: [PATCH 1/3] Added PostgreSQL compatibility --- classes/repos/queued_repo.php | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/classes/repos/queued_repo.php b/classes/repos/queued_repo.php index 9da54d69..91186e50 100644 --- a/classes/repos/queued_repo.php +++ b/classes/repos/queued_repo.php @@ -180,23 +180,27 @@ public static function get_for_user($user_id, $course_id = 0, $params = []) return $repo->result; } - private static function get_for_user_sql($course_id, $sort_by, $sort_dir, $as_count = false) - { - $sql = $as_count - ? 'SELECT COUNT(DISTINCT m.id) ' - : 'SELECT DISTINCT m.* '; + private static function get_for_user_sql($course_id, $sort_by, $sort_dir, $as_count = false) + { + $sql = $as_count + ? 'SELECT COUNT(res.id) FROM (select DISTINCT id, to_send_at ' + : 'SELECT DISTINCT m.* '; - $sql .= 'FROM {block_quickmail_messages} m + $sql .= 'FROM {block_quickmail_messages} m WHERE m.user_id = :user_id'; - if ($course_id) { - $sql .= ' AND m.course_id = :course_id'; - } - - $sql .= ' AND m.to_send_at <> 0 AND m.timedeleted = 0 AND m.sent_at = 0 AND m.is_draft = 0 ORDER BY ' . $sort_by . ' ' . $sort_dir; + if ($course_id) { + $sql .= ' AND m.course_id = :course_id'; + } - return $sql; - } + $sql .= ' AND m.to_send_at <> 0 AND m.timedeleted = 0 AND m.sent_at = 0 AND m.is_draft = 0 ORDER BY ' . $sort_by . ' ' . $sort_dir; + + $sql .= $as_count + ? ') as res ' + : ''; + + return $sql; + } /** * Returns an array of all messages that should be sent by the system right now From 11e4cbf1157fb3507b919e02399bcabd6173d22c Mon Sep 17 00:00:00 2001 From: Mario Wehr Date: Wed, 26 Sep 2018 21:25:12 +0200 Subject: [PATCH 2/3] Added PostgreSQL compatibility --- classes/repos/sent_repo.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/classes/repos/sent_repo.php b/classes/repos/sent_repo.php index 8645f891..84f00fd2 100644 --- a/classes/repos/sent_repo.php +++ b/classes/repos/sent_repo.php @@ -117,7 +117,7 @@ public static function get_for_user($user_id, $course_id = 0, $params = []) private static function get_for_user_sql($course_id, $sort_by, $sort_dir, $as_count = false) { $sql = $as_count - ? 'SELECT COUNT(DISTINCT m.id) ' + ? 'SELECT COUNT(res.id) FROM (select DISTINCT id, sent_at ' : 'SELECT DISTINCT m.* '; $sql .= 'FROM {block_quickmail_messages} m @@ -126,9 +126,13 @@ private static function get_for_user_sql($course_id, $sort_by, $sort_dir, $as_co if ($course_id) { $sql .= ' AND m.course_id = :course_id'; } - + $sql .= ' AND m.is_draft = 0 AND m.timedeleted = 0 AND m.sent_at > 0 ORDER BY ' . $sort_by . ' ' . $sort_dir; + $sql .= $as_count + ? ') as res ' + : ''; + return $sql; } From 1ee713c7e4c85099f095c07f41de2b29fd22ee6d Mon Sep 17 00:00:00 2001 From: Mario Wehr Date: Wed, 26 Sep 2018 21:54:37 +0200 Subject: [PATCH 3/3] Added PostgreSQL compatibility better sorter handling --- classes/repos/notification_repo.php | 11 +++++++---- classes/repos/queued_repo.php | 10 ++++++---- classes/repos/sent_repo.php | 6 +++--- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/classes/repos/notification_repo.php b/classes/repos/notification_repo.php index 94ff572d..a3c557dc 100644 --- a/classes/repos/notification_repo.php +++ b/classes/repos/notification_repo.php @@ -130,10 +130,10 @@ public static function get_notification_for_course_user_or_null($notification_id return $notification; } - private static function get_all_for_course_sql($course_id, $user_id = null, $sort_by, $sort_dir, $as_count = false) + private static function get_all_for_course_sql($course_id, $user_id = null, $sort_by = null, $sort_dir, $as_count = false) { $sql = $as_count - ? 'SELECT COUNT(DISTINCT n.id) ' + ? 'SELECT COUNT(res.id) FROM (select DISTINCT id' . ($sort_by? ','.$sort_by:'') : 'SELECT DISTINCT n.* '; $sql .= 'FROM {block_quickmail_notifs} n @@ -143,8 +143,11 @@ private static function get_all_for_course_sql($course_id, $user_id = null, $sor $sql .= ' AND n.user_id = :user_id'; } - $sql .= ' AND n.timedeleted = 0 - ORDER BY ' . $sort_by . ' ' . $sort_dir; + $sql .= ' AND n.timedeleted = 0 ORDER BY ' . $sort_by . ' ' . $sort_dir; + + $sql .= $as_count + ? ') as res ' + : ''; return $sql; } diff --git a/classes/repos/queued_repo.php b/classes/repos/queued_repo.php index 91186e50..64c287cb 100644 --- a/classes/repos/queued_repo.php +++ b/classes/repos/queued_repo.php @@ -180,13 +180,15 @@ public static function get_for_user($user_id, $course_id = 0, $params = []) return $repo->result; } - private static function get_for_user_sql($course_id, $sort_by, $sort_dir, $as_count = false) + private static function get_for_user_sql($course_id, $sort_by = null, $sort_dir, $as_count = false) { + + $sql = $as_count - ? 'SELECT COUNT(res.id) FROM (select DISTINCT id, to_send_at ' + ? 'SELECT COUNT(res.id) FROM (select DISTINCT id' . ($sort_by? ','.$sort_by:'') : 'SELECT DISTINCT m.* '; - $sql .= 'FROM {block_quickmail_messages} m + $sql .= ' FROM {block_quickmail_messages} m WHERE m.user_id = :user_id'; if ($course_id) { @@ -202,7 +204,7 @@ private static function get_for_user_sql($course_id, $sort_by, $sort_dir, $as_co return $sql; } - /** + /** * Returns an array of all messages that should be sent by the system right now * * @return array diff --git a/classes/repos/sent_repo.php b/classes/repos/sent_repo.php index 84f00fd2..932a903e 100644 --- a/classes/repos/sent_repo.php +++ b/classes/repos/sent_repo.php @@ -114,13 +114,13 @@ public static function get_for_user($user_id, $course_id = 0, $params = []) return $repo->result; } - private static function get_for_user_sql($course_id, $sort_by, $sort_dir, $as_count = false) + private static function get_for_user_sql($course_id, $sort_by = null, $sort_dir, $as_count = false) { $sql = $as_count - ? 'SELECT COUNT(res.id) FROM (select DISTINCT id, sent_at ' + ? 'SELECT COUNT(res.id) FROM (select DISTINCT id' . ($sort_by? ','.$sort_by:'') : 'SELECT DISTINCT m.* '; - $sql .= 'FROM {block_quickmail_messages} m + $sql .= ' FROM {block_quickmail_messages} m WHERE m.user_id = :user_id'; if ($course_id) {