MDL-65788 tool_policy: Prevent two modals rendering for guests.
[moodle.git] / blocks / news_items / block_news_items.php
CommitLineData
4ca6cfbf 1<?php
f25a6839
SH
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/>.
0f3fe4b6 16
f25a6839
SH
17/**
18 * This file contains the news item block class, based upon block_base.
19 *
20 * @package block_news_items
21 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25/**
26 * Class block_news_items
27 *
28 * @package block_news_items
29 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 */
e89d741a 32class block_news_items extends block_base {
9b4b78fd 33 function init() {
8e5c60bd 34 $this->title = get_string('pluginname', 'block_news_items');
89adb174 35 }
36
0f3fe4b6 37 function get_content() {
cb640229 38 global $CFG, $USER;
0f3fe4b6 39
675dbdd9 40 if ($this->content !== NULL) {
41 return $this->content;
42 }
43
9b4b78fd 44 $this->content = new stdClass;
45 $this->content->text = '';
46 $this->content->footer = '';
47
48 if (empty($this->instance)) {
0f3fe4b6 49 return $this->content;
50 }
51
0f3fe4b6 52
cb640229 53 if ($this->page->course->newsitems) { // Create a nice listing of recent postings
90ec387a 54
55 require_once($CFG->dirroot.'/mod/forum/lib.php'); // We'll need this
1519aa2b 56
90ec387a 57 $text = '';
58
cb640229 59 if (!$forum = forum_get_course_forum($this->page->course->id, 'news')) {
08103c93 60 return '';
90ec387a 61 }
62
cb640229 63 $modinfo = get_fast_modinfo($this->page->course);
dd97c328 64 if (empty($modinfo->instances['forum'][$forum->id])) {
08103c93
ML
65 return '';
66 }
dd97c328 67 $cm = $modinfo->instances['forum'][$forum->id];
f39bba50 68
3bcc53cd
PS
69 if (!$cm->uservisible) {
70 return '';
71 }
72
fe2fdd11 73 $context = context_module::instance($cm->id);
90ec387a 74
5e71406d 75 /// User must have perms to view discussions in that forum
76 if (!has_capability('mod/forum:viewdiscussion', $context)) {
77 return '';
78 }
79
08103c93 80 /// First work out whether we can post to this group and if so, include a link
d9bf1982 81 $groupmode = groups_get_activity_groupmode($cm);
82 $currentgroup = groups_get_activity_group($cm, true);
5e71406d 83
08103c93 84 if (forum_user_can_post_discussion($forum, $currentgroup, $groupmode, $cm, $context)) {
99b054e4 85 $text .= '<div class="newlink"><a href="'.$CFG->wwwroot.'/mod/forum/post.php?forum='.$forum->id.'">'.
90ec387a 86 get_string('addanewtopic', 'forum').'</a>...</div>';
90ec387a 87 }
88
89 /// Get all the recent discussions we're allowed to see
90
1e366657
AO
91 // This block displays the most recent posts in a forum in
92 // descending order. The call to default sort order here will use
93 // that unless the discussion that post is in has a timestart set
94 // in the future.
5f219cf1
BK
95 // This sort will ignore pinned posts as we want the most recent.
96 $sort = forum_get_default_sort_order(true, 'p.modified', 'd', false);
1e366657 97 if (! $discussions = forum_get_discussions($cm, $sort, false,
10186fe8
JK
98 -1, $this->page->course->newsitems,
99 false, -1, 0, FORUM_POSTS_ALL_USER_GROUPS) ) {
90ec387a 100 $text .= '('.get_string('nonews', 'forum').')';
101 $this->content->text = $text;
102 return $this->content;
103 }
104
105 /// Actually create the listing now
106
107 $strftimerecent = get_string('strftimerecent');
108 $strmore = get_string('more', 'forum');
109
5489fce7 110 /// Accessibility: markup as a list.
111 $text .= "\n<ul class='unlist'>\n";
90ec387a 112 foreach ($discussions as $discussion) {
113
114 $discussion->subject = $discussion->name;
115
c3cb9fb9 116 $discussion->subject = format_string($discussion->subject, true, $forum->course);
90ec387a 117
5489fce7 118 $text .= '<li class="post">'.
29ef7d4f 119 '<div class="head clearfix">'.
49f7d01d 120 '<div class="date">'.userdate($discussion->modified, $strftimerecent).'</div>'.
121 '<div class="name">'.fullname($discussion).'</div></div>'.
7edfa373 122 '<div class="info"><a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->discussion.'">'.$discussion->subject.'</a></div>'.
5489fce7 123 "</li>\n";
90ec387a 124 }
5489fce7 125 $text .= "</ul>\n";
90ec387a 126
127 $this->content->text = $text;
128
129 $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/forum/view.php?f='.$forum->id.'">'.
130 get_string('oldertopics', 'forum').'</a> ...';
131
f39bba50 132 /// If RSS is activated at site and forum level and this forum has rss defined, show link
133 if (isset($CFG->enablerssfeeds) && isset($CFG->forum_enablerssfeeds) &&
134 $CFG->enablerssfeeds && $CFG->forum_enablerssfeeds && $forum->rsstype && $forum->rssarticles) {
135 require_once($CFG->dirroot.'/lib/rsslib.php'); // We'll need this
136 if ($forum->rsstype == 1) {
6444367c 137 $tooltiptext = get_string('rsssubscriberssdiscussions','forum');
f39bba50 138 } else {
b3a7e5f3 139 $tooltiptext = get_string('rsssubscriberssposts','forum');
f39bba50 140 }
4f0c2d00 141 if (!isloggedin()) {
f534c1c0 142 $userid = $CFG->siteguest;
f39bba50 143 } else {
144 $userid = $USER->id;
145 }
d95a02fa 146
147 $this->content->footer .= '<br />'.rss_get_link($context->id, $userid, 'mod_forum', $forum->id, $tooltiptext);
f39bba50 148 }
149
0f3fe4b6 150 }
90ec387a 151
0f3fe4b6 152 return $this->content;
153 }
154}
155
4ca6cfbf 156