view_suivi_unit3.php 25.2 KB
Newer Older
Thomas Fradet's avatar
folder  
Thomas Fradet committed
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
<?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/>.

/**
 *
 * @package    format_iena
 * @category   format
 * @copyright  2018 Softia/Université lorraine
 * @author     Thomas Fradet
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

global $COURSE, $DB, $USER, $CFG;

require_once($CFG->libdir . '/completionlib.php');
$completion = new completion_info($course);
$context = context_course::instance($COURSE->id);

/* Groupes du cours avec id, nom et liste des id utilisateur de tous les membres (3 clefs d'un tableau de groupes : id, name, member) */
$groups = groups_get_all_groups($COURSE->id, 0, 0, 'g.*', false);

/* Groupe du GET provenant du sélecteur de la page du cours, ou premier groupe de l'utilisateur, ou groupe 0 (tous les groupes). */
$active_group_id = $_GET['groupid'];
// @TODO à supprimer si pas utilisé dans le JS (mettre dans la clause IF pour éviter un appel inutile si le GET existe)
$current_user_groups_ids = groups_get_user_groups($COURSE->id, $USER->id)[0];
Thomas Fradet's avatar
maj    
Thomas Fradet committed
40
41
/* Si pas de groupe dans le get ou groupe 0 (tous) mais pas le droit => premier groupe existant du user ou groupe 0 (tous) */
if ($active_group_id == NULL || ($active_group_id == 0 && !has_capability('course/iena:suivi_edit', $context, $USER->id)) ) {
Thomas Fradet's avatar
folder  
Thomas Fradet committed
42
43
44
45
46
47
	if ( count($current_user_groups_ids) == 0 ) {
		$active_group_id = 0;
	} else {
		$active_group_id = $current_user_groups_ids[0];
	}
}
Thomas Fradet's avatar
maj    
Thomas Fradet committed
48

Thomas Fradet's avatar
folder  
Thomas Fradet committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
$current_user_groups = [];
$active_group_name = "";
foreach ($groups as $group) {
	/* Récupération du nom du group actif au passage */
	if ( $group->id == $active_group_id ) {
		$active_group_name = $group->name;
	}
	foreach ($current_user_groups_ids as $ugi) {
		if ( $group->id == $ugi ) {
			$current_user_groups[] = $group;
		}
	}
}

/* Liste de tous les utilisateurs avec : 
- informations personnelles (tous les utilisateurs du cours)
- liste des activités avec état d'achèvement (si achevées, sinon vide) */
$progress = $completion->get_progress_all(
Thomas Fradet's avatar
Thomas Fradet committed
67
68
	'',
	array(),
Thomas Fradet's avatar
folder  
Thomas Fradet committed
69
	$active_group_id,
Thomas Fradet's avatar
Thomas Fradet committed
70
71
72
	'u.lastname ASC, u.firstname ASC',
	'',
	'',
Thomas Fradet's avatar
folder  
Thomas Fradet committed
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
	$context
);

/* Liste de toutes les activités du cours (sauf en attente de suppression). La liste est épurée et constitue un tableau d'objet. */
$activities = $completion->get_activities();
$modules = [];
foreach ($activities as $activity) {
	$module = new StdClass();
	$module->id = $activity->id;
	$module->name = $activity->name;
	$displayname = format_string($activity->name, true, array('context' => $activity->context));
	$module->displayname = strlen($displayname) > 20 ? mb_substr($displayname, 0, 19, 'UTF-8').'…' : $displayname;
	$module->section = $activity->section;
	$modules[] = $module;
}

// Liste des sections du cours
$modinfo = get_fast_modinfo($COURSE->id);
$sections_info_all = $modinfo->get_section_info_all();
$sections = [];
foreach ($sections_info_all as $key => $section_info) {
	$section = new StdClass();
	$section->id = $section_info->id;
	$section->name = $section_info->name === NULL ? 'Section ' . $key : $section_info->name;
	$sections[$key] = $section;
}

