|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 defined('MOODLE_INTERNAL') || die(); 00028 00032 require_once($CFG->dirroot .'/blog/rsslib.php'); 00033 require_once($CFG->dirroot.'/tag/lib.php'); 00034 00042 function blog_user_can_edit_entry($blogentry) { 00043 global $USER; 00044 00045 $sitecontext = get_context_instance(CONTEXT_SYSTEM); 00046 00047 if (has_capability('moodle/blog:manageentries', $sitecontext)) { 00048 return true; // can edit any blog entry 00049 } 00050 00051 if ($blogentry->userid == $USER->id && has_capability('moodle/blog:create', $sitecontext)) { 00052 return true; // can edit own when having blog:create capability 00053 } 00054 00055 return false; 00056 } 00057 00058 00064 function blog_user_can_view_user_entry($targetuserid, $blogentry=null) { 00065 global $CFG, $USER, $DB; 00066 00067 if (empty($CFG->bloglevel)) { 00068 return false; // blog system disabled 00069 } 00070 00071 if (isloggedin() && $USER->id == $targetuserid) { 00072 return true; // can view own entries in any case 00073 } 00074 00075 $sitecontext = get_context_instance(CONTEXT_SYSTEM); 00076 if (has_capability('moodle/blog:manageentries', $sitecontext)) { 00077 return true; // can manage all entries 00078 } 00079 00080 // coming for 1 entry, make sure it's not a draft 00081 if ($blogentry && $blogentry->publishstate == 'draft' && !has_capability('moodle/blog:viewdrafts', $sitecontext)) { 00082 return false; // can not view draft of others 00083 } 00084 00085 // coming for 0 entry, make sure user is logged in, if not a public blog 00086 if ($blogentry && $blogentry->publishstate != 'public' && !isloggedin()) { 00087 return false; 00088 } 00089 00090 switch ($CFG->bloglevel) { 00091 case BLOG_GLOBAL_LEVEL: 00092 return true; 00093 break; 00094 00095 case BLOG_SITE_LEVEL: 00096 if (isloggedin()) { // not logged in viewers forbidden 00097 return true; 00098 } 00099 return false; 00100 break; 00101 00102 case BLOG_USER_LEVEL: 00103 default: 00104 $personalcontext = get_context_instance(CONTEXT_USER, $targetuserid); 00105 return has_capability('moodle/user:readuserblogs', $personalcontext); 00106 break; 00107 00108 } 00109 } 00110 00115 function blog_remove_associations_for_user($userid) { 00116 global $DB; 00117 throw new coding_exception('function blog_remove_associations_for_user() is not finished'); 00118 /* 00119 $blogentries = blog_fetch_entries(array('user' => $userid), 'lasmodified DESC'); 00120 foreach ($blogentries as $entry) { 00121 if (blog_user_can_edit_entry($entry)) { 00122 blog_remove_associations_for_entry($entry->id); 00123 } 00124 } 00125 */ 00126 } 00127 00132 function blog_remove_associations_for_course($courseid) { 00133 global $DB; 00134 $context = get_context_instance(CONTEXT_COURSE, $courseid); 00135 $DB->delete_records('blog_association', array('contextid' => $context->id)); 00136 } 00137 00146 function blog_sync_external_entries($externalblog) { 00147 global $CFG, $DB; 00148 require_once($CFG->libdir . '/simplepie/moodle_simplepie.php'); 00149 00150 $rssfile = new moodle_simplepie_file($externalblog->url); 00151 $filetest = new SimplePie_Locator($rssfile); 00152 00153 $textlib = textlib_get_instance(); // Going to use textlib services 00154 00155 if (!$filetest->is_feed($rssfile)) { 00156 $externalblog->failedlastsync = 1; 00157 $DB->update_record('blog_external', $externalblog); 00158 return false; 00159 } else if (!empty($externalblog->failedlastsync)) { 00160 $externalblog->failedlastsync = 0; 00161 $DB->update_record('blog_external', $externalblog); 00162 } 00163 00164 $rss = new moodle_simplepie($externalblog->url); 00165 00166 if (empty($rss->data)) { 00167 return null; 00168 } 00169 //used to identify blog posts that have been deleted from the source feed 00170 $oldesttimestamp = null; 00171 $uniquehashes = array(); 00172 00173 foreach ($rss->get_items() as $entry) { 00174 // If filtertags are defined, use them to filter the entries by RSS category 00175 if (!empty($externalblog->filtertags)) { 00176 $containsfiltertag = false; 00177 $categories = $entry->get_categories(); 00178 $filtertags = explode(',', $externalblog->filtertags); 00179 $filtertags = array_map('trim', $filtertags); 00180 $filtertags = array_map('strtolower', $filtertags); 00181 00182 foreach ($categories as $category) { 00183 if (in_array(trim(strtolower($category->term)), $filtertags)) { 00184 $containsfiltertag = true; 00185 } 00186 } 00187 00188 if (!$containsfiltertag) { 00189 continue; 00190 } 00191 } 00192 00193 $uniquehashes[] = $entry->get_permalink(); 00194 00195 $newentry = new stdClass(); 00196 $newentry->userid = $externalblog->userid; 00197 $newentry->module = 'blog_external'; 00198 $newentry->content = $externalblog->id; 00199 $newentry->uniquehash = $entry->get_permalink(); 00200 $newentry->publishstate = 'site'; 00201 $newentry->format = FORMAT_HTML; 00202 // Clean subject of html, just in case 00203 $newentry->subject = clean_param($entry->get_title(), PARAM_TEXT); 00204 // Observe 128 max chars in DB 00205 // TODO: +1 to raise this to 255 00206 if ($textlib->strlen($newentry->subject) > 128) { 00207 $newentry->subject = $textlib->substr($newentry->subject, 0, 125) . '...'; 00208 } 00209 $newentry->summary = $entry->get_description(); 00210 00211 //used to decide whether to insert or update 00212 //uses enty permalink plus creation date if available 00213 $existingpostconditions = array('uniquehash' => $entry->get_permalink()); 00214 00215 //our DB doesnt allow null creation or modified timestamps so check the external blog supplied one 00216 $entrydate = $entry->get_date('U'); 00217 if (!empty($entrydate)) { 00218 $existingpostconditions['created'] = $entrydate; 00219 } 00220 00221 //the post ID or false if post not found in DB 00222 $postid = $DB->get_field('post', 'id', $existingpostconditions); 00223 00224 $timestamp = null; 00225 if (empty($entrydate)) { 00226 $timestamp = time(); 00227 } else { 00228 $timestamp = $entrydate; 00229 } 00230 00231 //only set created if its a new post so we retain the original creation timestamp if the post is edited 00232 if ($postid === false) { 00233 $newentry->created = $timestamp; 00234 } 00235 $newentry->lastmodified = $timestamp; 00236 00237 if (empty($oldesttimestamp) || $timestamp < $oldesttimestamp) { 00238 //found an older post 00239 $oldesttimestamp = $timestamp; 00240 } 00241 00242 $textlib = textlib_get_instance(); 00243 if ($textlib->strlen($newentry->uniquehash) > 255) { 00244 // The URL for this item is too long for the field. Rather than add 00245 // the entry without the link we will skip straight over it. 00246 // RSS spec says recommended length 500, we use 255. 00247 debugging('External blog entry skipped because of oversized URL', DEBUG_DEVELOPER); 00248 continue; 00249 } 00250 00251 if ($postid === false) { 00252 $id = $DB->insert_record('post', $newentry); 00253 00254 // Set tags 00255 if ($tags = tag_get_tags_array('blog_external', $externalblog->id)) { 00256 tag_set('post', $id, $tags); 00257 } 00258 } else { 00259 $newentry->id = $postid; 00260 $DB->update_record('post', $newentry); 00261 } 00262 } 00263 00264 // Look at the posts we have in the database to check if any of them have been deleted from the feed. 00265 // Only checking posts within the time frame returned by the rss feed. Older items may have been deleted or 00266 // may just not be returned anymore. We can't tell the difference so we leave older posts alone. 00267 $sql = "SELECT id, uniquehash 00268 FROM {post} 00269 WHERE module = 'blog_external' 00270 AND " . $DB->sql_compare_text('content') . " = " . $DB->sql_compare_text(':blogid') . " 00271 AND created > :ts"; 00272 $dbposts = $DB->get_records_sql($sql, array('blogid' => $externalblog->id, 'ts' => $oldesttimestamp)); 00273 00274 $todelete = array(); 00275 foreach($dbposts as $dbpost) { 00276 if ( !in_array($dbpost->uniquehash, $uniquehashes) ) { 00277 $todelete[] = $dbpost->id; 00278 } 00279 } 00280 $DB->delete_records_list('post', 'id', $todelete); 00281 00282 $DB->update_record('blog_external', array('id' => $externalblog->id, 'timefetched' => time())); 00283 } 00284 00290 function blog_delete_external_entries($externalblog) { 00291 global $DB; 00292 require_capability('moodle/blog:manageexternal', get_context_instance(CONTEXT_SYSTEM)); 00293 $DB->delete_records_select('post', 00294 "module='blog_external' AND " . $DB->sql_compare_text('content') . " = ?", 00295 array($externalblog->id)); 00296 } 00297 00305 function blog_get_context_url($context=null) { 00306 global $CFG; 00307 00308 $viewblogentriesurl = new moodle_url('/blog/index.php'); 00309 00310 if (empty($context)) { 00311 global $PAGE; 00312 $context = $PAGE->context; 00313 } 00314 00315 // Change contextlevel to SYSTEM if viewing the site course 00316 if ($context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID) { 00317 $context = context_system::instance(); 00318 } 00319 00320 $filterparam = ''; 00321 $strlevel = ''; 00322 00323 switch ($context->contextlevel) { 00324 case CONTEXT_SYSTEM: 00325 case CONTEXT_BLOCK: 00326 case CONTEXT_COURSECAT: 00327 break; 00328 case CONTEXT_COURSE: 00329 $filterparam = 'courseid'; 00330 $strlevel = get_string('course'); 00331 break; 00332 case CONTEXT_MODULE: 00333 $filterparam = 'modid'; 00334 $strlevel = print_context_name($context); 00335 break; 00336 case CONTEXT_USER: 00337 $filterparam = 'userid'; 00338 $strlevel = get_string('user'); 00339 break; 00340 } 00341 00342 if (!empty($filterparam)) { 00343 $viewblogentriesurl->param($filterparam, $context->instanceid); 00344 } 00345 00346 return $viewblogentriesurl; 00347 } 00348 00353 function blog_is_enabled_for_user() { 00354 global $CFG; 00355 //return (!empty($CFG->bloglevel) && $CFG->bloglevel <= BLOG_GLOBAL_LEVEL && isloggedin() && !isguestuser()); 00356 return (!empty($CFG->bloglevel) && (isloggedin() || ($CFG->bloglevel == BLOG_GLOBAL_LEVEL))); 00357 } 00358 00373 function blog_get_all_options(moodle_page $page, stdClass $userid = null) { 00374 global $CFG, $DB, $USER; 00375 00376 $options = array(); 00377 00378 // If blogs are enabled and the user is logged in and not a guest 00379 if (blog_is_enabled_for_user()) { 00380 // If the context is the user then assume we want to load for the users context 00381 if (is_null($userid) && $page->context->contextlevel == CONTEXT_USER) { 00382 $userid = $page->context->instanceid; 00383 } 00384 // Check the userid var 00385 if (!is_null($userid) && $userid!==$USER->id) { 00386 // Load the user from the userid... it MUST EXIST throw a wobbly if it doesn't! 00387 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST); 00388 } else { 00389 $user = null; 00390 } 00391 00392 if ($CFG->useblogassociations && $page->cm !== null) { 00393 // Load for the module associated with the page 00394 $options[CONTEXT_MODULE] = blog_get_options_for_module($page->cm, $user); 00395 } else if ($CFG->useblogassociations && $page->course->id != SITEID) { 00396 // Load the options for the course associated with the page 00397 $options[CONTEXT_COURSE] = blog_get_options_for_course($page->course, $user); 00398 } 00399 00400 // Get the options for the user 00401 if ($user !== null and !isguestuser($user)) { 00402 // Load for the requested user 00403 $options[CONTEXT_USER+1] = blog_get_options_for_user($user); 00404 } 00405 // Load for the current user 00406 if (isloggedin() and !isguestuser()) { 00407 $options[CONTEXT_USER] = blog_get_options_for_user(); 00408 } 00409 } 00410 00411 // If blog level is global then display a link to view all site entries 00412 if (!empty($CFG->bloglevel) && $CFG->bloglevel >= BLOG_GLOBAL_LEVEL && has_capability('moodle/blog:view', get_context_instance(CONTEXT_SYSTEM))) { 00413 $options[CONTEXT_SYSTEM] = array('viewsite' => array( 00414 'string' => get_string('viewsiteentries', 'blog'), 00415 'link' => new moodle_url('/blog/index.php') 00416 )); 00417 } 00418 00419 // Return the options 00420 return $options; 00421 } 00422 00432 function blog_get_options_for_user(stdClass $user=null) { 00433 global $CFG, $USER; 00434 // Cache 00435 static $useroptions = array(); 00436 00437 $options = array(); 00438 // Blogs must be enabled and the user must be logged in 00439 if (!blog_is_enabled_for_user()) { 00440 return $options; 00441 } 00442 00443 // Sort out the user var 00444 if ($user === null || $user->id == $USER->id) { 00445 $user = $USER; 00446 $iscurrentuser = true; 00447 } else { 00448 $iscurrentuser = false; 00449 } 00450 00451 // If we've already generated serve from the cache 00452 if (array_key_exists($user->id, $useroptions)) { 00453 return $useroptions[$user->id]; 00454 } 00455 00456 $sitecontext = get_context_instance(CONTEXT_SYSTEM); 00457 $canview = has_capability('moodle/blog:view', $sitecontext); 00458 00459 if (!$iscurrentuser && $canview && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) { 00460 // Not the current user, but we can view and its blogs are enabled for SITE or GLOBAL 00461 $options['userentries'] = array( 00462 'string' => get_string('viewuserentries', 'blog', fullname($user)), 00463 'link' => new moodle_url('/blog/index.php', array('userid'=>$user->id)) 00464 ); 00465 } else { 00466 // It's the current user 00467 if ($canview) { 00468 // We can view our own blogs .... BIG surprise 00469 $options['view'] = array( 00470 'string' => get_string('viewallmyentries', 'blog'), 00471 'link' => new moodle_url('/blog/index.php', array('userid'=>$USER->id)) 00472 ); 00473 } 00474 if (has_capability('moodle/blog:create', $sitecontext)) { 00475 // We can add to our own blog 00476 $options['add'] = array( 00477 'string' => get_string('addnewentry', 'blog'), 00478 'link' => new moodle_url('/blog/edit.php', array('action'=>'add')) 00479 ); 00480 } 00481 } 00482 if ($canview && $CFG->enablerssfeeds) { 00483 $options['rss'] = array( 00484 'string' => get_string('rssfeed', 'blog'), 00485 'link' => new moodle_url(rss_get_url($sitecontext->id, $USER->id, 'blog', 'user/'.$user->id)) 00486 ); 00487 } 00488 00489 // Cache the options 00490 $useroptions[$user->id] = $options; 00491 // Return the options 00492 return $options; 00493 } 00494 00503 function blog_get_options_for_course(stdClass $course, stdClass $user=null) { 00504 global $CFG, $USER; 00505 // Cache 00506 static $courseoptions = array(); 00507 00508 $options = array(); 00509 00510 // User must be logged in and blogs must be enabled 00511 if (!blog_is_enabled_for_user()) { 00512 return $options; 00513 } 00514 00515 // Check that the user can associate with the course 00516 $sitecontext = get_context_instance(CONTEXT_SYSTEM); 00517 if (!has_capability('moodle/blog:associatecourse', $sitecontext)) { 00518 return $options; 00519 } 00520 // Generate the cache key 00521 $key = $course->id.':'; 00522 if (!empty($user)) { 00523 $key .= $user->id; 00524 } else { 00525 $key .= $USER->id; 00526 } 00527 // Serve from the cache if we've already generated for this course 00528 if (array_key_exists($key, $courseoptions)) { 00529 return $courseoptions[$key]; 00530 } 00531 00532 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); 00533 $canparticipate = (is_enrolled($coursecontext) or is_viewing($coursecontext)); 00534 00535 if (has_capability('moodle/blog:view', $coursecontext)) { 00536 // We can view! 00537 if ($CFG->bloglevel >= BLOG_SITE_LEVEL) { 00538 // View entries about this course 00539 $options['courseview'] = array( 00540 'string' => get_string('viewcourseblogs', 'blog'), 00541 'link' => new moodle_url('/blog/index.php', array('courseid'=>$course->id)) 00542 ); 00543 } 00544 // View MY entries about this course 00545 $options['courseviewmine'] = array( 00546 'string' => get_string('viewmyentriesaboutcourse', 'blog'), 00547 'link' => new moodle_url('/blog/index.php', array('courseid'=>$course->id, 'userid'=>$USER->id)) 00548 ); 00549 if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) { 00550 // View the provided users entries about this course 00551 $options['courseviewuser'] = array( 00552 'string' => get_string('viewentriesbyuseraboutcourse', 'blog', fullname($user)), 00553 'link' => new moodle_url('/blog/index.php', array('courseid'=>$course->id, 'userid'=>$user->id)) 00554 ); 00555 } 00556 } 00557 00558 if (has_capability('moodle/blog:create', $sitecontext) and $canparticipate) { 00559 // We can blog about this course 00560 $options['courseadd'] = array( 00561 'string' => get_string('blogaboutthiscourse', 'blog'), 00562 'link' => new moodle_url('/blog/edit.php', array('action'=>'add', 'courseid'=>$course->id)) 00563 ); 00564 } 00565 00566 00567 // Cache the options for this course 00568 $courseoptions[$key] = $options; 00569 // Return the options 00570 return $options; 00571 } 00572 00581 function blog_get_options_for_module($module, $user=null) { 00582 global $CFG, $USER; 00583 // Cache 00584 static $moduleoptions = array(); 00585 00586 $options = array(); 00587 // User must be logged in, blogs must be enabled 00588 if (!blog_is_enabled_for_user()) { 00589 return $options; 00590 } 00591 00592 // Check the user can associate with the module 00593 $sitecontext = get_context_instance(CONTEXT_SYSTEM); 00594 if (!has_capability('moodle/blog:associatemodule', $sitecontext)) { 00595 return $options; 00596 } 00597 00598 // Generate the cache key 00599 $key = $module->id.':'; 00600 if (!empty($user)) { 00601 $key .= $user->id; 00602 } else { 00603 $key .= $USER->id; 00604 } 00605 if (array_key_exists($key, $moduleoptions)) { 00606 // Serve from the cache so we don't have to regenerate 00607 return $moduleoptions[$module->id]; 00608 } 00609 00610 $modcontext = get_context_instance(CONTEXT_MODULE, $module->id); 00611 $canparticipate = (is_enrolled($modcontext) or is_viewing($modcontext)); 00612 00613 if (has_capability('moodle/blog:view', $modcontext)) { 00614 // We can view! 00615 if ($CFG->bloglevel >= BLOG_SITE_LEVEL) { 00616 // View all entries about this module 00617 $a = new stdClass; 00618 $a->type = $module->modname; 00619 $options['moduleview'] = array( 00620 'string' => get_string('viewallmodentries', 'blog', $a), 00621 'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id)) 00622 ); 00623 } 00624 // View MY entries about this module 00625 $options['moduleviewmine'] = array( 00626 'string' => get_string('viewmyentriesaboutmodule', 'blog', $module->modname), 00627 'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id, 'userid'=>$USER->id)) 00628 ); 00629 if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) { 00630 // View the given users entries about this module 00631 $a = new stdClass; 00632 $a->mod = $module->modname; 00633 $a->user = fullname($user); 00634 $options['moduleviewuser'] = array( 00635 'string' => get_string('blogentriesbyuseraboutmodule', 'blog', $a), 00636 'link' => new moodle_url('/blog/index.php', array('modid'=>$module->id, 'userid'=>$user->id)) 00637 ); 00638 } 00639 } 00640 00641 if (has_capability('moodle/blog:create', $sitecontext) and $canparticipate) { 00642 // The user can blog about this module 00643 $options['moduleadd'] = array( 00644 'string' => get_string('blogaboutthismodule', 'blog', $module->modname), 00645 'link' => new moodle_url('/blog/edit.php', array('action'=>'add', 'modid'=>$module->id)) 00646 ); 00647 } 00648 // Cache the options 00649 $moduleoptions[$key] = $options; 00650 // Return the options 00651 return $options; 00652 } 00653 00673 function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=null) { 00674 global $CFG, $PAGE, $DB, $USER; 00675 00676 $id = optional_param('id', null, PARAM_INT); 00677 $tag = optional_param('tag', null, PARAM_NOTAGS); 00678 $tagid = optional_param('tagid', $tagid, PARAM_INT); 00679 $userid = optional_param('userid', $userid, PARAM_INT); 00680 $modid = optional_param('modid', null, PARAM_INT); 00681 $entryid = optional_param('entryid', null, PARAM_INT); 00682 $groupid = optional_param('groupid', $groupid, PARAM_INT); 00683 $courseid = optional_param('courseid', $courseid, PARAM_INT); 00684 $search = optional_param('search', null, PARAM_RAW); 00685 $action = optional_param('action', null, PARAM_ALPHA); 00686 $confirm = optional_param('confirm', false, PARAM_BOOL); 00687 00688 // Ignore userid when action == add 00689 if ($action == 'add' && $userid) { 00690 unset($userid); 00691 $PAGE->url->remove_params(array('userid')); 00692 } else if ($action == 'add' && $entryid) { 00693 unset($entryid); 00694 $PAGE->url->remove_params(array('entryid')); 00695 } 00696 00697 $headers = array('title' => '', 'heading' => '', 'cm' => null, 'filters' => array()); 00698 00699 $blogurl = new moodle_url('/blog/index.php'); 00700 00701 // If the title is not yet set, it's likely that the context isn't set either, so skip this part 00702 $pagetitle = $PAGE->title; 00703 if (!empty($pagetitle)) { 00704 $contexturl = blog_get_context_url(); 00705 00706 // Look at the context URL, it may have additional params that are not in the current URL 00707 if (!$blogurl->compare($contexturl)) { 00708 $blogurl = $contexturl; 00709 if (empty($courseid)) { 00710 $courseid = $blogurl->param('courseid'); 00711 } 00712 if (empty($modid)) { 00713 $modid = $blogurl->param('modid'); 00714 } 00715 } 00716 } 00717 00718 $headers['stradd'] = get_string('addnewentry', 'blog'); 00719 $headers['strview'] = null; 00720 00721 $site = $DB->get_record('course', array('id' => SITEID)); 00722 $sitecontext = get_context_instance(CONTEXT_SYSTEM); 00723 // Common Lang strings 00724 $strparticipants = get_string("participants"); 00725 $strblogentries = get_string("blogentries", 'blog'); 00726 00727 // Prepare record objects as needed 00728 if (!empty($courseid)) { 00729 $headers['filters']['course'] = $courseid; 00730 $course = $DB->get_record('course', array('id' => $courseid)); 00731 } 00732 00733 if (!empty($userid)) { 00734 $headers['filters']['user'] = $userid; 00735 $user = $DB->get_record('user', array('id' => $userid)); 00736 } 00737 00738 if (!empty($groupid)) { // groupid always overrides courseid 00739 $headers['filters']['group'] = $groupid; 00740 $group = $DB->get_record('groups', array('id' => $groupid)); 00741 $course = $DB->get_record('course', array('id' => $group->courseid)); 00742 } 00743 00744 $PAGE->set_pagelayout('standard'); 00745 00746 if (!empty($modid) && $CFG->useblogassociations && has_capability('moodle/blog:associatemodule', $sitecontext)) { // modid always overrides courseid, so the $course object may be reset here 00747 $headers['filters']['module'] = $modid; 00748 // A groupid param may conflict with this coursemod's courseid. Ignore groupid in that case 00749 $courseid = $DB->get_field('course_modules', 'course', array('id'=>$modid)); 00750 $course = $DB->get_record('course', array('id' => $courseid)); 00751 $cm = $DB->get_record('course_modules', array('id' => $modid)); 00752 $cm->modname = $DB->get_field('modules', 'name', array('id' => $cm->module)); 00753 $cm->name = $DB->get_field($cm->modname, 'name', array('id' => $cm->instance)); 00754 $a = new stdClass(); 00755 $a->type = get_string('modulename', $cm->modname); 00756 $PAGE->set_cm($cm, $course); 00757 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); 00758 $headers['strview'] = get_string('viewallmodentries', 'blog', $a); 00759 } 00760 00761 // Case 1: No entry, mod, course or user params: all site entries to be shown (filtered by search and tag/tagid) 00762 // Note: if action is set to 'add' or 'edit', we do this at the end 00763 if (empty($entryid) && empty($modid) && empty($courseid) && empty($userid) && !in_array($action, array('edit', 'add'))) { 00764 $shortname = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); 00765 $PAGE->navbar->add($strblogentries, $blogurl); 00766 $PAGE->set_title("$shortname: " . get_string('blog', 'blog')); 00767 $PAGE->set_heading("$shortname: " . get_string('blog', 'blog')); 00768 $headers['heading'] = get_string('siteblog', 'blog', $shortname); 00769 // $headers['strview'] = get_string('viewsiteentries', 'blog'); 00770 } 00771 00772 // Case 2: only entryid is requested, ignore all other filters. courseid is used to give more contextual information 00773 if (!empty($entryid)) { 00774 $headers['filters']['entry'] = $entryid; 00775 $sql = 'SELECT u.* FROM {user} u, {post} p WHERE p.id = ? AND p.userid = u.id'; 00776 $user = $DB->get_record_sql($sql, array($entryid)); 00777 $entry = $DB->get_record('post', array('id' => $entryid)); 00778 00779 $blogurl->param('userid', $user->id); 00780 00781 if (!empty($course)) { 00782 $mycourseid = $course->id; 00783 $blogurl->param('courseid', $mycourseid); 00784 } else { 00785 $mycourseid = $site->id; 00786 } 00787 $shortname = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); 00788 00789 $PAGE->navbar->add($strblogentries, $blogurl); 00790 00791 $blogurl->remove_params('userid'); 00792 $PAGE->navbar->add($entry->subject, $blogurl); 00793 $PAGE->set_title("$shortname: " . fullname($user) . ": $entry->subject"); 00794 $PAGE->set_heading("$shortname: " . fullname($user) . ": $entry->subject"); 00795 $headers['heading'] = get_string('blogentrybyuser', 'blog', fullname($user)); 00796 00797 // We ignore tag and search params 00798 if (empty($action) || !$CFG->useblogassociations) { 00799 $headers['url'] = $blogurl; 00800 return $headers; 00801 } 00802 } 00803 00804 // Case 3: A user's blog entries 00805 if (!empty($userid) && empty($entryid) && ((empty($courseid) && empty($modid)) || !$CFG->useblogassociations)) { 00806 $shortname = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); 00807 $blogurl->param('userid', $userid); 00808 $PAGE->set_title("$shortname: " . fullname($user) . ": " . get_string('blog', 'blog')); 00809 $PAGE->set_heading("$shortname: " . fullname($user) . ": " . get_string('blog', 'blog')); 00810 $headers['heading'] = get_string('userblog', 'blog', fullname($user)); 00811 $headers['strview'] = get_string('viewuserentries', 'blog', fullname($user)); 00812 00813 } else 00814 00815 // Case 4: No blog associations, no userid 00816 if (!$CFG->useblogassociations && empty($userid) && !in_array($action, array('edit', 'add'))) { 00817 $shortname = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); 00818 $PAGE->set_title("$shortname: " . get_string('blog', 'blog')); 00819 $PAGE->set_heading("$shortname: " . get_string('blog', 'blog')); 00820 $headers['heading'] = get_string('siteblog', 'blog', $shortname); 00821 } else 00822 00823 // Case 5: Blog entries associated with an activity by a specific user (courseid ignored) 00824 if (!empty($userid) && !empty($modid) && empty($entryid)) { 00825 $shortname = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); 00826 $blogurl->param('userid', $userid); 00827 $blogurl->param('modid', $modid); 00828 00829 // Course module navigation is handled by build_navigation as the second param 00830 $headers['cm'] = $cm; 00831 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id"); 00832 $PAGE->navbar->add($strblogentries, $blogurl); 00833 00834 $PAGE->set_title("$shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog')); 00835 $PAGE->set_heading("$shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog')); 00836 00837 $a = new stdClass(); 00838 $a->user = fullname($user); 00839 $a->mod = $cm->name; 00840 $a->type = get_string('modulename', $cm->modname); 00841 $headers['heading'] = get_string('blogentriesbyuseraboutmodule', 'blog', $a); 00842 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); 00843 $headers['strview'] = get_string('viewallmodentries', 'blog', $a); 00844 } else 00845 00846 // Case 6: Blog entries associated with a course by a specific user 00847 if (!empty($userid) && !empty($courseid) && empty($modid) && empty($entryid)) { 00848 $siteshortname = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); 00849 $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); 00850 $blogurl->param('userid', $userid); 00851 $blogurl->param('courseid', $courseid); 00852 00853 $PAGE->navbar->add($strblogentries, $blogurl); 00854 00855 $PAGE->set_title("$siteshortname: $courseshortname: " . fullname($user) . ': ' . get_string('blogentries', 'blog')); 00856 $PAGE->set_heading("$siteshortname: $courseshortname: " . fullname($user) . ': ' . get_string('blogentries', 'blog')); 00857 00858 $a = new stdClass(); 00859 $a->user = fullname($user); 00860 $a->course = format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); 00861 $a->type = get_string('course'); 00862 $headers['heading'] = get_string('blogentriesbyuseraboutcourse', 'blog', $a); 00863 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); 00864 $headers['strview'] = get_string('viewblogentries', 'blog', $a); 00865 00866 // Remove the userid from the URL to inform the blog_menu block correctly 00867 $blogurl->remove_params(array('userid')); 00868 } else 00869 00870 // Case 7: Blog entries by members of a group, associated with that group's course 00871 if (!empty($groupid) && empty($modid) && empty($entryid)) { 00872 $siteshortname = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); 00873 $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); 00874 $blogurl->param('courseid', $course->id); 00875 00876 $PAGE->navbar->add($strblogentries, $blogurl); 00877 $blogurl->remove_params(array('courseid')); 00878 $blogurl->param('groupid', $groupid); 00879 $PAGE->navbar->add($group->name, $blogurl); 00880 00881 $PAGE->set_title("$siteshortname: $courseshortname: " . get_string('blogentries', 'blog') . ": $group->name"); 00882 $PAGE->set_heading("$siteshortname: $courseshortname: " . get_string('blogentries', 'blog') . ": $group->name"); 00883 00884 $a = new stdClass(); 00885 $a->group = $group->name; 00886 $a->course = format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); 00887 $a->type = get_string('course'); 00888 $headers['heading'] = get_string('blogentriesbygroupaboutcourse', 'blog', $a); 00889 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); 00890 $headers['strview'] = get_string('viewblogentries', 'blog', $a); 00891 } else 00892 00893 // Case 8: Blog entries by members of a group, associated with an activity in that course 00894 if (!empty($groupid) && !empty($modid) && empty($entryid)) { 00895 $siteshortname = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); 00896 $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); 00897 $headers['cm'] = $cm; 00898 $blogurl->param('modid', $modid); 00899 $PAGE->navbar->add($strblogentries, $blogurl); 00900 00901 $blogurl->param('groupid', $groupid); 00902 $PAGE->navbar->add($group->name, $blogurl); 00903 00904 $PAGE->set_title("$siteshortname: $courseshortname: $cm->name: " . get_string('blogentries', 'blog') . ": $group->name"); 00905 $PAGE->set_heading("$siteshortname: $courseshortname: $cm->name: " . get_string('blogentries', 'blog') . ": $group->name"); 00906 00907 $a = new stdClass(); 00908 $a->group = $group->name; 00909 $a->mod = $cm->name; 00910 $a->type = get_string('modulename', $cm->modname); 00911 $headers['heading'] = get_string('blogentriesbygroupaboutmodule', 'blog', $a); 00912 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); 00913 $headers['strview'] = get_string('viewallmodentries', 'blog', $a); 00914 00915 } else 00916 00917 // Case 9: All blog entries associated with an activity 00918 if (!empty($modid) && empty($userid) && empty($groupid) && empty($entryid)) { 00919 $siteshortname = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); 00920 $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); 00921 $PAGE->set_cm($cm, $course); 00922 $blogurl->param('modid', $modid); 00923 $PAGE->navbar->add($strblogentries, $blogurl); 00924 $PAGE->set_title("$siteshortname: $courseshortname: $cm->name: " . get_string('blogentries', 'blog')); 00925 $PAGE->set_heading("$siteshortname: $courseshortname: $cm->name: " . get_string('blogentries', 'blog')); 00926 $headers['heading'] = get_string('blogentriesabout', 'blog', $cm->name); 00927 $a = new stdClass(); 00928 $a->type = get_string('modulename', $cm->modname); 00929 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); 00930 $headers['strview'] = get_string('viewallmodentries', 'blog', $a); 00931 } else 00932 00933 // Case 10: All blog entries associated with a course 00934 if (!empty($courseid) && empty($userid) && empty($groupid) && empty($modid) && empty($entryid)) { 00935 $siteshortname = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); 00936 $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); 00937 $blogurl->param('courseid', $courseid); 00938 $PAGE->navbar->add($strblogentries, $blogurl); 00939 $PAGE->set_title("$siteshortname: $courseshortname: " . get_string('blogentries', 'blog')); 00940 $PAGE->set_heading("$siteshortname: $courseshortname: " . get_string('blogentries', 'blog')); 00941 $a = new stdClass(); 00942 $a->type = get_string('course'); 00943 $headers['heading'] = get_string('blogentriesabout', 'blog', format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id)))); 00944 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); 00945 $headers['strview'] = get_string('viewblogentries', 'blog', $a); 00946 $blogurl->remove_params(array('userid')); 00947 } 00948 00949 if (!in_array($action, array('edit', 'add'))) { 00950 // Append Tag info 00951 if (!empty($tagid)) { 00952 $headers['filters']['tag'] = $tagid; 00953 $blogurl->param('tagid', $tagid); 00954 $tagrec = $DB->get_record('tag', array('id'=>$tagid)); 00955 $PAGE->navbar->add($tagrec->name, $blogurl); 00956 } elseif (!empty($tag)) { 00957 $blogurl->param('tag', $tag); 00958 $PAGE->navbar->add(get_string('tagparam', 'blog', $tag), $blogurl); 00959 } 00960 00961 // Append Search info 00962 if (!empty($search)) { 00963 $headers['filters']['search'] = $search; 00964 $blogurl->param('search', $search); 00965 $PAGE->navbar->add(get_string('searchterm', 'blog', $search), $blogurl->out()); 00966 } 00967 } 00968 00969 // Append edit mode info 00970 if (!empty($action) && $action == 'add') { 00971 00972 } else if (!empty($action) && $action == 'edit') { 00973 $PAGE->navbar->add(get_string('editentry', 'blog')); 00974 } 00975 00976 if (empty($headers['url'])) { 00977 $headers['url'] = $blogurl; 00978 } 00979 return $headers; 00980 } 00981 00988 function blog_get_associated_count($courseid, $cmid=null) { 00989 global $DB; 00990 $context = get_context_instance(CONTEXT_COURSE, $courseid); 00991 if ($cmid) { 00992 $context = get_context_instance(CONTEXT_MODULE, $cmid); 00993 } 00994 return $DB->count_records('blog_association', array('contextid' => $context->id)); 00995 } 00996 01014 function blog_comment_permissions($comment_param) { 01015 return array('post'=>true, 'view'=>true); 01016 } 01017 01030 function blog_comment_validate($comment_param) { 01031 global $DB; 01032 // validate comment itemid 01033 if (!$entry = $DB->get_record('post', array('id'=>$comment_param->itemid))) { 01034 throw new comment_exception('invalidcommentitemid'); 01035 } 01036 // validate comment area 01037 if ($comment_param->commentarea != 'format_blog') { 01038 throw new comment_exception('invalidcommentarea'); 01039 } 01040 // validation for comment deletion 01041 if (!empty($comment_param->commentid)) { 01042 if ($record = $DB->get_record('comments', array('id'=>$comment_param->commentid))) { 01043 if ($record->commentarea != 'format_blog') { 01044 throw new comment_exception('invalidcommentarea'); 01045 } 01046 if ($record->contextid != $comment_param->context->id) { 01047 throw new comment_exception('invalidcontext'); 01048 } 01049 if ($record->itemid != $comment_param->itemid) { 01050 throw new comment_exception('invalidcommentitemid'); 01051 } 01052 } else { 01053 throw new comment_exception('invalidcommentid'); 01054 } 01055 } 01056 return true; 01057 } 01058 01065 function blog_page_type_list($pagetype, $parentcontext, $currentcontext) { 01066 return array( 01067 '*'=>get_string('page-x', 'pagetype'), 01068 'blog-*'=>get_string('page-blog-x', 'blog'), 01069 'blog-index'=>get_string('page-blog-index', 'blog'), 01070 'blog-edit'=>get_string('page-blog-edit', 'blog') 01071 ); 01072 }