Skip to content

Commit

Permalink
quiz-data - Fix issues with git checking when making a subcall
Browse files Browse the repository at this point in the history
  • Loading branch information
EJMFarrow committed Nov 12, 2024
1 parent 960632d commit 752f503
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 28 deletions.
16 changes: 13 additions & 3 deletions classes/cli_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ public function parse_options(): array {
*/
public function validate_and_clean_args(): void {
$cliargs = $this->processedoptions;
if (!isset($cliargs['usegit'])) {
echo "\nAre you using Git? You will need to specify true or false for --usegit.\n";
static::call_exit();
}
if (!isset($cliargs['token'])) {
echo "\nYou will need a security token (--token).\n";
static::call_exit();
Expand Down Expand Up @@ -328,6 +332,10 @@ public function prioritise_options($commandlineargs): array {
} else {
$variables[$variablename] = $option['default'];
}
if (in_array($variablename, ['usegit'])) {
$variables[$variablename] = ($variables[$variablename] === 'true') ? true : $variables[$variablename];
$variables[$variablename] = ($variables[$variablename] === 'false') ? false : $variables[$variablename];
}
} else {
if (isset($commandlineargs[$option['longopt']]) || isset($commandlineargs[$option['shortopt']])) {
$variables[$variablename] = true;
Expand Down Expand Up @@ -562,8 +570,10 @@ public function commit_hash_setup(object $activity):void {
$this->create_gitignore($activity->manifestpath);
$manifestdirname = dirname($activity->manifestpath);
chdir($manifestdirname);
exec("git add --all");
exec('git commit -m "Initial Commit - ' . basename($activity->manifestpath) . '"');
if (!(isset($this->get_arguments()['subcall']) && $this->get_arguments()['subcall'])) {
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 @@ -672,7 +682,7 @@ public function create_directory(string $directory):string {
* @return void
*/
public function check_for_changes($fullmanifestpath) {
if (!$this->get_arguments()['usegit']) {
if (!$this->get_arguments()['usegit'] || (isset($this->get_arguments()['subcall']) && $this->get_arguments()['subcall'])) {
return;
}
$this->check_repo_initialised($fullmanifestpath);
Expand Down
4 changes: 2 additions & 2 deletions classes/create_repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public function create_quiz_directories($clihelper, $scriptdirectory) {
$rootdirectory = $clihelper->create_directory($quizdirectory);
echo "\nExporting quiz: {$quiz->name} to {$rootdirectory}\n";
chdir($scriptdirectory);
$output = shell_exec('php createrepo.php -r "' . $rootdirectory . '" -i "' . $moodleinstance . '" -l "module" -n ' . $instanceid . ' -t ' . $token . ' -z' . $ignorecat);
$output = shell_exec('php createrepo.php -z -r "' . $rootdirectory . '" -i "' . $moodleinstance . '" -l "module" -n ' . $instanceid . ' -t ' . $token . ' -x ' . $ignorecat);
echo $output;
$quizmanifestname = cli_helper::get_manifest_path($moodleinstance, 'module', null,
$contextinfo->contextinfo->coursename, $quiz->name, $rootdirectory);
Expand All @@ -279,7 +279,7 @@ public function create_quiz_directories($clihelper, $scriptdirectory) {
}
echo $output;
}
if ($arguments['usegit']) {
if ($arguments['usegit'] && !(isset($arguments()['subcall']) && $arguments()['subcall'])) {
// Commit the final quiz file.
// The others are committed by the following createrepo.
chdir($basedirectory);
Expand Down
26 changes: 14 additions & 12 deletions classes/export_repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,32 +308,34 @@ public function update_quiz_directories($clihelper, $scriptdirectory) {
$instanceid = (int) $quiz->instanceid;
if (!isset($locarray[$instanceid]) || !is_dir(dirname($basedirectory) . '/' . $locarray[$instanceid]->directory)) {
$rootdirectory = $clihelper->create_directory(cli_helper::get_quiz_directory($basedirectory, $quiz->name));
$quizlocation = new \StdClass();
$quizlocation->moduleid = $instanceid;
$quizlocation->directory = basename($rootdirectory);
$quizlocations[] = $quizlocation;
$this->manifestcontents->quizzes = $quizlocations;
$success = file_put_contents($this->manifestpath, json_encode($this->manifestcontents));
if ($success === false) {
echo "\nUnable to update manifest file: {$this->manifestpath}\n Aborting.\n";
exit();
if (!isset($locarray[$instanceid])) {
$quizlocation = new \StdClass();
$quizlocation->moduleid = $instanceid;
$quizlocation->directory = basename($rootdirectory);
$quizlocations[] = $quizlocation;
$this->manifestcontents->quizzes = $quizlocations;
$success = file_put_contents($this->manifestpath, json_encode($this->manifestcontents));
if ($success === false) {
echo "\nUnable to update manifest file: {$this->manifestpath}\n Aborting.\n";
exit();
}
}
echo "\nExporting quiz: {$quiz->name} to {$rootdirectory}\n";
chdir($scriptdirectory);
$output = shell_exec('php createrepo.php -r "' . $rootdirectory . '" -i "' . $moodleinstance . '" -l "module" -n ' . $instanceid . ' -t ' . $token . ' -z' . $ignorecat);
$output = shell_exec('php createrepo.php -w -z -r "' . $rootdirectory . '" -i "' . $moodleinstance . '" -l "module" -n ' . $instanceid . ' -t ' . $token . ' -x ' . $ignorecat);
} else {
$rootdirectory = dirname($basedirectory) . '/' . $locarray[$instanceid]->directory;
echo "\nExporting quiz: {$quiz->name} to {$rootdirectory}\n";
chdir($scriptdirectory);
$quizmanifestname = cli_helper::get_manifest_path($moodleinstance, 'module', null,
$contextinfo->contextinfo->coursename, $quiz->name, '');
$output = shell_exec('php exportrepofrommoodle.php -z -r "' . $rootdirectory . '" -i "' . $moodleinstance . '" -f "' . $quizmanifestname . '" -t ' . $token);
$output = shell_exec('php exportrepofrommoodle.php -w -z -r "' . $rootdirectory . '" -i "' . $moodleinstance . '" -f "' . $quizmanifestname . '" -t ' . $token);
}
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 ' . $instanceid . ' -t ' . $token. ' -p "' . $this->manifestpath. '" -f "' . $quizmanifestname . '"');
$output = shell_exec('php exportquizstructurefrommoodle.php -w -z -r "" -i "' . $moodleinstance . '" -n ' . $instanceid . ' -t ' . $token. ' -p "' . $this->manifestpath. '" -f "' . $quizmanifestname . '"');
echo $output;
}
}
Expand Down
4 changes: 2 additions & 2 deletions classes/import_repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -900,14 +900,14 @@ public function update_quizzes($clihelper, $scriptdirectory) {
if (is_dir($rootdirectory . '/top')) {
// Quiz could have no questions in its context.
echo "\nImporting quiz context: {$structurecontents->quiz->name}\n";
$output = shell_exec('php importrepotomoodle.php -z -r "' . $rootdirectory .
$output = shell_exec('php importrepotomoodle.php -w -z -r "' . $rootdirectory .
'" -i "' . $moodleinstance . '" -f "' . $quizmanifestname . '" -t ' . $token);
echo $output;
}
if ($instanceid === false) {
chdir($scriptdirectory);
echo "\nImporting quiz structure: {$structurecontents->quiz->name}\n";
$output = shell_exec('php importquizstructuretomoodle.php -z -r "" -i "' . $moodleinstance . '" -n ' .
$output = shell_exec('php importquizstructuretomoodle.php -w -z -r "" -i "' . $moodleinstance . '" -n ' .
$contextinfo->contextinfo->instanceid . ' -t ' . $token. ' -p "' .
$this->manifestpath. '" -f "' . $quizmanifestname . '" -a "' . $structurefile . '"');
echo $output;
Expand Down
11 changes: 10 additions & 1 deletion cli/createrepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
'description' => 'Is the repo controlled using Git?',
'default' => $usegit,
'variable' => 'usegit',
'valuerequired' => false,
'valuerequired' => true,
],
[
'longopt' => 'ignorecat',
Expand All @@ -155,6 +155,15 @@
'variable' => 'quiet',
'valuerequired' => false,
],
[
'longopt' => 'subcall',
'shortopt' => 'w',
'description' => 'Is this a subcall of the script?',
'default' => false,
'variable' => 'subcall',
'valuerequired' => false,
'hidden' => true,
],
];

if (!function_exists('simplexml_load_file')) {
Expand Down
2 changes: 1 addition & 1 deletion cli/createwholecourserepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
'description' => 'Is the repo controlled using Git?',
'default' => $usegit,
'variable' => 'usegit',
'valuerequired' => false,
'valuerequired' => true,
],
[
'longopt' => 'ignorecat',
Expand Down
2 changes: 1 addition & 1 deletion cli/deletefrommoodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
'description' => 'Is the repo controlled using Git?',
'default' => $usegit,
'variable' => 'usegit',
'valuerequired' => false,
'valuerequired' => true,
],
[
'longopt' => 'ignorecat',
Expand Down
11 changes: 10 additions & 1 deletion cli/exportquizstructurefrommoodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
'description' => 'Is the repo controlled using Git?',
'default' => $usegit,
'variable' => 'usegit',
'valuerequired' => false,
'valuerequired' => true,
],
[
'longopt' => 'modulename',
Expand All @@ -110,6 +110,15 @@
'default' => false,
'variable' => 'quiet',
'valuerequired' => false,
],
[
'longopt' => 'subcall',
'shortopt' => 'w',
'description' => 'Is this a subcall of the script?',
'default' => false,
'variable' => 'subcall',
'valuerequired' => false,
'hidden' => true,
]
];

Expand Down
11 changes: 10 additions & 1 deletion cli/exportrepofrommoodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
'description' => 'Is the repo controlled using Git?',
'default' => $usegit,
'variable' => 'usegit',
'valuerequired' => false,
'valuerequired' => true,
],
[
'longopt' => 'ignorecat',
Expand All @@ -113,6 +113,15 @@
'variable' => 'quiet',
'valuerequired' => false,
],
[
'longopt' => 'subcall',
'shortopt' => 'w',
'description' => 'Is this a subcall of the script?',
'default' => false,
'variable' => 'subcall',
'valuerequired' => false,
'hidden' => true,
],
];

if (!function_exists('simplexml_load_file')) {
Expand Down
2 changes: 1 addition & 1 deletion cli/exportwholecoursefrommoodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
'description' => 'Is the repo controlled using Git?',
'default' => $usegit,
'variable' => 'usegit',
'valuerequired' => false,
'valuerequired' => true,
],
[
'longopt' => 'ignorecat',
Expand Down
11 changes: 10 additions & 1 deletion cli/importquizstructuretomoodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,17 @@
'description' => 'Is the repo controlled using Git?',
'default' => $usegit,
'variable' => 'usegit',
'valuerequired' => true,
],
[
'longopt' => 'subcall',
'shortopt' => 'w',
'description' => 'Is this a subcall of the script?',
'default' => false,
'variable' => 'subcall',
'valuerequired' => false,
]
'hidden' => true,
],
];

if (!function_exists('simplexml_load_file')) {
Expand Down
11 changes: 10 additions & 1 deletion cli/importrepotomoodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
'description' => 'Is the repo controlled using Git?',
'default' => $usegit,
'variable' => 'usegit',
'valuerequired' => false,
'valuerequired' => true,
],
[
'longopt' => 'ignorecat',
Expand All @@ -153,6 +153,15 @@
'variable' => 'quiet',
'valuerequired' => false,
],
[
'longopt' => 'subcall',
'shortopt' => 'w',
'description' => 'Is this a subcall of the script?',
'default' => false,
'variable' => 'subcall',
'valuerequired' => false,
'hidden' => true,
],
];

if (!function_exists('simplexml_load_file')) {
Expand Down
2 changes: 1 addition & 1 deletion cli/importwholecoursetomoodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
'description' => 'Is the repo controlled using Git?',
'default' => $usegit,
'variable' => 'usegit',
'valuerequired' => false,
'valuerequired' => true,
],
[
'longopt' => 'ignorecat',
Expand Down

0 comments on commit 752f503

Please sign in to comment.