Skip to content

Commit 5298000

Browse files
Implement initial delay for reviews to allow posters time for editing
1 parent b2eb641 commit 5298000

File tree

8 files changed

+94
-19
lines changed

8 files changed

+94
-19
lines changed

classes/review.php

+24
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,29 @@ public static function get_first_review_post($afterpostid = null) {
114114
}
115115
}
116116

117+
/**
118+
* Return if the post does need/needed a review with the current moodleoverflow settings.
119+
* @param object $post
120+
* @param object $moodleoverflow
121+
* @return bool
122+
*/
123+
public static function should_post_be_reviewed($post, $moodleoverflow): bool {
124+
$reviewlevel = self::get_review_level($moodleoverflow);
125+
if ($post->parent) {
126+
return $reviewlevel == self::EVERYTHING;
127+
} else {
128+
return $reviewlevel >= self::QUESTIONS;
129+
}
130+
}
131+
132+
/**
133+
* Returns whether a post is reviewable depending on its review state and review period.
134+
* @param object $post
135+
* @return bool
136+
*/
137+
public static function is_post_in_review_period($post): bool {
138+
return time() - $post->created > get_config('moodleoverflow', 'reviewpossibleaftertime');
139+
}
140+
117141

118142
}

externallib.php

+29-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2323
*/
2424

25+
use mod_moodleoverflow\review;
26+
2527
defined('MOODLE_INTERNAL') || die;
2628

2729
require_once("$CFG->libdir/externallib.php");
@@ -176,23 +178,34 @@ public static function review_approve_post($postid) {
176178
$postid = $params['postid'];
177179

178180
$post = $DB->get_record('moodleoverflow_posts', ['id' => $postid], '*', MUST_EXIST);
179-
$moodleoverflow = $DB->get_record_sql(
180-
'SELECT m.* FROM {moodleoverflow} m ' .
181-
'JOIN {moodleoverflow_discussions} d ON d.moodleoverflow = m.id ' .
182-
'WHERE d.id = :discussionid',
183-
['discussionid' => $post->discussion], MUST_EXIST
184-
);
181+
$discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $post->discussion], '*', MUST_EXIST);
182+
$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $discussion->moodleoverflow], '*', MUST_EXIST);
183+
185184
$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id);
186185
$context = context_module::instance($cm->id);
187186

188187
require_capability('mod/moodleoverflow:reviewpost', $context);
189188

189+
if ($post->reviewed) {
190+
throw new coding_exception('post was already approved!');
191+
}
192+
193+
if (!review::is_post_in_review_period($post)) {
194+
throw new coding_exception('post is not yet in review period!');
195+
}
196+
190197
$post->reviewed = 1;
191198
$post->timereviewed = time();
192199

193200
$DB->update_record('moodleoverflow_posts', $post);
194201

195-
return \mod_moodleoverflow\review::get_first_review_post($post->id);
202+
if ($post->modified > $discussion->timemodified) {
203+
$discussion->timemodified = $post->modified;
204+
$discussion->usermodified = $post->userid;
205+
$DB->update_record('moodleoverflow_discussions', $discussion);
206+
}
207+
208+
return review::get_first_review_post($post->id);
196209
}
197210

