Пример #1
0
 if ($replycount && !has_capability('mod/twf:deleteanypost', $modcontext)) {
     print_error("couldnotdeletereplies", "twf", twf_go_back_to("discuss.php?d={$post->discussion}"));
 } else {
     if (!$post->parent) {
         // post is a discussion topic as well, so delete discussion
         if ($twf->type == 'single') {
             notice("Sorry, but you are not allowed to delete that discussion!", twf_go_back_to("discuss.php?d={$post->discussion}"));
         }
         twf_delete_discussion($discussion, false, $course, $cm, $twf);
         $params = array('objectid' => $discussion->id, 'context' => $modcontext, 'other' => array('twfid' => $twf->id));
         $event = \mod_twf\event\discussion_deleted::create($params);
         $event->add_record_snapshot('twf_discussions', $discussion);
         $event->trigger();
         redirect("view.php?f={$discussion->twf}");
     } else {
         if (twf_delete_post($post, has_capability('mod/twf:deleteanypost', $modcontext), $course, $cm, $twf)) {
             if ($twf->type == 'single') {
                 // Single discussion twfs are an exception. We show
                 // the twf itself since it only has one discussion
                 // thread.
                 $discussionurl = "view.php?f={$twf->id}";
             } else {
                 $discussionurl = "discuss.php?d={$post->discussion}";
             }
             $params = array('context' => $modcontext, 'objectid' => $post->id, 'other' => array('discussionid' => $discussion->id, 'twfid' => $twf->id, 'twftype' => $twf->type));
             if ($post->userid !== $USER->id) {
                 $params['relateduserid'] = $post->userid;
             }
             $event = \mod_twf\event\post_deleted::create($params);
             $event->add_record_snapshot('twf_posts', $post);
             $event->add_record_snapshot('twf_discussions', $discussion);
Пример #2
0
/**
 * Deletes a single twf 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 $twf Forum
 * @param bool $skipcompletion True to skip updating completion state if it
 *   would otherwise be updated, i.e. when deleting entire twf anyway.
 * @return bool
 */
function twf_delete_post($post, $children, $course, $cm, $twf, $skipcompletion = false)
{
    global $DB, $CFG;
    require_once $CFG->libdir . '/completionlib.php';
    $context = context_module::instance($cm->id);
    if ($children !== 'ignore' && ($childposts = $DB->get_records('twf_posts', array('parent' => $post->id)))) {
        if ($children) {
            foreach ($childposts as $childpost) {
                twf_delete_post($childpost, true, $course, $cm, $twf, $skipcompletion);
            }
        } else {
            return false;
        }
    }
    // Delete ratings.
    require_once $CFG->dirroot . '/rating/lib.php';
    $delopt = new stdClass();
    $delopt->contextid = $context->id;
    $delopt->component = 'mod_twf';
    $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_twf', 'attachment', $post->id);
    $fs->delete_area_files($context->id, 'mod_twf', 'post', $post->id);
    // Delete cached RSS feeds.
    if (!empty($CFG->enablerssfeeds)) {
        require_once $CFG->dirroot . '/mod/twf/rsslib.php';
        twf_rss_delete_file($twf);
    }
    if ($DB->delete_records("twf_posts", array("id" => $post->id))) {
        twf_tp_delete_read_records(-1, $post->id);
        // Just in case we are deleting the last post
        twf_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 && ($twf->completiondiscussions || $twf->completionreplies || $twf->completionposts)) {
                $completion->update_state($cm, COMPLETION_INCOMPLETE, $post->userid);
            }
        }
        return true;
    }
    return false;
}