|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 require_once($CFG->libdir . '/filelib.php'); 00030 00042 class blog_entry { 00043 // Public Database fields 00044 public $id; 00045 public $userid; 00046 public $subject; 00047 public $summary; 00048 public $rating = 0; 00049 public $attachment; 00050 public $publishstate; 00051 00052 // Locked Database fields (Don't touch these) 00053 public $courseid = 0; 00054 public $groupid = 0; 00055 public $module = 'blog'; 00056 public $moduleid = 0; 00057 public $coursemoduleid = 0; 00058 public $content; 00059 public $format = 1; 00060 public $uniquehash = ''; 00061 public $lastmodified; 00062 public $created; 00063 public $usermodified; 00064 00065 // Other class variables 00066 public $form; 00067 public $tags = array(); 00068 00069 // Methods 00075 public function __construct($id=null, $params=null, $form=null) { 00076 global $DB, $PAGE; 00077 00078 if (!empty($id)) { 00079 $object = $DB->get_record('post', array('id' => $id)); 00080 foreach ($object as $var => $val) { 00081 $this->$var = $val; 00082 } 00083 } else if (!empty($params) && (is_array($params) || is_object($params))) { 00084 foreach ($params as $var => $val) { 00085 $this->$var = $val; 00086 } 00087 } 00088 00089 $this->form = $form; 00090 } 00091 00098 public function print_html($return=false) { 00099 00100 global $USER, $CFG, $COURSE, $DB, $OUTPUT, $PAGE; 00101 00102 $user = $DB->get_record('user', array('id'=>$this->userid)); 00103 $cmttext = ''; 00104 if (!empty($CFG->usecomments) and $CFG->blogusecomments) { 00105 require_once($CFG->dirroot . '/comment/lib.php'); 00106 // Comments 00107 $cmt = new stdClass(); 00108 $cmt->context = get_context_instance(CONTEXT_USER, $user->id); 00109 $cmt->courseid = $PAGE->course->id; 00110 $cmt->area = 'format_blog'; 00111 $cmt->itemid = $this->id; 00112 $cmt->showcount = $CFG->blogshowcommentscount; 00113 $cmt->component = 'blog'; 00114 $comment = new comment($cmt); 00115 $cmttext = $comment->output(true); 00116 } 00117 $this->summary = file_rewrite_pluginfile_urls($this->summary, 'pluginfile.php', SYSCONTEXTID, 'blog', 'post', $this->id); 00118 00119 $options = array('overflowdiv'=>true); 00120 $template['body'] = format_text($this->summary, $this->summaryformat, $options); 00121 $template['title'] = format_string($this->subject); 00122 $template['userid'] = $user->id; 00123 $template['author'] = fullname($user); 00124 $template['created'] = userdate($this->created); 00125 00126 if ($this->created != $this->lastmodified) { 00127 $template['lastmod'] = userdate($this->lastmodified); 00128 } 00129 00130 $template['publishstate'] = $this->publishstate; 00131 00132 $stredit = get_string('edit'); 00133 $strdelete = get_string('delete'); 00134 00135 // Check to see if the entry is unassociated with group/course level access 00136 $unassociatedentry = false; 00137 if (!empty($CFG->useblogassociations) && ($this->publishstate == 'group' || $this->publishstate == 'course')) { 00138 if (!$DB->record_exists('blog_association', array('blogid' => $this->id))) { 00139 $unassociatedentry = true; 00140 } 00141 } 00142 00143 // Start printing of the blog 00144 $table = new html_table(); 00145 $table->cellspacing = 0; 00146 $table->attributes['class'] = 'forumpost blog_entry blog'. ($unassociatedentry ? 'draft' : $template['publishstate']); 00147 $table->attributes['id'] = 'b'.$this->id; 00148 $table->width = '100%'; 00149 00150 $picturecell = new html_table_cell(); 00151 $picturecell->attributes['class'] = 'picture left'; 00152 $picturecell->text = $OUTPUT->user_picture($user); 00153 00154 $table->head[] = $picturecell; 00155 00156 $topiccell = new html_table_cell(); 00157 $topiccell->attributes['class'] = 'topic starter'; 00158 $titlelink = html_writer::link(new moodle_url('/blog/index.php', array('entryid' => $this->id)), $template['title']); 00159 $topiccell->text = $OUTPUT->container($titlelink, 'subject'); 00160 $topiccell->text .= $OUTPUT->container_start('author'); 00161 00162 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $PAGE->course->id))); 00163 $by = new stdClass(); 00164 $by->name = html_writer::link(new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $PAGE->course->id)), $fullname); 00165 $by->date = $template['created']; 00166 00167 $topiccell->text .= get_string('bynameondate', 'forum', $by); 00168 $topiccell->text .= $OUTPUT->container_end(); 00169 00170 if ($this->uniquehash && $this->content) { 00171 if ($externalblog = $DB->get_record('blog_external', array('id' => $this->content))) { 00172 $urlparts = parse_url($externalblog->url); 00173 $topiccell->text .= $OUTPUT->container(get_string('retrievedfrom', 'blog').get_string('labelsep', 'langconfig').html_writer::link($urlparts['scheme'].'://'.$urlparts['host'], $externalblog->name), 'externalblog'); 00174 } 00175 } 00176 00177 $topiccell->header = false; 00178 $table->head[] = $topiccell; 00179 00180 // Actual content 00181 $mainrow = new html_table_row(); 00182 00183 $leftsidecell = new html_table_cell(); 00184 $leftsidecell->attributes['class'] = 'left side'; 00185 $mainrow->cells[] = $leftsidecell; 00186 00187 $contentcell = new html_table_cell(); 00188 $contentcell->attributes['class'] = 'content'; 00189 00190 $attachedimages = $OUTPUT->container($this->print_attachments(), 'attachments'); 00191 00192 // retrieve associations in case they're needed early 00193 $blogassociations = $DB->get_records('blog_association', array('blogid' => $this->id)); 00194 00195 // determine text for publish state 00196 switch ($template['publishstate']) { 00197 case 'draft': 00198 $blogtype = get_string('publishtonoone', 'blog'); 00199 break; 00200 case 'site': 00201 $blogtype = get_string('publishtosite', 'blog'); 00202 break; 00203 case 'public': 00204 $blogtype = get_string('publishtoworld', 'blog'); 00205 break; 00206 default: 00207 $blogtype = ''; 00208 break; 00209 00210 } 00211 00212 $contentcell->text .= $OUTPUT->container($blogtype, 'audience'); 00213 00214 $contentcell->text .= $template['body']; 00215 $contentcell->text .= $attachedimages; 00216 00217 // Uniquehash is used as a link to an external blog 00218 if (!empty($this->uniquehash)) { 00219 $contentcell->text .= $OUTPUT->container_start('externalblog'); 00220 $contentcell->text .= html_writer::link($this->uniquehash, get_string('linktooriginalentry', 'blog')); 00221 $contentcell->text .= $OUTPUT->container_end(); 00222 } 00223 00224 // Links to tags 00225 $officialtags = tag_get_tags_csv('post', $this->id, TAG_RETURN_HTML, 'official'); 00226 $defaulttags = tag_get_tags_csv('post', $this->id, TAG_RETURN_HTML, 'default'); 00227 00228 if (!empty($CFG->usetags) && ($officialtags || $defaulttags) ) { 00229 $contentcell->text .= $OUTPUT->container_start('tags'); 00230 00231 if ($officialtags) { 00232 $contentcell->text .= get_string('tags', 'tag') .': '. $OUTPUT->container($officialtags, 'officialblogtags'); 00233 if ($defaulttags) { 00234 $contentcell->text .= ', '; 00235 } 00236 } 00237 $contentcell->text .= $defaulttags; 00238 $contentcell->text .= $OUTPUT->container_end(); 00239 } 00240 00241 // Add associations 00242 if (!empty($CFG->useblogassociations) && $blogassociations) { 00243 $contentcell->text .= $OUTPUT->container_start('tags'); 00244 $assocstr = ''; 00245 $hascourseassocs = false; 00246 $assoctype = ''; 00247 00248 // First find and show the associated course 00249 foreach ($blogassociations as $assocrec) { 00250 $context = get_context_instance_by_id($assocrec->contextid); 00251 if ($context->contextlevel == CONTEXT_COURSE) { 00252 $assocurl = new moodle_url('/course/view.php', array('id' => $context->instanceid)); 00253 $text = $DB->get_field('course', 'shortname', array('id' => $context->instanceid)); //TODO: performance!!!! 00254 $assocstr .= $OUTPUT->action_icon($assocurl, new pix_icon('i/course', $text), null, array(), true); 00255 $hascourseassocs = true; 00256 $assoctype = get_string('course'); 00257 } 00258 } 00259 00260 // Now show mod association 00261 foreach ($blogassociations as $assocrec) { 00262 $context = get_context_instance_by_id($assocrec->contextid); 00263 00264 if ($context->contextlevel == CONTEXT_MODULE) { 00265 if ($hascourseassocs) { 00266 $assocstr .= ', '; 00267 $hascourseassocs = false; 00268 } 00269 00270 $modinfo = $DB->get_record('course_modules', array('id' => $context->instanceid)); 00271 $modname = $DB->get_field('modules', 'name', array('id' => $modinfo->module)); 00272 00273 $assocurl = new moodle_url('/mod/'.$modname.'/view.php', array('id' => $modinfo->id)); 00274 $text = $DB->get_field($modname, 'name', array('id' => $modinfo->instance)); //TODO: performance!!!! 00275 $assocstr .= $OUTPUT->action_icon($assocurl, new pix_icon('icon', $text, $modname), null, array(), true); 00276 $assocstr .= ', '; 00277 $assoctype = get_string('modulename', $modname); 00278 00279 } 00280 } 00281 $assocstr = substr($assocstr, 0, -2); 00282 $contentcell->text .= get_string('associated', 'blog', $assoctype) . ': '. $assocstr; 00283 00284 $contentcell->text .= $OUTPUT->container_end(); 00285 } 00286 00287 if ($unassociatedentry) { 00288 $contentcell->text .= $OUTPUT->container(get_string('associationunviewable', 'blog'), 'noticebox'); 00289 } 00290 00292 00293 $contentcell->text .= $OUTPUT->container_start('commands'); 00294 00295 if (blog_user_can_edit_entry($this) && empty($this->uniquehash)) { 00296 $contentcell->text .= html_writer::link(new moodle_url('/blog/edit.php', array('action' => 'edit', 'entryid' => $this->id)), $stredit) . ' | '; 00297 $contentcell->text .= html_writer::link(new moodle_url('/blog/edit.php', array('action' => 'delete', 'entryid' => $this->id)), $strdelete) . ' | '; 00298 } 00299 00300 $contentcell->text .= html_writer::link(new moodle_url('/blog/index.php', array('entryid' => $this->id)), get_string('permalink', 'blog')); 00301 00302 $contentcell->text .= $OUTPUT->container_end(); 00303 00304 if (isset($template['lastmod']) ){ 00305 $contentcell->text .= '<div style="font-size: 55%;">'; 00306 $contentcell->text .= ' [ '.get_string('modified').': '.$template['lastmod'].' ]'; 00307 $contentcell->text .= '</div>'; 00308 } 00309 00310 //add comments under everything 00311 $contentcell->text .= $cmttext; 00312 00313 $mainrow->cells[] = $contentcell; 00314 $table->data = array($mainrow); 00315 00316 if ($return) { 00317 return html_writer::table($table); 00318 } else { 00319 echo html_writer::table($table); 00320 } 00321 } 00322 00329 public function process_attachment($form) { 00330 $this->form = $form; 00331 } 00332 00339 public function add() { 00340 global $CFG, $USER, $DB; 00341 00342 unset($this->id); 00343 $this->module = 'blog'; 00344 $this->userid = (empty($this->userid)) ? $USER->id : $this->userid; 00345 $this->lastmodified = time(); 00346 $this->created = time(); 00347 00348 // Insert the new blog entry. 00349 $this->id = $DB->insert_record('post', $this); 00350 00351 // Update tags. 00352 $this->add_tags_info(); 00353 00354 if (!empty($CFG->useblogassociations)) { 00355 $this->add_associations(); 00356 add_to_log(SITEID, 'blog', 'add', 'index.php?userid='.$this->userid.'&entryid='.$this->id, $this->subject); 00357 } 00358 00359 tag_set('post', $this->id, $this->tags); 00360 } 00361 00368 public function edit($params=array(), $form=null, $summaryoptions=array(), $attachmentoptions=array()) { 00369 global $CFG, $USER, $DB, $PAGE; 00370 00371 $sitecontext = get_context_instance(CONTEXT_SYSTEM); 00372 $entry = $this; 00373 00374 $this->form = $form; 00375 foreach ($params as $var => $val) { 00376 $entry->$var = $val; 00377 } 00378 00379 $entry = file_postupdate_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id); 00380 $entry = file_postupdate_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext, 'blog', 'attachment', $entry->id); 00381 00382 if (!empty($CFG->useblogassociations)) { 00383 $entry->add_associations(); 00384 } 00385 00386 $entry->lastmodified = time(); 00387 00388 // Update record 00389 $DB->update_record('post', $entry); 00390 tag_set('post', $entry->id, $entry->tags); 00391 00392 add_to_log(SITEID, 'blog', 'update', 'index.php?userid='.$USER->id.'&entryid='.$entry->id, $entry->subject); 00393 } 00394 00400 public function delete() { 00401 global $DB, $USER; 00402 00403 $returnurl = ''; 00404 00405 $this->delete_attachments(); 00406 00407 $DB->delete_records('post', array('id' => $this->id)); 00408 tag_set('post', $this->id, array()); 00409 00410 add_to_log(SITEID, 'blog', 'delete', 'index.php?userid='. $this->userid, 'deleted blog entry with entry id# '. $this->id); 00411 } 00412 00417 public function add_associations($action='add') { 00418 global $DB, $USER; 00419 00420 $this->remove_associations(); 00421 00422 if (!empty($this->courseassoc)) { 00423 $this->add_association($this->courseassoc, $action); 00424 } 00425 00426 if (!empty($this->modassoc)) { 00427 $this->add_association($this->modassoc, $action); 00428 } 00429 } 00430 00435 public function add_association($contextid, $action='add') { 00436 global $DB, $USER; 00437 00438 $assocobject = new StdClass; 00439 $assocobject->contextid = $contextid; 00440 $assocobject->blogid = $this->id; 00441 $DB->insert_record('blog_association', $assocobject); 00442 00443 $context = get_context_instance_by_id($contextid); 00444 $courseid = null; 00445 00446 if ($context->contextlevel == CONTEXT_COURSE) { 00447 $courseid = $context->instanceid; 00448 add_to_log($courseid, 'blog', $action, 'index.php?userid='.$this->userid.'&entryid='.$this->id, $this->subject); 00449 } else if ($context->contextlevel == CONTEXT_MODULE) { 00450 $cm = $DB->get_record('course_modules', array('id' => $context->instanceid)); 00451 $modulename = $DB->get_field('modules', 'name', array('id' => $cm->module)); 00452 add_to_log($cm->course, 'blog', $action, 'index.php?userid='.$this->userid.'&entryid='.$this->id, $this->subject, $cm->id, $this->userid); 00453 } 00454 } 00455 00460 public function remove_associations() { 00461 global $DB; 00462 $DB->delete_records('blog_association', array('blogid' => $this->id)); 00463 } 00464 00470 public function delete_attachments() { 00471 $fs = get_file_storage(); 00472 $fs->delete_area_files(SYSCONTEXTID, 'blog', 'attachment', $this->id); 00473 $fs->delete_area_files(SYSCONTEXTID, 'blog', 'post', $this->id); 00474 } 00475 00484 public function print_attachments($return=false) { 00485 global $CFG, $OUTPUT; 00486 00487 require_once($CFG->libdir.'/filelib.php'); 00488 00489 $fs = get_file_storage(); 00490 00491 $syscontext = get_context_instance(CONTEXT_SYSTEM); 00492 00493 $files = $fs->get_area_files($syscontext->id, 'blog', 'attachment', $this->id); 00494 00495 $imagereturn = ""; 00496 $output = ""; 00497 00498 $strattachment = get_string("attachment", "forum"); 00499 00500 foreach ($files as $file) { 00501 if ($file->is_directory()) { 00502 continue; 00503 } 00504 00505 $filename = $file->get_filename(); 00506 $ffurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.SYSCONTEXTID.'/blog/attachment/'.$this->id.'/'.$filename); 00507 $mimetype = $file->get_mimetype(); 00508 00509 $icon = mimeinfo_from_type("icon", $mimetype); 00510 $type = mimeinfo_from_type("type", $mimetype); 00511 00512 $image = $OUTPUT->pix_icon("f/$icon", $filename, 'moodle', array('class'=>'icon')); 00513 00514 if ($return == "html") { 00515 $output .= html_writer::link($ffurl, $image); 00516 $output .= html_writer::link($ffurl, $filename); 00517 00518 } else if ($return == "text") { 00519 $output .= "$strattachment $filename:\n$ffurl\n"; 00520 00521 } else { 00522 if (in_array($type, array('image/gif', 'image/jpeg', 'image/png'))) { // Image attachments don't get printed as links 00523 $imagereturn .= '<br /><img src="'.$ffurl.'" alt="" />'; 00524 } else { 00525 $imagereturn .= html_writer::link($ffurl, $image); 00526 $imagereturn .= format_text(html_writer::link($ffurl, $filename), FORMAT_HTML, array('context'=>$syscontext)); 00527 } 00528 } 00529 } 00530 00531 if ($return) { 00532 return $output; 00533 } 00534 00535 return $imagereturn; 00536 00537 } 00538 00543 public function add_tags_info() { 00544 00545 $tags = array(); 00546 00547 if ($otags = optional_param('otags', '', PARAM_INT)) { 00548 foreach ($otags as $tagid) { 00549 // TODO : make this use the tag name in the form 00550 if ($tag = tag_get('id', $tagid)) { 00551 $tags[] = $tag->name; 00552 } 00553 } 00554 } 00555 00556 tag_set('post', $this->id, $tags); 00557 } 00558 00568 public function can_user_edit($userid=null) { 00569 global $CFG, $USER; 00570 00571 if (empty($userid)) { 00572 $userid = $USER->id; 00573 } 00574 00575 $sitecontext = get_context_instance(CONTEXT_SYSTEM); 00576 00577 if (has_capability('moodle/blog:manageentries', $sitecontext)) { 00578 return true; // can edit any blog entry 00579 } 00580 00581 if ($this->userid == $userid && has_capability('moodle/blog:create', $sitecontext)) { 00582 return true; // can edit own when having blog:create capability 00583 } 00584 00585 return false; 00586 } 00587 00597 public function can_user_view($targetuserid) { 00598 global $CFG, $USER, $DB; 00599 $sitecontext = get_context_instance(CONTEXT_SYSTEM); 00600 00601 if (empty($CFG->bloglevel) || !has_capability('moodle/blog:view', $sitecontext)) { 00602 return false; // blog system disabled or user has no blog view capability 00603 } 00604 00605 if (isloggedin() && $USER->id == $targetuserid) { 00606 return true; // can view own entries in any case 00607 } 00608 00609 if (has_capability('moodle/blog:manageentries', $sitecontext)) { 00610 return true; // can manage all entries 00611 } 00612 00613 // coming for 1 entry, make sure it's not a draft 00614 if ($this->publishstate == 'draft' && !has_capability('moodle/blog:viewdrafts', $sitecontext)) { 00615 return false; // can not view draft of others 00616 } 00617 00618 // coming for 1 entry, make sure user is logged in, if not a public blog 00619 if ($this->publishstate != 'public' && !isloggedin()) { 00620 return false; 00621 } 00622 00623 switch ($CFG->bloglevel) { 00624 case BLOG_GLOBAL_LEVEL: 00625 return true; 00626 break; 00627 00628 case BLOG_SITE_LEVEL: 00629 if (isloggedin()) { // not logged in viewers forbidden 00630 return true; 00631 } 00632 return false; 00633 break; 00634 00635 case BLOG_USER_LEVEL: 00636 default: 00637 $personalcontext = get_context_instance(CONTEXT_USER, $targetuserid); 00638 return has_capability('moodle/user:readuserblogs', $personalcontext); 00639 break; 00640 } 00641 } 00642 00651 public static function get_applicable_publish_states() { 00652 global $CFG; 00653 $options = array(); 00654 00655 // everyone gets draft access 00656 if ($CFG->bloglevel >= BLOG_USER_LEVEL) { 00657 $options['draft'] = get_string('publishtonoone', 'blog'); 00658 } 00659 00660 if ($CFG->bloglevel > BLOG_USER_LEVEL) { 00661 $options['site'] = get_string('publishtosite', 'blog'); 00662 } 00663 00664 if ($CFG->bloglevel >= BLOG_GLOBAL_LEVEL) { 00665 $options['public'] = get_string('publishtoworld', 'blog'); 00666 } 00667 00668 return $options; 00669 } 00670 } 00671 00680 class blog_listing { 00685 public $entries = array(); 00686 00691 public $filters = array(); 00692 00698 public function __construct($filters=array()) { 00699 // Unset filters overridden by more specific filters 00700 foreach ($filters as $type => $id) { 00701 if (!empty($type) && !empty($id)) { 00702 $this->filters[$type] = blog_filter::get_instance($id, $type); 00703 } 00704 } 00705 00706 foreach ($this->filters as $type => $filter) { 00707 foreach ($filter->overrides as $override) { 00708 if (array_key_exists($override, $this->filters)) { 00709 unset($this->filters[$override]); 00710 } 00711 } 00712 } 00713 } 00714 00720 public function get_entries($start=0, $limit=10) { 00721 global $DB; 00722 00723 if (empty($this->entries)) { 00724 if ($sqlarray = $this->get_entry_fetch_sql(false, 'created DESC')) { 00725 $this->entries = $DB->get_records_sql($sqlarray['sql'], $sqlarray['params'], $start, $limit); 00726 } else { 00727 return false; 00728 } 00729 } 00730 00731 return $this->entries; 00732 } 00733 00734 public function get_entry_fetch_sql($count=false, $sort='lastmodified DESC', $userid = false) { 00735 global $DB, $USER, $CFG; 00736 00737 if(!$userid) { 00738 $userid = $USER->id; 00739 } 00740 00741 // The query used to locate blog entries is complicated. It will be built from the following components: 00742 $requiredfields = "p.*, u.firstname, u.lastname, u.email"; // the SELECT clause 00743 $tables = array('p' => 'post', 'u' => 'user'); // components of the FROM clause (table_id => table_name) 00744 $conditions = array('u.deleted = 0', 'p.userid = u.id', '(p.module = \'blog\' OR p.module = \'blog_external\')'); // components of the WHERE clause (conjunction) 00745 00746 // build up a clause for permission constraints 00747 00748 $params = array(); 00749 00750 // fix for MDL-9165, use with readuserblogs capability in a user context can read that user's private blogs 00751 // admins can see all blogs regardless of publish states, as described on the help page 00752 if (has_capability('moodle/user:readuserblogs', get_context_instance(CONTEXT_SYSTEM))) { 00753 // don't add permission constraints 00754 00755 } else if(!empty($this->filters['user']) && has_capability('moodle/user:readuserblogs', 00756 get_context_instance(CONTEXT_USER, (empty($this->filters['user']->id) ? 0 : $this->filters['user']->id)))) { 00757 // don't add permission constraints 00758 00759 } else { 00760 if (isloggedin() and !isguestuser()) { 00761 $assocexists = $DB->record_exists('blog_association', array()); //dont check association records if there aren't any 00762 00763 //begin permission sql clause 00764 $permissionsql = '(p.userid = ? '; 00765 $params[] = $userid; 00766 00767 if ($CFG->bloglevel >= BLOG_SITE_LEVEL) { // add permission to view site-level entries 00768 $permissionsql .= " OR p.publishstate = 'site' "; 00769 } 00770 00771 if ($CFG->bloglevel >= BLOG_GLOBAL_LEVEL) { // add permission to view global entries 00772 $permissionsql .= " OR p.publishstate = 'public' "; 00773 } 00774 00775 $permissionsql .= ') '; //close permissions sql clause 00776 } else { // default is access to public entries 00777 $permissionsql = "p.publishstate = 'public'"; 00778 } 00779 $conditions[] = $permissionsql; //add permission constraints 00780 } 00781 00782 foreach ($this->filters as $type => $blogfilter) { 00783 $conditions = array_merge($conditions, $blogfilter->conditions); 00784 $params = array_merge($params, $blogfilter->params); 00785 $tables = array_merge($tables, $blogfilter->tables); 00786 } 00787 00788 $tablessql = ''; // build up the FROM clause 00789 foreach ($tables as $tablename => $table) { 00790 $tablessql .= ($tablessql ? ', ' : '').'{'.$table.'} '.$tablename; 00791 } 00792 00793 $sql = ($count) ? 'SELECT COUNT(*)' : 'SELECT ' . $requiredfields; 00794 $sql .= " FROM $tablessql WHERE " . implode(' AND ', $conditions); 00795 $sql .= ($count) ? '' : " ORDER BY $sort"; 00796 00797 return array('sql' => $sql, 'params' => $params); 00798 } 00799 00805 public function print_entries() { 00806 global $CFG, $USER, $DB, $OUTPUT; 00807 $sitecontext = get_context_instance(CONTEXT_SYSTEM); 00808 00809 $page = optional_param('blogpage', 0, PARAM_INT); 00810 $limit = optional_param('limit', get_user_preferences('blogpagesize', 10), PARAM_INT); 00811 $start = $page * $limit; 00812 00813 $morelink = '<br /> '; 00814 00815 if ($sqlarray = $this->get_entry_fetch_sql(true)) { 00816 $totalentries = $DB->count_records_sql($sqlarray['sql'], $sqlarray['params']); 00817 } else { 00818 $totalentries = 0; 00819 } 00820 00821 $entries = $this->get_entries($start, $limit); 00822 $pagingbar = new paging_bar($totalentries, $page, $limit, $this->get_baseurl()); 00823 $pagingbar->pagevar = 'blogpage'; 00824 $blogheaders = blog_get_headers(); 00825 00826 echo $OUTPUT->render($pagingbar); 00827 00828 if (has_capability('moodle/blog:create', $sitecontext)) { 00829 //the user's blog is enabled and they are viewing their own blog 00830 $userid = optional_param('userid', null, PARAM_INT); 00831 00832 if (empty($userid) || (!empty($userid) && $userid == $USER->id)) { 00833 $addurl = new moodle_url("$CFG->wwwroot/blog/edit.php"); 00834 $urlparams = array('action' => 'add', 00835 'userid' => $userid, 00836 'courseid' => optional_param('courseid', null, PARAM_INT), 00837 'groupid' => optional_param('groupid', null, PARAM_INT), 00838 'modid' => optional_param('modid', null, PARAM_INT), 00839 'tagid' => optional_param('tagid', null, PARAM_INT), 00840 'tag' => optional_param('tag', null, PARAM_INT), 00841 'search' => optional_param('search', null, PARAM_INT)); 00842 00843 foreach ($urlparams as $var => $val) { 00844 if (empty($val)) { 00845 unset($urlparams[$var]); 00846 } 00847 } 00848 $addurl->params($urlparams); 00849 00850 $addlink = '<div class="addbloglink">'; 00851 $addlink .= '<a href="'.$addurl->out().'">'. $blogheaders['stradd'].'</a>'; 00852 $addlink .= '</div>'; 00853 echo $addlink; 00854 } 00855 } 00856 00857 if ($entries) { 00858 $count = 0; 00859 00860 foreach ($entries as $entry) { 00861 $blogentry = new blog_entry(null, $entry); 00862 $blogentry->print_html(); 00863 $count++; 00864 } 00865 00866 echo $OUTPUT->render($pagingbar); 00867 00868 if (!$count) { 00869 print '<br /><div style="text-align:center">'. get_string('noentriesyet', 'blog') .'</div><br />'; 00870 } 00871 00872 print $morelink.'<br />'."\n"; 00873 return; 00874 } 00875 } 00876 00878 public function get_baseurl() { 00879 $getcopy = $_GET; 00880 00881 unset($getcopy['blogpage']); 00882 00883 if (!empty($getcopy)) { 00884 $first = false; 00885 $querystring = ''; 00886 00887 foreach ($getcopy as $var => $val) { 00888 if (!$first) { 00889 $first = true; 00890 $querystring .= "?$var=$val"; 00891 } else { 00892 $querystring .= '&'.$var.'='.$val; 00893 $hasparam = true; 00894 } 00895 } 00896 } else { 00897 $querystring = '?'; 00898 } 00899 00900 return strip_querystring(qualified_me()) . $querystring; 00901 00902 } 00903 } 00904 00914 abstract class blog_filter { 00919 public $availabletypes = array(); 00920 00925 public $type; 00926 00931 public $id; 00932 00937 public $tables = array(); 00938 00943 public $conditions = array(); 00944 00949 public $params = array(); 00950 00954 public $overrides = array(); 00955 00956 public function __construct($id, $type=null) { 00957 $this->id = $id; 00958 $this->type = $type; 00959 } 00960 00965 public static function get_instance($id, $type) { 00966 00967 switch ($type) { 00968 case 'site': 00969 case 'course': 00970 case 'module': 00971 return new blog_filter_context($id, $type); 00972 break; 00973 00974 case 'group': 00975 case 'user': 00976 return new blog_filter_user($id, $type); 00977 break; 00978 00979 case 'tag': 00980 return new blog_filter_tag($id); 00981 break; 00982 00983 default: 00984 $classname = "blog_filter_$type"; 00985 if (class_exists($classname)) { 00986 return new $classname($id, $type); 00987 } 00988 } 00989 } 00990 } 00991 00995 class blog_filter_context extends blog_filter { 01002 public function __construct($id=null, $type='site') { 01003 global $SITE, $CFG, $DB; 01004 01005 if (empty($id)) { 01006 $this->type = 'site'; 01007 } else { 01008 $this->id = $id; 01009 $this->type = $type; 01010 } 01011 01012 $this->availabletypes = array('site' => get_string('site'), 'course' => get_string('course'), 'module' => get_string('activity')); 01013 01014 switch ($this->type) { 01015 case 'course': // Careful of site course! 01016 // Ignore course filter if blog associations are not enabled 01017 if ($this->id != $SITE->id && !empty($CFG->useblogassociations)) { 01018 $this->overrides = array('site'); 01019 $context = get_context_instance(CONTEXT_COURSE, $this->id); 01020 $this->tables['ba'] = 'blog_association'; 01021 $this->conditions[] = 'p.id = ba.blogid'; 01022 $this->conditions[] = 'ba.contextid = '.$context->id; 01023 break; 01024 } else { 01025 // We are dealing with the site course, do not break from the current case 01026 } 01027 01028 case 'site': 01029 // No special constraints 01030 break; 01031 case 'module': 01032 if (!empty($CFG->useblogassociations)) { 01033 $this->overrides = array('course', 'site'); 01034 01035 $context = get_context_instance(CONTEXT_MODULE, $this->id); 01036 $this->tables['ba'] = 'blog_association'; 01037 $this->tables['p'] = 'post'; 01038 $this->conditions = array('p.id = ba.blogid', 'ba.contextid = ?'); 01039 $this->params = array($context->id); 01040 } 01041 break; 01042 } 01043 } 01044 } 01045 01050 class blog_filter_user extends blog_filter { 01051 public $tables = array('u' => 'user'); 01052 01059 public function __construct($id=null, $type='user') { 01060 global $CFG, $DB, $USER; 01061 $this->availabletypes = array('user' => get_string('user'), 'group' => get_string('group')); 01062 01063 if (empty($id)) { 01064 $this->id = $USER->id; 01065 $this->type = 'user'; 01066 } else { 01067 $this->id = $id; 01068 $this->type = $type; 01069 } 01070 01071 if ($this->type == 'user') { 01072 $this->conditions = array('u.id = ?'); 01073 $this->params = array($this->id); 01074 $this->overrides = array('group'); 01075 01076 } elseif ($this->type == 'group') { 01077 $this->overrides = array('course', 'site'); 01078 01079 $this->tables['gm'] = 'groups_members'; 01080 $this->conditions[] = 'p.userid = gm.userid'; 01081 $this->conditions[] = 'gm.groupid = ?'; 01082 $this->params[] = $this->id; 01083 01084 if (!empty($CFG->useblogassociations)) { // only show blog entries associated with this course 01085 $coursecontext = get_context_instance(CONTEXT_COURSE, $DB->get_field('groups', 'courseid', array('id' => $this->id))); 01086 $this->tables['ba'] = 'blog_association'; 01087 $this->conditions[] = 'gm.groupid = ?'; 01088 $this->conditions[] = 'ba.contextid = ?'; 01089 $this->conditions[] = 'ba.blogid = p.id'; 01090 $this->params[] = $this->id; 01091 $this->params[] = $coursecontext->id; 01092 } 01093 } 01094 01095 } 01096 } 01097 01101 class blog_filter_tag extends blog_filter { 01102 public $tables = array('t' => 'tag', 'ti' => 'tag_instance', 'p' => 'post'); 01103 01109 public function __construct($id) { 01110 global $DB; 01111 $this->id = $id; 01112 01113 $this->conditions = array('ti.tagid = t.id', 01114 "ti.itemtype = 'post'", 01115 'ti.itemid = p.id', 01116 't.id = ?'); 01117 $this->params = array($this->id); 01118 } 01119 } 01120 01124 class blog_filter_entry extends blog_filter { 01125 public $conditions = array('p.id = ?'); 01126 public $overrides = array('site', 'course', 'module', 'group', 'user', 'tag'); 01127 01128 public function __construct($id) { 01129 $this->id = $id; 01130 $this->params[] = $this->id; 01131 } 01132 } 01133 01137 class blog_filter_since extends blog_filter { 01138 public function __construct($interval) { 01139 $this->conditions[] = 'p.lastmodified >= ? AND p.lastmodified <= ?'; 01140 $this->params[] = mktime() - $interval; 01141 $this->params[] = mktime(); 01142 } 01143 } 01144 01148 class blog_filter_search extends blog_filter { 01149 01150 public function __construct($searchterm) { 01151 global $DB; 01152 $this->conditions = array("(".$DB->sql_like('p.summary', '?', false)." OR 01153 ".$DB->sql_like('p.content', '?', false)." OR 01154 ".$DB->sql_like('p.subject', '?', false).")"); 01155 $this->params[] = "%$searchterm%"; 01156 $this->params[] = "%$searchterm%"; 01157 $this->params[] = "%$searchterm%"; 01158 } 01159 }