Passed
Push — master ( a02707...7dc539 )
by Julito
12:00
created

TicketManager::add()   F

Complexity

Conditions 36
Paths > 20000

Size

Total Lines 291
Code Lines 176

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 36
eloc 176
nc 231391
nop 15
dl 0
loc 291
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Entity\Ticket;
5
use Chamilo\CoreBundle\Entity\TicketMessageAttachment;
6
use Chamilo\CoreBundle\Entity\TicketPriority;
7
use Chamilo\CoreBundle\Entity\TicketProject;
8
use Chamilo\CoreBundle\Entity\TicketStatus;
9
10
/**
11
 * Class TicketManager.
12
 */
13
class TicketManager
14
{
15
    const PRIORITY_NORMAL = 'NRM';
16
    const PRIORITY_HIGH = 'HGH';
17
    const PRIORITY_LOW = 'LOW';
18
19
    const SOURCE_EMAIL = 'MAI';
20
    const SOURCE_PHONE = 'TEL';
21
    const SOURCE_PLATFORM = 'PLA';
22
    const SOURCE_PRESENTIAL = 'PRE';
23
24
    const STATUS_NEW = 'NAT';
25
    const STATUS_PENDING = 'PND';
26
    const STATUS_UNCONFIRMED = 'XCF';
27
    const STATUS_CLOSE = 'CLS';
28
    const STATUS_FORWARDED = 'REE';
29
30
    /**
31
     * Constructor.
32
     */
33
    public function __construct()
34
    {
35
    }
36
37
    /**
38
     * Get categories of tickets.
39
     *
40
     * @param int    $projectId
41
     * @param string $order
42
     *
43
     * @return array
44
     */
45
    public static function get_all_tickets_categories($projectId, $order = '')
46
    {
47
        $table_support_category = Database::get_main_table(TABLE_TICKET_CATEGORY);
48
        $table_support_project = Database::get_main_table(TABLE_TICKET_PROJECT);
49
50
        $order = empty($order) ? 'category.total_tickets DESC' : $order;
51
        $order = Database::escape_string($order);
52
        $projectId = (int) $projectId;
53
54
        $sql = "SELECT
55
                    category.*,
56
                    category.id category_id,
57
                    project.other_area,
58
                    project.email
59
                FROM
60
                $table_support_category category
61
                INNER JOIN $table_support_project project
62
                ON project.id = category.project_id
63
                WHERE project.id  = $projectId
64
                ORDER BY $order";
65
        $result = Database::query($sql);
66
        $types = [];
67
        while ($row = Database::fetch_assoc($result)) {
68
            $types[] = $row;
69
        }
70
71
        return $types;
72
    }
73
74
    /**
75
     * @param $from
76
     * @param $numberItems
77
     * @param $column
78
     * @param $direction
79
     *
80
     * @return array
81
     */
82
    public static function getCategories($from, $numberItems, $column, $direction)
83
    {
84
        $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
85
        $sql = "SELECT id, name, description, total_tickets
86
                FROM $table";
87
88
        if (!in_array($direction, ['ASC', 'DESC'])) {
89
            $direction = 'ASC';
90
        }
91
        $column = (int) $column;
92
        $from = (int) $from;
93
        $numberItems = (int) $numberItems;
94
95
        //$sql .= " ORDER BY col$column $direction ";
96
        $sql .= " LIMIT $from,$numberItems";
97
98
        $result = Database::query($sql);
99
        $types = [];
100
        while ($row = Database::fetch_array($result)) {
101
            $types[] = $row;
102
        }
103
104
        return $types;
105
    }
106
107
    /**
108
     * @param int $id
109
     *
110
     * @return array|mixed
111
     */
112
    public static function getCategory($id)
113
    {
114
        $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
115
        $id = (int) $id;
116
        $sql = "SELECT id, name, description, total_tickets
117
                FROM $table WHERE id = $id";
118
119
        $result = Database::query($sql);
120
        $category = Database::fetch_array($result);
121
122
        return $category;
123
    }
124
125
    /**
126
     * @return int
127
     */
128
    public static function getCategoriesCount()
129
    {
130
        $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
131
132
        $sql = "SELECT count(id) count
133
                FROM $table ";
134
135
        $result = Database::query($sql);
136
        $category = Database::fetch_array($result);
137
138
        return $category['count'];
139
    }
140
141
    /**
142
     * @param int   $id
143
     * @param array $params
144
     */
145
    public static function updateCategory($id, $params)
146
    {
147
        $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
148
        $id = (int) $id;
149
        Database::update($table, $params, ['id = ?' => $id]);
150
    }
151
152
    /**
153
     * @param array $params
154
     */
155
    public static function addCategory($params)
156
    {
157
        $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
158
        Database::insert($table, $params);
159
    }
160
161
    /**
162
     * @param int $id
163
     *
164
     * @return bool
165
     */
166
    public static function deleteCategory($id)
167
    {
168
        $id = (int) $id;
169
        if (empty($id)) {
170
            return false;
171
        }
172
173
        $table = Database::get_main_table(TABLE_TICKET_TICKET);
174
        $sql = "UPDATE $table SET category_id = NULL WHERE category_id = $id";
175
        Database::query($sql);
176
177
        $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
178
        $sql = "DELETE FROM $table WHERE id = $id";
179
        Database::query($sql);
180
181
        return true;
182
    }
183
184
    /**
185
     * @param int   $categoryId
186
     * @param array $users
187
     *
188
     * @return bool
189
     */
190
    public static function addUsersToCategory($categoryId, $users)
191
    {
192
        if (empty($users) || empty($categoryId)) {
193
            return false;
194
        }
195
196
        $table = Database::get_main_table(TABLE_TICKET_CATEGORY_REL_USER);
197
        foreach ($users as $userId) {
198
            if (false === self::userIsAssignedToCategory($userId, $categoryId)) {
199
                $params = [
200
                    'category_id' => $categoryId,
201
                    'user_id' => $userId,
202
                ];
203
                Database::insert($table, $params);
204
            }
205
        }
206
207
        return true;
208
    }
209
210
    /**
211
     * @param int $userId
212
     * @param int $categoryId
213
     *
214
     * @return bool
215
     */
216
    public static function userIsAssignedToCategory($userId, $categoryId)
217
    {
218
        $table = Database::get_main_table(TABLE_TICKET_CATEGORY_REL_USER);
219
        $userId = (int) $userId;
220
        $categoryId = (int) $categoryId;
221
        $sql = "SELECT * FROM $table
222
                WHERE category_id = $categoryId AND user_id = $userId";
223
        $result = Database::query($sql);
224
225
        return Database::num_rows($result) > 0;
226
    }
227
228
    /**
229
     * @param int $categoryId
230
     *
231
     * @return array
232
     */
233
    public static function getUsersInCategory($categoryId)
234
    {
235
        $table = Database::get_main_table(TABLE_TICKET_CATEGORY_REL_USER);
236
        $categoryId = (int) $categoryId;
237
        $sql = "SELECT * FROM $table WHERE category_id = $categoryId";
238
        $result = Database::query($sql);
239
240
        return Database::store_result($result);
241
    }
242
243
    /**
244
     * @param int $categoryId
245
     */
246
    public static function deleteAllUserInCategory($categoryId)
247
    {
248
        $table = Database::get_main_table(TABLE_TICKET_CATEGORY_REL_USER);
249
        $categoryId = (int) $categoryId;
250
        $sql = "DELETE FROM $table WHERE category_id = $categoryId";
251
        Database::query($sql);
252
    }
253
254
    /**
255
     * Get all possible tickets statuses.
256
     *
257
     * @return array
258
     */
259
    public static function get_all_tickets_status()
260
    {
261
        $table = Database::get_main_table(TABLE_TICKET_STATUS);
262
        $sql = "SELECT * FROM $table";
263
        $result = Database::query($sql);
264
        $types = [];
265
        while ($row = Database::fetch_assoc($result)) {
266
            $types[] = $row;
267
        }
268
269
        return $types;
270
    }
271
272
    /**
273
     * Inserts a new ticket in the corresponding tables.
274
     *
275
     * @param int    $category_id
276
     * @param int    $course_id
277
     * @param int    $sessionId
278
     * @param int    $project_id
279
     * @param string $other_area
280
     * @param string $subject
281
     * @param string $content
282
     * @param string $personalEmail
283
     * @param array  $fileAttachments
284
     * @param string $source
285
     * @param string $priority
286
     * @param string $status
287
     * @param int    $assignedUserId
288
     * @param int    $exerciseId
289
     * @param int    $lpId
290
     *
291
     * @return bool
292
     */
293
    public static function add(
294
        $category_id,
295
        $course_id,
296
        $sessionId,
297
        $project_id,
298
        $other_area,
299
        $subject,
300
        $content,
301
        $personalEmail = '',
302
        $fileAttachments = [],
303
        $source = '',
304
        $priority = '',
305
        $status = '',
306
        $assignedUserId = 0,
307
        $exerciseId = null,
308
        $lpId = null
309
    ) {
310
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
311
        $table_support_category = Database::get_main_table(TABLE_TICKET_CATEGORY);
312
313
        if (empty($category_id)) {
314
            return false;
315
        }
316
317
        $currentUserId = api_get_user_id();
318
        $currentUserInfo = api_get_user_info();
319
        $now = api_get_utc_datetime();
320
        $course_id = (int) $course_id;
321
        $category_id = (int) $category_id;
322
        $project_id = (int) $project_id;
323
        $priority = empty($priority) ? self::PRIORITY_NORMAL : (int) $priority;
324
325
        if ('' === $status) {
326
            $status = self::STATUS_NEW;
327
            if ($other_area > 0) {
328
                $status = self::STATUS_FORWARDED;
329
            }
330
        }
331
332
        if (!empty($category_id)) {
333
            if (empty($assignedUserId)) {
334
                $usersInCategory = self::getUsersInCategory($category_id);
335
                if (!empty($usersInCategory) && count($usersInCategory) > 0) {
336
                    $userCategoryInfo = $usersInCategory[0];
337
                    if (isset($userCategoryInfo['user_id'])) {
338
                        $assignedUserId = $userCategoryInfo['user_id'];
339
                    }
340
                }
341
            }
342
        }
343
344
        $assignedUserInfo = [];
345
        if (!empty($assignedUserId)) {
346
            $assignedUserInfo = api_get_user_info($assignedUserId);
347
            if (empty($assignedUserInfo)) {
348
                return false;
349
            }
350
        }
351
352
        // insert_ticket
353
        $params = [
354
            'project_id' => $project_id,
355
            'category_id' => $category_id,
356
            'priority_id' => $priority,
357
            'personal_email' => $personalEmail,
358
            'status_id' => $status,
359
            'start_date' => $now,
360
            'sys_insert_user_id' => $currentUserId,
361
            'sys_insert_datetime' => $now,
362
            'sys_lastedit_user_id' => $currentUserId,
363
            'sys_lastedit_datetime' => $now,
364
            'source' => $source,
365
            'assigned_last_user' => $assignedUserId,
366
            'subject' => $subject,
367
            'message' => $content,
368
            'code' => '',
369
            'total_messages' => 0,
370
        ];
371
372
        if (!empty($exerciseId)) {
373
            $params['exercise_id'] = $exerciseId;
374
        }
375
376
        if (!empty($lpId)) {
377
            $params['lp_id'] = $lpId;
378
        }
379
        if (!empty($course_id)) {
380
            $params['course_id'] = $course_id;
381
        }
382
383
        if (!empty($sessionId)) {
384
            $params['session_id'] = $sessionId;
385
        }
386
        $ticketId = Database::insert($table_support_tickets, $params);
387
388
        if ($ticketId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ticketId of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
389
            $ticket_code = 'A'.str_pad($ticketId, 11, '0', STR_PAD_LEFT);
390
            $titleCreated = sprintf(
391
                get_lang('Ticket %s created'),
392
                $ticket_code
393
            );
394
395
            Display::addFlash(Display::return_message(
396
                $titleCreated,
397
                'normal',
398
                false
399
            ));
400
401
            if (0 != $assignedUserId) {
402
                self::assignTicketToUser(
403
                    $ticketId,
404
                    $assignedUserId
405
                );
406
407
                Display::addFlash(Display::return_message(
408
                    sprintf(
409
                        get_lang('TicketXAssignedToUserX'),
410
                        $ticket_code,
411
                        $assignedUserInfo['complete_name']
412
                    ),
413
                    'normal',
414
                    false
415
                ));
416
            }
417
418
            if (!empty($fileAttachments)) {
419
                $attachmentCount = 0;
420
                foreach ($fileAttachments as $attach) {
421
                    if (!empty($attach['tmp_name'])) {
422
                        $attachmentCount++;
423
                    }
424
                }
425
                if ($attachmentCount > 0) {
426
                    self::insertMessage(
427
                        $ticketId,
428
                        '',
429
                        '',
430
                        $fileAttachments,
431
                        $currentUserId
432
                    );
433
                }
434
            }
435
436
            // Update code
437
            $sql = "UPDATE $table_support_tickets
438
                    SET code = '$ticket_code'
439
                    WHERE id = '$ticketId'";
440
            Database::query($sql);
441
442
            // Update total
443
            $sql = "UPDATE $table_support_category
444
                    SET total_tickets = total_tickets + 1
445
                    WHERE id = $category_id";
446
            Database::query($sql);
447
448
            $helpDeskMessage =
449
                '<table>
450
                        <tr>
451
                            <td width="100px"><b>'.get_lang('User').'</b></td>
452
                            <td width="400px">'.$currentUserInfo['complete_name'].'</td>
453
                        </tr>
454
                        <tr>
455
                            <td width="100px"><b>'.get_lang('Username').'</b></td>
456
                            <td width="400px">'.$currentUserInfo['username'].'</td>
457
                        </tr>
458
                        <tr>
459
                            <td width="100px"><b>'.get_lang('Email').'</b></td>
460
                            <td width="400px">'.$currentUserInfo['email'].'</td>
461
                        </tr>
462
                        <tr>
463
                            <td width="100px"><b>'.get_lang('Phone').'</b></td>
464
                            <td width="400px">'.$currentUserInfo['phone'].'</td>
465
                        </tr>
466
                        <tr>
467
                            <td width="100px"><b>'.get_lang('Date').'</b></td>
468
                            <td width="400px">'.api_convert_and_format_date($now, DATE_TIME_FORMAT_LONG).'</td>
469
                        </tr>
470
                        <tr>
471
                            <td width="100px"><b>'.get_lang('Title').'</b></td>
472
                            <td width="400px">'.Security::remove_XSS($subject).'</td>
473
                        </tr>
474
                        <tr>
475
                            <td width="100px"><b>'.get_lang('Description').'</b></td>
476
                            <td width="400px">'.Security::remove_XSS($content).'</td>
477
                        </tr>
478
                    </table>';
479
480
            if (0 != $assignedUserId) {
481
                $href = api_get_path(WEB_CODE_PATH).'/ticket/ticket_details.php?ticket_id='.$ticketId;
482
                $helpDeskMessage .= sprintf(
483
                    get_lang('TicketAssignedToXCheckZAtLinkY'),
484
                    $assignedUserInfo['complete_name'],
485
                    $href,
486
                    $ticketId
487
                );
488
            }
489
490
            if (empty($category_id)) {
491
                if ('true' === api_get_setting('ticket_send_warning_to_all_admins')) {
492
                    $warningSubject = sprintf(
493
                        get_lang('Ticket %s createdWithNoCategory'),
494
                        $ticket_code
495
                    );
496
                    Display::addFlash(Display::return_message($warningSubject));
497
498
                    $admins = UserManager::get_all_administrators();
499
                    foreach ($admins as $userId => $data) {
500
                        if ($data['active']) {
501
                            MessageManager::send_message_simple(
502
                                $userId,
503
                                $warningSubject,
504
                                $helpDeskMessage
505
                            );
506
                        }
507
                    }
508
                }
509
            } else {
510
                $categoryInfo = self::getCategory($category_id);
511
                $usersInCategory = self::getUsersInCategory($category_id);
512
513
                $message = '<h2>'.get_lang('Ticket info').'</h2><br />'.$helpDeskMessage;
514
515
                if ('true' === api_get_setting('ticket_warn_admin_no_user_in_category')) {
516
                    $usersInCategory = self::getUsersInCategory($category_id);
517
                    if (empty($usersInCategory)) {
518
                        $subject = sprintf(
519
                            get_lang('Warning: No one has been assigned to category %s'),
520
                            $categoryInfo['name']
521
                        );
522
523
                        if ('true' === api_get_setting('ticket_send_warning_to_all_admins')) {
524
                            Display::addFlash(Display::return_message(
525
                                sprintf(
526
                                    get_lang('A notification was sent to the administrators to report this category has no user assigned'),
527
                                    $categoryInfo['name']
528
                                ),
529
                                null,
530
                                false
531
                            ));
532
533
                            $admins = UserManager::get_all_administrators();
534
                            foreach ($admins as $userId => $data) {
535
                                if ($data['active']) {
536
                                    self::sendNotification(
537
                                        $ticketId,
538
                                        $subject,
539
                                        $message,
540
                                        $userId
541
                                    );
542
                                }
543
                            }
544
                        } else {
545
                            Display::addFlash(Display::return_message($subject));
546
                        }
547
                    }
548
                }
549
550
                // Send notification to all users
551
                if (!empty($usersInCategory)) {
552
                    foreach ($usersInCategory as $data) {
553
                        if ($data['user_id']) {
554
                            self::sendNotification(
555
                                $ticketId,
556
                                $subject,
557
                                $message,
558
                                $data['user_id']
559
                            );
560
                        }
561
                    }
562
                }
563
            }
564
565
            if (!empty($personalEmail)) {
566
                api_mail_html(
567
                    get_lang('Virtual support'),
568
                    $personalEmail,
569
                    get_lang('IncidentResentToVirtual support'),
570
                    $helpDeskMessage
571
                );
572
            }
573
574
            self::sendNotification(
575
                $ticketId,
576
                $titleCreated,
577
                $helpDeskMessage
578
            );
579
580
            return true;
581
        }
582
583
        return false;
584
    }
585
586
    /**
587
     * Assign ticket to admin.
588
     *
589
     * @param int $ticketId
590
     * @param int $userId
591
     *
592
     * @return bool
593
     */
594
    public static function assignTicketToUser(
595
        $ticketId,
596
        $userId
597
    ) {
598
        $ticketId = (int) $ticketId;
599
        $userId = (int) $userId;
600
601
        if (empty($ticketId)) {
602
            return false;
603
        }
604
605
        $ticket = self::get_ticket_detail_by_id($ticketId);
606
607
        if ($ticket) {
608
            $table = Database::get_main_table(TABLE_TICKET_TICKET);
609
            $sql = "UPDATE $table
610
                    SET assigned_last_user = $userId
611
                    WHERE id = $ticketId";
612
            Database::query($sql);
613
614
            $table = Database::get_main_table(TABLE_TICKET_ASSIGNED_LOG);
615
            $params = [
616
                'ticket_id' => $ticketId,
617
                'user_id' => $userId,
618
                'sys_insert_user_id' => api_get_user_id(),
619
                'assigned_date' => api_get_utc_datetime(),
620
            ];
621
            Database::insert($table, $params);
622
623
            return true;
624
        } else {
625
            return false;
626
        }
627
    }
628
629
    /**
630
     * Insert message between Users and Admins.
631
     *
632
     * @param int    $ticketId
633
     * @param string $subject
634
     * @param string $content
635
     * @param array  $fileAttachments
636
     * @param int    $userId
637
     * @param string $status
638
     * @param bool   $sendConfirmation
639
     *
640
     * @return bool
641
     */
642
    public static function insertMessage(
643
        $ticketId,
644
        $subject,
645
        $content,
646
        $fileAttachments,
647
        $userId,
648
        $status = 'NOL',
649
        $sendConfirmation = false
650
    ) {
651
        $ticketId = (int) $ticketId;
652
        $userId = (int) $userId;
653
        $table_support_messages = Database::get_main_table(TABLE_TICKET_MESSAGE);
654
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
655
        if ($sendConfirmation) {
656
            $form =
657
                '<form action="ticket_details.php?ticket_id='.$ticketId.'" id="confirmticket" method="POST" >
658
                     <p>'.get_lang('Was this answer satisfactory?').'</p>
659
                     <button class="btn btn-primary responseyes" name="response" id="responseyes" value="1">'.
660
                get_lang('Yes').'</button>
661
                     <button class="btn btn-danger responseno" name="response" id="responseno" value="0">'.
662
                get_lang('No').'</button>
663
                 </form>';
664
            $content .= $form;
665
        }
666
667
        $now = api_get_utc_datetime();
668
669
        $params = [
670
            'ticket_id' => $ticketId,
671
            'subject' => $subject,
672
            'message' => $content,
673
            'ip_address' => api_get_real_ip(),
674
            'sys_insert_user_id' => $userId,
675
            'sys_insert_datetime' => $now,
676
            'sys_lastedit_user_id' => $userId,
677
            'sys_lastedit_datetime' => $now,
678
            'status' => $status,
679
        ];
680
        $messageId = Database::insert($table_support_messages, $params);
681
        if ($messageId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $messageId of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
682
            // update_total_message
683
            $sql = "UPDATE $table_support_tickets
684
                    SET
685
                        sys_lastedit_user_id = $userId,
686
                        sys_lastedit_datetime = '$now',
687
                        total_messages = (
688
                            SELECT COUNT(*) as total_messages
689
                            FROM $table_support_messages
690
                            WHERE ticket_id = $ticketId
691
                        )
692
                    WHERE id = $ticketId ";
693
            Database::query($sql);
694
695
            if (is_array($fileAttachments)) {
696
                foreach ($fileAttachments as $file_attach) {
697
                    if (0 == $file_attach['error']) {
698
                        self::saveMessageAttachmentFile(
699
                            $file_attach,
700
                            $ticketId,
701
                            $messageId
702
                        );
703
                    } else {
704
                        if (UPLOAD_ERR_NO_FILE != $file_attach['error']) {
705
                            return false;
706
                        }
707
                    }
708
                }
709
            }
710
        }
711
712
        return true;
713
    }
714
715
    /**
716
     * Attachment files when a message is sent.
717
     *
718
     * @param $file_attach
719
     * @param $ticketId
720
     * @param $message_id
721
     *
722
     * @return bool
723
     */
724
    public static function saveMessageAttachmentFile(
725
        $file_attach,
726
        $ticketId,
727
        $message_id
728
    ) {
729
        $now = api_get_utc_datetime();
730
        $userId = api_get_user_id();
731
        $ticketId = (int) $ticketId;
732
733
        $new_file_name = add_ext_on_mime(
734
            stripslashes($file_attach['name']),
735
            $file_attach['type']
736
        );
737
        $table_support_message_attachments = Database::get_main_table(TABLE_TICKET_MESSAGE_ATTACHMENTS);
738
        if (!filter_extension($new_file_name)) {
739
            echo Display::return_message(
740
                get_lang('File upload failed: this file extension or file type is prohibited'),
741
                'error'
742
            );
743
        } else {
744
            $result = api_upload_file('ticket_attachment', $file_attach, $ticketId);
745
            if ($result) {
746
                $safe_file_name = Database::escape_string($new_file_name);
747
                $safe_new_file_name = Database::escape_string($result['path_to_save']);
748
                $sql = "INSERT INTO $table_support_message_attachments (
749
                        filename,
750
                        path,
751
                        ticket_id,
752
                        message_id,
753
                        size,
754
                        sys_insert_user_id,
755
                        sys_insert_datetime,
756
                        sys_lastedit_user_id,
757
                        sys_lastedit_datetime
758
                    ) VALUES (
759
                        '$safe_file_name',
760
                        '$safe_new_file_name',
761
                        '$ticketId',
762
                        '$message_id',
763
                        '".$file_attach['size']."',
764
                        '$userId',
765
                        '$now',
766
                        '$userId',
767
                        '$now'
768
                    )";
769
                Database::query($sql);
770
771
                return true;
772
            }
773
        }
774
    }
