Skip to content

Commit

Permalink
quiz-data - Make import quiz easier. Not quite functioning - need to …
Browse files Browse the repository at this point in the history
…match two calls to import structure.
  • Loading branch information
EJMFarrow committed Dec 5, 2024
1 parent 297d494 commit 4a192d1
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 8 deletions.
2 changes: 2 additions & 0 deletions classes/external/import_quiz_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public static function execute_parameters() {
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'success' => new external_value(PARAM_BOOL, 'Import success?'),
'cmid' => new external_value(PARAM_SEQUENCE, 'CMID of quiz'),
]);
}

Expand Down Expand Up @@ -246,6 +247,7 @@ public static function execute(array $quiz, array $sections, array $questions, ?

$response = new \stdClass();
$response->success = true;
$response->cmid = (int) $module->cmid;
return $response;
}
}
63 changes: 55 additions & 8 deletions classes/import_quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
$arguments = $clihelper->get_arguments();
$moodleinstance = $arguments['moodleinstance'];
$instanceid = $arguments['instanceid'];
if ($arguments['quizmanifestpath']) {
if (!empty($arguments['quizmanifestpath'])) {
$this->quizmanifestpath = ($arguments['quizmanifestpath']) ?
$arguments['rootdirectory'] . '/' . $arguments['quizmanifestpath'] : null;
$this->quizmanifestcontents = json_decode(file_get_contents($this->quizmanifestpath));
Expand All @@ -137,19 +137,43 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {

} else {
$this->cmid = $this->quizmanifestcontents->context->instanceid;
$this->quizdatapath = cli_helper::get_quiz_structure_path($this->quizmanifestcontents->context->modulename,
dirname($this->quizmanifestpath));
$this->quizdatapath = ($arguments['quizdatapath']) ? $arguments['rootdirectory'] . '/' . $arguments['quizdatapath']
: cli_helper::get_quiz_structure_path($this->quizmanifestcontents->context->modulename,
dirname($this->quizmanifestpath));
}
} else {
if ($arguments['quizdatapath']) {
$this->quizdatapath = $arguments['rootdirectory'] . '/' . $arguments['quizdatapath'];
} else {
echo "\nPlease supply a quiz manifest filepath or a quiz data filepath.\nAborting.\n";
$this->call_exit();
return; // Required for unit tests.
if (empty($arguments['createquiz'])) {
echo "\nPlease supply a quiz manifest filepath or a quiz data filepath.\nAborting.\n";
$this->call_exit();
return; // Required for unit tests.
} else {
if ($arguments['directory']) {
$directory = $arguments['rootdirectory'] . '/' . $arguments['directory'];
} else {
$directory = $arguments['rootdirectory'];
}
$quizfiles = scandir($directory);
$structurefile = null;
// Find the structure file.
foreach ($quizfiles as $quizfile) {
if (preg_match('/.*_quiz\.json/', $quizfile)) {
$structurefile = $quizfile;
break;
}
}
if (!$structurefile) {
echo "\nNo quiz structure file found.\nAborting.\n";
$this->call_exit();
return; // Required for unit tests.
}
$this->quizdatapath = $directory . '/' . $structurefile;
}
}
}
if ($arguments['nonquizmanifestpath']) {
if (!empty($arguments['nonquizmanifestpath'])) {
$this->nonquizmanifestpath = ($arguments['nonquizmanifestpath']) ?
$arguments['rootdirectory'] . '/' . $arguments['nonquizmanifestpath'] : null;
$this->nonquizmanifestcontents = json_decode(file_get_contents($this->nonquizmanifestpath));
Expand Down Expand Up @@ -212,7 +236,7 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
if ($arguments['subcall']) {
echo "\nCreating quiz: {$this->quizdatacontents->quiz->name}\n";
} else {
echo "\nPreparing to create a new quiz in Moodle.\n";
echo "\nPreparing to create/update a quiz in Moodle.\n";
echo "Moodle URL: {$this->moodleurl}\n";
echo "Course: {$instanceinfo->contextinfo->coursename}\n";
echo "Quiz: {$this->quizdatacontents->quiz->name}\n";
Expand Down Expand Up @@ -249,6 +273,29 @@ public function process(): void {
$this->import_quiz_data();
}

public function import_all($clihelper, $scriptdirectory): void {
$arguments = $clihelper->get_arguments();
$moodleinstance = $arguments['moodleinstance'];
if ($arguments['directory']) {
$directory = $arguments['rootdirectory'] . '/' . $arguments['directory'];
} else {
$directory = $arguments['rootdirectory'];
}
if (is_array($arguments['token'])) {
$token = $arguments['token'][$moodleinstance];
} else {
$token = $arguments['token'];
}
$ignorecat = $arguments['ignorecat'];
$ignorecat = ($ignorecat) ? ' -x "' . $ignorecat . '"' : '';
$this->import_quiz_data();
chdir($scriptdirectory);
$output = shell_exec('php importrepotomoodle.php -u ' . $arguments['usegit'] . ' -w -r "' . $directory .
'" -i "' . $moodleinstance . '" -l "module" -n ' . $this->cmid . ' -t ' . $token . $ignorecat);
echo $output;
$this->import_quiz_data();
}

/**
* Wrapper for cURL request to allow mocking.
*
Expand Down
150 changes: 150 additions & 0 deletions cli/importquiztomoodle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php
// This file is part of Stack - http://stack.maths.ed.ac.uk/
//
// Stack is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Stack is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Stack. If not, see <http://www.gnu.org/licenses/>.

/**
* Import a git repo containing questions into Moodle.
*
* @package qbank_gitsync
* @copyright 2024 University of Edinburgh
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace qbank_gitsync;
define('CLI_SCRIPT', true);
require_once('./config.php');
require_once('../classes/curl_request.php');
require_once('../classes/cli_helper.php');
require_once('../classes/import_quiz.php');

$options = [
[
'longopt' => 'moodleinstance',
'shortopt' => 'i',
'description' => 'Key of Moodle instance in $moodleinstances to use. ' .
'Should match end of instance URL.',
'default' => $instance,
'variable' => 'moodleinstance',
'valuerequired' => true,
],
[
'longopt' => 'rootdirectory',
'shortopt' => 'r',
'description' => "Directory on user's computer containing repos.",
'default' => $rootdirectory,
'variable' => 'rootdirectory',
'valuerequired' => true,
],
[
'longopt' => 'directory',
'shortopt' => 'd',
'description' => 'Directory of repo on users computer containing "top" folder, ' .
'relative to root directory.',
'default' => '',
'variable' => 'directory',
'valuerequired' => true,
],
[
'longopt' => 'subdirectory',
'shortopt' => 's',
'description' => 'Relative subdirectory of repo to actually import.',
'default' => null,
'variable' => 'subdirectory',
'valuerequired' => true,
],
[
'longopt' => 'contextlevel',
'shortopt' => 'l',
'description' => 'Context in which to place quiz. Always course.',
'default' => 'course',
'variable' => 'contextlevel',
'valuerequired' => true,
'hidden' => true,
],
[
'longopt' => 'quizdatapath',
'shortopt' => 'a',
'description' => 'Filepath of quiz data file relative to root directory.',
'default' => null,
'variable' => 'quizdatapath',
'valuerequired' => true,
],
[
'longopt' => 'coursename',
'shortopt' => 'c',
'description' => 'Unique course name for course or module context.',
'default' => null,
'variable' => 'coursename',
'valuerequired' => true,
],
[
'longopt' => 'instanceid',
'shortopt' => 'n',
'description' => 'Numerical id of the course.',
'default' => null,
'variable' => 'instanceid',
'valuerequired' => true,
],
[
'longopt' => 'token',
'shortopt' => 't',
'description' => 'Security token for webservice.',
'default' => $token,
'variable' => 'token',
'valuerequired' => true,
],
[
'longopt' => 'help',
'shortopt' => 'h',
'description' => '',
'default' => false,
'variable' => 'help',
'valuerequired' => false,
],
[
'longopt' => 'createquiz',
'shortopt' => 'k',
'description' => 'Are we creating a quiz?',
'default' => true,
'variable' => 'createquiz',
'valuerequired' => false,
'hidden' => true,
],
[
'longopt' => 'usegit',
'shortopt' => 'u',
'description' => 'Is the repo controlled using Git?',
'default' => $usegit,
'variable' => 'usegit',
'valuerequired' => true,
],
[
'longopt' => 'ignorecat',
'shortopt' => 'x',
'description' => 'Regex of categories to ignore - add an extra leading / for Windows.',
'default' => $ignorecat,
'variable' => 'ignorecat',
'valuerequired' => true,
],
];

if (!function_exists('simplexml_load_file')) {
echo 'Please install the PHP library SimpleXML.' . "\n";
exit;
}
$scriptdirectory = dirname(__FILE__);
$clihelper = new cli_helper($options);
$importquiz = new import_quiz($clihelper, $moodleinstances);
$importquiz->import_all($clihelper, $scriptdirectory);

0 comments on commit 4a192d1

Please sign in to comment.