public function test_oublog_get_posts_personal()
 {
     global $SITE, $USER, $DB;
     $this->resetAfterTest(true);
     $this->setAdminUser();
     $stud1 = $this->get_new_user();
     $stud2 = $this->get_new_user();
     if (!($oublog = $DB->get_record('oublog', array('global' => 1)))) {
         $oublog = $this->get_new_oublog($SITE->id, array('global' => 1, 'maxvisibility' => OUBLOG_VISIBILITY_PUBLIC));
     }
     $cm = get_coursemodule_from_instance('oublog', $oublog->id);
     $context = context_module::instance($cm->id);
     // First make sure we have some posts to use.
     $post1stub = $this->get_post_stub($oublog->id);
     $post1stub->userid = $stud1->id;
     $post1stub->visibility = OUBLOG_VISIBILITY_COURSEUSER;
     // Private.
     oublog_add_post($post1stub, $cm, $oublog, $SITE);
     $post2stub = $this->get_post_stub($oublog->id);
     $post2stub->userid = $stud2->id;
     $post2stub->visibility = OUBLOG_VISIBILITY_LOGGEDINUSER;
     // User must be logged in.
     oublog_add_post($post2stub, $cm, $oublog, $SITE);
     $post3stub = $this->get_post_stub($oublog->id);
     $post3stub->userid = $stud2->id;
     $post3stub->visibility = OUBLOG_VISIBILITY_PUBLIC;
     // Any user.
     oublog_add_post($post3stub, $cm, $oublog, $SITE);
     // Test 1 - posts using admin.
     list($posts, $recordcount) = oublog_get_posts($oublog, $context, 0, $cm, 0);
     $this->assertEquals(3, $recordcount);
     list($posts, $recordcount) = oublog_get_posts($oublog, $context, 0, $cm, 0, -1, null, null, true, true);
     $this->assertEquals(2, $recordcount);
     list($posts, $recordcount) = oublog_get_posts($oublog, $context, 0, $cm, 0, $stud2->id);
     $this->assertEquals(2, $recordcount);
     // Test 2 -  posts using logged in user.
     $this->setUser($stud2);
     list($posts, $recordcount) = oublog_get_posts($oublog, $context, 0, $cm, 0);
     $this->assertEquals(2, $recordcount);
     list($posts, $recordcount) = oublog_get_posts($oublog, $context, 0, $cm, 0, $stud1->id);
     $this->assertEquals(0, $recordcount);
     // Test 3 - posts using guest (not logged in).
     $this->setGuestUser();
     list($posts, $recordcount) = oublog_get_posts($oublog, $context, 0, $cm, 0);
     $this->assertEquals(1, $recordcount);
     list($posts, $recordcount) = oublog_get_posts($oublog, $context, 0, $cm, 0, $stud1->id);
     $this->assertEquals(0, $recordcount);
     list($posts, $recordcount) = oublog_get_posts($oublog, $context, 0, $cm, 0, $stud2->id);
     $this->assertEquals(1, $recordcount);
 }
