-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_attestoodle.php
356 lines (320 loc) · 14.1 KB
/
block_attestoodle.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?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/>.
/**
* Block attestoodle is defined here.
*
* @package block_attestoodle
* @copyright 2019 Marc Leconte <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
use tool_attestoodle\factories\trainings_factory;
use tool_attestoodle\factories\learners_factory;
require_once(dirname(__FILE__).'/../../admin/tool/attestoodle/lib.php');
/**
* attestoodle block.
*
* @package block_attestoodle
* @copyright 2019 Marc Leconte <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_attestoodle extends block_base {
/**
* Initializes class member variables.
*/
public function init() {
// Needed by Moodle to differentiate between blocks.
$this->title = get_string('pluginname', 'block_attestoodle');
}
/**
* Act on instance data.
*/
public function specialization() {
if (!$this->config) {
$this->config = new stdClass();
$this->config->displayratiomilestones = true;
$this->config->displaynextcertificate = true;
$this->config->displayprogress = true;
$this->config->displaycertificatelist = true;
}
}
/**
* Returns the block contents.
*
* @return stdClass The block contents.
*/
public function get_content() {
global $CFG, $OUTPUT, $USER, $DB, $COURSE;
if ($this->content !== null) {
return $this->content;
}
if (empty($this->instance)) {
$this->content = '';
return $this->content;
}
$this->content = new stdClass();
$this->content->items = array();
$this->content->icons = array();
$this->content->footer = '';
$idtraining = optional_param('idtraining', '-1', PARAM_INT);
// Get training's value if student of one training.
$request = "select * from {tool_attestoodle_training}
where id in (select trainingid from {tool_attestoodle_milestone} where course = ?)
and id in (select trainingid from {tool_attestoodle_learner} where userid = ?)";
$records = $DB->get_records_sql($request, array($COURSE->id, $USER->id));
$nametraining = "";
$categoryid = "";
$trainingid = -1;
$enddate = -1;
$startdate = -1;
$duration = -1;
$student = 0;
$nextlaunch = 0;
$trainingsarray = array();
$pass = true;
foreach ($records as $record) {
if ($idtraining != -1) {
$pass = $idtraining == $record->id;
}
$trainingsarray[$record->id] = $record->name;
if ($pass) {
$nametraining = $record->name;
$categoryid = $record->categoryid;
$trainingid = $record->id;
$enddate = $record->enddate;
$startdate = $record->startdate;
$duration = $record->duration;
$nextlaunch = $record->nextlaunch;
$student++;
}
}
// Learner not enrolled in the training.
if ($student == 0) {
$coursecontext = context_course::instance($COURSE->id);
if (!has_capability('moodle/course:viewhiddensections', $coursecontext)) {
return $this->content;
}
}
// All milestones of all trainings (Teacher).
if ($student == 0) {
$records = $DB->get_records('tool_attestoodle_milestone', array('course' => $COURSE->id));
} else {
$records = $DB->get_records('tool_attestoodle_milestone', array('course' => $COURSE->id, 'trainingid' => $trainingid));
}
$nb = 0;
$tab = array();
$tabjalons = '[';
$tabcredit = '[';
$tabformation = array();
$tabtraining = array();
foreach ($records as $record) {
if (!isset($tabtraining[$record->trainingid])) {
$tabtraining[$record->trainingid] = 1;
}
$nb++;
if (!isset($tab[$record->moduleid])) {
$tab[$record->moduleid] = $record->moduleid;
$tabjalons .= '"' . $record->moduleid . '",';
if ($student == 0) {
$tabcredit .= '"Jalon",';
} else {
$tabcredit .= '"' . $record->creditedtime . ' min.",';
}
}
if (!isset($tabformation[$record->trainingid])) {
$tabformation[$record->trainingid] = $record->creditedtime;
} else {
$tabformation[$record->trainingid] += $record->creditedtime;
}
}
$tabjalons .= ']';
$tabcredit .= ']';
$scrpt = '<script langage="javascript">
var lienok =' . $tabjalons . ';
var infocredit = ' . $tabcredit . ';
var liens = document.getElementsByClassName("activity");
for (var i = 0; i < liens.length; i++) {
var status = liens[i].getAttribute("id");
var reste = status.split("module-");
for (var j = 0; j < lienok.length; j++) {
if (lienok[j] == reste[1]) {
img = document.createElement("img");
img.src = "../blocks/attestoodle/pix/neo2.gif";
img.title = infocredit[j];
listdiv = liens[i].getElementsByClassName("actions");
if (listdiv.length > 0) {
lsdivs = listdiv[0].getElementsByTagName("div");
if (lsdivs.length >0) {
lsdivs[0].appendChild(img);
} else {
listdiv[0].appendChild(img);
}
} else {
liens[i].appendChild(img);
}
}
}
}
</script>';
$this->content->text = $scrpt;
if ($student == 0 && $this->page->user_is_editing() && $nb > 0) {
$this->content->text .= '<div class="p-3 mb-2 bg-danger text-white">';
$this->content->text .= get_string('warning', 'block_attestoodle') . '</div>';
return $this->content;
}
$tabtrainingids = array();
foreach ($tabtraining as $key => $value) {
$tabtrainingids[] = $key;
}
$resulttrainings = $DB->get_records_list('tool_attestoodle_training', 'id', $tabtrainingids, $sort = 'name');
if ($student == 0) {
$this->content->text .= get_string('numbertraining', 'block_attestoodle') . count($tabtraining) . "<br/>";
$this->content->text .= get_string('numbermilestones', 'block_attestoodle') . $nb ."<br/>";
// No training for this course.
if ($nb == 0) {
return $this->content;
}
$this->content->text .= "<br/><b>" . get_string('traininglist', 'block_attestoodle') ."</b><br/>";
foreach ($resulttrainings as $res) {
$this->content->text .= $res->name . "<br/>";
}
}
$format = get_string('dateformat', 'block_attestoodle');
if ($student > 0) {
if (count($trainingsarray) > 1) {
foreach ($trainingsarray as $key => $res) {
if ($key != $trainingid) {
$lnk = \html_writer::link(
new \moodle_url('/course/view.php', array('id' => $COURSE->id, 'idtraining' => $key)),
$res);
$this->content->text .= $lnk."</br>";
}
}
}
$this->content->text .= "<p class='bg-info'>" . $nametraining ."</p>";
// Time credited for training.
trainings_factory::get_instance()->create_training_by_category($categoryid, $trainingid);
$training = trainings_factory::get_instance()->retrieve_training_by_id($trainingid);
$training->get_learners();
$learner = learners_factory::get_instance()->retrieve_learner($USER->id);
$now = new \DateTime();
$totalmarkerperiod = $learner->get_total_milestones($categoryid, null, $now);
// Past activities in this course.
$validatedactivities = $learner->get_validated_activities();
$nbact = 0;
$tabcourse = array();
foreach ($validatedactivities as $va) {
$act = $va->get_activity();
$idcourse = $act->get_course()->get_id();
if ($idcourse == $COURSE->id && $act->get_milestone() > 0) {
$nbact++;
}
if (!isset($tabcourse[$idcourse])) {
$tabcourse[$idcourse] = $act->get_course()->get_name();
}
}
if ($this->config->displayratiomilestones) {
$this->content->text .= get_string('coursemilestones', 'block_attestoodle') . $nbact;
$this->content->text .= " / " . count($tab) . "<br/>";
}
$totalmarkers = parse_minutes_to_hours($totalmarkerperiod);
$libtotal = get_string('timecredited', 'tool_attestoodle');
$this->content->text .= $libtotal . ' : ' . $totalmarkers;
if ($this->config->displaynextcertificate && $nextlaunch != 0 && $nextlaunch != null) {
$this->content->text .= "<br/>". get_string('nextdeadline', 'block_attestoodle') . date($format, $nextlaunch);
}
if ($this->config->displayprogress && $enddate > 0 && $duration > 0) {
$totaltimeduration = $enddate - $startdate;
$ecoule = $now->getTimestamp() - $startdate;
$pctimepast = round (($ecoule / $totaltimeduration) * 100.0, 2);
if ($pctimepast > 100.00) {
$pctimepast = 100.00;
}
$pccredited = round(($totalmarkerperiod / $duration) * 100.0, 2);
$diff = $pccredited - $pctimepast;
$etatdiff = "<p class='bg-warning'>" . get_string('intime', 'block_attestoodle') . $diff . " % </p>";
if ($diff > 5) {
$etatdiff = "<p class='bg-success'>" . get_string('inadvance', 'block_attestoodle') . $diff . " % </p>";
}
if ($diff < -5) {
$diff = -$diff;
$etatdiff = "<p class='bg-danger'>" . get_string('lateby', 'block_attestoodle') . $diff . " % </p>";
}
$this->content->text .= $etatdiff;
}
// Links to courses from the same training.
$request = "select * from {course}
where id in (select course from {tool_attestoodle_milestone} where trainingid = ?)";
$records = $DB->get_records_sql($request, array($trainingid));
if (count($records) > 1) {
$this->content->text .= "<hr/><p class='bg-secondary'>" . get_string('coursestraining', 'block_attestoodle');
$this->content->text .= "</p>";
foreach ($records as $record) {
if ($record->id != $COURSE->id) {
$this->content->text .= '<a href="./view.php?id=' . $record->id .'">'. $record->shortname .'</a><br/>';
} else {
$this->content->text .= $record->shortname .'<br/>';
}
}
}
if ($this->config->displaycertificatelist && $this->existcertif($trainingid, $USER->id)) {
$params = array('instanceid' => $this->instance->id, 'courseid' => $this->page->course->id,
'trainingid' => $trainingid, 'learnerid' => $USER->id);
$urlcertif = new moodle_url('/blocks/attestoodle/certif.php', $params);
$labelcertif = get_string('certificate', 'block_attestoodle');
$optionsbtn = array('class' => 'overviewButton');
$this->content->text .= '<br/><center>';
$this->content->text .= $OUTPUT->single_button($urlcertif, $labelcertif, 'get', $optionsbtn);
$this->content->text .= '</center>';
}
}
return $this->content;
}
/**
* Tests whether the learner has certificates for the training.
* @param int $trainingid The training identifier.
* @param int $userid The learner's identifier.
* @return boolean True if the learner has certificates for this training.
*/
public function existcertif($trainingid, $userid) {
global $DB;
$req = "select *
from {tool_attestoodle_certif_log} c
join {tool_attestoodle_launch_log} l on c.launchid = l.id
where trainingid = ? and learnerid = ?
order by enddate desc";
$records = $DB->get_records_sql($req, array($trainingid, $userid));
return count($records);
}
/**
* Where loack can be add.
*/
public function applicable_formats() {
return array('all' => false, 'course-view' => true);
}
/**
* No multiple instance for this block.
*/
public function instance_allow_multiple() {
return false;
}
/**
* No file settings.php for this block.
*/
public function has_config() {
return false;
}
}