Skip to content

Commit 11fea85

Browse files
committed
Fixed the issue certificate task failing on Oracle and MSSQL (#646)
1 parent 25f3ed1 commit 11fea85

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

classes/task/issue_certificates_task.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,25 @@ public function get_name(): string {
4545
* Execute.
4646
*/
4747
public function execute() {
48-
global $DB;
48+
global $CFG, $DB;
4949

5050
// Get the certificatesperrun, includeinnotvisiblecourses, and certificateexecutionperiod configurations.
5151
$certificatesperrun = (int)get_config('customcert', 'certificatesperrun');
5252
$includeinnotvisiblecourses = (bool)get_config('customcert', 'includeinnotvisiblecourses');
5353
$certificateexecutionperiod = (int)get_config('customcert', 'certificateexecutionperiod');
5454
$offset = (int)get_config('customcert', 'certificate_offset');
5555

56-
// We are going to issue certificates that have requested someone get emailed.
56+
if ($CFG->dbtype === 'oci') {
57+
// For Oracle, convert the CLOB to a VARCHAR2 (limiting to 4000 characters) since we are using DISTINCT.
58+
$emailothersselect = "DBMS_LOB.SUBSTR(c.emailothers, 4000, 1) AS emailothers";
59+
$emailotherslengthsql = "DBMS_LOB.GETLENGTH(c.emailothers)";
60+
} else {
61+
$emailothersselect = "c.emailothers";
62+
$emailotherslengthsql = $DB->sql_length('c.emailothers');
63+
}
64+
5765
$emailotherslengthsql = $DB->sql_length('c.emailothers');
58-
$sql = "SELECT c.id, c.templateid, c.course, c.requiredtime, c.emailstudents, c.emailteachers, c.emailothers,
66+
$sql = "SELECT DISTINCT c.id, c.templateid, c.course, c.requiredtime, c.emailstudents, c.emailteachers, $emailothersselect,
5967
ct.id AS templateid, ct.name AS templatename, ct.contextid, co.id AS courseid,
6068
co.fullname AS coursefullname, co.shortname AS courseshortname
6169
FROM {customcert} c
@@ -66,11 +74,10 @@ public function execute() {
6674
JOIN {course_categories} cat
6775
ON co.category = cat.id
6876
LEFT JOIN {customcert_issues} ci
69-
ON c.id = ci.customcertid";
70-
71-
$sql .= " WHERE (c.emailstudents = :emailstudents
72-
OR c.emailteachers = :emailteachers
73-
OR $emailotherslengthsql >= 3)";
77+
ON c.id = ci.customcertid
78+
WHERE (c.emailstudents = :emailstudents
79+
OR c.emailteachers = :emailteachers
80+
OR $emailotherslengthsql >= 3)";
7481

7582
$params = ['emailstudents' => 1, 'emailteachers' => 1];
7683

@@ -88,8 +95,6 @@ public function execute() {
8895
$params['enddate2'] = $params['enddate'];
8996
}
9097

91-
$sql .= " GROUP BY c.id, ct.id, ct.name, ct.contextid, co.id, co.fullname, co.shortname";
92-
9398
// Execute the SQL query.
9499
$customcerts = $DB->get_records_sql($sql, $params, $offset, $certificatesperrun);
95100

0 commit comments

Comments
 (0)