// Section du get acquise depuis le clic sur l'indicateur dans la page du cours, section depuis laquelle on a cliqué sur le bouton pour voir le suivi. 
$active_section_id = $_GET['sectionid'];
if ($active_section_id == NULL) {
	// no param given for section : first section (index 1, not generalities section 0)
	$active_section_id = 0;
}
// $active_section_name = "Toutes les sections";
// foreach ($sections as $section) {
// 	if ($section->id == $active_section_id) {
// 		$active_section_name = $section->name;
// 	}
// }

// Filtre actif si il existe (si la page est rechargée pour un filtre de groupe p.ex.)
Thomas Fradet's avatar
Thomas Fradet committed
114
115
116
117
if (isset($_GET['filter'])) {
	$filter = $_GET['filter'];
} else {
	$filter = 'all';
Thomas Fradet's avatar
folder  
Thomas Fradet committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
}

// echo "<pre>";
// var_dump($progress);
// var_dump($sections);
// var_dump($modules);
// var_dump($groups);
// var_dump($results);
// var_dump($current_user_groups_ids);
// var_dump($current_user_groups);
// echo "</pre>";

?>

<style>
133
134
135
136
137
138
	/* Permet la rotation des entêtes du tableau */
	th.th-rotate {
		/* Something you can count on */
		height: 140px;
		white-space: nowrap;
	}
Thomas Fradet's avatar
folder  
Thomas Fradet committed
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
	th.th-rotate > div {
		transform: 
		/* Magic Numbers */
		translate(17px, 49px)
		/* 45 is really 360 - 45 */
		rotate(315deg);
		width: 30px;
	}
	th.th-rotate > div > span {
		border-bottom: 1px solid #ccc;
		padding: 5px 10px;
	}
	/* Change la couleur une ligne sur deux dans le tableau */
	#table-body tr:nth-child(even) { background: #fafafa; }
	#table-body tr:nth-child(odd) { background: #eee; }
	/* Colorie la cellule en fonction de l'achèvement */
	.state-0 { background-color: rgba(0, 143, 132, 0.23); }
	.state-1 { background-color: #009085; }
	.state-2 { background-color: #009085; }
	.state-3 { background-color: rgba(214, 141, 1, 0.4); }


	#first-column-head {
		vertical-align: bottom;
		padding-bottom: 0.5rem;
	}
Thomas Fradet's avatar
folder  
Thomas Fradet committed
166

167
168
169
	.pointer-help {
		cursor: help;
	}
Thomas Fradet's avatar
folder  
Thomas Fradet committed
170

171
172
173
174
	.stud_perc {
		min-width: 45px;
		display: inline-block;
	}
Thomas Fradet's avatar
folder  
Thomas Fradet committed
175

176
	#table-body td { border-right: 1px solid #eee; }
Thomas Fradet's avatar
folder  
Thomas Fradet committed
177

178
179
180
181
182
183
184
185
	/* Scroll de la table */
	#table-body {
		display: block; 
		overflow-y:scroll; 
		margin-right: 10rem;
		height: 300px;
		height: 50vh; 
	}
Thomas Fradet's avatar
folder  
Thomas Fradet committed
186

187
188
189
	#suivi {
		margin-bottom: 1rem;
	}
Thomas Fradet's avatar
folder  
Thomas Fradet committed
190

191
192
193
	#suivi thead {
		display: block; overflow-x:scroll;
	}
Thomas Fradet's avatar
folder  
Thomas Fradet committed
194

195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
	#suivi thead tr th:nth-child(1) {
		width:20em;
		min-width:20em;
	}
	#suivi thead tr th {
		min-width:32px;
	}
	#suivi tbody tr th {
		width:20em;
		min-width:20em;
		word-break: break-word;
	}
	#suivi tbody tr td {
		min-width:32px;
	}
Thomas Fradet's avatar
folder  
Thomas Fradet committed
210

211
212
213
214
	#suivi input[type="checkbox"] {
		margin: 5px;
		font-size: 1rem;
	}
