Skip to content

Commit

Permalink
quiz-data - Export quiz data when creating multiple repos. Use only o…
Browse files Browse the repository at this point in the history
…ne git repo. (Renaming of script reuired or this is going to get very confusing!)
  • Loading branch information
EJMFarrow committed Oct 22, 2024
1 parent 5ba319d commit 66d0fdf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
26 changes: 11 additions & 15 deletions classes/cli_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ public function get_arguments(): array {
$longopts = $parsed['longopts'];
$commandlineargs = getopt($shortopts, $longopts);
$argcount = count($commandlineargs);
echo "\nProcessed {$argcount} valid command line argument" .
(($argcount !== 1) ? 's' : '') . ".\n";
if (!isset($commandlineargs['z'])) {
echo "\nProcessed {$argcount} valid command line argument" .
(($argcount !== 1) ? 's' : '') . ".\n";
}
$this->processedoptions = $this->prioritise_options($commandlineargs);
if ($this->processedoptions['help']) {
$this->show_help();
Expand Down Expand Up @@ -572,8 +574,8 @@ public function commit_hash_setup(object $activity):void {
$this->create_gitignore($activity->manifestpath);
$manifestdirname = dirname($activity->manifestpath);
chdir($manifestdirname);
exec("git add .");
exec('git commit -m "Initial Commit"');
exec("git add --all");
exec('git commit -m "Initial Commit - ' . basename($activity->manifestpath) . '"');
foreach ($activity->manifestcontents->questions as $question) {
$commithash = exec('git log -n 1 --pretty=format:%H -- "' . substr($question->filepath, 1) . '"');
if ($commithash) {
Expand Down Expand Up @@ -645,10 +647,8 @@ public function check_repo_initialised(string $fullmanifestpath):void {
$manifestdirname = dirname($fullmanifestpath);
if (chdir($manifestdirname)) {
// Will give path of .git if in repo or error.
// Working on the assumption we have to be at the top of the repo.
if (exec('git rev-parse --git-dir') !== '.git') {
echo "The Git repository has not been initialised or " .
"the manifest directory is not at the top level.\n";
if (substr(exec('git rev-parse --git-dir'), -4) !== '.git') {
echo "The Git repository has not been initialised.\n";
exit;
}
} else {
Expand All @@ -658,23 +658,19 @@ public function check_repo_initialised(string $fullmanifestpath):void {
}

/**
* Create initialised repo
* Create directory.
*
* @param string $directory
* @return string updated directory name
*/
public function create_initialised_repo(string $directory):string {
public function create_directory(string $directory):string {
$basename = $directory;
$i = 0;
while (is_dir($directory)) {
$i++;
$directory = $basename . '_' . $i;
}
if (!$this->get_arguments()['usegit']) {
mkdir($directory);
} else {
exec('git init "' . $directory . '" --quiet');
}
mkdir($directory);
return $directory;
}

Expand Down
9 changes: 4 additions & 5 deletions classes/export_quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
// Convert command line options into variables.
$arguments = $clihelper->get_arguments();
$moodleinstance = $arguments['moodleinstance'];
$rootdirectory = ($arguments['rootdirectory']) ? $arguments['rootdirectory'] . '/' : '';
if ($arguments['quizmanifestpath']) {
$this->quizmanifestpath = ($arguments['quizmanifestpath']) ?
$arguments['rootdirectory'] . '/' . $arguments['quizmanifestpath'] : null;
$rootdirectory . $arguments['quizmanifestpath'] : null;
$this->quizmanifestcontents = json_decode(file_get_contents($this->quizmanifestpath));
if (!$this->quizmanifestcontents) {
echo "\nUnable to access or parse manifest file: {$this->quizmanifestpath}\nAborting.\n";
Expand All @@ -109,7 +110,7 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
}
if ($arguments['nonquizmanifestpath']) {
$this->nonquizmanifestpath = ($arguments['nonquizmanifestpath']) ?
$arguments['rootdirectory'] . '/' . $arguments['nonquizmanifestpath'] : null;
$rootdirectory . $arguments['nonquizmanifestpath'] : null;
$this->nonquizmanifestcontents = json_decode(file_get_contents($this->nonquizmanifestpath));
if (!$this->nonquizmanifestcontents) {
echo "\nUnable to access or parse manifest file: {$this->nonquizmanifestpath}\nAborting.\n";
Expand Down Expand Up @@ -202,15 +203,13 @@ public function export_quiz_data() {
unset($question->questionbankentryid);
} else {
$multiple = ($this->quizmanifestpath && $this->nonquizmanifestpath) ? 's' : '';
echo "Question: {$question->name}\n";
echo "Question: {$question->questionbankentryid}\n";
echo "This question is in the quiz but not in the supplied manifest file" . $multiple . ".\n";
echo "Questions must either be in the repo for the quiz context defined by a supplied quiz manifest " .
"(--quizmanifestpath) or in the context (e.g. course) " .
"defined by a different manifest (--nonquizmanifestpath).\n";
echo "You can supply either or both. If your quiz questions are spread between 3 or more contexts " .
"consider consolidating them.\n";
echo "Aborting.\n";
$this->call_exit();
}
}
file_put_contents($this->filepath, json_encode($responsejson));
Expand Down
16 changes: 14 additions & 2 deletions cli/createmultiplerepos.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@
$arguments = $clihelper->get_arguments();
$arguments['contextlevel'] = 'course';
$clihelper->processedoptions = $arguments;
echo "Exporting a course. Associated quiz contexts will also be exported to individual repos.\n";
echo "Exporting a course. Associated quiz contexts will also be exported.\n";
$createrepo = new create_repo($clihelper, $moodleinstances);
$clihelper->create_directory(dirname($createrepo->manifestpath));
$clihelper->check_repo_initialised($createrepo->manifestpath);
$createrepo->process();
$clihelper->commit_hash_setup($createrepo);
Expand All @@ -160,15 +161,26 @@
$basedirectory = $arguments['rootdirectory'];
}
$moodleinstance = $arguments['moodleinstance'];
$coursemanifestname = cli_helper::get_manifest_path($moodleinstance, 'course', null,
$contextinfo->contextinfo->coursename, null, $basedirectory);
$instanceid = $arguments['instanceid'];
$token = $arguments['token'][$moodleinstance];
$ignorecat = $arguments['ignorecat'];
$ignorecat = ($ignorecat) ? ' -x "' . $ignorecat . '"' : '';
foreach ($contextinfo->quizzes as $quiz) {
$instanceid = "{$quiz->instanceid}";
$rootdirectory = $clihelper->create_initialised_repo(cli_helper::get_quiz_directory($basedirectory, $quiz->name));
$rootdirectory = $clihelper->create_directory(cli_helper::get_quiz_directory($basedirectory, $quiz->name));
echo "\nExporting quiz: {$quiz->name} to {$rootdirectory}\n";
chdir($scriptdirectory);
$output = shell_exec('php createrepo.php -r "' . $rootdirectory . '" -i "' . $moodleinstance . '" -l "module" -n ' . (int) $instanceid . ' -t ' . $token . ' -z' . $ignorecat);
echo $output;
$quizmanifestname = cli_helper::get_manifest_path($moodleinstance, 'module', null,
$contextinfo->contextinfo->coursename, $quiz->name, $rootdirectory);
chdir($scriptdirectory);
$output = shell_exec('php exportquizstructurefrommoodle.php -z -r "" -i "' . $moodleinstance . '" -n ' . (int) $instanceid . ' -t ' . $token. ' -p "' . $coursemanifestname. '" -f "' . $quizmanifestname . '"');
echo $output;
}
// Commit the final quiz file.
chdir($basedirectory);
exec("git add --all");
exec('git commit -m "Initial Commit - final update"');
16 changes: 14 additions & 2 deletions cli/exportquizstructurefrommoodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@
'default' => null,
'variable' => 'instanceid',
'valuerequired' => true,
],
[
'longopt' => 'quiet',
'shortopt' => 'z',
'description' => 'Do not display context info or option to abort.',
'default' => false,
'variable' => 'quiet',
'valuerequired' => false,
]
];

Expand All @@ -112,11 +120,15 @@
$clihelper = new cli_helper($options);
$exportquiz = new export_quiz($clihelper, $moodleinstances);
if ($exportquiz->nonquizmanifestpath) {
echo "Checking repo...\n";
if (!isset($clihelper->get_arguments()['quiet'])) {
echo "Checking repo...\n";
}
$clihelper->check_for_changes($exportquiz->nonquizmanifestpath);
}
if ($exportquiz->quizmanifestpath) {
echo "Checking quiz repo...\n";
if (!isset($clihelper->get_arguments()['quiet'])) {
echo "Checking quiz repo...\n";
}
$clihelper->check_for_changes($exportquiz->quizmanifestpath);
}
$exportquiz->process();

0 comments on commit 66d0fdf

Please sign in to comment.