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 |
/**
|
|
|
18 |
* Recalculate historical session durations.
|
|
|
19 |
*
|
|
|
20 |
* @package block_dedication
|
|
|
21 |
* @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
define('CLI_SCRIPT', true);
|
|
|
26 |
|
|
|
27 |
use block_dedication\lib\utils;
|
|
|
28 |
|
|
|
29 |
require('../../../config.php');
|
|
|
30 |
require_once($CFG->libdir.'/clilib.php');
|
|
|
31 |
require_once($CFG->libdir.'/adminlib.php');
|
|
|
32 |
|
|
|
33 |
$help =
|
|
|
34 |
"Recalculate session durations.
|
|
|
35 |
|
|
|
36 |
Options:
|
|
|
37 |
--start=timestamp Start from this date (unix timestamp)
|
|
|
38 |
--end=timestamp End at this date (unix timestamp)
|
|
|
39 |
--truncate Truncate table before running. (dangerous - deletes all existing data.)
|
|
|
40 |
-h, --help Print out this help.
|
|
|
41 |
|
|
|
42 |
Example:
|
|
|
43 |
\$ sudo -u www-data /usr/bin/php blocks/dedication/cli/recalculate.php --start=1637478193 --end=1653116593
|
|
|
44 |
";
|
|
|
45 |
|
|
|
46 |
list($options, $unrecognized) = cli_get_params(
|
|
|
47 |
array(
|
|
|
48 |
'start' => null,
|
|
|
49 |
'end' => null,
|
|
|
50 |
'truncate' => false,
|
|
|
51 |
'help' => false,
|
|
|
52 |
),
|
|
|
53 |
array(
|
|
|
54 |
'h' => 'help',
|
|
|
55 |
)
|
|
|
56 |
);
|
|
|
57 |
|
|
|
58 |
if ($options['help'] || $options['start'] === null || $options['end'] === null) {
|
|
|
59 |
echo $help;
|
|
|
60 |
exit(0);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
|
|
|
64 |
try {
|
|
|
65 |
$start = validate_param($options['start'], PARAM_INT);
|
|
|
66 |
$end = validate_param($options['end'], PARAM_INT);
|
|
|
67 |
} catch (invalid_parameter_exception $e) {
|
|
|
68 |
cli_error(get_string('invalidcharacter', 'tool_replace'));
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
if ($options['truncate']) {
|
|
|
72 |
$DB->delete_records('block_dedication');
|
|
|
73 |
echo "Truncated block_dedication table";
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
// Sanity check to make sure data doesn't exist between the values.
|
|
|
77 |
$sqlwhere = "timestart > ? AND timestart < ?";
|
|
|
78 |
if ($DB->record_exists_select('block_dedication', $sqlwhere, [$start, $end])) {
|
|
|
79 |
echo "Data already exists within the timeframe specified, you cannot import this as it may generate duplicate data ".
|
|
|
80 |
"- try with truncate if you want to delete existing data";
|
|
|
81 |
cli_heading(get_string('error'));
|
|
|
82 |
exit(1);
|
|
|
83 |
}
|
|
|
84 |
utils::generate_stats($start, $end);
|
|
|
85 |
cli_heading(get_string('success'));
|
|
|
86 |
exit(0);
|