define('BLOGDEFAULTTIMEWITHIN', 90);
define('BLOGDEFAULTNUMBEROFTAGS', 20);
-define('BLOGDEFAULTSORT', 'text');
+define('BLOGDEFAULTSORT', 'name');
require_once($CFG->dirroot .'/blog/lib.php');
}
}
-
function get_content() {
global $CFG, $SITE, $COURSE, $USER;
$this->content->text = '';
$this->content->footer = '';
-
/// Get a list of tags
$timewithin = $this->config->timewithin * 24 * 60 * 60; /// convert to seconds
- $sql = 'SELECT t.id, t.type, t.text, COUNT(DISTINCT bt.id) as ct ';
- $sql .= "FROM {$CFG->prefix}tags t, {$CFG->prefix}blog_tag_instance bt, {$CFG->prefix}post p ";
- $sql .= 'WHERE t.id = bt.tagid ';
- $sql .= 'AND p.id = bt.entryid ';
+ $sql = 'SELECT t.id, t.tagtype, t.name, COUNT(DISTINCT ti.id) as ct ';
+ $sql .= "FROM {$CFG->prefix}tag t, {$CFG->prefix}tag_instance ti, {$CFG->prefix}post p ";
+ $sql .= 'WHERE t.id = ti.tagid ';
+ $sql .= 'AND p.id = ti.itemid ';
$sql .= 'AND (p.publishstate = \'site\' or p.publishstate=\'public\') ';
- $sql .= "AND bt.timemodified > {$timewithin} ";
- $sql .= 'GROUP BY t.id, t.type, t.text ';
- $sql .= 'ORDER BY ct DESC, t.text ASC';
+ $sql .= "AND ti.timemodified > {$timewithin} ";
+ $sql .= 'GROUP BY t.id, t.tagtype, t.name ';
+ $sql .= 'ORDER BY ct DESC, t.name ASC';
if ($tags = get_records_sql($sql, 0, $this->config->numberoftags)) {
$size = 20 - ( (int)((($currenttag - 1) / $totaltags) * 20) );
}
- $tag->class = "$tag->type s$size";
+ $tag->class = "$tag->tagtype s$size";
$etags[] = $tag;
}
$this->content->text .= '<li><a href="'.$link.'" '.
'class="'.$tag->class.'" '.
'title="'.get_string('numberofentries','blog',$tag->ct).'">'.
- $tag->text.'</a></li> ';
+ $tag->name.'</a></li> ';
}
$this->content->text .= "\n</ul>\n";
/// set up sort select field
$sort = array();
- $sort['text'] = get_string('tagtext', 'blog');
+ $sort['name'] = get_string('tagtext', 'blog');
$sort['id'] = get_string('tagdatelastused', 'blog');
} else {
notice(get_string('blockconfigbad'), str_replace('blockaction=', 'dummy=', qualified_me()));
}
-
}
-
}
function blog_tags_sort($a, $b) {
require_once('../config.php');
include_once('lib.php');
+include_once($CFG->dirroot.'/tag/lib.php');
$action = required_param('action', PARAM_ALPHA);
$id = optional_param('id', 0, PARAM_INT);
$post->action = $action;
$strformheading = get_string('updateentrywithid', 'blog');
- if ($ptags = get_records_sql_menu("SELECT t.id, t.text FROM
- {$CFG->prefix}tags t,
- {$CFG->prefix}blog_tag_instance bti
- WHERE t.id = bti.tagid
- AND t.type = 'personal'
- AND bti.entryid = {$post->id}")) {
+ if ($ptags = get_records_sql_menu("SELECT t.id, t.name FROM
+ {$CFG->prefix}tag t,
+ {$CFG->prefix}tag_instance ti
+ WHERE t.id = ti.tagid
+ AND t.tagtype = 'default'
+ AND ti.itemid = {$post->id}")) {
$post->ptags = implode(', ', $ptags);
} else {
//was used but seems redundant.
$post->ptags = '';
}
- if ($otags = get_records_sql_menu("SELECT t.id, t.text FROM
- {$CFG->prefix}tags t,
- {$CFG->prefix}blog_tag_instance bti
- WHERE t.id = bti.tagid
- AND t.type = 'official'
- AND bti.entryid = {$post->id}")){
+ if ($otags = get_records_sql_menu("SELECT t.id, t.name FROM
+ {$CFG->prefix}tag t,
+ {$CFG->prefix}tag_instance ti
+ WHERE t.id = ti.tagid
+ AND t.tagtype = 'official'
+ AND ti.itemid = {$post->id}")){
$post->otags = array_keys($otags);
}
break;
function delete_otags($tagids, $sitecontext){
foreach ($tagids as $tagid) {
- if (!$tag = get_record('tags', 'id', $tagid)) {
+ if (!$tag = tag_by_id($tagid)) {
error('Can not delete tag, tag doesn\'t exist');
}
-
- if ($tag->type == 'official' and !has_capability('moodle/blog:manageofficialtags', $sitecontext)) {
- //can not delete
- error('Can not delete tag, you don\'t have permission to delete an official tag');
+ if ($tag->tagtype != 'official') {
+ continue;
}
-
- if ($tag->type == 'personal' and !has_capability('moodle/blog:managepersonaltags', $sitecontext)) {
+ if ($tag->tagtype == 'official' and !has_capability('moodle/blog:manageofficialtags', $sitecontext)) {
//can not delete
- error('Can not delete tag, you don\'t have permission to delete a personal tag');
+ error('Can not delete tag, you don\'t have permission to delete an official tag');
}
// Delete the tag itself
- if (!delete_records('tags', 'id', $tagid)) {
+ if (!tag_delete($tagid)) {
error('Can not delete tag');
}
-
- // Deleteing all references to this tag
- if (!delete_records('blog_tag_instance', 'tagid', $tagid)) {
- error('Can not delete blog tag instances');
- }
-
-
}
}
function add_otag($otag){
global $USER;
$error = '';
- if ($tag = get_record('tags', 'text', $otag)) {
- if ($tag->type == 'official') {
+
+ // When adding ofical tag, we see if there's already a personal tag
+ // With the same Name, if there is, we just change the type
+ if ($tag = tag_by_name ($otag)) {
+ if ($tag->tagtype == 'official') {
// official tag already exist
$error = get_string('tagalready');
+ break;
} else {
- $tag->type = 'official';
- update_record('tags', $tag);
+ // might not want to do this anymore?
+ $tag->tagtype = 'official';
+ update_record('tag', $tag);
$tagid = $tag->id;
}
+
} else { // Brand new offical tag
-
- $tag = new object();
- $tag->userid = $USER->id;
- $tag->text = $otag;
- $tag->type = 'official';
-
- if (!$tagid = insert_record('tags', $tag)) {
+ $tagid = tag_create($otag, 'official');
+ if (empty($tagid)) {
error('Can not create tag!');
- }
+ }
}
return $error;
}
global $returnurl;
$status = delete_records('post', 'id', $post->id);
- $status = delete_records('blog_tag_instance', 'entryid', $post->id) and $status;
-
+ //$status = delete_records('blog_tag_instance', 'entryid', $post->id) and $status;
+ untag_an_item('blog', $post->id);
+
blog_delete_old_attachments($post);
add_to_log(SITEID, 'blog', 'delete', 'index.php?userid='. $post->userid, 'deleted blog entry with entry id# '. $post->id);
// update record
if (update_record('post', $post)) {
// delete all tags associated with this entry
- delete_records('blog_tag_instance', 'entryid', $post->id);
+
+ //delete_records('blog_tag_instance', 'entryid', $post->id);
+ //delete_records('tag_instance', 'itemid', $post->id, 'itemtype', 'blog');
+ untag_an_item('blog', $post->id);
// add them back
add_tags_info($post->id);
-
add_to_log(SITEID, 'blog', 'update', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject);
} else {
$post = get_record('post', 'id', $postid);
- $tag = new object();
- $tag->entryid = $post->id;
- $tag->userid = $post->userid;
- $tag->timemodified = time();
-
/// Attach official tags
if ($otags = optional_param('otags', '', PARAM_INT)) {
foreach ($otags as $otag) {
$tag->tagid = $otag;
- insert_record('blog_tag_instance', $tag);
+ //insert_record('blog_tag_instance', $tag);
+ tag_an_item('blog', $postid, $otag, 'official');
}
}
// check for existance
// it does not matter whether it is an offical tag or personal tag
// we do not want to have 1 copy of offical tag and 1 copy of personal tag (for the same tag)
- if ($ctag = get_record('tags', 'text', $ptag)) {
- $tag->tagid = $ctag->id;
- insert_record('blog_tag_instance', $tag);
+ if ($ctag = tag_by_id($ptag)) {
+ tag_an_item('blog', $postid, $ctag);
} else { // create a personal tag
- $ctag = new object;
- $ctag->userid = $USER->id;
- $ctag->text = $ptag;
- $ctag->type = 'personal';
- if ($tagid = insert_record('tags', $ctag)) {
- $tag->tagid = $tagid;
- insert_record('blog_tag_instance', $tag);
+ if ($tagid = tag_create($ptag)) {
+ if ($tagid = array_shift($tagid)) {
+ tag_an_item('blog', $postid, $tagid);
+ }
}
}
}
function otags_select_setup(){
global $CFG;
$mform =& $this->_form;
- $otagsselect =& $mform->getElement('otags');
- $otagsselect->removeOptions();
- if ($otags = get_records_sql_menu('SELECT id, text from '.$CFG->prefix.'tags WHERE type=\'official\' ORDER by text ASC')){
+ if ($otagsselect =& $mform->getElement('otags')) {
+ $otagsselect->removeOptions();
+ }
+ if ($otags = get_records_sql_menu('SELECT id, name from '.$CFG->prefix.'tag WHERE tagtype=\'official\' ORDER by name ASC')){
$otagsselect->loadArray($otags);
} else {
- $mform->removeElement('otags');
+ // removing this causes errors
+ //$mform->removeElement('otags');
}
}
}
if (!empty($tagid)) {
- $taginstance = get_record('tags', 'id', $tagid);
+ $taginstance = get_record('tag', 'id', $tagid);
} else {
$tagid = '';
if (!empty($tag)) {
- $tagrec = get_record('tags', 'text', $tag);
- $tagid = $tagrec->id;
- $taginstance = get_record('tags', 'id', $tagid);
+ $taginstance = tag_id($tag);
}
}
case 'site':
if ($tagid || !empty($tag)) {
$navlinks[] = array('name' => $blogstring, 'link' => "index.php?filtertype=site", 'type' => 'misc');
- $navlinks[] = array('name' => "$tagstring: $taginstance->text", 'link' => null, 'type' => 'misc');
+ $navlinks[] = array('name' => "$tagstring: $taginstance->name", 'link' => null, 'type' => 'misc');
$navigation = build_navigation($navlinks);
print_header("$SITE->shortname: $blogstring", $SITE->fullname, $navigation,'','',true,$PAGE->get_extra_header_string());
} else {
$navlinks[] = array('name' => $blogstring,
'link' => "index.php?filtertype=course&filterselect=$filterselect",
'type' => 'misc');
- $navlinks[] = array('name' => "$tagstring: $taginstance->text", 'link' => null, 'type' => 'misc');
+ $navlinks[] = array('name' => "$tagstring: $taginstance->name", 'link' => null, 'type' => 'misc');
$navigation = build_navigation($navlinks);
print_header("$course->shortname: $blogstring", $course->fullname, $navigation,'','',true,$PAGE->get_extra_header_string());
} else {
$navlinks[] = array('name' => $blogstring,
'link' => "index.php?filtertype=group&filterselect=$filterselect",
'type' => 'misc');
- $navlinks[] = array('name' => "$tagstring: $taginstance->text", 'link' => null, 'type' => 'misc');
+ $navlinks[] = array('name' => "$tagstring: $taginstance->name", 'link' => null, 'type' => 'misc');
$navigation = build_navigation($navlinks);
print_header("$course->shortname: $blogstring", $course->fullname, $navigation,'','',true,$PAGE->get_extra_header_string());
} else {
$navlinks[] = array('name' => $blogstring,
'link' => "index.php?courseid=$course->id&filtertype=user&filterselect=$filterselect",
'type' => 'misc');
- $navlinks[] = array('name' => "$tagstring: $taginstance->text", 'link' => null, 'type' => 'misc');
+ $navlinks[] = array('name' => "$tagstring: $taginstance->name", 'link' => null, 'type' => 'misc');
$navigation = build_navigation($navlinks);
print_header("$course->shortname: $blogstring", $course->fullname, $navigation,'','',true,$PAGE->get_extra_header_string());
$navlinks[] = array('name' => $blogstring,
'link' => "index.php?filtertype=user&filterselect=$filterselect",
'type' => 'misc');
- $navlinks[] = array('name' => "$tagstring: $taginstance->text", 'link' => null, 'type' => 'misc');
+ $navlinks[] = array('name' => "$tagstring: $taginstance->name", 'link' => null, 'type' => 'misc');
$navigation = build_navigation($navlinks);
print_header("$SITE->shortname: $blogstring", $SITE->fullname, $navigation,'','',true,$PAGE->get_extra_header_string());
require_once($CFG->libdir .'/pagelib.php');
require_once($CFG->dirroot .'/blog/rsslib.php');
require_once($CFG->dirroot .'/blog/blogpage.php');
-
+ include_once($CFG->dirroot.'/tag/lib.php');
/**
* Definition of blogcourse page type (blog page with course id present).
echo $attachedimages;
/// Links to tags
+ /*
if ($blogtags = get_records_sql('SELECT t.* FROM '.$CFG->prefix.'tags t, '.$CFG->prefix.'blog_tag_instance ti
WHERE t.id = ti.tagid
AND ti.entryid = '.$blogEntry->id)) {
+ */
+ if ($blogtags = get_item_tags('blog', $blogEntry->id)) {
echo '<div class="tags">';
if ($blogtags) {
print_string('tags');
echo ': ';
foreach ($blogtags as $key => $blogtag) {
- $taglist[] = '<a href="index.php?filtertype='.$filtertype.'&filterselect='.$filterselect.'&tagid='.$blogtag->id.'">'.$blogtag->text.'</a>';
+ $taglist[] = '<a href="index.php?filtertype='.$filtertype.'&filterselect='.$filterselect.'&tagid='.$blogtag->id.'">'.$blogtag->name.'</a>';
}
echo implode(', ', $taglist);
}
if ($tagid) {
$tag = $tagid;
} else if ($tag) {
- if ($tagrec = get_record_sql('SELECT * FROM '.$CFG->prefix.'tags WHERE text LIKE "'.$tag.'"')) {
+ if ($tagrec = get_record_sql('SELECT * FROM '.$CFG->prefix.'tag WHERE name LIKE "'.$tag.'"')) {
$tag = $tagrec->id;
} else {
$tag = -1; //no records found
}
if ($tag) {
- $tagtablesql = $CFG->prefix.'blog_tag_instance bt, ';
- $tagquerysql = ' AND bt.entryid = p.id AND bt.tagid = '.$tag.' ';
+ $tagtablesql = $CFG->prefix.'tag_instance ti, ';
+ $tagquerysql = ' AND ti.itemid = p.id AND ti.tagid = '.$tag.' ';
} else {
$tagtablesql = '';
$tagquerysql = '';
$otag = trim(required_param('otag', PARAM_NOTAGS));
// When adding ofical tag, we see if there's already a personal tag
// With the same Name, if there is, we just change the type
- if ($tag = get_record('tags', 'text', $otag)) {
+ if ($tag = tag_by_name ($otag)) {
if ($tag->type == 'official') {
// official tag already exist
$error = get_string('tagalready');
break;
} else {
$tag->type = 'official';
- update_record('tags', $tag);
+ update_record('tag', $tag);
$tagid = $tag->id;
}
} else { // Brand new offical tag
-
- $tag = new object();
- $tag->userid = $USER->id;
- $tag->text = $otag;
- $tag->type = 'official';
-
- if (!$tagid = insert_record('tags', $tag)) {
+ if (!$tagid = tag_create($otag, 'official')) {
error('Can not create tag!');
}
}
/// Launch add field timemodified
$result = $result && add_field($table, $field);
}
+
+ /// migrate all tags table to tag
+ if ($result && $oldversion < 2007082701) {
+ require_once($CFG->dirroot.'/tag/lib.php');
+ $tagrefs = array(); // $tagrefs[$oldtagid] = $newtagid
+ if ($tags = get_records('tags')) {
+ foreach ($tags as $oldtag) {
+ // if this tag does not exist in tag table yet
+ if (!$newtag = get_record('tag', 'name', tag_normalize($oldtag->text))) {
+ $itag->name = tag_normalize($oldtag->text);
+ $itag->rawname = tag_normalize($oldtag->text, false);
+
+ if ($oldtag->type == 'official') {
+ $itag->tagtype = $oldtag->type;
+ } else {
+ $itag->tagtype = 'default';
+ }
+ $itag->userid = $oldtag->userid;
+ $itag->timemodified = time();
+ if ($idx = insert_record('tag', $itag)) {
+ $tagrefs[$oldtag->id] = $idx;
+ }
+ // if this tag is already used by tag table
+ } else {
+ $tagrefs[$oldtag->id] = $newtag->id;
+ }
+ }
+ }
+
+ // fetch all the tag instances and migrate them as well
+ if ($blogtags = get_records('blog_tag_instance')) {
+ foreach ($blogtags as $blogtag) {
+ if (!empty($tagrefs[$blogtag->tagid])) {
+ tag_an_item('blog', $blogtag->entryid, $tagrefs[$blogtag->tagid]);
+ }
+ }
+ }
+
+ $table = new XMLDBTable('tags');
+ drop_table($table);
+ $table = new XMLDBTable('blog_tag_instance');
+ drop_table($table);
+ }
return $result;
}
//covert all ids to names
$tag_names_csv = tag_name_from_string($tag_names_or_ids_csv);
-
+ $tag_ids_csv = tag_id_from_string($tag_names_csv);
//put apostrophes in names
$tag_names_csv_with_apos = "'" . str_replace(',', "','", $tag_names_csv) . "'";
+ $tag_ids_csv_with_apos = "'" . str_replace(',', "','", $tag_ids_csv) . "'";
- delete_records_select('tag',"name IN ($tag_names_csv_with_apos)");
+ // tag instances needs to be deleted as well
+ delete_records_select('tag_instance',"tagid IN ($tag_ids_csv_with_apos)");
+ return delete_records_select('tag',"name IN ($tag_names_csv_with_apos)");
}
$tag_instance->tagid = $tag_id;
$tag_instance->ordering = $ordering[$tag_normalized_name];
-
+ $tag_instance->timemodified = time();
$tag_instance_exists = get_record('tag_instance', 'tagid', $tag_id, 'itemtype', $item_type, 'itemid', $item_id);
if (!$tag_instance_exists) {
insert_record('tag_instance',$tag_instance);
}
else {
+ $tag_instance_exists->timemodified = time();
$tag_instance_exists->ordering = $ordering[$tag_normalized_name];
update_record('tag_instance',$tag_instance_exists);
}
// This is compared against the values stored in the database to determine
// whether upgrades should be performed (see lib/db/*.php)
- $version = 2007082700; // YYYYMMDD = date
+ $version = 2007082701; // YYYYMMDD = date
// XY = increments within a single day
$release = '1.9 Beta +'; // Human-friendly version name