775
776
    /**
777
     * Get tickets by userId.
778
     *
779
     * @param int $from
780
     * @param int $number_of_items
781
     * @param $column
782
     * @param $direction
783
     *
784
     * @return array
785
     */
786
    public static function getTicketsByCurrentUser(
787
        $from,
788
        $number_of_items,
789
        $column,
790
        $direction
791
    ) {
792
        $table_support_category = Database::get_main_table(TABLE_TICKET_CATEGORY);
793
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
794
        $table_support_priority = Database::get_main_table(TABLE_TICKET_PRIORITY);
795
        $table_support_status = Database::get_main_table(TABLE_TICKET_STATUS);
796
        $direction = !empty($direction) ? $direction : 'DESC';
797
        $userId = api_get_user_id();
798
        $userInfo = api_get_user_info($userId);
799
800
        if (empty($userInfo)) {
801
            return [];
802
        }
803
        $isAdmin = UserManager::is_admin($userId);
804
805
        if (!isset($_GET['project_id'])) {
806
            return [];
807
        }
808
809
        switch ($column) {
810
            case 0:
811
                $column = 'ticket_id';
812
                break;
813
            case 1:
814
                $column = 'status_name';
815
                break;
816
            case 2:
817
                $column = 'start_date';
818
                break;
819
            case 3:
820
                $column = 'sys_lastedit_datetime';
821
                break;
822
            case 4:
823
                $column = 'category_name';
824
                break;
825
            case 5:
826
                $column = 'sys_insert_user_id';
827
                break;
828
            case 6:
829
                $column = 'assigned_last_user';
830
                break;
831
            case 7:
832
                $column = 'total_messages';
833
                break;
834
            case 8:
835
                $column = 'subject';
836
                break;
837
            default:
838
                $column = 'ticket_id';
839
        }
840
841
        $sql = "SELECT DISTINCT
842
                ticket.*,
843
                ticket.id ticket_id,
844
                status.name AS status_name,
845
                ticket.start_date,
846
                ticket.sys_lastedit_datetime,
847
                cat.name AS category_name,
848
                priority.name AS priority_name,
849
                ticket.total_messages AS total_messages,
850
                ticket.message AS message,
851
                ticket.subject AS subject,
852
                ticket.assigned_last_user
853
            FROM $table_support_tickets ticket
854
            INNER JOIN $table_support_category cat
855
            ON (cat.id = ticket.category_id)
856
            INNER JOIN $table_support_priority priority
857
            ON (ticket.priority_id = priority.id)
858
            INNER JOIN $table_support_status status
859
            ON (ticket.status_id = status.id)
860
            WHERE 1=1
861
        ";
862
863
        $projectId = (int) $_GET['project_id'];
864
        $userIsAllowInProject = self::userIsAllowInProject($userInfo, $projectId);
865
866
        // Check if a role was set to the project
867
        if (false == $userIsAllowInProject) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
868
            $sql .= " AND (ticket.assigned_last_user = $userId OR ticket.sys_insert_user_id = $userId )";
869
        }
