/** * Validates the submitted discussion and any submitted files * * @param object $cm * @param object $forum * @param \context_module $context * @param object $discussion * @param upload_file $uploader * @return moodle_exception[] */ public function validate_discussion($cm, $forum, $context, $discussion, upload_file $uploader) { $errors = array(); if (!hsuforum_user_can_post_discussion($forum, $discussion->groupid, -1, $cm, $context)) { $errors[] = new \moodle_exception('nopostforum', 'hsuforum'); } $thresholdwarning = hsuforum_check_throttling($forum, $cm); if ($thresholdwarning !== false && $thresholdwarning->canpost === false) { $errors[] = new \moodle_exception($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional); } $subject = trim($discussion->subject); if (empty($subject)) { $errors[] = new \moodle_exception('subjectisrequired', 'hsuforum'); } if (hsuforum_str_empty($discussion->message)) { $errors[] = new \moodle_exception('messageisrequired', 'hsuforum'); } if ($uploader->was_file_uploaded()) { try { $uploader->validate_files(); } catch (\Exception $e) { $errors[] = $e; } } return $errors; }
/** * Get the simple edit post form * * @param object $cm * @param bool $isedit If we are editing or not * @param int $postid If editing, then the ID of the post we are editing. If * not editing, then the ID of the post we are replying to. * @param array $data Template data * @return string */ public function simple_edit_post($cm, $isedit = false, $postid = 0, array $data = array()) { global $DB, $CFG, $USER, $OUTPUT; $context = context_module::instance($cm->id); $forum = hsuforum_get_cm_forum($cm); $postuser = $USER; $ownpost = false; if ($isedit) { $param = 'edit'; $legend = get_string('editingpost', 'hsuforum'); $post = $DB->get_record('hsuforum_posts', ['id' => $postid]); if ($post->userid == $USER->id) { $ownpost = true; } else { $postuser = $DB->get_record('user', ['id' => $post->userid]); $postuser = hsuforum_anonymize_user($postuser, $forum, $post); $data['userpicture'] = $this->output->user_picture($postuser, array('link' => false, 'size' => 100)); } } else { // It is a reply, AKA new post $ownpost = true; $param = 'reply'; $legend = get_string('addareply', 'hsuforum'); $thresholdwarning = hsuforum_check_throttling($forum, $cm); if (!empty($thresholdwarning)) { $message = get_string($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional); $data['thresholdwarning'] = $OUTPUT->notification($message); if ($thresholdwarning->canpost === false) { $data['thresholdblocked'] = " hsuforum-threshold-blocked "; } } } $data += array('itemid' => 0, 'privatereply' => 0, 'reveal' => 0, 'messageformat' => FORMAT_HTML); $actionurl = new moodle_url('/mod/hsuforum/route.php', array('action' => $isedit ? 'update_post' : 'reply', $param => $postid, 'sesskey' => sesskey(), 'contextid' => $context->id, 'itemid' => $data['itemid'], 'messageformat' => $data['messageformat'])); $extrahtml = ''; if (has_capability('mod/hsuforum:allowprivate', $context, $postuser) && $forum->allowprivatereplies !== '0') { $extrahtml .= html_writer::tag('label', html_writer::checkbox('privatereply', 1, !empty($data['privatereply'])) . get_string('privatereply', 'hsuforum')); } if ($forum->anonymous && !$isedit || $forum->anonymous && $isedit && $ownpost) { $extrahtml .= html_writer::tag('label', html_writer::checkbox('reveal', 1, !empty($data['reveal'])) . get_string('reveal', 'hsuforum')); } $data += array('postid' => $isedit ? $postid : 0, 'context' => $context, 'forum' => $forum, 'actionurl' => $actionurl, 'class' => 'hsuforum-reply', 'legend' => $legend, 'extrahtml' => $extrahtml, 'subjectrequired' => $isedit, 'advancedurl' => new moodle_url('/mod/hsuforum/post.php', array($param => $postid))); return $this->simple_edit_template($data); }
// from now on user must be logged on properly 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) . ')'; }
/** * Validates the submitted post and any submitted files * * @param object $course * @param object $cm * @param object $forum * @param \context_module $context * @param object $discussion * @param object $post * @param upload_file $uploader * @return moodle_exception[] */ public function validate_post($course, $cm, $forum, $context, $discussion, $post, upload_file $uploader) { global $USER; $errors = array(); if (!hsuforum_user_can_post($forum, $discussion, null, $cm, $course, $context)) { $errors[] = new \moodle_exception('nopostforum', 'hsuforum'); } if (!empty($post->id)) { if (!($post->userid == $USER->id && (has_capability('mod/hsuforum:replypost', $context) || has_capability('mod/hsuforum:startdiscussion', $context)) || has_capability('mod/hsuforum:editanypost', $context))) { $errors[] = new \moodle_exception('cannotupdatepost', 'hsuforum'); } } if (empty($post->id)) { $thresholdwarning = hsuforum_check_throttling($forum, $cm); if ($thresholdwarning !== false && $thresholdwarning->canpost === false) { $errors[] = new \moodle_exception($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional); } } if (hsuforum_str_empty($post->subject)) { $errors[] = new \moodle_exception('subjectisrequired', 'hsuforum'); } if (hsuforum_str_empty($post->message)) { $errors[] = new \moodle_exception('messageisrequired', 'hsuforum'); } if ($post->privatereply) { if (!has_capability('mod/hsuforum:allowprivate', $context) || !$forum->allowprivatereplies) { $errors[] = new \moodle_exception('cannotmakeprivatereplies', 'hsuforum'); } } if ($uploader->was_file_uploaded()) { try { $uploader->validate_files(empty($post->id) ? 0 : $post->id); } catch (\Exception $e) { $errors[] = $e; } } return $errors; }