Commit | Line | Data |
---|---|---|
cae83708 | 1 | <?php |
2 | ||
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
5 | // Moodle is free software: you can redistribute it and/or modify | |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
cae83708 | 18 | /** |
19 | * Classes for Blogs. | |
20 | * | |
21 | * @package moodlecore | |
22 | * @subpackage blog | |
23 | * @copyright 2009 Nicolas Connault | |
24 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
25 | */ | |
26 | ||
35716b86 | 27 | defined('MOODLE_INTERNAL') || die(); |
cae83708 | 28 | |
99d19c13 PS |
29 | require_once($CFG->libdir . '/filelib.php'); |
30 | ||
cae83708 | 31 | /** |
32 | * Blog_entry class. Represents an entry in a user's blog. Contains all methods for managing this entry. | |
33 | * This class does not contain any HTML-generating code. See blog_listing sub-classes for such code. | |
34 | * This class follows the Object Relational Mapping technique, its member variables being mapped to | |
1c7b8b93 | 35 | * the fields of the post table. |
cae83708 | 36 | * |
37 | * @package moodlecore | |
38 | * @subpackage blog | |
39 | * @copyright 2009 Nicolas Connault | |
40 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
41 | */ | |
2591c7ae | 42 | class blog_entry implements renderable { |
cae83708 | 43 | // Public Database fields |
44 | public $id; | |
45 | public $userid; | |
46 | public $subject; | |
47 | public $summary; | |
1c7b8b93 NC |
48 | public $rating = 0; |
49 | public $attachment; | |
cae83708 | 50 | public $publishstate; |
51 | ||
52 | // Locked Database fields (Don't touch these) | |
1c7b8b93 NC |
53 | public $courseid = 0; |
54 | public $groupid = 0; | |
55 | public $module = 'blog'; | |
56 | public $moduleid = 0; | |
57 | public $coursemoduleid = 0; | |
cae83708 | 58 | public $content; |
59 | public $format = 1; | |
1c7b8b93 | 60 | public $uniquehash = ''; |
cae83708 | 61 | public $lastmodified; |
62 | public $created; | |
63 | public $usermodified; | |
64 | ||
65 | // Other class variables | |
66 | public $form; | |
67 | public $tags = array(); | |
68 | ||
f8133217 | 69 | /** @var StdClass Data needed to render the entry */ |
2591c7ae DM |
70 | public $renderable; |
71 | ||
cae83708 | 72 | // Methods |
73 | /** | |
74 | * Constructor. If given an id, will fetch the corresponding record from the DB. | |
75 | * | |
b73d1ca4 | 76 | * @param mixed $idorparams A blog entry id if INT, or data for a new entry if array |
cae83708 | 77 | */ |
1c7b8b93 | 78 | public function __construct($id=null, $params=null, $form=null) { |
af7e05d6 | 79 | global $DB, $PAGE, $CFG; |
cae83708 | 80 | |
1c7b8b93 NC |
81 | if (!empty($id)) { |
82 | $object = $DB->get_record('post', array('id' => $id)); | |
cae83708 | 83 | foreach ($object as $var => $val) { |
84 | $this->$var = $val; | |
85 | } | |
1c7b8b93 NC |
86 | } else if (!empty($params) && (is_array($params) || is_object($params))) { |
87 | foreach ($params as $var => $val) { | |
cae83708 | 88 | $this->$var = $val; |
89 | } | |
90 | } | |
91 | ||
af7e05d6 EL |
92 | if (!empty($CFG->useblogassociations)) { |
93 | $associations = $DB->get_records('blog_association', array('blogid' => $this->id)); | |
94 | foreach ($associations as $association) { | |
95 | $context = context::instance_by_id($association->contextid); | |
96 | if ($context->contextlevel == CONTEXT_COURSE) { | |
97 | $this->courseassoc = $association->contextid; | |
98 | } else if ($context->contextlevel == CONTEXT_MODULE) { | |
99 | $this->modassoc = $association->contextid; | |
100 | } | |
101 | } | |
102 | } | |
103 | ||
cae83708 | 104 | $this->form = $form; |
105 | } | |
106 | ||
2591c7ae | 107 | |
cae83708 | 108 | /** |
2591c7ae | 109 | * Gets the required data to print the entry |
cae83708 | 110 | */ |
2591c7ae | 111 | public function prepare_render() { |
cae83708 | 112 | |
2591c7ae | 113 | global $DB, $CFG, $PAGE; |
b73d1ca4 | 114 | |
2591c7ae | 115 | $this->renderable = new StdClass(); |
cae83708 | 116 | |
2591c7ae | 117 | $this->renderable->user = $DB->get_record('user', array('id'=>$this->userid)); |
cae83708 | 118 | |
af7e05d6 EL |
119 | // Entry comments. |
120 | if (!empty($CFG->usecomments) and $CFG->blogusecomments) { | |
2591c7ae | 121 | require_once($CFG->dirroot . '/comment/lib.php'); |
af7e05d6 EL |
122 | |
123 | $cmt = new stdClass(); | |
124 | $cmt->context = context_user::instance($this->userid); | |
125 | $cmt->courseid = $PAGE->course->id; | |
126 | $cmt->area = 'format_blog'; | |
127 | $cmt->itemid = $this->id; | |
128 | $cmt->showcount = $CFG->blogshowcommentscount; | |
129 | $cmt->component = 'blog'; | |
130 | $this->renderable->comment = new comment($cmt); | |
cae83708 | 131 | } |
2591c7ae | 132 | |
af7e05d6 EL |
133 | $this->summary = file_rewrite_pluginfile_urls($this->summary, 'pluginfile.php', SYSCONTEXTID, 'blog', 'post', $this->id); |
134 | ||
135 | // External blog link. | |
136 | if ($this->uniquehash && $this->content) { | |
137 | if ($externalblog = $DB->get_record('blog_external', array('id' => $this->content))) { | |
138 | $urlparts = parse_url($externalblog->url); | |
f8133217 | 139 | $this->renderable->externalblogtext = get_string('retrievedfrom', 'blog') . get_string('labelsep', 'langconfig'); |
af7e05d6 EL |
140 | $this->renderable->externalblogtext .= html_writer::link($urlparts['scheme'] . '://'.$urlparts['host'], $externalblog->name); |
141 | } | |
cae83708 | 142 | } |
f8133217 DM |
143 | |
144 | // Retrieve associations | |
145 | $this->renderable->unassociatedentry = false; | |
146 | if (!empty($CFG->useblogassociations)) { | |
af7e05d6 | 147 | |
f8133217 DM |
148 | // Adding the entry associations data. |
149 | if ($associations = $associations = $DB->get_records('blog_association', array('blogid' => $this->id))) { | |
af7e05d6 EL |
150 | |
151 | // Check to see if the entry is unassociated with group/course level access. | |
152 | if ($this->publishstate == 'group' || $this->publishstate == 'course') { | |
153 | $this->renderable->unassociatedentry = true; | |
f8133217 | 154 | } |
af7e05d6 EL |
155 | |
156 | foreach ($associations as $key => $assocrec) { | |
1c7b8b93 | 157 | |
f8133217 DM |
158 | if (!$context = context::instance_by_id($assocrec->contextid, IGNORE_MISSING)) { |
159 | unset($associations[$key]); | |
160 | continue; | |
161 | } | |
2591c7ae | 162 | |
f8133217 DM |
163 | // The renderer will need the contextlevel of the association. |
164 | $associations[$key]->contextlevel = $context->contextlevel; | |
af7e05d6 EL |
165 | |
166 | // Course associations. | |
2591c7ae DM |
167 | if ($context->contextlevel == CONTEXT_COURSE) { |
168 | $instancename = $DB->get_field('course', 'shortname', array('id' => $context->instanceid)); //TODO: performance!!!! | |
f8133217 | 169 | |
2591c7ae DM |
170 | $associations[$key]->url = $assocurl = new moodle_url('/course/view.php', array('id' => $context->instanceid)); |
171 | $associations[$key]->text = $instancename; | |
af7e05d6 | 172 | $associations[$key]->icon = new pix_icon('i/course', $associations[$key]->text); |
5f4d4d80 | 173 | } |
174 | ||
f8133217 | 175 | // Mod associations. |
2591c7ae | 176 | if ($context->contextlevel == CONTEXT_MODULE) { |
cae83708 | 177 | |
f8133217 DM |
178 | // Getting the activity type and the activity instance id |
179 | $sql = 'SELECT cm.instance, m.name FROM {course_modules} cm | |
180 | JOIN {modules} m ON m.id = cm.module | |
181 | WHERE cm.id = :cmid'; | |
182 | $modinfo = $DB->get_record_sql($sql, array('cmid' => $context->instanceid)); | |
183 | $instancename = $DB->get_field($modinfo->name, 'name', array('id' => $modinfo->instance)); //TODO: performance!!!! | |
cae83708 | 184 | |
f8133217 DM |
185 | $associations[$key]->type = get_string('modulename', $modinfo->name); |
186 | $associations[$key]->url = new moodle_url('/mod/' . $modinfo->name . '/view.php', array('id' => $context->instanceid)); | |
2591c7ae | 187 | $associations[$key]->text = $instancename; |
f8133217 | 188 | $associations[$key]->icon = new pix_icon('icon', $associations[$key]->text, $modinfo->name); |
af7e05d6 | 189 | } |
cae83708 | 190 | } |
191 | } | |
af7e05d6 EL |
192 | $this->renderable->blogassociations = $associations; |
193 | } | |
cae83708 | 194 | |
f8133217 | 195 | // Entry attachments. |
2591c7ae | 196 | $this->renderable->attachments = $this->get_attachments(); |
cae83708 | 197 | |
2591c7ae DM |
198 | $this->renderable->usercanedit = blog_user_can_edit_entry($this); |
199 | } | |
cae83708 | 200 | |
cae83708 | 201 | |
2591c7ae DM |
202 | /** |
203 | * Gets the entry attachments list | |
204 | * @return array List of blog_entry_attachment instances | |
205 | */ | |
206 | function get_attachments() { | |
207 | ||
af7e05d6 EL |
208 | global $CFG; |
209 | ||
210 | require_once($CFG->libdir.'/filelib.php'); | |
211 | ||
f8133217 | 212 | $syscontext = context_system::instance(); |
af7e05d6 | 213 | |
2591c7ae | 214 | $fs = get_file_storage(); |
af7e05d6 | 215 | $files = $fs->get_area_files($syscontext->id, 'blog', 'attachment', $this->id); |
cae83708 | 216 | |
f8133217 | 217 | // Adding a blog_entry_attachment for each non-directory file. |
af7e05d6 EL |
218 | $attachments = array(); |
219 | foreach ($files as $file) { | |
220 | if ($file->is_directory()) { | |
221 | continue; | |
4def8463 | 222 | } |
af7e05d6 EL |
223 | $attachments[] = new blog_entry_attachment($file, $this->id); |
224 | } | |
cae83708 | 225 | |
2591c7ae | 226 | return $attachments; |
cae83708 | 227 | } |
228 | ||
229 | /** | |
230 | * Inserts this entry in the database. Access control checks must be done by calling code. | |
231 | * | |
232 | * @param mform $form Used for attachments | |
233 | * @return void | |
234 | */ | |
235 | public function process_attachment($form) { | |
236 | $this->form = $form; | |
237 | } | |
238 | ||
239 | /** | |
240 | * Inserts this entry in the database. Access control checks must be done by calling code. | |
241 | * TODO Set the publishstate correctly | |
cae83708 | 242 | * @return void |
243 | */ | |
244 | public function add() { | |
245 | global $CFG, $USER, $DB; | |
246 | ||
247 | unset($this->id); | |
248 | $this->module = 'blog'; | |
249 | $this->userid = (empty($this->userid)) ? $USER->id : $this->userid; | |
250 | $this->lastmodified = time(); | |
251 | $this->created = time(); | |
252 | ||
253 | // Insert the new blog entry. | |
9d97f08e | 254 | $this->id = $DB->insert_record('post', $this); |
cae83708 | 255 | |
9d97f08e PS |
256 | // Update tags. |
257 | $this->add_tags_info(); | |
cae83708 | 258 | |
9d97f08e PS |
259 | if (!empty($CFG->useblogassociations)) { |
260 | $this->add_associations(); | |
cae83708 | 261 | } |
9d97f08e PS |
262 | |
263 | tag_set('post', $this->id, $this->tags); | |
3049780a AA |
264 | |
265 | // Trigger an event for the new entry. | |
77037e27 AA |
266 | $event = \core\event\blog_entry_created::create(array( |
267 | 'objectid' => $this->id, | |
268 | 'relateduserid' => $this->userid, | |
269 | 'other' => array('subject' => $this->subject) | |
270 | )); | |
3049780a AA |
271 | $event->set_custom_data($this); |
272 | $event->trigger(); | |
cae83708 | 273 | } |
274 | ||
275 | /** | |
276 | * Updates this entry in the database. Access control checks must be done by calling code. | |
277 | * | |
32dea439 AA |
278 | * @param array $params Entry parameters. |
279 | * @param moodleform $form Used for attachments. | |
280 | * @param array $summaryoptions Summary options. | |
281 | * @param array $attachmentoptions Attachment options. | |
282 | * | |
cae83708 | 283 | * @return void |
284 | */ | |
1c7b8b93 | 285 | public function edit($params=array(), $form=null, $summaryoptions=array(), $attachmentoptions=array()) { |
32dea439 | 286 | global $CFG, $DB; |
cae83708 | 287 | |
41b38360 | 288 | $sitecontext = context_system::instance(); |
1c7b8b93 NC |
289 | $entry = $this; |
290 | ||
cae83708 | 291 | $this->form = $form; |
292 | foreach ($params as $var => $val) { | |
1c7b8b93 | 293 | $entry->$var = $val; |
cae83708 | 294 | } |
295 | ||
64f93798 PS |
296 | $entry = file_postupdate_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id); |
297 | $entry = file_postupdate_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog', 'attachment', $entry->id); | |
b73d1ca4 | 298 | |
5f4d4d80 | 299 | if (!empty($CFG->useblogassociations)) { |
1c7b8b93 | 300 | $entry->add_associations(); |
cae83708 | 301 | } |
302 | ||
1c7b8b93 NC |
303 | $entry->lastmodified = time(); |
304 | ||
32dea439 | 305 | // Update record. |
1c7b8b93 NC |
306 | $DB->update_record('post', $entry); |
307 | tag_set('post', $entry->id, $entry->tags); | |
cae83708 | 308 | |
32dea439 AA |
309 | $event = \core\event\blog_entry_updated::create(array( |
310 | 'objectid' => $entry->id, | |
311 | 'relateduserid' => $entry->userid, | |
312 | 'other' => array('subject' => $entry->subject) | |
313 | )); | |
314 | $event->set_custom_data($entry); | |
315 | $event->trigger(); | |
cae83708 | 316 | } |
317 | ||
318 | /** | |
319 | * Deletes this entry from the database. Access control checks must be done by calling code. | |
320 | * | |
321 | * @return void | |
322 | */ | |
323 | public function delete() { | |
ac31c38e | 324 | global $DB; |
cae83708 | 325 | |
cae83708 | 326 | $this->delete_attachments(); |
ac31c38e | 327 | $this->remove_associations(); |
cae83708 | 328 | |
6c66b7f3 AA |
329 | // Get record to pass onto the event. |
330 | $record = $DB->get_record('post', array('id' => $this->id)); | |
1c7b8b93 NC |
331 | $DB->delete_records('post', array('id' => $this->id)); |
332 | tag_set('post', $this->id, array()); | |
cae83708 | 333 | |
77037e27 AA |
334 | $event = \core\event\blog_entry_deleted::create(array( |
335 | 'objectid' => $this->id, | |
336 | 'relateduserid' => $this->userid, | |
337 | 'other' => array('record' => (array) $record) | |
338 | )); | |
6c66b7f3 AA |
339 | $event->add_record_snapshot("post", $record); |
340 | $event->set_custom_data($this); | |
341 | $event->trigger(); | |
cae83708 | 342 | } |
343 | ||
344 | /** | |
345 | * function to add all context associations to an entry | |
346 | * @param int entry - data object processed to include all 'entry' fields and extra data from the edit_form object | |
347 | */ | |
1c7b8b93 | 348 | public function add_associations($action='add') { |
cae83708 | 349 | global $DB, $USER; |
350 | ||
cae83708 | 351 | $this->remove_associations(); |
352 | ||
353 | if (!empty($this->courseassoc)) { | |
1c7b8b93 | 354 | $this->add_association($this->courseassoc, $action); |
cae83708 | 355 | } |
356 | ||
357 | if (!empty($this->modassoc)) { | |
1c7b8b93 | 358 | $this->add_association($this->modassoc, $action); |
cae83708 | 359 | } |
360 | } | |
361 | ||
362 | /** | |
363 | * add a single association for a blog entry | |
364 | * @param int contextid - id of context to associate with the blog entry | |
365 | */ | |
1c7b8b93 NC |
366 | public function add_association($contextid, $action='add') { |
367 | global $DB, $USER; | |
cae83708 | 368 | |
1c7b8b93 NC |
369 | $assocobject = new StdClass; |
370 | $assocobject->contextid = $contextid; | |
371 | $assocobject->blogid = $this->id; | |
372 | $DB->insert_record('blog_association', $assocobject); | |
373 | ||
41b38360 | 374 | $context = context::instance_by_id($contextid); |
1c7b8b93 NC |
375 | $courseid = null; |
376 | ||
377 | if ($context->contextlevel == CONTEXT_COURSE) { | |
378 | $courseid = $context->instanceid; | |
379 | add_to_log($courseid, 'blog', $action, 'index.php?userid='.$this->userid.'&entryid='.$this->id, $this->subject); | |
380 | } else if ($context->contextlevel == CONTEXT_MODULE) { | |
381 | $cm = $DB->get_record('course_modules', array('id' => $context->instanceid)); | |
382 | $modulename = $DB->get_field('modules', 'name', array('id' => $cm->module)); | |
383 | add_to_log($cm->course, 'blog', $action, 'index.php?userid='.$this->userid.'&entryid='.$this->id, $this->subject, $cm->id, $this->userid); | |
384 | } | |
cae83708 | 385 | } |
386 | ||
387 | /** | |
388 | * remove all associations for a blog entry | |
389 | * @return voic | |
390 | */ | |
391 | public function remove_associations() { | |
392 | global $DB; | |
393 | $DB->delete_records('blog_association', array('blogid' => $this->id)); | |
394 | } | |
395 | ||
396 | /** | |
397 | * Deletes all the user files in the attachments area for an entry | |
398 | * | |
399 | * @return void | |
400 | */ | |
401 | public function delete_attachments() { | |
402 | $fs = get_file_storage(); | |
64f93798 PS |
403 | $fs->delete_area_files(SYSCONTEXTID, 'blog', 'attachment', $this->id); |
404 | $fs->delete_area_files(SYSCONTEXTID, 'blog', 'post', $this->id); | |
cae83708 | 405 | } |
406 | ||
cae83708 | 407 | /** |
408 | * function to attach tags into an entry | |
409 | * @return void | |
410 | */ | |
411 | public function add_tags_info() { | |
412 | ||
413 | $tags = array(); | |
414 | ||
415 | if ($otags = optional_param('otags', '', PARAM_INT)) { | |
416 | foreach ($otags as $tagid) { | |
417 | // TODO : make this use the tag name in the form | |
418 | if ($tag = tag_get('id', $tagid)) { | |
419 | $tags[] = $tag->name; | |
420 | } | |
421 | } | |
422 | } | |
423 | ||
1c7b8b93 | 424 | tag_set('post', $this->id, $tags); |
cae83708 | 425 | } |
426 | ||
427 | /** | |
428 | * User can edit a blog entry if this is their own blog entry and they have | |
429 | * the capability moodle/blog:create, or if they have the capability | |
430 | * moodle/blog:manageentries. | |
431 | * This also applies to deleting of entries. | |
432 | * | |
433 | * @param int $userid Optional. If not given, $USER is used | |
434 | * @return boolean | |
435 | */ | |
436 | public function can_user_edit($userid=null) { | |
437 | global $CFG, $USER; | |
438 | ||
439 | if (empty($userid)) { | |
440 | $userid = $USER->id; | |
441 | } | |
442 | ||
41b38360 | 443 | $sitecontext = context_system::instance(); |
cae83708 | 444 | |
445 | if (has_capability('moodle/blog:manageentries', $sitecontext)) { | |
446 | return true; // can edit any blog entry | |
447 | } | |
448 | ||
449 | if ($this->userid == $userid && has_capability('moodle/blog:create', $sitecontext)) { | |
450 | return true; // can edit own when having blog:create capability | |
451 | } | |
452 | ||
453 | return false; | |
454 | } | |
455 | ||
456 | /** | |
457 | * Checks to see if a user can view the blogs of another user. | |
458 | * Only blog level is checked here, the capabilities are enforced | |
459 | * in blog/index.php | |
460 | * | |
461 | * @param int $targetuserid ID of the user we are checking | |
462 | * | |
463 | * @return bool | |
464 | */ | |
465 | public function can_user_view($targetuserid) { | |
466 | global $CFG, $USER, $DB; | |
41b38360 | 467 | $sitecontext = context_system::instance(); |
cae83708 | 468 | |
850d2db8 | 469 | if (empty($CFG->enableblogs) || !has_capability('moodle/blog:view', $sitecontext)) { |
1c7b8b93 | 470 | return false; // blog system disabled or user has no blog view capability |
cae83708 | 471 | } |
472 | ||
4f0c2d00 | 473 | if (isloggedin() && $USER->id == $targetuserid) { |
cae83708 | 474 | return true; // can view own entries in any case |
475 | } | |
476 | ||
cae83708 | 477 | if (has_capability('moodle/blog:manageentries', $sitecontext)) { |
478 | return true; // can manage all entries | |
479 | } | |
480 | ||
481 | // coming for 1 entry, make sure it's not a draft | |
1c7b8b93 | 482 | if ($this->publishstate == 'draft' && !has_capability('moodle/blog:viewdrafts', $sitecontext)) { |
cae83708 | 483 | return false; // can not view draft of others |
484 | } | |
485 | ||
486 | // coming for 1 entry, make sure user is logged in, if not a public blog | |
1c7b8b93 | 487 | if ($this->publishstate != 'public' && !isloggedin()) { |
cae83708 | 488 | return false; |
489 | } | |
490 | ||
491 | switch ($CFG->bloglevel) { | |
492 | case BLOG_GLOBAL_LEVEL: | |
493 | return true; | |
494 | break; | |
495 | ||
496 | case BLOG_SITE_LEVEL: | |
4f0c2d00 | 497 | if (isloggedin()) { // not logged in viewers forbidden |
cae83708 | 498 | return true; |
499 | } | |
500 | return false; | |
501 | break; | |
502 | ||
503 | case BLOG_USER_LEVEL: | |
504 | default: | |
41b38360 | 505 | $personalcontext = context_user::instance($targetuserid); |
cae83708 | 506 | return has_capability('moodle/user:readuserblogs', $personalcontext); |
507 | break; | |
508 | } | |
509 | } | |
510 | ||
511 | /** | |
512 | * Use this function to retrieve a list of publish states available for | |
513 | * the currently logged in user. | |
514 | * | |
515 | * @return array This function returns an array ideal for sending to moodles' | |
516 | * choose_from_menu function. | |
517 | */ | |
518 | ||
519 | public static function get_applicable_publish_states() { | |
520 | global $CFG; | |
521 | $options = array(); | |
522 | ||
523 | // everyone gets draft access | |
524 | if ($CFG->bloglevel >= BLOG_USER_LEVEL) { | |
1c7b8b93 | 525 | $options['draft'] = get_string('publishtonoone', 'blog'); |
cae83708 | 526 | } |
527 | ||
528 | if ($CFG->bloglevel > BLOG_USER_LEVEL) { | |
1c7b8b93 | 529 | $options['site'] = get_string('publishtosite', 'blog'); |
cae83708 | 530 | } |
531 | ||
532 | if ($CFG->bloglevel >= BLOG_GLOBAL_LEVEL) { | |
1c7b8b93 | 533 | $options['public'] = get_string('publishtoworld', 'blog'); |
cae83708 | 534 | } |
535 | ||
536 | return $options; | |
537 | } | |
538 | } | |
539 | ||
540 | /** | |
541 | * Abstract Blog_Listing class: used to gather blog entries and output them as listings. One of the subclasses must be used. | |
542 | * | |
543 | * @package moodlecore | |
544 | * @subpackage blog | |
545 | * @copyright 2009 Nicolas Connault | |
546 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
547 | */ | |
548 | class blog_listing { | |
549 | /** | |
550 | * Array of blog_entry objects. | |
551 | * @var array $entries | |
552 | */ | |
553 | public $entries = array(); | |
554 | ||
555 | /** | |
556 | * An array of blog_filter_* objects | |
557 | * @var array $filters | |
558 | */ | |
559 | public $filters = array(); | |
560 | ||
561 | /** | |
562 | * Constructor | |
563 | * | |
564 | * @param array $filters An associative array of filtername => filterid | |
565 | */ | |
566 | public function __construct($filters=array()) { | |
567 | // Unset filters overridden by more specific filters | |
568 | foreach ($filters as $type => $id) { | |
569 | if (!empty($type) && !empty($id)) { | |
570 | $this->filters[$type] = blog_filter::get_instance($id, $type); | |
571 | } | |
572 | } | |
573 | ||
574 | foreach ($this->filters as $type => $filter) { | |
575 | foreach ($filter->overrides as $override) { | |
576 | if (array_key_exists($override, $this->filters)) { | |
577 | unset($this->filters[$override]); | |
578 | } | |
579 | } | |
580 | } | |
581 | } | |
582 | ||
583 | /** | |
584 | * Fetches the array of blog entries. | |
585 | * | |
586 | * @return array | |
587 | */ | |
588 | public function get_entries($start=0, $limit=10) { | |
589 | global $DB; | |
590 | ||
591 | if (empty($this->entries)) { | |
899d5e2d | 592 | if ($sqlarray = $this->get_entry_fetch_sql(false, 'created DESC')) { |
af3158d8 | 593 | $this->entries = $DB->get_records_sql($sqlarray['sql'], $sqlarray['params'], $start, $limit); |
cae83708 | 594 | } else { |
595 | return false; | |
596 | } | |
597 | } | |
598 | ||
599 | return $this->entries; | |
600 | } | |
601 | ||
602 | public function get_entry_fetch_sql($count=false, $sort='lastmodified DESC', $userid = false) { | |
603 | global $DB, $USER, $CFG; | |
604 | ||
605 | if(!$userid) { | |
606 | $userid = $USER->id; | |
607 | } | |
608 | ||
506c8d59 | 609 | $allnamefields = get_all_user_name_fields(true, 'u'); |
cae83708 | 610 | // The query used to locate blog entries is complicated. It will be built from the following components: |
506c8d59 | 611 | $requiredfields = "p.*, $allnamefields, u.email"; // the SELECT clause |
1c7b8b93 NC |
612 | $tables = array('p' => 'post', 'u' => 'user'); // components of the FROM clause (table_id => table_name) |
613 | $conditions = array('u.deleted = 0', 'p.userid = u.id', '(p.module = \'blog\' OR p.module = \'blog_external\')'); // components of the WHERE clause (conjunction) | |
cae83708 | 614 | |
615 | // build up a clause for permission constraints | |
616 | ||
617 | $params = array(); | |
618 | ||
619 | // fix for MDL-9165, use with readuserblogs capability in a user context can read that user's private blogs | |
620 | // admins can see all blogs regardless of publish states, as described on the help page | |
41b38360 | 621 | if (has_capability('moodle/user:readuserblogs', context_system::instance())) { |
cae83708 | 622 | // don't add permission constraints |
623 | ||
624 | } else if(!empty($this->filters['user']) && has_capability('moodle/user:readuserblogs', | |
41b38360 | 625 | context_user::instance((empty($this->filters['user']->id) ? 0 : $this->filters['user']->id)))) { |
cae83708 | 626 | // don't add permission constraints |
627 | ||
628 | } else { | |
4f0c2d00 | 629 | if (isloggedin() and !isguestuser()) { |
cae83708 | 630 | $assocexists = $DB->record_exists('blog_association', array()); //dont check association records if there aren't any |
631 | ||
632 | //begin permission sql clause | |
1c7b8b93 | 633 | $permissionsql = '(p.userid = ? '; |
cae83708 | 634 | $params[] = $userid; |
635 | ||
636 | if ($CFG->bloglevel >= BLOG_SITE_LEVEL) { // add permission to view site-level entries | |
1c7b8b93 | 637 | $permissionsql .= " OR p.publishstate = 'site' "; |
cae83708 | 638 | } |
639 | ||
640 | if ($CFG->bloglevel >= BLOG_GLOBAL_LEVEL) { // add permission to view global entries | |
1c7b8b93 | 641 | $permissionsql .= " OR p.publishstate = 'public' "; |
cae83708 | 642 | } |
643 | ||
644 | $permissionsql .= ') '; //close permissions sql clause | |
645 | } else { // default is access to public entries | |
1c7b8b93 | 646 | $permissionsql = "p.publishstate = 'public'"; |
cae83708 | 647 | } |
648 | $conditions[] = $permissionsql; //add permission constraints | |
649 | } | |
650 | ||
1c7b8b93 NC |
651 | foreach ($this->filters as $type => $blogfilter) { |
652 | $conditions = array_merge($conditions, $blogfilter->conditions); | |
653 | $params = array_merge($params, $blogfilter->params); | |
654 | $tables = array_merge($tables, $blogfilter->tables); | |
cae83708 | 655 | } |
656 | ||
657 | $tablessql = ''; // build up the FROM clause | |
658 | foreach ($tables as $tablename => $table) { | |
659 | $tablessql .= ($tablessql ? ', ' : '').'{'.$table.'} '.$tablename; | |
660 | } | |
661 | ||
662 | $sql = ($count) ? 'SELECT COUNT(*)' : 'SELECT ' . $requiredfields; | |
663 | $sql .= " FROM $tablessql WHERE " . implode(' AND ', $conditions); | |
527761e0 | 664 | $sql .= ($count) ? '' : " ORDER BY $sort"; |
cae83708 | 665 | |
666 | return array('sql' => $sql, 'params' => $params); | |
667 | } | |
668 | ||
669 | /** | |
670 | * Outputs all the blog entries aggregated by this blog listing. | |
671 | * | |
672 | * @return void | |
673 | */ | |
674 | public function print_entries() { | |
2591c7ae | 675 | global $CFG, $USER, $DB, $OUTPUT, $PAGE; |
41b38360 | 676 | $sitecontext = context_system::instance(); |
cae83708 | 677 | |
af7e05d6 | 678 | // Blog renderer |
2591c7ae DM |
679 | $output = $PAGE->get_renderer('blog'); |
680 | ||
cae83708 | 681 | $page = optional_param('blogpage', 0, PARAM_INT); |
682 | $limit = optional_param('limit', get_user_preferences('blogpagesize', 10), PARAM_INT); | |
683 | $start = $page * $limit; | |
684 | ||
685 | $morelink = '<br /> '; | |
686 | ||
1c7b8b93 NC |
687 | if ($sqlarray = $this->get_entry_fetch_sql(true)) { |
688 | $totalentries = $DB->count_records_sql($sqlarray['sql'], $sqlarray['params']); | |
cae83708 | 689 | } else { |
690 | $totalentries = 0; | |
691 | } | |
692 | ||
693 | $entries = $this->get_entries($start, $limit); | |
929d7a83 | 694 | $pagingbar = new paging_bar($totalentries, $page, $limit, $this->get_baseurl()); |
cae83708 | 695 | $pagingbar->pagevar = 'blogpage'; |
1c7b8b93 | 696 | $blogheaders = blog_get_headers(); |
cae83708 | 697 | |
929d7a83 | 698 | echo $OUTPUT->render($pagingbar); |
cae83708 | 699 | |
cae83708 | 700 | if (has_capability('moodle/blog:create', $sitecontext)) { |
701 | //the user's blog is enabled and they are viewing their own blog | |
702 | $userid = optional_param('userid', null, PARAM_INT); | |
703 | ||
704 | if (empty($userid) || (!empty($userid) && $userid == $USER->id)) { | |
4219ffab | 705 | |
207b6fc5 | 706 | $courseid = optional_param('courseid', null, PARAM_INT); |
4ef08298 AA |
707 | $modid = optional_param('modid', null, PARAM_INT); |
708 | ||
709 | $addurl = new moodle_url("$CFG->wwwroot/blog/edit.php"); | |
710 | $urlparams = array('action' => 'add', | |
711 | 'userid' => $userid, | |
712 | 'courseid' => $courseid, | |
713 | 'groupid' => optional_param('groupid', null, PARAM_INT), | |
714 | 'modid' => $modid, | |
715 | 'tagid' => optional_param('tagid', null, PARAM_INT), | |
716 | 'tag' => optional_param('tag', null, PARAM_INT), | |
717 | 'search' => optional_param('search', null, PARAM_INT)); | |
718 | ||
719 | $urlparams = array_filter($urlparams); | |
720 | $addurl->params($urlparams); | |
721 | ||
722 | $addlink = '<div class="addbloglink">'; | |
723 | $addlink .= '<a href="'.$addurl->out().'">'. $blogheaders['stradd'].'</a>'; | |
724 | $addlink .= '</div>'; | |
725 | echo $addlink; | |
cae83708 | 726 | } |
727 | } | |
728 | ||
729 | if ($entries) { | |
730 | $count = 0; | |
cae83708 | 731 | foreach ($entries as $entry) { |
1c7b8b93 | 732 | $blogentry = new blog_entry(null, $entry); |
2591c7ae DM |
733 | |
734 | // Get the required blog entry data to render it | |
735 | $blogentry->prepare_render(); | |
736 | echo $output->render($blogentry); | |
737 | ||
cae83708 | 738 | $count++; |
739 | } | |
740 | ||
929d7a83 | 741 | echo $OUTPUT->render($pagingbar); |
cae83708 | 742 | |
743 | if (!$count) { | |
744 | print '<br /><div style="text-align:center">'. get_string('noentriesyet', 'blog') .'</div><br />'; | |
745 | } | |
746 | ||
747 | print $morelink.'<br />'."\n"; | |
748 | return; | |
749 | } | |
750 | } | |
751 | ||
752 | /// Find the base url from $_GET variables, for print_paging_bar | |
753 | public function get_baseurl() { | |
754 | $getcopy = $_GET; | |
755 | ||
756 | unset($getcopy['blogpage']); | |
757 | ||
758 | if (!empty($getcopy)) { | |
759 | $first = false; | |
760 | $querystring = ''; | |
761 | ||
762 | foreach ($getcopy as $var => $val) { | |
763 | if (!$first) { | |
764 | $first = true; | |
765 | $querystring .= "?$var=$val"; | |
766 | } else { | |
767 | $querystring .= '&'.$var.'='.$val; | |
768 | $hasparam = true; | |
769 | } | |
770 | } | |
771 | } else { | |
772 | $querystring = '?'; | |
773 | } | |
774 | ||
775 | return strip_querystring(qualified_me()) . $querystring; | |
776 | ||
777 | } | |
778 | } | |
779 | ||
780 | /** | |
781 | * Abstract class for blog_filter objects. | |
782 | * A set of core filters are implemented here. To write new filters, you need to subclass | |
783 | * blog_filter and give it the name of the type you want (for example, blog_filter_entry). | |
784 | * The blog_filter abstract class will automatically use it when the filter is added to the | |
785 | * URL. The first parameter of the constructor is the ID of your filter, but it can be a string | |
786 | * or have any other meaning you wish it to have. The second parameter is called $type and is | |
787 | * used as a sub-type for filters that have a very similar implementation (see blog_filter_context for an example) | |
788 | */ | |
789 | abstract class blog_filter { | |
790 | /** | |
791 | * An array of strings representing the available filter types for each blog_filter. | |
1c7b8b93 | 792 | * @var array $availabletypes |
cae83708 | 793 | */ |
1c7b8b93 | 794 | public $availabletypes = array(); |
cae83708 | 795 | |
796 | /** | |
797 | * The type of filter (for example, types of blog_filter_context are site, course and module) | |
798 | * @var string $type | |
799 | */ | |
800 | public $type; | |
801 | ||
802 | /** | |
803 | * The unique ID for a filter's associated record | |
804 | * @var int $id | |
805 | */ | |
806 | public $id; | |
807 | ||
808 | /** | |
809 | * An array of table aliases that are used in the WHERE conditions | |
810 | * @var array $tables | |
811 | */ | |
812 | public $tables = array(); | |
813 | ||
814 | /** | |
815 | * An array of WHERE conditions | |
816 | * @var array $conditions | |
817 | */ | |
818 | public $conditions = array(); | |
819 | ||
820 | /** | |
821 | * An array of SQL params | |
822 | * @var array $params | |
823 | */ | |
824 | public $params = array(); | |
825 | ||
826 | /** | |
827 | * An array of filter types which this particular filter type overrides: their conditions will not be evaluated | |
828 | */ | |
829 | public $overrides = array(); | |
830 | ||
831 | public function __construct($id, $type=null) { | |
832 | $this->id = $id; | |
833 | $this->type = $type; | |
834 | } | |
835 | ||
836 | /** | |
837 | * TODO This is poor design. A parent class should not know anything about its children. | |
838 | * The default case helps to resolve this design issue | |
839 | */ | |
840 | public static function get_instance($id, $type) { | |
841 | ||
842 | switch ($type) { | |
843 | case 'site': | |
844 | case 'course': | |
845 | case 'module': | |
846 | return new blog_filter_context($id, $type); | |
847 | break; | |
848 | ||
849 | case 'group': | |
850 | case 'user': | |
851 | return new blog_filter_user($id, $type); | |
852 | break; | |
853 | ||
854 | case 'tag': | |
855 | return new blog_filter_tag($id); | |
856 | break; | |
857 | ||
858 | default: | |
1c7b8b93 NC |
859 | $classname = "blog_filter_$type"; |
860 | if (class_exists($classname)) { | |
861 | return new $classname($id, $type); | |
cae83708 | 862 | } |
863 | } | |
864 | } | |
865 | } | |
866 | ||
867 | /** | |
868 | * This filter defines the context level of the blog entries being searched: site, course, module | |
869 | */ | |
870 | class blog_filter_context extends blog_filter { | |
871 | /** | |
872 | * Constructor | |
873 | * | |
874 | * @param string $type | |
875 | * @param int $id | |
876 | */ | |
877 | public function __construct($id=null, $type='site') { | |
878 | global $SITE, $CFG, $DB; | |
879 | ||
880 | if (empty($id)) { | |
881 | $this->type = 'site'; | |
882 | } else { | |
883 | $this->id = $id; | |
884 | $this->type = $type; | |
885 | } | |
886 | ||
8eaf1ba1 | 887 | $this->availabletypes = array('site' => get_string('site'), 'course' => get_string('course'), 'module' => get_string('activity')); |
cae83708 | 888 | |
889 | switch ($this->type) { | |
890 | case 'course': // Careful of site course! | |
891 | // Ignore course filter if blog associations are not enabled | |
892 | if ($this->id != $SITE->id && !empty($CFG->useblogassociations)) { | |
893 | $this->overrides = array('site'); | |
41b38360 | 894 | $context = context_course::instance($this->id); |
cae83708 | 895 | $this->tables['ba'] = 'blog_association'; |
1c7b8b93 | 896 | $this->conditions[] = 'p.id = ba.blogid'; |
cae83708 | 897 | $this->conditions[] = 'ba.contextid = '.$context->id; |
898 | break; | |
899 | } else { | |
900 | // We are dealing with the site course, do not break from the current case | |
901 | } | |
902 | ||
903 | case 'site': | |
904 | // No special constraints | |
905 | break; | |
906 | case 'module': | |
907 | if (!empty($CFG->useblogassociations)) { | |
908 | $this->overrides = array('course', 'site'); | |
909 | ||
41b38360 | 910 | $context = context_module::instance($this->id); |
cae83708 | 911 | $this->tables['ba'] = 'blog_association'; |
1c7b8b93 NC |
912 | $this->tables['p'] = 'post'; |
913 | $this->conditions = array('p.id = ba.blogid', 'ba.contextid = ?'); | |
cae83708 | 914 | $this->params = array($context->id); |
915 | } | |
916 | break; | |
917 | } | |
918 | } | |
919 | } | |
920 | ||
921 | /** | |
922 | * This filter defines the user level of the blog entries being searched: a userid or a groupid. | |
923 | * It can be combined with a context filter in order to refine the search. | |
924 | */ | |
925 | class blog_filter_user extends blog_filter { | |
926 | public $tables = array('u' => 'user'); | |
927 | ||
928 | /** | |
929 | * Constructor | |
930 | * | |
931 | * @param string $type | |
932 | * @param int $id | |
933 | */ | |
934 | public function __construct($id=null, $type='user') { | |
320ae23a | 935 | global $CFG, $DB, $USER; |
1c7b8b93 | 936 | $this->availabletypes = array('user' => get_string('user'), 'group' => get_string('group')); |
cae83708 | 937 | |
938 | if (empty($id)) { | |
939 | $this->id = $USER->id; | |
940 | $this->type = 'user'; | |
941 | } else { | |
942 | $this->id = $id; | |
943 | $this->type = $type; | |
944 | } | |
945 | ||
946 | if ($this->type == 'user') { | |
947 | $this->conditions = array('u.id = ?'); | |
948 | $this->params = array($this->id); | |
949 | $this->overrides = array('group'); | |
950 | ||
951 | } elseif ($this->type == 'group') { | |
952 | $this->overrides = array('course', 'site'); | |
953 | ||
954 | $this->tables['gm'] = 'groups_members'; | |
1c7b8b93 | 955 | $this->conditions[] = 'p.userid = gm.userid'; |
cae83708 | 956 | $this->conditions[] = 'gm.groupid = ?'; |
957 | $this->params[] = $this->id; | |
958 | ||
959 | if (!empty($CFG->useblogassociations)) { // only show blog entries associated with this course | |
41b38360 | 960 | $coursecontext = context_course::instance($DB->get_field('groups', 'courseid', array('id' => $this->id))); |
cae83708 | 961 | $this->tables['ba'] = 'blog_association'; |
962 | $this->conditions[] = 'gm.groupid = ?'; | |
963 | $this->conditions[] = 'ba.contextid = ?'; | |
1c7b8b93 | 964 | $this->conditions[] = 'ba.blogid = p.id'; |
cae83708 | 965 | $this->params[] = $this->id; |
1c7b8b93 | 966 | $this->params[] = $coursecontext->id; |
cae83708 | 967 | } |
968 | } | |
b73d1ca4 | 969 | |
cae83708 | 970 | } |
971 | } | |
972 | ||
973 | /** | |
974 | * This filter defines a tag by which blog entries should be searched. | |
975 | */ | |
976 | class blog_filter_tag extends blog_filter { | |
1c7b8b93 | 977 | public $tables = array('t' => 'tag', 'ti' => 'tag_instance', 'p' => 'post'); |
cae83708 | 978 | |
979 | /** | |
980 | * Constructor | |
981 | * | |
982 | * @return void | |
983 | */ | |
984 | public function __construct($id) { | |
985 | global $DB; | |
986 | $this->id = $id; | |
987 | ||
988 | $this->conditions = array('ti.tagid = t.id', | |
1c7b8b93 NC |
989 | "ti.itemtype = 'post'", |
990 | 'ti.itemid = p.id', | |
cae83708 | 991 | 't.id = ?'); |
992 | $this->params = array($this->id); | |
993 | } | |
994 | } | |
995 | ||
996 | /** | |
997 | * This filter defines a specific blog entry id. | |
998 | */ | |
999 | class blog_filter_entry extends blog_filter { | |
1000 | public $conditions = array('p.id = ?'); | |
1001 | public $overrides = array('site', 'course', 'module', 'group', 'user', 'tag'); | |
1002 | ||
1003 | public function __construct($id) { | |
1004 | $this->id = $id; | |
1005 | $this->params[] = $this->id; | |
1006 | } | |
1007 | } | |
1008 | ||
1c7b8b93 | 1009 | /** |
caee6e6c | 1010 | * This filter restricts the results to a time interval in seconds up to time() |
1c7b8b93 NC |
1011 | */ |
1012 | class blog_filter_since extends blog_filter { | |
1013 | public function __construct($interval) { | |
1014 | $this->conditions[] = 'p.lastmodified >= ? AND p.lastmodified <= ?'; | |
caee6e6c PS |
1015 | $this->params[] = time() - $interval; |
1016 | $this->params[] = time(); | |
1c7b8b93 NC |
1017 | } |
1018 | } | |
1019 | ||
cae83708 | 1020 | /** |
1021 | * Filter used to perform full-text search on an entry's subject, summary and content | |
1022 | */ | |
1023 | class blog_filter_search extends blog_filter { | |
1024 | ||
1c7b8b93 | 1025 | public function __construct($searchterm) { |
cae83708 | 1026 | global $DB; |
c014d57c PS |
1027 | $this->conditions = array("(".$DB->sql_like('p.summary', '?', false)." OR |
1028 | ".$DB->sql_like('p.content', '?', false)." OR | |
1029 | ".$DB->sql_like('p.subject', '?', false).")"); | |
1c7b8b93 NC |
1030 | $this->params[] = "%$searchterm%"; |
1031 | $this->params[] = "%$searchterm%"; | |
1032 | $this->params[] = "%$searchterm%"; | |
cae83708 | 1033 | } |
1034 | } | |
2591c7ae DM |
1035 | |
1036 | ||
1037 | /** | |
1038 | * Renderable class to represent an entry attachment | |
1039 | */ | |
1040 | class blog_entry_attachment implements renderable { | |
1041 | ||
1042 | public $filename; | |
1043 | public $url; | |
1044 | public $file; | |
1045 | ||
1046 | /** | |
f8133217 DM |
1047 | * Gets the file data |
1048 | * | |
2591c7ae DM |
1049 | * @param stored_file $file |
1050 | * @param int $entryid Attachment entry id | |
1051 | */ | |
1052 | public function __construct($file, $entryid) { | |
1053 | ||
f8133217 | 1054 | global $CFG; |
2591c7ae DM |
1055 | |
1056 | $this->file = $file; | |
af7e05d6 EL |
1057 | $this->filename = $file->get_filename(); |
1058 | $this->url = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.SYSCONTEXTID.'/blog/attachment/'.$entryid.'/'.$this->filename); | |
2591c7ae DM |
1059 | } |
1060 | ||
1061 | } |