Completed
Push — master ( 1fcdba...966b12 )
by Julito
12:06
created

LpOrderItem::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Framework\Container;
5
use Chamilo\CourseBundle\Entity\CDocument;
6
use ChamiloSession as Session;
7
8
/**
9
 * Responses to AJAX calls.
10
 */
11
require_once __DIR__.'/../global.inc.php';
12
api_protect_course_script(true);
13
14
$debug = false;
15
$action = isset($_REQUEST['a']) ? $_REQUEST['a'] : '';
16
17
$courseId = api_get_course_int_id();
18
$sessionId = api_get_session_id();
19
20
if ($debug) {
21
    error_log('----------lp.ajax-------------- action '.$action);
22
}
23
24
switch ($action) {
25
    case 'get_documents':
26
        $courseInfo = api_get_course_info();
27
        $folderId = isset($_GET['folder_id']) ? $_GET['folder_id'] : null;
28
        if (empty($folderId)) {
29
            exit;
30
        }
31
        $lpId = isset($_GET['lp_id']) ? $_GET['lp_id'] : false;
32
        $url = isset($_GET['url']) ? $_GET['url'] : '';
33
        $addMove = isset($_GET['add_move_button']) && 1 == $_GET['add_move_button'] ? true : false;
34
35
        echo DocumentManager::get_document_preview(
36
            $courseInfo,
37
            $lpId,
38
            null,
39
            api_get_session_id(),
40
            $addMove,
41
            null,
42
            $url,
43
            true,
44
            false,
45
            $folderId,
46
            false
47
        );
48
        break;
49
    case 'add_lp_item':
50
        if (api_is_allowed_to_edit(null, true)) {
51
            /** @var learnpath $learningPath */
52
            $learningPath = Session::read('oLP');
53
            if ($learningPath) {
54
                $learningPath->set_modified_on();
55
                $title = isset($_REQUEST['title']) ? $_REQUEST['title'] : '';
56
                $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : '';
57
                $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0;
58
59
                switch ($type) {
60
                    case TOOL_QUIZ:
61
                        $title = Exercise::format_title_variable($title);
62
                        break;
63
                    case TOOL_DOCUMENT:
64
                        $repo = Container::getDocumentRepository();
65
                        /** @var CDocument $document */
66
                        $document = $repo->getResourceFromResourceNode($id);
67
                        $id = $document->getIid();
68
                        $title = $document->getTitle();
69
                        break;
70
                }
71
72
                $parentId = isset($_REQUEST['parent_id']) ? $_REQUEST['parent_id'] : '';
73
                $previousId = isset($_REQUEST['previous_id']) ? $_REQUEST['previous_id'] : '';
74
75
                $itemId = $learningPath->add_item(
76
                    $parentId,
77
                    $previousId,
78
                    $type,
79
                    $id,
80
                    $title,
81
                    null
82
                );
83
84
                /** @var learnpath $learningPath */
85
                $learningPath = Session::read('oLP');
86
                if ($learningPath) {
87
                    echo $learningPath->returnLpItemList(null);
88
                }
89
            }
90
        }
91
        break;
92
    case 'update_lp_item_order':
93
        if (api_is_allowed_to_edit(null, true)) {
94
            // $new_order gets a value like "647|0^648|0^649|0^"
95
            $new_order = $_POST['new_order'];
96
            $sections = explode('^', $new_order);
97
            $sections = array_filter($sections);
98
            // We have to update parent_item_id, previous_item_id, next_item_id, display_order in the database
99
            $orderList = [];
100
101
            foreach ($sections as $items) {
102
                list($id, $parentId) = explode('|', $items);
103
104
                $orderList[$id] = $parentId;
105
            }
106
107
            learnpath::sortItemByOrderList($orderList);
108
109
            echo Display::return_message(get_lang('Saved'), 'confirm');
110
        }
111
        break;
112
    case 'record_audio':
113
        if (false == api_is_allowed_to_edit(null, true)) {
114
            exit;
115
        }
116
        /** @var Learnpath $lp */
117
        $lp = Session::read('oLP');
118
        $course_info = api_get_course_info();
119
120
        $lpPathInfo = $lp->generate_lp_folder($course_info);
121
122
        if (empty($lpPathInfo)) {
123
            exit;
124
        }
125
126
        foreach (['video', 'audio'] as $type) {
127
            if (isset($_FILES["${type}-blob"])) {
128
                $fileName = $_POST["${type}-filename"];
129
                $file = $_FILES["${type}-blob"];
130
                $title = $_POST['audio-title'];
131
                $fileInfo = pathinfo($fileName);
132
133
                $file['name'] = $title.'.'.$fileInfo['extension'];
134
                $file['file'] = $file;
135
136
                $result = DocumentManager::upload_document(
137
                    $file,
138
                    '/audio',
139
                    $file['name'],
140
                    null,
141
                    0,
142
                    'overwrite',
143
                    false,
144
                    false
145
                );
146
147
                if (!empty($result) && is_array($result)) {
148
                    $newDocId = $result['id'];
149
                    $courseId = $result['c_id'];
150
151
                    $lp->set_modified_on();
152
153
                    $lpItem = new learnpathItem($_REQUEST['lp_item_id']);
154
                    $lpItem->add_audio_from_documents($newDocId);
155
                    $data = DocumentManager::get_document_data_by_id($newDocId, $course_info['code']);
156
                    echo $data['document_url'];
157
                    exit;
158
                }
159
            }
160
        }
161
162
        break;
163
    case 'get_forum_thread':
164
        $lpId = isset($_GET['lp']) ? intval($_GET['lp']) : 0;
165
        $lpItemId = isset($_GET['lp_item']) ? intval($_GET['lp_item']) : 0;
166
        $sessionId = api_get_session_id();
167
168
        if (empty($lpId) || empty($lpItemId)) {
169
            echo json_encode([
170
                'error' => true,
171
            ]);
172
173
            break;
174
        }
175
176
        $learningPath = learnpath::getLpFromSession(
177
            api_get_course_id(),
178
            $lpId,
179
            api_get_user_id()
180
        );
181
        $lpItem = $learningPath->getItem($lpItemId);
182
183
        if (empty($lpItem)) {
184
            echo json_encode([
185
                'error' => true,
186
            ]);
187
            break;
188
        }
189
190
        $lpHasForum = $learningPath->lpHasForum();
191
192
        if (!$lpHasForum) {
193
            echo json_encode([
194
                'error' => true,
195
            ]);
196
            break;
197
        }
198
199
        $forum = $learningPath->getForum($sessionId);
200
201
        if (empty($forum)) {
202
            require_once '../../forum/forumfunction.inc.php';
203
            $forumCategory = getForumCategoryByTitle(
204
                get_lang('Learning paths'),
205
                $courseId,
206
                $sessionId
207
            );
208
209
            if (empty($forumCategory)) {
210
                $forumCategoryId = store_forumcategory(
211
                    [
212
                        'lp_id' => 0,
213
                        'forum_category_title' => get_lang('Learning paths'),
214
                        'forum_category_comment' => null,
215
                    ],
216
                    [],
217
                    false
218
                );
219
            } else {
220
                $forumCategoryId = $forumCategory['cat_id'];
221
            }
222
223
            $forumId = $learningPath->createForum($forumCategoryId);
224
        } else {
225
            $forumId = $forum['forum_id'];
226
        }
227
228
        $lpItemHasThread = $lpItem->lpItemHasThread($courseId);
229
230
        if (!$lpItemHasThread) {
231
            echo json_encode([
232
                'error' => true,
233
            ]);
234
            break;
235
        }
236
237
        $forumThread = $lpItem->getForumThread($courseId, $sessionId);
238
        if (empty($forumThread)) {
239
            $lpItem->createForumThread($forumId);
240
            $forumThread = $lpItem->getForumThread($courseId, $sessionId);
241
        }
242
243
        $forumThreadId = $forumThread['thread_id'];
244
245
        echo json_encode([
246
            'error' => false,
247
            'forumId' => intval($forum['forum_id']),
248
            'threadId' => intval($forumThreadId),
249
        ]);
250
        break;
251
    case 'update_gamification':
252
        $lp = Session::read('oLP');
253
254
        $jsonGamification = [
255
            'stars' => 0,
256
            'score' => 0,
257
        ];
258
259
        if ($lp) {
260
            $score = $lp->getCalculateScore($sessionId);
261
            $jsonGamification['stars'] = $lp->getCalculateStars($sessionId);
262
            $jsonGamification['score'] = sprintf(get_lang('%s points'), $score);
263
        }
264
265
        echo json_encode($jsonGamification);
266
        break;
267
    case 'check_item_position':
268
        $lp = Session::read('oLP');
269
        $lpItemId = isset($_GET['lp_item']) ? intval($_GET['lp_item']) : 0;
270
        if ($lp) {
271
            $position = $lp->isFirstOrLastItem($lpItemId);
272
            echo json_encode($position);
273
        }
274
        break;
275
    case 'get_parent_names':
276
        $newItemId = isset($_GET['new_item']) ? intval($_GET['new_item']) : 0;
277
278
        if (!$newItemId) {
279
            break;
280
        }
281
282
        /** @var \learnpath $lp */
283
        $lp = Session::read('oLP');
284
        $parentNames = $lp->getCurrentItemParentNames($newItemId);
285
        $response = '';
286
        foreach ($parentNames as $parentName) {
287
            $response .= '<p class="h5 hidden-xs hidden-md">'.$parentName.'</p>';
288
        }
289
290
        echo $response;
291
        break;
292
    case 'get_item_prerequisites':
293
        /** @var learnpath $lp */
294
        $lp = Session::read('oLP');
295
        $itemId = isset($_GET['item_id']) ? (int) $_GET['item_id'] : 0;
296
        if (empty($lp) || empty($itemId)) {
297
            exit;
298
        }
299
        $result = $lp->prerequisites_match($itemId);
300
        if ($result) {
301
            echo '1';
302
        } else {
303
            if (!empty($lp->error)) {
304
                echo $lp->error;
305
            } else {
306
                echo get_lang('This learning object cannot display because the course prerequisites are not completed. This happens when a course imposes that you follow it step by step or get a minimum score in tests before you reach the next steps.');
307
            }
308
        }
309
        $lp->error = '';
310
        exit;
311
312
        break;
313
    default:
314
        echo '';
315
}
316