コード例 #1
0
ファイル: lib.php プロジェクト: MoodleMetaData/MoodleMetaData
/**
 * Prints a anonforum discussion
 *
 * @uses CONTEXT_MODULE
 * @uses ANONFORUM_MODE_FLATNEWEST
 * @uses ANONFORUM_MODE_FLATOLDEST
 * @uses ANONFORUM_MODE_THREADED
 * @uses ANONFORUM_MODE_NESTED
 * @param stdClass $course
 * @param stdClass $cm
 * @param stdClass $anonforum
 * @param stdClass $discussion
 * @param stdClass $post
 * @param int $mode
 * @param mixed $canreply
 * @param bool $canrate
 */
function anonforum_print_discussion($course, $cm, $anonforum, $discussion, $post, $mode, $canreply = NULL, $canrate = false)
{
    global $USER, $CFG;
    require_once $CFG->dirroot . '/rating/lib.php';
    $ownpost = isloggedin() && $USER->id == $post->userid;
    $modcontext = context_module::instance($cm->id);
    if ($canreply === NULL) {
        $reply = anonforum_user_can_post($anonforum, $discussion, $USER, $cm, $course, $modcontext);
    } else {
        $reply = $canreply;
    }
    // $cm holds general cache for anonymous forum functions
    $cm->cache = new stdClass();
    $cm->cache->groups = groups_get_all_groups($course->id, 0, $cm->groupingid);
    $cm->cache->usersgroups = array();
    $posters = array();
    // preload all posts - TODO: improve...
    if ($mode == ANONFORUM_MODE_FLATNEWEST) {
        $sort = "p.created DESC";
    } else {
        $sort = "p.created ASC";
    }
    $anonforumtracked = anonforum_tp_is_tracked($anonforum);
    $posts = anonforum_get_all_discussion_posts($discussion->id, $sort, $anonforumtracked);
    $post = $posts[$post->id];
    foreach ($posts as $pid => $p) {
        $posters[$p->userid] = $p->userid;
    }
    // preload all groups of ppl that posted in this discussion
    if ($postersgroups = groups_get_all_groups($course->id, $posters, $cm->groupingid, 'gm.id, gm.groupid, gm.userid')) {
        foreach ($postersgroups as $pg) {
            if (!isset($cm->cache->usersgroups[$pg->userid])) {
                $cm->cache->usersgroups[$pg->userid] = array();
            }
            $cm->cache->usersgroups[$pg->userid][$pg->groupid] = $pg->groupid;
        }
        unset($postersgroups);
    }
    //load ratings
    if ($anonforum->assessed != RATING_AGGREGATE_NONE) {
        $ratingoptions = new stdClass();
        $ratingoptions->context = $modcontext;
        $ratingoptions->component = 'mod_anonforum';
        $ratingoptions->ratingarea = 'post';
        $ratingoptions->items = $posts;
        $ratingoptions->aggregate = $anonforum->assessed;
        //the aggregation method
        $ratingoptions->scaleid = $anonforum->scale;
        $ratingoptions->userid = $USER->id;
        if ($anonforum->type == 'single' or !$discussion->id) {
            $ratingoptions->returnurl = "{$CFG->wwwroot}/mod/anonforum/view.php?id={$cm->id}";
        } else {
            $ratingoptions->returnurl = "{$CFG->wwwroot}/mod/anonforum/discuss.php?d={$discussion->id}";
        }
        $ratingoptions->assesstimestart = $anonforum->assesstimestart;
        $ratingoptions->assesstimefinish = $anonforum->assesstimefinish;
        $rm = new rating_manager();
        $posts = $rm->get_ratings($ratingoptions);
    }
    $post->anonforum = $anonforum->id;
    // Add the anonforum id to the post object, later used by anonforum_print_post
    $post->anonforumtype = $anonforum->type;
    $post->subject = format_string($post->subject);
    $postread = !empty($post->postread);
    anonforum_print_post($post, $discussion, $anonforum, $cm, $course, $ownpost, $reply, false, '', '', $postread, true, $anonforumtracked);
    switch ($mode) {
        case ANONFORUM_MODE_FLATOLDEST:
        case ANONFORUM_MODE_FLATNEWEST:
        default:
            anonforum_print_posts_flat($course, $cm, $anonforum, $discussion, $post, $mode, $reply, $anonforumtracked, $posts);
            break;
        case ANONFORUM_MODE_THREADED:
            anonforum_print_posts_threaded($course, $cm, $anonforum, $discussion, $post, 0, $reply, $anonforumtracked, $posts);
            break;
        case ANONFORUM_MODE_NESTED:
            anonforum_print_posts_nested($course, $cm, $anonforum, $discussion, $post, $reply, $anonforumtracked, $posts);
            break;
    }
}
コード例 #2
0
 /**
  * @global object
  */
 public function load_data()
 {
     global $DB;
     if ($this->postid) {
         if (!($this->post = $DB->get_record('anonforum_posts', array('id' => $this->postid)))) {
             throw new portfolio_caller_exception('invalidpostid', 'anonforum');
         }
     }
     $dparams = array();
     if ($this->discussionid) {
         $dbparams = array('id' => $this->discussionid);
     } else {
         if ($this->post) {
             $dbparams = array('id' => $this->post->discussion);
         } else {
             throw new portfolio_caller_exception('mustprovidediscussionorpost', 'anonforum');
         }
     }
     if (!($this->discussion = $DB->get_record('anonforum_discussions', $dbparams))) {
         throw new portfolio_caller_exception('invaliddiscussionid', 'anonforum');
     }
     if (!($this->anonforum = $DB->get_record('anonforum', array('id' => $this->discussion->anonforum)))) {
         throw new portfolio_caller_exception('invalidanonforumid', 'anonforum');
     }
     if (!($this->cm = get_coursemodule_from_instance('anonforum', $this->anonforum->id))) {
         throw new portfolio_caller_exception('invalidcoursemodule');
     }
     $this->modcontext = context_module::instance($this->cm->id);
     $fs = get_file_storage();
     if ($this->post) {
         if ($this->attachment) {
             $this->set_file_and_format_data($this->attachment);
         } else {
             $attach = $fs->get_area_files($this->modcontext->id, 'mod_anonforum', 'attachment', $this->post->id, 'timemodified', false);
             $embed = $fs->get_area_files($this->modcontext->id, 'mod_anonforum', 'post', $this->post->id, 'timemodified', false);
             $files = array_merge($attach, $embed);
             $this->set_file_and_format_data($files);
         }
         if (!empty($this->multifiles)) {
             $this->keyedfiles[$this->post->id] = $this->multifiles;
         } else {
             if (!empty($this->singlefile)) {
                 $this->keyedfiles[$this->post->id] = array($this->singlefile);
             }
         }
     } else {
         // whole thread
         $fs = get_file_storage();
         $this->posts = anonforum_get_all_discussion_posts($this->discussion->id, 'p.created ASC');
         $this->multifiles = array();
         foreach ($this->posts as $post) {
             $attach = $fs->get_area_files($this->modcontext->id, 'mod_anonforum', 'attachment', $post->id, 'timemodified', false);
             $embed = $fs->get_area_files($this->modcontext->id, 'mod_anonforum', 'post', $post->id, 'timemodified', false);
             $files = array_merge($attach, $embed);
             if ($files) {
                 $this->keyedfiles[$post->id] = $files;
             } else {
                 continue;
             }
             $this->multifiles = array_merge($this->multifiles, array_values($this->keyedfiles[$post->id]));
         }
     }
     if (empty($this->multifiles) && !empty($this->singlefile)) {
         $this->multifiles = array($this->singlefile);
         // copy_files workaround
     }
     // depending on whether there are files or not, we might have to change richhtml/plainhtml
     if (empty($this->attachment)) {
         if (!empty($this->multifiles)) {
             $this->add_format(PORTFOLIO_FORMAT_RICHHTML);
         } else {
             $this->add_format(PORTFOLIO_FORMAT_PLAINHTML);
         }
     }
 }