Пример #1
0
                 redirect("view.php?f={$discussion->forum}");
             } else {
                 if (forum_delete_post($post, has_capability('mod/forum:deleteanypost', $modcontext), $course, $cm, $forum)) {
                     if ($forum->type == 'single') {
                         // Single discussion forums are an exception. We show
                         // the forum itself since it only has one discussion
                         // thread.
                         $discussionurl = "view.php?f={$forum->id}";
                     } else {
                         $discussionurl = "discuss.php?d={$post->discussion}";
                     }
                     $params = array('context' => $modcontext, 'objectid' => $post->id, 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type));
                     if ($post->userid !== $USER->id) {
                         $params['relateduserid'] = $post->userid;
                     }
                     $event = \mod_forum\event\post_deleted::create($params);
                     $event->add_record_snapshot('forum_posts', $post);
                     $event->add_record_snapshot('forum_discussions', $discussion);
                     $event->trigger();
                     redirect(forum_go_back_to($discussionurl));
                 } else {
                     print_error('errorwhiledelete', 'forum');
                 }
             }
         }
     }
 } else {
     // User just asked to delete something
     forum_set_return();
     $PAGE->navbar->add(get_string('delete', 'forum'));
     $PAGE->set_title($course->shortname);
Пример #2
0
/**
 * Deletes a single forum post.
 *
 * @global object
 * @param object $post Forum post object
 * @param mixed $children Whether to delete children. If false, returns false
 *   if there are any children (without deleting the post). If true,
 *   recursively deletes all children. If set to special value 'ignore', deletes
 *   post regardless of children (this is for use only when deleting all posts
 *   in a disussion).
 * @param object $course Course
 * @param object $cm Course-module
 * @param object $forum Forum
 * @param bool $skipcompletion True to skip updating completion state if it
 *   would otherwise be updated, i.e. when deleting entire forum anyway.
 * @return bool
 */
function forum_delete_post($post, $children, $course, $cm, $forum, $skipcompletion = false)
{
    global $DB, $CFG, $USER;
    require_once $CFG->libdir . '/completionlib.php';
    $context = context_module::instance($cm->id);
    if ($children !== 'ignore' && ($childposts = $DB->get_records('forum_posts', array('parent' => $post->id)))) {
        if ($children) {
            foreach ($childposts as $childpost) {
                forum_delete_post($childpost, true, $course, $cm, $forum, $skipcompletion);
            }
        } else {
            return false;
        }
    }
    // Delete ratings.
    require_once $CFG->dirroot . '/rating/lib.php';
    $delopt = new stdClass();
    $delopt->contextid = $context->id;
    $delopt->component = 'mod_forum';
    $delopt->ratingarea = 'post';
    $delopt->itemid = $post->id;
    $rm = new rating_manager();
    $rm->delete_ratings($delopt);
    // Delete attachments.
    $fs = get_file_storage();
    $fs->delete_area_files($context->id, 'mod_forum', 'attachment', $post->id);
    $fs->delete_area_files($context->id, 'mod_forum', 'post', $post->id);
    // Delete cached RSS feeds.
    if (!empty($CFG->enablerssfeeds)) {
        require_once $CFG->dirroot . '/mod/forum/rsslib.php';
        forum_rss_delete_file($forum);
    }
    if ($DB->delete_records("forum_posts", array("id" => $post->id))) {
        forum_tp_delete_read_records(-1, $post->id);
        // Just in case we are deleting the last post
        forum_discussion_update_last_post($post->discussion);
        // Update completion state if we are tracking completion based on number of posts
        // But don't bother when deleting whole thing
        if (!$skipcompletion) {
            $completion = new completion_info($course);
            if ($completion->is_enabled($cm) == COMPLETION_TRACKING_AUTOMATIC && ($forum->completiondiscussions || $forum->completionreplies || $forum->completionposts)) {
                $completion->update_state($cm, COMPLETION_INCOMPLETE, $post->userid);
            }
        }
        $params = array('context' => $context, 'objectid' => $post->id, 'other' => array('discussionid' => $post->discussion, 'forumid' => $forum->id, 'forumtype' => $forum->type));
        if ($post->userid !== $USER->id) {
            $params['relateduserid'] = $post->userid;
        }
        $event = \mod_forum\event\post_deleted::create($params);
        $event->add_record_snapshot('forum_posts', $post);
        $event->trigger();
        return true;
    }
    return false;
}
Пример #3
0
 /**
  * Test post_deleted event for a single discussion forum.
  */
 public function test_post_deleted_single()
 {
     // Setup test data.
     $course = $this->getDataGenerator()->create_course();
     $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
     $user = $this->getDataGenerator()->create_user();
     // Add a discussion.
     $record = array();
     $record['course'] = $course->id;
     $record['forum'] = $forum->id;
     $record['userid'] = $user->id;
     $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
     // Add a post.
     $record = array();
     $record['discussion'] = $discussion->id;
     $record['userid'] = $user->id;
     $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
     $context = context_module::instance($forum->cmid);
     $params = array('context' => $context, 'objectid' => $post->id, 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type));
     $event = \mod_forum\event\post_deleted::create($params);
     // Trigger and capture the event.
     $sink = $this->redirectEvents();
     $event->trigger();
     $events = $sink->get_events();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_forum\\event\\post_deleted', $event);
     $this->assertEquals($context, $event->get_context());
     $expected = array($course->id, 'forum', 'delete post', "view.php?f={$forum->id}", $post->id, $forum->cmid);
     $this->assertEventLegacyLogData($expected, $event);
     $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
     $this->assertEquals($url, $event->get_url());
     $this->assertEventContextNotUsed($event);
     $this->assertNotEmpty($event->get_name());
 }