From 66d0fdf0af15823d47e99ab9bf2c8f59b4ed5539 Mon Sep 17 00:00:00 2001 From: Edmund Farrow Date: Tue, 22 Oct 2024 16:58:03 +0100 Subject: [PATCH] quiz-data - Export quiz data when creating multiple repos. Use only one git repo. (Renaming of script reuired or this is going to get very confusing!) --- classes/cli_helper.php | 26 +++++++++++--------------- classes/export_quiz.php | 9 ++++----- cli/createmultiplerepos.php | 16 ++++++++++++++-- cli/exportquizstructurefrommoodle.php | 16 ++++++++++++++-- 4 files changed, 43 insertions(+), 24 deletions(-) diff --git a/classes/cli_helper.php b/classes/cli_helper.php index fe0f0c9..e047e9b 100644 --- a/classes/cli_helper.php +++ b/classes/cli_helper.php @@ -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(); @@ -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) { @@ -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 { @@ -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; } diff --git a/classes/export_quiz.php b/classes/export_quiz.php index 2ace31f..0bb7113 100644 --- a/classes/export_quiz.php +++ b/classes/export_quiz.php @@ -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"; @@ -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"; @@ -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)); diff --git a/cli/createmultiplerepos.php b/cli/createmultiplerepos.php index 11bba72..af9cbd4 100755 --- a/cli/createmultiplerepos.php +++ b/cli/createmultiplerepos.php @@ -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); @@ -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"'); diff --git a/cli/exportquizstructurefrommoodle.php b/cli/exportquizstructurefrommoodle.php index 1037cdf..156eb82 100644 --- a/cli/exportquizstructurefrommoodle.php +++ b/cli/exportquizstructurefrommoodle.php @@ -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, ] ]; @@ -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();