aboutsummaryrefslogtreecommitdiff
path: root/lib/models/eTask/Task.php
blob: 68d4a208a5812bab91c24757469a1765268b2dcb (plain)
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
<?php

namespace eTask;

/**
 * eTask conforming task definition.
 *
 * @property int id database column
 * @property string type database column
 * @property string title database column
 * @property string description database column
 * @property string task database column
 * @property string user_id database column
 * @property string mkdate database column
 * @property string chdate database column
 * @property string options database column
 * @property User owner belongs_to User
 * @property SimpleORMapCollection tests additional field etask\Test
 * @property SimpleORMapCollection test_tasks has_many etask\TestTask
 * @property SimpleORMapCollection responses has_many etask\Response
 * @property JSONArrayobject task serialized database column
 * @property JSONArrayobject options serialized database column
 */
class Task extends \SimpleORMap implements \PrivacyObject
{
    use ConfigureTrait;

    /**
     * @see SimpleORMap::configure
     */
    protected static function configure($config = [])
    {
        $config['db_table'] = 'etask_tasks';

        $config['relationTypes'] = self::configureClassNames($config);

        $config['belongs_to']['owner'] = [
            'class_name' => '\\User',
            'foreign_key' => 'user_id'
        ];

        $config['additional_fields']['tests']['get'] = 'getTests';

        $config['has_many']['test_tasks'] = [
            'class_name' => $config['relationTypes']['TestTask'],
            'assoc_foreign_key' => 'task_id',
            'on_delete' => 'delete',
            'on_store' => 'store'
        ];

        $config['has_many']['responses'] = [
            'class_name' => $config['relationTypes']['Response'],
            'assoc_foreign_key' => 'task_id',
            'on_delete' => 'delete',
            'on_store' => 'store'
        ];

        $config['serialized_fields']['task'] = 'JSONArrayObject';
        $config['serialized_fields']['options'] = 'JSONArrayObject';

        parent::configure($config);
    }

    /**
     * Retrieve the tests associated to this task.
     *
     * @return SimpleORMapCollection the associated tests
     */
    public function getTests()
    {
        $klass = $this->relationTypes['Test'];

        return \SimpleORMapCollection::createFromArray(
            $klass::findThru(
                $this->id,
                [
                    'thru_table' => 'etask_test_tasks',
                    'thru_key' => 'task_id',
                    'thru_assoc_key' => 'test_id',
                    'assoc_foreign_key' => 'id',
                    'order_by' => 'ORDER BY etask_tests.chdate ASC'
                ]
            )
        );
    }

    /**
     * Export available data of a given user into a storage object
     * (an instance of the StoredUserData class) for that user.
     *
     * @param StoredUserData $storage object to store data into
     */
    public static function exportUserData(\StoredUserData $storage)
    {
        $sorm = self::findBySQL("user_id = ?", [$storage->user_id]);
        if ($sorm) {
            $field_data = [];
            foreach ($sorm as $row) {
                $field_data[] = $row->toRawArray();
            }
            if ($field_data) {
                $storage->addTabularData(_('eTask Aufgaben'), 'etask_tasks', $field_data);
            }
        }

        $field_data = \DBManager::get()->fetchAll("SELECT * FROM etask_task_tags WHERE user_id =?", [$storage->user_id]);
        if ($field_data) {
            $storage->addTabularData(_('eTask Aufgaben Tags'), 'etask_task_tags', $field_data);
        }
    }
}