/**
  * Display intro section for forum.
  * @param forum $forum Forum
  * @return string Intro HTML or '' if none
  */
 public function display_intro($forum)
 {
     $text = $forum->get_intro();
     if (trim($text) === '') {
         return '';
     }
     $options = (object) array('trusttext' => true);
     return '<div class="forumng-intro">' . format_text($text, FORMAT_HTML, $options, $forum->get_course_id()) . '</div>';
 }
 /**
  * Copy the discussion and its  posts to another forum and/or group.
  * @param forum $targetforum Forum to copy the discussion to
  * @param int $groupid If 'All participants' has been selected from the
  *   Separate groups dropdown box, use default value 0
  */
 function copy($targetforum, $groupid)
 {
     global $SESSION, $CFG;
     $oldforum = $this->get_forum();
     $oldforumid = $oldforum->get_id();
     $oldcourseid = $oldforum->get_course_id();
     $targetforumid = $targetforum->get_id();
     $targetcourseid = $targetforum->get_course_id();
     //Clone the old discussion
     $discussionobj = clone $this->discussionfields;
     unset($discussionobj->id);
     //update the forumid and gruopid to the target forumid and selected groupid
     $discussionobj->forumid = $targetforumid;
     unset($discussionobj->groupid);
     if ($targetforum->get_group_mode() && $groupid) {
         $discussionobj->groupid = $groupid;
     }
     forum_utils::start_transaction();
     $newdiscussionid = forum_utils::insert_record('forumng_discussions', $discussionobj);
     $rs = forum_utils::get_recordset('forumng_posts', 'discussionid', $this->get_id());
     //$newids and $parentused are temp arrays used to
     //$newids is a array of new postids using the indices of its old postids
     //update the parentid of the post records copied over
     //$hasattachments is a temp array for record the posts which has attachments.
     $newids = array();
     $parentsused = array();
     $hasattachments = array();
     while ($postrec = rs_fetch_next_record($rs)) {
         $oldpostid = $postrec->id;
         unset($postrec->id);
         $postrec->discussionid = $newdiscussionid;
         $postrec->mailstate = forum::MAILSTATE_DIGESTED;
         $postrec->subject = addslashes($postrec->subject);
         $postrec->message = addslashes($postrec->message);
         $newpostid = forum_utils::insert_record('forumng_posts', $postrec);
         $newids[$oldpostid] = $newpostid;
         if ($postrec->parentpostid) {
             $parentsused[$postrec->parentpostid] = true;
         }
         if ($postrec->attachments == 1) {
             $hasattachments[$oldpostid] = $newpostid;
         }
     }
     rs_close($rs);
     //Update the postid and lastpostid in the discussion table no matter if they are null or not
     $newpostid = $newids[$discussionobj->postid];
     $newlastpostid = $newids[$discussionobj->lastpostid];
     forum_utils::execute_sql("UPDATE {$CFG->prefix}forumng_discussions SET " . "postid=" . $newpostid . ", lastpostid=" . $newlastpostid . " WHERE id=" . $newdiscussionid);
     foreach ($parentsused as $key => $value) {
         $newparentpostid = $newids[$key];
         //Update the parentpostids which have just been copied over
         forum_utils::execute_sql("UPDATE {$CFG->prefix}forumng_posts SET " . "parentpostid=" . $newparentpostid . " WHERE parentpostid=" . $key . "AND discussionid = " . $newdiscussionid);
     }
     //Copy attachments
     foreach ($hasattachments as $key => $value) {
         $oldfolder = forum_post::get_any_attachment_folder($oldcourseid, $oldforumid, $this->get_id(), $key);
         $newfolder = forum_post::get_any_attachment_folder($targetcourseid, $targetforumid, $newdiscussionid, $value);
         $handle = forum_utils::opendir($oldfolder);
         $created = false;
         while (false !== ($name = readdir($handle))) {
             //Get attachment file name one by one instead of using the get_attachment_names() to
             //avoid creating a post object
             if ($name != '.' && $name != '..') {
                 if (!is_dir("{$oldfolder}/{$name}")) {
                     // creating target folders and copying files
                     if (!$created) {
                         if (!check_dir_exists($newfolder, true, true)) {
                             throw new forum_exception("Failed to create attachment folder {$newfolder}");
                         }
                         $created = true;
                     }
                     forum_utils::copy("{$oldfolder}/{$name}", "{$newfolder}/{$name}");
                 }
             }
         }
         closedir($handle);
     }
     forum_utils::finish_transaction();
 }