echo $OUTPUT->header(); echo $OUTPUT->heading(format_string($twf->name), 2); echo $OUTPUT->heading(format_string($discussion->name), 3, 'discussionname'); // is_guest should be used here as this also checks whether the user is a guest in the current course. // Guests and visitors cannot subscribe - only enrolled users. if (!is_guest($modcontext, $USER) && isloggedin() && has_capability('mod/twf:viewdiscussion', $modcontext)) { // Discussion subscription. if (\mod_twf\subscriptions::is_subscribable($twf)) { echo html_writer::div(twf_get_discussion_subscription_icon($twf, $post->discussion, null, true), 'discussionsubscription'); echo twf_get_discussion_subscription_icon_preloaders(); } } /// Check to see if groups are being used in this twf /// If so, make sure the current person is allowed to see this discussion /// Also, if we know they should be able to reply, then explicitly set $canreply for performance reasons $canreply = twf_user_can_post($twf, $discussion, $USER, $cm, $course, $modcontext); if (!$canreply and $twf->type !== 'news') { if (isguestuser() or !isloggedin()) { $canreply = true; } if (!is_enrolled($modcontext) and !is_viewing($modcontext)) { // allow guests and not-logged-in to see the link - they are prompted to log in after clicking the link // normal users with temporary guest access see this link too, they are asked to enrol instead $canreply = enrol_selfenrol_available($course->id); } } // Output the links to neighbour discussions. $neighbours = twf_get_discussion_neighbours($cm, $discussion); $neighbourlinks = $renderer->neighbouring_discussion_navigation($neighbours['prev'], $neighbours['next']); echo $neighbourlinks; /// Print the controls across the top
/** * Returns a list of twf posts for a discussion * * @param int $discussionid the post ids * @param string $sortby sort by this element (id, created or modified) * @param string $sortdirection sort direction: ASC or DESC * * @return array the twf post details * @since Moodle 2.7 */ public static function get_twf_discussion_posts($discussionid, $sortby = "created", $sortdirection = "DESC") { global $CFG, $DB, $USER; $posts = array(); $warnings = array(); // Validate the parameter. $params = self::validate_parameters(self::get_twf_discussion_posts_parameters(), array('discussionid' => $discussionid, 'sortby' => $sortby, 'sortdirection' => $sortdirection)); // Compact/extract functions are not recommended. $discussionid = $params['discussionid']; $sortby = $params['sortby']; $sortdirection = $params['sortdirection']; $sortallowedvalues = array('id', 'created', 'modified'); if (!in_array($sortby, $sortallowedvalues)) { throw new invalid_parameter_exception('Invalid value for sortby parameter (value: ' . $sortby . '),' . 'allowed values are: ' . implode(',', $sortallowedvalues)); } $sortdirection = strtoupper($sortdirection); $directionallowedvalues = array('ASC', 'DESC'); if (!in_array($sortdirection, $directionallowedvalues)) { throw new invalid_parameter_exception('Invalid value for sortdirection parameter (value: ' . $sortdirection . '),' . 'allowed values are: ' . implode(',', $directionallowedvalues)); } $discussion = $DB->get_record('twf_discussions', array('id' => $discussionid), '*', MUST_EXIST); $twf = $DB->get_record('twf', array('id' => $discussion->twf), '*', MUST_EXIST); $course = $DB->get_record('course', array('id' => $twf->course), '*', MUST_EXIST); $cm = get_coursemodule_from_instance('twf', $twf->id, $course->id, false, MUST_EXIST); // Validate the module context. It checks everything that affects the module visibility (including groupings, etc..). $modcontext = context_module::instance($cm->id); self::validate_context($modcontext); // This require must be here, see mod/twf/discuss.php. require_once $CFG->dirroot . "/mod/twf/lib.php"; // Check they have the view twf capability. require_capability('mod/twf:viewdiscussion', $modcontext, null, true, 'noviewdiscussionspermission', 'twf'); if (!($post = twf_get_post_full($discussion->firstpost))) { throw new moodle_exception('notexists', 'twf'); } // This function check groups, qanda, timed discussions, etc. if (!twf_user_can_see_post($twf, $discussion, $post, null, $cm)) { throw new moodle_exception('noviewdiscussionspermission', 'twf'); } $canviewfullname = has_capability('moodle/site:viewfullnames', $modcontext); // We will add this field in the response. $canreply = twf_user_can_post($twf, $discussion, $USER, $cm, $course, $modcontext); $twftracked = twf_tp_is_tracked($twf); $sort = 'p.' . $sortby . ' ' . $sortdirection; $allposts = twf_get_all_discussion_posts($discussion->id, $sort, $twftracked); foreach ($allposts as $post) { if (!twf_user_can_see_post($twf, $discussion, $post, null, $cm)) { $warning = array(); $warning['item'] = 'post'; $warning['itemid'] = $post->id; $warning['warningcode'] = '1'; $warning['message'] = 'You can\'t see this post'; $warnings[] = $warning; continue; } // Function twf_get_all_discussion_posts adds postread field. // Note that the value returned can be a boolean or an integer. The WS expects a boolean. if (empty($post->postread)) { $post->postread = false; } else { $post->postread = true; } $post->canreply = $canreply; if (!empty($post->children)) { $post->children = array_keys($post->children); } else { $post->children = array(); } $user = new stdclass(); $user->id = $post->userid; $user = username_load_fields_from_object($user, $post); $post->userfullname = fullname($user, $canviewfullname); // We can have post written by users that are deleted. In this case, those users don't have a valid context. $usercontext = context_user::instance($user->id, IGNORE_MISSING); if ($usercontext) { $post->userpictureurl = moodle_url::make_webservice_pluginfile_url($usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false); } else { $post->userpictureurl = ''; } // Rewrite embedded images URLs. list($post->message, $post->messageformat) = external_format_text($post->message, $post->messageformat, $modcontext->id, 'mod_twf', 'post', $post->id); // List attachments. if (!empty($post->attachment)) { $post->attachments = array(); $fs = get_file_storage(); if ($files = $fs->get_area_files($modcontext->id, 'mod_twf', 'attachment', $post->id, "filename", false)) { foreach ($files as $file) { $filename = $file->get_filename(); $fileurl = moodle_url::make_webservice_pluginfile_url($modcontext->id, 'mod_twf', 'attachment', $post->id, '/', $filename); $post->attachments[] = array('filename' => $filename, 'mimetype' => $file->get_mimetype(), 'fileurl' => $fileurl->out(false)); } } } $posts[] = $post; } $result = array(); $result['posts'] = $posts; $result['warnings'] = $warnings; return $result; }
/** * 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; }
} if (!($twf = $DB->get_record("twf", array("id" => $discussion->twf)))) { print_error('invalidtwfid', 'twf'); } if (!($course = $DB->get_record("course", array("id" => $discussion->course)))) { print_error('invalidcourseid'); } if (!($cm = get_coursemodule_from_instance("twf", $twf->id, $course->id))) { print_error('invalidcoursemodule'); } // Ensure lang, theme, etc. is set up properly. MDL-6926 $PAGE->set_cm($cm, $course, $twf); // Retrieve the contexts. $modcontext = context_module::instance($cm->id); $coursecontext = context_course::instance($course->id); if (!twf_user_can_post($twf, $discussion, $USER, $cm, $course, $modcontext)) { if (!isguestuser()) { if (!is_enrolled($coursecontext)) { // User is a guest here! $SESSION->wantsurl = qualified_me(); $SESSION->enrolcancel = get_local_referer(false); redirect(new moodle_url('/enrol/index.php', array('id' => $course->id, 'returnurl' => '/mod/twf/view.php?f=' . $twf->id)), get_string('youneedtoenrol')); } } print_error('noposttwf', 'twf'); } // Make sure user can post here if (isset($cm->groupmode) && empty($course->groupmodeforce)) { $groupmode = $cm->groupmode; } else { $groupmode = $course->groupmode;
/** * Prints a twf discussion * * @uses CONTEXT_MODULE * @uses FORUM_MODE_FLATNEWEST * @uses FORUM_MODE_FLATOLDEST * @uses FORUM_MODE_THREADED * @uses FORUM_MODE_NESTED * @param stdClass $course * @param stdClass $cm * @param stdClass $twf * @param stdClass $discussion * @param stdClass $post * @param int $mode * @param mixed $canreply * @param bool $canrate */ function twf_print_discussion($course, $cm, $twf, $discussion, $post, $mode, $canreply = NULL, $canrate = false) { global $USER, $CFG; require_once $CFG->dirroot . '/rating/lib.php'; $ownpost = isloggedin() && $USER->id == $post->userid; $modcontext = context_module::instance($cm->id); if ($canreply === NULL) { $reply = twf_user_can_post($twf, $discussion, $USER, $cm, $course, $modcontext); } else { $reply = $canreply; } // $cm holds general cache for twf functions $cm->cache = new stdClass(); $cm->cache->groups = groups_get_all_groups($course->id, 0, $cm->groupingid); $cm->cache->usersgroups = array(); $posters = array(); // preload all posts - TODO: improve... if ($mode == FORUM_MODE_FLATNEWEST) { $sort = "p.created DESC"; } else { $sort = "p.created ASC"; } $twftracked = twf_tp_is_tracked($twf); $posts = twf_get_all_discussion_posts($discussion->id, $sort, $twftracked); $post = $posts[$post->id]; foreach ($posts as $pid => $p) { $posters[$p->userid] = $p->userid; } // preload all groups of ppl that posted in this discussion if ($postersgroups = groups_get_all_groups($course->id, $posters, $cm->groupingid, 'gm.id, gm.groupid, gm.userid')) { foreach ($postersgroups as $pg) { if (!isset($cm->cache->usersgroups[$pg->userid])) { $cm->cache->usersgroups[$pg->userid] = array(); } $cm->cache->usersgroups[$pg->userid][$pg->groupid] = $pg->groupid; } unset($postersgroups); } //load ratings if ($twf->assessed != RATING_AGGREGATE_NONE) { $ratingoptions = new stdClass(); $ratingoptions->context = $modcontext; $ratingoptions->component = 'mod_twf'; $ratingoptions->ratingarea = 'post'; $ratingoptions->items = $posts; $ratingoptions->aggregate = $twf->assessed; //the aggregation method $ratingoptions->scaleid = $twf->scale; $ratingoptions->userid = $USER->id; if ($twf->type == 'single' or !$discussion->id) { $ratingoptions->returnurl = "{$CFG->wwwroot}/mod/twf/view.php?id={$cm->id}"; } else { $ratingoptions->returnurl = "{$CFG->wwwroot}/mod/twf/discuss.php?d={$discussion->id}"; } $ratingoptions->assesstimestart = $twf->assesstimestart; $ratingoptions->assesstimefinish = $twf->assesstimefinish; $rm = new rating_manager(); $posts = $rm->get_ratings($ratingoptions); } $post->twf = $twf->id; // Add the twf id to the post object, later used by twf_print_post $post->twftype = $twf->type; $post->subject = format_string($post->subject); $postread = !empty($post->postread); twf_print_post($post, $discussion, $twf, $cm, $course, $ownpost, $reply, false, '', '', $postread, true, $twftracked); switch ($mode) { case FORUM_MODE_FLATOLDEST: case FORUM_MODE_FLATNEWEST: default: twf_print_posts_flat($course, $cm, $twf, $discussion, $post, $mode, $reply, $twftracked, $posts); break; case FORUM_MODE_THREADED: twf_print_posts_threaded($course, $cm, $twf, $discussion, $post, 0, $reply, $twftracked, $posts); break; case FORUM_MODE_NESTED: twf_print_posts_nested($course, $cm, $twf, $discussion, $post, $reply, $twftracked, $posts); break; } }