public function prepare_message_for_edit($cm, $post)
 {
     $this->append_edited_by($post);
     $context = \context_module::instance($cm->id);
     $post = trusttext_pre_edit($post, 'message', $context);
     $itemid = file_get_submitted_draft_itemid('message');
     $message = file_prepare_draft_area($itemid, $context->id, 'mod_hsuforum', 'post', $post->id, \mod_hsuforum_post_form::editor_options($context, $post->id), $post->message);
     return array($message, $itemid);
 }
 /**
  * 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)));
 }
Пример #3
0
if (!($cm = get_coursemodule_from_instance('hsuforum', $forum->id, $course->id))) {
    // For the logs
    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);
Пример #4
0
/**
 * Given an object containing all the necessary data,
 * create a new discussion and return the id
 *
 * @param object $post
 * @param mixed $mform
 * @param string $unused
 * @param int $userid
 * @param \mod_hsuforum\upload_file $uploader
 * @return object
 */
function hsuforum_add_discussion($discussion, $mform = null, $unused = null, $userid = null, \mod_hsuforum\upload_file $uploader = null)
{
    global $USER, $CFG, $DB;
    $timenow = time();
    if (is_null($userid)) {
        $userid = $USER->id;
    }
    // The first post is stored as a real post, and linked
    // to from the discuss entry.
    $forum = $DB->get_record('hsuforum', array('id' => $discussion->forum));
    $cm = get_coursemodule_from_instance('hsuforum', $forum->id);
    $post = new stdClass();
    $post->discussion = 0;
    $post->parent = 0;
    $post->userid = $userid;
    $post->created = $timenow;
    $post->modified = $timenow;
    $post->mailed = HSUFORUM_MAILED_PENDING;
    $post->subject = $discussion->name;
    $post->message = $discussion->message;
    $post->messageformat = $discussion->messageformat;
    $post->messagetrust = $discussion->messagetrust;
    $post->attachments = isset($discussion->attachments) ? $discussion->attachments : null;
    $post->forum = $forum->id;
    // speedup
    $post->course = $forum->course;
    // speedup
    $post->mailnow = $discussion->mailnow;
    $post->reveal = $discussion->reveal;
    if (!is_null($mform)) {
        $data = $mform->get_data();
        if (!empty($data->reveal)) {
            $post->reveal = 1;
        }
    }
    $post->id = $DB->insert_record("hsuforum_posts", $post);
    // TODO: Fix the calling code so that there always is a $cm when this function is called
    if (!empty($cm->id) && !empty($discussion->itemid)) {
        // In "single simple discussions" this may not exist yet
        $context = context_module::instance($cm->id);
        $text = file_save_draft_area_files($discussion->itemid, $context->id, 'mod_hsuforum', 'post', $post->id, mod_hsuforum_post_form::editor_options($context, null), $post->message);
        $DB->set_field('hsuforum_posts', 'message', $text, array('id' => $post->id));
    }
    // Now do the main entry for the discussion, linking to this first post
    $discussion->firstpost = $post->id;
    $discussion->timemodified = $timenow;
    $discussion->usermodified = $post->userid;
    $discussion->userid = $userid;
    $discussion->assessed = 0;
    $post->discussion = $DB->insert_record("hsuforum_discussions", $discussion);
    // Finally, set the pointer on the post.
    $DB->set_field("hsuforum_posts", "discussion", $post->discussion, array("id" => $post->id));
    if (!empty($cm->id)) {
        hsuforum_add_attachment($post, $forum, $cm, $mform, $unused, $uploader);
    }
    hsuforum_mark_post_read($post->userid, $post, $post->forum);
    // Let Moodle know that assessable content is uploaded (eg for plagiarism detection)
    if (!empty($cm->id)) {
        hsuforum_trigger_content_uploaded_event($post, $cm, 'hsuforum_add_discussion');
    }
    return $post->discussion;
}
 /**
  * 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)));
 }