Exemplo n.º 1
0
 /**
  * Returns a list of twf discussions optionally sorted and paginated.
  *
  * @param int $twfid the twf 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 twf discussion details including warnings
  * @since Moodle 2.8
  */
 public static function get_twf_discussions_paginated($twfid, $sortby = 'timemodified', $sortdirection = 'DESC', $page = -1, $perpage = 0)
 {
     global $CFG, $DB, $USER;
     require_once $CFG->dirroot . "/mod/twf/lib.php";
     $warnings = array();
     $discussions = array();
     $params = self::validate_parameters(self::get_twf_discussions_paginated_parameters(), array('twfid' => $twfid, 'sortby' => $sortby, 'sortdirection' => $sortdirection, 'page' => $page, 'perpage' => $perpage));
     // Compact/extract functions are not recommended.
     $twfid = $params['twfid'];
     $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));
     }
     $twf = $DB->get_record('twf', array('id' => $twfid), '*', 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);
     // Check they have the view twf capability.
     require_capability('mod/twf:viewdiscussion', $modcontext, null, true, 'noviewdiscussionspermission', 'twf');
     $sort = 'd.' . $sortby . ' ' . $sortdirection;
     $alldiscussions = twf_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 twf id and returns data for all discussions.
         $unreads = array();
         if ($cantrack = twf_tp_can_track_twfs($twf)) {
             if ($twftracked = twf_tp_is_tracked($twf)) {
                 $unreads = twf_get_discussions_unread($cm);
             }
         }
         // The twf function returns the replies for all the discussions in a given twf.
         $replies = twf_count_discussion_replies($twfid, $sort, -1, $page, $perpage);
         foreach ($alldiscussions as $discussion) {
             // This function checks for qanda twfs.
             // Note that the twf_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 (!twf_user_can_see_discussion($twf, $discussionrec, $modcontext)) {
                 $warning = array();
                 // Function twf_get_discussions returns twf_posts ids not twf_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 && $twftracked) {
                 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_twf', '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_twf', '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_twf/attachment/' . $discussion->id . '/' . $filename));
                     }
                 }
             }
             $discussions[] = $discussion;
         }
     }
     $result = array();
     $result['discussions'] = $discussions;
     $result['warnings'] = $warnings;
     return $result;
 }
