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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
<?php
/**
*
* @author Nils Gehrke <nils.gehrke@uni-goettingen.de>
*
* @property integer id database column
* @property string user_id database column
* @property string range_id database column
* @property string range_type database column:
* name of class that implements FeedbackRange
*
* @property string course_id database column
* @property string question database column
* @property string description database column
* @property integer mode database column:
* 0 without rating;
* 1 with star rating from 1 to 5;
* 2 with star rating from 1 to 10;
*
* @property boolean results_visible database column:
* show rating results to users after feedback submission
* @property boolean commentable database column: users may comment ratings
*
*/
class FeedbackElement extends SimpleORMap
{
public static function configure($config = [])
{
$config['db_table'] = 'feedback';
$config['has_many']['entries'] = [
'class_name' => 'FeedbackEntry',
'assoc_foreign_key' => 'feedback_id',
'order_by' => 'ORDER BY mkdate DESC',
'on_delete' => 'delete'
];
$config['belongs_to']['course'] = [
'class_name' => Course::class,
'foreign_key' => 'course_id',
];
$config['belongs_to']['user'] = [
'class_name' => User::class,
'foreign_key' => 'user_id'
];
parent::configure($config);
}
/**
*
* @param string $user_id optional; use this ID instead of $GLOBALS['user']->id
*
* @return bool
*/
public function isFeedbackable(string $user_id = null): bool
{
$user_id = $user_id ?? $GLOBALS['user']->id;
$feedbackable = false;
if (Feedback::hasRangeAccess($this->range_id, $this->range_type, $user_id) && !$this->isOwner($user_id)) {
$already_feedbacked = $this->getOwnEntry($user_id);
if ($already_feedbacked === null) {
$feedbackable = true;
}
}
return $feedbackable;
}
/**
*
* @param string $user_id optional; use this ID instead of $GLOBALS['user']->id
*
* @return bool
*/
public function isOwner(string $user_id = null): bool
{
$user_id = $user_id ?? $GLOBALS['user']->id;
$ownership = false;
if ($this->user_id == $user_id) {
$ownership = true;
}
return $ownership;
}
/**
*
* @param string $user_id optional; use this ID instead of $GLOBALS['user']->id
*
* @return FeedbackEntry|null
*/
public function getOwnEntry(string $user_id = null)
{
$user_id = $user_id ?? $GLOBALS['user']->id;
return FeedbackEntry::findOneBySQL("feedback_id = ? AND user_id = ?", [$this->id, $user_id]);
}
public function getRatings()
{
$ratings = $this->entries->pluck('rating');
return $ratings;
}
public function getCountOfRating($rating)
{
$ratings = $this->entries->filter(function ($entry) use ($rating) {
return $entry->rating == $rating;
})->toArray();
return count($ratings);
}
public function getPercentageOfRating($rating)
{
$ratings = $this->getCountOfRating($rating);
$total = count($this->entries);
$percentage = ($ratings * 100) / $total;
return round($percentage);
}
public function getPercentageOfMeanRating($total)
{
$rating = round($this->getMeanOfRating(), 2);
$percentage = ($rating * 100) / $total;
return $percentage;
}
public function getMeanOfRating()
{
$ratings = $this->getRatings();
$count = count($ratings);
$mean = $count > 0 ? array_sum($ratings) / $count : 0;
return number_format($mean, 2, _(','), ' ');
}
public function getMaxRating()
{
switch ($this->mode) {
case 1:
// 5 Stars Rating
return 5;
break;
case 2:
// 10 Stars Rating
return 10;
break;
default:
return 0;
}
}
public function getRange()
{
return $this->range_type::find($this->range_id);
}
}
|