From 2b6e53e8e203f503afcd27edfe8fab7aa0b8b248 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Sun, 13 Jul 2014 18:43:09 +0800 Subject: [PATCH] MDL-46346 blog: coding style fixes --- blog/edit.php | 56 +++++---- blog/edit_form.php | 41 +++++-- blog/external_blog_edit.php | 17 +-- blog/external_blog_edit_form.php | 5 +- blog/external_blogs.php | 27 +++-- blog/index.php | 33 +++-- blog/lib.php | 200 +++++++++++++++---------------- blog/locallib.php | 127 +++++++++++--------- blog/preferences.php | 5 +- blog/preferences_form.php | 3 +- blog/renderer.php | 13 +- blog/rsslib.php | 53 ++++---- blog/tests/bloglib_test.php | 36 +++--- 13 files changed, 340 insertions(+), 276 deletions(-) diff --git a/blog/edit.php b/blog/edit.php index ebf4c969b5f..c3052f796d5 100644 --- a/blog/edit.php +++ b/blog/edit.php @@ -1,5 +1,4 @@ set_url('/blog/edit.php', array('action' => $action, 'entryid' => $id, 'confirm' => $confirm, 'modid' => $modid, 'courseid' => $courseid)); +$PAGE->set_url('/blog/edit.php', array('action' => $action, + 'entryid' => $id, + 'confirm' => $confirm, + 'modid' => $modid, + 'courseid' => $courseid)); -// If action is add, we ignore $id to avoid any further problems +// If action is add, we ignore $id to avoid any further problems. if (!empty($id) && $action == 'add') { $id = null; } @@ -65,7 +68,7 @@ if (!empty($courseid) && empty($modid)) { $returnurl->param('courseid', $courseid); } -// If a modid is given, guess courseid +// If a modid is given, guess courseid. if (!empty($modid)) { $returnurl->param('modid', $modid); $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid)); @@ -78,7 +81,7 @@ if (!has_capability('moodle/blog:create', $sitecontext) && !has_capability('mood print_error('cannoteditentryorblog'); } -// Make sure that the person trying to edit has access right +// Make sure that the person trying to edit has access right. if ($id) { if (!$entry = new blog_entry($id)) { print_error('wrongentryid', 'blog'); @@ -93,7 +96,7 @@ if ($id) { } else { if (!has_capability('moodle/blog:create', $sitecontext)) { - print_error('noentry', 'blog'); // manageentries is not enough for adding + print_error('noentry', 'blog'); // The capability "manageentries" is not enough for adding. } $entry = new stdClass(); $entry->id = null; @@ -104,14 +107,14 @@ $returnurl->param('userid', $userid); // Blog renderer. $output = $PAGE->get_renderer('blog'); -$strblogs = get_string('blogs','blog'); +$strblogs = get_string('blogs', 'blog'); -if ($action === 'delete'){ +if ($action === 'delete') { if (empty($entry->id)) { print_error('wrongentryid', 'blog'); } if (data_submitted() && $confirm && confirm_sesskey()) { - // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability + // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability. if (!blog_user_can_edit_entry($entry)) { print_error('nopermissionstodeleteentry', 'blog'); } else { @@ -131,7 +134,9 @@ if ($action === 'delete'){ echo $output->render($entry); echo '
'; - echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'), new moodle_url('edit.php', $optionsyes),new moodle_url( 'index.php', $optionsno)); + echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'), + new moodle_url('edit.php', $optionsyes), + new moodle_url('index.php', $optionsno)); echo $OUTPUT->footer(); die; } @@ -166,10 +171,21 @@ $summaryoptions = array('maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes, 'trusttext' 'subdirs'=>file_area_contains_subdirs($sitecontext, 'blog', 'post', $entry->id)); $attachmentoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes); -$blogeditform = new blog_edit_form(null, compact('entry', 'summaryoptions', 'attachmentoptions', 'sitecontext', 'courseid', 'modid')); +$blogeditform = new blog_edit_form(null, compact('entry', + 'summaryoptions', + 'attachmentoptions', + 'sitecontext', + 'courseid', + 'modid')); $entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id); -$entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog', 'attachment', $entry->id); +$entry = file_prepare_standard_filemanager($entry, + 'attachment', + $attachmentoptions, + $sitecontext, + 'blog', + 'attachment', + $entry->id); if (!empty($CFG->usetags) && !empty($entry->id)) { include_once($CFG->dirroot.'/tag/lib.php'); @@ -177,13 +193,13 @@ if (!empty($CFG->usetags) && !empty($entry->id)) { } $entry->action = $action; -// set defaults +// Set defaults. $blogeditform->set_data($entry); if ($blogeditform->is_cancelled()) { redirect($returnurl); -} else if ($data = $blogeditform->get_data()){ +} else if ($data = $blogeditform->get_data()) { switch ($action) { case 'add': @@ -208,23 +224,23 @@ if ($blogeditform->is_cancelled()) { } -// gui setup +// GUI setup. switch ($action) { case 'add': - // prepare new empty form + // Prepare new empty form. $entry->publishstate = 'site'; $strformheading = get_string('addnewentry', 'blog'); $entry->action = $action; if ($CFG->useblogassociations) { - //pre-select the course for associations + // Pre-select the course for associations. if ($courseid) { $context = context_course::instance($courseid); $entry->courseassoc = $context->id; } - //pre-select the mod for associations + // Pre-select the mod for associations. if ($modid) { $context = context_module::instance($modid); $entry->modassoc = $context->id; diff --git a/blog/edit_form.php b/blog/edit_form.php index 2b28a737567..1f60ebf6193 100644 --- a/blog/edit_form.php +++ b/blog/edit_form.php @@ -1,5 +1,4 @@ . if (!defined('MOODLE_INTERNAL')) { - die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page + die('Direct access to this script is forbidden.'); // It must be included from a Moodle page. } require_once($CFG->libdir.'/formslib.php'); @@ -24,7 +23,10 @@ require_once($CFG->libdir.'/formslib.php'); class blog_edit_form extends moodleform { public $modnames = array(); - function definition() { + /** + * Blog form definition. + */ + public function definition() { global $CFG, $DB; $mform =& $this->_form; @@ -50,12 +52,12 @@ class blog_edit_form extends moodleform { $mform->addElement('filemanager', 'attachment_filemanager', get_string('attachment', 'forum'), null, $attachmentoptions); - //disable publishstate options that are not allowed + // Disable publishstate options that are not allowed. $publishstates = array(); $i = 0; foreach (blog_entry::get_applicable_publish_states() as $state => $desc) { - $publishstates[$state] = $desc; //no maximum was set + $publishstates[$state] = $desc; // No maximum was set. $i++; } @@ -87,7 +89,12 @@ class blog_edit_form extends moodleform { } $mform->addElement('header', 'assochdr', get_string('associations', 'blog')); - $mform->addElement('advcheckbox', 'courseassoc', get_string('associatewithcourse', 'blog', $a), null, null, array(0, $contextid)); + $mform->addElement('advcheckbox', + 'courseassoc', + get_string('associatewithcourse', 'blog', $a), + null, + null, + array(0, $contextid)); $mform->setDefault('courseassoc', $contextid); } else if ((!empty($entry->modassoc) || !empty($modid))) { @@ -107,7 +114,12 @@ class blog_edit_form extends moodleform { } $mform->addElement('header', 'assochdr', get_string('associations', 'blog')); - $mform->addElement('advcheckbox', 'modassoc', get_string('associatewithmodule', 'blog', $a), null, null, array(0, $context->id)); + $mform->addElement('advcheckbox', + 'modassoc', + get_string('associatewithmodule', 'blog', $a), + null, + null, + array(0, $context->id)); $mform->setDefault('modassoc', $context->id); } } @@ -130,12 +142,17 @@ class blog_edit_form extends moodleform { $mform->setDefault('courseid', $courseid); } - function validation($data, $files) { + /** + * Validate the blog form data. + * @param $data array Data to be validated + * @param $files void unused + */ + public function validation($data, $files) { global $CFG, $DB, $USER; $errors = array(); - // validate course association + // Validate course association. if (!empty($data['courseassoc'])) { $coursecontext = context::instance_by_id($data['courseassoc']); @@ -144,16 +161,16 @@ class blog_edit_form extends moodleform { } } - // validate mod association + // Validate mod association. if (!empty($data['modassoc'])) { $modcontextid = $data['modassoc']; $modcontext = context::instance_by_id($modcontextid); if ($modcontext->contextlevel == CONTEXT_MODULE) { - // get context of the mod's course + // Get context of the mod's course. $coursecontext = $modcontext->get_course_context(true); - // ensure only one course is associated + // Ensure only one course is associated. if (!empty($data['courseassoc'])) { if ($data['courseassoc'] != $coursecontext->id) { $errors['modassoc'] = get_string('onlyassociateonecourse', 'blog'); diff --git a/blog/external_blog_edit.php b/blog/external_blog_edit.php index b28860a4d39..4798d8848c2 100644 --- a/blog/external_blog_edit.php +++ b/blog/external_blog_edit.php @@ -1,5 +1,4 @@ useexternalblogs is off, $CFG->maxexternalblogsperuser == 0, or if user doesn't have caps to manage external blogs +// TODO redirect if $CFG->useexternalblogs is off, +// $CFG->maxexternalblogsperuser == 0, +// or if user doesn't have caps to manage external blogs. $id = optional_param('id', null, PARAM_INT); $url = new moodle_url('/blog/external_blog_edit.php'); @@ -52,24 +53,24 @@ $action = (empty($id)) ? 'add' : 'edit'; $external = new stdClass(); -// Check that this id exists +// Check that this id exists. if (!empty($id) && !$DB->record_exists('blog_external', array('id' => $id))) { print_error('wrongexternalid', 'blog'); -} elseif (!empty($id)) { +} else if (!empty($id)) { $external = $DB->get_record('blog_external', array('id' => $id)); } $strformheading = ($action == 'edit') ? get_string('editexternalblog', 'blog') : get_string('addnewexternalblog', 'blog'); -$strexternalblogs = get_string('externalblogs','blog'); -$strblogs = get_string('blogs','blog'); +$strexternalblogs = get_string('externalblogs', 'blog'); +$strblogs = get_string('blogs', 'blog'); $externalblogform = new blog_edit_external_form(); -if ($externalblogform->is_cancelled()){ +if ($externalblogform->is_cancelled()) { redirect($returnurl); } else if ($data = $externalblogform->get_data()) { - //save stuff in db + // Save stuff in db. switch ($action) { case 'add': $rss = new moodle_simplepie($data->url); diff --git a/blog/external_blog_edit_form.php b/blog/external_blog_edit_form.php index 696e79af3c6..576feb1c23b 100644 --- a/blog/external_blog_edit_form.php +++ b/blog/external_blog_edit_form.php @@ -1,5 +1,4 @@ libdir.'/formslib.php'); @@ -120,7 +119,7 @@ class blog_edit_external_form extends moodleform { $mform->freeze('url'); $mform->freeze('filtertags'); // TODO change the filtertags element to a multiple select, using the tags of the external blog - // Use $rss->get_channel_tags() + // Use $rss->get_channel_tags(). } } } diff --git a/blog/external_blogs.php b/blog/external_blogs.php index 57af11a057b..800c85c6a31 100644 --- a/blog/external_blogs.php +++ b/blog/external_blogs.php @@ -1,5 +1,4 @@ get_field('blog_external', 'userid', array('id' => $delete)); if ($externalbloguserid == $USER->id) { - // Delete the external blog + // Delete the external blog. $DB->delete_records('blog_external', array('id' => $delete)); - // Delete the external blog's posts + // Delete the external blog's posts. $deletewhere = 'module = :module AND userid = :userid AND ' . $DB->sql_isnotempty('post', 'uniquehash', false, false) . ' AND ' . $DB->sql_compare_text('content') . ' = ' . $DB->sql_compare_text(':delete'); - $DB->delete_records_select('post', $deletewhere, array('module' => 'blog_external', 'userid' => $USER->id, 'delete' => $delete)); + $DB->delete_records_select('post', $deletewhere, array('module' => 'blog_external', + 'userid' => $USER->id, + 'delete' => $delete)); $message = get_string('externalblogdeleted', 'blog'); } @@ -77,7 +78,11 @@ if (!empty($blogs)) { $table = new html_table(); $table->cellpadding = 4; $table->attributes['class'] = 'generaltable boxaligncenter'; - $table->head = array(get_string('name'), get_string('url', 'blog'), get_string('timefetched', 'blog'), get_string('valid', 'blog'), get_string('actions')); + $table->head = array(get_string('name'), + get_string('url', 'blog'), + get_string('timefetched', 'blog'), + get_string('valid', 'blog'), + get_string('actions')); foreach ($blogs as $blog) { if ($blog->failedlastsync) { @@ -92,7 +97,11 @@ if (!empty($blogs)) { $deletelink = new moodle_url('/blog/external_blogs.php', array('delete' => $blog->id, 'sesskey'=>sesskey())); $deleteicon = $OUTPUT->action_icon($deletelink, new pix_icon('t/delete', get_string('deleteexternalblog', 'blog'))); - $table->data[] = new html_table_row(array($blog->name, $blog->url, userdate($blog->timefetched), $validicon, $editicon . ' '. $deleteicon)); + $table->data[] = new html_table_row(array($blog->name, + $blog->url, + userdate($blog->timefetched), + $validicon, + $editicon . ' '. $deleteicon)); } echo html_writer::table($table); } diff --git a/blog/index.php b/blog/index.php index 4bdfd35e3ab..4c58f3451e7 100644 --- a/blog/index.php +++ b/blog/index.php @@ -1,4 +1,18 @@ . /** * file index.php @@ -34,7 +48,7 @@ foreach ($url_params as $var => $val) { } $PAGE->set_url('/blog/index.php', $url_params); -//correct tagid if a text tag is provided as a param +// Correct tagid if a text tag is provided as a param. if (!empty($tag)) { if ($tagrec = $DB->get_record('tag', array('name' => $tag))) { $tagid = $tagrec->id; @@ -47,27 +61,27 @@ $sitecontext = context_system::instance(); // Blogs are always in system context. $PAGE->set_context($sitecontext); -// check basic permissions +// Check basic permissions. if ($CFG->bloglevel == BLOG_GLOBAL_LEVEL) { - // everybody can see anything - no login required unless site is locked down using forcelogin + // Everybody can see anything - no login required unless site is locked down using forcelogin. if ($CFG->forcelogin) { require_login(); } } else if ($CFG->bloglevel == BLOG_SITE_LEVEL) { - // users must log in and can not be guests + // Users must log in and can not be guests. require_login(); if (isguestuser()) { - // they must have entered the url manually... + // They must have entered the url manually. print_error('blogdisable', 'blog'); } } else if ($CFG->bloglevel == BLOG_USER_LEVEL) { - // users can see own blogs only! with the exception of ppl with special cap + // Users can see own blogs only! with the exception of people with special cap. require_login(); } else { - // weird! + // Weird! print_error('blogdisable', 'blog'); } @@ -206,13 +220,10 @@ if ($CFG->enablerssfeeds) { } $rsstitle = $blogheaders['heading']; - //check we haven't started output by outputting an error message + // Check we haven't started output by outputting an error message. if ($PAGE->state == moodle_page::STATE_BEFORE_HEADER) { blog_rss_add_http_header($rsscontext, $rsstitle, $filtertype, $thingid, $tagid); } - - //this works but there isn't a great place to put the link - //blog_rss_print_link($rsscontext, $filtertype, $thingid, $tagid); } echo $OUTPUT->header(); diff --git a/blog/lib.php b/blog/lib.php index 9190e465881..5b2691628bd 100644 --- a/blog/lib.php +++ b/blog/lib.php @@ -1,5 +1,4 @@ dirroot .'/blog/rsslib.php'); @@ -45,11 +44,11 @@ function blog_user_can_edit_entry($blogentry) { $sitecontext = context_system::instance(); if (has_capability('moodle/blog:manageentries', $sitecontext)) { - return true; // can edit any blog entry + return true; // Can edit any blog entry. } if ($blogentry->userid == $USER->id && has_capability('moodle/blog:create', $sitecontext)) { - return true; // can edit own when having blog:create capability + return true; // Can edit own when having blog:create capability. } return false; @@ -168,12 +167,12 @@ function blog_sync_external_entries($externalblog) { if (empty($rss->data)) { return null; } - //used to identify blog posts that have been deleted from the source feed + // Used to identify blog posts that have been deleted from the source feed. $oldesttimestamp = null; $uniquehashes = array(); foreach ($rss->get_items() as $entry) { - // If filtertags are defined, use them to filter the entries by RSS category + // If filtertags are defined, use them to filter the entries by RSS category. if (!empty($externalblog->filtertags)) { $containsfiltertag = false; $categories = $entry->get_categories(); @@ -201,26 +200,26 @@ function blog_sync_external_entries($externalblog) { $newentry->uniquehash = $entry->get_permalink(); $newentry->publishstate = 'site'; $newentry->format = FORMAT_HTML; - // Clean subject of html, just in case + // Clean subject of html, just in case. $newentry->subject = clean_param($entry->get_title(), PARAM_TEXT); - // Observe 128 max chars in DB - // TODO: +1 to raise this to 255 + // Observe 128 max chars in DB. + // TODO: +1 to raise this to 255. if (core_text::strlen($newentry->subject) > 128) { $newentry->subject = core_text::substr($newentry->subject, 0, 125) . '...'; } $newentry->summary = $entry->get_description(); - //used to decide whether to insert or update - //uses enty permalink plus creation date if available + // Used to decide whether to insert or update. + // Uses enty permalink plus creation date if available. $existingpostconditions = array('uniquehash' => $entry->get_permalink()); - //our DB doesnt allow null creation or modified timestamps so check the external blog supplied one + // Our DB doesnt allow null creation or modified timestamps so check the external blog supplied one. $entrydate = $entry->get_date('U'); if (!empty($entrydate)) { $existingpostconditions['created'] = $entrydate; } - //the post ID or false if post not found in DB + // The post ID or false if post not found in DB. $postid = $DB->get_field('post', 'id', $existingpostconditions); $timestamp = null; @@ -230,14 +229,14 @@ function blog_sync_external_entries($externalblog) { $timestamp = $entrydate; } - //only set created if its a new post so we retain the original creation timestamp if the post is edited + // Only set created if its a new post so we retain the original creation timestamp if the post is edited. if ($postid === false) { $newentry->created = $timestamp; } $newentry->lastmodified = $timestamp; if (empty($oldesttimestamp) || $timestamp < $oldesttimestamp) { - //found an older post + // Found an older post. $oldesttimestamp = $timestamp; } @@ -252,7 +251,7 @@ function blog_sync_external_entries($externalblog) { if ($postid === false) { $id = $DB->insert_record('post', $newentry); - // Set tags + // Set tags. if ($tags = tag_get_tags_array('blog_external', $externalblog->id)) { tag_set('post', $id, $tags, 'core', context_user::instance($externalblog->userid)->id); } @@ -273,7 +272,7 @@ function blog_sync_external_entries($externalblog) { $dbposts = $DB->get_records_sql($sql, array('blogid' => $externalblog->id, 'ts' => $oldesttimestamp)); $todelete = array(); - foreach($dbposts as $dbpost) { + foreach ($dbposts as $dbpost) { if ( !in_array($dbpost->uniquehash, $uniquehashes) ) { $todelete[] = $dbpost->id; } @@ -324,13 +323,13 @@ function blog_get_all_options(moodle_page $page, stdClass $userid = null) { $options = array(); - // If blogs are enabled and the user is logged in and not a guest + // 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 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 + // 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); @@ -339,33 +338,36 @@ function blog_get_all_options(moodle_page $page, stdClass $userid = null) { } if ($CFG->useblogassociations && $page->cm !== null) { - // Load for the module associated with the page + // 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 + // 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 + // Get the options for the user. if ($user !== null and !isguestuser($user)) { - // Load for the requested user + // Load for the requested user. $options[CONTEXT_USER+1] = blog_get_options_for_user($user); } - // Load for the current user + // Load for the current user. if (isloggedin() and !isguestuser()) { $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->enableblogs) && $CFG->bloglevel >= BLOG_GLOBAL_LEVEL && has_capability('moodle/blog:view', context_system::instance())) { + // 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())) { + $options[CONTEXT_SYSTEM] = array('viewsite' => array( 'string' => get_string('viewsiteentries', 'blog'), 'link' => new moodle_url('/blog/index.php') )); } - // Return the options + // Return the options. return $options; } @@ -380,16 +382,16 @@ function blog_get_all_options(moodle_page $page, stdClass $userid = null) { */ function blog_get_options_for_user(stdClass $user=null) { global $CFG, $USER; - // Cache + // Cache. static $useroptions = array(); $options = array(); - // Blogs must be enabled and the user must be logged in + // Blogs must be enabled and the user must be logged in. if (!blog_is_enabled_for_user()) { return $options; } - // Sort out the user var + // Sort out the user var. if ($user === null || $user->id == $USER->id) { $user = $USER; $iscurrentuser = true; @@ -397,7 +399,7 @@ function blog_get_options_for_user(stdClass $user=null) { $iscurrentuser = false; } - // If we've already generated serve from the cache + // If we've already generated serve from the cache. if (array_key_exists($user->id, $useroptions)) { return $useroptions[$user->id]; } @@ -406,22 +408,22 @@ function blog_get_options_for_user(stdClass $user=null) { $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 + // 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 + // It's the current user. if ($canview) { - // We can view our own blogs .... BIG surprise + // 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 + // 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')) @@ -432,12 +434,12 @@ function blog_get_options_for_user(stdClass $user=null) { $options['rss'] = array( 'string' => get_string('rssfeed', 'blog'), 'link' => new moodle_url(rss_get_url($sitecontext->id, $USER->id, 'blog', 'user/'.$user->id)) - ); + ); } - // Cache the options + // Cache the options. $useroptions[$user->id] = $options; - // Return the options + // Return the options. return $options; } @@ -451,47 +453,46 @@ function blog_get_options_for_user(stdClass $user=null) { */ function blog_get_options_for_course(stdClass $course, stdClass $user=null) { global $CFG, $USER; - // Cache + // Cache. static $courseoptions = array(); $options = array(); - // User must be logged in and blogs must be enabled + // 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 + // Check that the user can associate with the course. $sitecontext = context_system::instance(); - // Generate the cache key + // 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 + // Serve from the cache if we've already generated for this course. if (array_key_exists($key, $courseoptions)) { return $courseoptions[$key]; } - if (has_capability('moodle/blog:view', $sitecontext)) { // We can view! if ($CFG->bloglevel >= BLOG_SITE_LEVEL) { - // View entries about this course + // 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 + // 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 + // 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)) @@ -500,17 +501,16 @@ function blog_get_options_for_course(stdClass $course, stdClass $user=null) { } if (has_capability('moodle/blog:create', $sitecontext)) { - // We can blog about this course + // We can blog about this course. $options['courseadd'] = array( 'string' => get_string('blogaboutthiscourse', 'blog'), 'link' => new moodle_url('/blog/edit.php', array('action' => 'add', 'courseid' => $course->id)) ); } - - // Cache the options for this course + // Cache the options for this course. $courseoptions[$key] = $options; - // Return the options + // Return the options. return $options; } @@ -524,18 +524,18 @@ function blog_get_options_for_course(stdClass $course, stdClass $user=null) { */ function blog_get_options_for_module($module, $user=null) { global $CFG, $USER; - // Cache + // Cache. static $moduleoptions = array(); $options = array(); - // User must be logged in, blogs must be enabled + // User must be logged in, blogs must be enabled. if (!blog_is_enabled_for_user()) { return $options; } $sitecontext = context_system::instance(); - // Generate the cache key + // Generate the cache key. $key = $module->id.':'; if (!empty($user)) { $key .= $user->id; @@ -543,18 +543,17 @@ function blog_get_options_for_module($module, $user=null) { $key .= $USER->id; } if (array_key_exists($key, $moduleoptions)) { - // Serve from the cache so we don't have to regenerate + // Serve from the cache so we don't have to regenerate. return $moduleoptions[$key]; } - if (has_capability('moodle/blog:view', $sitecontext)) { // Save correct module name for later usage. $modulename = get_string('modulename', $module->modname); // We can view! if ($CFG->bloglevel >= BLOG_SITE_LEVEL) { - // View all entries about this module + // View all entries about this module. $a = new stdClass; $a->type = $modulename; $options['moduleview'] = array( @@ -562,13 +561,13 @@ function blog_get_options_for_module($module, $user=null) { 'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id)) ); } - // View MY entries about this module + // View MY entries about this module. $options['moduleviewmine'] = array( 'string' => get_string('viewmyentriesaboutmodule', 'blog', $modulename), '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 + // View the given users entries about this module. $a = new stdClass; $a->mod = $modulename; $a->user = fullname($user); @@ -580,15 +579,15 @@ function blog_get_options_for_module($module, $user=null) { } if (has_capability('moodle/blog:create', $sitecontext)) { - // The user can blog about this module + // The user can blog about this module. $options['moduleadd'] = array( 'string' => get_string('blogaboutthismodule', 'blog', $modulename), 'link' => new moodle_url('/blog/edit.php', array('action'=>'add', 'modid'=>$module->id)) ); } - // Cache the options + // Cache the options. $moduleoptions[$key] = $options; - // Return the options + // Return the options. return $options; } @@ -626,7 +625,7 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu $action = optional_param('action', null, PARAM_ALPHA); $confirm = optional_param('confirm', false, PARAM_BOOL); - // Ignore userid when action == add + // Ignore userid when action == add. if ($action == 'add' && $userid) { unset($userid); $PAGE->url->remove_params(array('userid')); @@ -644,11 +643,11 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu $site = $DB->get_record('course', array('id' => SITEID)); $sitecontext = context_system::instance(); - // Common Lang strings + // Common Lang strings. $strparticipants = get_string("participants"); $strblogentries = get_string("blogentries", 'blog'); - // Prepare record objects as needed + // Prepare record objects as needed. if (!empty($courseid)) { $headers['filters']['course'] = $courseid; $course = $DB->get_record('course', array('id' => $courseid)); @@ -659,7 +658,7 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu $user = $DB->get_record('user', array('id' => $userid)); } - if (!empty($groupid)) { // groupid always overrides courseid + if (!empty($groupid)) { // The groupid always overrides courseid. $headers['filters']['group'] = $groupid; $group = $DB->get_record('groups', array('id' => $groupid)); $course = $DB->get_record('course', array('id' => $group->courseid)); @@ -667,11 +666,11 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu $PAGE->set_pagelayout('standard'); - // modid always overrides courseid, so the $course object may be reset here + // The modid always overrides courseid, so the $course object may be reset here. if (!empty($modid) && $CFG->useblogassociations) { $headers['filters']['module'] = $modid; - // A groupid param may conflict with this coursemod's courseid. Ignore groupid in that case + // A groupid param may conflict with this coursemod's courseid. Ignore groupid in that case. $courseid = $DB->get_field('course_modules', 'course', array('id'=>$modid)); $course = $DB->get_record('course', array('id' => $courseid)); $cm = $DB->get_record('course_modules', array('id' => $modid)); @@ -685,17 +684,16 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu } // Case 1: No entry, mod, course or user params: all site entries to be shown (filtered by search and tag/tagid) - // Note: if action is set to 'add' or 'edit', we do this at the end + // Note: if action is set to 'add' or 'edit', we do this at the end. if (empty($entryid) && empty($modid) && empty($courseid) && empty($userid) && !in_array($action, array('edit', 'add'))) { $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID))); $PAGE->navbar->add($strblogentries, $blogurl); $PAGE->set_title("$shortname: " . get_string('blog', 'blog')); $PAGE->set_heading("$shortname: " . get_string('blog', 'blog')); $headers['heading'] = get_string('siteblog', 'blog', $shortname); - // $headers['strview'] = get_string('viewsiteentries', 'blog'); } - // Case 2: only entryid is requested, ignore all other filters. courseid is used to give more contextual information + // Case 2: only entryid is requested, ignore all other filters. courseid is used to give more contextual information. if (!empty($entryid)) { $headers['filters']['entry'] = $entryid; $sql = 'SELECT u.* FROM {user} u, {post} p WHERE p.id = ? AND p.userid = u.id'; @@ -720,15 +718,16 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu $PAGE->set_heading("$shortname: " . fullname($user) . ": $entry->subject"); $headers['heading'] = get_string('blogentrybyuser', 'blog', fullname($user)); - // We ignore tag and search params + // We ignore tag and search params. if (empty($action) || !$CFG->useblogassociations) { $headers['url'] = $blogurl; return $headers; } } - // Case 3: A user's blog entries if (!empty($userid) && empty($entryid) && ((empty($courseid) && empty($modid)) || !$CFG->useblogassociations)) { + // Case 3: A user's blog entries. + $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID))); $blogurl->param('userid', $userid); $PAGE->set_title("$shortname: " . fullname($user) . ": " . get_string('blog', 'blog')); @@ -736,23 +735,21 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu $headers['heading'] = get_string('userblog', 'blog', fullname($user)); $headers['strview'] = get_string('viewuserentries', 'blog', fullname($user)); - } else + } else if (!$CFG->useblogassociations && empty($userid) && !in_array($action, array('edit', 'add'))) { + // Case 4: No blog associations, no userid. - // Case 4: No blog associations, no userid - if (!$CFG->useblogassociations && empty($userid) && !in_array($action, array('edit', 'add'))) { $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID))); $PAGE->set_title("$shortname: " . get_string('blog', 'blog')); $PAGE->set_heading("$shortname: " . get_string('blog', 'blog')); $headers['heading'] = get_string('siteblog', 'blog', $shortname); - } else + } else if (!empty($userid) && !empty($modid) && empty($entryid)) { + // Case 5: Blog entries associated with an activity by a specific user (courseid ignored). - // Case 5: Blog entries associated with an activity by a specific user (courseid ignored) - if (!empty($userid) && !empty($modid) && empty($entryid)) { $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID))); $blogurl->param('userid', $userid); $blogurl->param('modid', $modid); - // Course module navigation is handled by build_navigation as the second param + // Course module navigation is handled by build_navigation as the second param. $headers['cm'] = $cm; $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id"); $PAGE->navbar->add($strblogentries, $blogurl); @@ -767,10 +764,9 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu $headers['heading'] = get_string('blogentriesbyuseraboutmodule', 'blog', $a); $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); $headers['strview'] = get_string('viewallmodentries', 'blog', $a); - } else + } else if (!empty($userid) && !empty($courseid) && empty($modid) && empty($entryid)) { + // Case 6: Blog entries associated with a course by a specific user. - // Case 6: Blog entries associated with a course by a specific user - if (!empty($userid) && !empty($courseid) && empty($modid) && empty($entryid)) { $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))); $blogurl->param('userid', $userid); @@ -789,12 +785,11 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); $headers['strview'] = get_string('viewblogentries', 'blog', $a); - // Remove the userid from the URL to inform the blog_menu block correctly + // Remove the userid from the URL to inform the blog_menu block correctly. $blogurl->remove_params(array('userid')); - } else + } else if (!empty($groupid) && empty($modid) && empty($entryid)) { + // Case 7: Blog entries by members of a group, associated with that group's course. - // Case 7: Blog entries by members of a group, associated with that group's course - if (!empty($groupid) && empty($modid) && empty($entryid)) { $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))); $blogurl->param('courseid', $course->id); @@ -814,10 +809,9 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu $headers['heading'] = get_string('blogentriesbygroupaboutcourse', 'blog', $a); $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); $headers['strview'] = get_string('viewblogentries', 'blog', $a); - } else + } else if (!empty($groupid) && !empty($modid) && empty($entryid)) { + // Case 8: Blog entries by members of a group, associated with an activity in that course. - // Case 8: Blog entries by members of a group, associated with an activity in that course - if (!empty($groupid) && !empty($modid) && empty($entryid)) { $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))); $headers['cm'] = $cm; @@ -838,10 +832,9 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); $headers['strview'] = get_string('viewallmodentries', 'blog', $a); - } else + } else if (!empty($modid) && empty($userid) && empty($groupid) && empty($entryid)) { + // Case 9: All blog entries associated with an activity. - // Case 9: All blog entries associated with an activity - if (!empty($modid) && empty($userid) && empty($groupid) && empty($entryid)) { $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))); $PAGE->set_cm($cm, $course); @@ -854,10 +847,9 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu $a->type = get_string('modulename', $cm->modname); $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); $headers['strview'] = get_string('viewallmodentries', 'blog', $a); - } else + } else if (!empty($courseid) && empty($userid) && empty($groupid) && empty($modid) && empty($entryid)) { + // Case 10: All blog entries associated with a course. - // Case 10: All blog entries associated with a course - if (!empty($courseid) && empty($userid) && empty($groupid) && empty($modid) && empty($entryid)) { $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))); $blogurl->param('courseid', $courseid); @@ -866,20 +858,24 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu $PAGE->set_heading("$siteshortname: $courseshortname: " . get_string('blogentries', 'blog')); $a = new stdClass(); $a->type = get_string('course'); - $headers['heading'] = get_string('blogentriesabout', 'blog', format_string($course->fullname, true, array('context' => context_course::instance($course->id)))); + $headers['heading'] = get_string('blogentriesabout', + 'blog', + format_string($course->fullname, + true, + array('context' => context_course::instance($course->id)))); $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'))) { - // Append Tag info + // Append Tag info. 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); - } elseif (!empty($tag)) { + } else if (!empty($tag)) { if ($tagrec = $DB->get_record('tag', array('name' => $tag))) { $tagid = $tagrec->id; $headers['filters']['tag'] = $tagid; @@ -888,7 +884,7 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu } } - // Append Search info + // Append Search info. if (!empty($search)) { $headers['filters']['search'] = $search; $blogurl->param('search', $search); @@ -896,7 +892,7 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu } } - // Append edit mode info + // Append edit mode info. if (!empty($action) && $action == 'add') { } else if (!empty($action) && $action == 'edit') { diff --git a/blog/locallib.php b/blog/locallib.php index 556105006c5..5df39ad929f 100644 --- a/blog/locallib.php +++ b/blog/locallib.php @@ -1,5 +1,4 @@ libdir . '/filelib.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class blog_entry implements renderable { - // Public Database fields + // Public Database fields. public $id; public $userid; public $subject; @@ -49,7 +48,7 @@ class blog_entry implements renderable { public $attachment; public $publishstate; - // Locked Database fields (Don't touch these) + // Locked Database fields (Don't touch these). public $courseid = 0; public $groupid = 0; public $module = 'blog'; @@ -62,14 +61,13 @@ class blog_entry implements renderable { public $created; public $usermodified; - // Other class variables + // Other class variables. public $form; public $tags = array(); /** @var StdClass Data needed to render the entry */ public $renderable; - // Methods /** * Constructor. If given an id, will fetch the corresponding record from the DB. * @@ -137,11 +135,12 @@ class blog_entry implements renderable { if ($externalblog = $DB->get_record('blog_external', array('id' => $this->content))) { $urlparts = parse_url($externalblog->url); $this->renderable->externalblogtext = get_string('retrievedfrom', 'blog') . get_string('labelsep', 'langconfig'); - $this->renderable->externalblogtext .= html_writer::link($urlparts['scheme'] . '://'.$urlparts['host'], $externalblog->name); + $this->renderable->externalblogtext .= html_writer::link($urlparts['scheme'] . '://' . $urlparts['host'], + $externalblog->name); } } - // Retrieve associations + // Retrieve associations. $this->renderable->unassociatedentry = false; if (!empty($CFG->useblogassociations)) { @@ -165,9 +164,11 @@ class blog_entry implements renderable { // Course associations. if ($context->contextlevel == CONTEXT_COURSE) { - $instancename = $DB->get_field('course', 'shortname', array('id' => $context->instanceid)); //TODO: performance!!!! + // TODO: performance!!!! + $instancename = $DB->get_field('course', 'shortname', array('id' => $context->instanceid)); - $associations[$key]->url = $assocurl = new moodle_url('/course/view.php', array('id' => $context->instanceid)); + $associations[$key]->url = $assocurl = new moodle_url('/course/view.php', + array('id' => $context->instanceid)); $associations[$key]->text = $instancename; $associations[$key]->icon = new pix_icon('i/course', $associations[$key]->text); } @@ -175,15 +176,17 @@ class blog_entry implements renderable { // Mod associations. if ($context->contextlevel == CONTEXT_MODULE) { - // Getting the activity type and the activity instance id + // Getting the activity type and the activity instance id. $sql = 'SELECT cm.instance, m.name FROM {course_modules} cm JOIN {modules} m ON m.id = cm.module WHERE cm.id = :cmid'; $modinfo = $DB->get_record_sql($sql, array('cmid' => $context->instanceid)); - $instancename = $DB->get_field($modinfo->name, 'name', array('id' => $modinfo->instance)); //TODO: performance!!!! + // TODO: performance!!!! + $instancename = $DB->get_field($modinfo->name, 'name', array('id' => $modinfo->instance)); $associations[$key]->type = get_string('modulename', $modinfo->name); - $associations[$key]->url = new moodle_url('/mod/' . $modinfo->name . '/view.php', array('id' => $context->instanceid)); + $associations[$key]->url = new moodle_url('/mod/' . $modinfo->name . '/view.php', + array('id' => $context->instanceid)); $associations[$key]->text = $instancename; $associations[$key]->icon = new pix_icon('icon', $associations[$key]->text, $modinfo->name); } @@ -203,7 +206,7 @@ class blog_entry implements renderable { * Gets the entry attachments list * @return array List of blog_entry_attachment instances */ - function get_attachments() { + public function get_attachments() { global $CFG; @@ -293,7 +296,13 @@ class blog_entry implements renderable { } $entry = file_postupdate_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id); - $entry = file_postupdate_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog', 'attachment', $entry->id); + $entry = file_postupdate_standard_filemanager($entry, + 'attachment', + $attachmentoptions, + $sitecontext, + 'blog', + 'attachment', + $entry->id); if (!empty($CFG->useblogassociations)) { $entry->add_associations(); @@ -427,7 +436,7 @@ class blog_entry implements renderable { if ($otags = optional_param('otags', '', PARAM_INT)) { foreach ($otags as $tagid) { - // TODO : make this use the tag name in the form + // TODO : make this use the tag name in the form. if ($tag = tag_get('id', $tagid)) { $tags[] = $tag->name; } @@ -456,11 +465,11 @@ class blog_entry implements renderable { $sitecontext = context_system::instance(); if (has_capability('moodle/blog:manageentries', $sitecontext)) { - return true; // can edit any blog entry + return true; // Can edit any blog entry. } if ($this->userid == $userid && has_capability('moodle/blog:create', $sitecontext)) { - return true; // can edit own when having blog:create capability + return true; // Can edit own when having blog:create capability. } return false; @@ -480,23 +489,23 @@ class blog_entry implements renderable { $sitecontext = context_system::instance(); if (empty($CFG->enableblogs) || !has_capability('moodle/blog:view', $sitecontext)) { - return false; // blog system disabled or user has no blog view capability + return false; // Blog system disabled or user has no blog view capability. } if (isloggedin() && $USER->id == $targetuserid) { - return true; // can view own entries in any case + return true; // Can view own entries in any case. } if (has_capability('moodle/blog:manageentries', $sitecontext)) { - return true; // can manage all entries + return true; // Can manage all entries. } - // coming for 1 entry, make sure it's not a draft + // Coming for 1 entry, make sure it's not a draft. if ($this->publishstate == 'draft' && !has_capability('moodle/blog:viewdrafts', $sitecontext)) { - return false; // can not view draft of others + return false; // Can not view draft of others. } - // coming for 1 entry, make sure user is logged in, if not a public blog + // Coming for 1 entry, make sure user is logged in, if not a public blog. if ($this->publishstate != 'public' && !isloggedin()) { return false; } @@ -507,7 +516,7 @@ class blog_entry implements renderable { break; case BLOG_SITE_LEVEL: - if (isloggedin()) { // not logged in viewers forbidden + if (isloggedin()) { // Not logged in viewers forbidden. return true; } return false; @@ -533,7 +542,7 @@ class blog_entry implements renderable { global $CFG; $options = array(); - // everyone gets draft access + // Everyone gets draft access. if ($CFG->bloglevel >= BLOG_USER_LEVEL) { $options['draft'] = get_string('publishtonoone', 'blog'); } @@ -577,7 +586,7 @@ class blog_listing { * @param array $filters An associative array of filtername => filterid */ public function __construct($filters=array()) { - // Unset filters overridden by more specific filters + // Unset filters overridden by more specific filters. foreach ($filters as $type => $id) { if (!empty($type) && !empty($id)) { $this->filters[$type] = blog_filter::get_instance($id, $type); @@ -615,50 +624,53 @@ class blog_listing { public function get_entry_fetch_sql($count=false, $sort='lastmodified DESC', $userid = false) { global $DB, $USER, $CFG; - if(!$userid) { + if (!$userid) { $userid = $USER->id; } $allnamefields = get_all_user_name_fields(true, 'u'); // The query used to locate blog entries is complicated. It will be built from the following components: - $requiredfields = "p.*, $allnamefields, u.email"; // the SELECT clause - $tables = array('p' => 'post', 'u' => 'user'); // components of the FROM clause (table_id => table_name) - $conditions = array('u.deleted = 0', 'p.userid = u.id', '(p.module = \'blog\' OR p.module = \'blog_external\')'); // components of the WHERE clause (conjunction) + $requiredfields = "p.*, $allnamefields, u.email"; // The SELECT clause. + $tables = array('p' => 'post', 'u' => 'user'); // Components of the FROM clause (table_id => table_name). + // Components of the WHERE clause (conjunction). + $conditions = array('u.deleted = 0', 'p.userid = u.id', '(p.module = \'blog\' OR p.module = \'blog_external\')'); - // build up a clause for permission constraints + // Build up a clause for permission constraints. $params = array(); - // fix for MDL-9165, use with readuserblogs capability in a user context can read that user's private blogs - // admins can see all blogs regardless of publish states, as described on the help page + // Fix for MDL-9165, use with readuserblogs capability in a user context can read that user's private blogs. + // Admins can see all blogs regardless of publish states, as described on the help page. if (has_capability('moodle/user:readuserblogs', context_system::instance())) { - // don't add permission constraints + // Don't add permission constraints. - } else if(!empty($this->filters['user']) && has_capability('moodle/user:readuserblogs', - context_user::instance((empty($this->filters['user']->id) ? 0 : $this->filters['user']->id)))) { - // don't add permission constraints + } else if (!empty($this->filters['user']) + && has_capability('moodle/user:readuserblogs', + context_user::instance((empty($this->filters['user']->id) ? 0 : $this->filters['user']->id)))) { + // Don't add permission constraints. } else { if (isloggedin() and !isguestuser()) { - $assocexists = $DB->record_exists('blog_association', array()); //dont check association records if there aren't any + // Dont check association records if there aren't any. + $assocexists = $DB->record_exists('blog_association', array()); - //begin permission sql clause + // Begin permission sql clause. $permissionsql = '(p.userid = ? '; $params[] = $userid; - if ($CFG->bloglevel >= BLOG_SITE_LEVEL) { // add permission to view site-level entries + if ($CFG->bloglevel >= BLOG_SITE_LEVEL) { // Add permission to view site-level entries. $permissionsql .= " OR p.publishstate = 'site' "; } - if ($CFG->bloglevel >= BLOG_GLOBAL_LEVEL) { // add permission to view global entries + if ($CFG->bloglevel >= BLOG_GLOBAL_LEVEL) { // Add permission to view global entries. $permissionsql .= " OR p.publishstate = 'public' "; } - $permissionsql .= ') '; //close permissions sql clause - } else { // default is access to public entries + $permissionsql .= ') '; // Close permissions sql clause. + } else { // Default is access to public entries. $permissionsql = "p.publishstate = 'public'"; } - $conditions[] = $permissionsql; //add permission constraints + $conditions[] = $permissionsql; // Add permission constraints. } foreach ($this->filters as $type => $blogfilter) { @@ -667,7 +679,7 @@ class blog_listing { $tables = array_merge($tables, $blogfilter->tables); } - $tablessql = ''; // build up the FROM clause + $tablessql = ''; // Build up the FROM clause. foreach ($tables as $tablename => $table) { $tablessql .= ($tablessql ? ', ' : '').'{'.$table.'} '.$tablename; } @@ -688,7 +700,7 @@ class blog_listing { global $CFG, $USER, $DB, $OUTPUT, $PAGE; $sitecontext = context_system::instance(); - // Blog renderer + // Blog renderer. $output = $PAGE->get_renderer('blog'); $page = optional_param('blogpage', 0, PARAM_INT); @@ -711,7 +723,7 @@ class blog_listing { echo $OUTPUT->render($pagingbar); if (has_capability('moodle/blog:create', $sitecontext)) { - //the user's blog is enabled and they are viewing their own blog + // The user's blog is enabled and they are viewing their own blog. $userid = optional_param('userid', null, PARAM_INT); if (empty($userid) || (!empty($userid) && $userid == $USER->id)) { @@ -744,7 +756,7 @@ class blog_listing { foreach ($entries as $entry) { $blogentry = new blog_entry(null, $entry); - // Get the required blog entry data to render it + // Get the required blog entry data to render it. $blogentry->prepare_render(); echo $output->render($blogentry); @@ -762,7 +774,7 @@ class blog_listing { } } - /// Find the base url from $_GET variables, for print_paging_bar + // Find the base url from $_GET variables, for print_paging_bar. public function get_baseurl() { $getcopy = $_GET; @@ -897,11 +909,13 @@ class blog_filter_context extends blog_filter { $this->type = $type; } - $this->availabletypes = array('site' => get_string('site'), 'course' => get_string('course'), 'module' => get_string('activity')); + $this->availabletypes = array('site' => get_string('site'), + 'course' => get_string('course'), + 'module' => get_string('activity')); switch ($this->type) { case 'course': // Careful of site course! - // Ignore course filter if blog associations are not enabled + // Ignore course filter if blog associations are not enabled. if ($this->id != $SITE->id && !empty($CFG->useblogassociations)) { $this->overrides = array('site'); $context = context_course::instance($this->id); @@ -910,11 +924,11 @@ class blog_filter_context extends blog_filter { $this->conditions[] = 'ba.contextid = '.$context->id; break; } else { - // We are dealing with the site course, do not break from the current case + // We are dealing with the site course, do not break from the current case. } case 'site': - // No special constraints + // No special constraints. break; case 'module': if (!empty($CFG->useblogassociations)) { @@ -961,7 +975,7 @@ class blog_filter_user extends blog_filter { $this->params = array($this->id); $this->overrides = array('group'); - } elseif ($this->type == 'group') { + } else if ($this->type == 'group') { $this->overrides = array('course', 'site'); $this->tables['gm'] = 'groups_members'; @@ -969,7 +983,7 @@ class blog_filter_user extends blog_filter { $this->conditions[] = 'gm.groupid = ?'; $this->params[] = $this->id; - if (!empty($CFG->useblogassociations)) { // only show blog entries associated with this course + if (!empty($CFG->useblogassociations)) { // Only show blog entries associated with this course. $coursecontext = context_course::instance($DB->get_field('groups', 'courseid', array('id' => $this->id))); $this->tables['ba'] = 'blog_association'; $this->conditions[] = 'gm.groupid = ?'; @@ -1068,7 +1082,8 @@ class blog_entry_attachment implements renderable { $this->file = $file; $this->filename = $file->get_filename(); - $this->url = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.SYSCONTEXTID.'/blog/attachment/'.$entryid.'/'.$this->filename); + $this->url = file_encode_url($CFG->wwwroot . '/pluginfile.php', + '/' . SYSCONTEXTID . '/blog/attachment/' . $entryid . '/' . $this->filename); } } diff --git a/blog/preferences.php b/blog/preferences.php index e3d82fc2c0d..59aea44dfac 100644 --- a/blog/preferences.php +++ b/blog/preferences.php @@ -1,5 +1,4 @@ enableblogs)) { // The preference is site wide not blog specific. Hence user should have permissions in site level. require_capability('moodle/blog:view', $sitecontext); -/// If data submitted, then process and store. +// If data submitted, then process and store. $mform = new blog_preferences_form('preferences.php'); $mform->set_data(array('pagesize' => get_user_preferences('blogpagesize'))); @@ -80,7 +79,7 @@ if (!$mform->is_cancelled() && $data = $mform->get_data()) { set_user_preference('blogpagesize', $pagesize); } -if ($mform->is_cancelled()){ +if ($mform->is_cancelled()) { redirect($CFG->wwwroot . '/blog/index.php'); } diff --git a/blog/preferences_form.php b/blog/preferences_form.php index d5c5005befc..12edfed9d66 100644 --- a/blog/preferences_form.php +++ b/blog/preferences_form.php @@ -1,5 +1,4 @@ libdir.'/formslib.php'); diff --git a/blog/renderer.php b/blog/renderer.php index af1724de1ab..832d4b12812 100644 --- a/blog/renderer.php +++ b/blog/renderer.php @@ -63,7 +63,9 @@ class core_blog_renderer extends plugin_renderer_base { $o .= $this->output->container_start('topic starter header clearfix'); // Title. - $titlelink = html_writer::link(new moodle_url('/blog/index.php', array('entryid' => $entry->id)), format_string($entry->subject)); + $titlelink = html_writer::link(new moodle_url('/blog/index.php', + array('entryid' => $entry->id)), + format_string($entry->subject)); $o .= $this->output->container($titlelink, 'subject'); // Post by. @@ -244,9 +246,14 @@ class core_blog_renderer extends plugin_renderer_base { $o = html_writer::empty_tag('img', $attrs); $class = 'attachedimages'; } else { - $image = $this->output->pix_icon(file_file_icon($attachment->file), $attachment->filename, 'moodle', array('class'=>'icon')); + $image = $this->output->pix_icon(file_file_icon($attachment->file), + $attachment->filename, + 'moodle', + array('class' => 'icon')); $o = html_writer::link($attachment->url, $image); - $o .= format_text(html_writer::link($attachment->url, $attachment->filename), FORMAT_HTML, array('context' => $syscontext)); + $o .= format_text(html_writer::link($attachment->url, $attachment->filename), + FORMAT_HTML, + array('context' => $syscontext)); $class = 'attachments'; } diff --git a/blog/rsslib.php b/blog/rsslib.php index 12854d6a2e8..3f26eb0f8aa 100644 --- a/blog/rsslib.php +++ b/blog/rsslib.php @@ -1,5 +1,4 @@ siteguest; } else { @@ -159,7 +155,7 @@ function blog_rss_get_feed($context, $args) { if ($CFG->bloglevel == BLOG_SITE_LEVEL) { if (isguestuser()) { - debugging(get_string('nopermissiontoshow','error')); + debugging(get_string('nopermissiontoshow', 'error')); return ''; } } @@ -170,7 +166,7 @@ function blog_rss_get_feed($context, $args) { } $type = clean_param($args[3], PARAM_ALPHA); - $id = clean_param($args[4], PARAM_INT); // could be groupid / courseid / userid depending on $type + $id = clean_param($args[4], PARAM_INT); // Could be groupid / courseid / userid depending on $type. $tagid=0; if ($args[5] != 'rss.xml') { @@ -183,14 +179,13 @@ function blog_rss_get_feed($context, $args) { if (file_exists($filename)) { if (filemtime($filename) + 3600 > time()) { - return $filename; // It's already done so we return cached version + return $filename; // It's already done so we return cached version. } } $courseid = $groupid = $userid = null; switch ($type) { case 'site': - //$siteid = $id; break; case 'course': $courseid = $id; @@ -203,26 +198,26 @@ function blog_rss_get_feed($context, $args) { break; } - // Get all the entries from the database + // Get all the entries from the database. require_once($CFG->dirroot .'/blog/locallib.php'); $blogheaders = blog_get_headers($courseid, $groupid, $userid, $tagid); $bloglisting = new blog_listing($blogheaders['filters']); $blogentries = $bloglisting->get_entries(); - // Now generate an array of RSS items + // Now generate an array of RSS items. if ($blogentries) { $items = array(); - foreach ($blogentries as $blog_entry) { - $item = NULL; - $item->author = fullname($DB->get_record('user', array('id'=>$blog_entry->userid))); // TODO: this is slow - $item->title = $blog_entry->subject; - $item->pubdate = $blog_entry->lastmodified; - $item->link = $CFG->wwwroot.'/blog/index.php?entryid='.$blog_entry->id; - $summary = file_rewrite_pluginfile_urls($blog_entry->summary, 'pluginfile.php', - $sitecontext->id, 'blog', 'post', $blog_entry->id); - $item->description = format_text($summary, $blog_entry->format); - if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_array('post', $blog_entry->id)) ) { + foreach ($blogentries as $blogentry) { + $item = null; + $item->author = fullname($DB->get_record('user', array('id' => $blogentry->userid))); // TODO: this is slow. + $item->title = $blogentry->subject; + $item->pubdate = $blogentry->lastmodified; + $item->link = $CFG->wwwroot.'/blog/index.php?entryid='.$blogentry->id; + $summary = file_rewrite_pluginfile_urls($blogentry->summary, 'pluginfile.php', + $sitecontext->id, 'blog', 'post', $blogentry->id); + $item->description = format_text($summary, $blogentry->format); + if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_array('post', $blogentry->id)) ) { if ($blogtags) { $item->tags = $blogtags; } @@ -230,12 +225,12 @@ function blog_rss_get_feed($context, $args) { } $items[] = $item; } - $articles = rss_add_items($items); /// Change structure to XML + $articles = rss_add_items($items); // Change structure to XML. } else { $articles = ''; } -/// Get header and footer information + // Get header and footer information. switch ($type) { case 'user': @@ -250,7 +245,7 @@ function blog_rss_get_feed($context, $args) { break; case 'group': $group = groups_get_group($id); - $info = $group->name; //TODO: $DB->get_field('groups', 'name', array('id'=>$id)) + $info = $group->name; // TODO: $DB->get_field('groups', 'name', array('id'=>$id)). break; default: $info = ''; @@ -261,18 +256,18 @@ function blog_rss_get_feed($context, $args) { $info .= ': '.$DB->get_field('tags', 'text', array('id'=>$tagid)); } - $header = rss_standard_header(get_string($type.'blog','blog', $info), + $header = rss_standard_header(get_string($type.'blog', 'blog', $info), $CFG->wwwroot.'/blog/index.php', - get_string('intro','blog')); + get_string('intro', 'blog')); $footer = rss_standard_footer(); // Save the XML contents to file. $rssdata = $header.$articles.$footer; - if (blog_rss_save_file($type,$id,$tagid,$rssdata)) { + if (blog_rss_save_file($type, $id, $tagid, $rssdata)) { return $filename; } else { - return false; // Couldn't find it or make it + return false; // Couldn't find it or make it. } } @@ -308,12 +303,12 @@ function blog_rss_save_file($type, $id, $tagid=0, $contents='') { $status = true; - //blog creates some additional dirs within the rss cache so make sure they all exist + // Blog creates some additional dirs within the rss cache so make sure they all exist. make_cache_directory('rss/blog'); make_cache_directory('rss/blog/'.$type); $filename = blog_rss_file_name($type, $id, $tagid); - $expandfilename = false; //we're supplying a full file path + $expandfilename = false; // We are supplying a full file path. $status = rss_save_file('blog', $filename, $contents, $expandfilename); return $status; diff --git a/blog/tests/bloglib_test.php b/blog/tests/bloglib_test.php index 2db22652c11..06b1f07d2f2 100644 --- a/blog/tests/bloglib_test.php +++ b/blog/tests/bloglib_test.php @@ -32,7 +32,7 @@ require_once($CFG->dirroot . '/blog/lib.php'); */ class core_bloglib_testcase extends advanced_testcase { - private $courseid; // To store important ids to be used in tests + private $courseid; private $cmid; private $groupid; private $userid; @@ -45,22 +45,22 @@ class core_bloglib_testcase extends advanced_testcase { $this->resetAfterTest(); - // Create default course + // Create default course. $course = $this->getDataGenerator()->create_course(array('category'=>1, 'shortname'=>'ANON')); $this->assertNotEmpty($course); $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id)); $this->assertNotEmpty($page); - // Create default group + // Create default group. $group = new stdClass(); $group->courseid = $course->id; $group->name = 'ANON'; $group->id = $DB->insert_record('groups', $group); - // Create default user + // Create default user. $user = $this->getDataGenerator()->create_user(array('username'=>'testuser', 'firstname'=>'Jimmy', 'lastname'=>'Kinnon')); - // Create default tag + // Create default tag. $tag = new stdClass(); $tag->userid = $user->id; $tag->name = 'testtagname'; @@ -68,14 +68,14 @@ class core_bloglib_testcase extends advanced_testcase { $tag->tagtype = 'official'; $tag->id = $DB->insert_record('tag', $tag); - // Create default post + // Create default post. $post = new stdClass(); $post->userid = $user->id; $post->groupid = $group->id; $post->content = 'test post content text'; $post->id = $DB->insert_record('post', $post); - // Grab important ids + // Grab important ids. $this->courseid = $course->id; $this->cmid = $page->cmid; $this->groupid = $group->id; @@ -88,7 +88,7 @@ class core_bloglib_testcase extends advanced_testcase { public function test_overrides() { global $SITE; - // Try all the filters at once: Only the entry filter is active + // Try all the filters at once: Only the entry filter is active. $filters = array('site' => $SITE->id, 'course' => $this->courseid, 'module' => $this->cmid, 'group' => $this->groupid, 'user' => $this->userid, 'tag' => $this->tagid, 'entry' => $this->postid); $blog_listing = new blog_listing($filters); @@ -100,7 +100,7 @@ class core_bloglib_testcase extends advanced_testcase { $this->assertFalse(array_key_exists('tag', $blog_listing->filters)); $this->assertTrue(array_key_exists('entry', $blog_listing->filters)); - // Again, but without the entry filter: This time, the tag, user and module filters are active + // Again, but without the entry filter: This time, the tag, user and module filters are active. $filters = array('site' => $SITE->id, 'course' => $this->courseid, 'module' => $this->cmid, 'group' => $this->groupid, 'user' => $this->userid, 'tag' => $this->postid); $blog_listing = new blog_listing($filters); @@ -111,7 +111,7 @@ class core_bloglib_testcase extends advanced_testcase { $this->assertTrue(array_key_exists('user', $blog_listing->filters)); $this->assertTrue(array_key_exists('tag', $blog_listing->filters)); - // We should get the same result by removing the 3 inactive filters: site, course and group: + // We should get the same result by removing the 3 inactive filters: site, course and group. $filters = array('module' => $this->cmid, 'user' => $this->userid, 'tag' => $this->tagid); $blog_listing = new blog_listing($filters); $this->assertFalse(array_key_exists('site', $blog_listing->filters)); @@ -128,26 +128,26 @@ class core_bloglib_testcase extends advanced_testcase { public function test_blog_get_headers_case_1() { global $CFG, $PAGE, $OUTPUT; - $blog_headers = blog_get_headers(); - $this->assertEquals($blog_headers['heading'], get_string('siteblog', 'blog', 'phpunit')); + $blogheaders = blog_get_headers(); + $this->assertEquals($blogheaders['heading'], get_string('siteblog', 'blog', 'phpunit')); } public function test_blog_get_headers_case_6() { global $CFG, $PAGE, $OUTPUT; - $blog_headers = blog_get_headers($this->courseid, NULL, $this->userid); - $this->assertNotEquals($blog_headers['heading'], ''); + $blogheaders = blog_get_headers($this->courseid, null, $this->userid); + $this->assertNotEquals($blogheaders['heading'], ''); } public function test_blog_get_headers_case_7() { global $CFG, $PAGE, $OUTPUT; - $blog_headers = blog_get_headers(NULL, $this->groupid); - $this->assertNotEquals($blog_headers['heading'], ''); + $blogheaders = blog_get_headers(null, $this->groupid); + $this->assertNotEquals($blogheaders['heading'], ''); } public function test_blog_get_headers_case_10() { global $CFG, $PAGE, $OUTPUT; - $blog_headers = blog_get_headers($this->courseid); - $this->assertNotEquals($blog_headers['heading'], ''); + $blogheaders = blog_get_headers($this->courseid); + $this->assertNotEquals($blogheaders['heading'], ''); } /** -- 2.43.0