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
|
<?php
namespace Lti;
use User;
use Range;
use Avatar;
use SimpleORMap;
use SimpleORMapCollection;
use Studip\Lti\Enum\PublicationStatus;
use Studip\Lti\Enum\UserProvisioningMode;
/**
* @property string $name
* @property string $version
* @property string $status
* @property string $publication_key
* @property array $config_values
* @property Range $range
* @property User $user
* @property int $mkdate
* @property int $chdate
* @property SimpleORMapCollection<Config> $configs
* @property SimpleORMapCollection<User> $members
*/
class Publication extends SimpleORMap
{
protected static function configure($config = []): void
{
$config['db_table'] = 'lti_publications';
$config['belongs_to']['user'] = [
'class_name' => User::class,
'foreign_key' => 'user_id',
'assoc_foreign_key' => 'user_id'
];
$config['additional_fields']['range'] = [
'set' => function (Publication $publication, string $field, Range | string | null $range) {
if ($range instanceof Range) {
$publication->range_id = $range->getRangeId();
}
},
'get' => function (Publication $publication): ?Range {
return get_object_by_range_id($publication->range_id);
},
];
$config['has_many']['configs'] = [
'class_name' => Config::class,
'assoc_foreign_key' => 'configurable_id'
];
$config['has_many']['members'] = [
'class_name' => PublicationUser::class,
'assoc_foreign_key' => 'publication_id'
];
$config['additional_fields']['config_values']['get'] = 'getConfigValues';
parent::configure($config);
}
public function getConfigValues(bool $typeCasting = false): array
{
$dateFields = ['start_date', 'end_date', 'enrollment_deadline'];
$numberFields = ['maximum_enrolled_users'];
return collect(parent::__get('configs'))->mapWithKeys(function ($config) use ($typeCasting, $dateFields, $numberFields) {
$key = strtolower($config->name);
$value = $config->value;
if ($value && $typeCasting && in_array($key, $dateFields, true)) {
$value = date('c', $value);
} else if ($value && $typeCasting && in_array($key, $numberFields, true)) {
$value = (int) $value;
} else if (in_array($key, ['provisioning_mode_instructor', 'provisioning_mode_student']) && $typeCasting) {
$value = UserProvisioningMode::get($value);
}
return [$key => $value];
})->toArray();
}
public function transformData($with = []): array
{
$base = [
...$this->toRawArray(),
'status' => PublicationStatus::get($this->status),
'range_name' => $this->range?->getFullName(),
'mkdate' => date('c', $this->mkdate),
'chdate' => date('c', $this->chdate),
'custom_parameter' => 'id='. $this->publication_key,
'user' => [
'id' => $this->user->id,
'name' => $this->user->getFullName(),
'username' => $this->user->username,
'avatar_url' => Avatar::getAvatar($this->user->id)->getURL(Avatar::MEDIUM)
],
...$this->getConfigValues(true)
];
if (in_array('members', $with)) {
$base['members'] = $this->members->transformData();
}
return $base;
}
}
|