MDL-45336 scorm: copied used string from quiz
[moodle.git] / blog / locallib.php
blob5df39ad929f806b5a9695af83e6f8deaaa347fcc
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
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/>.
17 /**
18 * Classes for Blogs.
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
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->libdir . '/filelib.php');
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
34 * the fields of the post table.
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
41 class blog_entry implements renderable {
42 // Public Database fields.
43 public $id;
44 public $userid;
45 public $subject;
46 public $summary;
47 public $rating = 0;
48 public $attachment;
49 public $publishstate;
51 // Locked Database fields (Don't touch these).
52 public $courseid = 0;
53 public $groupid = 0;
54 public $module = 'blog';
55 public $moduleid = 0;
56 public $coursemoduleid = 0;
57 public $content;
58 public $format = 1;
59 public $uniquehash = '';
60 public $lastmodified;
61 public $created;
62 public $usermodified;
64 // Other class variables.
65 public $form;
66 public $tags = array();
68 /** @var StdClass Data needed to render the entry */
69 public $renderable;
71 /**
72 * Constructor. If given an id, will fetch the corresponding record from the DB.
74 * @param mixed $idorparams A blog entry id if INT, or data for a new entry if array
76 public function __construct($id=null, $params=null, $form=null) {
77 global $DB, $PAGE, $CFG;
79 if (!empty($id)) {
80 $object = $DB->get_record('post', array('id' => $id));
81 foreach ($object as $var => $val) {
82 $this->$var = $val;
84 } else if (!empty($params) && (is_array($params) || is_object($params))) {
85 foreach ($params as $var => $val) {
86 $this->$var = $val;
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;
102 $this->form = $form;
107 * Gets the required data to print the entry
109 public function prepare_render() {
111 global $DB, $CFG, $PAGE;
113 $this->renderable = new StdClass();
115 $this->renderable->user = $DB->get_record('user', array('id'=>$this->userid));
117 // Entry comments.
118 if (!empty($CFG->usecomments) and $CFG->blogusecomments) {
119 require_once($CFG->dirroot . '/comment/lib.php');
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);
131 $this->summary = file_rewrite_pluginfile_urls($this->summary, 'pluginfile.php', SYSCONTEXTID, 'blog', 'post', $this->id);
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);
137 $this->renderable->externalblogtext = get_string('retrievedfrom', 'blog') . get_string('labelsep', 'langconfig');
138 $this->renderable->externalblogtext .= html_writer::link($urlparts['scheme'] . '://' . $urlparts['host'],
139 $externalblog->name);
143 // Retrieve associations.
144 $this->renderable->unassociatedentry = false;
145 if (!empty($CFG->useblogassociations)) {
147 // Adding the entry associations data.
148 if ($associations = $associations = $DB->get_records('blog_association', array('blogid' => $this->id))) {
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;
155 foreach ($associations as $key => $assocrec) {
157 if (!$context = context::instance_by_id($assocrec->contextid, IGNORE_MISSING)) {
158 unset($associations[$key]);
159 continue;
162 // The renderer will need the contextlevel of the association.
163 $associations[$key]->contextlevel = $context->contextlevel;
165 // Course associations.
166 if ($context->contextlevel == CONTEXT_COURSE) {
167 // TODO: performance!!!!
168 $instancename = $DB->get_field('course', 'shortname', array('id' => $context->instanceid));
170 $associations[$key]->url = $assocurl = new moodle_url('/course/view.php',
171 array('id' => $context->instanceid));
172 $associations[$key]->text = $instancename;
173 $associations[$key]->icon = new pix_icon('i/course', $associations[$key]->text);
176 // Mod associations.
177 if ($context->contextlevel == CONTEXT_MODULE) {
179 // Getting the activity type and the activity instance id.
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));
184 // TODO: performance!!!!
185 $instancename = $DB->get_field($modinfo->name, 'name', array('id' => $modinfo->instance));
187 $associations[$key]->type = get_string('modulename', $modinfo->name);
188 $associations[$key]->url = new moodle_url('/mod/' . $modinfo->name . '/view.php',
189 array('id' => $context->instanceid));
190 $associations[$key]->text = $instancename;
191 $associations[$key]->icon = new pix_icon('icon', $associations[$key]->text, $modinfo->name);
195 $this->renderable->blogassociations = $associations;
198 // Entry attachments.
199 $this->renderable->attachments = $this->get_attachments();
201 $this->renderable->usercanedit = blog_user_can_edit_entry($this);
206 * Gets the entry attachments list
207 * @return array List of blog_entry_attachment instances
209 public function get_attachments() {
211 global $CFG;
213 require_once($CFG->libdir.'/filelib.php');
215 $syscontext = context_system::instance();
217 $fs = get_file_storage();
218 $files = $fs->get_area_files($syscontext->id, 'blog', 'attachment', $this->id);
220 // Adding a blog_entry_attachment for each non-directory file.
221 $attachments = array();
222 foreach ($files as $file) {
223 if ($file->is_directory()) {
224 continue;
226 $attachments[] = new blog_entry_attachment($file, $this->id);
229 return $attachments;
233 * Inserts this entry in the database. Access control checks must be done by calling code.
235 * @param mform $form Used for attachments
236 * @return void
238 public function process_attachment($form) {
239 $this->form = $form;
243 * Inserts this entry in the database. Access control checks must be done by calling code.
244 * TODO Set the publishstate correctly
245 * @return void
247 public function add() {
248 global $CFG, $USER, $DB;
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();
256 // Insert the new blog entry.
257 $this->id = $DB->insert_record('post', $this);
259 // Update tags.
260 $this->add_tags_info();
262 if (!empty($CFG->useblogassociations)) {
263 $this->add_associations();
266 tag_set('post', $this->id, $this->tags, 'core', context_user::instance($this->userid)->id);
268 // Trigger an event for the new entry.
269 $event = \core\event\blog_entry_created::create(array(
270 'objectid' => $this->id,
271 'relateduserid' => $this->userid
273 $event->set_blog_entry($this);
274 $event->trigger();
278 * Updates this entry in the database. Access control checks must be done by calling code.
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.
285 * @return void
287 public function edit($params=array(), $form=null, $summaryoptions=array(), $attachmentoptions=array()) {
288 global $CFG, $DB;
290 $sitecontext = context_system::instance();
291 $entry = $this;
293 $this->form = $form;
294 foreach ($params as $var => $val) {
295 $entry->$var = $val;
298 $entry = file_postupdate_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
299 $entry = file_postupdate_standard_filemanager($entry,
300 'attachment',
301 $attachmentoptions,
302 $sitecontext,
303 'blog',
304 'attachment',
305 $entry->id);
307 if (!empty($CFG->useblogassociations)) {
308 $entry->add_associations();
311 $entry->lastmodified = time();
313 // Update record.
314 $DB->update_record('post', $entry);
315 tag_set('post', $entry->id, $entry->tags, 'core', context_user::instance($this->userid)->id);
317 $event = \core\event\blog_entry_updated::create(array(
318 'objectid' => $entry->id,
319 'relateduserid' => $entry->userid
321 $event->set_blog_entry($entry);
322 $event->trigger();
326 * Deletes this entry from the database. Access control checks must be done by calling code.
328 * @return void
330 public function delete() {
331 global $DB;
333 $this->delete_attachments();
334 $this->remove_associations();
336 // Get record to pass onto the event.
337 $record = $DB->get_record('post', array('id' => $this->id));
338 $DB->delete_records('post', array('id' => $this->id));
339 tag_set('post', $this->id, array(), 'core', context_user::instance($this->userid)->id);
341 $event = \core\event\blog_entry_deleted::create(array(
342 'objectid' => $this->id,
343 'relateduserid' => $this->userid
345 $event->add_record_snapshot("post", $record);
346 $event->set_blog_entry($this);
347 $event->trigger();
351 * Function to add all context associations to an entry.
352 * TODO : Remove $action in 2.9 (MDL-41330)
354 * @param string $action - This does nothing, do not use it. This is present only for Backward compatibility.
356 public function add_associations($action = null) {
358 if (!empty($action)) {
359 debugging('blog_entry->add_associations() does not accept any argument', DEBUG_DEVELOPER);
362 $this->remove_associations();
364 if (!empty($this->courseassoc)) {
365 $this->add_association($this->courseassoc);
368 if (!empty($this->modassoc)) {
369 $this->add_association($this->modassoc);
374 * Add a single association for a blog entry
375 * TODO : Remove $action in 2.9 (MDL-41330)
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.
380 public function add_association($contextid, $action = null) {
381 global $DB;
383 if (!empty($action)) {
384 debugging('blog_entry->add_association() accepts only one argument', DEBUG_DEVELOPER);
387 $assocobject = new StdClass;
388 $assocobject->contextid = $contextid;
389 $assocobject->blogid = $this->id;
390 $id = $DB->insert_record('blog_association', $assocobject);
392 // Trigger an association created event.
393 $context = context::instance_by_id($contextid);
394 $eventparam = array(
395 'objectid' => $id,
396 'other' => array('associateid' => $context->instanceid, 'subject' => $this->subject, 'blogid' => $this->id),
397 'relateduserid' => $this->userid
399 if ($context->contextlevel == CONTEXT_COURSE) {
400 $eventparam['other']['associatetype'] = 'course';
402 } else if ($context->contextlevel == CONTEXT_MODULE) {
403 $eventparam['other']['associatetype'] = 'coursemodule';
405 $event = \core\event\blog_association_created::create($eventparam);
406 $event->trigger();
410 * remove all associations for a blog entry
411 * @return voic
413 public function remove_associations() {
414 global $DB;
415 $DB->delete_records('blog_association', array('blogid' => $this->id));
419 * Deletes all the user files in the attachments area for an entry
421 * @return void
423 public function delete_attachments() {
424 $fs = get_file_storage();
425 $fs->delete_area_files(SYSCONTEXTID, 'blog', 'attachment', $this->id);
426 $fs->delete_area_files(SYSCONTEXTID, 'blog', 'post', $this->id);
430 * function to attach tags into an entry
431 * @return void
433 public function add_tags_info() {
435 $tags = array();
437 if ($otags = optional_param('otags', '', PARAM_INT)) {
438 foreach ($otags as $tagid) {
439 // TODO : make this use the tag name in the form.
440 if ($tag = tag_get('id', $tagid)) {
441 $tags[] = $tag->name;
446 tag_set('post', $this->id, $tags, 'core', context_user::instance($this->userid)->id);
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.
455 * @param int $userid Optional. If not given, $USER is used
456 * @return boolean
458 public function can_user_edit($userid=null) {
459 global $CFG, $USER;
461 if (empty($userid)) {
462 $userid = $USER->id;
465 $sitecontext = context_system::instance();
467 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
468 return true; // Can edit any blog entry.
471 if ($this->userid == $userid && has_capability('moodle/blog:create', $sitecontext)) {
472 return true; // Can edit own when having blog:create capability.
475 return false;
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
483 * @param int $targetuserid ID of the user we are checking
485 * @return bool
487 public function can_user_view($targetuserid) {
488 global $CFG, $USER, $DB;
489 $sitecontext = context_system::instance();
491 if (empty($CFG->enableblogs) || !has_capability('moodle/blog:view', $sitecontext)) {
492 return false; // Blog system disabled or user has no blog view capability.
495 if (isloggedin() && $USER->id == $targetuserid) {
496 return true; // Can view own entries in any case.
499 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
500 return true; // Can manage all entries.
503 // Coming for 1 entry, make sure it's not a draft.
504 if ($this->publishstate == 'draft' && !has_capability('moodle/blog:viewdrafts', $sitecontext)) {
505 return false; // Can not view draft of others.
508 // Coming for 1 entry, make sure user is logged in, if not a public blog.
509 if ($this->publishstate != 'public' && !isloggedin()) {
510 return false;
513 switch ($CFG->bloglevel) {
514 case BLOG_GLOBAL_LEVEL:
515 return true;
516 break;
518 case BLOG_SITE_LEVEL:
519 if (isloggedin()) { // Not logged in viewers forbidden.
520 return true;
522 return false;
523 break;
525 case BLOG_USER_LEVEL:
526 default:
527 $personalcontext = context_user::instance($targetuserid);
528 return has_capability('moodle/user:readuserblogs', $personalcontext);
529 break;
534 * Use this function to retrieve a list of publish states available for
535 * the currently logged in user.
537 * @return array This function returns an array ideal for sending to moodles'
538 * choose_from_menu function.
541 public static function get_applicable_publish_states() {
542 global $CFG;
543 $options = array();
545 // Everyone gets draft access.
546 if ($CFG->bloglevel >= BLOG_USER_LEVEL) {
547 $options['draft'] = get_string('publishtonoone', 'blog');
550 if ($CFG->bloglevel > BLOG_USER_LEVEL) {
551 $options['site'] = get_string('publishtosite', 'blog');
554 if ($CFG->bloglevel >= BLOG_GLOBAL_LEVEL) {
555 $options['public'] = get_string('publishtoworld', 'blog');
558 return $options;
563 * Abstract Blog_Listing class: used to gather blog entries and output them as listings. One of the subclasses must be used.
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
570 class blog_listing {
572 * Array of blog_entry objects.
573 * @var array $entries
575 public $entries = array();
578 * An array of blog_filter_* objects
579 * @var array $filters
581 public $filters = array();
584 * Constructor
586 * @param array $filters An associative array of filtername => filterid
588 public function __construct($filters=array()) {
589 // Unset filters overridden by more specific filters.
590 foreach ($filters as $type => $id) {
591 if (!empty($type) && !empty($id)) {
592 $this->filters[$type] = blog_filter::get_instance($id, $type);
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]);
606 * Fetches the array of blog entries.
608 * @return array
610 public function get_entries($start=0, $limit=10) {
611 global $DB;
613 if (empty($this->entries)) {
614 if ($sqlarray = $this->get_entry_fetch_sql(false, 'created DESC')) {
615 $this->entries = $DB->get_records_sql($sqlarray['sql'], $sqlarray['params'], $start, $limit);
616 } else {
617 return false;
621 return $this->entries;
624 public function get_entry_fetch_sql($count=false, $sort='lastmodified DESC', $userid = false) {
625 global $DB, $USER, $CFG;
627 if (!$userid) {
628 $userid = $USER->id;
631 $allnamefields = get_all_user_name_fields(true, 'u');
632 // The query used to locate blog entries is complicated. It will be built from the following components:
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\')');
638 // Build up a clause for permission constraints.
640 $params = array();
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.
644 if (has_capability('moodle/user:readuserblogs', context_system::instance())) {
645 // Don't add permission constraints.
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.
652 } else {
653 if (isloggedin() and !isguestuser()) {
654 // Dont check association records if there aren't any.
655 $assocexists = $DB->record_exists('blog_association', array());
657 // Begin permission sql clause.
658 $permissionsql = '(p.userid = ? ';
659 $params[] = $userid;
661 if ($CFG->bloglevel >= BLOG_SITE_LEVEL) { // Add permission to view site-level entries.
662 $permissionsql .= " OR p.publishstate = 'site' ";
665 if ($CFG->bloglevel >= BLOG_GLOBAL_LEVEL) { // Add permission to view global entries.
666 $permissionsql .= " OR p.publishstate = 'public' ";
669 $permissionsql .= ') '; // Close permissions sql clause.
670 } else { // Default is access to public entries.
671 $permissionsql = "p.publishstate = 'public'";
673 $conditions[] = $permissionsql; // Add permission constraints.
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);
682 $tablessql = ''; // Build up the FROM clause.
683 foreach ($tables as $tablename => $table) {
684 $tablessql .= ($tablessql ? ', ' : '').'{'.$table.'} '.$tablename;
687 $sql = ($count) ? 'SELECT COUNT(*)' : 'SELECT ' . $requiredfields;
688 $sql .= " FROM $tablessql WHERE " . implode(' AND ', $conditions);
689 $sql .= ($count) ? '' : " ORDER BY $sort";
691 return array('sql' => $sql, 'params' => $params);
695 * Outputs all the blog entries aggregated by this blog listing.
697 * @return void
699 public function print_entries() {
700 global $CFG, $USER, $DB, $OUTPUT, $PAGE;
701 $sitecontext = context_system::instance();
703 // Blog renderer.
704 $output = $PAGE->get_renderer('blog');
706 $page = optional_param('blogpage', 0, PARAM_INT);
707 $limit = optional_param('limit', get_user_preferences('blogpagesize', 10), PARAM_INT);
708 $start = $page * $limit;
710 $morelink = '<br />&nbsp;&nbsp;';
712 if ($sqlarray = $this->get_entry_fetch_sql(true)) {
713 $totalentries = $DB->count_records_sql($sqlarray['sql'], $sqlarray['params']);
714 } else {
715 $totalentries = 0;
718 $entries = $this->get_entries($start, $limit);
719 $pagingbar = new paging_bar($totalentries, $page, $limit, $this->get_baseurl());
720 $pagingbar->pagevar = 'blogpage';
721 $blogheaders = blog_get_headers();
723 echo $OUTPUT->render($pagingbar);
725 if (has_capability('moodle/blog:create', $sitecontext)) {
726 // The user's blog is enabled and they are viewing their own blog.
727 $userid = optional_param('userid', null, PARAM_INT);
729 if (empty($userid) || (!empty($userid) && $userid == $USER->id)) {
731 $courseid = optional_param('courseid', null, PARAM_INT);
732 $modid = optional_param('modid', null, PARAM_INT);
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));
744 $urlparams = array_filter($urlparams);
745 $addurl->params($urlparams);
747 $addlink = '<div class="addbloglink">';
748 $addlink .= '<a href="'.$addurl->out().'">'. $blogheaders['stradd'].'</a>';
749 $addlink .= '</div>';
750 echo $addlink;
754 if ($entries) {
755 $count = 0;
756 foreach ($entries as $entry) {
757 $blogentry = new blog_entry(null, $entry);
759 // Get the required blog entry data to render it.
760 $blogentry->prepare_render();
761 echo $output->render($blogentry);
763 $count++;
766 echo $OUTPUT->render($pagingbar);
768 if (!$count) {
769 print '<br /><div style="text-align:center">'. get_string('noentriesyet', 'blog') .'</div><br />';
772 print $morelink.'<br />'."\n";
773 return;
777 // Find the base url from $_GET variables, for print_paging_bar.
778 public function get_baseurl() {
779 $getcopy = $_GET;
781 unset($getcopy['blogpage']);
783 if (!empty($getcopy)) {
784 $first = false;
785 $querystring = '';
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;
796 } else {
797 $querystring = '?';
800 return strip_querystring(qualified_me()) . $querystring;
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)
814 abstract class blog_filter {
816 * An array of strings representing the available filter types for each blog_filter.
817 * @var array $availabletypes
819 public $availabletypes = array();
822 * The type of filter (for example, types of blog_filter_context are site, course and module)
823 * @var string $type
825 public $type;
828 * The unique ID for a filter's associated record
829 * @var int $id
831 public $id;
834 * An array of table aliases that are used in the WHERE conditions
835 * @var array $tables
837 public $tables = array();
840 * An array of WHERE conditions
841 * @var array $conditions
843 public $conditions = array();
846 * An array of SQL params
847 * @var array $params
849 public $params = array();
852 * An array of filter types which this particular filter type overrides: their conditions will not be evaluated
854 public $overrides = array();
856 public function __construct($id, $type=null) {
857 $this->id = $id;
858 $this->type = $type;
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
865 public static function get_instance($id, $type) {
867 switch ($type) {
868 case 'site':
869 case 'course':
870 case 'module':
871 return new blog_filter_context($id, $type);
872 break;
874 case 'group':
875 case 'user':
876 return new blog_filter_user($id, $type);
877 break;
879 case 'tag':
880 return new blog_filter_tag($id);
881 break;
883 default:
884 $classname = "blog_filter_$type";
885 if (class_exists($classname)) {
886 return new $classname($id, $type);
893 * This filter defines the context level of the blog entries being searched: site, course, module
895 class blog_filter_context extends blog_filter {
897 * Constructor
899 * @param string $type
900 * @param int $id
902 public function __construct($id=null, $type='site') {
903 global $SITE, $CFG, $DB;
905 if (empty($id)) {
906 $this->type = 'site';
907 } else {
908 $this->id = $id;
909 $this->type = $type;
912 $this->availabletypes = array('site' => get_string('site'),
913 'course' => get_string('course'),
914 'module' => get_string('activity'));
916 switch ($this->type) {
917 case 'course': // Careful of site course!
918 // Ignore course filter if blog associations are not enabled.
919 if ($this->id != $SITE->id && !empty($CFG->useblogassociations)) {
920 $this->overrides = array('site');
921 $context = context_course::instance($this->id);
922 $this->tables['ba'] = 'blog_association';
923 $this->conditions[] = 'p.id = ba.blogid';
924 $this->conditions[] = 'ba.contextid = '.$context->id;
925 break;
926 } else {
927 // We are dealing with the site course, do not break from the current case.
930 case 'site':
931 // No special constraints.
932 break;
933 case 'module':
934 if (!empty($CFG->useblogassociations)) {
935 $this->overrides = array('course', 'site');
937 $context = context_module::instance($this->id);
938 $this->tables['ba'] = 'blog_association';
939 $this->tables['p'] = 'post';
940 $this->conditions = array('p.id = ba.blogid', 'ba.contextid = ?');
941 $this->params = array($context->id);
943 break;
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.
952 class blog_filter_user extends blog_filter {
953 public $tables = array('u' => 'user');
956 * Constructor
958 * @param string $type
959 * @param int $id
961 public function __construct($id=null, $type='user') {
962 global $CFG, $DB, $USER;
963 $this->availabletypes = array('user' => get_string('user'), 'group' => get_string('group'));
965 if (empty($id)) {
966 $this->id = $USER->id;
967 $this->type = 'user';
968 } else {
969 $this->id = $id;
970 $this->type = $type;
973 if ($this->type == 'user') {
974 $this->conditions = array('u.id = ?');
975 $this->params = array($this->id);
976 $this->overrides = array('group');
978 } else if ($this->type == 'group') {
979 $this->overrides = array('course', 'site');
981 $this->tables['gm'] = 'groups_members';
982 $this->conditions[] = 'p.userid = gm.userid';
983 $this->conditions[] = 'gm.groupid = ?';
984 $this->params[] = $this->id;
986 if (!empty($CFG->useblogassociations)) { // Only show blog entries associated with this course.
987 $coursecontext = context_course::instance($DB->get_field('groups', 'courseid', array('id' => $this->id)));
988 $this->tables['ba'] = 'blog_association';
989 $this->conditions[] = 'gm.groupid = ?';
990 $this->conditions[] = 'ba.contextid = ?';
991 $this->conditions[] = 'ba.blogid = p.id';
992 $this->params[] = $this->id;
993 $this->params[] = $coursecontext->id;
1001 * This filter defines a tag by which blog entries should be searched.
1003 class blog_filter_tag extends blog_filter {
1004 public $tables = array('t' => 'tag', 'ti' => 'tag_instance', 'p' => 'post');
1007 * Constructor
1009 * @return void
1011 public function __construct($id) {
1012 global $DB;
1013 $this->id = $id;
1015 $this->conditions = array('ti.tagid = t.id',
1016 "ti.itemtype = 'post'",
1017 'ti.itemid = p.id',
1018 't.id = ?');
1019 $this->params = array($this->id);
1024 * This filter defines a specific blog entry id.
1026 class blog_filter_entry extends blog_filter {
1027 public $conditions = array('p.id = ?');
1028 public $overrides = array('site', 'course', 'module', 'group', 'user', 'tag');
1030 public function __construct($id) {
1031 $this->id = $id;
1032 $this->params[] = $this->id;
1037 * This filter restricts the results to a time interval in seconds up to time()
1039 class blog_filter_since extends blog_filter {
1040 public function __construct($interval) {
1041 $this->conditions[] = 'p.lastmodified >= ? AND p.lastmodified <= ?';
1042 $this->params[] = time() - $interval;
1043 $this->params[] = time();
1048 * Filter used to perform full-text search on an entry's subject, summary and content
1050 class blog_filter_search extends blog_filter {
1052 public function __construct($searchterm) {
1053 global $DB;
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).")");
1057 $this->params[] = "%$searchterm%";
1058 $this->params[] = "%$searchterm%";
1059 $this->params[] = "%$searchterm%";
1065 * Renderable class to represent an entry attachment
1067 class blog_entry_attachment implements renderable {
1069 public $filename;
1070 public $url;
1071 public $file;
1074 * Gets the file data
1076 * @param stored_file $file
1077 * @param int $entryid Attachment entry id
1079 public function __construct($file, $entryid) {
1081 global $CFG;
1083 $this->file = $file;
1084 $this->filename = $file->get_filename();
1085 $this->url = file_encode_url($CFG->wwwroot . '/pluginfile.php',
1086 '/' . SYSCONTEXTID . '/blog/attachment/' . $entryid . '/' . $this->filename);