Skip to content

Commit

Permalink
MDL-52000 mod_forum: Respect timed posts in course overview block
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Jan 7, 2016
1 parent e65dfd9 commit 6667ebb
Show file tree
Hide file tree
Showing 2 changed files with 279 additions and 1 deletion.
9 changes: 8 additions & 1 deletion mod/forum/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1394,8 +1394,11 @@ function forum_print_overview($courses,&$htmlarray) {
.'JOIN {forum_posts} p ON p.discussion = d.id '
."WHERE ($coursessql) "
.'AND p.userid != ? '
.'AND (d.timestart <= ? AND (d.timeend = 0 OR d.timeend > ?)) '
.'GROUP BY d.id, d.forum, d.course, d.groupid '
.'ORDER BY d.course, d.forum';
$params[] = time();
$params[] = time();

// Avoid warnings.
if (!$discussions = $DB->get_records_sql($sql, $params)) {
Expand Down Expand Up @@ -1440,8 +1443,12 @@ function forum_print_overview($courses,&$htmlarray) {
$params[] = $groupid;
}
$sql = substr($sql,0,-3); // take off the last OR
$sql .= ') AND p.modified >= ? AND r.id is NULL GROUP BY d.forum,d.course';
$sql .= ') AND p.modified >= ? AND r.id is NULL ';
$sql .= 'AND (d.timestart < ? AND (d.timeend = 0 OR d.timeend > ?)) ';
$sql .= 'GROUP BY d.forum,d.course';
$params[] = $cutoffdate;
$params[] = time();
$params[] = time();

if (!$unread = $DB->get_records_sql($sql, $params)) {
$unread = array();
Expand Down
271 changes: 271 additions & 0 deletions mod/forum/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2194,4 +2194,275 @@ public function test_mod_forum_myprofile_navigation_different_user() {
$this->assertArrayHasKey('forumposts', $nodes->getValue($tree));
$this->assertArrayHasKey('forumdiscussions', $nodes->getValue($tree));
}

public function test_print_overview() {
$this->resetAfterTest();
$course1 = self::getDataGenerator()->create_course();
$course2 = self::getDataGenerator()->create_course();

// Create an author user.
$author = self::getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($author->id, $course1->id);
$this->getDataGenerator()->enrol_user($author->id, $course2->id);

// Create a viewer user.
$viewer = self::getDataGenerator()->create_user((object) array('trackforums' => 1));
$this->getDataGenerator()->enrol_user($viewer->id, $course1->id);
$this->getDataGenerator()->enrol_user($viewer->id, $course2->id);

// Create two forums - one in each course.
$record = new stdClass();
$record->course = $course1->id;
$forum1 = self::getDataGenerator()->create_module('forum', (object) array('course' => $course1->id));
$forum2 = self::getDataGenerator()->create_module('forum', (object) array('course' => $course2->id));

// A standard post in the forum.
$record = new stdClass();
$record->course = $course1->id;
$record->userid = $author->id;
$record->forum = $forum1->id;
$this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);

$this->setUser($viewer->id);
$courses = array(
$course1->id => clone $course1,
$course2->id => clone $course2,
);

foreach ($courses as $courseid => $course) {
$courses[$courseid]->lastaccess = 0;
}
$results = array();
forum_print_overview($courses, $results);

// There should be one entry for course1, and no others.
$this->assertCount(1, $results);

// There should be one entry for a forum in course1.
$this->assertCount(1, $results[$course1->id]);
$this->assertArrayHasKey('forum', $results[$course1->id]);
}

public function test_print_overview_groups() {
global $SESSION;
$this->resetAfterTest();
$course1 = self::getDataGenerator()->create_course();
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));

// Create an author user.
$author = self::getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($author->id, $course1->id);

// Create two viewer users - one in each group.
$viewer1 = self::getDataGenerator()->create_user((object) array('trackforums' => 1));
$this->getDataGenerator()->enrol_user($viewer1->id, $course1->id);
$this->getDataGenerator()->create_group_member(array('userid' => $viewer1->id, 'groupid' => $group1->id));

$viewer2 = self::getDataGenerator()->create_user((object) array('trackforums' => 1));
$this->getDataGenerator()->enrol_user($viewer2->id, $course1->id);
$this->getDataGenerator()->create_group_member(array('userid' => $viewer2->id, 'groupid' => $group2->id));

// Create a forum.
$record = new stdClass();
$record->course = $course1->id;
$forum1 = self::getDataGenerator()->create_module('forum', (object) array(
'course' => $course1->id,
'groupmode' => SEPARATEGROUPS,
));

// A post in the forum for group1.
$record = new stdClass();
$record->course = $course1->id;
$record->userid = $author->id;
$record->forum = $forum1->id;
$record->groupid = $group1->id;
$this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);

$course1->lastaccess = 0;
$courses = array($course1->id => $course1);

// As viewer1 (same group as post).
$this->setUser($viewer1->id);
$results = array();
forum_print_overview($courses, $results);

// There should be one entry for course1.
$this->assertCount(1, $results);

