[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Core global functions for Blog. 19 * 20 * @package moodlecore 21 * @subpackage blog 22 * @copyright 2009 Nicolas Connault 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 /* 29 * Library of functions and constants for blog 30 */ 31 require_once($CFG->dirroot .'/blog/rsslib.php'); 32 33 /** 34 * User can edit a blog entry if this is their own blog entry and they have 35 * the capability moodle/blog:create, or if they have the capability 36 * moodle/blog:manageentries. 37 * 38 * This also applies to deleting of entries. 39 */ 40 function blog_user_can_edit_entry($blogentry) { 41 global $USER; 42 43 $sitecontext = context_system::instance(); 44 45 if (has_capability('moodle/blog:manageentries', $sitecontext)) { 46 return true; // Can edit any blog entry. 47 } 48 49 if ($blogentry->userid == $USER->id && has_capability('moodle/blog:create', $sitecontext)) { 50 return true; // Can edit own when having blog:create capability. 51 } 52 53 return false; 54 } 55 56 57 /** 58 * Checks to see if a user can view the blogs of another user. 59 * Only blog level is checked here, the capabilities are enforced 60 * in blog/index.php 61 */ 62 function blog_user_can_view_user_entry($targetuserid, $blogentry=null) { 63 global $CFG, $USER, $DB; 64 65 if (empty($CFG->enableblogs)) { 66 return false; // Blog system disabled. 67 } 68 69 if (isloggedin() && $USER->id == $targetuserid) { 70 return true; // Can view own entries in any case. 71 } 72 73 $sitecontext = context_system::instance(); 74 if (has_capability('moodle/blog:manageentries', $sitecontext)) { 75 return true; // Can manage all entries. 76 } 77 78 // If blog is in draft state, then make sure user have proper capability. 79 if ($blogentry && $blogentry->publishstate == 'draft' && !has_capability('moodle/blog:viewdrafts', $sitecontext)) { 80 return false; // Can not view draft of others. 81 } 82 83 // If blog entry is not public, make sure user is logged in. 84 if ($blogentry && $blogentry->publishstate != 'public' && !isloggedin()) { 85 return false; 86 } 87 88 // If blogentry is not passed or all above checks pass, then check capability based on system config. 89 switch ($CFG->bloglevel) { 90 case BLOG_GLOBAL_LEVEL: 91 return true; 92 break; 93 94 case BLOG_SITE_LEVEL: 95 if (isloggedin()) { // Not logged in viewers forbidden. 96 return true; 97 } 98 return false; 99 break; 100 101 case BLOG_USER_LEVEL: 102 default: 103 // If user is viewing other user blog, then user should have user:readuserblogs capability. 104 $personalcontext = context_user::instance($targetuserid); 105 return has_capability('moodle/user:readuserblogs', $personalcontext); 106 break; 107 108 } 109 } 110 111 /** 112 * remove all associations for the blog entries of a particular user 113 * @param int userid - id of user whose blog associations will be deleted 114 */ 115 function blog_remove_associations_for_user($userid) { 116 global $DB; 117 throw new coding_exception('function blog_remove_associations_for_user() is not finished'); 118 /* 119 $blogentries = blog_fetch_entries(array('user' => $userid), 'lasmodified DESC'); 120 foreach ($blogentries as $entry) { 121 if (blog_user_can_edit_entry($entry)) { 122 blog_remove_associations_for_entry($entry->id); 123 } 124 } 125 */ 126 } 127 128 /** 129 * remove all associations for the blog entries of a particular course 130 * @param int courseid - id of user whose blog associations will be deleted 131 */ 132 function blog_remove_associations_for_course($courseid) { 133 global $DB; 134 $context = context_course::instance($courseid); 135 $DB->delete_records('blog_association', array('contextid' => $context->id)); 136 } 137 138 /** 139 * Given a record in the {blog_external} table, checks the blog's URL 140 * for new entries not yet copied into Moodle. 141 * Also attempts to identify and remove deleted blog entries 142 * 143 * @param object $externalblog 144 * @return boolean False if the Feed is invalid 145 */ 146 function blog_sync_external_entries($externalblog) { 147 global $CFG, $DB; 148 require_once($CFG->libdir . '/simplepie/moodle_simplepie.php'); 149 150 $rss = new moodle_simplepie(); 151 $rssfile = $rss->registry->create('File', array($externalblog->url)); 152 $filetest = $rss->registry->create('Locator', array($rssfile)); 153 154 if (!$filetest->is_feed($rssfile)) { 155 $externalblog->failedlastsync = 1; 156 $DB->update_record('blog_external', $externalblog); 157 return false; 158 } else if (!empty($externalblog->failedlastsync)) { 159 $externalblog->failedlastsync = 0; 160 $DB->update_record('blog_external', $externalblog); 161 } 162 163 $rss->set_feed_url($externalblog->url); 164 $rss->init(); 165 166 if (empty($rss->data)) { 167 return null; 168 } 169 // Used to identify blog posts that have been deleted from the source feed. 170 $oldesttimestamp = null; 171 $uniquehashes = array(); 172 173 foreach ($rss->get_items() as $entry) { 174 // If filtertags are defined, use them to filter the entries by RSS category. 175 if (!empty($externalblog->filtertags)) { 176 $containsfiltertag = false; 177 $categories = $entry->get_categories(); 178 $filtertags = explode(',', $externalblog->filtertags); 179 $filtertags = array_map('trim', $filtertags); 180 $filtertags = array_map('strtolower', $filtertags); 181 182 if (!empty($categories)) { 183 foreach ($categories as $category) { 184 if (in_array(trim(strtolower($category->term)), $filtertags)) { 185 $containsfiltertag = true; 186 } 187 } 188 } 189 190 if (!$containsfiltertag) { 191 continue; 192 } 193 } 194 195 $uniquehashes[] = $entry->get_permalink(); 196 197 $newentry = new stdClass(); 198 $newentry->userid = $externalblog->userid; 199 $newentry->module = 'blog_external'; 200 $newentry->content = $externalblog->id; 201 $newentry->uniquehash = $entry->get_permalink(); 202 $newentry->publishstate = 'site'; 203 $newentry->format = FORMAT_HTML; 204 // Clean subject of html, just in case. 205 $newentry->subject = clean_param($entry->get_title(), PARAM_TEXT); 206 // Observe 128 max chars in DB. 207 // TODO: +1 to raise this to 255. 208 if (core_text::strlen($newentry->subject) > 128) { 209 $newentry->subject = core_text::substr($newentry->subject, 0, 125) . '...'; 210 } 211 $newentry->summary = $entry->get_description(); 212 213 // Used to decide whether to insert or update. 214 // Uses enty permalink plus creation date if available. 215 $existingpostconditions = array('uniquehash' => $entry->get_permalink()); 216 217 // Our DB doesnt allow null creation or modified timestamps so check the external blog supplied one. 218 $entrydate = $entry->get_date('U'); 219 if (!empty($entrydate)) { 220 $existingpostconditions['created'] = $entrydate; 221 } 222 223 // The post ID or false if post not found in DB. 224 $postid = $DB->get_field('post', 'id', $existingpostconditions); 225 226 $timestamp = null; 227 if (empty($entrydate)) { 228 $timestamp = time(); 229 } else { 230 $timestamp = $entrydate; 231 } 232 233 // Only set created if its a new post so we retain the original creation timestamp if the post is edited. 234 if ($postid === false) { 235 $newentry->created = $timestamp; 236 } 237 $newentry->lastmodified = $timestamp; 238 239 if (empty($oldesttimestamp) || $timestamp < $oldesttimestamp) { 240 // Found an older post. 241 $oldesttimestamp = $timestamp; 242 } 243 244 if (core_text::strlen($newentry->uniquehash) > 255) { 245 // The URL for this item is too long for the field. Rather than add 246 // the entry without the link we will skip straight over it. 247 // RSS spec says recommended length 500, we use 255. 248 debugging('External blog entry skipped because of oversized URL', DEBUG_DEVELOPER); 249 continue; 250 } 251 252 if ($postid === false) { 253 $id = $DB->insert_record('post', $newentry); 254 255 // Set tags. 256 if ($tags = core_tag_tag::get_item_tags_array('core', 'blog_external', $externalblog->id)) { 257 core_tag_tag::set_item_tags('core', 'post', $id, context_user::instance($externalblog->userid), $tags); 258 } 259 } else { 260 $newentry->id = $postid; 261 $DB->update_record('post', $newentry); 262 } 263 } 264 265 // Look at the posts we have in the database to check if any of them have been deleted from the feed. 266 // Only checking posts within the time frame returned by the rss feed. Older items may have been deleted or 267 // may just not be returned anymore. We can't tell the difference so we leave older posts alone. 268 $sql = "SELECT id, uniquehash 269 FROM {post} 270 WHERE module = 'blog_external' 271 AND " . $DB->sql_compare_text('content') . " = " . $DB->sql_compare_text(':blogid') . " 272 AND created > :ts"; 273 $dbposts = $DB->get_records_sql($sql, array('blogid' => $externalblog->id, 'ts' => $oldesttimestamp)); 274 275 $todelete = array(); 276 foreach ($dbposts as $dbpost) { 277 if ( !in_array($dbpost->uniquehash, $uniquehashes) ) { 278 $todelete[] = $dbpost->id; 279 } 280 } 281 $DB->delete_records_list('post', 'id', $todelete); 282 283 $DB->update_record('blog_external', array('id' => $externalblog->id, 'timefetched' => time())); 284 } 285 286 /** 287 * Given an external blog object, deletes all related blog entries from the post table. 288 * NOTE: The external blog's id is saved as post.content, a field that is not oterhwise used by blog entries. 289 * @param object $externablog 290 */ 291 function blog_delete_external_entries($externalblog) { 292 global $DB; 293 require_capability('moodle/blog:manageexternal', context_system::instance()); 294 $DB->delete_records_select('post', 295 "module='blog_external' AND " . $DB->sql_compare_text('content') . " = ?", 296 array($externalblog->id)); 297 } 298 299 /** 300 * This function checks that blogs are enabled, and that the user can see blogs at all 301 * @return bool 302 */ 303 function blog_is_enabled_for_user() { 304 global $CFG; 305 return (!empty($CFG->enableblogs) && (isloggedin() || ($CFG->bloglevel == BLOG_GLOBAL_LEVEL))); 306 } 307 308 /** 309 * This function gets all of the options available for the current user in respect 310 * to blogs. 311 * 312 * It loads the following if applicable: 313 * - Module options {@see blog_get_options_for_module} 314 * - Course options {@see blog_get_options_for_course} 315 * - User specific options {@see blog_get_options_for_user} 316 * - General options (BLOG_LEVEL_GLOBAL) 317 * 318 * @param moodle_page $page The page to load for (normally $PAGE) 319 * @param stdClass $userid Load for a specific user 320 * @return array An array of options organised by type. 321 */ 322 function blog_get_all_options(moodle_page $page, stdClass $userid = null) { 323 global $CFG, $DB, $USER; 324 325 $options = array(); 326 327 // If blogs are enabled and the user is logged in and not a guest. 328 if (blog_is_enabled_for_user()) { 329 // If the context is the user then assume we want to load for the users context. 330 if (is_null($userid) && $page->context->contextlevel == CONTEXT_USER) { 331 $userid = $page->context->instanceid; 332 } 333 // Check the userid var. 334 if (!is_null($userid) && $userid !== $USER->id) { 335 // Load the user from the userid... it MUST EXIST throw a wobbly if it doesn't! 336 $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST); 337 } else { 338 $user = null; 339 } 340 341 if ($CFG->useblogassociations && $page->cm !== null) { 342 // Load for the module associated with the page. 343 $options[CONTEXT_MODULE] = blog_get_options_for_module($page->cm, $user); 344 } else if ($CFG->useblogassociations && $page->course->id != SITEID) { 345 // Load the options for the course associated with the page. 346 $options[CONTEXT_COURSE] = blog_get_options_for_course($page->course, $user); 347 } 348 349 // Get the options for the user. 350 if ($user !== null and !isguestuser($user)) { 351 // Load for the requested user. 352 $options[CONTEXT_USER + 1] = blog_get_options_for_user($user); 353 } 354 // Load for the current user. 355 if (isloggedin() and !isguestuser()) { 356 $options[CONTEXT_USER] = blog_get_options_for_user(); 357 } 358 } 359 360 // If blog level is global then display a link to view all site entries. 361 if (!empty($CFG->enableblogs) 362 && $CFG->bloglevel >= BLOG_GLOBAL_LEVEL 363 && has_capability('moodle/blog:view', context_system::instance())) { 364 365 $options[CONTEXT_SYSTEM] = array('viewsite' => array( 366 'string' => get_string('viewsiteentries', 'blog'), 367 'link' => new moodle_url('/blog/index.php') 368 )); 369 } 370 371 // Return the options. 372 return $options; 373 } 374 375 /** 376 * Get all of the blog options that relate to the passed user. 377 * 378 * If no user is passed the current user is assumed. 379 * 380 * @staticvar array $useroptions Cache so we don't have to regenerate multiple times 381 * @param stdClass $user 382 * @return array The array of options for the requested user 383 */ 384 function blog_get_options_for_user(stdClass $user=null) { 385 global $CFG, $USER; 386 // Cache. 387 static $useroptions = array(); 388 389 $options = array(); 390 // Blogs must be enabled and the user must be logged in. 391 if (!blog_is_enabled_for_user()) { 392 return $options; 393 } 394 395 // Sort out the user var. 396 if ($user === null || $user->id == $USER->id) { 397 $user = $USER; 398 $iscurrentuser = true; 399 } else { 400 $iscurrentuser = false; 401 } 402 403 // If we've already generated serve from the cache. 404 if (array_key_exists($user->id, $useroptions)) { 405 return $useroptions[$user->id]; 406 } 407 408 $sitecontext = context_system::instance(); 409 $canview = has_capability('moodle/blog:view', $sitecontext); 410 411 if (!$iscurrentuser && $canview && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) { 412 // Not the current user, but we can view and its blogs are enabled for SITE or GLOBAL. 413 $options['userentries'] = array( 414 'string' => get_string('viewuserentries', 'blog', fullname($user)), 415 'link' => new moodle_url('/blog/index.php', array('userid' => $user->id)) 416 ); 417 } else { 418 // It's the current user. 419 if ($canview) { 420 // We can view our own blogs .... BIG surprise. 421 $options['view'] = array( 422 'string' => get_string('blogentries', 'blog'), 423 'link' => new moodle_url('/blog/index.php', array('userid' => $USER->id)) 424 ); 425 } 426 if (has_capability('moodle/blog:create', $sitecontext)) { 427 // We can add to our own blog. 428 $options['add'] = array( 429 'string' => get_string('addnewentry', 'blog'), 430 'link' => new moodle_url('/blog/edit.php', array('action' => 'add')) 431 ); 432 } 433 } 434 if ($canview && $CFG->enablerssfeeds) { 435 $options['rss'] = array( 436 'string' => get_string('rssfeed', 'blog'), 437 'link' => new moodle_url(rss_get_url($sitecontext->id, $USER->id, 'blog', 'user/'.$user->id)) 438 ); 439 } 440 441 // Cache the options. 442 $useroptions[$user->id] = $options; 443 // Return the options. 444 return $options; 445 } 446 447 /** 448 * Get the blog options that relate to the given course for the given user. 449 * 450 * @staticvar array $courseoptions A cache so we can save regenerating multiple times 451 * @param stdClass $course The course to load options for 452 * @param stdClass $user The user to load options for null == current user 453 * @return array The array of options 454 */ 455 function blog_get_options_for_course(stdClass $course, stdClass $user=null) { 456 global $CFG, $USER; 457 // Cache. 458 static $courseoptions = array(); 459 460 $options = array(); 461 462 // User must be logged in and blogs must be enabled. 463 if (!blog_is_enabled_for_user()) { 464 return $options; 465 } 466 467 // Check that the user can associate with the course. 468 $sitecontext = context_system::instance(); 469 // Generate the cache key. 470 $key = $course->id.':'; 471 if (!empty($user)) { 472 $key .= $user->id; 473 } else { 474 $key .= $USER->id; 475 } 476 // Serve from the cache if we've already generated for this course. 477 if (array_key_exists($key, $courseoptions)) { 478 return $courseoptions[$key]; 479 } 480 481 if (has_capability('moodle/blog:view', $sitecontext)) { 482 // We can view! 483 if ($CFG->bloglevel >= BLOG_SITE_LEVEL) { 484 // View entries about this course. 485 $options['courseview'] = array( 486 'string' => get_string('viewcourseblogs', 'blog'), 487 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id)) 488 ); 489 } 490 // View MY entries about this course. 491 $options['courseviewmine'] = array( 492 'string' => get_string('viewmyentriesaboutcourse', 'blog'), 493 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id, 'userid' => $USER->id)) 494 ); 495 if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) { 496 // View the provided users entries about this course. 497 $options['courseviewuser'] = array( 498 'string' => get_string('viewentriesbyuseraboutcourse', 'blog', fullname($user)), 499 'link' => new moodle_url('/blog/index.php', array('courseid' => $course->id, 'userid' => $user->id)) 500 ); 501 } 502 } 503 504 if (has_capability('moodle/blog:create', $sitecontext)) { 505 // We can blog about this course. 506 $options['courseadd'] = array( 507 'string' => get_string('blogaboutthiscourse', 'blog'), 508 'link' => new moodle_url('/blog/edit.php', array('action' => 'add', 'courseid' => $course->id)) 509 ); 510 } 511 512 // Cache the options for this course. 513 $courseoptions[$key] = $options; 514 // Return the options. 515 return $options; 516 } 517 518 /** 519 * Get the blog options relating to the given module for the given user 520 * 521 * @staticvar array $moduleoptions Cache 522 * @param stdClass|cm_info $module The module to get options for 523 * @param stdClass $user The user to get options for null == currentuser 524 * @return array 525 */ 526 function blog_get_options_for_module($module, $user=null) { 527 global $CFG, $USER; 528 // Cache. 529 static $moduleoptions = array(); 530 531 $options = array(); 532 // User must be logged in, blogs must be enabled. 533 if (!blog_is_enabled_for_user()) { 534 return $options; 535 } 536 537 $sitecontext = context_system::instance(); 538 539 // Generate the cache key. 540 $key = $module->id.':'; 541 if (!empty($user)) { 542 $key .= $user->id; 543 } else { 544 $key .= $USER->id; 545 } 546 if (array_key_exists($key, $moduleoptions)) { 547 // Serve from the cache so we don't have to regenerate. 548 return $moduleoptions[$key]; 549 } 550 551 if (has_capability('moodle/blog:view', $sitecontext)) { 552 // Save correct module name for later usage. 553 $modulename = get_string('modulename', $module->modname); 554 555 // We can view! 556 if ($CFG->bloglevel >= BLOG_SITE_LEVEL) { 557 // View all entries about this module. 558 $a = new stdClass; 559 $a->type = $modulename; 560 $options['moduleview'] = array( 561 'string' => get_string('viewallmodentries', 'blog', $a), 562 'link' => new moodle_url('/blog/index.php', array('modid' => $module->id)) 563 ); 564 } 565 // View MY entries about this module. 566 $options['moduleviewmine'] = array( 567 'string' => get_string('viewmyentriesaboutmodule', 'blog', $modulename), 568 'link' => new moodle_url('/blog/index.php', array('modid' => $module->id, 'userid' => $USER->id)) 569 ); 570 if (!empty($user) && ($CFG->bloglevel >= BLOG_SITE_LEVEL)) { 571 // View the given users entries about this module. 572 $a = new stdClass; 573 $a->mod = $modulename; 574 $a->user = fullname($user); 575 $options['moduleviewuser'] = array( 576 'string' => get_string('blogentriesbyuseraboutmodule', 'blog', $a), 577 'link' => new moodle_url('/blog/index.php', array('modid' => $module->id, 'userid' => $user->id)) 578 ); 579 } 580 } 581 582 if (has_capability('moodle/blog:create', $sitecontext)) { 583 // The user can blog about this module. 584 $options['moduleadd'] = array( 585 'string' => get_string('blogaboutthismodule', 'blog', $modulename), 586 'link' => new moodle_url('/blog/edit.php', array('action' => 'add', 'modid' => $module->id)) 587 ); 588 } 589 // Cache the options. 590 $moduleoptions[$key] = $options; 591 // Return the options. 592 return $options; 593 } 594 595 /** 596 * This function encapsulates all the logic behind the complex 597 * navigation, titles and headings of the blog listing page, depending 598 * on URL params. It looks at URL params and at the current context level. 599 * It builds and returns an array containing: 600 * 601 * 1. heading: The heading displayed above the blog entries 602 * 2. stradd: The text to be used as the "Add entry" link 603 * 3. strview: The text to be used as the "View entries" link 604 * 4. url: The moodle_url object used as the base for add and view links 605 * 5. filters: An array of parameters used to filter blog listings. Used by index.php and the Recent blogs block 606 * 607 * All other variables are set directly in $PAGE 608 * 609 * It uses the current URL to build these variables. 610 * A number of mutually exclusive use cases are used to structure this function. 611 * 612 * @return array 613 */ 614 function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=null) { 615 global $CFG, $PAGE, $DB, $USER; 616 617 $id = optional_param('id', null, PARAM_INT); 618 $tag = optional_param('tag', null, PARAM_NOTAGS); 619 $tagid = optional_param('tagid', $tagid, PARAM_INT); 620 $userid = optional_param('userid', $userid, PARAM_INT); 621 $modid = optional_param('modid', null, PARAM_INT); 622 $entryid = optional_param('entryid', null, PARAM_INT); 623 $groupid = optional_param('groupid', $groupid, PARAM_INT); 624 $courseid = optional_param('courseid', $courseid, PARAM_INT); 625 $search = optional_param('search', null, PARAM_RAW); 626 $action = optional_param('action', null, PARAM_ALPHA); 627 $confirm = optional_param('confirm', false, PARAM_BOOL); 628 629 // Ignore userid when action == add. 630 if ($action == 'add' && $userid) { 631 unset($userid); 632 $PAGE->url->remove_params(array('userid')); 633 } else if ($action == 'add' && $entryid) { 634 unset($entryid); 635 $PAGE->url->remove_params(array('entryid')); 636 } 637 638 $headers = array('title' => '', 'heading' => '', 'cm' => null, 'filters' => array()); 639 640 $blogurl = new moodle_url('/blog/index.php'); 641 642 $headers['stradd'] = get_string('addnewentry', 'blog'); 643 $headers['strview'] = null; 644 645 $site = $DB->get_record('course', array('id' => SITEID)); 646 $sitecontext = context_system::instance(); 647 // Common Lang strings. 648 $strparticipants = get_string("participants"); 649 $strblogentries = get_string("blogentries", 'blog'); 650 651 // Prepare record objects as needed. 652 if (!empty($courseid)) { 653 $headers['filters']['course'] = $courseid; 654 $course = $DB->get_record('course', array('id' => $courseid)); 655 } 656 657 if (!empty($userid)) { 658 $headers['filters']['user'] = $userid; 659 $user = $DB->get_record('user', array('id' => $userid)); 660 } 661 662 if (!empty($groupid)) { // The groupid always overrides courseid. 663 $headers['filters']['group'] = $groupid; 664 $group = $DB->get_record('groups', array('id' => $groupid)); 665 $course = $DB->get_record('course', array('id' => $group->courseid)); 666 } 667 668 $PAGE->set_pagelayout('standard'); 669 670 // The modid always overrides courseid, so the $course object may be reset here. 671 if (!empty($modid) && $CFG->useblogassociations) { 672 673 $headers['filters']['module'] = $modid; 674 // A groupid param may conflict with this coursemod's courseid. Ignore groupid in that case. 675 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid)); 676 $course = $DB->get_record('course', array('id' => $courseid)); 677 $cm = $DB->get_record('course_modules', array('id' => $modid)); 678 $cm->modname = $DB->get_field('modules', 'name', array('id' => $cm->module)); 679 $cm->name = $DB->get_field($cm->modname, 'name', array('id' => $cm->instance)); 680 $a = new stdClass(); 681 $a->type = get_string('modulename', $cm->modname); 682 $PAGE->set_cm($cm, $course); 683 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); 684 $headers['strview'] = get_string('viewallmodentries', 'blog', $a); 685 } 686 687 // Case 1: No entry, mod, course or user params: all site entries to be shown (filtered by search and tag/tagid) 688 // Note: if action is set to 'add' or 'edit', we do this at the end. 689 if (empty($entryid) && empty($modid) && empty($courseid) && empty($userid) && !in_array($action, array('edit', 'add'))) { 690 $PAGE->navbar->add($strblogentries, $blogurl); 691 $PAGE->set_title($site->fullname); 692 $PAGE->set_heading($site->fullname); 693 $headers['heading'] = get_string('siteblogheading', 'blog'); 694 } 695 696 // Case 2: only entryid is requested, ignore all other filters. courseid is used to give more contextual information. 697 if (!empty($entryid)) { 698 $headers['filters']['entry'] = $entryid; 699 $sql = 'SELECT u.* FROM {user} u, {post} p WHERE p.id = ? AND p.userid = u.id'; 700 $user = $DB->get_record_sql($sql, array($entryid)); 701 $entry = $DB->get_record('post', array('id' => $entryid)); 702 703 $blogurl->param('userid', $user->id); 704 705 if (!empty($course)) { 706 $mycourseid = $course->id; 707 $blogurl->param('courseid', $mycourseid); 708 } else { 709 $mycourseid = $site->id; 710 } 711 $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID))); 712 713 $PAGE->navbar->add($strblogentries, $blogurl); 714 715 $blogurl->remove_params('userid'); 716 $PAGE->navbar->add($entry->subject, $blogurl); 717 $PAGE->set_title("$shortname: " . fullname($user) . ": $entry->subject"); 718 $PAGE->set_heading("$shortname: " . fullname($user) . ": $entry->subject"); 719 $headers['heading'] = get_string('blogentrybyuser', 'blog', fullname($user)); 720 721 // We ignore tag and search params. 722 if (empty($action) || !$CFG->useblogassociations) { 723 $headers['url'] = $blogurl; 724 return $headers; 725 } 726 } 727 728 if (!empty($userid) && empty($entryid) && ((empty($courseid) && empty($modid)) || !$CFG->useblogassociations)) { 729 // Case 3: A user's blog entries. 730 731 $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID))); 732 $blogurl->param('userid', $userid); 733 $PAGE->set_title("$shortname: " . fullname($user) . ": " . get_string('blog', 'blog')); 734 $PAGE->set_heading("$shortname: " . fullname($user) . ": " . get_string('blog', 'blog')); 735 $headers['heading'] = get_string('userblog', 'blog', fullname($user)); 736 $headers['strview'] = get_string('viewuserentries', 'blog', fullname($user)); 737 738 } else if (!$CFG->useblogassociations && empty($userid) && !in_array($action, array('edit', 'add'))) { 739 // Case 4: No blog associations, no userid. 740 741 $PAGE->set_title($site->fullname); 742 $PAGE->set_heading($site->fullname); 743 $headers['heading'] = get_string('siteblogheading', 'blog'); 744 } else if (!empty($userid) && !empty($modid) && empty($entryid)) { 745 // Case 5: Blog entries associated with an activity by a specific user (courseid ignored). 746 747 $shortname = format_string($site->shortname, true, array('context' => context_course::instance(SITEID))); 748 $blogurl->param('userid', $userid); 749 $blogurl->param('modid', $modid); 750 751 // Course module navigation is handled by build_navigation as the second param. 752 $headers['cm'] = $cm; 753 $PAGE->navbar->add(fullname($user), "$CFG->wwwroot/user/view.php?id=$user->id"); 754 $PAGE->navbar->add($strblogentries, $blogurl); 755 756 $PAGE->set_title("$shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog')); 757 $PAGE->set_heading("$shortname: $cm->name: " . fullname($user) . ': ' . get_string('blogentries', 'blog')); 758 759 $a = new stdClass(); 760 $a->user = fullname($user); 761 $a->mod = $cm->name; 762 $a->type = get_string('modulename', $cm->modname); 763 $headers['heading'] = get_string('blogentriesbyuseraboutmodule', 'blog', $a); 764 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); 765 $headers['strview'] = get_string('viewallmodentries', 'blog', $a); 766 } else if (!empty($userid) && !empty($courseid) && empty($modid) && empty($entryid)) { 767 // Case 6: Blog entries associated with a course by a specific user. 768 769 $blogurl->param('userid', $userid); 770 $blogurl->param('courseid', $courseid); 771 772 $PAGE->set_title($course->fullname); 773 $PAGE->set_heading($course->fullname); 774 775 $a = new stdClass(); 776 $a->user = fullname($user); 777 $a->course = format_string($course->fullname, true, array('context' => context_course::instance($course->id))); 778 $a->type = get_string('course'); 779 $headers['heading'] = get_string('blogentriesbyuseraboutcourse', 'blog', $a); 780 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); 781 $headers['strview'] = get_string('viewblogentries', 'blog', $a); 782 783 // Remove the userid from the URL to inform the blog_menu block correctly. 784 $blogurl->remove_params(array('userid')); 785 } else if (!empty($groupid) && empty($modid) && empty($entryid)) { 786 // Case 7: Blog entries by members of a group, associated with that group's course. 787 788 $blogurl->param('courseid', $course->id); 789 790 $PAGE->navbar->add($strblogentries, $blogurl); 791 $blogurl->remove_params(array('courseid')); 792 $blogurl->param('groupid', $groupid); 793 $PAGE->navbar->add($group->name, $blogurl); 794 795 $PAGE->set_title($course->fullname); 796 $PAGE->set_heading($course->fullname); 797 798 $a = new stdClass(); 799 $a->group = $group->name; 800 $a->course = format_string($course->fullname, true, array('context' => context_course::instance($course->id))); 801 $a->type = get_string('course'); 802 $headers['heading'] = get_string('blogentriesbygroupaboutcourse', 'blog', $a); 803 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); 804 $headers['strview'] = get_string('viewblogentries', 'blog', $a); 805 } else if (!empty($groupid) && !empty($modid) && empty($entryid)) { 806 // Case 8: Blog entries by members of a group, associated with an activity in that course. 807 808 $headers['cm'] = $cm; 809 $blogurl->param('modid', $modid); 810 $PAGE->navbar->add($strblogentries, $blogurl); 811 812 $blogurl->param('groupid', $groupid); 813 $PAGE->navbar->add($group->name, $blogurl); 814 815 $PAGE->set_title($course->fullname); 816 $PAGE->set_heading($course->fullname); 817 818 $a = new stdClass(); 819 $a->group = $group->name; 820 $a->mod = $cm->name; 821 $a->type = get_string('modulename', $cm->modname); 822 $headers['heading'] = get_string('blogentriesbygroupaboutmodule', 'blog', $a); 823 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); 824 $headers['strview'] = get_string('viewallmodentries', 'blog', $a); 825 826 } else if (!empty($modid) && empty($userid) && empty($groupid) && empty($entryid)) { 827 // Case 9: All blog entries associated with an activity. 828 829 $PAGE->set_cm($cm, $course); 830 $blogurl->param('modid', $modid); 831 $PAGE->navbar->add($strblogentries, $blogurl); 832 $PAGE->set_title($course->fullname); 833 $PAGE->set_heading($course->fullname); 834 $headers['heading'] = get_string('blogentriesabout', 'blog', $cm->name); 835 $a = new stdClass(); 836 $a->type = get_string('modulename', $cm->modname); 837 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); 838 $headers['strview'] = get_string('viewallmodentries', 'blog', $a); 839 } else if (!empty($courseid) && empty($userid) && empty($groupid) && empty($modid) && empty($entryid)) { 840 // Case 10: All blog entries associated with a course. 841 842 $blogurl->param('courseid', $courseid); 843 $PAGE->navbar->add($strblogentries, $blogurl); 844 $PAGE->set_title($course->fullname); 845 $PAGE->set_heading($course->fullname); 846 $a = new stdClass(); 847 $a->type = get_string('course'); 848 $headers['heading'] = get_string('blogentriesabout', 849 'blog', 850 format_string($course->fullname, 851 true, 852 array('context' => context_course::instance($course->id)))); 853 $headers['stradd'] = get_string('blogaboutthis', 'blog', $a); 854 $headers['strview'] = get_string('viewblogentries', 'blog', $a); 855 $blogurl->remove_params(array('userid')); 856 } 857 858 if (!in_array($action, array('edit', 'add'))) { 859 // Append Tag info. 860 if (!empty($tagid)) { 861 $headers['filters']['tag'] = $tagid; 862 $blogurl->param('tagid', $tagid); 863 $tagrec = $DB->get_record('tag', array('id' => $tagid)); 864 $PAGE->navbar->add($tagrec->name, $blogurl); 865 } else if (!empty($tag)) { 866 if ($tagrec = $DB->get_record('tag', array('name' => $tag))) { 867 $tagid = $tagrec->id; 868 $headers['filters']['tag'] = $tagid; 869 $blogurl->param('tag', $tag); 870 $PAGE->navbar->add(get_string('tagparam', 'blog', $tag), $blogurl); 871 } 872 } 873 874 // Append Search info. 875 if (!empty($search)) { 876 $headers['filters']['search'] = $search; 877 $blogurl->param('search', $search); 878 $PAGE->navbar->add(get_string('searchterm', 'blog', $search), $blogurl->out()); 879 } 880 } 881 882 // Append edit mode info. 883 if (!empty($action) && $action == 'add') { 884 885 } else if (!empty($action) && $action == 'edit') { 886 $PAGE->navbar->add(get_string('editentry', 'blog')); 887 } 888 889 if (empty($headers['url'])) { 890 $headers['url'] = $blogurl; 891 } 892 return $headers; 893 } 894 895 /** 896 * Shortcut function for getting a count of blog entries associated with a course or a module 897 * @param int $courseid The ID of the course 898 * @param int $cmid The ID of the course_modules 899 * @return string The number of associated entries 900 */ 901 function blog_get_associated_count($courseid, $cmid=null) { 902 global $DB; 903 $context = context_course::instance($courseid); 904 if ($cmid) { 905 $context = context_module::instance($cmid); 906 } 907 return $DB->count_records('blog_association', array('contextid' => $context->id)); 908 } 909 910 /** 911 * Running addtional permission check on plugin, for example, plugins 912 * may have switch to turn on/off comments option, this callback will 913 * affect UI display, not like pluginname_comment_validate only throw 914 * exceptions. 915 * blog_comment_validate will be called before viewing/adding/deleting 916 * comment, so don't repeat checks. 917 * Capability check has been done in comment->check_permissions(), we 918 * don't need to do it again here. 919 * 920 * @package core_blog 921 * @category comment 922 * 923 * @param stdClass $commentparam { 924 * context => context the context object 925 * courseid => int course id 926 * cm => stdClass course module object 927 * commentarea => string comment area 928 * itemid => int itemid 929 * } 930 * @return array 931 */ 932 function blog_comment_permissions($commentparam) { 933 global $DB; 934 935 // If blog is public and current user is guest, then don't let him post comments. 936 $blogentry = $DB->get_record('post', array('id' => $commentparam->itemid), 'publishstate', MUST_EXIST); 937 938 if ($blogentry->publishstate != 'public') { 939 if (!isloggedin() || isguestuser()) { 940 return array('post' => false, 'view' => true); 941 } 942 } 943 return array('post' => true, 'view' => true); 944 } 945 946 /** 947 * Validate comment parameter before perform other comments actions 948 * 949 * @package core_blog 950 * @category comment 951 * 952 * @param stdClass $comment { 953 * context => context the context object 954 * courseid => int course id 955 * cm => stdClass course module object 956 * commentarea => string comment area 957 * itemid => int itemid 958 * } 959 * @return boolean 960 */ 961 function blog_comment_validate($commentparam) { 962 global $CFG, $DB, $USER; 963 964 // Check if blogs are enabled user can comment. 965 if (empty($CFG->enableblogs) || empty($CFG->blogusecomments)) { 966 throw new comment_exception('nopermissiontocomment'); 967 } 968 969 // Validate comment area. 970 if ($commentparam->commentarea != 'format_blog') { 971 throw new comment_exception('invalidcommentarea'); 972 } 973 974 $blogentry = $DB->get_record('post', array('id' => $commentparam->itemid), '*', MUST_EXIST); 975 976 // Validation for comment deletion. 977 if (!empty($commentparam->commentid)) { 978 if ($record = $DB->get_record('comments', array('id' => $commentparam->commentid))) { 979 if ($record->commentarea != 'format_blog') { 980 throw new comment_exception('invalidcommentarea'); 981 } 982 if ($record->contextid != $commentparam->context->id) { 983 throw new comment_exception('invalidcontext'); 984 } 985 if ($record->itemid != $commentparam->itemid) { 986 throw new comment_exception('invalidcommentitemid'); 987 } 988 } else { 989 throw new comment_exception('invalidcommentid'); 990 } 991 } 992 993 // Validate if user has blog view permission. 994 $sitecontext = context_system::instance(); 995 return has_capability('moodle/blog:view', $sitecontext) && 996 blog_user_can_view_user_entry($blogentry->userid, $blogentry); 997 } 998 999 /** 1000 * Return a list of page types 1001 * @param string $pagetype current page type 1002 * @param stdClass $parentcontext Block's parent context 1003 * @param stdClass $currentcontext Current context of block 1004 */ 1005 function blog_page_type_list($pagetype, $parentcontext, $currentcontext) { 1006 return array( 1007 '*' => get_string('page-x', 'pagetype'), 1008 'blog-*' => get_string('page-blog-x', 'blog'), 1009 'blog-index' => get_string('page-blog-index', 'blog'), 1010 'blog-edit' => get_string('page-blog-edit', 'blog') 1011 ); 1012 } 1013 1014 /** 1015 * Add nodes to myprofile page. 1016 * 1017 * @param \core_user\output\myprofile\tree $tree Tree object 1018 * @param stdClass $user user object 1019 * @param bool $iscurrentuser 1020 * @param stdClass $course Course object 1021 * 1022 * @return bool 1023 */ 1024 function core_blog_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) { 1025 global $CFG; 1026 if (!blog_is_enabled_for_user() || isguestuser($user)) { 1027 // The guest user cannot post, so it is not possible to view any posts. 1028 // Also blogs might be disabled. 1029 // May as well just bail aggressively here. 1030 return true; 1031 } 1032 if (!blog_user_can_view_user_entry($user->id)) { 1033 return true; 1034 } 1035 $url = new moodle_url("/blog/index.php", array('userid' => $user->id)); 1036 if (!empty($course)) { 1037 $url->param('courseid', $course->id); 1038 } 1039 if ($iscurrentuser) { 1040 $title = get_string('blogentries', 'core_blog'); 1041 } else { 1042 $title = get_string('myprofileuserblogs', 'core_blog'); 1043 } 1044 $blognode = new core_user\output\myprofile\node('miscellaneous', 'blogs', $title, null, $url); 1045 $tree->add_node($blognode); 1046 return true; 1047 } 1048 1049 /** 1050 * Returns posts tagged with a specified tag. 1051 * 1052 * @param core_tag_tag $tag 1053 * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag 1054 * are displayed on the page and the per-page limit may be bigger 1055 * @param int $fromctx context id where the link was displayed, may be used by callbacks 1056 * to display items in the same context first 1057 * @param int $ctx context id where to search for records 1058 * @param bool $rec search in subcontexts as well 1059 * @param int $page 0-based number of page being displayed 1060 * @return \core_tag\output\tagindex 1061 */ 1062 function blog_get_tagged_posts($tag, $exclusivemode = false, $fromctx = 0, $ctx = 0, $rec = true, $page = 0) { 1063 global $CFG, $OUTPUT; 1064 require_once($CFG->dirroot.'/user/lib.php'); 1065 1066 $systemcontext = context_system::instance(); 1067 $perpage = $exclusivemode ? 20 : 5; 1068 $context = $ctx ? context::instance_by_id($ctx) : context_system::instance(); 1069 1070 $content = ''; 1071 if (empty($CFG->enableblogs) || !has_capability('moodle/blog:view', $systemcontext)) { 1072 // Blogs are not enabled or are not visible to the current user. 1073 $totalpages = 0; 1074 } else if ($context->contextlevel != CONTEXT_SYSTEM && empty($CFG->useblogassociations)) { 1075 // No blog entries can be associated to the non-system context. 1076 $totalpages = 0; 1077 } else if (!$rec && $context->contextlevel != CONTEXT_COURSE && $context->contextlevel != CONTEXT_MODULE) { 1078 // No blog entries can be associated with category or block context. 1079 $totalpages = 0; 1080 } else { 1081 require_once($CFG->dirroot.'/blog/locallib.php'); 1082 1083 $filters = array('tag' => $tag->id); 1084 if ($rec) { 1085 if ($context->contextlevel != CONTEXT_SYSTEM) { 1086 $filters['context'] = $context->id; 1087 } 1088 } else if ($context->contextlevel == CONTEXT_COURSE) { 1089 $filters['course'] = $context->instanceid; 1090 } else if ($context->contextlevel == CONTEXT_MODULE) { 1091 $filters['module'] = $context->instanceid; 1092 } 1093 $bloglisting = new blog_listing($filters); 1094 $blogs = $bloglisting->get_entries($page * $perpage, $perpage); 1095 $totalcount = $bloglisting->count_entries(); 1096 $totalpages = ceil($totalcount / $perpage); 1097 if (!empty($blogs)) { 1098 $tagfeed = new core_tag\output\tagfeed(); 1099 foreach ($blogs as $blog) { 1100 $user = fullclone($blog); 1101 $user->id = $blog->userid; 1102 $user->deleted = 0; 1103 $img = $OUTPUT->user_picture($user, array('size' => 35)); 1104 $subject = format_string($blog->subject); 1105 1106 if ($blog->publishstate == 'draft') { 1107 $class = 'dimmed'; 1108 } else { 1109 $class = ''; 1110 } 1111 1112 $url = new moodle_url('/blog/index.php', array('entryid' => $blog->id)); 1113 $subject = html_writer::link($url, $subject, array('class' => $class)); 1114 1115 $fullname = fullname($user); 1116 if (user_can_view_profile($user)) { 1117 $profilelink = new moodle_url('/user/view.php', array('id' => $blog->userid)); 1118 $fullname = html_writer::link($profilelink, $fullname); 1119 } 1120 $details = $fullname . ', ' . userdate($blog->created); 1121 1122 $tagfeed->add($img, $subject, $details); 1123 } 1124 1125 $items = $tagfeed->export_for_template($OUTPUT); 1126 $content = $OUTPUT->render_from_template('core_tag/tagfeed', $items); 1127 1128 $urlparams = array('tagid' => $tag->id); 1129 if ($context->contextlevel == CONTEXT_COURSE) { 1130 $urlparams['courseid'] = $context->instanceid; 1131 } else if ($context->contextlevel == CONTEXT_MODULE) { 1132 $urlparams['modid'] = $context->instanceid; 1133 } 1134 $allblogsurl = new moodle_url('/blog/index.php', $urlparams); 1135 1136 $rv = new core_tag\output\tagindex($tag, 'core', 'post', 1137 $content, 1138 $exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages); 1139 $rv->exclusiveurl = $allblogsurl; 1140 return $rv; 1141 } 1142 } 1143 1144 $rv = new core_tag\output\tagindex($tag, 'core', 'post', 1145 $content, 1146 $exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages); 1147 $rv->exclusiveurl = null; 1148 return $rv; 1149 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |