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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
<?php
namespace eTask;
use JSONArrayObject;
use User;
/**
* eTask conforming test definition.
*
* @property int $id database column
* @property string $title database column
* @property string $description 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[] $testtasks has_many TestTask
* @property \SimpleORMapCollection|Assignment[] $assignments has_many Assignment
* @property \User $owner belongs_to \User
* @property \SimpleORMapCollection|Task[] $tasks has_and_belongs_to_many Task
*/
class Test extends \SimpleORMap implements \PrivacyObject
{
use ConfigureTrait;
/**
* @see SimpleORMap::configure
*/
protected static function configure($config = [])
{
$config['db_table'] = 'etask_tests';
$config['relationTypes'] = self::configureClassNames($config);
$config['has_and_belongs_to_many']['tasks'] = [
'class_name' => $config['relationTypes']['Task'],
'assoc_foreign_key' => 'id',
'thru_table' => 'etask_test_tasks',
'thru_key' => 'test_id',
'thru_assoc_key' => 'task_id',
'order_by' => 'ORDER BY position'
];
$config['has_many']['testtasks'] = [
'class_name' => $config['relationTypes']['TestTask'],
'assoc_foreign_key' => 'test_id',
'on_delete' => 'delete',
'on_store' => 'store'
];
$config['belongs_to']['owner'] = [
'class_name' => User::class,
'foreign_key' => 'user_id'
];
$config['has_many']['assignments'] = [
'class_name' => $config['relationTypes']['Assignment'],
'assoc_foreign_key' => 'test_id',
'on_delete' => 'delete',
'on_store' => 'store'
];
$config['serialized_fields']['options'] = JSONArrayObject::class;
parent::configure($config);
}
/**
* Retrieve the tasks associated to this test.
* @deprecated - use $this->tasks instead.
*
* @return SimpleORMapCollection the associated tasks
*/
public function getTasks()
{
$this->initRelation('tasks');
return $this->relations['tasks'];
}
/**
* Count all the tasks related to this test.
*
* @return int the number of related tasks
*/
public function countTasks()
{
return count($this->testtasks);
}
/**
* Convenience method to associate a task to this test.
* Creates a TestTask object and returns it.
*
* @param eTask\Task task the task to associate
*
* @return eTask\TestTask the created TestTask object
*/
public function addTask($task)
{
$class = static::config('relationTypes')['TestTask'];
$testTask = $class::create(
[
'test_id' => $this->id,
'task_id' => $task->id,
'position' => $this->countTasks() + 1,
'options' => []
]
);
$this->resetRelation('tasks');
$this->resetRelation('testtasks');
return $testTask;
}
/**
* 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 Tests'), 'etask_tests', $field_data);
}
}
$field_data = \DBManager::get()->fetchAll("SELECT * FROM etask_test_tags WHERE user_id =?", [$storage->user_id]);
if ($field_data) {
$storage->addTabularData(_('eTask Tests Tags'), 'etask_test_tags', $field_data);
}
}
}
|