function print_section_newsroom($course, $section, $mods, $modnamesused, $absolute = false, $width = "100%")
{
    /// Prints a section full of activity modules
    global $CFG, $USER;
    static $initialised;
    static $groupbuttons;
    static $groupbuttonslink;
    static $isediting;
    static $ismoving;
    static $strmovehere;
    static $strmovefull;
    static $strunreadpostsone;
    static $usetracking;
    static $groupings;
    if (!isset($initialised)) {
        $groupbuttons = ($course->groupmode or !$course->groupmodeforce);
        $groupbuttonslink = !$course->groupmodeforce;
        $isediting = isediting($course->id);
        $ismoving = $isediting && ismoving($course->id);
        if ($ismoving) {
            $strmovehere = get_string("movehere");
            $strmovefull = strip_tags(get_string("movefull", "", "'{$USER->activitycopyname}'"));
        }
        include_once $CFG->dirroot . '/mod/forum/lib.php';
        if ($usetracking = forum_tp_can_track_forums()) {
            $strunreadpostsone = get_string('unreadpostsone', 'forum');
        }
        $initialised = true;
    }
    $labelformatoptions = new object();
    $labelformatoptions->noclean = true;
    /// Casting $course->modinfo to string prevents one notice when the field is null
    $modinfo = get_fast_modinfo($course);
    //Acccessibility: replace table with list <ul>, but don't output empty list.
    if (!empty($section->sequence)) {
        // Fix bug #5027, don't want style=\"width:$width\".
        echo "<ul class=\"section img-text\">\n";
        $sectionmods = explode(",", $section->sequence);
        foreach ($sectionmods as $modnumber) {
            if (empty($mods[$modnumber])) {
                continue;
            }
            $mod = $mods[$modnumber];
            if ($ismoving and $mod->id == $USER->activitycopy) {
                // do not display moving mod
                continue;
            }
            if (isset($modinfo->cms[$modnumber])) {
                if (!$modinfo->cms[$modnumber]->uservisible) {
                    // visibility shortcut
                    continue;
                }
            } else {
                if (!file_exists("{$CFG->dirroot}/mod/{$mod->modname}/lib.php")) {
                    // module not installed
                    continue;
                }
                if (!coursemodule_visible_for_user($mod)) {
                    // full visibility check
                    continue;
                }
            }
            // The magic! ... if indent == 1 then ... hide module
            if ($mod->indent == 1) {
                $hiddemodule = 'hidden';
            } else {
                $hiddemodule = '';
            }
            echo '<li class="activity ' . $mod->modname . ' ' . $hiddemodule . '" id="module-' . $modnumber . '">';
            // Unique ID
            if ($ismoving) {
                echo '<a title="' . $strmovefull . '"' . ' href="' . $CFG->wwwroot . '/course/mod.php?moveto=' . $mod->id . '&amp;sesskey=' . $USER->sesskey . '">' . '<img class="movetarget" src="' . $CFG->pixpath . '/movehere.gif" ' . ' alt="' . $strmovehere . '" /></a><br />
                     ';
            }
            if ($mod->indent) {
                print_spacer(12, 20 * $mod->indent, false);
            }
            $extra = '';
            if (!empty($modinfo->cms[$modnumber]->extra)) {
                $extra = $modinfo->cms[$modnumber]->extra;
            }
            if ($mod->modname == "label") {
                echo "<span class=\"";
                if (!$mod->visible) {
                    echo 'dimmed_text';
                } else {
                    echo 'label';
                }
                echo '">';
                echo format_text($extra, FORMAT_HTML, $labelformatoptions);
                echo "</span>";
                if (!empty($CFG->enablegroupings) && !empty($mod->groupingid) && has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) {
                    if (!isset($groupings)) {
                        $groupings = groups_get_all_groupings($course->id);
                    }
                    echo " <span class=\"groupinglabel\">(" . format_string($groupings[$mod->groupingid]->name) . ')</span>';
                }
            } else {
                // Normal activity
                $instancename = format_string($modinfo->cms[$modnumber]->name, true, $course->id);
                if (!empty($modinfo->cms[$modnumber]->icon)) {
                    $icon = "{$CFG->pixpath}/" . $modinfo->cms[$modnumber]->icon;
                } else {
                    $icon = "{$CFG->modpixpath}/{$mod->modname}/icon.gif";
                }
                //Accessibility: for files get description via icon.
                $altname = '';
                if ('resource' == $mod->modname) {
                    if (!empty($modinfo->cms[$modnumber]->icon)) {
                        $possaltname = $modinfo->cms[$modnumber]->icon;
                        $mimetype = mimeinfo_from_icon('type', $possaltname);
                        $altname = get_mimetype_description($mimetype);
                    } else {
                        $altname = $mod->modfullname;
                    }
                } else {
                    $altname = $mod->modfullname;
                }
                // Avoid unnecessary duplication.
                if (false !== stripos($instancename, $altname)) {
                    $altname = '';
                }
                // File type after name, for alphabetic lists (screen reader).
                if ($altname) {
                    $altname = get_accesshide(' ' . $altname);
                }
                $linkcss = $mod->visible ? "" : " class=\"dimmed\" ";
                echo '<a ' . $linkcss . ' ' . $extra . ' href="' . $CFG->wwwroot . '/mod/' . $mod->modname . '/view.php?id=' . $mod->id . '">' . '<img src="' . $icon . '" class="activityicon" alt="" /> <span>' . $instancename . $altname . '</span></a>';
                //echo " $mod->modname ";
                // Special NEWSPAPER magic ... show summry from resource and blog's 200 chars from each post OUBlog (nadavkav)
                if ($mod->modname == 'resource') {
                    $article = get_record('resource', 'id', $mod->instance);
                    //print_r($article );
                    echo "{$article->summary}";
                }
                if ($mod->modname == 'oublog') {
                    require_once $CFG->dirroot . '/mod/oublog/locallib.php';
                    $oublog = get_record('oublog', 'id', $mod->instance);
                    if (!($oublogcm = get_coursemodule_from_instance('oublog', $oublog->id))) {
                        //error("Course module ID was incorrect");
                    }
                    $oublogcontext = get_context_instance(CONTEXT_MODULE, $oublogcm->id);
                    //oublog_check_view_permissions($oublog, $oublogcontext, $oublogcm);
                    $currentgroup = oublog_get_activity_group($oublogcm, true);
                    echo '<br/>';
                    /// Get Posts
                    list($posts, $recordcount) = oublog_get_posts($oublog, $oublogcontext, 0, $oublogcm, $currentgroup);
                    //, -1, $oubloguser->id, $tag, $canaudit);
                    $i = 0;
                    foreach ($posts as $post) {
                        if ($i < 3) {
                            echo '<a href="' . $CFG->wwwroot . '/mod/oublog/viewpost.php?blog=' . $oublog->id . '&post=' . $post->id . '">' . $post->title . '</a><br/>';
                            $summaryformatoptions->noclean = false;
                            echo mb_substr(format_text($post->message, FORMAT_HTML, $summaryformatoptions), 1, 600) . "<hr/>";
                        }
                        if ($i > 2 and $i < 6) {
                            echo '<a href="' . $CFG->wwwroot . '/mod/oublog/viewpost.php?blog=' . $oublog->id . '&post=' . $post->id . '">' . $post->title . '</a><br/>';
                        }
                        $i++;
                    }
                    //print_r($article );
                    //echo "$article->summary";
                    echo '<br/><a ' . $linkcss . ' ' . $extra . ' href="' . $CFG->wwwroot . '/mod/' . $mod->modname . '/view.php?id=' . $mod->id . '">' . '<img src="' . $icon . '" class="activityicon" alt="" /> <span>' . get_string('more', 'format_newsroom') . '</span></a>';
                }
                if (!empty($CFG->enablegroupings) && !empty($mod->groupingid) && has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) {
                    if (!isset($groupings)) {
                        $groupings = groups_get_all_groupings($course->id);
                    }
                    echo " <span class=\"groupinglabel\">(" . format_string($groupings[$mod->groupingid]->name) . ')</span>';
                }
            }
            if ($usetracking && $mod->modname == 'forum') {
                if ($unread = forum_tp_count_forum_unread_posts($mod, $course)) {
                    echo '<span class="unread"> <a href="' . $CFG->wwwroot . '/mod/forum/view.php?id=' . $mod->id . '">';
                    if ($unread == 1) {
                        echo $strunreadpostsone;
                    } else {
                        print_string('unreadpostsnumber', 'forum', $unread);
                    }
                    echo '</a></span>';
                }
            }
            if ($isediting) {
                // TODO: we must define this as mod property!
                if ($groupbuttons and $mod->modname != 'label' and $mod->modname != 'resource' and $mod->modname != 'glossary') {
                    if (!($mod->groupmodelink = $groupbuttonslink)) {
                        $mod->groupmode = $course->groupmode;
                    }
                } else {
                    $mod->groupmode = false;
                }
                echo '&nbsp;&nbsp;';
                echo make_editing_buttons($mod, $absolute, true, $mod->indent, $section->section);
            }
            echo "</li>\n";
        }
    } elseif ($ismoving) {
        echo "<ul class=\"section\">\n";
    }
    if ($ismoving) {
        echo '<li><a title="' . $strmovefull . '"' . ' href="' . $CFG->wwwroot . '/course/mod.php?movetosection=' . $section->id . '&amp;sesskey=' . $USER->sesskey . '">' . '<img class="movetarget" src="' . $CFG->pixpath . '/movehere.gif" ' . ' alt="' . $strmovehere . '" /></a></li>
             ';
    }
    if (!empty($section->sequence) || $ismoving) {
        echo "</ul><!--class='section'-->\n\n";
    }
}
if ($tag) {
    $returnurl .= '&amp;tag=' . urlencode($tag);
}
$canmanageposts = has_capability('mod/oublog:manageposts', $context);
$canaudit = has_capability('mod/oublog:audit', $context);
/// Log visit
add_to_log($course->id, "oublog", "allposts", $returnurl, $oublog->id, $cm->id);
/// Get strings
$stroublog = get_string('modulename', 'oublog');
$strnewposts = get_string('newerposts', 'oublog');
$strolderposts = get_string('olderposts', 'oublog');
$strfeeds = get_string('feeds', 'oublog');
$strfeeds .= '<img src="' . $CFG->pixpath . '/i/rss.gif" alt="' . get_string('blogfeed', 'oublog') . '"  class="feedicon" />';
$strblogsearch = get_string('searchblogs', 'oublog');
/// Get Posts
list($posts, $recordcount) = oublog_get_posts($oublog, $context, $offset, $cm, null, -1, null, $tag, $canaudit);
/// Generate extra navigation
$extranav = array();
if ($offset) {
    $a = new stdClass();
    $a->from = $offset + 1;
    $a->to = $recordcount - $offset > OUBLOG_POSTS_PER_PAGE ? $offset + OUBLOG_POSTS_PER_PAGE : $recordcount;
    $extranav = array('name' => get_string('extranavolderposts', 'oublog', $a), 'link' => '', 'type' => 'misc');
}
if ($tag) {
    $extranav = array('name' => get_string('extranavtag', 'oublog', $tag), 'link' => '', 'type' => 'misc');
}
/// Print the header
$navlinks = array();
$navlinks[] = array('name' => format_string($oublog->name), 'link' => "allposts.php", 'type' => 'activityinstance');
if ($extranav) {
 public function test_oublog_get_posts_pagination()
 {
     global $SITE, $USER, $DB;
     $this->resetAfterTest(true);
     $this->setAdminUser();
     $course = $this->get_new_course();
     // Test posts using standard course blog.
     $oublog = $this->get_new_oublog($course->id);
     $cm = get_coursemodule_from_id('oublog', $oublog->cmid);
     // Number of posts for test, more than posts per page
     $postcount = OUBLOG_POSTS_PER_PAGE + OUBLOG_POSTS_PER_PAGE / 2;
     $titlecheck = 'test_oublog_get_posts_pagination';
     // First make sure we have some posts to use.
     $posthashes = array();
     for ($i = 1; $i <= $postcount; $i++) {
         $posthashes[$i] = $this->get_post_stub($oublog->id);
         $posthashes[$i]->title = $titlecheck . '_' . $i;
     }
     // Create the posts - assumes oublog_add_post is working.
     $postids = array();
     foreach ($posthashes as $posthash) {
         $postids[] = oublog_add_post($posthash, $cm, $oublog, $course);
     }
     $context = context_module::instance($cm->id);
     // Build paging parameters for the first page .
     $page = 0;
     $offset = $page * OUBLOG_POSTS_PER_PAGE;
     // Get a list of the pages posts.
     list($posts, $recordcount) = oublog_get_posts($oublog, $context, $offset, $cm, 0);
     // Same number of records discovered that were created?
     $this->assertEquals($postcount, $recordcount);
     // Is the number of posts returned that were expected?.
     $this->assertEquals(OUBLOG_POSTS_PER_PAGE, count($posts));
     // Build paging parameters for the second page.
     $page = 1;
     $offset = $page * OUBLOG_POSTS_PER_PAGE;
     // Get the list of the second pages posts.
     list($posts, $recordcount) = oublog_get_posts($oublog, $context, $offset, $cm, 0);
     // Number of posts returned that were expected?.
     $this->assertEquals($postcount - $offset, count($posts));
 }
 /**
  * @global object
  */
 public function load_data()
 {
     global $DB, $COURSE;
     if (!($this->oublog = $DB->get_record('oublog', array('id' => $this->oublogid)))) {
         throw new portfolio_caller_exception('invalidpostid', 'oublog');
     }
     if (!($this->cm = get_coursemodule_from_instance('oublog', $this->oublogid))) {
         throw new portfolio_caller_exception('invalidcoursemodule');
     }
     // Convert tag from id to name.
     if (!empty($this->tag)) {
         if ($tagrec = $DB->get_record('oublog_tags', array('id' => $this->tag), 'tag')) {
             $this->tag = $tagrec->tag;
         }
     }
     // Call early to cache group mode - stops debugging warning from oublog_get_posts later.
     $this->cm->activitygroupmode = oublog_get_activity_groupmode($this->cm, $COURSE);
     $context = context_module::instance($this->cm->id);
     $this->modcontext = $context;
     if ($this->canaudit == 1) {
         $this->canaudit = true;
     } else {
         $this->canaudit = false;
     }
     if (empty($this->oubloguserid)) {
         $this->oubloguserid = null;
     }
     if (empty($this->currentindividual) || $this->currentindividual == 0) {
         $this->currentindividual = -1;
     }
     list($this->posts, $recordcount) = oublog_get_posts($this->oublog, $context, $this->offset, $this->cm, $this->currentgroup, $this->currentindividual, $this->oubloguserid, $this->tag, $this->canaudit);
     $fs = get_file_storage();
     $this->multifiles = array();
     foreach ($this->posts as $post) {
         $files = array();
         $attach = $fs->get_area_files($this->modcontext->id, 'mod_oublog', 'attachment', $post->id);
         $embed = $fs->get_area_files($this->modcontext->id, 'mod_oublog', 'message', $post->id);
         if (!empty($post->comments)) {
             foreach ($post->comments as $commentpost) {
                 $embedcomments = $fs->get_area_files($this->modcontext->id, 'mod_oublog', 'messagecomment', $commentpost->id);
                 $files = array_merge($files, $embedcomments);
             }
         }
         $files = array_merge($files, $attach, $embed);
         if ($files) {
             $this->keyedfiles[$post->id] = $files;
         } else {
             continue;
         }
         $this->multifiles = array_merge($this->multifiles, $files);
     }
     $this->set_file_and_format_data($this->multifiles);
     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->multifiles)) {
         $this->add_format(PORTFOLIO_FORMAT_RICHHTML);
     } else {
         $this->add_format(PORTFOLIO_FORMAT_PLAINHTML);
     }
 }
