/** * Save a new comment from submit * * @param object $comment submitted data from form. * @return array result array to use for ajax and non ajax request. */ public function save_comment_from_submit($comment, $course) { global $USER, $DB; // Ensure that post exists and get the right courseid. if (!($post = $DB->get_record('format_socialwall_posts', array('id' => $comment->postid)))) { print_error('invalidpostid', 'format_socialwall'); } // ... if post is locked or user has no cap to save, don't save the comment. $coursecontext = \context_course::instance($post->courseid); if (!has_capability('format/socialwall:writecomment', $coursecontext)) { print_error('missingcapwritecomment', 'format_socialwall'); } $comment->courseid = $post->courseid; $comment->fromuserid = $USER->id; $comment->timecreated = time(); $comment->timemodified = $comment->timecreated; // ... check if post is locked. if ($post->locked != 0) { print_error('postislocked', 'format_socialwall'); } // ...insert comment. if (!($comment->id = $DB->insert_record('format_socialwall_comments', $comment))) { print_error('commentsaveerror', 'format_socialwall'); } $result = array('error' => '0', 'message' => 'commentsaved', 'commentid' => $comment->id, 'postid' => $comment->postid, 'replycommentid' => $comment->replycommentid, 'countlikes' => 0, 'countcomments' => 0); $posts = posts::instance($comment->courseid); if ($post = $posts->refresh_comments_count($comment->postid)) { $result['countlikes'] = $post->countlikes; $result['countcomments'] = $post->countcomments; } // If this new comment is a reply update the countreplies attribute. if ($comment->replycommentid > 0) { $result['countreplies'] = $this->refresh_replies_count($comment->replycommentid); } // We use a instant enqueueing, if needed you might use events here. notification::enqueue_comment_created($comment); return $result; }