870
871
        // Search simple
872
        if (isset($_GET['submit_simple']) && '' != $_GET['keyword']) {
873
            $keyword = Database::escape_string(trim($_GET['keyword']));
874
            $sql .= " AND (
875
                      ticket.id LIKE '%$keyword%' OR
876
                      ticket.code LIKE '%$keyword%' OR
877
                      ticket.subject LIKE '%$keyword%' OR
878
                      ticket.message LIKE '%$keyword%' OR
879
                      ticket.keyword LIKE '%$keyword%' OR
880
                      ticket.source LIKE '%$keyword%' OR
881
                      cat.name LIKE '%$keyword%' OR
882
                      status.name LIKE '%$keyword%' OR
883
                      priority.name LIKE '%$keyword%' OR
884
                      ticket.personal_email LIKE '%$keyword%'
885
            )";
886
        }
887
888
        $keywords = [
889
            'project_id' => 'ticket.project_id',
890
            'keyword_category' => 'ticket.category_id',
891
            'keyword_assigned_to' => 'ticket.assigned_last_user',
892
            'keyword_source' => 'ticket.source ',
893
            'keyword_status' => 'ticket.status_id',
894
            'keyword_priority' => 'ticket.priority_id',
895
        ];
896
897
        foreach ($keywords as $keyword => $label) {
898
            if (isset($_GET[$keyword])) {
899
                $data = Database::escape_string(trim($_GET[$keyword]));
900
                if (!empty($data)) {
901
                    $sql .= " AND $label = '$data' ";
902
                }
903
            }
904
        }
905
906
        // Search advanced
907
        $keyword_start_date_start = isset($_GET['keyword_start_date_start']) ? Database::escape_string(trim($_GET['keyword_start_date_start'])) : '';
908
        $keyword_start_date_end = isset($_GET['keyword_start_date_end']) ? Database::escape_string(trim($_GET['keyword_start_date_end'])) : '';
909
        $keyword_course = isset($_GET['keyword_course']) ? Database::escape_string(trim($_GET['keyword_course'])) : '';
910
        $keyword_range = !empty($keyword_start_date_start) && !empty($keyword_start_date_end);
911
912
        if (false == $keyword_range && '' != $keyword_start_date_start) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
913
            $sql .= " AND DATE_FORMAT(ticket.start_date,'%d/%m/%Y') >= '$keyword_start_date_start' ";
914
        }
915
        if ($keyword_range && '' != $keyword_start_date_start && '' != $keyword_start_date_end) {
916
            $sql .= " AND DATE_FORMAT(ticket.start_date,'%d/%m/%Y') >= '$keyword_start_date_start'
917
                      AND DATE_FORMAT(ticket.start_date,'%d/%m/%Y') <= '$keyword_start_date_end'";
918
        }
919
920
        if ('' != $keyword_course) {
921
            $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
922
            $sql .= " AND ticket.course_id IN (
923
                     SELECT id FROM $course_table
924
                     WHERE (
925
                        title LIKE '%$keyword_course%' OR
926
                        code LIKE '%$keyword_course%' OR
927
                        visual_code LIKE '%$keyword_course%'
928
                     )
929
            )";
930
        }
931
        $sql .= " ORDER BY $column $direction";
932
        $sql .= " LIMIT $from, $number_of_items";
933
934
        $result = Database::query($sql);
935
        $tickets = [];
936
        $webPath = api_get_path(WEB_PATH);
937
        while ($row = Database::fetch_assoc($result)) {
938
            $userInfo = api_get_user_info($row['sys_insert_user_id']);
939
            $hrefUser = $webPath.'main/admin/user_information.php?user_id='.$userInfo['user_id'];
940
            $name = "<a href='$hrefUser'> {$userInfo['complete_name_with_username']} </a>";
941
            if (0 != $row['assigned_last_user']) {
942
                $assignedUserInfo = api_get_user_info($row['assigned_last_user']);
943
                if (!empty($assignedUserInfo)) {
944
                    $hrefResp = $webPath.'main/admin/user_information.php?user_id='.$assignedUserInfo['user_id'];
945
                    $row['assigned_last_user'] = "<a href='$hrefResp'> {$assignedUserInfo['complete_name_with_username']} </a>";
946
                } else {
947
                    $row['assigned_last_user'] = get_lang('Unknown user');
948
                }
949
            } else {
950
                if (self::STATUS_FORWARDED !== $row['status_id']) {
951
                    $row['assigned_last_user'] = '<span style="color:#ff0000;">'.get_lang('To be assigned').'</span>';
952
                } else {
953
                    $row['assigned_last_user'] = '<span style="color:#00ff00;">'.get_lang('Message resent').'</span>';
954
                }
955
            }
956
957
            switch ($row['source']) {
958
                case self::SOURCE_PRESENTIAL:
959
                    $img_source = 'icons/32/user.png';
960
                    break;
961
                case self::SOURCE_EMAIL:
962
                    $img_source = 'icons/32/mail.png';
963
                    break;
964
                case self::SOURCE_PHONE:
965
                    $img_source = 'icons/32/event.png';
966
                    break;
967
                default:
968
                    $img_source = 'icons/32/ticket.png';
969
                    break;
970
            }
971
972
            $row['start_date'] = Display::dateToStringAgoAndLongDate($row['start_date']);
973
            $row['sys_lastedit_datetime'] = Display::dateToStringAgoAndLongDate($row['sys_lastedit_datetime']);
974
975
            $icon = Display::return_icon(
976
                $img_source,
977
                get_lang('Information'),
978
                ['style' => 'margin-right: 10px; float: left;']
979
            );
980
981
            $icon .= '<a href="ticket_details.php?ticket_id='.$row['id'].'">'.$row['code'].'</a>';
982
983
            if ($isAdmin) {
984
                $ticket = [
985
                    $icon.' '.Security::remove_XSS($row['subject']),
986
                    $row['status_name'],
987
                    $row['start_date'],
988
                    $row['sys_lastedit_datetime'],
989
                    $row['category_name'],
990
                    $name,
991
                    $row['assigned_last_user'],
992
                    $row['total_messages'],
993
                ];
994
            } else {
995
                $ticket = [
996
                    $icon.' '.Security::remove_XSS($row['subject']),
997
                    $row['status_name'],
998
                    $row['start_date'],
999
                    $row['sys_lastedit_datetime'],
1000
                    $row['category_name'],
1001
                ];
1002
            }
1003
            if ($isAdmin) {
1004
                $ticket['0'] .= '&nbsp;&nbsp;<a href="javascript:void(0)" onclick="load_history_ticket(\'div_'.$row['ticket_id'].'\','.$row['ticket_id'].')">
1005
					<img onclick="load_course_list(\'div_'.$row['ticket_id'].'\','.$row['ticket_id'].')" onmouseover="clear_course_list (\'div_'.$row['ticket_id'].'\')" src="'.Display::returnIconPath('history.gif').'" title="'.get_lang('History').'" alt="'.get_lang('History').'"/>
1006
					<div class="blackboard_hide" id="div_'.$row['ticket_id'].'">&nbsp;&nbsp;</div>
1007
					</a>&nbsp;&nbsp;';
1008
            }
1009
            $tickets[] = $ticket;
1010
        }
