-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstances.php
151 lines (147 loc) · 6.42 KB
/
instances.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
<?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/>.
/**
* Gestion des instances.
*
* @package auth_entsync
* @copyright 2016 Thomas Jaisson
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
require(__DIR__ . '/../../config.php');
require_once($CFG->libdir . '/adminlib.php');
$entsync = \auth_entsync\container::services();
$conf = $entsync->query('conf');
if (!$conf->is_gw()) print_error('userautherror');
$instances = $entsync->query('instances');
$sitecontext = context_system::instance();
$returnurl = new moodle_url('/auth/entsync/instances.php');
$PAGE->set_url($returnurl);
$PAGE->set_context($sitecontext);
if (! has_capability('moodle/site:configview', $sitecontext)) print_error('userautherror');
require_login();
$is_admin = has_capability('moodle/site:config', $sitecontext);
$action = optional_param('action', 'list', PARAM_ACTION);
if (! $is_admin) $action = '';
if ($action === 'del') {
// Suppression d'une instance demandée.
$id = required_param('id', PARAM_INT);
$instance = $instances->instance($id);
$rne = $instance->get('dir');
$name = $instance->get('name');
$confirm = optional_param('confirm', '', PARAM_ALPHANUM); // The md5 confirmation hash.
// ... la suppression est-elle confirmée ?
if ($confirm != md5($id)) {
// ... non, alors on demande confirmation.
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('delete'));
$delurl = new moodle_url($returnurl,
['action' => 'del', 'id' => $id, 'confirm' => md5($id), 'sesskey' => sesskey()]);
$confirmbutt = new single_button($delurl, get_string('delete'), 'post');
echo $OUTPUT->confirm("Etes vous sur de vouloir supprimer l'instance {$rne}, {$name} ?",
$confirmbutt, $returnurl);
echo $OUTPUT->footer();
die();
} else if (confirm_sesskey() && !empty($_POST)) {
// ... oui, alors on supprime l'instance.
try {
$instance->delete();
\core\notification::success("L'instance {$rne}, {$name} a été supprimée.");
} catch (Exception $e) {
\core\notification::error($e->getMessage());
}
}
redirect($returnurl);
} else if ($action === 'edit') {
// Modification d'une instance demandée.
$id = optional_param('id', null, PARAM_INT);
$PAGE->set_url(new moodle_url('/auth/entsync/instances.php', ['id' => $id, 'action' => 'edit']));
$instance = null;
if (!empty($id)) {
$instance = $instances->instance($id);
}
$customdata = [
'persistent' => $instance,
];
$instance_form = $entsync->query('instance_form');
$form = $instance_form->createForm($PAGE->url->out(false), $customdata);
if ($form->is_cancelled()) {
redirect($returnurl);
} else if (($data = $form->get_data())) {
try {
if (empty($data->id)) {
// If we don't have an ID, we know that we must create a new record.
// Call your API to create a new persistent from this data.
// Or, do the following if you don't want capability checks (discouraged).
$instance = $instances->instance(0, $data);
$instance->create();
} else {
// We had an ID, this means that we are going to update a record.
// Call your API to update the persistent from the data.
// Or, do the following if you don't want capability checks (discouraged).
$instance->from_record($data);
$instance->update();
}
\core\notification::success(get_string('changessaved'));
} catch (Exception $e) {
\core\notification::error($e->getMessage());
}
// Modif ou création effectuée, donc redirection vers la liste d'instances.
redirect($returnurl);
}
echo $OUTPUT->header();
$form->display();
echo $OUTPUT->footer();
} else {
// Il faut afficher la liste des instances.
$jumpurl = new moodle_url('/auth/entsync/jump.php');
if ($is_admin) {
$editurl = new moodle_url($returnurl, ['action' => 'edit']);
$delurl = new moodle_url($returnurl, ['action' => 'del']);
} else {
$editurl = $delurl = $jumpurl;
}
$instancesList = $instances->get_instances([], 'dir');
$t = new html_table();
// Icons.
$editico = $OUTPUT->pix_icon('t/edit', get_string('edit'));
$delico = $OUTPUT->pix_icon('t/delete', get_string('delete'));
$editattr = ['title' => get_string('edit')];
$jumpattr = ['target' => '_blank', 'title' => get_string('login')];
$t->head = ['Répertoire', 'Nom', 'RNE', get_string('actions')];
foreach ($instancesList as $instance) {
$id = $instance->get('id');
$jumplnk = new moodle_url($jumpurl, ['inst' => $instance->get('dir')]);
$editlnk = new moodle_url($editurl, ['id' => $id]);
$dellnk = new moodle_url($delurl, ['id' => $id]);
$row = [];
$row[] = html_writer::link($jumplnk, $instance->get('dir'), $jumpattr);
$row[] = html_writer::link($editlnk, $instance->get('name'), $editattr);
$row[] = html_writer::link($editlnk, $instance->get('rne'), $editattr);
$buttons = [];
$buttons[] = html_writer::link($editlnk, $editico);
if ($is_admin) $buttons[] = html_writer::link($dellnk, $delico);
$row[] = implode(' ', $buttons);
$t->data[] = new html_table_row($row);
}
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('entsyncinst', 'auth_entsync'));
$nbinst = count($instancesList);
$plural = ($nbinst <= 1) ? '' : 's';
echo html_writer::tag('p', "Total : {$nbinst} instance{$plural}");
echo html_writer::table($t);
if ($is_admin) echo $OUTPUT->single_button($editurl, 'Ajouter une instance', 'get');
echo $OUTPUT->footer();
}