예제 #1
0
     $ratingoptions->context = $cm->context;
     $ratingoptions->items = array($post);
     $ratingoptions->aggregate = $forum->assessed;
     //the aggregation method
     $ratingoptions->scaleid = $forum->scale;
     $ratingoptions->userid = $user->id;
     $ratingoptions->assesstimestart = $forum->assesstimestart;
     $ratingoptions->assesstimefinish = $forum->assesstimefinish;
     if ($forum->type == 'single' or !$post->discussion) {
         $ratingoptions->returnurl = $forumurl;
     } else {
         $ratingoptions->returnurl = "{$CFG->wwwroot}/mod/forum/discuss.php?d={$discussion->id}";
     }
     $ratingoptions->assesstimestart = $forum->assesstimestart;
     $ratingoptions->assesstimefinish = $forum->assesstimefinish;
     $updatedpost = $rm->get_ratings($ratingoptions);
     //updating the array this way because we're iterating over a collection and updating them one by one
     $result->posts[$updatedpost[0]->id] = $updatedpost[0];
 }
 $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id)));
 $forumname = format_string($forum->name, true, array('context' => $cm->context));
 $fullsubjects = array();
 if (!$isspecificcourse && !$hasparentaccess) {
     $fullsubjects[] = html_writer::link(new moodle_url('/course/view.php', array('id' => $course->id)), $courseshortname);
     $fullsubjects[] = html_writer::link($forumurl, $forumname);
 } else {
     $fullsubjects[] = html_writer::tag('span', $courseshortname);
     $fullsubjects[] = html_writer::tag('span', $forumname);
 }
 if ($forum->type != 'single') {
     $discussionname = format_string($discussion->name, true, array('context' => $cm->context));
예제 #2
0
파일: lib.php 프로젝트: nuckey/moodle
    /**
    * Update this rating in the database
    * @param int $rating the integer value of this rating
    * @return void
    */
    public function update_rating($rating) {
        global $DB;

        $data = new stdclass();
        $table = 'rating';

        $item = new stdclass();
        $item->id = $this->itemid;
        $items = array($item);

        $ratingoptions = new stdclass();
        $ratingoptions->context = $this->context;
        $ratingoptions->items = $items;
        $ratingoptions->aggregate = RATING_AGGREGATE_AVERAGE;//we dont actually care what aggregation method is applied
        $ratingoptions->scaleid = $this->scaleid;
        $ratingoptions->userid = $this->userid;

        $rm = new rating_manager();
        $items = $rm->get_ratings($ratingoptions);
        if( empty($items) || empty($items[0]->rating) || empty($items[0]->rating->id) ) {
            $data->contextid    = $this->context->id;
            $data->rating       = $rating;
            $data->scaleid      = $this->scaleid;
            $data->userid       = $this->userid;
            $data->itemid       = $this->itemid;

            $time = time();
            $data->timecreated = $time;
            $data->timemodified = $time;

            $DB->insert_record($table, $data);
        }
        else {
            $data->id       = $items[0]->rating->id;
            $data->rating       = $rating;

            $time = time();
            $data->timemodified = $time;

            $DB->update_record($table, $data);
        }
    }
 require_once $CFG->dirroot . '/rating/lib.php';
 if ($glossary->assessed != RATING_AGGREGATE_NONE) {
     $ratingoptions = new stdClass();
     $ratingoptions->context = $context;
     $ratingoptions->component = 'mod_glossary';
     $ratingoptions->ratingarea = 'entry';
     $ratingoptions->items = $allentries;
     $ratingoptions->aggregate = $glossary->assessed;
     //the aggregation method
     $ratingoptions->scaleid = $glossary->scale;
     $ratingoptions->userid = $USER->id;
     $ratingoptions->returnurl = $CFG->wwwroot . '/mod/glossary/view.php?id=' . $cm->id;
     $ratingoptions->assesstimestart = $glossary->assesstimestart;
     $ratingoptions->assesstimefinish = $glossary->assesstimefinish;
     $rm = new rating_manager();
     $allentries = $rm->get_ratings($ratingoptions);
 }
 foreach ($allentries as $entry) {
     // Setting the pivot for the current entry
     $pivot = $entry->glossarypivot;
     $upperpivot = $textlib->strtoupper($pivot);
     $pivottoshow = $textlib->strtoupper(format_string($pivot, true, $fmtoptions));
     // Reduce pivot to 1cc if necessary
     if (!$fullpivot) {
         $upperpivot = $textlib->substr($upperpivot, 0, 1);
         $pivottoshow = $textlib->substr($pivottoshow, 0, 1);
     }
     // if there's a group break
     if ($currentpivot != $upperpivot) {
         // print the group break if apply
         if ($printpivot) {
예제 #4
0
     }
 }
 $post->subject = $fullsubject;
 $post->subjectnoformat = true;
 //add the ratings information to the post
 //Unfortunately seem to have do this individually as posts may be from different forums
 if ($forum->assessed != RATING_AGGREGATE_NONE) {
     $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
     $ratingoptions->context = $modcontext;
     $ratingoptions->items = array($post);
     $ratingoptions->aggregate = $forum->assessed;
     //the aggregation method
     $ratingoptions->scaleid = $forum->scale;
     $ratingoptions->assesstimestart = $forum->assesstimestart;
     $ratingoptions->assesstimefinish = $forum->assesstimefinish;
     $postswithratings = $rm->get_ratings($ratingoptions);
     if ($postswithratings && count($postswithratings) == 1) {
         $post = $postswithratings[0];
     }
 }
 // Identify search terms only found in HTML markup, and add a warning about them to
 // the start of the message text. However, do not do the highlighting here. forum_print_post
 // will do it for us later.
 $missing_terms = "";
 $options = new stdClass();
 $options->trusted = $post->messagetrust;
 $post->message = highlight($strippedsearch, format_text($post->message, $post->messageformat, $options, $course->id), 0, '<fgw9sdpq4>', '</fgw9sdpq4>');
 foreach ($searchterms as $searchterm) {
     if (preg_match("/{$searchterm}/i", $post->message) && !preg_match('/<fgw9sdpq4>' . $searchterm . '<\\/fgw9sdpq4>/i', $post->message)) {
         $missing_terms .= " {$searchterm}";
     }
예제 #5
0
파일: lib.php 프로젝트: alanaipe2015/moodle
 /**
  * Update this rating in the database
  *
  * @param int $rating the integer value of this rating
  */
 public function update_rating($rating)
 {
     global $DB;
     $time = time();
     $data = new stdClass();
     $data->rating = $rating;
     $data->timemodified = $time;
     $item = new stdclass();
     $item->id = $this->itemid;
     $items = array($item);
     $ratingoptions = new stdClass();
     $ratingoptions->context = $this->context;
     $ratingoptions->component = $this->component;
     $ratingoptions->ratingarea = $this->ratingarea;
     $ratingoptions->items = $items;
     $ratingoptions->aggregate = RATING_AGGREGATE_AVERAGE;
     // We dont actually care what aggregation method is applied.
     $ratingoptions->scaleid = $this->scaleid;
     $ratingoptions->userid = $this->userid;
     $rm = new rating_manager();
     $items = $rm->get_ratings($ratingoptions);
     $firstitem = $items[0]->rating;
     if (empty($firstitem->id)) {
         // Insert a new rating.
         $data->contextid = $this->context->id;
         $data->component = $this->component;
         $data->ratingarea = $this->ratingarea;
         $data->rating = $rating;
         $data->scaleid = $this->scaleid;
         $data->userid = $this->userid;
         $data->itemid = $this->itemid;
         $data->timecreated = $time;
         $data->timemodified = $time;
         $DB->insert_record('rating', $data);
     } else {
         // Update the rating.
         $data->id = $firstitem->id;
         $DB->update_record('rating', $data);
     }
 }
 /**
  * Obtains the root post of the discussion. This actually requests all
  * posts from the database; the first is returned, but others are
  * accessible from methods in the first.
  * If available, cached information is used unless
  * you set $usecache to false. The cache is stored within the discussion
  * object so will not persist beyond a request unless you make the
  * discussion object persist too.
  * @param bool $usecache True to use cache if available, false to
  *    request fresh data
  * @param int $userid User ID to get user-specific data (initially, post
  *   flags) for; 0 = current
  * @return mod_forumng_post Post object
  */
 public function get_root_post($usecache = true, $userid = 0)
 {
     global $CFG, $USER;
     require_once $CFG->dirroot . '/rating/lib.php';
     if (!$usecache || !$this->rootpost) {
         if (!$usecache || !$this->postscache) {
             $read = !mod_forumng::mark_read_automatically($userid);
             // Retrieve most posts in the discussion - even deleted
             // ones. These are necessary in case somebody deletes a post that has
             // replies. They will display as 'deleted post'. We don't retrieve
             // old versions of edited posts. Posts are retrieved in created order
             // so that the order of replies remains constant when we build the tree.
             $posts = mod_forumng_post::query_posts('fp.discussionid=? AND fp.oldversion=0', array($this->discussionfields->id), 'fp.created', $this->forum->has_ratings(), true, false, $userid, false, false, '', '', $read);
             // Load standard ratings.
             if ($this->get_forum()->get_enableratings() == mod_forumng::FORUMNG_STANDARD_RATING) {
                 // If grading is 'No grading' or 'Teacher grades students'.
                 if ($this->get_forum()->get_grading() == mod_forumng::GRADING_NONE || $this->get_forum()->get_grading() == mod_forumng::GRADING_MANUAL) {
                     // Set the aggregation method.
                     if ($this->get_forum()->get_rating_scale() > 0) {
                         $aggregate = RATING_AGGREGATE_AVERAGE;
                     } else {
                         $aggregate = RATING_AGGREGATE_COUNT;
                     }
                 } else {
                     $aggregate = $this->get_forum()->get_grading();
                 }
                 $ratingoptions = new stdClass();
                 $ratingoptions->context = $this->get_forum()->get_context();
                 $ratingoptions->component = 'mod_forumng';
                 $ratingoptions->ratingarea = 'post';
                 $ratingoptions->items = $posts;
                 $ratingoptions->aggregate = $aggregate;
                 $ratingoptions->scaleid = $this->get_forum()->get_rating_scale();
                 $ratingoptions->userid = $USER->id;
                 $ratingoptions->assesstimestart = $this->forum->get_ratingfrom();
                 $ratingoptions->assesstimefinish = $this->forum->get_ratinguntil();
                 $ratingoptions->returnurl = $this->get_moodle_url();
                 $rm = new rating_manager();
                 $posts = $rm->get_ratings($ratingoptions);
             }
             $this->postscache = serialize($posts);
         } else {
             $posts = unserialize($this->postscache);
         }
         // Add numbers to posts
         $i = 1;
         foreach ($posts as $post) {
             $post->number = $i++;
         }
         // Obtain post relationships
         $children = array();
         foreach ($posts as $id => $fields) {
             if (!array_key_exists($fields->parentpostid, $children)) {
                 $children[$fields->parentpostid] = array();
             }
             $children[$fields->parentpostid][] = $id;
         }
         // Recursively build posts
         $this->rootpost = $this->build_posts($posts, $children, $this->discussionfields->postid, null);
         // Update the 'next/previous' unread lists stored in posts
         if ($this->get_unread_data_user_id() != -1) {
             $linear = array();
             $this->rootpost->build_linear_children($linear);
             $nextunread = array();
             $dump = '';
             foreach ($linear as $index => $post) {
                 $nextunread[$index] = null;
                 if ($post->is_unread() && (!$post->get_deleted() || $post->can_undelete($dump))) {
                     for ($j = $index - 1; $j >= 0; $j--) {
                         if ($nextunread[$j]) {
                             break;
                         }
                         $nextunread[$j] = $post;
                     }
                 }
             }
             $previous = null;
             foreach ($linear as $index => $post) {
                 $post->set_unread_list($nextunread[$index], $previous);
                 if ($post->is_unread() && (!$post->get_deleted() || $post->can_undelete($dump))) {
                     $previous = $post;
                 }
             }
             // Update cached version to include this data
             if ($this->incache) {
                 $this->cache();
             }
         }
     }
     return $this->rootpost;
 }
예제 #7
0
/**
 * Displays the ratingsfor a specific episode
 * @global stdClass $CFG
 * @global stdClass $USER
 * @global stdClass $DB
 * @global stdClass $OUTPUT
 * @param object $episode
 * @param object $cm
 * @param object $course
 */
function pcast_display_episode_ratings($episode, $cm, $course)
{
    global $CFG, $USER, $DB, $OUTPUT;
    $sql = pcast_get_episode_sql();
    $sql .= " WHERE p.id = ?";
    $episodes = $DB->get_records_sql($sql, array('id' => $episode->id));
    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
    // load ratings
    require_once $CFG->dirroot . '/rating/lib.php';
    if ($episode->assessed != RATING_AGGREGATE_NONE) {
        $ratingoptions = new stdClass();
        // $ratingoptions->plugintype = 'mod';
        // $ratingoptions->pluginname = 'pcast';
        $ratingoptions->component = 'mod_pcast';
        $ratingoptions->context = $context;
        $ratingoptions->items = $episodes;
        $ratingoptions->aggregate = $episode->assessed;
        //the aggregation method
        $ratingoptions->scaleid = $episode->scale;
        $ratingoptions->userid = $USER->id;
        $ratingoptions->returnurl = new moodle_url('/mod/pcast/showepisode.php', array('eid' => $episode->id, 'mode' => PCAST_EPISODE_COMMENT_AND_RATE));
        $ratingoptions->assesstimestart = $episode->assesstimestart;
        $ratingoptions->assesstimefinish = $episode->assesstimefinish;
        $ratingoptions->ratingarea = 'episode';
        $rm = new rating_manager();
        $allepisodes = $rm->get_ratings($ratingoptions);
    }
    foreach ($allepisodes as $thisepisode) {
        if (!empty($thisepisode->rating)) {
            echo html_writer::tag('div', $OUTPUT->render($thisepisode->rating), array('class' => 'pcast-episode-rating'));
        }
    }
}
 /**
  * Creates a forum post object, forum object, and all related data from a
  * single forum post ID. Intended when entering a page which uses post ID
  * as a parameter.
  * @param int $id ID of forum post
  * @param int $cloneid If this is in a shared forum, must be the id of the
  *   clone forum currently in use, or CLONE_DIRECT; otherwise must be 0
  * @param bool $wholediscussion If true, retrieves entire discussion
  *   instead of just this single post
  * @param bool $usecache True to use cache when retrieving the discussion
  * @param int $userid User ID to get post on behalf of (controls flag data
  *   retrieved)
  * @return mod_forumng_post Post object
  */
 public static function get_from_id($id, $cloneid, $wholediscussion = false, $usecache = false, $userid = 0)
 {
     global $CFG, $USER;
     require_once $CFG->dirroot . '/rating/lib.php';
     if ($wholediscussion) {
         $discussion = mod_forumng_discussion::get_from_post_id($id, $cloneid, $usecache, $usecache);
         $root = $discussion->get_root_post();
         return $root->find_child($id);
     } else {
         // Get post data (including extra data such as ratings and flags)
         $records = self::query_posts('fp.id = ?', array($id), 'fp.id', true, true, false, $userid);
         if (count($records) != 1) {
             throw new coding_exception("Invalid post ID {$id}");
         }
         $postfields = reset($records);
         $discussion = mod_forumng_discussion::get_from_id($postfields->discussionid, $cloneid);
         // Load standard ratings.
         $forum = $discussion->get_forum();
         if ($forum->get_enableratings() == mod_forumng::FORUMNG_STANDARD_RATING) {
             // If grading is 'No grading' or 'Teacher grades students'.
             if ($forum->get_grading() == mod_forumng::GRADING_NONE || $forum->get_grading() == mod_forumng::GRADING_MANUAL) {
                 // Set the aggregation method.
                 if ($forum->get_rating_scale() > 0) {
                     $aggregate = RATING_AGGREGATE_AVERAGE;
                 } else {
                     $aggregate = RATING_AGGREGATE_COUNT;
                 }
             } else {
                 $aggregate = $forum->get_grading();
             }
             $ratingoptions = new stdClass();
             $ratingoptions->context = $forum->get_context(true);
             $ratingoptions->component = 'mod_forumng';
             $ratingoptions->ratingarea = 'post';
             $ratingoptions->items = array('post' => $postfields);
             $ratingoptions->aggregate = $aggregate;
             $ratingoptions->scaleid = $forum->get_rating_scale();
             $ratingoptions->userid = $USER->id;
             $ratingoptions->id = $id;
             $ratingoptions->assesstimestart = $forum->get_ratingfrom();
             $ratingoptions->assesstimefinish = $forum->get_ratinguntil();
             $ratingoptions->returnurl = $discussion->get_moodle_url();
             $rm = new rating_manager();
             $postwithratings = $rm->get_ratings($ratingoptions);
             $postfields = $postwithratings['post'];
             // Update 'post' object.
         }
         $newpost = new mod_forumng_post($discussion, $postfields);
         return $newpost;
     }
 }
예제 #9
0
 /**
  * Returns all posts in this forum by the given user within the given group.
  * @param object $forum
  * @param int $userid
  * @param int $groupid
  * @param int $ratedstart
  * @param int $ratedend
  * @param string $order Sort order; the default is fp.id - note this is preferable
  *   to fp.timecreated because it works correctly if there are two posts in
  *   the same second
  * @param bool $hasrating if true only returns posts which ahve been rated
  * @return array Array of mod_forumng_post objects
  */
 public function get_rated_posts_by_user($forum, $userid, $groupid, $order = 'fp.id', $ratedstart = null, $ratedend = null, $start = null, $end = null)
 {
     global $CFG, $USER;
     if ($forum->get_enableratings() != mod_forumng::FORUMNG_STANDARD_RATING) {
         return array();
     }
     $where = 'fd.forumngid = ? AND fp.userid <> ? AND fp.oldversion = 0 AND fp.deleted = 0';
     $whereparams = array($this->get_id(), $userid);
     if ($groupid != self::NO_GROUPS && $groupid != self::ALL_GROUPS) {
         $where .= ' AND (fd.groupid = ? OR fd.groupid IS NULL)';
         $whereparams[] = $groupid;
     }
     if (!empty($start)) {
         $where .= ' AND fp.created >= ?';
         $whereparams[] = $start;
     }
     if (!empty($end)) {
         $where .= ' AND fp.created <= ?';
         $whereparams[] = $end;
     }
     $sqlselectstring = 'SELECT r.itemid FROM {rating} r WHERE r.itemid = fp.id AND r.ratingarea = \'post\'
             AND r.contextid = ? AND r.userid = ?';
     $extraparams = array();
     if (!empty($ratedstart)) {
         $sqlselectstring .= ' AND r.timemodified >= ?';
         $extraparams[] = $ratedstart;
     }
     if (!empty($ratedend)) {
         $sqlselectstring .= ' AND r.timemodified <= ?';
         $extraparams[] = $ratedend;
     }
     $where .= ' AND ' . self::select_exists($sqlselectstring);
     $whereparams[] = $this->get_context(true)->id;
     $whereparams[] = $userid;
     $whereparams = array_merge($whereparams, $extraparams);
     $result = array();
     $posts = mod_forumng_post::query_posts($where, $whereparams, $order, false, false, true, 0, true, true);
     // Add standard ratings if enabled.
     if ($this->get_enableratings() == mod_forumng::FORUMNG_STANDARD_RATING) {
         require_once $CFG->dirroot . '/rating/lib.php';
         // If grading is 'No grading' or 'Teacher grades students'.
         if ($this->get_grading() == mod_forumng::GRADING_NONE || $this->get_grading() == mod_forumng::GRADING_MANUAL) {
             // Set the aggregation method.
             if ($this->get_rating_scale() > 0) {
                 $aggregate = RATING_AGGREGATE_AVERAGE;
             } else {
                 $aggregate = RATING_AGGREGATE_COUNT;
             }
         } else {
             $aggregate = $this->get_grading();
         }
         $ratingoptions = new stdClass();
         $ratingoptions->context = $this->get_context(true);
         $ratingoptions->component = 'mod_forumng';
         $ratingoptions->ratingarea = 'post';
         $ratingoptions->items = $posts;
         $ratingoptions->aggregate = $aggregate;
         $ratingoptions->scaleid = $this->get_rating_scale();
         $ratingoptions->userid = $USER->id;
         $ratingoptions->assesstimestart = $this->get_ratingfrom();
         $ratingoptions->assesstimefinish = $this->get_ratinguntil();
         $rm = new rating_manager();
         $posts = $rm->get_ratings($ratingoptions);
     }
     foreach ($posts as $fields) {
         $discussionfields = mod_forumng_utils::extract_subobject($fields, 'fd_');
         $discussion = new mod_forumng_discussion($this, $discussionfields, false, -1);
         $result[$fields->id] = new mod_forumng_post($discussion, $fields);
     }
     return $result;
 }
예제 #10
0
    /**
     * Test the current get_ratings method main sql
     */
    function test_get_ratings_sql() {

        // We load 3 items. Each is rated twice. For simplicity itemid == user id of the item owner
        $ctxid = SYSCONTEXTID;
        $this->load_test_data('rating',
                array('contextid', 'component', 'ratingarea', 'itemid', 'scaleid', 'rating', 'userid', 'timecreated', 'timemodified'), array(

                //user 1's items. Average == 2
                array(    $ctxid , 'mod_forum',       'post',       1 ,       10 ,       1 ,       2 ,            1 ,              1),
                array(    $ctxid , 'mod_forum',       'post',       1 ,       10 ,       3 ,       3 ,            1 ,              1),

                //user 2's items. Average == 3
                array(    $ctxid , 'mod_forum',       'post',       2 ,       10 ,       1 ,       1 ,            1 ,              1),
                array(    $ctxid , 'mod_forum',       'post',       2 ,       10 ,       5 ,       3 ,            1 ,              1),

                //user 3's items. Average == 4
                array(    $ctxid , 'mod_forum',       'post',       3 ,       10 ,       3 ,       1 ,            1 ,              1),
                array(    $ctxid , 'mod_forum',       'post',       3 ,       10 ,       5 ,       2 ,            1 ,              1)
                ));

        // a post (item) by user 1 (rated above by user 2 and 3 with average = 2)
        $user1posts = array(
                (object)array('id' => 1, 'userid' => 1, 'message' => 'hello'));
        // a post (item) by user 2 (rated above by user 1 and 3 with average = 3)
        $user2posts = array(
                (object)array('id' => 2, 'userid' => 2, 'message' => 'world'));
        // a post (item) by user 3 (rated above by user 1 and 2 with average = 4)
        $user3posts = array(
                (object)array('id' => 3, 'userid' => 3, 'message' => 'moodle'));

        // Prepare the default options
        $defaultoptions = array (
                'context'    => get_context_instance(CONTEXT_SYSTEM),
                'component'  => 'mod_forum',
                'ratingarea' => 'post',
                'scaleid'    => 10,
                'aggregate'  => RATING_AGGREGATE_AVERAGE);

        $rm = new rating_manager();

        // STEP 1: Retreive ratings using the current user

        // Get results for user 1's item (expected average 1 + 3 / 2 = 2)
        $toptions = (object)array_merge($defaultoptions, array('items' => $user1posts));
        $result = $rm->get_ratings($toptions);
        $this->assertEqual(count($result), count($user1posts));
        $this->assertEqual($result[0]->id, $user1posts[0]->id);
        $this->assertEqual($result[0]->userid, $user1posts[0]->userid);
        $this->assertEqual($result[0]->message, $user1posts[0]->message);
        $this->assertEqual($result[0]->rating->count, 2);
        $this->assertEqual($result[0]->rating->aggregate, 2);
        // Note that $result[0]->rating->rating is somewhat random
        // We didn't supply a user ID so $USER was used which will vary depending on who runs the tests

        // Get results for items of user 2 (expected average 1 + 5 / 2 = 3)
        $toptions = (object)array_merge($defaultoptions, array('items' => $user2posts));
        $result = $rm->get_ratings($toptions);
        $this->assertEqual(count($result), count($user2posts));
        $this->assertEqual($result[0]->id, $user2posts[0]->id);
        $this->assertEqual($result[0]->userid, $user2posts[0]->userid);
        $this->assertEqual($result[0]->message, $user2posts[0]->message);
        $this->assertEqual($result[0]->rating->count, 2);
        $this->assertEqual($result[0]->rating->aggregate, 3);
        // Note that $result[0]->rating->rating is somewhat random
        // We didn't supply a user ID so $USER was used which will vary depending on who runs the tests

        // Get results for items of user 3 (expected average 3 + 5 / 2 = 4)
        $toptions = (object)array_merge($defaultoptions, array('items' => $user3posts));
        $result = $rm->get_ratings($toptions);
        $this->assertEqual(count($result), count($user3posts));
        $this->assertEqual($result[0]->id, $user3posts[0]->id);
        $this->assertEqual($result[0]->userid, $user3posts[0]->userid);
        $this->assertEqual($result[0]->message, $user3posts[0]->message);
        $this->assertEqual($result[0]->rating->count, 2);
        $this->assertEqual($result[0]->rating->aggregate, 4);
        // Note that $result[0]->rating->rating is somewhat random
        // We didn't supply a user ID so $USER was used which will vary depending on who runs the tests

        // Get results for items of user 1 & 2 together (expected averages are 2 and 3, as tested above)
        $posts = array_merge($user1posts, $user2posts);
        $toptions = (object)array_merge($defaultoptions, array('items' => $posts));
        $result = $rm->get_ratings($toptions);
        $this->assertEqual(count($result), count($posts));
        $this->assertEqual($result[0]->id, $posts[0]->id);
        $this->assertEqual($result[0]->userid, $posts[0]->userid);
        $this->assertEqual($result[0]->message, $posts[0]->message);
        $this->assertEqual($result[0]->rating->count, 2);
        $this->assertEqual($result[0]->rating->aggregate, 2);
        // Note that $result[0]->rating->rating is somewhat random
        // We didn't supply a user ID so $USER was used which will vary depending on who runs the tests

        $this->assertEqual($result[1]->id, $posts[1]->id);
        $this->assertEqual($result[1]->userid, $posts[1]->userid);
        $this->assertEqual($result[1]->message, $posts[1]->message);
        $this->assertEqual($result[1]->rating->count, 2);
        $this->assertEqual($result[1]->rating->aggregate, 3);
        // Note that $result[0]->rating->rating is somewhat random
        // We didn't supply a user ID so $USER was used which will vary depending on who runs the tests

        // STEP 2: Retrieve ratings by a specified user
        //         We still expect complete aggregations and counts

        // Get results for items of user 1 rated by user 2 (avg 2, rating 1)
        $toptions = (object)array_merge($defaultoptions, array('items' => $user1posts, 'userid' => 2));
        $result = $rm->get_ratings($toptions);
        $this->assertEqual(count($result), count($user1posts));
        $this->assertEqual($result[0]->id, $user1posts[0]->id);
        $this->assertEqual($result[0]->userid, $user1posts[0]->userid);
        $this->assertEqual($result[0]->message, $user1posts[0]->message);
        $this->assertEqual($result[0]->rating->count, 2);
        $this->assertEqual($result[0]->rating->aggregate, 2);
        $this->assertEqual($result[0]->rating->rating, 1); //user 2 rated user 1 "1"
        $this->assertEqual($result[0]->rating->userid, $toptions->userid); // Must be the passed userid

        // Get results for items of user 1 rated by user 3
        $toptions = (object)array_merge($defaultoptions, array('items' => $user1posts, 'userid' => 3));
        $result = $rm->get_ratings($toptions);
        $this->assertEqual(count($result), count($user1posts));
        $this->assertEqual($result[0]->id, $user1posts[0]->id);
        $this->assertEqual($result[0]->userid, $user1posts[0]->userid);
        $this->assertEqual($result[0]->message, $user1posts[0]->message);
        $this->assertEqual($result[0]->rating->count, 2);
        $this->assertEqual($result[0]->rating->aggregate, 2);
        $this->assertEqual($result[0]->rating->rating, 3); //user 3 rated user 1 "3"
        $this->assertEqual($result[0]->rating->userid, $toptions->userid); // Must be the passed userid

        // Get results for items of user 1 & 2 together rated by user 3
        $posts = array_merge($user1posts, $user2posts);
        $toptions = (object)array_merge($defaultoptions, array('items' => $posts, 'userid' => 3));
        $result = $rm->get_ratings($toptions);
        $this->assertEqual(count($result), count($posts));
        $this->assertEqual($result[0]->id, $posts[0]->id);
        $this->assertEqual($result[0]->userid, $posts[0]->userid);
        $this->assertEqual($result[0]->message, $posts[0]->message);
        $this->assertEqual($result[0]->rating->count, 2);
        $this->assertEqual($result[0]->rating->aggregate, 2);
        $this->assertEqual($result[0]->rating->rating, 3); //user 3 rated user 1 "3"
        $this->assertEqual($result[0]->rating->userid, $toptions->userid); // Must be the passed userid

        $this->assertEqual($result[1]->id, $posts[1]->id);
        $this->assertEqual($result[1]->userid, $posts[1]->userid);
        $this->assertEqual($result[1]->message, $posts[1]->message);
        $this->assertEqual($result[1]->rating->count, 2);
        $this->assertEqual($result[1]->rating->aggregate, 3);
        $this->assertEqual($result[0]->rating->rating, 3); //user 3 rated user 2 "5"
        $this->assertEqual($result[1]->rating->userid, $toptions->userid); // Must be the passed userid

        // STEP 3: Some special cases

        // Get results for user 1's items (expected average 1 + 3 / 2 = 2)
        // supplying a non-existent user id so no rating from that user should be found
        $toptions = (object)array_merge($defaultoptions, array('items' => $user1posts));
        $toptions->userid = 123456; //non-existent user
        $result = $rm->get_ratings($toptions);
        $this->assertNull($result[0]->rating->userid);
        $this->assertNull($result[0]->rating->rating);
        $this->assertEqual($result[0]->rating->aggregate, 2);//should still get the aggregate

        // Get results for items of user 2 (expected average 1 + 5 / 2 = 3)
        // Supplying the user id of the user who owns the items so no rating should be found
        $toptions = (object)array_merge($defaultoptions, array('items' => $user2posts));
        $toptions->userid = 2; //user 2 viewing the ratings of their own item
        $result = $rm->get_ratings($toptions);
        //these should be null as the user is viewing their own item and thus cannot rate
        $this->assertNull($result[0]->rating->userid);
        $this->assertNull($result[0]->rating->rating);
        $this->assertEqual($result[0]->rating->aggregate, 3);//should still get the aggregate
    }
예제 #11
0
/**
 * Prints a forum discussion
 *
 * @uses CONTEXT_MODULE
 * @param stdClass $course
 * @param stdClass $cm
 * @param stdClass $forum
 * @param stdClass $discussion
 * @param stdClass $post
 * @param mixed $canreply
 * @param bool $canrate
 */
function hsuforum_print_discussion($course, $cm, $forum, $discussion, $post, $canreply = NULL, $canrate = false)
{
    global $USER, $CFG, $OUTPUT, $PAGE;
    require_once $CFG->dirroot . '/rating/lib.php';
    $modcontext = context_module::instance($cm->id);
    if ($canreply === NULL) {
        $reply = hsuforum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext);
    } else {
        $reply = $canreply;
    }
    $posters = array();
    $posts = hsuforum_get_all_discussion_posts($discussion->id);
    $post = $posts[$post->id];
    foreach ($posts as $pid => $p) {
        $posters[$p->userid] = $p->userid;
    }
    //load ratings
    if ($forum->assessed != RATING_AGGREGATE_NONE) {
        $ratingoptions = new stdClass();
        $ratingoptions->context = $modcontext;
        $ratingoptions->component = 'mod_hsuforum';
        $ratingoptions->ratingarea = 'post';
        $ratingoptions->items = $posts;
        $ratingoptions->aggregate = $forum->assessed;
        //the aggregation method
        $ratingoptions->scaleid = $forum->scale;
        $ratingoptions->userid = $USER->id;
        if ($forum->type == 'single' or !$discussion->id) {
            $ratingoptions->returnurl = "{$CFG->wwwroot}/mod/hsuforum/view.php?id={$cm->id}";
        } else {
            $ratingoptions->returnurl = "{$CFG->wwwroot}/mod/hsuforum/discuss.php?d={$discussion->id}";
        }
        $ratingoptions->assesstimestart = $forum->assesstimestart;
        $ratingoptions->assesstimefinish = $forum->assesstimefinish;
        $rm = new rating_manager();
        $posts = $rm->get_ratings($ratingoptions);
    }
    $post->forum = $forum->id;
    // Add the forum id to the post object, later used for rendering
    $post->forumtype = $forum->type;
    $post->subject = format_string($post->subject);
    $postread = !empty($post->postread);
    echo $OUTPUT->box_start("mod-hsuforum-posts-container article");
    $renderer = $PAGE->get_renderer('mod_hsuforum');
    echo $renderer->discussion_thread($cm, $discussion, $post, $posts, $reply);
    echo $OUTPUT->box_end();
    // End mod-hsuforum-posts-container
    return;
}
예제 #12
0
        if (function_exists($functionname)) {
            $functionname($modinstance, $rateduserid);
        }
    }
}
//object to return to client as json
$result = new stdClass();
$result->success = true;
//need to retrieve the updated item to get its new aggregate value
$item = new stdclass();
$item->id = $itemid;
$items = array($item);
//most of $ratingoptions variables were previously set
$ratingoptions->items = $items;
$ratingoptions->aggregate = $aggregationmethod;
$items = $rm->get_ratings($ratingoptions);
//for custom scales return text not the value
//this scales weirdness will go away when scales are refactored
$scalearray = null;
$aggregatetoreturn = round($items[0]->rating->aggregate, 1);
// Output a dash if aggregation method == COUNT as the count is output next to the aggregate anyway
if ($items[0]->rating->settings->aggregationmethod == RATING_AGGREGATE_COUNT or $items[0]->rating->count == 0) {
    $aggregatetoreturn = ' - ';
} else {
    if ($items[0]->rating->settings->scale->id < 0) {
        //if its non-numeric scale
        //dont use the scale item if the aggregation method is sum as adding items from a custom scale makes no sense
        if ($items[0]->rating->settings->aggregationmethod != RATING_AGGREGATE_SUM) {
            $scalerecord = $DB->get_record('scale', array('id' => -$items[0]->rating->settings->scale->id));
            if ($scalerecord) {
                $scalearray = explode(',', $scalerecord->scale);
예제 #13
0
/**
 * Get all data required to print a list of blog posts as efficiently as possible
 *
 *
 * @param object $oublog
 * @param int $offset
 * @param int $userid
 * @param bool $ignoreprivate set true to not return private posts (global blog only)
 * @return mixed all data to print a list of blog posts
 */
function oublog_get_posts($oublog, $context, $offset = 0, $cm, $groupid, $individualid = -1, $userid = null, $tag = '', $canaudit = false, $ignoreprivate = null)
{
    global $CFG, $USER, $DB;
    $params = array();
    $sqlwhere = "bi.oublogid = ?";
    $params[] = $oublog->id;
    $sqljoin = '';
    if (isset($userid)) {
        $sqlwhere .= " AND bi.userid = ? ";
        $params[] = $userid;
    }
    // Individual blog.
    if ($individualid > -1) {
        $capable = oublog_individual_has_permissions($cm, $oublog, $groupid, $individualid);
        oublog_individual_add_to_sqlwhere($sqlwhere, $params, 'bi.userid', $oublog->id, $groupid, $individualid, $capable);
    } else {
        // No individual blog.
        if (isset($groupid) && $groupid) {
            $sqlwhere .= " AND p.groupid =  ? ";
            $params[] = $groupid;
        }
    }
    if (!$canaudit) {
        $sqlwhere .= " AND (p.deletedby IS NULL or bi.userid = ?)";
        $params[] = $USER->id;
    }
    if ($tag) {
        $sqlwhere .= " AND t.tag = ? ";
        $params[] = $tag;
        $sqljoin .= " INNER JOIN {oublog_taginstances} ti ON p.id = ti.postid\n                       INNER JOIN {oublog_tags} t ON ti.tagid = t.id ";
    }
    // Visibility checks.
    if (!isloggedin() || isguestuser()) {
        $sqlwhere .= " AND p.visibility =" . OUBLOG_VISIBILITY_PUBLIC;
    } else {
        if ($oublog->global) {
            // Unless the current user has manageposts capability,
            // they cannot view 'private' posts except their own.
            if ($ignoreprivate) {
                $sqlwhere .= ' AND (p.visibility > ' . OUBLOG_VISIBILITY_COURSEUSER . ')';
            } else {
                if (!has_capability('mod/oublog:manageposts', context_system::instance())) {
                    $sqlwhere .= " AND (p.visibility >" . OUBLOG_VISIBILITY_COURSEUSER . " OR (p.visibility = " . OUBLOG_VISIBILITY_COURSEUSER . " AND u.id = ?))";
                    $params[] = $USER->id;
                }
            }
        } else {
            $context = context_module::instance($cm->id);
            if (has_capability('mod/oublog:view', $context)) {
                $sqlwhere .= " AND (p.visibility >= " . OUBLOG_VISIBILITY_COURSEUSER . " )";
            } else {
                $sqlwhere .= " AND p.visibility > " . OUBLOG_VISIBILITY_COURSEUSER;
            }
        }
    }
    $usernamefields = get_all_user_name_fields(true, 'u');
    $delusernamefields = get_all_user_name_fields(true, 'ud', null, 'del');
    $editusernamefields = get_all_user_name_fields(true, 'ue', null, 'ed');
    // Get posts. The post has the field timeposted not timecreated,
    // which is tested in rating::user_can_rate().
    $fieldlist = "p.*, p.timeposted AS timecreated,  bi.oublogid, {$usernamefields},\n                  bi.userid, u.idnumber, u.picture, u.imagealt, u.email, u.username,\n                {$delusernamefields},\n                {$editusernamefields}";
    $from = "FROM {oublog_posts} p\n                INNER JOIN {oublog_instances} bi ON p.oubloginstancesid = bi.id\n                INNER JOIN {user} u ON bi.userid = u.id\n                LEFT JOIN {user} ud ON p.deletedby = ud.id\n                LEFT JOIN {user} ue ON p.lasteditedby = ue.id\n                {$sqljoin}";
    $sql = "SELECT {$fieldlist}\n            {$from}\n            WHERE  {$sqlwhere}\n            ORDER BY p.timeposted DESC\n            ";
    $countsql = "SELECT count(p.id) {$from} WHERE {$sqlwhere}";
    $rs = $DB->get_recordset_sql($sql, $params, $offset, OUBLOG_POSTS_PER_PAGE);
    // Get paging info
    $recordcnt = $DB->count_records_sql($countsql, $params);
    if (!$rs->valid()) {
        return array(false, $recordcnt);
    }
    $cnt = 0;
    $posts = array();
    $postids = array();
    foreach ($rs as $post) {
        if ($cnt > OUBLOG_POSTS_PER_PAGE) {
            break;
        }
        if (oublog_can_view_post($post, $USER, $context, $oublog->global)) {
            if ($oublog->maxvisibility < $post->visibility) {
                $post->visibility = $oublog->maxvisibility;
            }
            if ($oublog->allowcomments == OUBLOG_COMMENTS_PREVENT) {
                $post->allowcomments = OUBLOG_COMMENTS_PREVENT;
            }
            $posts[$post->id] = $post;
            $postids[] = (int) $post->id;
            $cnt++;
        }
    }
    $rs->close();
    if (empty($posts)) {
        return array(true, $recordcnt);
    }
    // Get tags for all posts on page
    $sql = "SELECT t.*, ti.postid\n            FROM {oublog_taginstances} ti\n            INNER JOIN {oublog_tags} t ON ti.tagid = t.id\n            WHERE ti.postid IN (" . implode(",", $postids) . ") ";
    $rs = $DB->get_recordset_sql($sql);
    foreach ($rs as $tag) {
        $posts[$tag->postid]->tags[$tag->id] = $tag->tag;
    }
    // Load ratings.
    require_once $CFG->dirroot . '/rating/lib.php';
    if ($oublog->assessed != RATING_AGGREGATE_NONE) {
        $ratingoptions = new stdClass();
        $ratingoptions->context = $context;
        $ratingoptions->component = 'mod_oublog';
        $ratingoptions->ratingarea = 'post';
        $ratingoptions->items = $posts;
        $ratingoptions->aggregate = $oublog->assessed;
        // The aggregation method.
        $ratingoptions->scaleid = $oublog->scale;
        $ratingoptions->userid = $USER->id;
        $ratingoptions->assesstimestart = $oublog->assesstimestart;
        $ratingoptions->assesstimefinish = $oublog->assesstimefinish;
        $rm = new rating_manager();
        $posts = $rm->get_ratings($ratingoptions);
    }
    $rs->close();
    // Get comments for post on the page
    $sql = "SELECT c.id, c.postid, c.timeposted, c.authorname, c.authorip, c.timeapproved, c.userid, {$usernamefields}, u.picture, u.imagealt, u.email, u.idnumber\n            FROM {oublog_comments} c\n            LEFT JOIN {user} u ON c.userid = u.id\n            WHERE c.postid IN (" . implode(",", $postids) . ") AND c.deletedby IS NULL\n            ORDER BY c.timeposted ASC ";
    $rs = $DB->get_recordset_sql($sql);
    foreach ($rs as $comment) {
        $posts[$comment->postid]->comments[$comment->id] = $comment;
    }
    $rs->close();
    // Get count of comments waiting approval for posts on the page...
    if ($oublog->allowcomments >= OUBLOG_COMMENTS_ALLOWPUBLIC) {
        // Make list of all posts that allow public comments
        $publicallowed = array();
        foreach ($posts as $post) {
            if ($post->allowcomments >= OUBLOG_COMMENTS_ALLOWPUBLIC) {
                $publicallowed[] = (int) $post->id;
            }
        }
        // Only run a db query if there are some posts that allow public
        // comments (so, no performance degradation if feature is not used)
        if (count($publicallowed) > 0) {
            $sql = "SELECT cm.postid, COUNT(1) AS numpending\n                    FROM {oublog_comments_moderated} cm\n                    WHERE cm.postid IN (" . implode(",", $publicallowed) . ")\n                    AND cm.approval = 0\n                    GROUP BY cm.postid";
            $rs = $DB->get_recordset_sql($sql);
            foreach ($rs as $postinfo) {
                $posts[$postinfo->postid]->pendingcomments = $postinfo->numpending;
            }
            $rs->close();
        }
    }
    return array($posts, $recordcnt);
}
예제 #14
0
파일: view.php 프로젝트: hatone/moodle
     require_once $CFG->dirroot . '/rating/lib.php';
     if ($data->assessed != RATING_AGGREGATE_NONE) {
         $ratingoptions = new stdClass();
         $ratingoptions->context = $context;
         $ratingoptions->component = 'mod_data';
         $ratingoptions->ratingarea = 'entry';
         $ratingoptions->items = $records;
         $ratingoptions->aggregate = $data->assessed;
         //the aggregation method
         $ratingoptions->scaleid = $data->scale;
         $ratingoptions->userid = $USER->id;
         $ratingoptions->returnurl = $CFG->wwwroot . '/mod/data/' . $baseurl;
         $ratingoptions->assesstimestart = $data->assesstimestart;
         $ratingoptions->assesstimefinish = $data->assesstimefinish;
         $rm = new rating_manager();
         $records = $rm->get_ratings($ratingoptions);
     }
     data_print_template('singletemplate', $records, $data, $search, $page);
     echo $OUTPUT->paging_bar($totalcount, $page, $nowperpage, $baseurl);
 } else {
     // List template
     $baseurl = 'view.php?d=' . $data->id . '&amp;';
     //send the advanced flag through the URL so it is remembered while paging.
     $baseurl .= 'advanced=' . $advanced . '&amp;';
     if (!empty($search)) {
         $baseurl .= 'filter=1&amp;';
     }
     //pass variable to allow determining whether or not we are paging through results.
     $baseurl .= 'paging=' . $paging . '&amp;';
     echo $OUTPUT->paging_bar($totalcount, $page, $nowperpage, $baseurl);
     if (empty($data->listtemplate)) {
예제 #15
0
 /**
  * This function display the hub homepage
  * It is called early when loading any Moodle page.
  * @return integer return true if Moodle index.php home page must continue normal display
  */
 public function display_homepage()
 {
     global $PAGE, $SITE, $OUTPUT, $CFG, $USER;
     //check if the front page search should not be displayed
     //=> hand over the home page to Moodle index.php
     //Two cases possible:
     //1- the hub is private and the users are not logged in
     //2- the hub is set with no search form on the login page
     $hubprivacy = get_config('local_hub', 'privacy');
     $searchfornologin = get_config('local_hub', 'searchfornologin');
     if (($hubprivacy == HUBPRIVATE or $searchfornologin === '0') and !isloggedin()) {
         return true;
     }
     require_once $CFG->dirroot . "/local/hub/forms.php";
     $PAGE->set_url('/');
     $PAGE->set_pagetype('site-index');
     $PAGE->set_docs_path('');
     $PAGE->set_pagelayout('frontpage');
     $PAGE->set_title($SITE->fullname);
     $PAGE->set_heading($SITE->fullname);
     //little trick to require login in order to rate or comment
     $mustbelogged = optional_param('mustbelogged', false, PARAM_BOOL);
     if ($mustbelogged) {
         require_login();
     }
     //log redirection to a course page
     $redirectcourseid = optional_param('redirectcourseid', false, PARAM_INT);
     if (!empty($redirectcourseid)) {
         //do not check sesskey because can be call by RSS feed
         $course = $this->get_course($redirectcourseid);
         if (!empty($course->courseurl)) {
             $courseurl = new moodle_url($course->courseurl);
         } else {
             if (!empty($course->demourl)) {
                 $courseurl = new moodle_url($course->demourl);
             } else {
                 //we try to display a demo site but none has been set
                 echo $OUTPUT->header();
                 echo get_string('nodemo', 'local_hub');
                 echo $OUTPUT->footer();
                 die;
             }
         }
         $rss = optional_param('rss', false, PARAM_BOOL);
         $rss = empty($rss) ? '' : 'rss';
         add_to_log(SITEID, 'local_hub', 'course redirection ' . $rss, '', $redirectcourseid);
         redirect(new moodle_url($courseurl));
     }
     $search = optional_param('search', null, PARAM_TEXT);
     $renderer = $PAGE->get_renderer('local_hub');
     //forms
     //Warning: because we want to support GET and we want people to be able to give the url,
     // we need to bypass the sesskey form checking
     $_GET['sesskey'] = sesskey();
     $fromformdata['coverage'] = optional_param('coverage', 'all', PARAM_TEXT);
     $fromformdata['licence'] = optional_param('licence', 'all', PARAM_ALPHANUMEXT);
     $fromformdata['subject'] = optional_param('subject', 'all', PARAM_ALPHANUMEXT);
     $fromformdata['siteid'] = optional_param('siteid', 'all', PARAM_ALPHANUMEXT);
     $fromformdata['lastmodified'] = optional_param('lastmodified', HUB_LASTMODIFIED_WEEK, PARAM_ALPHANUMEXT);
     $fromformdata['audience'] = optional_param('audience', 'all', PARAM_ALPHANUMEXT);
     $currentlanguage = explode('_', current_language());
     $fromformdata['language'] = optional_param('language', $currentlanguage[0], PARAM_ALPHANUMEXT);
     $fromformdata['educationallevel'] = optional_param('educationallevel', 'all', PARAM_ALPHANUMEXT);
     $fromformdata['downloadable'] = optional_param('downloadable', 1, PARAM_ALPHANUM);
     $fromformdata['orderby'] = optional_param('orderby', 'newest', PARAM_ALPHA);
     $fromformdata['search'] = $search;
     $coursesearchform = new course_search_form('', $fromformdata, 'get');
     $fromform = $coursesearchform->get_data();
     $coursesearchform->set_data($fromformdata);
     $fromform = (object) $fromformdata;
     //Retrieve courses by web service
     $options = array();
     //special shortcut if a course id is given in param, we search straight forward this id
     if ($courseid = optional_param('courseid', 0, PARAM_INTEGER)) {
         $options['onlyvisible'] = true;
         $options['ids'] = array($courseid);
         $options['downloadable'] = true;
         $options['enrollable'] = true;
         $courses = $this->get_courses($options);
         $coursetotal = 1;
         //Add the name of the course to the page title
         //(useful because some sites as Facebook is going to read it to build a shared link name)
         foreach ($courses as $course) {
             $PAGE->set_title($course->fullname . ' - ' . $SITE->fullname);
         }
     } else {
         if (!empty($fromform) and optional_param('submitbutton', 0, PARAM_ALPHANUMEXT)) {
             $downloadable = optional_param('downloadable', false, PARAM_INTEGER);
             if (!empty($fromform->coverage)) {
                 $options['coverage'] = $fromform->coverage;
             }
             if ($fromform->licence != 'all') {
                 $options['licenceshortname'] = $fromform->licence;
             }
             if ($fromform->subject != 'all') {
                 $options['subject'] = $fromform->subject;
             }
             if ($fromform->audience != 'all') {
                 $options['audience'] = $fromform->audience;
             }
             if ($fromform->educationallevel != 'all') {
                 $options['educationallevel'] = $fromform->educationallevel;
             }
             if ($fromform->language != 'all') {
                 $options['language'] = $fromform->language;
             }
             //sort method
             switch ($fromform->orderby) {
                 case 'newest':
                     $options['orderby'] = 'timemodified DESC';
                     break;
                 case 'eldest':
                     $options['orderby'] = 'timemodified ASC';
                     break;
                 case 'publisher':
                     $options['orderby'] = 'publishername ASC';
                     break;
                 case 'fullname':
                     $options['orderby'] = 'fullname ASC';
                     break;
                 case 'ratingaverage':
                     $options['orderby'] = 'ratingaverage DESC';
                     break;
                 default:
                     break;
             }
             //get courses
             $options['search'] = $search;
             $options['onlyvisible'] = true;
             $options['downloadable'] = $downloadable;
             $options['enrollable'] = !$downloadable;
             $page = optional_param('page', 0, PARAM_INT);
             $courses = $this->get_courses($options, $page * HUB_COURSE_PER_PAGE, HUB_COURSE_PER_PAGE);
             $coursetotal = $this->get_courses($options, 0, 0, true);
             //reset the options
             $options['orderby'] = $fromform->orderby;
             unset($options['onlyvisible']);
         }
     }
     if (!empty($courses)) {
         //load javascript
         $courseids = array();
         //all result courses
         $courseimagenumbers = array();
         //number of screenshots of all courses (must be exact same order than $courseids)
         foreach ($courses as $course) {
             $courseids[] = $course->id;
             $courseimagenumbers[] = $course->screenshots;
         }
         $PAGE->requires->yui_module('moodle-block_community-imagegallery', 'M.blocks_community.init_imagegallery', array(array('imageids' => $courseids, 'imagenumbers' => $courseimagenumbers, 'huburl' => $CFG->wwwroot)));
         //get courses content
         foreach ($courses as $course) {
             $contents = $this->get_course_contents($course->id);
             if (!empty($contents)) {
                 foreach ($contents as $content) {
                     $course->contents[] = $content;
                 }
             }
         }
         //load ratings and comments
         require_once $CFG->dirroot . '/rating/lib.php';
         $ratingoptions = new stdclass();
         $ratingoptions->context = context_course::instance(SITEID);
         //front page course
         $ratingoptions->items = $courses;
         $ratingoptions->aggregate = RATING_AGGREGATE_COUNT;
         //the aggregation method
         $ratingoptions->scaleid = 0 - get_config('local_hub', 'courseratingscaleid');
         //rating API is expecting "minus scaleid"
         $ratingoptions->userid = $USER->id;
         $ratingoptions->returnurl = $CFG->wwwroot . "/local/hub/index.php";
         $ratingoptions->component = 'local_hub';
         $ratingoptions->ratingarea = 'featured';
         $rm = new rating_manager();
         $courses = $rm->get_ratings($ratingoptions);
         //this function return $ratingoptions->items with information about the ratings
         foreach ($courses as $course) {
             $course->rating->settings->permissions->viewany = 1;
         }
         require_once $CFG->dirroot . '/comment/lib.php';
         foreach ($courses as $course) {
             $commentoptions = new stdClass();
             $commentoptions->context = context_course::instance(SITEID);
             $commentoptions->area = 'local_hub';
             $commentoptions->itemid = $course->id;
             $commentoptions->showcount = true;
             $commentoptions->component = 'local_hub';
             $course->comment = new comment($commentoptions);
             $course->comment->set_view_permission(true);
         }
     }
     //create rss feed link
     $enablerssfeeds = get_config('local_hub', 'enablerssfeeds');
     if (!empty($enablerssfeeds)) {
         require $CFG->libdir . '/rsslib.php';
         $audience = key_exists('audience', $options) ? $options['audience'] : 'all';
         $educationallevel = key_exists('educationallevel', $options) ? $options['educationallevel'] : 'all';
         if (key_exists('downloadable', $options)) {
             $downloadable = empty($options['downloadable']) ? 0 : 1;
         } else {
             $downloadable = 'all';
         }
         $subject = key_exists('subject', $options) ? $options['subject'] : 'all';
         $licence = key_exists('licence', $options) ? $options['licence'] : 'all';
         $language = key_exists('language', $options) ? $options['language'] : 'all';
         $audience = key_exists('audience', $options) ? $options['audience'] : 'all';
         $orderby = key_exists('orderby', $options) ? $options['orderby'] : 'newest';
         $search = empty($search) ? 0 : urlencode($search);
         //retrieve guest user if user not logged in
         $userid = empty($USER->id) ? $CFG->siteguest : $USER->id;
         $ctx = context_course::instance(SITEID);
         //add the link tage to the header
         $rsslink = rss_get_url($ctx->id, $userid, 'local_hub', $downloadable . '/' . $audience . '/' . $educationallevel . '/' . $subject . '/' . $licence . '/' . $language . '/' . $search . '/');
         $PAGE->add_alternate_version('RSS', $rsslink, 'application/rss+xml');
         //create the rss icon
         $rssicon = rss_get_link($ctx->id, $userid, 'local_hub', $downloadable . '/' . $audience . '/' . $educationallevel . '/' . $subject . '/' . $licence . '/' . $language . '/' . $search . '/' . $orderby . '/', get_string('rsstooltip', 'local_hub'));
     }
     /// OUTPUT
     echo $OUTPUT->header();
     //notification message sent to publisher
     if (optional_param('messagesent', 0, PARAM_INTEGER)) {
         echo $OUTPUT->notification(get_string('messagesentsuccess', 'local_hub'), 'notifysuccess');
     }
     //search form
     $coursesearchform->display();
     //Course listing
     $options['submitbutton'] = 1;
     //need to set up the submitbutton to 1 for the paging bar (simulate search)
     //and paramlink
     //set language value back to 'all'
     if (!key_exists('language', $options)) {
         $options['language'] = 'all';
     }
     if (isset($courses) and empty($courseid)) {
         if (empty($coursetotal)) {
             $coursetotalhtml = get_string('nocourse', 'local_hub');
         } else {
             $coursetotalhtml = get_string('coursetotal', 'local_hub', $coursetotal);
         }
         echo html_writer::tag('div', $coursetotalhtml, array('class' => 'hubcoursetotal'));
     }
     if (!empty($courses)) {
         //paging bar
         if ($coursetotal > HUB_COURSE_PER_PAGE) {
             $baseurl = new moodle_url('', $options);
             $pagingbarhtml = $OUTPUT->paging_bar($coursetotal, $page, HUB_COURSE_PER_PAGE, $baseurl);
             $pagingbarhtml = html_writer::tag('div', $pagingbarhtml, array('class' => 'pagingbar'));
             echo $pagingbarhtml;
         }
         echo highlight($search, $renderer->course_list($courses));
         if (!empty($pagingbarhtml)) {
             echo $pagingbarhtml;
         }
     }
     //rss icon
     if (!empty($enablerssfeeds)) {
         echo html_writer::tag('div', $rssicon, array('class' => 'hubrsslink'));
     }
     //permalink
     if (!empty($courses)) {
         $permalinkparams = array();
         //special case: course list is a unique course for a given ID
         if (!empty($courseid)) {
             $permalinkparams['courseid'] = $courseid;
         } else {
             $permalinkparams = $options;
         }
         $permalink = html_writer::tag('div', html_writer::tag('a', get_string('permalink', 'local_hub'), array('href' => new moodle_url('', $permalinkparams))), array('class' => 'hubcoursepermalink'));
         echo $permalink;
     }
     echo $OUTPUT->footer();
 }
예제 #16
0
파일: lib.php 프로젝트: Jtgadbois/Pedadida
/**
 * Prints a forum discussion
 *
 * @uses CONTEXT_MODULE
 * @uses FORUM_MODE_FLATNEWEST
 * @uses FORUM_MODE_FLATOLDEST
 * @uses FORUM_MODE_THREADED
 * @uses FORUM_MODE_NESTED
 * @param stdClass $course
 * @param stdClass $cm
 * @param stdClass $forum
 * @param stdClass $discussion
 * @param stdClass $post
 * @param int $mode
 * @param mixed $canreply
 * @param bool $canrate
 */
function forum_print_discussion($course, $cm, $forum, $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 = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext);
    } else {
        $reply = $canreply;
    }

    // $cm holds general cache for 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 == FORUM_MODE_FLATNEWEST) {
        $sort = "p.created DESC";
    } else {
        $sort = "p.created ASC";
    }

    $forumtracked = forum_tp_is_tracked($forum);
    $posts = forum_get_all_discussion_posts($discussion->id, $sort, $forumtracked);
    $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 ($forum->assessed != RATING_AGGREGATE_NONE) {
        $ratingoptions = new stdClass;
        $ratingoptions->context = $modcontext;
        $ratingoptions->component = 'mod_forum';
        $ratingoptions->ratingarea = 'post';
        $ratingoptions->items = $posts;
        $ratingoptions->aggregate = $forum->assessed;//the aggregation method
        $ratingoptions->scaleid = $forum->scale;
        $ratingoptions->userid = $USER->id;
        if ($forum->type == 'single' or !$discussion->id) {
            $ratingoptions->returnurl = "$CFG->wwwroot/mod/forum/view.php?id=$cm->id";
        } else {
            $ratingoptions->returnurl = "$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id";
        }
        $ratingoptions->assesstimestart = $forum->assesstimestart;
        $ratingoptions->assesstimefinish = $forum->assesstimefinish;

        $rm = new rating_manager();
        $posts = $rm->get_ratings($ratingoptions);
    }


    $post->forum = $forum->id;   // Add the forum id to the post object, later used by forum_print_post
    $post->forumtype = $forum->type;

    $post->subject = format_string($post->subject);

    $postread = !empty($post->postread);

    forum_print_post($post, $discussion, $forum, $cm, $course, $ownpost, $reply, false,
                         '', '', $postread, true, $forumtracked);

    switch ($mode) {
        case FORUM_MODE_FLATOLDEST :
        case FORUM_MODE_FLATNEWEST :
        default:
            forum_print_posts_flat($course, $cm, $forum, $discussion, $post, $mode, $reply, $forumtracked, $posts);
            break;

        case FORUM_MODE_THREADED :
            forum_print_posts_threaded($course, $cm, $forum, $discussion, $post, 0, $reply, $forumtracked, $posts);
            break;

        case FORUM_MODE_NESTED :
            forum_print_posts_nested($course, $cm, $forum, $discussion, $post, $reply, $forumtracked, $posts);
            break;
    }
}
예제 #17
0
    //add rating information to the course
    require_once $CFG->dirroot . '/rating/lib.php';
    $ratingoptions = new stdclass();
    $ratingoptions->context = context_course::instance(SITEID);
    //front page course
    $ratingoptions->items = $courses;
    $ratingoptions->aggregate = RATING_AGGREGATE_COUNT;
    //the aggregation method
    $ratingoptions->scaleid = 0 - get_config('local_hub', 'courseratingscaleid');
    //rating API is expecting "minus scaleid"
    $ratingoptions->userid = $USER->id;
    $ratingoptions->returnurl = $CFG->wwwroot . "/local/hub/admin/managecourses.php";
    $ratingoptions->component = 'local_hub';
    $ratingoptions->ratingarea = 'featured';
    $rm = new rating_manager();
    $courses = $rm->get_ratings($ratingoptions);
    //this function return $ratingoptions->items with information about the ratings
    //load javascript for YUI imagegallery javascript screenshot
    $PAGE->requires->yui_module('moodle-block_community-imagegallery', 'M.blocks_community.init_imagegallery', array(array('imageids' => $courseids, 'imagenumbers' => $courseimagenumbers, 'huburl' => $CFG->wwwroot)));
}
//display header
echo $OUTPUT->header();
//display a message if we come back from site settings page
$updatecourse = optional_param('coursesettings', '', PARAM_TEXT);
if (!empty($updatecourse) and confirm_sesskey()) {
    echo $OUTPUT->notification(get_string('coursesettingsupdated', 'local_hub', $updatecourse), 'notifysuccess');
}
//display some notification
if (!empty($notification)) {
    echo $notification;
}