Exemple #1
0
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 = twf_get_post_full($parent))) {
    print_error("notexists", 'twf', "{$CFG->wwwroot}/mod/twf/view.php?f={$twf->id}");
}
if (!twf_user_can_see_post($twf, $discussion, $post, null, $cm)) {
    print_error('noviewdiscussionspermission', 'twf', "{$CFG->wwwroot}/mod/twf/view.php?id={$twf->id}");
}
if ($mark == 'read' or $mark == 'unread') {
    if ($CFG->twf_usermarksread && twf_tp_can_track_twfs($twf) && twf_tp_is_tracked($twf)) {
        if ($mark == 'read') {
            twf_tp_add_read_record($USER->id, $postid);
        } else {
            // unread
            twf_tp_delete_read_records($USER->id, $postid);
        }
    }
}
$searchform = twf_search_form($course);
$twfnode = $PAGE->navigation->find($cm->id, navigation_node::TYPE_ACTIVITY);
if (empty($twfnode)) {
    $twfnode = $PAGE->navbar;
} else {
    $twfnode->make_active();
}
Exemple #2
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;
 }
Exemple #3
0
// The twf to subscribe or unsubscribe to
$returnpage = optional_param('returnpage', 'index.php', PARAM_FILE);
// Page to return to.
require_sesskey();
if (!($twf = $DB->get_record("twf", array("id" => $id)))) {
    print_error('invalidtwfid', 'twf');
}
if (!($course = $DB->get_record("course", array("id" => $twf->course)))) {
    print_error('invalidcoursemodule');
}
if (!($cm = get_coursemodule_from_instance("twf", $twf->id, $course->id))) {
    print_error('invalidcoursemodule');
}
require_login($course, false, $cm);
$returnto = twf_go_back_to($returnpage . '?id=' . $course->id . '&f=' . $twf->id);
if (!twf_tp_can_track_twfs($twf)) {
    redirect($returnto);
}
$info = new stdClass();
$info->name = fullname($USER);
$info->twf = format_string($twf->name);
$eventparams = array('context' => context_module::instance($cm->id), 'relateduserid' => $USER->id, 'other' => array('twfid' => $twf->id));
if (twf_tp_is_tracked($twf)) {
    if (twf_tp_stop_tracking($twf->id)) {
        $event = \mod_twf\event\readtracking_disabled::create($eventparams);
        $event->trigger();
        redirect($returnto, get_string("nownottracking", "twf", $info), 1);
    } else {
        print_error('cannottrack', '', get_local_referer(false));
    }
} else {
Exemple #4
0
 /**
  * Test the logic in the twf_tp_can_track_twfs() function.
  */
 public function test_twf_tp_can_track_twfs()
 {
     global $CFG;
     $this->resetAfterTest();
     $useron = $this->getDataGenerator()->create_user(array('tracktwfs' => 1));
     $useroff = $this->getDataGenerator()->create_user(array('tracktwfs' => 0));
     $course = $this->getDataGenerator()->create_course();
     $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_OFF);
     // Off.
     $twfoff = $this->getDataGenerator()->create_module('twf', $options);
     $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_FORCED);
     // On.
     $twfforce = $this->getDataGenerator()->create_module('twf', $options);
     $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_OPTIONAL);
     // Optional.
     $twfoptional = $this->getDataGenerator()->create_module('twf', $options);
     // Allow force.
     $CFG->twf_allowforcedreadtracking = 1;
     // User on, twf off, should be off.
     $result = twf_tp_can_track_twfs($twfoff, $useron);
     $this->assertEquals(false, $result);
     // User on, twf on, should be on.
     $result = twf_tp_can_track_twfs($twfforce, $useron);
     $this->assertEquals(true, $result);
     // User on, twf optional, should be on.
     $result = twf_tp_can_track_twfs($twfoptional, $useron);
     $this->assertEquals(true, $result);
     // User off, twf off, should be off.
     $result = twf_tp_can_track_twfs($twfoff, $useroff);
     $this->assertEquals(false, $result);
     // User off, twf force, should be on.
     $result = twf_tp_can_track_twfs($twfforce, $useroff);
     $this->assertEquals(true, $result);
     // User off, twf optional, should be off.
     $result = twf_tp_can_track_twfs($twfoptional, $useroff);
     $this->assertEquals(false, $result);
     // Don't allow force.
     $CFG->twf_allowforcedreadtracking = 0;
     // User on, twf off, should be off.
     $result = twf_tp_can_track_twfs($twfoff, $useron);
     $this->assertEquals(false, $result);
     // User on, twf on, should be on.
     $result = twf_tp_can_track_twfs($twfforce, $useron);
     $this->assertEquals(true, $result);
     // User on, twf optional, should be on.
     $result = twf_tp_can_track_twfs($twfoptional, $useron);
     $this->assertEquals(true, $result);
     // User off, twf off, should be off.
     $result = twf_tp_can_track_twfs($twfoff, $useroff);
     $this->assertEquals(false, $result);
     // User off, twf force, should be off.
     $result = twf_tp_can_track_twfs($twfforce, $useroff);
     $this->assertEquals(false, $result);
     // User off, twf optional, should be off.
     $result = twf_tp_can_track_twfs($twfoptional, $useroff);
     $this->assertEquals(false, $result);
 }
Exemple #5
0
$strsubscribe = get_string('subscribe', 'twf');
$strunsubscribe = get_string('unsubscribe', 'twf');
$stryes = get_string('yes');
$strno = get_string('no');
$strrss = get_string('rss');
$stremaildigest = get_string('emaildigest');
$searchform = twf_search_form($course);
// Retrieve the list of twf digest options for later.
$digestoptions = twf_get_user_digest_options();
$digestoptions_selector = new single_select(new moodle_url('/mod/twf/maildigest.php', array('backtoindex' => 1)), 'maildigest', $digestoptions, null, '');
$digestoptions_selector->method = 'post';
// Start of the table for General Forums
$generaltable = new html_table();
$generaltable->head = array($strtwf, $strdescription, $strdiscussions);
$generaltable->align = array('left', 'left', 'center');
if ($usetracking = twf_tp_can_track_twfs()) {
    $untracked = twf_tp_get_untracked_twfs($USER->id, $course->id);
    $generaltable->head[] = $strunreadposts;
    $generaltable->align[] = 'center';
    $generaltable->head[] = $strtracking;
    $generaltable->align[] = 'center';
}
// Fill the subscription cache for this course and user combination.
\mod_twf\subscriptions::fill_subscription_cache_for_course($course->id, $USER->id);
$can_subscribe = is_enrolled($coursecontext);
if ($can_subscribe) {
    $generaltable->head[] = $strsubscribed;
    $generaltable->align[] = 'center';
    $generaltable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_twf');
    $generaltable->align[] = 'center';
}
Exemple #6
0
/**
 * Adds information about unread messages, that is only required for the course view page (and
 * similar), to the course-module object.
 * @param cm_info $cm Course-module object
 */
function twf_cm_info_view(cm_info $cm)
{
    global $CFG;
    if (twf_tp_can_track_twfs()) {
        if ($unread = twf_tp_count_twf_unread_posts($cm, $cm->get_course())) {
            $out = '<span class="unread"> <a href="' . $cm->url . '">';
            if ($unread == 1) {
                $out .= get_string('unreadpostsone', 'twf');
            } else {
                $out .= get_string('unreadpostsnumber', 'twf', $unread);
            }
            $out .= '</a></span>';
            $cm->set_after_link($out);
        }
    }
}