Passed
Push — ofaj ( bee127...8e94e9 )
by
unknown
11:26 queued 26s
created

TicketManager::get_assign_log()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 20
nc 3
nop 1
dl 0
loc 27
rs 9.6
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\TicketBundle\Entity\MessageAttachment;
5
use Chamilo\TicketBundle\Entity\Priority;
6
use Chamilo\TicketBundle\Entity\Project;
7
use Chamilo\TicketBundle\Entity\Status;
8
use Chamilo\TicketBundle\Entity\Ticket;
9
10
/**
11
 * Class TicketManager.
12
 *
13
 * @package chamilo.plugin.ticket
14
 */
15
class TicketManager
16
{
17
    const PRIORITY_NORMAL = 'NRM';
18
    const PRIORITY_HIGH = 'HGH';
19
    const PRIORITY_LOW = 'LOW';
20
21
    const SOURCE_EMAIL = 'MAI';
22
    const SOURCE_PHONE = 'TEL';
23
    const SOURCE_PLATFORM = 'PLA';
24
    const SOURCE_PRESENTIAL = 'PRE';
25
26
    const STATUS_NEW = 'NAT';
27
    const STATUS_PENDING = 'PND';
28
    const STATUS_UNCONFIRMED = 'XCF';
29
    const STATUS_CLOSE = 'CLS';
30
    const STATUS_FORWARDED = 'REE';
31
32
    /**
33
     * Constructor.
34
     */
35
    public function __construct()
36
    {
37
    }
38
39
    /**
40
     * Get categories of tickets.
41
     *
42
     * @param int    $projectId
43
     * @param string $order
44
     *
45
     * @return array
46
     */
47
    public static function get_all_tickets_categories($projectId, $order = '')
48
    {
49
        $table_support_category = Database::get_main_table(TABLE_TICKET_CATEGORY);
50
        $table_support_project = Database::get_main_table(TABLE_TICKET_PROJECT);
51
52
        $order = empty($order) ? 'category.total_tickets DESC' : $order;
53
        $order = Database::escape_string($order);
54
        $projectId = (int) $projectId;
55
56
        $sql = "SELECT
57
                    category.*,
58
                    category.id category_id,
59
                    project.other_area,
60
                    project.email
61
                FROM
62
                $table_support_category category
63
                INNER JOIN $table_support_project project
64
                ON project.id = category.project_id
65
                WHERE project.id  = $projectId
66
                ORDER BY $order";
67
        $result = Database::query($sql);
68
        $types = [];
69
        while ($row = Database::fetch_assoc($result)) {
70
            $types[] = $row;
71
        }
72
73
        return $types;
74
    }
75
76
    /**
77
     * @param $from
78
     * @param $numberItems
79
     * @param $column
80
     * @param $direction
81
     *
82
     * @return array
83
     */
84
    public static function getCategories($from, $numberItems, $column, $direction)
85
    {
86
        $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
87
        $sql = "SELECT id, name, description, total_tickets
88
                FROM $table";
89
90
        if (!in_array($direction, ['ASC', 'DESC'])) {
91
            $direction = 'ASC';
92
        }
93
        $column = (int) $column;
94
        $from = (int) $from;
95
        $numberItems = (int) $numberItems;
96
97
        //$sql .= " ORDER BY col$column $direction ";
98
        $sql .= " LIMIT $from,$numberItems";
99
100
        $result = Database::query($sql);
101
        $types = [];
102
        while ($row = Database::fetch_array($result)) {
103
            $types[] = $row;
104
        }
105
106
        return $types;
107
    }
108
109
    /**
110
     * @param int $id
111
     *
112
     * @return array|mixed
113
     */
114
    public static function getCategory($id)
115
    {
116
        $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
117
        $id = (int) $id;
118
        $sql = "SELECT id, name, description, total_tickets
119
                FROM $table WHERE id = $id";
120
121
        $result = Database::query($sql);
122
        $category = Database::fetch_array($result);
123
124
        return $category;
125
    }
126
127
    /**
128
     * @return int
129
     */
130
    public static function getCategoriesCount()
131
    {
132
        $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
133
134
        $sql = "SELECT count(id) count
135
                FROM $table ";
136
137
        $result = Database::query($sql);
138
        $category = Database::fetch_array($result);
139
140
        return $category['count'];
141
    }
142
143
    /**
144
     * @param int   $id
145
     * @param array $params
146
     */
147
    public static function updateCategory($id, $params)
148
    {
149
        $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
150
        $id = (int) $id;
151
        Database::update($table, $params, ['id = ?' => $id]);
152
    }
153
154
    /**
155
     * @param array $params
156
     */
157
    public static function addCategory($params)
158
    {
159
        $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
160
        Database::insert($table, $params);
161
    }
162
163
    /**
164
     * @param int $id
165
     *
166
     * @return bool
167
     */
168
    public static function deleteCategory($id)
169
    {
170
        $id = (int) $id;
171
        if (empty($id)) {
172
            return false;
173
        }
174
175
        $table = Database::get_main_table(TABLE_TICKET_TICKET);
176
        $sql = "UPDATE $table SET category_id = NULL WHERE category_id = $id";
177
        Database::query($sql);
178
179
        $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
180
        $sql = "DELETE FROM $table WHERE id = $id";
181
        Database::query($sql);
182
183
        return true;
184
    }
185
186
    /**
187
     * @param int   $categoryId
188
     * @param array $users
189
     *
190
     * @return bool
191
     */
192
    public static function addUsersToCategory($categoryId, $users)
193
    {
194
        if (empty($users) || empty($categoryId)) {
195
            return false;
196
        }
197
198
        $table = Database::get_main_table(TABLE_TICKET_CATEGORY_REL_USER);
199
        foreach ($users as $userId) {
200
            if (self::userIsAssignedToCategory($userId, $categoryId) === false) {
201
                $params = [
202
                    'category_id' => $categoryId,
203
                    'user_id' => $userId,
204
                ];
205
                Database::insert($table, $params);
206
            }
207
        }
208
209
        return true;
210
    }
211
212
    /**
213
     * @param int $userId
214
     * @param int $categoryId
215
     *
216
     * @return bool
217
     */
218
    public static function userIsAssignedToCategory($userId, $categoryId)
219
    {
220
        $table = Database::get_main_table(TABLE_TICKET_CATEGORY_REL_USER);
221
        $userId = (int) $userId;
222
        $categoryId = (int) $categoryId;
223
        $sql = "SELECT * FROM $table
224
                WHERE category_id = $categoryId AND user_id = $userId";
225
        $result = Database::query($sql);
226
227
        return Database::num_rows($result) > 0;
228
    }
229
230
    /**
231
     * @param int $categoryId
232
     *
233
     * @return array
234
     */
235
    public static function getUsersInCategory($categoryId)
236
    {
237
        $table = Database::get_main_table(TABLE_TICKET_CATEGORY_REL_USER);
238
        $categoryId = (int) $categoryId;
239
        $sql = "SELECT * FROM $table WHERE category_id = $categoryId";
240
        $result = Database::query($sql);
241
242
        return Database::store_result($result);
243
    }
244
245
    /**
246
     * @param int $categoryId
247
     */
248
    public static function deleteAllUserInCategory($categoryId)
249
    {
250
        $table = Database::get_main_table(TABLE_TICKET_CATEGORY_REL_USER);
251
        $categoryId = (int) $categoryId;
252
        $sql = "DELETE FROM $table WHERE category_id = $categoryId";
253
        Database::query($sql);
254
    }
255
256
    /**
257
     * Get all possible tickets statuses.
258
     *
259
     * @return array
260
     */
261
    public static function get_all_tickets_status()
262
    {
263
        $table = Database::get_main_table(TABLE_TICKET_STATUS);
264
        $sql = "SELECT * FROM $table";
265
        $result = Database::query($sql);
266
        $types = [];
267
        while ($row = Database::fetch_assoc($result)) {
268
            $types[] = $row;
269
        }
270
271
        return $types;
272
    }
273
274
    /**
275
     * Inserts a new ticket in the corresponding tables.
276
     *
277
     * @param int    $category_id
278
     * @param int    $course_id
279
     * @param int    $sessionId
280
     * @param int    $project_id
281
     * @param string $other_area
282
     * @param string $subject
283
     * @param string $content
284
     * @param string $personalEmail
285
     * @param array  $fileAttachments
286
     * @param string $source
287
     * @param string $priority
288
     * @param string $status
289
     * @param int    $assignedUserId
290
     * @param int    $exerciseId
291
     * @param int    $lpId
292
     *
293
     * @return bool
294
     */
295
    public static function add(
296
        $category_id,
297
        $course_id,
298
        $sessionId,
299
        $project_id,
300
        $other_area,
301
        $subject,
302
        $content,
303
        $personalEmail = '',
304
        $fileAttachments = [],
305
        $source = '',
306
        $priority = '',
307
        $status = '',
308
        $assignedUserId = 0,
309
        $exerciseId = null,
310
        $lpId = null
311
    ) {
312
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
313
        $table_support_category = Database::get_main_table(TABLE_TICKET_CATEGORY);
314
315
        if (empty($category_id)) {
316
            return false;
317
        }
318
319
        $currentUserId = api_get_user_id();
320
        $currentUserInfo = api_get_user_info();
321
        $now = api_get_utc_datetime();
322
        $course_id = (int) $course_id;
323
        $category_id = (int) $category_id;
324
        $project_id = (int) $project_id;
325
        $priority = empty($priority) ? self::PRIORITY_NORMAL : (int) $priority;
326
327
        if ($status === '') {
328
            $status = self::STATUS_NEW;
329
            if ($other_area > 0) {
330
                $status = self::STATUS_FORWARDED;
331
            }
332
        }
333
334
        if (!empty($category_id)) {
335
            if (empty($assignedUserId)) {
336
                $usersInCategory = self::getUsersInCategory($category_id);
337
                if (!empty($usersInCategory) && count($usersInCategory) > 0) {
338
                    $userCategoryInfo = $usersInCategory[0];
339
                    if (isset($userCategoryInfo['user_id'])) {
340
                        $assignedUserId = $userCategoryInfo['user_id'];
341
                    }
342
                }
343
            }
344
        }
345
346
        $assignedUserInfo = [];
347
        if (!empty($assignedUserId)) {
348
            $assignedUserInfo = api_get_user_info($assignedUserId);
349
            if (empty($assignedUserInfo)) {
350
                return false;
351
            }
352
        }
353
354
        // insert_ticket
355
        $params = [
356
            'project_id' => $project_id,
357
            'category_id' => $category_id,
358
            'priority_id' => $priority,
359
            'personal_email' => $personalEmail,
360
            'status_id' => $status,
361
            'start_date' => $now,
362
            'sys_insert_user_id' => $currentUserId,
363
            'sys_insert_datetime' => $now,
364
            'sys_lastedit_user_id' => $currentUserId,
365
            'sys_lastedit_datetime' => $now,
366
            'source' => $source,
367
            'assigned_last_user' => $assignedUserId,
368
            'subject' => $subject,
369
            'message' => $content,
370
        ];
371
        if (!empty($exerciseId)) {
372
            $params['exercise_id'] = $exerciseId;
373
        }
374
375
        if (!empty($lpId)) {
376
            $params['lp_id'] = $lpId;
377
        }
378
379
        if (!empty($exerciseId)) {
380
            $params['exercise_id'] = $exerciseId;
381
        }
382
383
        if (!empty($lpId)) {
384
            $params['lp_id'] = $lpId;
385
        }
386
387
        if (!empty($course_id)) {
388
            $params['course_id'] = $course_id;
389
        }
390
391
        if (!empty($sessionId)) {
392
            $params['session_id'] = $sessionId;
393
        }
394
395
        $ticketId = Database::insert($table_support_tickets, $params);
396
397
        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...
398
            $ticket_code = 'A'.str_pad($ticketId, 11, '0', STR_PAD_LEFT);
399
            $titleCreated = sprintf(
400
                get_lang('TicketXCreated'),
401
                $ticket_code
402
            );
403
404
            Display::addFlash(Display::return_message(
405
                $titleCreated,
406
                'normal',
407
                false
408
            ));
409
410
            if ($assignedUserId != 0) {
411
                self::assignTicketToUser(
412
                    $ticketId,
413
                    $assignedUserId
414
                );
415
416
                Display::addFlash(Display::return_message(
417
                    sprintf(
418
                        get_lang('TicketXAssignedToUserX'),
419
                        $ticket_code,
420
                        $assignedUserInfo['complete_name']
421
                    ),
422
                    'normal',
423
                    false
424
                ));
425
            }
426
427
            if (!empty($fileAttachments)) {
428
                $attachmentCount = 0;
429
                foreach ($fileAttachments as $attach) {
430
                    if (!empty($attach['tmp_name'])) {
431
                        $attachmentCount++;
432
                    }
433
                }
434
                if ($attachmentCount > 0) {
435
                    self::insertMessage(
436
                        $ticketId,
437
                        '',
438
                        '',
439
                        $fileAttachments,
440
                        $currentUserId
441
                    );
442
                }
443
            }
444
445
            // Update code
446
            $sql = "UPDATE $table_support_tickets
447
                    SET code = '$ticket_code'
448
                    WHERE id = '$ticketId'";
449
            Database::query($sql);
450
451
            // Update total
452
            $sql = "UPDATE $table_support_category
453
                    SET total_tickets = total_tickets + 1
454
                    WHERE id = $category_id";
455
            Database::query($sql);
456
457
            $helpDeskMessage =
458
                '<table>
459
                        <tr>
460
                            <td width="100px"><b>'.get_lang('User').'</b></td>
461
                            <td width="400px">'.$currentUserInfo['complete_name'].'</td>
462
                        </tr>
463
                        <tr>
464
                            <td width="100px"><b>'.get_lang('Username').'</b></td>
465
                            <td width="400px">'.$currentUserInfo['username'].'</td>
466
                        </tr>
467
                        <tr>
468
                            <td width="100px"><b>'.get_lang('Email').'</b></td>
469
                            <td width="400px">'.$currentUserInfo['email'].'</td>
470
                        </tr>
471
                        <tr>
472
                            <td width="100px"><b>'.get_lang('Phone').'</b></td>
473
                            <td width="400px">'.$currentUserInfo['phone'].'</td>
474
                        </tr>
475
                        <tr>
476
                            <td width="100px"><b>'.get_lang('Date').'</b></td>
477
                            <td width="400px">'.api_convert_and_format_date($now, DATE_TIME_FORMAT_LONG).'</td>
478
                        </tr>
479
                        <tr>
480
                            <td width="100px"><b>'.get_lang('Title').'</b></td>
481
                            <td width="400px">'.Security::remove_XSS($subject).'</td>
482
                        </tr>
483
                        <tr>
484
                            <td width="100px"><b>'.get_lang('Description').'</b></td>
485
                            <td width="400px">'.Security::remove_XSS($content).'</td>
486
                        </tr>
487
                    </table>';
488
489
            if ($assignedUserId != 0) {
490
                $href = api_get_path(WEB_CODE_PATH).'/ticket/ticket_details.php?ticket_id='.$ticketId;
491
                $helpDeskMessage .= sprintf(
492
                    get_lang('TicketAssignedToXCheckZAtLinkY'),
493
                    $assignedUserInfo['complete_name'],
494
                    $href,
495
                    $ticketId
496
                );
497
            }
498
499
            if (empty($category_id)) {
500
                if (api_get_setting('ticket_send_warning_to_all_admins') === 'true') {
501
                    $warningSubject = sprintf(
502
                        get_lang('TicketXCreatedWithNoCategory'),
503
                        $ticket_code
504
                    );
505
                    Display::addFlash(Display::return_message($warningSubject));
506
507
                    $admins = UserManager::get_all_administrators();
508
                    foreach ($admins as $userId => $data) {
509
                        if ($data['active']) {
510
                            MessageManager::send_message_simple(
511
                                $userId,
512
                                $warningSubject,
513
                                $helpDeskMessage
514
                            );
515
                        }
516
                    }
517
                }
518
            } else {
519
                $categoryInfo = self::getCategory($category_id);
520
                $usersInCategory = self::getUsersInCategory($category_id);
521
522
                $message = '<h2>'.get_lang('TicketInformation').'</h2><br />'.$helpDeskMessage;
523
524
                if (api_get_setting('ticket_warn_admin_no_user_in_category') === 'true') {
525
                    if (empty($usersInCategory)) {
526
                        $subject = sprintf(
527
                            get_lang('WarningCategoryXDoesntHaveUsers'),
528
                            $categoryInfo['name']
529
                        );
530
531
                        if (api_get_setting('ticket_send_warning_to_all_admins') === 'true') {
532
                            Display::addFlash(Display::return_message(
533
                                sprintf(
534
                                    get_lang('CategoryWithNoUserNotificationSentToAdmins'),
535
                                    $categoryInfo['name']
536
                                ),
537
                                null,
538
                                false
539
                            ));
540
541
                            $admins = UserManager::get_all_administrators();
542
                            foreach ($admins as $userId => $data) {
543
                                if ($data['active']) {
544
                                    self::sendNotification(
545
                                        $ticketId,
546
                                        $subject,
547
                                        $message,
548
                                        $userId
549
                                    );
550
                                }
551
                            }
552
                        } else {
553
                            Display::addFlash(Display::return_message($subject));
554
                        }
555
                    }
556
                }
557
558
                // Send notification to all users
559
                if (!empty($usersInCategory)) {
560
                    foreach ($usersInCategory as $data) {
561
                        if ($data['user_id']) {
562
                            self::sendNotification(
563
                                $ticketId,
564
                                $subject,
565
                                $message,
566
                                $data['user_id']
567
                            );
568
                        }
569
                    }
570
                }
571
            }
572
573
            if (!empty($personalEmail)) {
574
                api_mail_html(
575
                    get_lang('VirtualSupport'),
576
                    $personalEmail,
577
                    get_lang('IncidentResentToVirtualSupport'),
578
                    $helpDeskMessage
579
                );
580
            }
581
582
            self::sendNotification(
583
                $ticketId,
584
                $titleCreated,
585
                $helpDeskMessage
586
            );
587
588
            return true;
589
        }
590
591
        return false;
592
    }