Thomas Fradet's avatar
folder  
Thomas Fradet committed
215

216
217
	.mb1 { margin-bottom: 1rem; }
	.mt1 { margin-top: 1rem; }
Thomas Fradet's avatar
folder  
Thomas Fradet committed
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

</style>

<div>

	<h2 class="mb1" style="display: inline-block;">Suivi des étudiants</h2>

	<div style="float: right; line-height: 3rem;">
		<?php
		if (has_capability('course/iena:suivi_edit', $context = context_course::instance($COURSE->id), $USER->id)) {
			echo '<a target="_blank" href="' . $CFG->wwwroot . "/course/bulkcompletion.php?id=" . $COURSE->id . '">Paramétrer le suivi</a> || ';
		} 
		?>
		<a target="_blank" href="<?php echo $CFG->wwwroot."/report/progress/index.php?course=".$COURSE->id ?>">Vue classique</a>
	</div>

</div>

<form style="clear: right;" class="form-inline" action="<?php echo $CFG->wwwroot."/course/format/iena/suivi_unit.php" ?>" method="GET">
	<input name="courseid" type="number" hidden="hidden" style="display: none;" value="<?php echo $COURSE->id ?>">

	<label class="sr-only" for="section-select">Section</label>
	<div class="input-group">
		<div class="input-group-prepend">
			<div class="input-group-text">Section</div>
		</div>
		<select class="custom-select mr-sm-2" id="section-select" name="sectionid">
			<option value="0">Toutes</option>
			<?php 
			foreach ($sections as $section) {
				if ($section->id == $active_section_id) {
					echo "<option selected='selected' value='$section->id'>$section->name</option>";
				} else {
					echo "<option value='$section->id'>$section->name</option>";
				}
			} ?>
		</select>
	</div>

	<label class="sr-only" for="group-select">Groupes</label>
	<div class="input-group">
		<div class="input-group-prepend">
			<div class="input-group-text">Groupes</div>
		</div>
		<select class="custom-select mr-sm-2" id="group-select" name="groupid">
			<option value="0">Tous</option>
			<?php 
			if ( count(current_user_groups) > 0 ) {
				echo '<optgroup label="Mes groupes">';
				foreach ($current_user_groups as $group) {
					if ( $group->id == $active_group_id ) {
						echo "<option selected='selected' value='$group->id'>$group->name</option>";
					} else {
						echo "<option value='$group->id'>$group->name</option>";
					}
				}
				echo '</optgroup>';
			}
			echo '<optgroup label="Autres groupes">';
			foreach ($groups as $group) {
				if ( ! in_array($group->id, $current_user_groups_ids) ) {
					if ($group->id == $active_group_id) {
						echo "<option selected='selected' value='$group->id'>$group->name</option>";
					} else {
						echo "<option value='$group->id'>$group->name</option>";
					}
				}
			} 
			echo '</optgroup>';
			?>
		</select>
	</div>

	<label class="sr-only" for="filter-select">Filtre %</label>
	<div class="input-group">
		<div class="input-group-prepend">
			<div class="input-group-text">Filtre</div>
		</div>
		<select class="custom-select mr-sm-2" id="filter-select" name="filter">
			<?php 
			$filters = [
				"all" => "Tous",
				"=100" => "100%",
				"=0" => "0%",
				"<100" => "&lt;100%",
				"<50" => "&lt;50%",
				"<25" => "&lt;25%",
				">50" => "&gt;=50%",
				">25" => "&gt;=25%",
Thomas Fradet's avatar
maj    
Thomas Fradet committed
307
				">0" => "&gt;0%",
Thomas Fradet's avatar
folder  
Thomas Fradet committed
308
			];
Thomas Fradet's avatar
Thomas Fradet committed
309
			var_dump($filter);
