Issues (2128)

plugin/grading_electronic/generate.php (5 issues)

Labels
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Entity\Course;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Course. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
use Chamilo\CoreBundle\Entity\CourseRelUser;
6
use Chamilo\CoreBundle\Entity\Session;
7
use Chamilo\CoreBundle\Entity\SessionRelUser;
8
use Chamilo\UserBundle\Entity\User;
9
use Doctrine\Common\Collections\Criteria;
10
11
require_once '../../main/inc/global.inc.php';
12
13
$allowed = api_is_teacher() || api_is_platform_admin() || api_is_course_tutor();
14
15
$gradingElectronic = GradingElectronicPlugin::create();
16
17
try {
18
    if (!$allowed) {
19
        throw new Exception(get_lang('NotAllowed'));
20
    }
21
22
    $toolIsEnabled = $gradingElectronic->get('tool_enable') === 'true';
23
24
    if (!$toolIsEnabled) {
25
        throw new Exception($gradingElectronic->get_lang('PluginDisabled'));
26
    }
27
28
    $form = $gradingElectronic->getForm();
29
30
    if (!$form->validate()) {
31
        throw new Exception(implode('<br>', $form->_errors));
32
    }
33
34
    $em = Database::getManager();
35
36
    /** @var Course $course */
37
    $course = $em->find('ChamiloCoreBundle:Course', api_get_course_int_id());
38
    /** @var Session $session */
39
    $session = $em->find('ChamiloCoreBundle:Session', api_get_session_id());
40
41
    $values = $form->exportValues();
42
43
    $cFieldValue = new ExtraFieldValue('course');
44
    $uFieldValue = new ExtraFieldValue('user');
45
46
    $cFieldValue->save([
47
        'variable' => GradingElectronicPlugin::EXTRAFIELD_COURSE_ID,
48
        'item_id' => $course->getId(),
0 ignored issues
show
The method getId() does not exist on Chamilo\CourseBundle\Component\CourseCopy\Course. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        'item_id' => $course->/** @scrutinizer ignore-call */ getId(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
        'value' => $values['course'],
50
    ]);
51
52
    $item = $cFieldValue->get_item_id_from_field_variable_and_field_value(
53
        GradingElectronicPlugin::EXTRAFIELD_COURSE_ID,
54
        $values['course']
55
    );
56
57
    $fieldProvider = $cFieldValue->get_values_by_handler_and_field_variable(
58
        $course->getId(),
59
        GradingElectronicPlugin::EXTRAFIELD_COURSE_PROVIDER_ID
60
    );
61
    $fieldHours = $cFieldValue->get_values_by_handler_and_field_variable(
62
        $course->getId(),
63
        GradingElectronicPlugin::EXTRAFIELD_COURSE_HOURS
64
    );
65
66
    $students = [];
67
68
    if ($session) {
0 ignored issues
show
$session is of type Session, thus it always evaluated to true.
Loading history...
69
        $criteria = Criteria::create()->where(
70
            Criteria::expr()->eq('relationType', Session::STUDENT)
71
        );
72
73
        $subscriptions = $session->getUsers()->matching($criteria);
74
75
        /** @var SessionRelUser $subscription */
76
        foreach ($subscriptions as $subscription) {
77
            $students[] = $subscription->getUser();
78
        }
79
    } else {
80
        $subscriptions = $course->getStudents();
81
82
        /** @var CourseRelUser $subscription */
83
        foreach ($subscriptions as $subscription) {
84
            $students[] = $subscription->getUser();
85
        }
86
    }
87
88
    $cats = Category::load(
89
        null,
90
        null,
91
        $course->getCode(),
0 ignored issues
show
The method getCode() does not exist on Chamilo\CourseBundle\Component\CourseCopy\Course. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        $course->/** @scrutinizer ignore-call */ 
92
                 getCode(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
        null,
93
        null,
94
        $session ? $session->getId() : 0,
0 ignored issues
show
$session is of type Session, thus it always evaluated to true.
Loading history...
95
        'ORDER By id'
96
    );
97
98
    /** @var \Category $gradebook */
99
    $gradebook = $cats[0];
100
    /** @var \ExerciseLink $exerciseLink */
101
    /** commented until we get clear understanding of how to use the dates refs BT#12404.
102
        $exerciseInfo = ExerciseLib::get_exercise_by_id($exerciseId, $course->getId());
103
     */
104
    $dateStart = new DateTime($values['range_start'].' 00:00:00', new DateTimeZone('UTC'));
105
    $dateEnd = new DateTime($values['range_end'].' 23:59:59', new DateTimeZone('UTC'));
106
107
    $fileData = [];
108
    $fileData[] = sprintf(
109
        '1 %s %s%s',
110
        $fieldProvider ? $fieldProvider['value'] : null,
111
        $values['course'],
112
        $dateStart->format('m/d/Y')
113
    );
114
115
    /** @var User $student */
116
    foreach ($students as $student) {
117
        $userFinishedCourse = Category::userFinishedCourse(
118
            $student->getId(),
119
            $gradebook,
120
            true
121
        );
122
        if (!$userFinishedCourse) {
123
            continue;
124
        }
125
        /** commented until we get clear understanding of how to use the dates refs BT#12404.
126
                }
127
         */
128
        $fieldStudent = $uFieldValue->get_values_by_handler_and_field_variable(
129
            $student->getId(),
130
            GradingElectronicPlugin::EXTRAFIELD_STUDENT_ID
131
        );
132
        $scoretotal = $gradebook->calc_score($student->getId());
133
        $scoredisplay = ScoreDisplay::instance();
134
        $score = $scoredisplay->display_score(
135
            $scoretotal,
136
            SCORE_SIMPLE
137
        );
138
139
        /** old method to get the score.
140
                );
141
         */
142
        $fileData[] = sprintf(
143
            "2 %sPASS%s %s %s",
144
            $fieldStudent ? $fieldStudent['value'] : null,
145
            $fieldHours ? $fieldHours['value'] : null,
146
            $score,
147
            $dateEnd->format('m/d/Y')
148
        );
149
150
        if (!$gradebook->getGenerateCertificates()) {
151
            continue;
152
        }
153
154
        Category::generateUserCertificate(
155
            $gradebook->get_id(),
156
            $student->getId(),
157
            true
158
        );
159
    }
160
161
    $fileName = implode('_', [
162
        $gradingElectronic->get_title(),
163
        $values['course'],
164
        $values['range_start'],
165
        $values['range_end'],
166
    ]);
167
    $fileName = api_replace_dangerous_char($fileName).'.txt';
168
    $fileData[] = null;
169
170
    file_put_contents(
171
        api_get_path(SYS_ARCHIVE_PATH).$fileName,
172
        implode("\r\n", $fileData)
173
    );
174
175
    echo Display::toolbarButton(
176
        get_lang('Download'),
177
        api_get_path(WEB_ARCHIVE_PATH).$fileName,
178
        'download',
179
        'success',
180
        ['target' => '_blank', 'download' => $fileName]
181
    );
182
} catch (Exception $e) {
183
    echo Display::return_message($e->getMessage(), 'error');
184
}
185