} if (!($twf = $DB->get_record("twf", array("id" => $discussion->twf)))) { print_error('invalidtwfid', 'twf'); } if (!($cm = get_coursemodule_from_instance("twf", $twf->id, $twf->course))) { print_error('invalidcoursemodule'); } if (!($course = $DB->get_record('course', array('id' => $twf->course)))) { print_error('invalidcourseid'); } require_login($course, false, $cm); $modcontext = context_module::instance($cm->id); if (!($post->userid == $USER->id && has_capability('mod/twf:deleteownpost', $modcontext) || has_capability('mod/twf:deleteanypost', $modcontext))) { print_error('cannotdeletepost', 'twf'); } $replycount = twf_count_replies($post); if (!empty($confirm) && confirm_sesskey()) { // User has confirmed the delete //check user capability to delete post. $timepassed = time() - $post->created; if ($timepassed > $CFG->maxeditingtime && !has_capability('mod/twf:deleteanypost', $modcontext)) { print_error("cannotdeletepost", "twf", twf_go_back_to("discuss.php?d={$post->discussion}")); } if ($post->totalscore) { notice(get_string('couldnotdeleteratings', 'rating'), twf_go_back_to("discuss.php?d={$post->discussion}")); } else { 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
/** * @global object * @param object $post * @param bool $children * @return int */ function twf_count_replies($post, $children = true) { global $DB; $count = 0; if ($children) { if ($childposts = $DB->get_records('twf_posts', array('parent' => $post->id))) { foreach ($childposts as $childpost) { $count++; // For this child $count += twf_count_replies($childpost, true); } } } else { $count += $DB->count_records('twf_posts', array('parent' => $post->id)); } return $count; }