Completed
Pull Request — 1.10.x (#1342)
by José
42:42
created

work.lib.php ➔ checkExistingWorkFileName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 7
c 1
b 0
f 1
nc 1
nop 2
dl 9
loc 9
rs 9.6666
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use ChamiloSession as Session;
5
6
/**
7
 *  @package chamilo.work
8
 *  @author Thomas, Hugues, Christophe - original version
9
 *  @author Patrick Cool <[email protected]>, Ghent University -
10
 * ability for course admins to specify wether uploaded documents are visible or invisible by default.
11
 *  @author Roan Embrechts, code refactoring and virtual course support
12
 *  @author Frederic Vauthier, directories management
13
 *  @author Julio Montoya <[email protected]> BeezNest 2011 LOTS of bug fixes
14
 *  @todo   this lib should be convert in a static class and moved to main/inc/lib
15
 */
16
17
/**
18
 * Displays action links (for admins, authorized groups members and authorized students)
19
 * @param   string  Current dir
20
 * @param   integer Whether to show tool options
21
 * @param   integer Whether to show upload form option
22
 * @return  void
23
 */
24
function display_action_links($id, $cur_dir_path, $action)
25
{
26
    global $gradebook;
27
28
    $id = $my_back_id = intval($id);
29
    if ($action == 'list') {
30
        $my_back_id = 0;
31
    }
32
33
    $display_output = '';
34
    $origin = isset($_GET['origin']) ? Security::remove_XSS($_GET['origin']) : '';
35
36
    if (!empty($id)) {
37
        $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&origin='.$origin.'&gradebook='.$gradebook.'&id='.$my_back_id.'">'.
38
            Display::return_icon('back.png', get_lang('BackToWorksList'),'',ICON_SIZE_MEDIUM).'</a>';
39
    }
40
41
    if (api_is_allowed_to_edit(null, true) && $origin != 'learnpath') {
42
        // Create dir
43
        if (empty($id)) {
44
            $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=create_dir&origin='.$origin.'&gradebook='.$gradebook.'">';
45
            $display_output .= Display::return_icon('new_work.png', get_lang('CreateAssignment'),'',ICON_SIZE_MEDIUM).'</a>';
46
        }
47
        if (empty($id)) {
48
            // Options
49
            $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=settings&origin='.$origin.'&gradebook='.$gradebook.'">';
50
            $display_output .= Display::return_icon('settings.png', get_lang('EditToolOptions'),'',ICON_SIZE_MEDIUM).'</a>';
51
        }
52
        $display_output .= '<a id="open-view-list" href="#">' . Display::return_icon('listwork.png', get_lang('ViewStudents'),'',ICON_SIZE_MEDIUM) . '</a>';
53
54
    }
55
56
    if (api_is_allowed_to_edit(null, true) && $origin != 'learnpath' && api_is_allowed_to_session_edit(false, true)) {
57
        // Delete all files
58
        if (api_get_setting('permanently_remove_deleted_files') == 'true'){
59
            $message = get_lang('ConfirmYourChoiceDeleteAllfiles');
60
        } else {
61
            $message = get_lang('ConfirmYourChoice');
62
        }
63
    }
64
65
    if ($display_output != '') {
66
        echo '<div class="actions">';
67
        echo $display_output;
68
        echo '</div>';
69
    }
70
}
71
72
/**
73
 * Returns a form displaying all options for this tool.
74
 * These are
75
 * - make all files visible / invisible
76
 * - set the default visibility of uploaded files
77
 * @param $defaults
78
 * @return string The HTML form
79
 */
80
function settingsForm($defaults)
81
{
82
    $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
83
84
    if (!$is_allowed_to_edit) {
85
        return;
86
    }
87
88
    $url = api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq().'&action=settings';
89
    $form = new FormValidator('edit_settings', 'post', $url);
90
    $form->addElement('hidden', 'changeProperties', 1);
91
    $form->addElement('header', get_lang('EditToolOptions'));
92
93
    $group = array(
94
        $form->createElement('radio', 'show_score', null, get_lang('NewVisible'), 0),
95
        $form->createElement('radio', 'show_score', null, get_lang('NewUnvisible'), 1)
96
    );
97
    $form->addGroup($group, '', get_lang('DefaultUpload'));
98
99
    $group = array(
100
        $form->createElement('radio', 'student_delete_own_publication', null, get_lang('Yes'), 1),
101
        $form->createElement('radio', 'student_delete_own_publication', null, get_lang('No'), 0)
102
    );
103
    $form->addGroup($group, '', get_lang('StudentAllowedToDeleteOwnPublication'));
104
    $form->addButtonSave(get_lang('Save'));
105
    $form->setDefaults($defaults);
106
107
    return $form->returnForm();
108
}
109
110
/**
111
 * converts 1-9 to 01-09
112
 */
113
function two_digits($number)
114
{
115
    $number = (int)$number;
116
    return ($number < 10) ? '0'.$number : $number;
117
}
118
119
/**
120
 * Converts 2008-10-06 12:45:00 to -> array('prefix' => array(year'=>2008, 'month'=>10, etc...)
121
 * @param string
122
 * @param string
123
 * @param array
124
 */
125
function convert_date_to_array($date, $group)
126
{
127
    $parts = explode(' ', $date);
128
    $date_parts = explode('-', $parts[0]);
129
    $date_parts_tmp = array();
130
    foreach ($date_parts as $item) {
131
        $date_parts_tmp[] = intval($item);
132
    }
133
134
    $time_parts = explode(':', $parts[1]);
135
    $time_parts_tmp = array();
136
    foreach ($time_parts as $item) {
137
        $time_parts_tmp[] = intval($item);
138
    }
139
    list($data[$group]['year'], $data[$group]['month'], $data[$group]['day']) = $date_parts_tmp;
140
    list($data[$group]['hour'], $data[$group]['minute']) = $time_parts_tmp;
141
    return $data;
0 ignored issues
show
Bug introduced by
The variable $data does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
142
}
143
144
/**
145
 * get date from a group of date
146
 */
147 View Code Duplication
function get_date_from_group($group)
148
{
149
    return
150
        $_POST[$group]['year'].'-'.
151
        two_digits($_POST[$group]['month']).'-'.
152
        two_digits($_POST[$group]['day']).' '.
153
        two_digits($_POST[$group]['hour']).':'.
154
        two_digits($_POST[$group]['minute']).':00';
155
}
156
157
/**
158
 * Create a group of select from a date
159
 * @param FormValidator $form
160
 * @param string $prefix
161
 * @return array
162
 */
163
function create_group_date_select($form, $prefix = '')
164
{
165
    $minute = range(10, 59);
166
    $d_year = date('Y');
167
    array_unshift($minute, '00', '01', '02', '03', '04', '05', '06', '07', '08', '09');
168
169
    $group_name = array(
170
        $form->createElement('select', $prefix.'day', '', array_combine(range(1, 31), range(1, 31))),
171
        $form->createElement('select', $prefix.'month', '', array_combine(range(1, 12), api_get_months_long())),
172
        $form->createElement('select', $prefix.'year', '', array($d_year => $d_year, $d_year + 1 => $d_year + 1)),
173
        $form->createElement('select', $prefix.'hour', '', array_combine(range(0, 23), range(0, 23))),
174
        $form->createElement('select', $prefix.'minute', '', $minute)
175
    );
176
    return $group_name;
177
}
178
179
/**
180
 * @param string $path
181
 * @param int $courseId
182
 *
183
 * @return array
184
 */
185 View Code Duplication
function get_work_data_by_path($path, $courseId = null)
186
{
187
    $path = Database::escape_string($path);
188
    if (empty($courseId)) {
189
        $courseId = api_get_course_int_id();
190
    } else {
191
        $courseId = intval($courseId);
192
    }
193
194
    $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
195
    $sql = "SELECT *  FROM  ".$work_table."
196
            WHERE url = '$path' AND c_id = $courseId ";
197
    $result = Database::query($sql);
198
    $return = array();
199
    if (Database::num_rows($result)) {
200
        $return = Database::fetch_array($result, 'ASSOC');
201
    }
202
203
    return $return;
204
}
205
206
/**
207
 * @param int $id
208
 * @param int $courseId
209
 * @param int $sessionId
210
 * @return array
211
 */
212
function get_work_data_by_id($id, $courseId = null, $sessionId = null)
213
{
214
    $id = intval($id);
215
216
    if (!empty($courseId)) {
217
        $courseId = intval($courseId);
218
    } else {
219
        $courseId = api_get_course_int_id();
220
    }
221
222
    $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
223
224
    $sessionCondition = null;
225
    if (!empty($sessionId)) {
226
        $sessionCondition = api_get_session_condition($sessionId, true);
227
    }
228
229
    $sql = "SELECT * FROM $table
230
            WHERE
231
                id = $id AND c_id = $courseId
232
                $sessionCondition";
233
    $result = Database::query($sql);
234
    $work = array();
235
    if (Database::num_rows($result)) {
236
        $work = Database::fetch_array($result, 'ASSOC');
237
        if (empty($work['title'])) {
238
            $work['title'] = basename($work['url']);
239
        }
240
        $work['download_url'] = api_get_path(WEB_CODE_PATH).'work/download.php?id='.$work['id'].'&'.api_get_cidreq();
241
        $work['view_url'] = api_get_path(WEB_CODE_PATH).'work/view.php?id='.$work['id'].'&'.api_get_cidreq();
242
        $work['show_url'] = api_get_path(WEB_CODE_PATH).'work/show_file.php?id='.$work['id'].'&'.api_get_cidreq();
243
        $work['show_content'] = '';
244
        if ($work['contains_file']) {
245
            $fileInfo = pathinfo($work['title']);
246
            if (is_array($fileInfo) &&
247
                !empty($fileInfo['extension']) &&
248
                in_array($fileInfo['extension'], array('jpg', 'png', 'gif'))
249
            ) {
250
                $work['show_content'] = '<img src="'.$work['show_url'].'"/>';
251
            }
252
        }
253
    }
254
255
    return $work;
256
}
257
258
/**
259
 * @param int $user_id
260
 * @param int $work_id
261
 *
262
 * @return int
263
 */
264
function get_work_count_by_student($user_id, $work_id)
265
{
266
    $user_id = intval($user_id);
267
    $work_id = intval($work_id);
268
    $course_id = api_get_course_int_id();
269
    $session_id = api_get_session_id();
270
271
    $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
272
    $sql = "SELECT COUNT(*) as count
273
            FROM  $table
274
            WHERE
275
                c_id = $course_id AND
276
                parent_id = $work_id AND
277
                user_id = $user_id AND
278
                active IN (0, 1) AND
279
                session_id = $session_id ";
280
    $result = Database::query($sql);
281
    $return = 0;
282
    if (Database::num_rows($result)) {
283
        $return = Database::fetch_row($result, 'ASSOC');
284
        $return = intval($return[0]);
285
    }
286
287
    return $return;
288
}
289
290
/**
291
 * @param int $id
292
 * @param int $courseId
293
 *
294
 * @return array
295
 */
296 View Code Duplication
function get_work_assignment_by_id($id, $courseId = null)
297
{
298
    if (empty($courseId)) {
299
        $courseId = api_get_course_int_id();
300
    } else {
301
        $courseId = intval($courseId);
302
    }
303
    $id = intval($id);
304
305
    $table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
306
    $sql = "SELECT * FROM $table
307
            WHERE c_id = $courseId AND publication_id = $id";
308
    $result = Database::query($sql);
309
    $return = array();
310
    if (Database::num_rows($result)) {
311
        $return = Database::fetch_array($result, 'ASSOC');
312
    }
313
314
    return $return;
315
}
316
317
/**
318
 * @param int $id
319
 * @param array $my_folder_data
320
 * @param string $add_in_where_query
321
 *
322
 * @return array
323
 */
324
function getWorkList($id, $my_folder_data, $add_in_where_query = null)
325
{
326
    $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
327
328
    $course_id = api_get_course_int_id();
329
    $session_id = api_get_session_id();
330
    $condition_session = api_get_session_condition($session_id);
331
    $group_id = api_get_group_id();
332
    $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
333
334
    $linkInfo = GradebookUtils::is_resource_in_course_gradebook(
335
        api_get_course_id(),
336
        3,
337
        $id,
338
        api_get_session_id()
339
    );
340
341
    if ($linkInfo) {
342
        $workInGradeBookLinkId = $linkInfo['id'];
343
        if ($workInGradeBookLinkId) {
344
            if ($is_allowed_to_edit) {
345
                if (intval($my_folder_data['qualification']) == 0) {
346
                    Display::display_warning_message(
347
                        get_lang('MaxWeightNeedToBeProvided')
348
                    );
349
                }
350
            }
351
        }
352
    }
353
354
    $contains_file_query = '';
355
356
    // Get list from database
357
    if ($is_allowed_to_edit) {
358
        $active_condition = ' active IN (0, 1)';
359
        $sql = "SELECT * FROM $work_table
360
                WHERE
361
                    c_id = $course_id
362
                    $add_in_where_query
363
                    $condition_session AND
364
                    $active_condition AND
365
                    (parent_id = 0)
366
                    $contains_file_query AND
367
                    post_group_id = '".$group_id."'
368
                ORDER BY sent_date DESC";
369
    } else {
370 View Code Duplication
        if (!empty($group_id)) {
371
            // set to select only messages posted by the user's group
372
            $group_query = " WHERE c_id = $course_id AND post_group_id = '".$group_id."' ";
373
            $subdirs_query = "AND parent_id = 0";
374
        } else {
375
            $group_query = " WHERE c_id = $course_id AND  post_group_id = '0' ";
376
            $subdirs_query = "AND parent_id = 0";
377
        }
378
        //@todo how we can active or not an assignment?
379
        $active_condition = ' AND active IN (1, 0)';
380
        $sql = "SELECT * FROM  $work_table
381
                $group_query
382
                $subdirs_query
383
                $add_in_where_query
384
                $active_condition
385
                $condition_session
386
                ORDER BY title";
387
    }
388
389
    $work_parents = array();
390
391
    $sql_result = Database::query($sql);
392
    if (Database::num_rows($sql_result)) {
393
        while ($work = Database::fetch_object($sql_result)) {
394
            if ($work->parent_id == 0) {
395
                $work_parents[] = $work;
396
            }
397
        }
398
    }
399
400
    return $work_parents;
401
}
402
403
/**
404
 * @param int $userId
405
 * @return array
406
 */
407
function getWorkPerUser($userId)
408
{
409
    $works = getWorkList(null, null, null);
410
    $result = array();
411
    if (!empty($works)) {
412
        foreach ($works as $workData) {
413
            $workId = $workData->id;
414
            $result[$workId]['work'] = $workData;
415
            $result[$workId]['work']->user_results = get_work_user_list(
416
                0,
417
                100,
418
                null,
419
                null,
420
                $workId,
421
                null,
422
                $userId
423
            );
424
        }
425
    }
426
    return $result;
427
}
428
429
/**
430
 * @param int $workId
431
 * @param int $groupId
432
 * @param int $course_id
433
 * @param int $sessionId
434
 * @return mixed
435
 */
436
function getUniqueStudentAttemptsTotal($workId, $groupId, $course_id, $sessionId)
437
{
438
    $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
439
    $user_table = Database::get_main_table(TABLE_MAIN_USER);
440
    $course_id = intval($course_id);
441
    $workId = intval($workId);
442
    $sessionId = intval($sessionId);
443
    $groupId = intval($groupId);
444
445
    $sql = "SELECT count(DISTINCT u.user_id)
446
            FROM $work_table w
447
            INNER JOIN $user_table u
448
                ON w.user_id = u.user_id
449
            WHERE
450
                w.c_id = $course_id AND
451
                w.session_id = $sessionId AND
452
                w.parent_id = ".$workId." AND
453
                w.post_group_id = ".$groupId." AND
454
                w.active IN (0, 1)
455
            ";
456
457
    $res_document = Database::query($sql);
458
    $rowCount = Database::fetch_row($res_document);
459
460
    return $rowCount[0];
461
}
462
463
/**
464
 * @param mixed $workId
465
 * @param int $groupId
466
 * @param int $course_id
467
 * @param int $sessionId
468
 * @param int $userId user id to filter
469
 * @param array $onlyUserList only parse this user list
470
 * @return mixed
471
 */
472
function getUniqueStudentAttempts(
473
    $workId,
474
    $groupId,
475
    $course_id,
476
    $sessionId,
477
    $userId = null,
478
    $onlyUserList = array()
479
) {
480
    $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
481
    $user_table = Database::get_main_table(TABLE_MAIN_USER);
482
483
    $course_id = intval($course_id);
484
    $workCondition = null;
485
    if (is_array($workId)) {
486
        $workId = array_map('intval', $workId);
487
        $workId = implode("','", $workId);
488
        $workCondition = " w.parent_id IN ('".$workId."') AND";
489
    } else {
490
        $workId = intval($workId);
491
        $workCondition = " w.parent_id = ".$workId." AND";
492
    }
493
494
    $sessionId = intval($sessionId);
495
    $groupId = intval($groupId);
496
497
    $studentCondition = null;
498
499 View Code Duplication
    if (!empty($onlyUserList)) {
500
        $onlyUserList = array_map('intval', $onlyUserList);
501
        $studentCondition = "AND u.user_id IN ('".implode("', '", $onlyUserList)."') ";
502
    } else {
503
        if (empty($userId)) {
504
            return 0;
505
        }
506
    }
507
508
    $sql = "SELECT count(*) FROM (
509
                SELECT count(*), w.parent_id
510
                FROM $work_table w
511
                INNER JOIN $user_table u
512
                    ON w.user_id = u.user_id
513
                WHERE
514
                    w.filetype = 'file' AND
515
                    w.c_id = $course_id AND
516
                    w.session_id = $sessionId AND
517
                    $workCondition
518
                    w.post_group_id = ".$groupId." AND
519
                    w.active IN (0, 1) $studentCondition
520
                ";
521
    if (!empty($userId)) {
522
        $userId = intval($userId);
523
        $sql .= " AND u.user_id = ".$userId;
524
    }
525
    $sql .= " GROUP BY u.user_id, w.parent_id) as t";
526
    $result = Database::query($sql);
527
    $row = Database::fetch_row($result);
528
529
    return $row[0];
530
}
531
532
/**
533
 * Shows the work list (student view)
534
 * @return string
535
 */
536
function showStudentWorkGrid()
537
{
538
    $courseInfo = api_get_course_info();
539
    $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_work_student&'.api_get_cidreq();
540
541
    $columns = array(
542
        get_lang('Type'),
543
        get_lang('Title'),
544
        get_lang('HandOutDateLimit'),
545
        get_lang('Feedback'),
546
        get_lang('LastUpload')
547
    );
548
549
    $columnModel = array(
550
        array('name'=>'type', 'index'=>'type', 'width'=>'30',   'align'=>'center', 'sortable' => 'false'),
551
        array('name'=>'title', 'index'=>'title', 'width'=>'250',   'align'=>'left'),
552
        array('name'=>'expires_on', 'index'=>'expires_on', 'width'=>'80',  'align'=>'center', 'sortable'=>'false'),
553
        array('name'=>'feedback', 'index'=>'feedback', 'width'=>'80',  'align'=>'center'),
554
        array('name'=>'last_upload', 'index'=>'feedback', 'width'=>'125',  'align'=>'center'),
555
    );
556
557 View Code Duplication
    if ($courseInfo['show_score'] == 0) {
558
        $columnModel[] = array(
559
            'name' => 'others',
560
            'index' => 'others',
561
            'width' => '80',
562
            'align' => 'left',
563
            'sortable' => 'false'
564
        );
565
        $columns[] = get_lang('Others');
566
    }
567
568
    $params = array(
569
        'autowidth' => 'true',
570
        'height' => 'auto'
571
    );
572
573
    $html = '<script>
574
    $(function() {
575
        '.Display::grid_js('workList', $url, $columns, $columnModel, $params, array(), null, true).'
576
    });
577
    </script>';
578
579
    $html .= Display::grid_html('workList');
580
    return $html;
581
}
582
583
/**
584
 * Shows the work list (teacher view)
585
 * @return string
586
 */
587
function showTeacherWorkGrid()
588
{
589
    $columnModel = array(
590
        array('name'=>'type', 'index'=>'type', 'width'=>'35', 'align'=>'center', 'sortable' => 'false'),
591
        array('name'=>'title', 'index'=>'title',  'width'=>'300',   'align'=>'left', 'wrap_cell' => "true"),
592
        array('name'=>'sent_date', 'index'=>'sent_date', 'width'=>'125',  'align'=>'center'),
593
        array('name'=>'expires_on', 'index'=>'expires_on', 'width'=>'125',  'align'=>'center'),
594
        array('name'=>'amount', 'index'=>'end_on', 'width'=>'110',  'align'=>'center'),
595
        array('name'=>'actions', 'index'=>'actions', 'width'=>'110', 'align'=>'left', 'sortable'=>'false')
596
    );
597
598
    $token = null;
599
600
    $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_work_teacher&'.api_get_cidreq();
601
    $deleteUrl = api_get_path(WEB_AJAX_PATH).'work.ajax.php?a=delete_work&'.api_get_cidreq();
602
603
    $columns = array(
604
        get_lang('Type'),
605
        get_lang('Title'),
606
        get_lang('SentDate'),
607
        get_lang('HandOutDateLimit'),
608
        get_lang('AmountSubmitted'),
609
        get_lang('Actions')
610
    );
611
612
    $params = array(
613
        'multiselect' => true,
614
        'autowidth' => 'true',
615
        'height' => 'auto'
616
    );
617
618
    $html = '<script>
619
    $(function() {
620
        '.Display::grid_js('workList', $url, $columns, $columnModel, $params, array(), null, true).'
621
        $("#workList").jqGrid(
622
            "navGrid",
623
            "#workList_pager",
624
            { edit: false, add: false, del: true },
625
            { height:280, reloadAfterSubmit:false }, // edit options
626
            { height:280, reloadAfterSubmit:false }, // add options
627
            { reloadAfterSubmit:false, url: "'.$deleteUrl.'" }, // del options
628
            { width:500 } // search options
629
        );
630
    });
631
    </script>';
632
    $html .= Display::grid_html('workList');
633
    return $html;
634
}
635
636
/**
637
 * Builds the form thats enables the user to
638
 * select a directory to browse/upload in
639
 * This function has been copied from the document/document.inc.php library
640
 *
641
 * @param array $folders
642
 * @param string $curdirpath
643
 * @param string $group_dir
644
 * @return string html form
645
 */
646
// TODO: This function is a candidate for removal, it is not used anywhere.
647
function build_work_directory_selector($folders, $curdirpath, $group_dir = '')
648
{
649
    $form = '<form name="selector" action="'.api_get_self().'?'.api_get_cidreq().'" method="POST">';
650
    $form .= get_lang('CurrentDirectory').' <select name="curdirpath" onchange="javascript: document.selector.submit();">';
651
    //group documents cannot be uploaded in the root
652
    if ($group_dir == '') {
653
        $form .= '<option value="/">/ ('.get_lang('Root').')</option>';
654
        if (is_array($folders)) {
655
            foreach ($folders as $folder) {
656
                $selected = ($curdirpath == $folder) ? ' selected="selected"' : '';
657
                $form .= '<option'.$selected.' value="'.$folder.'">'.$folder.'</option>'."\n";
658
            }
659
        }
660
    } else {
661
        foreach ($folders as $folder) {
662
            $selected = ($curdirpath == $folder) ? ' selected="selected"' : '';
663
            $display_folder = substr($folder, strlen($group_dir));
664
            $display_folder = ($display_folder == '') ? '/ ('.get_lang('Root').')' : $display_folder;
665
            $form .= '<option'.$selected.' value="'.$folder.'">'.$display_folder.'</option>'."\n";
666
        }
667
    }
668
669
    $form .= '</select>';
670
    $form .= '<noscript><input type="submit" name="change_path" value="'.get_lang('Ok').'" /></noscript>';
671
    $form .= '</form>';
672
673
    return $form;
674
}
675
676
/**
677
 * Builds the form thats enables the user to
678
 * move a document from one directory to another
679
 * This function has been copied from the document/document.inc.php library
680
 *
681
 * @param array $folders
682
 * @param string $curdirpath
683
 * @param string $move_file
684
 * @param string $group_dir
685
 * @return string html form
686
 */
687
function build_work_move_to_selector($folders, $curdirpath, $move_file, $group_dir = '')
688
{
689
    $course_id = api_get_course_int_id();
690
    $move_file = intval($move_file);
691
    $tbl_work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
692
    $sql = "SELECT title, url FROM $tbl_work
693
            WHERE c_id = $course_id AND id ='".$move_file."'";
694
    $result = Database::query($sql);
695
    $row = Database::fetch_array($result, 'ASSOC');
696
    $title = empty($row['title']) ? basename($row['url']) : $row['title'];
697
698
    $form = new FormValidator(
699
        'move_to_form',
700
        'post',
701
        api_get_self().'?'.api_get_cidreq().'&curdirpath='.Security::remove_XSS($curdirpath)
702
    );
703
704
    $form->addHeader(get_lang('MoveFile').' - '.Security::remove_XSS($title));
705
    $form->addHidden('item_id', $move_file);
706
    $form->addHidden('action', 'move_to');
707
708
    //group documents cannot be uploaded in the root
709
    if ($group_dir == '') {
710
        if ($curdirpath != '/') {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
711
            //$form .= '<option value="0">/ ('.get_lang('Root').')</option>';
712
        }
713
        if (is_array($folders)) {
714
            foreach ($folders as $fid => $folder) {
715
                //you cannot move a file to:
716
                //1. current directory
717
                //2. inside the folder you want to move
718
                //3. inside a subfolder of the folder you want to move
719
                if (($curdirpath != $folder) && ($folder != $move_file) && (substr($folder, 0, strlen($move_file) + 1) != $move_file.'/')) {
720
                    //$form .= '<option value="'.$fid.'">'.$folder.'</option>';
721
                    $options[$fid] = $folder;
722
                }
723
            }
724
        }
725
    } else {
726
        if ($curdirpath != '/') {
727
            $form .= '<option value="0">/ ('.get_lang('Root').')</option>';
728
        }
729
        foreach ($folders as $fid => $folder) {
730
            if (($curdirpath != $folder) && ($folder != $move_file) && (substr($folder, 0, strlen($move_file) + 1) != $move_file.'/')) {
731
                //cannot copy dir into his own subdir
732
                $display_folder = substr($folder, strlen($group_dir));
733
                $display_folder = ($display_folder == '') ? '/ ('.get_lang('Root').')' : $display_folder;
734
                //$form .= '<option value="'.$fid.'">'.$display_folder.'</option>'."\n";
735
                $options[$fid] = $display_folder;
736
            }
737
        }
738
    }
739
740
    $form->addSelect('move_to_id', get_lang('Select'), $options);
741
    $form->addButtonSend(get_lang('MoveFile'), 'move_file_submit');
742
743
    return $form->returnForm();
744
}
745
746
/**
747
 * creates a new directory trying to find a directory name
748
 * that doesn't already exist
749
 * (we could use unique_name() here...)
750
 *
751
 * @author Hugues Peeters <[email protected]>
752
 * @author Bert Vanderkimpen
753
 * @author Yannick Warnier <[email protected]> Adaptation for work tool
754
 * @param   string $base_work_dir Base work dir (.../work)
755
 * @param   string $desiredDirName complete path of the desired name
0 ignored issues
show
Bug introduced by
There is no parameter named $desiredDirName. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
756
 *
757
 * @return  string actual directory name if it succeeds, boolean false otherwise
758
 */
759
function create_unexisting_work_directory($base_work_dir, $desired_dir_name)
760
{
761
    $nb = '';
762
    $base_work_dir = (substr($base_work_dir, -1, 1) == '/' ? $base_work_dir : $base_work_dir.'/');
763
    while (file_exists($base_work_dir.$desired_dir_name.$nb)) {
764
        $nb += 1;
765
    }
766
767
    if (@mkdir($base_work_dir.$desired_dir_name.$nb, api_get_permissions_for_new_directories())) {
768
        return $desired_dir_name.$nb;
769
    } else {
770
        return false;
771
    }
772
}
773
774
/**
775
 * Delete a work-tool directory
776
 * @param   int  $id work directory id to delete
777
 * @return  integer -1 on error
778
 */
779
function deleteDirWork($id)
780
{
781
    $locked = api_resource_is_locked_by_gradebook($id, LINK_STUDENTPUBLICATION);
782
783
    if ($locked == true) {
784
        Display::display_warning_message(get_lang('ResourceLockedByGradebook'));
785
        return false;
786
    }
787
788
    $_course = api_get_course_info();
789
    $id = intval($id);
790
    $work_data = get_work_data_by_id($id);
791
792
    if (empty($work_data)) {
793
        return false;
794
    }
795
796
    $base_work_dir = api_get_path(SYS_COURSE_PATH) .$_course['path'].'/work';
797
    $work_data_url = $base_work_dir.$work_data['url'];
798
    $check = Security::check_abs_path($work_data_url.'/', $base_work_dir.'/');
799
800
    $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
801
    $TSTDPUBASG = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
802
    $t_agenda = Database::get_course_table(TABLE_AGENDA);
803
804
    $course_id = api_get_course_int_id();
805
806
    if (!empty($work_data['url'])) {
807
        if ($check) {
808
            // Deleting all contents inside the folder
809
            $sql = "UPDATE $table SET active = 2
810
                    WHERE c_id = $course_id AND filetype = 'folder' AND id = $id";
811
            Database::query($sql);
812
813
            $sql = "UPDATE $table SET active = 2
814
                    WHERE c_id = $course_id AND parent_id = $id";
815
            Database::query($sql);
816
817
            $new_dir = $work_data_url.'_DELETED_'.$id;
818
819 View Code Duplication
            if (api_get_setting('permanently_remove_deleted_files') == 'true') {
820
                my_delete($work_data_url);
821
            } else {
822
                if (file_exists($work_data_url)) {
823
                    rename($work_data_url, $new_dir);
824
                }
825
            }
826
827
            // Gets calendar_id from student_publication_assigment
828
            $sql = "SELECT add_to_calendar FROM $TSTDPUBASG
829
                    WHERE c_id = $course_id AND publication_id = $id";
830
            $res = Database::query($sql);
831
            $calendar_id = Database::fetch_row($res);
832
833
            // delete from agenda if it exists
834
            if (!empty($calendar_id[0])) {
835
                $sql = "DELETE FROM $t_agenda
836
                        WHERE c_id = $course_id AND id = '".$calendar_id[0]."'";
837
                Database::query($sql);
838
            }
839
            $sql = "DELETE FROM $TSTDPUBASG
840
                    WHERE c_id = $course_id AND publication_id = $id";
841
            Database::query($sql);
842
843
            $link_info = GradebookUtils::is_resource_in_course_gradebook(
844
                api_get_course_id(),
845
                3,
846
                $id,
847
                api_get_session_id()
848
            );
849
            $link_id = $link_info['id'];
850
            if ($link_info !== false) {
851
                GradebookUtils::remove_resource_from_course_gradebook($link_id);
852
            }
853
            return true;
854
        }
855
    }
856
}
857
858
/**
859
 * Get the path of a document in the student_publication table (path relative to the course directory)
860
 * @param   integer $id
861
 * @return  string  Path (or -1 on error)
862
 */
863
function get_work_path($id)
864
{
865
    $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
866
    $course_id  = api_get_course_int_id();
867
    $sql = 'SELECT url FROM '.$table.'
868
            WHERE c_id = '.$course_id.' AND id='.intval($id);
869
    $res = Database::query($sql);
870
    if (Database::num_rows($res)) {
871
        $row = Database::fetch_array($res);
872
        return $row['url'];
873
    }
874
    return -1;
875
}
876
877
/**
878
 * Update the url of a work in the student_publication table
879
 * @param integer $id of the work to update
880
 * @param string  $new_path Destination directory where the work has been moved (must end with a '/')
881
 * @param int $parent_id
882
 *
883
 * @return  -1 on error, sql query result on success
0 ignored issues
show
Documentation introduced by
The doc-type -1 could not be parsed: Unknown type name "-1" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
884
 */
885
function updateWorkUrl($id, $new_path, $parent_id)
886
{
887
    if (empty($id)) {
888
        return -1;
889
    }
890
    $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
891
    $course_id = api_get_course_int_id();
892
    $id = intval($id);
893
    $parent_id = intval($parent_id);
894
895
    $sql = "SELECT * FROM $table
896
            WHERE c_id = $course_id AND id = $id";
897
    $res = Database::query($sql);
898
    if (Database::num_rows($res) != 1) {
899
        return -1;
900
    } else {
901
        $row = Database::fetch_array($res);
902
        $filename = basename($row['url']);
903
        $new_url = $new_path.$filename;
904
        $new_url = Database::escape_string($new_url);
905
906
        $sql = "UPDATE $table SET
907
                   url = '$new_url',
908
                   parent_id = '$parent_id'
909
                WHERE c_id = $course_id AND id = $id";
910
        $res = Database::query($sql);
911
912
        return $res;
913
    }
914
}
915
916
/**
917
 * Update the url of a dir in the student_publication table
918
 * @param  array $work_data work original data
919
 * @param  string $newPath Example: "folder1"
920
 * @return bool
921
 */
922
function updateDirName($work_data, $newPath)
923
{
924
    $course_id = $work_data['c_id'];
925
    $sessionId = intval($work_data['session_id']);
926
    $work_id = intval($work_data['id']);
927
    $oldPath = $work_data['url'];
928
    $originalNewPath = Database::escape_string($newPath);
929
    $newPath = Database::escape_string($newPath);
930
    $newPath = api_replace_dangerous_char($newPath);
931
    $newPath = disable_dangerous_file($newPath);
932
933
    if ($oldPath == '/'.$newPath) {
934
        return true;
935
    }
936
937
    if (!empty($newPath)) {
938
        $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
939
        $sql = "UPDATE $table SET
940
                    title = '".$originalNewPath."'
941
                WHERE
942
                    c_id = $course_id AND
943
                    id = $work_id AND
944
                    session_id = $sessionId
945
                ";
946
        Database::query($sql);
947
    }
948
}
949
950
/**
951
 * Return an array with all the folder's ids that are in the given path
952
 * @param   string Path of the directory
953
 * @return  array The list of ids of all the directories in the path
954
 * @author  Julio Montoya Dokeos
955
 * @version April 2008
956
 */
957
958
function get_parent_directories($id)
959
{
960
    $course_id = api_get_course_int_id();
961
    $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
962
    $id = intval($id);
963
    $sql = "SELECT id FROM $work_table WHERE c_id = $course_id AND parent_id = $id";
964
    $result = Database::query($sql);
965
    $list_id = array();
966
    if (Database::num_rows($result)) {
967
        while ($row = Database::fetch_array($result)) {
968
            $list_id[] = $row['id'];
969
        }
970
    }
971
    return $list_id;
972
}
973
974
/**
975
 * Transform an all directory structure (only directories) in an array
976
 * @param   string path of the directory
977
 * @return  array the directory structure into an array
978
 * @author  Julio Montoya Dokeos
979
 * @version April 2008
980
 */
981 View Code Duplication
function directory_to_array($directory)
982
{
983
    $array_items = array();
984
    if ($handle = @opendir($directory)) {
985
        while (false !== ($file = readdir($handle))) {
986
            if ($file != '.' && $file != '..') {
987
                if (is_dir($directory. '/' . $file)) {
988
                    $array_items = array_merge($array_items, directory_to_array($directory. '/' . $file));
989
                    $file = $directory . '/' . $file;
990
                    $array_items[] = preg_replace("/\/\//si", '/', $file);
991
                }
992
            }
993
        }
994
        closedir($handle);
995
    }
996
    return $array_items;
997
}
998
999
/**
1000
 * Insert into the DB of the course all the directories
1001
 * @param   string path of the /work directory of the course
1002
 * @return  -1 on error, sql query result on success
1003
 * @author  Julio Montoya
0 ignored issues
show
Documentation introduced by
The doc-type -1 could not be parsed: Unknown type name "-1" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1004
 * @version April 2008
1005
 * @param string $base_work_dir
1006
 */
1007
1008
function insert_all_directory_in_course_table($base_work_dir)
1009
{
1010
    $dir_to_array = directory_to_array($base_work_dir, true);
1011
    $only_dir = array();
1012
1013
    for ($i = 0; $i < count($dir_to_array); $i++) {
1014
        $only_dir[] = substr($dir_to_array[$i], strlen($base_work_dir), strlen($dir_to_array[$i]));
1015
    }
1016
    $course_id = api_get_course_int_id();
1017
    $group_id  = api_get_group_id();
1018
1019
    $work_table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
1020
1021
    for($i = 0; $i < count($only_dir); $i++) {
1022
        $url = $only_dir[$i];
1023
1024
        $params = [
1025
            'c_id' => $course_id,
1026
            'url' => $url,
1027
            'title' => '',
1028
            'description' => '',
1029
            'author' => '',
1030
            'active' => '1',
1031
            'accepted' => '1',
1032
            'filetype' => 'folder',
1033
            'post_group_id' => $group_id,
1034
        ];
1035
1036
        Database::insert($work_table, $params);
1037
    }
1038
}
1039
1040
/**
1041
 * This function displays the number of files contained in a directory
1042
 *
1043
 * @param   string the path of the directory
1044
 * @param   boolean true if we want the total quantity of files
1045
 * include in others child directories, false only  files in the directory
1046
 * @return  array the first element is an integer with the number of files
1047
 * in the folder, the second element is the number of directories
1048
 * @author  Julio Montoya
1049
 * @version April 2008
1050
 */
1051
function count_dir($path_dir, $recurse)
1052
{
1053
    $count = 0;
1054
    $count_dir = 0;
1055
    $d = dir($path_dir);
1056
    while ($entry = $d->Read()) {
1057
        if (!(($entry == '..') || ($entry == '.'))) {
1058
            if (is_dir($path_dir.'/'.$entry)) {
1059
                $count_dir++;
1060
                if ($recurse) {
1061
                    $count += count_dir($path_dir . '/' . $entry, $recurse);
1062
                }
1063
            } else {
1064
                $count++;
1065
            }
1066
        }
1067
    }
1068
    $return_array = array();
1069
    $return_array[] = $count;
1070
    $return_array[] = $count_dir;
1071
    return $return_array;
1072
}
1073
1074
/**
1075
 * returns all the javascript that is required for easily
1076
 * validation when you create a work
1077
 * this goes into the $htmlHeadXtra[] array
1078
 */
1079
function to_javascript_work()
1080
{
1081
    $js = '<script>
1082
        function updateDocumentTitle(value) {
1083
            var temp = value.indexOf("/");
1084
            //linux path
1085
            if(temp!=-1){
1086
                var temp=value.split("/");
1087
            } else {
1088
                var temp=value.split("\\\");
1089
            }
1090
            document.getElementById("file_upload").value=temp[temp.length-1];
1091
            $("#contains_file_id").attr("value", 1);
1092
        }
1093
1094
        function checkDate(month, day, year) {
1095
          var monthLength =
1096
            new Array(31,28,31,30,31,30,31,31,30,31,30,31);
1097
1098
          if (!day || !month || !year)
1099
            return false;
1100
1101
          // check for bisestile year
1102
          if (year/4 == parseInt(year/4))
1103
            monthLength[1] = 29;
1104
1105
          if (month < 1 || month > 12)
1106
            return false;
1107
1108
          if (day > monthLength[month-1])
1109
            return false;
1110
1111
          return true;
1112
        }
1113
1114
        function mktime() {
1115
1116
            var no, ma = 0, mb = 0, i = 0, d = new Date(), argv = arguments, argc = argv.length;
1117
            d.setHours(0,0,0); d.setDate(1); d.setMonth(1); d.setYear(1972);
1118
1119
            var dateManip = {
1120
                0: function(tt){ return d.setHours(tt); },
1121
                1: function(tt){ return d.setMinutes(tt); },
1122
                2: function(tt){ set = d.setSeconds(tt); mb = d.getDate() - 1; return set; },
1123
                3: function(tt){ set = d.setMonth(parseInt(tt)-1); ma = d.getFullYear() - 1972; return set; },
1124
                4: function(tt){ return d.setDate(tt+mb); },
1125
                5: function(tt){ return d.setYear(tt+ma); }
1126
            };
1127
1128
            for( i = 0; i < argc; i++ ){
1129
                no = parseInt(argv[i]*1);
1130
                if (isNaN(no)) {
1131
                    return false;
1132
                } else {
1133
                    // arg is number, lets manipulate date object
1134
                    if(!dateManip[i](no)){
1135
                        // failed
1136
                        return false;
1137
                    }
1138
                }
1139
            }
1140
            return Math.floor(d.getTime()/1000);
1141
        }
1142
1143
        function setFocus() {
1144
            $("#work_title").focus();
1145
        }
1146
1147
        $(document).ready(function() {
1148
            setFocus();
1149
1150
            var checked = $("#expiry_date").attr("checked");
1151
            if (checked) {
1152
                $("#option2").show();
1153
                $("#option3").show();
1154
                $("#end_date").attr("checked", true);
1155
            } else {
1156
                $("#option2").hide();
1157
                $("#option3").hide();
1158
                $("#end_date").attr("checked", false);
1159
            }
1160
1161
            $("#expiry_date").click(function() {
1162
                $("#option2").toggle();
1163
            });
1164
1165
            $("#end_date").click(function() {
1166
                $("#option3").toggle();
1167
            });
1168
        });
1169
    </script>';
1170
1171
    return $js;
1172
}
1173
1174
/**
1175
 * Gets the id of a student publication with a given path
1176
 * @param string $path
1177
 * @return true if is found / false if not found
1178
 */
1179
// TODO: The name of this function does not fit with the kind of information it returns. Maybe check_work_id() or is_work_id()?
1180
function get_work_id($path)
1181
{
1182
    $TBL_STUDENT_PUBLICATION = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
1183
    $TBL_PROP_TABLE = Database::get_course_table(TABLE_ITEM_PROPERTY);
1184
    $course_id = api_get_course_int_id();
1185
    $path = Database::escape_string($path);
1186
1187
    if (api_is_allowed_to_edit()) {
1188
        $sql = "SELECT work.id
1189
                FROM $TBL_STUDENT_PUBLICATION AS work, $TBL_PROP_TABLE AS props
1190
                WHERE
1191
                    props.c_id = $course_id AND
1192
                    work.c_id = $course_id AND
1193
                    props.tool='work' AND
1194
                    work.id=props.ref AND
1195
                    work.url LIKE 'work/".$path."%' AND
1196
                    work.filetype='file' AND
1197
                    props.visibility<>'2'";
1198
    } else {
1199
        $sql = "SELECT work.id
1200
                FROM $TBL_STUDENT_PUBLICATION AS work, $TBL_PROP_TABLE AS props
1201
                WHERE
1202
                    props.c_id = $course_id AND
1203
                    work.c_id = $course_id AND
1204
                    props.tool='work' AND
1205
                    work.id=props.ref AND
1206
                    work.url LIKE 'work/".$path."%' AND
1207
                    work.filetype='file' AND
1208
                    props.visibility<>'2' AND
1209
                    props.lastedit_user_id = '".api_get_user_id()."'";
1210
    }
1211
    $result = Database::query($sql);
1212
    $num_rows = Database::num_rows($result);
1213
1214
    if ($result && $num_rows > 0) {
1215
        return true;
1216
    } else {
1217
        return false;
1218
    }
1219
}
1220
1221
/**
1222
 * @param int $work_id
1223
 * @param int $onlyMeUserId show only my works
1224
 * @param int $notMeUserId show works from everyone except me
1225
 * @return int
1226
 */
1227
function get_count_work($work_id, $onlyMeUserId = null, $notMeUserId = null)
1228
{
1229
    $work_table      = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
1230
    $iprop_table     = Database::get_course_table(TABLE_ITEM_PROPERTY);
1231
    $user_table      = Database::get_main_table(TABLE_MAIN_USER);
1232
1233
    $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
1234
    $session_id = api_get_session_id();
1235
    $condition_session = api_get_session_condition($session_id, true, false, 'work.session_id');
1236
1237
    $group_id = api_get_group_id();
1238
    $course_info = api_get_course_info();
1239
    $course_id = $course_info['real_id'];
1240
    $work_id = intval($work_id);
1241
1242 View Code Duplication
    if (!empty($group_id)) {
1243
        // set to select only messages posted by the user's group
1244
        $extra_conditions = " work.post_group_id = '".intval($group_id)."' ";
1245
    } else {
1246
        $extra_conditions = " work.post_group_id = '0' ";
1247
    }
1248
1249 View Code Duplication
    if ($is_allowed_to_edit) {
1250
        $extra_conditions .= ' AND work.active IN (0, 1) ';
1251
    } else {
1252
        $extra_conditions .= ' AND work.active IN (0, 1) AND accepted = 1';
1253
        if (isset($course_info['show_score']) && $course_info['show_score'] == 1) {
1254
            $extra_conditions .= " AND work.user_id = ".api_get_user_id()." ";
1255
        } else {
1256
            $extra_conditions .= '';
1257
        }
1258
    }
1259
1260
    $extra_conditions .= " AND parent_id  = ".$work_id."  ";
1261
1262
    $where_condition = null;
1263
1264
    if (!empty($notMeUserId)) {
1265
        $where_condition .= " AND u.user_id <> ".intval($notMeUserId);
1266
    }
1267
1268
    if (!empty($onlyMeUserId)) {
1269
        $where_condition .= " AND u.user_id =  ".intval($onlyMeUserId);
1270
    }
1271
1272
    $sql = "SELECT count(*) as count
1273
            FROM $iprop_table prop
1274
            INNER JOIN $work_table work
1275
            ON (
1276
                prop.ref = work.id AND
1277
                prop.c_id = $course_id AND
1278
                prop.tool='work' AND
1279
                prop.visibility <> 2 AND
1280
                work.c_id = $course_id
1281
            )
1282
            INNER JOIN $user_table u ON (work.user_id = u.user_id)
1283
            WHERE $extra_conditions $where_condition $condition_session";
1284
1285
    $result = Database::query($sql);
1286
1287
    $users_with_work = 0;
1288
    if (Database::num_rows($result)) {
1289
        $result = Database::fetch_array($result);
0 ignored issues
show
Bug introduced by
It seems like $result can be null; however, fetch_array() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
1290
        $users_with_work = $result['count'];
1291
    }
1292
    return $users_with_work;
1293
}
1294
1295
/**
1296
 * @param int $start
1297
 * @param int $limit
1298
 * @param string $column
1299
 * @param string $direction
1300
 * @param string $where_condition
1301
 * @param bool $getCount
1302
 * @return array
1303
 */
1304
function getWorkListStudent(
1305
    $start,
1306
    $limit,
1307
    $column,
1308
    $direction,
1309
    $where_condition,
1310
    $getCount = false
1311
) {
1312
    $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
1313
    $workTableAssignment = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
1314
    $courseInfo = api_get_course_info();
1315
    $course_id = $courseInfo['real_id'];
1316
    $session_id = api_get_session_id();
1317
    $condition_session = api_get_session_condition($session_id);
1318
    $group_id = api_get_group_id();
1319
    $userId = api_get_user_id();
1320
1321
    $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh(
1322
        api_get_user_id(),
1323
        $courseInfo
1324
    );
1325
1326
    if (!in_array($direction, array('asc','desc'))) {
1327
        $direction = 'desc';
1328
    }
1329
    if (!empty($where_condition)) {
1330
        $where_condition = ' AND ' . $where_condition;
1331
    }
1332
1333
    $column = !empty($column) ? Database::escape_string($column) : 'sent_date';
1334
    $start = intval($start);
1335
    $limit = intval($limit);
1336
1337
    // Get list from database
1338
1339 View Code Duplication
    if (!empty($group_id)) {
1340
        $group_query = " WHERE w.c_id = $course_id AND post_group_id = '".$group_id."' ";
1341
        $subdirs_query = "AND parent_id = 0";
1342
    } else {
1343
        $group_query = " WHERE w.c_id = $course_id AND post_group_id = '0' ";
1344
        $subdirs_query = "AND parent_id = 0";
1345
    }
1346
1347
    $active_condition = ' AND active IN (1, 0)';
1348
1349
    if ($getCount) {
1350
        $select = "SELECT count(w.id) as count ";
1351
    } else {
1352
        $select = "SELECT w.*, a.expires_on, expires_on, ends_on, enable_qualification ";
1353
    }
1354
1355
    $sql = "$select
1356
            FROM $workTable w
1357
            LEFT JOIN $workTableAssignment a
1358
            ON (a.publication_id = w.id AND a.c_id = w.c_id)
1359
                $group_query
1360
                $subdirs_query
1361
                $active_condition
1362
                $condition_session
1363
                $where_condition
1364
            ";
1365
1366
    $sql .= " ORDER BY $column $direction ";
1367
1368
    if (!empty($start) && !empty($limit)) {
1369
        $sql .= " LIMIT $start, $limit";
1370
    }
1371
1372
    $result = Database::query($sql);
1373
1374
    if ($getCount) {
1375
        $row = Database::fetch_array($result);
1376
        return $row['count'];
1377
    }
1378
1379
    $works = array();
1380
1381
    $url = api_get_path(WEB_CODE_PATH).'work/work_list.php?'.api_get_cidreq();
1382
    if ($isDrhOfCourse) {
1383
        $url = api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq();
1384
    }
1385
1386
    $urlOthers = api_get_path(WEB_CODE_PATH).'work/work_list_others.php?'.api_get_cidreq().'&id=';
1387
    while ($work = Database::fetch_array($result, 'ASSOC')) {
1388
        $isSubscribed = userIsSubscribedToWork($userId, $work['id'], $course_id);
1389
        if ($isSubscribed == false) {
1390
            continue;
1391
        }
1392
1393
        $visibility = api_get_item_visibility($courseInfo, 'work', $work['id'], $session_id);
1394
1395
        if ($visibility != 1) {
1396
            continue;
1397
        }
1398
1399
        $work['type'] = Display::return_icon('work.png');
1400
        $work['expires_on'] = empty($work['expires_on']) ? null : api_get_local_time($work['expires_on']);
1401
1402
        if (empty($work['title'])) {
1403
            $work['title'] = basename($work['url']);
1404
        }
1405
1406
        $whereCondition = " AND u.user_id = ".intval($userId);
1407
1408
        $workList = get_work_user_list(
1409
            0,
1410
            1000,
1411
            null,
1412
            null,
1413
            $work['id'],
1414
            $whereCondition
1415
        );
1416
1417
        $count = getTotalWorkComment($workList, $courseInfo);
1418
1419 View Code Duplication
        if (!is_null($count) && !empty($count)) {
1420
            $work['feedback'] = ' '.Display::label($count.' '.get_lang('Feedback'), 'info');
1421
        }
1422
1423
        $lastWork = getLastWorkStudentFromParentByUser($userId, $work['id'], $courseInfo);
1424
1425
        if (!empty($lastWork)) {
1426
            $work['last_upload'] = Display::label($lastWork['qualification'], 'warning').' - ';
1427
            $work['last_upload'] .= api_get_local_time($lastWork['sent_date']);
1428
        }
1429
1430
1431
        $work['title'] = Display::url($work['title'], $url.'&id='.$work['id']);
1432
        $work['others'] = Display::url(
1433
            Display::return_icon('group.png', get_lang('Others')),
1434
            $urlOthers.$work['id']
1435
        );
1436
        $works[] = $work;
1437
    }
1438
1439
    return $works;
1440
}
1441
1442
/**
1443
 * @param int $start
1444
 * @param int $limit
1445
 * @param string $column
1446
 * @param string $direction
1447
 * @param string $where_condition
1448
 * @param bool $getCount
1449
 * @return array
1450
 */
1451
function getWorkListTeacher(
1452
    $start,
1453
    $limit,
1454
    $column,
1455
    $direction,
1456
    $where_condition,
1457
    $getCount = false
1458
) {
1459
    $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
1460
    $workTableAssignment = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
1461
1462
    $courseInfo = api_get_course_info();
1463
    $course_id = api_get_course_int_id();
1464
    $session_id = api_get_session_id();
1465
    $condition_session = api_get_session_condition($session_id);
1466
    $group_id = api_get_group_id();
1467
    $is_allowed_to_edit = api_is_allowed_to_edit() || api_is_coach();
1468
1469
    if (!in_array($direction, array('asc', 'desc'))) {
1470
        $direction = 'desc';
1471
    }
1472
    if (!empty($where_condition)) {
1473
        $where_condition = ' AND ' . $where_condition;
1474
    }
1475
1476
    $column = !empty($column) ? Database::escape_string($column) : 'sent_date';
1477
    $start = intval($start);
1478
    $limit = intval($limit);
1479
    $works = array();
1480
1481
    // Get list from database
1482
    if ($is_allowed_to_edit) {
1483
        $active_condition = ' active IN (0, 1)';
1484
        if ($getCount) {
1485
            $select = " SELECT count(w.id) as count";
1486
        } else {
1487
            $select = " SELECT w.*, a.expires_on, expires_on, ends_on, enable_qualification ";
1488
        }
1489
        $sql = " $select
1490
                FROM $workTable w
1491
                LEFT JOIN $workTableAssignment a
1492
                ON (a.publication_id = w.id AND a.c_id = w.c_id)
1493
                WHERE
1494
                    w.c_id = $course_id
1495
                    $condition_session AND
1496
                    $active_condition AND
1497
                    (parent_id = 0) AND
1498
                    post_group_id = '".$group_id."'
1499
                    $where_condition
1500
                ORDER BY $column $direction
1501
                LIMIT $start, $limit";
1502
        $result = Database::query($sql);
1503
1504
        if ($getCount) {
1505
            $row = Database::fetch_array($result);
1506
1507
            return $row['count'];
1508
        }
1509
        $url = api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq();
1510
        while ($work = Database::fetch_array($result, 'ASSOC')) {
1511
            $workId = $work['id'];
1512
            $work['type'] = Display::return_icon('work.png');
1513
            $work['expires_on'] = empty($work['expires_on']) ? null : api_get_local_time($work['expires_on']);
1514
1515
            $totalUsers = getStudentSubscribedToWork(
1516
                $workId,
1517
                $course_id,
1518
                $group_id,
1519
                $session_id,
1520
                true
1521
            );
1522
1523
            $countUniqueAttempts = getUniqueStudentAttemptsTotal(
1524
                $workId,
1525
                $group_id,
1526
                $course_id,
1527
                $session_id
1528
            );
1529
1530
            $work['amount'] = Display::label(
1531
                $countUniqueAttempts . '/' .
1532
                $totalUsers,
1533
                'success'
1534
            );
1535
1536
            $visibility = api_get_item_visibility($courseInfo, 'work', $workId, $session_id);
1537
1538
            if ($visibility == 1) {
1539
                $icon = 'visible.png';
1540
                $text = get_lang('Visible');
1541
                $action = 'invisible';
1542
                $class = '';
1543
            } else {
1544
                $icon = 'invisible.png';
1545
                $text = get_lang('Invisible');
1546
                $action = 'visible';
1547
                $class = 'muted';
1548
            }
1549
1550
            $visibilityLink = Display::url(
1551
                Display::return_icon($icon, $text, array(), ICON_SIZE_SMALL),
1552
                api_get_path(WEB_CODE_PATH).'work/work.php?id='.$workId.'&action='.$action.'&'.api_get_cidreq()
1553
            );
1554
1555
            if (empty($work['title'])) {
1556
                $work['title'] = basename($work['url']);
1557
            }
1558
            $work['title'] = Display::url($work['title'], $url.'&id='.$workId, ['class' => $class]);
1559
            $work['title'] .= ' '.Display::label(get_count_work($work['id']), 'success');
1560
            $work['sent_date'] = api_get_local_time($work['sent_date']);
1561
1562
            $editLink = Display::url(
1563
                Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
1564
                api_get_path(WEB_CODE_PATH).'work/edit_work.php?id='.$workId.'&'.api_get_cidreq()
1565
            );
1566
1567
            $correctionLink = Display::url(
1568
                Display::return_icon('upload_file.png', get_lang('UploadCorrections'), '', ICON_SIZE_SMALL),
1569
                api_get_path(WEB_CODE_PATH).'work/upload_corrections.php?'.api_get_cidreq().'&id='.$workId
1570
            );
1571
1572
            if ($countUniqueAttempts > 0) {
1573
                $downloadLink = Display::url(
1574
                    Display::return_icon(
1575
                        'save_pack.png',
1576
                        get_lang('Save'),
1577
                        array(),
1578
                        ICON_SIZE_SMALL
1579
                    ),
1580
                    api_get_path(WEB_CODE_PATH) . 'work/downloadfolder.inc.php?id=' . $workId . '&' . api_get_cidreq()
1581
                );
1582
            } else {
1583
                $downloadLink = Display::url(
1584
                    Display::return_icon(
1585
                        'save_pack_na.png',
1586
                        get_lang('Save'),
1587
                        array(),
1588
                        ICON_SIZE_SMALL
1589
                    ),
1590
                    '#'
1591
                );
1592
            }
1593
            // Remove Delete Work Button from action List
1594
            // Because removeXSS "removes" the onClick JS Event to do the action (See model.ajax.php - Line 1639)
1595
            // But still can use the another jqgrid button to remove works (trash icon)
1596
            //
1597
            // $deleteUrl = api_get_path(WEB_CODE_PATH).'work/work.php?id='.$workId.'&action=delete_dir&'.api_get_cidreq();
1598
            // $deleteLink = '<a href="#" onclick="showConfirmationPopup(this, \'' . $deleteUrl . '\' ) " >' .
1599
            //     Display::return_icon(
1600
            //         'delete.png',
1601
            //         get_lang('Delete'),
1602
            //         array(),
1603
            //         ICON_SIZE_SMALL
1604
            //     ) . '</a>';
1605
1606
            if (!api_is_allowed_to_edit()) {
1607
                // $deleteLink = null;
1608
                $editLink = null;
1609
            }
1610
            $work['actions'] = $visibilityLink.$correctionLink.$downloadLink.$editLink;
1611
            $works[] = $work;
1612
        }
1613
    }
1614
1615
    return $works;
1616
}
1617
1618
/**
1619
 * @param int $start
1620
 * @param int $limit
1621
 * @param string $column
1622
 * @param string $direction
1623
 * @param int $workId
1624
 * @param int $studentId
1625
 * @param string $whereCondition
1626
 * @param bool $getCount
1627
 * @return array
1628
 */
1629
function get_work_user_list_from_documents(
1630
    $start,
1631
    $limit,
1632
    $column,
1633
    $direction,
1634
    $workId,
1635
    $studentId = null,
1636
    $whereCondition,
1637
    $getCount = false
1638
) {
1639
    if ($getCount) {
1640
        $select1 = " SELECT count(u.user_id) as count ";
1641
        $select2 = " SELECT count(u.user_id) as count ";
1642
    } else {
1643
        $select1 = " SELECT DISTINCT u.firstname, u.lastname, u.user_id, w.title, w.parent_id, w.document_id document_id, w.id, qualification, qualificator_id";
1644
        $select2 = " SELECT DISTINCT u.firstname, u.lastname, u.user_id, d.title, w.parent_id, d.id document_id, 0, 0, 0";
1645
    }
1646
1647
    $documentTable = Database::get_course_table(TABLE_DOCUMENT);
1648
    $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
1649
    $workRelDocument = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_DOCUMENT);
1650
    $userTable = Database::get_main_table(TABLE_MAIN_USER);
1651
1652
    $courseId = api_get_course_int_id();
1653
    $sessionId = api_get_session_id();
1654
1655
    if (empty($studentId)) {
1656
        $studentId = api_get_user_id();
1657
    }
1658
    $studentId = intval($studentId);
1659
    $workId = intval($workId);
1660
1661
    $userCondition = " AND u.user_id = $studentId ";
1662
    $sessionCondition = " AND w.session_id = $sessionId ";
1663
    $workCondition = " AND w_rel.work_id = $workId";
1664
    $workParentCondition  = " AND w.parent_id = $workId";
1665
1666
    $sql = "(
1667
                $select1 FROM $userTable u
1668
                INNER JOIN $workTable w
1669
                ON (u.user_id = w.user_id AND w.active IN (0, 1) AND w.filetype = 'file')
1670
                WHERE
1671
                    w.c_id = $courseId
1672
                    $userCondition
1673
                    $sessionCondition
1674
                    $whereCondition
1675
                    $workParentCondition
1676
            ) UNION (
1677
                $select2 FROM $workTable w
1678
                INNER JOIN $workRelDocument w_rel
1679
                ON (w_rel.work_id = w.id AND w.active IN (0, 1) AND w_rel.c_id = w.c_id)
1680
                INNER JOIN $documentTable d
1681
                ON (w_rel.document_id = d.id AND d.c_id = w.c_id)
1682
                INNER JOIN $userTable u ON (u.user_id = $studentId)
1683
                WHERE
1684
                    w.c_id = $courseId
1685
                    $workCondition
1686
                    $sessionCondition AND
1687
                    d.id NOT IN (
1688
                        SELECT w.document_id id
1689
                        FROM $workTable w
1690
                        WHERE
1691
                            user_id = $studentId AND
1692
                            c_id = $courseId AND
1693
                            filetype = 'file' AND
1694
                            active IN (0, 1)
1695
                            $sessionCondition
1696
                            $workParentCondition
1697
                    )
1698
            )";
1699
1700
    $start = intval($start);
1701
    $limit = intval($limit);
1702
1703
    $direction = in_array(strtolower($direction), array('desc', 'asc')) ? $direction : 'desc';
1704
    $column = Database::escape_string($column);
1705
1706
    if ($getCount) {
1707
        $result = Database::query($sql);
1708
        $result = Database::fetch_array($result);
0 ignored issues
show
Bug introduced by
It seems like $result can be null; however, fetch_array() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
1709
        return $result['count'];
1710
    }
1711
1712
    $sql .= " ORDER BY $column $direction";
1713
    $sql .= " LIMIT $start, $limit";
1714
1715
    $result = Database::query($sql);
1716
1717
    $currentUserId = api_get_user_id();
1718
    $work_data = get_work_data_by_id($workId);
1719
1720
    $qualificationExists = false;
1721
1722 View Code Duplication
    if (!empty($work_data['qualification']) && intval($work_data['qualification']) > 0) {
1723
        $qualificationExists = true;
1724
    }
1725
1726
    $urlAdd = api_get_path(WEB_CODE_PATH).'work/upload_from_template.php?'.api_get_cidreq();
1727
    $urlEdit = api_get_path(WEB_CODE_PATH).'work/edit.php?'.api_get_cidreq();
1728
    $urlDelete = api_get_path(WEB_CODE_PATH).'work/work_list.php?action=delete&'.api_get_cidreq();
1729
    $urlView = api_get_path(WEB_CODE_PATH).'work/view.php?'.api_get_cidreq();
1730
1731
    $editIcon = Display::return_icon('edit.png', get_lang('Edit'));
1732
    $addIcon = Display::return_icon('add.png', get_lang('Add'));
1733
    $deleteIcon = Display::return_icon('delete.png', get_lang('Delete'));
1734
    $viewIcon = Display::return_icon('default.png', get_lang('View'));
1735
    $allowEdition = api_get_course_setting('student_delete_own_publication');
1736
1737
    $workList = array();
1738
    while ($row = Database::fetch_array($result, 'ASSOC')) {
1739
        $userId = $row['user_id'];
1740
        $documentId = $row['document_id'];
1741
        $itemId = $row['id'];
1742
        $addLinkShowed = false;
1743
1744
        if (empty($documentId)) {
1745
            $url = $urlEdit.'&item_id='.$row['id'].'&id='.$workId;
1746
            $editLink = Display::url($editIcon, $url);
1747
            if ($allowEdition == false) {
1748
                $editLink = null;
1749
            }
1750
        } else {
1751
            $documentToWork = getDocumentToWorkPerUser($documentId, $workId, $courseId, $sessionId, $userId);
1752
1753
            if (empty($documentToWork)) {
1754
                $url = $urlAdd.'&document_id='.$documentId.'&id='.$workId;
1755
                $editLink = Display::url($addIcon, $url);
1756
                $addLinkShowed = true;
1757
            } else {
1758
1759
                $row['title'] = $documentToWork['title'];
1760
                $row['sent_date'] = $documentToWork['sent_date'];
1761
                $newWorkId = $documentToWork['id'];
1762
                $url = $urlEdit.'&item_id='.$newWorkId.'&id='.$workId;
1763
                $editLink = Display::url($editIcon, $url);
1764
1765
                if ($allowEdition == false) {
1766
                    $editLink = null;
1767
                }
1768
            }
1769
        }
1770
1771
        if ($allowEdition && !empty($itemId)) {
1772
            $deleteLink  = Display::url($deleteIcon, $urlDelete.'&item_id='.$itemId.'&id='.$workId);
1773
        } else {
1774
            $deleteLink = null;
1775
        }
1776
1777
        $viewLink = null;
1778
1779
        if (!empty($itemId)) {
1780
            $viewLink = Display::url($viewIcon, $urlView.'&id='.$itemId);
1781
        }
1782
1783
        //$row['type'] = build_document_icon_tag('file', $row['url']);
1784
        $row['type'] = null;
1785
1786
        if ($qualificationExists) {
1787 View Code Duplication
            if (empty($row['qualificator_id'])) {
1788
                $status = Display::label(get_lang('NotRevised'), 'warning');
1789
            } else {
1790
                $status = Display::label(get_lang('Revised'), 'success');
1791
            }
1792
            $row['qualificator_id'] = $status;
1793
        }
1794
1795
        if (!empty($row['qualification'])) {
1796
            $row['qualification'] = Display::label($row['qualification'], 'info');
1797
        }
1798
1799
        if (!empty($row['sent_date'])) {
1800
            $row['sent_date'] = api_get_local_time($row['sent_date']);
1801
        }
1802
1803
        if ($userId == $currentUserId) {
1804
            $row['actions'] = $viewLink.$editLink.$deleteLink;
1805
        }
1806
1807
        if ($addLinkShowed) {
1808
            $row['qualification'] = '';
1809
            $row['qualificator_id'] = '';
1810
        }
1811
1812
        $workList[] = $row;
1813
    }
1814
1815
    return $workList;
1816
}
1817
1818
/**
1819
 * @param int $start
1820
 * @param int $limit
1821
 * @param int $column
1822
 * @param string $direction
1823
 * @param int $work_id
1824
 * @param array $where_condition
1825
 * @param int $studentId
1826
 * @param bool $getCount
1827
 * @return array
1828
 */
1829
function get_work_user_list(
1830
    $start,
1831
    $limit,
1832
    $column,
1833
    $direction,
1834
    $work_id,
1835
    $where_condition = null,
1836
    $studentId = null,
1837
    $getCount = false
1838
) {
1839
    $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
1840
    $iprop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
1841
    $user_table = Database::get_main_table(TABLE_MAIN_USER);
1842
1843
    $session_id = api_get_session_id();
1844
    $group_id = api_get_group_id();
1845
    $course_info = api_get_course_info();
1846
    $course_id = $course_info['real_id'];
1847
1848
    $work_id = intval($work_id);
1849
    $column = !empty($column) ? Database::escape_string($column) : 'sent_date';
1850
    $start = intval($start);
1851
    $limit = intval($limit);
1852
1853
    if (!in_array($direction, array('asc','desc'))) {
1854
        $direction = 'desc';
1855
    }
1856
1857
    $work_data = get_work_data_by_id($work_id);
1858
    $is_allowed_to_edit = api_is_allowed_to_edit() || api_is_coach();
1859
    $condition_session  = api_get_session_condition($session_id, true, false, 'work.session_id');
1860
    $locked = api_resource_is_locked_by_gradebook($work_id, LINK_STUDENTPUBLICATION);
1861
1862
    $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh(
1863
        api_get_user_id(),
1864
        $course_info
1865
    );
1866
1867
    if (!empty($work_data)) {
1868 View Code Duplication
        if (!empty($group_id)) {
1869
            $extra_conditions = " work.post_group_id = '".intval($group_id)."' ";
1870
            // set to select only messages posted by the user's group
1871
        } else {
1872
            $extra_conditions = " work.post_group_id = '0' ";
1873
        }
1874
1875 View Code Duplication
        if ($is_allowed_to_edit || $isDrhOfCourse) {
1876
            $extra_conditions .= ' AND work.active IN (0, 1) ';
1877
        } else {
1878
            if (isset($course_info['show_score']) &&
1879
                $course_info['show_score'] == 1
1880
            ) {
1881
                $extra_conditions .= " AND (u.user_id = ".api_get_user_id()." AND work.active IN (0, 1)) ";
1882
            } else {
1883
                $extra_conditions .= ' AND work.active IN (0, 1) ';
1884
            }
1885
        }
1886
1887
        $extra_conditions .= " AND parent_id  = ".$work_id." ";
1888
1889
        $select = 'SELECT DISTINCT
1890
                        u.user_id,
1891
                        work.id as id,
1892
                        title as title,
1893
                        description,
1894
                        url,
1895
                        sent_date,
1896
                        contains_file,
1897
                        has_properties,
1898
                        view_properties,
1899
                        qualification,
1900
                        weight,
1901
                        allow_text_assignment,
1902
                        u.firstname,
1903
                        u.lastname,
1904
                        u.username,
1905
                        parent_id,
1906
                        accepted,
1907
                        qualificator_id,
1908
                        url_correction
1909
                        ';
1910
        if ($getCount) {
1911
            $select = "SELECT DISTINCT count(u.user_id) as count ";
1912
        }
1913
1914
        $user_condition = "INNER JOIN $user_table u  ON (work.user_id = u.user_id) ";
1915
        $work_condition = "$iprop_table prop INNER JOIN $work_table work
1916
                           ON (prop.ref = work.id AND prop.c_id = $course_id AND work.c_id = $course_id ) ";
1917
1918
        $work_assignment = get_work_assignment_by_id($work_id);
1919
1920
        if (!empty($studentId)) {
1921
            $where_condition.= " AND u.user_id = ".intval($studentId);
1922
        }
1923
1924
        $sql = " $select
1925
                FROM $work_condition  $user_condition
1926
                WHERE $extra_conditions $where_condition $condition_session
1927
                    AND u.status != " . INVITEE . "
1928
                ORDER BY $column $direction";
1929
1930
        if (!empty($start) && !empty($limit)) {
1931
            $sql .= " LIMIT $start, $limit";
1932
        }
1933
        $result = Database::query($sql);
1934
        $works = array();
1935
1936
        if ($getCount) {
1937
            $work = Database::fetch_array($result, 'ASSOC');
1938
            return $work['count'];
1939
        }
1940
1941
        $url = api_get_path(WEB_CODE_PATH).'work/';
1942
1943
        $unoconv = api_get_configuration_value('unoconv.binaries');
1944
1945
        while ($work = Database::fetch_array($result, 'ASSOC')) {
1946
            $item_id = $work['id'];
1947
1948
            // Get the author ID for that document from the item_property table
1949
            $is_author  = false;
1950
            $can_read   = false;
1951
1952
            $owner_id = $work['user_id'];
1953
1954
            /* Because a bug found when saving items using the api_item_property_update()
1955
               the field $item_property_data['insert_user_id'] is not reliable. */
1956
1957
            if (!$is_allowed_to_edit && $owner_id == api_get_user_id()) {
1958
                $is_author = true;
1959
            }
1960
1961
            if ($course_info['show_score'] == 0) {
1962
                $can_read = true;
1963
            }
1964
1965
            if ($work['accepted'] == '0') {
1966
                $class = 'invisible';
1967
            } else {
1968
                $class = '';
1969
            }
1970
1971
            $qualification_exists = false;
1972 View Code Duplication
            if (!empty($work_data['qualification']) &&
1973
                intval($work_data['qualification']) > 0
1974
            ) {
1975
                $qualification_exists = true;
1976
            }
1977
1978
            $qualification_string = '';
1979
            if ($qualification_exists) {
1980
                if ($work['qualification'] == '') {
1981
                    $qualification_string = Display::label('-');
1982
                } else {
1983
                    $label = 'info';
1984
                    $relativeScore = $work['qualification']/$work_data['qualification'];
1985
                    if ($relativeScore < 0.5) {
1986
                        $label = 'important';
1987
                    } elseif ($relativeScore < 0.75) {
1988
                        $label = 'warning';
1989
                    }
1990
                    $qualification_string = Display::label(
1991
                        $work['qualification'].' / '.$work_data['qualification'],
1992
                        $label
1993
                    );
1994
                }
1995
            }
1996
1997
            $work['qualification_score'] = $work['qualification'];
1998
1999
            $add_string = '';
2000
2001
            $time_expires = '';
2002
            if (!empty($work_assignment['expires_on'])) {
2003
                $time_expires = api_strtotime(
2004
                    $work_assignment['expires_on'],
2005
                    'UTC'
2006
                );
2007
            }
2008
2009
            if (!empty($work_assignment['expires_on']) &&
2010
                !empty($time_expires) && ($time_expires < api_strtotime($work['sent_date'], 'UTC'))) {
2011
                $add_string = Display::label(get_lang('Expired'), 'important');
2012
            }
2013
2014
            if (($can_read && $work['accepted'] == '1') ||
2015
                ($is_author && in_array($work['accepted'], array('1', '0'))) ||
2016
                ($is_allowed_to_edit || api_is_drh())
2017
            ) {
2018
                // Firstname, lastname, username
2019
                $work['firstname'] = Display::div($work['firstname'], array('class' => $class));
2020
                $work['lastname'] = Display::div($work['lastname'], array('class' => $class));
2021
2022
                $work['title_clean'] = $work['title'];
2023
2024
                if (strlen($work['title']) > 30) {
2025
                    $short_title = substr($work['title'], 0, 27).'...';
2026
                    $work['title'] = Display::span($short_title, array('class' => $class, 'title' => $work['title']));
2027
                } else {
2028
                    $work['title'] = Display::div($work['title'], array('class' => $class));
2029
                }
2030
2031
                // Type.
2032
                $work['type'] = DocumentManager::build_document_icon_tag('file', $work['url']);
2033
2034
                // File name.
2035
                $link_to_download = null;
2036
2037
                // If URL is present then there's a file to download keep BC.
2038 View Code Duplication
                if ($work['contains_file'] || !empty($work['url'])) {
2039
                    $link_to_download = '<a href="'.$url.'download.php?id='.$item_id.'&'.api_get_cidreq().'">'.
2040
                        Display::return_icon('save.png', get_lang('Save'),array(), ICON_SIZE_SMALL).'</a> ';
2041
                }
2042
2043
                $send_to = Portfolio::share('work', $work['id'],  array('style' => 'white-space:nowrap;'));
2044
2045
                $feedback = null;
2046
                $count = getWorkCommentCount($item_id, $course_info);
2047
                if (!is_null($count) && !empty($count)) {
2048
                    if ($qualification_exists) {
2049
                        $feedback .= "<br />";
2050
                    }
2051
                    $feedback .= '<a href="'.$url.'view.php?'.api_get_cidreq().'&id='.$item_id.'" title="'.get_lang('View').'">'.
2052
                        Display::label($count.' '.get_lang('Feedback'), 'info').'</a> ';
2053
                }
2054
2055
                $work['qualification'] = $qualification_string.$feedback;
2056
                $work['qualification_only'] = $qualification_string;
2057
2058
                // Date.
2059
                $work_date = api_convert_and_format_date($work['sent_date']);
2060
2061
                $work['sent_date_from_db'] = $work['sent_date'];
2062
                $work['sent_date'] = '<div class="date-time">' . date_to_str_ago(api_get_local_time($work['sent_date'])) . ' ' . $add_string . ' ' . $work_date . '</div>';
2063
2064
                // Actions.
2065
                $correction = '';
2066
2067
                $action = '';
2068
                if (api_is_allowed_to_edit()) {
2069
                    if (!empty($work['url_correction'])) {
2070
                        $action .= Display::url(
2071
                            Display::return_icon('check-circle.png', get_lang('Correction'), null, ICON_SIZE_SMALL),
2072
                            api_get_path(WEB_CODE_PATH).'work/download.php?id='.$item_id.'&'.api_get_cidreq().'&correction=1'
2073
                        );
2074
                    }
2075
2076
                    $action .= '<a href="'.$url.'view.php?'.api_get_cidreq().'&id='.$item_id.'" title="'.get_lang('View').'">'.
2077
                        Display::return_icon('default.png', get_lang('View'), array(), ICON_SIZE_SMALL).'</a> ';
2078
2079
                    if ($unoconv && empty($work['contains_file'])) {
2080
                        $action .=  '<a href="'.$url.'work_list_all.php?'.api_get_cidreq().'&id='.$work_id.'&action=export_to_doc&item_id='.$item_id.'" title="'.get_lang('ExportToDoc').'" >'.
2081
                            Display::return_icon('export_doc.png', get_lang('ExportToDoc'),array(), ICON_SIZE_SMALL).'</a> ';
2082
                    }
2083
2084
                    $correction = '
2085
                        <form
2086
                        id="file_upload_'.$item_id.'"
2087
                        class="work_correction_file_upload file_upload_small"
2088
                        action="'.api_get_path(WEB_AJAX_PATH).'work.ajax.php?'.api_get_cidreq().'&a=upload_correction_file&item_id='.$item_id.'" method="POST" enctype="multipart/form-data"
2089
                        >
2090
                        <div class="button-load">
2091
                            '.get_lang('ClickOrDropOneFileHere').'
2092
                        </div>
2093
                        <input type="file" name="file" multiple>
2094
                        <button type="submit"></button>
2095
                        </form>
2096
                    ';
2097
2098
                    $correction .= "
2099
                        <script>
2100
                        $(document).ready(function() {
2101
                            $('#file_upload_".$item_id."').fileupload({
2102
                                uploadTable: $('.files'),
2103
                                downloadTable: $('.files'),
2104
                                buildUploadRow: function (files, index) {
2105
                                    $('.files').show();
2106
                                    return
2107
                                        $('<tr><td>' + files[index].name + '<\/td>' +
2108
                                        '<td class=\"file_upload_progress\"><div><\/div><\/td>' +
2109
                                        '<td class=\"file_upload_cancel\">' +
2110
                                        '<button class=\"ui-state-default ui-corner-all\" title=\"".get_lang('Cancel')."\">' +
2111
                                        '<span class=\"ui-icon ui-icon-cancel\">".get_lang('Cancel')."<\/span>' +'<\/button>'+
2112
                                        '<\/td><\/tr>');
2113
                                },
2114
                                buildDownloadRow: function (file) {
2115
                                    return $('<tr><td>' + file.name + '<\/td> <td> ' + file.size + ' <\/td>  <td>&nbsp;' + file.result + ' <\/td> <\/tr>');
2116
                                }
2117
                            });
2118
                        });
2119
                        </script>
2120
                    ";
2121
2122
                    if ($locked) {
2123
                        if ($qualification_exists) {
2124
                            $action .= Display::return_icon('rate_work_na.png', get_lang('CorrectAndRate'),array(), ICON_SIZE_SMALL);
2125
                        } else {
2126
                            $action .= Display::return_icon('edit_na.png', get_lang('Comment'),array(), ICON_SIZE_SMALL);
2127
                        }
2128 View Code Duplication
                    } else {
2129
                        if ($qualification_exists) {
2130
                            $action .= '<a href="'.$url.'edit.php?'.api_get_cidreq().'&item_id='.$item_id.'&id='.$work['parent_id'].'" title="'.get_lang('Edit').'"  >'.
2131
                                Display::return_icon('rate_work.png', get_lang('CorrectAndRate'), array(), ICON_SIZE_SMALL).'</a>';
2132
                        } else {
2133
                            $action .= '<a href="'.$url.'edit.php?'.api_get_cidreq().'&item_id='.$item_id.'&id='.$work['parent_id'].'" title="'.get_lang('Modify').'">'.
2134
                                Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL).'</a>';
2135
                        }
2136
                    }
2137
2138 View Code Duplication
                    if ($work['contains_file']) {
2139
                        if ($locked) {
2140
                            $action .= Display::return_icon('move_na.png', get_lang('Move'),array(), ICON_SIZE_SMALL);
2141
                        } else {
2142
                            $action .= '<a href="'.$url.'work.php?'.api_get_cidreq().'&action=move&item_id='.$item_id.'&id='.$work['parent_id'].'" title="'.get_lang('Move').'">'.
2143
                                Display::return_icon('move.png', get_lang('Move'),array(), ICON_SIZE_SMALL).'</a>';
2144
                        }
2145
                    }
2146
2147 View Code Duplication
                    if ($work['accepted'] == '1') {
2148
                        $action .= '<a href="'.$url.'work_list_all.php?'.api_get_cidreq().'&id='.$work_id.'&action=make_invisible&item_id='.$item_id.'" title="'.get_lang('Invisible').'" >'.
2149
                            Display::return_icon('visible.png', get_lang('Invisible'),array(), ICON_SIZE_SMALL).'</a>';
2150
                    } else {
2151
                        $action .= '<a href="'.$url.'work_list_all.php?'.api_get_cidreq().'&id='.$work_id.'&action=make_visible&item_id='.$item_id.'" title="'.get_lang('Visible').'" >'.
2152
                            Display::return_icon('invisible.png', get_lang('Visible'),array(), ICON_SIZE_SMALL).'</a> ';
2153
                    }
2154
2155
                    if ($locked) {
2156
                        $action .= Display::return_icon('delete_na.png', get_lang('Delete'), '', ICON_SIZE_SMALL);
2157
                    } else {
2158
                        $action .= '<a href="'.$url.'work_list_all.php?'.api_get_cidreq().'&id='.$work_id.'&action=delete&item_id='.$item_id.'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES))."'".')) return false;" title="'.get_lang('Delete').'" >'.
2159
                            Display::return_icon('delete.png', get_lang('Delete'),'',ICON_SIZE_SMALL).'</a>';
2160
                    }
2161
                } elseif ($is_author && (empty($work['qualificator_id']) || $work['qualificator_id'] == 0)) {
2162
                    $action .= '<a href="'.$url.'view.php?'.api_get_cidreq().'&id='.$item_id.'" title="'.get_lang('View').'">'.
2163
                        Display::return_icon('default.png', get_lang('View'),array(), ICON_SIZE_SMALL).'</a>';
2164
2165
                    if (api_get_course_setting('student_delete_own_publication') == 1) {
2166 View Code Duplication
                        if (api_is_allowed_to_session_edit(false, true)) {
2167
                            $action .= '<a href="'.$url.'edit.php?'.api_get_cidreq().'&item_id='.$item_id.'&id='.$work['parent_id'].'" title="'.get_lang('Modify').'">'.
2168
                                Display::return_icon('edit.png', get_lang('Comment'),array(), ICON_SIZE_SMALL).'</a>';
2169
                        }
2170
                        $action .= ' <a href="'.$url.'work_list.php?'.api_get_cidreq().'&action=delete&item_id='.$item_id.'&id='.$work['parent_id'].'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES))."'".')) return false;" title="'.get_lang('Delete').'"  >'.
2171
                            Display::return_icon('delete.png',get_lang('Delete'),'',ICON_SIZE_SMALL).'</a>';
2172
                    } else {
2173
                        $action .= Display::return_icon('edit_na.png', get_lang('Modify'),array(), ICON_SIZE_SMALL);
2174
                    }
2175
                } else {
2176
                    $action .= '<a href="'.$url.'view.php?'.api_get_cidreq().'&id='.$item_id.'" title="'.get_lang('View').'">'.
2177
                        Display::return_icon('default.png', get_lang('View'),array(), ICON_SIZE_SMALL).'</a>';
2178
                    $action .= Display::return_icon('edit_na.png', get_lang('Modify'),array(), ICON_SIZE_SMALL);
2179
                }
2180
2181
                // Status.
2182 View Code Duplication
                if (empty($work['qualificator_id'])) {
2183
                    $qualificator_id = Display::label(get_lang('NotRevised'), 'warning');
2184
                } else {
2185
                    $qualificator_id = Display::label(get_lang('Revised'), 'success');
2186
                }
2187
                $work['qualificator_id'] = $qualificator_id;
2188
                $work['actions'] = $send_to.$link_to_download.$action;
2189
                $work['correction'] = $correction;
2190
2191
                $works[] = $work;
2192
            }
2193
        }
2194
2195
        return $works;
2196
    }
2197
}
2198
2199
/**
2200
 * Send reminder to users who have not given the task
2201
 *
2202
 * @param int
2203
 * @return array
2204
 * @author cvargas [email protected] cfasanando, [email protected]
2205
 */
2206
function send_reminder_users_without_publication($task_data)
2207
{
2208
    $_course = api_get_course_info();
2209
    $task_id = $task_data['id'];
2210
    $task_title = !empty($task_data['title']) ? $task_data['title'] : basename($task_data['url']);
2211
    $subject = '[' . api_get_setting('siteName') . '] ';
2212
2213
    // The body can be as long as you wish, and any combination of text and variables
2214
    $content = get_lang('ReminderToSubmitPendingTask')."\n".get_lang('CourseName').' : '.$_course['name']."\n";
2215
    $content .= get_lang('WorkName').' : '.$task_title."\n";
2216
2217
    $list_users = get_list_users_without_publication($task_id);
2218
2219
    $mails_sent_to = array();
2220
    foreach ($list_users as $user) {
2221
        $name_user = api_get_person_name($user[1], $user[0], null, PERSON_NAME_EMAIL_ADDRESS);
2222
        $dear_line = get_lang('Dear')." ".api_get_person_name($user[1], $user[0]) .", \n\n";
2223
        $body      = $dear_line.$content;
2224
        MessageManager::send_message($user[3], $subject, $body);
2225
        $mails_sent_to[] = $name_user;
2226
    }
2227
    return $mails_sent_to;
2228
}
2229
2230
/**
2231
 * Sends an email to the students of a course when a homework is created
2232
 *
2233
 * @param int $courseId course_id
2234
 * @param int $sessionId session_id
2235
 * @param int $workId work_id
2236
 *
2237
 *
2238
 * @author Guillaume Viguier <[email protected]>
2239
 * @author Julio Montoya <[email protected]> Adding session support - 2011
2240
 */
2241
function send_email_on_homework_creation($courseId, $sessionId = 0, $workId)
2242
{
2243
    $courseInfo = api_get_course_info_by_id($courseId);
2244
    $courseCode = $courseInfo['code'];
2245
    // Get the students of the course
2246 View Code Duplication
    if (empty($session_id)) {
0 ignored issues
show
Bug introduced by
The variable $session_id seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
2247
        $students = CourseManager::get_student_list_from_course_code($courseCode);
2248
    } else {
2249
        $students = CourseManager::get_student_list_from_course_code($courseCode, true, $sessionId);
2250
    }
2251
    $emailsubject = '[' . api_get_setting('siteName') . '] '.get_lang('HomeworkCreated');
2252
    $currentUser = api_get_user_info(api_get_user_id());
2253
    if (!empty($students)) {
2254
        foreach($students as $student) {
2255
            $user_info = api_get_user_info($student["user_id"]);
2256
            if(!empty($user_info["mail"])) {
2257
                $name_user = api_get_person_name(
2258
                    $user_info["firstname"],
2259
                    $user_info["lastname"],
2260
                    null,
2261
                    PERSON_NAME_EMAIL_ADDRESS
2262
                );
2263
                $link = api_get_path(WEB_CODE_PATH) . 'work/work_list_all.php?' . api_get_cidreq() . '&id=' . $workId;
2264
                $emailbody = get_lang('Dear')." ".$name_user.",\n\n";
2265
                $emailbody .= get_lang('HomeworkHasBeenCreatedForTheCourse')." ".$courseCode.". "."\n\n".
2266
                    '<a href="'. $link . '">' . get_lang('PleaseCheckHomeworkPage') . '</a>';
2267
                $emailbody .= "\n\n".api_get_person_name($currentUser["firstname"], $currentUser["lastname"]);
2268
2269
                $additionalParameters = array(
2270
                    'smsType' => SmsPlugin::ASSIGNMENT_BEEN_CREATED_COURSE,
2271
                    'userId' => $student["user_id"],
2272
                    'courseTitle' => $courseCode,
2273
                    'link' => $link
2274
                );
2275
2276
                api_mail_html(
2277
                    $name_user,
2278
                    $user_info["mail"],
2279
                    $emailsubject,
2280
                    $emailbody,
2281
                    api_get_person_name(
2282
                        $currentUser["firstname"],
2283
                        $currentUser["lastname"],
2284
                        null,
2285
                        PERSON_NAME_EMAIL_ADDRESS
2286
                    ),
2287
                    $currentUser["mail"],
2288
                    null,
2289
                    null,
2290
                    null,
2291
                    $additionalParameters
2292
                );
2293
            }
2294
        }
2295
    }
2296
}
2297
2298
/**
2299
 * @param string $url
2300
 * @return bool
2301
 */
2302
function is_work_exist_by_url($url)
2303
{
2304
    $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
2305
    $url = Database::escape_string($url);
2306
    $sql = "SELECT id FROM $work_table WHERE url='$url'";
2307
    $result = Database::query($sql);
2308
    if (Database::num_rows($result)> 0) {
2309
        $row = Database::fetch_row($result);
2310
        if (empty($row)) {
2311
            return false;
2312
        } else {
2313
            return true;
2314
        }
2315
    } else {
2316
        return false;
2317
    }
2318
}
2319
2320
/**
2321
 * @param $name
2322
 * @param $values
2323
 * @param string $checked
2324
 * @return string
2325
 */
2326 View Code Duplication
function make_select($name, $values, $checked = '')
2327
{
2328
    $output = '<select name="'.$name.'" id="'.$name.'">';
2329
    foreach ($values as $key => $value) {
2330
        $output .= '<option value="'.$key.'" '.(($checked==$key) ? 'selected="selected"' : '').'>'.$value.'</option>';
2331
    }
2332
    $output .= '</select>';
2333
    return $output;
2334
}
2335
2336
/**
2337
 * @param $name
2338
 * @param string $checked
2339
 * @param null $label
2340
 * @return string
2341
 */
2342
function make_checkbox($name, $checked = '', $label = null)
2343
{
2344
    $check = '<input id ="'.$name.'" type="checkbox" value="1" name="'.$name.'" '.((!empty($checked))?'checked="checked"':'').'/>';
2345
    if (!empty($label)) {
2346
        $check .="<label for ='$name'>$label</label>";
2347
    }
2348
    return $check;
2349
}
2350
2351
/**
2352
 * @param $prefix
2353
 * @param string $default
2354
 * @return string
2355
 */
2356
function draw_date_picker($prefix, $default = '')
2357
{
2358
    if (empty($default)) {
2359
        $default = api_get_local_time();
2360
    }
2361
    $parts = explode(' ', $default);
2362
    list($d_year, $d_month, $d_day) = explode('-', $parts[0]);
2363
    list($d_hour, $d_minute) = explode(':', $parts[1]);
2364
2365
    $minute = range(10, 59);
2366
    array_unshift($minute, '00', '01', '02', '03', '04', '05', '06', '07', '08', '09');
2367
    $date_form = make_select($prefix.'_day', array_combine(range(1, 31), range(1, 31)), $d_day);
2368
    $date_form .= make_select($prefix.'_month', array_combine(range(1, 12), api_get_months_long()), $d_month);
2369
    $date_form .= make_select($prefix.'_year', array($d_year => $d_year, $d_year + 1 => $d_year + 1), $d_year).'&nbsp;&nbsp;&nbsp;&nbsp;';
2370
    $date_form .= make_select($prefix.'_hour', array_combine(range(0, 23), range(0, 23)), $d_hour).' : ';
2371
    $date_form .= make_select($prefix.'_minute', $minute, $d_minute);
2372
    return $date_form;
2373
}
2374
2375
/**
2376
 * @param string $prefix
2377
 * @param array of values
2378
 * @return string
2379
 *
2380
 */
2381 View Code Duplication
function get_date_from_select($prefix, $array = array())
2382
{
2383
    return
2384
        $array[$prefix]['year'].'-'.
2385
        two_digits($array[$prefix]['month']).'-'.
2386
        two_digits($array[$prefix]['day']).' '.
2387
        two_digits($array[$prefix]['hour']).':'.
2388
        two_digits($array[$prefix]['minute']).':00';
2389
}
2390
2391
/**
2392
 * Check if a user is the author of a work document.
2393
 * @param int $itemId
2394
 * @param int $userId
2395
 * @param int $courseId
2396
 * @param int $sessionId
2397
 * @return bool
2398
 */
2399
function user_is_author($itemId, $userId = null, $courseId = null, $sessionId = null)
2400
{
2401
    if (empty($itemId)) {
2402
        return false;
2403
    }
2404
2405
    if (empty($userId)) {
2406
        $userId = api_get_user_id();
2407
    }
2408
2409
    $isAuthor = false;
2410
    $is_allowed_to_edit = api_is_allowed_to_edit();
2411
2412
    if ($is_allowed_to_edit) {
2413
        $isAuthor = true;
2414
    } else {
2415
2416
        if (empty($courseId)) {
2417
            $courseId = api_get_course_int_id();
2418
        }
2419
        if (empty($sessionId)) {
2420
            $sessionId = api_get_session_id();
2421
        }
2422
2423
        $data = api_get_item_property_info($courseId, 'work', $itemId, $sessionId);
2424
        if ($data['insert_user_id'] == $userId) {
2425
            $isAuthor = true;
2426
        }
2427
2428
        $workData = get_work_data_by_id($itemId);
2429
        if ($workData['user_id'] == $userId) {
2430
            $isAuthor = true;
2431
        }
2432
    }
2433
2434
    if (!$isAuthor) {
2435
        return false;
2436
    }
2437
2438
    return $isAuthor;
2439
}
2440
2441
/**
2442
 * Get list of users who have not given the task
2443
 * @param int
2444
 * @param int
2445
 * @return array
2446
 * @author cvargas
2447
 * @author Julio Montoya <[email protected]> Fixing query
2448
 */
2449
function get_list_users_without_publication($task_id, $studentId = null)
2450
{
2451
    $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
2452
    $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
2453
    $table_user = Database::get_main_table(TABLE_MAIN_USER);
2454
    $session_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
2455
2456
    $users = getAllUserToWork($task_id, api_get_course_int_id());
2457
    $users = array_column($users, 'user_id');
2458
2459
    // Condition for the session
2460
    $session_id = api_get_session_id();
2461
    $course_id = api_get_course_int_id();
2462
    $task_id = intval($task_id);
2463
2464
    if ($session_id == 0) {
2465
        $sql = "SELECT user_id as id FROM $work_table
2466
                WHERE
2467
                    c_id = $course_id AND
2468
                    parent_id = '$task_id' AND
2469
                    active IN (0, 1)";
2470
    } else {
2471
        $sql = "SELECT user_id as id FROM $work_table
2472
                WHERE
2473
                    c_id = $course_id AND
2474
                    parent_id = '$task_id' AND
2475
                    session_id = '".$session_id."' AND
2476
                    active IN (0, 1)";
2477
    }
2478
2479
    $result = Database::query($sql);
2480
    $users_with_tasks = array();
2481
    while ($row = Database::fetch_array($result)) {
2482
        $users_with_tasks[] = $row['id'];
2483
    }
2484
2485
    if ($session_id == 0) {
2486
        $sql_users = "SELECT cu.user_id, u.lastname, u.firstname, u.email
2487
                      FROM $table_course_user AS cu, $table_user AS u
2488
                      WHERE u.status != 1 and cu.c_id='".$course_id."' AND u.user_id = cu.user_id";
2489
    } else {
2490
        $sql_users = "SELECT cu.user_id, u.lastname, u.firstname, u.email
2491
                      FROM $session_course_rel_user AS cu, $table_user AS u
2492
                      WHERE
2493
                        u.status != 1 AND
2494
                        cu.c_id='".$course_id."' AND
2495
                        u.user_id = cu.user_id AND
2496
                        cu.session_id = '".$session_id."'";
2497
    }
2498
2499
    if (!empty($studentId)) {
2500
        $sql_users.= " AND u.user_id = ".intval($studentId);
2501
    }
2502
2503
    $group_id = api_get_group_id();
2504
2505
    $new_group_user_list = array();
2506
2507
    if ($group_id) {
2508
        $group_user_list = GroupManager::get_subscribed_users($group_id);
2509
        if (!empty($group_user_list)) {
2510
            foreach($group_user_list as $group_user) {
2511
                $new_group_user_list[] = $group_user['user_id'];
2512
            }
2513
        }
2514
    }
2515
2516
    $result_users = Database::query($sql_users);
2517
    $users_without_tasks = array();
2518
    while ($rowUsers = Database::fetch_array($result_users)) {
2519
        $userId = $rowUsers['user_id'];
2520
        if (in_array($userId, $users_with_tasks)) {
2521
            continue;
2522
        }
2523
2524
        if ($group_id && !in_array($userId, $new_group_user_list)) {
2525
            continue;
2526
        }
2527
2528
        if (!empty($users)) {
2529
            if (!in_array($userId, $users)) {
2530
                continue;
2531
            }
2532
        }
2533
2534
        $row_users = [];
2535
        $row_users[0] = $rowUsers['lastname'];
2536
        $row_users[1] = $rowUsers['firstname'];
2537
        $row_users[2] = Display::encrypted_mailto_link($rowUsers['email']);
2538
        $row_users[3] = $userId;
2539
        $users_without_tasks[] = $row_users;
2540
    }
2541
2542
    return $users_without_tasks;
2543
}
2544
2545
/**
2546
 * Display list of users who have not given the task
2547
 *
2548
 * @param int task id
2549
 * @param int $studentId
2550
 * @return array
2551
 * @author cvargas [email protected] cfasanando, [email protected]
2552
 * @author Julio Montoya <[email protected]> Fixes
2553
 */
2554
function display_list_users_without_publication($task_id, $studentId = null)
2555
{
2556
    global $origin;
2557
    $table_header[] = array(get_lang('LastName'), true);
2558
    $table_header[] = array(get_lang('FirstName'), true);
2559
    $table_header[] = array(get_lang('Email'), true);
2560
2561
    $data = get_list_users_without_publication($task_id);
2562
2563
    $sorting_options = array();
2564
    $sorting_options['column'] = 1;
2565
    $paging_options = array();
2566
    $my_params = array();
2567
2568
    if (isset($_GET['edit_dir'])) {
2569
        $my_params['edit_dir'] = Security::remove_XSS($_GET['edit_dir']);
2570
    }
2571
    if (isset($_GET['list'])) {
2572
        $my_params['list'] = Security::remove_XSS($_GET['list']);
2573
    }
2574
    $my_params['origin'] = $origin;
2575
    $my_params['id'] = intval($_GET['id']);
2576
2577
    //$column_show
2578
    $column_show[] = 1;
2579
    $column_show[] = 1;
2580
    $column_show[] = 1;
2581
    Display::display_sortable_config_table(
2582
        'work',
2583
        $table_header,
2584
        $data,
2585
        $sorting_options,
2586
        $paging_options,
2587
        $my_params,
2588
        $column_show
2589
    );
2590
}
2591
2592
/**
2593
 * @param int $documentId
2594
 * @param int $workId
2595
 * @param int $courseId
2596
 */
2597 View Code Duplication
function addDocumentToWork($documentId, $workId, $courseId)
2598
{
2599
    $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_DOCUMENT);
2600
    $params = array(
2601
        'document_id' => $documentId,
2602
        'work_id' => $workId,
2603
        'c_id' => $courseId
2604
    );
2605
    Database::insert($table, $params);
2606
}
2607
2608
/**
2609
 * @param int $documentId
2610
 * @param int $workId
2611
 * @param int $courseId
2612
 * @return array
2613
 */
2614 View Code Duplication
function getDocumentToWork($documentId, $workId, $courseId)
2615
{
2616
    $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_DOCUMENT);
2617
    $params = array(
2618
        'document_id = ? and work_id = ? and c_id = ?' => array($documentId, $workId, $courseId)
2619
    );
2620
    return Database::select('*', $table, array('where' => $params));
2621
}
2622
2623
/**
2624
 * @param int $documentId
2625
 * @param int $workId
2626
 * @param int $courseId
2627
 * @param int $sessionId
2628
 * @param int $userId
2629
 * @param int $active
2630
 * @return array
2631
 */
2632
function getDocumentToWorkPerUser($documentId, $workId, $courseId, $sessionId, $userId, $active = 1)
2633
{
2634
    $workRel = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_DOCUMENT);
2635
    $work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
2636
2637
    $documentId = intval($documentId);
2638
    $workId = intval($workId);
2639
    $courseId = intval($courseId);
2640
    $userId = intval($userId);
2641
    $sessionId = intval($sessionId);
2642
    $active = intval($active);
2643
2644
    $sql = "SELECT w.* FROM $work w INNER JOIN $workRel rel ON (w.parent_id = rel.work_id)
2645
            WHERE
2646
                w.document_id = $documentId AND
2647
                w.parent_id = $workId AND
2648
                w.c_id = $courseId AND
2649
                w.session_id = $sessionId AND
2650
                user_id = $userId AND
2651
                active = $active
2652
            ";
2653
2654
    $result = Database::query($sql);
2655
    $workInfo = array();
2656
    if (Database::num_rows($result)) {
2657
        $workInfo = Database::fetch_array($result, 'ASSOC');
2658
    }
2659
    return $workInfo;
2660
}
2661
2662
/**
2663
 *
2664
 * @param int $workId
2665
 * @param int $courseId
2666
 * @return array
2667
 */
2668 View Code Duplication
function getAllDocumentToWork($workId, $courseId)
2669
{
2670
    $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_DOCUMENT);
2671
    $params = array(
2672
        'work_id = ? and c_id = ?' => array($workId, $courseId)
2673
    );
2674
    return Database::select('*', $table, array('where' => $params));
2675
}
2676
2677
/**
2678
 * @param int $documentId
2679
 * @param int $workId
2680
 * @param int $courseId
2681
 */
2682 View Code Duplication
function deleteDocumentToWork($documentId, $workId, $courseId)
2683
{
2684
    $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_DOCUMENT);
2685
    $params = array(
2686
        'document_id = ? and work_id = ? and c_id = ?' => array($documentId, $workId, $courseId)
2687
    );
2688
    Database::delete($table, $params);
2689
}
2690
2691
/**
2692
 * @param int $userId
2693
 * @param int $workId
2694
 * @param int $courseId
2695
 */
2696 View Code Duplication
function addUserToWork($userId, $workId, $courseId)
2697
{
2698
    $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_USER);
2699
    $params = array(
2700
        'user_id' => $userId,
2701
        'work_id' => $workId,
2702
        'c_id' => $courseId
2703
    );
2704
    Database::insert($table, $params);
2705
}
2706
2707
/**
2708
 * @param int $userId
2709
 * @param int $workId
2710
 * @param int $courseId
2711
 * @return array
2712
 */
2713 View Code Duplication
function getUserToWork($userId, $workId, $courseId)
2714
{
2715
    $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_USER);
2716
    $params = array(
2717
        'user_id = ? and work_id = ? and c_id = ?' => array($userId, $workId, $courseId)
2718
    );
2719
    return Database::select('*', $table, array('where' => $params));
2720
}
2721
2722
/**
2723
 * @param int $workId
2724
 * @param int $courseId
2725
 * @param bool $getCount
2726
 * @return array
2727
 */
2728
function getAllUserToWork($workId, $courseId, $getCount = false)
2729
{
2730
    $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_USER);
2731
    $params = array(
2732
        'work_id = ? and c_id = ?' => array($workId, $courseId)
2733
    );
2734
    if ($getCount) {
2735
        $count = 0;
2736
        $result = Database::select(
2737
            'count(user_id) as count',
2738
            $table,
2739
            array('where' => $params),
2740
            'simple'
2741
        );
2742
        if (!empty($result)) {
2743
            $count = intval($result['count']);
2744
        }
2745
        return $count;
2746
    } else {
2747
        return Database::select('*', $table, array('where' => $params));
2748
    }
2749
}
2750
2751
/**
2752
 * @param int $userId
2753
 * @param int $workId
2754
 * @param int $courseId
2755
 */
2756 View Code Duplication
function deleteUserToWork($userId, $workId, $courseId)
2757
{
2758
    $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_USER);
2759
    $params = array(
2760
        'user_id = ? and work_id = ? and c_id = ?' => array($userId, $workId, $courseId)
2761
    );
2762
    Database::delete($table, $params);
2763
}
2764
2765
/**
2766
 * @param int $userId
2767
 * @param int $workId
2768
 * @param int $courseId
2769
 * @return bool
2770
 */
2771
function userIsSubscribedToWork($userId, $workId, $courseId)
2772
{
2773
    $subscribedUsers = getAllUserToWork($workId, $courseId);
2774
2775
    if (empty($subscribedUsers)) {
2776
        return true;
2777
    } else {
2778
        $subscribedUsersList = array();
2779
        foreach ($subscribedUsers as $item) {
2780
            $subscribedUsersList[] = $item['user_id'];
2781
        }
2782
        if (in_array($userId, $subscribedUsersList)) {
2783
            return true;
2784
        }
2785
    }
2786
    return false;
2787
}
2788
2789
/**
2790
 * Get the list of students that have to submit their work
2791
 * @param integer $workId The internal ID of the assignment
2792
 * @param integer $courseId The course ID
2793
 * @param integer $groupId The group ID, if any
2794
 * @param integer $sessionId The session ID, if any
2795
 * @param bool $getCount Whether we want just the amount or the full result
2796
 * @return array|int An integer (if we just asked for the count) or an array of users
2797
 */
2798
function getStudentSubscribedToWork(
2799
    $workId,
2800
    $courseId,
2801
    $groupId = null,
2802
    $sessionId = null,
2803
    $getCount = false
2804
) {
2805
    $usersInWork = null;
2806
    $usersInCourse = null;
2807
2808
    if (empty($groupId)) {
2809
        $courseInfo = api_get_course_info_by_id($courseId);
2810
        $status = STUDENT;
2811
        if (!empty($sessionId)) {
2812
            $status = 0;
2813
        }
2814
        $usersInCourse = CourseManager::get_user_list_from_course_code(
2815
            $courseInfo['code'],
2816
            $sessionId,
2817
            null,
2818
            null,
2819
            $status,
2820
            $getCount
2821
        );
2822
    } else {
2823
        $usersInCourse = GroupManager::get_users(
2824
            $groupId,
2825
            false,
2826
            null,
2827
            null,
2828
            $getCount,
2829
            $courseId
2830
        );
2831
    }
2832
2833
    $usersInWork = getAllUserToWork($workId, $courseId, $getCount);
2834
2835
    if (empty($usersInWork)) {
2836
        return $usersInCourse;
2837
    } else {
2838
        return $usersInWork;
2839
    }
2840
2841
}
2842
2843
/**
2844
 * @param int $userId
2845
 * @param int $workId
2846
 * @param int $courseId
2847
 * @return bool
2848
 */
2849
function allowOnlySubscribedUser($userId, $workId, $courseId)
2850
{
2851
    if (api_is_platform_admin() || api_is_allowed_to_edit()) {
2852
        return true;
2853
    }
2854
2855
    if (userIsSubscribedToWork($userId, $workId, $courseId) == false) {
2856
        api_not_allowed(true);
2857
    }
2858
}
2859
2860
/**
2861
 * @param int $workId
2862
 * @param array $courseInfo
2863
 * @param int $documentId
2864
 * @return array
2865
 */
2866
function getDocumentTemplateFromWork($workId, $courseInfo, $documentId)
2867
{
2868
    $documents = getAllDocumentToWork($workId, $courseInfo['real_id']);
2869
    if (!empty($documents)) {
2870
        foreach ($documents as $doc) {
2871
            if ($documentId != $doc['document_id']) {
2872
                continue;
2873
            }
2874
            $docData = DocumentManager::get_document_data_by_id($doc['document_id'], $courseInfo['code']);
2875
            $fileInfo = pathinfo($docData['path']);
2876
            if ($fileInfo['extension'] == 'html') {
2877
                if (file_exists($docData['absolute_path']) && is_file($docData['absolute_path'])) {
2878
                    $docData['file_content'] = file_get_contents($docData['absolute_path']);
2879
                    return $docData;
2880
                }
2881
            }
2882
        }
2883
    }
2884
    return array();
2885
}
2886
2887
/**
2888
 * @param int $workId
2889
 * @param array $courseInfo
2890
 * @return string
2891
 */
2892
function getAllDocumentsFromWorkToString($workId, $courseInfo)
2893
{
2894
    $documents = getAllDocumentToWork($workId, $courseInfo['real_id']);
2895
    $content = null;
2896
    if (!empty($documents)) {
2897
        $content .= '<ul class="nav nav-list well">';
2898
        $content .= '<li class="nav-header">'.get_lang('Documents').'</li>';
2899
        foreach ($documents as $doc) {
2900
            $docData = DocumentManager::get_document_data_by_id($doc['document_id'], $courseInfo['code']);
2901
            if ($docData) {
2902
                $content .= '<li><a target="_blank" href="'.$docData['url'].'">'.$docData['title'].'</a></li>';
2903
            }
2904
        }
2905
        $content .= '</ul><br />';
2906
    }
2907
    return $content;
2908
}
2909
2910
/**
2911
 * Returns fck editor toolbar
2912
 * @return array
2913
 */
2914
function getWorkDescriptionToolbar()
2915
{
2916
    return array(
2917
        'ToolbarStartExpanded' => 'true',
2918
        'ToolbarSet' => 'Work',
2919
        'Width' => '100%',
2920
        'Height' => '400'
2921
    );
2922
}
2923
2924
/**
2925
 * @param array $work
2926
 * @return array
2927
 */
2928
function getWorkComments($work)
2929
{
2930
    $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
2931
    $userTable= Database::get_main_table(TABLE_MAIN_USER);
2932
2933
    $courseId = intval($work['c_id']);
2934
    $workId = intval($work['id']);
2935
2936
    if (empty($courseId) || empty($workId)) {
2937
        return array();
2938
    }
2939
2940
    $sql = "SELECT
2941
            c.id, c.user_id, u.firstname, u.lastname, u.username, u.picture_uri
2942
            FROM $commentTable c
2943
            INNER JOIN $userTable u ON (u.user_id = c.user_id)
2944
            WHERE c_id = $courseId AND work_id = $workId
2945
            ORDER BY sent_at
2946
            ";
2947
    $result = Database::query($sql);
2948
    $comments = Database::store_result($result, 'ASSOC');
2949
    if (!empty($comments)) {
2950
        foreach ($comments as &$comment) {
2951
            $comment['picture'] = UserManager::getUserPicture($comment['user_id']);
2952
            $commentInfo = getWorkComment($comment['id']);
2953
2954
            if (!empty($commentInfo)) {
2955
                $comment = array_merge($comment, $commentInfo);
2956
            }
2957
        }
2958
    }
2959
    return $comments;
2960
}
2961
2962
/**
2963
 * Get total score from a work list
2964
 * @param $workList
2965
 * @return int|null
2966
 */
2967
function getTotalWorkScore($workList)
2968
{
2969
    $count = 0;
2970
    foreach ($workList as $data) {
2971
        $count += $data['qualification_score'];
2972
    }
2973
    return $count;
2974
}
2975
2976
2977
/**
2978
 * Get comment count from a work list (docs sent by students)
2979
 * @param array $workList
2980
 * @param array $courseInfo
2981
 * @return int|null
2982
 */
2983
function getTotalWorkComment($workList, $courseInfo = array())
2984
{
2985
    if (empty($courseInfo)) {
2986
        $courseInfo = api_get_course_info();
2987
    }
2988
2989
    $count = 0;
2990
    foreach ($workList as $data) {
2991
        $count += getWorkCommentCount($data['id'], $courseInfo);
2992
    }
2993
    return $count;
2994
}
2995
2996
/**
2997
 * Get comment count for a specific work sent by a student.
2998
 * @param int $id
2999
 * @param array $courseInfo
3000
 * @return int
3001
 */
3002 View Code Duplication
function getWorkCommentCount($id, $courseInfo = array())
3003
{
3004
    if (empty($courseInfo)) {
3005
        $courseInfo = api_get_course_info();
3006
    }
3007
3008
    $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
3009
    $id = intval($id);
3010
3011
    $sql = "SELECT count(*) as count
3012
            FROM $commentTable
3013
            WHERE work_id = $id AND c_id = ".$courseInfo['real_id'];
3014
3015
    $result = Database::query($sql);
3016
    if (Database::num_rows($result)) {
3017
        $comment = Database::fetch_array($result);
3018
        return $comment['count'];
3019
    }
3020
3021
    return 0;
3022
}
3023
3024
/**
3025
 * Get comment count for a specific parent
3026
 * @param int $parentId
3027
 * @param array $courseInfo
3028
 * @param int $sessionId
3029
 * @return int
3030
 */
3031 View Code Duplication
function getWorkCommentCountFromParent(
3032
    $parentId,
3033
    $courseInfo = array(),
3034
    $sessionId = 0
3035
) {
3036
    if (empty($courseInfo)) {
3037
        $courseInfo = api_get_course_info();
3038
    }
3039
3040
    if (empty($sessionId)) {
3041
        $sessionId = api_get_session_id();
3042
    } else {
3043
        $sessionId = intval($sessionId);
3044
    }
3045
3046
    $work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
3047
    $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
3048
    $parentId = intval($parentId);
3049
3050
    $sql = "SELECT count(*) as count
3051
            FROM $commentTable c INNER JOIN $work w
3052
            ON c.c_id = w.c_id AND w.id = c.work_id
3053
            WHERE
3054
                session_id = $sessionId AND
3055
                parent_id = $parentId AND
3056
                w.c_id = ".$courseInfo['real_id'];
3057
3058
    $result = Database::query($sql);
3059
    if (Database::num_rows($result)) {
3060
        $comment = Database::fetch_array($result);
3061
        return $comment['count'];
3062
    }
3063
3064
    return 0;
3065
}
3066
3067
/**
3068
 * Get last work information from parent
3069
 * @param int $parentId
3070
 * @param array $courseInfo
3071
 * @param int $sessionId
3072
 * @return int
3073
 */
3074 View Code Duplication
function getLastWorkStudentFromParent(
3075
    $parentId,
3076
    $courseInfo = array(),
3077
    $sessionId = 0
3078
) {
3079
    if (empty($courseInfo)) {
3080
        $courseInfo = api_get_course_info();
3081
    }
3082
3083
    if (empty($sessionId)) {
3084
        $sessionId = api_get_session_id();
3085
    } else {
3086
        $sessionId = intval($sessionId);
3087
    }
3088
3089
    $work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
3090
    $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
3091
    $parentId = intval($parentId);
3092
3093
    $sql = "SELECT w.*
3094
            FROM $commentTable c INNER JOIN $work w
3095
            ON c.c_id = w.c_id AND w.id = c.work_id
3096
            WHERE
3097
                session_id = $sessionId AND
3098
                parent_id = $parentId AND
3099
                w.c_id = ".$courseInfo['real_id']."
3100
            ORDER BY w.sent_date
3101
            LIMIT 1
3102
            ";
3103
3104
    $result = Database::query($sql);
3105
    if (Database::num_rows($result)) {
3106
        $comment = Database::fetch_array($result, 'ASSOC');
3107
        /*if (!empty($comment)) {
3108
            $comment['assignment'] = get_work_assignment_by_id(
3109
                $comment['id'],
3110
                $courseInfo['real_id']
3111
            );
3112
        }*/
3113
        return $comment;
3114
    }
3115
3116
    return array();
3117
}
3118
3119
/**
3120
 * Get last work information from parent
3121
 * @param int $parentId
3122
 * @param array $courseInfo
3123
 * @param int $sessionId
3124
 * @return int
3125
 */
3126 View Code Duplication
function getLastWorkStudentFromParentByUser(
3127
    $userId,
3128
    $parentId,
3129
    $courseInfo = array(),
3130
    $sessionId = 0
3131
) {
3132
    if (empty($courseInfo)) {
3133
        $courseInfo = api_get_course_info();
3134
    }
3135
3136
    if (empty($sessionId)) {
3137
        $sessionId = api_get_session_id();
3138
    } else {
3139
        $sessionId = intval($sessionId);
3140
    }
3141
3142
    $userId = intval($userId);
3143
    $work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
3144
    $parentId = intval($parentId);
3145
3146
    $sql = "SELECT *
3147
            FROM  $work
3148
            WHERE
3149
                user_id = $userId AND
3150
                session_id = $sessionId AND
3151
                parent_id = $parentId AND
3152
                c_id = ".$courseInfo['real_id']."
3153
            ORDER BY sent_date DESC
3154
            LIMIT 1
3155
            ";
3156
    $result = Database::query($sql);
3157
    if (Database::num_rows($result)) {
3158
        $work = Database::fetch_array($result, 'ASSOC');
3159
        /*if (!empty($comment)) {
3160
            $comment['assignment'] = get_work_assignment_by_id(
3161
                $comment['id'],
3162
                $courseInfo['real_id']
3163
            );
3164
        }*/
3165
        return $work;
3166
    }
3167
3168
    return array();
3169
}
3170
3171
/**
3172
 * @param int $id comment id
3173
 * @param array $courseInfo
3174
 * @return string
3175
 */
3176
function getWorkComment($id, $courseInfo = array())
3177
{
3178
    if (empty($courseInfo)) {
3179
        $courseInfo = api_get_course_info();
3180
    }
3181
3182
    if (empty($courseInfo['real_id'])) {
3183
        return array();
3184
    }
3185
3186
    $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
3187
    $id = intval($id);
3188
3189
    $sql = "SELECT * FROM $commentTable
3190
            WHERE id = $id AND c_id = ".$courseInfo['real_id'];
3191
    $result = Database::query($sql);
3192
    $comment = array();
3193
    if (Database::num_rows($result)) {
3194
        $comment = Database::fetch_array($result, 'ASSOC');
3195
        $filePath = null;
3196
        $fileUrl = null;
3197
        $deleteUrl = null;
3198
        $fileName = null;
3199
        if (!empty($comment['file'])) {
3200
            $work = get_work_data_by_id($comment['work_id']);
3201
            $workParent = get_work_data_by_id($work['parent_id']);
3202
            $filePath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/work/'.$workParent['url'].'/'.$comment['file'];
3203
            $fileUrl = api_get_path(WEB_CODE_PATH).'work/download_comment_file.php?comment_id='.$id.'&'.api_get_cidreq();
3204
            $deleteUrl = api_get_path(WEB_CODE_PATH).'work/view.php?'.api_get_cidreq().'&id='.$comment['work_id'].'&action=delete_attachment&comment_id='.$id;
3205
            $fileParts = explode('_', $comment['file']);
3206
            $fileName = str_replace($fileParts[0].'_'.$fileParts[1].'_', '', $comment['file']);
3207
        }
3208
        $comment['delete_file_url'] = $deleteUrl;
3209
        $comment['file_path'] = $filePath;
3210
        $comment['file_url'] = $fileUrl;
3211
        $comment['file_name_to_show'] = $fileName;
3212
    }
3213
3214
    return $comment;
3215
}
3216
3217
/**
3218
 * @param int $id
3219
 * @param array $courseInfo
3220
 */
3221
function deleteCommentFile($id, $courseInfo = array())
3222
{
3223
    $workComment = getWorkComment($id, $courseInfo);
3224
    if (isset($workComment['file']) && !empty($workComment['file'])) {
3225
        if (file_exists($workComment['file_path'])) {
3226
            $result = my_delete($workComment['file_path']);
3227
            if ($result) {
3228
                $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
3229
                $params = array('file' => '');
3230
                Database::update(
3231
                    $commentTable,
3232
                    $params,
3233
                    array('id = ? AND c_id = ? ' => array($workComment['id'], $workComment['c_id']))
3234
                );
3235
            }
3236
        }
3237
    }
3238
}
3239
3240
/**
3241
 * Adds a comments to the work document
3242
 * @param array $courseInfo
3243
 * @param int $userId
3244
 * @param array $work
3245
 * @param array $data
3246
 * @return int
3247
 */
3248
function addWorkComment($courseInfo, $userId, $parentWork, $work, $data)
3249
{
3250
    $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
3251
3252
    $params = array(
3253
        'work_id' => $work['id'],
3254
        'c_id' => $work['c_id'],
3255
        'user_id' => $userId,
3256
        'comment' => $data['comment'],
3257
        'sent_at' => api_get_utc_datetime()
3258
    );
3259
3260
    $commentId = Database::insert($commentTable, $params);
3261
3262
    if ($commentId) {
3263
        $sql = "UPDATE $commentTable SET id = iid WHERE iid = $commentId";
3264
        Database::query($sql);
3265
    }
3266
3267
    $userIdListToSend = array();
3268
3269
    if (api_is_allowed_to_edit()) {
3270
        if (isset($data['send_mail']) && $data['send_mail']) {
3271
            // Teacher sends a feedback
3272
            $userIdListToSend = array($work['user_id']);
3273
        }
3274
    } else {
3275
        $sessionId = api_get_session_id();
3276
        if (empty($sessionId)) {
3277
            $teachers = CourseManager::get_teacher_list_from_course_code(
3278
                $courseInfo['code']
3279
            );
3280
            if (!empty($teachers)) {
3281
                $userIdListToSend = array_keys($teachers);
3282
            }
3283
        } else {
3284
            $teachers = SessionManager::getCoachesByCourseSession(
3285
                $sessionId,
3286
                $courseInfo['code']
3287
            );
3288
3289
            if (!empty($teachers)) {
3290
                $userIdListToSend = array_values($teachers);
3291
            }
3292
        }
3293
    }
3294
3295
    $url = api_get_path(WEB_CODE_PATH).'work/view.php?'.api_get_cidreq().'&id='.$work['id'];
3296
    $subject = sprintf(get_lang('ThereIsANewWorkFeedback'), $parentWork['title']);
3297
    $content = sprintf(get_lang('ThereIsANewWorkFeedbackInWorkXHere'), $work['title'], $url);
3298
3299
    if (!empty($userIdListToSend)) {
3300
        foreach ($userIdListToSend as $userId) {
3301
            MessageManager::send_message_simple(
3302
                $userId,
3303
                $subject,
3304
                $content
3305
            );
3306
        }
3307
    }
3308
3309
    $fileData = isset($data['file']) ? $data['file'] : null;
3310
    if (!empty($commentId) && !empty($fileData)) {
3311
        $workParent = get_work_data_by_id($work['parent_id']);
3312
        if (!empty($workParent)) {
3313
            $uploadDir = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/work'.$workParent['url'];
3314
            $newFileName = 'comment_'.$commentId.'_'.php2phps(
3315
                    api_replace_dangerous_char($fileData['name'])
3316
                );
3317
            $newFilePath = $uploadDir.'/'.$newFileName;
3318
            $result = move_uploaded_file($fileData['tmp_name'], $newFilePath);
3319
            if ($result) {
3320
                $params = array('file' => $newFileName);
3321
                Database::update(
3322
                    $commentTable,
3323
                    $params,
3324
                    array('id = ? AND c_id = ? ' => array($commentId, $work['c_id']))
3325
                );
3326
            }
3327
        }
3328
    }
3329
}
3330
3331
/**
3332
 * @param array $work
3333
 * @param string $page
3334
 * @return string
3335
 */
3336
function getWorkCommentForm($work, $page = 'view')
3337
{
3338
    $url = api_get_path(WEB_CODE_PATH).'work/view.php?id='.$work['id'].'&action=send_comment&'.api_get_cidreq().'&page='.$page;
3339
    $form = new FormValidator(
3340
        'work_comment',
3341
        'post',
3342
        $url
3343
    );
3344
3345
    $form->addElement('file', 'file', get_lang('Attachment'));
3346
    $form->addHtmlEditor('comment', get_lang('Comment'));
3347
    $form->addElement('hidden', 'id', $work['id']);
3348
    $form->addElement('hidden', 'page', $page);
3349
    if (api_is_allowed_to_edit()) {
3350
        $form->addElement('checkbox', 'send_mail', null, get_lang('SendMail'));
3351
    }
3352
    $form->addButtonSend(get_lang('Send'), 'button');
3353
3354
    return $form->returnForm();
3355
}
3356
3357
/**
3358
 * @param array $homework result of get_work_assignment_by_id()
3359
 * @return string
3360
 */
3361
function getWorkDateValidationStatus($homework)
3362
{
3363
    $message = null;
3364
    $has_expired = false;
3365
    $has_ended = false;
3366
3367
    if (!empty($homework)) {
3368
3369 View Code Duplication
        if (!empty($homework['expires_on']) || !empty($homework['ends_on'])) {
3370
            $time_now = time();
3371
3372
            if (!empty($homework['expires_on'])) {
3373
                $time_expires   = api_strtotime($homework['expires_on'], 'UTC');
3374
                $difference     = $time_expires - $time_now;
3375
                if ($difference < 0) {
3376
                    $has_expired = true;
3377
                }
3378
            }
3379
3380
            if (empty($homework['expires_on'])) {
3381
                $has_expired = false;
3382
            }
3383
3384
            if (!empty($homework['ends_on'])) {
3385
                $time_ends = api_strtotime($homework['ends_on'], 'UTC');
3386
                $difference2 = $time_ends - $time_now;
3387
                if ($difference2 < 0) {
3388
                    $has_ended = true;
3389
                }
3390
            }
3391
3392
            $ends_on = api_convert_and_format_date($homework['ends_on']);
3393
            $expires_on = api_convert_and_format_date($homework['expires_on']);
3394
        }
3395
3396
        if ($has_ended) {
3397
            $message = Display::return_message(get_lang('EndDateAlreadyPassed').' '.$ends_on, 'error');
3398
        } elseif ($has_expired) {
3399
            $message = Display::return_message(get_lang('ExpiryDateAlreadyPassed').' '.$expires_on, 'warning');
3400
        } else {
3401
            if ($has_expired) {
3402
                $message = Display::return_message(get_lang('ExpiryDateToSendWorkIs').' '.$expires_on);
3403
            }
3404
        }
3405
    }
3406
3407
    return array(
3408
        'message' => $message,
3409
        'has_ended' => $has_ended,
3410
        'has_expired' => $has_expired
3411
    );
3412
}
3413
3414
/**
3415
 * @param FormValidator $form
3416
 * @param int $uploadFormType
3417
 */
3418
function setWorkUploadForm($form, $uploadFormType = 0)
3419
{
3420
    $form->addElement('header', get_lang('UploadADocument'));
3421
    $form->addElement('hidden', 'contains_file', 0, array('id'=>'contains_file_id'));
3422
    $form->addElement('hidden', 'active', 1);
3423
    $form->addElement('hidden', 'accepted', 1);
3424
    $form->addElement('text', 'title', get_lang('Title'), array('id' => 'file_upload'));
3425
    $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
3426
3427
    switch ($uploadFormType) {
3428
        case 0:
3429
            // File and text.
3430
            $form->addElement('file', 'file', get_lang('UploadADocument'), 'size="40" onchange="updateDocumentTitle(this.value)"');
3431
            $form->add_real_progress_bar('uploadWork', 'file');
3432
            $form->addHtmlEditor('description', get_lang('Description'), false, false, getWorkDescriptionToolbar());
3433
            break;
3434
        case 1:
3435
            // Only text.
3436
            $form->addHtmlEditor('description', get_lang('Description'), false, false, getWorkDescriptionToolbar());
3437
            $form->addRule('description', get_lang('ThisFieldIsRequired'), 'required');
3438
            break;
3439
        case 2:
3440
            // Only file.
3441
            $form->addElement('file', 'file', get_lang('UploadADocument'), 'size="40" onchange="updateDocumentTitle(this.value)"');
3442
            $form->add_real_progress_bar('uploadWork', 'file');
3443
            $form->addRule('file', get_lang('ThisFieldIsRequired'), 'required');
3444
            break;
3445
    }
3446
3447
    $form->addButtonUpload(get_lang('Upload'), 'submitWork');
3448
}
3449
3450
/**
3451
 * @param array $my_folder_data
3452
 * @param array $_course
3453
 * @param bool $isCorrection
3454
 * @param array $workInfo
3455
 * @param array $file
3456
 *
3457
 * @return array
3458
 */
3459
function uploadWork($my_folder_data, $_course, $isCorrection = false, $workInfo = [], $file = [])
3460
{
3461
    if (isset($_FILES['file']) && !empty($_FILES['file'])) {
3462
        $file = $_FILES['file'];
3463
    }
3464
3465
    if (empty($file['size'])) {
3466
        return array('error' => Display :: return_message(get_lang('UplUploadFailedSizeIsZero'), 'error'));
3467
    }
3468
    $updir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/work/'; //directory path to upload
3469
3470
    // Try to add an extension to the file if it has'nt one
3471
    $filename = add_ext_on_mime(stripslashes($file['name']), $file['type']);
3472
3473
    // Replace dangerous characters
3474
    $filename = api_replace_dangerous_char($filename);
3475
3476
    // Transform any .php file in .phps fo security
3477
    $filename = php2phps($filename);
3478
    $filesize = filesize($file['tmp_name']);
3479
3480
    if (empty($filesize)) {
3481
        return array(
3482
            'error' => Display:: return_message(
3483
                get_lang('UplUploadFailedSizeIsZero'),
3484
                'error'
3485
            ),
3486
        );
3487
    } elseif (!filter_extension($new_file_name)) {
3488
        return array(
3489
            'error' => Display:: return_message(
3490
                get_lang('UplUnableToSaveFileFilteredExtension'),
3491
                'error'
3492
            ),
3493
        );
3494
    }
3495
3496
    $totalSpace = DocumentManager::documents_total_space($_course['real_id']);
3497
    $course_max_space = DocumentManager::get_course_quota($_course['code']);
3498
    $total_size = $filesize + $totalSpace;
3499
3500
    if ($total_size > $course_max_space) {
3501
        return array(
3502
            'error' => Display :: return_message(get_lang('NoSpace'), 'error')
3503
        );
3504
    }
3505
3506
    // Compose a unique file name to avoid any conflict
3507
    $new_file_name = api_get_unique_id();
3508
3509
    if ($isCorrection) {
3510
        if (!empty($workInfo['url'])) {
3511
            $new_file_name = basename($workInfo['url']).'_correction';
3512
        } else {
3513
            $new_file_name = $new_file_name.'_correction';
3514
        }
3515
    }
3516
3517
    $curdirpath = basename($my_folder_data['url']);
3518
3519
    // If we come from the group tools the groupid will be saved in $work_table
3520
    if (is_dir($updir.$curdirpath) || empty($curdirpath)) {
3521
        $result = move_uploaded_file(
3522
            $file['tmp_name'],
3523
            $updir.$curdirpath.'/'.$new_file_name
3524
        );
3525
    } else {
3526
        return array(
3527
            'error' => Display :: return_message(
3528
                get_lang('FolderDoesntExistsInFileSystem'),
3529
                'error'
3530
            )
3531
        );
3532
    }
3533
3534
    $url = null;
3535
    if ($result) {
3536
        $url = 'work/'.$curdirpath.'/'.$new_file_name;
3537
    }
3538
3539
    return array(
3540
        'url' => $url,
3541
        'filename' => $filename,
3542
        'filesize' => $filesize,
3543
        'error' => null
3544
    );
3545
}
3546
3547
/**
3548
 * Send an e-mail to users related to this work (course teachers, usually, but
3549
 * might include other group members)
3550
 * @param int $workId
3551
 * @param array $courseInfo
3552
 * @param int $session_id
3553
 */
3554
function sendAlertToUsers($workId, $courseInfo, $session_id)
3555
{
3556
    $user_list = array();
3557
    //$workData = get_work_assignment_by_id($workId, $courseInfo['real_id']);
3558
    $workData = get_work_data_by_id($workId, $courseInfo['real_id'], $session_id);
3559
    //last value is to check this is not "just" an edit
3560
    //YW Tis part serve to send a e-mail to the tutors when a new file is sent
3561
    $send = api_get_course_setting('email_alert_manager_on_new_doc');
3562
3563
    if ($send == SEND_EMAIL_EVERYONE || $send == SEND_EMAIL_TEACHERS) {
3564
        // Lets predefine some variables. Be sure to change the from address!
3565
        if (empty($session_id)) {
3566
            //Teachers
3567
            $user_list = CourseManager::get_user_list_from_course_code(
3568
                api_get_course_id(),
3569
                null,
3570
                null,
3571
                null,
3572
                COURSEMANAGER
3573
            );
3574
        } else {
3575
            // Coaches
3576
            $user_list = CourseManager::get_user_list_from_course_code(
3577
                api_get_course_id(),
3578
                $session_id,
3579
                null,
3580
                null,
3581
                2
3582
            );
3583
        }
3584
    }
3585
3586
    if ($send == SEND_EMAIL_EVERYONE || $send == SEND_EMAIL_STUDENTS) {
3587
        if (!$session_id) {
3588
            $session_id = null;
3589
        }
3590
        $student = CourseManager::get_user_list_from_course_code(
3591
            api_get_course_id(),
3592
            $session_id,
3593
            null,
3594
            null,
3595
            STUDENT,
3596
            null,
3597
            null,
3598
            null,
3599
            null,
3600
            null,
3601
            array(api_get_user_id())
3602
        );
3603
        $user_list = array_merge($user_list, $student);
3604
    }
3605
3606
    if ($send) {
3607
        $senderEmail = api_get_setting('emailAdministrator');
3608
        $senderName = api_get_person_name(
3609
            api_get_setting('administratorName'),
3610
            api_get_setting('administratorSurname'),
3611
            null,
3612
            PERSON_NAME_EMAIL_ADDRESS
3613
        );
3614
        $subject = "[" . api_get_setting('siteName') . "] ".get_lang('SendMailBody')."\n ".get_lang('CourseName').": ".$courseInfo['name']."  ";
3615
        foreach ($user_list as $user_data) {
0 ignored issues
show
Bug introduced by
The expression $user_list of type array|integer is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
3616
            $to_user_id = $user_data['user_id'];
3617
            $user_info = api_get_user_info($to_user_id);
3618
            $message = get_lang('SendMailBody')."\n".get_lang('CourseName')." : ".$courseInfo['name']."\n";
3619
            $message .= get_lang('UserName')." : ".api_get_person_name($user_info['firstname'], $user_info['lastname'])."\n";
3620
            $message .= get_lang('DateSent')." : ".api_format_date(api_get_local_time())."\n";
3621
            $url = api_get_path(WEB_CODE_PATH)."work/work.php?cidReq=".$courseInfo['code']."&id_session=".$session_id."&id=".$workData['id'];
3622
            $message .= get_lang('WorkName')." : ".$workData['title']."\n\n".'<a href="'.$url.'">'.get_lang('DownloadLink')."</a>\n";
3623
            //$message .= $url;
3624
            MessageManager::send_message_simple($to_user_id, $subject, $message);
3625
            api_mail_html(
3626
                api_get_person_name(
3627
                    $user_info['firstname'].' '.$user_info['lastname'],
3628
                    null,
3629
                    PERSON_NAME_EMAIL_ADDRESS
3630
                ),
3631
                $user_info['email'],
3632
                $subject,
3633
                $message,
3634
                $senderName,
3635
                $senderEmail
3636
            );
3637
        }
3638
    }
3639
}
3640
3641
/**
3642
 * Check if the current uploaded work filename already exists in the current assement
3643
 *
3644
 * @param $filename
3645
 * @param $workId
3646
 * @return mixed
3647
 */
3648 View Code Duplication
function checkExistingWorkFileName($filename, $workId)
3649
{
3650
    $work_table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
3651
    $filename = Database::escape_string($filename);
3652
    $sql = "SELECT title FROM $work_table
3653
                        WHERE parent_id = $workId AND title = '$filename'";
3654
    $result = Database::query($sql);
3655
    return Database::fetch_assoc($result);
3656
}
3657
3658
/**
3659
 * @param array $workInfo
3660
 * @param array $values
3661
 * @param array $courseInfo
3662
 * @param int $sessionId
3663
 * @param int $groupId
3664
 * @param int $userId
3665
 * @param array $file
3666
 * @param bool  $checkDuplicated
3667
 *
3668
 * @return null|string
3669
 */
3670
function processWorkForm($workInfo, $values, $courseInfo, $sessionId, $groupId, $userId, $file = [], $checkDuplicated = false)
3671
{
3672
    $work_table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
3673
3674
    $courseId = $courseInfo['real_id'];
3675
    $groupId = intval($groupId);
3676
    $sessionId = intval($sessionId);
3677
    $userId = intval($userId);
3678
3679
    $title = $values['title'];
3680
    $description = $values['description'];
3681
    $contains_file = isset($values['contains_file']) && !empty($values['contains_file']) ? intval($values['contains_file']): 0;
3682
3683
    $saveWork = true;
3684
    $message = null;
3685
    $filename = null;
3686
    $url = null;
3687
    $filesize = null;
3688
    $workData = [];
3689
3690
    if ($values['contains_file']) {
3691
        if ($checkDuplicated) {
3692
            if (checkExistingWorkFileName($file['name'], $workInfo['id'])) {
3693
                $saveWork = false;
3694
                $workData['error'] = get_lang('YouAlreadySentThisFile');
3695
            } else {
3696
                $result = uploadWork($workInfo, $courseInfo, false, [], $file);
3697
            }
3698
        } else {
3699
            $result = uploadWork($workInfo, $courseInfo, false, [], $file);
3700
        }
3701
3702
        if (isset($result['error'])) {
3703
            $message = $result['error'];
3704
            $saveWork = false;
3705
        }
3706
        $filename = isset($result['filename']) ? $result['filename'] : null;
3707
        if (empty($title)) {
3708
            $title = isset($result['title']) && !empty($result['title']) ? $result['title'] : get_lang('Untitled');
3709
        }
3710
3711
        $filesize = isset($result['filesize']) ? $result['filesize'] : null;
3712
        $url = isset($result['url']) ? $result['url'] : null;
3713
    }
3714
3715
    if (empty($title)) {
3716
        $title = get_lang('Untitled');
3717
    }
3718
3719
    if ($saveWork) {
3720
        $active = '1';
3721
        $params = [
3722
            'c_id' => $courseId,
3723
            'url' => $url,
3724
            'filetype' => 'file',
3725
            'title' => $title,
3726
            'description' => $description,
3727
            'contains_file' => $contains_file,
3728
            'active' => $active,
3729
            'accepted' => '1',
3730
            'qualificator_id' => 0,
3731
            'document_id' => 0,
3732
            'weight' => 0,
3733
            'allow_text_assignment' => 0,
3734
            'post_group_id' => $groupId,
3735
            'sent_date' => api_get_utc_datetime(),
3736
            'parent_id' => $workInfo['id'],
3737
            'session_id' => $sessionId,
3738
            'user_id' => $userId,
3739
            'has_properties' => 0,
3740
            'qualification' => 0
3741
3742
            //'filesize' => $filesize
3743
        ];
3744
        $workId = Database::insert($work_table, $params);
3745
3746
        if ($workId) {
3747
            $sql = "UPDATE $work_table SET id = iid WHERE iid = $workId ";
3748
            Database::query($sql);
3749
3750
            if (array_key_exists('filename', $workInfo) && !empty($filename)) {
3751
                $filename = Database::escape_string($filename);
3752
                $sql = "UPDATE $work_table SET
3753
                            filename = '$filename'
3754
                        WHERE iid = $workId";
3755
                Database::query($sql);
3756
            }
3757
3758
            if (array_key_exists('document_id', $workInfo)) {
3759
                $documentId = isset($values['document_id']) ? intval($values['document_id']) : 0;
3760
                $sql = "UPDATE $work_table SET
3761
                            document_id = '$documentId'
3762
                        WHERE iid = $workId";
3763
                Database::query($sql);
3764
            }
3765
            api_item_property_update(
3766
                $courseInfo,
3767
                'work',
3768
                $workId,
3769
                'DocumentAdded',
3770
                $userId,
3771
                $groupId
3772
            );
3773
            sendAlertToUsers($workId, $courseInfo, $sessionId);
3774
            Event::event_upload($workId);
3775
            $workData = get_work_data_by_id($workId);
3776
            Display::addFlash(Display::return_message(get_lang('DocAdd')));
3777
        }
3778
    } else {
3779
        Display::addFlash(Display::return_message(get_lang('IsNotPosibleSaveTheDocument'), 'error'));
3780
    }
3781
3782
    return $workData;
3783
}
3784
3785
/**
3786
 * Creates a new task (directory) in the assignment tool
3787
 * @param array $params
0 ignored issues
show
Bug introduced by
There is no parameter named $params. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
3788
 * @param int $user_id
3789
 * @param array $courseInfo
3790
 * @param int $group_id
3791
 * @param int $session_id
3792
 * @return bool|int
3793
 * @note $params can have the following elements, but should at least have the 2 first ones: (
3794
 *       'new_dir' => 'some-name',
3795
 *       'description' => 'some-desc',
3796
 *       'qualification' => 20 (e.g. 20),
3797
 *       'weight' => 50 (percentage) to add to gradebook (e.g. 50),
3798
 *       'allow_text_assignment' => 0/1/2,
3799
 * @todo Rename createAssignment or createWork, or something like that
3800
 */
3801
function addDir($formValues, $user_id, $courseInfo, $group_id, $session_id)
3802
{
3803
    $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
3804
3805
    $user_id = intval($user_id);
3806
    $group_id = intval($group_id);
3807
    $session_id = intval($session_id);
3808
3809
    $base_work_dir = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/work';
3810
    $course_id = $courseInfo['real_id'];
3811
3812
    $directory = api_replace_dangerous_char($formValues['new_dir']);
3813
    $directory = disable_dangerous_file($directory);
3814
    $created_dir = create_unexisting_work_directory($base_work_dir, $directory);
3815
3816
    if (!empty($created_dir)) {
3817
        $dirName = '/'.$created_dir;
3818
        $today = api_get_utc_datetime();
3819
3820
        $params = [
3821
            'c_id' => $course_id,
3822
            'url' => $dirName,
3823
            'title' => $formValues['new_dir'],
3824
            'description' => $formValues['description'],
3825
            'author' => '',
3826
            'active' => '1',
3827
            'accepted' => '1',
3828
            'filetype' => 'folder',
3829
            'post_group_id' => $group_id,
3830
            'sent_date' => $today,
3831
            'qualification' => $formValues['qualification'] != '' ? $formValues['qualification'] : 0,
3832
            'parent_id' => 0,
3833
            'qualificator_id' => 0,
3834
            'weight' => !empty($formValues['weight']) ? $formValues['weight'] : 0,
3835
            'session_id' => $session_id,
3836
            'allow_text_assignment' => $formValues['allow_text_assignment'],
3837
            'contains_file' => 0,
3838
            'user_id' => $user_id,
3839
            'has_properties' => 0,
3840
            'document_id' => 0
3841
        ];
3842
        $id = Database::insert($work_table, $params);
3843
3844
        if ($id) {
3845
3846
            $sql = "UPDATE $work_table SET id = iid WHERE iid = $id";
3847
            Database::query($sql);
3848
3849
            // Folder created
3850
            api_item_property_update(
3851
                $courseInfo,
3852
                'work',
3853
                $id,
3854
                'DirectoryCreated',
3855
                $user_id,
3856
                $group_id
3857
            );
3858
3859
            updatePublicationAssignment($id, $formValues, $courseInfo, $group_id);
3860
3861
            if (api_get_course_setting('email_alert_students_on_new_homework') == 1) {
3862
                send_email_on_homework_creation($course_id, $session_id, $id);
3863
            }
3864
3865
            return $id;
3866
        }
3867
    }
3868
    return false;
3869
}
3870
3871
/**
3872
 * @param int $workId
3873
 * @param array $courseInfo
3874
 * @return int
3875
 */
3876
function agendaExistsForWork($workId, $courseInfo)
3877
{
3878
    $workTable = Database :: get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
3879
    $courseId = $courseInfo['real_id'];
3880
    $workId = intval($workId);
3881
3882
    $sql = "SELECT add_to_calendar FROM $workTable
3883
            WHERE c_id = $courseId AND publication_id = ".$workId;
3884
    $res = Database::query($sql);
3885
    if (Database::num_rows($res)) {
3886
        $row = Database::fetch_array($res, 'ASSOC');
3887
        if (!empty($row['add_to_calendar'])) {
3888
            return $row['add_to_calendar'];
3889
        }
3890
    }
3891
    return 0;
3892
}
3893
3894
/**
3895
 * Update work description, qualification, weight, allow_text_assignment
3896
 * @param int $workId
3897
 * @param array $params
3898
 * @param array $courseInfo
3899
 * @param int $sessionId
3900
 */
3901
function updateWork($workId, $params, $courseInfo, $sessionId = 0)
3902
{
3903
    $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
3904
    $filteredParams = array(
3905
        'description' => $params['description'],
3906
        'qualification' => $params['qualification'],
3907
        'weight' => $params['weight'],
3908
        'allow_text_assignment' => $params['allow_text_assignment']
3909
    );
3910
3911
    Database::update(
3912
        $workTable,
3913
        $filteredParams,
3914
        array(
3915
            'id = ? AND c_id = ? AND session_id = ? ' => array(
3916
                $workId, $courseInfo['real_id'], $sessionId
3917
            )
3918
        )
3919
    );
3920
}
3921
3922
/**
3923
 * @param int $workId
3924
 * @param array $params
3925
 * @param array $courseInfo
3926
 * @param int $groupId
3927
 */
3928
function updatePublicationAssignment($workId, $params, $courseInfo, $groupId)
3929
{
3930
    $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
3931
    $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
3932
    $workId = intval($workId);
3933
    $time = api_get_utc_datetime();
3934
    $course_id = $courseInfo['real_id'];
3935
3936
    // Insert into agenda
3937
    $agendaId = 0;
3938
3939
    if (isset($params['add_to_calendar']) && $params['add_to_calendar'] == 1) {
3940
        require_once api_get_path(SYS_CODE_PATH).'resourcelinker/resourcelinker.inc.php';
3941
3942
        // Setting today date
3943
        $date = $end_date = $time;
3944
3945
        if (isset($params['enableExpiryDate'])) {
3946
            $end_date = $params['expires_on'];
3947
            $date = $end_date;
3948
        }
3949
3950
        $title = sprintf(get_lang('HandingOverOfTaskX'), $params['new_dir']);
3951
        $description = isset($params['description']) ? $params['description'] : '';
3952
        $content = '<a href="'.api_get_path(WEB_CODE_PATH).'work/work_list.php?'.api_get_cidreq().'&id='.$workId.'">'
3953
            .$params['new_dir'].'</a>'.$description;
3954
3955
        $agendaId = agendaExistsForWork($workId, $courseInfo);
3956
3957
        // Add/edit agenda
3958
        $agenda = new Agenda();
3959
        $agenda->set_course($courseInfo);
3960
        $agenda->type = 'course';
3961
3962
        if (empty($agendaId)) {
3963
            $agendaId = $agenda->addEvent(
3964
                $date,
3965
                $end_date,
3966
                'false',
3967
                $title,
3968
                $content,
3969
                array('GROUP:'.$groupId)
3970
            );
3971
        } else {
3972
            $agenda->editEvent(
3973
                $agendaId,
3974
                $end_date,
3975
                $end_date,
3976
                'false',
3977
                $title,
3978
                $content
3979
            );
3980
        }
3981
    }
3982
3983
    $qualification = isset($params['qualification']) && !empty($params['qualification']) ? 1 : 0;
3984
    $expiryDate = isset($params['enableExpiryDate']) && $params['enableExpiryDate'] == 1 ? api_get_utc_datetime($params['expires_on']) : '';
3985
    $endDate = isset($params['enableEndDate']) && $params['enableEndDate'] == 1 ? api_get_utc_datetime($params['ends_on']) : '';
3986
3987
    $data = get_work_assignment_by_id($workId, $course_id);
3988
3989
    if (!empty($expiryDate)) {
3990
        $expiryDateCondition = "expires_on = '".Database::escape_string($expiryDate)."', ";
3991
    } else {
3992
        $expiryDateCondition = "expires_on = null, ";
3993
    }
3994
3995
    if (!empty($endDate)) {
3996
        $endOnCondition = "ends_on = '".Database::escape_string($endDate)."', ";
3997
    } else {
3998
        $endOnCondition = "ends_on = null, ";
3999
    }
4000
4001
    if (empty($data)) {
4002
4003
        $sql = "INSERT INTO $table SET
4004
                c_id = $course_id ,
4005
                $expiryDateCondition
4006
                $endOnCondition
4007
                add_to_calendar = $agendaId,
4008
                enable_qualification = '$qualification',
4009
                publication_id = '$workId'";
4010
4011
        Database::query($sql);
4012
4013
        $my_last_id = Database::insert_id();
4014
        if ($my_last_id) {
4015
4016
            $sql = "UPDATE $table SET
4017
                        id = iid
4018
                    WHERE iid = $my_last_id";
4019
            Database::query($sql);
4020
4021
            $sql = "UPDATE $workTable SET
4022
                        has_properties  = $my_last_id,
4023
                        view_properties = 1
4024
                    WHERE c_id = $course_id AND id = $workId";
4025
            Database::query($sql);
4026
        }
4027
    } else {
4028
        $sql = "UPDATE $table SET
4029
                    $expiryDateCondition
4030
                    $endOnCondition
4031
                    add_to_calendar  = $agendaId,
4032
                    enable_qualification = '".$qualification."'
4033
                WHERE
4034
                    publication_id = $workId AND
4035
                    c_id = $course_id AND
4036
                    id = ".$data['id'];
4037
        Database::query($sql);
4038
    }
4039
4040
    if (!empty($params['category_id'])) {
4041
4042
        $link_info = GradebookUtils::is_resource_in_course_gradebook(
4043
            $courseInfo['code'],
4044
            LINK_STUDENTPUBLICATION,
4045
            $workId,
4046
            api_get_session_id()
4047
        );
4048
4049
        $linkId = null;
4050
        if (!empty($link_info)) {
4051
            $linkId = $link_info['id'];
4052
        }
4053
4054
        if (isset($params['make_calification']) &&
4055
            $params['make_calification'] == 1
4056
        ) {
4057
            if (empty($linkId)) {
4058
                GradebookUtils::add_resource_to_course_gradebook(
4059
                    $params['category_id'],
4060
                    $courseInfo['code'],
4061
                    LINK_STUDENTPUBLICATION,
4062
                    $workId,
4063
                    $params['new_dir'],
4064
                    (float)$params['weight'],
4065
                    (float)$params['qualification'],
4066
                    $params['description'],
4067
                    1,
4068
                    api_get_session_id()
4069
                );
4070
            } else {
4071
                GradebookUtils::update_resource_from_course_gradebook(
4072
                    $linkId,
4073
                    $courseInfo['code'],
4074
                    $params['weight']
4075
                );
4076
            }
4077
        } else {
4078
            // Delete everything of the gradebook for this $linkId
4079
            GradebookUtils::remove_resource_from_course_gradebook($linkId);
4080
        }
4081
    }
4082
}
4083
4084
/**
4085
 * Delete all work by student
4086
 * @param int $userId
4087
 * @param array $courseInfo
4088
 * @return array return deleted items
4089
 */
4090
function deleteAllWorkPerUser($userId, $courseInfo)
4091
{
4092
    $deletedItems = array();
4093
    $workPerUser = getWorkPerUser($userId);
4094
    if (!empty($workPerUser)) {
4095
        foreach ($workPerUser as $work) {
4096
            $work = $work['work'];
4097
            foreach ($work->user_results as $userResult) {
4098
                $result = deleteWorkItem($userResult['id'], $courseInfo);
4099
                if ($result) {
4100
                    $deletedItems[] = $userResult;
4101
                }
4102
            }
4103
        }
4104
    }
4105
    return $deletedItems;
4106
}
4107
4108
/**
4109
 * @param int $item_id
4110
 * @param array course info
4111
 * @return bool
4112
 */
4113
function deleteWorkItem($item_id, $courseInfo)
4114
{
4115
    $work_table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
4116
    $TSTDPUBASG = Database :: get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
4117
4118
    $currentCourseRepositorySys = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/';
4119
4120
    $is_allowed_to_edit = api_is_allowed_to_edit();
4121
    $file_deleted = false;
4122
    $item_id = intval($item_id);
4123
4124
    $is_author = user_is_author($item_id);
4125
    $work_data = get_work_data_by_id($item_id);
4126
    $locked = api_resource_is_locked_by_gradebook($work_data['parent_id'], LINK_STUDENTPUBLICATION);
4127
    $course_id = $courseInfo['real_id'];
4128
4129
    if (($is_allowed_to_edit && $locked == false) ||
4130
        (
4131
            $locked == false &&
4132
            $is_author &&
4133
            api_get_course_setting('student_delete_own_publication') == 1 &&
4134
            $work_data['qualificator_id'] == 0
4135
        )
4136
    ) {
4137
        // We found the current user is the author
4138
        $sql = "SELECT url, contains_file FROM $work_table
4139
                WHERE c_id = $course_id AND id = $item_id";
4140
        $result = Database::query($sql);
4141
        $row = Database::fetch_array($result);
4142
4143
        if (Database::num_rows($result) > 0) {
4144
            $sql = "UPDATE $work_table SET active = 2
4145
                    WHERE c_id = $course_id AND id = $item_id";
4146
            Database::query($sql);
4147
            $sql = "DELETE FROM $TSTDPUBASG
4148
                    WHERE c_id = $course_id AND publication_id = $item_id";
4149
            Database::query($sql);
4150
4151
            api_item_property_update(
4152
                $courseInfo,
4153
                'work',
4154
                $item_id,
4155
                'DocumentDeleted',
4156
                api_get_user_id()
4157
            );
4158
            $work = $row['url'];
4159
4160
            if ($row['contains_file'] == 1) {
4161
                if (!empty($work)) {
4162
                    if (api_get_setting('permanently_remove_deleted_files') == 'true') {
4163
                        my_delete($currentCourseRepositorySys.'/'.$work);
4164
                        $file_deleted = true;
4165
                    } else {
4166
                        $extension = pathinfo($work, PATHINFO_EXTENSION);
4167
                        $new_dir = $work.'_DELETED_'.$item_id.'.'.$extension;
4168
4169
                        if (file_exists($currentCourseRepositorySys.'/'.$work)) {
4170
                            rename($currentCourseRepositorySys.'/'.$work, $currentCourseRepositorySys.'/'.$new_dir);
4171
                            $file_deleted = true;
4172
                        }
4173
                    }
4174
                }
4175
            } else {
4176
                $file_deleted = true;
4177
            }
4178
        }
4179
    }
4180
    return $file_deleted;
4181
}
4182
4183
/**
4184
 * @param FormValidator $form
4185
 * @param array $defaults
4186
 * @return FormValidator
4187
 */
4188
function getFormWork($form, $defaults = array())
4189
{
4190
    if (!empty($defaults)) {
4191
        if (isset($defaults['submit'])) {
4192
            unset($defaults['submit']);
4193
        }
4194
    }
4195
4196
    // Create the form that asks for the directory name
4197
    $form->addElement('text', 'new_dir', get_lang('AssignmentName'));
4198
    $form->addRule('new_dir', get_lang('ThisFieldIsRequired'), 'required');
4199
    $form->addHtmlEditor('description', get_lang('Description'), false, false, getWorkDescriptionToolbar());
4200
    $form->addButtonAdvancedSettings('advanced_params', get_lang('AdvancedParameters'));
4201
4202
    if (!empty($defaults) && (isset($defaults['enableEndDate']) || isset($defaults['enableExpiryDate']))) {
4203
        $form->addElement('html', '<div id="advanced_params_options" style="display:block">');
4204
    } else {
4205
        $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
4206
    }
4207
4208
    // QualificationOfAssignment
4209
    $form->addElement('text', 'qualification', get_lang('QualificationNumeric'));
4210
4211
    if ((api_get_session_id() != 0 && Gradebook::is_active()) || api_get_session_id() == 0) {
4212
        $form->addElement(
4213
            'checkbox',
4214
            'make_calification',
4215
            null,
4216
            get_lang('MakeQualifiable'),
4217
            array(
4218
                'id' =>'make_calification_id',
4219
                'onclick' => "javascript: if(this.checked) { document.getElementById('option1').style.display='block';}else{document.getElementById('option1').style.display='none';}"
4220
            )
4221
        );
4222
    } else {
4223
        // QualificationOfAssignment
4224
        $form->addElement('hidden', 'make_calification', false);
4225
    }
4226
4227
    if (!empty($defaults) && isset($defaults['category_id'])) {
4228
        $form->addElement('html', '<div id=\'option1\' style="display:block">');
4229
    } else {
4230
        $form->addElement('html', '<div id=\'option1\' style="display:none">');
4231
    }
4232
4233
    // Loading Gradebook select
4234
    GradebookUtils::load_gradebook_select_in_tool($form);
4235
4236
    $form->addElement('text', 'weight', get_lang('WeightInTheGradebook'));
4237
    $form->addElement('html', '</div>');
4238
4239
    $form->addElement('checkbox', 'enableExpiryDate', null, get_lang('EnableExpiryDate'), 'id="expiry_date"');
4240
    if (isset($defaults['enableExpiryDate']) && $defaults['enableExpiryDate']) {
4241
        $form->addElement('html', '<div id="option2" style="display: block;">');
4242
    } else {
4243
        $form->addElement('html', '<div id="option2" style="display: none;">');
4244
    }
4245
4246
    $currentDate = substr(api_get_local_time(), 0, 10);
4247
4248 View Code Duplication
    if (!isset($defaults['expires_on'])) {
4249
        $date = substr($currentDate, 0, 10);
4250
        $defaults['expires_on'] = $date.' 23:59';
4251
    }
4252
4253 View Code Duplication
    if (!isset($defaults['ends_on'])) {
4254
        $date = substr($currentDate, 0, 10);
4255
        $defaults['ends_on'] = $date.' 23:59';
4256
    }
4257
4258
    $form->addElement('date_time_picker', 'expires_on', get_lang('ExpiresAt'));
4259
    $form->addElement('html', '</div>');
4260
4261
    $form->addElement('checkbox', 'enableEndDate', null, get_lang('EnableEndDate'), 'id="end_date"');
4262
4263
    if (isset($defaults['enableEndDate']) && $defaults['enableEndDate']) {
4264
        $form->addElement('html', '<div id="option3" style="display: block;">');
4265
    } else {
4266
        $form->addElement('html', '<div id="option3" style="display: none;">');
4267
    }
4268
4269
    $form->addElement('date_time_picker', 'ends_on', get_lang('EndsAt'));
4270
    $form->addElement('html', '</div>');
4271
4272
    $form->addElement('checkbox', 'add_to_calendar', null, get_lang('AddToCalendar'));
4273
    $form->addElement('select', 'allow_text_assignment', get_lang('DocumentType'), getUploadDocumentType());
4274
4275
    $form->addElement('html', '</div>');
4276
4277
    if (isset($defaults['enableExpiryDate']) && isset($defaults['enableEndDate'])) {
4278
        $form->addRule(array('expires_on', 'ends_on'), get_lang('DateExpiredNotBeLessDeadLine'), 'comparedate');
4279
    }
4280
    if (!empty($defaults)) {
4281
        $form->setDefaults($defaults);
4282
    }
4283
4284
    return $form;
4285
}
4286
4287
/**
4288
 * @return array
4289
 */
4290
function getUploadDocumentType()
4291
{
4292
    return array(
4293
        0 => get_lang('AllowFileOrText'),
4294
        1 => get_lang('AllowOnlyText'),
4295
        2 => get_lang('AllowOnlyFiles')
4296
    );
4297
}
4298
4299
/**
4300
 * @param array $courseInfo
4301
 * @param bool $showScore
4302
 * @param bool $studentDeleteOwnPublication
4303
 */
4304
function updateSettings($courseInfo, $showScore, $studentDeleteOwnPublication)
4305
{
4306
    $showScore = intval($showScore);
4307
    $courseId = api_get_course_int_id();
4308
    $main_course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
4309
    $table_course_setting = Database :: get_course_table(TOOL_COURSE_SETTING);
4310
4311
    $query = "UPDATE ".$main_course_table." SET show_score = '".$showScore."'
4312
              WHERE id = $courseId";
4313
    Database::query($query);
4314
4315
    /**
4316
     * Course data are cached in session so we need to update both the database
4317
     * and the session data
4318
     */
4319
    $_course['show_score'] = $showScore;
4320
    Session::write('_course', $courseInfo);
4321
4322
    // changing the tool setting: is a student allowed to delete his/her own document
4323
4324
    // counting the number of occurrences of this setting (if 0 => add, if 1 => update)
4325
    $query = "SELECT * FROM " . $table_course_setting . "
4326
              WHERE c_id = $courseId AND variable = 'student_delete_own_publication'";
4327
4328
    $result = Database::query($query);
4329
    $number_of_setting = Database::num_rows($result);
4330
4331
    if ($number_of_setting == 1) {
4332
        $query = "UPDATE " . $table_course_setting . " SET
4333
                  value='" . Database::escape_string($studentDeleteOwnPublication) . "'
4334
                  WHERE variable = 'student_delete_own_publication' AND c_id = $courseId";
4335
        Database::query($query);
4336
    } else {
4337
        $params = [
4338
            'c_id' => $courseId,
4339
            'variable' => 'student_delete_own_publication',
4340
            'value' => $studentDeleteOwnPublication,
4341
            'category' => 'work'
4342
        ];
4343
        Database::insert($table_course_setting, $params);
4344
    }
4345
}
4346
4347
/**
4348
 * @param int $item_id
4349
 * @param array $course_info
4350
 */
4351
function makeVisible($item_id, $course_info)
4352
{
4353
    $work_table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
4354
    $course_id = $course_info['real_id'];
4355
    $item_id = intval($item_id);
4356
4357
    $sql = "UPDATE $work_table SET accepted = 1
4358
            WHERE c_id = $course_id AND id = $item_id";
4359
    Database::query($sql);
4360
    api_item_property_update($course_info, 'work', $item_id, 'visible', api_get_user_id());
4361
}
4362
4363
/**
4364
 * @param int $item_id
4365
 * @param array $course_info
4366
 */
4367 View Code Duplication
function makeInvisible($item_id, $course_info)
4368
{
4369
    $work_table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
4370
    $item_id = intval($item_id);
4371
    $course_id = $course_info['real_id'];
4372
    $sql = "UPDATE  " . $work_table . "
4373
            SET accepted = 0
4374
            WHERE c_id = $course_id AND id = '" . $item_id . "'";
4375
    Database::query($sql);
4376
    api_item_property_update(
4377
        $course_info,
4378
        'work',
4379
        $item_id,
4380
        'invisible',
4381
        api_get_user_id()
4382
    );
4383
}
4384
4385
/**
4386
 * @param int $item_id
4387
 * @param string $path
4388
 * @param array $courseInfo
4389
 * @param int $groupId
4390
 * @param int $sessionId
4391
 * @return string
4392
 */
4393
function generateMoveForm($item_id, $path, $courseInfo, $groupId, $sessionId)
4394
{
4395
    $work_table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
4396
    $courseId = $courseInfo['real_id'];
4397
    $folders = array();
4398
    $session_id = intval($sessionId);
4399
    $groupId = intval($groupId);
4400
    $sessionCondition = empty($sessionId) ? " AND (session_id = 0 OR session_id IS NULL) " : " AND session_id='".$session_id."'";
4401
    $sql = "SELECT id, url, title
4402
            FROM $work_table
4403
            WHERE
4404
                c_id = $courseId AND
4405
                active IN (0, 1) AND
4406
                url LIKE '/%' AND
4407
                post_group_id = $groupId
4408
                $sessionCondition";
4409
    $res = Database::query($sql);
4410
    while ($folder = Database::fetch_array($res)) {
4411
        $title = empty($folder['title']) ? basename($folder['url']) : $folder['title'];
4412
        $folders[$folder['id']] = $title;
4413
    }
4414
    return build_work_move_to_selector($folders, $path, $item_id);
4415
}
4416
4417
/**
4418
 * @param int $workId
4419
 * @return string
4420
 */
4421
function showStudentList($workId)
4422
{
4423
    $columnModel = array(
4424
        array(
4425
            'name' => 'student',
4426
            'index' => 'student',
4427
            'width' => '350px',
4428
            'align' => 'left',
4429
            'sortable' => 'false',
4430
        ),
4431
        array(
4432
            'name' => 'works',
4433
            'index' => 'works',
4434
            'align' => 'center',
4435
            'sortable' => 'false',
4436
        ),
4437
    );
4438
    $token = null;
4439
4440
    $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_work_student_list_overview&work_id='.$workId.'&'.api_get_cidreq();
4441
4442
    $columns = array(
4443
        get_lang('Students'),
4444
        get_lang('Works')
4445
    );
4446
4447
    $order = api_is_western_name_order() ? 'firstname' : 'lastname';
4448
    $params = array(
4449
        'autowidth' => 'true',
4450
        'height' => 'auto',
4451
        'rowNum' => 5,
4452
        'sortname' => $order,
4453
        'sortorder' => 'asc'
4454
    );
4455
4456
    $html = '<script>
4457
    $(function() {
4458
        '.Display::grid_js('studentList', $url, $columns, $columnModel, $params, array(), null, true).'
4459
        $("#workList").jqGrid(
4460
            "navGrid",
4461
            "#studentList_pager",
4462
            { edit: false, add: false, del: false },
4463
            { height:280, reloadAfterSubmit:false }, // edit options
4464
            { height:280, reloadAfterSubmit:false }, // add options
4465
            { width:500 } // search options
4466
        );
4467
    });
4468
    </script>';
4469
    $html .= Display::grid_html('studentList');
4470
    return $html;
4471
}
4472
4473
/**
4474
 * @param string $courseCode
4475
 * @param int $sessionId
4476
 * @param int $groupId
4477
 * @param int $start
4478
 * @param int $limit
4479
 * @param string $sidx
4480
 * @param string $sord
4481
 * @param $getCount
4482
 * @return array|int
4483
 */
4484
function getWorkUserList($courseCode, $sessionId, $groupId, $start, $limit, $sidx, $sord, $getCount = false)
4485
{
4486
    if (!empty($groupId)) {
4487
        $userList = GroupManager::get_users(
4488
            $groupId,
4489
            false,
4490
            $start,
4491
            $limit,
4492
            $getCount,
4493
            null,
4494
            $sidx,
4495
            $sord
4496
        );
4497
    } else {
4498
        $limitString = null;
4499 View Code Duplication
        if (!empty($start) && !empty($limit)) {
4500
            $start = intval($start);
4501
            $limit = intval($limit);
4502
            $limitString = " LIMIT $start, $limit";
4503
        }
4504
4505
        $orderBy = null;
4506
4507
        if (!empty($sidx) && !empty($sord)) {
4508
            if (in_array($sidx, array('firstname', 'lastname'))) {
4509
                $orderBy = "ORDER BY $sidx $sord";
4510
            }
4511
        }
4512
4513
        if (empty($sessionId)) {
4514
            $userList = CourseManager::get_user_list_from_course_code(
4515
                $courseCode,
4516
                $sessionId,
4517
                $limitString,
4518
                $orderBy ,
4519
                STUDENT,
4520
                $getCount
4521
            );
4522
        } else {
4523
            $userList = CourseManager::get_user_list_from_course_code(
4524
                $courseCode,
4525
                $sessionId,
4526
                $limitString,
4527
                $orderBy,
4528
                0,
4529
                $getCount
4530
            );
4531
        }
4532
4533
        if ($getCount == false) {
4534
            $userList = array_keys($userList);
4535
        }
4536
    }
4537
    return $userList;
4538
}
4539
4540
/**
4541
 * @param int $workId
4542
 * @param string $courseCode
4543
 * @param int $sessionId
4544
 * @param int $groupId
4545
 * @param int $start
4546
 * @param int $limit
4547
 * @param int $sidx
4548
 * @param string $sord
4549
 * @param bool $getCount
4550
 * @return array|int
4551
 */
4552
function getWorkUserListData(
4553
    $workId,
4554
    $courseCode,
4555
    $sessionId,
4556
    $groupId,
4557
    $start,
4558
    $limit,
4559
    $sidx,
4560
    $sord,
4561
    $getCount = false
4562
) {
4563
    $my_folder_data = get_work_data_by_id($workId);
4564
    $workParents = array();
4565
    if (empty($my_folder_data)) {
4566
        $workParents = getWorkList($workId, $my_folder_data, null);
4567
    }
4568
4569
    $workIdList = array();
4570
    if (!empty($workParents)) {
4571
        foreach ($workParents as $work) {
4572
            $workIdList[] = $work->id;
4573
        }
4574
    }
4575
4576
    $courseInfo = api_get_course_info($courseCode);
4577
4578
    $userList = getWorkUserList(
4579
        $courseCode,
4580
        $sessionId,
4581
        $groupId,
4582
        $start,
4583
        $limit,
4584
        $sidx,
4585
        $sord,
4586
        $getCount
4587
    );
4588
4589
    if ($getCount) {
4590
        return $userList;
4591
    }
4592
    $results = array();
4593
    if (!empty($userList)) {
4594
        foreach ($userList as $userId) {
0 ignored issues
show
Bug introduced by
The expression $userList of type array|integer is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
4595
            $user = api_get_user_info($userId);
4596
            $link = api_get_path(WEB_CODE_PATH).'work/student_work.php?'.api_get_cidreq().'&studentId='.$user['user_id'];
4597
            $url = Display::url(api_get_person_name($user['firstname'], $user['lastname']), $link);
4598
            $userWorks = 0;
4599
            if (!empty($workIdList)) {
4600
                $userWorks = getUniqueStudentAttempts(
4601
                    $workIdList,
4602
                    $groupId,
4603
                    $courseInfo['real_id'],
4604
                    $sessionId,
4605
                    $user['user_id']
4606
                );
4607
            }
4608
            $works = $userWorks." / ".count($workParents);
4609
            $results[] = array(
4610
                'student' => $url,
4611
                'works' => Display::url($works, $link),
4612
            );
4613
        }
4614
    }
4615
    return $results;
4616
}
4617
4618
/**
4619
 * @param int $id
4620
 * @param array $course_info
4621
 * @param bool $isCorrection
4622
 *
4623
 * @return bool
4624
 */
4625
function downloadFile($id, $course_info, $isCorrection)
4626
{
4627
    return getFile($id, $course_info, true, $isCorrection);
4628
}
4629
4630
/**
4631
 * @param int $id
4632
 * @param array $course_info
4633
 * @param bool $download
4634
 * @param bool $isCorrection
4635
 *
4636
 * @return bool
4637
 */
4638
function getFile($id, $course_info, $download = true, $isCorrection = false)
4639
{
4640
    $file = getFileContents($id, $course_info, 0, $isCorrection);
4641
    if (!empty($file) && is_array($file)) {
4642
        return DocumentManager::file_send_for_download(
4643
            $file['path'],
4644
            $download,
4645
            $file['title']
4646
        );
4647
    }
4648
4649
    return false;
4650
}
4651
4652
4653
/**
4654
 * Get the file contents for an assigment
4655
 * @param int $id
4656
 * @param array $course_info
4657
 * @param int Session ID
4658
 * @param $correction
4659
 *
4660
 * @return array|bool
4661
 */
4662
function getFileContents($id, $course_info, $sessionId = 0, $correction = false)
4663
{
4664
    $id = intval($id);
4665
    if (empty($course_info) || empty($id)) {
4666
        return false;
4667
    }
4668
    if (empty($sessionId)) {
4669
        $sessionId = api_get_session_id();
4670
    }
4671
4672
    $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
4673
4674
    if (!empty($course_info['real_id'])) {
4675
        $sql = 'SELECT *
4676
                FROM '.$table.'
4677
                WHERE c_id = '.$course_info['real_id'].' AND id = "'.$id.'"';
4678
        $result = Database::query($sql);
4679
        if ($result && Database::num_rows($result)) {
4680
            $row = Database::fetch_array($result, 'ASSOC');
4681
4682
            if ($correction) {
4683
                $row['url'] = $row['url_correction'];
4684
            }
4685
4686
            if (empty($row['url'])) {
4687
                return false;
4688
            }
4689
4690
            $full_file_name = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/'.$row['url'];
4691
4692
            $item_info = api_get_item_property_info(
4693
                api_get_course_int_id(),
4694
                'work',
4695
                $row['id'],
4696
                $sessionId
4697
            );
4698
4699
            allowOnlySubscribedUser(
4700
                api_get_user_id(),
4701
                $row['parent_id'],
4702
                $course_info['real_id']
4703
            );
4704
4705
            if (empty($item_info)) {
4706
                api_not_allowed();
4707
            }
4708
4709
            /*
4710
            field show_score in table course :
4711
                0 =>    New documents are visible for all users
4712
                1 =>    New documents are only visible for the teacher(s)
4713
            field visibility in table item_property :
4714
                0 => eye closed, invisible for all students
4715
                1 => eye open
4716
            field accepted in table c_student_publication :
4717
                0 => eye closed, invisible for all students
4718
                1 => eye open
4719
            ( We should have visibility == accepted, otherwise there is an
4720
            inconsistency in the Database)
4721
            field value in table c_course_setting :
4722
                0 => Allow learners to delete their own publications = NO
4723
                1 => Allow learners to delete their own publications = YES
4724
4725
            +------------------+-------------------------+------------------------+
4726
            |Can download work?| doc visible for all = 0 | doc visible for all = 1|
4727
            +------------------+-------------------------+------------------------+
4728
            |  visibility = 0  | editor only             | editor only            |
4729
            |                  |                         |                        |
4730
            +------------------+-------------------------+------------------------+
4731
            |  visibility = 1  | editor                  | editor                 |
4732
            |                  | + owner of the work     | + any student          |
4733
            +------------------+-------------------------+------------------------+
4734
            (editor = teacher + admin + anybody with right api_is_allowed_to_edit)
4735
            */
4736
4737
            $work_is_visible = $item_info['visibility'] == 1 && $row['accepted'] == 1;
4738
            $doc_visible_for_all = ($course_info['show_score'] == 1);
4739
4740
            $is_editor = api_is_allowed_to_edit(true, true, true);
4741
            $student_is_owner_of_work = user_is_author($row['id'], $row['user_id']);
4742
4743
            if ($is_editor ||
4744
                ($student_is_owner_of_work) ||
4745
                ($doc_visible_for_all && $work_is_visible)
4746
            ) {
4747
                $title = $row['title'];
4748
                if ($correction) {
4749
                    $title = $row['title_correction'];
4750
                }
4751
                if (array_key_exists('filename', $row) && !empty($row['filename'])) {
4752
                    $title = $row['filename'];
4753
                }
4754
4755
                $title = str_replace(' ', '_', $title);
4756
                Event::event_download($title);
4757
                if (Security::check_abs_path(
4758
                    $full_file_name,
4759
                    api_get_path(SYS_COURSE_PATH).api_get_course_path().'/')
4760
                ) {
4761
                    return array(
4762
                        'path' => $full_file_name,
4763
                        'title' => $title,
4764
                        'title_correction' => $row['title_correction']
4765
                    );
4766
                }
4767
            }
4768
        }
4769
    }
4770
4771
    return false;
4772
}
4773
4774
/**
4775
 * @param int $userId
4776
 * @param array $courseInfo
4777
 * @param string $format
4778
 * @return bool
4779
 */
4780
function exportAllWork($userId, $courseInfo, $format = 'pdf')
4781
{
4782
    $userInfo = api_get_user_info($userId);
4783
    if (empty($userInfo) || empty($courseInfo)) {
4784
        return false;
4785
    }
4786
4787
    $workPerUser = getWorkPerUser($userId);
4788
4789
    switch ($format) {
4790
        case 'pdf':
4791
            if (!empty($workPerUser)) {
4792
                $pdf = new PDF();
4793
4794
                $content = null;
4795
                foreach ($workPerUser as $work) {
4796
                    $work = $work['work'];
4797
                    foreach ($work->user_results as $userResult) {
4798
                        //var_dump($userResult);exit;
4799
                        $content .= $userResult['title'];
4800
                        // No need to use api_get_local_time()
4801
                        $content .= $userResult['sent_date'];
4802
                        $content .= $userResult['qualification'];
4803
                        $content .= $userResult['description'];
4804
                        //$content .= getWorkComments($userResult);
4805
                    }
4806
                }
4807
4808
                if (!empty($content)) {
4809
                    $pdf->content_to_pdf(
4810
                        $content,
4811
                        null,
4812
                        api_replace_dangerous_char($userInfo['complete_name']),
4813
                        $courseInfo['code']
4814
                    );
4815
                }
4816
            }
4817
            break;
4818
    }
4819
}
4820
4821
/**
4822
 * @param int $workId
4823
 * @param array $courseInfo
4824
 * @param int $sessionId
4825
 * @param string $format
4826
 * @return bool
4827
 */
4828
function exportAllStudentWorkFromPublication(
4829
    $workId,
4830
    $courseInfo,
4831
    $sessionId,
4832
    $format = 'pdf'
4833
) {
4834
    if (empty($courseInfo)) {
4835
        return false;
4836
    }
4837
4838
    $workData = get_work_data_by_id($workId);
4839
4840
    if (empty($workData)) {
4841
        return false;
4842
    }
4843
4844
    $assignment = get_work_assignment_by_id($workId);
4845
4846
    $courseCode = $courseInfo['code'];
4847
    $header = get_lang('Course').': '.$courseInfo['title'];
4848
    $teachers = CourseManager::get_teacher_list_from_course_code_to_string(
4849
        $courseCode
4850
    );
4851
4852
    if (!empty($sessionId)) {
4853
        $sessionInfo = api_get_session_info($sessionId);
4854
        if (!empty($sessionInfo)) {
4855
            $header .= ' - ' . $sessionInfo['name'];
4856
            $header .= '<br />' . $sessionInfo['description'];
4857
            $teachers = SessionManager::getCoachesByCourseSessionToString(
4858
                $sessionId,
4859
                $courseInfo['real_id']
4860
            );
4861
        }
4862
    }
4863
4864
    $header .= '<br />'.get_lang('Teachers').': '.$teachers.'<br />';
4865
    $header .= '<br />'.get_lang('Date').': '.api_get_local_time().'<br />';
4866
    $header .= '<br />'.get_lang('WorkName').': '.$workData['title'].'<br />';
4867
4868
    $content = null;
4869
    $expiresOn = null;
4870
4871
    if (!empty($assignment) && isset($assignment['expires_on'])) {
4872
        $content .= '<br /><strong>' . get_lang('ExpirationDate') . '</strong>: ' . api_get_local_time($assignment['expires_on']);
4873
        $expiresOn = api_get_local_time($assignment['expires_on']);
4874
    }
4875
4876
    if (!empty($workData['description'])) {
4877
        $content .= '<br /><strong>' . get_lang('Description') . '</strong>: ' . $workData['description'];
4878
    }
4879
4880
    $workList = get_work_user_list(null, null, null, null, $workId);
4881
4882
    switch ($format) {
4883
        case 'pdf':
4884
            if (!empty($workList)) {
4885
4886
                $table = new HTML_Table(array('class' => 'data_table'));
4887
                $headers = array(
4888
                    get_lang('Name'),
4889
                    get_lang('User'),
4890
                    get_lang('HandOutDateLimit'),
4891
                    get_lang('SentDate'),
4892
                    get_lang('FileName'),
4893
                    get_lang('Score'),
4894
                    get_lang('Feedback')
4895
                );
4896
4897
                $column = 0;
4898
                foreach($headers as $header) {
4899
                    $table->setHeaderContents(0, $column, $header);
4900
                    $column++;
4901
                }
4902
4903
                $row = 1;
4904
4905
                //$pdf->set_custom_header($header);
4906
                foreach ($workList as $work) {
4907
                    $content .= '<hr />';
4908
                    // getWorkComments need c_id
4909
                    $work['c_id'] = $courseInfo['real_id'];
4910
4911
                    //$content .= get_lang('Date').': '.api_get_local_time($work['sent_date_from_db']).'<br />';
4912
                    $score = null;
4913
                    if (!empty($work['qualification_only'])) {
4914
                        $score = $work['qualification_only'];
4915
                    }
4916
                    //$content .= get_lang('Description').': '.$work['description'].'<br />';
4917
                    $comments = getWorkComments($work);
4918
4919
                    $feedback = null;
4920
                    if (!empty($comments)) {
4921
                        $content .= '<h4>'.get_lang('Feedback').': </h4>';
4922
                        foreach ($comments as $comment) {
4923
                            $feedback .= get_lang('User').': '.api_get_person_name(
4924
                                    $comment['firstname'],
4925
                                    $comment['lastname']
4926
                                ).'<br />';
4927
                            $feedback .= $comment['comment'].'<br />';
4928
                        }
4929
                    }
4930
4931
                    $table->setCellContents($row, 0, strip_tags($workData['title']));
4932
                    $table->setCellContents($row, 1, api_get_person_name(strip_tags($work['firstname']), strip_tags($work['lastname'])));
4933
                    $table->setCellContents($row, 2, $expiresOn);
4934
                    $table->setCellContents($row, 3, api_get_local_time($work['sent_date_from_db']));
4935
                    $table->setCellContents($row, 4, strip_tags($work['title']));
4936
                    $table->setCellContents($row, 5, $score);
4937
                    $table->setCellContents($row, 6, $feedback);
4938
4939
                    $row++;
4940
                }
4941
4942
                $content = $table->toHtml();
4943
4944
                if (!empty($content)) {
4945
                    $params = array(
4946
                        'filename' => $workData['title'] . '_' . api_get_local_time(),
4947
                        'pdf_title' => api_replace_dangerous_char($workData['title']),
4948
                        'course_code' => $courseInfo['code'],
4949
                        'add_signatures' => false
4950
                    );
4951
                    $pdf = new PDF('A4', null, $params);
4952
                    $pdf->html_to_pdf_with_template($content);
4953
                }
4954
                exit;
4955
            }
4956
            break;
4957
    }
4958
}
4959
4960
/**
4961
 * Downloads all user files per user
4962
 * @param int $userId
4963
 * @param array $courseInfo
4964
 * @return bool
4965
 */
4966
function downloadAllFilesPerUser($userId, $courseInfo)
4967
{
4968
    $userInfo = api_get_user_info($userId);
4969
4970
    if (empty($userInfo) || empty($courseInfo)) {
4971
        return false;
4972
    }
4973
4974
    $tempZipFile = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().".zip";
4975
    $coursePath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/work/';
4976
4977
    $zip  = new PclZip($tempZipFile);
4978
4979
    $workPerUser = getWorkPerUser($userId);
4980
4981
    if (!empty($workPerUser)) {
4982
        $files = array();
4983
        foreach ($workPerUser as $work) {
4984
            $work = $work['work'];
4985
            foreach ($work->user_results as $userResult) {
4986
                if (empty($userResult['url']) || empty($userResult['contains_file'])) {
4987
                    continue;
4988
                }
4989
                $data = getFileContents($userResult['id'], $courseInfo);
4990
                if (!empty($data) && isset($data['path'])) {
4991
                    $files[basename($data['path'])] = array(
4992
                        'title' => $data['title'],
4993
                        'path' => $data['path']
4994
                    );
4995
                }
4996
            }
4997
        }
4998
4999
        if (!empty($files)) {
5000
            Session::write('files', $files);
5001
            foreach ($files as $data) {
5002
                $zip->add(
5003
                    $data['path'],
5004
                    PCLZIP_OPT_REMOVE_PATH,
5005
                    $coursePath,
5006
                    PCLZIP_CB_PRE_ADD,
5007
                    'preAddAllWorkStudentCallback'
5008
                );
5009
            }
5010
        }
5011
5012
        // Start download of created file
5013
        $name = basename(api_replace_dangerous_char($userInfo['complete_name'])).'.zip';
5014
        Event::event_download($name.'.zip (folder)');
5015 View Code Duplication
        if (Security::check_abs_path($tempZipFile, api_get_path(SYS_ARCHIVE_PATH))) {
5016
            DocumentManager::file_send_for_download($tempZipFile, true, $name);
5017
            @unlink($tempZipFile);
5018
            exit;
5019
        }
5020
    }
5021
    exit;
5022
}
5023
5024
/**
5025
 * @param $p_event
5026
 * @param array $p_header
5027
 * @return int
5028
 */
5029 View Code Duplication
function preAddAllWorkStudentCallback($p_event, &$p_header)
5030
{
5031
    $files = Session::read('files');
5032
    if (isset($files[basename($p_header['stored_filename'])])) {
5033
        $p_header['stored_filename'] = $files[basename($p_header['stored_filename'])]['title'];
5034
        return 1;
5035
    }
5036
    return 0;
5037
}
5038
5039
/**
5040
 * Get all work created by a user
5041
 * @param int $user_id
5042
 * @param int $courseId
5043
 * @param int $sessionId
5044
 * @return array
5045
 */
5046
function getWorkCreatedByUser($user_id, $courseId, $sessionId)
5047
{
5048
    $items = api_get_item_property_list_by_tool_by_user(
5049
        $user_id,
5050
        'work',
5051
        $courseId,
5052
        $sessionId
5053
    );
5054
5055
    $forumList = array();
5056 View Code Duplication
    if (!empty($items)) {
5057
        foreach ($items as $forum) {
5058
            $item = get_work_data_by_id(
5059
                $forum['ref'],
5060
                $courseId,
5061
                $sessionId
5062
            );
5063
5064
            $forumList[] = array(
5065
                $item['title'],
5066
                api_get_local_time($forum['insert_date']),
5067
                api_get_local_time($forum['lastedit_date'])
5068
            );
5069
        }
5070
    }
5071
5072
    return $forumList;
5073
}
5074
5075
/**
5076
 * @param array $courseInfo
5077
 * @param int $workId
5078
 * @return bool
5079
 */
5080
function protectWork($courseInfo, $workId)
5081
{
5082
    $userId = api_get_user_id();
5083
    $groupId = api_get_group_id();
5084
    $sessionId = api_get_session_id();
5085
    $workData = get_work_data_by_id($workId);
5086
5087
    if (empty($workData) || empty($courseInfo)) {
5088
        api_not_allowed(true);
5089
    }
5090
5091
    if (api_is_platform_admin() || api_is_allowed_to_edit()) {
5092
        return true;
5093
    }
5094
5095
    $workId = $workData['id'];
5096
5097
    if ($workData['active'] != 1) {
5098
        api_not_allowed(true);
5099
    }
5100
5101
    $visibility = api_get_item_visibility($courseInfo, 'work', $workId, $sessionId);
5102
5103
    if ($visibility != 1) {
5104
        api_not_allowed(true);
5105
    }
5106
5107
    allowOnlySubscribedUser($userId, $workId, $courseInfo['real_id']);
5108
5109
    if (!empty($groupId)) {
5110
        $showWork = GroupManager::user_has_access(
5111
            $userId,
5112
            $groupId,
5113
            GroupManager::GROUP_TOOL_WORK
5114
        );
5115
        if (!$showWork) {
5116
            api_not_allowed(true);
5117
        }
5118
    }
5119
}
5120