aboutsummaryrefslogtreecommitdiff
path: root/lib/models/FeedbackElement.php
blob: 88f3ba76792e37112998b8445012e03305602fee (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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php

/**
 *
 * @author Nils Gehrke <nils.gehrke@uni-goettingen.de>
 * @author Ron Lucke <lucke@elan-ev.de>
 *
 * The column "range_type" represents the name of a class that implements
 * FeedbackRange.
 *
 * @property int $id database column
 * @property string $user_id database column
 * @property string $range_id database column
 * @property string $range_type database column
 * @property string $course_id database column
 * @property string $question database column
 * @property string $description database column
 * @property int $mode database column
 * @property int $results_visible database column
 * @property int $commentable database column
 * @property int $anonymous_entries database column
 * @property int $mkdate database column
 * @property int $chdate database column
 * @property SimpleORMapCollection<FeedbackEntry> $entries has_many FeedbackEntry
 * @property Course $course belongs_to Course
 * @property User $user belongs_to User
 */

class FeedbackElement extends SimpleORMap
{
    public const MODE_NO_RATING = 0;
    public const MODE_5STAR_RATING = 1;
    public const MODE_10STAR_RATING = 2;

    public static function configure($config = [])
    {
        $config['db_table'] = 'feedback';

        $config['has_many']['entries'] = [
            'class_name'        => FeedbackEntry::class,
            '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 self::MODE_5STAR_RATING:
                return 5;
                break;
            case self::MODE_10STAR_RATING:
                return 10;
                break;
            default:
                return 0;
        }
    }

    public function getAverageRating(): float
    {
        $ratings = $this->getRatings();

        if (empty($ratings)) {
            return 0;
        }

        return array_sum($ratings) / count($ratings);
    }

    public function hasEntries(): bool
    {
        return count($this->getRatings()) > 0;
    }

    public function getRange()
    {
        return $this->range_type::find($this->range_id);
    }
}