From 947eaf57816a51f3a9b931217034e25a7a97b7f2 Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Fri, 18 Sep 2009 06:57:52 +0000 Subject: [PATCH] MDL-19754 MDL-8776 MDL-19678 MDL-20314 Converting old post table to blog_entries, upgrading blog code accordingly, and tags in tag_instance table --- blog/edit.php | 2 +- blog/edit_form.php | 2 +- blog/index.php | 2 +- blog/lib.php | 14 +++--- blog/locallib.php | 108 +++++++++++++++++++-------------------------- blog/rsslib.php | 2 +- lib/db/install.xml | 99 ++++++++--------------------------------- lib/db/upgrade.php | 62 ++++++++++++++++++++++++++ version.php | 2 +- 9 files changed, 138 insertions(+), 155 deletions(-) diff --git a/blog/edit.php b/blog/edit.php index d519c72c64..e7ecd285f7 100755 --- a/blog/edit.php +++ b/blog/edit.php @@ -204,7 +204,7 @@ switch ($action) { $entry->fakesummary = $existing->summary; $entry->publishstate = $existing->publishstate; $entry->format = $existing->format; - $entry->tags = tag_get_tags_array('post', $entry->id); + $entry->tags = tag_get_tags_array('blog_entries', $entry->id); $entry->action = $action; if (!empty($existing->courseassoc)) { diff --git a/blog/edit_form.php b/blog/edit_form.php index e0c80d61e0..df0d801edc 100644 --- a/blog/edit_form.php +++ b/blog/edit_form.php @@ -43,7 +43,7 @@ class blog_edit_form extends moodleform { $mform->addElement('format', 'summaryformat', get_string('format')); - $mform->addElement('filemanager', 'attachment', get_string('attachment', 'forum')); + $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum')); //disable publishstate options that are not allowed $publishstates = array(); diff --git a/blog/index.php b/blog/index.php index 1f5365b478..e27296c18c 100755 --- a/blog/index.php +++ b/blog/index.php @@ -61,7 +61,7 @@ $tabsfile = null; if (!$userid && has_capability('moodle/blog:view', $sitecontext) && $CFG->bloglevel > BLOG_USER_LEVEL) { if ($entryid) { - if (!$entryobject = $DB->get_record('post', array('module'=>'blog', 'id'=>$entryid))) { + if (!$entryobject = $DB->get_record('blog_entries', array('id'=>$entryid))) { print_error('nosuchentry', 'blog'); } $userid = $entryobject->userid; diff --git a/blog/lib.php b/blog/lib.php index 302fcbf1ad..1d9be1962e 100755 --- a/blog/lib.php +++ b/blog/lib.php @@ -36,7 +36,9 @@ require_once($CFG->dirroot.'/tag/lib.php'); */ //not used at the moment, and may not need to be define('PAGE_BLOG_COURSE_VIEW', 'blog_course-view'); - +define('BLOG_PUBLISHSTATE_DRAFT', 0); +define('BLOG_PUBLISHSTATE_SITE', 1); +define('BLOG_PUBLISHSTATE_PUBLIC', 2); /** * Checks to see if user has visited blogpages before, if not, install 2 @@ -293,17 +295,17 @@ function blog_fetch_external_entries($external_blog) { 'publishstate' => 'site', 'format' => FORMAT_HTML); - if (!$DB->record_exists('post', $params)) { + if (!$DB->record_exists('blog_entries', $params)) { $params['subject'] = $entry->get_title(); $params['summary'] = $entry->get_description(); $params['created'] = $entry->get_date('U'); $params['lastmodified'] = $entry->get_date('U'); - $id = $DB->insert_record('post', $params); + $id = $DB->insert_record('blog_entries', $params); // Set tags if ($tags = tag_get_tags_array('blog_external', $external_blog->id)) { - tag_set('post', $id, $tags); + tag_set('blog_entries', $id, $tags); } } } @@ -435,9 +437,9 @@ function blog_get_headers() { // Case 1: only entryid is requested, ignore all other filters. courseid is used to give more contextual information // TODO Blog entries link has entryid instead of userid if (!empty($entryid)) { - $sql = 'SELECT u.* FROM {user} u, {post} p WHERE p.id = ? AND p.userid = u.id'; + $sql = 'SELECT u.* FROM {user} u, {blog_entries} p WHERE p.id = ? AND p.userid = u.id'; $user = $DB->get_record_sql($sql, array($entryid)); - $entry = $DB->get_record('post', array('id' => $entryid)); + $entry = $DB->get_record('blog_entries', array('id' => $entryid)); $blog_url->param('userid', $user->id); diff --git a/blog/locallib.php b/blog/locallib.php index 19cc15471d..f6eef0523d 100644 --- a/blog/locallib.php +++ b/blog/locallib.php @@ -30,7 +30,7 @@ * Blog_entry class. Represents an entry in a user's blog. Contains all methods for managing this entry. * This class does not contain any HTML-generating code. See blog_listing sub-classes for such code. * This class follows the Object Relational Mapping technique, its member variables being mapped to - * the fields of the posts table. + * the fields of the blog_entries table. * * @package moodlecore * @subpackage blog @@ -43,19 +43,13 @@ class blog_entry { public $userid; public $subject; public $summary; - public $rating = 0; - public $attachment; public $publishstate; // Locked Database fields (Don't touch these) - public $courseid = 0; - public $groupid = 0; - public $module = 'blog'; - public $moduleid = 0; - public $coursemoduleid = 0; public $content; public $format = 1; - public $uniquehash = ''; + public $summaryformat = 1; + public $permalink = ''; public $lastmodified; public $created; public $usermodified; @@ -74,7 +68,7 @@ class blog_entry { global $DB, $PAGE; if (!empty($idorparams) && !is_array($idorparams) && !is_object($idorparams)) { - $object = $DB->get_record('post', array('id' => $idorparams)); + $object = $DB->get_record('blog_entries', array('id' => $idorparams)); foreach ($object as $var => $val) { $this->$var = $val; } @@ -178,13 +172,13 @@ class blog_entry { $blogassociations = $DB->get_records('blog_association', array('blogid' => $this->id)); //determine text for publish state switch ($template['publishstate']) { - case 'draft': + case BLOG_PUBLISHSTATE_DRAFT: $blogtype = get_string('publishtonoone', 'blog'); break; - case 'site': + case BLOG_PUBLISHSTATE_SITE: $blogtype = get_string('publishtosite', 'blog'); break; - case 'public': + case BLOG_PUBLISHSTATE_PUBLIC: $blogtype = get_string('publishtoworld', 'blog'); break; default: @@ -198,16 +192,16 @@ class blog_entry { $contentcell->text .= $template['body']; $contentcell->text .= $attachedimages; - // Uniquehash is used as a link to an external blog - if (!empty($this->uniquehash) && blog_is_valid_url($this->uniquehash)) { + // permalink is used as a link to an external blog + if (!empty($this->permalink) && blog_is_valid_url($this->permalink)) { $contentcell->text .= $OUTPUT->container_start('externalblog'); - $contentcell->text .= $OUTPUT->link(html_link::make($this->uniquehash, get_string('linktooriginalentry', 'blog'))); + $contentcell->text .= $OUTPUT->link(html_link::make($this->permalink, get_string('linktooriginalentry', 'blog'))); $contentcell->text .= $OUTPUT->container_end(); } // Links to tags - if (!empty($CFG->usetags) && ($blogtags = tag_get_tags_csv('post', $this->id)) ) { + if (!empty($CFG->usetags) && ($blogtags = tag_get_tags_csv('blog_entries', $this->id)) ) { $contentcell->text .= $OUTPUT->container_start('tags'); if ($blogtags) { @@ -320,14 +314,9 @@ class blog_entry { $this->created = time(); // Insert the new blog entry. - $this->id = $DB->insert_record('post', $this); + $this->id = $DB->insert_record('blog_entries', $this); - // Add blog attachment - if (!empty($this->form) && $this->form->get_new_filename('attachment')) { - if ($this->form->save_stored_file('attachment', SYSCONTEXTID, 'blog', $this->id, '/', $this->form->get_new_filename('attachment'), $USER->id)) { - $DB->set_field("post", "attachment", 1, array("id"=>$this->id)); - } - } + // TODO File handling // Update tags. $this->add_tags_info(); @@ -338,7 +327,7 @@ class blog_entry { } - tag_set('post', $this->id, $this->tags); + tag_set('blog_entries', $this->id, $this->tags); } /** @@ -362,19 +351,11 @@ class blog_entry { } $this->lastmodified = time(); - - if (!empty($this->form) && $this->form->get_new_filename('attachment')) { - $this->delete_attachments(); - if ($this->form->save_stored_file('attachment', SYSCONTEXTID, 'blog', $this->id, '/', false, $USER->id)) { - $this->attachment = 1; - } else { - $this->attachment = 1; - } - } - + + // TODO Handle attachments // Update record - $DB->update_record('post', $this); - tag_set('post', $this->id, $this->tags); + $DB->update_record('blog_entries', $this); + tag_set('blog_entries', $this->id, $this->tags); add_to_log(SITEID, 'blog', 'update', 'index.php?userid='.$USER->id.'&entryid='.$this->id, $this->subject); } @@ -391,8 +372,8 @@ class blog_entry { $this->delete_attachments(); - $DB->delete_records('post', array('id' => $this->id)); - tag_set('post', $this->id, array()); + $DB->delete_records('blog_entries', array('id' => $this->id)); + tag_set('blog_entries', $this->id, array()); add_to_log(SITEID, 'blog', 'delete', 'index.php?userid='. $this->userid, 'deleted blog entry with entry id# '. $this->id); } @@ -457,6 +438,7 @@ class blog_entry { * 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 + * TODO Fix this so that it displays attachments correctly * * @param bool $return Whether to return or print the generated code * @return void @@ -531,7 +513,7 @@ class blog_entry { } } - tag_set('post', $this->id, $tags); + tag_set('blog_entries', $this->id, $tags); } /** @@ -589,12 +571,12 @@ class blog_entry { } // coming for 1 entry, make sure it's not a draft - if ($this->publishstate == 'draft') { + if ($this->publishstate == BLOG_PUBLISHSTATE_DRAFT) { return false; // can not view draft of others } // coming for 1 entry, make sure user is logged in, if not a public blog - if ($this->publishstate != 'public' && !isloggedin()) { + if ($this->publishstate != BLOG_PUBLISHSTATE_PUBLIC && !isloggedin()) { return false; } @@ -632,15 +614,15 @@ class blog_entry { // everyone gets draft access if ($CFG->bloglevel >= BLOG_USER_LEVEL) { - $options['draft'] = get_string('publishtonoone', 'blog'); + $options[BLOG_PUBLISHSTATE_DRAFT] = get_string('publishtonoone', 'blog'); } if ($CFG->bloglevel > BLOG_USER_LEVEL) { - $options['site'] = get_string('publishtosite', 'blog'); + $options[BLOG_PUBLISHSTATE_SITE] = get_string('publishtosite', 'blog'); } if ($CFG->bloglevel >= BLOG_GLOBAL_LEVEL) { - $options['public'] = get_string('publishtoworld', 'blog'); + $options[BLOG_PUBLISHSTATE_PUBLIC] = get_string('publishtoworld', 'blog'); } return $options; @@ -717,9 +699,9 @@ class blog_listing { } // The query used to locate blog entries is complicated. It will be built from the following components: - $requiredfields = "p.*, u.firstname, u.lastname, u.email"; // the SELECT clause - $tables = array('p' => 'post', 'u' => 'user'); // components of the FROM clause (table_id => table_name) - $conditions = array('u.deleted = 0', 'p.userid = u.id', 'p.module = \'blog\''); // components of the WHERE clause (conjunction) + $requiredfields = "b.*, u.firstname, u.lastname, u.email"; // the SELECT clause + $tables = array('b' => 'blog_entries', 'u' => 'user'); // components of the FROM clause (table_id => table_name) + $conditions = array('u.deleted = 0', 'b.userid = u.id'); // components of the WHERE clause (conjunction) // build up a clause for permission constraints @@ -739,20 +721,20 @@ class blog_listing { $assocexists = $DB->record_exists('blog_association', array()); //dont check association records if there aren't any //begin permission sql clause - $permissionsql = '(p.userid = ? '; + $permissionsql = '(b.userid = ? '; $params[] = $userid; if ($CFG->bloglevel >= BLOG_SITE_LEVEL) { // add permission to view site-level entries - $permissionsql .= " OR p.publishstate = 'site' "; + $permissionsql .= " OR b.publishstate = " . BLOG_PUBLISHSTATE_SITE; } if ($CFG->bloglevel >= BLOG_GLOBAL_LEVEL) { // add permission to view global entries - $permissionsql .= " OR p.publishstate = 'public' "; + $permissionsql .= " OR b.publishstate = " . BLOG_PUBLISHSTATE_DRAFT; } $permissionsql .= ') '; //close permissions sql clause } else { // default is access to public entries - $permissionsql = "p.publishstate = 'public'"; + $permissionsql = "b.publishstate = " . BLOG_PUBLISHSTATE_PUBLIC; } $conditions[] = $permissionsql; //add permission constraints } @@ -1001,7 +983,7 @@ class blog_filter_context extends blog_filter { $this->overrides = array('site'); $context = get_context_instance(CONTEXT_COURSE, $this->id); $this->tables['ba'] = 'blog_association'; - $this->conditions[] = 'p.id = ba.blogid'; + $this->conditions[] = 'b.id = ba.blogid'; $this->conditions[] = 'ba.contextid = '.$context->id; break; } else { @@ -1017,8 +999,8 @@ class blog_filter_context extends blog_filter { $context = get_context_instance(CONTEXT_MODULE, $this->id); $this->tables['ba'] = 'blog_association'; - $this->tables['p'] = 'post'; - $this->conditions = array('p.id = ba.blogid', 'ba.contextid = ?'); + $this->tables['b'] = 'blog_entries'; + $this->conditions = array('b.id = ba.blogid', 'ba.contextid = ?'); $this->params = array($context->id); } break; @@ -1060,7 +1042,7 @@ class blog_filter_user extends blog_filter { $this->overrides = array('course', 'site'); $this->tables['gm'] = 'groups_members'; - $this->conditions[] = 'p.userid = gm.userid'; + $this->conditions[] = 'b.userid = gm.userid'; $this->conditions[] = 'gm.groupid = ?'; $this->params[] = $this->id; @@ -1069,7 +1051,7 @@ class blog_filter_user extends blog_filter { $this->tables['ba'] = 'blog_association'; $this->conditions[] = 'gm.groupid = ?'; $this->conditions[] = 'ba.contextid = ?'; - $this->conditions[] = 'ba.blogid = p.id'; + $this->conditions[] = 'ba.blogid = b.id'; $this->params[] = $this->id; $this->params[] = $course_context->id; } @@ -1082,7 +1064,7 @@ class blog_filter_user extends blog_filter { * This filter defines a tag by which blog entries should be searched. */ class blog_filter_tag extends blog_filter { - public $tables = array('t' => 'tag', 'ti' => 'tag_instance', 'p' => 'post'); + public $tables = array('t' => 'tag', 'ti' => 'tag_instance', 'b' => 'blog_entries'); /** * Constructor @@ -1094,8 +1076,8 @@ class blog_filter_tag extends blog_filter { $this->id = $id; $this->conditions = array('ti.tagid = t.id', - "ti.itemtype = 'post'", - 'ti.itemid = p.id', + "ti.itemtype = 'blog_entries'", + 'ti.itemid = b.id', 't.id = ?'); $this->params = array($this->id); } @@ -1122,8 +1104,8 @@ class blog_filter_search extends blog_filter { public function __construct($search_term) { global $DB; $ilike = $DB->sql_ilike(); - $this->conditions = array("(p.summary $ilike '%$search_term%' OR - p.content $ilike '%$search_term%' OR - p.subject $ilike '%$search_term%')"); + $this->conditions = array("(b.summary $ilike '%$search_term%' OR + b.content $ilike '%$search_term%' OR + b.subject $ilike '%$search_term%')"); } } diff --git a/blog/rsslib.php b/blog/rsslib.php index b59250adb2..a31aa7dabf 100755 --- a/blog/rsslib.php +++ b/blog/rsslib.php @@ -76,7 +76,7 @@ $item->pubdate = $blog_entry->lastmodified; $item->link = $CFG->wwwroot.'/blog/index.php?entryid='.$blog_entry->id; $item->description = format_text($blog_entry->summary, $blog_entry->format); - if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_array('post', $blog_entry->id)) ) { + if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_array('blog_entries', $blog_entry->id)) ) { if ($blogtags) { $item->tags = $blogtags; } diff --git a/lib/db/install.xml b/lib/db/install.xml index 6b33009777..52e0908742 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -2218,7 +2218,7 @@ - +
@@ -2232,92 +2232,29 @@
- +
- - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - -
- - - - - - - - + + + + + + + + + + + + - + +
- - - - - - - - - - -
- - - - - - - - - - - -
-
\ No newline at end of file + diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 77a57aca3f..b7c989a8fc 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2658,6 +2658,68 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL"); upgrade_main_savepoint($result, 2009091310); } + if ($result && $oldversion < 2009091700) { + + /// Define table blog_entries to be created + $table = new xmldb_table('blog_entries'); + + /// Adding fields to table blog_entries + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('subject', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, 'blog entry'); + $table->add_field('summary', XMLDB_TYPE_TEXT, 'big', null, null, null, null); + $table->add_field('content', XMLDB_TYPE_TEXT, 'big', null, null, null, null); + $table->add_field('permalink', XMLDB_TYPE_CHAR, '255', null, null, null, null); + $table->add_field('format', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('summaryformat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('attachment', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('publishstate', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('lastmodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('created', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); + $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null); + + /// Adding keys to table blog_entries + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id')); + $table->add_key('usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id')); + + /// Conditionally launch create table for blog_entries + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + // Now convert all entries in post table + if ($blogentries = $DB->get_records('post')) { + foreach ($blogentries as $blogentry) { + $newentry = new stdClass(); + $newentry->userid = $blogentry->userid; + $newentry->subject = $blogentry->subject; + $newentry->summary = $blogentry->summary; + $newentry->summaryformat = $blogentry->summaryformat; + $newentry->content = $blogentry->content; + $newentry->format = $blogentry->format; + $newentry->permalink = $blogentry->uniquehash; + $newentry->lastmodified = $blogentry->lastmodified; + $newentry->created = $blogentry->created; + $newentry->usermodified = $blogentry->usermodified; + + if ($blogentry->publishstate == 'draft') { + $newentry->publishstate = 0; + } else { + $newentry->publishstate = 1; + } + $newentry->id = $DB->insert_record('blog_entries', $newentry); + + // Convert records in the tag_instance table to the new ids + $DB->set_field('tag_instance', 'itemid', $newentry->id, array('itemtype' => 'post', 'itemid' => $blogentry->id)); + } + // Convert records in the tag_instance table to the new table name + $DB->set_field('tag_instance', 'itemtype', 'blog_entries', array('itemtype' => 'post')); + } + /// Main savepoint reached + upgrade_main_savepoint($result, 2009091700); + } + return $result; } diff --git a/version.php b/version.php index 19f8c61cd4..e307ceb8e7 100644 --- a/version.php +++ b/version.php @@ -6,7 +6,7 @@ // This is compared against the values stored in the database to determine // whether upgrades should be performed (see lib/db/*.php) - $version = 2009091310; // YYYYMMDD = date of the last version bump + $version = 2009091700; // YYYYMMDD = date of the last version bump // XX = daily increments $release = '2.0 dev (Build: 20090918)'; // Human-friendly version name -- 2.39.5