Skip to content

Commit 92b5721

Browse files
Create groundwork for reviewable posts
1 parent dcb224f commit 92b5721

File tree

9 files changed

+124
-55
lines changed

9 files changed

+124
-55
lines changed

classes/readtracking.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
namespace mod_moodleoverflow;
2626

27+
use context_module;
2728
use moodle_exception;
2829

2930
/**
@@ -136,7 +137,7 @@ public static function moodleoverflow_mark_moodleoverflow_read($cm, $userid = nu
136137
foreach ($discussions as $discussionid => $amount) {
137138

138139
// Mark the discussion as read.
139-
if (!self::moodleoverflow_mark_discussion_read($discussionid, $userid)) {
140+
if (!self::moodleoverflow_mark_discussion_read($discussionid, context_module::instance($cm->id), $userid)) {
140141
throw new moodle_exception('markreadfailed', 'moodleoverflow');
141142

142143
return false;
@@ -150,13 +151,14 @@ public static function moodleoverflow_mark_moodleoverflow_read($cm, $userid = nu
150151
* Marks a specific discussion as read by a specific user.
151152
*
152153
* @param int $discussionid
154+
* @param context_module $modcontext
153155
* @param null $userid
154156
*/
155-
public static function moodleoverflow_mark_discussion_read($discussionid, $userid = null) {
157+
public static function moodleoverflow_mark_discussion_read($discussionid, $modcontext, $userid = null) {
156158
global $USER;
157159

158160
// Get all posts.
159-
$posts = moodleoverflow_get_all_discussion_posts($discussionid, true);
161+
$posts = moodleoverflow_get_all_discussion_posts($discussionid, true, $modcontext);
160162

161163
// If no user is submitted, use the current one.
162164
if (!isset($userid)) {

db/access.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,15 @@
234234
'clonepermissionsfrom' => 'mod/forum:createattachment'
235235
),
236236

237+
'mod/moodleoverflow:reviewpost' => array(
238+
'captype' => 'write',
239+
'contextlevel' => CONTEXT_MODULE,
240+
'archetypes' => array(
241+
'teacher' => CAP_ALLOW,
242+
'editingteacher' => CAP_ALLOW,
243+
'manager' => CAP_ALLOW
244+
),
245+
'clonepermissionsfrom' => 'moodle/course:manageactivities'
246+
),
247+
237248
);

db/install.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<FIELD NAME="gradescalefactor" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
2727
<FIELD NAME="gradecat" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
2828
<FIELD NAME="anonymous" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
29+
<FIELD NAME="needsreview" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
2930
</FIELDS>
3031
<KEYS>
3132
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
@@ -67,6 +68,7 @@
6768
<FIELD NAME="messageformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
6869
<FIELD NAME="attachment" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false"/>
6970
<FIELD NAME="mailed" TYPE="int" LENGTH="1" NOTNULL="false" DEFAULT="0" SEQUENCE="false"/>
71+
<FIELD NAME="reviewed" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" SEQUENCE="false"/>
7072
</FIELDS>
7173
<KEYS>
7274
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>

db/upgrade.php

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

198+
if ($oldversion < 2022070600) {
199+
200+
// Define field needsreview to be added to moodleoverflow.
201+
$table = new xmldb_table('moodleoverflow');
202+
$field = new xmldb_field('needsreview', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'anonymous');
203+
204+
// Conditionally launch add field needsreview.
205+
if (!$dbman->field_exists($table, $field)) {
206+
$dbman->add_field($table, $field);
207+
}
208+
209+
// Define field reviewed to be added to moodleoverflow_posts.
210+
$table = new xmldb_table('moodleoverflow_posts');
211+
$field = new xmldb_field('reviewed', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'mailed');
212+
213+
// Conditionally launch add field reviewed.
214+
if (!$dbman->field_exists($table, $field)) {
215+
$dbman->add_field($table, $field);
216+
}
217+
218+
// Moodleoverflow savepoint reached.
219+
upgrade_mod_savepoint(true, 2022070600, 'moodleoverflow');
220+
}
221+
198222
return true;
199223
}

