3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
23 * @copyright 2009 Nicolas Connault
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir . '/filelib.php');
32 * Blog_entry class. Represents an entry in a user's blog. Contains all methods for managing this entry.
33 * This class does not contain any HTML-generating code. See blog_listing sub-classes for such code.
34 * This class follows the Object Relational Mapping technique, its member variables being mapped to
35 * the fields of the post table.
39 * @copyright 2009 Nicolas Connault
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class blog_entry implements renderable {
43 // Public Database fields
52 // Locked Database fields (Don't touch these)
55 public $module = 'blog';
57 public $coursemoduleid = 0;
60 public $uniquehash = '';
65 // Other class variables
67 public $tags = array();
69 /** @var StdClass Data needed to render the entry */
74 * Constructor. If given an id, will fetch the corresponding record from the DB.
76 * @param mixed $idorparams A blog entry id if INT, or data for a new entry if array
78 public function __construct($id=null, $params=null, $form=null) {
79 global $DB, $PAGE, $CFG;
82 $object = $DB->get_record('post', array('id' => $id));
83 foreach ($object as $var => $val) {
86 } else if (!empty($params) && (is_array($params) || is_object($params))) {
87 foreach ($params as $var => $val) {
92 if (!empty($CFG->useblogassociations)) {
93 $associations = $DB->get_records('blog_association', array('blogid' => $this->id));
94 foreach ($associations as $association) {
95 $context = context::instance_by_id($association->contextid);
96 if ($context->contextlevel == CONTEXT_COURSE) {
97 $this->courseassoc = $association->contextid;
98 } else if ($context->contextlevel == CONTEXT_MODULE) {
99 $this->modassoc = $association->contextid;
109 * Gets the required data to print the entry
111 public function prepare_render() {
113 global $DB, $CFG, $PAGE;
115 $this->renderable = new StdClass();
117 $this->renderable->user = $DB->get_record('user', array('id'=>$this->userid));
120 if (!empty($CFG->usecomments) and $CFG->blogusecomments) {
121 require_once($CFG->dirroot . '/comment/lib.php');
123 $cmt = new stdClass();
124 $cmt->context = context_user::instance($this->userid);
125 $cmt->courseid = $PAGE->course->id;
126 $cmt->area = 'format_blog';
127 $cmt->itemid = $this->id;
128 $cmt->showcount = $CFG->blogshowcommentscount;
129 $cmt->component = 'blog';
130 $this->renderable->comment = new comment($cmt);
133 $this->summary = file_rewrite_pluginfile_urls($this->summary, 'pluginfile.php', SYSCONTEXTID, 'blog', 'post', $this->id);
135 // External blog link.
136 if ($this->uniquehash && $this->content) {
137 if ($externalblog = $DB->get_record('blog_external', array('id' => $this->content))) {
138 $urlparts = parse_url($externalblog->url);
139 $this->renderable->externalblogtext = get_string('retrievedfrom', 'blog') . get_string('labelsep', 'langconfig');
140 $this->renderable->externalblogtext .= html_writer::link($urlparts['scheme'] . '://'.$urlparts['host'], $externalblog->name);
144 // Retrieve associations
145 $this->renderable->unassociatedentry = false;
146 if (!empty($CFG->useblogassociations)) {
148 // Adding the entry associations data.
149 if ($associations = $associations = $DB->get_records('blog_association', array('blogid' => $this->id))) {
151 // Check to see if the entry is unassociated with group/course level access.
152 if ($this->publishstate == 'group' || $this->publishstate == 'course') {
153 $this->renderable->unassociatedentry = true;
156 foreach ($associations as $key => $assocrec) {
158 if (!$context = context::instance_by_id($assocrec->contextid, IGNORE_MISSING)) {
159 unset($associations[$key]);
163 // The renderer will need the contextlevel of the association.
164 $associations[$key]->contextlevel = $context->contextlevel;
166 // Course associations.
167 if ($context->contextlevel == CONTEXT_COURSE) {
168 $instancename = $DB->get_field('course', 'shortname', array('id' => $context->instanceid)); //TODO: performance!!!!
170 $associations[$key]->url = $assocurl = new moodle_url('/course/view.php', array('id' => $context->instanceid));
171 $associations[$key]->text = $instancename;
172 $associations[$key]->icon = new pix_icon('i/course', $associations[$key]->text);
176 if ($context->contextlevel == CONTEXT_MODULE) {
178 // Getting the activity type and the activity instance id
179 $sql = 'SELECT cm.instance, m.name FROM {course_modules} cm
180 JOIN {modules} m ON m.id = cm.module
181 WHERE cm.id = :cmid';
182 $modinfo = $DB->get_record_sql($sql, array('cmid' => $context->instanceid));
183 $instancename = $DB->get_field($modinfo->name, 'name', array('id' => $modinfo->instance)); //TODO: performance!!!!
185 $associations[$key]->type = get_string('modulename', $modinfo->name);
186 $associations[$key]->url = new moodle_url('/mod/' . $modinfo->name . '/view.php', array('id' => $context->instanceid));
187 $associations[$key]->text = $instancename;
188 $associations[$key]->icon = new pix_icon('icon', $associations[$key]->text, $modinfo->name);
192 $this->renderable->blogassociations = $associations;
195 // Entry attachments.
196 $this->renderable->attachments = $this->get_attachments();
198 $this->renderable->usercanedit = blog_user_can_edit_entry($this);
203 * Gets the entry attachments list
204 * @return array List of blog_entry_attachment instances
206 function get_attachments() {
210 require_once($CFG->libdir.'/filelib.php');
212 $syscontext = context_system::instance();
214 $fs = get_file_storage();
215 $files = $fs->get_area_files($syscontext->id, 'blog', 'attachment', $this->id);
217 // Adding a blog_entry_attachment for each non-directory file.
218 $attachments = array();
219 foreach ($files as $file) {
220 if ($file->is_directory()) {
223 $attachments[] = new blog_entry_attachment($file, $this->id);
230 * Inserts this entry in the database. Access control checks must be done by calling code.
232 * @param mform $form Used for attachments
235 public function process_attachment($form) {
240 * Inserts this entry in the database. Access control checks must be done by calling code.
241 * TODO Set the publishstate correctly
244 public function add() {
245 global $CFG, $USER, $DB;
248 $this->module = 'blog';
249 $this->userid = (empty($this->userid)) ? $USER->id : $this->userid;
250 $this->lastmodified = time();
251 $this->created = time();
253 // Insert the new blog entry.
254 $this->id = $DB->insert_record('post', $this);
257 $this->add_tags_info();
259 if (!empty($CFG->useblogassociations)) {
260 $this->add_associations();
263 tag_set('post', $this->id, $this->tags);
265 // Trigger an event for the new entry.
266 $event = \core\event\blog_entry_created::create(array(
267 'objectid' => $this->id,
268 'relateduserid' => $this->userid,
269 'other' => array('subject' => $this->subject)
271 $event->set_custom_data($this);
276 * Updates this entry in the database. Access control checks must be done by calling code.
278 * @param mform $form Used for attachments
281 public function edit($params=array(), $form=null, $summaryoptions=array(), $attachmentoptions=array()) {
282 global $CFG, $USER, $DB, $PAGE;
284 $sitecontext = context_system::instance();
288 foreach ($params as $var => $val) {
292 $entry = file_postupdate_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
293 $entry = file_postupdate_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog', 'attachment', $entry->id);
295 if (!empty($CFG->useblogassociations)) {
296 $entry->add_associations();
299 $entry->lastmodified = time();
302 $DB->update_record('post', $entry);
303 tag_set('post', $entry->id, $entry->tags);
305 add_to_log(SITEID, 'blog', 'update', 'index.php?userid='.$USER->id.'&entryid='.$entry->id, $entry->subject);
306 events_trigger('blog_entry_edited', $entry);
310 * Deletes this entry from the database. Access control checks must be done by calling code.
314 public function delete() {
317 $this->delete_attachments();
318 $this->remove_associations();
320 // Get record to pass onto the event.
321 $record = $DB->get_record('post', array('id' => $this->id));
322 $DB->delete_records('post', array('id' => $this->id));
323 tag_set('post', $this->id, array());
325 $event = \core\event\blog_entry_deleted::create(array(
326 'objectid' => $this->id,
327 'relateduserid' => $this->userid,
328 'other' => array('record' => (array) $record)
330 $event->add_record_snapshot("post", $record);
331 $event->set_custom_data($this);
336 * function to add all context associations to an entry
337 * @param int entry - data object processed to include all 'entry' fields and extra data from the edit_form object
339 public function add_associations($action='add') {
342 $this->remove_associations();
344 if (!empty($this->courseassoc)) {
345 $this->add_association($this->courseassoc, $action);
348 if (!empty($this->modassoc)) {
349 $this->add_association($this->modassoc, $action);
354 * add a single association for a blog entry
355 * @param int contextid - id of context to associate with the blog entry
357 public function add_association($contextid, $action='add') {
360 $assocobject = new StdClass;
361 $assocobject->contextid = $contextid;
362 $assocobject->blogid = $this->id;
363 $DB->insert_record('blog_association', $assocobject);
365 $context = context::instance_by_id($contextid);
368 if ($context->contextlevel == CONTEXT_COURSE) {
369 $courseid = $context->instanceid;
370 add_to_log($courseid, 'blog', $action, 'index.php?userid='.$this->userid.'&entryid='.$this->id, $this->subject);
371 } else if ($context->contextlevel == CONTEXT_MODULE) {
372 $cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
373 $modulename = $DB->get_field('modules', 'name', array('id' => $cm->module));
374 add_to_log($cm->course, 'blog', $action, 'index.php?userid='.$this->userid.'&entryid='.$this->id, $this->subject, $cm->id, $this->userid);
379 * remove all associations for a blog entry
382 public function remove_associations() {
384 $DB->delete_records('blog_association', array('blogid' => $this->id));
388 * Deletes all the user files in the attachments area for an entry
392 public function delete_attachments() {
393 $fs = get_file_storage();
394 $fs->delete_area_files(SYSCONTEXTID, 'blog', 'attachment', $this->id);
395 $fs->delete_area_files(SYSCONTEXTID, 'blog', 'post', $this->id);
399 * function to attach tags into an entry
402 public function add_tags_info() {
406 if ($otags = optional_param('otags', '', PARAM_INT)) {
407 foreach ($otags as $tagid) {
408 // TODO : make this use the tag name in the form
409 if ($tag = tag_get('id', $tagid)) {
410 $tags[] = $tag->name;
415 tag_set('post', $this->id, $tags);
419 * User can edit a blog entry if this is their own blog entry and they have
420 * the capability moodle/blog:create, or if they have the capability
421 * moodle/blog:manageentries.
422 * This also applies to deleting of entries.
424 * @param int $userid Optional. If not given, $USER is used
427 public function can_user_edit($userid=null) {
430 if (empty($userid)) {
434 $sitecontext = context_system::instance();
436 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
437 return true; // can edit any blog entry
440 if ($this->userid == $userid && has_capability('moodle/blog:create', $sitecontext)) {
441 return true; // can edit own when having blog:create capability
448 * Checks to see if a user can view the blogs of another user.
449 * Only blog level is checked here, the capabilities are enforced
452 * @param int $targetuserid ID of the user we are checking
456 public function can_user_view($targetuserid) {
457 global $CFG, $USER, $DB;
458 $sitecontext = context_system::instance();
460 if (empty($CFG->enableblogs) || !has_capability('moodle/blog:view', $sitecontext)) {
461 return false; // blog system disabled or user has no blog view capability
464 if (isloggedin() && $USER->id == $targetuserid) {
465 return true; // can view own entries in any case
468 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
469 return true; // can manage all entries
472 // coming for 1 entry, make sure it's not a draft
473 if ($this->publishstate == 'draft' && !has_capability('moodle/blog:viewdrafts', $sitecontext)) {
474 return false; // can not view draft of others
477 // coming for 1 entry, make sure user is logged in, if not a public blog
478 if ($this->publishstate != 'public' && !isloggedin()) {
482 switch ($CFG->bloglevel) {
483 case BLOG_GLOBAL_LEVEL:
487 case BLOG_SITE_LEVEL:
488 if (isloggedin()) { // not logged in viewers forbidden
494 case BLOG_USER_LEVEL:
496 $personalcontext = context_user::instance($targetuserid);
497 return has_capability('moodle/user:readuserblogs', $personalcontext);
503 * Use this function to retrieve a list of publish states available for
504 * the currently logged in user.
506 * @return array This function returns an array ideal for sending to moodles'
507 * choose_from_menu function.
510 public static function get_applicable_publish_states() {
514 // everyone gets draft access
515 if ($CFG->bloglevel >= BLOG_USER_LEVEL) {
516 $options['draft'] = get_string('publishtonoone', 'blog');
519 if ($CFG->bloglevel > BLOG_USER_LEVEL) {
520 $options['site'] = get_string('publishtosite', 'blog');
523 if ($CFG->bloglevel >= BLOG_GLOBAL_LEVEL) {
524 $options['public'] = get_string('publishtoworld', 'blog');
532 * Abstract Blog_Listing class: used to gather blog entries and output them as listings. One of the subclasses must be used.
534 * @package moodlecore
536 * @copyright 2009 Nicolas Connault
537 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
541 * Array of blog_entry objects.
542 * @var array $entries
544 public $entries = array();
547 * An array of blog_filter_* objects
548 * @var array $filters
550 public $filters = array();
555 * @param array $filters An associative array of filtername => filterid
557 public function __construct($filters=array()) {
558 // Unset filters overridden by more specific filters
559 foreach ($filters as $type => $id) {
560 if (!empty($type) && !empty($id)) {
561 $this->filters[$type] = blog_filter::get_instance($id, $type);
565 foreach ($this->filters as $type => $filter) {
566 foreach ($filter->overrides as $override) {
567 if (array_key_exists($override, $this->filters)) {
568 unset($this->filters[$override]);
575 * Fetches the array of blog entries.
579 public function get_entries($start=0, $limit=10) {
582 if (empty($this->entries)) {
583 if ($sqlarray = $this->get_entry_fetch_sql(false, 'created DESC')) {
584 $this->entries = $DB->get_records_sql($sqlarray['sql'], $sqlarray['params'], $start, $limit);
590 return $this->entries;
593 public function get_entry_fetch_sql($count=false, $sort='lastmodified DESC', $userid = false) {
594 global $DB, $USER, $CFG;
600 $allnamefields = get_all_user_name_fields(true, 'u');
601 // The query used to locate blog entries is complicated. It will be built from the following components:
602 $requiredfields = "p.*, $allnamefields, u.email"; // the SELECT clause
603 $tables = array('p' => 'post', 'u' => 'user'); // components of the FROM clause (table_id => table_name)
604 $conditions = array('u.deleted = 0', 'p.userid = u.id', '(p.module = \'blog\' OR p.module = \'blog_external\')'); // components of the WHERE clause (conjunction)
606 // build up a clause for permission constraints
610 // fix for MDL-9165, use with readuserblogs capability in a user context can read that user's private blogs
611 // admins can see all blogs regardless of publish states, as described on the help page
612 if (has_capability('moodle/user:readuserblogs', context_system::instance())) {
613 // don't add permission constraints
615 } else if(!empty($this->filters['user']) && has_capability('moodle/user:readuserblogs',
616 context_user::instance((empty($this->filters['user']->id) ? 0 : $this->filters['user']->id)))) {
617 // don't add permission constraints
620 if (isloggedin() and !isguestuser()) {
621 $assocexists = $DB->record_exists('blog_association', array()); //dont check association records if there aren't any
623 //begin permission sql clause
624 $permissionsql = '(p.userid = ? ';
627 if ($CFG->bloglevel >= BLOG_SITE_LEVEL) { // add permission to view site-level entries
628 $permissionsql .= " OR p.publishstate = 'site' ";
631 if ($CFG->bloglevel >= BLOG_GLOBAL_LEVEL) { // add permission to view global entries
632 $permissionsql .= " OR p.publishstate = 'public' ";
635 $permissionsql .= ') '; //close permissions sql clause
636 } else { // default is access to public entries
637 $permissionsql = "p.publishstate = 'public'";
639 $conditions[] = $permissionsql; //add permission constraints
642 foreach ($this->filters as $type => $blogfilter) {
643 $conditions = array_merge($conditions, $blogfilter->conditions);
644 $params = array_merge($params, $blogfilter->params);
645 $tables = array_merge($tables, $blogfilter->tables);
648 $tablessql = ''; // build up the FROM clause
649 foreach ($tables as $tablename => $table) {
650 $tablessql .= ($tablessql ? ', ' : '').'{'.$table.'} '.$tablename;
653 $sql = ($count) ? 'SELECT COUNT(*)' : 'SELECT ' . $requiredfields;
654 $sql .= " FROM $tablessql WHERE " . implode(' AND ', $conditions);
655 $sql .= ($count) ? '' : " ORDER BY $sort";
657 return array('sql' => $sql, 'params' => $params);
661 * Outputs all the blog entries aggregated by this blog listing.
665 public function print_entries() {
666 global $CFG, $USER, $DB, $OUTPUT, $PAGE;
667 $sitecontext = context_system::instance();
670 $output = $PAGE->get_renderer('blog');
672 $page = optional_param('blogpage', 0, PARAM_INT);
673 $limit = optional_param('limit', get_user_preferences('blogpagesize', 10), PARAM_INT);
674 $start = $page * $limit;
676 $morelink = '<br /> ';
678 if ($sqlarray = $this->get_entry_fetch_sql(true)) {
679 $totalentries = $DB->count_records_sql($sqlarray['sql'], $sqlarray['params']);
684 $entries = $this->get_entries($start, $limit);
685 $pagingbar = new paging_bar($totalentries, $page, $limit, $this->get_baseurl());
686 $pagingbar->pagevar = 'blogpage';
687 $blogheaders = blog_get_headers();
689 echo $OUTPUT->render($pagingbar);
691 if (has_capability('moodle/blog:create', $sitecontext)) {
692 //the user's blog is enabled and they are viewing their own blog
693 $userid = optional_param('userid', null, PARAM_INT);
695 if (empty($userid) || (!empty($userid) && $userid == $USER->id)) {
697 $courseid = optional_param('courseid', null, PARAM_INT);
698 $modid = optional_param('modid', null, PARAM_INT);
700 $addurl = new moodle_url("$CFG->wwwroot/blog/edit.php");
701 $urlparams = array('action' => 'add',
703 'courseid' => $courseid,
704 'groupid' => optional_param('groupid', null, PARAM_INT),
706 'tagid' => optional_param('tagid', null, PARAM_INT),
707 'tag' => optional_param('tag', null, PARAM_INT),
708 'search' => optional_param('search', null, PARAM_INT));
710 $urlparams = array_filter($urlparams);
711 $addurl->params($urlparams);
713 $addlink = '<div class="addbloglink">';
714 $addlink .= '<a href="'.$addurl->out().'">'. $blogheaders['stradd'].'</a>';
715 $addlink .= '</div>';
722 foreach ($entries as $entry) {
723 $blogentry = new blog_entry(null, $entry);
725 // Get the required blog entry data to render it
726 $blogentry->prepare_render();
727 echo $output->render($blogentry);
732 echo $OUTPUT->render($pagingbar);
735 print '<br /><div style="text-align:center">'. get_string('noentriesyet', 'blog') .'</div><br />';
738 print $morelink.'<br />'."\n";
743 /// Find the base url from $_GET variables, for print_paging_bar
744 public function get_baseurl() {
747 unset($getcopy['blogpage']);
749 if (!empty($getcopy)) {
753 foreach ($getcopy as $var => $val) {
756 $querystring .= "?$var=$val";
758 $querystring .= '&'.$var.'='.$val;
766 return strip_querystring(qualified_me()) . $querystring;
772 * Abstract class for blog_filter objects.
773 * A set of core filters are implemented here. To write new filters, you need to subclass
774 * blog_filter and give it the name of the type you want (for example, blog_filter_entry).
775 * The blog_filter abstract class will automatically use it when the filter is added to the
776 * URL. The first parameter of the constructor is the ID of your filter, but it can be a string
777 * or have any other meaning you wish it to have. The second parameter is called $type and is
778 * used as a sub-type for filters that have a very similar implementation (see blog_filter_context for an example)
780 abstract class blog_filter {
782 * An array of strings representing the available filter types for each blog_filter.
783 * @var array $availabletypes
785 public $availabletypes = array();
788 * The type of filter (for example, types of blog_filter_context are site, course and module)
794 * The unique ID for a filter's associated record
800 * An array of table aliases that are used in the WHERE conditions
803 public $tables = array();
806 * An array of WHERE conditions
807 * @var array $conditions
809 public $conditions = array();
812 * An array of SQL params
815 public $params = array();
818 * An array of filter types which this particular filter type overrides: their conditions will not be evaluated
820 public $overrides = array();
822 public function __construct($id, $type=null) {
828 * TODO This is poor design. A parent class should not know anything about its children.
829 * The default case helps to resolve this design issue
831 public static function get_instance($id, $type) {
837 return new blog_filter_context($id, $type);
842 return new blog_filter_user($id, $type);
846 return new blog_filter_tag($id);
850 $classname = "blog_filter_$type";
851 if (class_exists($classname)) {
852 return new $classname($id, $type);
859 * This filter defines the context level of the blog entries being searched: site, course, module
861 class blog_filter_context extends blog_filter {
865 * @param string $type
868 public function __construct($id=null, $type='site') {
869 global $SITE, $CFG, $DB;
872 $this->type = 'site';
878 $this->availabletypes = array('site' => get_string('site'), 'course' => get_string('course'), 'module' => get_string('activity'));
880 switch ($this->type) {
881 case 'course': // Careful of site course!
882 // Ignore course filter if blog associations are not enabled
883 if ($this->id != $SITE->id && !empty($CFG->useblogassociations)) {
884 $this->overrides = array('site');
885 $context = context_course::instance($this->id);
886 $this->tables['ba'] = 'blog_association';
887 $this->conditions[] = 'p.id = ba.blogid';
888 $this->conditions[] = 'ba.contextid = '.$context->id;
891 // We are dealing with the site course, do not break from the current case
895 // No special constraints
898 if (!empty($CFG->useblogassociations)) {
899 $this->overrides = array('course', 'site');
901 $context = context_module::instance($this->id);
902 $this->tables['ba'] = 'blog_association';
903 $this->tables['p'] = 'post';
904 $this->conditions = array('p.id = ba.blogid', 'ba.contextid = ?');
905 $this->params = array($context->id);
913 * This filter defines the user level of the blog entries being searched: a userid or a groupid.
914 * It can be combined with a context filter in order to refine the search.
916 class blog_filter_user extends blog_filter {
917 public $tables = array('u' => 'user');
922 * @param string $type
925 public function __construct($id=null, $type='user') {
926 global $CFG, $DB, $USER;
927 $this->availabletypes = array('user' => get_string('user'), 'group' => get_string('group'));
930 $this->id = $USER->id;
931 $this->type = 'user';
937 if ($this->type == 'user') {
938 $this->conditions = array('u.id = ?');
939 $this->params = array($this->id);
940 $this->overrides = array('group');
942 } elseif ($this->type == 'group') {
943 $this->overrides = array('course', 'site');
945 $this->tables['gm'] = 'groups_members';
946 $this->conditions[] = 'p.userid = gm.userid';
947 $this->conditions[] = 'gm.groupid = ?';
948 $this->params[] = $this->id;
950 if (!empty($CFG->useblogassociations)) { // only show blog entries associated with this course
951 $coursecontext = context_course::instance($DB->get_field('groups', 'courseid', array('id' => $this->id)));
952 $this->tables['ba'] = 'blog_association';
953 $this->conditions[] = 'gm.groupid = ?';
954 $this->conditions[] = 'ba.contextid = ?';
955 $this->conditions[] = 'ba.blogid = p.id';
956 $this->params[] = $this->id;
957 $this->params[] = $coursecontext->id;
965 * This filter defines a tag by which blog entries should be searched.
967 class blog_filter_tag extends blog_filter {
968 public $tables = array('t' => 'tag', 'ti' => 'tag_instance', 'p' => 'post');
975 public function __construct($id) {
979 $this->conditions = array('ti.tagid = t.id',
980 "ti.itemtype = 'post'",
983 $this->params = array($this->id);
988 * This filter defines a specific blog entry id.
990 class blog_filter_entry extends blog_filter {
991 public $conditions = array('p.id = ?');
992 public $overrides = array('site', 'course', 'module', 'group', 'user', 'tag');
994 public function __construct($id) {
996 $this->params[] = $this->id;
1001 * This filter restricts the results to a time interval in seconds up to time()
1003 class blog_filter_since extends blog_filter {
1004 public function __construct($interval) {
1005 $this->conditions[] = 'p.lastmodified >= ? AND p.lastmodified <= ?';
1006 $this->params[] = time() - $interval;
1007 $this->params[] = time();
1012 * Filter used to perform full-text search on an entry's subject, summary and content
1014 class blog_filter_search extends blog_filter {
1016 public function __construct($searchterm) {
1018 $this->conditions = array("(".$DB->sql_like('p.summary', '?', false)." OR
1019 ".$DB->sql_like('p.content', '?', false)." OR
1020 ".$DB->sql_like('p.subject', '?', false).")");
1021 $this->params[] = "%$searchterm%";
1022 $this->params[] = "%$searchterm%";
1023 $this->params[] = "%$searchterm%";
1029 * Renderable class to represent an entry attachment
1031 class blog_entry_attachment implements renderable {
1038 * Gets the file data
1040 * @param stored_file $file
1041 * @param int $entryid Attachment entry id
1043 public function __construct($file, $entryid) {
1047 $this->file = $file;
1048 $this->filename = $file->get_filename();
1049 $this->url = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.SYSCONTEXTID.'/blog/attachment/'.$entryid.'/'.$this->filename);