593
594
    /**
595
     * Assign ticket to admin.
596
     *
597
     * @param int $ticketId
598
     * @param int $userId
599
     *
600
     * @return bool
601
     */
602
    public static function assignTicketToUser(
603
        $ticketId,
604
        $userId
605
    ) {
606
        $ticketId = (int) $ticketId;
607
        $userId = (int) $userId;
608
609
        if (empty($ticketId)) {
610
            return false;
611
        }
612
613
        $ticket = self::get_ticket_detail_by_id($ticketId);
614
615
        if ($ticket) {
616
            $table = Database::get_main_table(TABLE_TICKET_TICKET);
617
            $sql = "UPDATE $table
618
                    SET assigned_last_user = $userId
619
                    WHERE id = $ticketId";
620
            Database::query($sql);
621
622
            $table = Database::get_main_table(TABLE_TICKET_ASSIGNED_LOG);
623
            $params = [
624
                'ticket_id' => $ticketId,
625
                'user_id' => $userId,
626
                'sys_insert_user_id' => api_get_user_id(),
627
                'assigned_date' => api_get_utc_datetime(),
628
            ];
629
            Database::insert($table, $params);
630
631
            return true;
632
        } else {
633
            return false;
634
        }
635
    }
636
637
    /**
638
     * Insert message between Users and Admins.
639
     *
640
     * @param int    $ticketId
641
     * @param string $subject
642
     * @param string $content
643
     * @param array  $fileAttachments
644
     * @param int    $userId
645
     * @param string $status
646
     * @param bool   $sendConfirmation
647
     *
648
     * @return bool
649
     */
