From 66518ee520fff1c1f440345dfa443e2fb6f1fbb6 Mon Sep 17 00:00:00 2001 From: Moritz Strohm <strohm@data-quest.de> Date: Mon, 13 Nov 2023 09:23:07 +0000 Subject: [PATCH] show "Matrikelnummer" field in admin/user result table, closes #3336 Closes #3336 Merge request studip/studip!2270 --- app/controllers/admin/user.php | 2 +- app/views/admin/user/_results.php | 2 + app/views/admin/user/edit.php | 10 +++ app/views/profile/index.php | 2 + .../Migrate/MigrateMatrikelnummer.php | 66 +++++++++++++++++++ cli/studip | 1 + .../5.5.6_add_matriculation_number.php | 28 ++++++++ lib/models/User.class.php | 1 + 8 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 cli/Commands/Migrate/MigrateMatrikelnummer.php create mode 100644 db/migrations/5.5.6_add_matriculation_number.php diff --git a/app/controllers/admin/user.php b/app/controllers/admin/user.php index 37b8b532f04..b6a40486eab 100644 --- a/app/controllers/admin/user.php +++ b/app/controllers/admin/user.php @@ -417,7 +417,7 @@ class Admin_UserController extends AuthenticatedController if (count($editPerms)) { $editUser['auth_user_md5.perms'] = $editPerms[0]; } - foreach (words('Vorname Nachname auth_plugin visible') as $param) { + foreach (['Vorname', 'Nachname', 'matriculation_number', 'auth_plugin', 'visible'] as $param) { if (Request::get($param)) $editUser['auth_user_md5.' . $param] = Request::get($param); } foreach (words('title_front title_rear geschlecht preferred_language') as $param) { diff --git a/app/views/admin/user/_results.php b/app/views/admin/user/_results.php index e41d995ead2..8751436164c 100644 --- a/app/views/admin/user/_results.php +++ b/app/views/admin/user/_results.php @@ -22,6 +22,7 @@ </a> </th> <th> </th> + <th><?= _('Matrikelnummer') ?></th> <th <?= $sortby === 'perms' ? 'class="sort' . $order . '"' : '' ?>> <a href="<?= $controller->link_for('admin/user',['sortby' =>'perms', 'order'=> $order ,'toggle' => $sortby === 'perms']) ?>"> <?= _('Status') ?> @@ -99,6 +100,7 @@ ?> <?= tooltipHtmlIcon(htmlReady($tooltxt, true, true)) ?> </td> + <td><?= htmlReady($user->matriculation_number) ?></td> <td><?= $user['perms'] ?></td> <td><?= htmlReady($user->Vorname) ?></td> <td><?= htmlReady($user->nachname) ?></td> diff --git a/app/views/admin/user/edit.php b/app/views/admin/user/edit.php index 9e1241eb449..8892d983176 100644 --- a/app/views/admin/user/edit.php +++ b/app/views/admin/user/edit.php @@ -204,6 +204,16 @@ use Studip\Button, Studip\LinkButton; </div> </section> + <section> + <label> + <?= _('Matrikelnummer') ?> + <input class="user_form" type="text" id="matriculation_number" + value="<?= htmlReady($user->matriculation_number) ?>" + <?= StudipAuthAbstract::CheckField('auth_user_md5.matriculation_number', $user->auth_plugin) + || LockRules::check($user->user_id, 'matriculation_number') ? 'readonly' : 'name="matriculation_number"' ?>> + </label> + </section> + <? if (!empty($user_roles)) : ?> <section> diff --git a/app/views/profile/index.php b/app/views/profile/index.php index 29f01197d20..82a001a48af 100644 --- a/app/views/profile/index.php +++ b/app/views/profile/index.php @@ -65,6 +65,8 @@ <? if ($current_user->user_id === $GLOBALS['user']->id) : ?> <dt><?= _('Status') ?>:</dt> <dd><?= htmlReady(ucfirst($current_user['perms'])) ?></dd> + <dt><?= _('Matrikelnummer') ?>:</dt> + <dd><?= htmlReady($current_user['matriculation_number']) ?></dd> <? endif ?> <? if (!empty($shortDatafields)) : ?> diff --git a/cli/Commands/Migrate/MigrateMatrikelnummer.php b/cli/Commands/Migrate/MigrateMatrikelnummer.php new file mode 100644 index 00000000000..fbad2d8c212 --- /dev/null +++ b/cli/Commands/Migrate/MigrateMatrikelnummer.php @@ -0,0 +1,66 @@ +<?php + +namespace Studip\Cli\Commands\Migrate; + +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class MigrateMatrikelnummer extends Command +{ + protected static $defaultName = 'migrate:matrikelnummer'; + + protected function configure() : void + { + $this->setDescription('Migrates the "Matrikelnummer" datafield into the matriculation_number column of the auth_user_md5 table.'); + $this->setHelp('This command migrates the "Matrikelnummer" datafield into the matriculation_number column of the auth_user_md5 table. The ID of the datafield must be provided. The datafield is not deleted in the migration process.'); + $this->addOption('id', 'i', InputOption::VALUE_REQUIRED, 'The ID of the "Matrikelnummer" datafield.', null); + } + + protected function execute(InputInterface $input, OutputInterface $output) : int + { + $datafield_id = $input->getOption('id'); + if (!$datafield_id) { + echo 'No ID for the "Matrikelnummer" datafield has been provided.' . "\n"; + return Command::FAILURE; + } + $datafield = \DataField::find($datafield_id); + if (!$datafield) { + echo 'The datafield could not be found.' . "\n"; + return Command::FAILURE; + } + //Get all entries of the datafield for users and set the + //matriculation_number field: + + $verbose = $input->getOption('verbose'); + + $modifier = function ($entry) use ($verbose) { + if (!$entry->content) { + return; + } + $user = \User::find($entry->range_id); + if (!$user) { + return; + } + $user->matriculation_number = $entry->content; + if ($user->isDirty()) { + $success = $user->store(); + if ($verbose) { + if ($success === false) { + printf('Error updating matriculation number for user %s.', $user->username) . "\n"; + } elseif ($success > 0) { + printf('Matriculation number updated for user %s.', $user->username) . "\n"; + } + } + } + }; + + \DatafieldEntryModel::findEachByDatafield_id( + $modifier, + $datafield->id + ); + + return Command::SUCCESS; + } +} diff --git a/cli/studip b/cli/studip index bef389ccd7c..c4addb93b93 100755 --- a/cli/studip +++ b/cli/studip @@ -38,6 +38,7 @@ $commands = [ Commands\Fix\EndTimeWeeklyRecurredEvents::class, Commands\Fix\IconDimensions::class, Commands\HelpContent\Migrate::class, + Commands\Migrate\MigrateMatrikelnummer::class, Commands\Migrate\MigrateList::class, Commands\Migrate\MigrateStatus::class, Commands\Migrate\Migrate::class, diff --git a/db/migrations/5.5.6_add_matriculation_number.php b/db/migrations/5.5.6_add_matriculation_number.php new file mode 100644 index 00000000000..4fa078459d9 --- /dev/null +++ b/db/migrations/5.5.6_add_matriculation_number.php @@ -0,0 +1,28 @@ +<?php + + +class AddMatriculationNumber extends Migration +{ + public function description() + { + return 'Add auth_user_md5.matriculation_number'; + } + + protected function up() + { + $db = DBManager::get(); + $db->exec( + "ALTER TABLE `auth_user_md5` + ADD COLUMN matriculation_number VARCHAR(255) NULL DEFAULT NULL" + ); + } + + protected function down() + { + $db = DBManager::get(); + $db->exec( + "ALTER TABLE `auth_user_md5` + DROP COLUMN matriculation_number" + ); + } +} diff --git a/lib/models/User.class.php b/lib/models/User.class.php index 627673d65b6..21f8f873b1b 100644 --- a/lib/models/User.class.php +++ b/lib/models/User.class.php @@ -30,6 +30,7 @@ * @property string $vorname database column * @property string $nachname database column * @property string $email database column + * @property string $matriculation_number database column * @property string $validation_key database column * @property string|null $auth_plugin database column * @property int $locked database column -- GitLab