]> git.mjollnir.org Git - moodle.git/commitdiff
migrated mod/forum/post.php to formslib and fixed a string in course/edit_form.php
authorjamiesensei <jamiesensei>
Tue, 7 Nov 2006 08:48:18 +0000 (08:48 +0000)
committerjamiesensei <jamiesensei>
Tue, 7 Nov 2006 08:48:18 +0000 (08:48 +0000)
course/edit_form.php
lang/en_utf8/form.php
lib/form/format.php
lib/formslib.php
mod/forum/lib.php
mod/forum/post.php

index 1729bddd8ee4b17350aa0aaaf71e4ca78ec3a1eb..b37ec1c2718ba91655e255f3b04b0f7239d800a4 100644 (file)
@@ -42,7 +42,7 @@ class course_edit_form extends moodleform {
 
 /// form definition with new course defaults
 //--------------------------------------------------------------------------------
-        $mform->addElement('header','general', get_string('general'));
+        $mform->addElement('header','general', get_string('general', 'form'));
 
         //must have create course capability in both categories in order to move course
         if (has_capability('moodle/course:create', $categorycontext)) {
index 00387bff8b8dca865385d7ab2f63a360ac58745c..62d16eb71c3d30375c446b72d8c02d39fa6e4830 100644 (file)
@@ -1,4 +1,4 @@
-<?PHP // $Id$ 
+<?PHP // $Id$
       // form.php - created with Moodle 1.7 beta + (2006101003)
 
 
@@ -16,5 +16,6 @@ $string['err_required']='You must supply a value here.';
 $string['nomethodforaddinghelpbutton'] = 'There is no method for adding a help button to form element $a->name (class $a->classname)';
 $string['nonexistentformelements'] = 'Trying to add help buttons to nonexistent form elements : $a';
 $string['requiredelement'] = 'Required field.';
+$string['general'] = 'General Settings';
 
 ?>
index 53cc03941548d47f44ed986c5550f1f8c7b1176b..cdf6d9d21d140b519b0371aefe0dc19b070e0a27 100644 (file)
@@ -32,7 +32,6 @@ class MoodleQuickForm_format extends MoodleQuickForm_select{
         HTML_QuickForm_element::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
         $this->_persistantFreeze = true;
         $this->_type = 'format';
-        echo $useHtmlEditor;
         $this->_useHtmlEditor=$useHtmlEditor;
         if ($this->_useHtmlEditor==null){
             $this->_useHtmlEditor=can_use_html_editor();
@@ -74,14 +73,15 @@ class MoodleQuickForm_format extends MoodleQuickForm_select{
                     }
                 }
                 if (null !== $value) {
-                    $this->setValue($value);
+                    $format=$value;
                 }else{
-                    if ($this->_useHtmlEditor){
-                        $this->setValue(array(FORMAT_HTML));
-
-                    }else{
-                        $this->setValue(array(FORMAT_MOODLE));
-                    }
+                    $format=FORMAT_MOODLE;
+                }
+                $caller->setFormat($this->getName(), $format, $this->_useHtmlEditor);
+                if ($this->_useHtmlEditor){
+                    $this->setValue(array(FORMAT_HTML));
+                }else{
+                    $this->setValue(array($format));
                 }
                 return true;
                 break;
index af2ebda8108bbec4c5b98641ba30013a8d14d994..0dcfea0cc40a846e7c0ff4308bd6774a581e283c 100644 (file)
@@ -131,6 +131,21 @@ class moodleform {
         }
     }
 
+    /**
+     * Called after setting up defaults for form and before displaying it.
+     *
+     * @param string $elementname
+     * @param string $formatname
+     * @param object $context
+     */
+    function trusttext_prepare_edit($elementname, $formatname, $context) {
+        $defaultvalue=$this->_form->_defaultValues[$elementname];
+        extract($this->_form->_formats[$formatname]);// format and usehtml
+        trusttext_prepare_edit($defaultvalue, $format,
+                         $usehtml, $context);
+        $this->_form->setDefault($elementname, $defaultvalue);
+    }
+
     function display() {
         $this->_form->display();
     }
@@ -160,6 +175,7 @@ class moodleform {
 
 class MoodleQuickForm extends HTML_QuickForm_DHTMLRulesTableless {
     var $_types = array();
+    var $_formats = array();
 
 
     /**
@@ -195,7 +211,9 @@ class MoodleQuickForm extends HTML_QuickForm_DHTMLRulesTableless {
                     $this->_helpImageURL.'" />');
         $this->setRequiredNote(get_string('denotesreq', 'form', $this->getReqHTML()));
     }
-
+    function setFormat($elementname, $format, $usehtml) {
+        $this->_formats[$elementname]=compact('format', 'usehtml');
+    }
     function setType($elementname, $paramtype) {
         $this->_types[$elementname] = $paramtype;
     }
index 6d9097c58a82a2ebcda2517596873f442fee23af..395f9278cc6f4b0d83e2de0f19443bccd45f4010 100644 (file)
@@ -99,7 +99,7 @@ function forum_add_instance($forum) {
     if (!isset($forum->assessed)) {
         $forum->assessed = 0;
     }
-    
+
     if (!empty($forum->ratingtime)) {
         $forum->assesstimestart  = make_timestamp($forum->startyear, $forum->startmonth, $forum->startday,
                                                   $forum->starthour, $forum->startminute, 0);
@@ -150,7 +150,7 @@ function forum_update_instance($forum) {
     if (empty($forum->assessed)) {
         $forum->assessed = 0;
     }
-    
+
     if (!empty($forum->ratingtime)) {
         $forum->assesstimestart  = make_timestamp($forum->startyear, $forum->startmonth, $forum->startday,
                                                   $forum->starthour, $forum->startminute, 0);
@@ -339,9 +339,9 @@ function forum_cron() {
                     }
                 }
             }
-            
+
             $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);   // Cached already
-            
+
             if ($users = forum_subscribed_users($course, $forum, 0, true)) {
 
                 $mailcount=0;
@@ -356,7 +356,7 @@ function forum_cron() {
                             }
                         }
                     }
-                    
+
                     // make sure we're allowed to see it...
                     if (!forum_user_can_see_post($forum, $discussion, $post, $userto)) {
                         continue;
@@ -397,7 +397,7 @@ function forum_cron() {
                         $mailcount++;
 
                     /// Mark post as read if forum_usermarksread is set off
-                        if (!$CFG->forum_usermarksread && forum_tp_can_track_forums($forum, $userto) && 
+                        if (!$CFG->forum_usermarksread && forum_tp_can_track_forums($forum, $userto) &&
                             forum_tp_is_tracked($forum, $userto->id)) {
                             if (!forum_tp_mark_post_read($userto->id, $post, $forum->id)) {
                                 mtrace("Error: mod/forum/cron.php: Could not mark post $post->id read for user $userto->id".
@@ -575,8 +575,8 @@ function forum_cron() {
                             $posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, $canreply, true, false);
 
                         /// Create an array of postid's for this user to mark as read.
-                            if (!$CFG->forum_usermarksread && 
-                                forum_tp_can_track_forums($forum, $userto) && 
+                            if (!$CFG->forum_usermarksread &&
+                                forum_tp_can_track_forums($forum, $userto) &&
                                 forum_tp_is_tracked($forum, $userto->id)) {
                                 $markread[$post->id]->post = $post;
                                 $markread[$post->id]->forumid = $forum->id;
@@ -609,8 +609,8 @@ function forum_cron() {
                     $usermailcount++;
 
                 /// Mark post as read if forum_usermarksread is set off
-                    if (!$CFG->forum_usermarksread && 
-                        forum_tp_can_track_forums($forum->id, $userto) && 
+                    if (!$CFG->forum_usermarksread &&
+                        forum_tp_can_track_forums($forum->id, $userto) &&
                         forum_tp_is_tracked($forum->id, $userto->id)) {
                         foreach ($markread as $postinfo) {
                             if (!forum_tp_mark_post_read($userto->id, $postinfo->post, $postinfo->forumid)) {
@@ -648,13 +648,13 @@ function forum_cron() {
 
 function forum_make_mail_text($course, $forum, $discussion, $post, $userfrom, $userto, $bare = false) {
     global $CFG;
-    
+
     if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) {
         error('Course Module ID was incorrect');
     }
     $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
     $viewfullnames = has_capability('moodle/site:viewfullnames', $modcontext);
-    
+
     $by = New stdClass;
     $by->name = fullname($userfrom, $viewfullnames);
     $by->date = userdate($post->modified, "", $userto->timezone);
@@ -796,11 +796,11 @@ function forum_print_overview($courses,&$htmlarray) {
 
     $sql .= ") AND l.module = 'forum' AND action $LIKE 'add post%' "
         ." AND userid != ".$USER->id." GROUP BY cmid,l.course,instance";
-    
+
     if (!$new = get_records_sql($sql)) {
         $new = array(); // avoid warnings
     }
-    
+
     // also get all forum tracking stuff ONCE.
     $trackingforums = array();
     foreach ($forums as $forum) {
@@ -808,7 +808,7 @@ function forum_print_overview($courses,&$htmlarray) {
             $trackingforums[$forum->id] = $forum;
         }
     }
-    
+
     if (count($trackingforums) > 0) {
         $cutoffdate = isset($CFG->forum_oldpostdays) ? (time() - ($CFG->forum_oldpostdays*24*60*60)) : 0;
         $sql = 'SELECT d.forum,d.course,COUNT(p.id) AS count '.
@@ -859,7 +859,7 @@ function forum_print_overview($courses,&$htmlarray) {
             }
             $str .= '</div></div>';
         }
-        if (!empty($str)) { 
+        if (!empty($str)) {
             if (!array_key_exists($forum->course,$htmlarray)) {
                 $htmlarray[$forum->course] = array();
             }
@@ -868,7 +868,7 @@ function forum_print_overview($courses,&$htmlarray) {
             }
             $htmlarray[$forum->course]['forum'] .= $str;
         }
-    }    
+    }
 }
 
 function forum_print_recent_activity($course, $isteacher, $timestart) {
@@ -919,7 +919,7 @@ function forum_print_recent_activity($course, $isteacher, $timestart) {
                     && $post->groupid != -1) {   /// Open discussions have groupid -1
 
                 $groupmode[$post->forum] = groupmode($course, $cm[$post->forum]);
-                
+
                 if ($groupmode[$post->forum]) {
                     //hope i didn't break anything
                     if (!@in_array($mygroupid, $post->groupid))/*$mygroupid != $post->groupid*/{
@@ -1096,7 +1096,7 @@ function forum_get_post_full($postid) {
 
 function forum_get_discussion_posts($discussion, $sort, $forumid) {
 /// Gets posts with all info ready for forum_print_post
-/// We pass forumid in because we always know it so no need to make a 
+/// We pass forumid in because we always know it so no need to make a
 /// complicated join to find it out.
     global $CFG;
 
@@ -1109,7 +1109,7 @@ function forum_get_discussion_posts($discussion, $sort, $forumid) {
 
 function forum_get_child_posts($parent, $forumid) {
 /// Gets posts with all info ready for forum_print_post
-/// We pass forumid in because we always know it so no need to make a 
+/// We pass forumid in because we always know it so no need to make a
 /// complicated join to find it out.
     global $CFG;
 
@@ -1130,13 +1130,13 @@ function forum_get_child_posts($parent, $forumid) {
  *         viewhiddentimedposts
  */
 function forum_get_readable_forums($userid, $courseid=0) {
-    
+
     global $CFG, $USER;
-    
+
     if (!$forummod = get_record('modules', 'name', 'forum')) {
         error('The forum module is not installed');
     }
-    
+
     if ($courseid) {
         $courses = get_records('course', 'id', $courseid);
     } else {
@@ -1147,16 +1147,16 @@ function forum_get_readable_forums($userid, $courseid=0) {
     }
 
     $readableforums = array();
-    
+
     foreach($courses as $course) {
         $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
-        
+
         if (has_capability('moodle/course:viewhiddenactivities', $coursecontext)) {
             $selecthidden = ' AND cm.visible = 1';
         } else {
             $selecthidden = '';
         }
-        
+
         $selectforums = "SELECT f.id AS id,
                                 f.name AS name,
                                 f.type AS type,
@@ -1171,20 +1171,20 @@ function forum_get_readable_forums($userid, $courseid=0) {
                             AND cm.module = {$forummod->id}
                                 $selecthidden
                                 ORDER BY f.name ASC";
-        
+
         if ($forums = get_records_sql($selectforums)) {
-            
+
             $group = user_group($course->id, $userid);
-            
+
             foreach ($forums as $forum) {
                 $forumcontext = get_context_instance(CONTEXT_MODULE, $forum->cmid);
-            
+
                 // Evaluate groupmode.
                 $cm = new object;
                 $cm->id = $forum->cmid;
                 $cm->groupmode = $forum->cmgroupmode;
                 $forum->cmgroupmode = groupmode($course, $cm);
-                
+
                 if ($forum->cmgroupmode == SEPARATEGROUPS
                         && !has_capability('moodle/site:accessallgroups', $forumcontext)) {
                     $forum->accessallgroups = false;
@@ -1198,14 +1198,14 @@ function forum_get_readable_forums($userid, $courseid=0) {
 
                     $forum->viewhiddentimedposts
                         = has_capability('mod/forum:viewhiddentimedposts', $forumcontext);
-                    
+
                     if ($forum->type == 'qanda'
                             && !has_capability('mod/forum:viewqandawithoutposting', $forumcontext)) {
 
                         // We need to check whether the user has posted in the qanda forum.
                         $forum->onlydiscussions = array();  // Holds discussion ids for the discussions
                                                             // the user is allowed to see in this forum.
-                        
+
                         if ($discussionspostedin =
                                     forum_discussions_user_has_posted_in($forum->id, $USER->id)) {
                             foreach ($discussionspostedin as $d) {
@@ -1218,10 +1218,10 @@ function forum_get_readable_forums($userid, $courseid=0) {
             }
         }
     } // End foreach $courses
-    
+
     //print_object($courses);
     //print_object($readableforums);
-    
+
     return $readableforums;
 }
 
@@ -1235,13 +1235,13 @@ function forum_get_readable_forums($userid, $courseid=0) {
  * @param $extrasql
  * @return array of posts found
  */
-function forum_search_posts($searchterms, $courseid=0, $limitfrom=0, $limitnum=50, 
+function forum_search_posts($searchterms, $courseid=0, $limitfrom=0, $limitnum=50,
                             &$totalcount, $extrasql='') {
     global $CFG, $USER;
     require_once($CFG->libdir.'/searchlib.php');
 
     $forums = forum_get_readable_forums($USER->id, $courseid);
-    
+
     if (count($forums) == 0) {
         return false;
     }
@@ -1298,7 +1298,7 @@ function forum_search_posts($searchterms, $courseid=0, $limitfrom=0, $limitnum=5
 
     $messagesearch = '';
     $searchstring = '';
-    
+
     // Need to concat these back together for parser to work.
     foreach($searchterms as $searchterm){
         if ($searchstring != '') {
@@ -1425,7 +1425,7 @@ function forum_get_post_from_log($log) {
 
     if ($log->action == "add post") {
 
-        return get_record_sql("SELECT p.*, f.type AS forumtype, d.forum, d.groupid, 
+        return get_record_sql("SELECT p.*, f.type AS forumtype, d.forum, d.groupid,
                                            u.firstname, u.lastname, u.email, u.picture
                                  FROM {$CFG->prefix}forum_discussions d,
                                       {$CFG->prefix}forum_posts p,
@@ -1440,7 +1440,7 @@ function forum_get_post_from_log($log) {
 
     } else if ($log->action == "add discussion") {
 
-        return get_record_sql("SELECT p.*, f.type AS forumtype, d.forum, d.groupid, 
+        return get_record_sql("SELECT p.*, f.type AS forumtype, d.forum, d.groupid,
                                            u.firstname, u.lastname, u.email, u.picture
                                  FROM {$CFG->prefix}forum_discussions d,
                                       {$CFG->prefix}forum_posts p,
@@ -1542,12 +1542,12 @@ function forum_get_discussions($forum="0", $forumsort="d.timemodified DESC",
     $timelimit = '';
 
     if (!empty($CFG->forum_enabletimedposts)) {
-        
+
         if (!$cm = get_coursemodule_from_instance('forum', $forum)) {
             error('Course Module ID was incorrect');
         }
         $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
-        
+
         if (!has_capability('mod/forum:viewhiddentimedposts', $modcontext)) {
             $now = time();
             $timelimit = " AND ((d.timestart = 0 OR d.timestart <= '$now') AND (d.timeend = 0 OR d.timeend > '$now')";
@@ -1594,7 +1594,7 @@ function forum_get_discussions($forum="0", $forumsort="d.timemodified DESC",
     }
 
     //TODO: there must be a nice way to do this that keeps both postgres and mysql 3.2x happy but I can't find it right now.
-    if ($CFG->dbtype == 'postgres7' || $CFG->dbtype == 'mssql' || 
+    if ($CFG->dbtype == 'postgres7' || $CFG->dbtype == 'mssql' ||
         $CFG->dbtype == 'mssql_n' || $CFG->dbtype == 'odbc_mssql' || $CFG->dbtype == 'oci8po') {
         return get_records_sql("SELECT $postdata, d.name, d.timemodified, d.usermodified, d.groupid,
                                    u.firstname, u.lastname, u.email, u.picture $umfields
@@ -1604,7 +1604,7 @@ function forum_get_discussions($forum="0", $forumsort="d.timemodified DESC",
                                    $umtable
                              WHERE d.forum = '$forum'
                                AND p.parent = 0
-                                   $timelimit $groupselect $userselect 
+                                   $timelimit $groupselect $userselect
                           ORDER BY $forumsort", $limitfrom, $limitnum);
     } else {
         return get_records_sql("SELECT $postdata, d.name, d.timemodified, d.usermodified, d.groupid,
@@ -1685,7 +1685,7 @@ function forum_subscribed_users($course, $forum, $groupid=0, $cache=false) {
     }
 
     return $results;
-    
+
 }
 
 
@@ -1763,7 +1763,7 @@ function forum_get_course_forum($courseid, $type) {
 
 function forum_make_mail_post(&$post, $user, $touser, $course,
                               $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
-    
+
     // Given the data about a posting, builds up the HTML to display it and
     // returns the HTML in a string.  This is designed for sending via HTML email.
 
@@ -1773,7 +1773,7 @@ function forum_make_mail_post(&$post, $user, $touser, $course,
     static $formattedtextid;      // The ID number of the post
 
     $post->forum = get_field('forum_discussions', 'forum', 'id', $post->discussion);
-    
+
     if (!$cm = get_coursemodule_from_instance('forum', $post->forum)) {
         mtrace('Course Module ID was incorrect');
     }
@@ -1851,7 +1851,7 @@ function forum_make_mail_post(&$post, $user, $touser, $course,
     if ($link) {
         $output .= '<div class="link">';
         $output .= '<a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#'.$post->id.'">'.
-                     get_string('postincontext', 'forum').'</a>';   
+                     get_string('postincontext', 'forum').'</a>';
         $output .= '</div>';
     }
 
@@ -1885,7 +1885,7 @@ function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link
         if (empty($SESSION->forum_search)) {
             // just viewing, return
             return;
-        } 
+        }
         echo '<a name="'.$post->id.'"></a>';
         echo '<table cellspacing="0" class="forumpost">';
         echo '<tr class="header"><td class="picture left">';
@@ -1900,12 +1900,12 @@ function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link
         echo '<div class="author">';
         print_string('forumauthorhidden','forum');
         echo '</div></td></tr>';
-        
+
         echo '<tr><td class="left side">';
         echo '&nbsp;';
-        
+
         /// Actual content
-        
+
         echo '</td><td class="content">'."\n";
         echo get_string('forumbodyhidden','forum');
         echo '</td></tr></table>';
@@ -1924,7 +1924,7 @@ function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link
         $strmarkunread = get_string('markunread', 'forum');
 
         if (!empty($post->forum)) {
-            $istracked = (forum_tp_can_track_forums($post->forum) && 
+            $istracked = (forum_tp_can_track_forums($post->forum) &&
                           forum_tp_is_tracked($post->forum));
         } else {
             $istracked = false;
@@ -2049,7 +2049,7 @@ function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link
     }
 
     $forumtype = get_field('forum', 'type', 'id', $post->forum);
-    
+
     $age = time() - $post->created;
     /// Hack for allow to edit news posts those are not displayed yet until they are displayed
     if (!$post->parent
@@ -2098,10 +2098,10 @@ function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link
         }
         if ($useratings) {
             $mypost = ($USER->id == $post->userid);
-            
+
             $canviewallratings = has_capability('mod/forum:viewanyrating', $modcontext);
-            
-            if ($canviewallratings and !$mypost) {    
+
+            if ($canviewallratings and !$mypost) {
                 forum_print_ratings_mean($post->id, $ratings->scale, $canviewallratings);
                 if (!empty($ratings->allow)) {
                     echo '&nbsp;';
@@ -2152,7 +2152,7 @@ function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link
 * It needs some discussion information and some post information, these
 * happen to be combined for efficiency in the $post parameter by the function
 * that calls this one: forum_print_latest_discussions()
-* 
+*
 * @param object $post The post object (passed by reference for speed).
 * @param object $forum The forum object.
 * @param int $group Current group.
@@ -2168,12 +2168,12 @@ function forum_print_discussion_header(&$post, $forum, $group=-1, $datestring=""
     static $rowcount;
     static $strmarkalldread;
 
-    
+
     if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course)) {
         error('Course Module ID was incorrect');
     }
     $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
-    
+
 
     if (!isset($rowcount)) {
         $rowcount = 0;
@@ -2875,7 +2875,9 @@ function forum_post_subscription($post) {
 
     global $USER;
 
-    if (empty($post->subscribe) and empty($post->unsubscribe)) {
+    $subscribed=forum_is_subscribed($USER->id, $post->forum);
+    if ((isset($post->subscribe) && $post->subscribe && $subscribed)
+        || (!$post->subscribe && !$subscribed)) {
         return "";
     }
 
@@ -2985,11 +2987,11 @@ function forum_user_can_post($forum, $user=NULL) {
 function forum_user_can_view_post($post, $course, $cm, $forum, $discussion, $user=NULL){
 
     global $CFG, $USER;
-    
+
     if (!$user){
         $user = $USER;
     }
-    
+
     $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
     if (!has_capability('mod/forum:viewdiscussion', $modcontext)) {
         return false;
@@ -3024,11 +3026,11 @@ function forum_user_can_see_discussion($forum, $discussion, $context, $user=NULL
             return false;
         }
     }
-    
+
     if (!has_capability('mod/forum:viewdiscussion', $context)) {
         return false;
     }
-    
+
     if ($forum->type == 'qanda' &&
             !forum_user_has_posted($forum->id, $discussion->id, $user->id) &&
             !has_capability('mod/forum:viewqandawithoutposting', $context)) {
@@ -3036,7 +3038,7 @@ function forum_user_can_see_discussion($forum, $discussion, $context, $user=NULL
     }
     return true;
 }
-    
+
 
 function forum_user_can_see_post($forum, $discussion, $post, $user=NULL) {
     global $USER;
@@ -3047,9 +3049,9 @@ function forum_user_can_see_post($forum, $discussion, $post, $user=NULL) {
             return false;
         }
     }
-    
+
     if (is_numeric($discussion)) {
-        if (!$discussion = get_record('forum_discussions','id',$discussion)) { 
+        if (!$discussion = get_record('forum_discussions','id',$discussion)) {
             return false;
         }
     }
@@ -3061,7 +3063,7 @@ function forum_user_can_see_post($forum, $discussion, $post, $user=NULL) {
     if (!isset($post->id) && isset($post->parent)) {
         $post->id = $post->parent;
     }
-    
+
     if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course)) {
         error('Course Module ID was incorrect');
     }
@@ -3074,10 +3076,10 @@ function forum_user_can_see_post($forum, $discussion, $post, $user=NULL) {
     if (!has_capability('mod/forum:viewdiscussion', $context, $user->id, false)) {
         return false;
     }
-    
+
     if ($forum->type == 'qanda') {
         $firstpost = forum_get_firstpost_from_discussion($discussion->id);
-        
+
         return (forum_user_has_posted($forum->id,$discussion->id,$user->id) ||
                 $firstpost->id == $post->id ||
                 has_capability('mod/forum:viewqandawithoutposting', $context, false, $user->id));
@@ -3088,26 +3090,26 @@ function forum_user_can_see_post($forum, $discussion, $post, $user=NULL) {
 
 /**
 * Prints the discussion view screen for a forum.
-* 
+*
 * @param object $course The current course object.
 * @param object $forum Forum to be printed.
 * @param int $maxdiscussions The maximum number of discussions per page(optional).
-* @param string $displayformat The display format to use (optional). 
+* @param string $displayformat The display format to use (optional).
 * @param string $sort Sort arguments for database query (optional).
 * @param int $currentgroup Group to display discussions for (optional).
 * @param int $groupmode Group mode of the forum (optional).
 * @param int $page Page mode, page to display (optional).
-* 
+*
 */
 function forum_print_latest_discussions($course, $forum, $maxdiscussions=5, $displayformat='plain', $sort='',
                                         $currentgroup=-1, $groupmode=-1, $page=-1) {
     global $CFG, $USER;
-    
+
     if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course)) {
         error('Course Module ID was incorrect');
     }
     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-    
+
 
 /// Sort out some defaults
 
@@ -3137,7 +3139,7 @@ function forum_print_latest_discussions($course, $forum, $maxdiscussions=5, $dis
         $currentgroup = get_current_group($course->id);
     }
 
-    if (!$currentgroup and ($groupmode != SEPARATEGROUPS or 
+    if (!$currentgroup and ($groupmode != SEPARATEGROUPS or
                 has_capability('moodle/site:accessallgroups', $context)) ) {
         $visiblegroups = -1;
     } else {
@@ -3147,15 +3149,15 @@ function forum_print_latest_discussions($course, $forum, $maxdiscussions=5, $dis
 /// If the user can post discussions, then this is a good place to put the button for it
     //add group mode in there, to test for visible group
     if (forum_user_can_post_discussion($forum, $currentgroup, $groupmode) ||
-        has_capability('moodle/legacy:guest', $context, NULL, false)) {  
+        has_capability('moodle/legacy:guest', $context, NULL, false)) {
 
         echo '<div class="singlebutton forumaddnew">';
         echo "<form name=\"newdiscussionform\" method=\"get\" action=\"$CFG->wwwroot/mod/forum/post.php\">";
         echo "<input type=\"hidden\" name=\"forum\" value=\"$forum->id\" />";
         echo '<input type="submit" value="';
-        echo ($forum->type == 'news') ? get_string('addanewtopic', 'forum') 
-            : (($forum->type == 'qanda') 
-               ? get_string('addanewquestion','forum') 
+        echo ($forum->type == 'news') ? get_string('addanewtopic', 'forum')
+            : (($forum->type == 'qanda')
+               ? get_string('addanewquestion','forum')
                : get_string('addanewdiscussion', 'forum'));
         echo '" />';
         echo '</form>';
@@ -3460,13 +3462,13 @@ function forum_print_posts_threaded($parent, $courseid, $depth, $ratings, $reply
     $istracking = forum_tp_can_track_forums($forumid) && forum_tp_is_tracked($forumid);
 
     if ($posts = forum_get_child_posts($parent, $forumid)) {
-        
+
         if (!$cm = get_coursemodule_from_instance('forum', $forumid, $courseid)) {
             error('Course Module ID was incorrect');
         }
         $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
         $canviewfullnames = has_capability('moodle/site:viewfullnames', $modcontext);
-        
+
         foreach ($posts as $post) {
 
             echo '<div class="indent">';
@@ -3526,7 +3528,7 @@ function forum_print_posts_nested($parent, $courseid, $ratings, $reply, &$user_r
             } else {
                 $ownpost = ($USER->id == $post->userid);
             }
+
             $post->subject = format_string($post->subject);
 
             if (forum_print_post($post, $courseid, $ownpost, $reply, $link, $ratings,
@@ -3583,10 +3585,10 @@ function forum_get_recent_mod_activity(&$activities, &$index, $sincetime, $cours
     }
 
     foreach ($posts as $post) {
-        
+
         $modcontext = get_context_instance(CONTEXT_MODULE, $post->cmid);
         $canviewallgroups = has_capability('moodle/site:accessallgroups', $modcontext);
-        
+
         if ($groupid and ($post->groupid != -1 and $groupid != $post->groupid and !$canviewallgroups)) {
             continue;
         }
@@ -3686,8 +3688,8 @@ function forum_update_subscriptions_button($courseid, $forumid) {
 /*
  * This function gets run whenever a role is assigned to a user in a context
  *
- * @param integer $userid 
- * @param object $context 
+ * @param integer $userid
+ * @param object $context
  * @return bool
  */
 function forum_role_assign($userid, $context) {
@@ -3698,8 +3700,8 @@ function forum_role_assign($userid, $context) {
 /*
  * This function gets run whenever a role is assigned to a user in a context
  *
- * @param integer $userid 
- * @param object $context 
+ * @param integer $userid
+ * @param object $context
  * @return bool
  */
 function forum_role_unassign($userid, $context) {
@@ -3733,7 +3735,7 @@ function forum_add_user_default_subscriptions($userid, $context) {
                                  }
                              }
                          }
-                         continue;  
+                         continue;
                      }
                      $subcontext = get_context_instance(CONTEXT_COURSE, $course->id);
                      forum_add_user_default_subscriptions($userid, $subcontext);
@@ -3745,7 +3747,7 @@ function forum_add_user_default_subscriptions($userid, $context) {
              if ($courses = get_records('course', 'category', $context->instanceid)) {
                  foreach ($courses as $course) {
                      if ($course->id == SITEID) {
-                        continue; // temporary workaround for bug MDL-7114  
+                        continue; // temporary workaround for bug MDL-7114
                      }
                      $subcontext = get_context_instance(CONTEXT_COURSE, $course->id);
                      forum_add_user_default_subscriptions($userid, $subcontext);
@@ -3758,7 +3760,7 @@ function forum_add_user_default_subscriptions($userid, $context) {
                  }
              }
              break;
-    
+
 
         case CONTEXT_COURSE:   // For a whole course
              if ($course = get_record('course', 'id', $context->instanceid)) {
@@ -3776,10 +3778,10 @@ function forum_add_user_default_subscriptions($userid, $context) {
                  }
              }
              break;
-         
+
         case CONTEXT_MODULE:   // Just one forum
              if ($cm = get_coursemodule_from_id('forum', $context->instanceid)) {
-                 if ($forum = get_record('forum', 'id', $cm->instance)) { 
+                 if ($forum = get_record('forum', 'id', $cm->instance)) {
                      if ($forum->forcesubscribe != FORUM_INITIALSUBSCRIBE) {
                          continue;
                      }
@@ -3816,10 +3818,10 @@ function forum_remove_user_subscriptions($userid, $context) {
                                              forum_unsubscribe($userid, $forum->id);
                                          }
                                      }
-                                }  
+                                }
                             }
                         }
-                        continue;  
+                        continue;
                     }
                     $subcontext = get_context_instance(CONTEXT_COURSE, $course->id);
                     forum_remove_user_subscriptions($userid, $subcontext);
@@ -3855,10 +3857,10 @@ function forum_remove_user_subscriptions($userid, $context) {
                  }
              }
              break;
-         
+
         case CONTEXT_MODULE:   // Just one forum
              if ($cm = get_coursemodule_from_id('forum', $context->instanceid)) {
-                 if ($forum = get_record('forum', 'id', $cm->instance)) { 
+                 if ($forum = get_record('forum', 'id', $cm->instance)) {
                      if (!has_capability('mod/forum:viewdiscussion', $context, $userid)) {
                          forum_unsubscribe($userid, $forum->id);
                      }
@@ -4069,9 +4071,9 @@ function forum_tp_count_forum_read_records($userid, $forumid, $groupid=false) {
 
     if ($CFG->dbtype === 'postgres7') {
         // this query takes 20ms, vs several minutes for the one below
-        $sql = " SELECT COUNT (DISTINCT u.id ) " 
+        $sql = " SELECT COUNT (DISTINCT u.id ) "
             .  " FROM ( "
-            .  "   SELECT  p.id " 
+            .  "   SELECT  p.id "
             .  "   FROM  {$CFG->prefix}forum_posts p "
             .  "       JOIN {$CFG->prefix}forum_discussions d ON p.discussion = d.id "
             .  "       JOIN {$CFG->prefix}forum_read r ON p.id = r.postid"
@@ -4142,7 +4144,7 @@ function forum_tp_delete_read_records($userid=-1, $postid=-1, $discussionid=-1,
 }
 /**
 * Get a list of forums not tracked by the user.
-* 
+*
 * @param int $userid The id of the user to use.
 * @param int $courseid The id of the course being checked (optional).
 * @return mixed An array indexed by forum id, or false.
@@ -4218,7 +4220,7 @@ function forum_tp_can_track_forums($forum=false, $user=false) {
 /**
 * Tells whether a specific forum is tracked by the user. A user can optionally
 * be specified. If not specified, the current user is assumed.
-* 
+*
 * @param mixed $forum If int, the id of the forum being checked; if object, the forum object
 * @param int $userid The id of the user being checked (optional).
 * @return boolean
@@ -4285,7 +4287,7 @@ function forum_tp_clean_read_records() {
             delete_records('forum_read', 'id', $oldreadpost->id);
         }
     }
-}    
+}
 
 /**
  * Sets the last post for a given discussion
@@ -4297,7 +4299,7 @@ function forum_discussion_update_last_post($discussionid) {
     if (!record_exists('forum_discussions', 'id', $discussionid)) {
         return false;
     }
-    
+
 /// Use SQL to find the last post for this discussion
     $sql = 'SELECT id, userid, modified '.
            'FROM '.$CFG->prefix.'forum_posts '.
@@ -4336,7 +4338,7 @@ function forum_get_separate_modules($courseid) {
 
     global $CFG,$db;
     $forummodule = get_record("modules", "name", "forum");
-    
+
     $sql = 'SELECT f.id, f.id FROM '.$CFG->prefix.'forum f, '.$CFG->prefix.'course_modules cm WHERE
            f.id = cm.instance AND cm.module ='.$forummodule->id.' AND cm.visible = 1 AND cm.course = '.$courseid.'
            AND cm.groupmode ='.SEPARATEGROUPS;
@@ -4354,15 +4356,15 @@ function forum_check_throttling($forum) {
     if (!is_object($forum)) {
         return false;  // this is broken.
     }
-    
+
     if (empty($forum->blockafter)) {
         return true;
     }
-    
+
     if (empty($forum->blockperiod)) {
         return true;
     }
-    
+
     if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course)) {
         error('Course Module ID was incorrect');
     }
@@ -4374,7 +4376,7 @@ function forum_check_throttling($forum) {
     // get the number of posts in the last period we care about
     $timenow = time();
     $timeafter = $timenow - $forum->blockperiod;
-    
+
     $numposts = count_records_sql('SELECT COUNT(p.id) FROM '.$CFG->prefix.'forum_posts p'
                                   .' JOIN '.$CFG->prefix.'forum_discussions d'
                                   .' ON p.discussion = d.id WHERE d.forum = '.$forum->id
@@ -4383,7 +4385,7 @@ function forum_check_throttling($forum) {
     $a->blockafter = $forum->blockafter;
     $a->numposts = $numposts;
     $a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
-    
+
     if ($forum->blockafter <= $numposts) {
         error(get_string('forumblockingtoomanyposts','error',$a),$CFG->wwwroot.'/mod/forum/view.php?f='.$forum->id);
     }
@@ -4391,7 +4393,7 @@ function forum_check_throttling($forum) {
         notify(get_string('forumblockingalmosttoomanyposts','forum',$a));
     }
 
-    
+
 }
 
 
@@ -4471,16 +4473,16 @@ function forum_reset_course_form($course) {
  */
 function forum_convert_to_roles($forum, $forummodid, $teacherroles=array(),
                                 $studentroles=array(), $guestroles=array(), $cmid=NULL) {
-    
+
     global $CFG;
-    
+
     if (!isset($forum->open) && !isset($forum->assesspublic)) {
         // We assume that this forum has already been converted to use the
         // Roles System. Columns forum.open and forum.assesspublic get dropped
         // once the forum module has been upgraded to use Roles.
         return false;
     }
-    
+
     if ($forum->type == 'teacher') {
 
         // Teacher forums should be converted to normal forums that
@@ -4568,7 +4570,7 @@ function forum_convert_to_roles($forum, $forummodid, $teacherroles=array(),
         }
     } else {
         // Non-teacher forum.
-        
+
         if (empty($cmid)) {
             // We were not given the course_module id. Try to find it.
             if (!$cm = get_coursemodule_from_instance('forum', $forum->id)) {
@@ -4650,7 +4652,7 @@ function forum_convert_to_roles($forum, $forummodid, $teacherroles=array(),
                 }
                 break;
         }
-        
+
         if (empty($cm)) {
             $cm = get_record('course_modules', 'id', $cmid);
         }
index 3d1585a98b0c3c2ece08f9ae684116e93102bc39..94aac962bc0aa9cb80a7ef960eb8c502c006d41d 100644 (file)
@@ -4,22 +4,29 @@
 
     require_once('../../config.php');
     require_once('lib.php');
+    require_once('post_form.php');
 
     $reply = optional_param('reply', 0, PARAM_INT);
     $forum = optional_param('forum', 0, PARAM_INT);
     $edit = optional_param('edit', 0, PARAM_INT);
     $delete = optional_param('delete', 0, PARAM_INT);
-    $prune = optional_param('prune',0,PARAM_INT);
-    $name = optional_param('name','',PARAM_CLEAN);
-    $confirm = optional_param('confirm',0,PARAM_INT);
-    
-    $sitecontext = get_context_instance(CONTEXT_SYSTEM);  
+    $prune = optional_param('prune', 0, PARAM_INT);
+    $name = optional_param('name', '', PARAM_CLEAN);
+    $confirm = optional_param('confirm', 0, PARAM_INT);
+
+
+    $page_params=array('reply'=>$reply,
+                        'forum'=>$forum,
+                        'edit'=>$edit); //these page_params will be passed as hidden variables
+                                    //later in the form.
+
+    $sitecontext = get_context_instance(CONTEXT_SYSTEM);
 
     if (has_capability('moodle/legacy:guest', $sitecontext, NULL, false)) {
 
         $wwwroot = $CFG->wwwroot.'/login/index.php';
         if (!empty($CFG->loginhttps)) {
-            $wwwroot = str_replace('http','https', $wwwroot);
+            $wwwroot = str_replace('http', 'https', $wwwroot);
         }
 
         if (!empty($forum)) {      // User is starting a new discussion in a forum
@@ -40,7 +47,7 @@
         if (! $course = get_record('course', 'id', $forum->course)) {
             error('The course number was incorrect');
         }
-        
+
         if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs
             error('Could not get the course module for the forum instance.');
         } else {
@@ -52,7 +59,7 @@
             print_header($course->shortname, $course->fullname,
                  "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> ->
                   <a href=\"../forum/index.php?id=$course->id\">$strforums</a> ->
-                  <a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>",
+                  <a href=\"view.php?f=$forum->id\">".format_string($forum->name, true)."</a>",
                   '', '', true, "", navmenu($course, $cm));
         } else {
             print_header($course->shortname, $course->fullname,
         print_footer($course);
         exit;
     }
-
     require_login(0, false);   // Script is useless unless they're logged in
 
-    if ($post = data_submitted()) {
-        if (empty($post->course)) {
-            error('No course was defined!');
-        }
-
-        if (!$course = get_record('course', 'id', $post->course)) {
-            error('Could not find specified course!');
-        }
-
-        if (!empty($forum)) {     // Check the forum id and change it to a full object
-            if (! $forum = get_record('forum', 'id', $forum)) {
-                error('The forum number was incorrect');
-            }
-        }
-        
-        if (!empty($course->lang)) {           // Override current language
-            $CFG->courselang = $course->lang;
-        }
-
-        if (empty($SESSION->fromurl)) {
-            $errordestination = "$CFG->wwwroot/mod/forum/view.php?f=$post->forum";
-        } else {
-            $errordestination = $SESSION->fromurl;
-        }
-
-        $post->subject = clean_param(strip_tags($post->subject, '<lang><span>'), PARAM_CLEAN); // Strip all tags except multilang
-
-        //$post->message will be cleaned later before display
-
-        $post->attachment = isset($_FILES['attachment']) ? $_FILES['attachment'] : NULL;
-
-        if (!$cm = get_coursemodule_from_instance("forum", $post->forum, $course->id)) { // For the logs
-            error('Could not get the course module for the forum instance.');
-        }
-        $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
-        trusttext_after_edit($post->message, $modcontext);
-
-        if (!$post->subject or !$post->message) {
-            $post->error = get_string("emptymessage", "forum");
-
-        } else if ($post->edit) {           // Updating a post
-            $post->id = $post->edit;
-            $message = '';
-
-            //fix for bug #4314
-            if (!$realpost = get_record('forum_posts','id',$post->id)) {
-                $realpost = new object;
-                $realpost->userid = -1;
-            }
-            
-            
-            // if user has edit any post capability
-            // or has either startnewdiscussion or reply capability and is editting own post
-            // then he can proceed
-            // MDL-7066
-            if ( !(($realpost->userid == $USER->id && (has_capability('mod/forum:replypost', $modcontext) 
-                                || has_capability('mod/forum:startdiscussion', $modcontext))) ||
-                                has_capability('mod/forum:editanypost', $modcontext)) ) {
-                error("You can not update this post");
-            }
-
-            if ($forum->type == 'news' && !$post->parent) {
-                $updatediscussion = new object;
-                $updatediscussion->id = $post->discussion;
-                if (empty($post->timestartdisabled)) {
-                    $updatediscussion->timestart = make_timestamp($post->timestartyear, $post->timestartmonth, $post->timestartday);
-                } else {
-                    $updatediscussion->timestart = 0;
-                }
-                if (empty($post->timeenddisabled)) {
-                    $updatediscussion->timeend = make_timestamp($post->timeendyear, $post->timeendmonth, $post->timeendday);
-                } else {
-                    $updatediscussion->timeend = 0;
-                }
-                if (empty($post->timeenddisabled) && $updatediscussion->timeend <= $updatediscussion->timestart) {
-                    $post->error = get_string('timestartenderror', 'forum');
-                } elseif (!update_record('forum_discussions', $updatediscussion)) {
-                    error(get_string("couldnotupdate", "forum"), $errordestination);
-                }
-            }
-
-            if (!isset($post->error)) {
-
-                if (forum_update_post($post,$message)) {
-
-                    $timemessage = 2;
-                    if (!empty($message)) { // if we're printing stuff about the file upload
-                        $timemessage = 4;
-                    }
-                    $message .= '<br />'.get_string("postupdated", "forum");
-
-                    if ($subscribemessage = forum_post_subscription($post)) {
-                        $timemessage = 4;
-                    }
-                    if ($forum->type == 'single') {
-                        // Single discussion forums are an exception. We show
-                        // the forum itself since it only has one discussion
-                        // thread.
-                        $discussionurl = "view.php?f=$forum->id";
-                    } else {
-                        $discussionurl = "discuss.php?d=$post->discussion#$post->id";
-                    }
-                    add_to_log($course->id, "forum", "update post",
-                            "$discussionurl&amp;parent=$post->id", "$post->id", $cm->id);
-                    
-                    redirect(forum_go_back_to("$discussionurl#$post->id"), $message.$subscribemessage, $timemessage);
-
-                } else {
-                    error(get_string("couldnotupdate", "forum"), $errordestination);
-                }
-                exit;
-            }
-            
-        } else if ($post->discussion) { // Adding a new post to an existing discussion
-            $message = '';
-            if ($post->id = forum_add_new_post($post,$message)) {
-
-                $timemessage = 2;
-                if (!empty($message)) { // if we're printing stuff about the file upload
-                    $timemessage = 4;
-                }
-                $message .= '<br />'.get_string("postadded", "forum", format_time($CFG->maxeditingtime));
-
-                if ($subscribemessage = forum_post_subscription($post)) {
-                    $timemessage = 4;
-                }
-
-                if (!empty($post->mailnow)) {
-                    $message .= get_string("postmailnow", "forum");
-                    $timemessage = 4;
-                }
-
-                if ($forum->type == 'single') {
-                    // Single discussion forums are an exception. We show
-                    // the forum itself since it only has one discussion
-                    // thread.
-                    $discussionurl = "view.php?f=$forum->id";
-                } else {
-                    $discussionurl = "discuss.php?d=$post->discussion";
-                }
-                add_to_log($course->id, "forum", "add post",
-                          "$discussionurl&amp;parent=$post->id", "$post->id", $cm->id);
-
-                redirect(forum_go_back_to("$discussionurl#$post->id"), $message.$subscribemessage, $timemessage);
-
-            } else {
-                error(get_string("couldnotadd", "forum"), $errordestination);
-            }
-            exit;
-
-        } else {                     // Adding a new discussion
-            $post->mailnow = empty($post->mailnow) ? 0 : 1;
-            $discussion = $post;
-            $discussion->name  = $post->subject;
-            $discussion->intro = $post->message;
-            $newstopic = false;
-
-            if ($forum->type == 'news' && !$post->parent) {
-                $newstopic = true;
-            }
-            if ($newstopic && empty($post->timestartdisabled)) {
-                $discussion->timestart = make_timestamp($post->timestartyear, $post->timestartmonth, $post->timestartday);
-            } else {
-                $discussion->timestart = 0;
-            }
-            if ($newstopic && empty($post->timeenddisabled)) {
-                $discussion->timeend = make_timestamp($post->timeendyear, $post->timeendmonth, $post->timeendday);
-            } else {
-                $discussion->timeend = 0;
-            }
-            if ($newstopic && empty($post->timeenddisabled) && $discussion->timeend <= $discussion->timestart) {
-                $post->error = get_string('timestartenderror', 'forum');
-            } else {
-                $message = '';
-                if ($discussion->id = forum_add_discussion($discussion,$message)) {
-
-                    add_to_log($course->id, "forum", "add discussion",
-                            "discuss.php?d=$discussion->id", "$discussion->id", $cm->id);
-
-                    $timemessage = 2;
-                    if (!empty($message)) { // if we're printing stuff about the file upload
-                        $timemessage = 4;
-                    }
-                    $message .= '<br />'.get_string("postadded", "forum", format_time($CFG->maxeditingtime));
-
-                    if ($post->mailnow) {
-                        $message .= get_string("postmailnow", "forum");
-                        $timemessage = 4;
-                    }
-
-                    if ($subscribemessage = forum_post_subscription($discussion)) {
-                        $timemessage = 4;
-                    }
-
-                    redirect(forum_go_back_to("view.php?f=$post->forum"), $message.$subscribemessage, $timemessage);
-
-                } else {
-                    error(get_string("couldnotadd", "forum"), $errordestination);
-                }
-
-                exit;
-            }
-        }
-    }
-
-    if ($usehtmleditor = can_use_html_editor()) {
-        $defaultformat = FORMAT_HTML;
-    } else {
-        $defaultformat = FORMAT_MOODLE;
-    }
-
-    if (isset($post->error)) {     // User is re-editing a failed posting
-
-        // Set up all the required objects again, and reuse the same $post
-
-        if (! $forum = get_record("forum", "id", $post->forum)) {
-            error("The forum number was incorrect ($post->forum)");
-        }
-
-        if (! $course = get_record("course", "id", $forum->course)) {
-            error("The course number was incorrect ($forum->course)");
-        }
-
-        if (!empty($post->parent)) {
-            if (! $parent = forum_get_post_full($post->parent)) {
-                error("Parent post ID was incorrect ($post->parent)");
-            }
-        }
-
-        if (!empty($post->discussion)) {
-            if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
-                error("This post is not part of a discussion! ($post->discussion)");
-            }
-        } else {
-            $discussion = new stdClass();
-            $newstopic = false;
-            if ($forum->type == 'news' && !$post->parent) {
-                $newstopic = true;
-            }
-            if ($newstopic && empty($post->timestartdisabled)) {
-                $discussion->timestart = make_timestamp($post->timestartyear, $post->timestartmonth, $post->timestartday);
-            } else {
-                $discussion->timestart = 0;
-            }
-            if ($newstopic && empty($post->timeenddisabled)) {
-                $discussion->timeend = make_timestamp($post->timeendyear, $post->timeendmonth, $post->timeendday);
-            } else {
-                $discussion->timeend = 0;
-            }
-        }
-
-        $post->subject = stripslashes_safe($post->subject);
-        $post->message = stripslashes_safe($post->message);
-
-    } else if (!empty($forum)) {      // User is starting a new discussion in a forum
-
-        $SESSION->fromurl = $_SERVER["HTTP_REFERER"];
-
+    if (!empty($forum)) {      // User is starting a new discussion in a forum
         if (! $forum = get_record("forum", "id", $forum)) {
             error("The forum number was incorrect ($forum)");
         }
             error("The course number was incorrect ($forum->course)");
         }
         $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
-        
         if (! forum_user_can_post_discussion($forum)) {
             if (has_capability('moodle/legacy:guest', $coursecontext, NULL, false)) {  // User is a guest here!
                 $SESSION->wantsurl = $FULLME;
                 print_error('nopostforum', 'forum');
             }
         }
-        
+
         if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
             if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $coursecontext)) {
                 error(get_string("activityiscurrentlyhidden"));
             }
         }
 
+        $SESSION->fromurl = $_SERVER["HTTP_REFERER"];
+
+
         // Load up the $post variable.
 
         $post->course = $course->id;
         $post->subject = "";
         $post->userid = $USER->id;
         $post->message = "";
-        $post->format = $defaultformat;
 
         $post->groupid = get_current_group($course->id);
         if ($post->groupid == 0) {
         }
 
         $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
-        
         if (! forum_user_can_post($forum)) {
             if (has_capability('moodle/legacy:guest', $coursecontext, NULL, false)) {  // User is a guest here!
                 $SESSION->wantsurl = $FULLME;
                 print_error('nopostforum', 'forum');
             }
         }
-        
+
         if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
             if (groupmode($course, $cm)) {   // Make sure user can post here
                 $mygroupid = mygroupid($course->id);
         $post->subject = $parent->subject;
         $post->userid = $USER->id;
         $post->message = "";
-        $post->format = $defaultformat;
 
         $strre = get_string('re', 'forum');
         if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
         }
 
         unset($SESSION->fromdiscussion);
-
     } else if (!empty($edit)) {  // User is editing their own post
 
         if (! $post = forum_get_post_full($edit)) {
                 error("Parent post ID was incorrect ($post->parent)");
             }
         }
+
         if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
-            error("This post is not part of a discussion! ($reply)");
+            error("This post is not part of a discussion! ($edit)");
         }
         if (! $forum = get_record("forum", "id", $discussion->forum)) {
             error("The forum number was incorrect ($discussion->forum)");
         unset($SESSION->fromdiscussion);
 
 
-    } else if (!empty($delete)) {  // User is deleting a post
+    }else if (!empty($delete)) {  // User is deleting a post
 
         if (! $post = forum_get_post_full($delete)) {
             error("Post ID was incorrect");
             }
         }
 
+
         $replycount = forum_count_replies($post);
 
         if (!empty($confirm)) {    // User has confirmed the delete
                     add_to_log($discussion->course, "forum", "delete discussion",
                                "view.php?id=$cm->id", "$forum->id", $cm->id);
 
-                    redirect("view.php?f=$discussion->forum",
-                             get_string("deleteddiscussion", "forum"), 1);
+                    redirect("view.php?f=$discussion->forum");
 
                 } else if (forum_delete_post($post, has_capability('mod/forum:deleteanypost', $modcontext))) {
-                    
+
                     if ($forum->type == 'single') {
                         // Single discussion forums are an exception. We show
                         // the forum itself since it only has one discussion
                     } else {
                         $discussionurl = "discuss.php?d=$post->discussion";
                     }
-                    
+
                     add_to_log($discussion->course, "forum", "delete post", $discussionurl, "$post->id", $cm->id);
 
-                    $feedback = $replycount ? get_string('deletedposts', 'forum') : get_string('deletedpost', 'forum');
-                    redirect(forum_go_back_to($discussionurl), $feedback, 1);
+                    redirect(forum_go_back_to($discussionurl));
                 } else {
                     error("An error occurred while deleting record $post->id");
                 }
             add_to_log($discussion->course, "forum", "prune post",
                            "discuss.php?d=$newid", "$post->id", $cm->id);
 
-            redirect(forum_go_back_to("discuss.php?d=$newid"), get_string("prunedpost", "forum"), 1);
+            redirect(forum_go_back_to("discuss.php?d=$newid"));
 
         } else { // User just asked to prune something
 
             $strforums = get_string("modulenameplural", "forum");
             print_header_simple(format_string($discussion->name).": ".format_string($post->subject), "",
                          "<a href=\"../forum/index.php?id=$course->id\">$strforums</a> ->
-                          <a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a> ->
-                          <a href=\"discuss.php?d=$discussion->id\">".format_string($post->subject,true)."</a> -> ".
+                          <a href=\"view.php?f=$forum->id\">".format_string($forum->name, true)."</a> ->
+                          <a href=\"discuss.php?d=$discussion->id\">".format_string($post->subject, true)."</a> -> ".
                           get_string("prune", "forum"), '', "", true, "", navmenu($course, $cm));
 
             print_heading(get_string('pruneheading', 'forum'));
         }
         print_footer($course);
         die;
-
-
     } else {
         error("No operation specified");
 
     }
 
+    if (!isset($coursecontext)) {
+        // Has not yet been set by post.php.
+        $coursecontext = get_context_instance(CONTEXT_COURSE, $forum->course);
+    }
+
+    $mform_post = new forum_post_form('post.php', compact('coursecontext', 'forum', 'post'));
+
+    if ($fromform = $mform_post->data_submitted()) {
+
+
+        if (!empty($course->lang)) {           // Override current language
+            $CFG->courselang = $course->lang;
+        }
+
+        if (empty($SESSION->fromurl)) {
+            $errordestination = "$CFG->wwwroot/mod/forum/view.php?f=$forum->id";
+        } else {
+            $errordestination = $SESSION->fromurl;
+        }
+
+        // TODO add attachment processing
+        //$fromform->attachment = isset($_FILES['attachment']) ? $_FILES['attachment'] : NULL;
+
+        if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) { // For the logs
+            error('Could not get the course module for the forum instance.');
+        }
+        $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
+        trusttext_after_edit($fromform->message, $modcontext);
+
+        if ($fromform->edit) {           // Updating a post
+            $fromform->id = $fromform->edit;
+            $message = '';
+
+            //fix for bug #4314
+            if (!$realpost = get_record('forum_posts', 'id', $fromform->id)) {
+                $realpost = new object;
+                $realpost->userid = -1;
+            }
+
+
+            // if user has edit any post capability
+            // or has either startnewdiscussion or reply capability and is editting own post
+            // then he can proceed
+            // MDL-7066
+            if ( !(($realpost->userid == $USER->id && (has_capability('mod/forum:replypost', $modcontext)
+                                || has_capability('mod/forum:startdiscussion', $modcontext))) ||
+                                has_capability('mod/forum:editanypost', $modcontext)) ) {
+                error("You can not update this post");
+            }
+
+            if ($forum->type == 'news' && !$fromform->parent) {
+                $updatediscussion = new object;
+                $updatediscussion->id = $fromform->discussion;
+                if (empty($fromform->timestartdisabled)) {
+                    $updatediscussion->timestart = $fromform->timestart;
+                } else {
+                    $updatediscussion->timestart = 0;
+                }
+                if (empty($fromform->timeenddisabled)) {
+                    $updatediscussion->timeend = $fromform->timeend;
+                } else {
+                    $updatediscussion->timeend = 0;
+                }
+
+                if (!update_record('forum_discussions', $updatediscussion)) {
+                    error(get_string("couldnotupdate", "forum"), $errordestination);
+                }
+            }
+
+            $updatepost=$fromform; //realpost
+            $updatepost->forum=$forum->id;
+            if (!forum_update_post($updatepost, $message)) {
+                error(get_string("couldnotupdate", "forum"), $errordestination);
+            }
+
+            $timemessage = 2;
+            if (!empty($message)) { // if we're printing stuff about the file upload
+                $timemessage = 4;
+            }
+            $message .= '<br />'.get_string("postupdated", "forum");
+
+            if ($subscribemessage = forum_post_subscription($fromform)) {
+                $timemessage = 4;
+            }
+            if ($forum->type == 'single') {
+                // Single discussion forums are an exception. We show
+                // the forum itself since it only has one discussion
+                // thread.
+                $discussionurl = "view.php?f=$forum->id";
+            } else {
+                $discussionurl = "discuss.php?d=$discussion->id#$fromform->id";
+            }
+            add_to_log($course->id, "forum", "update post",
+                    "$discussionurl&amp;parent=$fromform->id", "$fromform->id", $cm->id);
+
+            redirect(forum_go_back_to("$discussionurl"), $message.$subscribemessage, $timemessage);
+
+            exit;
+
+
+        } else if ($fromform->discussion) { // Adding a new post to an existing discussion
+            $message = '';
+            $addpost=$fromform;
+            $addpost->forum=$forum->id;
+            if ($fromform->id = forum_add_new_post($addpost, $message)) {
+
+                $timemessage = 2;
+                if (!empty($message)) { // if we're printing stuff about the file upload
+                    $timemessage = 4;
+                }
+                $message .= '<br />'.get_string("postadded", "forum", format_time($CFG->maxeditingtime));
+
+                if ($subscribemessage = forum_post_subscription($fromform)) {
+                    $timemessage = 4;
+                }
+
+                if (!empty($fromform->mailnow)) {
+                    $message .= get_string("postmailnow", "forum");
+                    $timemessage = 4;
+                }
+
+                if ($forum->type == 'single') {
+                    // Single discussion forums are an exception. We show
+                    // the forum itself since it only has one discussion
+                    // thread.
+                    $discussionurl = "view.php?f=$forum->id";
+                } else {
+                    $discussionurl = "discuss.php?d=$discussion->id";
+                }
+                add_to_log($course->id, "forum", "add post",
+                          "$discussionurl&amp;parent=$fromform->id", "$fromform->id", $cm->id);
+
+                redirect(forum_go_back_to("$discussionurl#$fromform->id"), $message.$subscribemessage, $timemessage);
+
+            } else {
+                error(get_string("couldnotadd", "forum"), $errordestination);
+            }
+            exit;
+
+        } else {                     // Adding a new discussion
+            $fromform->mailnow = empty($fromform->mailnow) ? 0 : 1;
+            $discussion = $fromform;
+            $discussion->name  = $fromform->subject;
+            $discussion->intro = $fromform->message;
+            $newstopic = false;
+
+            if ($forum->type == 'news' && !$fromform->parent) {
+                $newstopic = true;
+            }
+            if ($newstopic && empty($fromform->timestartdisabled)) {
+                $discussion->timestart = $fromform->timestart;
+            } else {
+                $discussion->timestart = 0;
+            }
+            if ($newstopic && empty($fromform->timeenddisabled)) {
+                $discussion->timeend = $fromform->timeend;
+            } else {
+                $discussion->timeend = 0;
+            }
+
+            $message = '';
+            if ($discussion->id = forum_add_discussion($discussion, $message)) {
+
+                add_to_log($course->id, "forum", "add discussion",
+                        "discuss.php?d=$discussion->id", "$discussion->id", $cm->id);
+
+                $timemessage = 2;
+                if (!empty($message)) { // if we're printing stuff about the file upload
+                    $timemessage = 4;
+                }
+                $message .= '<br />'.get_string("postadded", "forum", format_time($CFG->maxeditingtime));
+
+                if ($fromform->mailnow) {
+                    $message .= get_string("postmailnow", "forum");
+                    $timemessage = 4;
+                }
+
+                if ($subscribemessage = forum_post_subscription($discussion)) {
+                    $timemessage = 4;
+                }
+
+                redirect(forum_go_back_to("view.php?f=$fromform->forum"), $message.$subscribemessage, $timemessage);
+
+            } else {
+                error(get_string("couldnotadd", "forum"), $errordestination);
+            }
+
+            exit;
+        }
+    }
+
+
 
     // To get here they need to edit a post, and the $post
     // variable will be loaded with all the particulars,
                                                        get_string("addanewdiscussion", "forum");
     }
 
-    if (empty($post->subject)) {
-        $formstart = 'theform.subject';
-    } else {
-        $formstart = '';
-    }
+
 
     if ($post->parent) {
-        $navtail = ' -> <a href="discuss.php?d='.$discussion->id.'">'.format_string($toppost->subject,true).'</a> -> '.
+        $navtail = ' -> <a href="discuss.php?d='.$discussion->id.'">'.format_string($toppost->subject, true).'</a> -> '.
                     get_string('editing', 'forum');
     } else {
         $navtail = ' -> '.format_string($toppost->subject);
     $strforums = get_string("modulenameplural", "forum");
 
 
-    $navmiddle = "<a href=\"../forum/index.php?id=$course->id\">$strforums</a> -> <a href=\"view.php?f=$forum->id\">".format_string($forum->name,true).'</a> ';
+    $navmiddle = "<a href=\"../forum/index.php?id=$course->id\">$strforums</a> -> <a href=\"view.php?f=$forum->id\">".format_string($forum->name, true).'</a> ';
 
     if (empty($discussion->name)) {
         if (empty($discussion)) {
         print_header("$course->shortname: $strdiscussionname ".
                       format_string($toppost->subject), "$course->fullname",
                      "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> ->
-                      $navmiddle $navtail", $formstart, "", true, "", navmenu($course, $cm));
+                      $navmiddle $navtail", $mform_post->focus(), "", true, "", navmenu($course, $cm));
 
     } else {
         print_header("$course->shortname: $strdiscussionname ".
                       format_string($toppost->subject), "$course->fullname",
-                     "$navmiddle $navtail", "$formstart", "", true, "", navmenu($course, $cm));
+                     "$navmiddle $navtail", $mform_post->focus(), "", true, "", navmenu($course, $cm));
 
     }
 
     }
 
     if ($forum->type == 'qanda' && !has_capability('mod/forum:viewqandawithoutposting', $modcontext) &&
-                !forum_user_has_posted($forum->id,$discussion->id,$USER->id)) {
-        notify(get_string('qandanotify','forum'));
+                !forum_user_has_posted($forum->id, $discussion->id, $USER->id)) {
+        notify(get_string('qandanotify', 'forum'));
     }
 
     forum_check_throttling($forum);
                 forum_print_posts_threaded($parent->id, $course->id, 0, false, false, $user_read_array, $discussion->forum);
             }
         }
-        print_heading(get_string("yourreply", "forum").':');
+        $heading=get_string("yourreply", "forum");
     } else {
         $forum->intro = trim($forum->intro);
         if (!empty($forum->intro)) {
             print_simple_box(format_text($forum->intro), 'center');
         }
         if ($forum->type == 'qanda') {
-            print_heading(get_string('yournewquestion','forum'));
+            $heading=get_string('yournewquestion', 'forum');
         } else {
-            print_heading(get_string('yournewtopic', 'forum'));
+            $heading=get_string('yournewtopic', 'forum');
         }
     }
-    echo '<center>';
-    if (!empty($post->error)) {
-        notify($post->error);
-    }
-    echo '</center>';
 
     if ($USER->id != $post->userid) {   // Not the original author, so add a message to the end
         $data->date = userdate($post->modified);
         }
     }
 
-    print_simple_box_start("center");
-    require("post.html");
-    print_simple_box_end();
+    //load data into form
+    $subscribe=(isset($post->forum)&&forum_is_subscribed($USER->id, $post->forum)) ||
+                    (!empty($USER->autosubscribe));
+
+
+    $mform_post->set_defaults(array('general'=>$heading,
+                                    'subject'=>$post->subject,
+                                        'message'=>$post->message,
+                                        'subscribe'=>$subscribe?1:0,
+                                        'mailnow'=>!empty($post->mailnow),
+                                        'userid'=>$post->userid,
+                                        'parent'=>$post->parent,
+                                        'discussion'=>$post->discussion,
+                                        'course'=>$course->id)+
+
+                                        $page_params+
+
+                                (isset($post->format)?array(
+                                        'format'=>$post->format):
+                                    array())+
+
+                                (isset($post->groupid)?array(
+                                        'groupid'=>$post->groupid):
+                                    array())+
+
+                                (isset($discussion->timestart)?
+                                        array('timestart'=>$discussion->timestart):
+                                        array('timestart'=>0))+
+
+                                (isset($discussion->timeend)?
+                                        array('timeend'=>$discussion->timeend):
+                                        array('timeend'=>0))+
+
+                                (isset($discussion->timestartdisabled)?
+                                        array('timestartdisabled'=>$discussion->timestartdisabled):
+                                        array('timestartdisabled'=>1))+
+
+                                (isset($discussion->timeenddisabled)?
+                                        array('timeenddisabled'=>$discussion->timeenddisabled):
+                                        array('timeenddisabled'=>1))+
+
+                                (isset($discussion->id)?
+                                        array('discussion'=>$discussion->id):
+                                        array()));
+
+    $mform_post->trusttext_prepare_edit('message', 'format', $modcontext);
+
+    $mform_post->display();
 
-    if ($usehtmleditor) {
-        use_html_editor("message");
-    }
 
     print_footer($course);