43
43
function moodleoverflow_get_discussions ($ cm , $ page = -1 , $ perpage = 0 ) {
44
44
global $ DB , $ CFG ;
45
45
46
- $ params = array ($ cm ->instance );
47
-
48
46
// User must have the permission to view the discussions.
49
47
$ modcontext = context_module::instance ($ cm ->id );
50
48
if (!has_capability ('mod/moodleoverflow:viewdiscussion ' , $ modcontext )) {
@@ -69,7 +67,7 @@ function moodleoverflow_get_discussions($cm, $page = -1, $perpage = 0) {
69
67
} else {
70
68
$ allnames = get_all_user_name_fields (true , 'u ' );
71
69
}
72
- $ postdata = 'p.id, p.modified, p.discussion, p.userid ' ;
70
+ $ postdata = 'p.id, p.modified, p.discussion, p.userid, p.reviewed ' ;
73
71
$ discussiondata = 'd.name, d.timemodified, d.timestart, d.usermodified ' ;
74
72
$ userdata = 'u.email, u.picture, u.imagealt ' ;
75
73
@@ -81,15 +79,23 @@ function moodleoverflow_get_discussions($cm, $page = -1, $perpage = 0) {
81
79
$ usermodifiedfields = get_all_user_name_fields (true , 'um ' , null , 'um ' ) .
82
80
', um.email AS umemail, um.picture AS umpicture, um.imagealt AS umimagealt ' ;
83
81
}
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 );
85
91
86
92
// Retrieve and return all discussions from the database.
87
93
$ sql = "SELECT $ postdata, $ discussiondata, $ allnames, $ userdata, $ usermodifiedfields
88
94
FROM {moodleoverflow_discussions} d
89
95
JOIN {moodleoverflow_posts} p ON p.discussion = d.id
90
96
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
93
99
ORDER BY d.timestart DESC, d.id DESC " ;
94
100
95
101
return $ DB ->get_records_sql ($ sql , $ params , $ limitfrom , $ limitamount );
@@ -426,12 +432,21 @@ function moodleoverflow_user_can_post_discussion($moodleoverflow, $cm = null, $c
426
432
function moodleoverflow_get_discussions_count ($ cm ) {
427
433
global $ DB ;
428
434
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 );
430
445
431
446
$ sql = 'SELECT COUNT(d.id)
432
447
FROM {moodleoverflow_discussions} d
433
448
JOIN {moodleoverflow_posts} p ON p.discussion = d.id
434
- WHERE d.moodleoverflow = ? AND p.parent = 0 ' ;
449
+ WHERE ' . $ wheresql ;
435
450
436
451
return $ DB ->get_field_sql ($ sql , $ params );
437
452
}
@@ -447,29 +462,39 @@ function moodleoverflow_get_discussions_unread($cm) {
447
462
global $ DB , $ USER ;
448
463
449
464
// Get the current timestamp and the oldpost-timestamp.
450
- $ params = array ();
451
465
$ now = round (time (), -2 );
452
466
$ cutoffdate = $ now - (get_config ('moodleoverflow ' , 'oldpostdays ' ) * 24 * 60 * 60 );
453
467
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
+
454
483
// Define the sql-query.
455
484
$ 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 " ;
465
490
466
491
// Return the unread messages as an array.
467
492
if ($ unreads = $ DB ->get_records_sql ($ sql , $ params )) {
493
+ $ returnarray = [];
468
494
foreach ($ unreads as $ unread ) {
469
- $ unreads [$ unread ->id ] = $ unread ->unread ;
495
+ $ returnarray [$ unread ->id ] = $ unread ->unread ;
470
496
}
471
-
472
- return $ unreads ;
497
+ return $ returnarray ;
473
498
} else {
474
499
475
500
// If there are no unread messages, return an empty array.
@@ -516,11 +541,10 @@ function moodleoverflow_get_post_full($postid) {
516
541
* @param object $discussion
517
542
* @param object $post
518
543
* @param object $cm
519
- * @param null $user
520
544
*
521
545
* @return bool
522
546
*/
523
- function moodleoverflow_user_can_see_post ($ moodleoverflow , $ discussion , $ post , $ cm, $ user = null ) {
547
+ function moodleoverflow_user_can_see_post ($ moodleoverflow , $ discussion , $ post , $ cm ) {
524
548
global $ USER , $ DB ;
525
549
526
550
// Retrieve the modulecontext.
@@ -570,14 +594,18 @@ function moodleoverflow_user_can_see_post($moodleoverflow, $discussion, $post, $
570
594
571
595
// Check if the user can view the discussion.
572
596
$ 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 );
574
598
if (!$ canviewdiscussion &&
575
599
!has_all_capabilities (array ('moodle/user:viewdetails ' , 'moodle/user:readuserposts ' ),
576
600
context_user::instance ($ post ->userid ))
577
601
) {
578
602
return false ;
579
603
}
580
604
605
+ if ($ post ->reviewed == 0 && !has_capability ('mod/moodleoverflow:reviewpost ' , $ modulecontext , $ user )) {
606
+ return false ;
607
+ }
608
+
581
609
// Check the coursemodule settings.
582
610
if (isset ($ cm ->uservisible )) {
583
611
if (!$ cm ->uservisible ) {
@@ -681,6 +709,8 @@ function moodleoverflow_add_discussion($discussion, $modulecontext, $userid = nu
681
709
$ post ->moodleoverflow = $ moodleoverflow ->id ;
682
710
$ post ->course = $ moodleoverflow ->course ;
683
711
712
+ // TODO set reviewed = 0 if moodleoverflow is reviewable
713
+
684
714
// Submit the post to the database and get its id.
685
715
$ post ->id = $ DB ->insert_record ('moodleoverflow_posts ' , $ post );
686
716
@@ -823,7 +853,7 @@ function moodleoverflow_print_discussion($course, $cm, $moodleoverflow, $discuss
823
853
$ istracked = \mod_moodleoverflow \readtracking::moodleoverflow_is_tracked ($ moodleoverflow );
824
854
825
855
// 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 );
827
857
828
858
$ usermapping = anonymous::get_userid_mapping ($ moodleoverflow , $ discussion ->id );
829
859
@@ -883,10 +913,11 @@ function moodleoverflow_print_discussion($course, $cm, $moodleoverflow, $discuss
883
913
*
884
914
* @param int $discussionid The ID of the discussion
885
915
* @param boolean $tracking Whether tracking is activated
916
+ * @param context_module $modcontext Context of the module
886
917
*
887
918
* @return array
888
919
*/
889
- function moodleoverflow_get_all_discussion_posts ($ discussionid , $ tracking ) {
920
+ function moodleoverflow_get_all_discussion_posts ($ discussionid , $ tracking, $ modcontext ) {
890
921
global $ DB , $ USER , $ CFG ;
891
922
892
923
// Initiate tracking settings.
@@ -909,14 +940,20 @@ function moodleoverflow_get_all_discussion_posts($discussionid, $tracking) {
909
940
$ allnames = get_all_user_name_fields (true , 'u ' );
910
941
}
911
942
943
+ $ additionalwhere = '' ;
944
+
945
+ if (!has_capability ('mod/moodleoverflow:reviewpost ' , $ modcontext )) {
946
+ $ additionalwhere = ' AND p.reviewed = 1 ' ;
947
+ }
948
+
912
949
// Create the sql array.
913
950
$ sql = "SELECT p.*, m.ratingpreference, $ allnames, d.name as subject, u.email, u.picture, u.imagealt $ trackingselector
914
951
FROM {moodleoverflow_posts} p
915
952
LEFT JOIN {user} u ON p.userid = u.id
916
953
LEFT JOIN {moodleoverflow_discussions} d ON d.id = p.discussion
917
954
LEFT JOIN {moodleoverflow} m on m.id = d.moodleoverflow
918
955
$ trackingjoin
919
- WHERE p.discussion = :discussion
956
+ WHERE p.discussion = :discussion $ additionalwhere
920
957
ORDER BY p.created ASC " ;
921
958
$ params ['discussion ' ] = $ discussionid ;
922
959
@@ -1535,6 +1572,8 @@ function moodleoverflow_add_new_post($post) {
1535
1572
$ post ->totalscore = 0 ;
1536
1573
}
1537
1574
1575
+ // TODO add reiview = 0 if enabled on moodleoverflow module.
1576
+
1538
1577
// Add the post to the database.
1539
1578
$ post ->id = $ DB ->insert_record ('moodleoverflow_posts ' , $ post );
1540
1579
$ DB ->set_field ('moodleoverflow_posts ' , 'message ' , $ post ->message , array ('id ' => $ post ->id ));
@@ -1616,35 +1655,22 @@ function moodleoverflow_update_post($newpost) {
1616
1655
/**
1617
1656
* Count all replies of a post.
1618
1657
*
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.
1621
1660
*
1622
1661
* @return int Amount of replies
1623
1662
*/
1624
- function moodleoverflow_count_replies ($ post , $ recursive = null ) {
1663
+ function moodleoverflow_count_replies ($ post , $ onlyreviewed ) {
1625
1664
global $ DB ;
1626
1665
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 ];
1634
1667
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 ' ;
1644
1670
}
1645
1671
1646
1672
// Return the amount of replies.
1647
- return $ count ;
1673
+ return $ DB -> count_records ( ' moodleoverflow_posts ' , $ conditions ) ;
1648
1674
}
1649
1675
1650
1676
/**
@@ -1773,10 +1799,11 @@ function moodleoverflow_discussion_update_last_post($discussionid) {
1773
1799
return false ;
1774
1800
}
1775
1801
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)
1777
1803
$ sql = "SELECT id, userid, modified
1778
1804
FROM {moodleoverflow_posts}
1779
1805
WHERE discussion = ?
1806
+ AND reviewed = 1
1780
1807
ORDER BY modified DESC " ;
1781
1808
1782
1809
// Find the new last post of the discussion.
0 commit comments