Commit d946010e authored by Cecilia Vela Gurovic's avatar Cecilia Vela Gurovic Committed by Robert Lyon

Bug 1800321: Use institution tags in institution content

behatnotneeded

Change-Id: I26ebb888908a8be5ba96057920f73c5507e3bbc9
(cherry picked from commit 1fa6bfc5)
parent 1ae4cfca
......@@ -89,6 +89,7 @@ $form = array(
'title' => get_string('tags'),
'description' => get_string('tagsdescprofile'),
'help' => true,
'institution' => $institutionname,
),
'license' => license_form_el_basic(null),
'licensing_advanced' => license_form_el_advanced(null),
......
......@@ -143,6 +143,7 @@ $form = pieform(array(
'title' => get_string('tags'),
'description' => get_string('tagsdesc'),
'help' => true,
'institution' => $institutionname,
),
'license' => license_form_el_basic(isset($blogpostobj) ? $blogpostobj : null),
'licensing_advanced' => license_form_el_advanced(isset($blogpostobj) ? $blogpostobj : null),
......
......@@ -340,7 +340,8 @@ var FileBrowser = (function($) {
if (self.filedata[id].tags) {
for (var x in self.filedata[id].tags) {
var option = document.createElement("option");
option.text = option.value = self.filedata[id].tags[x];
option.text = self.filedata[id].tags[x];
option.value = x;
option.selected = "selected";
$('#' + self.id + '_edit_tags').append(option);
}
......@@ -408,6 +409,7 @@ var FileBrowser = (function($) {
'sesskey': self.config.sesskey,
'offset': 0,
'limit': 10,
'institution': $('#institutionselect_institution').val(),
};
},
processResults: function(data, page) {
......
......@@ -721,8 +721,11 @@ abstract class ArtefactTypeFileBase extends ArtefactType {
LEFT JOIN {institution} i ON i.name = t2.ownerid
WHERE t.resourcetype = 'artefact' AND t.resourceid IN (" . $tagwhere . ")");
if ($tags) {
require_once(get_config('docroot') . 'lib/form/elements/tags.php');
$alltags = get_all_tags_for_user();
foreach ($tags as $t) {
$filedata[$t->resourceid]->tags[] = $t->tag;
$tagname = remove_prefix($t->tag);
$filedata[$t->resourceid]->tags[$tagname] = display_tag($tagname, $alltags['tags']);
}
}
$where = 'artefact IN (' . join(',', array_keys($filedata)) . ')';
......
......@@ -21,13 +21,14 @@ $page = param_integer('page');
if ($page < 1) {
$page = 1;
}
$institution = param_variable('institution', null);
$tagsperpage = 10;
$more = true;
$tmptag = array();
while ($more && count($tmptag) < $tagsperpage) {
$tags = get_all_tags_for_user($request, $tagsperpage, $tagsperpage * ($page - 1));
$tags = get_all_tags_for_user($request, $tagsperpage, $tagsperpage * ($page - 1), $institution);
$more = $tags['count'] > $tagsperpage * $page;
if (!$tags['tags']) {
......
......@@ -565,6 +565,7 @@ class Collection {
'description' => get_string('tagsdescprofile'),
'defaultvalue' => null,
'help' => true,
'institution' => $this->institution,
),
'navigation' => array(
'type' => 'switchbox',
......
......@@ -121,6 +121,7 @@ function pieform_element_autocomplete(Pieform $form, $element) {
if (isset($element['description'])) {
$smarty->assign('describedby', $form->element_descriptors($element));
}
$smarty->assign('institution', $element['institution']);
return $smarty->fetch('form/autocomplete.tpl');
}
......
......@@ -1277,6 +1277,8 @@ function pieform_element_filebrowser_update(Pieform $form, $element, $data) {
$newtags = $data['tags'];
$updatetags = $oldtags != $newtags;
if ($updatetags) {
require_once(get_config('docroot') . 'lib/form/elements/tags.php');
$newtags = array_map('remove_prefix', $newtags);
$artefact->set('tags', $newtags);
}
......
......@@ -33,6 +33,7 @@ function pieform_element_tags(Pieform $form, $element) {
'ajaxextraparams' => array(),
'extraparams' => array('tags' => true),
'width' => '280px',
'institution' => isset($element['institution']) ? $element['institution'] : null,
);
return pieform_element_autocomplete($form, $newelement);
}
......@@ -46,11 +47,19 @@ function translate_tags_to_names(array $ids) {
$institutions = array_keys($institutions);
// Fetch valid institution tags
$validinstitutiontags = get_column_sql("SELECT tag FROM {tag}
WHERE ownertype = 'institution' AND ownerid IN ('" . join("','", $institutions) . "')");
WHERE ownertype = 'institution'
ANd resourcetype = 'institution'
AND ownerid IN ('" . join("','", $institutions) . "')");
}
else if ($USER->get('admin')) {
$validinstitutiontags = get_column_sql("SELECT tag FROM {tag}
WHERE ownertype = 'institution'
ANd resourcetype = 'institution'");
}
else {
$validinstitutiontags = array();
}
$ids = array_map(function($a) use ($validinstitutiontags) {
if (strpos($a, ': ')) {
if (in_array($a, $validinstitutiontags)) {
......@@ -65,11 +74,14 @@ function translate_tags_to_names(array $ids) {
$alltags = get_all_tags_for_user();
foreach ($ids as $id) {
if (isset($alltags['tags'][$id])) {
$results[] = (object) array('id' => $id, 'text' => display_tag($id, $alltags['tags']));
// if institution tag, we need to remove the prefix
$tagname = remove_prefix($id);
if (isset($alltags['tags'][$tagname])) {
$results[] = (object) array('id' => $tagname, 'text' => display_tag($tagname, $alltags['tags']));
}
else {
$results[] = (object) array('id' => $id, 'text' => $id);
$results[] = (object) array('id' => $tagname, 'text' => $tagname);
}
}
return $results;
......@@ -91,21 +103,60 @@ function display_tag($name, $alltags) {
return $alltags[$name]->tag . ' (' . $alltags[$name]->count . ')';
}
/**
* Return a tag name without institution prefix if it has one
* @param string $tagname the tag name
* @return string Institution tag without prefix or same tagname if it not an institution tag
*/
function remove_prefix($tagname) {
$institutions = get_column_sql('SELECT displayname FROM {institution} WHERE name != ?', array('mahara'));
foreach ($institutions as $institution) {
$prefix = $institution . ': ';
if (substr($tagname, 0, strlen($prefix)) == $prefix) {
$tagname = substr($tagname, strlen($prefix));
break;
}
}
return $tagname;
}
/**
* Get all tags available for this user
*
* @param string $query Search option
* @param int $limit
* @param int $offset
* @param string $institution name
* @retun array $tags The tags this user has created
*/
function get_all_tags_for_user($query = null, $limit = null, $offset = null) {
function get_all_tags_for_user($query = null, $limit = null, $offset = null, $institution = null) {
global $USER;
if ($USER->is_logged_in()) {
$usertags = "";
$userid = $USER->get('id');
$typecast = is_postgres() ? '::varchar' : '';
$values = array($userid, $userid);
// get all the institution tags the user can use
$values = array($userid);
if ($USER->get('admin') && isset($institution)) {
$values[] = $institution;
$insttagsforuser = "
UNION ALL
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 AND t.resourcetype='institution' AND i.name = ?
WHERE t.resourcetype != 'usr'";
}
else {
$values[] = $userid;
$insttagsforuser = "
UNION ALL
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 AND t.resourcetype='institution'
JOIN {usr_institution} ui ON ui.institution = i.name AND ui.usr = ?
WHERE t.resourcetype != 'usr'";
}
$querystr = '';
if ($query) {
$querystr = " WHERE tag " . db_ilike() . " '%' || ? || '%'";
......@@ -115,6 +166,8 @@ function get_all_tags_for_user($query = null, $limit = null, $offset = null) {
$querystr .= " OR prefix " . db_ilike() . " '%' || ? || '%'";
$values[] = $query;
}
// get all the tags the logged in user already has
$sql = "
SELECT tag, SUM(count) AS count, prefix
FROM (
......@@ -125,15 +178,10 @@ function get_all_tags_for_user($query = null, $limit = null, $offset = null) {
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', 'blocktype')
LEFT JOIN {institution} i ON i.name = t2.ownerid AND t2.resourcetype='institution'
WHERE t.editedby=? AND t.resourcetype IN ('artefact', 'view', 'collection', 'blocktype')
GROUP BY 1, 3
UNION ALL
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 = ?
WHERE t.resourcetype != 'usr'
" . $insttagsforuser . "
) tags
" . $querystr . "
GROUP BY tag, prefix
......@@ -141,6 +189,7 @@ function get_all_tags_for_user($query = null, $limit = null, $offset = null) {
";
$result = get_records_sql_assoc($sql, $values, $offset, $limit);
}
$results = !empty($result) ? $result : array();
$return = array('tags' => $results,
'count' => count($results),
......
......@@ -4917,6 +4917,14 @@ function generate_csv($data, $csvfields, $csvheaders = array()) {
function check_if_institution_tag($tag) {
global $USER;
$institutions = $USER->get('institutions');
if ($USER->get('admin') && $institutiontags = get_records_sql_array("
SELECT id FROM {tag}
WHERE tag = ?
AND resourcetype = ?
AND ownertype = ?",
array($tag, 'institution', 'institution'))) {
$tag = 'tagid_' . $institutiontags[0]->id;
}
if ($institutions && $institutiontags = get_records_sql_array("
SELECT id FROM {tag}
WHERE tag = ?
......
......@@ -23,6 +23,9 @@
'sesskey': "{{$sesskey}}",
'offset': 0,
'limit': 10,
{{if $institution}}
'institution': "{{$institution}}",
{{/if}}
}
},
processResults: function(data, page) {
......
......@@ -282,6 +282,7 @@ function get_basic_elements() {
'description' => get_string('tagsdescprofile'),
'defaultvalue' => $view->get('tags'),
'help' => true,
'institution' => $institution,
)
);
if (!($group || $institution) && $createtagsoptions) {
......
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