PageRenderTime 40ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/index.php

https://bitbucket.org/moodle/moodle
PHP | 186 lines | 131 code | 27 blank | 28 comment | 50 complexity | 69524d51d6bcb91af375fd4b77f2762c MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * file index.php
  18. * index page to view blogs. if no blog is specified then site wide entries are shown
  19. * if a blog id is specified then the latest entries from that blog are shown
  20. */
  21. require_once(__DIR__ . '/../config.php');
  22. require_once($CFG->dirroot .'/blog/lib.php');
  23. require_once($CFG->dirroot .'/blog/locallib.php');
  24. require_once($CFG->dirroot .'/course/lib.php');
  25. require_once($CFG->dirroot .'/comment/lib.php');
  26. $id = optional_param('id', null, PARAM_INT);
  27. $start = optional_param('formstart', 0, PARAM_INT);
  28. $tag = optional_param('tag', '', PARAM_NOTAGS);
  29. $userid = optional_param('userid', null, PARAM_INT);
  30. $tagid = optional_param('tagid', null, PARAM_INT);
  31. $modid = optional_param('modid', null, PARAM_INT);
  32. $entryid = optional_param('entryid', null, PARAM_INT);
  33. $groupid = optional_param('groupid', null, PARAM_INT);
  34. $courseid = optional_param('courseid', null, PARAM_INT);
  35. $search = optional_param('search', null, PARAM_RAW);
  36. comment::init();
  37. $urlparams = compact('id', 'start', 'tag', 'userid', 'tagid', 'modid', 'entryid', 'groupid', 'courseid', 'search');
  38. foreach ($urlparams as $var => $val) {
  39. if (empty($val)) {
  40. unset($urlparams[$var]);
  41. }
  42. }
  43. $PAGE->set_url('/blog/index.php', $urlparams);
  44. // Correct tagid if a text tag is provided as a param.
  45. if (!empty($tag)) {
  46. if ($tagrec = $DB->get_record('tag', array('name' => $tag))) {
  47. $tagid = $tagrec->id;
  48. } else {
  49. unset($tagid);
  50. }
  51. }
  52. // Set the userid to the entry author if we have the entry ID.
  53. if ($entryid and !isset($userid)) {
  54. $entry = new blog_entry($entryid);
  55. $userid = $entry->userid;
  56. }
  57. if (isset($userid) && empty($courseid) && empty($modid)) {
  58. $context = context_user::instance($userid);
  59. } else if (!empty($courseid) && $courseid != SITEID) {
  60. $context = context_course::instance($courseid);
  61. } else {
  62. $context = context_system::instance();
  63. }
  64. $PAGE->set_context($context);
  65. if (isset($userid) && $USER->id == $userid) {
  66. $blognode = $PAGE->navigation->find('siteblog', null);
  67. if ($blognode) {
  68. $blognode->make_inactive();
  69. }
  70. }
  71. // Check basic permissions.
  72. if ($CFG->bloglevel == BLOG_GLOBAL_LEVEL) {
  73. // Everybody can see anything - no login required unless site is locked down using forcelogin.
  74. if ($CFG->forcelogin) {
  75. require_login();
  76. }
  77. } else if ($CFG->bloglevel == BLOG_SITE_LEVEL) {
  78. // Users must log in and can not be guests.
  79. require_login();
  80. if (isguestuser()) {
  81. // They must have entered the url manually.
  82. print_error('noguest');
  83. }
  84. } else if ($CFG->bloglevel == BLOG_USER_LEVEL) {
  85. // Users can see own blogs only! with the exception of people with special cap.
  86. require_login();
  87. } else {
  88. // Weird!
  89. print_error('blogdisable', 'blog');
  90. }
  91. if (empty($CFG->enableblogs)) {
  92. print_error('blogdisable', 'blog');
  93. }
  94. list($courseid, $userid) = blog_validate_access($courseid, $modid, $groupid, $entryid, $userid);
  95. $courseid = (empty($courseid)) ? SITEID : $courseid;
  96. if ($courseid != SITEID) {
  97. $course = get_course($courseid);
  98. require_login($course);
  99. }
  100. if (!empty($userid)) {
  101. $user = core_user::get_user($userid);
  102. $PAGE->navigation->extend_for_user($user);
  103. }
  104. $blogheaders = blog_get_headers();
  105. $rsscontext = null;
  106. $filtertype = null;
  107. $thingid = null;
  108. $rsstitle = '';
  109. if ($CFG->enablerssfeeds) {
  110. list($thingid, $rsscontext, $filtertype) = blog_rss_get_params($blogheaders['filters']);
  111. if (empty($rsscontext)) {
  112. $rsscontext = context_system::instance();
  113. }
  114. $rsstitle = $blogheaders['heading'];
  115. // Check we haven't started output by outputting an error message.
  116. if ($PAGE->state == moodle_page::STATE_BEFORE_HEADER) {
  117. blog_rss_add_http_header($rsscontext, $rsstitle, $filtertype, $thingid, $tagid);
  118. }
  119. }
  120. $usernode = $PAGE->navigation->find('user'.$userid, null);
  121. if ($usernode && $courseid != SITEID) {
  122. $courseblogsnode = $PAGE->navigation->find('courseblogs', null);
  123. if ($courseblogsnode) {
  124. $courseblogsnode->remove();
  125. }
  126. $blogurl = new moodle_url($PAGE->url);
  127. $blognode = $usernode->add(get_string('blogscourse', 'blog'), $blogurl);
  128. $blognode->make_active();
  129. }
  130. if ($courseid != SITEID) {
  131. $PAGE->set_heading($course->fullname);
  132. echo $OUTPUT->header();
  133. if (!empty($user)) {
  134. $headerinfo = array('heading' => fullname($user), 'user' => $user);
  135. echo $OUTPUT->context_header($headerinfo, 2);
  136. }
  137. } else if (isset($userid)) {
  138. $PAGE->set_heading(fullname($user));
  139. echo $OUTPUT->header();
  140. } else if ($courseid == SITEID) {
  141. echo $OUTPUT->header();
  142. }
  143. echo $OUTPUT->heading($blogheaders['heading'], 2);
  144. $bloglisting = new blog_listing($blogheaders['filters']);
  145. $bloglisting->print_entries();
  146. if ($CFG->enablerssfeeds) {
  147. blog_rss_print_link($rsscontext, $filtertype, $thingid, $tagid, get_string('rssfeed', 'blog'));
  148. }
  149. echo $OUTPUT->footer();
  150. $eventparams = array(
  151. 'other' => array('entryid' => $entryid, 'tagid' => $tagid, 'userid' => $userid, 'modid' => $modid, 'groupid' => $groupid,
  152. 'search' => $search, 'fromstart' => $start)
  153. );
  154. if (!empty($userid)) {
  155. $eventparams['relateduserid'] = $userid;
  156. }
  157. $eventparams['other']['courseid'] = ($courseid === SITEID) ? 0 : $courseid;
  158. $event = \core\event\blog_entries_viewed::create($eventparams);
  159. $event->trigger();