コード例 #1
0
ファイル: lib_test.php プロジェクト: Gavinthisisit/Moodle
 /**
  * Test the logic in the twf_tp_get_course_unread_posts() function.
  */
 public function test_twf_tp_get_course_unread_posts()
 {
     global $CFG;
     $this->resetAfterTest();
     $useron = $this->getDataGenerator()->create_user(array('tracktwfs' => 1));
     $useroff = $this->getDataGenerator()->create_user(array('tracktwfs' => 0));
     $course = $this->getDataGenerator()->create_course();
     $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_OFF);
     // Off.
     $twfoff = $this->getDataGenerator()->create_module('twf', $options);
     $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_FORCED);
     // On.
     $twfforce = $this->getDataGenerator()->create_module('twf', $options);
     $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_OPTIONAL);
     // Optional.
     $twfoptional = $this->getDataGenerator()->create_module('twf', $options);
     // Add discussions to the tracking off twf.
     $record = new stdClass();
     $record->course = $course->id;
     $record->userid = $useron->id;
     $record->twf = $twfoff->id;
     $discussionoff = $this->getDataGenerator()->get_plugin_generator('mod_twf')->create_discussion($record);
     // Add discussions to the tracking forced twf.
     $record = new stdClass();
     $record->course = $course->id;
     $record->userid = $useron->id;
     $record->twf = $twfforce->id;
     $discussionforce = $this->getDataGenerator()->get_plugin_generator('mod_twf')->create_discussion($record);
     // Add post to the tracking forced discussion.
     $record = new stdClass();
     $record->course = $course->id;
     $record->userid = $useroff->id;
     $record->twf = $twfforce->id;
     $record->discussion = $discussionforce->id;
     $this->getDataGenerator()->get_plugin_generator('mod_twf')->create_post($record);
     // Add discussions to the tracking optional twf.
     $record = new stdClass();
     $record->course = $course->id;
     $record->userid = $useron->id;
     $record->twf = $twfoptional->id;
     $discussionoptional = $this->getDataGenerator()->get_plugin_generator('mod_twf')->create_discussion($record);
     // Allow force.
     $CFG->twf_allowforcedreadtracking = 1;
     $result = twf_tp_get_course_unread_posts($useron->id, $course->id);
     $this->assertEquals(2, count($result));
     $this->assertEquals(false, isset($result[$twfoff->id]));
     $this->assertEquals(true, isset($result[$twfforce->id]));
     $this->assertEquals(2, $result[$twfforce->id]->unread);
     $this->assertEquals(true, isset($result[$twfoptional->id]));
     $this->assertEquals(1, $result[$twfoptional->id]->unread);
     $result = twf_tp_get_course_unread_posts($useroff->id, $course->id);
     $this->assertEquals(1, count($result));
     $this->assertEquals(false, isset($result[$twfoff->id]));
     $this->assertEquals(true, isset($result[$twfforce->id]));
     $this->assertEquals(2, $result[$twfforce->id]->unread);
     $this->assertEquals(false, isset($result[$twfoptional->id]));
     // Don't allow force.
     $CFG->twf_allowforcedreadtracking = 0;
     $result = twf_tp_get_course_unread_posts($useron->id, $course->id);
     $this->assertEquals(2, count($result));
     $this->assertEquals(false, isset($result[$twfoff->id]));
     $this->assertEquals(true, isset($result[$twfforce->id]));
     $this->assertEquals(2, $result[$twfforce->id]->unread);
     $this->assertEquals(true, isset($result[$twfoptional->id]));
     $this->assertEquals(1, $result[$twfoptional->id]->unread);
     $result = twf_tp_get_course_unread_posts($useroff->id, $course->id);
     $this->assertEquals(0, count($result));
     $this->assertEquals(false, isset($result[$twfoff->id]));
     $this->assertEquals(false, isset($result[$twfforce->id]));
     $this->assertEquals(false, isset($result[$twfoptional->id]));
     // Stop tracking so we can test again.
     twf_tp_stop_tracking($twfforce->id, $useron->id);
     twf_tp_stop_tracking($twfoptional->id, $useron->id);
     twf_tp_stop_tracking($twfforce->id, $useroff->id);
     twf_tp_stop_tracking($twfoptional->id, $useroff->id);
     // Allow force.
     $CFG->twf_allowforcedreadtracking = 1;
     $result = twf_tp_get_course_unread_posts($useron->id, $course->id);
     $this->assertEquals(1, count($result));
     $this->assertEquals(false, isset($result[$twfoff->id]));
     $this->assertEquals(true, isset($result[$twfforce->id]));
     $this->assertEquals(2, $result[$twfforce->id]->unread);
     $this->assertEquals(false, isset($result[$twfoptional->id]));
     $result = twf_tp_get_course_unread_posts($useroff->id, $course->id);
     $this->assertEquals(1, count($result));
     $this->assertEquals(false, isset($result[$twfoff->id]));
     $this->assertEquals(true, isset($result[$twfforce->id]));
     $this->assertEquals(2, $result[$twfforce->id]->unread);
     $this->assertEquals(false, isset($result[$twfoptional->id]));
     // Don't allow force.
     $CFG->twf_allowforcedreadtracking = 0;
     $result = twf_tp_get_course_unread_posts($useron->id, $course->id);
     $this->assertEquals(0, count($result));
     $this->assertEquals(false, isset($result[$twfoff->id]));
     $this->assertEquals(false, isset($result[$twfforce->id]));
     $this->assertEquals(false, isset($result[$twfoptional->id]));
     $result = twf_tp_get_course_unread_posts($useroff->id, $course->id);
     $this->assertEquals(0, count($result));
     $this->assertEquals(false, isset($result[$twfoff->id]));
     $this->assertEquals(false, isset($result[$twfforce->id]));
     $this->assertEquals(false, isset($result[$twfoptional->id]));
 }
