Skip to content

Commit

Permalink
Merge pull request #16 from Laur0r/fix/rename_states
Browse files Browse the repository at this point in the history
Solve #14: replace numbers by having constant strings
  • Loading branch information
Laur0r authored Oct 29, 2020
2 parents 2422684 + 0defb43 commit 01364b3
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 16 deletions.
10 changes: 5 additions & 5 deletions classes/chunkupload_form_element.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down
44 changes: 44 additions & 0 deletions classes/state_type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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.
//
// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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;

}
17 changes: 10 additions & 7 deletions classes/task/cleanup_files.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
namespace local_chunkupload\task;

use local_chunkupload\chunkupload_form_element;
use local_chunkupload\state_type;

/**
* Chunkupload Cleanup Task
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion deleteupload_ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 3 additions & 2 deletions proceedupload_ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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();

Expand Down
3 changes: 2 additions & 1 deletion startupload_ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 01364b3

Please sign in to comment.