Beispiel #1
0
/**
 * This function return the XML rss contents about the twf
 * It returns false if something is wrong
 *
 * @param stdClass $twf the twf object
 * @param string $sql the SQL used to retrieve the contents from the database
 * @param array $params the SQL parameters used
 * @param object $context the context this twf relates to
 * @return bool|string false if the contents is empty, otherwise the contents of the feed is returned
 *
 * @Todo MDL-31129 implement post attachment handling
 */
function twf_rss_feed_contents($twf, $sql, $params, $context)
{
    global $CFG, $DB, $USER;
    $status = true;
    $recs = $DB->get_recordset_sql($sql, $params, 0, $twf->rssarticles);
    //set a flag. Are we displaying discussions or posts?
    $isdiscussion = true;
    if (!empty($twf->rsstype) && $twf->rsstype != 1) {
        $isdiscussion = false;
    }
    if (!($cm = get_coursemodule_from_instance('twf', $twf->id, $twf->course))) {
        print_error('invalidcoursemodule');
    }
    $formatoptions = new stdClass();
    $items = array();
    foreach ($recs as $rec) {
        $item = new stdClass();
        $discussion = new stdClass();
        $discussion->id = $rec->discussionid;
        $discussion->groupid = $rec->groupid;
        $discussion->timestart = $rec->timestart;
        $discussion->timeend = $rec->timeend;
        $post = null;
        if (!$isdiscussion) {
            $post = new stdClass();
            $post->id = $rec->postid;
            $post->parent = $rec->postparent;
            $post->userid = $rec->userid;
        }
        if ($isdiscussion && !twf_user_can_see_discussion($twf, $discussion, $context)) {
            // This is a discussion which the user has no permission to view
            $item->title = get_string('twfsubjecthidden', 'twf');
            $message = get_string('twfbodyhidden', 'twf');
            $item->author = get_string('twfauthorhidden', 'twf');
        } else {
            if (!$isdiscussion && !twf_user_can_see_post($twf, $discussion, $post, $USER, $cm)) {
                // This is a post which the user has no permission to view
                $item->title = get_string('twfsubjecthidden', 'twf');
                $message = get_string('twfbodyhidden', 'twf');
                $item->author = get_string('twfauthorhidden', 'twf');
            } else {
                // The user must have permission to view
                if ($isdiscussion && !empty($rec->discussionname)) {
                    $item->title = format_string($rec->discussionname);
                } else {
                    if (!empty($rec->postsubject)) {
                        $item->title = format_string($rec->postsubject);
                    } else {
                        //we should have an item title by now but if we dont somehow then substitute something somewhat meaningful
                        $item->title = format_string($twf->name . ' ' . userdate($rec->postcreated, get_string('strftimedatetimeshort', 'langconfig')));
                    }
                }
                $item->author = fullname($rec);
                $message = file_rewrite_pluginfile_urls($rec->postmessage, 'pluginfile.php', $context->id, 'mod_twf', 'post', $rec->postid);
                $formatoptions->trusted = $rec->posttrust;
            }
        }
        if ($isdiscussion) {
            $item->link = $CFG->wwwroot . "/mod/twf/discuss.php?d=" . $rec->discussionid;
        } else {
            $item->link = $CFG->wwwroot . "/mod/twf/discuss.php?d=" . $rec->discussionid . "&parent=" . $rec->postid;
        }
        $formatoptions->trusted = $rec->posttrust;
        $item->description = format_text($message, $rec->postformat, $formatoptions, $twf->course);
        //TODO: MDL-31129 implement post attachment handling
        /*if (!$isdiscussion) {
                        $post_file_area_name = str_replace('//', '/', "$twf->course/$CFG->moddata/twf/$twf->id/$rec->postid");
                        $post_files = get_directory_list("$CFG->dataroot/$post_file_area_name");
        
                        if (!empty($post_files)) {
                            $item->attachments = array();
                        }
                    }*/
        $item->pubdate = $rec->postcreated;
        $items[] = $item;
    }
    $recs->close();
    // Create the RSS header.
    $header = rss_standard_header(strip_tags(format_string($twf->name, true)), $CFG->wwwroot . "/mod/twf/view.php?f=" . $twf->id, format_string($twf->intro, true));
    // TODO: fix format
    // Now all the RSS items, if there are any.
    $articles = '';
    if (!empty($items)) {
        $articles = rss_add_items($items);
    }
    // Create the RSS footer.
    $footer = rss_standard_footer();
    return $header . $articles . $footer;
}
Beispiel #2
0
if ($mode) {
    set_user_preference('twf_displaymode', $mode);
}
$displaymode = get_user_preferences('twf_displaymode', $CFG->twf_displaymode);
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)) {
Beispiel #3
0
 /**
  * Returns a list of twf 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 twf post details
  * @since Moodle 2.7
  */
 public static function get_twf_discussion_posts($discussionid, $sortby = "created", $sortdirection = "DESC")
 {
     global $CFG, $DB, $USER;
     $posts = array();
     $warnings = array();
     // Validate the parameter.
     $params = self::validate_parameters(self::get_twf_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('twf_discussions', array('id' => $discussionid), '*', MUST_EXIST);
     $twf = $DB->get_record('twf', array('id' => $discussion->twf), '*', 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);
     // This require must be here, see mod/twf/discuss.php.
     require_once $CFG->dirroot . "/mod/twf/lib.php";
     // Check they have the view twf capability.
     require_capability('mod/twf:viewdiscussion', $modcontext, null, true, 'noviewdiscussionspermission', 'twf');
     if (!($post = twf_get_post_full($discussion->firstpost))) {
         throw new moodle_exception('notexists', 'twf');
     }
     // This function check groups, qanda, timed discussions, etc.
     if (!twf_user_can_see_post($twf, $discussion, $post, null, $cm)) {
         throw new moodle_exception('noviewdiscussionspermission', 'twf');
     }
     $canviewfullname = has_capability('moodle/site:viewfullnames', $modcontext);
     // We will add this field in the response.
     $canreply = twf_user_can_post($twf, $discussion, $USER, $cm, $course, $modcontext);
     $twftracked = twf_tp_is_tracked($twf);
     $sort = 'p.' . $sortby . ' ' . $sortdirection;
     $allposts = twf_get_all_discussion_posts($discussion->id, $sort, $twftracked);
     foreach ($allposts as $post) {
         if (!twf_user_can_see_post($twf, $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 twf_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_twf', '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_twf', 'attachment', $post->id, "filename", false)) {
                 foreach ($files as $file) {
                     $filename = $file->get_filename();
                     $fileurl = moodle_url::make_webservice_pluginfile_url($modcontext->id, 'mod_twf', '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;
 }
Beispiel #4
0
/**
 * Checks to see if a user can view a particular post.
 *
 * @deprecated since Moodle 2.4 use twf_user_can_see_post() instead
 *
 * @param object $post
 * @param object $course
 * @param object $cm
 * @param object $twf
 * @param object $discussion
 * @param object $user
 * @return boolean
 */
function twf_user_can_view_post($post, $course, $cm, $twf, $discussion, $user = null)
{
    debugging('twf_user_can_view_post() is deprecated. Please use twf_user_can_see_post() instead.', DEBUG_DEVELOPER);
    return twf_user_can_see_post($twf, $discussion, $post, $user, $cm);
}
Beispiel #5
0
$forcefocus = empty($reply) ? NULL : 'message';
if (!empty($discussion->id)) {
    $PAGE->navbar->add(format_string($toppost->subject, true), "discuss.php?d={$discussion->id}");
}
if ($post->parent) {
    $PAGE->navbar->add(get_string('reply', 'twf'));
}
if ($edit) {
    $PAGE->navbar->add(get_string('edit', 'twf'));
}
$PAGE->set_title("{$course->shortname}: {$strdiscussionname} " . format_string($toppost->subject));
$PAGE->set_heading($course->fullname);
echo $OUTPUT->header();
echo $OUTPUT->heading(format_string($twf->name), 2);
// checkup
if (!empty($parent) && !twf_user_can_see_post($twf, $discussion, $post, null, $cm)) {
    print_error('cannotreply', 'twf');
}
if (empty($parent) && empty($edit) && !twf_user_can_post_discussion($twf, $groupid, -1, $cm, $modcontext)) {
    print_error('cannotcreatediscussion', 'twf');
}
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)))) {
Beispiel #6
0
/**
 * @todo Document this function
 *
 * @global object
 * @global object
 * @uses CONTEXT_MODULE
 * @return void
 */
function twf_print_posts_threaded($course, &$cm, $twf, $discussion, $parent, $depth, $reply, $twftracked, $posts)
{
    global $USER, $CFG;
    $link = false;
    if (!empty($posts[$parent->id]->children)) {
        $posts = $posts[$parent->id]->children;
        $modcontext = context_module::instance($cm->id);
        $canviewfullnames = has_capability('moodle/site:viewfullnames', $modcontext);
        foreach ($posts as $post) {
            echo '<div class="indent">';
            if ($depth > 0) {
                $ownpost = $USER->id == $post->userid;
                $post->subject = format_string($post->subject);
                $postread = !empty($post->postread);
                twf_print_post($post, $discussion, $twf, $cm, $course, $ownpost, $reply, $link, '', '', $postread, true, $twftracked);
            } else {
                if (!twf_user_can_see_post($twf, $discussion, $post, NULL, $cm)) {
                    echo "</div>\n";
                    continue;
                }
                $by = new stdClass();
                $by->name = fullname($post, $canviewfullnames);
                $by->date = userdate($post->modified);
                if ($twftracked) {
                    if (!empty($post->postread)) {
                        $style = '<span class="twfthread read">';
                    } else {
                        $style = '<span class="twfthread unread">';
                    }
                } else {
                    $style = '<span class="twfthread">';
                }
                echo $style . "<a name=\"{$post->id}\"></a>" . "<a href=\"discuss.php?d={$post->discussion}&amp;parent={$post->id}\">" . format_string($post->subject, true) . "</a> ";
                print_string("bynameondate", "twf", $by);
                echo "</span>";
            }
            twf_print_posts_threaded($course, $cm, $twf, $discussion, $post, $depth - 1, $reply, $twftracked, $posts);
            echo "</div>\n";
        }
    }
}