/**
 * Get post in a format compatable with RSS lib
 *
 * @param int $blogid
 * @param int $bloginstancesid
 * @param object $user
 * @param bool $allowedvisibility
 * @param int $groupid
 * @param object $cm
 * @return array
 */
function oublog_get_feed_posts($blogid, $bloginstance, $user, $allowedvisibility, $groupid, $cm, $oublog, $individualid = -1)
{
    global $CFG, $DB;
    $params = array();
    $items = array();
    if ($bloginstance) {
        $sqlwhere = "AND p.oubloginstancesid = ? ";
        $params[] = $bloginstance->id;
    } else {
        $sqlwhere = "AND i.oublogid = ? ";
        $params[] = $blogid;
    }
    // If individual blog.
    if ($individualid > 0 || $oublog->individual > OUBLOG_NO_INDIVIDUAL_BLOGS) {
        $capable = oublog_individual_has_permissions($cm, $oublog, $groupid, $individualid, $user->id);
        oublog_individual_add_to_sqlwhere($sqlwhere, $params, 'i.userid', $oublog->id, $groupid, $individualid, $capable);
    } else {
        // No individual blog.
        if ($groupid) {
            $sqlwhere .= " AND p.groupid = ? ";
            $params[] = $groupid;
        } else {
            if (!empty($cm->groupingid)) {
                if ($groups = $DB->get_records('groupings_groups', array('groupingid' => $cm->groupingid), null, 'groupid')) {
                    $sqlwhere .= "AND p.groupid IN (" . implode(',', array_keys($groups)) . ") ";
                }
            }
        }
    }
    // Scheme URL for tags
    $scheme = $CFG->wwwroot . '/mod/oublog/';
    if ($oublog->global) {
        if (!$bloginstance) {
            $scheme .= 'allposts.php?tag=';
        } else {
            $scheme .= 'view.php?user='******'&tag=';
        }
    } else {
        $scheme .= 'view.php?id=' . $cm->id;
        if ($groupid) {
            $scheme .= '&group=' . $groupid;
        }
        $scheme .= '&tag=';
    }
    $usernamefields = get_all_user_name_fields(true, 'u');
    // Get posts
    $sql = "SELECT p.id, p.title, p.message AS description, p.timeposted AS pubdate, i.userid, {$usernamefields}, u.email, u.picture, u.imagealt, u.idnumber\n            FROM {oublog_posts} p\n            INNER JOIN {oublog_instances} i ON p.oubloginstancesid = i.id\n            INNER JOIN {user} u ON i.userid = u.id\n            WHERE p.deletedby IS NULL AND p.visibility >= {$allowedvisibility} {$sqlwhere}\n            ORDER BY p.timeposted DESC ";
    $rs = $DB->get_recordset_sql($sql, $params, 0, OUBLOG_MAX_FEED_ITEMS);
    $modcontext = context_module::instance($cm->id);
    foreach ($rs as $item) {
        $item->link = $CFG->wwwroot . '/mod/oublog/viewpost.php?post=' . $item->id;
        $item->author = fullname($item);
        $item->tags = array();
        $item->tagscheme = $scheme;
        // Feeds do not allow blank titles
        if ((string) $item->title === '') {
            $item->title = html_to_text(shorten_text($item->description));
        }
        // Rewrite image urls in oublog posts.
        $item->description = file_rewrite_pluginfile_urls($item->description, 'mod/oublog/pluginfile.php', $modcontext->id, 'mod_oublog', 'message', $item->id);
        $items[$item->id] = $item;
    }
    $rs->close();
    // Get all tags related to these posts and fill them in
    $sql = "SELECT p.id AS postid, t.id AS tagid, t.tag\n            FROM {oublog_posts} p\n            INNER JOIN {oublog_instances} i ON p.oubloginstancesid = i.id\n            INNER JOIN {oublog_taginstances} ti ON p.id = ti.postid\n            INNER JOIN {oublog_tags} t ON ti.tagid = t.id\n            WHERE p.deletedby IS NULL AND p.visibility >= {$allowedvisibility} {$sqlwhere}";
    $rs = $DB->get_recordset_sql($sql, $params);
    foreach ($rs as $tag) {
        if (array_key_exists($tag->postid, $items)) {
            $items[$tag->postid]->tags[$tag->tagid] = $tag->tag;
        }
    }
    $rs->close();
    return $items;
}
/**
 * Get post in a format compatable with RSS lib
 *
 * @param int $blogid
 * @param int $bloginstancesid
 * @param object $user
 * @param bool $allowedvisibility
 * @param int $groupid
 * @param object $cm
 * @return array
 */
