/** * 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; }
/** * @global object */ public function load_data() { global $DB; if ($this->postid) { if (!($this->post = $DB->get_record('quora_posts', array('id' => $this->postid)))) { throw new portfolio_caller_exception('invalidpostid', 'quora'); } } $dparams = array(); if ($this->discussionid) { $dbparams = array('id' => $this->discussionid); } else { if ($this->post) { $dbparams = array('id' => $this->post->discussion); } else { throw new portfolio_caller_exception('mustprovidediscussionorpost', 'quora'); } } if (!($this->discussion = $DB->get_record('quora_discussions', $dbparams))) { throw new portfolio_caller_exception('invaliddiscussionid', 'quora'); } if (!($this->quora = $DB->get_record('quora', array('id' => $this->discussion->quora)))) { throw new portfolio_caller_exception('invalidquoraid', 'quora'); } if (!($this->cm = get_coursemodule_from_instance('quora', $this->quora->id))) { throw new portfolio_caller_exception('invalidcoursemodule'); } $this->modcontext = context_module::instance($this->cm->id); $fs = get_file_storage(); if ($this->post) { if ($this->attachment) { $this->set_file_and_format_data($this->attachment); } else { $attach = $fs->get_area_files($this->modcontext->id, 'mod_quora', 'attachment', $this->post->id, 'timemodified', false); $embed = $fs->get_area_files($this->modcontext->id, 'mod_quora', 'post', $this->post->id, 'timemodified', false); $files = array_merge($attach, $embed); $this->set_file_and_format_data($files); } if (!empty($this->multifiles)) { $this->keyedfiles[$this->post->id] = $this->multifiles; } else { if (!empty($this->singlefile)) { $this->keyedfiles[$this->post->id] = array($this->singlefile); } } } else { // whole thread $fs = get_file_storage(); $this->posts = quora_get_all_discussion_posts($this->discussion->id, 'p.created ASC'); $this->multifiles = array(); foreach ($this->posts as $post) { $attach = $fs->get_area_files($this->modcontext->id, 'mod_quora', 'attachment', $post->id, 'timemodified', false); $embed = $fs->get_area_files($this->modcontext->id, 'mod_quora', 'post', $post->id, 'timemodified', false); $files = array_merge($attach, $embed); if ($files) { $this->keyedfiles[$post->id] = $files; } else { continue; } $this->multifiles = array_merge($this->multifiles, array_values($this->keyedfiles[$post->id])); } } if (empty($this->multifiles) && !empty($this->singlefile)) { $this->multifiles = array($this->singlefile); // copy_files workaround } // depending on whether there are files or not, we might have to change richhtml/plainhtml if (empty($this->attachment)) { if (!empty($this->multifiles)) { $this->add_format(PORTFOLIO_FORMAT_RICHHTML); } else { $this->add_format(PORTFOLIO_FORMAT_PLAINHTML); } } }
/** * Prints a quora 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 $quora * @param stdClass $discussion * @param stdClass $post * @param int $mode * @param mixed $canreply * @param bool $canrate */ function quora_print_discussion($course, $cm, $quora, $discussion, $post, $mode, $canreply = NULL, $canrate = false, $is_assessed) { 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 = quora_user_can_post($quora, $discussion, $USER, $cm, $course, $modcontext); } else { $reply = $canreply; } // $cm holds general cache for quora 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"; } $quoratracked = quora_tp_is_tracked($quora); $posts = quora_get_all_discussion_posts($discussion->id, $sort, $quoratracked); $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 ($quora->assessed != RATING_AGGREGATE_NONE) { $ratingoptions = new stdClass(); $ratingoptions->context = $modcontext; $ratingoptions->component = 'mod_quora'; $ratingoptions->ratingarea = 'post'; $ratingoptions->items = $posts; $ratingoptions->aggregate = $quora->assessed; //the aggregation method $ratingoptions->scaleid = $quora->scale; $ratingoptions->userid = $USER->id; if ($quora->type == 'single' or !$discussion->id) { $ratingoptions->returnurl = "{$CFG->wwwroot}/mod/quora/view.php?id={$cm->id}"; } else { $ratingoptions->returnurl = "{$CFG->wwwroot}/mod/quora/discuss.php?d={$discussion->id}"; } $ratingoptions->assesstimestart = $quora->assesstimestart; $ratingoptions->assesstimefinish = $quora->assesstimefinish; $rm = new rating_manager(); $posts = $rm->get_ratings($ratingoptions); } $post->quora = $quora->id; // Add the quora id to the post object, later used by quora_print_post $post->quoratype = $quora->type; $post->subject = format_string($post->subject); $postread = !empty($post->postread); quora_print_post($post, $discussion, $quora, $cm, $course, $is_assessed, $ownpost, $reply, false, '', '', $postread, true, $quoratracked); switch ($mode) { case FORUM_MODE_FLATOLDEST: case FORUM_MODE_FLATNEWEST: default: quora_print_posts_flat($course, $cm, $quora, $discussion, $post, $is_assessed, $mode, $reply, $quoratracked, $posts); break; case FORUM_MODE_THREADED: quora_print_posts_threaded($course, $cm, $quora, $discussion, $post, $is_assessed, 0, $reply, $quoratracked, $posts); break; case FORUM_MODE_NESTED: quora_print_posts_nested($course, $cm, $quora, $discussion, $post, $is_assessed, $reply, $quoratracked, $posts); break; } }
echo $OUTPUT->notification(get_string('qandanotify', 'quora')); } // If there is a warning message and we are not editing a post we need to handle the warning. if (!empty($thresholdwarning) && !$edit) { // Here we want to throw an exception if they are no longer allowed to post. quora_check_blocking_threshold($thresholdwarning); } if (!empty($parent)) { if (!($discussion = $DB->get_record('quora_discussions', array('id' => $parent->discussion)))) { print_error('notpartofdiscussion', 'quora'); } quora_print_post($parent, $discussion, $quora, $cm, $course, false, false, false); if (empty($post->edit)) { if ($quora->type != 'qanda' || quora_user_can_see_discussion($quora, $discussion, $modcontext)) { $quoratracked = quora_tp_is_tracked($quora); $posts = quora_get_all_discussion_posts($discussion->id, "created ASC", $quoratracked); quora_print_posts_threaded($course, $cm, $quora, $discussion, $parent, 0, false, $quoratracked, $posts); } } } else { if (!empty($quora->intro)) { echo $OUTPUT->box(format_module_intro('quora', $quora, $cm->id), 'generalbox', 'intro'); if (!empty($CFG->enableplagiarism)) { require_once $CFG->libdir . '/plagiarismlib.php'; echo plagiarism_print_disclosure($cm->id); } } } if (!empty($formheading)) { echo $OUTPUT->heading($formheading, 2, array('class' => 'accesshide')); }