Commit f705b13a authored by Son Nguyen's avatar Son Nguyen Committed by Son Nguyen

Add tagging feature for collections (Bug #1180997)

Change-Id: I33ce7dc6e6c14917c9632afc672c0faf5718647b
parent 0d8c1289
......@@ -88,6 +88,10 @@ function edit_tag_submit(Pieform $form, $values) {
"UPDATE {view_tag} SET tag = ? WHERE tag = ? AND \"view\" IN (SELECT id FROM {view} WHERE \"owner\" = ?)",
array($values['tagname'], $tag, $userid)
);
execute_sql(
"UPDATE {collection_tag} SET tag = ? WHERE tag = ? AND \"collection\" IN (SELECT id FROM {collection} WHERE \"owner\" = ?)",
array($values['tagname'], $tag, $userid)
);
execute_sql(
"UPDATE {artefact_tag} SET tag = ? WHERE tag = ? AND artefact IN (SELECT id FROM {artefact} WHERE \"owner\" = ?)",
array($values['tagname'], $tag, $userid)
......@@ -107,6 +111,10 @@ function delete_tag_submit(Pieform $form, $values) {
"DELETE FROM {view_tag} WHERE tag = ? AND view IN (SELECT id FROM {view} WHERE \"owner\" = ?)",
array($tag, $userid)
);
execute_sql(
"DELETE FROM {collection_tag} WHERE tag = ? AND collection IN (SELECT id FROM {collection} WHERE \"owner\" = ?)",
array($tag, $userid)
);
execute_sql(
"DELETE FROM {artefact_tag} WHERE tag = ? AND artefact IN (SELECT id FROM {artefact} WHERE \"owner\" = ?)",
array($tag, $userid)
......
......@@ -225,12 +225,20 @@ class PluginExportLeap extends PluginExport {
$this->smarty->assign('content', $collection->get('description'));
$this->smarty->assign('leaptype', 'selection');
$this->smarty->assign('categories', array(
array(
'scheme' => 'selection_type',
'term' => 'Website',
)
));
$tags = $collection->get('tags');
if ($tags) {
$tags = array_map(function ($a) {
return array(
'term' => LeapExportElement::normalise_tag($a),
'label' => $a
);}, $tags);
}
$this->smarty->assign('categories', array_merge(array(
array(
'scheme' => 'selection_type',
'term' => 'Website',
)
), $tags));
$links = array();
if (!empty($this->links->collectionview[$id])) {
......@@ -288,11 +296,11 @@ class PluginExportLeap extends PluginExport {
$tags = array();
if ($config['tags']) {
$tags = array_map(create_function('$a',
'return array(
\'term\' => LeapExportElement::normalise_tag($a),
\'label\' => $a
);'), $config['tags']);
$tags = array_map(function ($a) {
return array(
'term' => LeapExportElement::normalise_tag($a),
'label' => $a
);}, $config['tags']);
}
$this->smarty->assign('categories', array_merge(array(
array(
......
......@@ -725,6 +725,7 @@ class PluginImportLeap extends PluginImport {
$collectiondata = array(
'name' => (string)$entry->title,
'description' => (string)$entry->summary,
'tags' => self::get_entry_tags($entry),
'owner' => $this->get('usr'),
);
if ($published = strtotime((string)$entry->published)) {
......
......@@ -31,6 +31,7 @@ require(dirname(dirname(__FILE__)) . '/init.php');
if ($USER->is_logged_in()) {
$usertags = "";
$userid = $USER->get('id');
if ($USER->get('admin')) {
$usertags = "
UNION ALL
......@@ -47,13 +48,15 @@ if ($USER->is_logged_in()) {
FROM (
SELECT tag,COUNT(*) AS count FROM {artefact_tag} t INNER JOIN {artefact} a ON t.artefact=a.id WHERE a.owner=? GROUP BY 1
UNION ALL
SELECT tag,COUNT(*) AS count FROM {view_tag} t INNER JOIN {view} v ON t.view=v.id WHERE v.owner=? GROUP BY 1" .
$usertags . "
SELECT tag,COUNT(*) AS count FROM {view_tag} t INNER JOIN {view} v ON t.view=v.id WHERE v.owner=? GROUP BY 1
UNION ALL
SELECT tag,COUNT(*) AS count FROM {collection_tag} t INNER JOIN {collection} c ON t.collection=c.id WHERE c.owner=? GROUP BY 1
" . $usertags . "
) tags
GROUP BY tag
ORDER BY LOWER(tag)
",
array($USER->get('id'), $USER->get('id'))
array($userid, $userid, $userid)
);
}
......
......@@ -137,6 +137,7 @@ $string['tagfilter_file'] = 'Files';
$string['tagfilter_image'] = 'Images';
$string['tagfilter_text'] = 'Text';
$string['tagfilter_view'] = 'Pages';
$string['tagfilter_collection'] = 'Collections';
$string['edittags'] = 'Edit tags';
$string['selectatagtoedit'] = 'Select a tag to edit';
$string['edittag'] = 'Edit <a href="%s">%s</a>';
......
......@@ -42,6 +42,7 @@ class Collection {
private $submittedhost;
private $submittedtime;
private $views;
private $tags;
public function __construct($id=0, $data=null) {
......@@ -81,6 +82,9 @@ class Collection {
if (!property_exists($this, $field)) {
throw new InvalidArgumentException("Field $field wasn't found in class " . get_class($this));
}
if ($field == 'tags') {
return $this->get_tags();
}
if ($field == 'views') {
return $this->views();
}
......@@ -138,6 +142,7 @@ class Collection {
}
delete_records('collection_view','collection',$this->id);
delete_records('collection_tag','collection',$this->id);
delete_records('collection','id',$this->id);
// Secret url records belong to the collection, so remove them from the view.
......@@ -175,6 +180,15 @@ class Collection {
}
}
if (isset($this->tags)) {
delete_records('collection_tag', 'collection', $this->get('id'));
foreach ($this->get_tags() as $tag) {
//truncate the tag before insert it into the database
$tag = substr($tag, 0, 128);
insert_record('collection_tag', (object)array( 'collection' => $this->get('id'), 'tag' => $tag));
}
}
db_commit();
}
......@@ -243,6 +257,7 @@ class Collection {
$data->name = $collectiondata['name'];
}
$data->description = $colltemplate->get('description');
$data->tags = $colltemplate->get('tags');
$data->navigation = $colltemplate->get('navigation');
if (!empty($collectiondata['group'])) {
$data->group = $collectiondata['group'];
......@@ -430,6 +445,13 @@ class Collection {
'defaultvalue' => null,
'title' => get_string('description', 'collection'),
),
'tags' => array(
'type' => 'tags',
'title' => get_string('tags'),
'description' => get_string('tagsdescprofile'),
'defaultvalue' => null,
'help' => true,
),
'navigation' => array(
'type' => 'checkbox',
'title' => get_string('viewnavigation','collection'),
......@@ -441,7 +463,12 @@ class Collection {
// populate the fields with the existing values if any
if (!empty($this->id)) {
foreach ($elements as $k => $element) {
$elements[$k]['defaultvalue'] = $this->$k;
if ($k === 'tags') {
$elements[$k]['defaultvalue'] = $this->get_tags();
}
else {
$elements[$k]['defaultvalue'] = $this->$k;
}
}
$elements['id'] = array(
'type' => 'hidden',
......@@ -894,4 +921,17 @@ class Collection {
)
);
}
/**
* Returns the collection tags
*
* @return mixed
*/
public function get_tags() {
if (!isset($this->tags)) {
$this->tags = get_column('collection_tag', 'tag', 'collection', $this->get('id'));
}
return $this->tags;
}
}
......@@ -983,6 +983,16 @@
<KEY NAME="viewfk" TYPE="foreign" FIELDS="view" REFTABLE="view" REFFIELDS="id" />
</KEYS>
</TABLE>
<TABLE NAME="collection_tag">
<FIELDS>
<FIELD NAME="collection" TYPE="int" LENGTH="10" NOTNULL="true" />
<FIELD NAME="tag" TYPE="char" LENGTH="128" NOTNULL="true" />
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="collection,tag" />
<KEY NAME="collectionfk" TYPE="foreign" FIELDS="collection" REFTABLE="collection" REFFIELDS="id" />
</KEYS>
</TABLE>
<TABLE NAME="iframe_source_icon">
<FIELDS>
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" />
......
......@@ -3197,5 +3197,14 @@ function xmldb_core_upgrade($oldversion=0) {
set_field('usr_account_preference', 'value', LICENSE_INSTITUTION_DEFAULT, 'field', 'licensedefault', 'value', '-');
}
if ($oldversion < 2013050700) {
$table = new XMLDBTable('collection_tag');
$table->addFieldInfo('collection', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL);
$table->addFieldInfo('tag', XMLDB_TYPE_CHAR, 128, null, XMLDB_NOTNULL);
$table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('collection', 'tag'));
$table->addKeyInfo('collectionfk', XMLDB_KEY_FOREIGN, array('collection'), 'collection', array('id'));
create_table($table);
}
return $status;
}
......@@ -2624,10 +2624,14 @@ function get_my_tags($limit=null, $cloud=true, $sort='freq') {
(SELECT vt.tag, v.id, 'view' AS type
FROM {view_tag} vt JOIN {view} v ON v.id = vt.view
WHERE v.owner = ?)
) t
UNION
(SELECT ct.tag, c.id, 'collection' AS type
FROM {collection_tag} ct JOIN {collection} c ON c.id = ct.collection
WHERE c.owner = ?)
) t
GROUP BY t.tag
ORDER BY " . $sort . (is_null($limit) ? '' : " LIMIT $limit"),
array($id, $id)
array($id, $id, $id)
);
if (!$tagrecords) {
return false;
......@@ -3014,6 +3018,7 @@ function build_portfolio_search_html(&$data) {
global $THEME;
$artefacttypes = get_records_assoc('artefact_installed_type');
require_once('view.php');
require_once('collection.php');
foreach ($data->data as &$item) {
$item->ctime = format_date($item->ctime);
if ($item->type == 'view') {
......@@ -3023,6 +3028,12 @@ function build_portfolio_search_html(&$data) {
$v->set('dirty', false);
$item->url = $v->get_url();
}
else if ($item->type == 'collection') {
$item->typestr = get_string('Collection', 'collection');
$item->icon = $THEME->get_url('images/icon-collection.gif');
$c = new Collection(0, (array)$item);
$item->url = $c->get_url();
}
else { // artefact
safe_require('artefact', $artefacttypes[$item->artefacttype]->plugin);
$links = call_static_method(generate_artefact_class_name($item->artefacttype), 'get_links', $item->id);
......@@ -3040,11 +3051,12 @@ function build_portfolio_search_html(&$data) {
$data->baseurl = get_config('wwwroot') . 'tags.php' . (is_null($data->tag) ? '' : '?tag=' . urlencode($data->tag));
$data->sortcols = array('name', 'date');
$data->filtercols = array(
'all' => get_string('tagfilter_all'),
'file' => get_string('tagfilter_file'),
'image' => get_string('tagfilter_image'),
'text' => get_string('tagfilter_text'),
'view' => get_string('tagfilter_view'),
'all' => get_string('tagfilter_all'),
'file' => get_string('tagfilter_file'),
'image' => get_string('tagfilter_image'),
'text' => get_string('tagfilter_text'),
'view' => get_string('tagfilter_view'),
'collection' => get_string('tagfilter_collection'),
);
$smarty = smarty_core();
......
......@@ -616,10 +616,13 @@ function get_portfolio_types_from_param($filter) {
return null;
}
if ($filter == 'view') {
return array('view' => true, 'artefact' => false);
return array('view' => true, 'collection' => false, 'artefact' => false);
}
if ($filter == 'collection') {
return array('view' => false, 'collection' => true, 'artefact' => false);
}
require_once(get_config('docroot') . 'artefact/lib.php');
return array('view' => false, 'artefact' => artefact_get_types_from_filter($filter));
return array('view' => false, 'collection' => false, 'artefact' => artefact_get_types_from_filter($filter));
}
function get_portfolio_items_by_tag($tag, $owner, $limit, $offset, $sort='name', $type=null, $returntags=true) {
......
......@@ -138,6 +138,7 @@ function change_language($userid, $oldlang, $newlang) {
}
set_field_select('artefact_tag', 'tag', get_string_from_language($newlang, 'profile'), 'WHERE tag = ? AND artefact IN (SELECT id FROM {artefact} WHERE "owner" = ?)', array(get_string_from_language($oldlang, 'profile'), $userid));
set_field_select('view_tag', 'tag', get_string_from_language($newlang, 'profile'), 'WHERE tag = ? AND "view" IN (SELECT id FROM {view} WHERE "owner" = ?)', array(get_string_from_language($oldlang, 'profile'), $userid));
set_field_select('collection_tag', 'tag', get_string_from_language($newlang, 'profile'), 'WHERE tag = ? AND "collection" IN (SELECT id FROM {collection} WHERE "owner" = ?)', array(get_string_from_language($oldlang, 'profile'), $userid));
}
/**
......
......@@ -30,7 +30,7 @@ defined('INTERNAL') || die();
$config = new StdClass;
// See https://wiki.mahara.org/index.php/Developer_Area/Version_Numbering_Policy
// For upgrades on stable branches, increment the version by one. On master, use the date.
$config->version = 2013032202;
$config->version = 2013050700;
$config->release = '1.8.0dev';
$config->minupgradefrom = 2008040200;
$config->minupgraderelease = '1.0.0 (release tag 1.0.0_RELEASE)';
......
......@@ -933,6 +933,7 @@ class PluginSearchInternal extends PluginSearch {
*/
public static function portfolio_search_by_tag($tag, $owner, $limit, $offset, $sort, $types, $returntags) {
$viewfilter = is_null($types) || $types['view'] == true ? 'AND TRUE' : 'AND FALSE';
$collectionfilter = is_null($types) || $types['collection'] == true ? 'AND TRUE' : 'AND FALSE';
if (is_null($types)) {
$artefacttypefilter = '';
......@@ -947,13 +948,15 @@ class PluginSearchInternal extends PluginSearch {
if (!is_null($tag)) {
$artefacttypefilter .= ' AND at.tag = ?';
$viewfilter .= ' AND vt.tag = ?';
$values = array($owner->id, $tag, $owner->id, $tag);
$collectionfilter .= ' AND ct.tag = ?';
$values = array($owner->id, $tag, $owner->id, $tag, $owner->id, $tag);
}
else {
$values = array($owner->id, $owner->id);
$values = array($owner->id, $owner->id, $owner->id);
}
$from = "FROM (
$from = "
FROM (
(SELECT a.id, a.title, a.description, 'artefact' AS type, a.artefacttype, " . db_format_tsfield('a.ctime', 'ctime') . ",
a.owner, a.group, a.institution, NULL AS urlid
FROM {artefact} a JOIN {artefact_tag} at ON (a.id = at.artefact)
......@@ -963,6 +966,11 @@ class PluginSearchInternal extends PluginSearch {
v.owner, v.group, v.institution, v.urlid
FROM {view} v JOIN {view_tag} vt ON (v.id = vt.view)
WHERE v.owner = ? " . $viewfilter . ")
UNION
(SELECT c.id, c.name, c.description, 'collection' AS type, NULL AS artefacttype, " . db_format_tsfield('c.ctime', 'ctime') . ",
c.owner, c.group, c.institution, NULL AS urlid
FROM {collection} c JOIN {collection_tag} ct ON (c.id = ct.collection)
WHERE c.owner = ? " . $collectionfilter . ")
) p";
$result = (object) array(
......@@ -980,7 +988,7 @@ class PluginSearchInternal extends PluginSearch {
$sort = $sort == 'date' ? 'ctime DESC' : 'title ASC';
if ($data = get_records_sql_assoc("SELECT type || ':' || id AS tid, p.* " . $from . ' ORDER BY ' . $sort, $values, $offset, $limit)) {
if ($returntags) {
$ids = array('view' => array(), 'artefact' => array());
$ids = array('view' => array(), 'collection' => array(), 'artefact' => array());
foreach ($data as &$d) {
$ids[$d->type][$d->id] = 1;
}
......@@ -991,6 +999,13 @@ class PluginSearchInternal extends PluginSearch {
}
}
}
if (!empty($ids['collection'])) {
if ($collectiontags = get_records_select_array('collection_tag', 'collection IN (' . join(',', array_keys($ids['collection'])) . ')')) {
foreach ($collectiontags as &$ct) {
$data['collection:' . $ct->collection]->tags[] = $ct->tag;
}
}
}
if (!empty($ids['artefact'])) {
if ($artefacttags = get_records_select_array('artefact_tag', 'artefact IN (' . join(',', array_keys($ids['artefact'])) . ')', NULL, 'tag')) {
foreach ($artefacttags as &$at) {
......
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