diff --git a/classes/chunkupload_form_element.php b/classes/chunkupload_form_element.php index 42b3842..82102d8 100644 --- a/classes/chunkupload_form_element.php +++ b/classes/chunkupload_form_element.php @@ -115,7 +115,7 @@ public function tohtml() { if ($value = $this->getvalue()) { global $DB; if ($record = $DB->get_record('local_chunkupload_files', ['id' => $value])) { - if ($record->state == 2) { + if ($record->state == state_type::UPLOAD_COMPLETED) { $filenamestring = $record->filename; $showfinishedicon = true; } @@ -202,10 +202,10 @@ public function validatesubmitvalue($value) { return ""; } $record = $DB->get_record('local_chunkupload_files', ['id' => $value]); - if (!$record || $record->state == 0) { + if (!$record || $record->state == state_type::UNUSED_TOKEN_GENERATED) { return ""; } - if ($record->state == 1) { + if ($record->state == state_type::UPLOAD_STARTED) { return get_string('uploadnotfinished', 'local_chunkupload'); } $path = self::get_path_for_id($value); @@ -292,7 +292,7 @@ public static function export_to_filearea($chunkuploadid, $newcontextid, $newcom global $DB; $fs = get_file_storage(); $record = $DB->get_record('local_chunkupload_files', ['id' => $chunkuploadid], '*', IGNORE_MISSING); - if (!$record || $record->state !== 2) { + if (!$record || $record->state !== state_type::UPLOAD_COMPLETED) { return null; } @@ -338,7 +338,7 @@ public static function is_file_uploaded($id) { return false; } - if (!$record->state == 2) { + if (!$record->state == state_type::UPLOAD_COMPLETED) { return false; } return true; diff --git a/classes/state_type.php b/classes/state_type.php new file mode 100644 index 0000000..d0b6b2f --- /dev/null +++ b/classes/state_type.php @@ -0,0 +1,44 @@ +. + +/** + * Defines available state_types. + * @package local_chunkupload + * @copyright 2020 Laura Troost, Nina Herrmann WWU + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace local_chunkupload; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Defines available state_types. + * @package local_chunkupload + * @copyright 2020 Laura Troost, Nina Herrmann WWU + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class state_type { + + /** @var string Represents the type for a step subplugin. 0: token generated, not used; 1: file upload started; + * 2: file upload completed + */ + const UNUSED_TOKEN_GENERATED = 0; + /** @var string Represents the type for a trigger subplugin. */ + const UPLOAD_STARTED = 1; + /** @var string Represents the type for a trigger subplugin. */ + const UPLOAD_COMPLETED = 2; + +} diff --git a/classes/task/cleanup_files.php b/classes/task/cleanup_files.php index 2c5ce57..ac35dfb 100644 --- a/classes/task/cleanup_files.php +++ b/classes/task/cleanup_files.php @@ -25,6 +25,7 @@ namespace local_chunkupload\task; use local_chunkupload\chunkupload_form_element; +use local_chunkupload\state_type; /** * Chunkupload Cleanup Task @@ -51,13 +52,14 @@ public function execute() { global $DB; $config = get_config('local_chunkupload'); - // State 0. - $DB->delete_records_select('local_chunkupload_files', 'state = 0 AND lastmodified < :time', - array('time' => time() - $config->state0duration)); + // State UNUSED_TOKEN_GENERATED 0. + $DB->delete_records_select('local_chunkupload_files', 'state = :state AND lastmodified < :time', + array('time' => time() - $config->state0duration, 'state' => state_type::UNUSED_TOKEN_GENERATED)); - // State 1. + // State UPLOAD_STARTED 1. $ids = $DB->get_fieldset_select('local_chunkupload_files', 'id', - 'lastmodified < :time AND state = 1', array('time' => time() - $config->state1duration)); + 'lastmodified < :time AND state = :state', array('time' => time() - $config->state1duration, + 'state' => state_type::UPLOAD_STARTED)); $DB->delete_records_list('local_chunkupload_files', 'id', $ids); foreach ($ids as $id) { $path = chunkupload_form_element::get_path_for_id($id); @@ -66,9 +68,10 @@ public function execute() { } } - // State 2. + // State UPLOAD_COMPLETED 2. $ids = $DB->get_fieldset_select('local_chunkupload_files', 'id', - 'lastmodified < :time AND state = 2', array('time' => time() - $config->state2duration)); + 'lastmodified < :time AND state = :state', array('time' => time() - $config->state2duration, + 'state' => state_type::UPLOAD_COMPLETED)); $DB->delete_records_list('local_chunkupload_files', 'id', $ids); foreach ($ids as $id) { $path = chunkupload_form_element::get_path_for_id($id); diff --git a/deleteupload_ajax.php b/deleteupload_ajax.php index 15e0472..381fc5f 100644 --- a/deleteupload_ajax.php +++ b/deleteupload_ajax.php @@ -70,7 +70,7 @@ $record->currentpos = 0; $record->length = 0; $record->lastmodified = time(); - $record->state = 0; + $record->state = \local_chunkupload\state_type::UNUSED_TOKEN_GENERATED; $record->filename = ""; $DB->update_record('local_chunkupload_files', $record); } diff --git a/proceedupload_ajax.php b/proceedupload_ajax.php index b5da8b5..4cdb327 100644 --- a/proceedupload_ajax.php +++ b/proceedupload_ajax.php @@ -82,7 +82,7 @@ die(json_encode($err)); } -if ($record->state != 1) { +if ($record->state != \local_chunkupload\state_type::UPLOAD_STARTED) { $err->error = "File is in state $record->state, unable to proceed upload!"; die(json_encode($err)); } @@ -113,7 +113,8 @@ file_put_contents($path, $content, FILE_APPEND); -$record->state = $end == $record->length ? 2 : 1; +$record->state = $end == $record->length ? \local_chunkupload\state_type::UPLOAD_COMPLETED : + \local_chunkupload\state_type::UPLOAD_STARTED; $record->currentpos = $end; $record->lastmodified = time(); diff --git a/startupload_ajax.php b/startupload_ajax.php index 43699dc..40b8f38 100644 --- a/startupload_ajax.php +++ b/startupload_ajax.php @@ -106,7 +106,8 @@ $filerecord->currentpos = $end; $filerecord->length = $length; $filerecord->lastmodified = time(); -$filerecord->state = $end == $length ? 2 : 1; +$filerecord->state = $end == $length ? \local_chunkupload\state_type::UPLOAD_COMPLETED : + \local_chunkupload\state_type::UPLOAD_STARTED; $filerecord->filename = $filename; $DB->update_record('local_chunkupload_files', $filerecord);