650
    public static function insertMessage(
651
        $ticketId,
652
        $subject,
653
        $content,
654
        $fileAttachments,
655
        $userId,
656
        $status = 'NOL',
657
        $sendConfirmation = false
658
    ) {
659
        $ticketId = (int) $ticketId;
660
        $userId = (int) $userId;
661
        $table_support_messages = Database::get_main_table(TABLE_TICKET_MESSAGE);
662
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
663
        if ($sendConfirmation) {
664
            $form =
665
                '<form action="ticket_details.php?ticket_id='.$ticketId.'" id="confirmticket" method="POST" >
666
                     <p>'.get_lang('TicketWasThisAnswerSatisfying').'</p>
667
                     <button class="btn btn-primary responseyes" name="response" id="responseyes" value="1">'.
668
                get_lang('Yes').'</button>
669
                     <button class="btn btn-danger responseno" name="response" id="responseno" value="0">'.
670
                get_lang('No').'</button>
671
                 </form>';
672
            $content .= $form;
673
        }
674
675
        $now = api_get_utc_datetime();
676
677
        $params = [
678
            'ticket_id' => $ticketId,
679
            'subject' => $subject,
680
            'message' => $content,
681
            'ip_address' => api_get_real_ip(),
682
            'sys_insert_user_id' => $userId,
683
            'sys_insert_datetime' => $now,
684
            'sys_lastedit_user_id' => $userId,
685
            'sys_lastedit_datetime' => $now,
686
            'status' => $status,
687
        ];
688
        $messageId = Database::insert($table_support_messages, $params);
689
        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...
690
            // update_total_message
691
            $sql = "UPDATE $table_support_tickets
692
                    SET
693
                        sys_lastedit_user_id = $userId,
694
                        sys_lastedit_datetime = '$now',
695
                        total_messages = (
696
                            SELECT COUNT(*) as total_messages
697
                            FROM $table_support_messages
698
                            WHERE ticket_id = $ticketId
699
                        )
700
                    WHERE id = $ticketId ";
701
            Database::query($sql);
702
703
            if (is_array($fileAttachments)) {
704
                foreach ($fileAttachments as $file_attach) {
705
                    if ($file_attach['error'] == 0) {
706
                        self::saveMessageAttachmentFile(
707
                            $file_attach,
708
                            $ticketId,
709
                            $messageId
710
                        );
711
                    } else {
712
                        if ($file_attach['error'] != UPLOAD_ERR_NO_FILE) {
713
                            return false;
714
                        }
715
                    }
716
                }
717
            }
718
        }
719
720
        return true;
721
    }
722
723
    /**
724
     * Attachment files when a message is sent.
725
     *
726
     * @param $file_attach
727
     * @param $ticketId
728
     * @param $message_id
729
     *
730
     * @return bool
731
     */
732
    public static function saveMessageAttachmentFile(
733
        $file_attach,
734
        $ticketId,
735
        $message_id
736
    ) {
737
        $now = api_get_utc_datetime();
738
        $userId = api_get_user_id();
739
        $ticketId = (int) $ticketId;
740
741
        $new_file_name = add_ext_on_mime(
742
            stripslashes($file_attach['name']),
743
            $file_attach['type']
744
        );
745
        $table_support_message_attachments = Database::get_main_table(TABLE_TICKET_MESSAGE_ATTACHMENTS);
746
        if (!filter_extension($new_file_name)) {
747
            echo Display::return_message(
748
                get_lang('UplUnableToSaveFileFilteredExtension'),
749
                'error'
750
            );
751
        } else {
752
            $result = api_upload_file('ticket_attachment', $file_attach, $ticketId);
753
            if ($result) {
0 ignored issues
show
introduced by
$result is a non-empty array, thus is always true.
Loading history...
Bug Best Practice introduced by
The expression $result of type array<string,string> 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...
754
                $safe_file_name = Database::escape_string($new_file_name);
755
                $safe_new_file_name = Database::escape_string($result['path_to_save']);
756
                $sql = "INSERT INTO $table_support_message_attachments (
757
                        filename,
758
                        path,
759
                        ticket_id,
760
                        message_id,
761
                        size,
762
                        sys_insert_user_id,
763
                        sys_insert_datetime,
764
                        sys_lastedit_user_id,
765
                        sys_lastedit_datetime
766
                    ) VALUES (
767
                        '$safe_file_name',
768
                        '$safe_new_file_name',
769
                        '$ticketId',
770
                        '$message_id',
771
                        '".$file_attach['size']."',
772
                        '$userId',
773
                        '$now',
774
                        '$userId',
775
                        '$now'
776
                    )";
777
                Database::query($sql);
778
779
                return true;
780
            }
781
        }
782
    }
783
784
    /**
785
     * Get tickets by userId.
786
     *
787
     * @param int $from
788
     * @param int $number_of_items
789
     * @param $column
790
     * @param $direction
791
     *
792
     * @return array
793
     */
794
    public static function getTicketsByCurrentUser(
795
        $from,
796
        $number_of_items,
797
        $column,
798
        $direction
799
    ) {
800
        $table_support_category = Database::get_main_table(TABLE_TICKET_CATEGORY);
801
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
802
        $table_support_priority = Database::get_main_table(TABLE_TICKET_PRIORITY);
803
        $table_support_status = Database::get_main_table(TABLE_TICKET_STATUS);
804
        $direction = !empty($direction) ? $direction : 'DESC';
805
        $userId = api_get_user_id();
806
        $userInfo = api_get_user_info($userId);
807
808
        if (empty($userInfo)) {
809
            return [];
810
        }
811
        $isAdmin = UserManager::is_admin($userId);
812
813
        if (!isset($_GET['project_id'])) {
814
            return [];
815
        }
816
817
        switch ($column) {
818
            case 0:
819
                $column = 'ticket_id';
820
                break;
821
            case 1:
822
                $column = 'status_name';
823
                break;
824
            case 2:
825
                $column = 'start_date';
826
                break;
827
            case 3:
828
                $column = 'sys_lastedit_datetime';
829
                break;
830
            case 4:
831
                $column = 'category_name';
832
                break;
833
            case 5:
834
                $column = 'sys_insert_user_id';
835
                break;
836
            case 6:
837
                $column = 'assigned_last_user';
838
                break;
839
            case 7:
840
                $column = 'total_messages';
841
                break;
842
            case 8:
843
                $column = 'subject';
844
                break;
845
            default:
846
                $column = 'ticket_id';
847
        }
848
849
        $sql = "SELECT DISTINCT
850
                ticket.*,
851
                ticket.id ticket_id,
852
                status.name AS status_name,
853
                ticket.start_date,
854
                ticket.sys_lastedit_datetime,
855
                cat.name AS category_name,
856
                priority.name AS priority_name,
857
                ticket.total_messages AS total_messages,
858
                ticket.message AS message,
859
                ticket.subject AS subject,
860
                ticket.assigned_last_user
861
            FROM $table_support_tickets ticket
862
            INNER JOIN $table_support_category cat
863
            ON (cat.id = ticket.category_id)
864
            INNER JOIN $table_support_priority priority
865
            ON (ticket.priority_id = priority.id)
866
            INNER JOIN $table_support_status status
867
            ON (ticket.status_id = status.id)
868
            WHERE 1=1
869
        ";
870
871
        $projectId = (int) $_GET['project_id'];
872
        $userIsAllowInProject = self::userIsAllowInProject($userInfo, $projectId);
873
874
        // Check if a role was set to the project
875
        if ($userIsAllowInProject == false) {
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...
876
            $sql .= " AND (ticket.assigned_last_user = $userId OR ticket.sys_insert_user_id = $userId )";
877
        }
878
879
        // Search simple
880
        if (isset($_GET['submit_simple']) && $_GET['keyword'] != '') {
881
            $keyword = Database::escape_string(trim($_GET['keyword']));
882
            $sql .= " AND (
883
                      ticket.id LIKE '%$keyword%' OR
884
                      ticket.code LIKE '%$keyword%' OR
885
                      ticket.subject LIKE '%$keyword%' OR
886
                      ticket.message LIKE '%$keyword%' OR
887
                      ticket.keyword LIKE '%$keyword%' OR
888
                      ticket.source LIKE '%$keyword%' OR
889
                      cat.name LIKE '%$keyword%' OR
890
                      status.name LIKE '%$keyword%' OR
891
                      priority.name LIKE '%$keyword%' OR
892
                      ticket.personal_email LIKE '%$keyword%'
893
            )";
894
        }
895
896
        $keywords = [
897
            'project_id' => 'ticket.project_id',
898
            'keyword_category' => 'ticket.category_id',
899
            'keyword_assigned_to' => 'ticket.assigned_last_user',
900
            'keyword_source' => 'ticket.source ',
901
            'keyword_status' => 'ticket.status_id',
902
            'keyword_priority' => 'ticket.priority_id',
903
        ];
