/**
* Test forum_user_can_post_discussion
*/
public function test_forum_user_can_post_discussion()
{
global $CFG, $DB;
$this->resetAfterTest(true);
// Create course to add the module.
$course = self::getDataGenerator()->create_course(array('groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1));
$user = self::getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user->id, $course->id);
// Forum forcing separate gropus.
$record = new stdClass();
$record->course = $course->id;
$forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS));
$cm = get_coursemodule_from_instance('forum', $forum->id);
$context = context_module::instance($cm->id);
self::setUser($user);
// The user is not enroled in any group, try to post in a forum with separate groups.
$can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
$this->assertFalse($can);
// Create a group.
$group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
// Try to post in a group the user is not enrolled.
$can = forum_user_can_post_discussion($forum, $group->id, -1, $cm, $context);
$this->assertFalse($can);
// Add the user to a group.
groups_add_member($group->id, $user->id);
// Try to post in a group the user is not enrolled.
$can = forum_user_can_post_discussion($forum, $group->id + 1, -1, $cm, $context);
$this->assertFalse($can);
// Now try to post in the user group. (null means it will guess the group).
$can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
$this->assertTrue($can);
$can = forum_user_can_post_discussion($forum, $group->id, -1, $cm, $context);
$this->assertTrue($can);
// Test all groups.
$can = forum_user_can_post_discussion($forum, -1, -1, $cm, $context);
$this->assertFalse($can);
$this->setAdminUser();
$can = forum_user_can_post_discussion($forum, -1, -1, $cm, $context);
$this->assertTrue($can);
// Change forum type.
$forum->type = 'news';
$DB->update_record('forum', $forum);
// Admin can post news.
$can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
$this->assertTrue($can);
// Normal users don't.
self::setUser($user);
$can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
$this->assertFalse($can);
// Change forum type.
$forum->type = 'eachuser';
$DB->update_record('forum', $forum);
// I didn't post yet, so I should be able to post.
$can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
$this->assertTrue($can);
// Post now.
$record = new stdClass();
$record->course = $course->id;
$record->userid = $user->id;
$record->forum = $forum->id;
$discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
// I already posted, I shouldn't be able to post.
$can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
$this->assertFalse($can);
// Last check with no groups, normal forum and course.
$course->groupmode = NOGROUPS;
$course->groupmodeforce = 0;
$DB->update_record('course', $course);
$forum->type = 'general';
$forum->groupmode = NOGROUPS;
$DB->update_record('forum', $forum);
$can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
$this->assertTrue($can);
}
/**
* Check if the current user can add discussions in the given forum (and optionally for the given group).
*
* @param int $forumid the forum instance id
* @param int $groupid the group to check, default to active group. Use -1 to check if the user can post in all the groups.
* @return array of warnings and the status (true if the user can add discussions)
* @since Moodle 3.1
* @throws moodle_exception
*/
public static function can_add_discussion($forumid, $groupid = null)
{
global $DB, $CFG;
require_once $CFG->dirroot . "/mod/forum/lib.php";
$params = self::validate_parameters(self::can_add_discussion_parameters(), array('forumid' => $forumid, 'groupid' => $groupid));
$warnings = array();
// Request and permission validation.
$forum = $DB->get_record('forum', array('id' => $params['forumid']), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($forum, 'forum');
$context = context_module::instance($cm->id);
self::validate_context($context);
$status = forum_user_can_post_discussion($forum, $params['groupid'], -1, $cm, $context);
$result = array();
$result['status'] = $status;
$result['warnings'] = $warnings;
return $result;
}
/**
* Form definition
*
* @return void
*/
function definition()
{
global $CFG, $OUTPUT;
$mform =& $this->_form;
$course = $this->_customdata['course'];
$cm = $this->_customdata['cm'];
$coursecontext = $this->_customdata['coursecontext'];
$modcontext = $this->_customdata['modcontext'];
$forum = $this->_customdata['forum'];
$post = $this->_customdata['post'];
$subscribe = $this->_customdata['subscribe'];
$edit = $this->_customdata['edit'];
$thresholdwarning = $this->_customdata['thresholdwarning'];
$mform->addElement('header', 'general', '');
//fill in the data depending on page params later using set_data
// 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 display a warning if they can still post but have reached the warning threshold.
if ($thresholdwarning->canpost) {
$message = get_string($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional);
$mform->addElement('html', $OUTPUT->notification($message));
}
}
$mform->addElement('text', 'subject', get_string('subject', 'forum'), 'size="48"');
$mform->setType('subject', PARAM_TEXT);
$mform->addRule('subject', get_string('required'), 'required', null, 'client');
$mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$mform->addElement('editor', 'message', get_string('message', 'forum'), null, self::editor_options($modcontext, empty($post->id) ? null : $post->id));
$mform->setType('message', PARAM_RAW);
$mform->addRule('message', get_string('required'), 'required', null, 'client');
$manageactivities = has_capability('moodle/course:manageactivities', $coursecontext);
if (\mod_forum\subscriptions::is_forcesubscribed($forum)) {
$mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
$mform->freeze('discussionsubscribe');
$mform->setDefaults('discussionsubscribe', 0);
$mform->addHelpButton('discussionsubscribe', 'forcesubscribed', 'forum');
} else {
if (\mod_forum\subscriptions::subscription_disabled($forum) && !$manageactivities) {
$mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
$mform->freeze('discussionsubscribe');
$mform->setDefaults('discussionsubscribe', 0);
$mform->addHelpButton('discussionsubscribe', 'disallowsubscription', 'forum');
} else {
$mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
$mform->addHelpButton('discussionsubscribe', 'discussionsubscription', 'forum');
}
}
if (!empty($forum->maxattachments) && $forum->maxbytes != 1 && has_capability('mod/forum:createattachment', $modcontext)) {
// 1 = No attachments at all
$mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null, self::attachment_options($forum));
$mform->addHelpButton('attachments', 'attachment', 'forum');
}
if (empty($post->id) && $manageactivities) {
$mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
}
if (!empty($CFG->forum_enabletimedposts) && !$post->parent && has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) {
// hack alert
$mform->addElement('header', 'displayperiod', get_string('displayperiod', 'forum'));
$mform->addElement('date_time_selector', 'timestart', get_string('displaystart', 'forum'), array('optional' => true));
$mform->addHelpButton('timestart', 'displaystart', 'forum');
$mform->addElement('date_time_selector', 'timeend', get_string('displayend', 'forum'), array('optional' => true));
$mform->addHelpButton('timeend', 'displayend', 'forum');
} else {
$mform->addElement('hidden', 'timestart');
$mform->setType('timestart', PARAM_INT);
$mform->addElement('hidden', 'timeend');
$mform->setType('timeend', PARAM_INT);
$mform->setConstants(array('timestart' => 0, 'timeend' => 0));
}
if ($groupmode = groups_get_activity_groupmode($cm, $course)) {
$groupdata = groups_get_activity_allowed_groups($cm);
$groupinfo = array();
foreach ($groupdata as $groupid => $group) {
// Check whether this user can post in this group.
// We must make this check because all groups are returned for a visible grouped activity.
if (forum_user_can_post_discussion($forum, $groupid, null, $cm, $modcontext)) {
// Build the data for the groupinfo select.
$groupinfo[$groupid] = $group->name;
} else {
unset($groupdata[$groupid]);
}
}
$groupcount = count($groupinfo);
// Check whether a user can post to all of their own groups.
// Posts to all of my groups are copied to each group that the user is a member of. Certain conditions must be met.
// 1) It only makes sense to allow this when a user is in more than one group.
// Note: This check must come before we consider adding accessallgroups, because that is not a real group.
$canposttoowngroups = empty($post->edit) && $groupcount > 1;
// 2) Important: You can *only* post to multiple groups for a top level post. Never any reply.
$canposttoowngroups = $canposttoowngroups && empty($post->parent);
// 3) You also need the canposttoowngroups capability.
$canposttoowngroups = $canposttoowngroups && has_capability('mod/forum:canposttomygroups', $modcontext);
if ($canposttoowngroups) {
// This user is in multiple groups, and can post to all of their own groups.
// Note: This is not the same as accessallgroups. This option will copy a post to all groups that a
//.........这里部分代码省略.........
function get_content() {
global $CFG, $USER,$DB;
if ($this->content !== NULL) {
return $this->content;
}
$this->content = new stdClass;
$this->content->text = '';
$this->content->footer = '';
if (empty($this->instance)) {
return $this->content;
}
if ($this->page->course->newsitems) { // Create a nice listing of recent postings
require_once($CFG->dirroot.'/mod/forum/lib.php'); // We'll need this
$text = '';
if (!$forum = forum_get_course_forum($this->page->course->id, 'news')) {
return '';
}
$modinfo = get_fast_modinfo($this->page->course);
if (empty($modinfo->instances['forum'][$forum->id])) {
return '';
}
$cm = $modinfo->instances['forum'][$forum->id];
if (!$cm->uservisible) {
return '';
}
$context = context_module::instance($cm->id);
/// User must have perms to view discussions in that forum
if (!has_capability('mod/forum:viewdiscussion', $context)) {
return '';
}
/// First work out whether we can post to this group and if so, include a link
$groupmode = groups_get_activity_groupmode($cm);
$currentgroup = groups_get_activity_group($cm, true);
if (forum_user_can_post_discussion($forum, $currentgroup, $groupmode, $cm, $context)) {
$text .= '<div class="newlink"><a href="'.$CFG->wwwroot.'/mod/forum/post.php?forum='.$forum->id.'">'.
get_string('addanewtopic', 'forum').'</a></div>';
}
/// Get all the recent discussions we're allowed to see
if (! $discussions = forum_get_discussions($cm, 'p.modified DESC', false,
$currentgroup, $this->page->course->newsitems) ) {
$text .= '('.get_string('nonews', 'forum').')';
$this->content->text = $text;
return $this->content;
}
/// Actually create the listing now
$strftimerecent = get_string('strftimerecent');
$strmore = get_string('more', 'forum');
/// Accessibility: markup as a list.
$text .= "\n<ul class='unlist'>\n";
foreach ($discussions as $discussion) {
$discussion->subject = $discussion->name;
$discussion->subject = format_string($discussion->subject, true, $forum->course);
/******************code by sreekanth*****************/
$subject1 = html_writer::tag('b',get_string('subject', 'block_site_blog'),array());
$message = $DB->get_record_sql("SELECT * FROM {forum_posts} WHERE id = $discussion->id");
//print_object($message);
$text .= '<li class="post">'.
'<div class="head clearfix">'.
'<div class="date">'.userdate($discussion->modified, $strftimerecent).'</div>'.
'<div class="name">'.fullname($discussion).'</div></div>'.
'<div class="info">'.$subject1. ': <a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->discussion.'">'.$discussion->subject.'</a></div>'.
"</li>\n";
$string1= $message->message;
//$text .= $message->message;
$string = strip_tags($string1);
if (strlen($string) > 480) {
// truncate string
$stringCut = substr($string, 0, 480);
//print_object($stringCut);
// make sure it ends in a word so assassinate doesn't become ass...
$view = html_writer::tag('a',get_string('view', 'block_my_blog'),array('href'=>$CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->discussion));
$text .= "<div class='bulletin_message'>".substr($stringCut, 0, strrpos($stringCut, ' '))."...".$view."</div>";
} else{
$text .= "<div class='bulletin_message'>". $message->message ."</div>";
}
//.........这里部分代码省略.........
请发表评论