Proyectos de Subversion Moodle

Rev

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

Rev 1 Rev 1441
Línea 21... Línea 21...
21
use context_helper;
21
use context_helper;
22
use context_system;
22
use context_system;
23
use context_user;
23
use context_user;
24
use core\context;
24
use core\context;
25
use core_component;
25
use core_component;
-
 
26
use core_date;
-
 
27
use core_user;
26
use html_writer;
28
use html_writer;
27
use lang_string;
29
use lang_string;
28
use moodle_url;
30
use moodle_url;
29
use stdClass;
31
use stdClass;
30
use theme_config;
32
use theme_config;
Línea 72... Línea 74...
72
    protected function get_default_entity_title(): lang_string {
74
    protected function get_default_entity_title(): lang_string {
73
        return new lang_string('entityuser', 'core_reportbuilder');
75
        return new lang_string('entityuser', 'core_reportbuilder');
74
    }
76
    }
Línea 75... Línea 77...
75
 
77
 
76
    /**
78
    /**
77
     * Initialise the entity, add all user fields and all 'visible' user profile fields
79
     * Initialise the entity
78
     *
80
     *
79
     * @return base
81
     * @return base
80
     */
82
     */
-
 
83
    public function initialise(): base {
-
 
84
        $tablealias = $this->get_table_alias('user');
81
    public function initialise(): base {
85
 
-
 
86
        $userprofilefields = (new user_profile_fields(
-
 
87
            "{$tablealias}.id",
-
 
88
            $this->get_entity_name(),
-
 
89
        ))
Línea 82... Línea 90...
82
        $userprofilefields = $this->get_user_profile_fields();
90
            ->add_joins($this->get_joins());
83
 
91
 
84
        $columns = array_merge($this->get_all_columns(), $userprofilefields->get_columns());
92
        $columns = array_merge($this->get_all_columns(), $userprofilefields->get_columns());
85
        foreach ($columns as $column) {
93
        foreach ($columns as $column) {
Línea -... Línea 94...
-
 
94
            $this->add_column($column);
86
            $this->add_column($column);
95
        }
87
        }
96
 
88
 
-
 
89
        $filters = array_merge($this->get_all_filters(), $userprofilefields->get_filters());
97
        // All the filters defined by the entity can also be used as conditions.
90
        foreach ($filters as $filter) {
-
 
91
            $this->add_filter($filter);
-
 
92
        }
98
        $filters = array_merge($this->get_all_filters(), $userprofilefields->get_filters());
93
 
99
        foreach ($filters as $filter) {
94
        $conditions = array_merge($this->get_all_filters(), $userprofilefields->get_filters());
100
            $this
Línea 95... Línea 101...
95
        foreach ($conditions as $condition) {
101
                ->add_condition($filter)
96
            $this->add_condition($condition);
102
                ->add_filter($filter);
Línea 97... Línea 103...
97
        }
103
        }
98
 
-
 
99
        return $this;
-
 
100
    }
-
 
101
 
-
 
102
    /**
-
 
103
     * Get user profile fields helper instance
-
 
104
     *
-
 
105
     * @return user_profile_fields
-
 
106
     */
-
 
107
    protected function get_user_profile_fields(): user_profile_fields {
-
 
108
        $userprofilefields = new user_profile_fields($this->get_table_alias('user') . '.id', $this->get_entity_name());
-
 
109
        $userprofilefields->add_joins($this->get_joins());
104
 
110
        return $userprofilefields;
105
        return $this;
111
    }
106
    }
112
 
107
 
