2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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');
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.
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.
51 // Locked Database fields (Don't touch these).
54 public $module = 'blog';
56 public $coursemoduleid = 0;
59 public $uniquehash = '';
64 // Other class variables.
66 public $tags = array();
68 /** @var StdClass Data needed to render the entry */
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;
80 $object = $DB->get_record('post', array('id' => $id));
81 foreach ($object as $var => $val) {
84 } else if (!empty($params) && (is_array($params) ||
is_object($params))) {
85 foreach ($params as $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
;
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
));
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]);
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
);
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() {
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()) {
226 $attachments[] = new blog_entry_attachment($file, $this->id
);
233 * Inserts this entry in the database. Access control checks must be done by calling code.
235 * @param mform $form Used for attachments
238 public function process_attachment($form) {
243 * Inserts this entry in the database. Access control checks must be done by calling code.
244 * TODO Set the publishstate correctly
247 public function add() {
248 global $CFG, $USER, $DB;
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 if (!empty($CFG->useblogassociations
)) {
260 $this->add_associations();
263 core_tag_tag
::set_item_tags('core', 'post', $this->id
, context_user
::instance($this->userid
), $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
270 $event->set_blog_entry($this);
275 * Updates this entry in the database. Access control checks must be done by calling code.
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.
284 public function edit($params=array(), $form=null, $summaryoptions=array(), $attachmentoptions=array()) {
287 $sitecontext = context_system
::instance();
291 foreach ($params as $var => $val) {
295 $entry = file_postupdate_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id
);
296 $entry = file_postupdate_standard_filemanager($entry,
304 if (!empty($CFG->useblogassociations
)) {
305 $entry->add_associations();
308 $entry->lastmodified
= time();
311 $DB->update_record('post', $entry);
312 core_tag_tag
::set_item_tags('core', 'post', $entry->id
, context_user
::instance($this->userid
), $entry->tags
);
314 $event = \core\event\blog_entry_updated
::create(array(
315 'objectid' => $entry->id
,
316 'relateduserid' => $entry->userid
318 $event->set_blog_entry($entry);
323 * Deletes this entry from the database. Access control checks must be done by calling code.
327 public function delete() {
330 $this->delete_attachments();
331 $this->remove_associations();
333 // Get record to pass onto the event.
334 $record = $DB->get_record('post', array('id' => $this->id
));
335 $DB->delete_records('post', array('id' => $this->id
));
336 core_tag_tag
::remove_all_item_tags('core', 'post', $this->id
);
338 $event = \core\event\blog_entry_deleted
::create(array(
339 'objectid' => $this->id
,
340 'relateduserid' => $this->userid
342 $event->add_record_snapshot("post", $record);
343 $event->set_blog_entry($this);
348 * Function to add all context associations to an entry.
350 * @param string $unused This does nothing, do not use it.
352 public function add_associations($unused = null) {
354 if ($unused !== null) {
355 debugging('Illegal argument used in blog_entry->add_associations()', DEBUG_DEVELOPER
);
358 $this->remove_associations();
360 if (!empty($this->courseassoc
)) {
361 $this->add_association($this->courseassoc
);
364 if (!empty($this->modassoc
)) {
365 $this->add_association($this->modassoc
);
370 * Add a single association for a blog entry
372 * @param int $contextid - id of context to associate with the blog entry.
373 * @param string $unused This does nothing, do not use it.
375 public function add_association($contextid, $unused = null) {
378 if ($unused !== null) {
379 debugging('Illegal argument used in blog_entry->add_association()', DEBUG_DEVELOPER
);
382 $assocobject = new StdClass
;
383 $assocobject->contextid
= $contextid;
384 $assocobject->blogid
= $this->id
;
385 $id = $DB->insert_record('blog_association', $assocobject);
387 // Trigger an association created event.
388 $context = context
::instance_by_id($contextid);
391 'other' => array('associateid' => $context->instanceid
, 'subject' => $this->subject
, 'blogid' => $this->id
),
392 'relateduserid' => $this->userid
394 if ($context->contextlevel
== CONTEXT_COURSE
) {
395 $eventparam['other']['associatetype'] = 'course';
397 } else if ($context->contextlevel
== CONTEXT_MODULE
) {
398 $eventparam['other']['associatetype'] = 'coursemodule';
400 $event = \core\event\blog_association_created
::create($eventparam);
405 * remove all associations for a blog entry
408 public function remove_associations() {
410 $DB->delete_records('blog_association', array('blogid' => $this->id
));
414 * Deletes all the user files in the attachments area for an entry
418 public function delete_attachments() {
419 $fs = get_file_storage();
420 $fs->delete_area_files(SYSCONTEXTID
, 'blog', 'attachment', $this->id
);
421 $fs->delete_area_files(SYSCONTEXTID
, 'blog', 'post', $this->id
);
425 * User can edit a blog entry if this is their own blog entry and they have
426 * the capability moodle/blog:create, or if they have the capability
427 * moodle/blog:manageentries.
428 * This also applies to deleting of entries.
430 * @param int $userid Optional. If not given, $USER is used
433 public function can_user_edit($userid=null) {
436 if (empty($userid)) {
440 $sitecontext = context_system
::instance();
442 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
443 return true; // Can edit any blog entry.
446 if ($this->userid
== $userid && has_capability('moodle/blog:create', $sitecontext)) {
447 return true; // Can edit own when having blog:create capability.
454 * Checks to see if a user can view the blogs of another user.
455 * Only blog level is checked here, the capabilities are enforced
458 * @param int $targetuserid ID of the user we are checking
462 public function can_user_view($targetuserid) {
463 global $CFG, $USER, $DB;
464 $sitecontext = context_system
::instance();
466 if (empty($CFG->enableblogs
) ||
!has_capability('moodle/blog:view', $sitecontext)) {
467 return false; // Blog system disabled or user has no blog view capability.
470 if (isloggedin() && $USER->id
== $targetuserid) {
471 return true; // Can view own entries in any case.
474 if (has_capability('moodle/blog:manageentries', $sitecontext)) {
475 return true; // Can manage all entries.
478 // Coming for 1 entry, make sure it's not a draft.
479 if ($this->publishstate
== 'draft' && !has_capability('moodle/blog:viewdrafts', $sitecontext)) {
480 return false; // Can not view draft of others.
483 // Coming for 1 entry, make sure user is logged in, if not a public blog.
484 if ($this->publishstate
!= 'public' && !isloggedin()) {
488 switch ($CFG->bloglevel
) {
489 case BLOG_GLOBAL_LEVEL
:
493 case BLOG_SITE_LEVEL
:
494 if (isloggedin()) { // Not logged in viewers forbidden.
500 case BLOG_USER_LEVEL
:
502 $personalcontext = context_user
::instance($targetuserid);
503 return has_capability('moodle/user:readuserblogs', $personalcontext);
509 * Use this function to retrieve a list of publish states available for
510 * the currently logged in user.
512 * @return array This function returns an array ideal for sending to moodles'
513 * choose_from_menu function.
516 public static function get_applicable_publish_states() {
520 // Everyone gets draft access.
521 if ($CFG->bloglevel
>= BLOG_USER_LEVEL
) {
522 $options['draft'] = get_string('publishtonoone', 'blog');
525 if ($CFG->bloglevel
> BLOG_USER_LEVEL
) {
526 $options['site'] = get_string('publishtosite', 'blog');
529 if ($CFG->bloglevel
>= BLOG_GLOBAL_LEVEL
) {
530 $options['public'] = get_string('publishtoworld', 'blog');
538 * Abstract Blog_Listing class: used to gather blog entries and output them as listings. One of the subclasses must be used.
540 * @package moodlecore
542 * @copyright 2009 Nicolas Connault
543 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
547 * Array of blog_entry objects.
548 * @var array $entries
550 public $entries = null;
553 * Caches the total number of the entries.
556 public $totalentries = null;
559 * An array of blog_filter_* objects
560 * @var array $filters
562 public $filters = array();
567 * @param array $filters An associative array of filtername => filterid
569 public function __construct($filters=array()) {
570 // Unset filters overridden by more specific filters.
571 foreach ($filters as $type => $id) {
572 if (!empty($type) && !empty($id)) {
573 $this->filters
[$type] = blog_filter
::get_instance($id, $type);
577 foreach ($this->filters
as $type => $filter) {
578 foreach ($filter->overrides
as $override) {
579 if (array_key_exists($override, $this->filters
)) {
580 unset($this->filters
[$override]);
587 * Fetches the array of blog entries.
591 public function get_entries($start=0, $limit=10) {
594 if ($this->entries
=== null) {
595 if ($sqlarray = $this->get_entry_fetch_sql(false, 'created DESC')) {
596 $this->entries
= $DB->get_records_sql($sqlarray['sql'], $sqlarray['params'], $start, $limit);
597 if (!$start && count($this->entries
) < $limit) {
598 $this->totalentries
= count($this->entries
);
605 return $this->entries
;
609 * Finds total number of blog entries
613 public function count_entries() {
615 if ($this->totalentries
=== null) {
616 if ($sqlarray = $this->get_entry_fetch_sql(true)) {
617 $this->totalentries
= $DB->count_records_sql($sqlarray['sql'], $sqlarray['params']);
619 $this->totalentries
= 0;
622 return $this->totalentries
;
625 public function get_entry_fetch_sql($count=false, $sort='lastmodified DESC', $userid = false) {
626 global $DB, $USER, $CFG;
632 $allnamefields = \user_picture
::fields('u', null, 'useridalias');
633 // The query used to locate blog entries is complicated. It will be built from the following components:
634 $requiredfields = "p.*, $allnamefields"; // The SELECT clause.
635 $tables = array('p' => 'post', 'u' => 'user'); // Components of the FROM clause (table_id => table_name).
636 // Components of the WHERE clause (conjunction).
637 $conditions = array('u.deleted = 0', 'p.userid = u.id', '(p.module = \'blog\' OR p.module = \'blog_external\')');
639 // Build up a clause for permission constraints.
643 // Fix for MDL-9165, use with readuserblogs capability in a user context can read that user's private blogs.
644 // Admins can see all blogs regardless of publish states, as described on the help page.
645 if (has_capability('moodle/user:readuserblogs', context_system
::instance())) {
646 // Don't add permission constraints.
648 } else if (!empty($this->filters
['user'])
649 && has_capability('moodle/user:readuserblogs',
650 context_user
::instance((empty($this->filters
['user']->id
) ?
0 : $this->filters
['user']->id
)))) {
651 // Don't add permission constraints.
654 if (isloggedin() and !isguestuser()) {
655 // Dont check association records if there aren't any.
656 $assocexists = $DB->record_exists('blog_association', array());
658 // Begin permission sql clause.
659 $permissionsql = '(p.userid = ? ';
662 if ($CFG->bloglevel
>= BLOG_SITE_LEVEL
) { // Add permission to view site-level entries.
663 $permissionsql .= " OR p.publishstate = 'site' ";
666 if ($CFG->bloglevel
>= BLOG_GLOBAL_LEVEL
) { // Add permission to view global entries.
667 $permissionsql .= " OR p.publishstate = 'public' ";
670 $permissionsql .= ') '; // Close permissions sql clause.
671 } else { // Default is access to public entries.
672 $permissionsql = "p.publishstate = 'public'";
674 $conditions[] = $permissionsql; // Add permission constraints.
677 foreach ($this->filters
as $type => $blogfilter) {
678 $conditions = array_merge($conditions, $blogfilter->conditions
);
679 $params = array_merge($params, $blogfilter->params
);
680 $tables = array_merge($tables, $blogfilter->tables
);
683 $tablessql = ''; // Build up the FROM clause.
684 foreach ($tables as $tablename => $table) {
685 $tablessql .= ($tablessql ?
', ' : '').'{'.$table.'} '.$tablename;
688 $sql = ($count) ?
'SELECT COUNT(*)' : 'SELECT ' . $requiredfields;
689 $sql .= " FROM $tablessql WHERE " . implode(' AND ', $conditions);
690 $sql .= ($count) ?
'' : " ORDER BY $sort";
692 return array('sql' => $sql, 'params' => $params);
696 * Outputs all the blog entries aggregated by this blog listing.
700 public function print_entries() {
701 global $CFG, $USER, $DB, $OUTPUT, $PAGE;
702 $sitecontext = context_system
::instance();
705 $output = $PAGE->get_renderer('blog');
707 $page = optional_param('blogpage', 0, PARAM_INT
);
708 $limit = optional_param('limit', get_user_preferences('blogpagesize', 10), PARAM_INT
);
709 $start = $page * $limit;
711 $morelink = '<br /> ';
713 $entries = $this->get_entries($start, $limit);
714 $totalentries = $this->count_entries();
715 $pagingbar = new paging_bar($totalentries, $page, $limit, $this->get_baseurl());
716 $pagingbar->pagevar
= 'blogpage';
717 $blogheaders = blog_get_headers();
719 echo $OUTPUT->render($pagingbar);
721 if (has_capability('moodle/blog:create', $sitecontext)) {
722 // The user's blog is enabled and they are viewing their own blog.
723 $userid = optional_param('userid', null, PARAM_INT
);
725 if (empty($userid) ||
(!empty($userid) && $userid == $USER->id
)) {
727 $courseid = optional_param('courseid', null, PARAM_INT
);
728 $modid = optional_param('modid', null, PARAM_INT
);
730 $addurl = new moodle_url("$CFG->wwwroot/blog/edit.php");
731 $urlparams = array('action' => 'add',
733 'courseid' => $courseid,
734 'groupid' => optional_param('groupid', null, PARAM_INT
),
736 'tagid' => optional_param('tagid', null, PARAM_INT
),
737 'tag' => optional_param('tag', null, PARAM_INT
),
738 'search' => optional_param('search', null, PARAM_INT
));
740 $urlparams = array_filter($urlparams);
741 $addurl->params($urlparams);
743 $addlink = '<div class="addbloglink">';
744 $addlink .= '<a href="'.$addurl->out().'">'. $blogheaders['stradd'].'</a>';
745 $addlink .= '</div>';
752 foreach ($entries as $entry) {
753 $blogentry = new blog_entry(null, $entry);
755 // Get the required blog entry data to render it.
756 $blogentry->prepare_render();
757 echo $output->render($blogentry);
762 echo $OUTPUT->render($pagingbar);
765 print '<br /><div style="text-align:center">'. get_string('noentriesyet', 'blog') .'</div><br />';
768 print $morelink.'<br />'."\n";
773 // Find the base url from $_GET variables, for print_paging_bar.
774 public function get_baseurl() {
777 unset($getcopy['blogpage']);
779 if (!empty($getcopy)) {
783 foreach ($getcopy as $var => $val) {
786 $querystring .= "?$var=$val";
788 $querystring .= '&'.$var.'='.$val;
796 return strip_querystring(qualified_me()) . $querystring;
802 * Abstract class for blog_filter objects.
803 * A set of core filters are implemented here. To write new filters, you need to subclass
804 * blog_filter and give it the name of the type you want (for example, blog_filter_entry).
805 * The blog_filter abstract class will automatically use it when the filter is added to the
806 * URL. The first parameter of the constructor is the ID of your filter, but it can be a string
807 * or have any other meaning you wish it to have. The second parameter is called $type and is
808 * used as a sub-type for filters that have a very similar implementation (see blog_filter_context for an example)
810 abstract class blog_filter
{
812 * An array of strings representing the available filter types for each blog_filter.
813 * @var array $availabletypes
815 public $availabletypes = array();
818 * The type of filter (for example, types of blog_filter_context are site, course and module)
824 * The unique ID for a filter's associated record
830 * An array of table aliases that are used in the WHERE conditions
833 public $tables = array();
836 * An array of WHERE conditions
837 * @var array $conditions
839 public $conditions = array();
842 * An array of SQL params
845 public $params = array();
848 * An array of filter types which this particular filter type overrides: their conditions will not be evaluated
850 public $overrides = array();
852 public function __construct($id, $type=null) {
858 * TODO This is poor design. A parent class should not know anything about its children.
859 * The default case helps to resolve this design issue
861 public static function get_instance($id, $type) {
867 return new blog_filter_context($id, $type);
872 return new blog_filter_user($id, $type);
876 return new blog_filter_tag($id);
880 $classname = "blog_filter_$type";
881 if (class_exists($classname)) {
882 return new $classname($id, $type);
889 * This filter defines the context level of the blog entries being searched: site, course, module
891 class blog_filter_context
extends blog_filter
{
895 * @param string $type
898 public function __construct($id=null, $type='site') {
899 global $SITE, $CFG, $DB;
902 $this->type
= 'site';
908 $this->availabletypes
= array('site' => get_string('site'),
909 'course' => get_string('course'),
910 'module' => get_string('activity'),
911 'context' => get_string('coresystem'));
913 switch ($this->type
) {
914 case 'course': // Careful of site course!
915 // Ignore course filter if blog associations are not enabled.
916 if ($this->id
!= $SITE->id
&& !empty($CFG->useblogassociations
)) {
917 $this->overrides
= array('site', 'context');
918 $context = context_course
::instance($this->id
);
919 $this->tables
['ba'] = 'blog_association';
920 $this->conditions
[] = 'p.id = ba.blogid';
921 $this->conditions
[] = 'ba.contextid = '.$context->id
;
924 // We are dealing with the site course, do not break from the current case.
928 // No special constraints.
931 if (!empty($CFG->useblogassociations
)) {
932 $this->overrides
= array('course', 'site', 'context');
934 $context = context_module
::instance($this->id
);
935 $this->tables
['ba'] = 'blog_association';
936 $this->tables
['p'] = 'post';
937 $this->conditions
= array('p.id = ba.blogid', 'ba.contextid = ?');
938 $this->params
= array($context->id
);
942 if ($id != context_system
::instance()->id
&& !empty($CFG->useblogassociations
)) {
943 $this->overrides
= array('site');
944 $context = context
::instance_by_id($this->id
);
945 $this->tables
['ba'] = 'blog_association';
946 $this->tables
['ctx'] = 'context';
947 $this->conditions
[] = 'p.id = ba.blogid';
948 $this->conditions
[] = 'ctx.id = ba.contextid';
949 $this->conditions
[] = 'ctx.path LIKE ?';
950 $this->params
= array($context->path
. '%');
959 * This filter defines the user level of the blog entries being searched: a userid or a groupid.
960 * It can be combined with a context filter in order to refine the search.
962 class blog_filter_user
extends blog_filter
{
963 public $tables = array('u' => 'user');
968 * @param string $type
971 public function __construct($id=null, $type='user') {
972 global $CFG, $DB, $USER;
973 $this->availabletypes
= array('user' => get_string('user'), 'group' => get_string('group'));
976 $this->id
= $USER->id
;
977 $this->type
= 'user';
983 if ($this->type
== 'user') {
984 $this->conditions
= array('u.id = ?');
985 $this->params
= array($this->id
);
986 $this->overrides
= array('group');
988 } else if ($this->type
== 'group') {
989 $this->overrides
= array('course', 'site');
991 $this->tables
['gm'] = 'groups_members';
992 $this->conditions
[] = 'p.userid = gm.userid';
993 $this->conditions
[] = 'gm.groupid = ?';
994 $this->params
[] = $this->id
;
996 if (!empty($CFG->useblogassociations
)) { // Only show blog entries associated with this course.
997 $coursecontext = context_course
::instance($DB->get_field('groups', 'courseid', array('id' => $this->id
)));
998 $this->tables
['ba'] = 'blog_association';
999 $this->conditions
[] = 'gm.groupid = ?';
1000 $this->conditions
[] = 'ba.contextid = ?';
1001 $this->conditions
[] = 'ba.blogid = p.id';
1002 $this->params
[] = $this->id
;
1003 $this->params
[] = $coursecontext->id
;
1011 * This filter defines a tag by which blog entries should be searched.
1013 class blog_filter_tag
extends blog_filter
{
1014 public $tables = array('t' => 'tag', 'ti' => 'tag_instance', 'p' => 'post');
1021 public function __construct($id) {
1025 $this->conditions
= array('ti.tagid = t.id',
1026 "ti.itemtype = 'post'",
1027 "ti.component = 'core'",
1030 $this->params
= array($this->id
);
1035 * This filter defines a specific blog entry id.
1037 class blog_filter_entry
extends blog_filter
{
1038 public $conditions = array('p.id = ?');
1039 public $overrides = array('site', 'course', 'module', 'group', 'user', 'tag');
1041 public function __construct($id) {
1043 $this->params
[] = $this->id
;
1048 * This filter restricts the results to a time interval in seconds up to time()
1050 class blog_filter_since
extends blog_filter
{
1051 public function __construct($interval) {
1052 $this->conditions
[] = 'p.lastmodified >= ? AND p.lastmodified <= ?';
1053 $this->params
[] = time() - $interval;
1054 $this->params
[] = time();
1059 * Filter used to perform full-text search on an entry's subject, summary and content
1061 class blog_filter_search
extends blog_filter
{
1063 public function __construct($searchterm) {
1065 $this->conditions
= array("(".$DB->sql_like('p.summary', '?', false)." OR
1066 ".$DB->sql_like('p.content', '?', false)." OR
1067 ".$DB->sql_like('p.subject', '?', false).")");
1068 $this->params
[] = "%$searchterm%";
1069 $this->params
[] = "%$searchterm%";
1070 $this->params
[] = "%$searchterm%";
1076 * Renderable class to represent an entry attachment
1078 class blog_entry_attachment
implements renderable
{
1085 * Gets the file data
1087 * @param stored_file $file
1088 * @param int $entryid Attachment entry id
1090 public function __construct($file, $entryid) {
1094 $this->file
= $file;
1095 $this->filename
= $file->get_filename();
1096 $this->url
= file_encode_url($CFG->wwwroot
. '/pluginfile.php',
1097 '/' . SYSCONTEXTID
. '/blog/attachment/' . $entryid . '/' . $this->filename
);