Commit b156f2e2 authored by Robert Lyon's avatar Robert Lyon
Browse files

Bug 1774103: Looking to combine all the *_tag tables



So that we only have one 'tag' table that holds all the tag information

behatnotneeded

Change-Id: Ieb83e61da45edddfa8703816c11f3658c5394c47
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent ffa51659
......@@ -270,7 +270,7 @@ $elements['externalauthjs'] = array(
'value' => $js,
);
$tags = get_column_sql('SELECT tag FROM {usr_tag} WHERE usr = ? AND NOT tag ' . db_ilike() . " 'lastinstitution:%'", array($user->id));
$tags = get_column_sql('SELECT tag FROM {tag} WHERE resourcetype = ? AND resourceid = ? AND NOT tag ' . db_ilike() . " 'lastinstitution:%'", array('usr', $user->id));
$elements['tags'] = array(
'defaultvalue' => $tags,
......@@ -603,18 +603,23 @@ function edituser_site_submit(Pieform $form, $values) {
// Update user's primary email address
set_user_primary_email($user->id, $values['email']);
delete_records('usr_tag', 'usr', $user->id);
delete_records('tag', 'resourcetype', 'usr', 'resourceid', $user->id);
if (is_array($values['tags'])) {
$values['tags'] = check_case_sensitive($values['tags'], 'usr_tag');
$values['tags'] = check_case_sensitive($values['tags'], 'tag');
foreach(array_unique($values['tags']) as $tag) {
if (empty($tag)) {
continue;
}
insert_record(
'usr_tag',
'tag',
(object) array(
'usr' => $user->id,
'resourcetype' => 'usr',
'resourceid' => $user->id,
'ownertype' => 'user',
'ownerid' => $user->id,
'tag' => strtolower($tag),
'ctime' => db_format_timestamp(time()),
'editedby' => $USER->get('id'),
)
);
}
......
......@@ -348,13 +348,13 @@ function search_folders_and_files($username, $search='') {
$filetypesql = "('" . join("','", $filetypes) . "')";
$ownersql = artefact_owner_sql($user->id);
//retrieve folders and files of a specific Mahara folder
$typecast = is_postgres() ? '::varchar' : '';
// Retrieve folders and files of a specific Mahara folder
$sql = "SELECT
*
FROM
{artefact} a
LEFT JOIN {artefact_tag} at ON (at.artefact = a.id)
LEFT JOIN {tag} at ON (at.resourcetype = 'artefact' AND at.resourceid = a.id" . $typecast . ")
WHERE
$ownersql
AND
......
......@@ -107,31 +107,32 @@ class PluginBlocktypeTaggedposts extends MaharaCoreBlocktype {
}
$tagsout = array_filter($tagsout);
$sqlvalues = array($view);
$typecast = is_postgres() ? '::varchar' : '';
$sql =
'SELECT a.title, p.title AS parenttitle, a.id, a.parent, a.owner, a.description, a.allowcomments, at.tag, a.ctime, a.mtime
"SELECT a.title, p.title AS parenttitle, a.id, a.parent, a.owner, a.description, a.allowcomments, at.tag, a.ctime, a.mtime
FROM {artefact} a
JOIN {artefact} p ON a.parent = p.id
JOIN {artefact_blog_blogpost} ab ON (ab.blogpost = a.id AND ab.published = 1)
JOIN {artefact_tag} at ON (at.artefact = a.id)
WHERE a.artefacttype = \'blogpost\'
AND a.owner = (SELECT "owner" from {view} WHERE id = ?)';
JOIN {tag} at ON (at.resourcetype = 'artefact' AND at.resourceid = a.id" . $typecast . ")
WHERE a.artefacttype = 'blogpost'
AND a.owner = (SELECT \"owner\" from {view} WHERE id = ?)";
if (!empty($tagsin)) {
foreach ($tagsin as $tagin) {
$sql .= ' AND EXISTS (
SELECT * FROM {artefact_tag} AS at
WHERE a.id = at.artefact
$sql .= " AND EXISTS (
SELECT * FROM {tag} AS at
WHERE at.resourcetype = 'artefact' AND at.resourceid = a.id" . $typecast . "
AND at.tag = ?
)';
)";
}
$sqlvalues = array_merge($sqlvalues, $tagsin);
}
if (!empty($tagsout)) {
foreach ($tagsout as $tagout) {
$sql .= ' AND NOT EXISTS (
SELECT * FROM {artefact_tag} AS at
WHERE a.id = at.artefact
$sql .= " AND NOT EXISTS (
SELECT * FROM {tag} AS at
WHERE at.resourcetype = 'artefact' AND at.resourceid = a.id" . $typecast . "
AND at.tag = ?
)';
)";
}
$sqlvalues = array_merge($sqlvalues, $tagsout);
}
......@@ -234,7 +235,7 @@ class PluginBlocktypeTaggedposts extends MaharaCoreBlocktype {
$result->comments = $comments;
// get all tags for this post
$taglist = get_records_array('artefact_tag', 'artefact', $result->id, "tag DESC");
$taglist = get_records_sql_array("SELECT tag FROM {tag} WHERE resourcetype = 'artefact' AND resourceid = ? ORDER BY tag DESC", array($result->id));
foreach ($taglist as $t) {
$result->taglist[] = $t->tag;
}
......@@ -318,12 +319,11 @@ class PluginBlocktypeTaggedposts extends MaharaCoreBlocktype {
private static function get_chooseable_tags() {
global $USER;
$typecast = is_postgres() ? '::varchar' : '';
return get_records_sql_array("
SELECT at.tag
FROM
{artefact_tag} at
JOIN {artefact} a
ON a.id = at.artefact
FROM {tag} at
JOIN {artefact} a ON (at.resourcetype ='artefact' AND at.resourceid = a.id" . $typecast . ")
WHERE
a.owner = ?
AND a.artefacttype = 'blogpost'
......
......@@ -28,9 +28,12 @@ if ($page < 1) {
}
$tagsperpage = 5;
$values = array($USER->id);
$sql = "SELECT at.tag FROM {artefact_tag} at
JOIN {artefact} a ON a.id = at.artefact
$typecast = is_postgres() ? '::varchar' : '';
$sql = "SELECT at.tag FROM {tag} at
JOIN {artefact} a ON (at.resourcetype = 'artefact' AND at.resourceid = a.id" . $typecast . ")
WHERE a.owner = ?
AND at.resourcetype = 'artefact'
AND at.resourceid = a.id" . $typecast . "
AND a.artefacttype = 'blogpost'";
if ($request !== '') {
$sql .= " AND at.tag LIKE '%' || ? || '%'";
......
......@@ -1056,7 +1056,7 @@ class ArtefactTypeBlogPost extends ArtefactType {
if ($tags = ArtefactType::tags_from_id_list($postids)) {
foreach($tags as &$at) {
$data[$at->artefact]->tags[] = $at->tag;
$data[$at->resourceid]->tags[] = $at->tag;
}
}
......
......@@ -693,13 +693,14 @@ abstract class ArtefactTypeFileBase extends ArtefactType {
}
}
}
$where = 'artefact IN (' . join(',', array_keys($filedata)) . ')';
$tags = get_records_select_array('artefact_tag', $where);
$tagwhere = "'" . join("','", array_keys($filedata)) . "'";
$tags = get_records_sql_array("SELECT tag, resourceid FROM {tag} WHERE resourcetype = 'artefact' AND resourceid IN (" . $tagwhere . ")");
if ($tags) {
foreach ($tags as $t) {
$filedata[$t->artefact]->tags[] = $t->tag;
$filedata[$t->resourceid]->tags[] = $t->tag;
}
}
$where = 'artefact IN (' . join(',', array_keys($filedata)) . ')';
if ($group) { // Fetch permissions for each artefact
$perms = get_records_select_array('artefact_access_role', $where);
if ($perms) {
......
......@@ -588,6 +588,8 @@ abstract class ArtefactType implements IArtefactType {
* this method, and call parent::commit() in your own function.
*/
public function commit() {
global $USER;
static $last_source, $last_output;
$is_new = false;
......@@ -634,20 +636,36 @@ abstract class ArtefactType implements IArtefactType {
}
if (!$is_new) {
$deleted = delete_records('artefact_tag', 'artefact', $this->id);
$deleted = delete_records('tag', 'resourcetype', 'artefact', 'resourceid', $this->id);
}
if (is_array($this->tags)) {
$this->tags = check_case_sensitive($this->tags, 'artefact_tag');
if ($this->group) {
$ownertype = 'group';
$ownerid = $this->group;
}
else if ($this->institution) {
$ownertype = 'institution';
$ownerid = $this->institution;
}
else {
$ownertype = 'user';
$ownerid = $this->owner;
}
$this->tags = check_case_sensitive($this->tags, 'tag');
foreach (array_unique($this->tags) as $tag) {
if (empty($tag)) {
continue;
}
insert_record(
'artefact_tag',
insert_record('tag',
(object) array(
'artefact' => $this->id,
'tag' => $tag,
'resourcetype' => 'artefact',
'resourceid' => $this->get('id'),
'ownertype' => $ownertype,
'ownerid' => $ownerid,
'tag' => $tag,
'ctime' => db_format_timestamp(time()),
'editedby' => $USER->get('id'),
)
);
}
......@@ -831,7 +849,7 @@ abstract class ArtefactType implements IArtefactType {
BlockInstance::bulk_remove_artefacts($artefactids);
delete_records_select('view_artefact', "artefact IN $idstr");
delete_records_select('artefact_tag', "artefact IN $idstr");
delete_records_select('tag', "resourcetype = 'artefact' AND resourceid IN ('" . join("','", array_map('intval', $artefactids)) . "')");
delete_records_select('artefact_access_role', "artefact IN $idstr");
delete_records_select('artefact_access_usr', "artefact IN $idstr");
execute_sql("UPDATE {usr} SET profileicon = NULL WHERE profileicon IN $idstr");
......@@ -1215,8 +1233,8 @@ abstract class ArtefactType implements IArtefactType {
if (empty($artefactids)) {
return array();
}
$artefactids = join(',', array_map('intval', $artefactids));
$tags = get_records_select_array('artefact_tag', 'artefact IN (' . $artefactids . ')');
$artefactids = join("','", array_map('intval', $artefactids));
$tags = get_records_sql_array("SELECT tag, resourceid FROM {tag} WHERE resourcetype = 'artefact' AND resourceid IN ('" . $artefactids . "')");
if (!$tags) {
return array();
}
......@@ -1232,10 +1250,10 @@ abstract class ArtefactType implements IArtefactType {
// load tags
if ($list) {
$tags = get_records_select_array('artefact_tag', 'artefact IN (' . join(',', array_keys($list)) . ')');
$tags = get_records_sql_array("SELECT tag, resourceid FROM {tag} WHERE resourcetype = 'artefact' AND resourceid IN ('" . join("','", array_keys($list)) . "')");
if ($tags) {
foreach ($tags as $t) {
$list[$t->artefact]->tags[] = $t->tag;
$list[$t->resourceid]->tags[] = $t->tag;
}
foreach ($list as &$attachment) {
if (!empty($attachment->tags)) {
......@@ -1338,7 +1356,7 @@ abstract class ArtefactType implements IArtefactType {
if (empty($id)) {
return array();
}
$tags = get_column_sql('SELECT tag FROM {artefact_tag} WHERE artefact = ? ORDER BY tag', array($id));
$tags = get_column_sql('SELECT tag FROM {tag} WHERE resourcetype = ? AND resourceid = ? ORDER BY tag', array('artefact', $id));
if (!$tags) {
return array();
}
......
......@@ -69,16 +69,8 @@ function edit_tag_submit(Pieform $form, $values) {
}
db_begin();
execute_sql(
"UPDATE {view_tag} SET tag = ? WHERE tag = ? AND \"view\" IN (SELECT id FROM {view} WHERE \"owner\" = ?)",
array($values['tagname'], $tag, $userid)
);
execute_sql(
"UPDATE {collection_tag} SET tag = ? WHERE tag = ? AND \"collection\" IN (SELECT id FROM {collection} WHERE \"owner\" = ?)",
array($values['tagname'], $tag, $userid)
);
execute_sql(
"UPDATE {artefact_tag} SET tag = ? WHERE tag = ? AND artefact IN (SELECT id FROM {artefact} WHERE \"owner\" = ?)",
array($values['tagname'], $tag, $userid)
"UPDATE {tag} SET tag = ? WHERE tag = ? AND ownertype = ? AND ownerid = ? AND resourcetype IN ('artefact', 'view', 'collection')",
array($values['tagname'], $tag, 'user', $userid)
);
db_commit();
$SESSION->add_ok_msg(get_string('tagupdatedsuccessfully'));
......@@ -92,16 +84,8 @@ function delete_tag_submit(Pieform $form, $values) {
}
db_begin();
execute_sql(
"DELETE FROM {view_tag} WHERE tag = ? AND view IN (SELECT id FROM {view} WHERE \"owner\" = ?)",
array($tag, $userid)
);
execute_sql(
"DELETE FROM {collection_tag} WHERE tag = ? AND collection IN (SELECT id FROM {collection} WHERE \"owner\" = ?)",
array($tag, $userid)
);
execute_sql(
"DELETE FROM {artefact_tag} WHERE tag = ? AND artefact IN (SELECT id FROM {artefact} WHERE \"owner\" = ?)",
array($tag, $userid)
"DELETE FROM {tag} WHERE tag = ? AND ownertype = ? AND ownerid = ? AND resourcetype IN ('artefact', 'view', 'collection')",
array($tag, 'user', $userid)
);
db_commit();
$SESSION->add_ok_msg(get_string('tagdeletedsuccessfully'));
......
......@@ -152,7 +152,7 @@ class Collection {
}
delete_records('collection_view','collection',$this->id);
delete_records('collection_tag','collection',$this->id);
delete_records('tag', 'resourcetype', 'collection', 'resourceid', $this->id);
delete_records('collection','id',$this->id);
delete_records('existingcopy', 'collection', $this->id);
......@@ -173,6 +173,7 @@ class Collection {
* This method updates the contents of the collection table only.
*/
public function commit() {
global $USER;
$fordb = new StdClass;
foreach (get_object_vars($this) as $k => $v) {
......@@ -196,12 +197,34 @@ class Collection {
}
if (isset($this->tags)) {
delete_records('collection_tag', 'collection', $this->get('id'));
$tags = check_case_sensitive($this->get_tags(), 'collection_tag');
if ($this->group) {
$ownertype = 'group';
$ownerid = $this->group;
}
else if ($this->institution) {
$ownertype = 'institution';
$ownerid = $this->institution;
}
else {
$ownertype = 'user';
$ownerid = $this->owner;
}
delete_records('tag', 'resourcetype', 'collection', 'resourceid', $this->get('id'));
$tags = check_case_sensitive($this->get_tags(), 'tag');
foreach ($tags as $tag) {
//truncate the tag before insert it into the database
$tag = substr($tag, 0, 128);
insert_record('collection_tag', (object)array( 'collection' => $this->get('id'), 'tag' => $tag));
insert_record('tag',
(object)array(
'resourcetype' => 'collection',
'resourceid' => $this->get('id'),
'ownertype' => $ownertype,
'ownerid' => $ownerid,
'tag' => $tag,
'ctime' => db_format_timestamp(time()),
'editedby' => $USER->get('id'),
)
);
}
}
......@@ -1332,7 +1355,7 @@ class Collection {
*/
public function get_tags() {
if (!isset($this->tags)) {
$this->tags = get_column('collection_tag', 'tag', 'collection', $this->get('id'));
$this->tags = get_column_sql('SELECT tag FROM {tag} WHERE resourcetype = ? AND resourceid = ? ORDER BY tag', array('collection', $this->get('id')));
}
return $this->tags;
}
......
......@@ -5915,6 +5915,11 @@ function xmldb_core_upgrade($oldversion=0) {
FROM {usr_tag} ut
JOIN {usr} u ON u.id = ut.usr
WHERE u.deleted = 0");
// Drop old *_tag tables
execute_sql("DROP TABLE {artefact_tag}");
execute_sql("DROP TABLE {usr_tag}");
execute_sql("DROP TABLE {view_tag}");
execute_sql("DROP TABLE {collection_tag}");
}
}
......
......@@ -81,16 +81,17 @@ function get_all_tags_for_user($query = null, $limit = null, $offset = null) {
if ($USER->is_logged_in()) {
$usertags = "";
$userid = $USER->get('id');
$typecast = is_postgres() ? '::varchar' : '';
if ($USER->get('admin')) {
$usertags = "
UNION ALL
SELECT tag,COUNT(*) AS count FROM {usr_tag} t INNER JOIN {usr} u ON t.usr=u.id GROUP BY 1";
SELECT tag, COUNT(*) AS count FROM {tag} t INNER JOIN {usr} u ON (t.resourcetype = 'usr' AND t.resourceid = u.id" . $typecast . ") GROUP BY 1";
}
else if ($admininstitutions = $USER->get('admininstitutions')) {
$insql = "'" . join("','", $admininstitutions) . "'";
$usertags = "
UNION ALL
SELECT tag,COUNT(*) AS count FROM {usr_tag} t INNER JOIN {usr} u ON t.usr=u.id INNER JOIN {usr_institution} ui ON ui.usr=u.id WHERE ui.institution IN ($insql) GROUP BY 1";
SELECT tag, COUNT(*) AS count FROM {tag} t INNER JOIN {usr} u ON (t.resourcetype = 'usr' AND t.resourceid = u.id" . $typecast . ") INNER JOIN {usr_institution} ui ON ui.usr=u.id WHERE ui.institution IN ($insql) GROUP BY 1";
}
$values = array($userid, $userid, $userid);
$querystr = '';
......@@ -101,11 +102,11 @@ function get_all_tags_for_user($query = null, $limit = null, $offset = null) {
$sql = "
SELECT tag, SUM(count) AS count
FROM (
SELECT tag,COUNT(*) AS count FROM {artefact_tag} t INNER JOIN {artefact} a ON t.artefact=a.id WHERE a.owner=? GROUP BY 1
SELECT tag,COUNT(*) AS count FROM {tag} t INNER JOIN {artefact} a ON (t.resourcetype = 'artefact' AND t.resourceid = a.id" . $typecast . ") WHERE a.owner=? GROUP BY 1
UNION ALL
SELECT tag,COUNT(*) AS count FROM {view_tag} t INNER JOIN {view} v ON t.view=v.id WHERE v.owner=? GROUP BY 1
SELECT tag,COUNT(*) AS count FROM {tag} t INNER JOIN {view} v ON (t.resourcetype = 'view' AND t.resourceid = v.id" . $typecast . ") WHERE v.owner=? GROUP BY 1
UNION ALL
SELECT tag,COUNT(*) AS count FROM {collection_tag} t INNER JOIN {collection} c ON t.collection=c.id WHERE c.owner=? GROUP BY 1
SELECT tag,COUNT(*) AS count FROM {tag} t INNER JOIN {collection} c ON (t.resourcetype = 'collection' AND t.resourceid = c.id" . $typecast . ") WHERE c.owner=? GROUP BY 1
" . $usertags . "
) tags
" . $querystr . "
......
......@@ -343,9 +343,9 @@ class Institution {
insert_record('usr_institution', $userinst);
delete_records('usr_institution_request', 'usr', $userinst->usr, 'institution', $this->name);
execute_sql("
DELETE FROM {usr_tag}
WHERE usr = ? AND tag " . db_ilike() . " 'lastinstitution:%'",
array($user->id)
DELETE FROM {tag}
WHERE resourcetype = ? AND resourceid = ? AND tag " . db_ilike() . " 'lastinstitution:%'",
array('usr', $user->id)
);
// Copy institution views and collection to the user's portfolio
$checkviewaccess = empty($user->newuser) && !$USER->get('admin');
......@@ -677,16 +677,21 @@ class Institution {
);
execute_sql("
DELETE FROM {usr_tag}
WHERE usr = ? AND tag " . db_ilike() . " 'lastinstitution:%'",
array($user->id)
DELETE FROM {tag}
WHERE resourcetype = ? AND resourceid = ? AND tag " . db_ilike() . " 'lastinstitution:%'",
array('usr', $user->id)
);
insert_record(
'usr_tag',
'tag',
(object) array(
'usr' => $user->id,
'resourcetype' => 'usr',
'resourceid' => $user->id,
'ownertype' => 'institution',
'ownerid' => $this->name,
'tag' => 'lastinstitution:' . strtolower($this->name),
'ctime' => db_format_timestamp(time()),
'editedby' => $USER->get('id'),
)
);
......
......@@ -3738,13 +3738,14 @@ function profile_sideblock() {
}
}
$typecast = is_postgres() ? '::varchar' : '';
$data['grouplimitstr'] = $limitstr;
$data['views'] = get_records_sql_array(
'SELECT v.id, v.title, v.urlid, v.owner
FROM {view} v
INNER JOIN {view_tag} vt ON (vt.tag = ? AND vt.view = v.id)
WHERE v.owner = ?
ORDER BY v.title',
"SELECT v.id, v.title, v.urlid, v.owner
FROM {view} v
INNER JOIN {tag} vt ON (vt.tag = ? AND vt.resourcetype = 'view' AND vt.resourceid = v.id" . $typecast . ")
WHERE v.owner = ?
ORDER BY v.title",
array(get_string('profile'), $USER->get('id'))
);
if ($data['views']) {
......@@ -3756,12 +3757,12 @@ function profile_sideblock() {
}
}
$data['artefacts'] = get_records_sql_array(
'SELECT a.id, a.artefacttype, a.title
"SELECT a.id, a.artefacttype, a.title
FROM {artefact} a
INNER JOIN {artefact_tag} at ON (a.id = at.artefact AND tag = ?)
INNER JOIN {tag} at ON (at.tag = ? AND at.resourcetype = 'artefact' AND at.resourceid = a.id" . $typecast . ")
WHERE a.owner = ?
ORDER BY a.title',
array(get_string('profile'), $USER->get('id'))
ORDER BY a.title",
array(get_string('profile'), $USER->get('id'))
);
if (!empty($data['artefacts'])) {
// check if we have any blogposts and fetch their blog id if we do
......@@ -3862,24 +3863,14 @@ function get_my_tags($limit=null, $cloud=true, $sort='freq') {
$sort = 't.tag ASC';
}
$tagrecords = get_records_sql_array("
SELECT
t.tag, COUNT(t.tag) AS count
FROM (
(SELECT at.tag, a.id, 'artefact' AS type
FROM {artefact_tag} at JOIN {artefact} a ON a.id = at.artefact
WHERE a.owner = ?)
UNION
(SELECT vt.tag, v.id, 'view' AS type
FROM {view_tag} vt JOIN {view} v ON v.id = vt.view
WHERE v.owner = ?)
UNION
(SELECT ct.tag, c.id, 'collection' AS type
FROM {collection_tag} ct JOIN {collection} c ON c.id = ct.collection
WHERE c.owner = ?)
) t
SELECT t.tag, COUNT(t.tag) AS count
FROM {tag} t
WHERE t.resourcetype IN ('artefact', 'view', 'collection')
AND t.ownertype = 'user'
AND t.ownerid = ?
GROUP BY t.tag
ORDER BY " . $sort . (is_null($limit) ? '' : " LIMIT $limit"),
array($id, $id, $id)
array($id)
);
if (!$tagrecords) {
return array();
......
......@@ -115,9 +115,9 @@ function change_language($userid, $oldlang, $newlang) {
safe_require('artefact', 'file');
ArtefactTypeFolder::change_language($userid, $oldlang, $newlang);
}
set_field_select('artefact_tag', 'tag', get_string_from_language($newlang, 'profile'), 'WHERE tag = ? AND artefact IN (SELECT id FROM {artefact} WHERE "owner" = ?)', array(get_string_from_language($oldlang, 'profile'), $userid));
set_field_select('view_tag', 'tag', get_string_from_language($newlang, 'profile'), 'WHERE tag = ? AND "view" IN (SELECT id FROM {view} WHERE "owner" = ?)', array(get_string_from_language($oldlang, 'profile'), $userid));
set_field_select('collection_tag', 'tag', get_string_from_language($newlang, 'profile'), 'WHERE tag = ? AND "collection" IN (SELECT id FROM {collection} WHERE "owner" = ?)', array(get_string_from_language($oldlang, 'profile'), $userid));
set_field_select('tag', 'tag', get_string_from_language($newlang, 'profile'), "WHERE tag = ? AND resourcetype = 'artefact' AND resourceid IN (SELECT id FROM {artefact} WHERE \"owner\" = ?)", array(get_string_from_language($oldlang, 'profile'), $userid));
set_field_select('tag', 'tag', get_string_from_language($newlang, 'profile'), "WHERE tag = ? AND resourcetype = 'view' AND resourceid IN (SELECT id FROM {view} WHERE \"owner\" = ?)", array(get_string_from_language($oldlang, 'profile'), $userid));
set_field_select('tag', 'tag', get_string_from_language($newlang, 'profile'), "WHERE tag = ? AND resourcetype = 'collection' AND resourceid IN (SELECT id FROM {collection} WHERE \"owner\" = ?)", array(get_string_from_language($oldlang, 'profile'), $userid));
}
/**
......
......@@ -706,7 +706,7 @@ class View {
public function get_tags() {
if (!isset($this->tags)) {
$this->tags = get_column('view_tag', 'tag', 'view', $this->get('id'));
$this->tags = get_column_sql('SELECT tag FROM {tag} WHERE resourcetype = ? AND resourceid = ? ORDER BY tag', array('view', $this->get('id')));
}
return $this->tags;
}
......@@ -753,6 +753,8 @@ class View {
* This method updates the contents of the view table only.
*/
public function commit() {
global $USER;
if (empty($this->dirty)) {
return;
}
......@@ -781,12 +783,34 @@ class View {
}
if (isset($this->tags)) {
$this->tags = check_case_sensitive($this->tags, 'view_tag');
delete_records('view_tag', 'view', $this->get('id'));
if ($this->group) {
$ownertype = 'group';
$ownerid = $this->group;
}
else if ($this->institution) {
$ownertype = 'institution';
$ownerid = $this->institution;
}
else {
$ownertype = 'user';
$ownerid = $this->owner;
}
$this->tags = check_case_sensitive($this->tags, 'tag');
delete_records('tag', 'resourcetype', 'view', 'resourceid', $this->get('id'));
foreach ($this->get_tags() as $tag) {
//truncate the tag before insert it into the database
$tag = substr($tag, 0, 128);
insert_record('view_tag', (object)array( 'view' => $this->get('id'), 'tag' => $tag));
insert_record('tag',
(object)array(
'resourcetype' => 'view',
'resourceid' => $this->get('id'),
'ownertype' => $ownertype,
'ownerid' => $ownerid,
'tag' => $tag,
'ctime' => db_format_timestamp(time()),
'editedby' => $USER->get('id'),
)
);
}
}
......@@ -881,7 +905,7 @@ class View {