aboutsummaryrefslogtreecommitdiff
path: root/lib/models/eTask/Task.php
blob: 40c5404ac94556994ab8a53ec6cede3f15c3b2e6 (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
<?php

namespace eTask;

use JSONArrayObject;
use User;

/**
 * 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 \JSONArrayObject $task database column
 * @property string $user_id database column
 * @property int $mkdate database column
 * @property int $chdate database column
 * @property \JSONArrayObject $options database column
 * @property \SimpleORMapCollection|TestTask[] $test_tasks has_many TestTask
 * @property \SimpleORMapCollection|Response[] $responses has_many Response
 * @property \User $owner belongs_to \User
 * @property \SimpleORMapCollection|Test[] $tests has_and_belongs_to_many Test
 */
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::class,
            'foreign_key' => 'user_id'
        ];

        $config['has_and_belongs_to_many']['tests'] = [
            'class_name' => $config['relationTypes']['Test'],
            'assoc_foreign_key' => 'id',
            'thru_table' => 'etask_test_tasks',
            'thru_key' => 'task_id',
            'thru_assoc_key' => 'test_id'
        ];

        $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::class;
        $config['serialized_fields']['options'] = JSONArrayObject::class;

        parent::configure($config);
    }

    /**
     * Retrieve the tests associated to this task.
     * @deprecated - use $this->tests instead.
     *
     * @return SimpleORMapCollection the associated tests
     */
    public function getTests()
    {
        $this->initRelation('tests');
        return $this->relations['tests'];
    }

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