1011
1012
        return $tickets;
1013
    }
1014
1015
    /**
1016
     * @return int
1017
     */
1018
    public static function getTotalTicketsCurrentUser()
1019
    {
1020
        $table_support_category = Database::get_main_table(TABLE_TICKET_CATEGORY);
1021
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
1022
        $table_support_priority = Database::get_main_table(TABLE_TICKET_PRIORITY);
1023
        $table_support_status = Database::get_main_table(TABLE_TICKET_STATUS);
1024
1025
        $userInfo = api_get_user_info();
1026
        if (empty($userInfo)) {
1027
            return 0;
1028
        }
1029
        $userId = $userInfo['id'];
1030
1031
        if (!isset($_GET['project_id'])) {
1032
            return 0;
1033
        }
1034
1035
        $sql = "SELECT COUNT(ticket.id) AS total
1036
                FROM $table_support_tickets ticket
1037
                INNER JOIN $table_support_category cat
1038
                ON (cat.id = ticket.category_id)
1039
                INNER JOIN $table_support_priority priority
1040
                ON (ticket.priority_id = priority.id)
1041
                INNER JOIN $table_support_status status
1042
                ON (ticket.status_id = status.id)
1043
	            WHERE 1 = 1";
1044
1045
        $projectId = (int) $_GET['project_id'];
1046
        $allowRoleList = self::getAllowedRolesFromProject($projectId);
1047
1048
        // Check if a role was set to the project
1049
        if (!empty($allowRoleList) && is_array($allowRoleList)) {
1050
            if (!in_array($userInfo['status'], $allowRoleList)) {
1051
                $sql .= " AND (ticket.assigned_last_user = $userId OR ticket.sys_insert_user_id = $userId )";
1052
            }
1053
        } else {
1054
            if (!api_is_platform_admin()) {
1055
                $sql .= " AND (ticket.assigned_last_user = $userId OR ticket.sys_insert_user_id = $userId )";
1056
            }
1057
        }
1058
1059
        // Search simple
1060
        if (isset($_GET['submit_simple'])) {
1061
            if ('' != $_GET['keyword']) {
1062
                $keyword = Database::escape_string(trim($_GET['keyword']));
1063
                $sql .= " AND (
1064
                          ticket.code LIKE '%$keyword%' OR
1065
                          ticket.subject LIKE '%$keyword%' OR
1066
                          ticket.message LIKE '%$keyword%' OR
1067
                          ticket.keyword LIKE '%$keyword%' OR
1068
                          ticket.personal_email LIKE '%$keyword%' OR
1069
                          ticket.source LIKE '%$keyword%'
1070
                )";
1071
            }
1072
        }
1073
1074
        $keywords = [
1075
            'project_id' => 'ticket.project_id',
1076
            'keyword_category' => 'ticket.category_id',
1077
            'keyword_assigned_to' => 'ticket.assigned_last_user',
1078
            'keyword_source' => 'ticket.source',
1079
            'keyword_status' => 'ticket.status_id',
1080
            'keyword_priority' => 'ticket.priority_id',
1081
        ];
1082
1083
        foreach ($keywords as $keyword => $sqlLabel) {
1084
            if (isset($_GET[$keyword])) {
1085
                $data = Database::escape_string(trim($_GET[$keyword]));
1086
                $sql .= " AND $sqlLabel = '$data' ";
1087
            }
1088
        }
1089
1090
        // Search advanced
1091
        $keyword_start_date_start = isset($_GET['keyword_start_date_start']) ? Database::escape_string(trim($_GET['keyword_start_date_start'])) : '';
1092
        $keyword_start_date_end = isset($_GET['keyword_start_date_end']) ? Database::escape_string(trim($_GET['keyword_start_date_end'])) : '';
1093
        $keyword_range = isset($_GET['keyword_dates']) ? Database::escape_string(trim($_GET['keyword_dates'])) : '';
1094
        $keyword_course = isset($_GET['keyword_course']) ? Database::escape_string(trim($_GET['keyword_course'])) : '';
1095
1096
        if (false == $keyword_range && '' != $keyword_start_date_start) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $keyword_range of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
1097
            $sql .= " AND DATE_FORMAT( ticket.start_date,'%d/%m/%Y') = '$keyword_start_date_start' ";
1098
        }
1099
        if ($keyword_range && '' != $keyword_start_date_start && '' != $keyword_start_date_end) {
1100
            $sql .= " AND DATE_FORMAT( ticket.start_date,'%d/%m/%Y') >= '$keyword_start_date_start'
1101
                      AND DATE_FORMAT( ticket.start_date,'%d/%m/%Y') <= '$keyword_start_date_end'";
1102
        }
1103
        if ('' != $keyword_course) {
1104
            $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
1105
            $sql .= " AND ticket.course_id IN (
1106
                        SELECT id
1107
                        FROM $course_table
1108
                        WHERE (
1109
                            title LIKE '%$keyword_course%' OR
1110
                            code LIKE '%$keyword_course%' OR
1111
                            visual_code LIKE '%$keyword_course%'
1112
                        )
1113
                   ) ";
1114
        }
1115
1116
        $res = Database::query($sql);
1117
        $obj = Database::fetch_object($res);
1118
1119
        return (int) $obj->total;
1120
    }
1121
1122
    /**
1123
     * @param int $id
1124
     *
1125
     * @return false|TicketMessageAttachment
1126
     */
1127
    public static function getTicketMessageAttachment($id)
1128
    {
1129
        $id = (int) $id;
1130
        $em = Database::getManager();
1131
        $item = $em->getRepository('TicketMessageAttachment')->find($id);
1132
        if ($item) {
1133
            return $item;
1134
        }
1135
1136
        return false;
1137
    }
1138
1139
    /**
1140
     * @param int $id
1141
     *
1142
     * @return array
1143
     */
1144
    public static function getTicketMessageAttachmentsByTicketId($id)
1145
    {
1146
        $id = (int) $id;
1147
        $em = Database::getManager();
1148
        $items = $em->getRepository('TicketMessageAttachment')->findBy(['ticket' => $id]);
1149
        if ($items) {
1150
            return $items;
1151
        }
1152
1153
        return false;
1154
    }
1155
1156
    /**
1157
     * @param int $ticketId
1158
     *
1159
     * @return array
1160
     */
1161
    public static function get_ticket_detail_by_id($ticketId)
1162
    {
1163
        $ticketId = (int) $ticketId;
1164
        $table_support_category = Database::get_main_table(TABLE_TICKET_CATEGORY);
1165
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
1166
        $table_support_priority = Database::get_main_table(TABLE_TICKET_PRIORITY);
1167
        $table_support_status = Database::get_main_table(TABLE_TICKET_STATUS);
1168
        $table_support_messages = Database::get_main_table(TABLE_TICKET_MESSAGE);
1169
        $table_support_message_attachments = Database::get_main_table(TABLE_TICKET_MESSAGE_ATTACHMENTS);
1170
        $table_main_user = Database::get_main_table(TABLE_MAIN_USER);
1171
1172
        $sql = "SELECT
1173
                    ticket.*,
1174
                    cat.name,
1175
                    status.name as status,
1176
                    priority.name priority
1177
                FROM $table_support_tickets ticket
1178
                INNER JOIN $table_support_category cat
1179
                ON (cat.id = ticket.category_id)
1180
                INNER JOIN $table_support_priority priority
1181
                ON (priority.id = ticket.priority_id)
1182
                INNER JOIN $table_support_status status
1183
                ON (status.id = ticket.status_id)
1184
		        WHERE
1185
                    ticket.id = $ticketId ";
1186
        $result = Database::query($sql);
1187
        $ticket = [];
1188
        if (Database::num_rows($result) > 0) {
1189
            while ($row = Database::fetch_assoc($result)) {
1190
                $row['course'] = null;
1191
                $row['start_date_from_db'] = $row['start_date'];
1192
                $row['start_date'] = api_convert_and_format_date(
1193
                    api_get_local_time($row['start_date']),
1194
                    DATE_TIME_FORMAT_LONG,
1195
                    api_get_timezone()
1196
                );
1197
                $row['end_date_from_db'] = $row['end_date'];
1198
                $row['end_date'] = api_convert_and_format_date(
1199
                    api_get_local_time($row['end_date']),
1200
                    DATE_TIME_FORMAT_LONG,
1201
                    api_get_timezone()
1202
                );
1203
                $row['sys_lastedit_datetime_from_db'] = $row['sys_lastedit_datetime'];
1204
                $row['sys_lastedit_datetime'] = api_convert_and_format_date(
1205
                    api_get_local_time($row['sys_lastedit_datetime']),
1206
                    DATE_TIME_FORMAT_LONG,
1207
                    api_get_timezone()
1208
                );
1209
                $row['course_url'] = null;
1210
                if (0 != $row['course_id']) {
1211
                    $course = api_get_course_info_by_id($row['course_id']);
1212
                    $sessionId = 0;
1213
                    if ($row['session_id']) {
1214
                        $sessionId = $row['session_id'];
1215
                    }
1216
                    if ($course) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $course of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1217
                        $row['course_url'] = '<a href="'.$course['course_public_url'].'?id_session='.$sessionId.'">'.$course['name'].'</a>';
1218
                    }
1219
                    $row['exercise_url'] = null;
1220
1221
                    if (!empty($row['exercise_id'])) {
1222
                        $exerciseTitle = ExerciseLib::getExerciseTitleById($row['exercise_id']);
0 ignored issues
show
Bug introduced by
The method getExerciseTitleById() does not exist on ExerciseLib. ( Ignorable by Annotation )

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

1222
                        /** @scrutinizer ignore-call */ 
1223
                        $exerciseTitle = ExerciseLib::getExerciseTitleById($row['exercise_id']);

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

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

Loading history...
1223
                        $dataExercise = [
1224
                            'cidReq' => $course['code'],
1225
                            'id_session' => $sessionId,
1226
                            'exerciseId' => $row['exercise_id'],
1227
                        ];
1228
                        $urlParamsExercise = http_build_query($dataExercise);
1229
1230
                        $row['exercise_url'] = '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/overview.php?'.$urlParamsExercise.'">'.$exerciseTitle.'</a>';
1231
                    }
1232
1233
                    $row['lp_url'] = null;
1234
1235
                    if (!empty($row['lp_id'])) {
1236
                        $lpName = learnpath::getLpNameById($row['lp_id']);
1237
                        $dataLp = [
1238
                            'cidReq' => $course['code'],
1239
                            'id_session' => $sessionId,
1240
                            'lp_id' => $row['lp_id'],
1241
                            'action' => 'view',
1242
                        ];
1243
                        $urlParamsLp = http_build_query($dataLp);
1244
1245
                        $row['lp_url'] = '<a href="'.api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.$urlParamsLp.'">'.$lpName.'</a>';
1246
                    }
1247
                }
1248
1249
                $userInfo = api_get_user_info($row['sys_insert_user_id']);
1250
                $row['user_url'] = '<a href="'.api_get_path(WEB_PATH).'main/admin/user_information.php?user_id='.$userInfo['user_id'].'">
1251
                '.$userInfo['complete_name'].'</a>';
1252
                $ticket['usuario'] = $userInfo;
1253
                $ticket['ticket'] = $row;
1254
            }
1255
1256
            $sql = "SELECT *, message.id as message_id
1257
                    FROM $table_support_messages message
1258
                    INNER JOIN $table_main_user user
1259
                    ON (message.sys_insert_user_id = user.user_id)
1260
                    WHERE
1261
                        message.ticket_id = '$ticketId' ";
1262
            $result = Database::query($sql);
1263
            $ticket['messages'] = [];
1264
            $attach_icon = Display::return_icon('attachment.gif', '');
1265
            $webPath = api_get_path(WEB_CODE_PATH);
1266
            while ($row = Database::fetch_assoc($result)) {
1267
                $message = $row;
1268
                $message['admin'] = UserManager::is_admin($message['user_id']);
1269
                $message['user_info'] = api_get_user_info($message['user_id']);
1270
                $sql = "SELECT *
1271
                        FROM $table_support_message_attachments
1272
                        WHERE
1273
                            message_id = ".$row['message_id']." AND
1274
                            ticket_id = $ticketId";
1275
1276
                $result_attach = Database::query($sql);
1277
                while ($row2 = Database::fetch_assoc($result_attach)) {
1278
                    $archiveURL = $webPath.'ticket/download.php?ticket_id='.$ticketId.'&id='.$row2['id'];
1279
                    $row2['attachment_link'] = $attach_icon.
1280
                        '&nbsp;<a href="'.$archiveURL.'">'.$row2['filename'].'</a>&nbsp;('.$row2['size'].')';
1281
                    $message['attachments'][] = $row2;
1282
                }
1283
                $ticket['messages'][] = $message;
1284
            }
1285
        }
