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