Skip to content

Commit

Permalink
quiz-data - Unit tests for export_quiz
Browse files Browse the repository at this point in the history
  • Loading branch information
EJMFarrow committed Nov 19, 2024
1 parent a5e3005 commit 377e970
Show file tree
Hide file tree
Showing 11 changed files with 321 additions and 65 deletions.
5 changes: 3 additions & 2 deletions classes/cli_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,13 @@ public function validate_and_clean_args(): void {
break;
}
}
if (!(isset($cliargs['manifestpath']) || isset($cliargs['quizmanifestpath']) || isset($cliargs['nonquizmanifestpath'])) && !isset($cliargs['contextlevel'])) {
if (!(isset($cliargs['manifestpath']) || isset($cliargs['quizmanifestpath'])) && !isset($cliargs['contextlevel'])) {
echo "\nYou have not specified context. " .
"You must specify context level (--contextlevel) unless " .
"using a function where this information can be read from a manifest file, in which case " .
"you could set a manifest path (--manifestpath) instead. If using exportrepofrommoodle, you " .
"must set manifest path only. If you still see this message, you may be using invalid arguments.\n";
"must set manifest path only. If dealing with import/export of quizzes, you must specify --quizmanifestpath. " .
"If you still see this message, you may be using invalid arguments.\n";
static::call_exit();
}

Expand Down
2 changes: 1 addition & 1 deletion classes/create_repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public function create_quiz_directories($clihelper, $scriptdirectory) {
$quizmanifestname = cli_helper::get_manifest_path($moodleinstance, 'module', null,
$contextinfo->contextinfo->coursename, $quiz->name, $rootdirectory);
chdir($scriptdirectory);
$output = shell_exec('php exportquizstructurefrommoodle.php -w -r "" -i "' . $moodleinstance . '" -n ' . $instanceid . ' -t ' . $token. ' -p "' . $this->manifestpath . '" -f "' . $quizmanifestname . '"');
$output = shell_exec('php exportquizstructurefrommoodle.php -w -r "" -i "' . $moodleinstance . ' -t ' . $token. ' -p "' . $this->manifestpath . '" -f "' . $quizmanifestname . '"');
$quizlocation = new \StdClass();
$quizlocation->moduleid = $instanceid;
$quizlocation->directory = basename($rootdirectory);
Expand Down
66 changes: 34 additions & 32 deletions classes/export_quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,6 @@ class export_quiz {
* @var \stdClass|null
*/
public ?\stdClass $nonquizmanifestcontents = null;
/**
* URL of Moodle instance
*
* @var string
*/
/**
* Full path to output file
*
Expand All @@ -112,18 +107,15 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
// Convert command line options into variables.
$arguments = $clihelper->get_arguments();
$moodleinstance = $arguments['moodleinstance'];
$instanceid = $arguments['instanceid'];
$rootdirectory = ($arguments['rootdirectory']) ? $arguments['rootdirectory'] . '/' : '';
if ($arguments['quizmanifestpath']) {
$this->quizmanifestpath = ($arguments['quizmanifestpath']) ?
$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";
$this->call_exit();
}
$instanceid = $this->quizmanifestcontents->context->instanceid;
$rootdirectory = ($arguments['rootdirectory']) ? $arguments['rootdirectory'] . '/' : '';
$this->quizmanifestpath = ($arguments['quizmanifestpath']) ?
$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";
$this->call_exit();
}
$instanceid = $this->quizmanifestcontents->context->instanceid;
if ($arguments['nonquizmanifestpath']) {
$this->nonquizmanifestpath = ($arguments['nonquizmanifestpath']) ?
$rootdirectory . $arguments['nonquizmanifestpath'] : null;
Expand All @@ -147,8 +139,6 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
'wstoken' => $token,
'wsfunction' => 'qbank_gitsync_export_quiz_data',
'moodlewsrestformat' => 'json',
'coursename' => $arguments['coursename'],
'quizname' => $arguments['modulename'],
'moduleid' => $instanceid,
];
$this->curlrequest->set_option(CURLOPT_RETURNTRANSFER, true);
Expand Down Expand Up @@ -209,19 +199,22 @@ public function export_quiz_data() {
if (!$responsejson) {
echo "Broken JSON returned from Moodle:\n";
echo $response . "\n";
echo "{$this->filepath} not updated.\n";
echo "Quiz data file not updated.\n";
$this->call_exit();
$responsejson = json_decode('{"quiz": {"name": ""}, "questions": []}'); // For unit test purposes.
} else if (property_exists($responsejson, 'exception')) {
echo "{$responsejson->message}\n";
if (property_exists($responsejson, 'debuginfo')) {
echo "{$responsejson->debuginfo}\n";
}
echo "{$this->filepath} not updated.\n";
echo "Quiz data file not updated.\n";
$this->call_exit();
$responsejson = json_decode('{"quiz": {"name": ""}, "questions": []}'); // For unit test purposes.
}
$quizmanifestentries = [];
$nonquizmanifestentries = [];
// Determine quiz info location based on loactions of manifest paths.
$missingquestions = false;
// Determine quiz info location based on locations of manifest paths.
if ($this->quizmanifestpath) {
$this->filepath = cli_helper::get_quiz_structure_path($responsejson->quiz->name, dirname($this->quizmanifestpath));
$quizmanifestentries = array_column($this->quizmanifestcontents->questions, null, 'questionbankentryid');
Expand All @@ -242,20 +235,29 @@ public function export_quiz_data() {
$question->nonquizfilepath = $nonquizmanifestentry->filepath;
unset($question->questionbankentryid);
} else {
$missingquestions = true;
$multiple = ($this->quizmanifestpath && $this->nonquizmanifestpath) ? 's' : '';
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 "\nQuestion: {$question->questionbankentryid}\n";
echo "This question is in the quiz but not in the supplied manifest file{$multiple}\n";
}
}
if ($missingquestions) {
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 " .
"you will need to consolidate them.\n";
echo "Quiz structure file: {$this->filepath} not updated.\n";
} else {
// Save exported information (including relative file location but not QBE id so Moodle independent).
$success = file_put_contents($this->filepath, json_encode($responsejson));
if ($success === false) {
echo "\nUnable to update quiz structure file: {$this->filepath}\n Aborting.\n";
$this->call_exit();
}
echo "Quiz data exported to:\n";
echo "{$this->filepath}\n";
}
// Save exported information (including relative file location but not QBE id so Moodle independent).
file_put_contents($this->filepath, json_encode($responsejson));
echo "Quiz data exported to:\n";
echo "{$this->filepath}\n";
}

/**
Expand Down
2 changes: 1 addition & 1 deletion classes/export_repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public function update_quiz_directories($clihelper, $scriptdirectory) {
$quizmanifestname = cli_helper::get_manifest_path($moodleinstance, 'module', null,
$contextinfo->contextinfo->coursename, $quiz->name, $rootdirectory);
chdir($scriptdirectory);
$output = shell_exec('php exportquizstructurefrommoodle.php -w -r "" -i "' . $moodleinstance . '" -n ' . $instanceid . ' -t ' . $token. ' -p "' . $this->manifestpath. '" -f "' . $quizmanifestname . '"');
$output = shell_exec('php exportquizstructurefrommoodle.php -w -r "" -i "' . $moodleinstance . ' -t ' . $token. ' -p "' . $this->manifestpath. '" -f "' . $quizmanifestname . '"');
echo $output;
}
}
Expand Down
24 changes: 0 additions & 24 deletions cli/exportquizstructurefrommoodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,6 @@
'variable' => 'usegit',
'valuerequired' => true,
],
[
'longopt' => 'coursename',
'shortopt' => 'c',
'description' => 'Unique course name for course or module context.',
'default' => null,
'variable' => 'coursename',
'valuerequired' => true,
],
[
'longopt' => 'modulename',
'shortopt' => 'm',
'description' => 'Unique (within course) quiz name.',
'default' => null,
'variable' => 'modulename',
'valuerequired' => true,
],
[
'longopt' => 'instanceid',
'shortopt' => 'n',
'description' => 'Numerical course module id of quiz.',
'default' => null,
'variable' => 'instanceid',
'valuerequired' => true,
],
[
'longopt' => 'subcall',
'shortopt' => 'w',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
"importedversion": "1",
"exportedversion": "1",
"format": "xml"
},
{
"questionbankentryid":"36002",
"filepath": "\/top\/quiz-cat\/Quiz-Question-2.xml",
"importedversion": "1",
"exportedversion": "1",
"format": "xml"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<quiz>
<question type="category">
<category>
<text>top/cat 1</text>
<text>top</text>
</category>
<info format="moodle_auto_format">
<text>First imported folder</text>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<quiz>
<question type="shortanswer">
<name>
<text>Quiz Question 2</text>
</name>
<questiontext format="html">
<text><![CDATA[<p dir="ltr" style="text-align: left;">This is a test question.<br></p>]]></text>
</questiontext>
<generalfeedback format="html">
<text></text>
</generalfeedback>
<defaultgrade>1</defaultgrade>
<penalty>0.3333333</penalty>
<hidden>0</hidden>
<idnumber></idnumber>
<usecase>0</usecase>
<answer fraction="100" format="moodle_auto_format">
<text>This is a test answer.</text>
<feedback format="html">
<text></text>
</feedback>
</answer>
</question>
</quiz>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<quiz>
<question type="shortanswer">
<name>
<text>Quiz Question 3</text>
</name>
<questiontext format="html">
<text><![CDATA[<p dir="ltr" style="text-align: left;">This is a test question.<br></p>]]></text>
</questiontext>
<generalfeedback format="html">
<text></text>
</generalfeedback>
<defaultgrade>1</defaultgrade>
<penalty>0.3333333</penalty>
<hidden>0</hidden>
<idnumber></idnumber>
<usecase>0</usecase>
<answer fraction="100" format="moodle_auto_format">
<text>This is a test answer.</text>
<feedback format="html">
<text></text>
</feedback>
</answer>
</question>
</quiz>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<quiz>
<question type="category">
<category>
<text>top/quiz-cat</text>
</category>
<info format="moodle_auto_format">
<text>First imported folder</text>
</info>
<idnumber></idnumber>
</question>
</quiz>
Loading

0 comments on commit 377e970

Please sign in to comment.