198211
/**
@@ -239,8 +252,16 @@ public static function review_reject_post($postid, $reason = null) {
239252

240253
require_capability('mod/moodleoverflow:reviewpost', $context);
241254

255+
if ($post->reviewed) {
256+
throw new coding_exception('post was already approved!');
257+
}
258+
259+
if (!review::is_post_in_review_period($post)) {
260+
throw new coding_exception('post is not yet in review period!');
261+
}
262+
242263
// Has to be done before deleting the post.
243-
$url = \mod_moodleoverflow\review::get_first_review_post($post->id);
264+
$url = review::get_first_review_post($post->id);
244265

245266
moodleoverflow_delete_post($post, true, $cm, $moodleoverflow);
246267

lang/en/moodleoverflow.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
$string['maxattachments_help'] = 'This setting specifies the maximum number of files that can be attached to a forum post.';
9393
$string['configmaxattachments'] = 'Default maximum number of attachments allowed per post.';
9494
$string['maxeditingtime'] = 'Maximum amount of time during which a post can be edited by its owner (sec)';
95-
$string['configmaxeditingtime'] = 'Default maximum seconds are 3600 (= one hour).';
95+
$string['configmaxeditingtime'] = 'Default maximum seconds are 3600 (= one hour). Regarding editing posts, please also consider the <a href="#admin-reviewpossibleaftertime">"Review possible after" setting</a> for moderated forums.';
9696
$string['configoldpostdays'] = 'Number of days old any post is considered read.';
9797
$string['oldpostdays'] = 'Read after days';
9898
$string['trackingoff'] = 'Off';
@@ -425,7 +425,7 @@
425425
$string['nothing'] = 'Nothing';
426426
$string['questions'] = 'Questions';
427427
$string['questions_and_posts'] = 'Questions and answers';
428-
$string['allowreview'] = 'Allow reviews';
428+
$string['allowreview'] = 'Allow moderated forums';
429429
$string['allowreview_desc'] = 'Allow teachers to enable that all posts (or only all questions) have to be reviewed by them in order to be published.';
430430
$string['amount_waiting_for_review'] = '{$a} post(s) need to be reviewed!';
431431
$string['pending_review'] = 'Pending review';
@@ -436,6 +436,9 @@
436436
$string['give_a_reason'] = 'Give a reason (optional)';
437437
$string['approve'] = 'Approve';
438438
$string['reject'] = 'Reject';
439+
$string['reviewpossibleaftertime'] = 'Review possible after (secs)';
440+
$string['reviewpossibleaftertime_desc'] = 'A teacher can only reject or approve a post at least this amount of time (in seconds) after the creation of the post. After a teacher has approved a post, the post cannot be edited by it\'s author anymore, even if still within the maxeditingtime duration.';
441+
$string['pending_review_but_cannot_now'] = 'Pending review, but can only be approved at least {$a} after the creation of this post to allow the author a bit of time to edit it.';
439442

440443
$string['rejected_email_subject'] = 'One of your posts in {$a->course} {$a->activity} has been rejected.';
441444
$string['rejected_email_content'] = 'This post has been rejected in {$a->course} {$a->activity}';

locallib.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -1191,8 +1191,9 @@ function moodleoverflow_print_post($post, $discussion, $moodleoverflow, $cm, $co
11911191
// Calculate the age of the post.
11921192
$age = time() - $post->created;
11931193

1194-
// Make a link to edit your own post within the given time.
1195-
if (($ownpost AND ($age < get_config('moodleoverflow', 'maxeditingtime')))
1194+
// Make a link to edit your own post within the given time and not already reviewed.
1195+
if (($ownpost AND ($age < get_config('moodleoverflow', 'maxeditingtime')) &&
1196+
(!review::should_post_be_reviewed($post, $moodleoverflow) || !$post->reviewed))
11961197
OR $cm->cache->caps['mod/moodleoverflow:editanypost']
11971198
) {
11981199
$editurl = new moodle_url('/mod/moodleoverflow/post.php', array('edit' => $post->id));
@@ -1363,8 +1364,12 @@ function moodleoverflow_print_post($post, $discussion, $moodleoverflow, $cm, $co
13631364
$options->trusted = false;
13641365
$options->context = $modulecontext;
13651366

1367+
$reviewdelay = get_config('moodleoverflow', 'reviewpossibleaftertime');
1368+
$mustachedata->reviewdelay = format_time($reviewdelay);
13661369
$mustachedata->needsreview = !$post->reviewed;
1367-
$mustachedata->canreview = $cm->cache->caps['mod/moodleoverflow:reviewpost'];
1370+
$reviewable = time() - $post->created > $reviewdelay;
1371+
$mustachedata->canreview = $cm->cache->caps['mod/moodleoverflow:reviewpost'] && $reviewable;
1372+
$mustachedata->withinreviewperiod = $reviewable;
13681373

13691374
// Prepare the post.
13701375
$mustachedata->postcontent = format_text($post->message, $post->messageformat, $options, $course->id);
@@ -1644,9 +1649,9 @@ function moodleoverflow_update_post($newpost) {
16441649
}
16451650
}
16461651

1652+
$post->modified = time();
16471653
if ($newpost->reviewed ?? $post->reviewed) {
16481654
// Update the date and the user of the post and the discussion.
1649-
$post->modified = time();
16501655
$discussion->timemodified = $post->modified;
16511656
$discussion->usermodified = $post->userid;
16521657
}

post.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
// TODO refactor this. For more readability, and to avoid security issues.
2626

2727
// Include config and locallib.
28+
use mod_moodleoverflow\review;
29+
2830
require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
2931
global $CFG, $USER, $DB, $PAGE, $SESSION, $OUTPUT;
3032
require_once(dirname(__FILE__) . '/locallib.php');
@@ -298,12 +300,15 @@
298300
$PAGE->set_cm($cm, $course, $moodleoverflow);
299301

300302
// Check if the post can be edited.
301-
$intime = ((time() - $post->created) > get_config('moodleoverflow', 'maxeditingtime'));
302-
if ($intime AND !has_capability('mod/moodleoverflow:editanypost', $modulecontext)) {
303+
$beyondtime = ((time() - $post->created) > get_config('moodleoverflow', 'maxeditingtime'));
304+
$alreadyreviewed = review::should_post_be_reviewed($post, $moodleoverflow) && $post->reviewed;
305+
if (($beyondtime || $alreadyreviewed) AND !has_capability('mod/moodleoverflow:editanypost', $modulecontext)) {
303306
throw new moodle_exception('maxtimehaspassed', 'moodleoverflow', '',
304307
format_time(get_config('moodleoverflow', 'maxeditingtime')));
305308
}
306309

310+
311+
307312
// If the current user is not the one who posted this post.
308313
if ($post->userid <> $USER->id) {
309314

@@ -603,7 +608,7 @@
603608
$replypost = has_capability('mod/moodleoverflow:replypost', $modulecontext);
604609
$startdiscussion = has_capability('mod/moodleoverflow:startdiscussion', $modulecontext);
605610
$ownpost = ($realpost->userid == $USER->id);
606-
if (!((($ownpost AND $replypost OR $startdiscussion)) OR $editanypost)) {
611+
if (!(($ownpost AND ($replypost OR $startdiscussion)) OR $editanypost)) {
607612
throw new moodle_exception('cannotupdatepost', 'moodleoverflow');
608613
}
609614

settings.php

+3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@
9797
$settings->add(new admin_setting_configcheckbox('moodleoverflow/allowreview',
9898
get_string('allowreview', 'moodleoverflow'), get_string('allowreview_desc', 'moodleoverflow'), 1));
9999

100+
$settings->add(new admin_setting_configtext('moodleoverflow/reviewpossibleaftertime', get_string('reviewpossibleaftertime', 'moodleoverflow'),
101+
get_string('reviewpossibleaftertime_desc', 'moodleoverflow'), 3600, PARAM_INT));
102+
100103
// Set scales for the reputation.
101104
$votesettings = [];
102105

templates/answer.mustache

+8-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,14 @@
4747
{{/showvotes}}
4848
{{#needsreview}}
4949
<div class="p-2 onlyifnotreviewed">
50-
{{#pix}}i/pending-big, mod_moodleoverflow, {{#str}}pending_review, mod_moodleoverflow{{/str}}{{/pix}}
50+
{{#pix}}i/pending-big, mod_moodleoverflow,
51+
{{#withinreviewperiod}}
52+
{{#str}}pending_review, mod_moodleoverflow{{/str}}
53+
{{/withinreviewperiod}}
54+
{{^withinreviewperiod}}
55+
{{#str}}pending_review_but_cannot_now, mod_moodleoverflow, {{reviewdelay}} {{/str}}
56+
{{/withinreviewperiod}}
57+
{{/pix}}
5158
</div>
5259
{{/needsreview}}
5360
<div class="answercell">

templates/comment.mustache

+8-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,14 @@
4949
{{/needsreview}}
5050
{{#needsreview}}
5151
<div class="p-2">
52-
{{#pix}}i/pending-big, mod_moodleoverflow, {{#str}}pending_review, mod_moodleoverflow{{/str}}{{/pix}}
52+
{{#pix}}i/pending-big, mod_moodleoverflow,
53+
{{#withinreviewperiod}}
54+
{{#str}}pending_review, mod_moodleoverflow{{/str}}
55+
{{/withinreviewperiod}}
56+
{{^withinreviewperiod}}
57+
{{#str}}pending_review_but_cannot_now, mod_moodleoverflow, {{reviewdelay}} {{/str}}
58+
{{/withinreviewperiod}}
59+
{{/pix}}
5360
</div>
5461
{{/needsreview}}
5562
<div class="commentcell">

0 commit comments

Comments
 (0)