/**
  * Marks a post as read
  *
  * @throws coding_exception
  */
 public function markread_action()
 {
     global $PAGE, $DB, $CFG, $USER;
     if (!AJAX_SCRIPT) {
         throw new coding_exception('This is an AJAX action and you cannot access it directly');
     }
     require_once $CFG->dirroot . '/rating/lib.php';
     $postid = required_param('postid', PARAM_INT);
     $forum = $PAGE->activityrecord;
     $cm = $PAGE->cm;
     if (!($post = hsuforum_get_post_full($postid))) {
         print_error("notexists", 'hsuforum', "{$CFG->wwwroot}/mod/hsuforum/view.php?f={$forum->id}");
     }
     $discussion = $DB->get_record('hsuforum_discussions', array('id' => $post->discussion), '*', MUST_EXIST);
     if ($forum->type == 'news') {
         if (!($USER->id == $discussion->userid || ($discussion->timestart == 0 || $discussion->timestart <= time()) && ($discussion->timeend == 0 || $discussion->timeend > time()))) {
             print_error('invaliddiscussionid', 'hsuforum', "{$CFG->wwwroot}/mod/hsuforum/view.php?f={$forum->id}");
         }
     }
     if (!hsuforum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
         print_error('nopermissiontoview', 'hsuforum', "{$CFG->wwwroot}/mod/hsuforum/view.php?f={$forum->id}");
     }
     hsuforum_tp_add_read_record($USER->id, $post->id);
     return new json_response(array('postid' => $postid, 'discussionid' => $discussion->id));
 }
$event = \mod_hsuforum\event\discussion_viewed::create($params);
$event->add_record_snapshot('hsuforum_discussions', $discussion);
$event->add_record_snapshot('hsuforum', $forum);
$event->trigger();
unset($SESSION->fromdiscussion);
if (!$root) {
    $root = $discussion->firstpost;
}
if (!($post = hsuforum_get_post_full($root))) {
    print_error("notexists", 'hsuforum', "{$CFG->wwwroot}/mod/hsuforum/view.php?f={$forum->id}");
}
if (!hsuforum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
    print_error('noviewdiscussionspermission', 'hsuforum', "{$CFG->wwwroot}/mod/hsuforum/view.php?id={$forum->id}");
}
if ($mark == 'read') {
    hsuforum_tp_add_read_record($USER->id, $postid);
}
$forumnode = $PAGE->navigation->find($cm->id, navigation_node::TYPE_ACTIVITY);
if (empty($forumnode)) {
    $forumnode = $PAGE->navbar;
} else {
    $forumnode->make_active();
}
$node = $forumnode->add(format_string($discussion->name), new moodle_url('/mod/hsuforum/discuss.php', array('d' => $discussion->id)));
$node->display = false;
if ($node && $post->id != $discussion->firstpost) {
    $node->add(format_string($post->subject), $PAGE->url);
}
$dsort = hsuforum_lib_discussion_sort::get_from_session($forum, $modcontext);
$renderer = $PAGE->get_renderer('mod_hsuforum');
$PAGE->requires->js_init_call('M.mod_hsuforum.init', null, false, $renderer->get_js_module());
Esempio n. 3
0
/**
 * If its an old post, do nothing. If the record exists, the maintenance will clear it up later.
 *
 * @return bool
 */
function hsuforum_mark_post_read($userid, $post, $forumid)
{
    if (!hsuforum_tp_is_post_old($post)) {
        return hsuforum_tp_add_read_record($userid, $post->id);
    } else {
        return true;
    }
}
 /**
  * Test mod_hsuforum_observer methods.
  */
 public function test_observers()
 {
     global $DB, $CFG;
     require_once $CFG->dirroot . '/mod/hsuforum/lib.php';
     $forumgen = $this->getDataGenerator()->get_plugin_generator('mod_hsuforum');
     $course = $this->getDataGenerator()->create_course();
     $trackedrecord = array('course' => $course->id, 'type' => 'general', 'forcesubscribe' => HSUFORUM_INITIALSUBSCRIBE);
     $untrackedrecord = array('course' => $course->id, 'type' => 'general');
     $trackedforum = $this->getDataGenerator()->create_module('hsuforum', $trackedrecord);
     $untrackedforum = $this->getDataGenerator()->create_module('hsuforum', $untrackedrecord);
     // Used functions don't require these settings; adding
     // them just in case there are APIs changes in future.
     $user = $this->getDataGenerator()->create_user(array('maildigest' => 1, 'trackforums' => 1));
     $manplugin = enrol_get_plugin('manual');
     $manualenrol = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'));
     $student = $DB->get_record('role', array('shortname' => 'student'));
     // The role_assign observer does it's job adding the hsuforum_subscriptions record.
     $manplugin->enrol_user($manualenrol, $user->id, $student->id);
     $record = array();
     $record['course'] = $course->id;
     $record['forum'] = $trackedforum->id;
     $record['userid'] = $user->id;
     // Creating a discussion calls hsuforum_add_discussion which automatically adds a read record
     // So at this point the read count is 1.
     $discussion = $forumgen->create_discussion($record);
     $record = array();
     $record['discussion'] = $discussion->id;
     $record['userid'] = $user->id;
     // Creating a post doesn't automatically add a read record.
     $post = $forumgen->create_post($record);
     // Add a read record for this post - the read record count is now 2
     hsuforum_tp_add_read_record($user->id, $post->id);
     hsuforum_set_user_maildigest($trackedforum, 2, $user);
     $this->assertEquals(1, $DB->count_records('hsuforum_subscriptions', array('userid' => $user->id)));
     $this->assertEquals(1, $DB->count_records('hsuforum_digests', array('userid' => $user->id)));
     $this->assertEquals(2, $DB->count_records('hsuforum_read', array('userid' => $user->id)));
     // The course_module_created observer does it's job adding a subscription.
     $forumrecord = array('course' => $course->id, 'type' => 'general', 'forcesubscribe' => HSUFORUM_INITIALSUBSCRIBE);
     $extraforum = $this->getDataGenerator()->create_module('hsuforum', $forumrecord);
     $this->assertEquals(2, $DB->count_records('hsuforum_subscriptions'));
     $manplugin->unenrol_user($manualenrol, $user->id);
     $this->assertEquals(0, $DB->count_records('hsuforum_digests'));
     $this->assertEquals(0, $DB->count_records('hsuforum_subscriptions'));
     $this->assertEquals(0, $DB->count_records('hsuforum_track_prefs'));
     $this->assertEquals(0, $DB->count_records('hsuforum_read'));
 }