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

Bug 1722861: Add the create institution tag logic

1. Added the create tag form on the institution
tags page.
2. Store the isntitution tag in the "tag" table.

Change-Id: I837ec4137758d28d8a42c7629433b92f04a82600
parent b156f2e2
......@@ -20,6 +20,7 @@ require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once(get_config('libroot') . 'institution.php');
$institution = param_alphanum('institution', false);
$new = param_boolean('new', 0);
// Get all the institutions that the current user has access to.
$institutionelement = get_institution_selector(true, false, false, false, false, true);
......@@ -55,8 +56,115 @@ $(document).ready(function() {
});
EOF;
// Check if user is a institution admin
$canedit = $USER->get('admin') || $USER->is_institutional_admin();
if (!$canedit) {
throw new AccessDeniedException(get_string('cantlistinstitutiontags'));
}
// Building the new tag form.
$elements = array(
'tag' => array(
'type' => 'text',
'defaultvalue' => null,
'title' => get_string('institutiontag'),
'size' => 30,
'description' => get_string('institutiontagdesc'),
'rules' => array(
'required' => true,
),
),
'submit' => array(
'type' => 'submitcancel',
'class' => 'btn-primary',
'value' => array(get_string('save'), get_string('cancel')),
'confirm' => null,
)
);
$form = pieform(array(
'name' => 'institutiontag',
'plugintype' => 'core',
'pluginname' => 'tags',
'elements' => $elements,
));
/**
* Submit the new institution tag form
*
* @param Pieform $form The form to submit
* @param array $values The values submitted
*/
function institutiontag_submit(Pieform $form, $values) {
global $SESSION, $institution, $USER;
$id = insert_record('tag',
(object) array(
'resourcetype' => 'institution',
'resourceid' => get_field('institution', 'id', 'name', $institution),
'ownertype' => 'institution',
'ownerid' => $institution,
'tag' => $values['tag'],
'ctime' => db_format_timestamp(time()),
'mtime' => db_format_timestamp(time()),
'editedby' => $USER->id,
), 'id', true);
if ($id) {
$SESSION->add_ok_msg(get_string('institutiontagsaved'));
}
else {
$SESSION->add_error_msg(get_string('institutiontagcantbesaved'));
}
redirect("/admin/users/institutiontags.php?institution={$institution}");
}
/**
* Cancel the submission of the new institution tag form.
*/
function institutiontag_cancel_submit() {
global $institution;
redirect("/admin/users/institutiontags.php?institution={$institution}");
}
/**
* Validate the submitted data from the new institution tag form. New tags must not:
* - be empty strings
* - match an existing tag within the institution
*
* @param Pieform $form The form to validate
* @param array $values The values submitted
*/
function institutiontag_validate(Pieform $form, $values) {
global $institution;
// Don't even start attempting to parse if there are previous errors
if ($form->has_errors()) {
return;
}
if (empty(trim($values['tag'])) || trim($values['tag']) === '') {
$form->set_error('tag', get_string('error:emptytag'));
return;
}
$id = get_field('institution', 'id', 'name', $institution);
if (record_exists('tag', 'resourcetype', 'institution', 'resourceid', $id, 'tag', $values['tag'])) {
$form->set_error('tag', get_string('error:duplicatetag'));
return;
}
}
// 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));
$smarty = smarty(array('paginator'));
setpageicon($smarty, 'icon-university');
$smarty->assign('institutionselector', $institutionselector);
$smarty->assign('INLINEJAVASCRIPT', $js);
$smarty->assign('canedit', $canedit);
$smarty->assign('institution', $institution);
$smarty->assign('new', $new);
$smarty->assign('form', $form);
$smarty->assign('tags', $tags);
$smarty->assign('SUBPAGETOP', 'admin/users/institutiontagsactions.tpl');
$smarty->assign('addonelink', get_config('wwwroot') . "admin/users/institutiontags.php?new=1&institution={$institution}");
$smarty->display('admin/users/institutiontags.tpl');
......@@ -217,8 +217,17 @@ $string['resultsperpage'] = 'Results per page';
// Institution tags
$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['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['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.';
// License metadata
$string['license'] = 'License';
......
......@@ -97,6 +97,7 @@ class BehatDataGenerators extends BehatBase {
'staff' => 'text',
'admins' => 'text',
'authname' => 'string',
'tags' => 'bool',
),
'required' => array('name', 'displayname')
),
......
{include file="header.tpl"}
<p class="lead view-description">{str tag=institutiontagsdescription}</p>
{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">
<div class="list-group-item r0 ">
<div class="row">
<div class="col-md-9">
<h3 class="title list-group-item-heading" title="{$tag->tag}">
{$tag->tag}
</h3>
</div>
</div>
</div>
</div>
</div>
</div>
{/foreach}
{$pagination|safe}
{if $pagination_js}
<script type="application/javascript">
{$pagination_js|safe}
</script>
{/if}
{else}
{if $new}
{$form|safe}
{else}
<p class="lead view-description">{str tag=institutiontagsdescription}</p>
<p class="no-results">
{str tag=notags}{if $addonelink} <a href={$addonelink}>{str tag=addone}</a>{/if}
</p>
{/if}
{/if}
{include file="footer.tpl"}
\ No newline at end of file
{if $canedit && !$new}
<div class="btn-group btn-top-right btn-group-top {if $GROUP}pagetabs{/if}">
<a class="btn btn-default" href="{$WWWROOT}admin/users/institutiontags.php?new=1&institution={$institution}">
<span class="icon icon-plus icon-lg left" role="presentation" aria-hidden="true"></span>
{str tag=createtag}</a>
</div>
{/if}
@javascript @core @core_institution @core_administration
Feature: Create Institution tags
In order to create institution tags
As an admin I need to go to Institution tags page
So I can add institution tags
Background:
Given the following "institutions" exist:
| name | displayname | registerallowed | registerconfirm | tags |
| instone | Institution One | ON | OFF | 1 |
Scenario: Creating institution tags
# Log in as "Admin" user
Given I log in as "admin" with password "Kupuh1pa!"
# Creating an Institution
And I choose "Tags" in "Institutions" from administration menu
And I follow "Create tag"
And I set the field "Institution tag" to "One tag"
And I press "Save"
Then I should see "Institution tag saved"
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