/**
  * @return json_response
  * @throws \coding_exception
  */
 public function delete_post_action()
 {
     global $USER, $DB, $PAGE;
     if (!AJAX_SCRIPT) {
         throw new coding_exception('This is an AJAX action and you cannot access it directly');
     }
     require_sesskey();
     $postid = required_param('postid', PARAM_INT);
     $post = $DB->get_record('hsuforum_posts', array('id' => $postid), '*', MUST_EXIST);
     $discussion = $DB->get_record('hsuforum_discussions', array('id' => $post->discussion), '*', MUST_EXIST);
     $candeleteown = $post->userid == $USER->id && has_capability('mod/hsuforum:deleteownpost', $PAGE->context);
     if (!($candeleteown || has_capability('mod/hsuforum:deleteanypost', $PAGE->context))) {
         print_error('cannotdeletepost', 'hsuforum');
     }
     $redirect = hsuforum_verify_and_delete_post($PAGE->course, $PAGE->cm, $PAGE->activityrecord, $PAGE->context, $discussion, $post);
     $html = '';
     if ($discussion->firstpost != $post->id) {
         $html = $this->discussionservice->render_full_thread($discussion->id);
         $message = get_string('postdeleted', 'hsuforum');
     } else {
         $message = get_string('deleteddiscussion', 'hsuforum');
     }
     /** @var \core_renderer $renderer */
     $renderer = $PAGE->get_renderer('core', null, RENDERER_TARGET_GENERAL);
     return new json_response(array('redirecturl' => $redirect, 'html' => $html, 'postid' => $post->id, 'livelog' => $message, 'notificationhtml' => $renderer->notification($message, 'notifysuccess'), 'discussionid' => $discussion->id));
 }
 /**
  * Does all the grunt work for updating a post
  *
  * @param object $course
  * @param object $cm
  * @param object $forum
  * @param \context_module $context
  * @param object $discussion
  * @param object $post
  * @param array $deletefiles
  * @param array $options These override default post values, EG: set the post message with this
  * @return json_response
  */
 public function handle_update_post($course, $cm, $forum, $context, $discussion, $post, array $deletefiles = array(), array $options)
 {
     $this->require_can_edit_post($forum, $context, $discussion, $post);
     $uploader = new upload_file(new attachments($forum, $context, $deletefiles), \mod_hsuforum_post_form::attachment_options($forum));
     // Apply updates to the post.
     foreach ($options as $name => $value) {
         if (property_exists($post, $name)) {
             $post->{$name} = $value;
         }
     }
     $post->itemid = empty($options['itemid']) ? 0 : $options['itemid'];
     $errors = $this->validate_post($course, $cm, $forum, $context, $discussion, $post, $uploader);
     if (!empty($errors)) {
         return $this->create_error_response($errors);
     }
     $this->save_post($discussion, $post, $uploader);
     // If the user has access to all groups and they are changing the group, then update the post.
     if (empty($post->parent) && has_capability('mod/hsuforum:movediscussions', $context)) {
         $this->db->set_field('hsuforum_discussions', 'groupid', $options['groupid'], array('id' => $discussion->id));
     }
     $this->trigger_post_updated($context, $forum, $discussion, $post);
     return new json_response((object) array('eventaction' => 'postupdated', 'discussionid' => (int) $discussion->id, 'postid' => (int) $post->id, 'livelog' => get_string('postwasupdated', 'hsuforum'), 'html' => $this->discussionservice->render_full_thread($discussion->id)));
 }