Permalink
Browse files

MDL-37953 mod_forum: using named parameters to build SQL statement

  • Loading branch information...
markn86 committed Feb 11, 2013
1 parent da48404 commit 8076010d3791c323b565a5b5b1c73c6262a9df89
Showing with 36 additions and 24 deletions.
  1. +36 −24 mod/forum/lib.php
View
@@ -45,7 +45,7 @@
define('FORUM_TRACKING_OPTIONAL', 1);
define('FORUM_TRACKING_ON', 2);
-define('FORUM_MAILED_NOTYET', 0);
+define('FORUM_MAILED_PENDING', 0);
define('FORUM_MAILED_SUCCESS', 1);
define('FORUM_MAILED_ERROR', 2);
@@ -756,7 +756,7 @@ function forum_cron() {
foreach ($posts as $post) {
mtrace($mailcount[$post->id]." users were sent post $post->id, '$post->subject'");
if ($errorcount[$post->id]) {
- $DB->set_field("forum_posts", "mailed", FORUM_MAILED_ERROR, array("id" => "$post->id"));
+ $DB->set_field('forum_posts', 'mailed', FORUM_MAILED_ERROR, array('id' => $post->id));
}
}
}
@@ -2139,26 +2139,31 @@ function forum_get_ratings($context, $postid, $sort = "u.firstname ASC") {
function forum_get_unmailed_posts($starttime, $endtime, $now=null) {
global $CFG, $DB;
- $params = array($starttime, $endtime);
+ $params = array();
+ $params['mailed'] = FORUM_MAILED_PENDING;
+ $params['ptimestart'] = $starttime;
+ $params['ptimeend'] = $endtime;
+ $params['mailnow'] = 1;
+
if (!empty($CFG->forum_enabletimedposts)) {
if (empty($now)) {
$now = time();
}
- $timedsql = "AND (d.timestart < ? AND (d.timeend = 0 OR d.timeend > ?))";
- $params[] = $now;
- $params[] = $now;
+ $timedsql = "AND (d.timestart < :dtimestart AND (d.timeend = 0 OR d.timeend > :dtimeend))";
+ $params['dtimestart'] = $now;
+ $params['dtimeend'] = $now;
} else {
$timedsql = "";
}
return $DB->get_records_sql("SELECT p.*, d.course, d.forum
- FROM {forum_posts} p
- JOIN {forum_discussions} d ON d.id = p.discussion
- WHERE p.mailed = ".FORUM_MAILED_NOTYET."
- AND p.created >= ?
- AND (p.created < ? OR p.mailnow = 1)
- $timedsql
- ORDER BY p.modified ASC", $params);
+ FROM {forum_posts} p
+ JOIN {forum_discussions} d ON d.id = p.discussion
+ WHERE p.mailed = :mailed
+ AND p.created >= :ptimestart
+ AND (p.created < :ptimeend OR p.mailnow = :mailnow)
+ $timedsql
+ ORDER BY p.modified ASC", $params);
}
/**
@@ -2172,24 +2177,31 @@ function forum_get_unmailed_posts($starttime, $endtime, $now=null) {
*/
function forum_mark_old_posts_as_mailed($endtime, $now=null) {
global $CFG, $DB;
+
if (empty($now)) {
$now = time();
}
+ $params = array();
+ $params['mailedsuccess'] = FORUM_MAILED_SUCCESS;
+ $params['now'] = $now;
+ $params['endtime'] = $endtime;
+ $params['mailnow'] = 1;
+ $params['mailedpending'] = FORUM_MAILED_PENDING;
+
if (empty($CFG->forum_enabletimedposts)) {
return $DB->execute("UPDATE {forum_posts}
- SET mailed = ".FORUM_MAILED_SUCCESS."
- WHERE (created < ? OR mailnow = 1)
- AND mailed = " . FORUM_MAILED_NOTYET, array($endtime));
-
+ SET mailed = :mailedsuccess
+ WHERE (created < :endtime OR mailnow = :mailnow)
+ AND mailed = :mailedpending", $params);
} else {
return $DB->execute("UPDATE {forum_posts}
- SET mailed = ".FORUM_MAILED_SUCCESS."
+ SET mailed = :mailedsuccess
WHERE discussion NOT IN (SELECT d.id
- FROM {forum_discussions} d
- WHERE d.timestart > ?)
- AND (created < ? OR mailnow = 1)
- AND mailed = " . FORUM_MAILED_NOTYET, array($now, $endtime));
+ FROM {forum_discussions} d
+ WHERE d.timestart > :now)
+ AND (created < :endtime OR mailnow = :mailnow)
+ AND mailed = :mailedpending", $params);
}
}
@@ -4316,7 +4328,7 @@ function forum_add_new_post($post, $mform, &$message) {
$context = context_module::instance($cm->id);
$post->created = $post->modified = time();
- $post->mailed = FORUM_MAILED_NOTYET;
+ $post->mailed = FORUM_MAILED_PENDING;
$post->userid = $USER->id;
$post->attachment = "";
@@ -4420,7 +4432,7 @@ function forum_add_discussion($discussion, $mform=null, $unused=null, $userid=nu
$post->userid = $userid;
$post->created = $timenow;
$post->modified = $timenow;
- $post->mailed = FORUM_MAILED_NOTYET;
+ $post->mailed = FORUM_MAILED_PENDING;
$post->subject = $discussion->name;
$post->message = $discussion->message;
$post->messageformat = $discussion->messageformat;

0 comments on commit 8076010

Please sign in to comment.