From 03889403de838ec4c428aa1f1d4ff8bec7547829 Mon Sep 17 00:00:00 2001 From: Daniil Fajnberg Date: Tue, 5 Mar 2024 13:36:57 +0100 Subject: [PATCH] Comply with Moodle SQL code style Also slightly simplify SQL statement construction/concatenation --- classes/course_files.php | 50 +++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/classes/course_files.php b/classes/course_files.php index d8babe9..7f38109 100644 --- a/classes/course_files.php +++ b/classes/course_files.php @@ -155,14 +155,14 @@ public function get_file_list($offset, $limit) { $usernameselect = get_all_user_name_fields(true, 'u'); } - $sql = 'FROM {files} f - LEFT JOIN {context} c ON (c.id = f.contextid) - LEFT JOIN {user} u ON (u.id = f.userid) - WHERE f.filename NOT LIKE \'.\' - AND (c.path LIKE :path OR c.id = :cid) ' . $sqlwhere; + $sql = "FROM {files} f + LEFT JOIN {context} c ON (c.id = f.contextid) + LEFT JOIN {user} u ON (u.id = f.userid) + WHERE f.filename NOT LIKE '.' + AND (c.path LIKE :path OR c.id = :cid) $sqlwhere"; - $sqlselectfiles = 'SELECT f.*, c.contextlevel, c.instanceid, ' . $usernameselect . - ' ' . $sql . ' ORDER BY f.component, f.filename'; + $sqlselectfiles = "SELECT f.*, c.contextlevel, c.instanceid, $usernameselect $sql + ORDER BY f.component, f.filename"; $params = [ 'path' => $this->context->path . '/%', @@ -176,7 +176,7 @@ public function get_file_list($offset, $limit) { if (count($this->filelist) < $limit) { $this->filescount = count($this->filelist) + $offset; } else { - $sqlcount = 'SELECT COUNT(*) ' . $sql; + $sqlcount = "SELECT COUNT(*) $sql"; $this->filescount = $DB->count_records_sql($sqlcount, $params); } @@ -230,12 +230,12 @@ public function get_components() { return $this->components; } - $sql = 'SELECT f.component - FROM {files} f - LEFT JOIN {context} c ON (c.id = f.contextid) - WHERE f.filename NOT LIKE \'.\' - AND (c.path LIKE :path OR c.id = :cid) - GROUP BY f.component'; + $sql = "SELECT f.component + FROM {files} f + LEFT JOIN {context} c ON (c.id = f.contextid) + WHERE f.filename NOT LIKE '.' + AND (c.path LIKE :path OR c.id = :cid) + GROUP BY f.component"; $params = ['path' => $this->context->path . '/%', 'cid' => $this->context->id]; $ret = $DB->get_fieldset_sql($sql, $params); @@ -280,10 +280,10 @@ public function set_files_license($fileids, $license) { // Check if the given files really belong to the context. list($sqlin, $paramfids) = $DB->get_in_or_equal(array_keys($fileids), SQL_PARAMS_QM); - $sql = 'SELECT f.id, f.contextid, c.path - FROM {files} f - JOIN {context} c ON (c.id = f.contextid) - WHERE f.id ' . $sqlin; + $sql = "SELECT f.id, f.contextid, c.path + FROM {files} f + JOIN {context} c ON (c.id = f.contextid) + WHERE f.id $sqlin"; $res = $DB->get_records_sql($sql, $paramfids); $checkedfileids = array_column($this->check_files_context($res), 'id'); @@ -293,9 +293,7 @@ public function set_files_license($fileids, $license) { list($sqlin, $paramfids) = $DB->get_in_or_equal($checkedfileids, SQL_PARAMS_QM); $transaction = $DB->start_delegated_transaction(); - $sql = 'UPDATE {files} - SET license = ? - WHERE id ' . $sqlin; + $sql = "UPDATE {files} SET license = ? WHERE id $sqlin"; $DB->execute($sql, array_merge([$license], $paramfids)); foreach ($checkedfileids as $fid) { @@ -351,11 +349,11 @@ public function download_files(&$fileids) { } list($sqlin, $paramfids) = $DB->get_in_or_equal(array_keys($fileids), SQL_PARAMS_QM); - $sql = 'SELECT f.*, c.path, r.repositoryid, r.reference, r.lastsync AS referencelastsync - FROM {files} f - LEFT JOIN {context} c ON (c.id = f.contextid) - LEFT JOIN {files_reference} r ON (f.referencefileid = r.id) - WHERE f.id ' . $sqlin; + $sql = "SELECT f.*, c.path, r.repositoryid, r.reference, r.lastsync AS referencelastsync + FROM {files} f + LEFT JOIN {context} c ON (c.id = f.contextid) + LEFT JOIN {files_reference} r ON (f.referencefileid = r.id) + WHERE f.id $sqlin"; $res = $DB->get_records_sql($sql, $paramfids); $checkedfiles = $this->check_files_context($res);