Commit 2bec90d0 authored by Cecilia Vela Gurovic's avatar Cecilia Vela Gurovic Committed by Robert Lyon
Browse files

Bug 1800321: Use institution tags in institution content

behatnotneeded

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