Thomas Fradet's avatar
folder  
Thomas Fradet committed
310
311
312
313
314
315
316
317
318
319
320
321
322
323
			foreach ($filters as $value => $name) {
				if ( $value == $filter ) {
					echo "<option selected='selected' value='$value'>$name</option>";
				} else {
					echo "<option value='$value'>$name</option>";
				}
			}
			?>
		</select>
	</div>

	<!-- <label class="my-1 mr-2" for="filter-select">Filtre %</label>
	<select class="custom-select my-1 mr-sm-2" id="filter-select" name="filter">
		<?php 
Thomas Fradet's avatar
maj    
Thomas Fradet committed
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
		// $filters = [
		// 	"all" => "Tous",
		// 	"=100" => "100%",
		// 	"=0" => "0%",
		// 	"<100" => "&lt;100%",
		// 	"<50" => "&lt;50%",
		// 	"<25" => "&lt;25%",
		// 	">50" => "&gt;50%",
		// 	">25" => "&gt;25%",
		// 	">0" => "&gt;0%",
		// ];
		// foreach ($filters as $value => $name) {
		// 	if ( $value == $filter ) {
		// 		echo "<option selected='selected' value='$value'>$name</option>";
		// 	} else {
		// 		echo "<option value='$value'>$name</option>";
		// 	}
		// }
Thomas Fradet's avatar
folder  
Thomas Fradet committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
		?>
	</select> -->

</form>

<!-- <div style="text-align: right;">
	<?php
	if (has_capability('course/iena:suivi_edit', $context = context_course::instance($COURSE->id), $USER->id)) {
		echo '<a target="_blank" href="' . $CFG->wwwroot . "/course/bulkcompletion.php?id=" . $COURSE->id . '">Paramétrer le suivi</a> || ';
	} 
	?>
	<a target="_blank" href="<?php echo $CFG->wwwroot."/report/progress/index.php?course=".$COURSE->id ?>">Vue classique</a>
</div> -->

<div class="mt1">
	<a href="#" class="select_all_studs">Sélectionner tout</a> || <a href="#" class="send_msg_sel">Message aux étudiants sélectionnés</a>
</div>

360
361
362
<div id="no_result" class="alert alert-warning" style="display: none;">Aucun étudiants trouvé. Vérifiez la section, le groupe et le filtre choisi.</div>

<div id="no_result_activity" class="alert alert-warning" style="display: none;">Aucune activité se semble suivie pour cette section. </div>
Thomas Fradet's avatar
maj    
Thomas Fradet committed
363

Thomas Fradet's avatar
folder  
Thomas Fradet committed
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<table id="suivi">
	<thead>
		<tr id="modules" style="">
			<th id="first-column-head">Étudiants</th>
		</tr>
	</thead>
	<tbody id="table-body">
	</tbody>
</table>

<a href="#" class="select_all_studs">Sélectionner tout</a> || <a href="#" class="send_msg_sel">Message aux étudiants sélectionnés</a>

<form id="send_msg_form" hidden="hidden" style="display: none;" action="<?php echo $CFG->wwwroot."/course/format/iena/suivi_unit.php?courseid=".$COURSE->id ?>" method="POST">
	<input id="send_msg_input" type="text" name="api_url" />
</form>

<div class="table-dl">
	<a href="#">Télécharger ce tableau</a>
	<p>Télécharge le tableau pour la section et le groupe sélectionné en haut de page. Les autres filtres ne sont pas pris en compte pour cet export. Sélectionnez "Tout" en haut de page pour chaque filtre si vous souhaitez le tableau complet.</p>
</div>

