• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ewallah / moodle-report_growth / 14518858290

17 Apr 2025 03:09PM UTC coverage: 88.462% (-5.9%) from 94.379%
14518858290

push

github

rdebleu
CoversClass

299 of 338 relevant lines covered (88.46%)

61.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.82
/classes/output/category_renderer.php
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
 * Growth category report renderer.
19
 *
20
 * @package   report_growth
21
 * @copyright eWallah (www.eWallah.net)
22
 * @author    Renaat Debleu <info@eWallah.net>
23
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25

26
namespace report_growth\output;
27

28
use moodle_url;
29
use html_writer;
30
use plugin_renderer_base;
31
use renderable;
32
use tabobject;
33
use core\{chart_bar, chart_line, chart_series};
34

35
/**
36
 * Growth category report renderer.
37
 *
38
 * @package   report_growth
39
 * @copyright eWallah (www.eWallah.net)
40
 * @author    Renaat Debleu <info@eWallah.net>
41
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42
 */
43
class category_renderer extends growth_renderer {
44
    /** @var int categoryid. */
45
    private int $categoryid;
46

47
    /** @var array courseids. */
48
    private array $courseids;
49

50
    /**
51
     * Create Tabs.
52
     *
53
     * @param \stdClass $context Selected $coursecontext
54
     * @param int $p Selected tab
55
     * @return string
56
     */
57
    public function create_tabtree($context, $p = 1) {
58
        global $CFG;
59
        $this->categoryid = $context->instanceid;
72✔
60
        $this->context = $context;
72✔
61
        $coursecat = \core_course_category::get($this->categoryid);
72✔
62
        $this->courseids = array_values($coursecat->get_courses(['recursive' => true, 'idonly' => true]));
72✔
63
        sort($this->courseids);
72✔
64
        $txt = get_strings(['activities', 'lastaccess', 'coursecompletions']);
72✔
65
        $rows = [
72✔
66
            'enrolments' => get_string('enrolments', 'enrol'),
72✔
67
            'lastaccess' => $txt->lastaccess,
72✔
68
            'activities' => $txt->activities, ];
72✔
69
        if (!empty($CFG->enablecompletion)) {
72✔
70
            $rows['activitiescompleted'] = get_string('activitiescompleted', 'completion');
72✔
71
            $rows['coursecompletions'] = $txt->coursecompletions;
72✔
72
        }
73
        $rows = array_merge($rows, $this->certificate_tabs());
72✔
74
        $rows['countries'] = get_string('countries', 'report_growth');
72✔
75
        // Trigger a report viewed event.
76
        $this->trigger_page($p);
72✔
77
        return $this->render_page($rows, $p);
72✔
78
    }
79

80
    /**
81
     * Table enrolments.
82
     *
83
     * @param string $title Title
84
     * @return string
85
     */
86
    public function table_enrolments($title = ''): string {
87
        return $this->collect_cat2($title, 'enrol', 'courseid', 'user_enrolments', 'enrolid', 'timecreated');
48✔
88
    }
89

90
    /**
91
     * Table last access.
92
     *
93
     * @param string $title Title
94
     * @return string
95
     */
96
    public function table_lastaccess($title = ''): string {
97
        return $this->collect_cat($title, 'user_lastaccess', 'courseid', 'timeaccess');
4✔
98
    }
99

100
    /**
101
     * Table activities.
102
     *
103
     * @param string $title Title
104
     * @return string
105
     */
106
    public function table_activities($title = ''): string {
107
        return $this->collect_cat($title, 'course_modules', 'course', 'added');
4✔
108
    }
109

110
    /**
111
     * Table activities completed.
112
     *
113
     * @param string $title Title
114
     * @return string
115
     */
116
    public function table_activitiescompleted($title = ''): string {
117
        return $this->collect_cat2(
4✔
118
            $title,
4✔
119
            'course_modules',
4✔
120
            'course',
4✔
121
            'course_modules_completion',
4✔
122
            'coursemoduleid',
4✔
123
            'timemodified'
4✔
124
        );
4✔
125
    }
126

127
    /**
128
     * Table completions.
129
     *
130
     * @param string $title Title
131
     * @return string
132
     */
133
    public function table_coursecompletions($title = ''): string {
134
        return $this->collect_cat($title, 'course_completions', 'course', 'timecompleted');
4✔
135
    }
136

137
    /**
138
     * Table badges.
139
     *
140
     * @param string $title Title
141
     * @return string
142
     */
143
    public function table_badges($title = ''): string {
144
        return $this->collect_cat2($title, 'badge', 'courseid', 'badge_issued', 'badgeid', 'dateissued');
4✔
145
    }
146

147
    /**
148
     * Table certificates.
149
     *
150
     * @param string $title Title
151
     * @return string
152
     */
153
    public function table_certificates($title = ''): string {
154
        global $CFG;
155
        $s = '';
×
156
        if (file_exists($CFG->dirroot . '/mod/certificate')) {
×
157
            $s = $this->collect_cat2($title, 'certificate', 'course', 'certificate_issues', 'certificateid', 'timecreated');
×
158
        }
159
        return $s;
×
160

161
    }
162

163
    /**
164
     * Table custom certificates.
165
     *
166
     * @param string $title Title
167
     * @return string
168
     */
169
    public function table_customcerts($title = ''): string {
170
        global $CFG;
171
        $s = '';
×
172
        if (file_exists($CFG->dirroot . '/mod/customcert')) {
×
173
            $s = $this->collect_cat2($title, 'customcert', 'course', 'customcert_issues', 'customcertid', 'timecreated');
×
174
        }
175
        return $s;
×
176
    }
177

178
    /**
179
     * Table course certificates.
180
     *
181
     * @param string $title Title
182
     * @return string
183
     */
184
    public function table_coursecertificates($title = ''): string {
185
        global $CFG;
186
        $s = '';
×
187
        if (file_exists($CFG->dirroot . '/mod/coursecertificate')) {
×
188
            $s = $this->collect_cat($title, 'tool_certificate_issues', 'courseid', 'timecreated');
×
189
        }
190
        return $s;
×
191
    }
192

193
    /**
194
     * Table country.
195
     *
196
     * @param string $title Title
197
     * @return string
198
     */
199
    public function table_countries($title = ''): string {
200
        global $DB;
201
        $title = get_string('users');
4✔
202
        $out = get_string('nostudentsfound', 'moodle', $title);
4✔
203
        if (count($this->courseids) > 0) {
4✔
204
            [$insql, $inparams] = $this->insql($this->courseids, 'courseid', 'courseid');
4✔
205
            $ids = $DB->get_fieldset_select('enrol', 'id', $insql, $inparams);
4✔
206
            if (count($ids) > 0) {
4✔
207
                [$insql, $inparams] = $this->insql($ids, 'enrolid', 'enrolid');
4✔
208
                $userids = $DB->get_fieldset_select('user_enrolments', 'userid', $insql, $inparams);
4✔
209
                if (count($userids) > 0) {
4✔
210
                    [$insql, $inparams] = $this->insql($userids, 'id', 'id');
4✔
211
                    $sql = "SELECT country, COUNT(country) AS newusers FROM {user} WHERE $insql GROUP BY country ORDER BY country";
4✔
212
                    $rows = $DB->get_records_sql($sql, $inparams);
4✔
213
                    $out = $this->create_countries($rows, $title);
4✔
214
                }
215
            }
216
        }
217
        return $out;
4✔
218
    }
219

220
    /**
221
     * Collect category table.
222
     *
223
     * @param string $title Title
224
     * @param string $table First table
225
     * @param string $fieldwhere Where lookup
226
     * @param string $fieldresult The field that has to be calculated
227
     * @return string
228
     */
229
    protected function collect_cat($title, $table, $fieldwhere, $fieldresult): string {
230
        [$insql, $inparams] = $this->insql($this->courseids, $fieldwhere, $fieldresult);
12✔
231
        return $this->create_charts($table, $title, $fieldresult, $insql, $inparams);
12✔
232
    }
233

234
    /**
235
     * Collect category table.
236
     *
237
     * @param string $title Title
238
     * @param string $table1 First table
239
     * @param string $field1 Where lookup
240
     * @param string $table2 Second table
241
     * @param string $field2 Where lookup
242
     * @param string $fieldresult The field that has to be calculated
243
     * @return string
244
     */
245
    protected function collect_cat2($title, $table1, $field1, $table2, $field2, $fieldresult): string {
246
        global $DB;
247
        if (count($this->courseids) > 0) {
56✔
248
            [$insql, $inparams] = $this->insql($this->courseids, $field1, $fieldresult);
56✔
249
            $ids = $DB->get_fieldset_select($table1, 'id', $insql, $inparams);
56✔
250
            if (count($ids) > 0) {
56✔
251
                sort($ids);
48✔
252
                [$insql, $inparams] = $this->insql($ids, $field2, $fieldresult);
48✔
253
                return $this->create_charts($table2, $title, $fieldresult, $insql, $inparams);
48✔
254
            }
255
        }
256
        return get_string('nostudentsfound', 'moodle', $title);
8✔
257
    }
258
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc