Skip to content

Commit

Permalink
MBS-8820: Fix a lot of coding style errors
Browse files Browse the repository at this point in the history
  • Loading branch information
PM84 committed Mar 6, 2024
1 parent afbdfbf commit 761ddde
Show file tree
Hide file tree
Showing 36 changed files with 641 additions and 595 deletions.
154 changes: 78 additions & 76 deletions classes/ArchiveJob.php

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions classes/BackupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
class BackupManager {

/** @var \stdClass Backup controller metadata from DB */
protected \stdClass $backup_metadata;
protected \stdClass $backupmetadata;

/** @var array Define what to include and exclude in backups */
const BACKUP_SETTINGS = [
Expand Down Expand Up @@ -150,7 +150,7 @@ public function is_associated_with_job(ArchiveJob $job): bool {
* @throws \base_task_exception
* @throws \dml_exception
*/
protected static function initiate_backup(string $type, int $id, int $user_id): object {
protected static function initiate_backup(string $type, int $id, int $userid): object {
global $CFG;

// Validate type and set variables accordingly
Expand All @@ -172,7 +172,7 @@ protected static function initiate_backup(string $type, int $id, int $user_id):
backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO,
backup::MODE_ASYNC,
$user_id,
$userid,
backup::RELEASESESSION_YES
);
$backupid = $bc->get_backupid();
Expand All @@ -184,8 +184,8 @@ protected static function initiate_backup(string $type, int $id, int $user_id):
if ($task instanceof \backup_root_task) {
$task->get_setting('filename')->set_value($filename);

foreach (self::BACKUP_SETTINGS as $setting_name => $setting_value) {
$task->get_setting($setting_name)->set_value($setting_value);
foreach (self::BACKUP_SETTINGS as $settingname => $settingvalue) {
$task->get_setting($settingname)->set_value($settingvalue);
}
}
}
Expand All @@ -195,7 +195,7 @@ protected static function initiate_backup(string $type, int $id, int $user_id):
$asynctask = new \core\task\asynchronous_backup_task();
$asynctask->set_blocking(false);
$asynctask->set_custom_data(['backupid' => $backupid]);
$asynctask->set_userid($user_id);
$asynctask->set_userid($userid);
\core\task\manager::queue_adhoc_task($asynctask);

// Generate backup file url
Expand All @@ -208,14 +208,14 @@ protected static function initiate_backup(string $type, int $id, int $user_id):
$filename
));

$internal_wwwroot = get_config('quiz_archiver')->internal_wwwroot;
if ($internal_wwwroot) {
$url = str_replace(rtrim($CFG->wwwroot, '/'), rtrim($internal_wwwroot, '/'), $url);
$internalwwwroot = get_config('quiz_archiver')->internal_wwwroot;
if ($internalwwwroot) {
$url = str_replace(rtrim($CFG->wwwroot, '/'), rtrim($internalwwwroot, '/'), $url);
}

return (object) [
'backupid' => $backupid,
'userid' => $user_id,
'userid' => $userid,
'context' => $contextid,
'component' => 'backup',
'filearea' => $type,
Expand All @@ -238,8 +238,8 @@ protected static function initiate_backup(string $type, int $id, int $user_id):
* @throws \base_task_exception
* @throws \dml_exception
*/
public static function initiate_quiz_backup(int $cm_id, int $user_id): object {
return self::initiate_backup(backup::TYPE_1ACTIVITY, $cm_id, $user_id);
public static function initiate_quiz_backup(int $cmid, int $userid): object {
return self::initiate_backup(backup::TYPE_1ACTIVITY, $cmid, $userid);
}

/**
Expand All @@ -252,8 +252,8 @@ public static function initiate_quiz_backup(int $cm_id, int $user_id): object {
* @throws \base_task_exception
* @throws \dml_exception
*/
public static function initiate_course_backup(int $course_id, int $user_id): object {
return self::initiate_backup(backup::TYPE_1COURSE, $course_id, $user_id);
public static function initiate_course_backup(int $courseid, int $userid): object {
return self::initiate_backup(backup::TYPE_1COURSE, $courseid, $userid);
}

}
54 changes: 27 additions & 27 deletions classes/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ class FileManager {
const ARTIFACT_EXPORT_TEMPFILE_LIFETIME_SECONDS = 86400;

/** @var int ID of the course this FileManager is associated with */
protected int $course_id;
protected int $courseid;
/** @var int ID of the course module this FileManager is associated with */
protected int $cm_id;
protected int $cmid;
/** @var int ID of the quiz this FileManager is associated with */
protected int $quiz_id;
protected int $quizid;
/** @var context_course Context of the course this FileManager is associated with */
protected context_course $context;

Expand All @@ -71,11 +71,11 @@ class FileManager {
* @param int $cm_id ID of the course module
* @param int $quiz_id ID of the quiz
*/
public function __construct(int $course_id, int $cm_id, int $quiz_id) {
$this->course_id = $course_id;
$this->cm_id = $cm_id;
$this->quiz_id = $quiz_id;
$this->context = context_course::instance($course_id);
public function __construct(int $courseid, int $cmid, int $quizid) {
$this->course_id = $courseid;
$this->cm_id = $cmid;
$this->quiz_id = $quizid;
$this->context = context_course::instance($courseid);
}

/**
Expand All @@ -87,17 +87,17 @@ public function __construct(int $course_id, int $cm_id, int $quiz_id) {
* @param int $quiz_id ID of the quiz
* @return string Path according to passed IDs
*/
public static function get_file_path(int $course_id = -1, int $cm_id = -1, int $quiz_id = -1): string {
public static function get_file_path(int $courseid = -1, int $cmid = -1, int $quizid = -1): string {
$path = '';

if ($course_id > 0) {
$path .= "/$course_id";
if ($courseid > 0) {
$path .= "/$courseid";

if ($cm_id > 0) {
$path .= "/$cm_id";
if ($cmid > 0) {
$path .= "/$cmid";

if ($quiz_id > 0) {
$path .= "/$quiz_id";
if ($quizid > 0) {
$path .= "/$quizid";
}
}
}
Expand Down Expand Up @@ -196,12 +196,12 @@ public static function hash_file(stored_file $file, string $algo = 'sha256'): ?s

// Calculate file hash chunk-wise
$fh = $file->get_content_file_handle(stored_file::FILE_HANDLE_FOPEN);
$hash_ctx = hash_init($algo);
$hashctx = hash_init($algo);
while (!feof($fh)) {
hash_update($hash_ctx, fgets($fh, 4096));
hash_update($hashctx, fgets($fh, 4096));
}

return hash_final($hash_ctx);
return hash_final($hashctx);
}

/**
Expand Down Expand Up @@ -380,24 +380,24 @@ public function extract_attempt_data_from_artifact(stored_file $artifactfile, in
}

// Create new archive from extracted attempt data into temp filearea
$export_expiry = time() + self::ARTIFACT_EXPORT_TEMPFILE_LIFETIME_SECONDS;
$export_file = $packer->archive_to_storage(
$exportexpiry = time() + self::ARTIFACT_EXPORT_TEMPFILE_LIFETIME_SECONDS;
$exportfile = $packer->archive_to_storage(
[
$workdir."/attemptdata"
$workdir."/attemptdata",
],
$this->context->id,
self::COMPONENT_NAME,
self::TEMP_FILEAREA_NAME,
0,
"/{$export_expiry}/",
"/{$exportexpiry}/",
"attempt_export_jid{$jobid}_cid{$this->course_id}_cmid{$this->cm_id}_qid{$this->quiz_id}_aid{$attemptid}.tar.gz",
);

if (!$export_file) {
if (!$exportfile) {
throw new \moodle_exception('Failed to create attempt data archive');
}

return $export_file;
return $exportfile;
} catch (\Exception $e) {
// Ignore skipped archives but always execute cleanup code!
if (!($e instanceof \invalid_state_exception)) {
Expand Down Expand Up @@ -427,7 +427,7 @@ public static function cleanup_temp_files(): int {
// Prepare
$fs = get_file_storage();
$now = time();
$files_deleted = 0;
$filesdeleted = 0;

// Query using raw SQL to get temp files independent of contextid to speed this up a LOT
$tempfilerecords = $DB->get_records_sql("
Expand All @@ -445,13 +445,13 @@ public static function cleanup_temp_files(): int {
if ($expiry < $now) {
$fs->get_file_by_id($f->id)->delete();
if ($f->filesize > 0) {
$files_deleted++;
$filesdeleted++;
}
}
}
}

return $files_deleted;
return $filesdeleted;
}

}
Binary file added classes/JAP.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 22 additions & 22 deletions classes/RemoteArchiveWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
class RemoteArchiveWorker {

/** @var string URL of the remote Quiz Archive Worker instance */
protected string $server_url;
protected string $serverurl;
/** @var int Seconds to wait until a connection can be established before aborting */
protected int $connection_timeout;
protected int $connectiontimeout;
/** @var int Seconds to wait for the request to complete before aborting */
protected int $request_timeout;
protected int $requesttimeout;
/** @var \stdClass Moodle config object for this plugin */
protected \stdClass $config;

Expand All @@ -54,10 +54,10 @@ class RemoteArchiveWorker {
* @param int $request_timeout Seconds to wait for the request to complete before aborting
* @throws \dml_exception If retrieving of the plugin config failed
*/
public function __construct(string $server_url, int $connection_timeout, int $request_timeout) {
$this->server_url = $server_url;
$this->connection_timeout = $connection_timeout;
$this->request_timeout = $request_timeout;
public function __construct(string $serverurl, int $connectiontimeout, int $requesttimeout) {
$this->server_url = $serverurl;
$this->connection_timeout = $connectiontimeout;
$this->request_timeout = $requesttimeout;
$this->config = get_config('quiz_archiver');
}

Expand All @@ -79,46 +79,46 @@ public function __construct(string $server_url, int $connection_timeout, int $re
* service or decoding of the response failed
* @throws \RuntimeException if the archive worker service reported an error
*/
public function enqueue_archive_job(string $wstoken, int $courseid, int $cmid, int $quizid, array $job_options, $task_archive_quiz_attempts, $task_moodle_backups) {
public function enqueue_archive_job(string $wstoken, int $courseid, int $cmid, int $quizid, array $joboptions, $taskarchivequizattempts, $taskmoodlebackups) {
global $CFG;
$moodle_url_base = rtrim($this->config->internal_wwwroot ?: $CFG->wwwroot, '/');
$moodleurlbase = rtrim($this->config->internal_wwwroot ?: $CFG->wwwroot, '/');

// Prepare request payload
$request_payload = json_encode(array_merge(
$requestpayload = json_encode(array_merge(
[
"api_version" => self::API_VERSION,
"moodle_base_url" => $moodle_url_base,
"moodle_ws_url" => $moodle_url_base.'/webservice/rest/server.php',
"moodle_upload_url" => $moodle_url_base.'/webservice/upload.php',
"moodle_base_url" => $moodleurlbase,
"moodle_ws_url" => $moodleurlbase.'/webservice/rest/server.php',
"moodle_upload_url" => $moodleurlbase.'/webservice/upload.php',
"wstoken" => $wstoken,
"courseid" => $courseid,
"cmid" => $cmid,
"quizid" => $quizid,
"task_archive_quiz_attempts" => $task_archive_quiz_attempts,
"task_moodle_backups" => $task_moodle_backups,
"task_archive_quiz_attempts" => $taskarchivequizattempts,
"task_moodle_backups" => $taskmoodlebackups,
],
$job_options
$joboptions
));

// Execute request
// Moodle curl wrapper automatically closes curl handle after requests. No need to call curl_close() manually.
$c = new curl(['ignoresecurity' => true]); // Ignore URL filter since we require custom ports and the URL is only configurable by admins
$result = $c->post($this->server_url, $request_payload, [
$result = $c->post($this->server_url, $requestpayload, [
'CURLOPT_CONNECTTIMEOUT' => $this->connection_timeout,
'CURLOPT_TIMEOUT' => $this->request_timeout,
'CURLOPT_HTTPHEADER' => [
'Content-Type: application/json',
'Content-Length: '.strlen($request_payload),
]
'Content-Length: '.strlen($requestpayload),
],
]);

$http_status = $c->get_info()['http_code']; // Invalid PHPDoc in Moodle curl wrapper. Array returned instead of string
$httpstatus = $c->get_info()['http_code']; // Invalid PHPDoc in Moodle curl wrapper. Array returned instead of string
$data = json_decode($result);

// Handle errors
if ($http_status != 200) {
if ($httpstatus != 200) {
if ($data === null) {
throw new \UnexpectedValueException("Decoding of the archive worker response failed. HTTP status code $http_status");
throw new \UnexpectedValueException("Decoding of the archive worker response failed. HTTP status code $httpstatus");
}
throw new \RuntimeException($data->error);
} else {
Expand Down
Loading

0 comments on commit 761ddde

Please sign in to comment.