Skip to content

Commit

Permalink
quiz-data - Fix defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
EJMFarrow committed Dec 3, 2024
1 parent abb5d9a commit 52d5d47
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 20 deletions.
10 changes: 5 additions & 5 deletions classes/create_repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ public function create_quiz_directories(object $clihelper, string $scriptdirecto
echo "\nExporting quiz: {$quiz->name} to {$rootdirectory}\n";
$output = $this->call_repo_creation($rootdirectory, $moodleinstance, $instanceid, $token, $ignorecat, $scriptdirectory);
echo $output;
$quizmanifestname = cli_helper::get_manifest_path($moodleinstance, 'module', null,
$quizmanifestpath = cli_helper::get_manifest_path($moodleinstance, 'module', null,
$contextinfo->contextinfo->coursename, $quiz->name, $rootdirectory);
$output = $this->call_export_quiz($moodleinstance, $token, $quizmanifestname, $scriptdirectory);
$output = $this->call_export_quiz($moodleinstance, $token, $quizmanifestpath, $scriptdirectory);
$quizlocation = new \StdClass();
$quizlocation->moduleid = $instanceid;
$quizlocation->directory = basename($rootdirectory);
Expand Down Expand Up @@ -313,13 +313,13 @@ public function call_repo_creation(string $rootdirectory, string $moodleinstance
*
* @param string $moodleinstance
* @param string $token
* @param string $quizmanifestname
* @param string $quizmanifestpath
* @return string|null
*/
public function call_export_quiz(string $moodleinstance, string $token,
string $quizmanifestname, string $scriptdirectory): ?string {
string $quizmanifestpath, string $scriptdirectory): ?string {
chdir($scriptdirectory);
return shell_exec('php exportquizstructurefrommoodle.php -w -r "" -i "' . $moodleinstance . '" -t "'
. $token. '" -p "' . $this->manifestpath . '" -f "' . $quizmanifestname . '"');
. $token. '" -p "' . $this->manifestpath . '" -f "' . $quizmanifestpath . '"');
}
}
11 changes: 6 additions & 5 deletions classes/export_repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ public function update_quiz_directories($clihelper, $scriptdirectory) {
} else if (!is_dir(dirname($basedirectory) . '/' . $locarray[$instanceid]->directory)) {
$rootdirectory = dirname($basedirectory) . '/' . $locarray[$instanceid]->directory;
mkdir($rootdirectory);
mkdir($rootdirectory . '/top');
echo "\nExporting quiz: {$quiz->name} to {$rootdirectory}\n";
$output = $this->call_repo_creation($rootdirectory, $moodleinstance,
$instanceid, $token, $ignorecat, $scriptdirectory);
Expand All @@ -342,9 +343,9 @@ public function update_quiz_directories($clihelper, $scriptdirectory) {
$quizmanifestname, $scriptdirectory);
}
echo $output;
$quizmanifestname = cli_helper::get_manifest_path($moodleinstance, 'module', null,
$quizmanifestpath = cli_helper::get_manifest_path($moodleinstance, 'module', null,
$contextinfo->contextinfo->coursename, $quiz->name, $rootdirectory);
$output = $this->call_export_quiz($moodleinstance, $token, $quizmanifestname, $scriptdirectory);
$output = $this->call_export_quiz($moodleinstance, $token, $quizmanifestpath, $scriptdirectory);
echo $output;
}
}
Expand Down Expand Up @@ -372,14 +373,14 @@ public function call_repo_creation(string $rootdirectory, string $moodleinstance
*
* @param string $moodleinstance
* @param string $token
* @param string $quizmanifestname
* @param string $quizmanifestpath
* @return string|null
*/
public function call_export_quiz(string $moodleinstance, string $token,
string $quizmanifestname, string $scriptdirectory): ?string {
string $quizmanifestpath, string $scriptdirectory): ?string {
chdir($scriptdirectory);
return shell_exec('php exportquizstructurefrommoodle.php -w -r "" -i "' . $moodleinstance . ' -t '
. $token. ' -p "' . $this->manifestpath . '" -f "' . $quizmanifestname . '"');
. $token. ' -p "' . $this->manifestpath . '" -f "' . $quizmanifestpath . '"');
}

