Example #1
0
 /**
  * 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;
 }
Example #2
0
/**
 * 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;
    }
}
Example #3
0
 }
 if (!($quora = $DB->get_record("quora", array("id" => $discussion->quora)))) {
     print_error('invalidquoraid', 'quora');
 }
 if (!($course = $DB->get_record("course", array("id" => $discussion->course)))) {
     print_error('invalidcourseid');
 }
 if (!($cm = get_coursemodule_from_instance("quora", $quora->id, $course->id))) {
     print_error('invalidcoursemodule');
 }
 // Ensure lang, theme, etc. is set up properly. MDL-6926
 $PAGE->set_cm($cm, $course, $quora);
 // Retrieve the contexts.
 $modcontext = context_module::instance($cm->id);
 $coursecontext = context_course::instance($course->id);
 if (!quora_user_can_post($quora, $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/quora/view.php?f=' . $quora->id)), get_string('youneedtoenrol'));
         }
     }
     print_error('nopostquora', 'quora');
 }
 // Make sure user can post here
 if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
     $groupmode = $cm->groupmode;
 } else {
     $groupmode = $course->groupmode;
Example #4
0
}
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)) {
            print_string("allowsdiscussions", "quora");
        } else {
            echo '&nbsp;';
        }
        echo '</p>';
        if (!empty($showall)) {
Example #5
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;
 }