1286
1287
        return $ticket;
1288
    }
1289
1290
    /**
1291
     * @param int $ticketId
1292
     * @param int $userId
1293
     *
1294
     * @return bool
1295
     */
1296
    public static function update_message_status($ticketId, $userId)
1297
    {
1298
        $ticketId = (int) $ticketId;
1299
        $userId = (int) $userId;
1300
        $table_support_messages = Database::get_main_table(TABLE_TICKET_MESSAGE);
1301
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
1302
        $now = api_get_utc_datetime();
1303
        $sql = "UPDATE $table_support_messages
1304
                SET
1305
                    status = 'LEI',
1306
                    sys_lastedit_user_id ='".api_get_user_id()."',
1307
                    sys_lastedit_datetime ='".$now."'
1308
                WHERE ticket_id ='$ticketId' ";
1309
1310
        if (api_is_platform_admin()) {
1311
            $sql .= " AND sys_insert_user_id = '$userId'";
1312
        } else {
1313
            $sql .= " AND sys_insert_user_id != '$userId'";
1314
        }
1315
        $result = Database::query($sql);
1316
        if (Database::affected_rows($result) > 0) {
1317
            Database::query(
1318
                "UPDATE $table_support_tickets SET
1319
                    status_id = '".self::STATUS_PENDING."'
1320
                 WHERE id ='$ticketId' AND status_id = '".self::STATUS_NEW."'"
1321
            );
1322
1323
            return true;
1324
        }
1325
1326
        return false;
1327
    }
1328
1329
    /**
1330
     * Send notification to a user through the internal messaging system.
1331
     *
1332
     * @param int    $ticketId
1333
     * @param string $title
1334
     * @param string $message
1335
     * @param int    $onlyToUserId
1336
     *
1337
     * @return bool
1338
     */
1339
    public static function sendNotification($ticketId, $title, $message, $onlyToUserId = 0)
1340
    {
1341
        $ticketInfo = self::get_ticket_detail_by_id($ticketId);
1342
1343
        if (empty($ticketInfo)) {
1344
            return false;
1345
        }
1346
1347
        $assignedUserInfo = api_get_user_info($ticketInfo['ticket']['assigned_last_user']);
1348
        $requestUserInfo = $ticketInfo['usuario'];
1349
        $ticketCode = $ticketInfo['ticket']['code'];
1350
        $status = $ticketInfo['ticket']['status'];
1351
        $priority = $ticketInfo['ticket']['priority'];
1352
1353
        // Subject
1354
        $titleEmail = "[$ticketCode] $title";
1355
1356
        // Content
1357
        $href = api_get_path(WEB_CODE_PATH).'/ticket/ticket_details.php?ticket_id='.$ticketId;
1358
        $ticketUrl = Display::url($ticketCode, $href);
1359
        $messageEmail = get_lang('TicketNum').": $ticketUrl <br />";
1360
        $messageEmail .= get_lang('Status').": $status <br />";
1361
        $messageEmail .= get_lang('Priority').": $priority <br />";
1362
        $messageEmail .= '<hr /><br />';
1363
        $messageEmail .= $message;
1364
        $currentUserId = api_get_user_id();
1365
        $attachmentList = [];
1366
        $attachments = self::getTicketMessageAttachmentsByTicketId($ticketId);
1367
        if (!empty($attachments)) {
1368
            /** @var TicketMessageAttachment $attachment */
1369
            foreach ($attachments as $attachment) {
1370
                $file = api_get_uploaded_file(
1371
                    'ticket_attachment',
1372
                    $ticketId,
1373
                    $attachment->getPath()
1374
                );
1375
                if (!empty($file)) {
1376
                    $attachmentList[] = [
1377
                        'tmp_name' => api_get_uploaded_file(
1378
                            'ticket_attachment',
1379
                            $ticketId,
1380
                            $attachment->getPath()
1381
                        ),
1382
                        'size' => $attachment->getSize(),
1383
                        'name' => $attachment->getFilename(),
1384
                        'error' => 0,
1385
                    ];
1386
                }
1387
            }
1388
        }
1389
1390
        if (!empty($onlyToUserId)) {
1391
            // Send only to specific user
1392
            if ($currentUserId != $onlyToUserId) {
1393
                MessageManager::send_message_simple(
1394
                    $onlyToUserId,
1395
                    $titleEmail,
1396
                    $messageEmail,
1397
                    0,
1398
                    false,
1399
                    false,
1400
                    [],
1401
                    false,
1402
                    $attachmentList
1403
                );
1404
            }
1405
        } else {
1406
            // Send to assigned user and to author
1407
            if ($requestUserInfo && $currentUserId != $requestUserInfo['id']) {
1408
                MessageManager::send_message_simple(
1409
                    $requestUserInfo['id'],
1410
                    $titleEmail,
1411
                    $messageEmail,
1412
                    0,
1413
                    false,
1414
                    false,
1415
                    [],
1416
                    false,
1417
                    $attachmentList
1418
                );
1419
            }
1420
1421
            if ($assignedUserInfo &&
1422
                $requestUserInfo['id'] != $assignedUserInfo['id'] &&
1423
                $currentUserId != $assignedUserInfo['id']
1424
            ) {
1425
                MessageManager::send_message_simple(
1426
                    $assignedUserInfo['id'],
1427
                    $titleEmail,
1428
                    $messageEmail,
1429
                    0,
1430
                    false,
1431
                    false,
1432
                    [],
1433
                    false,
1434
                    $attachmentList
1435
                );
1436
            }
1437
        }
1438
    }
1439
1440
    /**
1441
     * @param array $params
1442
     * @param int   $ticketId
1443
     * @param int   $userId
1444
     *
1445
     * @return bool
1446
     */
1447
    public static function updateTicket(
1448
        $params,
1449
        $ticketId,
1450
        $userId
1451
    ) {
1452
        $now = api_get_utc_datetime();
1453
        $table = Database::get_main_table(TABLE_TICKET_TICKET);
1454
        $newParams = [
1455
            'priority_id' => isset($params['priority_id']) ? (int) $params['priority_id'] : '',
1456
            'status_id' => isset($params['status_id']) ? (int) $params['status_id'] : '',
1457
            'sys_lastedit_user_id' => (int) $userId,
1458
            'sys_lastedit_datetime' => $now,
1459
        ];
1460
        Database::update($table, $newParams, ['id = ? ' => $ticketId]);
1461
1462
        return true;
1463
    }
1464
1465
    /**
1466
     * @param int $status_id
1467
     * @param int $ticketId
1468
     * @param int $userId
1469
     *
1470
     * @return bool
1471
     */
1472
    public static function update_ticket_status(
1473
        $status_id,
1474
        $ticketId,
1475
        $userId
1476
    ) {
1477
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
1478
1479
        $ticketId = (int) $ticketId;
1480
        $status_id = (int) $status_id;
1481
        $userId = (int) $userId;
1482
        $now = api_get_utc_datetime();
1483
1484
        $sql = "UPDATE $table_support_tickets
1485
                SET
1486
                    status_id = '$status_id',
1487
                    sys_lastedit_user_id ='$userId',
1488
                    sys_lastedit_datetime ='".$now."'
1489
                WHERE id ='$ticketId'";
1490
        $result = Database::query($sql);
1491
1492
        if (Database::affected_rows($result) > 0) {
1493
            self::sendNotification(
1494
                $ticketId,
1495
                get_lang('Ticket updated'),
1496
                get_lang('Ticket updated')
1497
            );
1498
1499
            return true;
1500
        }
1501
1502
        return false;
1503
    }
1504
1505
    /**
1506
     * @return mixed
1507
     */
1508
    public static function getNumberOfMessages()
1509
    {
1510
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
1511
        $table_support_messages = Database::get_main_table(TABLE_TICKET_MESSAGE);
1512
        $table_main_user = Database::get_main_table(TABLE_MAIN_USER);
1513
        $table_main_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
1514
        $user_info = api_get_user_info();
1515
        $userId = $user_info['user_id'];
1516
        $sql = "SELECT COUNT(DISTINCT ticket.id) AS unread
1517
                FROM $table_support_tickets ticket,
1518
                $table_support_messages message ,
1519
                $table_main_user user
1520
                WHERE
1521
                    ticket.id = message.ticket_id AND
1522
                    message.status = 'NOL' AND
1523
                    user.user_id = message.sys_insert_user_id ";
1524
        if (!api_is_platform_admin()) {
1525
            $sql .= " AND ticket.request_user = '$userId'
1526
                      AND user_id IN (SELECT user_id FROM $table_main_admin)  ";
1527
        } else {
1528
            $sql .= " AND user_id NOT IN (SELECT user_id FROM $table_main_admin)
1529
                      AND ticket.status_id != '".self::STATUS_FORWARDED."'";
1530
        }
1531
        $sql .= "  AND ticket.project_id != '' ";
1532
        $res = Database::query($sql);
1533
        $obj = Database::fetch_object($res);
1534
1535
        return $obj->unread;
1536
    }
1537
1538
    /**
1539
     * @param int $ticketId
1540
     * @param int $userId
1541
     */
1542
    public static function send_alert($ticketId, $userId)
1543
    {
1544
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
1545
        $now = api_get_utc_datetime();
1546
1547
        $ticketId = (int) $ticketId;
1548
        $userId = (int) $userId;
1549
1550
        $sql = "UPDATE $table_support_tickets SET
1551
                  priority_id = '".self::PRIORITY_HIGH."',
1552
                  sys_lastedit_user_id = $userId,
1553
                  sys_lastedit_datetime = '$now'
1554
                WHERE id = $ticketId";
1555
        Database::query($sql);
1556
    }
1557
1558
    /**
1559
     * @param int $ticketId
1560
     * @param int $userId
1561
     */
