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 stdClass;
21
use stdClass;
22
use invalid_parameter_exception;
22
use invalid_parameter_exception;
23
use core\persistent;
23
use core\persistent;
24
use core_reportbuilder\datasource;
24
use core_reportbuilder\datasource;
25
use core_reportbuilder\manager;
25
use core_reportbuilder\manager;
26
use core_reportbuilder\local\models\column;
-
 
27
use core_reportbuilder\local\models\filter;
26
use core_reportbuilder\table\{custom_report_table_view, system_report_table};
28
use core_reportbuilder\local\models\report as report_model;
27
use core_reportbuilder\local\models\{audience as audience_model, column, filter, report as report_model, schedule};
29
use core_tag_tag;
28
use core_tag_tag;
Línea 30... Línea 29...
30
 
29
 
31
/**
30
/**
32
 * Helper class for manipulating custom reports and their elements (columns, filters, conditions, etc)
31
 * Helper class for manipulating custom reports and their elements (columns, filters, conditions, etc)
Línea 66... Línea 65...
66
        if (property_exists($data, "tags")) {
65
        if (property_exists($data, "tags")) {
67
            core_tag_tag::set_item_tags('core_reportbuilder', 'reportbuilder_report', $report->get('id'),
66
            core_tag_tag::set_item_tags('core_reportbuilder', 'reportbuilder_report', $report->get('id'),
68
                $report->get_context(), $data->tags);
67
                $report->get_context(), $data->tags);
69
        }
68
        }
Línea -... Línea 69...
-
 
69
 
-
 
70
        // Report custom fields.
-
 
71
        $data->id = $report->get('id');
-
 
72
        \core_reportbuilder\customfield\report_handler::create()->instance_form_save($data, true);
70
 
73
 
71
        return $report;
74
        return $report;
Línea 72... Línea 75...
72
    }
75
    }
73
 
76
 
Línea 92... Línea 95...
92
        if (property_exists($data, "tags")) {
95
        if (property_exists($data, "tags")) {
93
            core_tag_tag::set_item_tags('core_reportbuilder', 'reportbuilder_report', $report->get('id'),
96
            core_tag_tag::set_item_tags('core_reportbuilder', 'reportbuilder_report', $report->get('id'),
94
                $report->get_context(), $data->tags);
97
                $report->get_context(), $data->tags);
95
        }
98
        }
Línea -... Línea 99...
-
 
99
 
-
 
100
        // Report custom fields.
-
 
101
        \core_reportbuilder\customfield\report_handler::create()->instance_form_save($data, false);
96
 
102
 
97
        return $report;
103
        return $report;
Línea -... Línea 104...
-
 
104
    }
-
 
105
 
-
 
106
 
-
 
107
    /**
-
 
108
     * Duplicate custom report
-
 
109
     *
-
 
110
     * @param report_model $report The report to duplicate.
-
 
111
     * @param string $reportname
-
 
112
     * @param bool $duplicateaudiences
-
 
113
     * @param bool $duplicateschedules
-
 
114
     * @return report_model The duplicated report.
-
 
115
     */
-
 
116
    public static function duplicate_report(
-
 
117
        report_model $report,
-
 
118
        string $reportname,
-
 
119
        bool $duplicateaudiences,
-
 
120
        bool $duplicateschedules,
-
 
121
    ): report_model {
-
 
122
        $reportinstance = manager::get_report_from_persistent($report);
-
 
123
 
-
 
124
        // Copy the original report, removing properties to be re-created when duplicating.
-
 
125
        $record = $report->to_record();
-
 
126
        unset($record->id, $record->usercreated);
-
 
127
 
-
 
128
        // Create new report.
-
 
129
        $record->name = $reportname;
-
 
130
        $record->tags = core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report', $report->get('id'));
-
 
131
        $newreport = static::create_report($record, false);
-
 
132
 
-
 
133
        // Duplicate report content.
-
 
134
        $columns = array_map(fn($column) => $column->get_persistent(), $reportinstance->get_active_columns());
-
 
135
        static::duplicate_report_content($columns, $newreport->get('id'));
-
 
136
 
-
 
137
        $conditions = array_map(fn($condition) => $condition->get_persistent(), $reportinstance->get_active_conditions());
-
 
138
        static::duplicate_report_content($conditions, $newreport->get('id'));
-
 
139
 
-
 
140
        $filters = array_map(fn($filter) => $filter->get_persistent(), $reportinstance->get_active_filters());
-
 
141
        static::duplicate_report_content($filters, $newreport->get('id'));
-
 
142
 
-
 
143
        // Duplicate audiences.
-
 
144
        if ($duplicateaudiences) {
-
 
145
            $audiencemap = [];
-
 
146
 
-
 
147
            foreach (audience::get_base_records($report->get('id')) as $audienceinstance) {
-
 
148
 
-
 
149
                // If user can't edit the current audience then do not copy it.
-
 
150
                if (!$audienceinstance->user_can_edit()) {
-
 
151
                    continue;
-
 
152
                }
-
 
153
 
-
 
154
                $audiencerecord = $audienceinstance->get_persistent()->to_record();
-
 
155
                $audiencerecordid = $audiencerecord->id;
-
 
156
                unset($audiencerecord->id, $audiencerecord->usercreated);
-
 
157
 
-
 
158
                $newaudience = (new audience_model(0, $audiencerecord))
-
 
159
                    ->set('reportid', $newreport->get('id'))
-
 
160
                    ->create();
-
 
161
 
-
 
162
                $audiencemap[$audiencerecordid] = $newaudience->get('id');
-
 
163
            }
-
 
164
 
-
 
165
            // Duplicate schedules and map them to the new audience ids.
-
 
166
            if ($duplicateschedules) {
-
 
167
                foreach (schedule::get_records(['reportid' => $report->get('id')]) as $schedule) {
-
 
168
                    $schedulerecord = $schedule->to_record();
-
 
169
                    unset($schedulerecord->id, $schedulerecord->usercreated);
-
 
170
 
-
 
171
                    // Map new audience ids with the old ones.
-
 
172
                    $audiences = array_map(
-
 
173
                        fn($audienceid) => $audiencemap[$audienceid] ?? 0,
-
 
174
                        (array) json_decode($schedulerecord->audiences),
-
 
175
                    );
-
 
176
 
-
 
177
                    (new schedule(0, $schedulerecord))
-
 
178
                        ->set_many([
-
 
179
                            'reportid' => $newreport->get('id'),
-
 
180
                            'audiences' => json_encode($audiences),
-
 
181
                        ])
-
 
182
                        ->create();
-
 
183
                }
-
 
184
            }
-
 
185
        }
-
 
186
 
-
 
187
        // Duplicate custom fields.
-
 
188
        $reportdata = $report->to_record();
-
 
189
        \core_reportbuilder\customfield\report_handler::create()->instance_form_before_set_data($reportdata);
-
 
190
        $reportdata->id = $newreport->get('id');
-
 
191
        \core_reportbuilder\customfield\report_handler::create()->instance_form_save($reportdata);
-
 
192
 
-
 
193
        return $newreport;
98
    }
