/** * 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); }
if (!empty($thresholdwarning) && !$edit) { // Here we want to throw an exception if they are no longer allowed to post. hsuforum_check_blocking_threshold($thresholdwarning); } if (!empty($parent)) { if (!($discussion = $DB->get_record('hsuforum_discussions', array('id' => $parent->discussion)))) { print_error('notpartofdiscussion', 'hsuforum'); } echo $renderer->svg_sprite(); // We don't have the valid unread status. Set to read so we don't see // the unread tag. $parent->postread = true; echo $renderer->post($cm, $discussion, $parent); if (empty($post->edit)) { if ($forum->type != 'qanda' || hsuforum_user_can_see_discussion($forum, $discussion, $modcontext)) { $posts = hsuforum_get_all_discussion_posts($discussion->id); } } } else { if (!empty($forum->intro)) { echo $OUTPUT->box(format_module_intro('hsuforum', $forum, $cm->id), 'generalbox', 'intro'); if (!empty($CFG->enableplagiarism)) { require_once $CFG->libdir . '/plagiarismlib.php'; echo plagiarism_print_disclosure($cm->id); } } } if (!empty($formheading)) { echo $OUTPUT->heading($formheading, 4); } $mform_post->display();
/** * Prints a forum discussion * * @uses CONTEXT_MODULE * @param stdClass $course * @param stdClass $cm * @param stdClass $forum * @param stdClass $discussion * @param stdClass $post * @param mixed $canreply * @param bool $canrate */ function hsuforum_print_discussion($course, $cm, $forum, $discussion, $post, $canreply = NULL, $canrate = false) { global $USER, $CFG, $OUTPUT, $PAGE; require_once $CFG->dirroot . '/rating/lib.php'; $modcontext = context_module::instance($cm->id); if ($canreply === NULL) { $reply = hsuforum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext); } else { $reply = $canreply; } $posters = array(); $posts = hsuforum_get_all_discussion_posts($discussion->id); $post = $posts[$post->id]; foreach ($posts as $pid => $p) { $posters[$p->userid] = $p->userid; } //load ratings if ($forum->assessed != RATING_AGGREGATE_NONE) { $ratingoptions = new stdClass(); $ratingoptions->context = $modcontext; $ratingoptions->component = 'mod_hsuforum'; $ratingoptions->ratingarea = 'post'; $ratingoptions->items = $posts; $ratingoptions->aggregate = $forum->assessed; //the aggregation method $ratingoptions->scaleid = $forum->scale; $ratingoptions->userid = $USER->id; if ($forum->type == 'single' or !$discussion->id) { $ratingoptions->returnurl = "{$CFG->wwwroot}/mod/hsuforum/view.php?id={$cm->id}"; } else { $ratingoptions->returnurl = "{$CFG->wwwroot}/mod/hsuforum/discuss.php?d={$discussion->id}"; } $ratingoptions->assesstimestart = $forum->assesstimestart; $ratingoptions->assesstimefinish = $forum->assesstimefinish; $rm = new rating_manager(); $posts = $rm->get_ratings($ratingoptions); } $post->forum = $forum->id; // Add the forum id to the post object, later used for rendering $post->forumtype = $forum->type; $post->subject = format_string($post->subject); $postread = !empty($post->postread); echo $OUTPUT->box_start("mod-hsuforum-posts-container article"); $renderer = $PAGE->get_renderer('mod_hsuforum'); echo $renderer->discussion_thread($cm, $discussion, $post, $posts, $reply); echo $OUTPUT->box_end(); // End mod-hsuforum-posts-container return; }
/** * 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); }
/** * @global object */ public function load_data() { global $DB; if ($this->postid) { if (!($this->post = $DB->get_record('hsuforum_posts', array('id' => $this->postid)))) { throw new portfolio_caller_exception('invalidpostid', 'hsuforum'); } } $dparams = array(); if ($this->discussionid) { $dbparams = array('id' => $this->discussionid); } else { if ($this->post) { $dbparams = array('id' => $this->post->discussion); } else { throw new portfolio_caller_exception('mustprovidediscussionorpost', 'hsuforum'); } } if (!($this->discussion = $DB->get_record('hsuforum_discussions', $dbparams))) { throw new portfolio_caller_exception('invaliddiscussionid', 'hsuforum'); } if (!($this->forum = $DB->get_record('hsuforum', array('id' => $this->discussion->forum)))) { throw new portfolio_caller_exception('invalidforumid', 'hsuforum'); } if (!($this->cm = get_coursemodule_from_instance('hsuforum', $this->forum->id))) { throw new portfolio_caller_exception('invalidcoursemodule'); } $this->modcontext = context_module::instance($this->cm->id); $fs = get_file_storage(); if ($this->post) { if ($this->attachment) { $this->set_file_and_format_data($this->attachment); } else { $attach = $fs->get_area_files($this->modcontext->id, 'mod_hsuforum', 'attachment', $this->post->id, 'timemodified', false); $embed = $fs->get_area_files($this->modcontext->id, 'mod_hsuforum', 'post', $this->post->id, 'timemodified', false); $files = array_merge($attach, $embed); $this->set_file_and_format_data($files); } if (!empty($this->multifiles)) { $this->keyedfiles[$this->post->id] = $this->multifiles; } else { if (!empty($this->singlefile)) { $this->keyedfiles[$this->post->id] = array($this->singlefile); } } } else { // whole thread $fs = get_file_storage(); $this->posts = hsuforum_get_all_discussion_posts($this->discussion->id); $this->multifiles = array(); foreach ($this->posts as $post) { $attach = $fs->get_area_files($this->modcontext->id, 'mod_hsuforum', 'attachment', $post->id, 'timemodified', false); $embed = $fs->get_area_files($this->modcontext->id, 'mod_hsuforum', 'post', $post->id, 'timemodified', false); $files = array_merge($attach, $embed); if ($files) { $this->keyedfiles[$post->id] = $files; } else { continue; } $this->multifiles = array_merge($this->multifiles, array_values($this->keyedfiles[$post->id])); } } if (empty($this->multifiles) && !empty($this->singlefile)) { $this->multifiles = array($this->singlefile); // copy_files workaround } // depending on whether there are files or not, we might have to change richhtml/plainhtml if (empty($this->attachment)) { if (!empty($this->multifiles)) { $this->add_format(PORTFOLIO_FORMAT_RICHHTML); } else { $this->add_format(PORTFOLIO_FORMAT_PLAINHTML); } } }
/** * 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; }