diff --git a/main/inc/lib/notebook.lib.php b/main/inc/lib/notebook.lib.php index 9259e8db60..97c5ed2560 100755 --- a/main/inc/lib/notebook.lib.php +++ b/main/inc/lib/notebook.lib.php @@ -41,28 +41,32 @@ class NotebookManager * This functions stores the note in the database * * @param array $values + * @param int $userId Optional. The user ID + * @param int $courseId Optional. The course ID + * @param int $sessionId Optional. The session ID * @return bool * @author Christian Fasanando * @author Patrick Cool , Ghent University, Belgium * @version januari 2009, dokeos 1.8.6 - * */ - static function save_note($values) + static function save_note($values, $userId = 0, $courseId = 0, $sessionId = 0) { if (!is_array($values) or empty($values['note_title'])) { return false; } // Database table definition $table = Database :: get_course_table(TABLE_NOTEBOOK); - $course_id = api_get_course_int_id(); - $sessionId = api_get_session_id(); - + $userId = $userId ?: api_get_user_id(); + $courseId = $courseId ?: api_get_course_int_id(); + $courseInfo = api_get_course_info_by_id($courseId); + $courseCode = $courseInfo['code']; + $sessionId = $sessionId ?: api_get_session_id(); $now = api_get_utc_datetime(); $params = [ 'notebook_id' => 0, - 'c_id' => $course_id, - 'user_id' => api_get_user_id(), - 'course' => api_get_course_id(), + 'c_id' => $courseId, + 'user_id' => $userId, + 'course' => $courseCode, 'session_id' => $sessionId, 'title' => $values['note_title'], 'description' => $values['note_comment'], @@ -77,13 +81,8 @@ class NotebookManager Database::query($sql); //insert into item_property - api_item_property_update( - api_get_course_info(), - TOOL_NOTEBOOK, - $id, - 'NotebookAdded', - api_get_user_id() - ); + api_item_property_update($courseInfo, TOOL_NOTEBOOK, $id, 'NotebookAdded', $userId); + return $id; } } diff --git a/main/inc/lib/webservices/Rest.php b/main/inc/lib/webservices/Rest.php index 81853057cb..dc141c455e 100644 --- a/main/inc/lib/webservices/Rest.php +++ b/main/inc/lib/webservices/Rest.php @@ -38,6 +38,7 @@ class Rest extends WebService const GET_USER_SESSIONS = 'user_sessions'; const SAVE_USER_MESSAGE = 'save_user_message'; const GET_MESSAGE_USERS = 'message_users'; + const SAVE_COURSE_NOTEBOOK = 'save_course_notebook'; const EXTRAFIELD_GCM_ID = 'gcm_registration_id'; @@ -948,4 +949,26 @@ class Rest extends WebService return $data; } + + /** + * @param string $title + * @param string $text + * @return bool + */ + public function saveCourseNotebook($title, $text) + { + $values = ['note_title' => $title, 'note_comment' => $text]; + $sessionId = $this->session ? $this->session->getId() : 0; + + $noteBookId = NotebookManager::save_note( + $values, + $this->user->getId(), + $this->course->getId(), + $sessionId + ); + + return [ + 'registered' => $noteBookId + ]; + } } diff --git a/main/webservices/api/v2.php b/main/webservices/api/v2.php index 139d715285..5658a1fb8e 100644 --- a/main/webservices/api/v2.php +++ b/main/webservices/api/v2.php @@ -209,6 +209,15 @@ try { $restResponse->setData($data); break; + case Rest::SAVE_COURSE_NOTEBOOK: + $title = !empty($_POST['title'])? $_POST['title'] : null; + $text = !empty($_POST['text'])? $_POST['text'] : null; + + $data = $restApi->saveCourseNotebook($title, $text); + + $restResponse->setData($data); + break; + default: throw new Exception(get_lang('InvalidAction')); }