1 |
efrain |
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 |
* Library of functions for forum outside of the core api
|
|
|
19 |
*
|
|
|
20 |
* @package mod_forum
|
|
|
21 |
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
// Event types.
|
|
|
26 |
define('FORUM_EVENT_TYPE_DUE', 'due');
|
|
|
27 |
|
|
|
28 |
require_once($CFG->dirroot . '/mod/forum/lib.php');
|
|
|
29 |
require_once($CFG->libdir . '/portfolio/caller.php');
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* @package mod_forum
|
|
|
33 |
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
class forum_portfolio_caller extends portfolio_module_caller_base {
|
|
|
37 |
|
|
|
38 |
protected $postid;
|
|
|
39 |
protected $discussionid;
|
|
|
40 |
protected $attachment;
|
|
|
41 |
|
|
|
42 |
private $post;
|
|
|
43 |
private $forum;
|
|
|
44 |
private $discussion;
|
|
|
45 |
private $posts;
|
|
|
46 |
private $keyedfiles; // just using multifiles isn't enough if we're exporting a full thread
|
|
|
47 |
|
|
|
48 |
/** @var context_module context instance. */
|
|
|
49 |
private $modcontext;
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* @return array
|
|
|
53 |
*/
|
|
|
54 |
public static function expected_callbackargs() {
|
|
|
55 |
return array(
|
|
|
56 |
'postid' => false,
|
|
|
57 |
'discussionid' => false,
|
|
|
58 |
'attachment' => false,
|
|
|
59 |
);
|
|
|
60 |
}
|
|
|
61 |
/**
|
|
|
62 |
* @param array $callbackargs
|
|
|
63 |
*/
|
|
|
64 |
function __construct($callbackargs) {
|
|
|
65 |
parent::__construct($callbackargs);
|
|
|
66 |
if (!$this->postid && !$this->discussionid) {
|
|
|
67 |
throw new portfolio_caller_exception('mustprovidediscussionorpost', 'forum');
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
/**
|
|
|
71 |
* @global object
|
|
|
72 |
*/
|
|
|
73 |
public function load_data() {
|
|
|
74 |
global $DB;
|
|
|
75 |
|
|
|
76 |
if ($this->postid) {
|
|
|
77 |
if (!$this->post = $DB->get_record('forum_posts', array('id' => $this->postid))) {
|
|
|
78 |
throw new portfolio_caller_exception('invalidpostid', 'forum');
|
|
|
79 |
}
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
$dparams = array();
|
|
|
83 |
if ($this->discussionid) {
|
|
|
84 |
$dbparams = array('id' => $this->discussionid);
|
|
|
85 |
} else if ($this->post) {
|
|
|
86 |
$dbparams = array('id' => $this->post->discussion);
|
|
|
87 |
} else {
|
|
|
88 |
throw new portfolio_caller_exception('mustprovidediscussionorpost', 'forum');
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
if (!$this->discussion = $DB->get_record('forum_discussions', $dbparams)) {
|
|
|
92 |
throw new portfolio_caller_exception('invaliddiscussionid', 'forum');
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
if (!$this->forum = $DB->get_record('forum', array('id' => $this->discussion->forum))) {
|
|
|
96 |
throw new portfolio_caller_exception('invalidforumid', 'forum');
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
if (!$this->cm = get_coursemodule_from_instance('forum', $this->forum->id)) {
|
|
|
100 |
throw new portfolio_caller_exception('invalidcoursemodule');
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
$this->modcontext = context_module::instance($this->cm->id);
|
|
|
104 |
$fs = get_file_storage();
|
|
|
105 |
if ($this->post) {
|
|
|
106 |
if ($this->attachment) {
|
|
|
107 |
// Make sure the requested file belongs to this post.
|
|
|
108 |
$file = $fs->get_file_by_id($this->attachment);
|
|
|
109 |
if ($file->get_contextid() != $this->modcontext->id
|
|
|
110 |
|| $file->get_itemid() != $this->post->id) {
|
|
|
111 |
throw new portfolio_caller_exception('filenotfound');
|
|
|
112 |
}
|
|
|
113 |
$this->set_file_and_format_data($this->attachment);
|
|
|
114 |
} else {
|
|
|
115 |
$attach = $fs->get_area_files($this->modcontext->id, 'mod_forum', 'attachment', $this->post->id, 'timemodified', false);
|
|
|
116 |
$embed = $fs->get_area_files($this->modcontext->id, 'mod_forum', 'post', $this->post->id, 'timemodified', false);
|
|
|
117 |
$files = array_merge($attach, $embed);
|
|
|
118 |
$this->set_file_and_format_data($files);
|
|
|
119 |
}
|
|
|
120 |
if (!empty($this->multifiles)) {
|
|
|
121 |
$this->keyedfiles[$this->post->id] = $this->multifiles;
|
|
|
122 |
} else if (!empty($this->singlefile)) {
|
|
|
123 |
$this->keyedfiles[$this->post->id] = array($this->singlefile);
|
|
|
124 |
}
|
|
|
125 |
} else { // whole thread
|
|
|
126 |
$fs = get_file_storage();
|
|
|
127 |
$this->posts = forum_get_all_discussion_posts($this->discussion->id, 'p.created ASC');
|
|
|
128 |
$this->multifiles = array();
|
|
|
129 |
foreach ($this->posts as $post) {
|
|
|
130 |
$attach = $fs->get_area_files($this->modcontext->id, 'mod_forum', 'attachment', $post->id, 'timemodified', false);
|
|
|
131 |
$embed = $fs->get_area_files($this->modcontext->id, 'mod_forum', 'post', $post->id, 'timemodified', false);
|
|
|
132 |
$files = array_merge($attach, $embed);
|
|
|
133 |
if ($files) {
|
|
|
134 |
$this->keyedfiles[$post->id] = $files;
|
|
|
135 |
} else {
|
|
|
136 |
continue;
|
|
|
137 |
}
|
|
|
138 |
$this->multifiles = array_merge($this->multifiles, array_values($this->keyedfiles[$post->id]));
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
if (empty($this->multifiles) && !empty($this->singlefile)) {
|
|
|
142 |
$this->multifiles = array($this->singlefile); // copy_files workaround
|
|
|
143 |
}
|
|
|
144 |
// depending on whether there are files or not, we might have to change richhtml/plainhtml
|
|
|
145 |
if (empty($this->attachment)) {
|
|
|
146 |
if (!empty($this->multifiles)) {
|
|
|
147 |
$this->add_format(PORTFOLIO_FORMAT_RICHHTML);
|
|
|
148 |
} else {
|
|
|
149 |
$this->add_format(PORTFOLIO_FORMAT_PLAINHTML);
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* @global object
|
|
|
156 |
* @return string
|
|
|
157 |
*/
|
|
|
158 |
function get_return_url() {
|
|
|
159 |
global $CFG;
|
|
|
160 |
return $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $this->discussion->id;
|
|
|
161 |
}
|
|
|
162 |
/**
|
|
|
163 |
* @global object
|
|
|
164 |
* @return array
|
|
|
165 |
*/
|
|
|
166 |
function get_navigation() {
|
|
|
167 |
global $CFG;
|
|
|
168 |
|
|
|
169 |
$navlinks = array();
|
|
|
170 |
$navlinks[] = array(
|
|
|
171 |
'name' => format_string($this->discussion->name),
|
|
|
172 |
'link' => $CFG->wwwroot . '/mod/forum/discuss.php?d=' . $this->discussion->id,
|
|
|
173 |
'type' => 'title'
|
|
|
174 |
);
|
|
|
175 |
return array($navlinks, $this->cm);
|
|
|
176 |
}
|
|
|
177 |
/**
|
|
|
178 |
* either a whole discussion
|
|
|
179 |
* a single post, with or without attachment
|
|
|
180 |
* or just an attachment with no post
|
|
|
181 |
*
|
|
|
182 |
* @global object
|
|
|
183 |
* @global object
|
|
|
184 |
* @uses PORTFOLIO_FORMAT_RICH
|
|
|
185 |
* @return mixed
|
|
|
186 |
*/
|
|
|
187 |
function prepare_package() {
|
|
|
188 |
global $CFG;
|
|
|
189 |
|
|
|
190 |
// set up the leap2a writer if we need it
|
|
|
191 |
$writingleap = false;
|
|
|
192 |
if ($this->exporter->get('formatclass') == PORTFOLIO_FORMAT_LEAP2A) {
|
|
|
193 |
$leapwriter = $this->exporter->get('format')->leap2a_writer();
|
|
|
194 |
$writingleap = true;
|
|
|
195 |
}
|
|
|
196 |
if ($this->attachment) { // simplest case first - single file attachment
|
|
|
197 |
$this->copy_files(array($this->singlefile), $this->attachment);
|
|
|
198 |
if ($writingleap) { // if we're writing leap, make the manifest to go along with the file
|
|
|
199 |
$entry = new portfolio_format_leap2a_file($this->singlefile->get_filename(), $this->singlefile);
|
|
|
200 |
$leapwriter->add_entry($entry);
|
|
|
201 |
return $this->exporter->write_new_file($leapwriter->to_xml(), $this->exporter->get('format')->manifest_name(), true);
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
} else if (empty($this->post)) { // exporting whole discussion
|
|
|
205 |
$content = ''; // if we're just writing HTML, start a string to add each post to
|
|
|
206 |
$ids = array(); // if we're writing leap2a, keep track of all entryids so we can add a selection element
|
|
|
207 |
foreach ($this->posts as $post) {
|
|
|
208 |
$posthtml = $this->prepare_post($post);
|
|
|
209 |
if ($writingleap) {
|
|
|
210 |
$ids[] = $this->prepare_post_leap2a($leapwriter, $post, $posthtml);
|
|
|
211 |
} else {
|
|
|
212 |
$content .= $posthtml . '<br /><br />';
|
|
|
213 |
}
|
|
|
214 |
}
|
|
|
215 |
$this->copy_files($this->multifiles);
|
|
|
216 |
$name = 'discussion.html';
|
|
|
217 |
$manifest = ($this->exporter->get('format') instanceof PORTFOLIO_FORMAT_RICH);
|
|
|
218 |
if ($writingleap) {
|
|
|
219 |
// add on an extra 'selection' entry
|
|
|
220 |
$selection = new portfolio_format_leap2a_entry('forumdiscussion' . $this->discussionid,
|
|
|
221 |
get_string('discussion', 'forum') . ': ' . $this->discussion->name, 'selection');
|
|
|
222 |
$leapwriter->add_entry($selection);
|
|
|
223 |
$leapwriter->make_selection($selection, $ids, 'Grouping');
|
|
|
224 |
$content = $leapwriter->to_xml();
|
|
|
225 |
$name = $this->get('exporter')->get('format')->manifest_name();
|
|
|
226 |
}
|
|
|
227 |
$this->get('exporter')->write_new_file($content, $name, $manifest);
|
|
|
228 |
|
|
|
229 |
} else { // exporting a single post
|
|
|
230 |
$posthtml = $this->prepare_post($this->post);
|
|
|
231 |
|
|
|
232 |
$content = $posthtml;
|
|
|
233 |
$name = 'post.html';
|
|
|
234 |
$manifest = ($this->exporter->get('format') instanceof PORTFOLIO_FORMAT_RICH);
|
|
|
235 |
|
|
|
236 |
if ($writingleap) {
|
|
|
237 |
$this->prepare_post_leap2a($leapwriter, $this->post, $posthtml);
|
|
|
238 |
$content = $leapwriter->to_xml();
|
|
|
239 |
$name = $this->exporter->get('format')->manifest_name();
|
|
|
240 |
}
|
|
|
241 |
$this->copy_files($this->multifiles);
|
|
|
242 |
$this->get('exporter')->write_new_file($content, $name, $manifest);
|
|
|
243 |
}
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
/**
|
|
|
247 |
* helper function to add a leap2a entry element
|
|
|
248 |
* that corresponds to a single forum post,
|
|
|
249 |
* including any attachments
|
|
|
250 |
*
|
|
|
251 |
* the entry/ies are added directly to the leapwriter, which is passed by ref
|
|
|
252 |
*
|
|
|
253 |
* @param portfolio_format_leap2a_writer $leapwriter writer object to add entries to
|
|
|
254 |
* @param object $post the stdclass object representing the database record
|
|
|
255 |
* @param string $posthtml the content of the post (prepared by {@link prepare_post}
|
|
|
256 |
*
|
|
|
257 |
* @return int id of new entry
|
|
|
258 |
*/
|
|
|
259 |
private function prepare_post_leap2a(portfolio_format_leap2a_writer $leapwriter, $post, $posthtml) {
|
|
|
260 |
$entry = new portfolio_format_leap2a_entry('forumpost' . $post->id, $post->subject, 'resource', $posthtml);
|
|
|
261 |
$entry->published = $post->created;
|
|
|
262 |
$entry->updated = $post->modified;
|
|
|
263 |
$entry->author = $post->author;
|
|
|
264 |
if (is_array($this->keyedfiles) && array_key_exists($post->id, $this->keyedfiles) && is_array($this->keyedfiles[$post->id])) {
|
|
|
265 |
$leapwriter->link_files($entry, $this->keyedfiles[$post->id], 'forumpost' . $post->id . 'attachment');
|
|
|
266 |
}
|
|
|
267 |
$entry->add_category('web', 'resource_type');
|
|
|
268 |
$leapwriter->add_entry($entry);
|
|
|
269 |
return $entry->id;
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
/**
|
|
|
273 |
* @param array $files
|
|
|
274 |
* @param mixed $justone false of id of single file to copy
|
|
|
275 |
* @return bool|void
|
|
|
276 |
*/
|
|
|
277 |
private function copy_files($files, $justone=false) {
|
|
|
278 |
if (empty($files)) {
|
|
|
279 |
return;
|
|
|
280 |
}
|
|
|
281 |
foreach ($files as $f) {
|
|
|
282 |
if ($justone && $f->get_id() != $justone) {
|
|
|
283 |
continue;
|
|
|
284 |
}
|
|
|
285 |
$this->get('exporter')->copy_existing_file($f);
|
|
|
286 |
if ($justone && $f->get_id() == $justone) {
|
|
|
287 |
return true; // all we need to do
|
|
|
288 |
}
|
|
|
289 |
}
|
|
|
290 |
}
|
|
|
291 |
/**
|
|
|
292 |
* this is a very cut down version of what is in forum_make_mail_post
|
|
|
293 |
*
|
|
|
294 |
* @global object
|
|
|
295 |
* @param int $post
|
|
|
296 |
* @return string
|
|
|
297 |
*/
|
|
|
298 |
private function prepare_post($post, $fileoutputextras=null) {
|
|
|
299 |
global $DB;
|
|
|
300 |
static $users;
|
|
|
301 |
if (empty($users)) {
|
|
|
302 |
$users = array($this->user->id => $this->user);
|
|
|
303 |
}
|
|
|
304 |
if (!array_key_exists($post->userid, $users)) {
|
|
|
305 |
$users[$post->userid] = $DB->get_record('user', array('id' => $post->userid));
|
|
|
306 |
}
|
|
|
307 |
// add the user object on to the post so we can pass it to the leap writer if necessary
|
|
|
308 |
$post->author = $users[$post->userid];
|
|
|
309 |
$viewfullnames = true;
|
|
|
310 |
// format the post body
|
|
|
311 |
$options = portfolio_format_text_options();
|
|
|
312 |
$options->context = $this->modcontext;
|
|
|
313 |
$format = $this->get('exporter')->get('format');
|
|
|
314 |
$formattedtext = format_text($post->message, $post->messageformat, $options);
|
|
|
315 |
$formattedtext = portfolio_rewrite_pluginfile_urls($formattedtext, $this->modcontext->id, 'mod_forum', 'post', $post->id, $format);
|
|
|
316 |
|
|
|
317 |
$output = '<table border="0" cellpadding="3" cellspacing="0" class="forumpost">';
|
|
|
318 |
|
|
|
319 |
$output .= '<tr class="header"><td>';// can't print picture.
|
|
|
320 |
$output .= '</td>';
|
|
|
321 |
|
|
|
322 |
if ($post->parent) {
|
|
|
323 |
$output .= '<td class="topic">';
|
|
|
324 |
} else {
|
|
|
325 |
$output .= '<td class="topic starter">';
|
|
|
326 |
}
|
|
|
327 |
$output .= '<div class="subject">'.format_string($post->subject).'</div>';
|
|
|
328 |
|
|
|
329 |
$fullname = fullname($users[$post->userid], $viewfullnames);
|
|
|
330 |
$by = new stdClass();
|
|
|
331 |
$by->name = $fullname;
|
|
|
332 |
$by->date = userdate($post->modified, '', core_date::get_user_timezone($this->user));
|
|
|
333 |
$output .= '<div class="author">'.get_string('bynameondate', 'forum', $by).'</div>';
|
|
|
334 |
|
|
|
335 |
$output .= '</td></tr>';
|
|
|
336 |
|
|
|
337 |
$output .= '<tr><td class="left side" valign="top">';
|
|
|
338 |
|
|
|
339 |
$output .= '</td><td class="content">';
|
|
|
340 |
|
|
|
341 |
$output .= $formattedtext;
|
|
|
342 |
|
|
|
343 |
if (is_array($this->keyedfiles) && array_key_exists($post->id, $this->keyedfiles) && is_array($this->keyedfiles[$post->id]) && count($this->keyedfiles[$post->id]) > 0) {
|
|
|
344 |
$output .= '<div class="attachments">';
|
|
|
345 |
$output .= '<br /><b>' . get_string('attachments', 'forum') . '</b>:<br /><br />';
|
|
|
346 |
foreach ($this->keyedfiles[$post->id] as $file) {
|
|
|
347 |
$output .= $format->file_output($file) . '<br/ >';
|
|
|
348 |
}
|
|
|
349 |
$output .= "</div>";
|
|
|
350 |
}
|
|
|
351 |
|
|
|
352 |
$output .= '</td></tr></table>'."\n\n";
|
|
|
353 |
|
|
|
354 |
return $output;
|
|
|
355 |
}
|
|
|
356 |
/**
|
|
|
357 |
* @return string
|
|
|
358 |
*/
|
|
|
359 |
function get_sha1() {
|
|
|
360 |
$filesha = '';
|
|
|
361 |
try {
|
|
|
362 |
$filesha = $this->get_sha1_file();
|
|
|
363 |
} catch (portfolio_caller_exception $e) { } // no files
|
|
|
364 |
|
|
|
365 |
if ($this->post) {
|
|
|
366 |
return sha1($filesha . ',' . $this->post->subject . ',' . $this->post->message);
|
|
|
367 |
} else {
|
|
|
368 |
$sha1s = array($filesha);
|
|
|
369 |
foreach ($this->posts as $post) {
|
|
|
370 |
$sha1s[] = sha1($post->subject . ',' . $post->message);
|
|
|
371 |
}
|
|
|
372 |
return sha1(implode(',', $sha1s));
|
|
|
373 |
}
|
|
|
374 |
}
|
|
|
375 |
|
|
|
376 |
function expected_time() {
|
|
|
377 |
$filetime = $this->expected_time_file();
|
|
|
378 |
if ($this->posts) {
|
|
|
379 |
$posttime = portfolio_expected_time_db(count($this->posts));
|
|
|
380 |
if ($filetime < $posttime) {
|
|
|
381 |
return $posttime;
|
|
|
382 |
}
|
|
|
383 |
}
|
|
|
384 |
return $filetime;
|
|
|
385 |
}
|
|
|
386 |
/**
|
|
|
387 |
* @uses CONTEXT_MODULE
|
|
|
388 |
* @return bool
|
|
|
389 |
*/
|
|
|
390 |
function check_permissions() {
|
|
|
391 |
$context = context_module::instance($this->cm->id);
|
|
|
392 |
if ($this->post) {
|
|
|
393 |
return (has_capability('mod/forum:exportpost', $context)
|
|
|
394 |
|| ($this->post->userid == $this->user->id
|
|
|
395 |
&& has_capability('mod/forum:exportownpost', $context)));
|
|
|
396 |
}
|
|
|
397 |
return has_capability('mod/forum:exportdiscussion', $context);
|
|
|
398 |
}
|
|
|
399 |
/**
|
|
|
400 |
* @return string
|
|
|
401 |
*/
|
|
|
402 |
public static function display_name() {
|
|
|
403 |
return get_string('modulename', 'forum');
|
|
|
404 |
}
|
|
|
405 |
|
|
|
406 |
public static function base_supported_formats() {
|
|
|
407 |
return array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_RICHHTML, PORTFOLIO_FORMAT_PLAINHTML, PORTFOLIO_FORMAT_LEAP2A);
|
|
|
408 |
}
|
|
|
409 |
}
|
|
|
410 |
|
|
|
411 |
|
|
|
412 |
/**
|
|
|
413 |
* Class representing the virtual node with all itemids in the file browser
|
|
|
414 |
*
|
|
|
415 |
* @category files
|
|
|
416 |
* @copyright 2012 David Mudrak <david@moodle.com>
|
|
|
417 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
418 |
*/
|
|
|
419 |
class forum_file_info_container extends file_info {
|
|
|
420 |
/** @var file_browser */
|
|
|
421 |
protected $browser;
|
|
|
422 |
/** @var stdClass */
|
|
|
423 |
protected $course;
|
|
|
424 |
/** @var stdClass */
|
|
|
425 |
protected $cm;
|
|
|
426 |
/** @var string */
|
|
|
427 |
protected $component;
|
|
|
428 |
/** @var stdClass */
|
|
|
429 |
protected $context;
|
|
|
430 |
/** @var array */
|
|
|
431 |
protected $areas;
|
|
|
432 |
/** @var string */
|
|
|
433 |
protected $filearea;
|
|
|
434 |
|
|
|
435 |
/**
|
|
|
436 |
* Constructor (in case you did not realize it ;-)
|
|
|
437 |
*
|
|
|
438 |
* @param file_browser $browser
|
|
|
439 |
* @param stdClass $course
|
|
|
440 |
* @param stdClass $cm
|
|
|
441 |
* @param stdClass $context
|
|
|
442 |
* @param array $areas
|
|
|
443 |
* @param string $filearea
|
|
|
444 |
*/
|
|
|
445 |
public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
|
|
|
446 |
parent::__construct($browser, $context);
|
|
|
447 |
$this->browser = $browser;
|
|
|
448 |
$this->course = $course;
|
|
|
449 |
$this->cm = $cm;
|
|
|
450 |
$this->component = 'mod_forum';
|
|
|
451 |
$this->context = $context;
|
|
|
452 |
$this->areas = $areas;
|
|
|
453 |
$this->filearea = $filearea;
|
|
|
454 |
}
|
|
|
455 |
|
|
|
456 |
/**
|
|
|
457 |
* @return array with keys contextid, filearea, itemid, filepath and filename
|
|
|
458 |
*/
|
|
|
459 |
public function get_params() {
|
|
|
460 |
return array(
|
|
|
461 |
'contextid' => $this->context->id,
|
|
|
462 |
'component' => $this->component,
|
|
|
463 |
'filearea' => $this->filearea,
|
|
|
464 |
'itemid' => null,
|
|
|
465 |
'filepath' => null,
|
|
|
466 |
'filename' => null,
|
|
|
467 |
);
|
|
|
468 |
}
|
|
|
469 |
|
|
|
470 |
/**
|
|
|
471 |
* Can new files or directories be added via the file browser
|
|
|
472 |
*
|
|
|
473 |
* @return bool
|
|
|
474 |
*/
|
|
|
475 |
public function is_writable() {
|
|
|
476 |
return false;
|
|
|
477 |
}
|
|
|
478 |
|
|
|
479 |
/**
|
|
|
480 |
* Should this node be considered as a folder in the file browser
|
|
|
481 |
*
|
|
|
482 |
* @return bool
|
|
|
483 |
*/
|
|
|
484 |
public function is_directory() {
|
|
|
485 |
return true;
|
|
|
486 |
}
|
|
|
487 |
|
|
|
488 |
/**
|
|
|
489 |
* Returns localised visible name of this node
|
|
|
490 |
*
|
|
|
491 |
* @return string
|
|
|
492 |
*/
|
|
|
493 |
public function get_visible_name() {
|
|
|
494 |
return $this->areas[$this->filearea];
|
|
|
495 |
}
|
|
|
496 |
|
|
|
497 |
/**
|
|
|
498 |
* Returns list of children nodes
|
|
|
499 |
*
|
|
|
500 |
* @return array of file_info instances
|
|
|
501 |
*/
|
|
|
502 |
public function get_children() {
|
|
|
503 |
return $this->get_filtered_children('*', false, true);
|
|
|
504 |
}
|
|
|
505 |
/**
|
|
|
506 |
* Help function to return files matching extensions or their count
|
|
|
507 |
*
|
|
|
508 |
* @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
|
|
|
509 |
* @param bool|int $countonly if false returns the children, if an int returns just the
|
|
|
510 |
* count of children but stops counting when $countonly number of children is reached
|
|
|
511 |
* @param bool $returnemptyfolders if true returns items that don't have matching files inside
|
|
|
512 |
* @return array|int array of file_info instances or the count
|
|
|
513 |
*/
|
|
|
514 |
private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
|
|
|
515 |
global $DB;
|
|
|
516 |
$params = array('contextid' => $this->context->id,
|
|
|
517 |
'component' => $this->component,
|
|
|
518 |
'filearea' => $this->filearea);
|
|
|
519 |
$sql = 'SELECT DISTINCT itemid
|
|
|
520 |
FROM {files}
|
|
|
521 |
WHERE contextid = :contextid
|
|
|
522 |
AND component = :component
|
|
|
523 |
AND filearea = :filearea';
|
|
|
524 |
if (!$returnemptyfolders) {
|
|
|
525 |
$sql .= ' AND filename <> :emptyfilename';
|
|
|
526 |
$params['emptyfilename'] = '.';
|
|
|
527 |
}
|
|
|
528 |
list($sql2, $params2) = $this->build_search_files_sql($extensions);
|
|
|
529 |
$sql .= ' '.$sql2;
|
|
|
530 |
$params = array_merge($params, $params2);
|
|
|
531 |
if ($countonly !== false) {
|
|
|
532 |
$sql .= ' ORDER BY itemid DESC';
|
|
|
533 |
}
|
|
|
534 |
|
|
|
535 |
$rs = $DB->get_recordset_sql($sql, $params);
|
|
|
536 |
$children = array();
|
|
|
537 |
foreach ($rs as $record) {
|
|
|
538 |
if (($child = $this->browser->get_file_info($this->context, 'mod_forum', $this->filearea, $record->itemid))
|
|
|
539 |
&& ($returnemptyfolders || $child->count_non_empty_children($extensions))) {
|
|
|
540 |
$children[] = $child;
|
|
|
541 |
}
|
|
|
542 |
if ($countonly !== false && count($children) >= $countonly) {
|
|
|
543 |
break;
|
|
|
544 |
}
|
|
|
545 |
}
|
|
|
546 |
$rs->close();
|
|
|
547 |
if ($countonly !== false) {
|
|
|
548 |
return count($children);
|
|
|
549 |
}
|
|
|
550 |
return $children;
|
|
|
551 |
}
|
|
|
552 |
|
|
|
553 |
/**
|
|
|
554 |
* Returns list of children which are either files matching the specified extensions
|
|
|
555 |
* or folders that contain at least one such file.
|
|
|
556 |
*
|
|
|
557 |
* @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
|
|
|
558 |
* @return array of file_info instances
|
|
|
559 |
*/
|
|
|
560 |
public function get_non_empty_children($extensions = '*') {
|
|
|
561 |
return $this->get_filtered_children($extensions, false);
|
|
|
562 |
}
|
|
|
563 |
|
|
|
564 |
/**
|
|
|
565 |
* Returns the number of children which are either files matching the specified extensions
|
|
|
566 |
* or folders containing at least one such file.
|
|
|
567 |
*
|
|
|
568 |
* @param string|array $extensions, for example '*' or array('.gif','.jpg')
|
|
|
569 |
* @param int $limit stop counting after at least $limit non-empty children are found
|
|
|
570 |
* @return int
|
|
|
571 |
*/
|
|
|
572 |
public function count_non_empty_children($extensions = '*', $limit = 1) {
|
|
|
573 |
return $this->get_filtered_children($extensions, $limit);
|
|
|
574 |
}
|
|
|
575 |
|
|
|
576 |
/**
|
|
|
577 |
* Returns parent file_info instance
|
|
|
578 |
*
|
|
|
579 |
* @return file_info or null for root
|
|
|
580 |
*/
|
|
|
581 |
public function get_parent() {
|
|
|
582 |
return $this->browser->get_file_info($this->context);
|
|
|
583 |
}
|
|
|
584 |
}
|
|
|
585 |
|
|
|
586 |
/**
|
|
|
587 |
* Returns forum posts tagged with a specified tag.
|
|
|
588 |
*
|
|
|
589 |
* This is a callback used by the tag area mod_forum/forum_posts to search for forum posts
|
|
|
590 |
* tagged with a specific tag.
|
|
|
591 |
*
|
|
|
592 |
* @param core_tag_tag $tag
|
|
|
593 |
* @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
|
|
|
594 |
* are displayed on the page and the per-page limit may be bigger
|
|
|
595 |
* @param int $fromctx context id where the link was displayed, may be used by callbacks
|
|
|
596 |
* to display items in the same context first
|
|
|
597 |
* @param int $ctx context id where to search for records
|
|
|
598 |
* @param bool $rec search in subcontexts as well
|
|
|
599 |
* @param int $page 0-based number of page being displayed
|
|
|
600 |
* @return \core_tag\output\tagindex
|
|
|
601 |
*/
|
|
|
602 |
function mod_forum_get_tagged_posts($tag, $exclusivemode = false, $fromctx = 0, $ctx = 0, $rec = 1, $page = 0) {
|
|
|
603 |
global $OUTPUT;
|
|
|
604 |
$perpage = $exclusivemode ? 20 : 5;
|
|
|
605 |
|
|
|
606 |
// Build the SQL query.
|
|
|
607 |
$ctxselect = context_helper::get_preload_record_columns_sql('ctx');
|
|
|
608 |
$query = "SELECT fp.id, fp.subject, fd.forum, fp.discussion, f.type, fd.timestart, fd.timeend, fd.groupid, fd.firstpost,
|
|
|
609 |
fp.parent, fp.userid,
|
|
|
610 |
cm.id AS cmid, c.id AS courseid, c.shortname, c.fullname, $ctxselect
|
|
|
611 |
FROM {forum_posts} fp
|
|
|
612 |
JOIN {forum_discussions} fd ON fp.discussion = fd.id
|
|
|
613 |
JOIN {forum} f ON f.id = fd.forum
|
|
|
614 |
JOIN {modules} m ON m.name='forum'
|
|
|
615 |
JOIN {course_modules} cm ON cm.module = m.id AND cm.instance = f.id
|
|
|
616 |
JOIN {tag_instance} tt ON fp.id = tt.itemid
|
|
|
617 |
JOIN {course} c ON cm.course = c.id
|
|
|
618 |
JOIN {context} ctx ON ctx.instanceid = cm.id AND ctx.contextlevel = :coursemodulecontextlevel
|
|
|
619 |
WHERE tt.itemtype = :itemtype AND tt.tagid = :tagid AND tt.component = :component
|
|
|
620 |
AND cm.deletioninprogress = 0
|
|
|
621 |
AND fp.id %ITEMFILTER% AND c.id %COURSEFILTER%";
|
|
|
622 |
|
|
|
623 |
$params = array('itemtype' => 'forum_posts', 'tagid' => $tag->id, 'component' => 'mod_forum',
|
|
|
624 |
'coursemodulecontextlevel' => CONTEXT_MODULE);
|
|
|
625 |
|
|
|
626 |
if ($ctx) {
|
|
|
627 |
$context = $ctx ? context::instance_by_id($ctx) : context_system::instance();
|
|
|
628 |
$query .= $rec ? ' AND (ctx.id = :contextid OR ctx.path LIKE :path)' : ' AND ctx.id = :contextid';
|
|
|
629 |
$params['contextid'] = $context->id;
|
|
|
630 |
$params['path'] = $context->path.'/%';
|
|
|
631 |
}
|
|
|
632 |
|
|
|
633 |
$query .= " ORDER BY ";
|
|
|
634 |
if ($fromctx) {
|
|
|
635 |
// In order-clause specify that modules from inside "fromctx" context should be returned first.
|
|
|
636 |
$fromcontext = context::instance_by_id($fromctx);
|
|
|
637 |
$query .= ' (CASE WHEN ctx.id = :fromcontextid OR ctx.path LIKE :frompath THEN 0 ELSE 1 END),';
|
|
|
638 |
$params['fromcontextid'] = $fromcontext->id;
|
|
|
639 |
$params['frompath'] = $fromcontext->path.'/%';
|
|
|
640 |
}
|
|
|
641 |
$query .= ' c.sortorder, cm.id, fp.id';
|
|
|
642 |
|
|
|
643 |
$totalpages = $page + 1;
|
|
|
644 |
|
|
|
645 |
// Use core_tag_index_builder to build and filter the list of items.
|
|
|
646 |
$builder = new core_tag_index_builder('mod_forum', 'forum_posts', $query, $params, $page * $perpage, $perpage + 1);
|
|
|
647 |
while ($item = $builder->has_item_that_needs_access_check()) {
|
|
|
648 |
context_helper::preload_from_record($item);
|
|
|
649 |
$courseid = $item->courseid;
|
|
|
650 |
if (!$builder->can_access_course($courseid)) {
|
|
|
651 |
$builder->set_accessible($item, false);
|
|
|
652 |
continue;
|
|
|
653 |
}
|
|
|
654 |
$modinfo = get_fast_modinfo($builder->get_course($courseid));
|
|
|
655 |
// Set accessibility of this item and all other items in the same course.
|
|
|
656 |
$builder->walk(function ($taggeditem) use ($courseid, $modinfo, $builder, $item) {
|
|
|
657 |
// Checking permission for Q&A forums performs additional DB queries, do not do them in bulk.
|
|
|
658 |
if ($taggeditem->courseid == $courseid && ($taggeditem->type != 'qanda' || $taggeditem->id == $item->id)) {
|
|
|
659 |
$cm = $modinfo->get_cm($taggeditem->cmid);
|
|
|
660 |
$forum = (object)['id' => $taggeditem->forum,
|
|
|
661 |
'course' => $taggeditem->courseid,
|
|
|
662 |
'type' => $taggeditem->type
|
|
|
663 |
];
|
|
|
664 |
$discussion = (object)['id' => $taggeditem->discussion,
|
|
|
665 |
'timestart' => $taggeditem->timestart,
|
|
|
666 |
'timeend' => $taggeditem->timeend,
|
|
|
667 |
'groupid' => $taggeditem->groupid,
|
|
|
668 |
'firstpost' => $taggeditem->firstpost
|
|
|
669 |
];
|
|
|
670 |
$post = (object)['id' => $taggeditem->id,
|
|
|
671 |
'parent' => $taggeditem->parent,
|
|
|
672 |
'userid' => $taggeditem->userid,
|
|
|
673 |
'groupid' => $taggeditem->groupid
|
|
|
674 |
];
|
|
|
675 |
|
|
|
676 |
$accessible = forum_user_can_see_post($forum, $discussion, $post, null, $cm);
|
|
|
677 |
$builder->set_accessible($taggeditem, $accessible);
|
|
|
678 |
}
|
|
|
679 |
});
|
|
|
680 |
}
|
|
|
681 |
|
|
|
682 |
$items = $builder->get_items();
|
|
|
683 |
if (count($items) > $perpage) {
|
|
|
684 |
$totalpages = $page + 2; // We don't need exact page count, just indicate that the next page exists.
|
|
|
685 |
array_pop($items);
|
|
|
686 |
}
|
|
|
687 |
|
|
|
688 |
// Build the display contents.
|
|
|
689 |
if ($items) {
|
|
|
690 |
$tagfeed = new core_tag\output\tagfeed();
|
|
|
691 |
foreach ($items as $item) {
|
|
|
692 |
context_helper::preload_from_record($item);
|
|
|
693 |
$modinfo = get_fast_modinfo($item->courseid);
|
|
|
694 |
$cm = $modinfo->get_cm($item->cmid);
|
|
|
695 |
$pageurl = new moodle_url('/mod/forum/discuss.php', array('d' => $item->discussion), 'p' . $item->id);
|
|
|
696 |
$pagename = format_string($item->subject, true, array('context' => context_module::instance($item->cmid)));
|
|
|
697 |
$pagename = html_writer::link($pageurl, $pagename);
|
|
|
698 |
$courseurl = course_get_url($item->courseid, $cm->sectionnum);
|
|
|
699 |
$cmname = html_writer::link($cm->url, $cm->get_formatted_name());
|
|
|
700 |
$coursename = format_string($item->fullname, true, array('context' => context_course::instance($item->courseid)));
|
|
|
701 |
$coursename = html_writer::link($courseurl, $coursename);
|
|
|
702 |
$icon = html_writer::link($pageurl, html_writer::empty_tag('img', array('src' => $cm->get_icon_url())));
|
|
|
703 |
$tagfeed->add($icon, $pagename, $cmname.'<br>'.$coursename);
|
|
|
704 |
}
|
|
|
705 |
|
|
|
706 |
$content = $OUTPUT->render_from_template('core_tag/tagfeed',
|
|
|
707 |
$tagfeed->export_for_template($OUTPUT));
|
|
|
708 |
|
|
|
709 |
return new core_tag\output\tagindex($tag, 'mod_forum', 'forum_posts', $content,
|
|
|
710 |
$exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages);
|
|
|
711 |
}
|
|
|
712 |
}
|
|
|
713 |
|
|
|
714 |
/**
|
|
|
715 |
* Update the calendar entries for this forum activity.
|
|
|
716 |
*
|
|
|
717 |
* @param stdClass $forum the row from the database table forum.
|
|
|
718 |
* @param int $cmid The coursemodule id
|
|
|
719 |
* @return bool
|
|
|
720 |
*/
|
|
|
721 |
function forum_update_calendar($forum, $cmid) {
|
|
|
722 |
global $DB, $CFG;
|
|
|
723 |
|
|
|
724 |
require_once($CFG->dirroot.'/calendar/lib.php');
|
|
|
725 |
|
|
|
726 |
$event = new stdClass();
|
|
|
727 |
|
|
|
728 |
if (!empty($forum->duedate)) {
|
|
|
729 |
$event->name = get_string('calendardue', 'forum', $forum->name);
|
|
|
730 |
$event->description = format_module_intro('forum', $forum, $cmid, false);
|
|
|
731 |
$event->format = FORMAT_HTML;
|
|
|
732 |
$event->courseid = $forum->course;
|
|
|
733 |
$event->modulename = 'forum';
|
|
|
734 |
$event->instance = $forum->id;
|
|
|
735 |
$event->type = CALENDAR_EVENT_TYPE_ACTION;
|
|
|
736 |
$event->eventtype = FORUM_EVENT_TYPE_DUE;
|
|
|
737 |
$event->timestart = $forum->duedate;
|
|
|
738 |
$event->timesort = $forum->duedate;
|
|
|
739 |
$event->visible = instance_is_visible('forum', $forum);
|
|
|
740 |
}
|
|
|
741 |
|
|
|
742 |
$event->id = $DB->get_field('event', 'id',
|
|
|
743 |
array('modulename' => 'forum', 'instance' => $forum->id, 'eventtype' => FORUM_EVENT_TYPE_DUE));
|
|
|
744 |
|
|
|
745 |
if ($event->id) {
|
|
|
746 |
$calendarevent = calendar_event::load($event->id);
|
|
|
747 |
if (!empty($forum->duedate)) {
|
|
|
748 |
// Calendar event exists so update it.
|
|
|
749 |
$calendarevent->update($event);
|
|
|
750 |
} else {
|
|
|
751 |
// Calendar event is no longer needed.
|
|
|
752 |
$calendarevent->delete();
|
|
|
753 |
}
|
|
|
754 |
} else if (!empty($forum->duedate)) {
|
|
|
755 |
// Event doesn't exist so create one.
|
|
|
756 |
calendar_event::create($event);
|
|
|
757 |
}
|
|
|
758 |
|
|
|
759 |
return true;
|
|
|
760 |
}
|