From 37db482851a7724520e5db7aef89dcd8490cc081 Mon Sep 17 00:00:00 2001
From: Malf Furious <m@lfurio.us>
Date: Thu, 1 Jun 2017 23:35:44 -0400
Subject: Add issue class

---
 app/class/issue.class.php | 117 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 117 insertions(+)
 create mode 100644 app/class/issue.class.php

(limited to 'app/class')

diff --git a/app/class/issue.class.php b/app/class/issue.class.php
new file mode 100644
index 0000000..e439dff
--- /dev/null
+++ b/app/class/issue.class.php
@@ -0,0 +1,117 @@
+<?php
+
+/*
+ * SCROTT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * For more information, please refer to UNLICENSE
+ */
+
+require_once "class/object.class.php";
+require_once "class/stage.class.php";
+require_once "class/user.class.php";
+require_once "class/mesg.class.php";
+
+/*
+ * This class models Scrott issues.  Issues represent units of work, can
+ * be assigned to users, and advance through a pipeline.
+ */
+class issue extends object
+{
+    /*
+     * Constructor
+     */
+    public function __construct(?string $guid = NULL)
+    {
+        $this->fields['issues'] = array(
+            "guid",
+            "numb",
+            "assignee",
+            "seen",
+            "description",
+            "due",
+            "tags",
+        );
+
+        parent::__construct($guid);
+        $this->expectType("issue");
+    }
+
+    /*
+     * Initialize a new issue object with the given name, parent, and
+     * owner.
+     */
+    public static function initNew(string $name, user $owner, stage $parent) : issue
+    {
+        $pad = $parent->getParent();
+        $numb = $pad->issueNumb++;
+        $pad->saveObj();
+
+        $issue = new issue();
+        $issue->setOwner($owner);
+        $issue->setParent($parent);
+        $issue->name = $name;
+        $issue->objtype = "issue";
+        $issue->numb = $numb;
+        $issue->saveObj();
+        return $issue;
+    }
+
+    /*
+     * Get all activity for this issue.  Messages are sorted by date
+     * created.
+     */
+    public function getMesgs_ordByDatetime() : array
+    {
+        $query = "SELECT guid FROM objects WHERE objtype = 'mesg' AND " .
+            "parent = '" . database::esc($this->guid) . "' ORDER BY created";
+        $res = database::query($query);
+
+        $mesgs = array();
+
+        foreach ($res as $m)
+            $mesgs[] = new mesg($m['guid']);
+
+        return $mesgs;
+    }
+
+    /*
+     * Reset the seen flag and reassign this issue.
+     */
+    public function assignTo(user $assignee) : void
+    {
+        $this->seen = 0;
+        $this->assignee = $assignee->guid;
+        $this->saveObj();
+    }
+
+    /*
+     * Advance this issue in the pipeline, closing it if already in the
+     * last stage.
+     */
+    public function advance() : void
+    {
+        $stage = $this->getParent();
+
+        if (!($next = $stage->getNext()))
+            $this->close();
+
+        $this->setParent($next);
+    }
+
+    /*
+     * Mark this issue as completed and closed.
+     */
+    public function close() : void
+    {
+        $pad = $this->getParent()->getParent();
+        $this->setParent($pad);
+    }
+}
+
+?>
-- 
cgit v1.2.3