// There should be one entry for a forum in course1.
$this->assertCount(1, $results[$course1->id]);
$this->assertArrayHasKey('forum', $results[$course1->id]);

// As viewer2 (different group to post).
// Temporary hack to workaround for MDL-52656.
$SESSION->currentgroup = null;

$this->setUser($viewer2->id);
$results = array();
forum_print_overview($courses, $results);

// There should be one entry for course1.
$this->assertCount(0, $results);
}

/**
* @dataProvider print_overview_timed_provider
*/
public function test_print_overview_timed($config, $hasresult) {
$this->resetAfterTest();
$course1 = self::getDataGenerator()->create_course();

// Create an author user.
$author = self::getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($author->id, $course1->id);

// Create a viewer user.
$viewer = self::getDataGenerator()->create_user((object) array('trackforums' => 1));
$this->getDataGenerator()->enrol_user($viewer->id, $course1->id);

// Create a forum.
$record = new stdClass();
$record->course = $course1->id;
$forum1 = self::getDataGenerator()->create_module('forum', (object) array('course' => $course1->id));

// A timed post with a timestart in the past (24 hours ago).
$record = new stdClass();
$record->course = $course1->id;
$record->userid = $author->id;
$record->forum = $forum1->id;
if (isset($config['timestartmodifier'])) {
$record->timestart = time() + $config['timestartmodifier'];
}
if (isset($config['timeendmodifier'])) {
$record->timeend = time() + $config['timeendmodifier'];
}
$this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);

$course1->lastaccess = 0;
$courses = array($course1->id => $course1);

// As viewer, check the forum_print_overview result.
$this->setUser($viewer->id);
$results = array();
forum_print_overview($courses, $results);

if ($hasresult) {
// There should be one entry for course1.
$this->assertCount(1, $results);

// There should be one entry for a forum in course1.
$this->assertCount(1, $results[$course1->id]);
$this->assertArrayHasKey('forum', $results[$course1->id]);
} else {
// There should be no entries for any course.
$this->assertCount(0, $results);
}
}

/**
* @dataProvider print_overview_timed_provider
*/
public function test_print_overview_timed_groups($config, $hasresult) {
global $SESSION;
$this->resetAfterTest();
$course1 = self::getDataGenerator()->create_course();
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));

// Create an author user.
$author = self::getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($author->id, $course1->id);

// Create two viewer users - one in each group.
$viewer1 = self::getDataGenerator()->create_user((object) array('trackforums' => 1));
$this->getDataGenerator()->enrol_user($viewer1->id, $course1->id);
$this->getDataGenerator()->create_group_member(array('userid' => $viewer1->id, 'groupid' => $group1->id));

$viewer2 = self::getDataGenerator()->create_user((object) array('trackforums' => 1));
$this->getDataGenerator()->enrol_user($viewer2->id, $course1->id);
$this->getDataGenerator()->create_group_member(array('userid' => $viewer2->id, 'groupid' => $group2->id));

// Create a forum.
$record = new stdClass();
$record->course = $course1->id;
$forum1 = self::getDataGenerator()->create_module('forum', (object) array(
'course' => $course1->id,
'groupmode' => SEPARATEGROUPS,
));

// A post in the forum for group1.
$record = new stdClass();
$record->course = $course1->id;
$record->userid = $author->id;
$record->forum = $forum1->id;
$record->groupid = $group1->id;
if (isset($config['timestartmodifier'])) {
$record->timestart = time() + $config['timestartmodifier'];
}
if (isset($config['timeendmodifier'])) {
$record->timeend = time() + $config['timeendmodifier'];
}
$this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);

$course1->lastaccess = 0;
$courses = array($course1->id => $course1);

// As viewer1 (same group as post).
$this->setUser($viewer1->id);
$results = array();
forum_print_overview($courses, $results);

if ($hasresult) {
// There should be one entry for course1.
$this->assertCount(1, $results);

// There should be one entry for a forum in course1.
$this->assertCount(1, $results[$course1->id]);
$this->assertArrayHasKey('forum', $results[$course1->id]);
} else {
// There should be no entries for any course.
$this->assertCount(0, $results);
}

// As viewer2 (different group to post).
// Temporary hack to workaround for MDL-52656.
$SESSION->currentgroup = null;

$this->setUser($viewer2->id);
$results = array();
forum_print_overview($courses, $results);

// There should be one entry for course1.
$this->assertCount(0, $results);
}

public function print_overview_timed_provider() {
return array(
'timestart_past' => array(
'discussionconfig' => array(
'timestartmodifier' => -86000,
),
'hasresult' => true,
),
'timestart_future' => array(
'discussionconfig' => array(
'timestartmodifier' => 86000,
),
'hasresult' => false,
),
'timeend_past' => array(
'discussionconfig' => array(
'timeendmodifier' => -86000,
),
'hasresult' => false,
),
'timeend_future' => array(
'discussionconfig' => array(
'timeendmodifier' => 86000,
),
'hasresult' => true,
),
);
}
}

0 comments on commit 6667ebb

Please sign in to comment.