PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/index.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 293 lines | 217 code | 47 blank | 29 comment | 90 complexity | 7249cbdaea59ba1eab5d399c83ebf81e MD5 | raw file
  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. $sitecontext = context_system::instance();
  66. if (isset($userid) && $USER->id == $userid) {
  67. $blognode = $PAGE->navigation->find('siteblog', null);
  68. if ($blognode) {
  69. $blognode->make_inactive();
  70. }
  71. }
  72. // Check basic permissions.
  73. if ($CFG->bloglevel == BLOG_GLOBAL_LEVEL) {
  74. // Everybody can see anything - no login required unless site is locked down using forcelogin.
  75. if ($CFG->forcelogin) {
  76. require_login();
  77. }
  78. } else if ($CFG->bloglevel == BLOG_SITE_LEVEL) {
  79. // Users must log in and can not be guests.
  80. require_login();
  81. if (isguestuser()) {
  82. // They must have entered the url manually.
  83. print_error('noguest');
  84. }
  85. } else if ($CFG->bloglevel == BLOG_USER_LEVEL) {
  86. // Users can see own blogs only! with the exception of people with special cap.
  87. require_login();
  88. } else {
  89. // Weird!
  90. print_error('blogdisable', 'blog');
  91. }
  92. if (empty($CFG->enableblogs)) {
  93. print_error('blogdisable', 'blog');
  94. }
  95. // Add courseid if modid or groupid is specified: This is used for navigation and title.
  96. if (!empty($modid) && empty($courseid)) {
  97. $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
  98. }
  99. if (!empty($groupid) && empty($courseid)) {
  100. $courseid = $DB->get_field('groups', 'courseid', array('id' => $groupid));
  101. }
  102. if (!$userid && has_capability('moodle/blog:view', $sitecontext) && $CFG->bloglevel > BLOG_USER_LEVEL) {
  103. if ($entryid) {
  104. if (!$entryobject = $DB->get_record('post', array('id' => $entryid))) {
  105. print_error('nosuchentry', 'blog');
  106. }
  107. $userid = $entryobject->userid;
  108. }
  109. } else if (!$userid) {
  110. $userid = $USER->id;
  111. }
  112. if (!empty($modid)) {
  113. if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
  114. print_error(get_string('nocourseblogs', 'blog'));
  115. }
  116. if (!$mod = $DB->get_record('course_modules', array('id' => $modid))) {
  117. print_error(get_string('invalidmodid', 'blog'));
  118. }
  119. $courseid = $mod->course;
  120. }
  121. if ((empty($courseid) ? true : $courseid == SITEID) && empty($userid)) {
  122. if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
  123. print_error('siteblogdisable', 'blog');
  124. }
  125. if (!has_capability('moodle/blog:view', $sitecontext)) {
  126. print_error('cannotviewsiteblog', 'blog');
  127. }
  128. $COURSE = $DB->get_record('course', array('format' => 'site'));
  129. $courseid = $COURSE->id;
  130. }
  131. if (!empty($courseid)) {
  132. if (!$course = $DB->get_record('course', array('id' => $courseid))) {
  133. print_error('invalidcourseid');
  134. }
  135. $courseid = $course->id;
  136. require_login($course);
  137. if (!has_capability('moodle/blog:view', $sitecontext)) {
  138. print_error('cannotviewcourseblog', 'blog');
  139. }
  140. } else {
  141. $coursecontext = context_course::instance(SITEID);
  142. }
  143. if (!empty($groupid)) {
  144. if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
  145. print_error('groupblogdisable', 'blog');
  146. }
  147. if (! $group = groups_get_group($groupid)) {
  148. print_error(get_string('invalidgroupid', 'blog'));
  149. }
  150. if (!$course = $DB->get_record('course', array('id' => $group->courseid))) {
  151. print_error('invalidcourseid');
  152. }
  153. $coursecontext = context_course::instance($course->id);
  154. $courseid = $course->id;
  155. require_login($course);
  156. if (!has_capability('moodle/blog:view', $sitecontext)) {
  157. print_error(get_string('cannotviewcourseorgroupblog', 'blog'));
  158. }
  159. if (groups_get_course_groupmode($course) == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $coursecontext)) {
  160. if (!groups_is_member($groupid)) {
  161. print_error('notmemberofgroup');
  162. }
  163. }
  164. }
  165. if (!empty($userid)) {
  166. if ($CFG->bloglevel < BLOG_USER_LEVEL) {
  167. print_error('blogdisable', 'blog');
  168. }
  169. if (!$user = $DB->get_record('user', array('id' => $userid))) {
  170. print_error('invaliduserid');
  171. }
  172. if ($user->deleted) {
  173. echo $OUTPUT->header();
  174. echo $OUTPUT->heading(get_string('userdeleted'));
  175. echo $OUTPUT->footer();
  176. die;
  177. }
  178. if ($USER->id == $userid) {
  179. if (!has_capability('moodle/blog:create', $sitecontext)
  180. && !has_capability('moodle/blog:view', $sitecontext)) {
  181. print_error('donothaveblog', 'blog');
  182. }
  183. } else {
  184. if (!has_capability('moodle/blog:view', $sitecontext) || !blog_user_can_view_user_entry($userid)) {
  185. print_error('cannotviewcourseblog', 'blog');
  186. }
  187. $PAGE->navigation->extend_for_user($user);
  188. }
  189. }
  190. $courseid = (empty($courseid)) ? SITEID : $courseid;
  191. $blogheaders = blog_get_headers();
  192. $rsscontext = null;
  193. $filtertype = null;
  194. $thingid = null;
  195. $rsstitle = '';
  196. if ($CFG->enablerssfeeds) {
  197. list($thingid, $rsscontext, $filtertype) = blog_rss_get_params($blogheaders['filters']);
  198. if (empty($rsscontext)) {
  199. $rsscontext = context_system::instance();
  200. }
  201. $rsstitle = $blogheaders['heading'];
  202. // Check we haven't started output by outputting an error message.
  203. if ($PAGE->state == moodle_page::STATE_BEFORE_HEADER) {
  204. blog_rss_add_http_header($rsscontext, $rsstitle, $filtertype, $thingid, $tagid);
  205. }
  206. }
  207. $usernode = $PAGE->navigation->find('user'.$userid, null);
  208. if ($usernode && $courseid != SITEID) {
  209. $courseblogsnode = $PAGE->navigation->find('courseblogs', null);
  210. if ($courseblogsnode) {
  211. $courseblogsnode->remove();
  212. }
  213. $blogurl = new moodle_url($PAGE->url);
  214. $blognode = $usernode->add(get_string('blogscourse', 'blog'), $blogurl);
  215. $blognode->make_active();
  216. }
  217. if ($courseid != SITEID) {
  218. $PAGE->set_heading($course->fullname);
  219. echo $OUTPUT->header();
  220. if (!empty($user)) {
  221. $headerinfo = array('heading' => fullname($user), 'user' => $user);
  222. echo $OUTPUT->context_header($headerinfo, 2);
  223. }
  224. } else if (isset($userid)) {
  225. $PAGE->set_heading(fullname($user));
  226. echo $OUTPUT->header();
  227. } else if ($courseid == SITEID) {
  228. echo $OUTPUT->header();
  229. }
  230. echo $OUTPUT->heading($blogheaders['heading'], 2);
  231. $bloglisting = new blog_listing($blogheaders['filters']);
  232. $bloglisting->print_entries();
  233. if ($CFG->enablerssfeeds) {
  234. blog_rss_print_link($rsscontext, $filtertype, $thingid, $tagid, get_string('rssfeed', 'blog'));
  235. }
  236. echo $OUTPUT->footer();
  237. $eventparams = array(
  238. 'other' => array('entryid' => $entryid, 'tagid' => $tagid, 'userid' => $userid, 'modid' => $modid, 'groupid' => $groupid,
  239. 'search' => $search, 'fromstart' => $start)
  240. );
  241. if (!empty($userid)) {
  242. $eventparams['relateduserid'] = $userid;
  243. }
  244. $eventparams['other']['courseid'] = ($courseid === SITEID) ? 0 : $courseid;
  245. $event = \core\event\blog_entries_viewed::create($eventparams);
  246. $event->trigger();