Example #6
0
    // If separate individual and visible group, do not show groupselector
    // unless the current user has permission.
    if ($oublog->individual == OUBLOG_SEPARATE_INDIVIDUAL_BLOGS && !has_capability('mod/oublog:viewindividual', $context)) {
        $showgroupselector = false;
    }
    $canpost = true;
    $individualdetails = oublog_individual_get_activity_details($cm, $returnurl, $oublog, $currentgroup, $context);
    if ($individualdetails) {
        $currentindividual = $individualdetails->activeindividual;
        if (!$individualdetails->newblogpost) {
            $canpost = false;
        }
    }
}
// Get Posts.
list($posts, $recordcount) = oublog_get_posts($oublog, $context, $offset, $cm, $currentgroup, $currentindividual, $oubloguser->id, $tag, $canaudit);
$hideunusedblog = !$posts && !$canpost && !$canaudit;
if ($oublog->global && !$hideunusedblog) {
    // Bit about hidden with if global then $posts
    // In order to prevent people from looping through numbers to get the
    // name of every user in the site (in case these names are considered
    // private), don't display the header when not displaying posts, except
    // to users who can post.
    oublog_build_navigation($oublog, $oubloginstance, $oubloguser);
} else {
    oublog_build_navigation($oublog, $oubloginstance, null);
}
if (!$hideunusedblog) {
    // Generate extra navigation.
    $CFG->additionalhtmlhead .= oublog_get_meta_tags($oublog, $oubloginstance, $currentgroup, $cm);
    $PAGE->set_button($buttontext);
Example #7
0
/**
 * Return grade for given user or all users.
 *
 * @global object
 * @param object $dataplus
 * @param int $userid optional user id, 0 means all users
 * @return array array of grades, false if none
 */
function oublog_get_user_grades($oublog, $userid = 0)
{
    global $CFG, $DB;
    require_once $CFG->dirroot . '/rating/lib.php';
    require_once $CFG->dirroot . '/mod/oublog/locallib.php';
    $options = new stdClass();
    $options->component = 'mod_oublog';
    $options->ratingarea = 'post';
    $options->modulename = 'oublog';
    $options->moduleid = $oublog->id;
    $options->userid = $userid;
    $options->aggregationmethod = $oublog->assessed;
    $options->scaleid = $oublog->scale;
    $options->cmid = $oublog->cmidnumber;
    // There now follows a lift of get_user_grades() from rating lib
    // but with the requirement for items modified.
    $rm = new rating_manager();
    if (!isset($options->component)) {
        throw new coding_exception('The component option is now a required option when getting user grades from ratings.');
    }
    if (!isset($options->ratingarea)) {
        throw new coding_exception('The ratingarea option is now a required option when getting user grades from ratings.');
    }
    // Going direct to the db for the context id seemed wrong.
    $context = context_module::instance($options->cmid);
    $params = array();
    $params['contextid'] = $context->id;
    $params['component'] = $options->component;
    $params['ratingarea'] = $options->ratingarea;
    $scaleid = $options->scaleid;
    $aggregationstring = $rm->get_aggregation_method($options->aggregationmethod);
    // If userid is not 0 we only want the grade for a single user.
    $singleuserwhere = '';
    if ($options->userid != 0) {
        // Get the grades for the {posts} the user is responsible for.
        $cm = get_coursemodule_from_id('oublog', $oublog->cmidnumber);
        list($posts, $recordcount) = oublog_get_posts($oublog, $context, 0, $cm, 0, $options->userid);
        foreach ($posts as $post) {
            $postids[] = (int) $post->id;
        }
        $params['userid'] = $userid;
        $singleuserwhere = " AND i.userid = :userid";
    }
    $sql = "SELECT u.id as id, u.id AS userid, {$aggregationstring}(r.rating) AS rawgrade\n              FROM {oublog} o\n              JOIN {oublog_instances} i ON i.oublogid = o.id\n              JOIN {oublog_posts} p ON p.oubloginstancesid = i.id\n              JOIN {rating} r ON r.itemid = p.id\n              JOIN {user} u ON i.userid = u.id\n             WHERE r.contextid = :contextid\n                   AND r.component = :component\n                   AND r.ratingarea = :ratingarea\n                   {$singleuserwhere}\n          GROUP BY u.id";
    $results = $DB->get_records_sql($sql, $params);
    if ($results) {
        $scale = null;
        $max = 0;
        if ($options->scaleid >= 0) {
            // Numeric.
            $max = $options->scaleid;
        } else {
            // Custom scales.
            $scale = $DB->get_record('scale', array('id' => -$options->scaleid));
            if ($scale) {
                $scale = explode(',', $scale->scale);
                $max = count($scale);
            } else {
                debugging('rating_manager::get_user_grades() received a scale ID that doesnt exist');
            }
        }
        // It could throw off the grading if count and sum returned a rawgrade higher than scale
        // so to prevent it we review the results and ensure that rawgrade does not exceed
        // the scale, if it does we set rawgrade = scale (i.e. full credit).
        foreach ($results as $rid => $result) {
            if ($options->scaleid >= 0) {
                // Numeric.
                if ($result->rawgrade > $options->scaleid) {
                    $results[$rid]->rawgrade = $options->scaleid;
                }
            } else {
                // Scales.
                if (!empty($scale) && $result->rawgrade > $max) {
                    $results[$rid]->rawgrade = $max;
                }
            }
        }
    }
    return $results;
}