-
Notifications
You must be signed in to change notification settings - Fork 0
/
plan.php
145 lines (119 loc) · 5.03 KB
/
plan.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Entry point on the planning of certification dates.
*
* @package tool_taskattestoodle
* @copyright 2019 [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// Main configuration importation (instanciate the $CFG global variable).
require_once(dirname(__FILE__) . '/../../../config.php');
use tool_attestoodle\factories\trainings_factory;
use tool_attestoodle\utils\db_accessor;
require_once(dirname(__FILE__) . "/classes/training_update_form.php");
use tool_taskattestoodle\forms\training_update_form;
$trainingid = required_param('trainingid', PARAM_INT);
$reinit = optional_param('reinit', 0, PARAM_INT);
$context = context_system::instance();
require_login();
$PAGE->set_context($context);
$PAGE->navbar->ignore_active();
$navlevel1 = get_string('navlevel1', 'tool_attestoodle');
$PAGE->navbar->add($navlevel1, new moodle_url('/admin/tool/attestoodle/index.php', array()));
$title = get_string('titleplanning1', 'tool_taskattestoodle');
$url = new moodle_url('/admin/tool/taskattestoodle/plan.php', array('trainingid' => $trainingid));
$PAGE->navbar->add($title, $url);
$PAGE->set_url($url);
$PAGE->set_title($title);
$PAGE->set_heading($title);
global $DB, $USER;
trainings_factory::get_instance()->create_training_by_category(1, $trainingid);
$training = trainings_factory::get_instance()->retrieve_training_by_id($trainingid);
if ($reinit == 1) {
$DB->delete_records('tool_taskattestoodle', array('trainingid' => $trainingid));
$training->set_nextlaunch(null);
db_accessor::get_instance()->updatetraining($training);
}
$records = $DB->get_records('tool_taskattestoodle', array('trainingid' => $trainingid));
$cpt = count($records);
$mform = new training_update_form($url);
if ($fromform = $mform->get_data()) {
// Process validated data.
if (isset($fromform->cancel)) {
$redirecturl = new \moodle_url('/admin/tool/attestoodle/index.php', array());
redirect($redirecturl);
return;
}
// Creating intervals.
if ($cpt == 0) {
$now = new \DateTime();
$secnow = $now->getTimestamp();
$inter = floor (($fromform->enddate - $fromform->startdate) / $fromform->nbautolaunch);
$executiondate = $fromform->startdate + ($fromform->offset * 86400);
$beginperiod = $fromform->startdate;
$auto = 0;
if (isset($fromform->auto)) {
$auto = 1;
}
$nextlaunch = 0;
$wdate = new \DateTime();
for ($i = 1; $i <= $fromform->nbautolaunch; $i++) {
$dataobject = new \stdClass();
$dataobject->trainingid = $trainingid;
$executiondate += $inter;
$wdate->setTimestamp($executiondate);
$wdate->setTime($fromform->hour, $fromform->minu);
$dataobject->executiondate = $wdate->getTimestamp();
$dataobject->beginperiod = $beginperiod;
$beginperiod += $inter;
$dataobject->endperiod = $beginperiod;
$dataobject->mailto = $fromform->email;
$dataobject->operatorid = $USER->id;
$dataobject->auto = $auto;
$dataobject->togenerate = 1;
if ($secnow > $executiondate) {
$dataobject->togenerate = 0;
} else if ($nextlaunch == 0) {
$nextlaunch = $executiondate;
}
$DB->insert_record('tool_taskattestoodle', $dataobject);
}
if ($nextlaunch == 0) {
$nextlaunch = $fromform->enddate;
}
$training->set_nextlaunch($nextlaunch);
$training->set_nbautolaunch($fromform->nbautolaunch);
$training->set_start($fromform->startdate);
$training->set_end($fromform->enddate);
db_accessor::get_instance()->updatetraining($training);
$cpt = $fromform->nbautolaunch;
}
}
if ($cpt > 0) {
// If a planning already exists, the user is redirected to the management of the attestation dates.
$redirecturl = new \moodle_url('/admin/tool/taskattestoodle/listinterval.php', array('trainingid' => $trainingid));
redirect($redirecturl);
return;
}
$entry = array ('name' => $training->get_name(),
'startdate' => $training->get_start(),
'enddate' => $training->get_end(),
'email' => $USER->email);
$mform->set_data($entry);
echo $OUTPUT->header();
$mform->display();
echo $OUTPUT->footer();