113
    /**
108
    /**
Línea 200... Línea 195...
200
            new lang_string('fullname'),
195
            new lang_string('fullname'),
201
            $this->get_entity_name()
196
            $this->get_entity_name()
202
        ))
197
        ))
203
            ->add_joins($this->get_joins())
198
            ->add_joins($this->get_joins())
204
            ->add_fields($fullnameselect)
199
            ->add_fields($fullnameselect)
205
            ->set_type(column::TYPE_TEXT)
-
 
206
            ->set_is_sortable($this->is_sortable('fullname'), $fullnamesort)
200
            ->set_is_sortable(true, $fullnamesort)
207
            ->add_callback(static function(?string $value, stdClass $row) use ($viewfullnames): string {
201
            ->add_callback(static function($value, stdClass $row) use ($viewfullnames): string {
-
 
202
 
208
                if ($value === null) {
203
                // Ensure we have at least one field present.
-
 
204
                if (count(array_filter((array) $row, fn($field) => $field !== null)) === 0) {
209
                    return '';
205
                    return '';
210
                }
206
                }
Línea 211... Línea 207...
211
 
207
 
212
                // Ensure we populate all required name properties.
208
                // Ensure we populate all required name properties.
Línea 231... Línea 227...
231
                $this->get_entity_name()
227
                $this->get_entity_name()
232
            ))
228
            ))
233
                ->add_joins($this->get_joins())
229
                ->add_joins($this->get_joins())
234
                ->add_fields($fullnameselect)
230
                ->add_fields($fullnameselect)
235
                ->add_field("{$usertablealias}.id")
231
                ->add_field("{$usertablealias}.id")
236
                ->set_type(column::TYPE_TEXT)
-
 
237
                ->set_is_sortable($this->is_sortable($fullnamefield), $fullnamesort)
232
                ->set_is_sortable(true, $fullnamesort)
238
                ->add_callback(static function(?string $value, stdClass $row) use ($fullnamefield, $viewfullnames): string {
233
                ->add_callback(static function($value, stdClass $row) use ($fullnamefield, $viewfullnames): string {
239
                    global $OUTPUT;
234
                    global $OUTPUT;
Línea 240... Línea 235...
240
 
235
 
-
 
236
                    // Ensure we have at least one field present.
241
                    if ($value === null) {
237
                    if (count(array_filter((array) $row, fn($field) => $field !== null)) === 0) {
242
                        return '';
238
                        return '';
Línea 243... Línea 239...
243
                    }
239
                    }
244
 
240
 
Línea 260... Línea 256...
260
                        return html_writer::link(new moodle_url('/user/profile.php', ['id' => $row->id]),
256
                        return html_writer::link(new moodle_url('/user/profile.php', ['id' => $row->id]),
261
                            $OUTPUT->user_picture($row, ['link' => false, 'alttext' => false]) .
257
                            $OUTPUT->user_picture($row, ['link' => false, 'alttext' => false]) .
262
                            fullname($row, $viewfullnames));
258
                            fullname($row, $viewfullnames));
263
                    }
259
                    }
Línea 264... Línea 260...
264
 
260
 
265
                    return $value;
261
                    return (string) $value;
Línea 266... Línea 262...
266
                });
262
                });
267
 
263
 
268
            // Picture fields need some more data.
264
            // Picture fields need some more data.
Línea 274... Línea 270...
274
        }
270
        }
Línea 275... Línea 271...
275
 
271
 
276
        // Picture column.
272
        // Picture column.
277
        $columns[] = (new column(
273
        $columns[] = (new column(
278
            'picture',
274
            'picture',
279
            new lang_string('userpicture', 'core_reportbuilder'),
275
            new lang_string('picture'),
280
            $this->get_entity_name()
276
            $this->get_entity_name()
281
        ))
277
        ))
282
            ->add_joins($this->get_joins())
278
            ->add_joins($this->get_joins())
283
            ->add_fields($userpictureselect)
-
 
284
            ->set_type(column::TYPE_INTEGER)
-
 
285
            ->set_is_sortable($this->is_sortable('picture'))
-
 
286
            // It doesn't make sense to offer integer aggregation methods for this column.
-
 
287
            ->set_disabled_aggregation(['avg', 'max', 'min', 'sum'])
279
            ->add_fields($userpictureselect)
288
            ->add_callback(static function ($value, stdClass $row): string {
280
            ->add_callback(static function($value, stdClass $row): string {
Línea 289... Línea 281...
289
                global $OUTPUT;
281
                global $OUTPUT;
290
 
282
 
Línea 291... Línea 283...
291
                return !empty($row->id) ? $OUTPUT->user_picture($row, ['link' => false, 'alttext' => false]) : '';
283
                return !empty($row->id) ? $OUTPUT->user_picture($row, ['link' => false, 'alttext' => false]) : '';
292
            });
284
            });
293
 
285
 
294
        // Add all other user fields.
286
        // Add all other user fields.
Línea 295... Línea -...
295
        $userfields = $this->get_user_fields();
-
 
296
        foreach ($userfields as $userfield => $userfieldlang) {
-
 
297
            $columntype = $this->get_user_field_type($userfield);
-
 
298
 
-
 
299
            $columnfieldsql = "{$usertablealias}.{$userfield}";
-
 
300
            if ($columntype === column::TYPE_LONGTEXT && $DB->get_dbfamily() === 'oracle') {
287
        $userfields = $this->get_user_fields();
301
                $columnfieldsql = $DB->sql_order_by_text($columnfieldsql, 1024);
288
        foreach ($userfields as $userfield => $userfieldlang) {
302
            }
289
            $columntype = $this->get_user_field_type($userfield);
303
 
290
 
304
            $column = (new column(
291
            $column = (new column(
305
                $userfield,
292
                $userfield,
306
                $userfieldlang,
293
                $userfieldlang,
307
                $this->get_entity_name()
294
                $this->get_entity_name()
308
            ))
295
            ))
309
                ->add_joins($this->get_joins())
296
                ->add_joins($this->get_joins())
Línea 310... Línea 297...
310
                ->set_type($columntype)
297
                ->set_type($columntype)
311
                ->add_field($columnfieldsql, $userfield)
298
                ->add_field("{$usertablealias}.{$userfield}")
312
                ->set_is_sortable($this->is_sortable($userfield))
299
                ->set_is_sortable(true)
Línea 327... Línea 314...
327
 
314
 
328
        return $columns;
315
        return $columns;
Línea 329... Línea 316...
329
    }
316
    }
330
 
-
 
331
    /**
-
 
332
     * Check if this field is sortable
-
 
333
     *
-
 
334
     * @param string $fieldname
-
 
335
     * @return bool
-
 
336
     */
