function make_discussion($forum, $posts, $readusers, &$userids, $ratingpercent) { set_time_limit(200); // Make discussion static $index = 0; $index++; list($discussionid, $postid) = $forum->create_discussion(null, 'Discussion ' . $index, get_post_text(), FORMAT_HTML, array(), false, 0, 0, false, false, $userids[mt_rand(0, count($userids) - 1)], false); $discussion = forum_discussion::get_from_id($discussionid, forum::CLONE_DIRECT); // Make posts $count = my_random($posts) - 1; $allposts = array($discussion->get_root_post()); for ($i = 0; $i < $count; $i++) { make_post($discussion, $allposts, $userids, $ratingpercent); } // Mark the discussion read if requested if ($readusers > 0) { $now = time(); for ($i = 0; $i < $readusers; $i++) { $discussion->mark_read($now, $userids[$i]); } } // Progress print '.'; }
<?php require_once '../../../../config.php'; require_once $CFG->dirroot . '/mod/forumng/forum.php'; $d = required_param('d', PARAM_INT); $cloneid = optional_param('clone', 0, PARAM_INT); $delete = required_param('delete', PARAM_INT); try { $discussion = forum_discussion::get_from_id($d, $cloneid); $forum = $discussion->get_forum(); $cm = $forum->get_course_module(); $course = $forum->get_course(); // Check permission for change $discussion->require_edit(); // Is this the actual delete? if ($_SERVER['REQUEST_METHOD'] == 'POST') { if ($delete) { $discussion->delete(); redirect($forum->get_url(forum::PARAM_PLAIN)); } else { $discussion->undelete(); redirect('../../discuss.php?' . $discussion->get_link_params(forum::PARAM_PLAIN)); } } // Confirm page. Work out navigation for header $pagename = get_string($delete ? 'deletediscussion' : 'undeletediscussion', 'forumng', $discussion->get_subject(false)); $navigation = array(); $navigation[] = array('name' => shorten_text(htmlspecialchars($discussion->get_subject())), 'link' => $discussion->get_url(), 'type' => 'forumng'); $navigation[] = array('name' => $pagename, 'type' => 'forumng'); $PAGEWILLCALLSKIPMAINDESTINATION = true; print_header_simple(format_string($forum->get_name()) . ': ' . $pagename, "", build_navigation($navigation, $cm), "", "", true, '', navmenu($course, $cm));
$OUMOBILESUPPORT = true; ou_set_is_mobile(ou_get_is_mobile_from_cookies()); } require_once 'forum.php'; if (class_exists('ouflags')) { $DASHBOARD_COUNTER = DASHBOARD_FORUMNG_VIEW; } // Require discussion parameter here. Other parameters may be required in forum // type. $discussionid = required_param('d', PARAM_INT); $cloneid = optional_param('clone', 0, PARAM_INT); try { // Construct discussion variable (will check id is valid) // Retrieve new copy of discussion from database, but store it in cache // for further use. $discussion = forum_discussion::get_from_id($discussionid, $cloneid, 0, false, true); $forum = $discussion->get_forum(); $course = $forum->get_course(); $cm = $forum->get_course_module(); $context = $forum->get_context(); $draftid = optional_param('draft', 0, PARAM_INT); if ($draftid) { $draft = forum_draft::get_from_id($draftid); if (!$draft->is_reply() || $draft->get_discussion_id() != $discussionid) { print_error('draft_mismatch', 'forumng', $forum->get_url(forum::PARAM_HTML)); } $root = $discussion->get_root_post(); $inreplyto = $root->find_child($draft->get_parent_post_id(), false); if (!$inreplyto || !$inreplyto->can_reply($whynot) || !$discussion->can_view()) { print_error('draft_cannotreply', 'forumng', $forum->get_url(forum::PARAM_HTML), get_string($whynot, 'forumng')); }
/** * This function handles all aspects of page processing and then calls * methods in $selector at the appropriate moments. * @param post_selector $selector Object that extends this base class */ static function go($selector) { $d = required_param('d', PARAM_INT); $cloneid = optional_param('clone', 0, PARAM_INT); $fromselect = optional_param('fromselect', 0, PARAM_INT); $all = optional_param('all', '', PARAM_RAW); $select = optional_param('select', '', PARAM_RAW); try { // Get basic objects $discussion = forum_discussion::get_from_id($d, $cloneid); if (optional_param('cancel', '', PARAM_RAW)) { // CALL TYPE 6 redirect('../../discuss.php?' . $discussion->get_link_params(forum::PARAM_PLAIN)); } $forum = $discussion->get_forum(); $cm = $forum->get_course_module(); $course = $forum->get_course(); $isform = optional_param('postselectform', 0, PARAM_INT); // Page name and permissions $pagename = $selector->get_page_name(); $buttonname = $selector->get_button_name(); $discussion->require_view(); $selector->require_capability($forum->get_context(), $discussion); if (!($fromselect || $isform || $all)) { // Either an initial request (non-JS) to display the 'dialog' box, // or a request to show the list of posts with checkboxes for // selection // Both types share same navigation $discussion->print_subpage_header($pagename); if (!$select) { // Show initial dialog print_box_start(); ?> <h2><?php print $buttonname; ?> </h2> <form action="<?php echo $_SERVER['PHP_SELF']; ?> " method="get"><div> <?php print $discussion->get_link_params(forum::PARAM_FORM); ?> <p><?php print_string('selectorall', 'forumng'); ?> </p> <div class="forumng-buttons"> <input type="submit" name="all" value="<?php print_string('discussion', 'forumng'); ?> " /> <input type="submit" name="select" value="<?php print_string('selectedposts', 'forumng'); ?> " /> </div> </div></form> <?php print_box_end(); } else { // Show list of posts to select ?> <div class="forumng-selectintro"> <p><?php print_string('selectintro', 'forumng'); ?> </p> </div> <form action="<?php echo $_SERVER['PHP_SELF']; ?> " method="post"><div> <?php print $discussion->get_link_params(forum::PARAM_FORM); ?> <input type="hidden" name="fromselect" value="1" /> <?php print $forum->get_type()->display_discussion($discussion, array(forum_post::OPTION_NO_COMMANDS => true, forum_post::OPTION_CHILDREN_EXPANDED => true, forum_post::OPTION_SELECTABLE => true)); ?> <div class="forumng-selectoutro"> <input type="submit" value="<?php print_string('confirmselection', 'forumng'); ?> " /> <input type="submit" name="cancel" value="<?php print_string('cancel'); ?> " /> </div> </div></form> <?php } // Display footer print_footer($course); } else { // Call types 3, 4, and 5 use the form (and may include list of postids) if ($all) { $postids = false; } else { $postids = array(); foreach ($_POST as $field => $value) { $matches = array(); if ((string) $value !== '0' && preg_match('~^selectp([0-9]+)$~', $field, $matches)) { $postids[] = $matches[1]; } } } // Get form to use $mform = $selector->get_form($discussion, $all, $postids); if (!$mform) { // Some options do not need a confirmation form; in that case, // just apply the action immediately. $selector->apply($discussion, $all, $postids, null); exit; } // Check cancel if ($mform->is_cancelled()) { redirect('../../discuss.php?' . $discussion->get_link_params(forum::PARAM_PLAIN)); } if ($fromform = $mform->get_data()) { // User submitted form to confirm process, which should now be // applied by selector. $selector->apply($discussion, $all, $postids, $fromform); exit; } else { $discussion->print_subpage_header($pagename); // User requested form either via JavaScript or the other way, and // either with all messages or the whole discussion. // Print form print $mform->display(); // Print optional content that goes after form print $selector->get_content_after_form($discussion, $all, $postids, $fromform); // Display footer print_footer($course); } } } catch (forum_exception $e) { forum_utils::handle_exception($e); } }
/** * Either delete or archive old discussions based on the forum setting */ static function archive_old_discussions() { global $CFG; $now = time(); $housekeepingquery = " \nFROM \n {$CFG->prefix}forumng_discussions fd\n INNER JOIN {$CFG->prefix}forumng_posts fp ON fd.lastpostid = fp.id\n INNER JOIN {$CFG->prefix}forumng f ON fd.forumid = f.id\nWHERE\n f.removeafter<>0 AND fd.sticky<>1 AND fp.modified<{$now} - f.removeafter \n"; $count = forum_utils::count_records_sql("SELECT COUNT(1) {$housekeepingquery}"); if ($count) { mtrace("\nBeginning processing {$count} discussion archiving/deleting requests"); $housekeepingrs = forum_utils::get_recordset_sql("\nSELECT \n fd.id AS discussionid, f.id AS forumid, f.removeafter, f.removeto {$housekeepingquery} ORDER BY f.removeto\n "); $targetforum = null; $targetcourseid = null; $cron_log = ''; $discussionmovecount = 0; $discussiondeletecount = 0; while ($rec = rs_fetch_next_record($housekeepingrs)) { $discussion = forum_discussion::get_from_id($rec->discussionid, forum::CLONE_DIRECT); if ($rec->removeto) { //moving to a different forum $forum = $discussion->get_forum(); $course = $forum->get_course(); $modinfo = get_fast_modinfo($course); if ($forum->can_archive_forum($modinfo, $cron_log)) { //Do not get the target forum and course id again if the target forum is the same if (!$targetforum || $targetforum->get_id() != $rec->removeto) { $targetforum = forum::get_from_id($rec->removeto, forum::CLONE_DIRECT); $targetforum = $targetforum->get_real_forum(); } //target discussion groupid must be the same as the original groupid $targetgroupmode = $targetforum->get_group_mode(); $targetgroupid = $targetgroupmode ? $discussion->get_group_id() : null; $discussion->move($targetforum, $targetgroupid); $discussionmovecount++; } } else { //Delete all discussions and relevant data permanently $discussion->permanently_delete(); $discussiondeletecount++; } } rs_close($housekeepingrs); mtrace("\n {$discussionmovecount} discussions have been archived and {$discussiondeletecount} discussions have been deleted permanently."); } }
<?php // Scripts for paste the discussion or cancel the paste. require_once '../../../../config.php'; require_once $CFG->dirroot . '/mod/forumng/forum.php'; $cmid = required_param('cmid', PARAM_INT); $groupid = optional_param('group', forum::NO_GROUPS, PARAM_INT); $cloneid = optional_param('clone', 0, PARAM_INT); try { $targetforum = forum::get_from_cmid($cmid, $cloneid); if (optional_param('cancel', '', PARAM_RAW)) { unset($SESSION->forumng_copyfrom); redirect($targetforum->get_url(forum::PARAM_PLAIN)); } //If the paste action has already been done or cancelled in a different window/tab if (!isset($SESSION->forumng_copyfrom)) { redirect($targetforum->get_url(forum::PARAM_PLAIN)); } $olddiscussionid = $SESSION->forumng_copyfrom; $oldcloneid = $SESSION->forumng_copyfromclone; $olddiscussion = forum_discussion::get_from_id($olddiscussionid, $oldcloneid); // Check permission to copy the discussion require_capability('mod/forumng:copydiscussion', $olddiscussion->get_forum()->get_context()); //security check to see if can start a new discussion in the target forum $targetforum->require_start_discussion($groupid); $olddiscussion->copy($targetforum, $groupid); unset($SESSION->forumng_copyfrom); redirect($targetforum->get_url(forum::PARAM_PLAIN)); } catch (forum_exception $e) { forum_utils::handle_exception($e); }
/** * Moves discussion to another forum. This will also move any attachments * in the filesystem. You can also use this method to change group. * (Note that once a discussion has been moved its data fields are no longer * valid and the object should be discarded.) * @param forum $targetforum Target forum for move * @param int $targetforumid New forum ID * @param int $targetgroupid New group ID */ public function move($targetforum, $targetgroupid) { $update = new StdClass(); if ($targetforum->get_id() != $this->discussionfields->forumid) { $update->forumid = $targetforum->get_id(); } if ($targetgroupid != $this->discussionfields->groupid) { $update->groupid = $targetgroupid; } if (count((array) $update) == 0) { // No change return; } $update->id = $this->discussionfields->id; forum_utils::start_transaction(); forum_utils::update_record('forumng_discussions', $update); if ($targetforum->get_id() != $this->forum->get_id()) { // Moving to different forum, we need to move attachments if any. $folder = $this->get_attachment_folder(); if (is_dir($folder)) { $targetfolder = $this->get_attachment_folder($targetforum->get_course_id(), $targetforum->get_id()); check_dir_exists(dirname($targetfolder), true, true); forum_utils::rename($folder, $targetfolder); } // Completion status may have changed in source and target forums // Performance optimise: only do this if completion is enabled if ($this->forum->is_auto_completion_enabled()) { $this->update_completion(false); $newdiscussion = forum_discussion::get_from_id($this->get_id(), $this->get_forum()->get_course_module_id(), -1); $newdiscussion->update_completion(true); } } $this->uncache(); forum_utils::finish_transaction(); }
/** * Update all documents for ousearch. * @param bool $feedback If true, prints feedback as HTML list items * @param int $courseid If specified, restricts to particular courseid * @param int $cmid If specified, restricts to particular cmid */ static function search_update_all($feedback = false, $courseid = 0, $cmid = 0) { global $CFG; // If cmid is specified, only retrieve that one if ($cmid) { $cmrestrict = "cm.id = {$cmid} AND"; } else { $cmrestrict = ''; } // Get module-instances that need updating $cms = get_records_sql("\nSELECT\n cm.id, cm.course, cm.instance, f.name\nFROM\n {$CFG->prefix}forumng f\n INNER JOIN {$CFG->prefix}course_modules cm ON cm.instance=f.id\nWHERE\n {$cmrestrict}\n cm.module = (SELECT id FROM {$CFG->prefix}modules m WHERE name='forumng')" . ($courseid ? " AND f.course={$courseid}" : '')); $cms = $cms ? $cms : array(); // Print count if ($feedback && !$cmid) { print '<li>' . get_string('search_update_count', 'forumng', '<strong>' . count($cms) . '</strong>') . '</li>'; } // This can take a while, so let's be sure to reset the time limit. // Store the existing limit; we will set this existing value again // each time around the loop. Note: Despite the name, ini_get returns // the most recently set time limit, not the one from php.ini. $timelimitbefore = ini_get('max_execution_time'); // Loop around updating foreach ($cms as $cm) { forum_utils::start_transaction(); // Wipe existing search data, if any ousearch_document::delete_module_instance_data($cm); // Get all discussions for this forum $discussions = get_records('forumng_discussions', 'forumid', $cm->instance, '', 'id, postid'); $discussions = $discussions ? $discussions : array(); if ($feedback) { print '<li><strong>' . $cm->name . '</strong> (' . count($discussions) . '):'; } // Process each discussion foreach ($discussions as $discussionrec) { // Ignore discussion with no postid // (This should not happen, where ther is a $discussionrec->id // it also shopuld have a $discussionrec->postid. This if-statement // fixes bug 10497 and would not have any side-effect.) if (!$discussionrec->postid) { continue; } set_time_limit($timelimitbefore); $discussion = forum_discussion::get_from_id($discussionrec->id, forum::CLONE_DIRECT, -1); $root = $discussion->get_root_post(); $root->search_update(); $root->search_update_children(); print '. '; flush(); } forum_utils::finish_transaction(); if ($feedback) { print '</li>'; } } }
$discussion = forum_discussion::get_from_id($d, $cloneid); $forum = $discussion->get_forum(); $cm = $forum->get_course_module(); $course = $forum->get_course(); // Require that you can see this discussion (etc) and merge them $discussion->require_view(); if (!$discussion->can_split($whynot)) { print_error($whynot, 'forumng'); } if ($stage == 2) { if (!confirm_sesskey()) { print_error('invalidsesskey'); } if (!isset($_POST['cancel'])) { // Get source discussion and check permissions $sourcediscussion = forum_discussion::get_from_id($SESSION->forumng_mergefrom, $SESSION->forumng_mergefromclone); $sourcediscussion->require_view(); if (!$sourcediscussion->can_split($whynot)) { print_error($whynot, 'forumng'); } // Do actual merge $sourcediscussion->merge_into($discussion); } unset($SESSION->forumng_mergefrom); redirect('../../discuss.php?' . $discussion->get_link_params(forum::PARAM_PLAIN)); } // Create form require_once 'merge_form.php'; $mform = new mod_forumng_merge_form('merge.php', array('d' => $d, 'clone' => $cloneid)); if ($mform->is_cancelled()) { redirect('../../discuss.php?' . $discussion->get_link_params(forum::PARAM_PLAIN));
/** * Calls search_update on each child of the current post, and recurses. * Used when the subject's discussion is changed. */ function search_update_children() { if (!forum::search_installed()) { return; } // If the in-memory post object isn't already part of a full // discussion... if (!is_array($this->children)) { // ...then get one $discussion = forum_discussion::get_from_id($this->discussion->get_id(), $this->get_forum()->get_course_module_id()); $post = $discussion->get_root_post()->find_child($this->get_id()); // Do this update on the new discussion $post->search_update_children(); return; } // Loop through all children foreach ($this->children as $child) { // Update its search fields $child->search_update(); // Recurse $child->search_update_children(); } }