$newdiscussion->groupid = $discussion->groupid; $newdiscussion->assessed = $discussion->assessed; $newdiscussion->usermodified = $post->userid; $newdiscussion->timestart = $discussion->timestart; $newdiscussion->timeend = $discussion->timeend; $newid = $DB->insert_record('hsuforum_discussions', $newdiscussion); $newpost = new stdClass(); $newpost->id = $post->id; $newpost->parent = 0; $newpost->subject = $name; $newpost->privatereply = 0; $DB->update_record("hsuforum_posts", $newpost); hsuforum_change_discussionid($post->id, $newid); // update last post in each discussion hsuforum_discussion_update_last_post($discussion->id); hsuforum_discussion_update_last_post($newid); // Fire events to reflect the split.. $params = array('context' => $modcontext, 'objectid' => $discussion->id, 'other' => array('forumid' => $forum->id)); $event = \mod_hsuforum\event\discussion_updated::create($params); $event->trigger(); $params = array('context' => $modcontext, 'objectid' => $newid, 'other' => array('forumid' => $forum->id)); $event = \mod_hsuforum\event\discussion_created::create($params); $event->trigger(); $params = array('context' => $modcontext, 'objectid' => $post->id, 'other' => array('discussionid' => $newid, 'forumid' => $forum->id, 'forumtype' => $forum->type)); $event = \mod_hsuforum\event\post_updated::create($params); $event->add_record_snapshot('hsuforum_discussions', $discussion); $event->trigger(); redirect(hsuforum_go_back_to("discuss.php?d={$newid}")); } else { // User just asked to prune something $course = $DB->get_record('course', array('id' => $forum->course));
/** * Function to create a dummy post. * * @param array|stdClass $record * @return stdClass the post object */ public function create_post($record = null) { global $DB; // Increment the forum post count. $this->forumpostcount++; // Variable to store time. $time = time() + $this->forumpostcount; $record = (array) $record; if (!isset($record['discussion'])) { throw new coding_exception('discussion must be present in phpunit_util::create_post() $record'); } if (!isset($record['userid'])) { throw new coding_exception('userid must be present in phpunit_util::create_post() $record'); } if (!isset($record['parent'])) { $record['parent'] = 0; } if (!isset($record['subject'])) { $record['subject'] = 'Forum post subject ' . $this->forumpostcount; } if (!isset($record['message'])) { $record['message'] = html_writer::tag('p', 'Forum message post ' . $this->forumpostcount); } if (!isset($record['created'])) { $record['created'] = $time; } if (!isset($record['modified'])) { $record['modified'] = $time; } if (!isset($record['mailed'])) { $record['mailed'] = 0; } if (!isset($record['messageformat'])) { $record['messageformat'] = 0; } if (!isset($record['messagetrust'])) { $record['messagetrust'] = 0; } if (!isset($record['attachment'])) { $record['attachment'] = ""; } if (!isset($record['totalscore'])) { $record['totalscore'] = 0; } if (!isset($record['mailnow'])) { $record['mailnow'] = 0; } if (!isset($record['reveal'])) { $record['reveal'] = 0; } $record = (object) $record; // Add the post. $record->id = $DB->insert_record('hsuforum_posts', $record); // Update the last post. hsuforum_discussion_update_last_post($record->discussion); return $record; }
/** * Deletes a single forum post. * * @global object * @param object $post Forum post object * @param mixed $children Whether to delete children. If false, returns false * if there are any children (without deleting the post). If true, * recursively deletes all children. If set to special value 'ignore', deletes * post regardless of children (this is for use only when deleting all posts * in a disussion). * @param object $course Course * @param object $cm Course-module * @param object $forum Forum * @param bool $skipcompletion True to skip updating completion state if it * would otherwise be updated, i.e. when deleting entire forum anyway. * @return bool */ function hsuforum_delete_post($post, $children, $course, $cm, $forum, $skipcompletion = false) { global $DB, $CFG; require_once $CFG->libdir . '/completionlib.php'; $context = context_module::instance($cm->id); if ($children !== 'ignore' && ($childposts = $DB->get_records('hsuforum_posts', array('parent' => $post->id)))) { if ($children) { foreach ($childposts as $childpost) { hsuforum_delete_post($childpost, true, $course, $cm, $forum, $skipcompletion); } } else { return false; } } //delete ratings require_once $CFG->dirroot . '/rating/lib.php'; $delopt = new stdClass(); $delopt->contextid = $context->id; $delopt->component = 'mod_hsuforum'; $delopt->ratingarea = 'post'; $delopt->itemid = $post->id; $rm = new rating_manager(); $rm->delete_ratings($delopt); //delete attachments $fs = get_file_storage(); $fs->delete_area_files($context->id, 'mod_hsuforum', 'attachment', $post->id); $fs->delete_area_files($context->id, 'mod_hsuforum', 'post', $post->id); if ($DB->delete_records("hsuforum_posts", array("id" => $post->id))) { hsuforum_delete_read_records_for_post($post->id); // Just in case we are deleting the last post hsuforum_discussion_update_last_post($post->discussion); // Update completion state if we are tracking completion based on number of posts // But don't bother when deleting whole thing if (!$skipcompletion) { $completion = new completion_info($course); if ($completion->is_enabled($cm) == COMPLETION_TRACKING_AUTOMATIC && ($forum->completiondiscussions || $forum->completionreplies || $forum->completionposts)) { $completion->update_state($cm, COMPLETION_INCOMPLETE, $post->userid); } } return true; } return false; }