lib.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,14 +1006,15 @@ function moodleoverflow_get_unmailed_posts($starttime, $endtime) {
10061006
// Set params for the sql query.
10071007
$params = array();
10081008
$params['mailed'] = MOODLEOVERFLOW_MAILED_PENDING;
1009+
$params['reviewed'] = 1;
10091010
$params['ptimestart'] = $starttime;
10101011
$params['ptimeend'] = $endtime;
10111012

10121013
// Retrieve the records.
10131014
$sql = "SELECT p.*, d.course, d.moodleoverflow
10141015
FROM {moodleoverflow_posts} p
10151016
JOIN {moodleoverflow_discussions} d ON d.id = p.discussion
1016-
WHERE p.mailed = :mailed AND p.created >= :ptimestart AND p.created < :ptimeend
1017+
WHERE p.mailed = :mailed AND p.created >= :ptimestart AND p.created < :ptimeend AND p.reviewed = :reviewed
10171018
ORDER BY p.modified ASC";
10181019

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

1043+
// TODO do something about this. Add field timereviewed to post? / Make reviewed contain either null or timestamp?
1044+
10421045
// Define the sql query.
10431046
$sql = "UPDATE {moodleoverflow_posts}
10441047
SET mailed = :mailedsuccess

locallib.php

Lines changed: 75 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343
function moodleoverflow_get_discussions($cm, $page = -1, $perpage = 0) {
4444
global $DB, $CFG;
4545

46-
$params = array($cm->instance);
47-
4846
// User must have the permission to view the discussions.
4947
$modcontext = context_module::instance($cm->id);
5048
if (!has_capability('mod/moodleoverflow:viewdiscussion', $modcontext)) {
@@ -69,7 +67,7 @@ function moodleoverflow_get_discussions($cm, $page = -1, $perpage = 0) {
6967
} else {
7068
$allnames = get_all_user_name_fields(true, 'u');
7169
}
72-
$postdata = 'p.id, p.modified, p.discussion, p.userid';
70+
$postdata = 'p.id, p.modified, p.discussion, p.userid, p.reviewed';
7371
$discussiondata = 'd.name, d.timemodified, d.timestart, d.usermodified';
7472
$userdata = 'u.email, u.picture, u.imagealt';
7573

@@ -81,15 +79,23 @@ function moodleoverflow_get_discussions($cm, $page = -1, $perpage = 0) {
8179
$usermodifiedfields = get_all_user_name_fields(true, 'um', null, 'um') .
8280
', um.email AS umemail, um.picture AS umpicture, um.imagealt AS umimagealt';
8381
}
84-
$usermodifiedtable = " LEFT JOIN {user} um ON (d.usermodified = um.id)";
82+
83+
$params = [$cm->instance];
84+
$whereconditions = ['d.moodleoverflow = ?', 'p.parent = 0'];
85+
86+
if (!has_capability('mod/moodleoverflow:reviewpost', $modcontext)) {
87+
$whereconditions[] = 'p.reviewed = 1';
88+
}
89+
90+
$wheresql = join(' AND ', $whereconditions);
8591

8692
// Retrieve and return all discussions from the database.
8793
$sql = "SELECT $postdata, $discussiondata, $allnames, $userdata, $usermodifiedfields
8894
FROM {moodleoverflow_discussions} d
8995
JOIN {moodleoverflow_posts} p ON p.discussion = d.id
9096
LEFT JOIN {user} u ON p.userid = u.id
91-
$usermodifiedtable
92-
WHERE d.moodleoverflow = ? AND p.parent = 0
97+
LEFT JOIN {user} um ON d.usermodified = um.id
98+
WHERE $wheresql
9399
ORDER BY d.timestart DESC, d.id DESC";
94100

95101
return $DB->get_records_sql($sql, $params, $limitfrom, $limitamount);
@@ -426,12 +432,21 @@ function moodleoverflow_user_can_post_discussion($moodleoverflow, $cm = null, $c
426432
function moodleoverflow_get_discussions_count($cm) {
427433
global $DB;
428434

429-
$params = array($cm->instance);
435+
$modcontext = context_module::instance($cm->id);
436+
437+
$params = [$cm->instance];
438+
$whereconditions = ['d.moodleoverflow = ?', 'p.parent = 0'];
439+
440+
if (!has_capability('mod/moodleoverflow:reviewpost', $modcontext)) {
441+
$whereconditions[] = 'p.reviewed = 1';
442+
}
443+
444+
$wheresql = join(' AND ', $whereconditions);
430445

431446
$sql = 'SELECT COUNT(d.id)
432447
FROM {moodleoverflow_discussions} d
433448
JOIN {moodleoverflow_posts} p ON p.discussion = d.id
434-
WHERE d.moodleoverflow = ? AND p.parent = 0';
449+
WHERE ' . $wheresql;
435450

436451
return $DB->get_field_sql($sql, $params);
437452
}
@@ -447,29 +462,39 @@ function moodleoverflow_get_discussions_unread($cm) {
447462
global $DB, $USER;
448463

449464
// Get the current timestamp and the oldpost-timestamp.
450-
$params = array();
451465
$now = round(time(), -2);
452466
$cutoffdate = $now - (get_config('moodleoverflow', 'oldpostdays') * 24 * 60 * 60);
453467

468+
$modcontext = context_module::instance($cm->id);
469+
470+
$whereconditions = ['d.moodleoverflow = :instance', 'p.modified >= :cutoffdate', 'r.id is NULL'];
471+
$params = [
472+
'userid' => $USER->id,
473+
'instance' => $cm->instance,
474+
'cutoffdate' => $cutoffdate
475+
];
476+
477+
if (!has_capability('mod/moodleoverflow:reviewpost', $modcontext)) {
478+
$whereconditions[] = 'p.reviewed = 1';
479+
}
480+
481+
$wheresql = join(' AND ', $whereconditions);
482+
454483
// Define the sql-query.
455484
$sql = "SELECT d.id, COUNT(p.id) AS unread
456-
FROM {moodleoverflow_discussions} d
457-
JOIN {moodleoverflow_posts} p ON p.discussion = d.id
458-
LEFT JOIN {moodleoverflow_read} r ON (r.postid = p.id AND r.userid = :userid)
459-
WHERE d.moodleoverflow = :instance
460-
AND p.modified >= :cutoffdate AND r.id is NULL
461-
GROUP BY d.id";
462-
$params['userid'] = $USER->id;
463-
$params['instance'] = $cm->instance;
464-
$params['cutoffdate'] = $cutoffdate;
485+
FROM {moodleoverflow_discussions} d
486+
JOIN {moodleoverflow_posts} p ON p.discussion = d.id
487+
LEFT JOIN {moodleoverflow_read} r ON (r.postid = p.id AND r.userid = :userid)
488+
WHERE $wheresql
489+
GROUP BY d.id";
465490

466491
// Return the unread messages as an array.
467492
if ($unreads = $DB->get_records_sql($sql, $params)) {
493+
$returnarray = [];
468494
foreach ($unreads as $unread) {
469-
$unreads[$unread->id] = $unread->unread;
495+
$returnarray[$unread->id] = $unread->unread;
470496
}
471-
472-
return $unreads;
497+
return $returnarray;
473498
} else {
474499

475500
// If there are no unread messages, return an empty array.
@@ -516,11 +541,10 @@ function moodleoverflow_get_post_full($postid) {
516541
* @param object $discussion
517542
* @param object $post
518543
* @param object $cm
519-
* @param null $user
520544
*
521545
* @return bool
522546
*/
523-
function moodleoverflow_user_can_see_post($moodleoverflow, $discussion, $post, $cm, $user = null) {
547+
function moodleoverflow_user_can_see_post($moodleoverflow, $discussion, $post, $cm) {
524548
global $USER, $DB;
525549

526550
// Retrieve the modulecontext.
@@ -570,14 +594,18 @@ function moodleoverflow_user_can_see_post($moodleoverflow, $discussion, $post, $
570594

571595
// Check if the user can view the discussion.
572596
$canviewdiscussion = !empty($cm->cache->caps['mod/moodleoverflow:viewdiscussion']) ||
573-
has_capability('mod/moodleoverflow:viewdiscussion', $modulecontext, $user->id);
597+
has_capability('mod/moodleoverflow:viewdiscussion', $modulecontext, $user);
574598
if (!$canviewdiscussion &&
575599
!has_all_capabilities(array('moodle/user:viewdetails', 'moodle/user:readuserposts'),
576600
context_user::instance($post->userid))
577601
) {
578602
return false;
579603
}
580604

605+
if ($post->reviewed == 0 && !has_capability('mod/moodleoverflow:reviewpost', $modulecontext, $user)) {
606+
return false;
607+
}
608+
581609
// Check the coursemodule settings.
582610
if (isset($cm->uservisible)) {
583611
if (!$cm->uservisible) {
@@ -681,6 +709,8 @@ function moodleoverflow_add_discussion($discussion, $modulecontext, $userid = nu
681709
$post->moodleoverflow = $moodleoverflow->id;
682710
$post->course = $moodleoverflow->course;
683711

712+
// TODO set reviewed = 0 if moodleoverflow is reviewable
713+
684714
// Submit the post to the database and get its id.
685715
$post->id = $DB->insert_record('moodleoverflow_posts', $post);
686716

@@ -823,7 +853,7 @@ function moodleoverflow_print_discussion($course, $cm, $moodleoverflow, $discuss
823853
$istracked = \mod_moodleoverflow\readtracking::moodleoverflow_is_tracked($moodleoverflow);
824854

825855
// Retrieve all posts of the discussion.
826-
$posts = moodleoverflow_get_all_discussion_posts($discussion->id, $istracked);
856+
$posts = moodleoverflow_get_all_discussion_posts($discussion->id, $istracked, $modulecontext);
827857

828858
$usermapping = anonymous::get_userid_mapping($moodleoverflow, $discussion->id);
829859

@@ -883,10 +913,11 @@ function moodleoverflow_print_discussion($course, $cm, $moodleoverflow, $discuss
883913
*
884914
* @param int $discussionid The ID of the discussion
885915
* @param boolean $tracking Whether tracking is activated
916+
* @param context_module $modcontext Context of the module
886917
*
887918
* @return array
888919
*/
889-
function moodleoverflow_get_all_discussion_posts($discussionid, $tracking) {
920+
function moodleoverflow_get_all_discussion_posts($discussionid, $tracking, $modcontext) {
890921
global $DB, $USER, $CFG;
891922

892923
// Initiate tracking settings.
@@ -909,14 +940,20 @@ function moodleoverflow_get_all_discussion_posts($discussionid, $tracking) {
909940
$allnames = get_all_user_name_fields(true, 'u');
910941
}
911942

943+
$additionalwhere = '';
944+
945+
if (!has_capability('mod/moodleoverflow:reviewpost', $modcontext)) {
946+
$additionalwhere = ' AND p.reviewed = 1 ';
947+
}
948+
912949
// Create the sql array.
913950
$sql = "SELECT p.*, m.ratingpreference, $allnames, d.name as subject, u.email, u.picture, u.imagealt $trackingselector
914951
FROM {moodleoverflow_posts} p
915952
LEFT JOIN {user} u ON p.userid = u.id
916953
LEFT JOIN {moodleoverflow_discussions} d ON d.id = p.discussion
917954
LEFT JOIN {moodleoverflow} m on m.id = d.moodleoverflow
918955
$trackingjoin
919-
WHERE p.discussion = :discussion
956+
WHERE p.discussion = :discussion $additionalwhere
920957
ORDER BY p.created ASC";
921958
$params['discussion'] = $discussionid;
922959

@@ -1535,6 +1572,8 @@ function moodleoverflow_add_new_post($post) {
15351572
$post->totalscore = 0;
15361573
}
15371574

1575+
// TODO add reiview = 0 if enabled on moodleoverflow module.
1576+
15381577
// Add the post to the database.
15391578
$post->id = $DB->insert_record('moodleoverflow_posts', $post);
15401579
$DB->set_field('moodleoverflow_posts', 'message', $post->message, array('id' => $post->id));
@@ -1616,35 +1655,22 @@ function moodleoverflow_update_post($newpost) {
16161655
/**
16171656
* Count all replies of a post.
16181657
*
1619-
* @param object $post The post object
1620-
* @param bool $recursive Whether the deletion should be recursive
1658+
* @param object $post The post object
1659+
* @param bool $onlyreviewed Whether to count only reviewed posts.
16211660
*
16221661
* @return int Amount of replies
16231662
*/
1624-
function moodleoverflow_count_replies($post, $recursive = null) {
1663+
function moodleoverflow_count_replies($post, $onlyreviewed) {
16251664
global $DB;
16261665

1627-
// Initiate the variable.
1628-
$count = 0;
1629-
1630-
// Count the posts recursively?
1631-
if (isset($recursive)) {
1632-
// Get all the direct children.
1633-
if ($childposts = $DB->get_records('moodleoverflow_posts', array('parent' => $post->id))) {
1666+
$conditions = ['parent' => $post->id];
16341667

1635-
// And count their children as well.
1636-
foreach ($childposts as $childpost) {
1637-
$count++;
1638-
$count += moodleoverflow_count_replies($childpost, true);
1639-
}
1640-
}
1641-
} else {
1642-
// Just count the direct children.
1643-
$count += $DB->count_records('moodleoverflow_posts', array('parent' => $post->id));
1668+
if ($onlyreviewed) {
1669+
$conditions['reviewed'] = '1';
16441670
}
16451671

16461672
// Return the amount of replies.
1647-
return $count;
1673+
return $DB->count_records('moodleoverflow_posts', $conditions);
16481674
}
16491675

16501676
/**
@@ -1773,10 +1799,11 @@ function moodleoverflow_discussion_update_last_post($discussionid) {
17731799
return false;
17741800
}
17751801

1776-
// Find the last post of the discussion.
1802+
// Find the last reviewed post of the discussion. (even if user has review capability, because it is written to DB)
17771803
$sql = "SELECT id, userid, modified
17781804
FROM {moodleoverflow_posts}
17791805
WHERE discussion = ?
1806+
AND reviewed = 1
17801807
ORDER BY modified DESC";
17811808

17821809
// Find the new last post of the discussion.

0 commit comments

Comments
 (0)