-
 
337
    protected function is_sortable(string $fieldname): bool {
-
 
338
        // Some columns can't be sorted, like longtext or images.
-
 
339
        $nonsortable = [
-
 
340
            'description',
-
 
341
            'picture',
-
 
342
        ];
-
 
343
 
-
 
344
        return !in_array($fieldname, $nonsortable);
-
 
345
    }
-
 
346
 
317
 
347
    /**
318
    /**
348
     * Formats the user field for display.
319
     * Formats the user field for display.
349
     *
320
     *
350
     * @param mixed $value Current field value.
321
     * @param mixed $value Current field value.
Línea 363... Línea 334...
363
            return format::userdate($value, $row);
334
            return format::userdate($value, $row);
364
        }
335
        }
Línea 365... Línea 336...
365
 
336
 
366
        // If the column has corresponding filter, determine the value from its options.
337
        // If the column has corresponding filter, determine the value from its options.
367
        $options = $this->get_options_for($fieldname);
338
        $options = $this->get_options_for($fieldname);
368
        if ($options !== null && array_key_exists($value, $options)) {
339
        if ($options !== null && $value !== null && array_key_exists($value, $options)) {
369
            return $options[$value];
340
            return $options[$value];
Línea 370... Línea 341...
370
        }
341
        }
371
 
342
 
Línea 396... Línea 367...
396
     */
367
     */
397
    public static function get_name_fields_select(string $usertablealias = 'u'): string {
368
    public static function get_name_fields_select(string $usertablealias = 'u'): string {
Línea 398... Línea 369...
398
 
369
 
Línea 399... Línea -...
399
        $namefields = fields::get_name_fields(true);
-
 
400
 
-
 
401
        // Create a dummy user object containing all name fields.
370
        $namefields = fields::get_name_fields(true);
402
        $dummyuser = (object) array_combine($namefields, $namefields);
371
 
Línea 403... Línea 372...
403
        $viewfullnames = has_capability('moodle/site:viewfullnames', context_system::instance());
372
        $viewfullnames = has_capability('moodle/site:viewfullnames', context_system::instance());
404
        $dummyfullname = fullname($dummyuser, $viewfullnames);
373
        $dummyfullname = core_user::get_dummy_fullname(null, ['override' => $viewfullnames]);
Línea 405... Línea 374...
405
 
374
 
Línea 427... Línea 396...
427
            'firstname' => new lang_string('firstname'),
396
            'firstname' => new lang_string('firstname'),
428
            'lastname' => new lang_string('lastname'),
397
            'lastname' => new lang_string('lastname'),
429
            'email' => new lang_string('email'),
398
            'email' => new lang_string('email'),
430
            'city' => new lang_string('city'),
399
            'city' => new lang_string('city'),
431
            'country' => new lang_string('country'),
400
            'country' => new lang_string('country'),
-
 
401
            'lang' => new lang_string('language'),
-
 
402
            'timezone' => new lang_string('timezone'),
432
            'theme' => new lang_string('theme'),
403
            'theme' => new lang_string('theme'),
433
            'description' => new lang_string('description'),
404
            'description' => new lang_string('description'),
434
            'firstnamephonetic' => new lang_string('firstnamephonetic'),
405
            'firstnamephonetic' => new lang_string('firstnamephonetic'),
435
            'lastnamephonetic' => new lang_string('lastnamephonetic'),
406
            'lastnamephonetic' => new lang_string('lastnamephonetic'),
436
            'middlename' => new lang_string('middlename'),
407
            'middlename' => new lang_string('middlename'),
Línea 485... Línea 456...
485
     * Return list of all available filters
456
     * Return list of all available filters
486
     *
457
     *
487
     * @return filter[]
458
     * @return filter[]
488
     */
459
     */
489
    protected function get_all_filters(): array {
460
    protected function get_all_filters(): array {
490
        global $DB;
-
 
491
 
-
 
492
        $filters = [];
-
 
493
        $tablealias = $this->get_table_alias('user');
461
        $tablealias = $this->get_table_alias('user');
Línea 494... Línea 462...
494
 
462
 
495
        // Fullname filter.
463
        // Fullname filter.
496
        $canviewfullnames = has_capability('moodle/site:viewfullnames', context_system::instance());
464
        $canviewfullnames = has_capability('moodle/site:viewfullnames', context_system::instance());
Línea 503... Línea 471...
503
            $fullnamesql,
471
            $fullnamesql,
504
            $fullnameparams
472
            $fullnameparams
505
        ))
473
        ))
