From 27bad0a6298d43188fecee95f0ddbeefb1bcdacd Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Fri, 23 Apr 2010 04:05:16 +0000 Subject: [PATCH] blog MDL-22161 Fixed up blog menu block and added navigation options for users. --- blocks/blog_menu/block_blog_menu.php | 106 ++++------ blog/lib.php | 296 ++++++++++++++++++++++++++- lang/en/blog.php | 16 +- lib/navigationlib.php | 13 ++ 4 files changed, 360 insertions(+), 71 deletions(-) diff --git a/blocks/blog_menu/block_blog_menu.php b/blocks/blog_menu/block_blog_menu.php index 1943fc59384..a6ca2216667 100755 --- a/blocks/blog_menu/block_blog_menu.php +++ b/blocks/blog_menu/block_blog_menu.php @@ -25,9 +25,14 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ - +/** + * Require the blog lib file, several useful functions in there + */ require_once($CFG->dirroot .'/blog/lib.php'); +/** + * The blog menu block class + */ class block_blog_menu extends block_base { function init() { @@ -52,84 +57,57 @@ class block_blog_menu extends block_base { } function get_content() { - global $CFG, $USER, $PAGE, $OUTPUT; + + // Check if we've already generated content + if (!empty($this->content)) { + return $this->content; + } - $context = $PAGE->context; + // Prep the content + $this->content = new stdClass; - // don't display menu block if block is set at site level, and user is not logged in - if (empty($CFG->bloglevel) || ($CFG->bloglevel < BLOG_GLOBAL_LEVEL && !(isloggedin() && !isguestuser()))) { + /** + * Prepare the content for this block + */ + $options = blog_get_all_options($this->page); + if (count($options)==0) { + // Don't display menu block if block is set at site level, and user is not logged in $this->content->text = ''; if ($this->page->user_is_editing()) { + // If editing is enabled show an informative message $this->content->text = get_string('blogdisable', 'blog'); } return $this->content; } - - $output = ''; - - $this->content = new stdClass; - - $blogheaders = blog_get_headers(); - $canviewblogs = has_capability('moodle/blog:view', $context); - - /// Accessibility: markup as a list. - - $blogmodon = false; + + // Iterate the option types $menulist = array(); - - if (!empty($blogheaders['strview']) && $CFG->useblogassociations) { - if ($blogheaders['url']->compare($PAGE->url) == URL_MATCH_EXACT) { - $menulist[] = html_writer::tag('span', $blogheaders['strview'], array('class'=>'current')); - } else { - $menulist[] = html_writer::link($blogheaders['url'], $blogheaders['strview']); + foreach ($options as $types) { + foreach ($types as $link) { + $menulist[] = html_writer::link($link['link'], $link['string']); } + $menulist[] = '
'; } + // Remove the last element (will be an HR) + array_pop($menulist); + // Display the content as a list + $this->content->text = html_writer::alist($menulist, array('class'=>'list')); - // show View site entries link - if ($CFG->bloglevel >= BLOG_SITE_LEVEL && $canviewblogs) { - if (!$PAGE->url->param('search') && !$PAGE->url->param('tag') && !$PAGE->url->param('tagid') && - !$PAGE->url->param('modid') && !$PAGE->url->param('courseid') && !$PAGE->url->param('userid') && !$PAGE->url->param('entryid')) { - // no - } else { - $menulist[] = html_writer::link($CFG->wwwroot .'/blog/index.php', get_string('viewsiteentries', 'blog')); - } - } - - $output .= ''; - - // show View my entries link - $pageuserid = $PAGE->url->param('userid'); - if (!empty($pageuserid) && $pageuserid == $USER->id) { - // no - } else { - $murl = new moodle_url('/blog/index.php', array('userid' => $USER->id)); - $murl->params($blogheaders['url']->params()); - $murl->param('userid', $USER->id); - $menulist[] = html_writer::link($murl, get_string('viewmyentries', 'blog')); - } - - // show "Add entry" or "Blog about this" link - $sitecontext = get_context_instance(CONTEXT_SYSTEM); - if (has_capability('moodle/blog:create', $sitecontext)) { - $aurl = new moodle_url('/blog/edit.php', array('action' => 'add')); - $aurl->params($blogheaders['url']->params()); - if ($PAGE->url->compare($aurl) != URL_MATCH_EXACT) { - $menulist[] =html_writer::link($aurl, $blogheaders['stradd']); - } - } - - // Full-text search field - if (has_capability('moodle/blog:search', $sitecontext)) { - $target = new moodle_url($blogheaders['url']); - $form = '
'; - $form .= '
'; - $form .= ''; - $form .= '
'; - $this->content->footer = $form; + /** + * Prepare the footer for this block + */ + if (has_capability('moodle/blog:search', get_context_instance(CONTEXT_SYSTEM))) { + // Full-text search field + $form = html_writer::tag('label', get_string('search', 'admin'), array('for'=>'blogsearchquery', 'class'=>'accesshide')); + $form .= html_writer::empty_tag('input', array('id'=>'blogsearchquery', 'type'=>'text', 'name'=>'search')); + $form .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('search'))); + $this->content->footer = html_writer::tag('form', html_writer::tag('div', $form), array('class'=>'blogsearchform', 'method'=>'get', 'action'=>new moodle_url('/blog/index.php'))); } else { + // No footer to display $this->content->footer = ''; } - $this->content->text = html_writer::alist($menulist, array('class'=>'list')); + // Return the content object + return $this->content; } } diff --git a/blog/lib.php b/blog/lib.php index 4852792748d..e7203ec930b 100755 --- a/blog/lib.php +++ b/blog/lib.php @@ -268,6 +268,296 @@ function blog_get_context_url($context=null) { return $viewblogentriesurl; } +/** + * This function checks that blogs are enabled, and that the user is logged in an + * is not the guest user. + * @return bool + */ +function blog_is_enabled_for_user() { + global $CFG; + return (!empty($CFG->bloglevel) && $CFG->bloglevel <= BLOG_GLOBAL_LEVEL && isloggedin() && !isguestuser()); +} + +/** + * This function gets all of the options available for the current user in respect + * to blogs. + * + * 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(); + + // If blogs are enabled and the user is logged in and not a guest + if (blog_is_enabled_for_user()) { + // If the context is the user then assume we want to load for the users context + if (is_null($userid) && $page->context->contextlevel == CONTEXT_USER) { + $userid = $page->context->instanceid; + } + // Check the userid var + 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) { + // Load for the module associated with the page + $options[CONTEXT_MODULE] = blog_get_options_for_module($page->cm, $user); + } else if ($CFG->useblogassociations && $page->course->id != SITEID) { + // Load the options for the course associated with the page + $options[CONTEXT_COURSE] = blog_get_options_for_course($page->course, $user); + } + + // Get the options for the user + if ($user !== null) { + // Load for the requested user + $options[CONTEXT_USER+1] = blog_get_options_for_user($user); + } + // Load for the current user + $options[CONTEXT_USER] = blog_get_options_for_user(); + } + + // If blog level is global then display a link to view all site entries + if (!empty($CFG->bloglevel) && $CFG->bloglevel >= BLOG_GLOBAL_LEVEL && has_capability('moodle/blog:view', get_context_instance(CONTEXT_SYSTEM))) { + $options[CONTEXT_SYSTEM] = array('viewsite' => array( + 'string' => get_string('viewsiteentries', 'blog'), + 'link' => new moodle_url('/blog/index.php') + )); + } + + // Return the options + 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; + // Cache + static $useroptions = array(); + + $options = array(); + // Blogs must be enabled and the user must be logged in + if (!blog_is_enabled_for_user()) { + return $options; + } + + // Sort out the user var + if ($user === null || $user->id == $USER->id) { + $user = $USER; + $iscurrentuser = true; + } else { + $iscurrentuser = false; + } + + // If we've already generated serve from the cache + if (array_key_exists($user->id, $useroptions)) { + return $useroptions[$user->id]; + } + + $sitecontext = get_context_instance(CONTEXT_SYSTEM); + $canview = has_capability('moodle/blog:view', $sitecontext); + + if (!$iscurrentuser && $canview && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) { + // Not the current user, but we can view and its blogs are enabled for SITE or GLOBAL + $options['userentries'] = array( + 'string' => get_string('viewuserentries', 'blog', fullname($user)), + 'link' => new moodle_url('/blog/index.php', array('userid'=>$user->id)) + ); + } else { + // It's the current user + if ($canview) { + // We can view our own blogs .... BIG surprise + $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)) { + // We can add to our own blog + $options['add'] = array( + 'string' => get_string('addnewentry', 'blog'), + 'link' => new moodle_url('/blog/edit.php', array('action'=>'add')) + ); + } + } + // Cache the options + $useroptions[$user->id] = $options; + // Return the options + 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; + // Cache + static $courseoptions = array(); + + $options = array(); + + // User must be logged in and blogs must be enabled + if (!blog_is_enabled_for_user()) { + return $options; + } + + // Check that the user can associate with the course + $sitecontext = get_context_instance(CONTEXT_SYSTEM); + if (!has_capability('moodle/blog:associatecourse', $sitecontext)) { + return $options; + } + // Generate the cache key + $key = $course->id.':'; + if (!empty($user)) { + $key .= $user->id; + } else { + $key .= $USER->id; + } + // Serve from the cache if we've already generated for this course + if (array_key_exists($key, $courseoptions)) { + return $courseoptions[$course->id]; + } + + if (has_capability('moodle/blog:view', get_context_instance(CONTEXT_COURSE, $course->id))) { + // We can view! + if ($CFG->bloglevel >= BLOG_SITE_LEVEL) { + // View entries about this course + $options['courseview'] = array( + 'string' => get_string('viewcourseblogs', 'blog'), + 'link' => new moodle_url('/blog/index.php', array('courseid'=>$course->id)) + ); + } + // View MY entries about this course + $options['courseviewmine'] = array( + 'string' => get_string('viewmyentriesaboutcourse', 'blog'), + 'link' => new moodle_url('/blog/index.php', array('courseid'=>$course->id, 'userid'=>$USER->id)) + ); + if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) { + // View the provided users entries about this course + $options['courseviewuser'] = array( + 'string' => get_string('viewentriesbyuseraboutcourse', 'blog', fullname($user)), + 'link' => new moodle_url('/blog/index.php', array('courseid'=>$course->id, 'userid'=>$user->id)) + ); + } + } + + if (has_capability('moodle/blog:create', $sitecontext)) { + // We can blog about this course + $options['courseadd'] = array( + 'string' => get_string('blogaboutthiscourse', 'blog', get_string('course')), + 'link' => new moodle_url('/blog/edit.php', array('action'=>'add', 'courseid'=>$course->id)) + ); + } + + + // Cache the options for this course + $courseoptions[$key] = $options; + // Return the options + return $options; +} + +/** + * Get the blog options relating to the given module for the given user + * + * @staticvar array $moduleoptions Cache + * @param stdClass $module The module to get options for + * @param stdClass $user The user to get options for null == currentuser + * @return array + */ +function blog_get_options_for_module(stdClass $module, stdClass $user=null) { + global $CFG, $USER; + // Cache + static $moduleoptions = array(); + + $options = array(); + // User must be logged in, blogs must be enabled + if (!blog_is_enabled_for_user()) { + return $options; + } + + // Check the user can associate with the module + $sitecontext = get_context_instance(CONTEXT_SYSTEM); + if (!has_capability('moodle/blog:associatemodule', $sitecontext)) { + return $options; + } + + // Generate the cache key + $key = $module->id.':'; + if (!empty($user)) { + $key .= $user->id; + } else { + $key .= $USER->id; + } + if (array_key_exists($key, $moduleoptions)) { + // Serve from the cache so we don't have to regenerate + return $moduleoptions[$module->id]; + } + + if (has_capability('moodle/blog:view', get_context_instance(CONTEXT_MODULE, $module->id))) { + // We can view! + if ($CFG->bloglevel >= BLOG_SITE_LEVEL) { + // View all entries about this module + $a = new stdClass; + $a->type = $module->modname; + $options['moduleview'] = array( + 'string' => get_string('viewallmodentries', 'blog', $a), + 'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id)) + ); + } + // View MY entries about this module + $options['moduleviewmine'] = array( + 'string' => get_string('viewmyentriesaboutmodule', 'blog', $module->modname), + 'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id, 'userid'=>$USER->id)) + ); + if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) { + // View the given users entries about this module + $a = new stdClass; + $a->mod = $module->modname; + $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)) + ); + } + } + + if (has_capability('moodle/blog:create', $sitecontext)) { + // The user can blog about this module + $options['moduleadd'] = array( + 'string' => get_string('blogaboutthismodule', 'blog', $module->modname), + 'link' => new moodle_url('/blog/edit.php', array('action'=>'add', 'modid'=>$module->id)) + ); + } + // Cache the options + $moduleoptions[$key] = $options; + // Return the options + return $options; +} + /** * This function encapsulates all the logic behind the complex * navigation, titles and headings of the blog listing page, depending @@ -358,6 +648,8 @@ function blog_get_headers() { $course = $DB->get_record('course', array('id' => $group->courseid)); } + $PAGE->set_pagelayout('standard'); + if (!empty($modid) && $CFG->useblogassociations && has_capability('moodle/blog:associatemodule', $sitecontext)) { // modid always overrides courseid, so the $course object may be reset here $headers['filters']['module'] = $modid; // A groupid param may conflict with this coursemod's courseid. Ignore groupid in that case @@ -425,7 +717,7 @@ function blog_get_headers() { $PAGE->set_title("$site->shortname: " . fullname($user) . ": " . get_string('blog', 'blog')); $PAGE->set_heading("$site->shortname: " . fullname($user) . ": " . get_string('blog', 'blog')); $headers['heading'] = get_string('userblog', 'blog', fullname($user)); - $headers['strview'] = get_string('viewuserentries', 'blog'); + $headers['strview'] = get_string('viewuserentries', 'blog', fullname($user)); } else @@ -622,4 +914,4 @@ function blog_get_associated_count($courseid, $cmid=null) { $context = get_context_instance(CONTEXT_MODULE, $cmid); } return $DB->count_records('blog_association', array('contextid' => $context->id)); -} +} \ No newline at end of file diff --git a/lang/en/blog.php b/lang/en/blog.php index cc7b7a77ad2..fff971f5a76 100755 --- a/lang/en/blog.php +++ b/lang/en/blog.php @@ -38,6 +38,8 @@ $string['blockexternalstitle'] = 'External Blogs'; $string['blocktitle'] = 'Blog tags block title'; $string['blog'] = 'Blog'; $string['blogaboutthis'] = 'Blog about this {$a->type}'; +$string['blogaboutthiscourse'] = 'Add an entry about this course'; +$string['blogaboutthismodule'] = 'Add an entry about this {$a}'; $string['blogadministration'] = 'Blog administration'; $string['blogdeleteconfirm'] = 'Delete this blog?'; $string['blogdisable'] = 'Blogging is disabled!'; @@ -46,7 +48,7 @@ $string['blogentriesabout'] = 'Blog entries about {$a}'; $string['blogentriesbygroupaboutcourse'] = 'Blog entries about {$a->course} by {$a->group}'; $string['blogentriesbygroupaboutmodule'] = 'Blog entries about {$a->mod} by {$a->group}'; $string['blogentriesbyuseraboutcourse'] = 'Blog entries about {$a->course} by {$a->user}'; -$string['blogentriesbyuseraboutmodule'] = 'Blog entries about {$a->mod} by {$a->user}'; +$string['blogentriesbyuseraboutmodule'] = 'Blog entries about this {$a->mod} by {$a->user}'; $string['blogentrybyuser'] = 'Blog entry by {$a}'; $string['blogpreferences'] = 'Blog preferences'; $string['blogs'] = 'Blogs'; @@ -141,16 +143,20 @@ $string['userblog'] = 'User blog: {$a}'; $string['userblogentries'] = 'Blog entries by {$a}'; $string['valid'] = 'Valid'; $string['viewallblogentries'] = 'All entries about this {$a}'; -$string['viewallmodentries'] = 'All entries about this {$a->type}'; +$string['viewallmodentries'] = 'View all entries about this {$a->type}'; +$string['viewallmyentries'] = 'View all of my entries'; +$string['viewentriesbyuseraboutcourse'] = 'View entries about this course by {$a}'; $string['viewblogentries'] = 'Entries about this {$a->type}'; $string['viewblogsfor'] = 'View all entries for...'; -$string['viewcourseblogs'] = 'View entries for course...'; +$string['viewcourseblogs'] = 'View all entries for this course'; $string['viewgroupblogs'] = 'View entries for group...'; $string['viewgroupentries'] = 'Group entries'; $string['viewmodblogs'] = 'View entries for module...'; $string['viewmodentries'] = 'Module entries'; $string['viewmyentries'] = 'My entries'; -$string['viewsiteentries'] = 'All entries'; -$string['viewuserentries'] = 'User entries'; +$string['viewmyentriesaboutmodule'] = 'View my entries about this {$a}'; +$string['viewmyentriesaboutcourse'] = 'View my entries about this course'; +$string['viewsiteentries'] = 'View all entries'; +$string['viewuserentries'] = 'View all entries by {$a}'; $string['worldblogs'] = 'The world can read entries set to be world-accessible'; $string['wrongpostid'] = 'Wrong blog post id'; diff --git a/lib/navigationlib.php b/lib/navigationlib.php index f0c128cf7d1..f601be19394 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -1237,6 +1237,19 @@ class global_navigation extends navigation_node { } } + // Add blog nodes + if (!empty($CFG->bloglevel)) { + require_once($CFG->dirroot.'/blog/lib.php'); + // Get all options for the user + $options = blog_get_options_for_user($user); + if (count($options) > 0) { + $blogs = $usernode->add(get_string('blogs', 'blog'), null, navigation_node::TYPE_CONTAINER); + foreach ($options as $option) { + $blogs->add($option['string'], $option['link']); + } + } + } + // Add a node to view the users notes if permitted if (!empty($CFG->enablenotes) && has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $coursecontext)) { $url = new moodle_url('/notes/index.php',array('user'=>$user->id));