1562
    public static function close_ticket($ticketId, $userId)
1563
    {
1564
        $ticketId = (int) $ticketId;
1565
        $userId = (int) $userId;
1566
1567
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
1568
        $now = api_get_utc_datetime();
1569
        $sql = "UPDATE $table_support_tickets SET
1570
                    status_id = '".self::STATUS_CLOSE."',
1571
                    sys_lastedit_user_id ='$userId',
1572
                    sys_lastedit_datetime ='".$now."',
1573
                    end_date ='$now'
1574
                WHERE id ='$ticketId'";
1575
        Database::query($sql);
1576
1577
        self::sendNotification(
1578
            $ticketId,
1579
            get_lang('Ticket closed'),
1580
            get_lang('Ticket closed')
1581
        );
1582
    }
1583
1584
    /**
1585
     * Close old tickets.
1586
     */
1587
    public static function close_old_tickets()
1588
    {
1589
        $table = Database::get_main_table(TABLE_TICKET_TICKET);
1590
        $now = api_get_utc_datetime();
1591
        $userId = api_get_user_id();
1592
        $sql = "UPDATE $table
1593
                SET
1594
                    status_id = '".self::STATUS_CLOSE."',
1595
                    sys_lastedit_user_id ='$userId',
1596
                    sys_lastedit_datetime ='$now',
1597
                    end_date = '$now'
1598
                WHERE
1599
                    DATEDIFF('$now', sys_lastedit_datetime) > 7 AND
1600
                    status_id != '".self::STATUS_CLOSE."' AND
1601
                    status_id != '".self::STATUS_NEW."' AND
1602
                    status_id != '".self::STATUS_FORWARDED."'";
1603
        Database::query($sql);
1604
    }
1605
1606
    /**
1607
     * @param int $ticketId
1608
     *
1609
     * @return array
1610
     */
1611
    public static function get_assign_log($ticketId)
1612
    {
1613
        $table = Database::get_main_table(TABLE_TICKET_ASSIGNED_LOG);
1614
        $ticketId = (int) $ticketId;
1615
1616
        $sql = "SELECT * FROM $table
1617
                WHERE ticket_id = $ticketId
1618
                ORDER BY assigned_date DESC";
1619
        $result = Database::query($sql);
1620
        $history = [];
1621
        $webpath = api_get_path(WEB_PATH);
1622
        while ($row = Database::fetch_assoc($result)) {
1623
            if (0 != $row['user_id']) {
1624
                $assignuser = api_get_user_info($row['user_id']);
1625
                $row['assignuser'] = '<a href="'.$webpath.'main/admin/user_information.php?user_id='.$row['user_id'].'"  target="_blank">'.
1626
                $assignuser['username'].'</a>';
1627
            } else {
1628
                $row['assignuser'] = get_lang('Unassign');
1629
            }
1630
            $row['assigned_date'] = Display::dateToStringAgoAndLongDate($row['assigned_date']);
1631
            $insertuser = api_get_user_info($row['sys_insert_user_id']);
1632
            $row['insertuser'] = '<a href="'.$webpath.'main/admin/user_information.php?user_id='.$row['sys_insert_user_id'].'"  target="_blank">'.
1633
                $insertuser['username'].'</a>';
1634
            $history[] = $row;
1635
        }
1636
1637
        return $history;
1638
    }
1639
1640
    /**
1641
     * @param $from
1642
     * @param $number_of_items
1643
     * @param $column
1644
     * @param $direction
1645
     * @param null $userId
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $userId is correct as it would always require null to be passed?
Loading history...
1646
     *
1647
     * @return array
1648
     */
1649
    public static function export_tickets_by_user_id(
1650
        $from,
1651
        $number_of_items,
1652
        $column,
1653
        $direction,
1654
        $userId = null
1655
    ) {
1656
        $from = (int) $from;
1657
        $number_of_items = (int) $number_of_items;
1658
        $table_support_category = Database::get_main_table(
1659
            TABLE_TICKET_CATEGORY
1660
        );
1661
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
1662
        $table_support_priority = Database::get_main_table(TABLE_TICKET_PRIORITY);
1663
        $table_support_status = Database::get_main_table(TABLE_TICKET_STATUS);
1664
        $table_support_messages = Database::get_main_table(TABLE_TICKET_MESSAGE);
1665
        $table_main_user = Database::get_main_table(TABLE_MAIN_USER);
1666
1667
        if (is_null($direction)) {
1668
            $direction = 'DESC';
1669
        }
1670
        if (is_null($userId) || 0 == $userId) {
1671
            $userId = api_get_user_id();
1672
        }
1673
1674
        $sql = "SELECT
1675
                    ticket.code,
1676
                    ticket.sys_insert_datetime,
1677
                    ticket.sys_lastedit_datetime,
1678
                    cat.name as category,
1679
                    CONCAT(user.lastname,' ', user.firstname) AS fullname,
1680
                    status.name as status,
1681
                    ticket.total_messages as messages,
1682
                    ticket.assigned_last_user as responsable
1683
                FROM $table_support_tickets ticket,
1684
                $table_support_category cat ,
1685
                $table_support_priority priority,
1686
                $table_support_status status ,
1687
                $table_main_user user
1688
                WHERE
1689
                    cat.id = ticket.category_id
1690
                    AND ticket.priority_id = priority.id
1691
                    AND ticket.status_id = status.id
1692
                    AND user.user_id = ticket.request_user ";
1693
        // Search simple
1694
        if (isset($_GET['submit_simple'])) {
1695
            if ('' !== $_GET['keyword']) {
1696
                $keyword = Database::escape_string(trim($_GET['keyword']));
1697
                $sql .= " AND (ticket.code = '$keyword'
1698
                          OR user.firstname LIKE '%$keyword%'
1699
                          OR user.lastname LIKE '%$keyword%'
1700
                          OR concat(user.firstname,' ',user.lastname) LIKE '%$keyword%'
1701
                          OR concat(user.lastname,' ',user.firstname) LIKE '%$keyword%'
1702
                          OR user.username LIKE '%$keyword%')  ";
1703
            }
1704
        }
1705
        // Search advanced
1706
        if (isset($_GET['submit_advanced'])) {
1707
            $keyword_category = Database::escape_string(
1708
                trim($_GET['keyword_category'])
1709
            );
1710
            $keyword_request_user = Database::escape_string(
1711
                trim($_GET['keyword_request_user'])
1712
            );
1713
            $keywordAssignedTo = (int) $_GET['keyword_assigned_to'];
1714
            $keyword_start_date_start = Database::escape_string(
1715
                trim($_GET['keyword_start_date_start'])
1716
            );
1717
            $keyword_start_date_end = Database::escape_string(
1718
                trim($_GET['keyword_start_date_end'])
1719
            );
1720
            $keyword_status = Database::escape_string(
1721
                trim($_GET['keyword_status'])
1722
            );
1723
            $keyword_source = Database::escape_string(
1724
                trim($_GET['keyword_source'])
1725
            );
1726
            $keyword_priority = Database::escape_string(
1727
                trim($_GET['keyword_priority'])
1728
            );
1729
            $keyword_range = Database::escape_string(
1730
                trim($_GET['keyword_dates'])
1731
            );
1732
            $keyword_unread = Database::escape_string(
1733
                trim($_GET['keyword_unread'])
1734
            );
1735
            $keyword_course = Database::escape_string(
1736
                trim($_GET['keyword_course'])
1737
            );
1738
1739
            if ('' != $keyword_category) {
1740
                $sql .= " AND ticket.category_id = '$keyword_category'  ";
1741
            }
1742
            if ('' != $keyword_request_user) {
1743
                $sql .= " AND (ticket.request_user = '$keyword_request_user'
1744
                          OR user.firstname LIKE '%$keyword_request_user%'
1745
                          OR user.official_code LIKE '%$keyword_request_user%'
1746
                          OR user.lastname LIKE '%$keyword_request_user%'
1747
                          OR concat(user.firstname,' ',user.lastname) LIKE '%$keyword_request_user%'
1748
                          OR concat(user.lastname,' ',user.firstname) LIKE '%$keyword_request_user%'
1749
                          OR user.username LIKE '%$keyword_request_user%') ";
1750
            }
1751
            if (!empty($keywordAssignedTo)) {
1752
                $sql .= " AND ticket.assigned_last_user = $keywordAssignedTo ";
1753
            }
1754
            if ('' != $keyword_status) {
1755
                $sql .= " AND ticket.status_id = '$keyword_status'  ";
1756
            }
1757
            if ('' == $keyword_range && '' != $keyword_start_date_start) {
1758
                $sql .= " AND DATE_FORMAT( ticket.start_date,'%d/%m/%Y') = '$keyword_start_date_start' ";
1759
            }
1760
            if ('1' == $keyword_range && '' != $keyword_start_date_start && '' != $keyword_start_date_end) {
1761
                $sql .= " AND DATE_FORMAT( ticket.start_date,'%d/%m/%Y') >= '$keyword_start_date_start'
1762
                          AND DATE_FORMAT( ticket.start_date,'%d/%m/%Y') <= '$keyword_start_date_end'";
1763
            }
1764
            if ('' != $keyword_priority) {
1765
                $sql .= " AND ticket.priority_id = '$keyword_priority'  ";
1766
            }
1767
            if ('' != $keyword_source) {
1768
                $sql .= " AND ticket.source = '$keyword_source' ";
1769
            }
1770
            if ('' != $keyword_priority) {
1771
                $sql .= " AND ticket.priority_id = '$keyword_priority' ";
1772
            }
1773
            if ('' != $keyword_course) {
1774
                $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
1775
                $sql .= " AND ticket.course_id IN ( ";
1776
                $sql .= "SELECT id
1777
                         FROM $course_table
1778
                         WHERE (title LIKE '%$keyword_course%'
1779
                         OR code LIKE '%$keyword_course%'
1780
                         OR visual_code LIKE '%$keyword_course%' )) ";
1781
            }
1782
            if ('yes' == $keyword_unread) {
1783
                $sql .= " AND ticket.id IN (
1784
                          SELECT ticket.id
1785
                          FROM $table_support_tickets ticket,
1786
                          $table_support_messages message,
1787
                          $table_main_user user
1788
                          WHERE ticket.id = message.ticket_id
1789
                          AND message.status = 'NOL'
1790
                          AND message.sys_insert_user_id = user.user_id
1791
                          AND user.status != 1   AND ticket.status_id != '".self::STATUS_FORWARDED."'
1792
                          GROUP BY ticket.id)";
1793
            } else {
1794
                if ('no' == $keyword_unread) {
1795
                    $sql .= " AND ticket.id NOT IN (
1796
                              SELECT ticket.id
1797
                              FROM  $table_support_tickets ticket,
1798
                              $table_support_messages message,
1799
                              $table_main_user user
1800
                              WHERE ticket.id = message.ticket_id
1801
                              AND message.status = 'NOL'
1802
                              AND message.sys_insert_user_id = user.user_id
1803
                              AND user.status != 1
1804
                              AND ticket.status_id != '".self::STATUS_FORWARDED."'
1805
                             GROUP BY ticket.id)";
1806
                }
1807
            }
1808
        }
1809
1810
        $sql .= " LIMIT $from,$number_of_items";
1811
1812
        $result = Database::query($sql);
