Skip to content

Commit

Permalink
quiz-data - Fix directory paths
Browse files Browse the repository at this point in the history
  • Loading branch information
EJMFarrow committed Dec 9, 2024
1 parent fc351ea commit 96f7669
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 20 deletions.
3 changes: 2 additions & 1 deletion classes/create_repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
$moodleinstance = $arguments['moodleinstance'];
$this->usegit = $arguments['usegit'];
if ($arguments['directory']) {
$this->directory = $arguments['rootdirectory'] . '/' . $arguments['directory'];
$this->directory = ($arguments['rootdirectory']) ?
$arguments['rootdirectory'] . '/' . $arguments['directory'] : $arguments['directory'];
} else {
$this->directory = $arguments['rootdirectory'];
}
Expand Down
3 changes: 2 additions & 1 deletion classes/export_repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
$moodleinstance = $arguments['moodleinstance'];
$this->usegit = $arguments['usegit'];
$defaultwarning = false;
$this->manifestpath = $arguments['rootdirectory'] . '/' . $arguments['manifestpath'];
$this->manifestpath = ($arguments['rootdirectory']) ? $arguments['rootdirectory'] . '/' . $arguments['manifestpath'] :
$arguments['manifestpath'];
if (is_array($arguments['token'])) {
$token = $arguments['token'][$moodleinstance];
} else {
Expand Down
17 changes: 10 additions & 7 deletions classes/import_quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,26 +132,28 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
$this->clihelper = $clihelper;
$arguments = $clihelper->get_arguments();
if (isset($arguments['directory'])) {
$directory = $arguments['rootdirectory'] . '/' . $arguments['directory'];
$directory = ($arguments['rootdirectory']) ? $arguments['rootdirectory'] . '/' . $arguments['directory'] :
$arguments['directory'] ;
} else {
$directory = $arguments['rootdirectory'];
}
$directoryprefix = ($directory) ? $directory . '/' : '';
$coursename = $arguments['coursename'];
$moodleinstance = $arguments['moodleinstance'];
$instanceid = $arguments['instanceid'];
$contextlevel = 50;
$this->usegit = $arguments['usegit'];
if (!empty($arguments['quizmanifestpath'])) {
$this->quizmanifestpath = ($arguments['quizmanifestpath']) ?
$arguments['rootdirectory'] . '/' . $arguments['quizmanifestpath'] : null;
$directoryprefix . $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";
$this->call_exit();

} else {
$this->cmid = $this->quizmanifestcontents->context->instanceid;
$this->quizdatapath = ($arguments['quizdatapath']) ? $arguments['rootdirectory'] . '/' . $arguments['quizdatapath']
$this->quizdatapath = ($arguments['quizdatapath']) ? $directoryprefix . $arguments['quizdatapath']
: cli_helper::get_quiz_structure_path($this->quizmanifestcontents->context->modulename,
dirname($this->quizmanifestpath));
$instanceid = $this->cmid;
Expand All @@ -160,7 +162,7 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
}
} else {
if ($arguments['quizdatapath']) {
$this->quizdatapath = $arguments['rootdirectory'] . '/' . $arguments['quizdatapath'];
$this->quizdatapath = $directoryprefix . $arguments['quizdatapath'];
} else {
if (empty($arguments['createquiz'])) {
echo "\nPlease supply a quiz manifest filepath or a quiz data filepath.\nAborting.\n";
Expand All @@ -181,13 +183,13 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
$this->call_exit();
return; // Required for unit tests.
}
$this->quizdatapath = $directory . '/' . $structurefile;
$this->quizdatapath = $directoryprefix . $structurefile;
}
}
}
if (!empty($arguments['nonquizmanifestpath'])) {
$this->nonquizmanifestpath = ($arguments['nonquizmanifestpath']) ?
$arguments['rootdirectory'] . '/' . $arguments['nonquizmanifestpath'] : null;
$directoryprefix . $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 @@ -303,7 +305,8 @@ public function import_all($clihelper, $scriptdirectory): void {
$arguments = $clihelper->get_arguments();
$moodleinstance = $arguments['moodleinstance'];
if ($arguments['directory']) {
$directory = $arguments['rootdirectory'] . '/' . $arguments['directory'];
$directory = ($arguments['rootdirectory']) ?
$arguments['rootdirectory'] . '/' . $arguments['directory'] : $arguments['directory'];
} else {
$directory = $arguments['rootdirectory'];
}
Expand Down
15 changes: 10 additions & 5 deletions classes/import_repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,12 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
$moodleinstance = $arguments['moodleinstance'];
$manifestpath = $arguments['manifestpath'];
if ($arguments['directory']) {
$this->directory = $arguments['rootdirectory'] . '/' . $arguments['directory'];
$this->directory = ($arguments['rootdirectory']) ? $arguments['rootdirectory'] . '/' . $arguments['directory'] :
$arguments['directory'];
} else {
if ($manifestpath && dirname($manifestpath) !== '.') {
$this->directory = $arguments['rootdirectory'] . '/' . dirname($manifestpath);
$this->directory = ($arguments['rootdirectory']) ? $arguments['rootdirectory'] . '/' . dirname($manifestpath) :
dirname($manifestpath);
} else {
$this->directory = $arguments['rootdirectory'];
}
Expand Down Expand Up @@ -249,7 +251,8 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
$this->listcurlrequest->set_option(CURLOPT_POST, 1);

if ($manifestpath) {
$this->manifestpath = $arguments['rootdirectory'] . '/' . $manifestpath;
$this->manifestpath = ($arguments['rootdirectory']) ? $arguments['rootdirectory'] . '/' . $manifestpath :
$manifestpath;
} else {
$this->subdirectory = ($arguments['subdirectory']) ? $arguments['subdirectory'] : 'top';
$instanceinfo = $this->clihelper->check_context($this, false, false);
Expand Down Expand Up @@ -333,7 +336,9 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
if ($this->subdirectory === 'top') {
$qcategoryname = 'top';
} else {
$listqcategoryfile = $this->directory . '/' . $this->subdirectory . '/' . cli_helper::CATEGORY_FILE . '.xml';
$listqcategoryfile = ($this->directory ) ?
$this->directory . '/' . $this->subdirectory . '/' . cli_helper::CATEGORY_FILE . '.xml' :
$this->subdirectory . '/' . cli_helper::CATEGORY_FILE . '.xml';
$qcategoryname = cli_helper::get_question_category_from_file($listqcategoryfile);
}
if (!$qcategoryname) {
Expand Down Expand Up @@ -482,7 +487,7 @@ public function upload_file($repoitem): bool {
*/
public function import_questions() {
if ($this->subdirectory) {
$subdirectory = $this->directory . '/' . $this->subdirectory;
$subdirectory = ($this->directory) ? $this->directory . '/' . $this->subdirectory : $this->subdirectory;
} else {
$subdirectory = $this->directory;
}
Expand Down
4 changes: 3 additions & 1 deletion cli/createrepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@
$clihelper->check_repo_initialised($createrepo->manifestpath);
$createrepo->process();
$clihelper->commit_hash_setup($createrepo);
if ($createrepo->manifestcontents->context->contextlevel === 70) {
// If we're exporting a quiz then we try getting the structure as well.
// Skip if we're creating a whole cpurse repo or we'll do it twice!
if ($createrepo->manifestcontents->context->contextlevel === 70 && !$clihelper->get_arguments()['subcall']) {
$scriptdirectory = dirname(__FILE__);
$createrepo->export_quiz_structure($clihelper, $scriptdirectory);
chdir(dirname($createrepo->manifestpath));
Expand Down
2 changes: 1 addition & 1 deletion cli/exportrepofrommoodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
$clihelper->check_for_changes($exportrepo->manifestpath);
$clihelper->backup_manifest($exportrepo->manifestpath);
$exportrepo->process();
if ($exportrepo->manifestcontents->context->contextlevel === 70) {
if ($exportrepo->manifestcontents->context->contextlevel === 70 && !$clihelper->get_arguments()['subcall']) {
$scriptdirectory = dirname(__FILE__);
$exportrepo->export_quiz_structure($clihelper, $scriptdirectory);
}
8 changes: 4 additions & 4 deletions doc/usinggit.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ Initialise repo for quiz:
Export quiz with `cmid=50` into directory `quizexport` (assuming rootdirectory, token, moodleinstance, usegit, etc, all set in your config file.):
`php createrepo.php -d 'quizexport' -l module -n 50`
Export questions/structures again after updates in Moodle:
`php exportrepofrommoodle -f 'quizexport/instance1_module_course-1_quiz-only_question_manifest.json'`
`php exportrepofrommoodle.php -f 'quizexport/instance1_module_course-1_quiz-only_question_manifest.json'`
Import questions again after updates in the repo:
`php importrepotomoodle -f 'quizexport/instance1_module_course-1_quiz-only_question_manifest.json'`
`php importrepotomoodle.php -f 'quizexport/instance1_module_course-1_quiz-only_question_manifest.json'`
Create quiz and import into course with `id=2`:
`php importquiztomoodle.php -d 'quizexport' -n 2`

Expand All @@ -190,10 +190,10 @@ Export quiz with `cmid=50` into directory `quizexport` (assuming rootdirectory,
Export quiz structure by supplying quiz and course manifests:
`php exportquizstructurefrommoodle.php -f 'quizexport/instance1_module_course-1_mixed-quiz_question_manifest.json' -p 'course1/instance1_course_course-1_question_manifest.json'`
Export questions/structures again after updates in Moodle:
`php exportrepofrommoodle -f 'quizexport/instance1_module_course-1_quiz-only_question_manifest.json'`
`php exportrepofrommoodle.php -f 'quizexport/instance1_module_course-1_quiz-only_question_manifest.json'`
`php exportquizstructurefrommoodle.php -f 'quizexport/instance1_module_course-1_mixed-quiz_question_manifest.json' -p 'course1/instance1_course_course-1_question_manifest.json'`
Import questions again after updates in the repo:
`php importrepotomoodle -f 'quizexport/instance1_module_course-1_quiz-only_question_manifest.json'`
`php importrepotomoodle.php -f 'quizexport/instance1_module_course-1_quiz-only_question_manifest.json'`
Create quiz and import into course with `id=2`:
`php importquiztomoodle.php -d 'quizexport' -n 2 -p 'course1/instance1_course_course-1_question_manifest.json'`

Expand Down

0 comments on commit 96f7669

Please sign in to comment.