/** * 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; }
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(); }
/** * Generate and return the track or no track link for a twf. * * @global object * @global object * @global object * @param object $twf the twf. Fields used are $twf->id and $twf->forcesubscribe. * @param array $messages * @param bool $fakelink * @return string * @deprecated since Moodle 2.0 MDL-14632 - please do not use this function any more. */ function twf_get_tracking_link($twf, $messages = array(), $fakelink = true) { debugging('twf_get_tracking_link() is deprecated.', DEBUG_DEVELOPER); global $CFG, $USER, $PAGE, $OUTPUT; static $strnotracktwf, $strtracktwf; if (isset($messages['tracktwf'])) { $strtracktwf = $messages['tracktwf']; } if (isset($messages['notracktwf'])) { $strnotracktwf = $messages['notracktwf']; } if (empty($strtracktwf)) { $strtracktwf = get_string('tracktwf', 'twf'); } if (empty($strnotracktwf)) { $strnotracktwf = get_string('notracktwf', 'twf'); } if (twf_tp_is_tracked($twf)) { $linktitle = $strnotracktwf; $linktext = $strnotracktwf; } else { $linktitle = $strtracktwf; $linktext = $strtracktwf; } $link = ''; if ($fakelink) { $PAGE->requires->js('/mod/twf/twf.js'); $PAGE->requires->js_function_call('twf_produce_tracking_link', array($twf->id, $linktext, $linktitle)); // use <noscript> to print button in case javascript is not enabled $link .= '<noscript>'; } $url = new moodle_url('/mod/twf/settracking.php', array('id' => $twf->id, 'sesskey' => sesskey())); $link .= $OUTPUT->single_button($url, $linktext, 'get', array('title' => $linktitle)); if ($fakelink) { $link .= '</noscript>'; } return $link; }
if ($twf->type == 'qanda' && !has_capability('mod/twf:viewqandawithoutposting', $modcontext) && !empty($discussion->id) && !twf_user_has_posted($twf->id, $discussion->id, $USER->id)) { echo $OUTPUT->notification(get_string('qandanotify', 'twf')); } // 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. twf_check_blocking_threshold($thresholdwarning); } if (!empty($parent)) { if (!($discussion = $DB->get_record('twf_discussions', array('id' => $parent->discussion)))) { print_error('notpartofdiscussion', 'twf'); } twf_print_post($parent, $discussion, $twf, $cm, $course, false, false, false); if (empty($post->edit)) { if ($twf->type != 'qanda' || twf_user_can_see_discussion($twf, $discussion, $modcontext)) { $twftracked = twf_tp_is_tracked($twf); $posts = twf_get_all_discussion_posts($discussion->id, "created ASC", $twftracked); twf_print_posts_threaded($course, $cm, $twf, $discussion, $parent, 0, false, $twftracked, $posts); } } } else { if (!empty($twf->intro)) { echo $OUTPUT->box(format_module_intro('twf', $twf, $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'));
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 { // subscribe if (twf_tp_start_tracking($twf->id)) { $event = \mod_twf\event\readtracking_enabled::create($eventparams); $event->trigger(); redirect($returnto, get_string("nowtracking", "twf", $info), 1); } else { print_error('cannottrack', '', get_local_referer(false));
/** * Test the logic in the test_twf_tp_is_tracked() function. */ public function test_twf_tp_is_tracked() { 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_is_tracked($twfoff, $useron); $this->assertEquals(false, $result); // User on, twf force, should be on. $result = twf_tp_is_tracked($twfforce, $useron); $this->assertEquals(true, $result); // User on, twf optional, should be on. $result = twf_tp_is_tracked($twfoptional, $useron); $this->assertEquals(true, $result); // User off, twf off, should be off. $result = twf_tp_is_tracked($twfoff, $useroff); $this->assertEquals(false, $result); // User off, twf force, should be on. $result = twf_tp_is_tracked($twfforce, $useroff); $this->assertEquals(true, $result); // User off, twf optional, should be off. $result = twf_tp_is_tracked($twfoptional, $useroff); $this->assertEquals(false, $result); // Don't allow force. $CFG->twf_allowforcedreadtracking = 0; // User on, twf off, should be off. $result = twf_tp_is_tracked($twfoff, $useron); $this->assertEquals(false, $result); // User on, twf force, should be on. $result = twf_tp_is_tracked($twfforce, $useron); $this->assertEquals(true, $result); // User on, twf optional, should be on. $result = twf_tp_is_tracked($twfoptional, $useron); $this->assertEquals(true, $result); // User off, twf off, should be off. $result = twf_tp_is_tracked($twfoff, $useroff); $this->assertEquals(false, $result); // User off, twf force, should be off. $result = twf_tp_is_tracked($twfforce, $useroff); $this->assertEquals(false, $result); // User off, twf optional, should be off. $result = twf_tp_is_tracked($twfoptional, $useroff); $this->assertEquals(false, $result); // Stop tracking so we can test again. twf_tp_stop_tracking($twfforce->id, $useron->id); twf_tp_stop_tracking($twfoptional->id, $useron->id); twf_tp_stop_tracking($twfforce->id, $useroff->id); twf_tp_stop_tracking($twfoptional->id, $useroff->id); // Allow force. $CFG->twf_allowforcedreadtracking = 1; // User on, preference off, twf force, should be on. $result = twf_tp_is_tracked($twfforce, $useron); $this->assertEquals(true, $result); // User on, preference off, twf optional, should be on. $result = twf_tp_is_tracked($twfoptional, $useron); $this->assertEquals(false, $result); // User off, preference off, twf force, should be on. $result = twf_tp_is_tracked($twfforce, $useroff); $this->assertEquals(true, $result); // User off, preference off, twf optional, should be off. $result = twf_tp_is_tracked($twfoptional, $useroff); $this->assertEquals(false, $result); // Don't allow force. $CFG->twf_allowforcedreadtracking = 0; // User on, preference off, twf force, should be on. $result = twf_tp_is_tracked($twfforce, $useron); $this->assertEquals(false, $result); // User on, preference off, twf optional, should be on. $result = twf_tp_is_tracked($twfoptional, $useron); $this->assertEquals(false, $result); // User off, preference off, twf force, should be off. $result = twf_tp_is_tracked($twfforce, $useroff); $this->assertEquals(false, $result); // User off, preference off, twf optional, should be off. $result = twf_tp_is_tracked($twfoptional, $useroff); $this->assertEquals(false, $result); }
/** * Adds module specific settings to the settings block * * @param settings_navigation $settings The settings navigation object * @param navigation_node $twfnode The node to add module settings to */ function twf_extend_settings_navigation(settings_navigation $settingsnav, navigation_node $twfnode) { global $USER, $PAGE, $CFG, $DB, $OUTPUT; $twfobject = $DB->get_record("twf", array("id" => $PAGE->cm->instance)); if (empty($PAGE->cm->context)) { $PAGE->cm->context = context_module::instance($PAGE->cm->instance); } $params = $PAGE->url->params(); if (!empty($params['d'])) { $discussionid = $params['d']; } // for some actions you need to be enrolled, beiing admin is not enough sometimes here $enrolled = is_enrolled($PAGE->cm->context, $USER, '', false); $activeenrolled = is_enrolled($PAGE->cm->context, $USER, '', true); $canmanage = has_capability('mod/twf:managesubscriptions', $PAGE->cm->context); $subscriptionmode = \mod_twf\subscriptions::get_subscription_mode($twfobject); $cansubscribe = $activeenrolled && !\mod_twf\subscriptions::is_forcesubscribed($twfobject) && (!\mod_twf\subscriptions::subscription_disabled($twfobject) || $canmanage); if ($canmanage) { $mode = $twfnode->add(get_string('subscriptionmode', 'twf'), null, navigation_node::TYPE_CONTAINER); $allowchoice = $mode->add(get_string('subscriptionoptional', 'twf'), new moodle_url('/mod/twf/subscribe.php', array('id' => $twfobject->id, 'mode' => FORUM_CHOOSESUBSCRIBE, 'sesskey' => sesskey())), navigation_node::TYPE_SETTING); $forceforever = $mode->add(get_string("subscriptionforced", "twf"), new moodle_url('/mod/twf/subscribe.php', array('id' => $twfobject->id, 'mode' => FORUM_FORCESUBSCRIBE, 'sesskey' => sesskey())), navigation_node::TYPE_SETTING); $forceinitially = $mode->add(get_string("subscriptionauto", "twf"), new moodle_url('/mod/twf/subscribe.php', array('id' => $twfobject->id, 'mode' => FORUM_INITIALSUBSCRIBE, 'sesskey' => sesskey())), navigation_node::TYPE_SETTING); $disallowchoice = $mode->add(get_string('subscriptiondisabled', 'twf'), new moodle_url('/mod/twf/subscribe.php', array('id' => $twfobject->id, 'mode' => FORUM_DISALLOWSUBSCRIBE, 'sesskey' => sesskey())), navigation_node::TYPE_SETTING); switch ($subscriptionmode) { case FORUM_CHOOSESUBSCRIBE: // 0 $allowchoice->action = null; $allowchoice->add_class('activesetting'); break; case FORUM_FORCESUBSCRIBE: // 1 $forceforever->action = null; $forceforever->add_class('activesetting'); break; case FORUM_INITIALSUBSCRIBE: // 2 $forceinitially->action = null; $forceinitially->add_class('activesetting'); break; case FORUM_DISALLOWSUBSCRIBE: // 3 $disallowchoice->action = null; $disallowchoice->add_class('activesetting'); break; } } else { if ($activeenrolled) { switch ($subscriptionmode) { case FORUM_CHOOSESUBSCRIBE: // 0 $notenode = $twfnode->add(get_string('subscriptionoptional', 'twf')); break; case FORUM_FORCESUBSCRIBE: // 1 $notenode = $twfnode->add(get_string('subscriptionforced', 'twf')); break; case FORUM_INITIALSUBSCRIBE: // 2 $notenode = $twfnode->add(get_string('subscriptionauto', 'twf')); break; case FORUM_DISALLOWSUBSCRIBE: // 3 $notenode = $twfnode->add(get_string('subscriptiondisabled', 'twf')); break; } } } if ($cansubscribe) { if (\mod_twf\subscriptions::is_subscribed($USER->id, $twfobject, null, $PAGE->cm)) { $linktext = get_string('unsubscribe', 'twf'); } else { $linktext = get_string('subscribe', 'twf'); } $url = new moodle_url('/mod/twf/subscribe.php', array('id' => $twfobject->id, 'sesskey' => sesskey())); $twfnode->add($linktext, $url, navigation_node::TYPE_SETTING); if (isset($discussionid)) { if (\mod_twf\subscriptions::is_subscribed($USER->id, $twfobject, $discussionid, $PAGE->cm)) { $linktext = get_string('unsubscribediscussion', 'twf'); } else { $linktext = get_string('subscribediscussion', 'twf'); } $url = new moodle_url('/mod/twf/subscribe.php', array('id' => $twfobject->id, 'sesskey' => sesskey(), 'd' => $discussionid, 'returnurl' => $PAGE->url->out())); $twfnode->add($linktext, $url, navigation_node::TYPE_SETTING); } } if (has_capability('mod/twf:viewsubscribers', $PAGE->cm->context)) { $url = new moodle_url('/mod/twf/subscribers.php', array('id' => $twfobject->id)); $twfnode->add(get_string('showsubscribers', 'twf'), $url, navigation_node::TYPE_SETTING); } if ($enrolled && twf_tp_can_track_twfs($twfobject)) { // keep tracking info for users with suspended enrolments if ($twfobject->trackingtype == FORUM_TRACKING_OPTIONAL || !$CFG->twf_allowforcedreadtracking && $twfobject->trackingtype == FORUM_TRACKING_FORCED) { if (twf_tp_is_tracked($twfobject)) { $linktext = get_string('notracktwf', 'twf'); } else { $linktext = get_string('tracktwf', 'twf'); } $url = new moodle_url('/mod/twf/settracking.php', array('id' => $twfobject->id, 'sesskey' => sesskey())); $twfnode->add($linktext, $url, navigation_node::TYPE_SETTING); } } if (!isloggedin() && $PAGE->course->id == SITEID) { $userid = guest_user()->id; } else { $userid = $USER->id; } $hascourseaccess = $PAGE->course->id == SITEID || can_access_course($PAGE->course, $userid); $enablerssfeeds = !empty($CFG->enablerssfeeds) && !empty($CFG->twf_enablerssfeeds); if ($enablerssfeeds && $twfobject->rsstype && $twfobject->rssarticles && $hascourseaccess) { if (!function_exists('rss_get_url')) { require_once "{$CFG->libdir}/rsslib.php"; } if ($twfobject->rsstype == 1) { $string = get_string('rsssubscriberssdiscussions', 'twf'); } else { $string = get_string('rsssubscriberssposts', 'twf'); } $url = new moodle_url(rss_get_url($PAGE->cm->context->id, $userid, "mod_twf", $twfobject->id)); $twfnode->add($string, $url, settings_navigation::TYPE_SETTING, null, null, new pix_icon('i/rss', '')); } }