MDL-14679 towards /blog conversion

This commit is contained in:
skodak
2008-06-01 13:48:12 +00:00
parent 9f43d70d24
commit 45df7de3ad
8 changed files with 112 additions and 105 deletions
+3 -2
View File
@@ -73,9 +73,10 @@ class block_blog_tags extends block_base {
$timewithin = time() - $this->config->timewithin * 24 * 60 * 60; /// convert to seconds
// admins should be able to read all tags
// admins should be able to read all tags
$type = '';
if (!has_capability('moodle/user:readuserblogs', get_context_instance(CONTEXT_SYSTEM))) {
$type .= " AND (p.publishstate = 'site' or p.publishstate='public')";
$type = " AND (p.publishstate = 'site' or p.publishstate='public')";
}
$sql = "SELECT t.id, t.tagtype, t.rawname, t.name, COUNT(DISTINCT ti.id) AS ct
+9 -5
View File
@@ -41,11 +41,15 @@ class page_blog extends page_base {
}
}
// Here you should load up all heavy-duty data for your page. Basically everything that
// does not NEED to be loaded for the class to make basic decisions should NOT be loaded
// in init_quick() and instead deferred here. Of course this function had better recognize
// $this->full_init_done to prevent wasteful multiple-time data retrieval.
/**
* Here you should load up all heavy-duty data for your page. Basically everything that
* does not NEED to be loaded for the class to make basic decisions should NOT be loaded
* in init_quick() and instead deferred here. Of course this function had better recognize
* $this->full_init_done to prevent wasteful multiple-time data retrieval.
*/
function init_full() {
global $DB;
if ($this->full_init_done) {
return;
}
@@ -56,7 +60,7 @@ class page_blog extends page_base {
$this->courseid = '';
$courserecord = NULL;
} else {
if (! ($courserecord = get_record('course', 'id', $this->courseid)) ) {
if (! ($courserecord = $DB->get_record('course', array('id'=>$this->courseid))) ) {
print_error('invalidcourseid', 'error', '', $this->courseid);
}
}
+12 -14
View File
@@ -26,7 +26,7 @@ if (!has_capability('moodle/blog:create', $sitecontext) and !has_capability('moo
// Make sure that the person trying to edit have access right
if ($id) {
if (!$existing = get_record('post', 'id', $id)) {
if (!$existing = $DB->get_record('post', array('id'=>$id))) {
print_error('wrongpostid', 'blog');
}
@@ -50,7 +50,7 @@ if (!empty($courseid)) {
$strblogs = get_string('blogs','blog');
if ($action=='delete'){
if ($action === 'delete'){
if (!$existing) {
print_error('wrongpostid', 'blog');
}
@@ -74,7 +74,7 @@ $blogeditform = new blog_edit_form(null, compact('existing', 'sitecontext'));
if ($blogeditform->is_cancelled()){
redirect($returnurl);
} else if ($fromform = $blogeditform->get_data()){
} else if ($fromform = $blogeditform->get_data(false)){
//save stuff in db
switch ($action) {
case 'add':
@@ -128,7 +128,7 @@ switch ($action) {
}
// done here in order to allow deleting of posts with wrong user id above
if (!$user = get_record('user', 'id', $userid)) {
if (!$user = $DB->get_record('user', array('id'=>$userid))) {
print_error('invaliduserid');
}
$navlinks = array();
@@ -149,13 +149,13 @@ die;
/***************************** edit.php functions ***************************/
/*
/**
* Delete blog post from database
*/
function do_delete($post) {
global $returnurl;
global $returnurl, $DB;
$status = delete_records('post', 'id', $post->id);
$status = $DB->delete_records('post', array('id'=>$post->id));
//$status = delete_records('blog_tag_instance', 'entryid', $post->id) and $status;
tag_set('post', $post->id, array());
@@ -172,7 +172,7 @@ function do_delete($post) {
* Write a new blog entry into database
*/
function do_add($post, $blogeditform) {
global $CFG, $USER, $returnurl;
global $CFG, $USER, $returnurl, $DB;
$post->module = 'blog';
$post->userid = $USER->id;
@@ -180,12 +180,12 @@ function do_add($post, $blogeditform) {
$post->created = time();
// Insert the new blog entry.
if ($id = insert_record('post', $post)) {
if ($id = $DB->insert_record('post', $post)) {
$post->id = $id;
// add blog attachment
$dir = blog_file_area_name($post);
if ($blogeditform->save_files($dir) and $newfilename = $blogeditform->get_new_filename()) {
set_field("post", "attachment", $newfilename, "id", $post->id);
$DB->set_field("post", "attachment", $newfilename, array("id"=>$post->id));
}
add_tags_info($post->id);
add_to_log(SITEID, 'blog', 'add', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject);
@@ -202,9 +202,7 @@ function do_add($post, $blogeditform) {
* @todo complete documenting this function. enable trackback and pingback between entries on the same server
*/
function do_edit($post, $blogeditform) {
global $CFG, $USER, $returnurl;
global $CFG, $USER, $returnurl, $DB;
$post->lastmodified = time();
@@ -214,7 +212,7 @@ function do_edit($post, $blogeditform) {
}
// update record
if (update_record('post', $post)) {
if ($DB->update_record('post', $post)) {
// delete all tags associated with this entry
//delete_records('blog_tag_instance', 'entryid', $post->id);
+4 -3
View File
@@ -5,8 +5,8 @@ require_once($CFG->libdir.'/formslib.php');
class blog_edit_form extends moodleform {
function definition() {
global $CFG, $COURSE, $USER;
$mform =& $this->_form;
$post = $this->_customdata['existing'];
@@ -75,13 +75,14 @@ class blog_edit_form extends moodleform {
*
*/
function otags_select_setup(){
global $CFG;
global $CFG, $DB;
$mform =& $this->_form;
if ($otagsselect =& $mform->getElement('otags')) {
$otagsselect->removeOptions();
}
$namefield = empty($CFG->keeptagnamecase) ? 'name' : 'rawname';
if ($otags = get_records_sql_menu('SELECT id, '.$namefield.' from '.$CFG->prefix.'tag WHERE tagtype=\'official\' ORDER by name ASC')){
if ($otags = $DB->get_records_sql_menu("SELECT id, $namefield FROM {tag} WHERE tagtype='official' ORDER by name ASC")){
$otagsselect->loadArray($otags);
}
}
+4 -4
View File
@@ -16,7 +16,7 @@ $blockid = optional_param('blockid', 0, PARAM_INT);
blog_check_and_install_blocks();
if (!$course = get_record('course', 'id', $courseid)) {
if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
print_error('invalidcourseid', '', '', $courseid);
}
@@ -83,7 +83,7 @@ if ($editing) {
}
if (!empty($tagid)) {
$taginstance = get_record('tag', 'id', $tagid);
$taginstance = $DB->get_record('tag', array('id'=>$tagid));
} elseif (!empty($tag)) {
$taginstance = tag_id($tag);
}
@@ -98,7 +98,7 @@ $blogstring = get_string('blogs','blog');
$tagstring = get_string('tag');
// needed also for user tabs later
if (!$course = get_record('course', 'id', $courseid)) {
if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
print_error('invalidcourseid', '', '', $courseid);
}
@@ -164,7 +164,7 @@ $navlinks = array();
case 'user':
$participants = get_string('participants');
if (!$user = get_record('user', 'id', $filterselect)) {
if (!$user = $DB->get_record('user', array('id'=>$filterselect))) {
print_error('invaliduserid');
}
+4 -4
View File
@@ -42,7 +42,7 @@ if (empty($filtertype)) {
} else if (has_capability('moodle/blog:view', $sitecontext) and $CFG->bloglevel > BLOG_USER_LEVEL) {
if ($postid) {
$filtertype = 'user';
if (!$postobject = get_record('post', 'module', 'blog', 'id', $postid)) {
if (!$postobject = $DB->get_record('post', array('module'=>'blog', 'id'=>$postid))) {
error('No such blog entry');
}
$filterselect = $postobject->userid;
@@ -77,7 +77,7 @@ switch ($filtertype) {
if ($CFG->bloglevel < BLOG_COURSE_LEVEL) {
print_error('courseblogdisable', 'blog');
}
if (!$course = get_record('course', 'id', $filterselect)) {
if (!$course = $DB->get_record('course', array('id'=>$filterselect))) {
print_error('invalidcourseid');
}
$courseid = $course->id;
@@ -97,7 +97,7 @@ switch ($filtertype) {
if (! $group = groups_get_group($filterselect)) { //TODO:check.
print_error('invalidgroupid');
}
if (!$course = get_record('course', 'id', $group->courseid)) {
if (!$course = $DB->get_record('course', array('id'=>$group->courseid))) {
print_error('invalidcourseid');
}
$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
@@ -119,7 +119,7 @@ switch ($filtertype) {
if ($CFG->bloglevel < BLOG_USER_LEVEL) {
print_error('blogdisable', 'blog');
}
if (!$user = get_record('user', 'id', $filterselect)) {
if (!$user = $DB->get_record('user', array('id'=>$filterselect))) {
print_error('invaliduserid');
}
if ($USER->id == $filterselect) {
+71 -68
View File
@@ -21,13 +21,14 @@
* default blocks (blog_menu and blog_tags).
*/
function blog_check_and_install_blocks() {
global $USER;
global $USER, $DB;
if (isloggedin() && !isguest()) {
// if this user has not visited this page before
if (!get_user_preferences('blogpagesize')) {
// find the correct ids for blog_menu and blog_from blocks
$menublock = get_record('block','name','blog_menu');
$tagsblock = get_record('block','name','blog_tags');
$menublock = $DB->get_record('block', array('name'=>'blog_menu'));
$tagsblock = $DB->get_record('block', array('name'=>'blog_tags'));
// add those 2 into block_instance page
// add blog_menu block
@@ -38,12 +39,12 @@
$newblock->position = 'r';
$newblock->weight = 0;
$newblock->visible = 1;
insert_record('block_instance', $newblock);
$DB->insert_record('block_instance', $newblock);
// add blog_tags menu
$newblock -> blockid = $tagsblock->id;
$newblock -> weight = 1;
insert_record('block_instance', $newblock);
$DB->insert_record('block_instance', $newblock);
// finally we set the page size pref
set_user_preference('blogpagesize', 10);
@@ -140,15 +141,14 @@
* display the entry in its abbreviated format (eg. index page)
*/
function blog_print_entry($blogEntry, $viewtype='full', $filtertype='', $filterselect='', $mode='loud') {
global $USER, $CFG, $COURSE, $ME;
global $USER, $CFG, $COURSE, $ME, $DB;
$template['body'] = format_text($blogEntry->summary, $blogEntry->format);
//$template['title'] = '<a name="'. $blogEntry->subject .'"></a>';
//enclose the title in nolink tags so that moodle formatting doesn't autolink the text
$template['title'] = '<span class="nolink">'.$blogEntry->subject.'</span>';
$template['userid'] = $blogEntry->userid;
$template['author'] = fullname(get_record('user','id',$blogEntry->userid));
$template['author'] = fullname($DB->get_record('user', array('id'=>$blogEntry->userid)));
$template['lastmod'] = userdate($blogEntry->lastmodified);
$template['created'] = userdate($blogEntry->created);
$template['publishstate'] = $blogEntry->publishstate;
@@ -164,7 +164,7 @@
$stredit = get_string('edit');
$strdelete = get_string('delete');
$user = get_record('user','id',$template['userid']);
$user = $DB->get_record('user', array('id'=>$template['userid']));
/// Start printing of the blog
@@ -247,10 +247,11 @@
}
/**
* Creates a directory file name, suitable for make_upload_directory()
* $CFG->dataroot/blog/attachments/xxxx/file.jpg
*/
function blog_file_area_name($blogentry) {
// Creates a directory file name, suitable for make_upload_directory()
global $CFG;
// $CFG->dataroot/blog/attachments/xxxx/file.jpg
return "blog/attachments/$blogentry->id";
}
@@ -258,10 +259,11 @@
return make_upload_directory( blog_file_area_name($blogentry) );
}
/**
* Deletes all the user files in the attachments area for a post
* EXCEPT for any file named $exception
*/
function blog_delete_old_attachments($post, $exception="") {
// Deletes all the user files in the attachments area for a post
// EXCEPT for any file named $exception
if ($basedir = blog_file_area($post)) {
if ($files = get_directory_list($basedir)) {
foreach ($files as $file) {
@@ -277,11 +279,12 @@
}
}
/**
* if return=html, then return a html string.
* if return=text, then return a text-only string.
* otherwise, print HTML for non-images, and return image HTML
*/
function blog_print_attachments($blogentry, $return=NULL) {
// if return=html, then return a html string.
// if return=text, then return a text-only string.
// otherwise, print HTML for non-images, and return image HTML
global $CFG;
$filearea = blog_file_area_name($blogentry);
@@ -338,7 +341,6 @@
* choose_from_menu function.
*/
function blog_applicable_publish_states($courseid='') {
global $CFG;
// everyone gets draft access
@@ -366,7 +368,6 @@
* This also applies to deleting of posts.
*/
function blog_user_can_edit_post($blogEntry) {
global $CFG, $USER;
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
@@ -390,7 +391,7 @@
* in blog/index.php
*/
function blog_user_can_view_user_post($targetuserid, $blogEntry=null) {
global $CFG, $USER;
global $CFG, $USER, $DB;
if (empty($CFG->bloglevel)) {
return false; // blog system disabled
@@ -442,7 +443,7 @@
$usercourses = array_keys(get_my_courses($targetuserid));
$shared = array_intersect($mycourses, $usercourses);
foreach ($shared as $courseid) {
$course = get_record('course', 'id', $courseid);
$course = $DB->get_record('course', array('id'=>$courseid));
$coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
if (has_capability('moodle/site:accessallgroups', $coursecontext)
or groups_get_course_groupmode($course) != SEPARATEGROUPS) {
@@ -474,17 +475,16 @@
* Main filter function.
*/
function blog_fetch_entries($postid='', $fetchlimit=10, $fetchstart='', $filtertype='', $filterselect='', $tagid='', $tag ='', $sort='lastmodified DESC', $limit=true) {
global $CFG, $USER;
global $CFG, $USER, $DB;
/// the post table will be used for other things too
$typesql = " AND p.module = 'blog' ";
$typesql = "AND p.module = 'blog'";
/// set the tag id for searching
if ($tagid) {
$tag = $tagid;
} else if ($tag) {
if ($tagrec = get_record_sql('SELECT * FROM '.$CFG->prefix.'tag WHERE name LIKE "'.$tag.'"')) {
if ($tagrec = $DB->get_record_sql("SELECT * FROM {tag} WHERE name LIKE ?", array($tag))) {
$tag = $tagrec->id;
} else {
$tag = -1; //no records found
@@ -495,12 +495,11 @@
// Just return 1 entry
if ($postid) {
if ($post = get_record('post', 'id', $postid)) {
if ($post = $DB->get_record('post', array('id'=>$postid))) {
if (blog_user_can_view_user_post($post->userid, $post)) {
if ($user = get_record('user', 'id', $post->userid)) {
if ($user = $DB->get_record('user', array('id'=>$post->userid))) {
$post->email = $user->email;
$post->firstname = $user->firstname;
$post->lastname = $user->lastname;
@@ -516,18 +515,22 @@
}
}
$params = array();
if ($tag) {
$tagtablesql = $CFG->prefix.'tag_instance ti, ';
$tagquerysql = ' AND ti.itemid = p.id AND ti.tagid = '.$tag.' AND ti.itemtype = \'post\' ';
$tagtablesql = ", {tag_instance} ti";
$tagquerysql = "AND ti.itemid = p.id AND ti.tagid = :tag AND ti.itemtype = 'post'";
$params['tag'] = $tag;
} else {
$tagtablesql = '';
$tagquerysql = '';
}
if (isloggedin() && !has_capability('moodle/legacy:guest', get_context_instance(CONTEXT_SYSTEM), $USER->id, false)) {
$permissionsql = 'AND (p.publishstate = \'site\' OR p.publishstate = \'public\' OR p.userid = '.$USER->id.')';
$permissionsql = "AND (p.publishstate = 'site' OR p.publishstate = 'public' OR p.userid = :userid)";
$params['userid'] = $USER->id;
} else {
$permissionsql = 'AND p.publishstate = \'public\'';
$permissionsql = "AND p.publishstate = 'public'";
}
// fix for MDL-9165, use with readuserblogs capability in a user context can read that user's private blogs
@@ -542,7 +545,7 @@
* different possible sqls *
****************************************/
$requiredfields = 'p.*, u.firstname,u.lastname,u.email';
$requiredfields = "p.*, u.firstname,u.lastname,u.email";
if ($filtertype == 'course' && $filterselect == SITEID) { // Really a site
$filtertype = 'site';
@@ -552,11 +555,11 @@
case 'site':
$SQL = 'SELECT '.$requiredfields.' FROM '.$CFG->prefix.'post p, '.$tagtablesql
.$CFG->prefix.'user u
WHERE p.userid = u.id '.$tagquerysql.'
AND u.deleted = 0
'.$permissionsql.$typesql;
$SQL = "SELECT $requiredfields
FROM {post} p, {user} u $tagtablesql
WHERE p.userid = u.id $tagquerysql
AND u.deleted = 0
$permissionsql $typesql";
break;
@@ -568,37 +571,39 @@
if (has_capability('moodle/role:viewhiddenassigns', $context)) {
$hiddensql = '';
} else {
$hiddensql = ' AND ra.hidden = 0 ';
$hiddensql = 'AND ra.hidden = 0';
}
$SQL = 'SELECT '.$requiredfields.' FROM '.$CFG->prefix.'post p, '.$tagtablesql
.$CFG->prefix.'role_assignments ra, '.$CFG->prefix.'user u
WHERE p.userid = ra.userid '.$tagquerysql.'
AND ra.contextid '.get_related_contexts_string($context).'
AND u.id = p.userid
AND u.deleted = 0
'.$hiddensql.$permissionsql.$typesql;
$SQL = "SELECT $requiredfields
FROM {post} p, {user} u, {role_assignments} ra $tagtablesql
WHERE p.userid = ra.userid $tagquerysql
AND ra.contextid ".get_related_contexts_string($context)."
AND u.id = p.userid
AND u.deleted = 0
$hiddensql $permissionsql $typesql";
break;
case 'group':
$SQL = 'SELECT '.$requiredfields.' FROM '.$CFG->prefix.'post p, '.$tagtablesql
.$CFG->prefix.'groups_members gm, '.$CFG->prefix.'user u
WHERE p.userid = gm.userid AND u.id = p.userid '.$tagquerysql.'
AND gm.groupid = '.$filterselect.'
AND u.deleted = 0
'.$permissionsql.$typesql;
$SQL = "SELECT $requiredfields
FROM {post} p, {user} u, {groups_members} gm $tagtablesql
WHERE p.userid = gm.userid AND u.id = p.userid $tagquerysql
AND gm.groupid = :groupid
AND u.deleted = 0
$permissionsql $typesql";
$params['groupid'] = $filterselect;
break;
case 'user':
$SQL = 'SELECT '.$requiredfields.' FROM '.$CFG->prefix.'post p, '.$tagtablesql
.$CFG->prefix.'user u
WHERE p.userid = u.id '.$tagquerysql.'
AND u.id = '.$filterselect.'
AND u.deleted = 0
'.$permissionsql.$typesql;
$SQL = "SELECT $requiredfields
FROM {post} p, {user} u $tagtablesql
WHERE p.userid = u.id $tagquerysql
AND u.id = :uid
AND u.deleted = 0
$permissionsql $typesql";
$params['uid'] = $filterselect;
break;
}
@@ -610,9 +615,9 @@
$limitnum = $fetchlimit;
}
$orderby = ' ORDER BY '. $sort .' ';
$orderby = "ORDER BY $sort";
$records = get_records_sql($SQL . $orderby, $limitfrom, $limitnum);
$records = $DB->get_records_sql("$SQL $orderby", $params, $limitfrom, $limitnum);
if (empty($records)) {
return array();
@@ -686,12 +691,10 @@
* Used in backup of site courses.
*/
function blog_get_participants() {
global $CFG, $DB;
global $CFG;
return get_records_sql("SELECT userid as id
FROM {$CFG->prefix}post
WHERE module = 'blog'
AND courseid = 0");
return $DB->get_records_sql("SELECT userid AS id
FROM {post}
WHERE module = 'blog' AND courseid = 0");
}
?>
+5 -5
View File
@@ -50,7 +50,7 @@
// Generate any blog RSS feed via one function (called by ../rss/file.php)
function blog_generate_rss_feed($type, $id, $tagid=0) {
global $CFG, $SITE;
global $CFG, $SITE, $DB;
if (empty($CFG->enablerssfeeds)) {
debugging('Sorry, RSS feeds are disabled on this site');
@@ -74,7 +74,7 @@
$items = array();
foreach ($blogposts as $blogpost) {
$item = NULL;
$item->author = fullname(get_record('user','id',$blogpost->userid));
$item->author = fullname($DB->get_record('user', array('id'=>$blogpost->userid))); // TODO: this is slow
$item->title = $blogpost->subject;
$item->pubdate = $blogpost->lastmodified;
$item->link = $CFG->wwwroot.'/blog/index.php?postid='.$blogpost->id;
@@ -90,10 +90,10 @@
switch ($type) {
case 'user':
$info = fullname(get_record('user', 'id', $id, '','','','','firstname,lastname'));
$info = fullname($DB->get_record('user', array('id'=>$id), 'firstname,lastname'));
break;
case 'course':
$info = get_field('course', 'fullname', 'id', $id);
$info = $DB->get_field('course', 'fullname', array('id'=>$id));
break;
case 'site':
$info = $SITE->fullname;
@@ -108,7 +108,7 @@
}
if ($tagid) {
$info .= ': '.get_field('tags', 'text', 'id', $tagid);
$info .= ': '.$DB->get_field('tags', 'text', array('id'=>$tagid));
}
$header = rss_standard_header(get_string($type.'blog','blog', $info),