Passed
Pull Request — master (#6229)
by
unknown
11:01 queued 02:20
created

StudentPublicationLink::get_studpub_table()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Framework\Container;
6
use Chamilo\CourseBundle\Entity\CStudentPublication;
7
8
/**
9
 * Gradebook link to student publication item.
10
 *
11
 * @author Bert Steppé
12
 */
13
class StudentPublicationLink extends AbstractLink
14
{
15
    private $studpub_table;
16
17
    public function __construct()
18
    {
19
        parent::__construct();
20
        $this->set_type(LINK_STUDENTPUBLICATION);
21
    }
22
23
    /**
24
     * @return string
25
     */
26
    public function get_type_name()
27
    {
28
        return get_lang('Assignments');
29
    }
30
31
    public function is_allowed_to_change_name()
32
    {
33
        return false;
34
    }
35
36
    /**
37
     * Generate an array of all exercises available.
38
     *
39
     * @return array 2-dimensional array - every element contains 2 subelements (id, name)
40
     */
41
    public function get_all_links()
42
    {
43
        if (empty($this->course_id)) {
44
            return [];
45
        }
46
47
        $sessionId = $this->get_session_id();
48
        $session = api_get_session_entity($sessionId);
49
        $repo = Container::getStudentPublicationRepository();
50
        $qb = $repo->findAllByCourse(api_get_course_entity($this->course_id), $session, null, 1, 'folder');
51
        $links = $qb->getQuery()->getResult();
52
        $cats = [];
53
        foreach ($links as $data) {
54
            $work_name = $data->getTitle();
55
            if (empty($work_name)) {
56
                $work_name = basename($data->getUrl());
57
            }
58
            $cats[] = [$data->getIid(), $work_name];
59
        }
60
61
        return $cats;
62
    }
63
64
    /**
65
     * Has anyone done this exercise yet ?
66
     */
67
    public function has_results()
68
    {
69
        $studentPublication = $this->getStudentPublication();
70
71
        if (empty($studentPublication)) {
72
            return '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return '' returns the type string which is incompatible with the return type mandated by AbstractLink::has_results() of boolean.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
73
        }
74
        $id = $studentPublication->getIid();
75
        $session = api_get_session_entity($this->get_session_id());
76
        $results = Container::getStudentPublicationRepository()
77
            ->getStudentAssignments($studentPublication, api_get_course_entity($this->course_id), $session);
78
79
80
        return 0 !== count($results);
81
    }
82
83
    /**
84
     * @param null $studentId
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $studentId is correct as it would always require null to be passed?
Loading history...
85
     *
86
     * @return array
87
     */
88
    public function calc_score($studentId = null, $type = null)
89
    {
90
        $studentId = (int) $studentId;
91
        $assignment = $this->getStudentPublication();
92
93
        if (empty($assignment)) {
94
            return [];
95
        }
96
        $session = api_get_session_entity($this->get_session_id());
97
        $course = api_get_course_entity($this->course_id);
98
99
        $qb = Container::getStudentPublicationRepository()
100
            ->getStudentAssignments($assignment, $course, $session, null, $studentId ? api_get_user_entity($studentId) : null);
101
102
        $order = api_get_setting('student_publication_to_take_in_gradebook');
103
104
        switch ($order) {
105
            case 'last':
106
                $qb->orderBy('resource.sentDate', 'DESC');
107
                break;
108
            case 'first':
109
            default:
110
                $qb->orderBy('resource.iid', 'ASC');
111
                break;
112
        }
113
114
        $scores = $qb->getQuery()->getResult();
115
116
        // for 1 student
117
        if (!empty($studentId)) {
118
            if (!count($scores)) {
119
                return [null, null];
120
            }
121
122
            $data = $scores[0];
123
124
            return [
125
                $data->getQualification(),
126
                $assignment->getQualification(),
127
                api_get_local_time($assignment->getDateOfQualification()),
128
                1,
129
            ];
130
        }
131
132
        // multiple students
133
        $students = [];
134
        $rescount = 0;
135
        $sum = 0;
136
        $bestResult = 0;
137
        $weight = 0;
138
        $sumResult = 0;
139
140
        foreach ($scores as $data) {
141
            if (!array_key_exists($data->getUserId(), $students)) {
142
                if (0 != $assignment->getQualification()) {
143
                    $students[$data->getUserId()] = $data->getQualification();
144
                    $rescount++;
145
                    $sum += $data->getQualification() / $assignment->getQualification();
146
                    $sumResult += $data->getQualification();
147
148
                    if ($data->getQualification() > $bestResult) {
149
                        $bestResult = $data->getQualification();
150
                    }
151
                    $weight = $assignment->getQualification();
152
                }
153
            }
154
        }
155
156
        if (0 == $rescount) {
157
            return [null, null];
158
        }
159
160
        switch ($type) {
161
            case 'best':
162
                return [$bestResult, $weight];
163
            case 'average':
164
                return [$sumResult / $rescount, $weight];
165
            case 'ranking':
166
                return AbstractLink::getCurrentUserRanking($studentId, $students);
167
            default:
168
                return [$sum, $rescount];
169
        }
170
    }
171
172
    public function needs_name_and_description()
173
    {
174
        return false;
175
    }
176
177
    public function get_name()
178
    {
179
        $studentPublication = $this->getStudentPublication();
180
        $title = $studentPublication->getTitle();
181
182
        return empty($title) ? get_lang('Untitled') : $title;
183
    }
184
185
    public function get_description()
186
    {
187
        $studentPublication = $this->getStudentPublication();
188
189
        return $studentPublication->getDescription();
190
    }
191
192
    public function get_link()
193
    {
194
        $studentPublication = $this->getStudentPublication();
195
        $sessionId = $this->get_session_id();
196
        $url = api_get_path(WEB_PATH).'main/work/work.php?'.
197
            api_get_cidreq_params($this->getCourseId(), $sessionId).
198
            '&id='.$studentPublication->getIid().'&gradebook=view';
199
200
        return $url;
201
    }
202
203
    public function needs_max()
204
    {
205
        return false;
206
    }
207
208
    public function needs_results()
209
    {
210
        return false;
211
    }
212
213
    public function is_valid_link()
214
    {
215
        $studentPublication = $this->getStudentPublication();
216
217
        return null !== $studentPublication;
218
    }
219
220
    public function get_icon_name()
221
    {
222
        return 'studentpublication';
223
    }
224
225
    public function save_linked_data()
226
    {
227
        $studentPublication = $this->getStudentPublication();
228
229
        if (empty($studentPublication)) {
230
            return '';
231
        }
232
233
        $weight = api_float_val($this->get_weight());
234
        $studentPublication->setWeight($weight);
235
236
        $repo = Container::getStudentPublicationRepository();
237
        $repo->update($studentPublication);
238
    }
239
240
    /**
241
     * @return string
242
     */
243
    public function delete_linked_data()
244
    {
245
        /*$data = $this->get_exercise_data();
246
        if (empty($data)) {
247
            return '';
248
        }*/
249
250
        /*if (!empty($id)) {
251
            //Cleans works
252
            $sql = 'UPDATE '.$this->get_studpub_table().'
253
                    SET weight = 0
254
                    WHERE c_id = '.$this->course_id.' AND id ='.$id;
255
            Database::query($sql);
256
        }*/
257
    }
258
259
    /**
260
     * Lazy load function to get the database table of the student publications.
261
     */
262
    private function get_studpub_table()
0 ignored issues
show
Unused Code introduced by
The method get_studpub_table() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
263
    {
264
        return $this->studpub_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
265
    }
266
267
    private function getStudentPublication(): ?CStudentPublication
268
    {
269
        $repo = Container::getStudentPublicationRepository();
270
        if (!empty($this->get_ref_id())) {
271
            return $repo->find($this->get_ref_id());
272
        }
273
274
        return null;
275
    }
276
}
277