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
namespace tool_brickfield;
18
 
19
use tool_brickfield\local\tool\filter;
20
use tool_brickfield\local\tool\tool;
21
 
22
/**
23
 * Provides the Brickfield Accessibility toolkit site data API.
24
 *
25
 * @package    tool_brickfield
26
 * @copyright  2020 onward Brickfield Education Labs Ltd, https://www.brickfield.ie
27
 * @author     Mike Churchward (mike@brickfieldlabs.ie)
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class sitedata {
31
 
32
    /** @var array An array of SQL parts to build an SQL statement. */
33
    private $sqlparts = ['SELECT' => [], 'FROM' => '', 'WHERE' => '', 'GROUPBY' => '', 'ORDERBY' => ''];
34
 
35
    /** @var array An array of SQL parameters. */
36
    private $sqlparams = [];
37
 
38
    /** @var array An array of labels to be displayed. */
39
    private $grouplabels = [];
40
 
41
    /** @var array An array of the used check group numbers. */
42
    private $groupnumbers = [];
43
 
44
    /** @var int The count of group labels. */
45
    private $groupcount = 0;
46
 
47
    /**
48
     * Return the total number of courses that have been checked.
49
     * @return int
50
     * @throws \dml_exception
51
     */
52
    public static function get_total_courses_checked(): int {
53
        global $DB;
54
        return $DB->count_records_select(manager::DB_AREAS, '', [], 'COUNT(DISTINCT courseid)');
55
    }
56
 
57
    /**
58
     * Get records of component per course summary data.
59
     * @param local\tool\filter $filter
60
     * @return array
61
     * @throws \coding_exception
62
     * @throws \dml_exception
63
     */
64
    public static function get_component_data(filter $filter): array {
65
        global $DB;
66
 
67
        $data = [];
68
        if ($filter->validate_filters()) {
69
            list($wheresql, $params) = $filter->get_course_sql('', true);
70
            $sql = 'SELECT component, SUM(errorcount) as errorsum, SUM(totalactivities) as total, ' .
71
                'SUM(failedactivities) as failed, SUM(passedactivities) as passed ' .
72
                'FROM {' . manager::DB_CACHEACTS . '} area ' .
73
                (empty($wheresql) ? '' : ('WHERE ' . $wheresql . ' ')) .
74
                'GROUP BY area.component ' .
75
                'ORDER BY area.component ASC';
76
 
77
            $data = $DB->get_records_sql($sql, $params);
78
 
79
            foreach ($data as $key => $componentsummary) {
80
                $data[$key]->componentlabel = tool::get_module_label($componentsummary->component);
81
            }
82
        }
83
 
84
        return $data;
85
    }
86
 
87
    /**
88
     * Get records of check group per course summary data.
89
     * @param local\tool\filter $filter
90
     * @return array
91
     * @throws \coding_exception
92
     * @throws \dml_exception
93
     */
94
    public function get_checkgroup_data(filter $filter): array {
95
        global $DB;
96
 
97
        $data = [];
98
        if ($filter->validate_filters()) {
99
            $this->get_base_checkgroup_sql($filter);
100
 
101
            $sql = $this->get_select_string() . ' ' .
102
                'FROM ' . $this->sqlparts['FROM'] . ' ' .
103
                (!empty($this->sqlparts['WHERE']) ? 'WHERE ' . $this->sqlparts['WHERE'] . ' ' : '') .
104
                (!empty($this->sqlparts['GROUPBY']) ? 'GROUP BY ' . $this->sqlparts['GROUPBY'] . ' ' : '') .
105
                (!empty($this->sqlparts['ORDERBY']) ? 'ORDER BY ' . $this->sqlparts['ORDERBY'] . ' ' : '');
106
            $data = array_values($DB->get_records_sql($sql, $this->sqlparams,
107
                ($filter->page * $filter->perpage), $filter->perpage));
108
            if (empty($data)) {
109
                $data[0] = (object)(array_values($this->grouplabels));
110
            } else {
111
                $data[0] = (object)(array_merge((array)$data[0], $this->grouplabels));
112
            }
113
            $data[0]->groupcount = $this->groupcount;
114
        }
115
 
116
        return $data;
117
    }
118
 
119
    /**
120
     * Get records of check group per course summary data.
121
     * @param local\tool\filter $filter
122
     * @return array
123
     * @throws \coding_exception
124
     * @throws \dml_exception
125
     */
126
    public function get_checkgroup_by_course_data(filter $filter): array {
127
        $this->sqlparts['GROUPBY'] = 'courseid';
128
        $this->add_select_item('courseid');
129
        return $this->get_checkgroup_data($filter);
130
    }
131
 
132
    /**
133
     * Get records of check group per course summary data.
134
     * @param local\tool\filter $filter
135
     * @return array
136
     * @throws \coding_exception
137
     * @throws \dml_exception
138
     */
139
    public function get_checkgroup_with_failed_data(filter $filter): array {
140
        $this->add_select_item('SUM(activitiesfailed) as failed');
141
        return $this->get_checkgroup_data($filter);
142
    }
143
 
144
    /**
145
     * Load up the base SQL parts.
146
     * @param filter $filter
147
     * @throws \coding_exception
148
     * @throws \dml_exception
149
     */
150
    private function get_base_checkgroup_sql(filter $filter) {
151
        if ($filter->validate_filters()) {
152
            $this->get_grouplabel_info();
153
 
154
            list($this->sqlparts['WHERE'], $this->sqlparams) = $filter->get_course_sql('summ', true);
155
            $this->add_select_item('SUM(activities) as activities');
156
            foreach ($this->groupnumbers as $lab) {
157
                $this->add_select_item("SUM(errorschecktype$lab) as errorsvalue$lab");
158
                $this->add_select_item("SUM(failedchecktype$lab) as failedvalue$lab");
159
                $this->add_select_item("AVG(percentchecktype$lab) as percentvalue$lab");
160
            }
161
            $this->sqlparts['FROM'] = '{' . manager::DB_SUMMARY . '} summ';
162
        }
163
    }
164
 
165
    /**
166
     * Load the group label information.
167
     * @throws \coding_exception
168
     * @throws \dml_exception
169
     */
170
    private function get_grouplabel_info() {
171
        global $DB;
172
 
173
        // Don't need to do this more than once.
174
        if (empty($this->grouplabels)) {
175
            // Determine the checkgroups being used by this site.
176
            $labelsql = 'SELECT DISTINCT checkgroup, checkgroup as cg2 FROM {' . manager::DB_CHECKS . '} ' .
177
                'WHERE status = ? ORDER BY checkgroup ASC';
178
            $groupvals = $DB->get_records_sql_menu($labelsql, [1]);
179
            $grouplabels = array_intersect_key(area_base::CHECKGROUP_NAMES, $groupvals);
180
 
181
            foreach ($grouplabels as $lab => $label) {
182
                $this->grouplabels['componentlabel' . $lab] = get_string('checktype:' . $label, manager::PLUGINNAME);
183
                $this->groupnumbers[] = $lab;
184
                $this->groupcount++;
185
            }
186
        }
187
    }
188
 
189
    /**
190
     * Add a select item to the sqlparts array.
191
     * @param string $item
192
     */
193
    private function add_select_item(string $item): void {
194
        $this->sqlparts['SELECT'][] = $item;
195
    }
196
 
197
    /**
198
     * Assemble and return the select portion of the sql.
199
     * @return string
200
     */
201
    private function get_select_string(): string {
202
        return 'SELECT ' . implode(', ', $this->sqlparts['SELECT']);
203
    }
204
}