506
            ->add_joins($this->get_joins());
474
            ->add_joins($this->get_joins());
Línea -... Línea 475...
-
 
475
 
-
 
476
        // Picture filter.
-
 
477
        $filters[] = (new filter(
-
 
478
            boolean_select::class,
-
 
479
            'picture',
-
 
480
            new lang_string('picture'),
-
 
481
            $this->get_entity_name(),
-
 
482
            "CASE WHEN {$tablealias}.picture > 0 THEN 1 ELSE 0 END",
-
 
483
        ))
-
 
484
            ->add_joins($this->get_joins());
507
 
485
 
508
        // User fields filters.
486
        // User fields filters.
509
        $fields = $this->get_user_fields();
487
        $fields = $this->get_user_fields();
510
        foreach ($fields as $field => $name) {
-
 
511
            $filterfieldsql = "{$tablealias}.{$field}";
-
 
512
            if ($this->get_user_field_type($field) === column::TYPE_LONGTEXT) {
-
 
513
                $filterfieldsql = $DB->sql_cast_to_char($filterfieldsql);
-
 
514
            }
-
 
515
 
488
        foreach ($fields as $field => $name) {
516
            $optionscallback = [static::class, 'get_options_for_' . $field];
489
            $optionscallback = [static::class, 'get_options_for_' . $field];
517
            if (is_callable($optionscallback)) {
490
            if (is_callable($optionscallback)) {
518
                $classname = select::class;
491
                $classname = select::class;
519
            } else if ($this->get_user_field_type($field) === column::TYPE_BOOLEAN) {
492
            } else if ($this->get_user_field_type($field) === column::TYPE_BOOLEAN) {
Línea 527... Línea 500...
527
            $filter = (new filter(
500
            $filter = (new filter(
528
                $classname,
501
                $classname,
529
                $field,
502
                $field,
530
                $name,
503
                $name,
531
                $this->get_entity_name(),
504
                $this->get_entity_name(),
532
                $filterfieldsql
505
                "{$tablealias}.{$field}"
533
            ))
506
            ))
534
                ->add_joins($this->get_joins());
507
                ->add_joins($this->get_joins());
Línea 535... Línea 508...
535
 
508
 
536
            // Populate filter options by callback, if available.
509
            // Populate filter options by callback, if available.
Línea 595... Línea 568...
595
    public static function get_options_for_country(): array {
568
    public static function get_options_for_country(): array {
596
        return get_string_manager()->get_list_of_countries();
569
        return get_string_manager()->get_list_of_countries();
597
    }
570
    }
Línea 598... Línea 571...
598
 
571
 
-
 
572
    /**
-
 
573
     * List of options for the field lang.
-
 
574
     *
-
 
575
     * @return string[]
-
 
576
     */
-
 
577
    public static function get_options_for_lang(): array {
-
 
578
        return get_string_manager()->get_list_of_translations();
-
 
579
    }
-
 
580
 
-
 
581
    /**
-
 
582
     * List of options for the field timezone.
-
 
583
     *
-
 
584
     * @return string[]
-
 
585
     */
-
 
586
    public static function get_options_for_timezone(): array {
-
 
587
        return core_date::get_list_of_timezones(null, true);
-
 
588
    }
-
 
589
 
599
    /**
590
    /**
600
     * List of options for the field theme.
591
     * List of options for the field theme.
601
     *
592
     *
602
     * @return string[]
593
     * @return string[]
603
     */
594
     */