194
    }
99
 
195
 
100
    /**
196
    /**
101
     * Delete custom report
197
     * Delete custom report
102
     *
198
     *
Línea 414... Línea 510...
414
 
510
 
415
        return static::reorder_persistents_by_field($filter, $filters, $position, 'filterorder');
511
        return static::reorder_persistents_by_field($filter, $filters, $position, 'filterorder');
Línea 416... Línea 512...
416
    }
512
    }
417
 
513
 
418
    /**
-
 
419
     * Get available columns for a given report
-
 
420
     *
-
 
421
     * @param report_model $persistent
514
    /**
-
 
515
     * Get total row count for given custom or system report
-
 
516
     *
-
 
517
     * @param int $reportid
-
 
518
     * @param array $parameters Applicable for system reports only
-
 
519
     * @return int
-
 
520
     */
-
 
521
    public static function get_report_row_count(int $reportid, array $parameters = []): int {
-
 
522
        $report = new report_model($reportid);
-
 
523
        if ($report->get('type') === datasource::TYPE_CUSTOM_REPORT) {
-
 
524
            $table = custom_report_table_view::create($report->get('id'));
-
 
525
        } else {
-
 
526
            $table = system_report_table::create($report->get('id'), $parameters);
-
 
527
            $table->guess_base_url();
-
 
528
        }
-
 
529
        $table->setup();
-
 
530
        return $table->get_total_row_count();
-
 
531
    }
422
     * @return array
532
 
423
     *
533
    /**
424
     * @deprecated since Moodle 4.1 - please do not use this function any more, {@see custom_report_column_cards_exporter}
-
 
425
     */
-
 
426
    public static function get_available_columns(report_model $persistent): array {
534
     * @deprecated since Moodle 4.1 - please do not use this function any more, {@see custom_report_column_cards_exporter}
427
        debugging('The function ' . __FUNCTION__ . '() is deprecated, please do not use it any more. ' .
-
 
428
            'See \'custom_report_column_cards_exporter\' class for replacement', DEBUG_DEVELOPER);
-
 
429
 
-
 
430
        $available = [];
-
 
431
 
-
 
432
        $report = manager::get_report_from_persistent($persistent);
-
 
433
 
535
     */
434
        // Get current report columns.
-
 
435
        foreach ($report->get_columns() as $column) {
-
 
436
            $entityname = $column->get_entity_name();
536
    #[\core\attribute\deprecated('custom_report_column_cards_exporter', since: '4.1', final: true)]
437
            $entitytitle = $column->get_title();
-
 
438
            if (!array_key_exists($entityname, $available)) {
-
 
439
                $available[$entityname] = [
-
 
440
                    'name' => (string) $report->get_entity_title($entityname),
-
 
441
                    'key' => $entityname,
-
 
442
                    'items' => [],
537
    public static function get_available_columns() {
Línea -... Línea 538...
-
 
538
        \core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
443
                ];
539
    }
-
 
540
 
444
            }
541
    /**
-
 
542
     * Duplicate report content to given report ID
-
 
543
     *
-
 
544
     * @param persistent[] $persistents
445
 
545
     * @param int $reportid
446
            $available[$entityname]['items'][] = [
546
     */
447
                'name' => $entitytitle,
547
    private static function duplicate_report_content(array $persistents, int $reportid): void {
-
 
548
        foreach ($persistents as $persistent) {
448
                'identifier' => $column->get_unique_identifier(),
549
            $record = $persistent->to_record();
449
                'title' => get_string('addcolumn', 'core_reportbuilder', $entitytitle),
550
            unset($record->id, $record->usercreated);
Línea 450... Línea 551...
450
                'action' => 'report-add-column'
551
 
-
 
552
            /** @var persistent $persistentclass */
-
 
553
            $persistentclass = get_class($persistent);
-
 
554
 
451
            ];
555
            (new $persistentclass(0, $record))
Línea 452... Línea 556...
452
        }
556
                ->set('reportid', $reportid)
453
 
557
                ->create();
454
        return array_values($available);
558
        }