Skip to content

Commit

Permalink
Merge pull request #57 from maths/quiz-data
Browse files Browse the repository at this point in the history
Quiz data
  • Loading branch information
EJMFarrow authored Dec 10, 2024
2 parents 703dedd + e61b047 commit 84242f0
Show file tree
Hide file tree
Showing 73 changed files with 5,542 additions and 260 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/moodle-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

services:
postgres:
image: postgres:13
image: postgres:14
env:
POSTGRES_USER: 'postgres'
POSTGRES_HOST_AUTH_METHOD: 'trust'
Expand All @@ -32,7 +32,16 @@ jobs:
matrix: # I don't know why, but mariadb is much slower, so mostly use pgsql.
include:
- php: '8.2'
moodle-branch: 'master'
moodle-branch: 'main'
database: 'pgsql'
- php: '8.2'
moodle-branch: 'MOODLE_405_STABLE'
database: 'pgsql'
- php: '8.2'
moodle-branch: 'MOODLE_404_STABLE'
database: 'pgsql'
- php: '8.1'
moodle-branch: 'MOODLE_403_STABLE'
database: 'pgsql'
- php: '8.1'
moodle-branch: 'MOODLE_402_STABLE'
Expand Down
158 changes: 105 additions & 53 deletions classes/cli_helper.php

Large diffs are not rendered by default.

