Пример #1
0
// from now on user must be logged on properly
if (!($cm = get_coursemodule_from_instance('twf', $twf->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($twf->maxattachments)) {
    // TODO - delete this once we add a field to the twf table
    $twf->maxattachments = 3;
}
$thresholdwarning = twf_check_throttling($twf, $cm);
//var_dump($syx_newdiscussion);die;
$mform_post = new mod_twf_post_form('post.php', array('course' => $course, 'cm' => $cm, 'coursecontext' => $coursecontext, 'modcontext' => $modcontext, 'twf' => $twf, 'post' => $post, 'subscribe' => \mod_twf\subscriptions::is_subscribed($USER->id, $twf, null, $cm), 'thresholdwarning' => $thresholdwarning, 'edit' => $edit, 'syx_newdiscussion' => $syx_newdiscussion, 'syx_instance' => $syx_instance, 'syx_phase' => $syx_phase, 'syx_teamwork' => $syx_teamwork), 'post', '', array('id' => 'mformtwf'));
$draftitemid = file_get_submitted_draft_itemid('attachments');
file_prepare_draft_area($draftitemid, $modcontext->id, 'mod_twf', 'attachment', empty($post->id) ? null : $post->id, mod_twf_post_form::attachment_options($twf));
//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><span class="edited">(' . get_string('editedby', 'twf', $data) . ')</span></p>';
    } else {
        $data->name = fullname($USER);
        $post->message .= "\n\n(" . get_string('editedby', 'twf', $data) . ')';
Пример #2
0
 /**
  * Process a message received and validated by the Inbound Message processor.
  *
  * @throws \core\message\inbound\processing_failed_exception
  * @param \stdClass $messagedata The Inbound Message record
  * @param \stdClass $messagedata The message data packet
  * @return bool Whether the message was successfully processed.
  */
 public function process_message(\stdClass $record, \stdClass $messagedata)
 {
     global $DB, $USER;
     // Load the post being replied to.
     $post = $DB->get_record('twf_posts', array('id' => $record->datavalue));
     if (!$post) {
         mtrace("--> Unable to find a post matching with id {$record->datavalue}");
         return false;
     }
     // Load the discussion that this post is in.
     $discussion = $DB->get_record('twf_discussions', array('id' => $post->discussion));
     if (!$post) {
         mtrace("--> Unable to find the discussion for post {$record->datavalue}");
         return false;
     }
     // Load the other required data.
     $twf = $DB->get_record('twf', array('id' => $discussion->twf));
     $course = $DB->get_record('course', array('id' => $twf->course));
     $cm = get_fast_modinfo($course->id)->instances['twf'][$twf->id];
     $modcontext = \context_module::instance($cm->id);
     $usercontext = \context_user::instance($USER->id);
     // Make sure user can post in this discussion.
     $canpost = true;
     if (!twf_user_can_post($twf, $discussion, $USER, $cm, $course, $modcontext)) {
         $canpost = false;
     }
     if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
         $groupmode = $cm->groupmode;
     } else {
         $groupmode = $course->groupmode;
     }
     if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $modcontext)) {
         if ($discussion->groupid == -1) {
             $canpost = false;
         } else {
             if (!groups_is_member($discussion->groupid)) {
                 $canpost = false;
             }
         }
     }
     if (!$canpost) {
         $data = new \stdClass();
         $data->twf = $twf;
         throw new \core\message\inbound\processing_failed_exception('messageinboundnoposttwf', 'mod_twf', $data);
     }
     // And check the availability.
     if (!\core_availability\info_module::is_user_visible($cm)) {
         $data = new \stdClass();
         $data->twf = $twf;
         throw new \core\message\inbound\processing_failed_exception('messageinboundtwfhidden', 'mod_twf', $data);
     }
     // Before we add this we must check that the user will not exceed the blocking threshold.
     // This should result in an appropriate reply.
     $thresholdwarning = twf_check_throttling($twf, $cm);
     if (!empty($thresholdwarning) && !$thresholdwarning->canpost) {
         $data = new \stdClass();
         $data->twf = $twf;
         $data->message = get_string($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional);
         throw new \core\message\inbound\processing_failed_exception('messageinboundthresholdhit', 'mod_twf', $data);
     }
     $subject = clean_param($messagedata->envelope->subject, PARAM_TEXT);
     $restring = get_string('re', 'twf');
     if (strpos($subject, $discussion->name)) {
         // The discussion name is mentioned in the e-mail subject. This is probably just the standard reply. Use the
         // standard reply subject instead.
         $newsubject = $restring . ' ' . $discussion->name;
         mtrace("--> Note: Post subject matched discussion name. Optimising from {$subject} to {$newsubject}");
         $subject = $newsubject;
     } else {
         if (strpos($subject, $post->subject)) {
             // The replied-to post's subject is mentioned in the e-mail subject.
             // Use the previous post's subject instead of the e-mail subject.
             $newsubject = $post->subject;
             if (!strpos($restring, $post->subject)) {
                 // The previous post did not contain a re string, add it.
                 $newsubject = $restring . ' ' . $newsubject;
             }
             mtrace("--> Note: Post subject matched original post subject. Optimising from {$subject} to {$newsubject}");
             $subject = $newsubject;
         }
     }
     $addpost = new \stdClass();
     $addpost->course = $course->id;
     $addpost->twf = $twf->id;
     $addpost->discussion = $discussion->id;
     $addpost->modified = $messagedata->timestamp;
     $addpost->subject = $subject;
     $addpost->parent = $post->id;
     $addpost->itemid = file_get_unused_draft_itemid();
     list($message, $format) = self::remove_quoted_text($messagedata);
     $addpost->message = $message;
     $addpost->messageformat = $format;
     // We don't trust text coming from e-mail.
     $addpost->messagetrust = false;
     // Add attachments to the post.
     if (!empty($messagedata->attachments['attachment']) && count($messagedata->attachments['attachment'])) {
         $attachmentcount = count($messagedata->attachments['attachment']);
         if (empty($twf->maxattachments) || $twf->maxbytes == 1 || !has_capability('mod/twf:createattachment', $modcontext)) {
             // Attachments are not allowed.
             mtrace("--> User does not have permission to attach files in this twf. Rejecting e-mail.");
             $data = new \stdClass();
             $data->twf = $twf;
             $data->attachmentcount = $attachmentcount;
             throw new \core\message\inbound\processing_failed_exception('messageinboundattachmentdisallowed', 'mod_twf', $data);
         }
         if ($twf->maxattachments < $attachmentcount) {
             // Too many attachments.
             mtrace("--> User attached {$attachmentcount} files when only {$twf->maxattachments} where allowed. " . " Rejecting e-mail.");
             $data = new \stdClass();
             $data->twf = $twf;
             $data->attachmentcount = $attachmentcount;
             throw new \core\message\inbound\processing_failed_exception('messageinboundfilecountexceeded', 'mod_twf', $data);
         }
         $filesize = 0;
         $addpost->attachments = file_get_unused_draft_itemid();
         foreach ($messagedata->attachments['attachment'] as $attachment) {
             mtrace("--> Processing {$attachment->filename} as an attachment.");
             $this->process_attachment('*', $usercontext, $addpost->attachments, $attachment);
             $filesize += $attachment->filesize;
         }
         if ($twf->maxbytes < $filesize) {
             // Too many attachments.
             mtrace("--> User attached {$filesize} bytes of files when only {$twf->maxbytes} where allowed. " . "Rejecting e-mail.");
             $data = new \stdClass();
             $data->twf = $twf;
             $data->maxbytes = display_size($twf->maxbytes);
             $data->filesize = display_size($filesize);
             throw new \core\message\inbound\processing_failed_exception('messageinboundfilesizeexceeded', 'mod_twf', $data);
         }
     }
     // Process any files in the message itself.
     if (!empty($messagedata->attachments['inline'])) {
         foreach ($messagedata->attachments['inline'] as $attachment) {
             mtrace("--> Processing {$attachment->filename} as an inline attachment.");
             $this->process_attachment('*', $usercontext, $addpost->itemid, $attachment);
             // Convert the contentid link in the message.
             $draftfile = \moodle_url::make_draftfile_url($addpost->itemid, '/', $attachment->filename);
             $addpost->message = preg_replace('/cid:' . $attachment->contentid . '/', $draftfile, $addpost->message);
         }
     }
     // Insert the message content now.
     $addpost->id = twf_add_new_post($addpost, true);
     // Log the new post creation.
     $params = array('context' => $modcontext, 'objectid' => $addpost->id, 'other' => array('discussionid' => $discussion->id, 'twfid' => $twf->id, 'twftype' => $twf->type));
     $event = \mod_twf\event\post_created::create($params);
     $event->add_record_snapshot('twf_posts', $addpost);
     $event->add_record_snapshot('twf_discussions', $discussion);
     $event->trigger();
     mtrace("--> Created a post {$addpost->id} in {$discussion->id}.");
     return $addpost;
 }