Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 77... Línea 77...
77
        // Track the index of conditions/filters as we iterate over them.
77
        // Track the index of conditions/filters as we iterate over them.
78
        $conditionindex = $filterindex = 0;
78
        $conditionindex = $filterindex = 0;
Línea 79... Línea 79...
79
 
79
 
80
        // For each condition, we need to ensure their values are always accounted for in the report.
80
        // For each condition, we need to ensure their values are always accounted for in the report.
81
        $conditionvalues = $this->report->get_condition_values();
81
        $conditionvalues = $this->report->get_condition_values();
82
        foreach ($this->report->get_active_conditions() as $condition) {
82
        foreach ($this->report->get_active_conditions(false) as $condition) {
83
            [$conditionsql, $conditionparams] = $this->get_filter_sql($condition, $conditionvalues, 'c' . $conditionindex++);
83
            [$conditionsql, $conditionparams] = $this->get_filter_sql($condition, $conditionvalues, 'c' . $conditionindex++);
84
            if ($conditionsql !== '') {
84
            if ($conditionsql !== '') {
85
                $joins = array_merge($joins, $condition->get_joins());
85
                $joins = array_merge($joins, $condition->get_joins());
86
                $wheres[] = "({$conditionsql})";
86
                $wheres[] = "({$conditionsql})";
Línea 114... Línea 114...
114
 
114
 
115
        // Add unique table joins.
115
        // Add unique table joins.
Línea 116... Línea 116...
116
        $from .= ' ' . implode(' ', array_unique($joins));
116
        $from .= ' ' . implode(' ', array_unique($joins));
117
 
-
 
118
        $this->set_sql($fields, $from, $wheresql, $params);
-
 
119
 
-
 
120
        $counttablealias = database::generate_alias();
-
 
121
        $this->set_count_sql("
-
 
122
            SELECT COUNT(1)
-
 
123
              FROM (SELECT {$fields}
-
 
124
                      FROM {$from}
-
 
125
                     WHERE {$wheresql}
-
 
126
                           {$this->groupbysql}
117
 
Línea 127... Línea 118...
127
                   ) {$counttablealias}", $params);
118
        $this->set_sql($fields, $from, $wheresql, $params);
128
    }
119
    }
129
 
120
 
Línea 163... Línea 154...
163
    }
154
    }
Línea 164... Línea 155...
164
 
155
 
165
    /**
156
    /**
166
     * Generate suitable SQL for the table
157
     * Generate suitable SQL for the table
-
 
158
     *
167
     *
159
     * @param bool $includesort
168
     * @return string
160
     * @return string
169
     */
161
     */
170
    protected function get_table_sql(): string {
162
    protected function get_table_sql(bool $includesort = true): string {
Línea 171... Línea 163...
171
        $sql = "SELECT {$this->sql->fields} FROM {$this->sql->from} WHERE {$this->sql->where} {$this->groupbysql}";
163
        $sql = "SELECT {$this->sql->fields} FROM {$this->sql->from} WHERE {$this->sql->where} {$this->groupbysql}";
172
 
-
 
173
        $sort = $this->get_sql_sort();
164
 
174
        if ($sort) {
165
        if ($includesort && ($sort = $this->get_sql_sort())) {
Línea 175... Línea 166...
175
            $sql .= " ORDER BY {$sort}";
166
            $sql .= " ORDER BY {$sort}";
176
        }
167
        }
Línea 186... Línea 177...
186
     */
177
     */
187
    public function query_db($pagesize, $useinitialsbar = true): void {
178
    public function query_db($pagesize, $useinitialsbar = true): void {
188
        global $DB;
179
        global $DB;
Línea 189... Línea 180...
189
 
180
 
190
        if (!$this->is_downloading()) {
-
 
Línea 191... Línea 181...
191
            $this->pagesize($pagesize, $DB->count_records_sql($this->countsql, $this->countparams));
181
        if (!$this->is_downloading()) {
-
 
182
 
-
 
183
            // Initially set the page size, so the following SQL read has correct values.
-
 
184
            $this->pagesize($pagesize, 0);
-
 
185
 
-
 
186
            $countedcolumn = database::generate_alias();
-
 
187
            $countedrecordset = $DB->get_counted_recordset_sql(
-
 
188
                $this->get_table_sql(false),
-
 
189
                $countedcolumn,
-
 
190
                $this->get_sql_sort(),
192
 
191
                $this->sql->params,
-
 
192
                (int) $this->get_page_start(),
-
 
193
                (int) $this->get_page_size(),
-
 
194
            );
-
 
195
 
-
 
196
            // Now set the total page size.
-
 
197
            $countedsize = (int) ($countedrecordset->current()->{$countedcolumn} ?? 0);
-
 
198
            $this->pagesize($pagesize, $countedsize);
193
            $this->rawdata = $DB->get_recordset_sql($this->get_table_sql(), $this->sql->params, $this->get_page_start(),
199
 
194
                $this->get_page_size());
200
            $this->rawdata = $countedrecordset;
195
        } else {
201
        } else {
196
            $this->rawdata = $DB->get_recordset_sql($this->get_table_sql(), $this->sql->params);
202
            $this->rawdata = $DB->get_recordset_sql($this->get_table_sql(), $this->sql->params);
Línea 197... Línea 203...
197
        }
203
        }
-
 
204
    }
-
 
205
 
-
 
206
    /**
-
 
207
     * Return total row count for report table. Note we'd typically use {@see query_db} and then read the {@see totalrows}
-
 
208
     * property to reduce DB calls, however we can use this method when we specifically don't also need to obtain all data
-
 
209
     *
-
 
210
     * @return int
-
 
211
     */
-
 
212
    public function get_total_row_count(): int {
-
 
213
        global $DB;
-
 
214
 
-
 
215
        $counttablesql = $this->get_table_sql(false);
-
 
216
        $counttablealias = database::generate_alias();
-
 
217
 
-
 
218
        return $DB->count_records_sql(
-
 
219
            "SELECT COUNT(1) FROM ({$counttablesql}) {$counttablealias}",
-
 
220
            $this->sql->params,
-
 
221
        );
198
    }
222
    }
199
 
223
 
200
    /**
224
    /**
201
     * Override parent method of the same, to ensure that any columns with custom sort fields are accounted for
225
     * Override parent method of the same, to ensure that any columns with custom sort fields are accounted for
202
     *
226
     *
Línea 219... Línea 243...
219
        // Iterate over all sorted report columns, replace with columns own fields if applicable.
243
        // Iterate over all sorted report columns, replace with columns own fields if applicable.
220
        foreach ($sortedcolumns as $alias => $order) {
244
        foreach ($sortedcolumns as $alias => $order) {
221
            $column = $columnsbyalias[$alias] ?? null;
245
            $column = $columnsbyalias[$alias] ?? null;
Línea 222... Línea 246...
222
 
246
 
223
            // If the column is not being aggregated and defines custom sort fields, then use them.
-
 
224
            if ($column && !$column->get_aggregation() &&
247
            // If the column is not being aggregated and defines custom sort fields, then use them.
225
                    ($sortfields = $column->get_sort_fields())) {
-
 
226
 
248
            if ($column && !$column->get_aggregation() && ($sortfields = $column->get_sort_fields())) {
-
 
249
                foreach ($sortfields as $sortfield) {
227
                foreach ($sortfields as $sortfield) {
250
                    if (!array_key_exists($sortfield, $columnsortby)) {
-
 
251
                        $columnsortby[$sortfield] = $order;
-
 
252
                    }
-
 
253
                }
-
 
254
            } else if (array_key_exists($alias, $sortedcolumnsfullname)) {
-
 
255
                // Ensure that magic fullname sorted columns refer to correct alias.
-
 
256
                foreach ($columnsbyalias as $column) {
-
 
257
                    $sortfieldalias = array_filter(
-
 
258
                        $column->get_sort_fields(),
-
 
259
                        fn(string $key) => preg_match("/^c[\d]+_{$alias}$/", $key),
-
 
260
                    );
-
 
261
                    if (count($sortfieldalias) > 0) {
-
 
262
                        $columnsortby[reset($sortfieldalias)] = $order;
-
 
263
                        break;
228
                    $columnsortby[$sortfield] = $order;
264
                    }
229
                }
265
                }
230
            } else {
266
            } else {
231
                $columnsortby[$alias] = $order;
267
                $columnsortby[$alias] = $order;
232
            }
268
            }
Línea 233... Línea -...
233
        }
-
 
234
 
-
 
235
        // Now ensure that any fullname sorted columns have duplicated aliases removed.
-
 
236
        $columnsortby = array_filter($columnsortby, static function(string $alias) use ($sortedcolumnsfullname): bool {
-
 
237
            if (preg_match('/^c[\d]+_(?<column>.*)$/', $alias, $matches)) {
-
 
238
                return !array_key_exists($matches['column'], $sortedcolumnsfullname);
-
 
239
            }
-
 
240
            return true;
-
 
241
        }, ARRAY_FILTER_USE_KEY);
269
        }
242
 
270
 
Línea 243... Línea 271...
243
        return static::construct_order_by($columnsortby);
271
        return static::construct_order_by($columnsortby);
244
    }
272
    }
Línea 285... Línea 313...
285
        // Render button to allow user to reset table preferences.
313
        // Render button to allow user to reset table preferences.
286
        echo $this->render_reset_button();
314
        echo $this->render_reset_button();
Línea 287... Línea 315...
287
 
315
 
Línea 288... Línea 316...
288
        $this->wrap_html_start();
316
        $this->wrap_html_start();
Línea 289... Línea 317...
289
 
317
 
290
        $this->set_caption($this->report::get_name(), ['class' => 'sr-only']);
318
        $this->set_caption($this->report::get_name(), ['class' => 'visually-hidden']);
291
 
319
 
292
        echo html_writer::start_tag('div');
320
        echo html_writer::start_tag('div', ['class' => 'table-responsive']);