示例#1
0
 /**
  * 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;
 }
示例#2
0
// Trigger discussion viewed event.
twf_discussion_view($modcontext, $twf, $discussion);
unset($SESSION->fromdiscussion);
if ($mode) {
    set_user_preference('twf_displaymode', $mode);
}
$displaymode = get_user_preferences('twf_displaymode', $CFG->twf_displaymode);
if ($parent) {
    // If flat AND parent, then force nested display this time
    if ($displaymode == FORUM_MODE_FLATOLDEST or $displaymode == FORUM_MODE_FLATNEWEST) {
        $displaymode = FORUM_MODE_NESTED;
    }
} else {
    $parent = $discussion->firstpost;
}
if (!($post = twf_get_post_full($parent))) {
    print_error("notexists", 'twf', "{$CFG->wwwroot}/mod/twf/view.php?f={$twf->id}");
}
if (!twf_user_can_see_post($twf, $discussion, $post, null, $cm)) {
    print_error('noviewdiscussionspermission', 'twf', "{$CFG->wwwroot}/mod/twf/view.php?id={$twf->id}");
}
if ($mark == 'read' or $mark == 'unread') {
    if ($CFG->twf_usermarksread && twf_tp_can_track_twfs($twf) && twf_tp_is_tracked($twf)) {
        if ($mark == 'read') {
            twf_tp_add_read_record($USER->id, $postid);
        } else {
            // unread
            twf_tp_delete_read_records($USER->id, $postid);
        }
    }
}
示例#3
0
                 $posts = twf_get_all_discussion_posts($discussion->id, "created ASC", $twftracked);
                 twf_print_posts_nested($course, $cm, $twf, $discussion, $post, false, false, $twftracked, $posts);
             }
         } else {
             echo $OUTPUT->header();
             echo $OUTPUT->heading(format_string($twf->name), 2);
             echo $OUTPUT->confirm(get_string("deletesure", "twf", $replycount), "post.php?delete={$delete}&confirm={$delete}", $CFG->wwwroot . '/mod/twf/discuss.php?d=' . $post->discussion . '#p' . $post->id);
             twf_print_post($post, $discussion, $twf, $cm, $course, false, false, false);
         }
     }
     echo $OUTPUT->footer();
     die;
 } else {
     if (!empty($prune)) {
         // Pruning
         if (!($post = twf_get_post_full($prune))) {
             print_error('invalidpostid', 'twf');
         }
         if (!($discussion = $DB->get_record("twf_discussions", array("id" => $post->discussion)))) {
             print_error('notpartofdiscussion', 'twf');
         }
         if (!($twf = $DB->get_record("twf", array("id" => $discussion->twf)))) {
             print_error('invalidtwfid', 'twf');
         }
         if ($twf->type == 'single') {
             print_error('cannotsplit', 'twf');
         }
         if (!$post->parent) {
             print_error('alreadyfirstpost', 'twf');
         }
         if (!($cm = get_coursemodule_from_instance("twf", $twf->id, $twf->course))) {