Explorar el Código

Add ability to mark projects as finished

Alan Lam hace 7 años
padre
commit
95501ee25d
Se han modificado 7 ficheros con 85 adiciones y 3 borrados
  1. 26 0
      controllers/project_mark_finished.php
  2. 21 1
      engine/project.php
  3. 4 2
      engine/user.php
  4. 4 0
      engine/util.php
  5. 1 0
      routes.php
  6. 24 0
      views/project.php
  7. 5 0
      views/project_list.php

+ 26 - 0
controllers/project_mark_finished.php

@@ -0,0 +1,26 @@
+<?php
+
+$project = Project::get($id);
+if(!$project) throw new Exception("Project cannot be found.");
+if(!Core::$user->hasPermission($project)) throw new Exception("You do not have permission to access this resource.");
+
+try {
+
+	switch($action) {
+		case "mark-finished":
+			$project->markFinished();
+			$message = "The project has been marked as finished.";
+			break;
+		case "mark-unfinished":
+			$project->markUnfinished();
+			$message = "The project has been marked as unfinished.";
+			break;
+	}
+
+	Core::addAlert("success", $message);
+}
+catch(Exception $e) {
+	Core::addAlert("danger", $e->getMessage());
+}
+
+HTTP::redirect($project->getURL());

+ 21 - 1
engine/project.php

@@ -4,7 +4,7 @@ class Project extends Model {
 
 	protected static $table = "project";
 	protected static $id_field = "id";
-	protected static $fillable_fields = ["customer_id", "template_id", "name", "test_count", "min_age", "max_age", "default_tester_fee", "time_of_day", "tester_can_claim", "template_overrides_json", "dts_start", "dts_deadline", "dts_created", "dts_updated", "dts_report"];
+	protected static $fillable_fields = ["customer_id", "template_id", "name", "test_count", "min_age", "max_age", "default_tester_fee", "time_of_day", "tester_can_claim", "template_overrides_json", "dts_start", "dts_deadline", "dts_created", "dts_updated", "dts_report", "dts_finished", "user_id_finished"];
 
 	protected static $field_map = [
 	];
@@ -83,6 +83,10 @@ class Project extends Model {
 		}
 	}
 
+	public function getURL() {
+		return "/account/projects/{$this->id}";
+	}
+
 	public function isAnytime() {
 		return $this->time_of_day == "A";
 	}
@@ -100,6 +104,22 @@ class Project extends Model {
 		return time() > $deadline_unix;
 	}
 
+	public function markFinished() {
+
+		if(!Core::$user) throw new Exception("Trying to mark project as finished while not logged in.");
+		if(!Core::$user->isAdmin()) throw new Exception("Trying to mark project as finished with non-admin account.");
+
+		$this->dts_finished = Util::getUTCDateTime();
+		$this->user_id_finished = Core::$user->id;
+		return $this->commit();
+	}
+
+	public function markUnfinished() {
+		$this->dts_finished = null;
+		$this->user_id_finished = null;
+		return $this->commit();
+	}
+
 }
 
 ?>

+ 4 - 2
engine/user.php

@@ -483,6 +483,8 @@ class User extends Model {
 		return User::PAYMENT_METHODS[$this->preferred_payment_method];
 	}
 
-}
+	public function getURL() {
+		return "/account/users/{$this->id}";
+	}
 
-?>
+}

+ 4 - 0
engine/util.php

@@ -207,6 +207,10 @@ class Util {
 		return gmdate("Y-m-d", $time);
 	}
 
+	public static function getUTCDateTime($time = false) {
+		return Util::getDateTime($time);
+	}
+
 	public static function getDateTime($time = false) {
 		if(!$time) $time = time();
 		return gmdate("Y-m-d H:i:s", $time);

+ 1 - 0
routes.php

@@ -67,6 +67,7 @@ $routes = [
 	"/^account\/projects\/$id_pattern\/export-csv$/" => ["controller" => "project_csv", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/$id_pattern\/export-customer-csv$/" => ["controller" => "project_customer_csv", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/$id_pattern\/export-kml$/" => ["controller" => "project_kml", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
+	"/^account\/projects\/$id_pattern\/(mark-finished|mark-unfinished)$/" => ["controller" => "project_mark_finished", "params" => ["id", "action"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/$id_pattern\/(get-locations|get-testers|create-test|assign-tester)$/" => ["controller" => "project_ajax", "params" => ["id", "action"], "type" => "json", "require_user_level" => User::LEVEL_ADMIN],
 
 	// Tests

+ 24 - 0
views/project.php

@@ -32,6 +32,18 @@ var projectID = <?=$project->id?>;
 					<a class="dropdown-item" href="/account/projects/<?=$project->id?>/export-csv">Export CSV</a>
 					<a class="dropdown-item" href="/account/projects/<?=$project->id?>/export-customer-csv">Export Customer CSV</a>
 					<a class="dropdown-item" href="/account/projects/<?=$project->id?>/export-kml">Export KML</a>
+					<?php
+					if($project->dts_finished) {
+						?>
+						<a class="dropdown-item" href="/account/projects/<?=$project->id?>/mark-unfinished">Mark Unfinished</a>
+						<?php
+					}
+					else {
+						?>
+						<a class="dropdown-item" href="/account/projects/<?=$project->id?>/mark-finished">Mark Finished</a>
+						<?php
+					}
+					?>
 				</div>
 			</div>
 			<?php
@@ -40,6 +52,18 @@ var projectID = <?=$project->id?>;
 		</div>
 	</div>
 
+	<?php
+	if($project->dts_finished) {
+		$user = User::queryOne(["id" => $project->user_id_finished]);
+		?>
+		<div class="alert alert-success">
+			<i class="fa fa-fw fa-check"></i>
+			This project was marked as finished by <a href="<?=htmlspecialchars($user->getURL())?>"><?=htmlspecialchars($user->getDisplayName())?></a> on <?=Util::getDisplayDateTimeFromUTC($project->dts_finished)?>.
+		</div>
+		<?php
+	}
+	?>
+
 	<div class="row">
 		<div class="col-md-6">
 			<div class="card mb-3">

+ 5 - 0
views/project_list.php

@@ -51,6 +51,11 @@
 					<i class="fal fa-<?=$time_of_day_icon?>" data-toggle="tooltip" data-title="<?=$time_of_day_text?> Tests"></i>
 					<?php
 				}
+				if($project->dts_finished) {
+					?>
+					<i class="fa fa-fw fa-check text-success" title="Finished" data-toggle="tooltip"></i>
+					<?php
+				}
 				?>
 			</td>
 			<td class="align-middle">