Exemplo n.º 2
0
/**
 * Prints the discussion view screen for a twf.
 *
 * @global object
 * @global object
 * @param object $course The current course object.
 * @param object $twf 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 twf (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 twf_print_latest_discussions($course, $twf, $maxdiscussions = -1, $displayformat = 'plain', $sort = '', $currentgroup = -1, $groupmode = -1, $page = -1, $perpage = 100, $cm = null)
{
    global $CFG, $USER, $OUTPUT;
    //return;
    if (!$cm) {
        if (!($cm = get_coursemodule_from_instance('twf', $twf->id, $twf->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 = twf_user_can_post_discussion($twf, $currentgroup, $groupmode, $cm, $context);
    if (!$canstart and $twf->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 twfaddnew">';
        echo "<form id=\"newdiscussionform\" method=\"get\" action=\"{$CFG->wwwroot}/mod/twf/post.php\">";
        echo '<div>';
        echo "<input type=\"hidden\" name=\"twf\" value=\"{$twf->id}\" />";
        switch ($twf->type) {
            case 'news':
            case 'blog':
                $buttonadd = get_string('addanewtopic', 'twf');
                break;
            case 'qanda':
                $buttonadd = get_string('addanewquestion', 'twf');
                break;
            default:
                $buttonadd = get_string('addanewdiscussion', 'twf');
                break;
        }
        echo '<input type="submit" value="' . $buttonadd . '" />';
        echo '</div>';
        echo '</form>';
        echo "</div>\n";
    } else {
        if (isguestuser() or !isloggedin() or $twf->type == 'news' or $twf->type == 'qanda' and !has_capability('mod/twf:addquestion', $context) or $twf->type != 'qanda' and !has_capability('mod/twf: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', 'twf'));
                } else {
                    if (!groups_is_member($currentgroup)) {
                        echo $OUTPUT->notification(get_string('cannotadddiscussion', 'twf'));
                    }
                }
            }
        }
    }
    // Get all the recent discussions we're allowed to see
    $getuserlastmodified = $displayformat == 'header';
    if (!($discussions = twf_get_discussions($cm, $sort, $fullpost, null, $maxdiscussions, $getuserlastmodified, $page, $perpage))) {
        echo '<div class="twfnodiscuss">';
        if ($twf->type == 'news') {
            echo '(' . get_string('nonews', 'twf') . ')';
        } else {
            if ($twf->type == 'qanda') {
                echo '(' . get_string('noquestions', 'twf') . ')';
            } else {
                echo '(' . get_string('nodiscussions', 'twf') . ')';
            }
        }
        echo "</div>\n";
        return;
    }
    // If we want paging
    if ($page != -1) {
        ///Get the number of discussions found
        $numdiscussions = twf_get_discussions_count($cm);
        ///Show the paging bar
        echo $OUTPUT->paging_bar($numdiscussions, $page, $perpage, "view.php?f={$twf->id}");
        if ($numdiscussions > 1000) {
            // saves some memory on sites with very large twfs
            $replies = twf_count_discussion_replies($twf->id, $sort, $maxdiscussions, $page, $perpage);
        } else {
            $replies = twf_count_discussion_replies($twf->id);
        }
    } else {
        $replies = twf_count_discussion_replies($twf->id);
        if ($maxdiscussions > 0 and $maxdiscussions <= count($discussions)) {
            $olddiscussionlink = true;
        }
    }
    $canviewparticipants = has_capability('moodle/course:viewparticipants', $context);
    $strdatestring = get_string('strftimerecentfull');
    // Check if the twf is tracked.
    if ($cantrack = twf_tp_can_track_twfs($twf)) {
        $twftracked = twf_tp_is_tracked($twf);
    } else {
        $twftracked = false;
    }
    if ($twftracked) {
        $unreads = twf_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', 'twf') . '</th>';
        echo '<th class="header author" colspan="2" scope="col">' . get_string('startedby', 'twf') . '</th>';
        if ($groupmode > 0) {
            echo '<th class="header group" scope="col">' . get_string('group') . '</th>';
        }
        if (has_capability('mod/twf:viewdiscussion', $context)) {
            echo '<th class="header replies" scope="col">' . get_string('replies', 'twf') . '</th>';
            // If the twf can be tracked, display the unread column.
            if ($cantrack) {
                echo '<th class="header replies" scope="col">' . get_string('unread', 'twf');
                if ($twftracked) {
                    echo '<a title="' . get_string('markallread', 'twf') . '" href="' . $CFG->wwwroot . '/mod/twf/markposts.php?f=' . $twf->id . '&amp;mark=read&amp;returnpage=view.php">' . '<img src="' . $OUTPUT->pix_url('t/markasread') . '" class="iconsmall" alt="' . get_string('markallread', 'twf') . '" /></a>';
                }
                echo '</th>';
            }
        }
        echo '<th class="header lastpost" scope="col">' . get_string('lastpost', 'twf') . '</th>';
        if (!is_guest($context, $USER) && isloggedin() && has_capability('mod/twf:viewdiscussion', $context)) {
            if (\mod_twf\subscriptions::is_subscribable($twf)) {
                echo '<th class="header discussionsubscription" scope="col">';
                echo twf_get_discussion_subscription_icon_preloaders();
                echo '</th>';
            }
        }
        echo '</tr>';
        echo '</thead>';
        echo '<tbody>';
    }
    foreach ($discussions as $discussion) {
        if ($twf->type == 'qanda' && !has_capability('mod/twf:viewqandawithoutposting', $context) && !twf_user_has_posted($twf->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 (!$twftracked) {
            $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;
                }
                twf_print_discussion_header($discussion, $twf, $group, $strdatestring, $cantrack, $twftracked, $canviewparticipants, $context);
                break;
            default:
                $link = false;
                if ($discussion->replies) {
                    $link = true;
                } else {
                    $modcontext = context_module::instance($cm->id);
                    $link = twf_user_can_see_discussion($twf, $discussion, $modcontext, $USER);
                }
                $discussion->twf = $twf->id;
                twf_print_post($discussion, $discussion, $twf, $cm, $course, $ownpost, 0, $link, false, '', null, true, $twftracked);
                break;
        }
    }
    if ($displayformat == "header") {
        echo '</tbody>';
        echo '</table>';
    }
    if ($olddiscussionlink) {
        if ($twf->type == 'news') {
            $strolder = get_string('oldertopics', 'twf');
        } else {
            $strolder = get_string('olderdiscussions', 'twf');
        }
        echo '<div class="forumolddiscuss">';
        echo '<a href="' . $CFG->wwwroot . '/mod/twf/view.php?f=' . $twf->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={$twf->id}");
    }
}