Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

main/inc/ajax/student_follow_page.ajax.php (2 issues)

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CourseBundle\Entity\CLp;
6
use Chamilo\CourseBundle\Entity\CLpView;
7
use Symfony\Component\HttpFoundation\Request as HttpRequest;
0 ignored issues
show
This use statement conflicts with another class in this namespace, HttpRequest. 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...
8
9
require_once __DIR__.'/../global.inc.php';
10
11
$httpRequest = HttpRequest::createFromGlobals();
12
13
$isAllowedToEdit = api_is_allowed_to_edit();
14
15
switch ($httpRequest->get('a')) {
16
    case 'form_adquisition':
17
        displayForm(
18
            $httpRequest->query->getInt('lp_view')
19
        );
20
        break;
21
    case 'views_invisible':
22
        processViewsInvisible(
23
            $httpRequest->request->get('chkb_view') ?: [],
24
            $httpRequest->request->getBoolean('state')
25
        );
26
        break;
27
}
28
29
function displayForm(int $lpViewId)
30
{
31
    $em = Database::getManager();
32
33
    $lpView = $em->find(CLpView::class, $lpViewId);
34
35
    if (null === $lpView) {
36
        return;
37
    }
38
39
    $lp = $em->find(CLp::class, $lpView->getLpId());
40
41
    $extraField = new ExtraField('lp_view');
42
    $field = $extraField->get_handler_field_info_by_field_variable(StudentFollowPage::VARIABLE_ACQUISITION);
43
44
    $extraFieldValue = new ExtraFieldValue('lp_view');
45
    $value = $extraFieldValue->get_values_by_handler_and_field_variable(
46
        $lpViewId,
47
        StudentFollowPage::VARIABLE_ACQUISITION
48
    );
49
50
    $options = [];
51
52
    foreach ($field['options'] as $option) {
53
        $options[$option['option_value']] = ExtraFieldOption::translateDisplayName($option['display_text']);
54
    }
55
56
    $frmId = 'frm_lp_acquisition_'.$lpView->getLpId();
57
    $frmAction = api_get_self().'?'.http_build_query(['lp_view' => $lpViewId, 'a' => 'form_adquisition']);
58
59
    $form = new FormValidator($frmId, 'post', $frmAction);
60
    $form->addRadio(StudentFollowPage::VARIABLE_ACQUISITION, get_lang('Acquisition'), $options);
61
    $form->addHidden('lp_view', $lpViewId);
62
    $form->addButtonSave(get_lang('Save'));
63
64
    if ($form->validate()) {
65
        $values = $form->exportValues();
66
67
        $extraFieldValue = new ExtraFieldValue('lp_view');
68
        $extraFieldValue->save(
69
            [
70
                'variable' => StudentFollowPage::VARIABLE_ACQUISITION,
71
                'item_id' => $lpViewId,
72
                'comment' => json_encode(['user' => api_get_user_id(), 'datetime' => api_get_utc_datetime()]),
73
                'value' => $values[StudentFollowPage::VARIABLE_ACQUISITION],
74
            ]
75
        );
76
77
        echo StudentFollowPage::getLpAcquisition(
78
            [
79
                'iid' => $lp->getIid(),
80
                'lp_name' => $lp->getName(),
81
            ],
82
            $lpView->getUserId(),
83
            $lpView->getCId(),
84
            $lpView->getSessionId(),
85
            true
86
        );
87
        exit;
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
88
    }
89
90
    if (!empty($value)) {
91
        $form->setDefaults([StudentFollowPage::VARIABLE_ACQUISITION => $value['value']]);
92
    }
93
94
    echo $form->returnForm()
95
        ."<script>$(function () {
96
            $('#$frmId').on('submit', function (e) {
97
                e.preventDefault();
98
99
                var self = $(this);
100
101
                self.find(':submit').prop('disabled', true);
102
103
                $.post(this.action, self.serialize()).done(function (response) {
104
                    $('#acquisition-$lpViewId').html(response);
105
106
                    $('#global-modal').modal('hide');
107
108
                    self.find(':submit').prop('disabled', false);
109
                });
110
            })
111
        })</script>";
112
}
113
114
function processViewsInvisible(array $lpViews, bool $state)
115
{
116
    foreach ($lpViews as $lpViewData) {
117
        $parts = explode('_', $lpViewData);
118
119
        [$lpId, $userId, $courseId, $sessionId] = array_map('intval', $parts);
120
121
        $lpView = learnpath::findLastView($lpId, $userId, $courseId, $sessionId);
122
123
        if (empty($lpView)) {
124
            $tblLpView = Database::get_course_table(TABLE_LP_VIEW);
125
126
            $lpViewId = Database::insert(
127
                $tblLpView,
128
                [
129
                    'c_id' => $courseId,
130
                    'lp_id' => $lpId,
131
                    'user_id' => $userId,
132
                    'view_count' => 1,
133
                    'session_id' => $sessionId,
134
                ]
135
            );
136
            Database::update($tblLpView, ['id' => $lpViewId], ['iid = ?' => $lpViewId]);
137
        } else {
138
            $lpViewId = $lpView['iid'];
139
        }
140
141
        $extraFieldValue = new ExtraFieldValue('lp_view');
142
        $extraFieldValue->save(
143
            [
144
                'variable' => StudentFollowPage::VARIABLE_INVISIBLE,
145
                'item_id' => $lpViewId,
146
                'comment' => json_encode(['user' => api_get_user_id(), 'datetime' => api_get_utc_datetime()]),
147
                'value' => $state,
148
            ]
149
        );
150
    }
151
}
152