Пример #1
0
/**
 * 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;
}
 /**
  * Save the discussion to the DB
  *
  * @param object $discussion
  * @param upload_file $uploader
  */
 public function save_discussion($discussion, upload_file $uploader)
 {
     $message = '';
     $discussion->id = hsuforum_add_discussion($discussion, null, $message);
     $file = $uploader->process_file_upload($discussion->firstpost);
     if (!is_null($file)) {
         $this->db->set_field('hsuforum_posts', 'attachment', 1, array('id' => $discussion->firstpost));
     }
 }
 /**
  * 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;
 }