Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Contains class core_tag\output\tagindex
19
 *
20
 * @package   core_tag
21
 * @copyright 2015 Marina Glancy
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace core_tag\output;
26
 
27
use renderable;
28
use templatable;
29
use renderer_base;
30
use stdClass;
31
use moodle_url;
32
use core_tag_tag;
33
 
34
/**
35
 * Class to display a tag cloud - set of tags where each has a weight.
36
 *
37
 * @package   core_tag
38
 * @copyright 2015 Marina Glancy
39
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
41
class tagcloud implements templatable {
42
 
43
    /** @var array */
44
    protected $tagset;
45
 
46
    /** @var int */
47
    protected $totalcount;
48
 
49
    /**
50
     * Constructor
51
     *
52
     * @param array $tagset array of core_tag or stdClass elements, each of them must have attributes:
53
     *              name, rawname, tagcollid
54
     *              preferrably also have attributes:
55
     *              isstandard, count, flag
56
     * @param int $totalcount total count of tags (for example to indicate that there are more tags than the count of tagset)
57
     *            leave 0 if count of tagset is the actual count of tags
58
     * @param int $fromctx context id where this tag cloud is displayed
59
     * @param int $ctx context id for tag view link
60
     * @param int $rec recursive argument for tag view link
61
     */
62
    public function __construct($tagset, $totalcount = 0, $fromctx = 0, $ctx = 0, $rec = 1) {
63
        $canmanagetags = has_capability('moodle/tag:manage', \context_system::instance());
64
 
65
        $maxcount = 1;
66
        foreach ($tagset as $tag) {
67
            if (isset($tag->count) && $tag->count > $maxcount) {
68
                $maxcount = $tag->count;
69
            }
70
        }
71
 
72
        $this->tagset = array();
73
        foreach ($tagset as $idx => $tag) {
74
            $this->tagset[$idx] = new stdClass();
75
 
76
            $this->tagset[$idx]->name = core_tag_tag::make_display_name($tag, false);
77
 
78
            if ($canmanagetags && !empty($tag->flag)) {
79
                $this->tagset[$idx]->flag = 1;
80
            }
81
 
82
            $viewurl = core_tag_tag::make_url($tag->tagcollid, $tag->rawname, 0, $fromctx, $ctx, $rec);
83
            $this->tagset[$idx]->viewurl = $viewurl->out(false);
84
 
85
            if (isset($tag->isstandard)) {
86
                $this->tagset[$idx]->isstandard = $tag->isstandard ? 1 : 0;
87
            }
88
 
89
            if (!empty($tag->count)) {
90
                $this->tagset[$idx]->count = $tag->count;
91
                $this->tagset[$idx]->size = (int)($tag->count / $maxcount * 20);
92
            }
93
        }
94
 
95
        $this->totalcount = $totalcount ? $totalcount : count($this->tagset);
96
    }
97
 
98
    /**
99
     * Returns number of tags in the cloud
100
     * @return int
101
     */
102
    public function get_count() {
103
        return count($this->tagset);
104
    }
105
 
106
    /**
107
     * Export this data so it can be used as the context for a mustache template.
108
     *
109
     * @param renderer_base $output
110
     * @return stdClass
111
     */
112
    public function export_for_template(renderer_base $output) {
113
        $cnt = count($this->tagset);
114
        return (object)array(
115
            'tags' => $this->tagset,
116
            'tagscount' => $cnt,
117
            'totalcount' => $this->totalcount,
118
            'overflow' => ($this->totalcount > $cnt) ? 1 : 0,
119
        );
120
    }
121
}