Skip to content

Commit b2eb641

Browse files
Posts of users who are reviewers are already marked as reviewed upon creation; Correctly mail reviewed posts
1 parent dacb55d commit b2eb641

File tree

7 files changed

+51
-21
lines changed

7 files changed

+51
-21
lines changed

db/install.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<XMLDB PATH="mod/moodleoverflow/db" VERSION="20190523" COMMENT="XMLDB file for Moodle mod/moodleoverflow"
2+
<XMLDB PATH="mod/moodleoverflow/db" VERSION="20220720" COMMENT="XMLDB file for Moodle mod/moodleoverflow"
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
55
>
@@ -69,6 +69,7 @@
6969
<FIELD NAME="attachment" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false"/>
7070
<FIELD NAME="mailed" TYPE="int" LENGTH="1" NOTNULL="false" DEFAULT="0" SEQUENCE="false"/>
7171
<FIELD NAME="reviewed" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" SEQUENCE="false"/>
72+
<FIELD NAME="timereviewed" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
7273
</FIELDS>
7374
<KEYS>
7475
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>

db/upgrade.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ function xmldb_moodleoverflow_upgrade($oldversion) {
195195
upgrade_mod_savepoint(true, 2021111700, 'moodleoverflow');
196196
}
197197

198-
if ($oldversion < 2022070600) {
198+
if ($oldversion < 2022072000) {
199199

200200
// Define field needsreview to be added to moodleoverflow.
201201
$table = new xmldb_table('moodleoverflow');
@@ -206,7 +206,7 @@ function xmldb_moodleoverflow_upgrade($oldversion) {
206206
$dbman->add_field($table, $field);
207207
}
208208

209-
// Define field reviewed to be added to moodleoverflow_posts.
209+
// Define field reviewed and timereviewed to be added to moodleoverflow_posts.
210210
$table = new xmldb_table('moodleoverflow_posts');
211211
$field = new xmldb_field('reviewed', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'mailed');
212212

@@ -215,8 +215,15 @@ function xmldb_moodleoverflow_upgrade($oldversion) {
215215
$dbman->add_field($table, $field);
216216
}
217217

218+
$field = new xmldb_field('timereviewed', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'reviewed');
219+
220+
// Conditionally launch add field timereviewed.
221+
if (!$dbman->field_exists($table, $field)) {
222+
$dbman->add_field($table, $field);
223+
}
224+
218225
// Moodleoverflow savepoint reached.
219-
upgrade_mod_savepoint(true, 2022070600, 'moodleoverflow');
226+
upgrade_mod_savepoint(true, 2022072000, 'moodleoverflow');
220227
}
221228

222229
return true;

externallib.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ public static function review_approve_post_returns() {
163163
return new external_value(PARAM_TEXT, 'the url of the next post to review');
164164
}
165165

166+
/**
167+
* Approve a post.
168+
*
169+
* @param int $postid ID of post to approve.
170+
* @return string|null Url of next post to review.
171+
*/
166172
public static function review_approve_post($postid) {
167173
global $DB;
168174

@@ -182,6 +188,7 @@ public static function review_approve_post($postid) {
182188
require_capability('mod/moodleoverflow:reviewpost', $context);
183189

184190
$post->reviewed = 1;
191+
$post->timereviewed = time();
185192

186193
$DB->update_record('moodleoverflow_posts', $post);
187194

@@ -207,10 +214,17 @@ public static function review_reject_post_returns() {
207214
return new external_value(PARAM_TEXT, 'the url of the next post to review');
208215
}
209216

210-
public static function review_reject_post($postid) {
217+
/**
218+
* Rejects a post.
219+
*
220+
* @param int $postid ID of post to reject.
221+
* @param string|null $reason The reason for rejection.
222+
* @return string|null Url of next post to review.
223+
*/
224+
public static function review_reject_post($postid, $reason = null) {
211225
global $DB;
212226

213-
$params = self::validate_parameters(self::review_reject_post_parameters(), ['postid' => $postid]);
227+
$params = self::validate_parameters(self::review_reject_post_parameters(), ['postid' => $postid, 'reason' => $reason]);
214228
$postid = $params['postid'];
215229

216230
$post = $DB->get_record('moodleoverflow_posts', ['id' => $postid], '*', MUST_EXIST);

lib.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,8 @@ function moodleoverflow_get_unmailed_posts($starttime, $endtime) {
10131013
$sql = "SELECT p.*, d.course, d.moodleoverflow
10141014
FROM {moodleoverflow_posts} p
10151015
JOIN {moodleoverflow_discussions} d ON d.id = p.discussion
1016-
WHERE p.mailed = :mailed AND p.created >= :ptimestart AND p.created < :ptimeend AND p.reviewed = 1
1016+
WHERE p.mailed = :mailed AND p.reviewed = 1
1017+
AND COALESCE(p.timereview, p.created) >= :ptimestart AND COALESCE(p.timereview, p.created) < :ptimeend
10171018
ORDER BY p.modified ASC";
10181019

10191020
return $DB->get_records_sql($sql, $params);
@@ -1039,12 +1040,10 @@ function moodleoverflow_mark_old_posts_as_mailed($endtime) {
10391040
$params['endtime'] = $endtime;
10401041
$params['mailedpending'] = MOODLEOVERFLOW_MAILED_PENDING;
10411042

1042-
// TODO do something about this. Add field timereviewed to post? / Make reviewed contain either null or timestamp?
1043-
10441043
// Define the sql query.
10451044
$sql = "UPDATE {moodleoverflow_posts}
10461045
SET mailed = :mailedsuccess
1047-
WHERE (created < :endtime) AND mailed = :mailedpending";
1046+
WHERE (created < :endtime) AND mailed = :mailedpending AND reviewed = 1";
10481047

10491048
return $DB->execute($sql, $params);
10501049

@@ -1096,7 +1095,7 @@ function moodleoverflow_cm_info_view(cm_info $cm) {
10961095
* Check if the user can create attachments in moodleoverflow.
10971096
*
10981097
* @param stdClass $moodleoverflow moodleoverflow object
1099-
* @param stdClass $context context object
1098+
* @param context_module $context context object
11001099
*
11011100
* @return bool true if the user can create attachments, false otherwise
11021101
* @since Moodle 3.3

locallib.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ function moodleoverflow_user_can_see_discussion($moodleoverflow, $discussion, $c
696696
* Creates a new moodleoverflow discussion.
697697
*
698698
* @param stdClass $discussion The discussion object
699-
* @param object $modulecontext
699+
* @param context_module $modulecontext
700700
* @param int $userid The user ID
701701
*
702702
* @return bool|int The id of the created discussion
@@ -737,7 +737,9 @@ function moodleoverflow_add_discussion($discussion, $modulecontext, $userid = nu
737737
$post->moodleoverflow = $moodleoverflow->id;
738738
$post->course = $moodleoverflow->course;
739739

740-
if (review::get_review_level($moodleoverflow) > review::QUESTIONS) {
740+
// Set to not reviewed, if questions should be reviewed, and user is not a reviewer themselves.
741+
if (review::get_review_level($moodleoverflow) > review::QUESTIONS &&
742+
!has_capability('mod/moodleoverflow:reviewpost', $modulecontext, $userid)) {
741743
$post->reviewed = 0;
742744
}
743745

@@ -1562,10 +1564,11 @@ function moodleoverflow_add_attachment($post, $forum, $cm) {
15621564
* Adds a new post in an existing discussion.
15631565
*
15641566
* @param object $post The post object
1567+
* @param context_module $modulecontext
15651568
*
15661569
* @return bool|int The Id of the post if operation was successful
15671570
*/
1568-
function moodleoverflow_add_new_post($post) {
1571+
function moodleoverflow_add_new_post($post, $modulecontext) {
15691572
global $USER, $DB;
15701573

15711574
// We do not check if these variables exist because this function
@@ -1581,18 +1584,24 @@ function moodleoverflow_add_new_post($post) {
15811584
$post->totalscore = 0;
15821585
}
15831586

1584-
if (review::get_review_level($moodleoverflow) == review::EVERYTHING) {
1587+
// Set to not reviewed, if posts should be reviewed, and user is not a reviewer themselves.
1588+
if (review::get_review_level($moodleoverflow) == review::EVERYTHING &&
1589+
!has_capability('mod/moodleoverflow:reviewpost', $modulecontext)) {
15851590
$post->reviewed = 0;
1591+
} else {
1592+
$post->reviewed = 1;
15861593
}
15871594

15881595
// Add the post to the database.
15891596
$post->id = $DB->insert_record('moodleoverflow_posts', $post);
15901597
$DB->set_field('moodleoverflow_posts', 'message', $post->message, array('id' => $post->id)); // ??
15911598
moodleoverflow_add_attachment($post, $moodleoverflow, $cm);
15921599

1593-
// Update the discussion.
1594-
$DB->set_field('moodleoverflow_discussions', 'timemodified', $post->modified, array('id' => $post->discussion));
1595-
$DB->set_field('moodleoverflow_discussions', 'usermodified', $post->userid, array('id' => $post->discussion));
1600+
if ($post->reviewed) {
1601+
// Update the discussion.
1602+
$DB->set_field('moodleoverflow_discussions', 'timemodified', $post->modified, array('id' => $post->discussion));
1603+
$DB->set_field('moodleoverflow_discussions', 'usermodified', $post->userid, array('id' => $post->discussion));
1604+
}
15961605

15971606
// Mark the created post as read if the user is tracking the discussion.
15981607
$cantrack = \mod_moodleoverflow\readtracking::moodleoverflow_can_track_moodleoverflows($moodleoverflow);
@@ -1635,7 +1644,7 @@ function moodleoverflow_update_post($newpost) {
16351644
}
16361645
}
16371646

1638-
if (isset($newpost->reviewed) ? $newpost->reviewed : $post->reviewed) {
1647+
if ($newpost->reviewed ?? $post->reviewed) {
16391648
// Update the date and the user of the post and the discussion.
16401649
$post->modified = time();
16411650
$discussion->timemodified = $post->modified;

post.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@
659659
$addpost->moodleoverflow = $moodleoverflow->id;
660660

661661
// Create the new post.
662-
if ($fromform->id = moodleoverflow_add_new_post($addpost)) {
662+
if ($fromform->id = moodleoverflow_add_new_post($addpost, $modulecontext)) {
663663

664664
// Subscribe to this thread.
665665
$discussion = new \stdClass();

version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
defined('MOODLE_INTERNAL') || die();
2929

3030
$plugin->component = 'mod_moodleoverflow';
31-
$plugin->version = 2022071900;
31+
$plugin->version = 2022072000;
3232
$plugin->release = 'v4.0-r2';
3333
$plugin->requires = 2020061500; // Requires Moodle 3.9+.
3434
$plugin->maturity = MATURITY_STABLE;

0 commit comments

Comments
 (0)