/** * 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)); }
/** * Get a discussion posts and related info * * @param $discussionid * @return array */ public function get_posts($discussionid) { global $PAGE, $DB, $CFG, $COURSE, $USER; $discussion = $DB->get_record('hsuforum_discussions', array('id' => $discussionid), '*', MUST_EXIST); $forum = $PAGE->activityrecord; $course = $COURSE; $cm = get_coursemodule_from_id('hsuforum', $PAGE->cm->id, $course->id, false, MUST_EXIST); // Cannot use cm_info because it is read only. $context = $PAGE->context; 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 (!($post = hsuforum_get_post_full($discussion->firstpost))) { 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('nopermissiontoview', 'hsuforum', "{$CFG->wwwroot}/mod/hsuforum/view.php?f={$forum->id}"); } $posts = hsuforum_get_all_discussion_posts($discussion->id); $canreply = hsuforum_user_can_post($forum, $discussion, $USER, $cm, $course, $context); hsuforum_get_ratings_for_posts($context, $forum, $posts); return array($cm, $discussion, $posts, $canreply); }
echo $OUTPUT->heading(format_string($forum->name), 2); echo $OUTPUT->confirm(get_string("deletesureplural", "hsuforum", $replycount + 1), "post.php?delete={$delete}&confirm={$delete}", $CFG->wwwroot . '/mod/hsuforum/discuss.php?d=' . $post->discussion . '#p' . $post->id); echo $renderer->post($cm, $discussion, $post, false, null, false); } else { echo $OUTPUT->header(); echo $OUTPUT->heading(format_string($forum->name), 2); echo $OUTPUT->confirm(get_string("deletesure", "hsuforum", $replycount), "post.php?delete={$delete}&confirm={$delete}", $CFG->wwwroot . '/mod/hsuforum/discuss.php?d=' . $post->discussion . '#p' . $post->id); echo $renderer->post($cm, $discussion, $post, false, null, false); } } echo $OUTPUT->footer(); die; } else { if (!empty($prune)) { // Pruning if (!($post = hsuforum_get_post_full($prune))) { print_error('invalidpostid', 'hsuforum'); } if (!($discussion = $DB->get_record("hsuforum_discussions", array("id" => $post->discussion)))) { print_error('notpartofdiscussion', 'hsuforum'); } if (!($forum = $DB->get_record("hsuforum", array("id" => $discussion->forum)))) { print_error('invalidforumid', 'hsuforum'); } if ($forum->type == 'single') { print_error('cannotsplit', 'hsuforum'); } if (!$post->parent) { print_error('alreadyfirstpost', 'hsuforum'); } if (!($cm = get_coursemodule_from_instance("hsuforum", $forum->id, $forum->course))) {
require_once $CFG->dirroot . '/mod/hsuforum/rsslib.php'; hsuforum_rss_delete_file($forum); hsuforum_rss_delete_file($forumto); redirect($return . '&moved=-1&sesskey=' . sesskey()); } } $params = array('context' => $modcontext, 'objectid' => $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)));
/** * @param $userid * @param $cm * @param null|stdClass $showonlypreference * * @return string * @author Mark Nielsen */ public function user_posts_overview($userid, $cm, $showonlypreference = null) { global $PAGE; require_once __DIR__ . '/lib/flag.php'; $config = get_config('hsuforum'); $forum = hsuforum_get_cm_forum($cm); $showonlypreferencebutton = ''; if (!empty($showonlypreference) and !empty($showonlypreference->button) and !$forum->anonymous) { $showonlypreferencebutton = $showonlypreference->button; } $output = ''; $postcount = $discussioncount = $flagcount = 0; $flaglib = new hsuforum_lib_flag(); if ($posts = hsuforum_get_user_posts($forum->id, $userid, context_module::instance($cm->id))) { $discussions = hsuforum_get_user_involved_discussions($forum->id, $userid); if (!empty($showonlypreference) and !empty($showonlypreference->preference)) { foreach ($discussions as $discussion) { if ($discussion->userid == $userid and array_key_exists($discussion->firstpost, $posts)) { $discussionpost = $posts[$discussion->firstpost]; $discussioncount++; if ($flaglib->is_flagged($discussionpost->flags, 'substantive')) { $flagcount++; } } else { if (!($discussionpost = hsuforum_get_post_full($discussion->firstpost))) { continue; } } if (!$forum->anonymous) { $output .= $this->post($cm, $discussion, $discussionpost, false, null, false); $output .= html_writer::start_tag('div', array('class' => 'indent')); } foreach ($posts as $post) { if ($post->discussion == $discussion->id and !empty($post->parent)) { $postcount++; if ($flaglib->is_flagged($post->flags, 'substantive')) { $flagcount++; } $command = html_writer::link(new moodle_url('/mod/hsuforum/discuss.php', array('d' => $discussion->id), 'p' . $post->id), get_string('postincontext', 'hsuforum'), array('target' => '_blank')); if (!$forum->anonymous) { $output .= $this->post($cm, $discussion, $post, false, null, false); } } } if (!$forum->anonymous) { $output .= html_writer::end_tag('div'); } } } else { foreach ($posts as $post) { if (!empty($post->parent)) { $postcount++; } else { $discussioncount++; } if ($flaglib->is_flagged($post->flags, 'substantive')) { $flagcount++; } if (!$forum->anonymous) { $command = html_writer::link(new moodle_url('/mod/hsuforum/discuss.php', array('d' => $post->discussion), 'p' . $post->id), get_string('postincontext', 'hsuforum'), array('target' => '_blank')); $output .= $this->post($cm, $discussions[$post->discussion], $post, false, null, false); } } } } if (!empty($postcount) or !empty($discussioncount)) { if ($forum->anonymous) { $output = html_writer::tag('h3', get_string('thisisanonymous', 'hsuforum')); } $counts = array(get_string('totalpostsanddiscussions', 'hsuforum', $discussioncount + $postcount), get_string('totaldiscussions', 'hsuforum', $discussioncount), get_string('totalreplies', 'hsuforum', $postcount)); if (!empty($config->showsubstantive)) { $counts[] = get_string('totalsubstantive', 'hsuforum', $flagcount); } if ($grade = hsuforum_get_user_formatted_rating_grade($forum, $userid)) { $counts[] = get_string('totalrating', 'hsuforum', $grade); } $countshtml = ''; foreach ($counts as $count) { $countshtml .= html_writer::tag('div', $count, array('class' => 'hsuforum_count')); } $output = html_writer::div($countshtml, 'hsuforum_counts') . $showonlypreferencebutton . $output; $output = html_writer::div($output, 'mod-hsuforum-posts-container article'); } return $output; }
/** * Run the actual export on a discussion * * @param \stdClass $discussion * @param int $userid Only export posts with this user as the author */ public function process_discussion($discussion, $userid = 0) { global $USER; if (hsuforum_get_cm_forum($this->cm)->type == 'news') { if (!($USER->id == $discussion->userid || ($discussion->timestart == 0 || $discussion->timestart <= time()) && ($discussion->timeend == 0 || $discussion->timeend > time()))) { return; } } if (!empty($userid)) { $conditions = array('p.userid' => $userid); } else { $conditions = array(); } $posts = hsuforum_get_all_discussion_posts($discussion->id, $conditions); if (array_key_exists($discussion->firstpost, $posts)) { $post = $posts[$discussion->firstpost]; } else { $post = hsuforum_get_post_full($discussion->firstpost); } if (!hsuforum_user_can_see_post(hsuforum_get_cm_forum($this->cm), $discussion, $post, null, $this->cm)) { return; } $this->clean_posts($discussion, $posts); $this->adapter->send_discussion($discussion, $posts); }
/** * Get the edit post form HTML * * @return json_response */ public function edit_post_form_action() { global $DB, $PAGE; $postid = required_param('postid', PARAM_INT); if (!($post = hsuforum_get_post_full($postid))) { print_error('invalidpostid', 'hsuforum'); } $discussion = $DB->get_record('hsuforum_discussions', array('id' => $post->discussion), '*', MUST_EXIST); $this->postservice->require_can_edit_post($PAGE->activityrecord, $PAGE->context, $discussion, $post); if (!empty($post->parent)) { $html = $this->formservice->edit_post_form($PAGE->cm, $post); } else { $html = $this->formservice->edit_discussion_form($PAGE->cm, $discussion, $post); } return new json_response(array('html' => $html)); }
/** * Returns a list of forum posts for a discussion * * @param int $discussionid the post ids * * @return array the forum post details * @since Moodle 2.7 */ public static function get_forum_discussion_posts($discussionid) { global $CFG, $DB, $USER; $warnings = array(); // Validate the parameter. $params = self::validate_parameters(self::get_forum_discussion_posts_parameters(), array('discussionid' => $discussionid)); // Compact/extract functions are not recommended. $discussionid = $params['discussionid']; $discussion = $DB->get_record('hsuforum_discussions', array('id' => $discussionid), '*', MUST_EXIST); $forum = $DB->get_record('hsuforum', array('id' => $discussion->forum), '*', MUST_EXIST); $course = $DB->get_record('course', array('id' => $forum->course), '*', MUST_EXIST); $cm = get_coursemodule_from_instance('hsuforum', $forum->id, $course->id, false, MUST_EXIST); // Validate the module context. It checks everything that affects the module visibility (including groupings, etc..). $modcontext = context_module::instance($cm->id); self::validate_context($modcontext); // This require must be here, see mod/hsuforum/discuss.php. require_once $CFG->dirroot . "/mod/hsuforum/lib.php"; // Check they have the view forum capability. require_capability('mod/hsuforum:viewdiscussion', $modcontext, null, true, 'noviewdiscussionspermission', 'forum'); if (!($post = hsuforum_get_post_full($discussion->firstpost))) { throw new moodle_exception('notexists', 'hsuforum'); } // This function check groups, qanda, timed discussions, etc. if (!hsuforum_user_can_see_post($forum, $discussion, $post, null, $cm)) { throw new moodle_exception('noviewdiscussionspermission', 'hsuforum'); } $canviewfullname = has_capability('moodle/site:viewfullnames', $modcontext); // We will add this field in the response. $canreply = hsuforum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext); $posts = hsuforum_get_all_discussion_posts($discussion->id); foreach ($posts as $pid => $post) { if (!hsuforum_user_can_see_post($forum, $discussion, $post, null, $cm)) { $warning = array(); $warning['item'] = 'post'; $warning['itemid'] = $post->id; $warning['warningcode'] = '1'; $warning['message'] = 'You can\'t see this post'; $warnings[] = $warning; continue; } // Function hsuforum_get_all_discussion_posts adds postread field. // Note that the value returned can be a boolean or an integer. The WS expects a boolean. if (empty($post->postread)) { $posts[$pid]->postread = false; } else { $posts[$pid]->postread = true; } $posts[$pid]->canreply = $canreply; if (!empty($posts[$pid]->children)) { $posts[$pid]->children = array_keys($posts[$pid]->children); } else { $posts[$pid]->children = array(); } $user = new stdclass(); $user = username_load_fields_from_object($user, $post); $posts[$pid]->userfullname = fullname($user, $canviewfullname); $posts[$pid] = (array) $post; } $result = array(); $result['posts'] = $posts; $result['warnings'] = $warnings; return $result; }