MDL-65069 mod_forum: inline 'add discussion' form
authorPeter <peter@moodle.com>
Mon, 25 Mar 2019 03:07:25 +0000 (11:07 +0800)
committerPeter <peter@moodle.com>
Wed, 24 Apr 2019 02:29:30 +0000 (10:29 +0800)
mod/forum/classes/local/renderers/discussion_list.php
mod/forum/templates/discussion_list.mustache

index cd4ce42..bde944f 100644 (file)
@@ -185,6 +185,7 @@ class discussion_list {
             $forumview,
             [
                 'pagination' => $this->renderer->render(new \paging_bar($alldiscussionscount, $pageno, $pagesize, $PAGE->url, 'p')),
+                'newdiscussionhtml' => $this->get_discussion_form($user, $cm, $groupid)
             ],
             $exportedposts
         );
@@ -192,6 +193,48 @@ class discussion_list {
         return $this->renderer->render_from_template($this->template, $forumview);
     }
 
+    private function get_discussion_form(stdClass $user, \cm_info $cm, ?int $groupid) {
+        global $PAGE;
+
+        $forum = $this->forum;
+        $forumrecord = $this->legacydatamapperfactory->get_forum_data_mapper()->to_legacy_object($forum);
+        $modcontext = \context_module::instance($cm->id);
+        $post = new stdClass();
+        $post->course        = $forum->get_course_id();
+        $post->forum         = $forum->get_id();
+        $post->discussion    = 0;           // Ie discussion # not defined yet.
+        $post->parent        = 0;
+        $post->subject       = '';
+        $post->userid        = $user->id;
+        $post->message       = '';
+        $post->messageformat = editors_get_preferred_format();
+        $post->messagetrust  = 0;
+        $post->groupid = $groupid;
+
+        $thresholdwarning = forum_check_throttling($forum, $cm);
+        //$forumrecord = $DB->get_record('forum', array('id' => $forum->get_id()));
+        $mformpost = new \mod_forum_post_form('post.php', array('course' => $forum->get_course_record(),
+            'cm' => $cm,
+            'coursecontext' => $forum->get_context(),
+            'modcontext' => $modcontext,
+            'forum' => $forumrecord,
+            'post' => $post,
+            'subscribe' => \mod_forum\subscriptions::is_subscribed($user->id, $forumrecord,
+                null, $cm),
+            'thresholdwarning' => $thresholdwarning,
+            'edit' => 0), 'post', '', array('id' => 'mformforum'));
+        // Setup the file related components.
+        $draftitemid = file_get_submitted_draft_itemid('attachments');
+        $attachoptions = \mod_forum_post_form::attachment_options($forumrecord);
+        file_prepare_draft_area($draftitemid, $modcontext->id, 'mod_forum', 'attachment', null, $attachoptions);
+
+        $params = array('reply' => 0, 'forum' => $forumrecord->id, 'edit' => 0) +
+            (isset($post->groupid) ? array('groupid' => $post->groupid) : array());
+        $mformpost->set_data($params);
+
+        return $mformpost->render();
+    }
+
     /**
      * Get the list of groups to show based on the current user and requested groupid.
      *
index 58b0cc3..6abcf92 100644 (file)
 
     {{#forum.capabilities.create}}
         <div class="p-t-1 p-b-1">
-            <a href="{{forum.urls.create}}" class="btn btn-primary">
+            <a class="btn btn-primary" data-toggle="collapse" href="#collapseAddForm">
                 {{$discussion_create_text}}
                     {{#str}}addanewdiscussion, forum{{/str}}
                 {{/discussion_create_text}}
             </a>
+            <div class="collapse" id="collapseAddForm">
+                {{{newdiscussionhtml}}}
+            </div>
         </div>
     {{/forum.capabilities.create}}