Browse Source

Allow add new forum thread by api REST - refs #8366

Angel Fernando Quiroz Campos 8 years ago
parent
commit
8583e24d31
3 changed files with 62 additions and 14 deletions
  1. 17 13
      main/forum/forumfunction.inc.php
  2. 22 1
      main/inc/lib/webservices/Rest.php
  3. 23 0
      main/webservices/api/v2.php

+ 17 - 13
main/forum/forumfunction.inc.php

@@ -2418,15 +2418,16 @@ function updateThread($values)
  * @param array $values
  * @param array $courseInfo
  * @param bool $showMessage
+ * @param int $userId Optional. The user ID
+ * @param int $sessionId
  * @return int
- *
  * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  * @version february 2006, dokeos 1.8
  */
-function store_thread($current_forum, $values, $courseInfo = array(), $showMessage = true)
+function store_thread($current_forum, $values, $courseInfo = array(), $showMessage = true, $userId = 0, $sessionId = 0)
 {
     $courseInfo = empty($courseInfo) ? api_get_course_info() : $courseInfo;
-    $_user = api_get_user_info();
+    $userId = $userId ?: api_get_user_id();
     $course_id = $courseInfo['real_id'];
     $courseCode = $courseInfo['code'];
     $groupId = api_get_group_id();
@@ -2435,6 +2436,7 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
     if (!empty($groupInfo)) {
         $groupIid = $groupInfo['iid'];
     }
+    $sessionId = $sessionId ?: api_get_session_id();
 
     $em = Database::getManager();
     $table_threads = Database :: get_course_table(TABLE_FORUM_THREAD);
@@ -2475,7 +2477,7 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
         ->setCId($course_id)
         ->setThreadTitle($clean_post_title)
         ->setForumId($values['forum_id'])
-        ->setThreadPosterId($_user['user_id'])
+        ->setThreadPosterId($userId)
         ->setThreadPosterName(isset($values['poster_name']) ? $values['poster_name'] : null)
         ->setThreadDate($post_date)
         ->setThreadSticky(isset($values['thread_sticky']) ? $values['thread_sticky'] : 0)
@@ -2485,7 +2487,7 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
         ->setThreadQualifyMax(isset($values['numeric_calification']) ? (int) $values['numeric_calification'] : 0)
         ->setThreadWeight(isset($values['weight_calification']) ? (int) $values['weight_calification'] : 0)
         ->setThreadPeerQualify(isset($values['thread_peer_qualify']) ? (int) $values['thread_peer_qualify'] : 0)
-        ->setSessionId(api_get_session_id())
+        ->setSessionId($sessionId)
         ->setLpItemId(isset($values['lp_item_id']) ? (int) $values['lp_item_id'] : 0)
         ->setThreadId(0)
         ->setLocked(0)
@@ -2516,7 +2518,7 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
             $maxqualify,
             $resourcedescription,
             0,
-            api_get_session_id()
+            $sessionId
         );
     }
 
@@ -2531,12 +2533,12 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
             TOOL_FORUM_THREAD,
             $lastThread->getIid(),
             'ForumThreadAdded',
-            api_get_user_id(),
+            $userId,
             $groupIid,
             null,
             null,
             null,
-            api_get_session_id()
+            $sessionId
         );
 
         // If the forum properties tell that the posts have to be approved
@@ -2551,7 +2553,9 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
             $lastThread->getIid(),
             TOOL_FORUM_THREAD,
             $groupIid,
-            $courseInfo
+            $courseInfo,
+            $sessionId,
+            $userId
         );
 
         if ($visible == 0) {
@@ -2560,7 +2564,7 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
                 TOOL_FORUM_THREAD,
                 $lastThread->getIid(),
                 'invisible',
-                api_get_user_id(),
+                $userId,
                 $groupIid
 
             );
@@ -2576,7 +2580,7 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
         ->setPostText($values['post_text'])
         ->setThreadId($lastThread->getIid())
         ->setForumId($values['forum_id'])