904
905
        foreach ($keywords as $keyword => $label) {
906
            if (isset($_GET[$keyword])) {
907
                $data = Database::escape_string(trim($_GET[$keyword]));
908
                if (!empty($data)) {
909
                    $sql .= " AND $label = '$data' ";
910
                }
911
            }
912
        }
913
914
        // Search advanced
915
        $keyword_start_date_start = isset($_GET['keyword_start_date_start']) ? Database::escape_string(trim($_GET['keyword_start_date_start'])) : '';
916
        $keyword_start_date_end = isset($_GET['keyword_start_date_end']) ? Database::escape_string(trim($_GET['keyword_start_date_end'])) : '';
917
        $keyword_course = isset($_GET['keyword_course']) ? Database::escape_string(trim($_GET['keyword_course'])) : '';
918
        $keyword_range = !empty($keyword_start_date_start) && !empty($keyword_start_date_end);
919
920
        if ($keyword_range == false && $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...
921
            $sql .= " AND DATE_FORMAT(ticket.start_date,'%d/%m/%Y') >= '$keyword_start_date_start' ";
922
        }
923
        if ($keyword_range && $keyword_start_date_start != '' && $keyword_start_date_end != '') {
924
            $sql .= " AND DATE_FORMAT(ticket.start_date,'%d/%m/%Y') >= '$keyword_start_date_start'
925
                      AND DATE_FORMAT(ticket.start_date,'%d/%m/%Y') <= '$keyword_start_date_end'";
926
        }
927
928
        if ($keyword_course != '') {
929
            $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
930
            $sql .= " AND ticket.course_id IN (
931
                     SELECT id FROM $course_table
932
                     WHERE (
933
                        title LIKE '%$keyword_course%' OR
934
                        code LIKE '%$keyword_course%' OR
935
                        visual_code LIKE '%$keyword_course%'
936
                     )
937
            )";
938
        }
939
        $sql .= " ORDER BY `$column` $direction";
940
        $sql .= " LIMIT $from, $number_of_items";
941
942
        $result = Database::query($sql);
943
        $tickets = [];
944
        $webPath = api_get_path(WEB_PATH);
945
        while ($row = Database::fetch_assoc($result)) {
946
            $userInfo = api_get_user_info($row['sys_insert_user_id']);
947
            $hrefUser = $webPath.'main/admin/user_information.php?user_id='.$userInfo['user_id'];
948
            $name = "<a href='$hrefUser'> {$userInfo['complete_name_with_username']} </a>";
949
            if ($row['assigned_last_user'] != 0) {
950
                $assignedUserInfo = api_get_user_info($row['assigned_last_user']);
951
                if (!empty($assignedUserInfo)) {
952
                    $hrefResp = $webPath.'main/admin/user_information.php?user_id='.$assignedUserInfo['user_id'];
953
                    $row['assigned_last_user'] = "<a href='$hrefResp'> {$assignedUserInfo['complete_name_with_username']} </a>";
954
                } else {
955
                    $row['assigned_last_user'] = get_lang('UnknownUser');
956
                }
957
            } else {
958
                if ($row['status_id'] !== self::STATUS_FORWARDED) {
959
                    $row['assigned_last_user'] = '<span style="color:#ff0000;">'.get_lang('ToBeAssigned').'</span>';
960
                } else {
961
                    $row['assigned_last_user'] = '<span style="color:#00ff00;">'.get_lang('MessageResent').'</span>';
962
                }
963
            }
964
965
            switch ($row['source']) {
966
                case self::SOURCE_PRESENTIAL:
967
                    $img_source = 'icons/32/user.png';
968
                    break;
969
                case self::SOURCE_EMAIL:
970
                    $img_source = 'icons/32/mail.png';
971
                    break;
972
                case self::SOURCE_PHONE:
973
                    $img_source = 'icons/32/event.png';
974
                    break;
975
                default:
976
                    $img_source = 'icons/32/ticket.png';
977
                    break;
978
            }
979
980
            $row['start_date'] = Display::dateToStringAgoAndLongDate($row['start_date']);
981
            $row['sys_lastedit_datetime'] = Display::dateToStringAgoAndLongDate($row['sys_lastedit_datetime']);
982
983
            $icon = Display::return_icon(
984
                $img_source,
985
                get_lang('Info'),
986
                ['style' => 'margin-right: 10px; float: left;']
987
            );
988
989
            $icon .= '<a href="ticket_details.php?ticket_id='.$row['id'].'">'.$row['code'].'</a>';
990
991
            if ($isAdmin) {
992
                $ticket = [
993
                    $icon.' '.Security::remove_XSS($row['subject']),
994
                    $row['status_name'],
995
                    $row['start_date'],
996
                    $row['sys_lastedit_datetime'],
997
                    $row['category_name'],
998
                    $name,
999
                    $row['assigned_last_user'],
1000
                    $row['total_messages'],
1001
                ];
1002
            } else {
1003
                $ticket = [
1004
                    $icon.' '.Security::remove_XSS($row['subject']),
1005
                    $row['status_name'],
1006
                    $row['start_date'],
1007
                    $row['sys_lastedit_datetime'],
1008
                    $row['category_name'],
1009
                ];
1010
            }
1011
            if ($isAdmin) {
1012
                $ticket['0'] .= '&nbsp;&nbsp;<a href="javascript:void(0)" onclick="load_history_ticket(\'div_'.$row['ticket_id'].'\','.$row['ticket_id'].')">
1013
					<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('Historial').'" alt="'.get_lang('Historial').'"/>
1014
					<div class="blackboard_hide" id="div_'.$row['ticket_id'].'">&nbsp;&nbsp;</div>
1015
					</a>&nbsp;&nbsp;';
1016
            }
1017
            $tickets[] = $ticket;
1018
        }
1019
1020
        return $tickets;
1021
    }
1022
1023
    /**
1024
     * @return int
1025
     */
1026
    public static function getTotalTicketsCurrentUser()
1027
    {
1028
        $table_support_category = Database::get_main_table(TABLE_TICKET_CATEGORY);
1029
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
1030
        $table_support_priority = Database::get_main_table(TABLE_TICKET_PRIORITY);
1031
        $table_support_status = Database::get_main_table(TABLE_TICKET_STATUS);
1032
1033
        $userInfo = api_get_user_info();
1034
        if (empty($userInfo)) {
1035
            return 0;
1036
        }
1037
        $userId = $userInfo['id'];
1038
1039
        if (!isset($_GET['project_id'])) {
1040
            return 0;
1041
        }
1042
1043
        $sql = "SELECT COUNT(ticket.id) AS total
1044
                FROM $table_support_tickets ticket
1045
                INNER JOIN $table_support_category cat
1046
                ON (cat.id = ticket.category_id)
1047
                INNER JOIN $table_support_priority priority
1048
                ON (ticket.priority_id = priority.id)
1049
                INNER JOIN $table_support_status status
1050
                ON (ticket.status_id = status.id)
1051
	            WHERE 1 = 1";
1052
1053
        $projectId = (int) $_GET['project_id'];
1054
        $allowRoleList = self::getAllowedRolesFromProject($projectId);
1055
1056
        // Check if a role was set to the project
1057
        if (!empty($allowRoleList) && is_array($allowRoleList)) {
1058
            if (!in_array($userInfo['status'], $allowRoleList)) {
1059
                $sql .= " AND (ticket.assigned_last_user = $userId OR ticket.sys_insert_user_id = $userId )";
1060
            }
1061
        } else {
1062
            if (!api_is_platform_admin()) {
1063
                $sql .= " AND (ticket.assigned_last_user = $userId OR ticket.sys_insert_user_id = $userId )";
1064
            }
1065
        }
1066
1067
        // Search simple
1068
        if (isset($_GET['submit_simple'])) {
1069
            if ($_GET['keyword'] != '') {
1070
                $keyword = Database::escape_string(trim($_GET['keyword']));
1071
                $sql .= " AND (
1072
                          ticket.code LIKE '%$keyword%' OR
1073
                          ticket.subject LIKE '%$keyword%' OR
1074
                          ticket.message LIKE '%$keyword%' OR
1075
                          ticket.keyword LIKE '%$keyword%' OR
1076
                          ticket.personal_email LIKE '%$keyword%' OR
1077
                          ticket.source LIKE '%$keyword%'
1078
                )";
1079
            }
1080
        }
1081
1082
        $keywords = [
1083
            'project_id' => 'ticket.project_id',
1084
            'keyword_category' => 'ticket.category_id',
1085
            'keyword_assigned_to' => 'ticket.assigned_last_user',
1086
            'keyword_source' => 'ticket.source',
1087
            'keyword_status' => 'ticket.status_id',
1088
            'keyword_priority' => 'ticket.priority_id',
1089
        ];
1090
1091
        foreach ($keywords as $keyword => $sqlLabel) {
1092
            if (isset($_GET[$keyword])) {
1093
                $data = Database::escape_string(trim($_GET[$keyword]));
1094
                $sql .= " AND $sqlLabel = '$data' ";
1095
            }
1096
        }
1097
1098
        // Search advanced
1099
        $keyword_start_date_start = isset($_GET['keyword_start_date_start']) ? Database::escape_string(trim($_GET['keyword_start_date_start'])) : '';
1100
        $keyword_start_date_end = isset($_GET['keyword_start_date_end']) ? Database::escape_string(trim($_GET['keyword_start_date_end'])) : '';
1101
        $keyword_range = isset($_GET['keyword_dates']) ? Database::escape_string(trim($_GET['keyword_dates'])) : '';
1102
        $keyword_course = isset($_GET['keyword_course']) ? Database::escape_string(trim($_GET['keyword_course'])) : '';
