-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add cli script to assign a user to a role on a given course category …
…(or create a new course cat), code and documentation improvements.
- Loading branch information
1 parent
b9140bf
commit a4cacd5
Showing
8 changed files
with
320 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace local_adler\local\exceptions; | ||
|
||
use Exception; | ||
use Throwable; | ||
|
||
class exit_exception extends Exception { | ||
public function __construct($code = 0, Throwable $previous = null) { | ||
parent::__construct( | ||
"Exiting script", | ||
$code, | ||
$previous | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
use local_adler\local\course_category_manager; | ||
use local_adler\local\exceptions\exit_exception; | ||
|
||
if (!defined('CLI_SCRIPT')) { | ||
define('CLI_SCRIPT', true); | ||
} | ||
|
||
require_once(__DIR__ . '/../../../config.php'); | ||
global $CFG; | ||
require_once("{$CFG->libdir}/clilib.php"); | ||
|
||
$help = | ||
"Create a new course category and grant the user permission to create adler courses in it. | ||
Options: | ||
--username=STRING User name | ||
--role=STRING Role name | ||
--category_path=STRING Category path (optional) | ||
-h, --help Print out this help | ||
"; | ||
|
||
// Parse command line arguments | ||
list($options, $unrecognized) = cli_get_params( | ||
array( | ||
'username' => false, | ||
'role' => false, | ||
'category_path' => false, | ||
'help' => false | ||
), | ||
array( | ||
'h' => 'help' | ||
) | ||
); | ||
|
||
if ($unrecognized) { | ||
$unrecognized = implode("\n ", $unrecognized); | ||
cli_write(get_string('cliunknowoption', 'admin', $unrecognized) . "\n"); | ||
echo $help; | ||
throw new exit_exception(1); | ||
} | ||
|
||
if (!empty($options['help'])) { | ||
echo $help; | ||
} else { | ||
if (empty(trim($options['username']))) { | ||
cli_writeln('--username is required'); | ||
throw new exit_exception(1); | ||
} | ||
|
||
if (empty(trim($options['role']))) { | ||
cli_writeln('--role is required'); | ||
throw new exit_exception(1); | ||
} | ||
|
||
$username = trim($options['username']); | ||
$role = trim($options['role']); | ||
$category_path = trim($options['category_path']); | ||
|
||
|
||
try { | ||
$category_id = course_category_manager::create_category_user_can_create_courses_in($username, $role, $category_path); | ||
} catch (moodle_exception $e) { | ||
cli_writeln($e->getMessage()); | ||
throw new exit_exception(1); | ||
} | ||
|
||
cli_writeln("Created category with ID $category_id and assigned user $username the role $role in it."); | ||
} | ||
|
||
|
Oops, something went wrong.