/** * 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 mod_forumng $targetforum Target forum for move * @param int $targetforumngid New forum ID * @param int $targetgroupid New group ID */ public function move($targetforum, $targetgroupid) { global $DB; $update = new StdClass(); if ($targetforum->get_id() != $this->discussionfields->forumngid) { $update->forumngid = $targetforum->get_id(); } if ($targetgroupid != $this->discussionfields->groupid) { $update->groupid = $targetgroupid; } if (count((array) $update) == 0) { // No change return; } // Delete search data for this discussion before moving. $this->ismakingsearchchange = true; $root = $this->get_root_post(); $root->search_update(); $root->search_update_children(); $this->ismakingsearchchange = false; $update->id = $this->discussionfields->id; $transaction = $DB->start_delegated_transaction(); $DB->update_record('forumng_discussions', $update); $targetcloneid = $targetforum->is_shared() ? $targetforum->get_course_module_id() : 0; $newdiscussion = self::get_from_id($this->get_id(), $targetcloneid, -1); if ($targetforum->get_id() != $this->forum->get_id()) { // Moving to different forum, we need to move attachments + ratings if any... // Get old and new contexts $fs = get_file_storage(); $filecontext = $this->get_forum()->get_context(true); $newfilecontext = $targetforum->get_context(true); // Get list of all affected post ids (includes edited, deleted) // that have attachments $postids = $DB->get_records('forumng_posts', array('discussionid' => $this->get_id()), '', 'id'); // Loop through all posts copying attachments & deleting old one foreach ($postids as $postid => $junk) { // Move core ratings if enabled (Note move to new forum even if not enabled there). if ($this->get_forum()->get_enableratings() == mod_forumng::FORUMNG_STANDARD_RATING) { $DB->set_field_select('rating', 'contextid', $newfilecontext->id, "itemid = {$postid} AND contextid = {$filecontext->id} AND component\n = 'mod_forumng' AND ratingarea = 'post'"); } foreach (array('attachment', 'message') as $filearea) { $oldfiles = $fs->get_area_files($filecontext->id, 'mod_forumng', $filearea, $postid, 'id', false); foreach ($oldfiles as $oldfile) { $filerecord = new stdClass(); $filerecord->contextid = $newfilecontext->id; $fs->create_file_from_storedfile($filerecord, $oldfile); $oldfile->delete(); } } } if ($this->get_forum()->get_enableratings() == mod_forumng::FORUMNG_STANDARD_RATING) { $this->forum->update_grades(); } if ($targetforum->get_enableratings() == mod_forumng::FORUMNG_STANDARD_RATING) { $targetforum->update_grades(); } // 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->update_completion(true); } } // Update the search data after the move. $newroot = $newdiscussion->get_root_post(); $newroot->search_update(); $newroot->search_update_children(); // Update the tags after the move. if ($targetforum->get_id() != $this->forum->get_id()) { // Update tags for moved discussions. $oldcontext = $this->forum->get_context(true); $newcontext = $targetforum->get_context(true); $DB->set_field('tag_instance', 'contextid', $newcontext->id, array('itemid' => $this->get_id(), 'itemtype' => 'forumng_discussions')); } $this->uncache(); $transaction->allow_commit(); }