taggedposts.json.php 1.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
/**
 *
 * @package    mahara
 * @subpackage artefact-blog-taggedposts
 * @author     Catalyst IT Ltd
 * @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.
 *
 */

define('INTERNAL', 1);
define('JSON', 1);

require(dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/init.php');

global $USER;

19
$request = param_variable('q', '');
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
$tagexcluded = '';
if (substr($request, 0, 1) == '-') {
    $request = substr($request, 1);
    $tagexcluded = '-';
}
$page = param_integer('page');
if ($page < 1) {
    $page = 1;
}
$tagsperpage = 5;
$values = array($USER->id);
$sql = "SELECT at.tag FROM {artefact_tag} at
        JOIN {artefact} a ON a.id = at.artefact
        WHERE a.owner = ?
        AND a.artefacttype = 'blogpost'";
if ($request !== '') {
    $sql .= " AND at.tag LIKE '%' || ? || '%'";
    $values[] = $request;
}
$sql .= " GROUP BY at.tag
          ORDER BY at.tag ASC";
$more = true;
$tmptags = array();
$alltags = get_records_sql_array($sql, $values);
while ($alltags !== false && $more && count($tmptags) < $tagsperpage) {
    $tags = array_slice($alltags, $tagsperpage * ($page - 1), $tagsperpage);
    $more = sizeof($alltags) > $tagsperpage * $page;

    foreach ($tags as $tag) {
        if (count($tmptags) >= $tagsperpage) {
            $more = true;
            continue;
        }
        if (stripos($tag->tag, $request) !== false || $request === '') {
54
            $tmptags[] = (object) array(
55
56
                    'id' => $tagexcluded . $tag->tag,
                    'text' => $tag->tag,
57
            );
58
59
60
61
62
63
64
65
66
        }
    }
    $page++;
}

echo json_encode(array(
    'more' => $more,
    'results' => $tmptags,
));