1103
1104
        if ($keyword_range == false && $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...
1105
            $sql .= " AND DATE_FORMAT( ticket.start_date,'%d/%m/%Y') = '$keyword_start_date_start' ";
1106
        }
1107
        if ($keyword_range && $keyword_start_date_start != '' && $keyword_start_date_end != '') {
1108
            $sql .= " AND DATE_FORMAT( ticket.start_date,'%d/%m/%Y') >= '$keyword_start_date_start'
1109
                      AND DATE_FORMAT( ticket.start_date,'%d/%m/%Y') <= '$keyword_start_date_end'";
1110
        }
1111
        if ($keyword_course != '') {
1112
            $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
1113
            $sql .= " AND ticket.course_id IN (
1114
                        SELECT id
1115
                        FROM $course_table
1116
                        WHERE (
1117
                            title LIKE '%$keyword_course%' OR
1118
                            code LIKE '%$keyword_course%' OR
1119
                            visual_code LIKE '%$keyword_course%'
1120
                        )
1121
                   ) ";
1122
        }
1123
1124
        $res = Database::query($sql);
1125
        $obj = Database::fetch_object($res);
1126
1127
        return (int) $obj->total;
1128
    }
1129
1130
    /**
1131
     * @param int $id
1132
     *
1133
     * @return false|MessageAttachment
1134
     */
1135
    public static function getTicketMessageAttachment($id)
1136
    {
1137
        $id = (int) $id;
1138
        $em = Database::getManager();
1139
        $item = $em->getRepository('ChamiloTicketBundle:MessageAttachment')->find($id);
1140
        if ($item) {
1141
            return $item;
1142
        }
1143
1144
        return false;
1145
    }
1146
1147
    /**
1148
     * @param int $id
1149
     *
1150
     * @return array
1151
     */
1152
    public static function getTicketMessageAttachmentsByTicketId($id)
1153
    {
1154
        $id = (int) $id;
1155
        $em = Database::getManager();
1156
        $items = $em->getRepository('ChamiloTicketBundle:MessageAttachment')->findBy(['ticket' => $id]);
1157
        if ($items) {
1158
            return $items;
1159
        }
1160
1161
        return false;
1162
    }
1163
1164
    /**
1165
     * @param int $ticketId
1166
     *
1167
     * @return array
1168
     */
1169
    public static function get_ticket_detail_by_id($ticketId)
1170
    {
1171
        $ticketId = (int) $ticketId;
1172
        $table_support_category = Database::get_main_table(TABLE_TICKET_CATEGORY);
1173
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
1174
        $table_support_priority = Database::get_main_table(TABLE_TICKET_PRIORITY);
1175
        $table_support_status = Database::get_main_table(TABLE_TICKET_STATUS);
1176
        $table_support_messages = Database::get_main_table(TABLE_TICKET_MESSAGE);
1177
        $table_support_message_attachments = Database::get_main_table(TABLE_TICKET_MESSAGE_ATTACHMENTS);
1178
        $table_main_user = Database::get_main_table(TABLE_MAIN_USER);
1179
1180
        $sql = "SELECT
1181
                    ticket.*,
1182
                    cat.name,
1183
                    status.name as status,
1184
                    priority.name priority
1185
                FROM $table_support_tickets ticket
1186
                INNER JOIN $table_support_category cat
1187
                ON (cat.id = ticket.category_id)
1188
                INNER JOIN $table_support_priority priority
1189
                ON (priority.id = ticket.priority_id)
1190
                INNER JOIN $table_support_status status
1191
                ON (status.id = ticket.status_id)
1192
		        WHERE
1193
                    ticket.id = $ticketId ";
1194
        $result = Database::query($sql);
1195
        $ticket = [];
1196
        if (Database::num_rows($result) > 0) {
1197
            while ($row = Database::fetch_assoc($result)) {
1198
                $row['course'] = null;
1199
                $row['start_date_from_db'] = $row['start_date'];
1200
                $row['start_date'] = api_convert_and_format_date(
1201
                    api_get_local_time($row['start_date']),
1202
                    DATE_TIME_FORMAT_LONG,
1203
                    api_get_timezone()
1204
                );
1205
                $row['end_date_from_db'] = $row['end_date'];
1206
                $row['end_date'] = api_convert_and_format_date(
1207
                    api_get_local_time($row['end_date']),
1208
                    DATE_TIME_FORMAT_LONG,
1209
                    api_get_timezone()
1210
                );
1211
                $row['sys_lastedit_datetime_from_db'] = $row['sys_lastedit_datetime'];
1212
                $row['sys_lastedit_datetime'] = api_convert_and_format_date(
1213
                    api_get_local_time($row['sys_lastedit_datetime']),
1214
                    DATE_TIME_FORMAT_LONG,
1215
                    api_get_timezone()
1216
                );
1217
                $row['course_url'] = null;
1218
                if ($row['course_id'] != 0) {
1219
                    $course = api_get_course_info_by_id($row['course_id']);
1220
                    $sessionId = 0;
1221
                    if ($row['session_id']) {
1222
                        $sessionId = $row['session_id'];
1223
                    }
1224
                    if ($course) {
1225
                        $row['course_url'] = '<a href="'.$course['course_public_url'].'?id_session='.$sessionId.'">'.$course['name'].'</a>';
1226
                    }
1227
1228
                    $row['exercise_url'] = null;
1229
1230
                    if (!empty($row['exercise_id'])) {
1231
                        $exerciseTitle = ExerciseLib::getExerciseTitleById($row['exercise_id']);
1232
                        $dataExercise = [
1233
                            'cidReq' => $course['code'],
1234
                            'id_session' => $sessionId,
1235
                            'exerciseId' => $row['exercise_id'],
1236
                        ];
1237
                        $urlParamsExercise = http_build_query($dataExercise);
1238
1239
                        $row['exercise_url'] = '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/overview.php?'.$urlParamsExercise.'">'.$exerciseTitle.'</a>';
1240
                    }
1241
1242
                    $row['lp_url'] = null;
1243
1244
                    if (!empty($row['lp_id'])) {
1245
                        $lpName = learnpath::getLpNameById($row['lp_id']);
1246
                        $dataLp = [
1247
                            'cidReq' => $course['code'],
1248
                            'id_session' => $sessionId,
1249
                            'lp_id' => $row['lp_id'],
1250
                            'action' => 'view',
1251
                        ];
1252
                        $urlParamsLp = http_build_query($dataLp);
1253
1254
                        $row['lp_url'] = '<a href="'.api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.$urlParamsLp.'">'.$lpName.'</a>';
1255
                    }
1256
                }
1257
1258
                $userInfo = api_get_user_info($row['sys_insert_user_id']);
1259
                $row['user_url'] = '<a href="'.api_get_path(WEB_PATH).'main/admin/user_information.php?user_id='.$userInfo['user_id'].'">
1260
                '.$userInfo['complete_name'].'</a>';
1261
                $ticket['usuario'] = $userInfo;
1262
                $ticket['ticket'] = $row;
1263
            }
1264
1265
            $sql = "SELECT *, message.id as message_id
1266
                    FROM $table_support_messages message
1267
                    INNER JOIN $table_main_user user
1268
                    ON (message.sys_insert_user_id = user.user_id)
1269
                    WHERE
1270
                        message.ticket_id = '$ticketId' ";
1271
            $result = Database::query($sql);
1272
            $ticket['messages'] = [];
1273
            $attach_icon = Display::return_icon('attachment.gif', '');
1274
            $webPath = api_get_path(WEB_CODE_PATH);
1275
            while ($row = Database::fetch_assoc($result)) {
1276
                $message = $row;
1277
                $message['admin'] = UserManager::is_admin($message['user_id']);
1278
                $message['user_info'] = api_get_user_info($message['user_id']);
1279
                $sql = "SELECT *
1280
                        FROM $table_support_message_attachments
1281
                        WHERE
1282
                            message_id = ".$row['message_id']." AND
1283
                            ticket_id = $ticketId";
1284
1285
                $result_attach = Database::query($sql);
1286
                while ($row2 = Database::fetch_assoc($result_attach)) {
1287
                    $archiveURL = $webPath.'ticket/download.php?ticket_id='.$ticketId.'&id='.$row2['id'];
1288
                    $row2['attachment_link'] = $attach_icon.
1289
                        '&nbsp;<a href="'.$archiveURL.'">'.$row2['filename'].'</a>&nbsp;('.$row2['size'].')';
1290
                    $message['attachments'][] = $row2;
1291
                }
1292
                $ticket['messages'][] = $message;
1293
            }
1294
        }
1295
1296
        return $ticket;
1297
    }
1298
1299
    /**
1300
     * @param int $ticketId
1301
     * @param int $userId
1302
     *
1303
     * @return bool
1304
     */
1305
    public static function update_message_status($ticketId, $userId)
1306
    {
1307
        $ticketId = (int) $ticketId;
1308
        $userId = (int) $userId;
1309
        $table_support_messages = Database::get_main_table(TABLE_TICKET_MESSAGE);
1310
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
1311
        $now = api_get_utc_datetime();
1312
        $sql = "UPDATE $table_support_messages
1313
                SET
1314
                    status = 'LEI',
1315
                    sys_lastedit_user_id ='".api_get_user_id()."',
1316
                    sys_lastedit_datetime ='".$now."'
1317
                WHERE ticket_id ='$ticketId' ";
1318
1319
        if (api_is_platform_admin()) {
1320
            $sql .= " AND sys_insert_user_id = '$userId'";
1321
        } else {
1322
            $sql .= " AND sys_insert_user_id != '$userId'";
1323
        }
1324
        $result = Database::query($sql);
1325
        if (Database::affected_rows($result) > 0) {
1326
            Database::query(
1327
                "UPDATE $table_support_tickets SET
1328
                    status_id = '".self::STATUS_PENDING."'
1329
                 WHERE id ='$ticketId' AND status_id = '".self::STATUS_NEW."'"
1330
            );
1331
1332
            return true;
1333
        }
1334
1335
        return false;
1336
    }
