Esempio n. 1
0
 /**
  * Function to create a dummy post.
  *
  * @param array|stdClass $record
  * @return stdClass the post object
  */
 public function create_post($record = null)
 {
     global $DB;
     // Increment the anonymous forum post count.
     $this->anonforumpostcount++;
     // Variable to store time.
     $time = time() + $this->anonforumpostcount;
     $record = (array) $record;
     if (!isset($record['discussion'])) {
         throw new coding_exception('discussion must be present in phpunit_util::create_post() $record');
     }
     if (!isset($record['userid'])) {
         throw new coding_exception('userid must be present in phpunit_util::create_post() $record');
     }
     if (!isset($record['parent'])) {
         $record['parent'] = 0;
     }
     if (!isset($record['subject'])) {
         $record['subject'] = 'Anonymous forum post subject ' . $this->anonforumpostcount;
     }
     if (!isset($record['message'])) {
         $record['message'] = html_writer::tag('p', 'Anonymous forum message post ' . $this->anonforumpostcount);
     }
     if (!isset($record['created'])) {
         $record['created'] = $time;
     }
     if (!isset($record['modified'])) {
         $record['modified'] = $time;
     }
     $record = (object) $record;
     // Add the post.
     $record->id = $DB->insert_record('anonforum_posts', $record);
     // Update the last post.
     anonforum_discussion_update_last_post($record->discussion);
     return $record;
 }
Esempio n. 2
0
/**
 * Deletes a single anonforum 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 $anonforum Forum
 * @param bool $skipcompletion True to skip updating completion state if it
 *   would otherwise be updated, i.e. when deleting entire anonforum anyway.
 * @return bool
 */
function anonforum_delete_post($post, $children, $course, $cm, $anonforum, $skipcompletion = false)
{
    global $DB, $CFG;
    require_once $CFG->libdir . '/completionlib.php';
    $context = context_module::instance($cm->id);
    if ($children !== 'ignore' && ($childposts = $DB->get_records('anonforum_posts', array('parent' => $post->id)))) {
        if ($children) {
            foreach ($childposts as $childpost) {
                anonforum_delete_post($childpost, true, $course, $cm, $anonforum, $skipcompletion);
            }
        } else {
            return false;
        }
    }
    // Delete ratings.
    require_once $CFG->dirroot . '/rating/lib.php';
    $delopt = new stdClass();
    $delopt->contextid = $context->id;
    $delopt->component = 'mod_anonforum';
    $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_anonforum', 'attachment', $post->id);
    $fs->delete_area_files($context->id, 'mod_anonforum', 'post', $post->id);
    // Delete cached RSS feeds.
    if (!empty($CFG->enablerssfeeds)) {
        require_once $CFG->dirroot . '/mod/anonforum/rsslib.php';
        anonforum_rss_delete_file($anonforum);
    }
    if ($DB->delete_records("anonforum_posts", array("id" => $post->id))) {
        anonforum_tp_delete_read_records(-1, $post->id);
        // Just in case we are deleting the last post
        anonforum_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 && ($anonforum->completiondiscussions || $anonforum->completionreplies || $anonforum->completionposts)) {
                $completion->update_state($cm, COMPLETION_INCOMPLETE, $post->userid);
            }
        }
        return true;
    }
    return false;
}