From 7f0a32784ad349719a3aefd32fe067fb2897fb62 Mon Sep 17 00:00:00 2001 From: Edmund Farrow Date: Tue, 10 Dec 2024 16:01:50 +0000 Subject: [PATCH] quiz-data - Docs, more filepath fixes --- classes/create_repo.php | 13 +++++++++---- classes/export_repo.php | 20 ++++++++++++++------ classes/export_trait.php | 10 ++++++---- classes/import_quiz.php | 8 ++++++-- classes/import_repo.php | 5 +++++ doc/usinggit.md | 2 +- 6 files changed, 41 insertions(+), 17 deletions(-) diff --git a/classes/create_repo.php b/classes/create_repo.php index 6f076c4..e2b34fc 100644 --- a/classes/create_repo.php +++ b/classes/create_repo.php @@ -87,9 +87,9 @@ class create_repo { /** * Path to actual manifest file. * - * @var string + * @var string|null */ - public string $nonquizmanifestpath; + public ?string $nonquizmanifestpath; /** * Path to temporary manifest file. * @@ -140,8 +140,12 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) { } else { $this->directory = $arguments['rootdirectory']; } - $this->nonquizmanifestpath = ($arguments['rootdirectory']) ? + if ($arguments['nonquizmanifestpath']) { + $this->nonquizmanifestpath = ($arguments['rootdirectory']) ? $arguments['rootdirectory'] . '/' . $arguments['nonquizmanifestpath'] : $arguments['nonquizmanifestpath']; + } else { + $this->nonquizmanifestpath = null; + } $this->subcategory = ($arguments['subcategory']) ? $arguments['subcategory'] : 'top'; if (is_array($arguments['token'])) { $token = $arguments['token'][$moodleinstance]; @@ -284,7 +288,8 @@ public function create_quiz_directories(object $clihelper, string $scriptdirecto echo $output; $quizmanifestpath = cli_helper::get_manifest_path($moodleinstance, 'module', null, $contextinfo->contextinfo->coursename, $quiz->name, $rootdirectory); - $output = $this->call_export_quiz($moodleinstance, $token, $quizmanifestpath, $scriptdirectory); + $output = $this->call_export_quiz($moodleinstance, $token, $quizmanifestpath, + $this->manifestpath, $scriptdirectory); $quizlocation = new \StdClass(); $quizlocation->moduleid = $instanceid; $quizlocation->directory = basename($rootdirectory); diff --git a/classes/export_repo.php b/classes/export_repo.php index 96c2129..0684238 100644 --- a/classes/export_repo.php +++ b/classes/export_repo.php @@ -71,9 +71,9 @@ class export_repo { /** * Full path to manifest file * - * @var string + * @var string|null */ - public string $nonquizmanifestpath; + public ?string $nonquizmanifestpath; /** * Path to temporary manifest file * @@ -125,10 +125,18 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) { $this->moodleurl = $moodleinstances[$moodleinstance]; $this->usegit = $arguments['usegit']; $defaultwarning = false; - $this->manifestpath = ($arguments['rootdirectory']) ? $arguments['rootdirectory'] . '/' . $arguments['manifestpath'] : + if ($arguments['manifestpath']) { + $this->manifestpath = ($arguments['rootdirectory']) ? $arguments['rootdirectory'] . '/' . $arguments['manifestpath'] : $arguments['manifestpath']; - $this->nonquizmanifestpath = ($arguments['rootdirectory']) ? - $arguments['rootdirectory'] . '/' . $arguments['nonquizmanifestpath'] : $arguments['nonquizmanifestpath']; + } else { + $this->manifestpath = null; + } + if ($arguments['nonquizmanifestpath']) { + $this->nonquizmanifestpath = ($arguments['rootdirectory']) ? + $arguments['rootdirectory'] . '/' . $arguments['nonquizmanifestpath'] : $arguments['nonquizmanifestpath']; + } else { + $this->nonquizmanifestpath = null; + } if (is_array($arguments['token'])) { $token = $arguments['token'][$moodleinstance]; } else { @@ -366,7 +374,7 @@ public function update_quiz_directories($clihelper, $scriptdirectory) { echo $output; $quizmanifestpath = cli_helper::get_manifest_path($moodleinstance, 'module', null, $contextinfo->contextinfo->coursename, $quiz->name, $rootdirectory); - $output = $this->call_export_quiz($moodleinstance, $token, $quizmanifestpath, $scriptdirectory); + $output = $this->call_export_quiz($moodleinstance, $token, $quizmanifestpath, $this->manifestpath, $scriptdirectory); echo $output; } } diff --git a/classes/export_trait.php b/classes/export_trait.php index 09f3ab2..9295bdf 100644 --- a/classes/export_trait.php +++ b/classes/export_trait.php @@ -231,7 +231,8 @@ public function export_quiz_structure($clihelper, $scriptdirectory) { $quizmanifestpath = cli_helper::get_manifest_path($moodleinstance, 'module', null, $this->manifestcontents->context->coursename, $this->manifestcontents->context->modulename, dirname($this->manifestpath)); - $output = $this->call_export_quiz($moodleinstance, $token, $quizmanifestpath, $scriptdirectory); + $output = $this->call_export_quiz($moodleinstance, $token, $quizmanifestpath, + $this->nonquizmanifestpath, $scriptdirectory); echo $output; } @@ -241,13 +242,14 @@ public function export_quiz_structure($clihelper, $scriptdirectory) { * @param string $moodleinstance * @param string $token * @param string $quizmanifestpath + * @param string|null $nonquizmanifestpath * @param string $scriptdirectory * @return string|null */ - public function call_export_quiz(string $moodleinstance, string $token, - string $quizmanifestpath, string $scriptdirectory): ?string { + public function call_export_quiz(string $moodleinstance, string $token, string $quizmanifestpath, + ?string $nonquizmanifestpath, string $scriptdirectory): ?string { chdir($scriptdirectory); - $nonquiz = ($this->nonquizmanifestpath) ? ' -p "' . $this->nonquizmanifestpath . '"' : ''; + $nonquiz = ($nonquizmanifestpath) ? ' -p "' . $nonquizmanifestpath . '"' : ''; return shell_exec('php exportquizstructurefrommoodle.php -u ' . $this->usegit . ' -w -r "" -i "' . $moodleinstance . '" -t "' . $token. '" -f "' . $quizmanifestpath . '"' . $nonquiz); diff --git a/classes/import_quiz.php b/classes/import_quiz.php index caec9c8..5947b3d 100644 --- a/classes/import_quiz.php +++ b/classes/import_quiz.php @@ -317,8 +317,12 @@ public function import_all($clihelper, $scriptdirectory): void { } else { $directory = $arguments['rootdirectory']; } - $this->nonquizmanifestpath = ($arguments['rootdirectory']) ? - $arguments['rootdirectory'] . '/' . $arguments['nonquizmanifestpath'] : $arguments['nonquizmanifestpath']; + if ($arguments['nonquizmanifestpath']) { + $this->nonquizmanifestpath = ($arguments['rootdirectory']) ? + $arguments['rootdirectory'] . '/' . $arguments['nonquizmanifestpath'] : $arguments['nonquizmanifestpath']; + } else { + $this->nonquizmanifestpath = null; + } if (is_array($arguments['token'])) { $token = $arguments['token'][$moodleinstance]; } else { diff --git a/classes/import_repo.php b/classes/import_repo.php index a4eb167..205d07f 100644 --- a/classes/import_repo.php +++ b/classes/import_repo.php @@ -910,6 +910,11 @@ public function update_quizzes($clihelper, $scriptdirectory) { break; } } + if (!$structurefile) { + echo "\nNo structure file in {$rootdirectory}\nQuiz not imported.\n"; + continue; + } + $structurefilepath = $rootdirectory . '/' . $structurefile; $contentsjson = file_get_contents($structurefilepath); $structurecontents = json_decode($contentsjson); diff --git a/doc/usinggit.md b/doc/usinggit.md index 2a46c3b..8c24582 100644 --- a/doc/usinggit.md +++ b/doc/usinggit.md @@ -156,7 +156,7 @@ When importing a quiz into a different course, `importquiztomoodle.php` should b ### Whole course -Normally Gitsync retrieves questions within a Moodle context, returning all or a subselection of question categories, with the repo directory structure matching the category structure in Moodle. Courses and quizzes are in separate contexts, however. There are scripts specifically to export and import a 'whole course' to and from a single Git repo with the course and quizzes in sibling directories. The course and quizzes still have separate manifests so can also be imported/exported individually if required. +Normally Gitsync retrieves questions within a Moodle context, returning all or a subselection of question categories, with the repo directory structure matching the category structure in Moodle. Courses and quizzes are in separate contexts, however. There are scripts specifically to export and import a 'whole course' to and from a single Git repo with the course and quizzes in sibling directories. The course and quizzes still have separate manifests so can also be imported/exported individually if required. The manifest for the course links the quiz instance ids in Moodle to the quiz directories in the repo. ## Quiz examples