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

Bug 1722861: Allow admins to delete institution tag

The institution/site admins can delete the
institution tags that have not been used
by any user.

If a tag has been used, instead of the
delete icon, the admins will see how
many times the tag has been used.

Change-Id: I29be28c2b62e278952412f9e648eba5dd9e0ed91
parent dce59fe6
......@@ -152,9 +152,44 @@ function institutiontag_validate(Pieform $form, $values) {
}
}
// Get the exiting institution tags.
$id = get_field('institution', 'id', 'name', $institution);
$tags = get_records_sql_array("SELECT * FROM {tag} t WHERE resourcetype = 'institution' AND resourceid = ?", array($id));
// Get the institution tags and their used status.
$typecast = is_postgres() ? '::varchar' : '';
$sql = "
SELECT id, tag, SUM(count) AS count
FROM (
SELECT id, tag, 0 AS count
FROM {tag}
WHERE resourcetype = 'institution'
AND ownertype = 'institution' AND ownerid IN (?)
UNION
SELECT t2.id, t2.tag AS tag, COUNT(t2.tag) AS count
FROM {tag} t
JOIN {tag} t2 ON t2.id" . $typecast . " = SUBSTRING(t.tag, 7)
JOIN {institution} i ON i.name = t2.ownerid
WHERE t.resourcetype IN ('artefact', 'view', 'collection') AND i.name = ?
GROUP BY 1, 2
) AS tags
GROUP BY tags.tag, tags.id
ORDER BY LOWER(tags.tag)";
$tags = get_records_sql_assoc($sql, array($institution, $institution));
// Delete tag.
$delete = param_integer('delete', null);
if ($delete) {
db_begin();
if (isset($tags[$delete]) && $tags[$delete]->count == 0 && delete_records_select('tag', " ownertype = 'institution' AND ownerid = ? AND id = ?", array($institution, $delete))) {
$SESSION->add_ok_msg(get_string('institutiontagdeleted'));
}
else {
$SESSION->add_error_msg(get_string('institutiontagdeletefail'));
}
db_commit();
redirect("/admin/users/institutiontags.php?institution=" . $institution);
}
if (!$tags) {
$tags = array();
}
$smarty = smarty(array('paginator'));
setpageicon($smarty, 'icon-university');
......
......@@ -219,15 +219,20 @@ $string['allowinstitutiontags'] = 'Allow institution tags';
$string['allowinstitutiontagsdescription'] = 'Administrators can define a set of tags that institution members can use.';
$string['cantlistinstitutiontags'] = 'You are not allowed to list institution tags.';
$string['createtag'] = 'Create tag';
$string['deleteinstitutiontag'] = 'Delete institution tag';
$string['editinstitutiontag'] = 'Edit institution tag';
$string['error:duplicatetag'] = 'Institutional tag already exists.';
$string['error:emptytag'] = 'Institutional tag cannot be empty.';
$string['institutiontagcantbesaved'] = 'Institution tag can\'t be saved.';
$string['institutiontag'] = 'Institution tag';
$string['institutiontagdesc'] = 'You can add one tag at a time';
$string['institutiontagdeleted'] = 'Institution tag deleted successfully.';
$string['institutiontagdeletefail'] = 'Failed to delete institution tag.';
$string['institutiontags'] = 'Institution tags';
$string['institutiontagsdescription'] = 'The institution tags are predefined by the institution and available for use by all institution members. You can create as many tags as you like.';
$string['institutiontagsaved'] = 'Institution tag saved.';
$string['notags'] = 'No tags for this institution.';
$string['timesused'] = 'Times used:';
// License metadata
$string['license'] = 'License';
......
{include file="header.tpl"}
{if !$canedit}<p>{str tag=cantlistinstitutiontags}</p>{/if}
{if $tags && !$new}
<p class="lead view-description">{str tag=institutiontagsdescription}</p>
{foreach $tags tag}
<div class="panel panel-default">
<div id="institutiontags" class="list-group">
<div id="institutiontags" class="list-group">
{foreach $tags tag}
<div class="list-group-item r0 ">
<div class="row">
<div class="col-md-9">
......@@ -12,12 +14,26 @@
{$tag->tag}
</h3>
</div>
<div class="col-md-3">
<div class="inner-link btn-action-list">
<div class="btn-top-right btn-group btn-group-top">
{if $tag->count <= 0}
<a href="{$WWWROOT}admin/users/institutiontags.php?delete={$tag->id}&institution={$institution}" title="{str tag=deleteinstitutiontag}" class="btn btn-default btn-xs">
<span class="icon icon-trash icon-lg text-danger" role="presentation" aria-hidden="true"></span>
<span class="sr-only">{str tag=deleteinstitutiontag}</span>
</a>
{else}
{str tag=timesused} {$tag->count}
{/if}
</div>
</div>
</div>
</div>
</div>
{/foreach}
</div>
</div>
</div>
{/foreach}
{$pagination|safe}
{if $pagination_js}
<script type="application/javascript">
......@@ -35,4 +51,4 @@
{/if}
{/if}
{include file="footer.tpl"}
\ No newline at end of file
{include file="footer.tpl"}
......@@ -35,6 +35,11 @@ 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 follow "Create tag"
And I set the field "Institution tag" to "Two tag"
And I press "Save"
And I follow "Delete institution tag" in "Two tag" row
Then I should see "Institution tag deleted successfully"
And I log out
Given I log in as "UserA" with password "Kupuh1pa!"
......
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