Commit fe026d2f authored by Maria Sorica's avatar Maria Sorica Committed by Robert Lyon
Browse files

Bug 1722861: Select and save institution tags

The user can tag artefacts, collections and views
with the institution tags (if he is part of that
institution).

The institution name appears in front of
the institution tag to differentiate itself
from the other tags.

Change-Id: I6b71ed160ecf20fb7813bf127ad34b30432ba7af
parent e11863f8
......@@ -270,7 +270,25 @@ $elements['externalauthjs'] = array(
'value' => $js,
);
$tags = get_column_sql('SELECT tag FROM {tag} WHERE resourcetype = ? AND resourceid = ? AND NOT tag ' . db_ilike() . " 'lastinstitution:%'", array('usr', $user->id));
$tags = array();
if ($tagsarray = get_records_sql_array("SELECT t.tag, t.prefix, t.ownerid
FROM (
SELECT ut.tag, NULL AS prefix, 0 AS ownerid
FROM {tag} ut
WHERE resourcetype = ? AND resourceid = ? AND ownertype <> 'instituion'
AND NOT tag " . db_ilike() . " 'lastinstitution:%'
UNION
SELECT it.tag, it.ownerid AS prefix, i.id AS ownerid
FROM {tag} it
JOIN {institution} i ON i.name = it.ownerid
WHERE resourcetype = ? AND resourceid = ?
AND tag " . db_ilike() . " 'lastinstitution:%'
) t
GROUP BY t.tag, t.prefix, t.ownerid", array('usr', $user->id, 'usr', $user->id))) {
foreach ($tagsarray as $k => $v) {
$tags[] = $v->tag;
}
}
$elements['tags'] = array(
'defaultvalue' => $tags,
......@@ -610,6 +628,9 @@ function edituser_site_submit(Pieform $form, $values) {
if (empty($tag)) {
continue;
}
if ($tagid = get_field('tag', 'resourceid', 'resourcetype', 'institution', 'tag', $tag)) {
$tag = 'tagid_' . $tagid;
}
insert_record(
'tag',
(object) array(
......@@ -617,7 +638,7 @@ function edituser_site_submit(Pieform $form, $values) {
'resourceid' => $user->id,
'ownertype' => 'user',
'ownerid' => $user->id,
'tag' => strtolower($tag),
'tag' => $tag,
'ctime' => db_format_timestamp(time()),
'editedby' => $USER->get('id'),
)
......
......@@ -653,10 +653,14 @@ abstract class ArtefactType implements IArtefactType {
$ownerid = $this->owner;
}
$this->tags = check_case_sensitive($this->tags, 'tag');
foreach (array_unique($this->tags) as $tag) {
if (empty($tag)) {
continue;
}
if ($institutiontag = get_record('tag', 'tag', $tag, 'resourcetype', 'institution', 'ownertype', 'institution')) {
$tag = 'tagid_' . $institutiontag->id;
}
insert_record('tag',
(object) array(
'resourcetype' => 'artefact',
......@@ -1233,8 +1237,18 @@ abstract class ArtefactType implements IArtefactType {
if (empty($artefactids)) {
return array();
}
$typecast = is_postgres() ? '::varchar' : '';
$artefactids = join("','", array_map('intval', $artefactids));
$tags = get_records_sql_array("SELECT tag, resourceid FROM {tag} WHERE resourcetype = 'artefact' AND resourceid IN ('" . $artefactids . "')");
$tags = get_records_sql_array("
SELECT
(CASE
WHEN t.tag LIKE 'tagid_%' THEN CONCAT(i.displayname, ': ', t2.tag)
ELSE t.tag
END) AS tag, t.resourceid
FROM {tag} t
LEFT JOIN {tag} t2 ON t2.id" . $typecast . " = SUBSTRING(t.tag, 7)
LEFT JOIN {institution} i ON i.name = t2.ownerid
WHERE t.resourcetype = 'artefact' AND t.resourceid IN ('" . $artefactids . "')");
if (!$tags) {
return array();
}
......@@ -1249,8 +1263,18 @@ abstract class ArtefactType implements IArtefactType {
ORDER BY a.title', array($this->id));
// load tags
$typecast = is_postgres() ? '::varchar' : '';
if ($list) {
$tags = get_records_sql_array("SELECT tag, resourceid FROM {tag} WHERE resourcetype = 'artefact' AND resourceid IN ('" . join("','", array_keys($list)) . "')");
$tags = get_records_sql_array("
SELECT
(CASE
WHEN t.tag LIKE 'tagid_%' THEN CONCAT(i.displayname, ': ', t2.tag)
ELSE t.tag
END) AS tag, t.resourceid
FROM {tag} t
LEFT JOIN {tag} t2 ON t2.id" . $typecast . " = SUBSTRING(t.tag, 7)
LEFT JOIN {institution} i ON i.name = t2.ownerid
WHERE t.resourcetype = 'artefact' AND t.resourceid IN ('" . join("','", array_keys($list)) . "')");
if ($tags) {
foreach ($tags as $t) {
$list[$t->resourceid]->tags[] = $t->tag;
......@@ -1356,7 +1380,18 @@ abstract class ArtefactType implements IArtefactType {
if (empty($id)) {
return array();
}
$tags = get_column_sql('SELECT tag FROM {tag} WHERE resourcetype = ? AND resourceid = ? ORDER BY tag', array('artefact', $id));
$typecast = is_postgres() ? '::varchar' : '';
$tags = get_column_sql("
SELECT
(CASE
WHEN t.tag LIKE 'tagid_%' THEN CONCAT(i.displayname, ': ', t2.tag)
ELSE t.tag
END) AS tag
FROM {tag} t
LEFT JOIN {tag} t2 ON t2.id" . $typecast . " = SUBSTRING(t.tag, 7)
LEFT JOIN {institution} i ON i.name = t2.ownerid
WHERE t.resourcetype = ? AND t.resourceid = ?
ORDER BY tag", array('artefact', $id));
if (!$tags) {
return array();
}
......
......@@ -214,6 +214,9 @@ class Collection {
foreach ($tags as $tag) {
//truncate the tag before insert it into the database
$tag = substr($tag, 0, 128);
if ($institutiontag = get_record('tag', 'tag', $tag, 'resourcetype', 'institution', 'ownertype', 'institution')) {
$tag = 'tagid_' . $institutiontag->id;
}
insert_record('tag',
(object)array(
'resourcetype' => 'collection',
......@@ -1355,7 +1358,18 @@ class Collection {
*/
public function get_tags() {
if (!isset($this->tags)) {
$this->tags = get_column_sql('SELECT tag FROM {tag} WHERE resourcetype = ? AND resourceid = ? ORDER BY tag', array('collection', $this->get('id')));
$typecast = is_postgres() ? '::varchar' : '';
$this->tags = get_column_sql("
SELECT
(CASE
WHEN t.tag LIKE 'tagid_%' THEN CONCAT(i.displayname, ': ', t2.tag)
ELSE t.tag
END) AS tag
FROM {tag} t
LEFT JOIN {tag} t2 ON t2.id" . $typecast . " = SUBSTRING(t.tag, 7)
LEFT JOIN {institution} i ON i.name = t2.ownerid
WHERE t.resourcetype = ? AND t.resourceid = ?
ORDER BY tag", array('collection', $this->get('id')));
}
return $this->tags;
}
......
......@@ -18,7 +18,6 @@ require_once(get_config('docroot') . 'lib/form/elements/autocomplete.php');
* @return string The HTML for the element
*/
function pieform_element_tags(Pieform $form, $element) {
$newelement = array(
'type' => 'autocomplete',
'title' => $element['title'],
......@@ -42,6 +41,15 @@ function translate_tags_to_names(array $ids) {
global $USER;
// for an empty list, the element '' is transmitted
$ids = array_diff($ids, array(''));
$ids = array_map(function($a) {
if (strpos($a, ':')) {
$arr = explode(': ', $a);
return $arr[1];
}
return $a;
}, $ids);
$results = array();
$alltags = get_all_tags_for_user();
......@@ -65,11 +73,15 @@ function translate_tags_to_names(array $ids) {
* @return $tag Formatted tag
*/
function display_tag($name, $alltags) {
return $name . ' (' . $alltags[$name]->count . ')';
if ($alltags[$name]->prefix && !empty($alltags[$name]->prefix)) {
$prefix = $alltags[$name]->prefix;
return $prefix . ': '. $alltags[$name]->tag . ' (' . $alltags[$name]->count . ')';
}
return $alltags[$name]->tag . ' (' . $alltags[$name]->count . ')';
}
/**
* Get all tags created by this user
* Get all tags available for this user
*
* @param string $query Search option
* @param int $limit
......@@ -85,32 +97,42 @@ function get_all_tags_for_user($query = null, $limit = null, $offset = null) {
if ($USER->get('admin')) {
$usertags = "
UNION ALL
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";
SELECT tag, COUNT(*) AS count, 'lastinstitution' AS prefix 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 {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";
SELECT tag, COUNT(*) AS count, 'lastinstitution' AS prefix 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);
$values = array($userid, $userid);
$querystr = '';
if ($query) {
$querystr = " WHERE tag LIKE '%' || ? || '%'";
$values[] = $query;
}
$sql = "
SELECT tag, SUM(count) AS count
SELECT tag, SUM(count) AS count, prefix
FROM (
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 {tag} t INNER JOIN {view} v ON (t.resourcetype = 'view' AND t.resourceid = v.id" . $typecast . ") WHERE v.owner=? GROUP BY 1
SELECT
(CASE
WHEN t.tag LIKE 'tagid_%' THEN t2.tag
ELSE t.tag
END) AS tag, COUNT(*) AS count, i.displayname AS prefix
FROM {tag} t
LEFT JOIN {tag} t2 ON t2.id" . $typecast . " = SUBSTRING(t.tag, 7)
LEFT JOIN {institution} i ON i.name = t2.ownerid
WHERE t.ownerid=? AND t.resourcetype IN ('artefact', 'view', 'collection')
GROUP BY 1, 3
UNION ALL
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
SELECT t.tag, 0 AS count, i.displayname AS prefix
FROM {tag} t
JOIN {institution} i ON i.name = t.ownerid AND i.tags = 1
JOIN {usr_institution} ui ON ui.institution = i.name AND ui.usr = ?
" . $usertags . "
) tags
" . $querystr . "
GROUP BY tag
GROUP BY tag, prefix
ORDER BY LOWER(tag)
";
$result = get_records_sql_assoc($sql, $values, $offset, $limit);
......
......@@ -1822,11 +1822,13 @@ function load_user_institutions($userid) {
$table = new XMLDBTable('institution');
$field = new XMLDBField('logoxs');
$logoxs = field_exists($table, $field) ? ',i.logoxs' : '';
$field = new XMLDBField('tags');
$tags = field_exists($table, $field) ? ',i.tags' : '';
if ($userid !== 0 && $institutions = get_records_sql_assoc('
SELECT u.institution, ' . db_format_tsfield('ctime') . ',' . db_format_tsfield('u.expiry', 'membership_expiry') . ',
u.studentid, u.staff, u.admin, i.displayname, i.theme, i.registerallowed, i.showonlineusers,
i.allowinstitutionpublicviews, i.logo' . $logoxs . ', i.style, i.licensemandatory, i.licensedefault,
i.dropdownmenu, i.skins, i.suspended
i.dropdownmenu, i.skins, i.suspended' . $tags . '
FROM {usr_institution} u INNER JOIN {institution} i ON u.institution = i.name
WHERE u.usr = ? ORDER BY i.priority DESC', array($userid))) {
return $institutions;
......
......@@ -706,7 +706,18 @@ class View {
public function get_tags() {
if (!isset($this->tags)) {
$this->tags = get_column_sql('SELECT tag FROM {tag} WHERE resourcetype = ? AND resourceid = ? ORDER BY tag', array('view', $this->get('id')));
$typecast = is_postgres() ? '::varchar' : '';
$this->tags = get_column_sql("
SELECT
(CASE
WHEN t.tag LIKE 'tagid_%' THEN CONCAT(i.displayname, ': ', t2.tag)
ELSE t.tag
END) AS tag
FROM {tag} t
LEFT JOIN {tag} t2 ON t2.id" . $typecast . " = SUBSTRING(t.tag, 7)
LEFT JOIN {institution} i ON i.name = t2.ownerid
WHERE t.resourcetype = ? AND t.resourceid = ?
ORDER BY tag", array('view', $this->get('id')));
}
return $this->tags;
}
......@@ -800,6 +811,9 @@ class View {
foreach ($this->get_tags() as $tag) {
//truncate the tag before insert it into the database
$tag = substr($tag, 0, 128);
if ($institutiontag = get_record('tag', 'tag', $tag, 'resourcetype', 'institution', 'ownertype', 'institution')) {
$tag = 'tagid_' . $institutiontag->id;
}
insert_record('tag',
(object)array(
'resourcetype' => 'view',
......
......@@ -9,6 +9,14 @@ Background:
| name | displayname | registerallowed | registerconfirm | tags |
| instone | Institution One | ON | OFF | 1 |
And the following "users" exist:
| username | password | email | firstname | lastname | institution | authname | role |
| UserA | Kupuh1pa! | UserA@example.org | Angela | User | instone | internal | member |
And the following "pages" exist:
| title | description | ownertype | ownername |
| Page UserA_01 | Page 01| user | UserA |
Scenario: Creating institution tags
# Log in as "Admin" user
Given I log in as "admin" with password "Kupuh1pa!"
......@@ -19,3 +27,13 @@ Scenario: Creating institution tags
And I set the field "Institution tag" to "One tag"
And I press "Save"
Then I should see "Institution tag saved"
And I log out
Given I log in as "UserA" with password "Kupuh1pa!"
And I choose "Pages and collections" in "Portfolio" from main menu
And I click on "Edit" in "Page UserA_01" panel menu
And I follow "Settings" in the "Toolbar buttons" property
And I fill in select2 input "settings_tags" with "One tag" and select "Institution One: One tag (0)"
And I press "Save"
And I follow "Display page"
Then I should see "Institution One: One tag"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment