Beispiel #1
0
             $message .= get_string("postmailnow", "quora");
             $timemessage = 4;
         } else {
             $message .= '<p>' . get_string("postaddedsuccess", "quora") . '</p>';
             $message .= '<p>' . get_string("postaddedtimeleft", "quora", format_time($CFG->maxeditingtime)) . '</p>';
         }
         if ($quora->type == 'single') {
             // Single discussion quoras are an exception. We show
             // the quora itself since it only has one discussion
             // thread.
             $discussionurl = "view.php?f={$quora->id}";
         } else {
             $discussionurl = "discuss.php?d={$discussion->id}";
         }
         $params = array('context' => $modcontext, 'objectid' => $fromform->id, 'other' => array('discussionid' => $discussion->id, 'quoraid' => $quora->id, 'quoratype' => $quora->type));
         $event = \mod_quora\event\post_created::create($params);
         $event->add_record_snapshot('quora_posts', $fromform);
         $event->add_record_snapshot('quora_discussions', $discussion);
         $event->trigger();
         // Update completion state
         $completion = new completion_info($course);
         if ($completion->is_enabled($cm) && ($quora->completionreplies || $quora->completionposts)) {
             $completion->update_state($cm, COMPLETION_COMPLETE);
         }
         redirect(quora_go_back_to("{$discussionurl}#p{$fromform->id}"), $message . $subscribemessage, $timemessage);
     } else {
         print_error("couldnotadd", "quora", $errordestination);
     }
     exit;
 } else {
     // Adding a new discussion.
Beispiel #2
0
 /**
  * Test the post_created event for a single discussion quora.
  */
 public function test_post_created_single()
 {
     // Setup test data.
     $course = $this->getDataGenerator()->create_course();
     $quora = $this->getDataGenerator()->create_module('quora', array('course' => $course->id, 'type' => 'single'));
     $user = $this->getDataGenerator()->create_user();
     // Add a discussion.
     $record = array();
     $record['course'] = $course->id;
     $record['quora'] = $quora->id;
     $record['userid'] = $user->id;
     $discussion = $this->getDataGenerator()->get_plugin_generator('mod_quora')->create_discussion($record);
     // Add a post.
     $record = array();
     $record['discussion'] = $discussion->id;
     $record['userid'] = $user->id;
     $post = $this->getDataGenerator()->get_plugin_generator('mod_quora')->create_post($record);
     $context = context_module::instance($quora->cmid);
     $params = array('context' => $context, 'objectid' => $post->id, 'other' => array('discussionid' => $discussion->id, 'quoraid' => $quora->id, 'quoratype' => $quora->type));
     $event = \mod_quora\event\post_created::create($params);
     // Trigger and capturing the event.
     $sink = $this->redirectEvents();
     $event->trigger();
     $events = $sink->get_events();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_quora\\event\\post_created', $event);
     $this->assertEquals($context, $event->get_context());
     $expected = array($course->id, 'quora', 'add post', "view.php?f={$quora->id}#p{$post->id}", $quora->id, $quora->cmid);
     $this->assertEventLegacyLogData($expected, $event);
     $url = new \moodle_url('/mod/quora/view.php', array('f' => $quora->id));
     $url->set_anchor('p' . $event->objectid);
     $this->assertEquals($url, $event->get_url());
     $this->assertEventContextNotUsed($event);
     $this->assertNotEmpty($event->get_name());
 }
Beispiel #3
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('quora_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('quora_discussions', array('id' => $post->discussion));
     if (!$post) {
         mtrace("--> Unable to find the discussion for post {$record->datavalue}");
         return false;
     }
     // Load the other required data.
     $quora = $DB->get_record('quora', array('id' => $discussion->quora));
     $course = $DB->get_record('course', array('id' => $quora->course));
     $cm = get_fast_modinfo($course->id)->instances['quora'][$quora->id];
     $modcontext = \context_module::instance($cm->id);
     $usercontext = \context_user::instance($USER->id);
     // Make sure user can post in this discussion.
     $canpost = true;
     if (!quora_user_can_post($quora, $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->quora = $quora;
         throw new \core\message\inbound\processing_failed_exception('messageinboundnopostquora', 'mod_quora', $data);
     }
     // And check the availability.
     if (!\core_availability\info_module::is_user_visible($cm)) {
         $data = new \stdClass();
         $data->quora = $quora;
         throw new \core\message\inbound\processing_failed_exception('messageinboundquorahidden', 'mod_quora', $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 = quora_check_throttling($quora, $cm);
     if (!empty($thresholdwarning) && !$thresholdwarning->canpost) {
         $data = new \stdClass();
         $data->quora = $quora;
         $data->message = get_string($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional);
         throw new \core\message\inbound\processing_failed_exception('messageinboundthresholdhit', 'mod_quora', $data);
     }
     $subject = clean_param($messagedata->envelope->subject, PARAM_TEXT);
     $restring = get_string('re', 'quora');
     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->quora = $quora->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($quora->maxattachments) || $quora->maxbytes == 1 || !has_capability('mod/quora:createattachment', $modcontext)) {
             // Attachments are not allowed.
             mtrace("--> User does not have permission to attach files in this quora. Rejecting e-mail.");
             $data = new \stdClass();
             $data->quora = $quora;
             $data->attachmentcount = $attachmentcount;
             throw new \core\message\inbound\processing_failed_exception('messageinboundattachmentdisallowed', 'mod_quora', $data);
         }
         if ($quora->maxattachments < $attachmentcount) {
             // Too many attachments.
             mtrace("--> User attached {$attachmentcount} files when only {$quora->maxattachments} where allowed. " . " Rejecting e-mail.");
             $data = new \stdClass();
             $data->quora = $quora;
             $data->attachmentcount = $attachmentcount;
             throw new \core\message\inbound\processing_failed_exception('messageinboundfilecountexceeded', 'mod_quora', $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 ($quora->maxbytes < $filesize) {
             // Too many attachments.
             mtrace("--> User attached {$filesize} bytes of files when only {$quora->maxbytes} where allowed. " . "Rejecting e-mail.");
             $data = new \stdClass();
             $data->quora = $quora;
             $data->maxbytes = display_size($quora->maxbytes);
             $data->filesize = display_size($filesize);
             throw new \core\message\inbound\processing_failed_exception('messageinboundfilesizeexceeded', 'mod_quora', $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 = quora_add_new_post($addpost, true);
     // Log the new post creation.
     $params = array('context' => $modcontext, 'objectid' => $addpost->id, 'other' => array('discussionid' => $discussion->id, 'quoraid' => $quora->id, 'quoratype' => $quora->type));
     $event = \mod_quora\event\post_created::create($params);
     $event->add_record_snapshot('quora_posts', $addpost);
     $event->add_record_snapshot('quora_discussions', $discussion);
     $event->trigger();
     mtrace("--> Created a post {$addpost->id} in {$discussion->id}.");
     return $addpost;
 }