1337
1338
    /**
1339
     * Send notification to a user through the internal messaging system.
1340
     *
1341
     * @param int    $ticketId
1342
     * @param string $title
1343
     * @param string $message
1344
     * @param int    $onlyToUserId
1345
     *
1346
     * @return bool
1347
     */
1348
    public static function sendNotification($ticketId, $title, $message, $onlyToUserId = 0)
1349
    {
1350
        $ticketInfo = self::get_ticket_detail_by_id($ticketId);
1351
1352
        if (empty($ticketInfo)) {
1353
            return false;
1354
        }
1355
1356
        $assignedUserInfo = api_get_user_info($ticketInfo['ticket']['assigned_last_user']);
1357
        $requestUserInfo = $ticketInfo['usuario'];
1358
        $ticketCode = $ticketInfo['ticket']['code'];
1359
        $status = $ticketInfo['ticket']['status'];
1360
        $priority = $ticketInfo['ticket']['priority'];
1361
1362
        // Subject
1363
        $titleEmail = "[$ticketCode] $title";
1364
1365
        // Content
1366
        $href = api_get_path(WEB_CODE_PATH).'ticket/ticket_details.php?ticket_id='.$ticketId;
1367
        $ticketUrl = Display::url($ticketCode, $href);
1368
        $messageEmail = get_lang('TicketNum').": $ticketUrl <br />";
1369
        $messageEmail .= get_lang('Status').": $status <br />";
1370
        $messageEmail .= get_lang('Priority').": $priority <br />";
1371
        $messageEmail .= '<hr /><br />';
1372
        $messageEmail .= $message;
1373
        $currentUserId = api_get_user_id();
1374
        $attachmentList = [];
1375
        $attachments = self::getTicketMessageAttachmentsByTicketId($ticketId);
1376
        if (!empty($attachments)) {
1377
            /** @var MessageAttachment $attachment */
1378
            foreach ($attachments as $attachment) {
1379
                $file = api_get_uploaded_file(
1380
                    'ticket_attachment',
1381
                    $ticketId,
1382
                    $attachment->getPath()
1383
                );
1384
                if (!empty($file)) {
1385
                    $attachmentList[] = [
1386
                        'tmp_name' => api_get_uploaded_file(
1387
                            'ticket_attachment',
1388
                            $ticketId,
1389
                            $attachment->getPath()
1390
                        ),
1391
                        'size' => $attachment->getSize(),
1392
                        'name' => $attachment->getFilename(),
1393
                        'error' => 0,
1394
                    ];
1395
                }
1396
            }
1397
        }
1398
1399
        if (!empty($onlyToUserId)) {
1400
            // Send only to specific user
1401
            if ($currentUserId != $onlyToUserId) {
1402
                MessageManager::send_message_simple(
1403
                    $onlyToUserId,
1404
                    $titleEmail,
1405
                    $messageEmail,
1406
                    0,
1407
                    false,
1408
                    false,
1409
                    [],
1410
                    false,
1411
                    $attachmentList
1412
                );
1413
            }
1414
        } else {
1415
            // Send to assigned user and to author
1416
            if ($requestUserInfo && $currentUserId != $requestUserInfo['id']) {
1417
                MessageManager::send_message_simple(
1418
                    $requestUserInfo['id'],
1419
                    $titleEmail,
1420
                    $messageEmail,
1421
                    0,
1422
                    false,
1423
                    false,
1424
                    [],
1425
                    false,
1426
                    $attachmentList
1427
                );
1428
            }
1429
1430
            if ($assignedUserInfo &&
1431
                $requestUserInfo['id'] != $assignedUserInfo['id'] &&
1432
                $currentUserId != $assignedUserInfo['id']
1433
            ) {
1434
                MessageManager::send_message_simple(
1435
                    $assignedUserInfo['id'],
1436
                    $titleEmail,
1437
                    $messageEmail,
1438
                    0,
1439
                    false,
1440
                    false,
1441
                    [],
1442
                    false,
1443
                    $attachmentList
1444
                );
1445
            }
1446
        }
1447
    }
1448
1449
    /**
1450
     * @param array $params
1451
     * @param int   $ticketId
1452
     * @param int   $userId
1453
     *
1454
     * @return bool
1455
     */
1456
    public static function updateTicket(
1457
        $params,
1458
        $ticketId,
1459
        $userId
1460
    ) {
1461
        $now = api_get_utc_datetime();
1462
        $table = Database::get_main_table(TABLE_TICKET_TICKET);
1463
        $newParams = [
1464
            'priority_id' => isset($params['priority_id']) ? (int) $params['priority_id'] : '',
1465
            'status_id' => isset($params['status_id']) ? (int) $params['status_id'] : '',
1466
            'sys_lastedit_user_id' => (int) $userId,
1467
            'sys_lastedit_datetime' => $now,
1468
        ];
1469
        Database::update($table, $newParams, ['id = ? ' => $ticketId]);
1470
1471
        return true;
1472
    }
1473
1474
    /**
1475
     * @param int $status_id
1476
     * @param int $ticketId
1477
     * @param int $userId
1478
     *
1479
     * @return bool
1480
     */
1481
    public static function update_ticket_status(
1482
        $status_id,
1483
        $ticketId,
1484
        $userId
1485
    ) {
1486
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
1487
1488
        $ticketId = (int) $ticketId;
1489
        $status_id = (int) $status_id;
1490
        $userId = (int) $userId;
1491
        $now = api_get_utc_datetime();
1492
1493
        $sql = "UPDATE $table_support_tickets
1494
                SET
1495
                    status_id = '$status_id',
1496
                    sys_lastedit_user_id ='$userId',
1497
                    sys_lastedit_datetime ='".$now."'
1498
                WHERE id ='$ticketId'";
1499
        $result = Database::query($sql);
1500
1501
        if (Database::affected_rows($result) > 0) {
1502
            self::sendNotification(
1503
                $ticketId,
1504
                get_lang('TicketUpdated'),
1505
                get_lang('TicketUpdated')
1506
            );
1507
1508
            return true;
1509
        }
1510
1511
        return false;
1512
    }
1513
1514
    /**
1515
     * @return mixed
1516
     */
1517
    public static function getNumberOfMessages()
1518
    {
1519
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
1520
        $table_support_messages = Database::get_main_table(TABLE_TICKET_MESSAGE);
1521
        $table_main_user = Database::get_main_table(TABLE_MAIN_USER);
1522
        $table_main_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
1523
        $user_info = api_get_user_info();
1524
        $userId = $user_info['user_id'];
1525
        $sql = "SELECT COUNT(DISTINCT ticket.id) AS unread
1526
                FROM $table_support_tickets ticket,
1527
                $table_support_messages message ,
1528
                $table_main_user user
1529
                WHERE
1530
                    ticket.id = message.ticket_id AND
1531
                    message.status = 'NOL' AND
1532
                    user.user_id = message.sys_insert_user_id ";
1533
        if (!api_is_platform_admin()) {
1534
            $sql .= " AND ticket.request_user = '$userId'
1535
                      AND user_id IN (SELECT user_id FROM $table_main_admin)  ";
1536
        } else {
1537
            $sql .= " AND user_id NOT IN (SELECT user_id FROM $table_main_admin)
1538
                      AND ticket.status_id != '".self::STATUS_FORWARDED."'";
1539
        }
1540
        $sql .= "  AND ticket.project_id != '' ";
1541
        $res = Database::query($sql);
1542
        $obj = Database::fetch_object($res);
1543
1544
        return $obj->unread;
1545
    }
1546
1547
    /**
1548
     * @param int $ticketId
1549
     * @param int $userId
1550
     */
1551
    public static function send_alert($ticketId, $userId)
1552
    {
1553
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
1554
        $now = api_get_utc_datetime();
1555
1556
        $ticketId = (int) $ticketId;
1557
        $userId = (int) $userId;
1558
1559
        $sql = "UPDATE $table_support_tickets SET
1560
                  priority_id = '".self::PRIORITY_HIGH."',
1561
                  sys_lastedit_user_id = $userId,
1562
                  sys_lastedit_datetime = '$now'
1563
                WHERE id = $ticketId";
1564
        Database::query($sql);
1565
    }
1566
1567
    /**
1568
     * @param int $ticketId
1569
     * @param int $userId
1570
     */
1571
    public static function close_ticket($ticketId, $userId)
1572
    {
1573
        $ticketId = (int) $ticketId;
1574
        $userId = (int) $userId;
1575
1576
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
1577
        $now = api_get_utc_datetime();
1578
        $sql = "UPDATE $table_support_tickets SET
1579
                    status_id = '".self::STATUS_CLOSE."',
1580
                    sys_lastedit_user_id ='$userId',
1581
                    sys_lastedit_datetime ='".$now."',
1582
                    end_date ='$now'
1583
                WHERE id ='$ticketId'";
1584
        Database::query($sql);
1585
1586
        self::sendNotification(
1587
            $ticketId,
1588
            get_lang('TicketClosed'),
1589
            get_lang('TicketClosed')
1590
        );
1591
    }
1592
1593
    /**
1594
     * Close old tickets.
1595
     */
1596
    public static function close_old_tickets()
