aboutsummaryrefslogtreecommitdiff
path: root/cli/Commands/DB/MoveMatrikelnummer.php
blob: 251aba57beadb0a09b4d816e7dbfca55879fc98e (plain)
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
<?php

namespace Studip\Cli\Commands\DB;

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 MoveMatrikelnummer extends Command
{
    protected static $defaultName = 'db:move_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;
    }
}