/**
Expand Down
4 changes: 4 additions & 0 deletions classes/export_trait.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public function export_to_repo() {
* @return void
*/
public function export_to_repo_main_process(object $moodlequestionlist): void {
// Make top folder in case we don't have any questions.
if (!is_dir(dirname($this->manifestpath) . '/top')) {
mkdir(dirname($this->manifestpath) . '/top');
}
$this->subdirectory = 'top';
$questionsinmoodle = $moodlequestionlist->questions;
$this->postsettings['includecategory'] = 1;
Expand Down
1 change: 1 addition & 0 deletions classes/external/import_quiz_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ public static function execute(array $quiz, array $sections, array $questions, a
}
});
if ($params['quiz']['cmid']) {
// We can only add questions if the quiz already exists.
foreach ($params['questions'] as $question) {
$qdata = get_minimal_question_data($question['questionbankentryid']);
// Double-check user has question access.
Expand Down
13 changes: 3 additions & 10 deletions cli/importwholecoursetomoodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@
[
'longopt' => 'contextlevel',
'shortopt' => 'l',
'description' => 'Context in which to place questions. Set to system, coursecategory, course or module',
'default' => null,
'description' => 'Context from which to extract questions. Should always be course.',
'default' => 'course',
'variable' => 'contextlevel',
'valuerequired' => true,
'hidden' => true,
],
[
'longopt' => 'coursename',
Expand All @@ -97,14 +98,6 @@
'variable' => 'modulename',
'valuerequired' => true,
],
[
'longopt' => 'coursecategory',
'shortopt' => 'g',
'description' => 'Unique course category name for coursecategory context.',
'default' => null,
'variable' => 'coursecategory',
'valuerequired' => true,
],
[
'longopt' => 'instanceid',
'shortopt' => 'n',
Expand Down
18 changes: 18 additions & 0 deletions tests/external/get_question_list_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,21 @@ public function test_list(): void {
['name' => self::QNAME . '2', 'category' => $qcategory2->id]);
$qbankentryid2 = $DB->get_field('question_versions', 'questionbankentryid',
['questionid' => $q2->id], $strictness = MUST_EXIST);
$quizgenerator = new \testing_data_generator();
$quizgenerator = $quizgenerator->get_plugin_generator('mod_quiz');

$quiz = $quizgenerator->create_instance(['course' => $this->course->id,
'name' => 'Quiz 1', 'questionsperpage' => 0,
'grade' => 100.0, 'sumgrades' => 2, 'preferredbehaviour' => 'immediatefeedback']);

\quiz_add_quiz_question($this->q->id, $quiz);
\quiz_add_quiz_question($q2->id, $quiz);
if (class_exists('\mod_quiz\quiz_settings')) {
$quizobj = \mod_quiz\quiz_settings::create($quiz->id);
} else {
$quizobj = \quiz::create($quiz->id);
}
\mod_quiz\structure::create_for_quiz($quizobj);
$sink = $this->redirectEvents();
$returnvalue = get_question_list::execute('top', 50, $this->course->fullname, null, null,
null, null, false, ['']);
Expand Down Expand Up @@ -208,6 +223,9 @@ public function test_list(): void {
$this->assertEquals($this->course->id, $returnvalue['contextinfo']['instanceid']);
$this->assertEquals(null, $returnvalue['contextinfo']['categoryname']);
$this->assertEquals(null, $returnvalue['contextinfo']['modulename']);
$this->assertEquals(1, count($returnvalue['quizzes']));
$this->assertEquals('Quiz 1', $returnvalue['quizzes'][0]['name']);
$this->assertEquals($quiz->cmid, $returnvalue['quizzes'][0]['instanceid']);

$events = $sink->get_events();
$this->assertEquals(count($events), 0);
Expand Down

0 comments on commit 52d5d47

Please sign in to comment.