/** * Does all the grunt work for adding a discussion * * @param object $course * @param object $cm * @param object $forum * @param \context_module $context * @param array $options These override default post values, EG: set the post message with this * @return json_response */ public function handle_add_discussion($course, $cm, $forum, $context, array $options) { global $PAGE, $OUTPUT; $uploader = new upload_file(new attachments($forum, $context), \mod_hsuforum_post_form::attachment_options($forum)); $discussion = $this->create_discussion_object($forum, $context, $options); $errors = $this->validate_discussion($cm, $forum, $context, $discussion, $uploader); if (!empty($errors)) { /** @var \mod_hsuforum_renderer $renderer */ $renderer = $PAGE->get_renderer('mod_hsuforum'); return new json_response((object) array('errors' => true, 'html' => $renderer->validation_errors($errors))); } $this->save_discussion($discussion, $uploader); $this->trigger_discussion_created($course, $context, $cm, $forum, $discussion); $message = get_string('postaddedsuccess', 'hsuforum'); $renderer = $PAGE->get_renderer('mod_hsuforum'); return new json_response((object) array('eventaction' => 'discussioncreated', 'discussionid' => (int) $discussion->id, 'livelog' => $message, 'notificationhtml' => $OUTPUT->notification($message, 'notifysuccess'), 'html' => $renderer->render_discussionsview($forum))); }
/** * If successful, this function returns the name of the file * * @global object * @param object $post is a full post record, including course and forum * @param object $forum * @param object $cm * @param mixed $mform * @param string $unused * @param \mod_hsuforum\upload_file $uploader * @return bool */ function hsuforum_add_attachment($post, $forum, $cm, $mform = null, $unused = null, \mod_hsuforum\upload_file $uploader = null) { global $DB; if ($uploader instanceof \mod_hsuforum\upload_file) { $files = $uploader->process_file_upload($post->id); $DB->set_field('hsuforum_posts', 'attachment', empty($files) ? 0 : 1, array('id' => $post->id)); return true; } if (empty($mform)) { return false; } if (empty($post->attachments)) { return true; // Nothing to do } $context = context_module::instance($cm->id); $info = file_get_draft_area_info($post->attachments); $present = $info['filecount'] > 0 ? '1' : ''; file_save_draft_area_files($post->attachments, $context->id, 'mod_hsuforum', 'attachment', $post->id, mod_hsuforum_post_form::attachment_options($forum)); $DB->set_field('hsuforum_posts', 'attachment', $present, array('id' => $post->id)); return true; }
print_error('invalidcoursemodule'); } $modcontext = context_module::instance($cm->id); require_login($course, false, $cm); if (isguestuser()) { // just in case print_error('noguest'); } if (!isset($forum->maxattachments)) { // TODO - delete this once we add a field to the forum table $forum->maxattachments = 3; } $thresholdwarning = hsuforum_check_throttling($forum, $cm); $mform_post = new mod_hsuforum_post_form('post.php', array('course' => $course, 'cm' => $cm, 'coursecontext' => $coursecontext, 'modcontext' => $modcontext, 'forum' => $forum, 'post' => $post, 'thresholdwarning' => $thresholdwarning, 'edit' => $edit), 'post', '', array('id' => 'mformhsuforum')); $draftitemid = file_get_submitted_draft_itemid('attachments'); file_prepare_draft_area($draftitemid, $modcontext->id, 'mod_hsuforum', 'attachment', empty($post->id) ? null : $post->id, mod_hsuforum_post_form::attachment_options($forum)); //load data into form NOW! if ($USER->id != $post->userid) { // Not the original author, so add a message to the end $data = new stdClass(); $data->date = userdate($post->modified); if ($post->messageformat == FORMAT_HTML) { $data->name = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $USER->id . '&course=' . $post->course . '">' . fullname($USER) . '</a>'; $post->message .= '<p class="edited">(' . get_string('editedby', 'hsuforum', $data) . ')</p>'; } else { $data->name = fullname($USER); $post->message .= "\n\n(" . get_string('editedby', 'hsuforum', $data) . ')'; } unset($data); } $formheading = '';
/** * 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))); }