2009-09-04 00:36:43 +00:00
|
|
|
<?php
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Core global functions for Blog.
|
|
|
|
*
|
|
|
|
* @package moodlecore
|
|
|
|
* @subpackage blog
|
|
|
|
* @copyright 2009 Nicolas Connault
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2010-07-30 20:51:01 +00:00
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
/*
|
2009-09-04 00:36:43 +00:00
|
|
|
* Library of functions and constants for blog
|
|
|
|
*/
|
|
|
|
require_once($CFG->dirroot .'/blog/rsslib.php');
|
|
|
|
require_once($CFG->dirroot.'/tag/lib.php');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* User can edit a blog entry if this is their own blog entry and they have
|
|
|
|
* the capability moodle/blog:create, or if they have the capability
|
|
|
|
* moodle/blog:manageentries.
|
|
|
|
*
|
|
|
|
* This also applies to deleting of entries.
|
|
|
|
*/
|
2009-10-30 07:25:50 +00:00
|
|
|
function blog_user_can_edit_entry($blogentry) {
|
|
|
|
global $USER;
|
2006-04-12 08:58:49 +00:00
|
|
|
|
2012-07-24 09:52:41 +08:00
|
|
|
$sitecontext = context_system::instance();
|
2006-03-10 06:53:01 +00:00
|
|
|
|
2009-09-04 00:36:43 +00:00
|
|
|
if (has_capability('moodle/blog:manageentries', $sitecontext)) {
|
2014-07-13 18:43:09 +08:00
|
|
|
return true; // Can edit any blog entry.
|
2009-09-04 00:36:43 +00:00
|
|
|
}
|
2006-04-12 02:05:46 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
if ($blogentry->userid == $USER->id && has_capability('moodle/blog:create', $sitecontext)) {
|
2014-07-13 18:43:09 +08:00
|
|
|
return true; // Can edit own when having blog:create capability.
|
2009-09-04 00:36:43 +00:00
|
|
|
}
|
2006-03-10 06:53:01 +00:00
|
|
|
|
2009-09-04 00:36:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-08-23 14:58:15 +00:00
|
|
|
|
2009-08-07 01:41:27 +00:00
|
|
|
|
2009-09-04 00:36:43 +00:00
|
|
|
/**
|
|
|
|
* Checks to see if a user can view the blogs of another user.
|
|
|
|
* Only blog level is checked here, the capabilities are enforced
|
|
|
|
* in blog/index.php
|
|
|
|
*/
|
2009-10-30 07:25:50 +00:00
|
|
|
function blog_user_can_view_user_entry($targetuserid, $blogentry=null) {
|
2009-09-04 00:36:43 +00:00
|
|
|
global $CFG, $USER, $DB;
|
2006-04-12 02:05:46 +00:00
|
|
|
|
2012-07-19 11:30:05 +08:00
|
|
|
if (empty($CFG->enableblogs)) {
|
2013-05-07 12:02:26 +08:00
|
|
|
return false; // Blog system disabled.
|
2009-09-04 00:36:43 +00:00
|
|
|
}
|
|
|
|
|
2010-09-17 11:21:36 +00:00
|
|
|
if (isloggedin() && $USER->id == $targetuserid) {
|
2013-05-07 12:02:26 +08:00
|
|
|
return true; // Can view own entries in any case.
|
2009-09-04 00:36:43 +00:00
|
|
|
}
|
|
|
|
|
2012-07-24 09:52:41 +08:00
|
|
|
$sitecontext = context_system::instance();
|
2009-09-04 00:36:43 +00:00
|
|
|
if (has_capability('moodle/blog:manageentries', $sitecontext)) {
|
2013-05-07 12:02:26 +08:00
|
|
|
return true; // Can manage all entries.
|
2009-09-04 00:36:43 +00:00
|
|
|
}
|
2006-04-12 02:05:46 +00:00
|
|
|
|
2013-05-07 12:02:26 +08:00
|
|
|
// If blog is in draft state, then make sure user have proper capability.
|
2009-10-30 07:25:50 +00:00
|
|
|
if ($blogentry && $blogentry->publishstate == 'draft' && !has_capability('moodle/blog:viewdrafts', $sitecontext)) {
|
2013-05-07 12:02:26 +08:00
|
|
|
return false; // Can not view draft of others.
|
2009-09-04 00:36:43 +00:00
|
|
|
}
|
|
|
|
|
2013-03-08 09:37:21 +08:00
|
|
|
// If blog entry is not public, make sure user is logged in.
|
2009-10-30 07:25:50 +00:00
|
|
|
if ($blogentry && $blogentry->publishstate != 'public' && !isloggedin()) {
|
2009-09-04 00:36:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-08-23 14:58:15 +00:00
|
|
|
|
2013-05-07 12:02:26 +08:00
|
|
|
// If blogentry is not passed or all above checks pass, then check capability based on system config.
|
2009-09-04 00:36:43 +00:00
|
|
|
switch ($CFG->bloglevel) {
|
|
|
|
case BLOG_GLOBAL_LEVEL:
|
|
|
|
return true;
|
|
|
|
break;
|
2006-04-12 02:05:46 +00:00
|
|
|
|
2009-09-04 00:36:43 +00:00
|
|
|
case BLOG_SITE_LEVEL:
|
2013-05-07 12:02:26 +08:00
|
|
|
if (isloggedin()) { // Not logged in viewers forbidden.
|
2009-09-04 00:36:43 +00:00
|
|
|
return true;
|
2006-04-12 02:05:46 +00:00
|
|
|
}
|
2009-09-04 00:36:43 +00:00
|
|
|
return false;
|
|
|
|
break;
|
2007-08-23 14:58:15 +00:00
|
|
|
|
2009-09-04 00:36:43 +00:00
|
|
|
case BLOG_USER_LEVEL:
|
|
|
|
default:
|
2013-05-07 12:02:26 +08:00
|
|
|
// If user is viewing other user blog, then user should have user:readuserblogs capability.
|
2012-07-24 09:52:41 +08:00
|
|
|
$personalcontext = context_user::instance($targetuserid);
|
2009-09-04 00:36:43 +00:00
|
|
|
return has_capability('moodle/user:readuserblogs', $personalcontext);
|
|
|
|
break;
|
2006-03-10 06:53:01 +00:00
|
|
|
|
2009-09-04 00:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remove all associations for the blog entries of a particular user
|
|
|
|
* @param int userid - id of user whose blog associations will be deleted
|
|
|
|
*/
|
|
|
|
function blog_remove_associations_for_user($userid) {
|
2009-10-30 07:25:50 +00:00
|
|
|
global $DB;
|
2010-09-17 11:22:51 +00:00
|
|
|
throw new coding_exception('function blog_remove_associations_for_user() is not finished');
|
|
|
|
/*
|
2009-10-30 07:25:50 +00:00
|
|
|
$blogentries = blog_fetch_entries(array('user' => $userid), 'lasmodified DESC');
|
|
|
|
foreach ($blogentries as $entry) {
|
|
|
|
if (blog_user_can_edit_entry($entry)) {
|
|
|
|
blog_remove_associations_for_entry($entry->id);
|
|
|
|
}
|
2009-09-09 03:26:18 +00:00
|
|
|
}
|
2010-09-17 11:22:51 +00:00
|
|
|
*/
|
2009-09-04 00:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-10-30 07:25:50 +00:00
|
|
|
* remove all associations for the blog entries of a particular course
|
|
|
|
* @param int courseid - id of user whose blog associations will be deleted
|
2009-09-04 00:36:43 +00:00
|
|
|
*/
|
2009-10-30 07:25:50 +00:00
|
|
|
function blog_remove_associations_for_course($courseid) {
|
|
|
|
global $DB;
|
2012-07-24 09:52:41 +08:00
|
|
|
$context = context_course::instance($courseid);
|
2009-10-30 07:25:50 +00:00
|
|
|
$DB->delete_records('blog_association', array('contextid' => $context->id));
|
2009-09-04 00:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a record in the {blog_external} table, checks the blog's URL
|
2010-12-27 15:56:58 +01:00
|
|
|
* for new entries not yet copied into Moodle.
|
2010-12-20 14:58:02 +08:00
|
|
|
* Also attempts to identify and remove deleted blog entries
|
2009-09-04 00:36:43 +00:00
|
|
|
*
|
2009-10-30 07:25:50 +00:00
|
|
|
* @param object $externalblog
|
|
|
|
* @return boolean False if the Feed is invalid
|
2009-09-04 00:36:43 +00:00
|
|
|
*/
|
2009-10-30 07:25:50 +00:00
|
|
|
function blog_sync_external_entries($externalblog) {
|
2009-09-04 00:36:43 +00:00
|
|
|
global $CFG, $DB;
|
2009-09-04 07:09:45 +00:00
|
|
|
require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
|
2006-03-10 06:53:01 +00:00
|
|
|
|
2013-04-06 19:57:28 +02:00
|
|
|
$rss = new moodle_simplepie();
|
|
|
|
$rssfile = $rss->registry->create('File', array($externalblog->url));
|
|
|
|
$filetest = $rss->registry->create('Locator', array($rssfile));
|
2009-10-30 07:25:50 +00:00
|
|
|
|
|
|
|
if (!$filetest->is_feed($rssfile)) {
|
|
|
|
$externalblog->failedlastsync = 1;
|
|
|
|
$DB->update_record('blog_external', $externalblog);
|
|
|
|
return false;
|
2010-06-29 03:19:22 +00:00
|
|
|
} else if (!empty($externalblog->failedlastsync)) {
|
2009-10-30 07:25:50 +00:00
|
|
|
$externalblog->failedlastsync = 0;
|
|
|
|
$DB->update_record('blog_external', $externalblog);
|
2006-04-12 02:05:46 +00:00
|
|
|
}
|
2006-03-10 06:53:01 +00:00
|
|
|
|
2013-04-06 19:57:28 +02:00
|
|
|
$rss->set_feed_url($externalblog->url);
|
|
|
|
$rss->init();
|
2006-04-12 02:05:46 +00:00
|
|
|
|
2009-09-04 07:09:45 +00:00
|
|
|
if (empty($rss->data)) {
|
2009-09-04 00:36:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
2014-07-13 18:43:09 +08:00
|
|
|
// Used to identify blog posts that have been deleted from the source feed.
|
2010-12-20 11:16:11 +08:00
|
|
|
$oldesttimestamp = null;
|
2010-12-20 14:58:02 +08:00
|
|
|
$uniquehashes = array();
|
2006-05-01 05:33:47 +00:00
|
|
|
|
2009-09-04 07:09:45 +00:00
|
|
|
foreach ($rss->get_items() as $entry) {
|
2014-07-13 18:43:09 +08:00
|
|
|
// If filtertags are defined, use them to filter the entries by RSS category.
|
2009-10-30 07:25:50 +00:00
|
|
|
if (!empty($externalblog->filtertags)) {
|
|
|
|
$containsfiltertag = false;
|
|
|
|
$categories = $entry->get_categories();
|
|
|
|
$filtertags = explode(',', $externalblog->filtertags);
|
|
|
|
$filtertags = array_map('trim', $filtertags);
|
|
|
|
$filtertags = array_map('strtolower', $filtertags);
|
|
|
|
|
2014-07-23 14:54:10 +08:00
|
|
|
if (!empty($categories)) {
|
|
|
|
foreach ($categories as $category) {
|
|
|
|
if (in_array(trim(strtolower($category->term)), $filtertags)) {
|
|
|
|
$containsfiltertag = true;
|
|
|
|
}
|
2009-10-30 07:25:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$containsfiltertag) {
|
|
|
|
continue;
|
2006-04-12 02:05:46 +00:00
|
|
|
}
|
|
|
|
}
|
2010-12-22 13:27:46 +08:00
|
|
|
|
2010-12-20 14:58:02 +08:00
|
|
|
$uniquehashes[] = $entry->get_permalink();
|
2009-10-30 07:25:50 +00:00
|
|
|
|
2010-09-21 08:11:06 +00:00
|
|
|
$newentry = new stdClass();
|
2009-10-30 07:25:50 +00:00
|
|
|
$newentry->userid = $externalblog->userid;
|
|
|
|
$newentry->module = 'blog_external';
|
|
|
|
$newentry->content = $externalblog->id;
|
|
|
|
$newentry->uniquehash = $entry->get_permalink();
|
|
|
|
$newentry->publishstate = 'site';
|
|
|
|
$newentry->format = FORMAT_HTML;
|
2014-07-13 18:43:09 +08:00
|
|
|
// Clean subject of html, just in case.
|
2011-02-13 23:03:55 +01:00
|
|
|
$newentry->subject = clean_param($entry->get_title(), PARAM_TEXT);
|
2014-07-13 18:43:09 +08:00
|
|
|
// Observe 128 max chars in DB.
|
|
|
|
// TODO: +1 to raise this to 255.
|
2013-08-06 20:58:28 +02:00
|
|
|
if (core_text::strlen($newentry->subject) > 128) {
|
|
|
|
$newentry->subject = core_text::substr($newentry->subject, 0, 125) . '...';
|
2011-02-13 23:03:55 +01:00
|
|
|
}
|
2009-10-30 07:25:50 +00:00
|
|
|
$newentry->summary = $entry->get_description();
|
2010-12-22 13:27:46 +08:00
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// Used to decide whether to insert or update.
|
|
|
|
// Uses enty permalink plus creation date if available.
|
2010-12-22 13:24:55 +08:00
|
|
|
$existingpostconditions = array('uniquehash' => $entry->get_permalink());
|
2010-11-15 03:56:12 +00:00
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// Our DB doesnt allow null creation or modified timestamps so check the external blog supplied one.
|
2010-11-15 03:56:12 +00:00
|
|
|
$entrydate = $entry->get_date('U');
|
2010-12-17 09:46:46 +08:00
|
|
|
if (!empty($entrydate)) {
|
|
|
|
$existingpostconditions['created'] = $entrydate;
|
|
|
|
}
|
2010-12-22 13:27:46 +08:00
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// The post ID or false if post not found in DB.
|
2010-12-17 09:46:46 +08:00
|
|
|
$postid = $DB->get_field('post', 'id', $existingpostconditions);
|
2010-12-22 13:27:46 +08:00
|
|
|
|
2010-12-17 09:46:46 +08:00
|
|
|
$timestamp = null;
|
2010-11-15 03:56:12 +00:00
|
|
|
if (empty($entrydate)) {
|
2010-12-17 09:46:46 +08:00
|
|
|
$timestamp = time();
|
2010-11-15 03:56:12 +00:00
|
|
|
} else {
|
2010-12-17 09:46:46 +08:00
|
|
|
$timestamp = $entrydate;
|
2010-11-15 03:56:12 +00:00
|
|
|
}
|
2010-12-22 13:27:46 +08:00
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// Only set created if its a new post so we retain the original creation timestamp if the post is edited.
|
2010-12-22 13:24:55 +08:00
|
|
|
if ($postid === false) {
|
2010-12-17 09:46:46 +08:00
|
|
|
$newentry->created = $timestamp;
|
|
|
|
}
|
|
|
|
$newentry->lastmodified = $timestamp;
|
2010-12-22 13:29:01 +08:00
|
|
|
|
2010-12-22 13:24:55 +08:00
|
|
|
if (empty($oldesttimestamp) || $timestamp < $oldesttimestamp) {
|
2014-07-13 18:43:09 +08:00
|
|
|
// Found an older post.
|
2010-12-20 11:16:11 +08:00
|
|
|
$oldesttimestamp = $timestamp;
|
|
|
|
}
|
2009-10-30 07:25:50 +00:00
|
|
|
|
2013-08-06 20:58:28 +02:00
|
|
|
if (core_text::strlen($newentry->uniquehash) > 255) {
|
2010-10-27 05:25:00 +00:00
|
|
|
// The URL for this item is too long for the field. Rather than add
|
|
|
|
// the entry without the link we will skip straight over it.
|
|
|
|
// RSS spec says recommended length 500, we use 255.
|
|
|
|
debugging('External blog entry skipped because of oversized URL', DEBUG_DEVELOPER);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-12-22 13:24:55 +08:00
|
|
|
if ($postid === false) {
|
2010-12-16 16:21:41 +08:00
|
|
|
$id = $DB->insert_record('post', $newentry);
|
2009-10-30 07:25:50 +00:00
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// Set tags.
|
2010-12-16 16:21:41 +08:00
|
|
|
if ($tags = tag_get_tags_array('blog_external', $externalblog->id)) {
|
2014-01-12 18:41:18 -08:00
|
|
|
tag_set('post', $id, $tags, 'core', context_user::instance($externalblog->userid)->id);
|
2010-12-16 16:21:41 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$newentry->id = $postid;
|
2010-12-17 09:46:46 +08:00
|
|
|
$DB->update_record('post', $newentry);
|
2009-10-30 07:25:50 +00:00
|
|
|
}
|
2009-09-04 00:36:43 +00:00
|
|
|
}
|
2010-12-22 13:27:46 +08:00
|
|
|
|
2011-02-18 17:33:37 +01:00
|
|
|
// Look at the posts we have in the database to check if any of them have been deleted from the feed.
|
|
|
|
// Only checking posts within the time frame returned by the rss feed. Older items may have been deleted or
|
|
|
|
// may just not be returned anymore. We can't tell the difference so we leave older posts alone.
|
|
|
|
$sql = "SELECT id, uniquehash
|
|
|
|
FROM {post}
|
|
|
|
WHERE module = 'blog_external'
|
|
|
|
AND " . $DB->sql_compare_text('content') . " = " . $DB->sql_compare_text(':blogid') . "
|
|
|
|
AND created > :ts";
|
2011-02-02 15:02:48 +08:00
|
|
|
$dbposts = $DB->get_records_sql($sql, array('blogid' => $externalblog->id, 'ts' => $oldesttimestamp));
|
|
|
|
|
2010-12-20 14:58:02 +08:00
|
|
|
$todelete = array();
|
2014-07-13 18:43:09 +08:00
|
|
|
foreach ($dbposts as $dbpost) {
|
2010-12-24 09:31:58 +08:00
|
|
|
if ( !in_array($dbpost->uniquehash, $uniquehashes) ) {
|
2010-12-20 14:58:02 +08:00
|
|
|
$todelete[] = $dbpost->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$DB->delete_records_list('post', 'id', $todelete);
|
2006-04-12 02:05:46 +00:00
|
|
|
|
2010-12-27 16:20:39 +01:00
|
|
|
$DB->update_record('blog_external', array('id' => $externalblog->id, 'timefetched' => time()));
|
2009-10-30 07:25:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an external blog object, deletes all related blog entries from the post table.
|
|
|
|
* NOTE: The external blog's id is saved as post.content, a field that is not oterhwise used by blog entries.
|
|
|
|
* @param object $externablog
|
|
|
|
*/
|
|
|
|
function blog_delete_external_entries($externalblog) {
|
|
|
|
global $DB;
|
2012-07-24 09:52:41 +08:00
|
|
|
require_capability('moodle/blog:manageexternal', context_system::instance());
|
2010-11-19 21:57:15 +00:00
|
|
|
$DB->delete_records_select('post',
|
|
|
|
"module='blog_external' AND " . $DB->sql_compare_text('content') . " = ?",
|
|
|
|
array($externalblog->id));
|
2009-09-04 00:36:43 +00:00
|
|
|
}
|
2007-08-23 14:58:15 +00:00
|
|
|
|
2010-04-23 04:05:16 +00:00
|
|
|
/**
|
2010-05-01 14:33:07 +00:00
|
|
|
* This function checks that blogs are enabled, and that the user can see blogs at all
|
2010-04-23 04:05:16 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function blog_is_enabled_for_user() {
|
|
|
|
global $CFG;
|
2012-07-19 11:30:05 +08:00
|
|
|
return (!empty($CFG->enableblogs) && (isloggedin() || ($CFG->bloglevel == BLOG_GLOBAL_LEVEL)));
|
2010-04-23 04:05:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function gets all of the options available for the current user in respect
|
|
|
|
* to blogs.
|
2010-07-15 18:09:11 +00:00
|
|
|
*
|
2010-04-23 04:05:16 +00:00
|
|
|
* It loads the following if applicable:
|
|
|
|
* - Module options {@see blog_get_options_for_module}
|
|
|
|
* - Course options {@see blog_get_options_for_course}
|
|
|
|
* - User specific options {@see blog_get_options_for_user}
|
|
|
|
* - General options (BLOG_LEVEL_GLOBAL)
|
|
|
|
*
|
|
|
|
* @param moodle_page $page The page to load for (normally $PAGE)
|
|
|
|
* @param stdClass $userid Load for a specific user
|
|
|
|
* @return array An array of options organised by type.
|
|
|
|
*/
|
|
|
|
function blog_get_all_options(moodle_page $page, stdClass $userid = null) {
|
|
|
|
global $CFG, $DB, $USER;
|
|
|
|
|
|
|
|
$options = array();
|
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// If blogs are enabled and the user is logged in and not a guest.
|
2010-04-23 04:05:16 +00:00
|
|
|
if (blog_is_enabled_for_user()) {
|
2014-07-13 18:43:09 +08:00
|
|
|
// If the context is the user then assume we want to load for the users context.
|
2010-04-23 04:05:16 +00:00
|
|
|
if (is_null($userid) && $page->context->contextlevel == CONTEXT_USER) {
|
|
|
|
$userid = $page->context->instanceid;
|
|
|
|
}
|
2014-07-13 18:43:09 +08:00
|
|
|
// Check the userid var.
|
2010-04-23 04:05:16 +00:00
|
|
|
if (!is_null($userid) && $userid!==$USER->id) {
|
|
|
|
// Load the user from the userid... it MUST EXIST throw a wobbly if it doesn't!
|
|
|
|
$user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
|
|
|
|
} else {
|
|
|
|
$user = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($CFG->useblogassociations && $page->cm !== null) {
|
2014-07-13 18:43:09 +08:00
|
|
|
// Load for the module associated with the page.
|
2010-04-23 04:05:16 +00:00
|
|
|
$options[CONTEXT_MODULE] = blog_get_options_for_module($page->cm, $user);
|
|
|
|
} else if ($CFG->useblogassociations && $page->course->id != SITEID) {
|
2014-07-13 18:43:09 +08:00
|
|
|
// Load the options for the course associated with the page.
|
2010-04-23 04:05:16 +00:00
|
|
|
$options[CONTEXT_COURSE] = blog_get_options_for_course($page->course, $user);
|
|
|
|
}
|
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// Get the options for the user.
|
2011-03-20 12:34:41 +01:00
|
|
|
if ($user !== null and !isguestuser($user)) {
|
2014-07-13 18:43:09 +08:00
|
|
|
// Load for the requested user.
|
2010-04-23 04:05:16 +00:00
|
|
|
$options[CONTEXT_USER+1] = blog_get_options_for_user($user);
|
|
|
|
}
|
2014-07-13 18:43:09 +08:00
|
|
|
// Load for the current user.
|
2011-03-20 12:34:41 +01:00
|
|
|
if (isloggedin() and !isguestuser()) {
|
|
|
|
$options[CONTEXT_USER] = blog_get_options_for_user();
|
|
|
|
}
|
2010-04-23 04:05:16 +00:00
|
|
|
}
|
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// If blog level is global then display a link to view all site entries.
|
|
|
|
if (!empty($CFG->enableblogs)
|
|
|
|
&& $CFG->bloglevel >= BLOG_GLOBAL_LEVEL
|
|
|
|
&& has_capability('moodle/blog:view', context_system::instance())) {
|
|
|
|
|
2010-04-23 04:05:16 +00:00
|
|
|
$options[CONTEXT_SYSTEM] = array('viewsite' => array(
|
|
|
|
'string' => get_string('viewsiteentries', 'blog'),
|
|
|
|
'link' => new moodle_url('/blog/index.php')
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// Return the options.
|
2010-04-23 04:05:16 +00:00
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all of the blog options that relate to the passed user.
|
|
|
|
*
|
|
|
|
* If no user is passed the current user is assumed.
|
|
|
|
*
|
|
|
|
* @staticvar array $useroptions Cache so we don't have to regenerate multiple times
|
|
|
|
* @param stdClass $user
|
|
|
|
* @return array The array of options for the requested user
|
|
|
|
*/
|
|
|
|
function blog_get_options_for_user(stdClass $user=null) {
|
|
|
|
global $CFG, $USER;
|
2014-07-13 18:43:09 +08:00
|
|
|
// Cache.
|
2010-04-23 04:05:16 +00:00
|
|
|
static $useroptions = array();
|
|
|
|
|
|
|
|
$options = array();
|
2014-07-13 18:43:09 +08:00
|
|
|
// Blogs must be enabled and the user must be logged in.
|
2010-04-23 04:05:16 +00:00
|
|
|
if (!blog_is_enabled_for_user()) {
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// Sort out the user var.
|
2010-04-23 04:05:16 +00:00
|
|
|
if ($user === null || $user->id == $USER->id) {
|
|
|
|
$user = $USER;
|
|
|
|
$iscurrentuser = true;
|
|
|
|
} else {
|
|
|
|
$iscurrentuser = false;
|
|
|
|
}
|
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// If we've already generated serve from the cache.
|
2010-04-23 04:05:16 +00:00
|
|
|
if (array_key_exists($user->id, $useroptions)) {
|
|
|
|
return $useroptions[$user->id];
|
|
|
|
}
|
|
|
|
|
2012-07-24 09:52:41 +08:00
|
|
|
$sitecontext = context_system::instance();
|
2010-04-23 04:05:16 +00:00
|
|
|
$canview = has_capability('moodle/blog:view', $sitecontext);
|
|
|
|
|
|
|
|
if (!$iscurrentuser && $canview && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
|
2014-07-13 18:43:09 +08:00
|
|
|
// Not the current user, but we can view and its blogs are enabled for SITE or GLOBAL.
|
2010-04-23 04:05:16 +00:00
|
|
|
$options['userentries'] = array(
|
|
|
|
'string' => get_string('viewuserentries', 'blog', fullname($user)),
|
|
|
|
'link' => new moodle_url('/blog/index.php', array('userid'=>$user->id))
|
|
|
|
);
|
|
|
|
} else {
|
2014-07-13 18:43:09 +08:00
|
|
|
// It's the current user.
|
2010-04-23 04:05:16 +00:00
|
|
|
if ($canview) {
|
2014-07-13 18:43:09 +08:00
|
|
|
// We can view our own blogs .... BIG surprise.
|
2010-04-23 04:05:16 +00:00
|
|
|
$options['view'] = array(
|
|
|
|
'string' => get_string('viewallmyentries', 'blog'),
|
|
|
|
'link' => new moodle_url('/blog/index.php', array('userid'=>$USER->id))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (has_capability('moodle/blog:create', $sitecontext)) {
|
2014-07-13 18:43:09 +08:00
|
|
|
// We can add to our own blog.
|
2010-04-23 04:05:16 +00:00
|
|
|
$options['add'] = array(
|
|
|
|
'string' => get_string('addnewentry', 'blog'),
|
|
|
|
'link' => new moodle_url('/blog/edit.php', array('action'=>'add'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2011-12-01 12:03:31 +08:00
|
|
|
if ($canview && $CFG->enablerssfeeds) {
|
2011-11-30 11:22:13 +08:00
|
|
|
$options['rss'] = array(
|
|
|
|
'string' => get_string('rssfeed', 'blog'),
|
|
|
|
'link' => new moodle_url(rss_get_url($sitecontext->id, $USER->id, 'blog', 'user/'.$user->id))
|
2014-07-13 18:43:09 +08:00
|
|
|
);
|
2011-11-30 11:22:13 +08:00
|
|
|
}
|
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// Cache the options.
|
2010-04-23 04:05:16 +00:00
|
|
|
$useroptions[$user->id] = $options;
|
2014-07-13 18:43:09 +08:00
|
|
|
// Return the options.
|
2010-04-23 04:05:16 +00:00
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the blog options that relate to the given course for the given user.
|
|
|
|
*
|
|
|
|
* @staticvar array $courseoptions A cache so we can save regenerating multiple times
|
|
|
|
* @param stdClass $course The course to load options for
|
|
|
|
* @param stdClass $user The user to load options for null == current user
|
|
|
|
* @return array The array of options
|
|
|
|
*/
|
|
|
|
function blog_get_options_for_course(stdClass $course, stdClass $user=null) {
|
|
|
|
global $CFG, $USER;
|
2014-07-13 18:43:09 +08:00
|
|
|
// Cache.
|
2010-04-23 04:05:16 +00:00
|
|
|
static $courseoptions = array();
|
2010-07-15 18:09:11 +00:00
|
|
|
|
2010-04-23 04:05:16 +00:00
|
|
|
$options = array();
|
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// User must be logged in and blogs must be enabled.
|
2010-04-23 04:05:16 +00:00
|
|
|
if (!blog_is_enabled_for_user()) {
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// Check that the user can associate with the course.
|
2012-07-24 09:52:41 +08:00
|
|
|
$sitecontext = context_system::instance();
|
2014-07-13 18:43:09 +08:00
|
|
|
// Generate the cache key.
|
2010-04-23 04:05:16 +00:00
|
|
|
$key = $course->id.':';
|
|
|
|
if (!empty($user)) {
|
|
|
|
$key .= $user->id;
|
|
|
|
} else {
|
|
|
|
$key .= $USER->id;
|
|
|
|
}
|
2014-07-13 18:43:09 +08:00
|
|
|
// Serve from the cache if we've already generated for this course.
|
2010-04-23 04:05:16 +00:00
|
|
|
if (array_key_exists($key, $courseoptions)) {
|
2010-04-23 04:08:28 +00:00
|
|
|
return $courseoptions[$key];
|
2010-04-23 04:05:16 +00:00
|
|
|
}
|
2010-07-15 18:09:11 +00:00
|
|
|
|
2013-04-04 15:46:05 +08:00
|
|
|
if (has_capability('moodle/blog:view', $sitecontext)) {
|
2010-04-23 04:05:16 +00:00
|
|
|
// We can view!
|
|
|
|
if ($CFG->bloglevel >= BLOG_SITE_LEVEL) {
|
2014-07-13 18:43:09 +08:00
|
|
|
// View entries about this course.
|
2010-04-23 04:05:16 +00:00
|
|
|
$options['courseview'] = array(
|
|
|
|
'string' => get_string('viewcourseblogs', 'blog'),
|
2013-04-04 15:46:05 +08:00
|
|
|
'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id))
|
2010-04-23 04:05:16 +00:00
|
|
|
);
|
|
|
|
}
|
2014-07-13 18:43:09 +08:00
|
|
|
// View MY entries about this course.
|
2010-04-23 04:05:16 +00:00
|
|
|
$options['courseviewmine'] = array(
|
|
|
|
'string' => get_string('viewmyentriesaboutcourse', 'blog'),
|
2013-04-04 15:46:05 +08:00
|
|
|
'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id, 'userid' => $USER->id))
|
2010-04-23 04:05:16 +00:00
|
|
|
);
|
|
|
|
if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
|
2014-07-13 18:43:09 +08:00
|
|
|
// View the provided users entries about this course.
|
2010-04-23 04:05:16 +00:00
|
|
|
$options['courseviewuser'] = array(
|
|
|
|
'string' => get_string('viewentriesbyuseraboutcourse', 'blog', fullname($user)),
|
2013-04-04 15:46:05 +08:00
|
|
|
'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id, 'userid' => $user->id))
|
2010-04-23 04:05:16 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-04 15:46:05 +08:00
|
|
|
if (has_capability('moodle/blog:create', $sitecontext)) {
|
2014-07-13 18:43:09 +08:00
|
|
|
// We can blog about this course.
|
2010-04-23 04:05:16 +00:00
|
|
|
$options['courseadd'] = array(
|
2010-11-26 09:42:02 +00:00
|
|
|
'string' => get_string('blogaboutthiscourse', 'blog'),
|
2013-04-04 15:46:05 +08:00
|
|
|
'link' => new moodle_url('/blog/edit.php', array('action' => 'add', 'courseid' => $course->id))
|
2010-04-23 04:05:16 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// Cache the options for this course.
|
2010-04-23 04:05:16 +00:00
|
|
|
$courseoptions[$key] = $options;
|
2014-07-13 18:43:09 +08:00
|
|
|
// Return the options.
|
2010-04-23 04:05:16 +00:00
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the blog options relating to the given module for the given user
|
|
|
|
*
|
|
|
|
* @staticvar array $moduleoptions Cache
|
2011-02-02 10:37:33 +08:00
|
|
|
* @param stdClass|cm_info $module The module to get options for
|
2010-04-23 04:05:16 +00:00
|
|
|
* @param stdClass $user The user to get options for null == currentuser
|
|
|
|
* @return array
|
|
|
|
*/
|
2011-02-02 10:37:33 +08:00
|
|
|
function blog_get_options_for_module($module, $user=null) {
|
2010-04-23 04:05:16 +00:00
|
|
|
global $CFG, $USER;
|
2014-07-13 18:43:09 +08:00
|
|
|
// Cache.
|
2010-04-23 04:05:16 +00:00
|
|
|
static $moduleoptions = array();
|
|
|
|
|
|
|
|
$options = array();
|
2014-07-13 18:43:09 +08:00
|
|
|
// User must be logged in, blogs must be enabled.
|
2010-04-23 04:05:16 +00:00
|
|
|
if (!blog_is_enabled_for_user()) {
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
2012-07-24 09:52:41 +08:00
|
|
|
$sitecontext = context_system::instance();
|
2010-04-23 04:05:16 +00:00
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// Generate the cache key.
|
2010-04-23 04:05:16 +00:00
|
|
|
$key = $module->id.':';
|
|
|
|
if (!empty($user)) {
|
|
|
|
$key .= $user->id;
|
|
|
|
} else {
|
|
|
|
$key .= $USER->id;
|
|
|
|
}
|
|
|
|
if (array_key_exists($key, $moduleoptions)) {
|
2014-07-13 18:43:09 +08:00
|
|
|
// Serve from the cache so we don't have to regenerate.
|
2013-03-08 13:46:27 +08:00
|
|
|
return $moduleoptions[$key];
|
2010-04-23 04:05:16 +00:00
|
|
|
}
|
|
|
|
|
2013-04-04 15:46:05 +08:00
|
|
|
if (has_capability('moodle/blog:view', $sitecontext)) {
|
2012-07-17 15:21:27 +08:00
|
|
|
// Save correct module name for later usage.
|
|
|
|
$modulename = get_string('modulename', $module->modname);
|
2012-07-10 18:51:23 +02:00
|
|
|
|
2010-04-23 04:05:16 +00:00
|
|
|
// We can view!
|
|
|
|
if ($CFG->bloglevel >= BLOG_SITE_LEVEL) {
|
2014-07-13 18:43:09 +08:00
|
|
|
// View all entries about this module.
|
2010-04-23 04:05:16 +00:00
|
|
|
$a = new stdClass;
|
2012-07-17 15:21:27 +08:00
|
|
|
$a->type = $modulename;
|
2010-04-23 04:05:16 +00:00
|
|
|
$options['moduleview'] = array(
|
|
|
|
'string' => get_string('viewallmodentries', 'blog', $a),
|
|
|
|
'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id))
|
|
|
|
);
|
|
|
|
}
|
2014-07-13 18:43:09 +08:00
|
|
|
// View MY entries about this module.
|
2010-04-23 04:05:16 +00:00
|
|
|
$options['moduleviewmine'] = array(
|
2012-07-17 15:21:27 +08:00
|
|
|
'string' => get_string('viewmyentriesaboutmodule', 'blog', $modulename),
|
2010-04-23 04:05:16 +00:00
|
|
|
'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id, 'userid'=>$USER->id))
|
|
|
|
);
|
|
|
|
if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) {
|
2014-07-13 18:43:09 +08:00
|
|
|
// View the given users entries about this module.
|
2010-04-23 04:05:16 +00:00
|
|
|
$a = new stdClass;
|
2012-07-17 15:21:27 +08:00
|
|
|
$a->mod = $modulename;
|
2010-04-23 04:05:16 +00:00
|
|
|
$a->user = fullname($user);
|
|
|
|
$options['moduleviewuser'] = array(
|
|
|
|
'string' => get_string('blogentriesbyuseraboutmodule', 'blog', $a),
|
|
|
|
'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id, 'userid'=>$user->id))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-04 15:46:05 +08:00
|
|
|
if (has_capability('moodle/blog:create', $sitecontext)) {
|
2014-07-13 18:43:09 +08:00
|
|
|
// The user can blog about this module.
|
2010-04-23 04:05:16 +00:00
|
|
|
$options['moduleadd'] = array(
|
2012-07-17 15:21:27 +08:00
|
|
|
'string' => get_string('blogaboutthismodule', 'blog', $modulename),
|
2010-04-23 04:05:16 +00:00
|
|
|
'link' => new moodle_url('/blog/edit.php', array('action'=>'add', 'modid'=>$module->id))
|
|
|
|
);
|
|
|
|
}
|
2014-07-13 18:43:09 +08:00
|
|
|
// Cache the options.
|
2010-04-23 04:05:16 +00:00
|
|
|
$moduleoptions[$key] = $options;
|
2014-07-13 18:43:09 +08:00
|
|
|
// Return the options.
|
2010-04-23 04:05:16 +00:00
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
2009-09-04 00:36:43 +00:00
|
|
|
/**
|
|
|
|
* This function encapsulates all the logic behind the complex
|
|
|
|
* navigation, titles and headings of the blog listing page, depending
|
2009-10-30 07:25:50 +00:00
|
|
|
* on URL params. It looks at URL params and at the current context level.
|
|
|
|
* It builds and returns an array containing:
|
|
|
|
*
|
|
|
|
* 1. heading: The heading displayed above the blog entries
|
|
|
|
* 2. stradd: The text to be used as the "Add entry" link
|
|
|
|
* 3. strview: The text to be used as the "View entries" link
|
|
|
|
* 4. url: The moodle_url object used as the base for add and view links
|
|
|
|
* 5. filters: An array of parameters used to filter blog listings. Used by index.php and the Recent blogs block
|
2009-09-04 00:36:43 +00:00
|
|
|
*
|
2009-09-04 03:40:01 +00:00
|
|
|
* All other variables are set directly in $PAGE
|
2009-09-04 00:36:43 +00:00
|
|
|
*
|
|
|
|
* It uses the current URL to build these variables.
|
|
|
|
* A number of mutually exclusive use cases are used to structure this function.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2010-07-30 03:16:14 +00:00
|
|
|
function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=null) {
|
2009-09-04 00:36:43 +00:00
|
|
|
global $CFG, $PAGE, $DB, $USER;
|
|
|
|
|
2009-09-08 03:05:09 +00:00
|
|
|
$id = optional_param('id', null, PARAM_INT);
|
2009-09-04 00:36:43 +00:00
|
|
|
$tag = optional_param('tag', null, PARAM_NOTAGS);
|
2010-07-30 03:16:14 +00:00
|
|
|
$tagid = optional_param('tagid', $tagid, PARAM_INT);
|
|
|
|
$userid = optional_param('userid', $userid, PARAM_INT);
|
2009-09-04 00:36:43 +00:00
|
|
|
$modid = optional_param('modid', null, PARAM_INT);
|
|
|
|
$entryid = optional_param('entryid', null, PARAM_INT);
|
2010-07-30 03:16:14 +00:00
|
|
|
$groupid = optional_param('groupid', $groupid, PARAM_INT);
|
|
|
|
$courseid = optional_param('courseid', $courseid, PARAM_INT);
|
2009-09-04 00:36:43 +00:00
|
|
|
$search = optional_param('search', null, PARAM_RAW);
|
|
|
|
$action = optional_param('action', null, PARAM_ALPHA);
|
|
|
|
$confirm = optional_param('confirm', false, PARAM_BOOL);
|
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// Ignore userid when action == add.
|
2009-10-30 07:25:50 +00:00
|
|
|
if ($action == 'add' && $userid) {
|
|
|
|
unset($userid);
|
|
|
|
$PAGE->url->remove_params(array('userid'));
|
|
|
|
} else if ($action == 'add' && $entryid) {
|
|
|
|
unset($entryid);
|
|
|
|
$PAGE->url->remove_params(array('entryid'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$headers = array('title' => '', 'heading' => '', 'cm' => null, 'filters' => array());
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2010-01-16 15:39:56 +00:00
|
|
|
$blogurl = new moodle_url('/blog/index.php');
|
2009-10-30 07:25:50 +00:00
|
|
|
|
|
|
|
$headers['stradd'] = get_string('addnewentry', 'blog');
|
|
|
|
$headers['strview'] = null;
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
$site = $DB->get_record('course', array('id' => SITEID));
|
2012-07-24 09:52:41 +08:00
|
|
|
$sitecontext = context_system::instance();
|
2014-07-13 18:43:09 +08:00
|
|
|
// Common Lang strings.
|
2009-09-04 00:36:43 +00:00
|
|
|
$strparticipants = get_string("participants");
|
|
|
|
$strblogentries = get_string("blogentries", 'blog');
|
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// Prepare record objects as needed.
|
2009-09-04 00:36:43 +00:00
|
|
|
if (!empty($courseid)) {
|
2009-10-30 07:25:50 +00:00
|
|
|
$headers['filters']['course'] = $courseid;
|
2009-09-04 00:36:43 +00:00
|
|
|
$course = $DB->get_record('course', array('id' => $courseid));
|
|
|
|
}
|
2006-10-06 10:11:52 +00:00
|
|
|
|
2009-09-04 00:36:43 +00:00
|
|
|
if (!empty($userid)) {
|
2009-10-30 07:25:50 +00:00
|
|
|
$headers['filters']['user'] = $userid;
|
2009-09-04 00:36:43 +00:00
|
|
|
$user = $DB->get_record('user', array('id' => $userid));
|
2006-03-17 07:38:08 +00:00
|
|
|
}
|
2006-08-08 05:13:06 +00:00
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
if (!empty($groupid)) { // The groupid always overrides courseid.
|
2009-10-30 07:25:50 +00:00
|
|
|
$headers['filters']['group'] = $groupid;
|
2009-09-04 00:36:43 +00:00
|
|
|
$group = $DB->get_record('groups', array('id' => $groupid));
|
|
|
|
$course = $DB->get_record('course', array('id' => $group->courseid));
|
|
|
|
}
|
2006-08-08 05:13:06 +00:00
|
|
|
|
2010-07-15 18:09:11 +00:00
|
|
|
$PAGE->set_pagelayout('standard');
|
2010-04-23 04:05:16 +00:00
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// The modid always overrides courseid, so the $course object may be reset here.
|
2012-07-26 17:40:51 +08:00
|
|
|
if (!empty($modid) && $CFG->useblogassociations) {
|
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
$headers['filters']['module'] = $modid;
|
2014-07-13 18:43:09 +08:00
|
|
|
// A groupid param may conflict with this coursemod's courseid. Ignore groupid in that case.
|
2009-10-30 07:25:50 +00:00
|
|
|
$courseid = $DB->get_field('course_modules', 'course', array('id'=>$modid));
|
|
|
|
$course = $DB->get_record('course', array('id' => $courseid));
|
2009-09-04 00:36:43 +00:00
|
|
|
$cm = $DB->get_record('course_modules', array('id' => $modid));
|
|
|
|
$cm->modname = $DB->get_field('modules', 'name', array('id' => $cm->module));
|
|
|
|
$cm->name = $DB->get_field($cm->modname, 'name', array('id' => $cm->instance));
|
2010-09-21 08:11:06 +00:00
|
|
|
$a = new stdClass();
|
2009-10-30 07:25:50 +00:00
|
|
|
$a->type = get_string('modulename', $cm->modname);
|
2009-09-04 03:40:01 +00:00
|
|
|
$PAGE->set_cm($cm, $course);
|
2009-10-30 07:25:50 +00:00
|
|
|
$headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
|
|
|
|
$headers['strview'] = get_string('viewallmodentries', 'blog', $a);
|
2009-09-04 00:36:43 +00:00
|
|
|
}
|
2009-09-16 10:05:34 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
// Case 1: No entry, mod, course or user params: all site entries to be shown (filtered by search and tag/tagid)
|
2014-07-13 18:43:09 +08:00
|
|
|
// Note: if action is set to 'add' or 'edit', we do this at the end.
|
2009-10-30 07:25:50 +00:00
|
|
|
if (empty($entryid) && empty($modid) && empty($courseid) && empty($userid) && !in_array($action, array('edit', 'add'))) {
|
2012-07-24 09:52:41 +08:00
|
|
|
$shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
|
2009-10-30 07:25:50 +00:00
|
|
|
$PAGE->navbar->add($strblogentries, $blogurl);
|
2011-09-07 11:46:28 +12:00
|
|
|
$PAGE->set_title("$shortname: " . get_string('blog', 'blog'));
|
|
|
|
$PAGE->set_heading("$shortname: " . get_string('blog', 'blog'));
|
|
|
|
$headers['heading'] = get_string('siteblog', 'blog', $shortname);
|
2009-09-11 07:00:43 +00:00
|
|
|
}
|
2006-04-12 02:05:46 +00:00
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// Case 2: only entryid is requested, ignore all other filters. courseid is used to give more contextual information.
|
2009-09-04 00:36:43 +00:00
|
|
|
if (!empty($entryid)) {
|
2009-10-30 07:25:50 +00:00
|
|
|
$headers['filters']['entry'] = $entryid;
|
|
|
|
$sql = 'SELECT u.* FROM {user} u, {post} p WHERE p.id = ? AND p.userid = u.id';
|
2009-09-04 00:36:43 +00:00
|
|
|
$user = $DB->get_record_sql($sql, array($entryid));
|
2009-10-30 07:25:50 +00:00
|
|
|
$entry = $DB->get_record('post', array('id' => $entryid));
|
2006-04-12 02:05:46 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
$blogurl->param('userid', $user->id);
|
2006-04-12 02:05:46 +00:00
|
|
|
|
2009-09-04 00:36:43 +00:00
|
|
|
if (!empty($course)) {
|
2009-09-08 03:05:09 +00:00
|
|
|
$mycourseid = $course->id;
|
2009-10-30 07:25:50 +00:00
|
|
|
$blogurl->param('courseid', $mycourseid);
|
2006-04-12 02:05:46 +00:00
|
|
|
} else {
|
2009-09-08 03:05:09 +00:00
|
|
|
$mycourseid = $site->id;
|
2006-04-12 02:05:46 +00:00
|
|
|
}
|
2012-07-24 09:52:41 +08:00
|
|
|
$shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
|
2009-09-04 07:09:45 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
$PAGE->navbar->add($strblogentries, $blogurl);
|
|
|
|
|
|
|
|
$blogurl->remove_params('userid');
|
|
|
|
$PAGE->navbar->add($entry->subject, $blogurl);
|
2011-09-07 11:46:28 +12:00
|
|
|
$PAGE->set_title("$shortname: " . fullname($user) . ": $entry->subject");
|
|
|
|
$PAGE->set_heading("$shortname: " . fullname($user) . ": $entry->subject");
|
2009-09-04 00:36:43 +00:00
|
|
|
$headers['heading'] = get_string('blogentrybyuser', 'blog', fullname($user));
|
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// We ignore tag and search params.
|
2009-10-30 07:25:50 +00:00
|
|
|
if (empty($action) || !$CFG->useblogassociations) {
|
|
|
|
$headers['url'] = $blogurl;
|
2009-09-08 03:05:09 +00:00
|
|
|
return $headers;
|
|
|
|
}
|
2006-04-12 02:05:46 +00:00
|
|
|
}
|
2008-04-13 01:17:30 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
if (!empty($userid) && empty($entryid) && ((empty($courseid) && empty($modid)) || !$CFG->useblogassociations)) {
|
2014-07-13 18:43:09 +08:00
|
|
|
// Case 3: A user's blog entries.
|
|
|
|
|
2012-07-24 09:52:41 +08:00
|
|
|
$shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
|
2009-10-30 07:25:50 +00:00
|
|
|
$blogurl->param('userid', $userid);
|
2011-09-07 11:46:28 +12:00
|
|
|
$PAGE->set_title("$shortname: " . fullname($user) . ": " . get_string('blog', 'blog'));
|
|
|
|
$PAGE->set_heading("$shortname: " . fullname($user) . ": " . get_string('blog', 'blog'));
|
2009-09-04 00:36:43 +00:00
|
|
|
$headers['heading'] = get_string('userblog', 'blog', fullname($user));
|
2010-04-23 04:05:16 +00:00
|
|
|
$headers['strview'] = get_string('viewuserentries', 'blog', fullname($user));
|
2009-10-30 07:25:50 +00:00
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
} else if (!$CFG->useblogassociations && empty($userid) && !in_array($action, array('edit', 'add'))) {
|
|
|
|
// Case 4: No blog associations, no userid.
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2012-07-24 09:52:41 +08:00
|
|
|
$shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
|
2011-09-07 11:46:28 +12:00
|
|
|
$PAGE->set_title("$shortname: " . get_string('blog', 'blog'));
|
|
|
|
$PAGE->set_heading("$shortname: " . get_string('blog', 'blog'));
|
|
|
|
$headers['heading'] = get_string('siteblog', 'blog', $shortname);
|
2014-07-13 18:43:09 +08:00
|
|
|
} else if (!empty($userid) && !empty($modid) && empty($entryid)) {
|
|
|
|
// Case 5: Blog entries associated with an activity by a specific user (courseid ignored).
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2012-07-24 09:52:41 +08:00
|
|
|
$shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
|
2009-10-30 07:25:50 +00:00
|
|
|
$blogurl->param('userid', $userid);
|
|
|
|
$blogurl->param('modid', $modid);
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// Course module navigation is handled by build_navigation as the second param.
|
2009-09-04 00:36:43 +00:00
|
|
|
$headers['cm'] = $cm;
|
2009-09-04 03:40:01 +00:00
|
|
|
$PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id");
|
2009-10-30 07:25:50 +00:00
|
|
|
$PAGE->navbar->add($strblogentries, $blogurl);
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2011-09-07 11:46:28 +12:00
|
|
|
$PAGE->set_title("$shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
|
|
|
|
$PAGE->set_heading("$shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2010-09-21 08:11:06 +00:00
|
|
|
$a = new stdClass();
|
2009-09-04 00:36:43 +00:00
|
|
|
$a->user = fullname($user);
|
|
|
|
$a->mod = $cm->name;
|
2009-10-30 07:25:50 +00:00
|
|
|
$a->type = get_string('modulename', $cm->modname);
|
2009-09-04 00:36:43 +00:00
|
|
|
$headers['heading'] = get_string('blogentriesbyuseraboutmodule', 'blog', $a);
|
2009-10-30 07:25:50 +00:00
|
|
|
$headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
|
|
|
|
$headers['strview'] = get_string('viewallmodentries', 'blog', $a);
|
2014-07-13 18:43:09 +08:00
|
|
|
} else if (!empty($userid) && !empty($courseid) && empty($modid) && empty($entryid)) {
|
|
|
|
// Case 6: Blog entries associated with a course by a specific user.
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2012-07-24 09:52:41 +08:00
|
|
|
$siteshortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
|
|
|
|
$courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
|
2009-10-30 07:25:50 +00:00
|
|
|
$blogurl->param('userid', $userid);
|
|
|
|
$blogurl->param('courseid', $courseid);
|
2010-09-17 11:21:36 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
$PAGE->navbar->add($strblogentries, $blogurl);
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2011-09-07 11:46:28 +12:00
|
|
|
$PAGE->set_title("$siteshortname: $courseshortname: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
|
|
|
|
$PAGE->set_heading("$siteshortname: $courseshortname: " . fullname($user) . ': ' . get_string('blogentries', 'blog'));
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2010-09-21 08:11:06 +00:00
|
|
|
$a = new stdClass();
|
2009-09-04 00:36:43 +00:00
|
|
|
$a->user = fullname($user);
|
2012-07-24 09:52:41 +08:00
|
|
|
$a->course = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
|
2009-10-30 07:25:50 +00:00
|
|
|
$a->type = get_string('course');
|
2009-09-04 00:36:43 +00:00
|
|
|
$headers['heading'] = get_string('blogentriesbyuseraboutcourse', 'blog', $a);
|
2009-10-30 07:25:50 +00:00
|
|
|
$headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
|
|
|
|
$headers['strview'] = get_string('viewblogentries', 'blog', $a);
|
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// Remove the userid from the URL to inform the blog_menu block correctly.
|
2009-10-30 07:25:50 +00:00
|
|
|
$blogurl->remove_params(array('userid'));
|
2014-07-13 18:43:09 +08:00
|
|
|
} else if (!empty($groupid) && empty($modid) && empty($entryid)) {
|
|
|
|
// Case 7: Blog entries by members of a group, associated with that group's course.
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2012-07-24 09:52:41 +08:00
|
|
|
$siteshortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
|
|
|
|
$courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
|
2009-10-30 07:25:50 +00:00
|
|
|
$blogurl->param('courseid', $course->id);
|
2009-09-04 07:09:45 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
$PAGE->navbar->add($strblogentries, $blogurl);
|
|
|
|
$blogurl->remove_params(array('courseid'));
|
|
|
|
$blogurl->param('groupid', $groupid);
|
|
|
|
$PAGE->navbar->add($group->name, $blogurl);
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2011-09-07 11:46:28 +12:00
|
|
|
$PAGE->set_title("$siteshortname: $courseshortname: " . get_string('blogentries', 'blog') . ": $group->name");
|
|
|
|
$PAGE->set_heading("$siteshortname: $courseshortname: " . get_string('blogentries', 'blog') . ": $group->name");
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2010-09-21 08:11:06 +00:00
|
|
|
$a = new stdClass();
|
2009-09-04 00:36:43 +00:00
|
|
|
$a->group = $group->name;
|
2012-07-24 09:52:41 +08:00
|
|
|
$a->course = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
|
2009-10-30 07:25:50 +00:00
|
|
|
$a->type = get_string('course');
|
2009-09-04 00:36:43 +00:00
|
|
|
$headers['heading'] = get_string('blogentriesbygroupaboutcourse', 'blog', $a);
|
2009-10-30 07:25:50 +00:00
|
|
|
$headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
|
|
|
|
$headers['strview'] = get_string('viewblogentries', 'blog', $a);
|
2014-07-13 18:43:09 +08:00
|
|
|
} else if (!empty($groupid) && !empty($modid) && empty($entryid)) {
|
|
|
|
// Case 8: Blog entries by members of a group, associated with an activity in that course.
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2012-07-24 09:52:41 +08:00
|
|
|
$siteshortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
|
|
|
|
$courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
|
2009-09-04 00:36:43 +00:00
|
|
|
$headers['cm'] = $cm;
|
2009-10-30 07:25:50 +00:00
|
|
|
$blogurl->param('modid', $modid);
|
|
|
|
$PAGE->navbar->add($strblogentries, $blogurl);
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
$blogurl->param('groupid', $groupid);
|
|
|
|
$PAGE->navbar->add($group->name, $blogurl);
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2011-09-07 11:46:28 +12:00
|
|
|
$PAGE->set_title("$siteshortname: $courseshortname: $cm->name: " . get_string('blogentries', 'blog') . ": $group->name");
|
|
|
|
$PAGE->set_heading("$siteshortname: $courseshortname: $cm->name: " . get_string('blogentries', 'blog') . ": $group->name");
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2010-09-21 08:11:06 +00:00
|
|
|
$a = new stdClass();
|
2009-09-04 00:36:43 +00:00
|
|
|
$a->group = $group->name;
|
|
|
|
$a->mod = $cm->name;
|
2009-10-30 07:25:50 +00:00
|
|
|
$a->type = get_string('modulename', $cm->modname);
|
2009-09-04 00:36:43 +00:00
|
|
|
$headers['heading'] = get_string('blogentriesbygroupaboutmodule', 'blog', $a);
|
2009-10-30 07:25:50 +00:00
|
|
|
$headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
|
|
|
|
$headers['strview'] = get_string('viewallmodentries', 'blog', $a);
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
} else if (!empty($modid) && empty($userid) && empty($groupid) && empty($entryid)) {
|
|
|
|
// Case 9: All blog entries associated with an activity.
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2012-07-24 09:52:41 +08:00
|
|
|
$siteshortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
|
|
|
|
$courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
|
2009-09-04 03:40:01 +00:00
|
|
|
$PAGE->set_cm($cm, $course);
|
2009-10-30 07:25:50 +00:00
|
|
|
$blogurl->param('modid', $modid);
|
|
|
|
$PAGE->navbar->add($strblogentries, $blogurl);
|
2011-09-07 11:46:28 +12:00
|
|
|
$PAGE->set_title("$siteshortname: $courseshortname: $cm->name: " . get_string('blogentries', 'blog'));
|
|
|
|
$PAGE->set_heading("$siteshortname: $courseshortname: $cm->name: " . get_string('blogentries', 'blog'));
|
2009-09-04 00:36:43 +00:00
|
|
|
$headers['heading'] = get_string('blogentriesabout', 'blog', $cm->name);
|
2010-09-21 08:11:06 +00:00
|
|
|
$a = new stdClass();
|
2009-10-30 07:25:50 +00:00
|
|
|
$a->type = get_string('modulename', $cm->modname);
|
|
|
|
$headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
|
|
|
|
$headers['strview'] = get_string('viewallmodentries', 'blog', $a);
|
2014-07-13 18:43:09 +08:00
|
|
|
} else if (!empty($courseid) && empty($userid) && empty($groupid) && empty($modid) && empty($entryid)) {
|
|
|
|
// Case 10: All blog entries associated with a course.
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2012-07-24 09:52:41 +08:00
|
|
|
$siteshortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID)));
|
|
|
|
$courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
|
2009-10-30 07:25:50 +00:00
|
|
|
$blogurl->param('courseid', $courseid);
|
|
|
|
$PAGE->navbar->add($strblogentries, $blogurl);
|
2011-09-07 11:46:28 +12:00
|
|
|
$PAGE->set_title("$siteshortname: $courseshortname: " . get_string('blogentries', 'blog'));
|
|
|
|
$PAGE->set_heading("$siteshortname: $courseshortname: " . get_string('blogentries', 'blog'));
|
2010-09-21 08:11:06 +00:00
|
|
|
$a = new stdClass();
|
2009-10-30 07:25:50 +00:00
|
|
|
$a->type = get_string('course');
|
2014-07-13 18:43:09 +08:00
|
|
|
$headers['heading'] = get_string('blogentriesabout',
|
|
|
|
'blog',
|
|
|
|
format_string($course->fullname,
|
|
|
|
true,
|
|
|
|
array('context' => context_course::instance($course->id))));
|
2009-10-30 07:25:50 +00:00
|
|
|
$headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
|
|
|
|
$headers['strview'] = get_string('viewblogentries', 'blog', $a);
|
|
|
|
$blogurl->remove_params(array('userid'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array($action, array('edit', 'add'))) {
|
2014-07-13 18:43:09 +08:00
|
|
|
// Append Tag info.
|
2009-10-30 07:25:50 +00:00
|
|
|
if (!empty($tagid)) {
|
|
|
|
$headers['filters']['tag'] = $tagid;
|
|
|
|
$blogurl->param('tagid', $tagid);
|
|
|
|
$tagrec = $DB->get_record('tag', array('id'=>$tagid));
|
|
|
|
$PAGE->navbar->add($tagrec->name, $blogurl);
|
2014-07-13 18:43:09 +08:00
|
|
|
} else if (!empty($tag)) {
|
2013-11-21 13:36:48 +08:00
|
|
|
if ($tagrec = $DB->get_record('tag', array('name' => $tag))) {
|
|
|
|
$tagid = $tagrec->id;
|
|
|
|
$headers['filters']['tag'] = $tagid;
|
|
|
|
$blogurl->param('tag', $tag);
|
|
|
|
$PAGE->navbar->add(get_string('tagparam', 'blog', $tag), $blogurl);
|
|
|
|
}
|
2009-10-30 07:25:50 +00:00
|
|
|
}
|
2008-04-13 01:17:30 +00:00
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// Append Search info.
|
2009-10-30 07:25:50 +00:00
|
|
|
if (!empty($search)) {
|
|
|
|
$headers['filters']['search'] = $search;
|
|
|
|
$blogurl->param('search', $search);
|
|
|
|
$PAGE->navbar->add(get_string('searchterm', 'blog', $search), $blogurl->out());
|
|
|
|
}
|
2009-09-10 07:05:50 +00:00
|
|
|
}
|
|
|
|
|
2014-07-13 18:43:09 +08:00
|
|
|
// Append edit mode info.
|
2009-09-04 00:36:43 +00:00
|
|
|
if (!empty($action) && $action == 'add') {
|
2010-08-19 05:56:57 +00:00
|
|
|
|
2009-09-04 00:36:43 +00:00
|
|
|
} else if (!empty($action) && $action == 'edit') {
|
2009-09-04 03:40:01 +00:00
|
|
|
$PAGE->navbar->add(get_string('editentry', 'blog'));
|
2008-04-13 01:17:30 +00:00
|
|
|
}
|
2009-09-10 07:05:50 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
if (empty($headers['url'])) {
|
|
|
|
$headers['url'] = $blogurl;
|
|
|
|
}
|
2009-09-04 00:36:43 +00:00
|
|
|
return $headers;
|
|
|
|
}
|
2009-09-15 07:57:54 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
/**
|
|
|
|
* Shortcut function for getting a count of blog entries associated with a course or a module
|
|
|
|
* @param int $courseid The ID of the course
|
|
|
|
* @param int $cmid The ID of the course_modules
|
|
|
|
* @return string The number of associated entries
|
|
|
|
*/
|
|
|
|
function blog_get_associated_count($courseid, $cmid=null) {
|
|
|
|
global $DB;
|
2012-07-24 09:52:41 +08:00
|
|
|
$context = context_course::instance($courseid);
|
2009-10-30 07:25:50 +00:00
|
|
|
if ($cmid) {
|
2012-07-24 09:52:41 +08:00
|
|
|
$context = context_module::instance($cmid);
|
2009-10-30 07:25:50 +00:00
|
|
|
}
|
|
|
|
return $DB->count_records('blog_association', array('contextid' => $context->id));
|
2010-05-01 14:33:07 +00:00
|
|
|
}
|
2011-05-04 17:23:46 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Running addtional permission check on plugin, for example, plugins
|
|
|
|
* may have switch to turn on/off comments option, this callback will
|
|
|
|
* affect UI display, not like pluginname_comment_validate only throw
|
|
|
|
* exceptions.
|
2013-03-08 09:37:21 +08:00
|
|
|
* blog_comment_validate will be called before viewing/adding/deleting
|
|
|
|
* comment, so don't repeat checks.
|
2011-05-04 17:23:46 +08:00
|
|
|
* Capability check has been done in comment->check_permissions(), we
|
|
|
|
* don't need to do it again here.
|
|
|
|
*
|
2012-02-15 11:21:35 +08:00
|
|
|
* @package core_blog
|
|
|
|
* @category comment
|
|
|
|
*
|
2011-05-04 17:23:46 +08:00
|
|
|
* @param stdClass $comment_param {
|
|
|
|
* context => context the context object
|
|
|
|
* courseid => int course id
|
|
|
|
* cm => stdClass course module object
|
|
|
|
* commentarea => string comment area
|
|
|
|
* itemid => int itemid
|
|
|
|
* }
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function blog_comment_permissions($comment_param) {
|
2013-03-08 09:37:21 +08:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
// If blog is public and current user is guest, then don't let him post comments.
|
|
|
|
$blogentry = $DB->get_record('post', array('id' => $comment_param->itemid), 'publishstate', MUST_EXIST);
|
|
|
|
|
|
|
|
if ($blogentry->publishstate != 'public') {
|
|
|
|
if (!isloggedin() || isguestuser()) {
|
|
|
|
return array('post' => false, 'view' => true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array('post' => true, 'view' => true);
|
2011-05-04 17:23:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate comment parameter before perform other comments actions
|
|
|
|
*
|
2012-02-15 11:21:35 +08:00
|
|
|
* @package core_blog
|
|
|
|
* @category comment
|
|
|
|
*
|
2011-05-04 17:23:46 +08:00
|
|
|
* @param stdClass $comment {
|
|
|
|
* context => context the context object
|
|
|
|
* courseid => int course id
|
|
|
|
* cm => stdClass course module object
|
|
|
|
* commentarea => string comment area
|
|
|
|
* itemid => int itemid
|
|
|
|
* }
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function blog_comment_validate($comment_param) {
|
2013-03-08 09:37:21 +08:00
|
|
|
global $CFG, $DB, $USER;
|
|
|
|
|
|
|
|
// Check if blogs are enabled user can comment.
|
|
|
|
if (empty($CFG->enableblogs) || empty($CFG->blogusecomments)) {
|
|
|
|
throw new comment_exception('nopermissiontocomment');
|
2011-05-04 17:23:46 +08:00
|
|
|
}
|
2013-03-08 09:37:21 +08:00
|
|
|
|
2013-05-07 12:02:26 +08:00
|
|
|
// Validate comment area.
|
2011-05-04 17:23:46 +08:00
|
|
|
if ($comment_param->commentarea != 'format_blog') {
|
|
|
|
throw new comment_exception('invalidcommentarea');
|
|
|
|
}
|
2013-03-08 09:37:21 +08:00
|
|
|
|
|
|
|
$blogentry = $DB->get_record('post', array('id' => $comment_param->itemid), '*', MUST_EXIST);
|
|
|
|
|
2013-05-07 12:02:26 +08:00
|
|
|
// Validation for comment deletion.
|
2011-05-04 17:23:46 +08:00
|
|
|
if (!empty($comment_param->commentid)) {
|
|
|
|
if ($record = $DB->get_record('comments', array('id'=>$comment_param->commentid))) {
|
|
|
|
if ($record->commentarea != 'format_blog') {
|
|
|
|
throw new comment_exception('invalidcommentarea');
|
|
|
|
}
|
|
|
|
if ($record->contextid != $comment_param->context->id) {
|
|
|
|
throw new comment_exception('invalidcontext');
|
|
|
|
}
|
|
|
|
if ($record->itemid != $comment_param->itemid) {
|
|
|
|
throw new comment_exception('invalidcommentitemid');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new comment_exception('invalidcommentid');
|
|
|
|
}
|
|
|
|
}
|
2013-03-08 09:37:21 +08:00
|
|
|
|
|
|
|
// Validate if user has blog view permission.
|
|
|
|
$sitecontext = context_system::instance();
|
|
|
|
return has_capability('moodle/blog:view', $sitecontext) &&
|
|
|
|
blog_user_can_view_user_entry($blogentry->userid, $blogentry);
|
2011-05-04 17:23:46 +08:00
|
|
|
}
|
MDL-26105 Block settings should contains less options, and be more user friendly
AMOS BEGIN
MOV [page-blog-index, pagetype], [page-blog-index, blog]
MOV [page-blog-x, pagetype], [page-blog-x, blog]
MOV [page-tag-x, pagetype], [page-tag-x, tag]
MOV [page-course-view-weeks, pagetype], [page-course-view-weeks, format_weeks]
MOV [page-course-view-weeks-x, pagetype], [page-course-view-weeks-x, format_weeks]
MOV [page-course-view-topics, pagetype], [page-course-view-topics, format_topics]
MOV [page-course-view-topics-x, pagetype], [page-course-view-topics-x, format_topics]
AMOS END
2011-04-28 11:20:30 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of page types
|
|
|
|
* @param string $pagetype current page type
|
|
|
|
* @param stdClass $parentcontext Block's parent context
|
|
|
|
* @param stdClass $currentcontext Current context of block
|
|
|
|
*/
|
2011-06-17 16:23:10 +08:00
|
|
|
function blog_page_type_list($pagetype, $parentcontext, $currentcontext) {
|
MDL-26105 Block settings should contains less options, and be more user friendly
AMOS BEGIN
MOV [page-blog-index, pagetype], [page-blog-index, blog]
MOV [page-blog-x, pagetype], [page-blog-x, blog]
MOV [page-tag-x, pagetype], [page-tag-x, tag]
MOV [page-course-view-weeks, pagetype], [page-course-view-weeks, format_weeks]
MOV [page-course-view-weeks-x, pagetype], [page-course-view-weeks-x, format_weeks]
MOV [page-course-view-topics, pagetype], [page-course-view-topics, format_topics]
MOV [page-course-view-topics-x, pagetype], [page-course-view-topics-x, format_topics]
AMOS END
2011-04-28 11:20:30 +08:00
|
|
|
return array(
|
|
|
|
'*'=>get_string('page-x', 'pagetype'),
|
|
|
|
'blog-*'=>get_string('page-blog-x', 'blog'),
|
|
|
|
'blog-index'=>get_string('page-blog-index', 'blog'),
|
|
|
|
'blog-edit'=>get_string('page-blog-edit', 'blog')
|
|
|
|
);
|
|
|
|
}
|