summaryrefslogtreecommitdiffstats
path: root/app/class/issue.class.php
blob: 3127a8708c38e335b7059c2aa08f263137bc43aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?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 ($stage->objtype != "stage")
            return;

        if (!($next = $stage->getNext()))
            $this->close();
        else
            $this->setParent($next);
    }

    /*
     * Mark this issue as completed and closed.
     */
    public function close() : void
    {
        $pad = $this->getParent()->getParent();

        if ($pad)
            $this->setParent($pad);
    }
}

?>