Beispiel #1
0
/**
 * Prints the discussion view screen for a quora.
 *
 * @global object
 * @global object
 * @param object $course The current course object.
 * @param object $quora Forum to be printed.
 * @param int $maxdiscussions .
 * @param string $displayformat The display format to use (optional).
 * @param string $sort Sort arguments for database query (optional).
 * @param int $groupmode Group mode of the quora (optional).
 * @param void $unused (originally current group)
 * @param int $page Page mode, page to display (optional).
 * @param int $perpage The maximum number of discussions per page(optional)
 * @param boolean $subscriptionstatus Whether the user is currently subscribed to the discussion in some fashion.
 *
 */
function quora_print_latest_discussions($course, $quora, $maxdiscussions = -1, $displayformat = 'plain', $sort = '', $currentgroup = -1, $groupmode = -1, $page = -1, $perpage = 100, $cm = null)
{
    global $CFG, $USER, $OUTPUT;
    if (!$cm) {
        if (!($cm = get_coursemodule_from_instance('quora', $quora->id, $quora->course))) {
            print_error('invalidcoursemodule');
        }
    }
    $context = context_module::instance($cm->id);
    if (empty($sort)) {
        $sort = "d.timemodified DESC";
    }
    $olddiscussionlink = false;
    // Sort out some defaults
    if ($perpage <= 0) {
        $perpage = 0;
        $page = -1;
    }
    if ($maxdiscussions == 0) {
        // all discussions - backwards compatibility
        $page = -1;
        $perpage = 0;
        if ($displayformat == 'plain') {
            $displayformat = 'header';
            // Abbreviate display by default
        }
    } else {
        if ($maxdiscussions > 0) {
            $page = -1;
            $perpage = $maxdiscussions;
        }
    }
    $fullpost = false;
    if ($displayformat == 'plain') {
        $fullpost = true;
    }
    // Decide if current user is allowed to see ALL the current discussions or not
    // First check the group stuff
    if ($currentgroup == -1 or $groupmode == -1) {
        $groupmode = groups_get_activity_groupmode($cm, $course);
        $currentgroup = groups_get_activity_group($cm);
    }
    $groups = array();
    //cache
    // If the user can post discussions, then this is a good place to put the
    // button for it. We do not show the button if we are showing site news
    // and the current user is a guest.
    $canstart = quora_user_can_post_discussion($quora, $currentgroup, $groupmode, $cm, $context);
    if (!$canstart and $quora->type !== 'news') {
        if (isguestuser() or !isloggedin()) {
            $canstart = true;
        }
        if (!is_enrolled($context) and !is_viewing($context)) {
            // allow guests and not-logged-in to see the button - they are prompted to log in after clicking the link
            // normal users with temporary guest access see this button too, they are asked to enrol instead
            // do not show the button to users with suspended enrolments here
            $canstart = enrol_selfenrol_available($course->id);
        }
    }
    if ($canstart) {
        echo '<div class="singlebutton quoraaddnew">';
        echo "<form id=\"newdiscussionform\" method=\"get\" action=\"{$CFG->wwwroot}/mod/quora/post.php\">";
        echo '<div>';
        echo "<input type=\"hidden\" name=\"quora\" value=\"{$quora->id}\" />";
        switch ($quora->type) {
            case 'news':
            case 'blog':
                $buttonadd = get_string('addanewtopic', 'quora');
                break;
            case 'qanda':
                $buttonadd = get_string('addanewquestion', 'quora');
                break;
            default:
                $buttonadd = get_string('addanewdiscussion', 'quora');
                break;
        }
        echo '<input type="submit" value="' . $buttonadd . '" />';
        echo '</div>';
        echo '</form>';
        echo "</div>\n";
    } else {
        if (isguestuser() or !isloggedin() or $quora->type == 'news' or $quora->type == 'qanda' and !has_capability('mod/quora:addquestion', $context) or $quora->type != 'qanda' and !has_capability('mod/quora:startdiscussion', $context)) {
            // no button and no info
        } else {
            if ($groupmode and !has_capability('moodle/site:accessallgroups', $context)) {
                // inform users why they can not post new discussion
                if (!$currentgroup) {
                    echo $OUTPUT->notification(get_string('cannotadddiscussionall', 'quora'));
                } else {
                    if (!groups_is_member($currentgroup)) {
                        echo $OUTPUT->notification(get_string('cannotadddiscussion', 'quora'));
                    }
                }
            }
        }
    }
    // Get all the recent discussions we're allowed to see
    $getuserlastmodified = $displayformat == 'header';
    if (!($discussions = quora_get_discussions($cm, $sort, $fullpost, null, $maxdiscussions, $getuserlastmodified, $page, $perpage))) {
        echo '<div class="quoranodiscuss">';
        if ($quora->type == 'news') {
            echo '(' . get_string('nonews', 'quora') . ')';
        } else {
            if ($quora->type == 'qanda') {
                echo '(' . get_string('noquestions', 'quora') . ')';
            } else {
                echo '(' . get_string('nodiscussions', 'quora') . ')';
            }
        }
        echo "</div>\n";
        return;
    }
    // If we want paging
    if ($page != -1) {
        ///Get the number of discussions found
        $numdiscussions = quora_get_discussions_count($cm);
        ///Show the paging bar
        echo $OUTPUT->paging_bar($numdiscussions, $page, $perpage, "view.php?f={$quora->id}");
        if ($numdiscussions > 1000) {
            // saves some memory on sites with very large quoras
            $replies = quora_count_discussion_replies($quora->id, $sort, $maxdiscussions, $page, $perpage);
        } else {
            $replies = quora_count_discussion_replies($quora->id);
        }
    } else {
        $replies = quora_count_discussion_replies($quora->id);
        if ($maxdiscussions > 0 and $maxdiscussions <= count($discussions)) {
            $olddiscussionlink = true;
        }
    }
    $canviewparticipants = has_capability('moodle/course:viewparticipants', $context);
    $strdatestring = get_string('strftimerecentfull');
    // Check if the quora is tracked.
    if ($cantrack = quora_tp_can_track_quoras($quora)) {
        $quoratracked = quora_tp_is_tracked($quora);
    } else {
        $quoratracked = false;
    }
    if ($quoratracked) {
        $unreads = quora_get_discussions_unread($cm);
    } else {
        $unreads = array();
    }
    if ($displayformat == 'header') {
        echo '<table cellspacing="0" class="forumheaderlist">';
        echo '<thead>';
        echo '<tr>';
        echo '<th class="header topic" scope="col">' . get_string('discussion', 'quora') . '</th>';
        echo '<th class="header author" colspan="2" scope="col">' . get_string('startedby', 'quora') . '</th>';
        if ($groupmode > 0) {
            echo '<th class="header group" scope="col">' . get_string('group') . '</th>';
        }
        if (has_capability('mod/quora:viewdiscussion', $context)) {
            echo '<th class="header replies" scope="col">' . get_string('replies', 'quora') . '</th>';
            // If the quora can be tracked, display the unread column.
            if ($cantrack) {
                echo '<th class="header replies" scope="col">' . get_string('unread', 'quora');
                if ($quoratracked) {
                    echo '<a title="' . get_string('markallread', 'quora') . '" href="' . $CFG->wwwroot . '/mod/quora/markposts.php?f=' . $quora->id . '&amp;mark=read&amp;returnpage=view.php">' . '<img src="' . $OUTPUT->pix_url('t/markasread') . '" class="iconsmall" alt="' . get_string('markallread', 'quora') . '" /></a>';
                }
                echo '</th>';
            }
        }
        echo '<th class="header lastpost" scope="col">' . get_string('lastpost', 'quora') . '</th>';
        if (!is_guest($context, $USER) && isloggedin() && has_capability('mod/quora:viewdiscussion', $context)) {
            if (\mod_quora\subscriptions::is_subscribable($quora)) {
                echo '<th class="header discussionsubscription" scope="col">';
                echo quora_get_discussion_subscription_icon_preloaders();
                echo '</th>';
            }
        }
        echo '</tr>';
        echo '</thead>';
        echo '<tbody>';
    }
    foreach ($discussions as $discussion) {
        if ($quora->type == 'qanda' && !has_capability('mod/quora:viewqandawithoutposting', $context) && !quora_user_has_posted($quora->id, $discussion->discussion, $USER->id)) {
            $canviewparticipants = false;
        }
        if (!empty($replies[$discussion->discussion])) {
            $discussion->replies = $replies[$discussion->discussion]->replies;
            $discussion->lastpostid = $replies[$discussion->discussion]->lastpostid;
        } else {
            $discussion->replies = 0;
        }
        // SPECIAL CASE: The front page can display a news item post to non-logged in users.
        // All posts are read in this case.
        if (!$quoratracked) {
            $discussion->unread = '-';
        } else {
            if (empty($USER)) {
                $discussion->unread = 0;
            } else {
                if (empty($unreads[$discussion->discussion])) {
                    $discussion->unread = 0;
                } else {
                    $discussion->unread = $unreads[$discussion->discussion];
                }
            }
        }
        if (isloggedin()) {
            $ownpost = $discussion->userid == $USER->id;
        } else {
            $ownpost = false;
        }
        // Use discussion name instead of subject of first post
        $discussion->subject = $discussion->name;
        switch ($displayformat) {
            case 'header':
                if ($groupmode > 0) {
                    if (isset($groups[$discussion->groupid])) {
                        $group = $groups[$discussion->groupid];
                    } else {
                        $group = $groups[$discussion->groupid] = groups_get_group($discussion->groupid);
                    }
                } else {
                    $group = -1;
                }
                quora_print_discussion_header($discussion, $quora, $group, $strdatestring, $cantrack, $quoratracked, $canviewparticipants, $context);
                break;
            default:
                $link = false;
                if ($discussion->replies) {
                    $link = true;
                } else {
                    $modcontext = context_module::instance($cm->id);
                    $link = quora_user_can_see_discussion($quora, $discussion, $modcontext, $USER);
                }
                $discussion->quora = $quora->id;
                quora_print_post($discussion, $discussion, $quora, $cm, $course, $ownpost, 0, $link, false, '', null, true, $quoratracked);
                break;
        }
    }
    if ($displayformat == "header") {
        echo '</tbody>';
        echo '</table>';
    }
    if ($olddiscussionlink) {
        if ($quora->type == 'news') {
            $strolder = get_string('oldertopics', 'quora');
        } else {
            $strolder = get_string('olderdiscussions', 'quora');
        }
        echo '<div class="quoraolddiscuss">';
        echo '<a href="' . $CFG->wwwroot . '/mod/quora/view.php?f=' . $quora->id . '&amp;showall=1">';
        echo $strolder . '</a> ...</div>';
    }
    if ($page != -1) {
        ///Show the paging bar
        echo $OUTPUT->paging_bar($numdiscussions, $page, $perpage, "view.php?f={$quora->id}");
    }
}
Beispiel #2
0
 /**
  * Returns a list of quora discussions optionally sorted and paginated.
  *
  * @param int $quoraid the quora instance id
  * @param string $sortby sort by this element (id, timemodified, timestart or timeend)
  * @param string $sortdirection sort direction: ASC or DESC
  * @param int $page page number
  * @param int $perpage items per page
  *
  * @return array the quora discussion details including warnings
  * @since Moodle 2.8
  */
 public static function get_quora_discussions_paginated($quoraid, $sortby = 'timemodified', $sortdirection = 'DESC', $page = -1, $perpage = 0)
 {
     global $CFG, $DB, $USER;
     require_once $CFG->dirroot . "/mod/quora/lib.php";
     $warnings = array();
     $discussions = array();
     $params = self::validate_parameters(self::get_quora_discussions_paginated_parameters(), array('quoraid' => $quoraid, 'sortby' => $sortby, 'sortdirection' => $sortdirection, 'page' => $page, 'perpage' => $perpage));
     // Compact/extract functions are not recommended.
     $quoraid = $params['quoraid'];
     $sortby = $params['sortby'];
     $sortdirection = $params['sortdirection'];
     $page = $params['page'];
     $perpage = $params['perpage'];
     $sortallowedvalues = array('id', 'timemodified', 'timestart', 'timeend');
     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));
     }
     $quora = $DB->get_record('quora', array('id' => $quoraid), '*', 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);
     // Check they have the view quora capability.
     require_capability('mod/quora:viewdiscussion', $modcontext, null, true, 'noviewdiscussionspermission', 'quora');
     $sort = 'd.' . $sortby . ' ' . $sortdirection;
     $alldiscussions = quora_get_discussions($cm, $sort, true, -1, -1, true, $page, $perpage);
     if ($alldiscussions) {
         $canviewfullname = has_capability('moodle/site:viewfullnames', $modcontext);
         // Get the unreads array, this takes a quora id and returns data for all discussions.
         $unreads = array();
         if ($cantrack = quora_tp_can_track_quoras($quora)) {
             if ($quoratracked = quora_tp_is_tracked($quora)) {
                 $unreads = quora_get_discussions_unread($cm);
             }
         }
         // The quora function returns the replies for all the discussions in a given quora.
         $replies = quora_count_discussion_replies($quoraid, $sort, -1, $page, $perpage);
         foreach ($alldiscussions as $discussion) {
             // This function checks for qanda quoras.
             // Note that the quora_get_discussions returns as id the post id, not the discussion id so we need to do this.
             $discussionrec = clone $discussion;
             $discussionrec->id = $discussion->discussion;
             if (!quora_user_can_see_discussion($quora, $discussionrec, $modcontext)) {
                 $warning = array();
                 // Function quora_get_discussions returns quora_posts ids not quora_discussions ones.
                 $warning['item'] = 'post';
                 $warning['itemid'] = $discussion->id;
                 $warning['warningcode'] = '1';
                 $warning['message'] = 'You can\'t see this discussion';
                 $warnings[] = $warning;
                 continue;
             }
             $discussion->numunread = 0;
             if ($cantrack && $quoratracked) {
                 if (isset($unreads[$discussion->discussion])) {
                     $discussion->numunread = (int) $unreads[$discussion->discussion];
                 }
             }
             $discussion->numreplies = 0;
             if (!empty($replies[$discussion->discussion])) {
                 $discussion->numreplies = (int) $replies[$discussion->discussion]->replies;
             }
             // Load user objects from the results of the query.
             $user = new stdclass();
             $user->id = $discussion->userid;
             $user = username_load_fields_from_object($user, $discussion);
             $discussion->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) {
                 $discussion->userpictureurl = moodle_url::make_webservice_pluginfile_url($usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false);
             } else {
                 $discussion->userpictureurl = '';
             }
             $usermodified = new stdclass();
             $usermodified->id = $discussion->usermodified;
             $usermodified = username_load_fields_from_object($usermodified, $discussion, 'um');
             $discussion->usermodifiedfullname = fullname($usermodified, $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($usermodified->id, IGNORE_MISSING);
             if ($usercontext) {
                 $discussion->usermodifiedpictureurl = moodle_url::make_webservice_pluginfile_url($usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false);
             } else {
                 $discussion->usermodifiedpictureurl = '';
             }
             // Rewrite embedded images URLs.
             list($discussion->message, $discussion->messageformat) = external_format_text($discussion->message, $discussion->messageformat, $modcontext->id, 'mod_quora', 'post', $discussion->id);
             // List attachments.
             if (!empty($discussion->attachment)) {
                 $discussion->attachments = array();
                 $fs = get_file_storage();
                 if ($files = $fs->get_area_files($modcontext->id, 'mod_quora', 'attachment', $discussion->id, "filename", false)) {
                     foreach ($files as $file) {
                         $filename = $file->get_filename();
                         $discussion->attachments[] = array('filename' => $filename, 'mimetype' => $file->get_mimetype(), 'fileurl' => file_encode_url($CFG->wwwroot . '/webservice/pluginfile.php', '/' . $modcontext->id . '/mod_quora/attachment/' . $discussion->id . '/' . $filename));
                     }
                 }
             }
             $discussions[] = $discussion;
         }
     }
     $result = array();
     $result['discussions'] = $discussions;
     $result['warnings'] = $warnings;
     return $result;
 }
Beispiel #3
0
 public function test_count_discussion_replies_limited_sorted_small_reverse()
 {
     list($quora, $discussionids) = $this->create_multiple_discussions_with_replies(10, 5);
     // Adding limits, and a quorasort shouldn't make a difference.
     $result = quora_count_discussion_replies($quora->id, "d.id desc", 5);
     $this->assertCount(5, $result);
     foreach ($result as $row) {
         // Grab the last discussionid.
         $discussionid = array_pop($discussionids);
         $this->assertEquals($discussionid, $row->discussion);
     }
 }