1813
        $tickets[0] = [
0 ignored issues
show
Comprehensibility Best Practice introduced by
$tickets was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tickets = array(); before regardless.
Loading history...
1814
            utf8_decode('Ticket#'),
1815
            utf8_decode('Fecha'),
1816
            utf8_decode('Fecha Edicion'),
1817
            utf8_decode('Categoria'),
1818
            utf8_decode('Usuario'),
1819
            utf8_decode('Estado'),
1820
            utf8_decode('Mensajes'),
1821
            utf8_decode('Responsable'),
1822
            utf8_decode('Programa'),
1823
        ];
1824
1825
        while ($row = Database::fetch_assoc($result)) {
1826
            if (0 != $row['responsable']) {
1827
                $row['responsable'] = api_get_user_info($row['responsable']);
1828
                $row['responsable'] = $row['responsable']['firstname'].' '.$row['responsable']['lastname'];
1829
            }
1830
            $row['sys_insert_datetime'] = api_format_date(
1831
                $row['sys_insert_datetime'],
1832
                '%d/%m/%y - %I:%M:%S %p'
1833
            );
1834
            $row['sys_lastedit_datetime'] = api_format_date(
1835
                $row['sys_lastedit_datetime'],
1836
                '%d/%m/%y - %I:%M:%S %p'
1837
            );
1838
            $row['category'] = utf8_decode($row['category']);
1839
            $row['programa'] = utf8_decode($row['fullname']);
1840
            $row['fullname'] = utf8_decode($row['fullname']);
1841
            $row['responsable'] = utf8_decode($row['responsable']);
1842
            $tickets[] = $row;
1843
        }
1844
1845
        return $tickets;
1846
    }
1847
1848
    /**
1849
     * @param string $url
1850
     * @param int    $projectId
1851
     *
1852
     * @return FormValidator
1853
     */
1854
    public static function getCategoryForm($url, $projectId)
1855
    {
1856
        $form = new FormValidator('category', 'post', $url);
1857
        $form->addText('name', get_lang('Name'));
1858
        $form->addHtmlEditor('description', get_lang('Description'));
1859
        $form->addHidden('project_id', $projectId);
1860
        $form->addButtonUpdate(get_lang('Save'));
1861
1862
        return $form;
1863
    }
1864
1865
    /**
1866
     * @return array
1867
     */
1868
    public static function getStatusList()
1869
    {
1870
        $items = Database::getManager()->getRepository('TicketStatus')->findAll();
1871
1872
        $list = [];
1873
        /** @var TicketStatus $row */
1874
        foreach ($items as $row) {
1875
            $list[$row->getId()] = $row->getName();
1876
        }
1877
1878
        return $list;
1879
    }
1880
1881
    /**
1882
     * @param array $criteria
1883
     *
1884
     * @return array
1885
     */
1886
    public static function getTicketsFromCriteria($criteria)
1887
    {
1888
        $items = Database::getManager()->getRepository('ChamiloCoreBundle:Ticket')->findBy($criteria);
1889
1890
        $list = [];
1891
        /** @var Ticket $row */
1892
        foreach ($items as $row) {
1893
            $list[$row->getId()] = $row->getCode();
1894
        }
1895
1896
        return $list;
1897
    }
1898
1899
    /**
1900
     * @param string $code
1901
     *
1902
     * @return int
1903
     */
1904
    public static function getStatusIdFromCode($code)
1905
    {
1906
        $item = Database::getManager()
1907
            ->getRepository('TicketStatus')
1908
            ->findOneBy(['code' => $code])
1909
        ;
1910
1911
        if ($item) {
1912
            return $item->getId();
1913
        }
1914
1915
        return 0;
1916
    }
1917
1918
    /**
1919
     * @return array
1920
     */
1921
    public static function getPriorityList()
1922
    {
1923
        $projects = Database::getManager()->getRepository('TicketPriority')->findAll();
1924
1925
        $list = [];
1926
        /** @var TicketPriority $row */
1927
        foreach ($projects as $row) {
1928
            $list[$row->getId()] = $row->getName();
1929
        }
1930
1931
        return $list;
1932
    }
1933
1934
    /**
1935
     * @return array
1936
     */
1937
    public static function getProjects()
1938
    {
1939
        $projects = Database::getManager()->getRepository('TicketProject')->findAll();
1940
1941
        $list = [];
1942
        /** @var TicketProject $row */
1943
        foreach ($projects as $row) {
1944
            $list[] = [
1945
                'id' => $row->getId(),
1946
                '0' => $row->getId(),
1947
                '1' => $row->getName(),
1948
                '2' => $row->getDescription(),
1949
                '3' => $row->getId(),
1950
            ];
1951
        }
1952
1953
        return $list;
1954
    }
1955
1956
    /**
1957
     * @return array
1958
     */
1959
    public static function getProjectsSimple()
1960
    {
1961
        $projects = Database::getManager()->getRepository('TicketProject')->findAll();
1962
1963
        $list = [];
1964
        /** @var TicketProject $row */
1965
        foreach ($projects as $row) {
1966
            $list[] = [
1967
                'id' => $row->getId(),
1968
                '0' => $row->getId(),
1969
                '1' => Display::url(
1970
                    $row->getName(),
1971
                    api_get_path(WEB_CODE_PATH).'ticket/tickets.php?project_id='.$row->getId()
1972
                ),
1973
                '2' => $row->getDescription(),
1974
            ];
1975
        }
1976
1977
        return $list;
1978
    }
1979
1980
    /**
1981
     * @return int
1982
     */
1983
    public static function getProjectsCount()
1984
    {
1985
        $count = Database::getManager()->getRepository('TicketProject')->createQueryBuilder('p')
1986
            ->select('COUNT(p.id)')
1987
            ->getQuery()
1988
            ->getSingleScalarResult();
1989
1990
        return $count;
1991
    }
1992
1993
    /**
1994
     * @param array $params
1995
     */
1996
    public static function addProject($params)
1997
    {
1998
        $project = new TicketProject();
1999
        $project->setName($params['name']);
2000
        $project->setDescription($params['description']);
2001
        $project->setInsertUserId(api_get_user_id());
2002
2003
        Database::getManager()->persist($project);
2004
        Database::getManager()->flush();
2005
    }
2006
2007
    /**
2008
     * @param int $id
2009
     *
2010
     * @return TicketProject
2011
     */
2012
    public static function getProject($id)
2013
    {
2014
        return Database::getManager()->getRepository('TicketProject')->find($id);
2015
    }
2016
2017
    /**
2018
     * @param int   $id
2019
     * @param array $params
2020
     */
2021
    public static function updateProject($id, $params)
2022
    {
2023
        $project = self::getProject($id);
2024
        $project->setName($params['name']);
2025
        $project->setDescription($params['description']);
2026
        $project->setLastEditDateTime(new DateTime($params['sys_lastedit_datetime']));
2027
        $project->setLastEditUserId($params['sys_lastedit_user_id']);
2028
2029
        Database::getManager()->persist($project);
2030
        Database::getManager()->flush();
2031
    }
2032
2033
    /**
2034
     * @param int $id
2035
     */
2036
    public static function deleteProject($id)
2037
    {
2038
        $project = self::getProject($id);
2039
        if ($project) {
0 ignored issues
show
introduced by
$project is of type Chamilo\CoreBundle\Entity\TicketProject, thus it always evaluated to true.
Loading history...
2040
            Database::getManager()->remove($project);
2041
            Database::getManager()->flush();
2042
        }
2043
    }
2044
2045
    /**
2046
     * @param string $url
2047
     *
2048
     * @return FormValidator
2049
     */
2050
    public static function getProjectForm($url)
2051
    {
2052
        $form = new FormValidator('project', 'post', $url);
2053
        $form->addText('name', get_lang('Name'));
2054
        $form->addHtmlEditor('description', get_lang('Description'));
2055
        $form->addButtonUpdate(get_lang('Save'));
2056
2057
        return $form;
2058
    }
2059
2060
    /**
2061
     * @return array
2062
     */
2063
    public static function getStatusAdminList()
2064
    {
2065
        $items = Database::getManager()->getRepository('TicketStatus')->findAll();
2066
2067
        $list = [];
2068
        /** @var TicketStatus $row */
2069
        foreach ($items as $row) {
2070
            $list[] = [
2071
                'id' => $row->getId(),
2072
                'code' => $row->getCode(),
2073
                '0' => $row->getId(),
2074
                '1' => $row->getName(),
2075
                '2' => $row->getDescription(),
2076
                '3' => $row->getId(),
2077
            ];
2078
        }
2079
2080
        return $list;
2081
    }
2082
2083
    /**
2084
     * @return array
2085
     */
2086
    public static function getStatusSimple()
2087
    {
2088
        $projects = Database::getManager()->getRepository('TicketStatus')->findAll();
2089
2090
        $list = [];
2091
        /** @var TicketProject $row */
2092
        foreach ($projects as $row) {
2093
            $list[] = [
2094
                'id' => $row->getId(),
2095
                '0' => $row->getId(),
2096
                '1' => Display::url($row->getName()),
0 ignored issues
show
Bug introduced by
The call to Display::url() has too few arguments starting with url. ( Ignorable by Annotation )

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

2096
                '1' => Display::/** @scrutinizer ignore-call */ url($row->getName()),

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
2097
                '2' => $row->getDescription(),
2098
            ];
2099
        }
2100
2101
        return $list;
2102
    }
2103
2104
    /**
2105
     * @return int
2106
     */
2107
    public static function getStatusCount()
2108
    {
2109
        $count = Database::getManager()->getRepository('TicketStatus')->createQueryBuilder('p')
2110
            ->select('COUNT(p.id)')
2111
            ->getQuery()
2112
            ->getSingleScalarResult();
2113
2114
        return $count;
2115
    }
2116
2117
    /**
2118
     * @param array $params
2119
     */
2120
    public static function addStatus($params)
2121
    {
2122
        $item = new TicketStatus();
2123
        $item->setCode(URLify::filter($params['name']));
2124
        $item->setName($params['name']);
2125
        $item->setDescription($params['description']);
2126
2127
        Database::getManager()->persist($item);
2128
        Database::getManager()->flush();
2129
    }
2130
2131
    /**
2132
     * @param $id
2133
     *
2134
     * @return TicketProject
2135
     */
2136
    public static function getStatus($id)
2137
    {
2138
        return Database::getManager()->getRepository('TicketStatus')->find($id);
2139
    }
2140
2141
    /**
2142
     * @param int   $id
2143
     * @param array $params
2144
     */
2145
    public static function updateStatus($id, $params)
2146
    {
2147
        $item = self::getStatus($id);
2148
        $item->setName($params['name']);
2149
        $item->setDescription($params['description']);
2150
2151
        Database::getManager()->persist($item);
2152
        Database::getManager()->flush();
2153
    }
2154
2155
    /**
2156
     * @param int $id
2157
     */
2158
    public static function deleteStatus($id)
2159
    {
2160
        $item = self::getStatus($id);
2161
        if ($item) {
0 ignored issues
show
introduced by
$item is of type Chamilo\CoreBundle\Entity\TicketProject, thus it always evaluated to true.
Loading history...
2162
            Database::getManager()->remove($item);
2163
            Database::getManager()->flush();
2164
        }
2165
    }
2166
2167
    /**
2168
     * @param string $url
2169
     *
2170
     * @return FormValidator
2171
     */
2172
    public static function getStatusForm($url)
