Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions classes/local/cli/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use local_devkit\local\cli\commands\mcp\mcp_serve;
use local_devkit\local\cli\commands\plugins\plugins_list;
use local_devkit\local\cli\commands\purge\handler as purge_handler;
use local_devkit\local\cli\commands\seed\users as seed_users;
use Symfony\Component\Console\Application as BaseApplication;

/**
Expand All @@ -49,6 +50,7 @@ public function __construct() {
$this->addCommand(new env_show());
$this->addCommand(new format());
$this->addCommand(new mcp_serve());
$this->addCommand(new seed_users());
lint_handler::register($this);
purge_handler::register($this);
}
Expand Down
57 changes: 57 additions & 0 deletions classes/local/cli/commands/seed/users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.

declare(strict_types=1);

namespace local_devkit\local\cli\commands\seed;

use local_devkit\local\seeders\users as users_seeder;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Attribute\Option;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressIndicator;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* Command to seed a bunch of users.
*
* @package local_devkit
* @copyright 2026 Felix Yeung
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
#[AsCommand(name: 'seed:users')]
class users extends Command {
/**
* Invoke
*/
public function __invoke(
SymfonyStyle $io,
OutputInterface $output,
#[Option]
int $count = 1,
): int {
$progress = $output instanceof ConsoleOutputInterface
? new ProgressIndicator($output->getErrorOutput())
: null;
$progress?->start('Starting...');
$seeder = new users_seeder($progress, $count);
$seeder->seed();
$progress?->finish('Completed!');
return Command::SUCCESS;
}
}
44 changes: 44 additions & 0 deletions classes/local/seeders/base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.

declare(strict_types=1);

namespace local_devkit\local\seeders;

use Symfony\Component\Console\Helper\ProgressIndicator;

/**
* Class base
*
* @package local_devkit
* @copyright 2026 Felix
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class base {
/**
* Constructor
*/
public function __construct(
/** @var $progress */
protected readonly ?ProgressIndicator $progress,
) {
}

/**
* Run the seeder.
*/
abstract public function seed(): void;
}
84 changes: 84 additions & 0 deletions classes/local/seeders/users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.

namespace local_devkit\local\seeders;

use csv_import_reader;
use Faker\Factory;
use InvalidArgumentException;
use Symfony\Component\Console\Helper\ProgressIndicator;
use tool_uploaduser\process;

/**
* Class users
*
* @package local_devkit
* @copyright 2026 Felix
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class users extends base {
/**
* Constructor.
*/
public function __construct(
?ProgressIndicator $progress,
/** @var int */
private readonly int $count = 1,
) {
parent::__construct($progress);
if ($count <= 0) {
throw new InvalidArgumentException('count must be positive integer');
}
}

#[\Override]
public function seed(): void {
global $CFG;
require_once($CFG->dirroot . '/user/lib.php');
require_once($CFG->libdir . '/csvlib.class.php');

$faker = Factory::create();

$delimiter = ',';
$columns = ['firstname', 'lastname', 'username', 'email', 'password'];
$iid = csv_import_reader::get_new_iid('uploaduser');
$cir = new csv_import_reader($iid, 'uploaduser');
$cir->load_csv_content(implode($delimiter, $columns), 'utf8', $delimiter);

$process = new process($cir, \tool_uploaduser\local\text_progress_tracker::class);
$process->set_form_data((object) ['uutype' => UU_USER_ADDNEW]);

// Remove text_progress_tracker's output as we are using our own progress tracking.
ob_start();
$process->process();
ob_end_clean();

// This is to ensure the password passes Moodle's password policy.
$passwordsuffix = '@aA1!';

foreach (range(1, $this->count) as $index) {
$this->progress?->setMessage("Seeding $index/$this->count");
$user = [
'firstname' => $faker->firstName(),
'lastname' => $faker->lastName(),
'username' => $faker->userName(),
'email' => $faker->safeEmail(),
'password' => $faker->password() . $passwordsuffix,
];
$process->process_line(array_values($user));
}
}
}
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"mcp/sdk": "^0.7.0",
"symfony/process": "^7.4",
"symfony/yaml": "^7.4",
"symfony/finder": "^7.4"
"symfony/finder": "^7.4",
"fakerphp/faker": "^1.24"
},
"require-dev": {
"phpstan/phpstan": "^2.1",
Expand All @@ -32,4 +33,4 @@
"php": "8.3"
}
}
}
}
65 changes: 64 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading