/** * Returns a list of quora 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 quora post details * @since Moodle 2.7 */ public static function get_quora_discussion_posts($discussionid, $sortby = "created", $sortdirection = "DESC") { global $CFG, $DB, $USER; $posts = array(); $warnings = array(); // Validate the parameter. $params = self::validate_parameters(self::get_quora_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('quora_discussions', array('id' => $discussionid), '*', MUST_EXIST); $quora = $DB->get_record('quora', array('id' => $discussion->quora), '*', MUST_EXIST); $course = $DB->get_record('course', array('id' => $quora->course), '*', MUST_EXIST); $cm = get_coursemodule_from_instance('quora', $quora->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/quora/discuss.php. require_once $CFG->dirroot . "/mod/quora/lib.php"; // Check they have the view quora capability. require_capability('mod/quora:viewdiscussion', $modcontext, null, true, 'noviewdiscussionspermission', 'quora'); if (!($post = quora_get_post_full($discussion->firstpost))) { throw new moodle_exception('notexists', 'quora'); } // This function check groups, qanda, timed discussions, etc. if (!quora_user_can_see_post($quora, $discussion, $post, null, $cm)) { throw new moodle_exception('noviewdiscussionspermission', 'quora'); } $canviewfullname = has_capability('moodle/site:viewfullnames', $modcontext); // We will add this field in the response. $canreply = quora_user_can_post($quora, $discussion, $USER, $cm, $course, $modcontext); $quoratracked = quora_tp_is_tracked($quora); $sort = 'p.' . $sortby . ' ' . $sortdirection; $allposts = quora_get_all_discussion_posts($discussion->id, $sort, $quoratracked); foreach ($allposts as $post) { if (!quora_user_can_see_post($quora, $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 quora_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_quora', '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_quora', 'attachment', $post->id, "filename", false)) { foreach ($files as $file) { $filename = $file->get_filename(); $fileurl = moodle_url::make_webservice_pluginfile_url($modcontext->id, 'mod_quora', '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; }
// Trigger discussion viewed event. quora_discussion_view($modcontext, $quora, $discussion); unset($SESSION->fromdiscussion); if ($mode) { set_user_preference('quora_displaymode', $mode); } $displaymode = get_user_preferences('quora_displaymode', $CFG->quora_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 = quora_get_post_full($parent))) { print_error("notexists", 'quora', "{$CFG->wwwroot}/mod/quora/view.php?f={$quora->id}"); } if (!quora_user_can_see_post($quora, $discussion, $post, null, $cm)) { print_error('noviewdiscussionspermission', 'quora', "{$CFG->wwwroot}/mod/quora/view.php?id={$quora->id}"); } if ($mark == 'read' or $mark == 'unread') { if ($CFG->quora_usermarksread && quora_tp_can_track_quoras($quora) && quora_tp_is_tracked($quora)) { if ($mark == 'read') { quora_tp_add_read_record($USER->id, $postid); } else { // unread quora_tp_delete_read_records($USER->id, $postid); } } }
} if (!empty($quora->blockafter) && !empty($quora->blockperiod)) { $a = new stdClass(); $a->blockafter = $quora->blockafter; $a->blockperiod = get_string('secondstotime' . $quora->blockperiod); echo $OUTPUT->notification(get_string('thisquoraisthrottled', 'quora', $a)); } if ($quora->type == 'qanda' && !has_capability('moodle/course:manageactivities', $context)) { echo $OUTPUT->notification(get_string('qandanotify', 'quora')); } switch ($quora->type) { case 'single': if (!empty($discussions) && count($discussions) > 1) { echo $OUTPUT->notification(get_string('warnformorepost', 'quora')); } if (!($post = quora_get_post_full($discussion->firstpost))) { print_error('cannotfindfirstpost', 'quora'); } if ($mode) { set_user_preference("quora_displaymode", $mode); } $canreply = quora_user_can_post($quora, $discussion, $USER, $cm, $course, $context); $canrate = has_capability('mod/quora:rate', $context); $displaymode = get_user_preferences("quora_displaymode", $CFG->quora_displaymode); echo ' '; // this should fix the floating in FF quora_print_discussion($course, $cm, $quora, $discussion, $post, $displaymode, $canreply, $canrate); break; case 'eachuser': echo '<p class="mdl-align">'; if (quora_user_can_post_discussion($quora, null, -1, $cm)) {
$posts = quora_get_all_discussion_posts($discussion->id, "created ASC", $quoratracked); quora_print_posts_nested($course, $cm, $quora, $discussion, $post, false, false, $quoratracked, $posts); } } else { echo $OUTPUT->header(); echo $OUTPUT->heading(format_string($quora->name), 2); echo $OUTPUT->confirm(get_string("deletesure", "quora", $replycount), "post.php?delete={$delete}&confirm={$delete}", $CFG->wwwroot . '/mod/quora/discuss.php?d=' . $post->discussion . '#p' . $post->id); quora_print_post($post, $discussion, $quora, $cm, $course, false, false, false); } } echo $OUTPUT->footer(); die; } else { if (!empty($prune)) { // Pruning if (!($post = quora_get_post_full($prune))) { print_error('invalidpostid', 'quora'); } if (!($discussion = $DB->get_record("quora_discussions", array("id" => $post->discussion)))) { print_error('notpartofdiscussion', 'quora'); } if (!($quora = $DB->get_record("quora", array("id" => $discussion->quora)))) { print_error('invalidquoraid', 'quora'); } if ($quora->type == 'single') { print_error('cannotsplit', 'quora'); } if (!$post->parent) { print_error('alreadyfirstpost', 'quora'); } if (!($cm = get_coursemodule_from_instance("quora", $quora->id, $quora->course))) {