1597
    {
1598
        $table = Database::get_main_table(TABLE_TICKET_TICKET);
1599
        $now = api_get_utc_datetime();
1600
        $userId = api_get_user_id();
1601
        $sql = "UPDATE $table
1602
                SET
1603
                    status_id = '".self::STATUS_CLOSE."',
1604
                    sys_lastedit_user_id ='$userId',
1605
                    sys_lastedit_datetime ='$now',
1606
                    end_date = '$now'
1607
                WHERE
1608
                    DATEDIFF('$now', sys_lastedit_datetime) > 7 AND
1609
                    status_id != '".self::STATUS_CLOSE."' AND
1610
                    status_id != '".self::STATUS_NEW."' AND
1611
                    status_id != '".self::STATUS_FORWARDED."'";
1612
        Database::query($sql);
1613
    }
1614
1615
    /**
1616
     * @param int $ticketId
1617
     *
1618
     * @return array
1619
     */
1620
    public static function get_assign_log($ticketId)
1621
    {
1622
        $table = Database::get_main_table(TABLE_TICKET_ASSIGNED_LOG);
1623
        $ticketId = (int) $ticketId;
1624
1625
        $sql = "SELECT * FROM $table
1626
                WHERE ticket_id = $ticketId
1627
                ORDER BY assigned_date DESC";
1628
        $result = Database::query($sql);
1629
        $history = [];
1630
        $webpath = api_get_path(WEB_PATH);
1631
        while ($row = Database::fetch_assoc($result)) {
1632
            if ($row['user_id'] != 0) {
1633
                $assignuser = api_get_user_info($row['user_id']);
1634
                $row['assignuser'] = '<a href="'.$webpath.'main/admin/user_information.php?user_id='.$row['user_id'].'"  target="_blank">'.
1635
                $assignuser['username'].'</a>';
1636
            } else {
1637
                $row['assignuser'] = get_lang('Unassign');
1638
            }
1639
            $row['assigned_date'] = Display::dateToStringAgoAndLongDate($row['assigned_date']);
1640
            $insertuser = api_get_user_info($row['sys_insert_user_id']);
1641
            $row['insertuser'] = '<a href="'.$webpath.'main/admin/user_information.php?user_id='.$row['sys_insert_user_id'].'"  target="_blank">'.
1642
                $insertuser['username'].'</a>';
1643
            $history[] = $row;
1644
        }
1645
1646
        return $history;
1647
    }
1648
1649
    /**
1650
     * @param $from
1651
     * @param $number_of_items
1652
     * @param $column
1653
     * @param $direction
1654
     * @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...
1655
     *
1656
     * @return array
1657
     */
1658
    public static function export_tickets_by_user_id(
1659
        $from,
1660
        $number_of_items,
1661
        $column,
1662
        $direction,
1663
        $userId = null
1664
    ) {
1665
        $from = (int) $from;
1666
        $number_of_items = (int) $number_of_items;
1667
        $table_support_category = Database::get_main_table(
1668
            TABLE_TICKET_CATEGORY
1669
        );
1670
        $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
1671
        $table_support_priority = Database::get_main_table(TABLE_TICKET_PRIORITY);
1672
        $table_support_status = Database::get_main_table(TABLE_TICKET_STATUS);
1673
        $table_support_messages = Database::get_main_table(TABLE_TICKET_MESSAGE);
1674
        $table_main_user = Database::get_main_table(TABLE_MAIN_USER);
1675
1676
        if (is_null($direction)) {
1677
            $direction = 'DESC';
1678
        }
1679
        if (is_null($userId) || $userId == 0) {
1680
            $userId = api_get_user_id();
1681
        }
1682
1683
        $sql = "SELECT
1684
                    ticket.code,
1685
                    ticket.sys_insert_datetime,
1686
                    ticket.sys_lastedit_datetime,
1687
                    cat.name as category,
1688
                    CONCAT(user.lastname,' ', user.firstname) AS fullname,
1689
                    status.name as status,
1690
                    ticket.total_messages as messages,
1691
                    ticket.assigned_last_user as responsable
1692
                FROM $table_support_tickets ticket,
1693
                $table_support_category cat ,
1694
                $table_support_priority priority,
1695
                $table_support_status status ,
1696
                $table_main_user user
1697
                WHERE
1698
                    cat.id = ticket.category_id
1699
                    AND ticket.priority_id = priority.id
1700
                    AND ticket.status_id = status.id
1701
                    AND user.user_id = ticket.request_user ";
1702
        // Search simple
1703
        if (isset($_GET['submit_simple'])) {
1704
            if ($_GET['keyword'] !== '') {
1705
                $keyword = Database::escape_string(trim($_GET['keyword']));
1706
                $sql .= " AND (ticket.code = '$keyword'
1707
                          OR user.firstname LIKE '%$keyword%'
1708
                          OR user.lastname LIKE '%$keyword%'
1709
                          OR concat(user.firstname,' ',user.lastname) LIKE '%$keyword%'
1710
                          OR concat(user.lastname,' ',user.firstname) LIKE '%$keyword%'
1711
                          OR user.username LIKE '%$keyword%')  ";
1712
            }
1713
        }
1714
        // Search advanced
1715
        if (isset($_GET['submit_advanced'])) {
1716
            $keyword_category = Database::escape_string(
1717
                trim($_GET['keyword_category'])
1718
            );
1719
            $keyword_request_user = Database::escape_string(
1720
                trim($_GET['keyword_request_user'])
1721
            );
1722
            $keywordAssignedTo = (int) $_GET['keyword_assigned_to'];
1723
            $keyword_start_date_start = Database::escape_string(
1724
                trim($_GET['keyword_start_date_start'])
1725
            );
1726
            $keyword_start_date_end = Database::escape_string(
1727
                trim($_GET['keyword_start_date_end'])
1728
            );
1729
            $keyword_status = Database::escape_string(
1730
                trim($_GET['keyword_status'])
1731
            );
1732
            $keyword_source = Database::escape_string(
1733
                trim($_GET['keyword_source'])
1734
            );
1735
            $keyword_priority = Database::escape_string(
1736
                trim($_GET['keyword_priority'])
1737
            );
1738
            $keyword_range = Database::escape_string(
1739
                trim($_GET['keyword_dates'])
1740
            );
1741
            $keyword_unread = Database::escape_string(
1742
                trim($_GET['keyword_unread'])
1743
            );
1744
            $keyword_course = Database::escape_string(
1745
                trim($_GET['keyword_course'])
1746
            );
1747
1748
            if ($keyword_category != '') {
1749
                $sql .= " AND ticket.category_id = '$keyword_category'  ";
1750
            }
1751
            if ($keyword_request_user != '') {
1752
                $sql .= " AND (ticket.request_user = '$keyword_request_user'
1753
                          OR user.firstname LIKE '%$keyword_request_user%'
1754
                          OR user.official_code LIKE '%$keyword_request_user%'
1755
                          OR user.lastname LIKE '%$keyword_request_user%'
1756
                          OR concat(user.firstname,' ',user.lastname) LIKE '%$keyword_request_user%'
1757
                          OR concat(user.lastname,' ',user.firstname) LIKE '%$keyword_request_user%'
1758
                          OR user.username LIKE '%$keyword_request_user%') ";
1759
            }
1760
            if (!empty($keywordAssignedTo)) {
1761
                $sql .= " AND ticket.assigned_last_user = $keywordAssignedTo ";
1762
            }
1763
            if ($keyword_status != '') {
1764
                $sql .= " AND ticket.status_id = '$keyword_status'  ";
1765
            }
1766
            if ($keyword_range == '' && $keyword_start_date_start != '') {
1767
                $sql .= " AND DATE_FORMAT( ticket.start_date,'%d/%m/%Y') = '$keyword_start_date_start' ";
1768
            }
1769
            if ($keyword_range == '1' && $keyword_start_date_start != '' && $keyword_start_date_end != '') {
1770
                $sql .= " AND DATE_FORMAT( ticket.start_date,'%d/%m/%Y') >= '$keyword_start_date_start'
1771
                          AND DATE_FORMAT( ticket.start_date,'%d/%m/%Y') <= '$keyword_start_date_end'";
1772
            }
1773
            if ($keyword_priority != '') {
1774
                $sql .= " AND ticket.priority_id = '$keyword_priority'  ";
1775
            }
1776
            if ($keyword_source != '') {
1777
                $sql .= " AND ticket.source = '$keyword_source' ";
1778
            }
1779
            if ($keyword_priority != '') {
1780
                $sql .= " AND ticket.priority_id = '$keyword_priority' ";
1781
            }
1782
            if ($keyword_course != '') {
1783
                $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
1784
                $sql .= " AND ticket.course_id IN ( ";
1785
                $sql .= "SELECT id
1786
                         FROM $course_table
1787
                         WHERE (title LIKE '%$keyword_course%'
1788
                         OR code LIKE '%$keyword_course%'
1789
                         OR visual_code LIKE '%$keyword_course%' )) ";
1790
            }
1791
            if ($keyword_unread == 'yes') {
1792
                $sql .= " AND ticket.id IN (
1793
                          SELECT ticket.id
1794
                          FROM $table_support_tickets ticket,
1795
                          $table_support_messages message,
1796
                          $table_main_user user
1797
                          WHERE ticket.id = message.ticket_id
1798
                          AND message.status = 'NOL'
1799
                          AND message.sys_insert_user_id = user.user_id
1800
                          AND user.status != 1   AND ticket.status_id != '".self::STATUS_FORWARDED."'
1801
                          GROUP BY ticket.id)";
