Skip to content

Commit

Permalink
feat: adjust to qppe changes
Browse files Browse the repository at this point in the history
  • Loading branch information
janbritz authored Aug 26, 2024
1 parent 06e630e commit 48b6501
Show file tree
Hide file tree
Showing 22 changed files with 726 additions and 603 deletions.
23 changes: 3 additions & 20 deletions classes/api/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use moodle_exception;
use qtype_questionpy\array_converter\array_converter;
use qtype_questionpy\package\package_raw;
use qtype_questionpy\package\package_versions_info;
use stored_file;
use TypeError;

Expand All @@ -33,7 +34,7 @@ class api {
/**
* Retrieves QuestionPy packages from the application server.
*
* @return package_raw[]
* @return package_versions_info[]
* @throws moodle_exception
*/
public function get_packages(): array {
Expand All @@ -45,7 +46,7 @@ public function get_packages(): array {
$result = [];
foreach ($packages as $package) {
try {
$result[] = array_converter::from_array(package_raw::class, $package);
$result[] = array_converter::from_array(package_versions_info::class, $package);
} catch (TypeError $e) {
// TODO: decide what to do with faulty package.
debugging($e->getMessage());
Expand All @@ -55,24 +56,6 @@ public function get_packages(): array {
return $result;
}

/**
* Retrieves the package with the given hash, returns null if not found.
*
* @param string $hash the hash of the package to get
* @return ?package_raw the package with the given hash or null if not found
* @throws moodle_exception
*/
public function get_package(string $hash): ?package_raw {
$connector = connector::default();
$response = $connector->get("/packages/$hash");

if ($response->code === 404) {
return null;
}
$response->assert_2xx();
return array_converter::from_array(package_raw::class, $response->get_data());
}

/**
* Returns the {@see package_api} of a specific package.
*
Expand Down
20 changes: 13 additions & 7 deletions classes/external/load_packages.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use external_value;
use moodle_exception;
use qtype_questionpy\api\api;
use qtype_questionpy\package\package;
use qtype_questionpy\package\package_version;

/**
Expand Down Expand Up @@ -60,17 +61,22 @@ public static function execute(): array {

$transaction = $DB->start_delegated_transaction();

// Remove every package version.
$versions = package_version::get_many();
foreach ($versions as $version) {
$version->delete();
}

// Load and store packages from the application server.
$api = new api();
$packages = $api->get_packages();
$incomingpackageids = [];
foreach ($packages as $package) {
$package->store();
[$incomingpackageids[], ] = $package->upsert();
}

// Remove old packages.
if (empty($incomingpackageids)) {
// There are no incoming packages -> remove every package.
package::delete_all();
} else {
[$sql, $params] = $DB->get_in_or_equal($incomingpackageids, SQL_PARAMS_NAMED, prefix: 'packageid', equal: false);
$removedpackageids = $DB->get_fieldset_select('qtype_questionpy_package', 'id', "id $sql", $params);
package::delete_by_id(...$removedpackageids);
}

$transaction->allow_commit();
Expand Down
11 changes: 2 additions & 9 deletions classes/external/remove_packages.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use external_single_structure;
use external_value;
use moodle_exception;
use qtype_questionpy\package\package_version;
use qtype_questionpy\package\package;

/**
* This service removes every package from the database.
Expand Down Expand Up @@ -55,14 +55,7 @@ public static function execute_parameters(): external_function_parameters {
public static function execute(): array {
global $DB;

$transaction = $DB->start_delegated_transaction();

$versions = package_version::get_many();
foreach ($versions as $version) {
$version->delete();
}

$transaction->allow_commit();
package::delete_all();

return [
'packages' => $DB->count_records('qtype_questionpy_package'),
Expand Down
93 changes: 46 additions & 47 deletions classes/package/package.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,6 @@ public function __construct(int $id, string $shortname, string $namespace, array
);
}

/**
* Retrieves each version of the package.
*
* @return array
* @throws moodle_exception
*/
public function get_version_array(): array {
global $DB;
return $DB->get_records('qtype_questionpy_pkgversion', ['packageid' => $this->id]);
}

/**
* Returns the package of the given package version id.
*
Expand All @@ -101,22 +90,57 @@ public static function get_by_version(int $pkgversionid): package {
}

/**
* Get packages from the db matching given conditions. Note: only conditions stored in the package version table
* are applicable.
* Deletes a package and every version from the database with the given id.
*
* @param array|null $conditions
* @return package[]
* @param int ...$ids
* @throws moodle_exception
*/
public static function get_records(?array $conditions = null): array {
public static function delete_by_id(int ...$ids): void {
global $DB;
$packages = [];
$records = $DB->get_records('qtype_questionpy_package', $conditions);
foreach ($records as $record) {
$package = self::get_package_data($record->id);
$packages[] = array_converter::from_array(self::class, (array) $package);

if (empty($ids)) {
return;
}

$transaction = $DB->start_delegated_transaction();
foreach ($ids as $id) {
$DB->delete_records('qtype_questionpy_pkgversion', ['packageid' => $id]);
$DB->delete_records('qtype_questionpy_language', ['packageid' => $id]);
$DB->delete_records('qtype_questionpy_pkgtag', ['packageid' => $id]);
$DB->delete_records('qtype_questionpy_package', ['id' => $id]);
last_used_service::remove_by_package($id);
}
return $packages;

$DB->execute("
DELETE
FROM {qtype_questionpy_tag}
WHERE id NOT IN (
SELECT tagid
FROM {qtype_questionpy_pkgtag}
)
");
$transaction->allow_commit();
}

/**
* Deletes every package and every version from the database.
*
* @throws moodle_exception
*/
public static function delete_all(): void {
global $DB;
$ids = $DB->get_fieldset('qtype_questionpy_package', 'id');
self::delete_by_id(...$ids);
}

/**
* Deletes the package and every version from the database.
*
* @return void
* @throws moodle_exception
*/
public function delete(): void {
self::delete_by_id($this->id);
}

/**
Expand Down Expand Up @@ -178,31 +202,6 @@ private static function get_tag_data(int $packageid): array {
", ['packageid' => $packageid]);
}

/**
* Deletes the package and every version from the database.
*
* @return void
* @throws moodle_exception
*/
public function delete(): void {
global $DB;
$transaction = $DB->start_delegated_transaction();
$DB->delete_records('qtype_questionpy_pkgversion', ['packageid' => $this->id]);
$DB->delete_records('qtype_questionpy_language', ['packageid' => $this->id]);
$DB->delete_records('qtype_questionpy_pkgtag', ['packageid' => $this->id]);
$DB->execute("
DELETE
FROM {qtype_questionpy_tag}
WHERE id NOT IN (
SELECT tagid
FROM {qtype_questionpy_pkgtag}
)
");
$DB->delete_records('qtype_questionpy_package', ['id' => $this->id]);
last_used_service::remove_by_package($this->id);
$transaction->allow_commit();
}

/**
* Provides the differences between two packages, i.e. an array with all the parameters which are different in the
* two objects.
Expand Down
24 changes: 12 additions & 12 deletions classes/package/package_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,57 +30,57 @@ class package_base {
/**
* @var string package shortname
*/
protected string $shortname;
public readonly string $shortname;

/**
* @var string package namespace
*/
protected string $namespace;
public readonly string $namespace;

/**
* @var array package name
*/
protected array $name;
public readonly array $name;

/**
* @var string package type
*/
protected string $type;
public readonly string $type;

/**
* @var string|null package author
*/
protected ?string $author;
public readonly ?string $author;

/**
* @var string|null package url
*/
protected ?string $url;
public readonly ?string $url;

/**
* @var array|null package languages
*/
protected ?array $languages;
public readonly ?array $languages;

/**
* @var array|null package description
*/
protected ?array $description;
public readonly ?array $description;

/**
* @var string|null package icon
*/
protected ?string $icon;
public readonly ?string $icon;

/**
* @var string|null package license
*/
protected ?string $license;
public readonly ?string $license;

/**
* @var array|null package tags
*/
protected ?array $tags;
public readonly ?array $tags;

/**
* Constructs package class.
Expand Down Expand Up @@ -118,7 +118,7 @@ public function __construct(string $shortname, string $namespace, array $name, s
/**
* Creates a localized array representation of the package.
*
* @param array|null $languages
* @param array $languages
* @return array array representation of the package
* @throws moodle_exception
*/
Expand Down
Loading

0 comments on commit 48b6501

Please sign in to comment.