2173
    {
2174
        $form = new FormValidator('status', 'post', $url);
2175
        $form->addText('name', get_lang('Name'));
2176
        $form->addHtmlEditor('description', get_lang('Description'));
2177
        $form->addButtonUpdate(get_lang('Save'));
2178
2179
        return $form;
2180
    }
2181
2182
    /**
2183
     * @return array
2184
     */
2185
    public static function getPriorityAdminList()
2186
    {
2187
        $items = Database::getManager()->getRepository('TicketPriority')->findAll();
2188
2189
        $list = [];
2190
        /** @var TicketStatus $row */
2191
        foreach ($items as $row) {
2192
            $list[] = [
2193
                'id' => $row->getId(),
2194
                'code' => $row->getCode(),
2195
                '0' => $row->getId(),
2196
                '1' => $row->getName(),
2197
                '2' => $row->getDescription(),
2198
                '3' => $row->getId(),
2199
            ];
2200
        }
2201
2202
        return $list;
2203
    }
2204
2205
    /**
2206
     * @return array
2207
     */
2208
    public static function getPrioritySimple()
2209
    {
2210
        $projects = Database::getManager()->getRepository('TicketPriority')->findAll();
2211
2212
        $list = [];
2213
        /** @var TicketPriority $row */
2214
        foreach ($projects as $row) {
2215
            $list[] = [
2216
                'id' => $row->getId(),
2217
                '0' => $row->getId(),
2218
                '1' => Display::url($row->getName()),
0 ignored issues
show
Bug introduced by
The call to Display::url() has too few arguments starting with url. ( Ignorable by Annotation )

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

2218
                '1' => Display::/** @scrutinizer ignore-call */ url($row->getName()),

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
2219
                '2' => $row->getDescription(),
2220
            ];
2221
        }
2222
2223
        return $list;
2224
    }
2225
2226
    /**
2227
     * @return int
2228
     */
2229
    public static function getPriorityCount()
2230
    {
2231
        $count = Database::getManager()->getRepository('TicketPriority')->createQueryBuilder('p')
2232
            ->select('COUNT(p.id)')
2233
            ->getQuery()
2234
            ->getSingleScalarResult();
2235
2236
        return $count;
2237
    }
2238
2239
    /**
2240
     * @param array $params
2241
     */
2242
    public static function addPriority($params)
2243
    {
2244
        $item = new TicketPriority();
2245
        $item
2246
            ->setCode(URLify::filter($params['name']))
2247
            ->setName($params['name'])
2248
            ->setDescription($params['description'])
2249
            ->setColor('')
2250
            ->setInsertUserId(api_get_user_id())
2251
            ->setUrgency('')
2252
        ;
2253
2254
        Database::getManager()->persist($item);
2255
        Database::getManager()->flush();
2256
    }
2257
2258
    /**
2259
     * @param $id
2260
     *
2261
     * @return TicketPriority
2262
     */
2263
    public static function getPriority($id)
2264
    {
2265
        return Database::getManager()->getRepository('TicketPriority')->find($id);
2266
    }
2267
2268
    /**
2269
     * @param int   $id
2270
     * @param array $params
2271
     */
2272
    public static function updatePriority($id, $params)
2273
    {
2274
        $item = self::getPriority($id);
2275
        $item->setName($params['name']);
2276
        $item->setDescription($params['description']);
2277
2278
        Database::getManager()->persist($item);
2279
        Database::getManager()->flush();
2280
    }
2281
2282
    /**
2283
     * @param int $id
2284
     */
2285
    public static function deletePriority($id)
2286
    {
2287
        $item = self::getPriority($id);
2288
        if ($item) {
0 ignored issues
show
introduced by
$item is of type Chamilo\CoreBundle\Entity\TicketPriority, thus it always evaluated to true.
Loading history...
2289
            Database::getManager()->remove($item);
2290
            Database::getManager()->flush();
2291
        }
2292
    }
2293
2294
    /**
2295
     * @param string $url
2296
     *
2297
     * @return FormValidator
2298
     */
2299
    public static function getPriorityForm($url)
2300
    {
2301
        $form = new FormValidator('priority', 'post', $url);
2302
        $form->addText('name', get_lang('Name'));
2303
        $form->addHtmlEditor('description', get_lang('Description'));
2304
        $form->addButtonUpdate(get_lang('Save'));
2305
2306
        return $form;
2307
    }
2308
2309
    /**
2310
     * Returns a list of menu elements for the tickets system's configuration.
2311
     *
2312
     * @param string $exclude The element to exclude from the list
2313
     *
2314
     * @return array
2315
     */
2316
    public static function getSettingsMenuItems($exclude = null)
2317
    {
2318
        $project = [
2319
            'icon' => 'project.png',
2320
            'url' => 'projects.php',
2321
            'content' => get_lang('Projects'),
2322
        ];
2323
        $status = [
2324
            'icon' => 'check-circle.png',
2325
            'url' => 'status.php',
2326
            'content' => get_lang('Status'),
2327
        ];
2328
        $priority = [
2329
            'icon' => 'tickets_urgent.png',
2330
            'url' => 'priorities.php',
2331
            'content' => get_lang('Priority'),
2332
        ];
2333
        switch ($exclude) {
2334
            case 'project':
2335
                $items = [$status, $priority];
2336
                break;
2337
            case 'status':
2338
                $items = [$project, $priority];
2339
                break;
2340
            case 'priority':
2341
                $items = [$project, $status];
2342
                break;
2343
            default:
2344
                $items = [$project, $status, $priority];
2345
                break;
2346
        }
2347
2348
        return $items;
2349
    }
2350
2351
    /**
2352
     * Returns a list of strings representing the default statuses.
2353
     *
2354
     * @return array
2355
     */
2356
    public static function getDefaultStatusList()
2357
    {
2358
        return [
2359
            self::STATUS_NEW,
2360
            self::STATUS_PENDING,
2361
            self::STATUS_UNCONFIRMED,
2362
            self::STATUS_CLOSE,
2363
            self::STATUS_FORWARDED,
2364
        ];
2365
    }
2366
2367
    /**
2368
     * @return array
2369
     */
2370
    public static function getDefaultPriorityList()
2371
    {
2372
        return [
2373
            self::PRIORITY_NORMAL,
2374
            self::PRIORITY_HIGH,
2375
            self::PRIORITY_LOW,
2376
            self::STATUS_CLOSE,
2377
            self::STATUS_FORWARDED,
2378
        ];
2379
    }
2380
2381
    /**
2382
     * Deletes the user from all the ticket system.
2383
     *
2384
     * @param int $userId
2385
     */
2386
    public static function deleteUserFromTicketSystem($userId)
2387
    {
2388
        $userId = (int) $userId;
2389
        $schema = Database::getManager()->getConnection()->getSchemaManager();
2390
2391
        if ($schema->tablesExist('ticket_assigned_log')) {
2392
            $sql = "UPDATE ticket_assigned_log SET user_id = NULL WHERE user_id = $userId";
2393
            Database::query($sql);
2394
2395
            $sql = "UPDATE ticket_assigned_log SET sys_insert_user_id = NULL WHERE sys_insert_user_id = $userId";
2396
            Database::query($sql);
2397
        }
2398
2399
        if ($schema->tablesExist('ticket_ticket')) {
2400
            $sql = "UPDATE ticket_ticket SET assigned_last_user = NULL WHERE assigned_last_user = $userId";
2401
            Database::query($sql);
2402
2403
            $sql = "UPDATE ticket_ticket SET sys_insert_user_id = NULL WHERE sys_insert_user_id = $userId";
2404
            Database::query($sql);
2405
2406
            $sql = "UPDATE ticket_ticket SET sys_lastedit_user_id = NULL WHERE sys_lastedit_user_id = $userId";
2407
            Database::query($sql);
2408
        }
2409
2410
        if ($schema->tablesExist('ticket_category')) {
2411
            $sql = "UPDATE ticket_category SET sys_insert_user_id = NULL WHERE sys_insert_user_id = $userId";
2412
            Database::query($sql);
2413
2414
            $sql = "UPDATE ticket_category SET sys_lastedit_user_id = NULL WHERE sys_lastedit_user_id = $userId";
2415
            Database::query($sql);
2416
        }
2417
2418
        if ($schema->tablesExist('ticket_category_rel_user')) {
2419
            $sql = "DELETE FROM ticket_category_rel_user WHERE user_id = $userId";
2420
            Database::query($sql);
2421
        }
2422
2423
        if ($schema->tablesExist('ticket_message')) {
2424
            $sql = "UPDATE ticket_message SET sys_insert_user_id = NULL WHERE sys_insert_user_id = $userId";
2425
            Database::query($sql);
2426
2427
            $sql = "UPDATE ticket_message SET sys_lastedit_user_id = NULL WHERE sys_lastedit_user_id = $userId";
2428
            Database::query($sql);
2429
        }
2430
2431
        if ($schema->tablesExist('ticket_message_attachments')) {
2432
            $sql = "UPDATE ticket_message_attachments SET sys_insert_user_id = NULL WHERE sys_insert_user_id = $userId";
2433
            Database::query($sql);
2434
2435
            $sql = "UPDATE ticket_message_attachments SET sys_lastedit_user_id = NULL WHERE sys_lastedit_user_id = $userId";
2436
            Database::query($sql);
2437
        }
2438
2439
        if ($schema->tablesExist('ticket_priority')) {
2440
            $sql = "UPDATE ticket_priority SET sys_insert_user_id = NULL WHERE sys_insert_user_id = $userId";
2441
            Database::query($sql);
2442
2443
            $sql = "UPDATE ticket_priority SET sys_lastedit_user_id = NULL WHERE sys_lastedit_user_id = $userId";
2444
            Database::query($sql);
2445
        }
2446
2447
        if ($schema->tablesExist('ticket_project')) {
2448
            $sql = "UPDATE ticket_project SET sys_insert_user_id = NULL WHERE sys_insert_user_id = $userId";
2449
            Database::query($sql);
2450
2451
            $sql = "UPDATE ticket_project SET sys_lastedit_user_id = NULL WHERE sys_lastedit_user_id = $userId";
2452
            Database::query($sql);
2453
        }
2454
    }
2455
2456
    /**
2457
     * @param array $userInfo
2458
     * @param int   $projectId
2459
     *
2460
     * @return bool
2461
     */
2462
    public static function userIsAllowInProject($userInfo, $projectId)
2463
    {
2464
        if (api_is_platform_admin()) {
2465
            return true;
2466
        }
2467
2468
        $allowRoleList = self::getAllowedRolesFromProject($projectId);
2469
2470
        // Check if a role was set to the project
2471
        // Project 1 is considered the default and is accessible to all users
2472
        if (!empty($allowRoleList) && is_array($allowRoleList)) {
2473
            if (in_array($userInfo['status'], $allowRoleList)) {
2474
                return true;
2475
            }
2476
        }
2477
2478
        return false;
2479
    }
2480
2481
    /**
2482
     * @param int $projectId
2483
     *
2484
     * @todo load from database instead of configuration.php setting
2485
     *
2486
     * @return array
2487
     */
2488
    public static function getAllowedRolesFromProject($projectId)
2489
    {
2490
        $options = api_get_configuration_value('ticket_project_user_roles');
2491
        if ($options) {
2492
            if (isset($options['permissions'][$projectId])) {
2493
                return $options['permissions'][$projectId];
2494
            }
2495
        }
2496
2497
        return [];
2498
    }
2499
}
2500