-        ->setPosterId($_user['user_id'])
+        ->setPosterId($userId)
         ->setPosterName(isset($values['poster_name']) ? $values['poster_name'] : null)
         ->setPostDate($post_date)
         ->setPostNotification(isset($values['post_notification']) ? (int) $values['post_notification'] : null)
@@ -2667,7 +2671,7 @@ function store_thread($current_forum, $values, $courseInfo = array(), $showMessa
         set_notification('thread', $lastThread->getIid(), true);
     }
 
-    send_notification_mails($lastThread->getIid(), $reply_info);
+    send_notification_mails($lastThread->getIid(), $reply_info, $courseInfo['code']);
 
     Session::erase('formelements');
     Session::erase('origin');
@@ -4024,7 +4028,7 @@ function send_notification_mails($thread_id, $reply_info)
     // 3. the thread is visible
     // 4. the reply is visible (=when there is)
     $current_thread = get_thread_information($thread_id);
-    $current_forum = get_forum_information($current_thread['forum_id']);
+    $current_forum = get_forum_information($current_thread['forum_id'], $current_thread['c_id']);
 
     $current_forum_category = null;
     if (isset($current_forum['forum_category'])) {

+ 22 - 1
main/inc/lib/webservices/Rest.php

@@ -39,6 +39,7 @@ class Rest extends WebService
     const SAVE_USER_MESSAGE = 'save_user_message';
     const GET_MESSAGE_USERS = 'message_users';
     const SAVE_COURSE_NOTEBOOK = 'save_course_notebook';
+    const SAVE_FORUM_THREAD = 'save_forum_thread';
 
     const EXTRAFIELD_GCM_ID = 'gcm_registration_id';
 
@@ -844,7 +845,7 @@ class Rest extends WebService
     {
         require_once api_get_path(SYS_CODE_PATH) . 'forum/forumfunction.inc.php';
 
-        $forum = get_forums($forumId);
+        $forum = get_forums($forumId, $this->course->getCode());
 
         store_reply($forum, $postValues, $this->course->getId(), $this->user->getId());
 
@@ -971,4 +972,24 @@ class Rest extends WebService
             'registered' => $noteBookId
         ];
     }
+
+    /**
+     * @param array $values
+     * @param int $forumId
+     * @return array
+     */
+    public function saveForumThread(array $values, $forumId)
+    {
+        require_once api_get_path(SYS_CODE_PATH) . 'forum/forumfunction.inc.php';
+
+        $forum = get_forums($forumId, $this->course->getCode());
+        $courseInfo = api_get_course_info($this->course->getCode());
+        $sessionId = $this->session ? $this->session->getId() : 0;
+
+        $id = store_thread($forum, $values, $courseInfo, false, $this->user->getId(), $sessionId);
+
+        return [
+            'registered' => $id
+        ];
+    }
 }

+ 23 - 0
main/webservices/api/v2.php

@@ -164,6 +164,7 @@ try {
                 throw new Exception(get_lang('NoData'));
             }
 
+            $forumId = isset($_POST['forum']) ? intval($_POST['forum']) : 0;
             $notify = !empty($_POST['notify']);
             $parentId = !empty($_POST['parent']) ? intval($_POST['parent']) : null;
 
@@ -218,6 +219,28 @@ try {
             $restResponse->setData($data);
             break;
 
+        case Rest::SAVE_FORUM_THREAD:
+            if (
+                empty($_POST['title']) || empty($_POST['text']) || empty($_POST['forum'])
+            ) {
+                throw new Exception(get_lang('NoData'));
+            }
+
+            $forumId = isset($_POST['forum']) ? intval($_POST['forum']) : 0;
+            $notify = !empty($_POST['notify']);
+
+            $threadInfo = [
+                'post_title' => $_POST['title'],
+                'forum_id' => $_POST['forum'],
+                'post_text' => nl2br($_POST['text']),
+                'post_notification' => $notify
+            ];
+
+            $data = $restApi->saveForumThread($threadInfo, $forumId);
+
+            $restResponse->setData($data);
+            break;
+
         default:
             throw new Exception(get_lang('InvalidAction'));
     }