tags.php 4.52 KB
Newer Older
1
<?php
2
require_once(get_config('docroot') . 'lib/form/elements/autocomplete.php');
3
/**
4
 *
5
6
 * @package    mahara
 * @subpackage form-element
7
 * @author     Catalyst IT Ltd
8
9
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
10
11
12
13
14
15
16
17
18
19
20
21
 *
 */

/**
 * Provides a tag input field
 *
 * @param Pieform  $form    The form to render the element for
 * @param array    $element The element to render
 * @return string           The HTML for the element
 */
function pieform_element_tags(Pieform $form, $element) {

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    $newelement = array(
        'type' => 'autocomplete',
        'title' => $element['title'],
        'id' => $element['id'],
        'name' => $element['name'],
        'defaultvalue' => isset($element['defaultvalue']) ? $element['defaultvalue'] : null,
        'description' => isset($element['description']) ? $element['description'] : null,
        'help' => isset($element['help']) ? $element['help'] : false,
        'ajaxurl' => get_config('wwwroot') . 'json/taglist.php',
        'multiple' => true,
        'allowclear' => false,
        'initfunction' => 'translate_tags_to_names',
        'ajaxextraparams' => array(),
        'extraparams' => array('tags' => true),
        'width' => '280px',
    );
    return pieform_element_autocomplete($form, $newelement);
}
40

41
42
43
44
45
46
function translate_tags_to_names(array $ids) {
    global $USER;
    // for an empty list, the element '' is transmitted
    $ids = array_diff($ids, array(''));
    $results = array();
    $alltags = get_all_tags_for_user();
47

48
49
50
51
52
    foreach ($ids as $id) {
        if (isset($alltags['tags'][$id])) {
            $results[] = (object) array('id' => $id, 'text' => display_tag($id, $alltags['tags']));
        }
        else {
53
            $results[] = (object) array('id' => $id, 'text' => $id);
54
        }
55
    }
56
57
    return $results;
}
58

59
60
61
62
63
64
65
66
67
68
69
/**
 * Display formatted tag
 * Currently is tag name plus the usage count
 *
 * @param string $name    Tag name
 * @param string $alltags  Array of tags to get the information from
 * @return $tag Formatted tag
 */
function display_tag($name, $alltags) {
    return $name . ' (' . $alltags[$name]->count . ')';
}
70

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
 * Get all tags created by this user
 *
 * @param string $query Search option
 * @param int $limit
 * @param int $offset
 * @retun array $tags  The tags this user has created
 */
function get_all_tags_for_user($query = null, $limit = null, $offset = null) {
    global $USER;
    if ($USER->is_logged_in()) {
        $usertags = "";
        $userid = $USER->get('id');
        if ($USER->get('admin')) {
            $usertags = "
                UNION ALL
                SELECT tag,COUNT(*) AS count FROM {usr_tag} t INNER JOIN {usr} u ON t.usr=u.id GROUP BY 1";
        }
        else if ($admininstitutions = $USER->get('admininstitutions')) {
            $insql = "'" . join("','", $admininstitutions) . "'";
            $usertags = "
                UNION ALL
                SELECT tag,COUNT(*) AS count FROM {usr_tag} t INNER JOIN {usr} u ON t.usr=u.id INNER JOIN {usr_institution} ui ON ui.usr=u.id WHERE ui.institution IN ($insql) GROUP BY 1";
        }
        $values = array($userid, $userid, $userid);
        $querystr = '';
        if ($query) {
            $querystr = " WHERE tag LIKE '%' || ? || '%'";
            $values[] = $query;
        }
        $sql = "
            SELECT tag, SUM(count) AS count
            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
                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
            " . $querystr . "
            GROUP BY tag
            ORDER BY LOWER(tag)
            ";
        $result = get_records_sql_assoc($sql, $values, $offset, $limit);
116
    }
117
118
119
120
    $results = !empty($result) ? $result : array();
    $return = array('tags' => $results,
                    'count' => count($results),
    );
121

122
123
    return $return;
}
124

125
126
function pieform_element_tags_get_headdata($element) {
    return pieform_element_autocomplete_get_headdata($element);
127
128
129
}

function pieform_element_tags_get_value(Pieform $form, $element) {
130
    return pieform_element_autocomplete_get_value($form, $element);
131
}