Aligned SQL to conform to Moodle standards

This commit is contained in:
Mark Nelson 2015-03-11 01:22:44 -07:00
parent 8a55c96dab
commit 6821122c30
6 changed files with 44 additions and 47 deletions

View file

@ -99,12 +99,12 @@ class restore_customcert_activity_task extends restore_activity_task {
// Get the customcert elements. // Get the customcert elements.
$sql = "SELECT e.* $sql = "SELECT e.*
FROM {customcert_elements} e FROM {customcert_elements} e
INNER JOIN {customcert_pages} p INNER JOIN {customcert_pages} p
ON e.pageid = p.id ON e.pageid = p.id
INNER JOIN {customcert} c INNER JOIN {customcert} c
ON p.customcertid = c.id ON p.customcertid = c.id
WHERE c.id = :customcertid"; WHERE c.id = :customcertid";
if ($elements = $DB->get_records_sql($sql, array('customcertid' => $this->get_activityid()))) { if ($elements = $DB->get_records_sql($sql, array('customcertid' => $this->get_activityid()))) {
// Go through the elements for the certificate. // Go through the elements for the certificate.
foreach ($elements as $e) { foreach ($elements as $e) {

View file

@ -110,9 +110,9 @@ class customcert_element_date extends customcert_element_base {
} else if ($dateitem == CUSTOMCERT_DATE_COMPLETION) { } else if ($dateitem == CUSTOMCERT_DATE_COMPLETION) {
// Get the enrolment end date. // Get the enrolment end date.
$sql = "SELECT MAX(c.timecompleted) as timecompleted $sql = "SELECT MAX(c.timecompleted) as timecompleted
FROM {course_completions} c FROM {course_completions} c
WHERE c.userid = :userid WHERE c.userid = :userid
AND c.course = :courseid"; AND c.course = :courseid";
if ($timecompleted = $DB->get_record_sql($sql, array('userid' => $issue->userid, 'courseid' => $COURSE->id))) { if ($timecompleted = $DB->get_record_sql($sql, array('userid' => $issue->userid, 'courseid' => $COURSE->id))) {
if (!empty($timecompleted->timecompleted)) { if (!empty($timecompleted->timecompleted)) {
$date = $timecompleted->timecompleted; $date = $timecompleted->timecompleted;

View file

@ -212,8 +212,8 @@ abstract class customcert_element_base {
$sequence = 1; $sequence = 1;
// Check if there already elements that exist, if so, overwrite value. // Check if there already elements that exist, if so, overwrite value.
$sql = "SELECT MAX(sequence) as maxsequence $sql = "SELECT MAX(sequence) as maxsequence
FROM {customcert_elements} FROM {customcert_elements}
WHERE pageid = :id"; WHERE pageid = :id";
// Get the current max sequence on this page and add 1 to get the new sequence. // Get the current max sequence on this page and add 1 to get the new sequence.
if ($maxseq = $DB->get_record_sql($sql, array('id' => $pageid))) { if ($maxseq = $DB->get_record_sql($sql, array('id' => $pageid))) {
$sequence = $maxseq->maxsequence + 1; $sequence = $maxseq->maxsequence + 1;

12
lib.php
View file

@ -90,10 +90,10 @@ function customcert_delete_instance($id) {
// Delete the elements. // Delete the elements.
$sql = "SELECT e.* $sql = "SELECT e.*
FROM {customcert_elements} e FROM {customcert_elements} e
INNER JOIN {customcert_pages} p INNER JOIN {customcert_pages} p
ON e.pageid = p.id ON e.pageid = p.id
WHERE p.customcertid = :customcertid"; WHERE p.customcertid = :customcertid";
if ($elements = $DB->get_records_sql($sql, array('customcertid' => $id))) { if ($elements = $DB->get_records_sql($sql, array('customcertid' => $id))) {
foreach ($elements as $element) { foreach ($elements as $element) {
// Get an instance of the element class. // Get an instance of the element class.
@ -137,8 +137,8 @@ function customcert_reset_userdata($data) {
if (!empty($data->reset_customcert)) { if (!empty($data->reset_customcert)) {
$sql = "SELECT cert.id $sql = "SELECT cert.id
FROM {customcert} cert FROM {customcert} cert
WHERE cert.course = :courseid"; WHERE cert.course = :courseid";
$DB->delete_records_select('customcert_issues', "customcertid IN ($sql)", array('courseid' => $data->courseid)); $DB->delete_records_select('customcert_issues', "customcertid IN ($sql)", array('courseid' => $data->courseid));
$status[] = array('component' => $componentstr, 'item' => get_string('customcertremoved', 'customcert'), 'error' => false); $status[] = array('component' => $componentstr, 'item' => get_string('customcertremoved', 'customcert'), 'error' => false);
} }

View file

@ -44,10 +44,10 @@ require_capability('mod/customcert:manage', $context);
if ($confirm) { if ($confirm) {
// First, remove all the existing elements and pages. // First, remove all the existing elements and pages.
$sql = "SELECT e.* $sql = "SELECT e.*
FROM {customcert_elements} e FROM {customcert_elements} e
INNER JOIN {customcert_pages} p INNER JOIN {customcert_pages} p
ON e.pageid = p.id ON e.pageid = p.id
WHERE p.customcertid = :customcertid"; WHERE p.customcertid = :customcertid";
if ($elements = $DB->get_records_sql($sql, array('customcertid' => $customcert->id))) { if ($elements = $DB->get_records_sql($sql, array('customcertid' => $customcert->id))) {
foreach ($elements as $element) { foreach ($elements as $element) {
// Get an instance of the element class. // Get an instance of the element class.

View file

@ -252,8 +252,8 @@ function customcert_add_page($data) {
$pagenumber = 1; $pagenumber = 1;
// Get the max page number. // Get the max page number.
$sql = "SELECT MAX(pagenumber) as maxpage $sql = "SELECT MAX(pagenumber) as maxpage
FROM {customcert_pages} cp FROM {customcert_pages} cp
WHERE cp.customcertid = :customcertid"; WHERE cp.customcertid = :customcertid";
if ($maxpage = $DB->get_record_sql($sql, array('customcertid' => $data->id))) { if ($maxpage = $DB->get_record_sql($sql, array('customcertid' => $data->id))) {
$pagenumber = $maxpage->maxpage + 1; $pagenumber = $maxpage->maxpage + 1;
} }
@ -301,11 +301,10 @@ function customcert_delete_page($pageid) {
// Now we want to decrease the page number values of // Now we want to decrease the page number values of
// the pages that are greater than the page we deleted. // the pages that are greater than the page we deleted.
$sql = "UPDATE {customcert_pages} $sql = "UPDATE {customcert_pages}
SET pagenumber = pagenumber - 1 SET pagenumber = pagenumber - 1
WHERE customcertid = :customcertid WHERE customcertid = :customcertid
AND pagenumber > :pagenumber"; AND pagenumber > :pagenumber";
$DB->execute($sql, array('customcertid' => $page->customcertid, $DB->execute($sql, array('customcertid' => $page->customcertid, 'pagenumber' => $page->pagenumber));
'pagenumber' => $page->pagenumber));
} }
/** /**
@ -407,17 +406,15 @@ function customcert_get_issues($customcertid, $groupmode, $cm, $page, $perpage)
$allparams = $conditionsparams + array('customcertid' => $customcertid); $allparams = $conditionsparams + array('customcertid' => $customcertid);
// Return the issues. // Return the issues.
return $DB->get_records_sql("SELECT u.*, ci.code, ci.timecreated $sql = "SELECT u.*, ci.code, ci.timecreated
FROM {user} u FROM {user} u
INNER JOIN {customcert_issues} ci INNER JOIN {customcert_issues} ci
ON u.id = ci.userid ON u.id = ci.userid
WHERE u.deleted = 0 WHERE u.deleted = 0
AND ci.customcertid = :customcertid AND ci.customcertid = :customcertid
$conditionssql $conditionssql
ORDER BY " . $DB->sql_fullname(), ORDER BY " . $DB->sql_fullname();
$allparams, return $DB->get_records_sql($sql, $allparams, $page * $perpage, $perpage);
$page * $perpage,
$perpage);
} }
/** /**
@ -438,14 +435,14 @@ function customcert_get_number_of_issues($customcertid, $cm, $groupmode) {
$allparams = $conditionsparams + array('customcertid' => $customcertid); $allparams = $conditionsparams + array('customcertid' => $customcertid);
// Return the number of issues. // Return the number of issues.
return $DB->count_records_sql("SELECT COUNT(u.id) as count $sql = "SELECT COUNT(u.id) as count
FROM {user} u FROM {user} u
INNER JOIN {customcert_issues} ci INNER JOIN {customcert_issues} ci
ON u.id = ci.userid ON u.id = ci.userid
WHERE u.deleted = 0 WHERE u.deleted = 0
AND ci.customcertid = :customcertid AND ci.customcertid = :customcertid
$conditionssql", $conditionssql";
$allparams); return $DB->count_records_sql($sql, $allparams);
} }
/** /**