Merge branch 'master_MDL-65116' of https://github.com/yao9394/moodle
[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
0e32a565 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.
0e32a565 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.
0e32a565 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 if (!empty($CFG->useblogassociations)) {
260 $this->add_associations();
cae83708 261 }
9d97f08e 262
abea2c5d 263 core_tag_tag::set_item_tags('core', 'post', $this->id, context_user::instance($this->userid), $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,
aa139299 268 'relateduserid' => $this->userid
77037e27 269 ));
24c32bdf 270 $event->set_blog_entry($this);
3049780a 271 $event->trigger();
cae83708 272 }
273
274 /**
275 * Updates this entry in the database. Access control checks must be done by calling code.
276 *
32dea439
AA
277 * @param array $params Entry parameters.
278 * @param moodleform $form Used for attachments.
279 * @param array $summaryoptions Summary options.
280 * @param array $attachmentoptions Attachment options.
281 *
cae83708 282 * @return void
283 */
1c7b8b93 284 public function edit($params=array(), $form=null, $summaryoptions=array(), $attachmentoptions=array()) {
32dea439 285 global $CFG, $DB;
cae83708 286
41b38360 287 $sitecontext = context_system::instance();
1c7b8b93
NC
288 $entry = $this;
289
cae83708 290 $this->form = $form;
291 foreach ($params as $var => $val) {
1c7b8b93 292 $entry->$var = $val;
cae83708 293 }
294
64f93798 295 $entry = file_postupdate_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
2b6e53e8
AD
296 $entry = file_postupdate_standard_filemanager($entry,
297 'attachment',
298 $attachmentoptions,
299 $sitecontext,
300 'blog',
301 'attachment',
302 $entry->id);
b73d1ca4 303
5f4d4d80 304 if (!empty($CFG->useblogassociations)) {
1c7b8b93 305 $entry->add_associations();
cae83708 306 }
307
1c7b8b93
NC
308 $entry->lastmodified = time();
309
32dea439 310 // Update record.
1c7b8b93 311 $DB->update_record('post', $entry);
abea2c5d 312 core_tag_tag::set_item_tags('core', 'post', $entry->id, context_user::instance($this->userid), $entry->tags);
cae83708 313
32dea439
AA
314 $event = \core\event\blog_entry_updated::create(array(
315 'objectid' => $entry->id,
aa139299 316 'relateduserid' => $entry->userid
32dea439 317 ));
24c32bdf 318 $event->set_blog_entry($entry);
32dea439 319 $event->trigger();
cae83708 320 }
321
322 /**
323 * Deletes this entry from the database. Access control checks must be done by calling code.
324 *
325 * @return void
326 */
327 public function delete() {
ac31c38e 328 global $DB;
cae83708 329
cae83708 330 $this->delete_attachments();
ac31c38e 331 $this->remove_associations();
cae83708 332
6c66b7f3
AA
333 // Get record to pass onto the event.
334 $record = $DB->get_record('post', array('id' => $this->id));
1c7b8b93 335 $DB->delete_records('post', array('id' => $this->id));
abea2c5d 336 core_tag_tag::remove_all_item_tags('core', 'post', $this->id);
cae83708 337
77037e27
AA
338 $event = \core\event\blog_entry_deleted::create(array(
339 'objectid' => $this->id,
aa139299
RT
340 'relateduserid' => $this->userid
341 ));
6c66b7f3 342 $event->add_record_snapshot("post", $record);
24c32bdf 343 $event->set_blog_entry($this);
6c66b7f3 344 $event->trigger();
cae83708 345 }
346
347 /**
6b364115 348 * Function to add all context associations to an entry.
6b364115 349 *
077b347d 350 * @param string $unused This does nothing, do not use it.
cae83708 351 */
077b347d 352 public function add_associations($unused = null) {
6b364115 353
077b347d
FM
354 if ($unused !== null) {
355 debugging('Illegal argument used in blog_entry->add_associations()', DEBUG_DEVELOPER);
6b364115 356 }
cae83708 357
cae83708 358 $this->remove_associations();
359
360 if (!empty($this->courseassoc)) {
6b364115 361 $this->add_association($this->courseassoc);
cae83708 362 }
363
364 if (!empty($this->modassoc)) {
6b364115 365 $this->add_association($this->modassoc);
cae83708 366 }
367 }
368
369 /**
6b364115 370 * Add a single association for a blog entry
6b364115
AA
371 *
372 * @param int $contextid - id of context to associate with the blog entry.
077b347d 373 * @param string $unused This does nothing, do not use it.
cae83708 374 */
077b347d 375 public function add_association($contextid, $unused = null) {
6b364115
AA
376 global $DB;
377
077b347d
FM
378 if ($unused !== null) {
379 debugging('Illegal argument used in blog_entry->add_association()', DEBUG_DEVELOPER);
6b364115 380 }
cae83708 381
1c7b8b93
NC
382 $assocobject = new StdClass;
383 $assocobject->contextid = $contextid;
384 $assocobject->blogid = $this->id;
6b364115 385 $id = $DB->insert_record('blog_association', $assocobject);
1c7b8b93 386
6b364115 387 // Trigger an association created event.
41b38360 388 $context = context::instance_by_id($contextid);
6b364115
AA
389 $eventparam = array(
390 'objectid' => $id,
391 'other' => array('associateid' => $context->instanceid, 'subject' => $this->subject, 'blogid' => $this->id),
392 'relateduserid' => $this->userid
393 );
1c7b8b93 394 if ($context->contextlevel == CONTEXT_COURSE) {
6b364115
AA
395 $eventparam['other']['associatetype'] = 'course';
396
1c7b8b93 397 } else if ($context->contextlevel == CONTEXT_MODULE) {
6b364115 398 $eventparam['other']['associatetype'] = 'coursemodule';
1c7b8b93 399 }
6b364115
AA
400 $event = \core\event\blog_association_created::create($eventparam);
401 $event->trigger();
cae83708 402 }
403
404 /**
405 * remove all associations for a blog entry
02ce2e41
SB
406 *
407 * @return void
cae83708 408 */
409 public function remove_associations() {
410 global $DB;
02ce2e41
SB
411
412 $associations = $DB->get_records('blog_association', array('blogid' => $this->id));
413 foreach ($associations as $association) {
414
415 // Trigger an association deleted event.
416 $context = context::instance_by_id($association->contextid);
417 $eventparam = array(
418 'objectid' => $this->id,
419 'other' => array('subject' => $this->subject, 'blogid' => $this->id),
420 'relateduserid' => $this->userid
421 );
422 $event = \core\event\blog_association_deleted::create($eventparam);
423 $event->add_record_snapshot('blog_association', $association);
424 $event->trigger();
425
426 // Now remove the association.
427 $DB->delete_records('blog_association', array('id' => $association->id));
428 }
cae83708 429 }
430
431 /**
432 * Deletes all the user files in the attachments area for an entry
433 *
434 * @return void
435 */
436 public function delete_attachments() {
437 $fs = get_file_storage();
64f93798
PS
438 $fs->delete_area_files(SYSCONTEXTID, 'blog', 'attachment', $this->id);
439 $fs->delete_area_files(SYSCONTEXTID, 'blog', 'post', $this->id);
cae83708 440 }
441
cae83708 442 /**
443 * User can edit a blog entry if this is their own blog entry and they have
444 * the capability moodle/blog:create, or if they have the capability
445 * moodle/blog:manageentries.
446 * This also applies to deleting of entries.
447 *
448 * @param int $userid Optional. If not given, $USER is used
449 * @return boolean
450 */
451 public function can_user_edit($userid=null) {
452 global $CFG, $USER;
453
454 if (empty($userid)) {
455 $userid = $USER->id;
456 }
457
41b38360 458 $sitecontext = context_system::instance();
cae83708 459
460 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
2b6e53e8 461 return true; // Can edit any blog entry.
cae83708 462 }
463
464 if ($this->userid == $userid && has_capability('moodle/blog:create', $sitecontext)) {
2b6e53e8 465 return true; // Can edit own when having blog:create capability.
cae83708 466 }
467
468 return false;
469 }
470
471 /**
472 * Checks to see if a user can view the blogs of another user.
473 * Only blog level is checked here, the capabilities are enforced
474 * in blog/index.php
475 *
476 * @param int $targetuserid ID of the user we are checking
477 *
478 * @return bool
479 */
480 public function can_user_view($targetuserid) {
481 global $CFG, $USER, $DB;
41b38360 482 $sitecontext = context_system::instance();
cae83708 483
850d2db8 484 if (empty($CFG->enableblogs) || !has_capability('moodle/blog:view', $sitecontext)) {
2b6e53e8 485 return false; // Blog system disabled or user has no blog view capability.
cae83708 486 }
487
4f0c2d00 488 if (isloggedin() && $USER->id == $targetuserid) {
2b6e53e8 489 return true; // Can view own entries in any case.
cae83708 490 }
491
cae83708 492 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
2b6e53e8 493 return true; // Can manage all entries.
cae83708 494 }
495
2b6e53e8 496 // Coming for 1 entry, make sure it's not a draft.
1c7b8b93 497 if ($this->publishstate == 'draft' && !has_capability('moodle/blog:viewdrafts', $sitecontext)) {
2b6e53e8 498 return false; // Can not view draft of others.
cae83708 499 }
500
2b6e53e8 501 // Coming for 1 entry, make sure user is logged in, if not a public blog.
1c7b8b93 502 if ($this->publishstate != 'public' && !isloggedin()) {
cae83708 503 return false;
504 }
505
506 switch ($CFG->bloglevel) {
507 case BLOG_GLOBAL_LEVEL:
508 return true;
509 break;
510
511 case BLOG_SITE_LEVEL:
2b6e53e8 512 if (isloggedin()) { // Not logged in viewers forbidden.
cae83708 513 return true;
514 }
515 return false;
516 break;
517
518 case BLOG_USER_LEVEL:
519 default:
41b38360 520 $personalcontext = context_user::instance($targetuserid);
cae83708 521 return has_capability('moodle/user:readuserblogs', $personalcontext);
522 break;
523 }
524 }
525
526 /**
527 * Use this function to retrieve a list of publish states available for
528 * the currently logged in user.
529 *
530 * @return array This function returns an array ideal for sending to moodles'
531 * choose_from_menu function.
532 */
533
534 public static function get_applicable_publish_states() {
535 global $CFG;
536 $options = array();
537
2b6e53e8 538 // Everyone gets draft access.
cae83708 539 if ($CFG->bloglevel >= BLOG_USER_LEVEL) {
1c7b8b93 540 $options['draft'] = get_string('publishtonoone', 'blog');
cae83708 541 }
542
543 if ($CFG->bloglevel > BLOG_USER_LEVEL) {
1c7b8b93 544 $options['site'] = get_string('publishtosite', 'blog');
cae83708 545 }
546
547 if ($CFG->bloglevel >= BLOG_GLOBAL_LEVEL) {
1c7b8b93 548 $options['public'] = get_string('publishtoworld', 'blog');
cae83708 549 }
550
551 return $options;
552 }
553}
554
555/**
556 * Abstract Blog_Listing class: used to gather blog entries and output them as listings. One of the subclasses must be used.
557 *
558 * @package moodlecore
559 * @subpackage blog
560 * @copyright 2009 Nicolas Connault
561 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
562 */
563class blog_listing {
564 /**
565 * Array of blog_entry objects.
566 * @var array $entries
567 */
abea2c5d
MG
568 public $entries = null;
569
570 /**
571 * Caches the total number of the entries.
572 * @var int
573 */
574 public $totalentries = null;
cae83708 575
576 /**
577 * An array of blog_filter_* objects
578 * @var array $filters
579 */
580 public $filters = array();
581
582 /**
583 * Constructor
584 *
585 * @param array $filters An associative array of filtername => filterid
586 */
587 public function __construct($filters=array()) {
2b6e53e8 588 // Unset filters overridden by more specific filters.
cae83708 589 foreach ($filters as $type => $id) {
590 if (!empty($type) && !empty($id)) {
591 $this->filters[$type] = blog_filter::get_instance($id, $type);
592 }
593 }
594
595 foreach ($this->filters as $type => $filter) {
596 foreach ($filter->overrides as $override) {
597 if (array_key_exists($override, $this->filters)) {
598 unset($this->filters[$override]);
599 }
600 }
601 }
602 }
603
604 /**
605 * Fetches the array of blog entries.
606 *
607 * @return array
608 */
609 public function get_entries($start=0, $limit=10) {
610 global $DB;
611
abea2c5d 612 if ($this->entries === null) {
899d5e2d 613 if ($sqlarray = $this->get_entry_fetch_sql(false, 'created DESC')) {
af3158d8 614 $this->entries = $DB->get_records_sql($sqlarray['sql'], $sqlarray['params'], $start, $limit);
abea2c5d
MG
615 if (!$start && count($this->entries) < $limit) {
616 $this->totalentries = count($this->entries);
617 }
cae83708 618 } else {
619 return false;
620 }
621 }
622
623 return $this->entries;
624 }
625
abea2c5d
MG
626 /**
627 * Finds total number of blog entries
628 *
629 * @return int
630 */
631 public function count_entries() {
632 global $DB;
633 if ($this->totalentries === null) {
634 if ($sqlarray = $this->get_entry_fetch_sql(true)) {
635 $this->totalentries = $DB->count_records_sql($sqlarray['sql'], $sqlarray['params']);
636 } else {
637 $this->totalentries = 0;
638 }
639 }
640 return $this->totalentries;
641 }
642
cae83708 643 public function get_entry_fetch_sql($count=false, $sort='lastmodified DESC', $userid = false) {
644 global $DB, $USER, $CFG;
645
2b6e53e8 646 if (!$userid) {
cae83708 647 $userid = $USER->id;
648 }
649
abea2c5d 650 $allnamefields = \user_picture::fields('u', null, 'useridalias');
cae83708 651 // The query used to locate blog entries is complicated. It will be built from the following components:
25430f75 652 $requiredfields = "p.*, $allnamefields"; // The SELECT clause.
2b6e53e8
AD
653 $tables = array('p' => 'post', 'u' => 'user'); // Components of the FROM clause (table_id => table_name).
654 // Components of the WHERE clause (conjunction).
655 $conditions = array('u.deleted = 0', 'p.userid = u.id', '(p.module = \'blog\' OR p.module = \'blog_external\')');
cae83708 656
2b6e53e8 657 // Build up a clause for permission constraints.
cae83708 658
659 $params = array();
660
2b6e53e8
AD
661 // Fix for MDL-9165, use with readuserblogs capability in a user context can read that user's private blogs.
662 // Admins can see all blogs regardless of publish states, as described on the help page.
41b38360 663 if (has_capability('moodle/user:readuserblogs', context_system::instance())) {
2b6e53e8 664 // Don't add permission constraints.
cae83708 665
2b6e53e8
AD
666 } else if (!empty($this->filters['user'])
667 && has_capability('moodle/user:readuserblogs',
668 context_user::instance((empty($this->filters['user']->id) ? 0 : $this->filters['user']->id)))) {
669 // Don't add permission constraints.
cae83708 670
671 } else {
4f0c2d00 672 if (isloggedin() and !isguestuser()) {
2b6e53e8
AD
673 // Dont check association records if there aren't any.
674 $assocexists = $DB->record_exists('blog_association', array());
cae83708 675
2b6e53e8 676 // Begin permission sql clause.
0e32a565 677 $permissionsql = '(p.userid = ? ';
cae83708 678 $params[] = $userid;
679
2b6e53e8 680 if ($CFG->bloglevel >= BLOG_SITE_LEVEL) { // Add permission to view site-level entries.
1c7b8b93 681 $permissionsql .= " OR p.publishstate = 'site' ";
cae83708 682 }
683
2b6e53e8 684 if ($CFG->bloglevel >= BLOG_GLOBAL_LEVEL) { // Add permission to view global entries.
1c7b8b93 685 $permissionsql .= " OR p.publishstate = 'public' ";
cae83708 686 }
687
2b6e53e8
AD
688 $permissionsql .= ') '; // Close permissions sql clause.
689 } else { // Default is access to public entries.
1c7b8b93 690 $permissionsql = "p.publishstate = 'public'";
cae83708 691 }
2b6e53e8 692 $conditions[] = $permissionsql; // Add permission constraints.
cae83708 693 }
694
1c7b8b93
NC
695 foreach ($this->filters as $type => $blogfilter) {
696 $conditions = array_merge($conditions, $blogfilter->conditions);
697 $params = array_merge($params, $blogfilter->params);
698 $tables = array_merge($tables, $blogfilter->tables);
cae83708 699 }
700
2b6e53e8 701 $tablessql = ''; // Build up the FROM clause.
cae83708 702 foreach ($tables as $tablename => $table) {
703 $tablessql .= ($tablessql ? ', ' : '').'{'.$table.'} '.$tablename;
704 }
705
706 $sql = ($count) ? 'SELECT COUNT(*)' : 'SELECT ' . $requiredfields;
707 $sql .= " FROM $tablessql WHERE " . implode(' AND ', $conditions);
527761e0 708 $sql .= ($count) ? '' : " ORDER BY $sort";
cae83708 709
710 return array('sql' => $sql, 'params' => $params);
711 }
712
713 /**
714 * Outputs all the blog entries aggregated by this blog listing.
715 *
716 * @return void
717 */
718 public function print_entries() {
2591c7ae 719 global $CFG, $USER, $DB, $OUTPUT, $PAGE;
41b38360 720 $sitecontext = context_system::instance();
cae83708 721
2b6e53e8 722 // Blog renderer.
2591c7ae
DM
723 $output = $PAGE->get_renderer('blog');
724
cae83708 725 $page = optional_param('blogpage', 0, PARAM_INT);
726 $limit = optional_param('limit', get_user_preferences('blogpagesize', 10), PARAM_INT);
727 $start = $page * $limit;
728
729 $morelink = '<br />&nbsp;&nbsp;';
730
cae83708 731 $entries = $this->get_entries($start, $limit);
abea2c5d 732 $totalentries = $this->count_entries();
929d7a83 733 $pagingbar = new paging_bar($totalentries, $page, $limit, $this->get_baseurl());
cae83708 734 $pagingbar->pagevar = 'blogpage';
1c7b8b93 735 $blogheaders = blog_get_headers();
cae83708 736
929d7a83 737 echo $OUTPUT->render($pagingbar);
cae83708 738
cae83708 739 if (has_capability('moodle/blog:create', $sitecontext)) {
2b6e53e8 740 // The user's blog is enabled and they are viewing their own blog.
cae83708 741 $userid = optional_param('userid', null, PARAM_INT);
742
743 if (empty($userid) || (!empty($userid) && $userid == $USER->id)) {
4219ffab 744
207b6fc5 745 $courseid = optional_param('courseid', null, PARAM_INT);
4ef08298
AA
746 $modid = optional_param('modid', null, PARAM_INT);
747
748 $addurl = new moodle_url("$CFG->wwwroot/blog/edit.php");
749 $urlparams = array('action' => 'add',
750 'userid' => $userid,
751 'courseid' => $courseid,
752 'groupid' => optional_param('groupid', null, PARAM_INT),
753 'modid' => $modid,
754 'tagid' => optional_param('tagid', null, PARAM_INT),
755 'tag' => optional_param('tag', null, PARAM_INT),
756 'search' => optional_param('search', null, PARAM_INT));
757
758 $urlparams = array_filter($urlparams);
759 $addurl->params($urlparams);
760
761 $addlink = '<div class="addbloglink">';
762 $addlink .= '<a href="'.$addurl->out().'">'. $blogheaders['stradd'].'</a>';
763 $addlink .= '</div>';
764 echo $addlink;
cae83708 765 }
766 }
767
768 if ($entries) {
769 $count = 0;
cae83708 770 foreach ($entries as $entry) {
1c7b8b93 771 $blogentry = new blog_entry(null, $entry);
2591c7ae 772
2b6e53e8 773 // Get the required blog entry data to render it.
2591c7ae
DM
774 $blogentry->prepare_render();
775 echo $output->render($blogentry);
776
cae83708 777 $count++;
778 }
779
929d7a83 780 echo $OUTPUT->render($pagingbar);
cae83708 781
782 if (!$count) {
783 print '<br /><div style="text-align:center">'. get_string('noentriesyet', 'blog') .'</div><br />';
784 }
785
786 print $morelink.'<br />'."\n";
787 return;
788 }
789 }
790
2b6e53e8 791 // Find the base url from $_GET variables, for print_paging_bar.
cae83708 792 public function get_baseurl() {
793 $getcopy = $_GET;
794
795 unset($getcopy['blogpage']);
796
797 if (!empty($getcopy)) {
798 $first = false;
799 $querystring = '';
800
801 foreach ($getcopy as $var => $val) {
802 if (!$first) {
803 $first = true;
804 $querystring .= "?$var=$val";
805 } else {
806 $querystring .= '&amp;'.$var.'='.$val;
807 $hasparam = true;
808 }
809 }
810 } else {
811 $querystring = '?';
812 }
813
814 return strip_querystring(qualified_me()) . $querystring;
815
816 }
817}
818
819/**
820 * Abstract class for blog_filter objects.
821 * A set of core filters are implemented here. To write new filters, you need to subclass
822 * blog_filter and give it the name of the type you want (for example, blog_filter_entry).
823 * The blog_filter abstract class will automatically use it when the filter is added to the
824 * URL. The first parameter of the constructor is the ID of your filter, but it can be a string
825 * or have any other meaning you wish it to have. The second parameter is called $type and is
826 * used as a sub-type for filters that have a very similar implementation (see blog_filter_context for an example)
827 */
828abstract class blog_filter {
829 /**
830 * An array of strings representing the available filter types for each blog_filter.
1c7b8b93 831 * @var array $availabletypes
cae83708 832 */
1c7b8b93 833 public $availabletypes = array();
cae83708 834
835 /**
836 * The type of filter (for example, types of blog_filter_context are site, course and module)
837 * @var string $type
838 */
839 public $type;
840
841 /**
842 * The unique ID for a filter's associated record
843 * @var int $id
844 */
845 public $id;
846
847 /**
848 * An array of table aliases that are used in the WHERE conditions
849 * @var array $tables
850 */
851 public $tables = array();
852
853 /**
854 * An array of WHERE conditions
855 * @var array $conditions
856 */
857 public $conditions = array();
858
859 /**
860 * An array of SQL params
861 * @var array $params
862 */
863 public $params = array();
864
865 /**
866 * An array of filter types which this particular filter type overrides: their conditions will not be evaluated
867 */
868 public $overrides = array();
869
870 public function __construct($id, $type=null) {
871 $this->id = $id;
872 $this->type = $type;
873 }
874
875 /**
876 * TODO This is poor design. A parent class should not know anything about its children.
877 * The default case helps to resolve this design issue
878 */
879 public static function get_instance($id, $type) {
880
881 switch ($type) {
882 case 'site':
883 case 'course':
884 case 'module':
885 return new blog_filter_context($id, $type);
886 break;
887
888 case 'group':
889 case 'user':
890 return new blog_filter_user($id, $type);
891 break;
892
893 case 'tag':
894 return new blog_filter_tag($id);
895 break;
896
897 default:
1c7b8b93
NC
898 $classname = "blog_filter_$type";
899 if (class_exists($classname)) {
900 return new $classname($id, $type);
cae83708 901 }
902 }
903 }
904}
905
906/**
907 * This filter defines the context level of the blog entries being searched: site, course, module
908 */
909class blog_filter_context extends blog_filter {
910 /**
911 * Constructor
912 *
913 * @param string $type
914 * @param int $id
915 */
916 public function __construct($id=null, $type='site') {
917 global $SITE, $CFG, $DB;
918
919 if (empty($id)) {
920 $this->type = 'site';
921 } else {
922 $this->id = $id;
923 $this->type = $type;
924 }
925
2b6e53e8
AD
926 $this->availabletypes = array('site' => get_string('site'),
927 'course' => get_string('course'),
abea2c5d
MG
928 'module' => get_string('activity'),
929 'context' => get_string('coresystem'));
cae83708 930
931 switch ($this->type) {
932 case 'course': // Careful of site course!
2b6e53e8 933 // Ignore course filter if blog associations are not enabled.
cae83708 934 if ($this->id != $SITE->id && !empty($CFG->useblogassociations)) {
abea2c5d 935 $this->overrides = array('site', 'context');
41b38360 936 $context = context_course::instance($this->id);
cae83708 937 $this->tables['ba'] = 'blog_association';
1c7b8b93 938 $this->conditions[] = 'p.id = ba.blogid';
cae83708 939 $this->conditions[] = 'ba.contextid = '.$context->id;
940 break;
941 } else {
2b6e53e8 942 // We are dealing with the site course, do not break from the current case.
cae83708 943 }
944
945 case 'site':
2b6e53e8 946 // No special constraints.
cae83708 947 break;
948 case 'module':
949 if (!empty($CFG->useblogassociations)) {
abea2c5d 950 $this->overrides = array('course', 'site', 'context');
cae83708 951
41b38360 952 $context = context_module::instance($this->id);
cae83708 953 $this->tables['ba'] = 'blog_association';
1c7b8b93
NC
954 $this->tables['p'] = 'post';
955 $this->conditions = array('p.id = ba.blogid', 'ba.contextid = ?');
cae83708 956 $this->params = array($context->id);
957 }
958 break;
abea2c5d
MG
959 case 'context':
960 if ($id != context_system::instance()->id && !empty($CFG->useblogassociations)) {
961 $this->overrides = array('site');
962 $context = context::instance_by_id($this->id);
963 $this->tables['ba'] = 'blog_association';
964 $this->tables['ctx'] = 'context';
965 $this->conditions[] = 'p.id = ba.blogid';
966 $this->conditions[] = 'ctx.id = ba.contextid';
967 $this->conditions[] = 'ctx.path LIKE ?';
968 $this->params = array($context->path . '%');
969 }
970 break;
971
cae83708 972 }
973 }
974}
975
976/**
977 * This filter defines the user level of the blog entries being searched: a userid or a groupid.
978 * It can be combined with a context filter in order to refine the search.
979 */
980class blog_filter_user extends blog_filter {
981 public $tables = array('u' => 'user');
982
983 /**
984 * Constructor
985 *
986 * @param string $type
987 * @param int $id
988 */
989 public function __construct($id=null, $type='user') {
320ae23a 990 global $CFG, $DB, $USER;
1c7b8b93 991 $this->availabletypes = array('user' => get_string('user'), 'group' => get_string('group'));
cae83708 992
993 if (empty($id)) {
994 $this->id = $USER->id;
995 $this->type = 'user';
996 } else {
997 $this->id = $id;
998 $this->type = $type;
999 }
1000
1001 if ($this->type == 'user') {
1002 $this->conditions = array('u.id = ?');
1003 $this->params = array($this->id);
1004 $this->overrides = array('group');
1005
2b6e53e8 1006 } else if ($this->type == 'group') {
cae83708 1007 $this->overrides = array('course', 'site');
1008
1009 $this->tables['gm'] = 'groups_members';
1c7b8b93 1010 $this->conditions[] = 'p.userid = gm.userid';
cae83708 1011 $this->conditions[] = 'gm.groupid = ?';
1012 $this->params[] = $this->id;
1013
2b6e53e8 1014 if (!empty($CFG->useblogassociations)) { // Only show blog entries associated with this course.
41b38360 1015 $coursecontext = context_course::instance($DB->get_field('groups', 'courseid', array('id' => $this->id)));
cae83708 1016 $this->tables['ba'] = 'blog_association';
1017 $this->conditions[] = 'gm.groupid = ?';
1018 $this->conditions[] = 'ba.contextid = ?';
1c7b8b93 1019 $this->conditions[] = 'ba.blogid = p.id';
cae83708 1020 $this->params[] = $this->id;
1c7b8b93 1021 $this->params[] = $coursecontext->id;
cae83708 1022 }
1023 }
b73d1ca4 1024
cae83708 1025 }
1026}
1027
1028/**
1029 * This filter defines a tag by which blog entries should be searched.
1030 */
1031class blog_filter_tag extends blog_filter {
1c7b8b93 1032 public $tables = array('t' => 'tag', 'ti' => 'tag_instance', 'p' => 'post');
cae83708 1033
1034 /**
1035 * Constructor
1036 *
1037 * @return void
1038 */
1039 public function __construct($id) {
1040 global $DB;
1041 $this->id = $id;
1042
1043 $this->conditions = array('ti.tagid = t.id',
1c7b8b93 1044 "ti.itemtype = 'post'",
abea2c5d 1045 "ti.component = 'core'",
1c7b8b93 1046 'ti.itemid = p.id',
cae83708 1047 't.id = ?');
1048 $this->params = array($this->id);
1049 }
1050}
1051
1052/**
1053 * This filter defines a specific blog entry id.
1054 */
1055class blog_filter_entry extends blog_filter {
1056 public $conditions = array('p.id = ?');
1057 public $overrides = array('site', 'course', 'module', 'group', 'user', 'tag');
1058
1059 public function __construct($id) {
1060 $this->id = $id;
1061 $this->params[] = $this->id;
1062 }
1063}
1064
1c7b8b93 1065/**
caee6e6c 1066 * This filter restricts the results to a time interval in seconds up to time()
1c7b8b93
NC
1067 */
1068class blog_filter_since extends blog_filter {
1069 public function __construct($interval) {
1070 $this->conditions[] = 'p.lastmodified >= ? AND p.lastmodified <= ?';
caee6e6c
PS
1071 $this->params[] = time() - $interval;
1072 $this->params[] = time();
1c7b8b93
NC
1073 }
1074}
1075
cae83708 1076/**
1077 * Filter used to perform full-text search on an entry's subject, summary and content
1078 */
1079class blog_filter_search extends blog_filter {
1080
1c7b8b93 1081 public function __construct($searchterm) {
cae83708 1082 global $DB;
c014d57c
PS
1083 $this->conditions = array("(".$DB->sql_like('p.summary', '?', false)." OR
1084 ".$DB->sql_like('p.content', '?', false)." OR
1085 ".$DB->sql_like('p.subject', '?', false).")");
1c7b8b93
NC
1086 $this->params[] = "%$searchterm%";
1087 $this->params[] = "%$searchterm%";
1088 $this->params[] = "%$searchterm%";
cae83708 1089 }
1090}
2591c7ae
DM
1091
1092
1093/**
1094 * Renderable class to represent an entry attachment
1095 */
1096class blog_entry_attachment implements renderable {
1097
1098 public $filename;
1099 public $url;
1100 public $file;
1101
1102 /**
f8133217
DM
1103 * Gets the file data
1104 *
2591c7ae
DM
1105 * @param stored_file $file
1106 * @param int $entryid Attachment entry id
1107 */
1108 public function __construct($file, $entryid) {
1109
f8133217 1110 global $CFG;
2591c7ae
DM
1111
1112 $this->file = $file;
af7e05d6 1113 $this->filename = $file->get_filename();
2b6e53e8
AD
1114 $this->url = file_encode_url($CFG->wwwroot . '/pluginfile.php',
1115 '/' . SYSCONTEXTID . '/blog/attachment/' . $entryid . '/' . $this->filename);
2591c7ae
DM
1116 }
1117
1118}