From b73963d5dfb7274227b4e7ee01326fce6748b5c1 Mon Sep 17 00:00:00 2001 From: fredtempez Date: Thu, 23 Nov 2023 18:42:08 +0100 Subject: [PATCH] =?UTF-8?q?D=C3=A9sincription=20en=20masse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/module/course/course.php | 126 +++++++++++++++--- core/module/course/view/users/users.js.php | 6 - core/module/course/view/users/users.php | 6 +- .../course/view/usersDelete/usersDelete.css | 18 +++ .../view/usersDelete/usersDelete.js.php | 86 ++++++++++++ .../course/view/usersDelete/usersDelete.php | 42 ++++++ 6 files changed, 260 insertions(+), 24 deletions(-) create mode 100644 core/module/course/view/usersDelete/usersDelete.css create mode 100644 core/module/course/view/usersDelete/usersDelete.js.php create mode 100644 core/module/course/view/usersDelete/usersDelete.php diff --git a/core/module/course/course.php b/core/module/course/course.php index bb0169e..a789f50 100644 --- a/core/module/course/course.php +++ b/core/module/course/course.php @@ -539,7 +539,7 @@ class course extends common template::button('userDelete' . $userId, [ 'class' => 'userDelete buttonRed', 'href' => helper::baseUrl() . 'course/userDelete/' . $courseId . '/' . $userId, - 'value' => template::ico('trash'), + 'value' => template::ico('user'), 'help' => 'Désinscrire' ]) ]; @@ -711,23 +711,119 @@ class course extends common */ public function usersDelete() { - // Accès refusé + + // Contenu sélectionné + $courseId = $this->getUrl(2); + + // Inscription des utilisateurs cochés if ( - $this->getUser('permission', __CLASS__, __FUNCTION__) !== true + isset($_POST['courseUsersDeleteSubmit']) ) { - // Valeurs en sortie - $this->addOutput([ - 'access' => false - ]); - } else { - $this->setData(['enrolment', $this->getUrl(2), array()]); - // Valeurs en sortie - $this->addOutput([ - 'redirect' => helper::baseUrl() . 'course/user/' . $this->getUrl(2), - 'notification' => helper::translate('Contenu réinitialisé'), - 'state' => true - ]); + foreach ($_POST as $keyPost => $valuePost) { + // Exclure les variables post qui ne sont pas des userId et ne traiter que les non inscrits + if ( + $this->getData(['user', $keyPost]) !== null + && $this->getData(['enrolment', $courseId, $keyPost]) !== null + ) { + $this->deleteData(['enrolment', $courseId, $keyPost]); + } + } } + + // Liste des groupes et des profils + $courseGroups = $this->getData(['profil']); + foreach ($courseGroups as $groupId => $groupValue) { + switch ($groupId) { + case "-1": + case "0": + break; + case "3": + self::$courseGroups['30'] = 'Administrateur'; + $profils['30'] = 0; + break; + case "1": + case "2": + foreach ($groupValue as $profilId => $profilValue) { + if ($profilId) { + self::$courseGroups[$groupId . $profilId] = sprintf(helper::translate('Groupe %s - Profil %s'), self::$groupPublics[$groupId], $profilValue['name']); + $profils[$groupId . $profilId] = 0; + } + } + } + } + + // Liste alphabétique + self::$alphabet = range('A', 'Z'); + $alphabet = range('A', 'Z'); + self::$alphabet = array_combine($alphabet, self::$alphabet); + self::$alphabet = array_merge(['all' => 'Tout'], self::$alphabet); + + // Liste des inscrits dans le contenu sélectionné. + $users = $this->getData(['enrolment', $courseId]); + + // Tri du tableau par défaut par $userId + ksort($users); + + foreach ($users as $userId => $userValue) { + + // Compte les rôles + $profils[$this->getData(['user', $userId, 'group']) . $this->getData(['user', $userId, 'profil'])]++; + + // Filtres + if ( + isset($_POST['courseFilterGroup']) + || isset($_POST['courseFilterFirstName']) + || isset($_POST['courseFilterLastName']) + ) { + + // Groupe et profils + $group = (string) $this->getData(['user', $userId, 'group']); + $profil = (string) $this->getData(['user', $userId, 'profil']); + $firstName = $this->getData(['user', $userId, 'firstname']); + $lastName = $this->getData(['user', $userId, 'lastname']); + if ( + $this->getInput('courseFilterGroup', helper::FILTER_INT) > 0 + && $this->getInput('courseFilterGroup', helper::FILTER_STRING_SHORT) !== $group . $profil + ) + continue; + // Première lettre du prénom + if ( + $this->getInput('courseFilterFirstName', helper::FILTER_STRING_SHORT) !== 'all' + && $this->getInput('courseFilterFirstName', helper::FILTER_STRING_SHORT) !== strtoupper(substr($firstName, 0, 1)) + ) + continue; + // Première lettre du nom + if ( + $this->getInput('courseFilterLastName', helper::FILTER_STRING_SHORT) !== 'all' + && $this->getInput('courseFilterLastName', helper::FILTER_STRING_SHORT) !== strtoupper(substr($lastName, 0, 1)) + ) + continue; + } + + // Construction du tableau + self::$courseUsers[] = [ + $userId, + $this->getData(['user', $userId, 'firstname']), + $this->getData(['user', $userId, 'lastname']), + template::checkbox($userId, true, '', ['class' => 'checkboxSelect']) + ]; + + } + + // Ajoute les effectifs aux profils du sélecteur + foreach (self::$courseGroups as $groupId => $groupValue) { + if ($groupId === 'all') { + self::$courseGroups['all'] = self::$courseGroups['all'] . ' (' . array_sum($profils) . ')'; + } else { + self::$courseGroups[$groupId] = self::$courseGroups[$groupId] . ' (' . $profils[$groupId] . ')'; + } + } + + // Valeurs en sortie + $this->addOutput([ + 'title' => helper::translate('Désincription en masse'), + 'view' => 'usersDelete' + ]); } /* diff --git a/core/module/course/view/users/users.js.php b/core/module/course/view/users/users.js.php index 7818d67..5d342e2 100644 --- a/core/module/course/view/users/users.js.php +++ b/core/module/course/view/users/users.js.php @@ -21,12 +21,6 @@ $(document).ready((function () { $(location).attr("href", _this.attr("href")) })) })); - $(".userDeleteAll").on("click", (function () { - var _this = $(this); - return message = "", core.confirm(message, (function () { - $(location).attr("href", _this.attr("href")) - })) - })); $('#dataTables').DataTable({ language: { diff --git a/core/module/course/view/users/users.php b/core/module/course/view/users/users.php index 638911d..177ffbf 100644 --- a/core/module/course/view/users/users.php +++ b/core/module/course/view/users/users.php @@ -17,8 +17,8 @@ 'userDeleteAll buttonRed', 'href' => helper::baseUrl() . 'course/usersDelete/' . $this->getUrl(2), - 'value' => template::ico('trash'), - 'help' => 'Désinscrire tous les utilisateurs', + 'value' => template::ico('users'), + 'help' => 'Désinscrire en masse', ])?>
@@ -26,7 +26,7 @@ 'class' => 'buttonGreen', 'href' => helper::baseUrl() . 'course/usersAdd/' . $this->getUrl(2), 'value' => template::ico('users'), - 'help' => 'Inscription', + 'help' => 'Inscription en masse', ]) ?>
diff --git a/core/module/course/view/usersDelete/usersDelete.css b/core/module/course/view/usersDelete/usersDelete.css new file mode 100644 index 0000000..ec84629 --- /dev/null +++ b/core/module/course/view/usersDelete/usersDelete.css @@ -0,0 +1,18 @@ +/** + * This file is part of Zwii. + * + * For full copyright and license information, please see the LICENSE + * file that was distributed with this source code. + * + * @author Rémi Jean + * @copyright Copyright (C) 2008-2018, Rémi Jean + * @author Frédéric Tempez + * @copyright Copyright (C) 2018-2023, Frédéric Tempez + * @license CC Attribution-NonCommercial-NoDerivatives 4.0 International + * @link http://zwiicms.fr/ + */ + + +/** NE PAS EFFACER +* admin.css +*/ \ No newline at end of file diff --git a/core/module/course/view/usersDelete/usersDelete.js.php b/core/module/course/view/usersDelete/usersDelete.js.php new file mode 100644 index 0000000..377a53b --- /dev/null +++ b/core/module/course/view/usersDelete/usersDelete.js.php @@ -0,0 +1,86 @@ +/** + * This file is part of Zwii. + * For full copyright and license information, please see the LICENSE + * file that was distributed with this source code. + * + * @author Rémi Jean + * @copyright Copyright (C) 2008-2018, Rémi Jean + * @author Frédéric Tempez + * @copyright Copyright (C) 2018-2023, Frédéric Tempez + * @license CC Attribution-NonCommercial-NoDerivatives 4.0 International + * @link http://zwiicms.fr/ + */ + +$(document).ready((function () { + + + $("#courseFilterGroup, #courseFilterFirstName, #courseFilterLastName").change(function () { + saveCheckboxState(); + $("#courseUsersDeleteForm").submit(); + }); + + var table = $('#dataTables').DataTable({ + language: { + url: "core/vendor/datatables/french.json" + }, + "columnDefs": [ + { + target: 3, + orderable: false, + searchable: false, + } + ] + }); + + // Handle checkbox change event + $('.checkboxSelect').on('change', function () { + // Save checkbox state to cookies or local storage + saveCheckboxState(); + }); + + // Handle checkbox state on DataTables draw event + table.on('draw', function () { + // Restore checkbox state from cookies or local storage + restoreCheckboxState(); + }); + + // Empty local storage after submit + $("#courseUsersDeleteSubmit").on("click", function () { + + localStorage.setItem('checkboxState', JSON.stringify({})); + + }); + + // Restore checkbox state on page load + restoreCheckboxState(); + + function saveCheckboxState() { + + // Récupérer d'abord les données existantes dans le localStorage + var existingData = JSON.parse(localStorage.getItem('checkboxState')) || {}; + + // Ajouter ou mettre à jour les données actuelles + $('.checkboxSelect').each(function () { + var checkboxId = $(this).attr('id'); + var checked = $(this).prop('checked'); + existingData[checkboxId] = checked; + }); + + // Sauvegarder les données mises à jour dans le localStorage + localStorage.setItem('checkboxState', JSON.stringify(existingData)); + } + + // Function to restore checkbox state + function restoreCheckboxState() { + var checkboxState = JSON.parse(localStorage.getItem('checkboxState')) || {}; + // console.log(checkboxState); + for (var checkboxId in checkboxState) { + if (checkboxState.hasOwnProperty(checkboxId)) { + var checked = checkboxState[checkboxId]; + // Update checkbox state based on stored information + $('#' + checkboxId).prop('checked', checked); + } + } + } + +})); \ No newline at end of file diff --git a/core/module/course/view/usersDelete/usersDelete.php b/core/module/course/view/usersDelete/usersDelete.php new file mode 100644 index 0000000..228e431 --- /dev/null +++ b/core/module/course/view/usersDelete/usersDelete.php @@ -0,0 +1,42 @@ + +
+
+ 'buttonGrey', + 'href' => helper::baseUrl() . 'course/users/' . $this->getUrl(2), + 'value' => template::ico('left') + ]); ?> +
+
+
+ 'buttonRed', + 'value' => 'Désinscrire' + ]); ?> +
+
+
+ 'Groupes / Profils', + 'selected' => isset($_POST['courseFilterGroup']) ? $_POST['courseFilterGroup'] : 'all', + ]); ?> +
+
+ 'Prénom commence par', + 'selected' => isset($_POST['courseFilterFirstName']) ? $_POST['courseFilterFirstName'] : 'all', + ]); ?> +
+
+ 'Nom commence par', + 'selected' => isset($_POST['courseFilterLastName']) ? $_POST['courseFilterLastName'] : 'all', + ]); ?> +
+
+ + 'dataTables']); ?> + + + + \ No newline at end of file