function oublog_get_feed_posts($blogid, $bloginstance, $user, $allowedvisibility, $groupid, $cm, $oublog, $individualid = -1)
{
    global $CFG;
    $items = array();
    if ($bloginstance) {
        $sqlwhere = "AND p.oubloginstancesid = {$bloginstance->id} ";
    } else {
        $sqlwhere = "AND i.oublogid = {$blogid} ";
    }
    //if individual blog
    if ($individualid > -1) {
        $capable = oublog_individual_has_permissions($cm, $oublog, $groupid, $individualid);
        oublog_individual_add_to_sqlwhere(&$sqlwhere, 'i.userid', $oublog->id, $groupid, $individualid, $capable);
    } else {
        if ($groupid) {
            $sqlwhere .= " AND p.groupid = {$groupid} ";
        }
        if (!empty($CFG->enablegroupings) && !empty($cm->groupingid)) {
            if ($groups = get_records('groupings_groups', 'groupingid', $cm->groupingid, null, 'groupid')) {
                $sqlwhere .= "AND p.groupid IN (" . implode(',', array_keys($groups)) . ") ";
            }
        }
    }
    // Scheme URL for tags
    $scheme = $CFG->wwwroot . '/mod/oublog/';
    if ($oublog->global) {
        if (!$bloginstance) {
            $scheme .= 'allposts.php?tag=';
        } else {
            $scheme .= 'view.php?user='******'&tag=';
        }
    } else {
        $scheme .= 'view.php?id=' . $cm->id;
        if ($groupid) {
            $scheme .= '&group=' . $groupid;
        }
        $scheme .= '&tag=';
    }
    // Get posts
    $sql = "SELECT p.id, p.title, p.message AS description, p.timeposted AS pubdate, i.userid, u.firstname, u.lastname, u.email, u.picture, u.imagealt, u.idnumber\n            FROM {$CFG->prefix}oublog_posts p\n            INNER JOIN {$CFG->prefix}oublog_instances i ON p.oubloginstancesid = i.id\n            INNER JOIN {$CFG->prefix}user u ON i.userid = u.id\n            WHERE p.deletedby IS NULL AND p.visibility >= {$allowedvisibility} {$sqlwhere}\n            ORDER BY p.timeposted DESC ";
    $rs = get_recordset_sql($sql, 0, OUBLOG_MAX_FEED_ITEMS);
    while ($item = rs_fetch_next_record($rs)) {
        $item->link = $CFG->wwwroot . '/mod/oublog/viewpost.php?post=' . $item->id;
        $item->author = fullname($item);
        $item->tags = array();
        $item->tagscheme = $scheme;
        // Feeds do not allow blank titles
        if ((string) $item->title === '') {
            $item->title = html_to_text(shorten_text($item->description));
        }
        $items[$item->id] = $item;
    }
    rs_close($rs);
    // Get all tags related to these posts and fill them in
    $sql = "SELECT p.id AS postid, t.id AS tagid, t.tag\n            FROM {$CFG->prefix}oublog_posts p\n            INNER JOIN {$CFG->prefix}oublog_instances i ON p.oubloginstancesid = i.id\n            INNER JOIN {$CFG->prefix}oublog_taginstances ti ON p.id = ti.postid\n            INNER JOIN {$CFG->prefix}oublog_tags t ON ti.tagid = t.id\n            WHERE p.deletedby IS NULL AND p.visibility >= {$allowedvisibility} {$sqlwhere}";
    $rs = get_recordset_sql($sql);
    while ($tag = rs_fetch_next_record($rs)) {
        if (array_key_exists($tag->postid, $items)) {
            $items[$tag->postid]->tags[$tag->tagid] = $tag->tag;
        }
    }
    rs_close($rs);
    return $items;
}