125 changes: 115 additions & 10 deletions classes/create_repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ class create_repo {
* @var string
*/
public string $manifestpath;
/**
* Path to actual manifest file.
*
* @var string|null
*/
public ?string $nonquizmanifestpath;
/**
* Path to temporary manifest file.
*
Expand All @@ -109,6 +115,12 @@ class create_repo {
* @var string
*/
public string $moodleurl;
/**
* Are we using git?.
* Set in config. Adds commit hash to manifest.
* @var bool
*/
public bool $usegit;

/**
* Constructor.
Expand All @@ -121,11 +133,19 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
// (Moodle code rules don't allow 'extract()').
$arguments = $clihelper->get_arguments();
$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'];
}
if (!empty($arguments['nonquizmanifestpath'])) {
$this->nonquizmanifestpath = ($arguments['rootdirectory']) ?
$arguments['rootdirectory'] . '/' . $arguments['nonquizmanifestpath'] : $arguments['nonquizmanifestpath'];
} else {
$this->nonquizmanifestpath = null;
}
$this->subcategory = ($arguments['subcategory']) ? $arguments['subcategory'] : 'top';
if (is_array($arguments['token'])) {
$token = $arguments['token'][$moodleinstance];
Expand All @@ -134,8 +154,8 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
}
$contextlevel = $arguments['contextlevel'];
$coursename = $arguments['coursename'];
$modulename = $arguments['modulename'];
$coursecategory = $arguments['coursecategory'];
$modulename = (isset($arguments['modulename'])) ? $arguments['modulename'] : null;
$coursecategory = (isset($arguments['coursecategory'])) ? $arguments['coursecategory'] : null;
$qcategoryid = $arguments['qcategoryid'];
$instanceid = $arguments['instanceid'];
$this->ignorecat = $arguments['ignorecat'];
Expand Down Expand Up @@ -197,6 +217,18 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
$this->tempfilepath = str_replace(cli_helper::MANIFEST_FILE,
'_export' . cli_helper::TEMP_MANIFEST_FILE,
$this->manifestpath);
$this->manifestcontents = new \stdClass();
$this->manifestcontents->context = new \stdClass();
$this->manifestcontents->context->contextlevel = cli_helper::get_context_level($instanceinfo->contextinfo->contextlevel);
$this->manifestcontents->context->coursename = $instanceinfo->contextinfo->coursename;
$this->manifestcontents->context->modulename = $instanceinfo->contextinfo->modulename;
$this->manifestcontents->context->coursecategory = $instanceinfo->contextinfo->categoryname;
$this->manifestcontents->context->instanceid = $instanceinfo->contextinfo->instanceid;
$this->manifestcontents->context->defaultsubcategoryid = $this->qcategoryid;
$this->manifestcontents->context->defaultsubdirectory = null;
$this->manifestcontents->context->defaultignorecat = $this->ignorecat;
$this->manifestcontents->context->moodleurl = $this->moodleurl;
$this->manifestcontents->questions = [];
}

/**
Expand All @@ -205,14 +237,11 @@ public function __construct(cli_helper $clihelper, array $moodleinstances) {
*
* @return void
*/
public function process():void {
$this->manifestcontents = new \stdClass();
$this->manifestcontents->context = null;
$this->manifestcontents->questions = [];
public function process(): void {
$this->export_to_repo();
$this->manifestcontents->context->defaultsubdirectory = $this->subdirectory;
cli_helper::create_manifest_file($this->manifestcontents, $this->tempfilepath,
$this->manifestpath, $this->moodleurl,
$this->qcategoryid, $this->subdirectory, false);
$this->manifestpath, false);
unlink($this->tempfilepath);
}

Expand All @@ -222,7 +251,83 @@ public function process():void {
* @param string $wsurl webservice URL
* @return curl_request
*/
public function get_curl_request($wsurl):curl_request {
public function get_curl_request($wsurl): curl_request {
return new \qbank_gitsync\curl_request($wsurl);
}

/**
* Create quiz directories and populate.
* @param object $clihelper
* @param string $scriptdirectory - directory of CLI scripts
* @return void
*/
public function create_quiz_directories(object $clihelper, string $scriptdirectory): void {
$contextinfo = $clihelper->check_context($this, false, true);
$arguments = $clihelper->get_arguments();
if ($arguments['directory']) {
$basedirectory = $arguments['rootdirectory'] . '/' . $arguments['directory'];
} else {
$basedirectory = $arguments['rootdirectory'];
}
$moodleinstance = $arguments['moodleinstance'];
$instanceid = $arguments['instanceid'];
if (is_array($arguments['token'])) {
$token = $arguments['token'][$moodleinstance];
} else {
$token = $arguments['token'];
}
$ignorecat = $arguments['ignorecat'];
$ignorecat = ($ignorecat) ? ' -x "' . $ignorecat . '"' : '';
$quizlocations = [];
foreach ($contextinfo->quizzes as $quiz) {
$instanceid = $quiz->instanceid;
$quizdirectory = cli_helper::get_quiz_directory($basedirectory, $quiz->name);
$rootdirectory = $clihelper->create_directory($quizdirectory);
echo "\nExporting quiz: {$quiz->name} to {$rootdirectory}\n";
$output = $this->call_repo_creation($rootdirectory, $moodleinstance, $instanceid, $token, $ignorecat, $scriptdirectory);
echo $output;
$quizmanifestpath = cli_helper::get_manifest_path($moodleinstance, 'module', null,
$contextinfo->contextinfo->coursename, $quiz->name, $rootdirectory);
$output = $this->call_export_quiz($moodleinstance, $token, $quizmanifestpath,
$this->manifestpath, $scriptdirectory);
$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 $output;
}
if ($arguments['usegit'] && empty($arguments['subcall'])) {
// Commit the final quiz file.
// The others are committed by the following createrepo.
chdir($basedirectory);
exec("git add --all");
exec('git commit -m "Initial Commit - final update"');
}
}

/**
* Separate out exec call for mocking.
*
* @param string $rootdirectory
* @param string $moodleinstance
* @param string $instanceid
* @param string $token
* @param string $ignorecat
* @param string $scriptdirectory
* @return string|null
*/
public function call_repo_creation(string $rootdirectory, string $moodleinstance, string $instanceid,
string $token, string $ignorecat, string $scriptdirectory
): ?string {
chdir($scriptdirectory);
return shell_exec('php createrepo.php -u ' . $this->usegit . ' -w -r "' .
$rootdirectory . '" -i "' . $moodleinstance .
'" -l "module" -n ' . $instanceid . ' -t ' . $token . $ignorecat);
}
}
Loading

0 comments on commit 84242f0

Please sign in to comment.