<script type="text/javascript">
//
var data = {
	all_selected: false,
	active_section_id: <?php echo $active_section_id; ?>,
	progress: <?php echo json_encode( array_values($progress) ); ?>,
	modules: <?php echo json_encode( $modules ); ?>,
	groups: <?php echo json_encode( $groups ); ?>,
	active_group_name: "<?php echo $active_group_name; ?>",
	sections: <?php echo json_encode( $sections ); ?>,
};
//
(function () {

	set_table_head();
	set_table_body();
	set_reactivity();

Thomas Fradet's avatar
maj    
Thomas Fradet committed
403
404
405
406
407
408
409
410
	function count_lines () {
		var rows = document.querySelectorAll("#table-body > tr"); 
		var counter = 0;
		for (var i = 0; i < rows.length; i++) {
			if (rows[i].style.display != "none") {
				counter++;
			}
		}
411
		var no_rows = false;
Thomas Fradet's avatar
maj    
Thomas Fradet committed
412
		if (counter == 0) {
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
			no_rows = true;
		}

		var cols = document.querySelectorAll("#suivi > thead > tr > th.th-rotate");
		var counter = 0;
		for (var i = 0; i < cols.length; i++) {
			if (cols[i].style.display != "none") {
				counter++;
			}
		}
		var no_cols = false;
		if (counter == 0) {
			no_cols = true;
		}

		if (no_cols) {
			document.getElementById('no_result_activity').style.display = "";
			document.getElementById('no_result').style.display = "none";
Thomas Fradet's avatar
maj    
Thomas Fradet committed
431
			document.getElementById('suivi').style.display = "none";
432
433
		} else if (no_rows) {
			document.getElementById('no_result_activity').style.display = "none";
Thomas Fradet's avatar
maj    
Thomas Fradet committed
434
			document.getElementById('no_result').style.display = "";
435
			document.getElementById('suivi').style.display = "none";
Thomas Fradet's avatar
maj    
Thomas Fradet committed
436
		} else {
437
			document.getElementById('no_result_activity').style.display = "none";
Thomas Fradet's avatar
maj    
Thomas Fradet committed
438
			document.getElementById('no_result').style.display = "none";
439
			document.getElementById('suivi').style.display = "";
Thomas Fradet's avatar
maj    
Thomas Fradet committed
440
		}
441

Thomas Fradet's avatar
maj    
Thomas Fradet committed
442
443
	}

Thomas Fradet's avatar
folder  
Thomas Fradet committed
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
	function set_table_head() {
		/* Insert la liste d'activité en entête */
		var table_heads = document.getElementById('modules');
		for (var i = 0; i < data.modules.length; i++) {
			var th = document.createElement('th');
			var div = document.createElement('div');
			var span = document.createElement('span');
			var display_name = document.createTextNode(data.modules[i].displayname);
			th.setAttribute('class', 'th-rotate');
			th.id = data.modules[i].id;
			th.dataset.fullname = data.modules[i].name;
			th.dataset.section = data.modules[i].section;
			span.append(display_name);
			div.append(span);
			th.append(div);
			table_heads.append(th);
		}
	}

	function set_table_body() {

		/* Insert une ligne d'étudiant dans le tbody */
		var table_body = document.getElementById('table-body');
		for (var i = 0; i < data.progress.length; i++) {
			var student = data.progress[i]
			/* Nom de l'étudiant */
			var tr = document.createElement('tr');
			tr.dataset.userid = student.id;
			var th = document.createElement('th');
			// th.dataset.stud_id = student.id;
			var student_full_name = document.createTextNode(student.firstname + " " + student.lastname)

			/* TODO: boutons lien vers rapports de l'étudiant */

			/* Etat d'achèvement de l'étudiant et pourcentage de l'étudiant */
			var tds = [];
			for (var j = 0; j < data.modules.length; j++) {
				var td = document.createElement('td');
				td.title = data.modules[j].name;
				td.dataset.section = data.modules[j].section;
				td.setAttribute('class', 'pointer-help state-0');
				if ( student.progress[data.modules[j].id] !== undefined ) {
					var stud_mod = student.progress[data.modules[j].id];
					td.setAttribute('class', 'pointer-help state-' + stud_mod.completionstate);
				}
				tds.push(td);
			}

			/* Check pour envoyer des message à plusieurs après avoir filtré */
			var check = document.createElement('input');
			check.type = "checkbox";
			th.append(check);

			/* Span où sera placé le pourcentage qui doit être raffraichit dynamiquement par la suite */
			var span = document.createElement('span');
			span.setAttribute('class', 'stud_perc');
			th.append(span);

			/* Icons liens vers rapports et message étudiants */
			/* Lien vers le rapport de cours résumé */
			var href = "<?php echo $CFG->wwwroot . "/report/outline/user.php?id=" ?>" + student.id + "<?php echo "&course=" . $COURSE->id . "&mode=outline" ?>";
			var icon = document.createElement('i');
			icon.setAttribute('class', 'icon fa fa-graduation-cap fa-fw');
			var link = document.createElement('a');
			link.href = href;
			link.setAttribute('target', '_blank');
			link.append(icon);
			th.append(link);
			/* Lien vers un message personnel */
			href = "<?php echo $CFG->wwwroot . "/message/index.php?id=" ?>" + student.id;
			icon = document.createElement('i');
			icon.setAttribute('class', 'icon fa fa-envelope fa-fw');
			link = document.createElement('a');
			link.href = href;
			link.setAttribute('target', '_blank');
			link.append(icon);
			th.append(link);

			th.append(student_full_name);
			tr.append(th);
			for (var k = 0; k < tds.length; k++) {
				tr.append(tds[k]);
			}
			table_body.append(tr);

		}

	}

	function set_reactivity() {

		/* Réactivité sur le sélecteur de section */
		var section_sel = document.getElementById('section-select');
		section_filter(section_sel.value);
		section_sel.onchange = function (e) {
			data.active_section_id = parseInt(e.target.value);
			section_filter(e.target.value);
			display_percentage();
			filter_students(document.getElementById('filter-select').value);
		}

		display_percentage();

		/* Réactivité sur le sélecteur de groupe */
		var group_sel = document.getElementById('group-select');
		group_sel.onchange = function () {
			this.form.submit();
		}

		/* Réactivité sur le filtre de pourcentage d'achèvement */
		var filter_sel = document.getElementById('filter-select');
		filter_students("<?php echo $filter; ?>");
		filter_sel.onchange = function (e) {
			filter_students(e.target.value);
		}

		var select_all_studs_boxes = document.querySelectorAll('.select_all_studs');
		for (var i = 0; i < select_all_studs_boxes.length; i++) {
			select_all_studs_boxes[i].onclick = function (e) {
				select_all_studs(e);
			}
		}

		/* Change le label sélectionner tout par désélectionner tout quand une case est cochée */
		var checks = document.querySelectorAll("#table-body input[type='checkbox']");
		for (var j = 0; j < checks.length; j++) {
			checks[j].onclick = function (e) {
				if ( e.target.checked ) {
					var select_all_studs_boxes = document.querySelectorAll('.select_all_studs');
					for (var k = 0; k < select_all_studs_boxes.length; k++) {
						select_all_studs_boxes[k].innerHTML = "Désélectionner tout";
					}
					data.all_selected = true;
				}
			}
		}

		var send_msg_sel_links = document.querySelectorAll('.send_msg_sel');
		for (var i = 0; i < send_msg_sel_links.length; i++) {
			send_msg_sel_links[i].onclick = function (e) {
				send_msg_sel(e);
			}
		}

		document.querySelector(".table-dl a").onclick = function (e) {
			e.preventDefault();
			table_download();
		}

	}

	function section_filter (section_id) {
		var cells = document.querySelectorAll('#suivi td, #suivi th');
		Array.prototype.forEach.call(cells, function (cell) {
			if (section_id == 0) {
				cell.style.display = '';
			} else {
				if ( cell.dataset.section != undefined && cell.dataset.section != section_id ) {
					cell.style.display = 'none';
				} else {
					cell.style.display = '';
				}
			}
		})
Thomas Fradet's avatar
maj    
Thomas Fradet committed
608
		count_lines();
Thomas Fradet's avatar
folder  
Thomas Fradet committed
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
	}

	function filter_students(filter) {
		var rows = document.querySelectorAll("#table-body tr");

		var all = filter == 'all';
		var inf = filter[0] == "<";
		var sup = filter[0] == ">";
		var equ = filter[0] == "=";
		var perc = filter.substring(1);
		if (all) {
			for (var i = 0; i < rows.length; i++) {
				rows[i].style.display = "";
			}
		} else {
			for (var i = 0; i < rows.length; i++) {
				var stud_perc = rows[i].dataset.percent;
				if (inf && parseInt(stud_perc) < perc) {
					rows[i].style.display = "";
				} else if (sup && parseInt(stud_perc) >= perc && perc != 0) {
					rows[i].style.display = "";
				} else if (sup && parseInt(stud_perc) > perc && perc == 0) {
					rows[i].style.display = "";
				} else if (equ && parseInt(stud_perc) == perc) {
					rows[i].style.display = "";
				} else {
					rows[i].style.display = "none";
				}
			}
		}
Thomas Fradet's avatar
maj    
Thomas Fradet committed
639
		count_lines();
Thomas Fradet's avatar
folder  
Thomas Fradet committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
	}

	function display_percentage() {
		var rows = document.querySelectorAll("#table-body tr");
		
		/* Calcul du nombre de modules affichés dans cette section */
		var modules = document.querySelectorAll("#modules th");
		var modules_nbr = -1;
		for (var m = 0; m < modules.length; m++) {
			if (modules[m].style.display != 'none') {
				modules_nbr++;
			}
		}

		/* Détermination du pourcentage fait et injection dans l'HTML */
		for (var i = 0; i < rows.length; i++) {
			var tds = rows[i].querySelectorAll('td');
			var done = 0;
			for (var j = 0; j < tds.length; j++) {
				var state = tds[j].getAttribute('class').replace('pointer-help state-', '');
				if ( (state == 1 || state == 2) && tds[j].style.display != "none" ) {
					done++;
				}
			}
Thomas Fradet's avatar
Thomas Fradet committed
664
665
666
667
668
			if (modules_nbr > 0) {
				var stud_perc = 100 * done / modules_nbr;
			} else {
				var stud_perc = 0;
			}
Thomas Fradet's avatar
folder  
Thomas Fradet committed
669
670
671
672
673
674
675
676
			rows[i].querySelector('span').innerHTML = Math.floor(stud_perc) + "% ";
			rows[i].dataset.percent = stud_perc;
		}

	}

	function select_all_studs(e) {
		e.preventDefault();
Thomas Fradet's avatar
maj    
Thomas Fradet committed
677
678
		var counter = 0;
		var checks = document.querySelectorAll("#table-body tr");
Thomas Fradet's avatar
folder  
Thomas Fradet committed
679
		for (var i = 0; i < checks.length; i++) {
Thomas Fradet's avatar
maj    
Thomas Fradet committed
680
681
682
683
684
			if ( checks[i].style.display != "none" || data.all_selected ) {
				var box = checks[i].querySelector("input[type='checkbox']");
				box.checked = !data.all_selected;
				counter++;
			}
Thomas Fradet's avatar
folder  
Thomas Fradet committed
685
		}
Thomas Fradet's avatar
maj    
Thomas Fradet committed
686
687
688
689
690
691
		if ( counter > 0 ) {
			var select_all_studs_boxes = document.querySelectorAll('.select_all_studs');
			for (var i = 0; i < select_all_studs_boxes.length; i++) {
				select_all_studs_boxes[i].innerHTML = data.all_selected ? "Sélectionner tout" : "Désélectionner tout"
			}
			data.all_selected = !data.all_selected;
Thomas Fradet's avatar
folder  
Thomas Fradet committed
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
		}
	}

	function send_msg_sel(e) {
		var user_ids = [];
		e.preventDefault();
		var rows = document.querySelectorAll("#table-body tr");
		for (var i = 0; i < rows.length; i++) {
			if ( rows[i].querySelector('input').checked ) {
				user_ids.push( rows[i].dataset.userid )
			}
		}
		document.getElementById('send_msg_input').value = user_ids
		document.getElementById('send_msg_form').submit();
	}

	function table_download() {
		var csvContent = "data:text/csv;charset=utf-8,";
		var head = ['Moodle_id', 'Prénom', 'Nom', 'Groupe', 'Complétion (en %)'];

		for ( var m = 0; m < data.modules.length; m++ ) {
			if ( data.active_section_id != data.modules[m].section && data.active_section_id != 0 ) {
				continue;
			}
			head.push('"'+data.modules[m].displayname.replace(',', '')+'"');
		}


		csvContent += head.join(",");
		csvContent += "\n";
		/* Pour chaque étudiant */
		for ( var s = 0; s < data.progress.length; s++ ) {
			var arr_cpl = [];

			/* Pour chaque activité du cours */
			var activities_nbr = 0;
			var done_activities_nbr = 0;
			for ( var mod = 0; mod < data.modules.length; mod++ ) {
				/* Si le module n'est pas dans la section actuellement filtrée */
				if ( data.active_section_id != data.modules[mod].section && data.active_section_id != 0 ) {
					continue;
				}

				var stud_progress = data.progress[s].progress
				/* Si l'étudiant n'a pas de progres*/
				if ( stud_progress.length == 0 ) {
					arr_cpl.push(0);
				} else {
					// var prog = 0;
					/* Pour chaque trace d'achèvement */
					var cpl = 0;
					for (progress in stud_progress) {
						/* Quand l'index de progression correspond à l'id du module */
						if ( data.modules[mod].id == progress ) {
							cpl = stud_progress[progress].completionstate;
							if (cpl == 3) {
								cpl = 0.5;
								cpl = '"' + cpl.toLocaleString() + '"';
							}
						}
					}
					if (cpl == 1 || cpl == 2) {
						done_activities_nbr++;
					}
					arr_cpl.push(cpl);
				}
				activities_nbr++;
			}

			console.log(done_activities_nbr)
			console.log(activities_nbr)
			
			var perc = Math.round(100 * 100 * done_activities_nbr / activities_nbr) / 100;
			var arr = [
			data.progress[s].id,
			data.progress[s].firstname,
			data.progress[s].lastname,
			'"' + data.active_group_name.replace('"', '') + '"',
			'"'+perc.toLocaleString()+'"',
			arr_cpl
			];
			var dataString = arr.join(",");
			csvContent += dataString + "\n";
		}
		
		// stud_cpl.forEach(function(arr, index){
		// 	var dataString = arr.join(",");
		// 	csvContent += dataString + "\n";
		// });
		
		var encodedUri = encodeURI(csvContent);

		var section_name = data.active_section_id;
		if ( section_name == 0 ) {
			section_name = "cours-complet";
		} else {
			for (var i = 0; i < data.sections.length; i++) {
				if ( data.sections[i].id == data.active_section_id ) {
					section_name = slug_it(data.sections[i].name).substring(0, 20);
				}
			}
		}
		// var section_name = slug_it(this.active_section.section_name).substring(0, 10);
		var d = new Date;
		month = d.getMonth()+1;
		date = d.getFullYear() +"."+ month.padLeft() +"."+ d.getDate().padLeft() +"-"+ d.getHours().padLeft() +"."+ d.getMinutes().padLeft() +"."+ d.getSeconds().padLeft();

		var link = document.createElement("a");
		link.setAttribute("href", encodedUri);
		link.setAttribute("download", "report-"+section_name+"-"+ date +".csv");
		document.body.appendChild(link);
		link.click();
		document.body.removeChild(link); 
	}

	function slug_it(str) {
		return str.toString().toLowerCase().trim()
		.replace(/\s+/g, '-')           /* Replace spaces with - */
		.replace(/&/g, '-and-')         /* Replace & with 'and' */
		.replace(/[^\w\-]+/g, '')       /* Remove all non-word chars */
		.replace(/\-\-+/g, '-');        /* Replace multiple - with single - */
	}

	Number.prototype.padLeft = function(base,chr){
		var  len = (String(base || 10).length - String(this).length)+1;
		return len > 0? new Array(len).join(chr || '0')+this : this;
	}

// 
})()

</script>