コード例 #2
0
ファイル: lib.php プロジェクト: Gavinthisisit/Moodle
/**
 * Returns the count of records for the provided user and twf and [optionally] group.
 *
 * @global object
 * @global object
 * @global object
 * @param object $cm
 * @param object $course
 * @return int
 */
function twf_tp_count_twf_unread_posts($cm, $course)
{
    global $CFG, $USER, $DB;
    static $readcache = array();
    $twfid = $cm->instance;
    if (!isset($readcache[$course->id])) {
        $readcache[$course->id] = array();
        if ($counts = twf_tp_get_course_unread_posts($USER->id, $course->id)) {
            foreach ($counts as $count) {
                $readcache[$course->id][$count->id] = $count->unread;
            }
        }
    }
    if (empty($readcache[$course->id][$twfid])) {
        // no need to check group mode ;-)
        return 0;
    }
    $groupmode = groups_get_activity_groupmode($cm, $course);
    if ($groupmode != SEPARATEGROUPS) {
        return $readcache[$course->id][$twfid];
    }
    if (has_capability('moodle/site:accessallgroups', context_module::instance($cm->id))) {
        return $readcache[$course->id][$twfid];
    }
    require_once $CFG->dirroot . '/course/lib.php';
    $modinfo = get_fast_modinfo($course);
    $mygroups = $modinfo->get_groups($cm->groupingid);
    // add all groups posts
    $mygroups[-1] = -1;
    list($groups_sql, $groups_params) = $DB->get_in_or_equal($mygroups);
    $now = round(time(), -2);
    // db cache friendliness
    $cutoffdate = $now - $CFG->twf_oldpostdays * 24 * 60 * 60;
    $params = array($USER->id, $twfid, $cutoffdate);
    if (!empty($CFG->twf_enabletimedposts)) {
        $timedsql = "AND d.timestart < ? AND (d.timeend = 0 OR d.timeend > ?)";
        $params[] = $now;
        $params[] = $now;
    } else {
        $timedsql = "";
    }
    $params = array_merge($params, $groups_params);
    $sql = "SELECT COUNT(p.id)\n              FROM {twf_posts} p\n                   JOIN {twf_discussions} d ON p.discussion = d.id\n                   LEFT JOIN {twf_read} r   ON (r.postid = p.id AND r.userid = ?)\n             WHERE d.twf = ?\n                   AND p.modified >= ? AND r.id is NULL\n                   {$timedsql}\n                   AND d.groupid {$groups_sql}";
    return $DB->get_field_sql($sql, $params);
}