1802
            } else {
1803
                if ($keyword_unread == 'no') {
1804
                    $sql .= " AND ticket.id NOT IN (
1805
                              SELECT ticket.id
1806
                              FROM  $table_support_tickets ticket,
1807
                              $table_support_messages message,
1808
                              $table_main_user user
1809
                              WHERE ticket.id = message.ticket_id
1810
                              AND message.status = 'NOL'
1811
                              AND message.sys_insert_user_id = user.user_id
1812
                              AND user.status != 1
1813
                              AND ticket.status_id != '".self::STATUS_FORWARDED."'
1814
                             GROUP BY ticket.id)";
1815
                }
1816
            }
1817
        }
1818
1819
        $sql .= " LIMIT $from,$number_of_items";
1820
1821
        $result = Database::query($sql);
1822
        $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...
1823
            utf8_decode('Ticket#'),
1824
            utf8_decode('Fecha'),
1825
            utf8_decode('Fecha Edicion'),
1826
            utf8_decode('Categoria'),
1827
            utf8_decode('Usuario'),
1828
            utf8_decode('Estado'),
1829
            utf8_decode('Mensajes'),
1830
            utf8_decode('Responsable'),
1831
            utf8_decode('Programa'),
1832
        ];
1833
1834
        while ($row = Database::fetch_assoc($result)) {
1835
            if ($row['responsable'] != 0) {
1836
                $row['responsable'] = api_get_user_info($row['responsable']);
1837
                $row['responsable'] = $row['responsable']['firstname'].' '.$row['responsable']['lastname'];
1838
            }
1839
            $row['sys_insert_datetime'] = api_format_date(
1840
                $row['sys_insert_datetime'],
1841
                '%d/%m/%y - %I:%M:%S %p'
1842
            );
1843
            $row['sys_lastedit_datetime'] = api_format_date(
1844
                $row['sys_lastedit_datetime'],
1845
                '%d/%m/%y - %I:%M:%S %p'
1846
            );
1847
            $row['category'] = utf8_decode($row['category']);
1848
            $row['programa'] = utf8_decode($row['fullname']);
1849
            $row['fullname'] = utf8_decode($row['fullname']);
1850
            $row['responsable'] = utf8_decode($row['responsable']);
1851
            $tickets[] = $row;
1852
        }
1853
1854
        return $tickets;
1855
    }
1856
1857
    /**
1858
     * @param string $url
1859
     * @param int    $projectId
1860
     *
1861
     * @return FormValidator
1862
     */
1863
    public static function getCategoryForm($url, $projectId)
1864
    {
1865
        $form = new FormValidator('category', 'post', $url);
1866
        $form->addText('name', get_lang('Name'));
1867
        $form->addHtmlEditor('description', get_lang('Description'));
1868
        $form->addHidden('project_id', $projectId);
1869
        $form->addButtonUpdate(get_lang('Save'));
1870
1871
        return $form;
1872
    }
1873
1874
    /**
1875
     * @return array
1876
     */
1877
    public static function getStatusList()
1878
    {
1879
        $items = Database::getManager()->getRepository('ChamiloTicketBundle:Status')->findAll();
1880
1881
        $list = [];
1882
        /** @var Status $row */
1883
        foreach ($items as $row) {
1884
            $list[$row->getId()] = $row->getName();
1885
        }
1886
1887
        return $list;
1888
    }
1889
1890
    /**
1891
     * @param array $criteria
1892
     *
1893
     * @return array
1894
     */
1895
    public static function getTicketsFromCriteria($criteria)
1896
    {
1897
        $items = Database::getManager()->getRepository('ChamiloTicketBundle:Ticket')->findBy($criteria);
1898
1899
        $list = [];
1900
        /** @var Ticket $row */
1901
        foreach ($items as $row) {
1902
            $list[$row->getId()] = $row->getCode();
1903
        }
1904
1905
        return $list;
1906
    }
1907
1908
    /**
1909
     * @param string $code
1910
     *
1911
     * @return int
1912
     */
1913
    public static function getStatusIdFromCode($code)
1914
    {
1915
        $item = Database::getManager()
1916
            ->getRepository('ChamiloTicketBundle:Status')
1917
            ->findOneBy(['code' => $code])
1918
        ;
1919
1920
        if ($item) {
1921
            return $item->getId();
1922
        }
1923
1924
        return 0;
1925
    }
1926
1927
    /**
1928
     * @return array
1929
     */
1930
    public static function getPriorityList()
1931
    {
1932
        $projects = Database::getManager()->getRepository('ChamiloTicketBundle:Priority')->findAll();
1933
1934
        $list = [];
1935
        /** @var Priority $row */
1936
        foreach ($projects as $row) {
1937
            $list[$row->getId()] = $row->getName();
1938
        }
1939
1940
        return $list;
1941
    }
1942
1943
    /**
1944
     * @return array
1945
     */
1946
    public static function getProjects()
1947
    {
1948
        $projects = Database::getManager()->getRepository('ChamiloTicketBundle:Project')->findAll();
1949
1950
        $list = [];
1951
        /** @var Project $row */
1952
        foreach ($projects as $row) {
1953
            $list[] = [
1954
                'id' => $row->getId(),
1955
                '0' => $row->getId(),
1956
                '1' => $row->getName(),
1957
                '2' => $row->getDescription(),
1958
                '3' => $row->getId(),
1959
            ];
1960
        }
1961
1962
        return $list;
1963
    }
1964
1965
    /**
1966
     * @return array
1967
     */
1968
    public static function getProjectsSimple()
1969
    {
1970
        $projects = Database::getManager()->getRepository('ChamiloTicketBundle:Project')->findAll();
1971
1972
        $list = [];
1973
        /** @var Project $row */
1974
        foreach ($projects as $row) {
1975
            $list[] = [
1976
                'id' => $row->getId(),
1977
                '0' => $row->getId(),
1978
                '1' => Display::url(
1979
                    $row->getName(),
1980
                    api_get_path(WEB_CODE_PATH).'ticket/tickets.php?project_id='.$row->getId()
1981
                ),
1982
                '2' => $row->getDescription(),
1983
            ];
1984
        }
1985
1986
        return $list;
1987
    }
1988
1989
    /**
1990
     * @return int
1991
     */
1992
    public static function getProjectsCount()
1993
    {
1994
        $count = Database::getManager()->getRepository('ChamiloTicketBundle:Project')->createQueryBuilder('p')
1995
            ->select('COUNT(p.id)')
1996
            ->getQuery()
1997
            ->getSingleScalarResult();
1998
1999
        return $count;
2000
    }
2001
2002
    /**
2003
     * @param array $params
2004
     */
2005
    public static function addProject($params)
2006
    {
2007
        $project = new Project();
2008
        $project->setName($params['name']);
2009
        $project->setDescription($params['description']);
2010
        $project->setInsertUserId(api_get_user_id());
2011
2012
        Database::getManager()->persist($project);
2013
        Database::getManager()->flush();
2014
    }
2015
2016
    /**
2017
     * @param int $id
2018
     *
2019
     * @return Project
2020
     */
2021
    public static function getProject($id)
2022
    {
2023
        return Database::getManager()->getRepository('ChamiloTicketBundle:Project')->find($id);
2024
    }
2025
2026
    /**
2027
     * @param int   $id
2028
     * @param array $params
2029
     */
2030
    public static function updateProject($id, $params)
2031
    {
2032
        $project = self::getProject($id);
2033
        $project->setName($params['name']);
2034
        $project->setDescription($params['description']);
2035
        $project->setLastEditDateTime(new DateTime($params['sys_lastedit_datetime']));
2036
        $project->setLastEditUserId($params['sys_lastedit_user_id']);
2037
2038
        Database::getManager()->merge($project);
2039
        Database::getManager()->flush();
2040
    }
2041
2042
    /**
2043
     * @param int $id
2044
     */
2045
    public static function deleteProject($id)
2046
    {
2047
        $project = self::getProject($id);
2048
        if ($project) {
0 ignored issues
show
introduced by
$project is of type Chamilo\TicketBundle\Entity\Project, thus it always evaluated to true.
Loading history...
2049
            Database::getManager()->remove($project);
2050
            Database::getManager()->flush();
2051
        }
2052
    }
2053
2054
    /**
2055
     * @param string $url
2056
     *
2057
     * @return FormValidator
2058
     */
2059
    public static function getProjectForm($url)
2060
    {
2061
        $form = new FormValidator('project', 'post', $url);
2062
        $form->addText('name', get_lang('Name'));
2063
        $form->addHtmlEditor('description', get_lang('Description'));
2064
        $form->addButtonUpdate(get_lang('Save'));
2065
2066
        return $form;
2067
    }
2068
2069
    /**
2070
     * @return array
2071
     */
2072
    public static function getStatusAdminList()
2073
    {
2074
        $items = Database::getManager()->getRepository('ChamiloTicketBundle:Status')->findAll();
2075
2076
        $list = [];
2077
        /** @var Status $row */
2078
        foreach ($items as $row) {
2079
            $list[] = [
2080
                'id' => $row->getId(),
2081
                'code' => $row->getCode(),
2082
                '0' => $row->getId(),
2083
                '1' => $row->getName(),
2084
                '2' => $row->getDescription(),
2085
                '3' => $row->getId(),
2086
            ];
2087
        }
2088
2089
        return $list;
2090
    }
2091
2092
    /**
2093
     * @return array
2094
     */
2095
    public static function getStatusSimple()
2096
    {
2097
        $projects = Database::getManager()->getRepository('ChamiloTicketBundle:Status')->findAll();
2098
2099
        $list = [];
2100
        /** @var Project $row */
2101
        foreach ($projects as $row) {
2102
            $list[] = [
2103
                'id' => $row->getId(),
2104
                '0' => $row->getId(),
2105
                '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

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

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