@@ -78,32 +78,34 @@ public static function get_short_review_info_for_discussion(int $discussionid) {
7878 }
7979
8080 /**
81- * @param $afterpostid
81+ * @param int $moodleoverflowid ID of moodleoverflow to look in.
82+ * @param int $afterpostid ID of post after which to look for the first post to review.
8283 * @return string|null
8384 */
84- public static function get_first_review_post ($ afterpostid = null ) {
85+ public static function get_first_review_post ($ moodleoverflowid , $ afterpostid = null ) {
8586 global $ DB ;
8687
87- $ params = [];
88+ $ params = [' moodleoverflowid ' => $ moodleoverflowid ];
8889 $ orderby = '' ;
8990 $ addwhere = '' ;
9091
9192 if ($ afterpostid ) {
9293 $ afterdiscussionid = $ DB ->get_field ('moodleoverflow_posts ' , 'discussion ' , ['id ' => $ afterpostid ],
9394 MUST_EXIST );
9495
95- $ orderby = 'CASE WHEN (discussion > :afterdiscussionid OR (discussion = :afterdiscussionid2 AND id > :afterpostid)) THEN 0 ELSE 1 END, ' ;
96+ $ orderby = 'CASE WHEN (p. discussion > :afterdiscussionid OR (p. discussion = :afterdiscussionid2 AND p. id > :afterpostid)) THEN 0 ELSE 1 END, ' ;
9697 $ params ['afterdiscussionid ' ] = $ afterdiscussionid ;
9798 $ params ['afterdiscussionid2 ' ] = $ afterdiscussionid ;
9899 $ params ['afterpostid ' ] = $ afterpostid ;
99100
100- $ addwhere = ' AND id != :notpostid ' ;
101+ $ addwhere = ' AND p. id != :notpostid ' ;
101102 $ params ['notpostid ' ] = $ afterpostid ;
102103 }
103104 $ record = $ DB ->get_record_sql (
104- 'SELECT id as postid, discussion as discussionid FROM {moodleoverflow_posts} ' .
105- "WHERE reviewed = 0 $ addwhere " .
106- "ORDER BY $ orderby discussion, id " .
105+ 'SELECT p.id as postid, p.discussion as discussionid FROM {moodleoverflow_posts} p ' .
106+ 'JOIN {moodleoverflow_discussions} d ON d.id = p.discussion ' .
107+ "WHERE p.reviewed = 0 AND d.moodleoverflow = :moodleoverflowid $ addwhere " .
108+ "ORDER BY $ orderby p.discussion, p.id " .
107109 'LIMIT 1 ' ,
108110 $ params
109111 );
0 commit comments