Proyectos de Subversion Moodle

Rev

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

Rev 1 Rev 1441
Línea 17... Línea 17...
17
declare(strict_types=1);
17
declare(strict_types=1);
Línea 18... Línea 18...
18
 
18
 
Línea 19... Línea 19...
19
namespace core_reportbuilder\local\helpers;
19
namespace core_reportbuilder\local\helpers;
-
 
20
 
20
 
21
use context_user;
21
use context_user;
22
use core\{clock, di};
22
use core_user;
23
use core_user;
23
use invalid_parameter_exception;
24
use invalid_parameter_exception;
24
use stdClass;
25
use stdClass;
Línea 41... Línea 42...
41
 
42
 
42
    /**
43
    /**
43
     * Create report schedule, calculate when it should be next sent
44
     * Create report schedule, calculate when it should be next sent
44
     *
45
     *
45
     * @param stdClass $data
46
     * @param stdClass $data
46
     * @param int|null $timenow Time to use as comparison against current date (defaults to current time)
47
     * @param int|null $timenow Deprecated since Moodle 4.5 - please use {@see clock} dependency injection
47
     * @return model
48
     * @return model
48
     */
49
     */
-
 
50
    public static function create_schedule(stdClass $data, ?int $timenow = null): model {
-
 
51
        if ($timenow !== null) {
-
 
52
            debugging('Passing $timenow is deprecated, please use \core\clock dependency injection', DEBUG_DEVELOPER);
-
 
53
        }
49
    public static function create_schedule(stdClass $data, ?int $timenow = null): model {
54
 
Línea 50... Línea 55...
50
        $data->name = trim($data->name);
55
        $data->name = trim($data->name);
51
 
56
 
Línea 52... Línea 57...
52
        $schedule = (new model(0, $data));
57
        $schedule = (new model(0, $data));
53
        $schedule->set('timenextsend', self::calculate_next_send_time($schedule, $timenow));
58
        $schedule->set('timenextsend', self::calculate_next_send_time($schedule));
Línea 54... Línea 59...
54
 
59
 
Línea 134... Línea 139...
134
    /**
139
    /**
135
     * Return count of schedule report rows
140
     * Return count of schedule report rows
136
     *
141
     *
137
     * @param model $schedule
142
     * @param model $schedule
138
     * @return int
143
     * @return int
-
 
144
     *
-
 
145
     * @deprecated since Moodle 5.0 - please do not use this function any more, {@see report::get_report_row_count}
139
     */
146
     */
-
 
147
    #[\core\attribute\deprecated('report::get_report_row_count', since: '5.0', mdl: 'MDL-74488')]
140
    public static function get_schedule_report_count(model $schedule): int {
148
    public static function get_schedule_report_count(model $schedule): int {
141
        global $DB;
-
 
142
 
-
 
143
        $table = custom_report_table_view::create($schedule->get('reportid'));
149
        \core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
144
        $table->setup();
-
 
Línea 145... Línea 150...
145
 
150
 
146
        return $DB->count_records_sql($table->countsql, $table->countparams);
151
        return report::get_report_row_count($schedule->get('reportid'));
Línea 147... Línea 152...
147
    }
152
    }
148
 
153
 
149
    /**
154
    /**
Línea 156... Línea 161...
156
        global $CFG, $USER;
161
        global $CFG, $USER;
Línea 157... Línea 162...
157
 
162
 
Línea 158... Línea 163...
158
        require_once("{$CFG->libdir}/filelib.php");
163
        require_once("{$CFG->libdir}/filelib.php");
159
 
-
 
160
        $table = custom_report_table_view::create($schedule->get('reportid'));
164
 
161
 
165
        $table = custom_report_table_view::create($schedule->get('reportid'));
Línea 162... Línea 166...
162
        $table->setup();
166
        $table->setup();
163
        $table->query_db(0, false);
167
        $table->query_db(0, false);
Línea 207... Línea 211...
207
    public static function should_send_schedule(model $schedule): bool {
211
    public static function should_send_schedule(model $schedule): bool {
208
        if (!$schedule->get('enabled')) {
212
        if (!$schedule->get('enabled')) {
209
            return false;
213
            return false;
210
        }
214
        }
Línea 211... Línea 215...
211
 
215
 
Línea 212... Línea 216...
212
        $timenow = time();
216
        $timenow = di::get(clock::class)->time();
213
 
217
 
214
        // Ensure we've reached the initial scheduled start time.
218
        // Ensure we've reached the initial scheduled start time.
215
        $timescheduled = $schedule->get('timescheduled');
219
        $timescheduled = $schedule->get('timescheduled');
Línea 229... Línea 233...
229
    /**
233
    /**
230
     * Calculate the next time a schedule should be sent, based on it's recurrence and when it was initially scheduled. Ensures
234
     * Calculate the next time a schedule should be sent, based on it's recurrence and when it was initially scheduled. Ensures
231
     * returned value is after the current date
235
     * returned value is after the current date
232
     *
236
     *
233
     * @param model $schedule
237
     * @param model $schedule
234
     * @param int|null $timenow Time to use as comparison against current date (defaults to current time)
238
     * @param int|null $timenow Deprecated since Moodle 4.5 - please use {@see clock} dependency injection
235
     * @return int
239
     * @return int
236
     */
240
     */
237
    public static function calculate_next_send_time(model $schedule, ?int $timenow = null): int {
241
    public static function calculate_next_send_time(model $schedule, ?int $timenow = null): int {
238
        global $CFG;
242
        global $CFG;
Línea -... Línea 243...
-
 
243
 
-
 
244
        if ($timenow !== null) {
-
 
245
            debugging('Passing $timenow is deprecated, please use \core\clock dependency injection', DEBUG_DEVELOPER);
-
 
246
        }
239
 
247
 
Línea 240... Línea 248...
240
        $timenow = $timenow ?? time();
248
        $timenow = di::get(clock::class)->time();
241
 
249
 
Línea 242... Línea 250...
242
        $recurrence = $schedule->get('recurrence');
250
        $recurrence = $schedule->get('recurrence');
Línea 287... Línea 295...
287
        $timestamp = make_timestamp($year, $month, $day, $hour, $minute, 0, $CFG->timezone);
295
        $timestamp = make_timestamp($year, $month, $day, $hour, $minute, 0, $CFG->timezone);
288
        if ($timestamp < $timenow) {
296
        if ($timestamp < $timenow) {
289
            // Ensure we don't modify anything in the original model.
297
            // Ensure we don't modify anything in the original model.
290
            $scheduleclone = new model(0, $schedule->to_record());
298
            $scheduleclone = new model(0, $schedule->to_record());
Línea 291... Línea -...
291
 
-
 
292
            return self::calculate_next_send_time(
299
 
293
                $scheduleclone->set('timescheduled', $timestamp), $timenow);
300
            return self::calculate_next_send_time($scheduleclone->set('timescheduled', $timestamp));
294
        } else {
301
        } else {
295
            return $timestamp;
302
            return $timestamp;
296
        }
303
        }