@@ -242,7 +242,7 @@ public static function getUsersInCategory($categoryId)
242
242
return Database::store_result ($ result );
243
243
}
244
244
245
- /**
245
+ /**
246
246
* Returns the list of category IDs assigned to a user.
247
247
*
248
248
* @param int $userId
@@ -355,14 +355,37 @@ public static function add(
355
355
$ course_id = (int ) $ course_id ;
356
356
$ category_id = (int ) $ category_id ;
357
357
$ project_id = (int ) $ project_id ;
358
- $ priority = empty ($ priority ) ? self ::PRIORITY_NORMAL : (int ) $ priority ;
359
358
360
- if ($ status === '' ) {
361
- $ status = self ::STATUS_NEW ;
359
+ // Resolve priority to numeric ID (accepts ID or code like 'NRM')
360
+ $ priorityId = null ;
361
+ $ priorityCodeOrId = $ priority ;
362
+ if (empty ($ priorityCodeOrId )) {
363
+ $ priorityCodeOrId = self ::PRIORITY_NORMAL ;
364
+ }
365
+ if (is_numeric ($ priorityCodeOrId )) {
366
+ $ priorityId = (int ) $ priorityCodeOrId ;
367
+ } else {
368
+ $ priorityId = self ::getPriorityIdFromCode ((string ) $ priorityCodeOrId );
369
+ }
370
+ if (empty ($ priorityId )) {
371
+ // Fallback to default priority if mapping failed
372
+ $ priorityId = self ::getPriorityIdFromCode (self ::PRIORITY_NORMAL );
373
+ }
374
+
375
+ // Resolve status to numeric ID (accepts ID or code like 'NAT')
376
+ $ statusId = null ;
377
+ $ statusCodeOrId = $ status ;
378
+ if ($ statusCodeOrId === '' || $ statusCodeOrId === null ) {
379
+ $ statusCodeOrId = self ::STATUS_NEW ;
362
380
if ($ other_area > 0 ) {
363
- $ status = self ::STATUS_FORWARDED ;
381
+ $ statusCodeOrId = self ::STATUS_FORWARDED ;
364
382
}
365
383
}
384
+ if (is_numeric ($ statusCodeOrId )) {
385
+ $ statusId = (int ) $ statusCodeOrId ;
386
+ } else {
387
+ $ statusId = self ::getStatusIdFromCode ((string ) $ statusCodeOrId );
388
+ }
366
389
367
390
if (!empty ($ category_id )) {
368
391
if (empty ($ assignedUserId )) {
@@ -388,9 +411,9 @@ public static function add(
388
411
$ params = [
389
412
'project_id ' => $ project_id ,
390
413
'category_id ' => $ category_id ,
391
- 'priority_id ' => $ priority ,
414
+ 'priority_id ' => $ priorityId ,
392
415
'personal_email ' => $ personalEmail ,
393
- 'status_id ' => $ status ,
416
+ 'status_id ' => $ statusId ,
394
417
'start_date ' => $ now ,
395
418
'sys_insert_user_id ' => $ currentUserId ,
396
419
'sys_insert_datetime ' => $ now ,
@@ -935,7 +958,7 @@ public static function getTicketsByCurrentUser(
935
958
936
959
// Check if a role was set to the project
937
960
if ($ userIsAllowInProject == false ) {
938
- $ categoryList = self ::getCategoryIdsByUser ($ userId , $ projectId );
961
+ $ categoryList = self ::getCategoryIdsByUser ($ userId , $ projectId );
939
962
$ categoryCondition = '' ;
940
963
if (!empty ($ categoryList )) {
941
964
$ categoryIds = implode (', ' , array_map ('intval ' , $ categoryList ));
@@ -945,6 +968,7 @@ public static function getTicketsByCurrentUser(
945
968
$ sql .= " AND (ticket.assigned_last_user = $ userId OR ticket.sys_insert_user_id = $ userId " .$ categoryCondition .") " ;
946
969
}
947
970
971
+
948
972
// Search simple
949
973
if (isset ($ _GET ['submit_simple ' ]) && $ _GET ['keyword ' ] != '' ) {
950
974
$ keyword = Database::escape_string (trim ($ _GET ['keyword ' ]));
@@ -1112,65 +1136,54 @@ public static function getTotalTicketsCurrentUser()
1112
1136
if (empty ($ userInfo )) {
1113
1137
return 0 ;
1114
1138
}
1115
- $ userId = $ userInfo ['id ' ];
1139
+ $ userId = ( int ) $ userInfo ['id ' ];
1116
1140
1117
1141
if (!isset ($ _GET ['project_id ' ])) {
1118
1142
return 0 ;
1119
1143
}
1120
1144
1121
- $ sql = "SELECT COUNT(ticket.id) AS total
1145
+ $ sql = "SELECT COUNT(DISTINCT ticket.id) AS total
1122
1146
FROM $ table_support_tickets ticket
1123
1147
INNER JOIN $ table_support_category cat
1124
- ON (cat.id = ticket.category_id)
1148
+ ON (cat.id = ticket.category_id)
1125
1149
INNER JOIN $ table_support_priority priority
1126
- ON (ticket.priority_id = priority.id)
1150
+ ON (ticket.priority_id = priority.id)
1127
1151
INNER JOIN $ table_support_status status
1128
- ON (ticket.status_id = status.id)
1129
- WHERE 1 = 1 " ;
1152
+ ON (ticket.status_id = status.id)
1153
+ WHERE 1 = 1 " ;
1130
1154
1131
1155
$ projectId = (int ) $ _GET ['project_id ' ];
1132
- $ allowRoleList = self ::getAllowedRolesFromProject ($ projectId );
1133
-
1134
- // Check if a role was set to the project
1135
- if (!empty ($ allowRoleList ) && is_array ($ allowRoleList )) {
1136
- if (!in_array ($ userInfo ['status ' ], $ allowRoleList )) {
1137
- $ categoryList = self ::getCategoryIdsByUser ($ userId , $ projectId );
1138
- $ categoryCondition = '' ;
1139
- if (!empty ($ categoryList )) {
1140
- $ categoryIds = implode (', ' , array_map ('intval ' , $ categoryList ));
1141
- $ categoryCondition = " OR ticket.category_id IN ( $ categoryIds) " ;
1142
- }
1143
-
1144
- $ sql .= " AND (ticket.assigned_last_user = $ userId OR ticket.sys_insert_user_id = $ userId " .$ categoryCondition .") " ;
1145
- }
1146
- } else {
1147
- if (!api_is_platform_admin ()) {
1148
- $ categoryList = self ::getCategoryIdsByUser ($ userId , $ projectId );
1149
- $ categoryCondition = '' ;
1150
- if (!empty ($ categoryList )) {
1151
- $ categoryIds = implode (', ' , array_map ('intval ' , $ categoryList ));
1152
- $ categoryCondition = " OR ticket.category_id IN ( $ categoryIds) " ;
1153
- }
1156
+ $ userIsAllowInProject = self ::userIsAllowInProject ($ userInfo , $ projectId );
1154
1157
1155
- $ sql .= " AND (ticket.assigned_last_user = $ userId OR ticket.sys_insert_user_id = $ userId " .$ categoryCondition .") " ;
1158
+ // Apply same permission constraints as getTicketsByCurrentUser
1159
+ if ($ userIsAllowInProject == false ) {
1160
+ $ categoryList = self ::getCategoryIdsByUser ($ userId , $ projectId );
1161
+ $ categoryCondition = '' ;
1162
+ if (!empty ($ categoryList )) {
1163
+ $ categoryIds = implode (', ' , array_map ('intval ' , $ categoryList ));
1164
+ $ categoryCondition = " OR ticket.category_id IN ( $ categoryIds) " ;
1156
1165
}
1166
+ $ sql .= " AND (ticket.assigned_last_user = $ userId OR ticket.sys_insert_user_id = $ userId " .$ categoryCondition .") " ;
1157
1167
}
1158
1168
1159
- // Search simple
1160
- if (isset ($ _GET ['submit_simple ' ])) {
1161
- if ($ _GET ['keyword ' ] != '' ) {
1162
- $ keyword = Database::escape_string (trim ($ _GET ['keyword ' ]));
1163
- $ sql .= " AND (
1164
- ticket.code LIKE '% $ keyword%' OR
1165
- ticket.subject LIKE '% $ keyword%' OR
1166
- ticket.message LIKE '% $ keyword%' OR
1167
- ticket.keyword LIKE '% $ keyword%' OR
1168
- ticket.personal_email LIKE '% $ keyword%' OR
1169
- ticket.source LIKE '% $ keyword%'
1170
- ) " ;
1171
- }
1169
+ // Simple search (align with getTicketsByCurrentUser)
1170
+ if (isset ($ _GET ['submit_simple ' ]) && $ _GET ['keyword ' ] !== '' ) {
1171
+ $ keyword = Database::escape_string (trim ($ _GET ['keyword ' ]));
1172
+ $ sql .= " AND (
1173
+ ticket.id LIKE '% $ keyword%' OR
1174
+ ticket.code LIKE '% $ keyword%' OR
1175
+ ticket.subject LIKE '% $ keyword%' OR
1176
+ ticket.message LIKE '% $ keyword%' OR
1177
+ ticket.keyword LIKE '% $ keyword%' OR
1178
+ ticket.source LIKE '% $ keyword%' OR
1179
+ cat.name LIKE '% $ keyword%' OR
1180
+ status.name LIKE '% $ keyword%' OR
1181
+ priority.name LIKE '% $ keyword%' OR
1182
+ ticket.personal_email LIKE '% $ keyword%'
1183
+ ) " ;
1172
1184
}
1173
1185
1186
+ // Exact-match filters
1174
1187
$ keywords = [
1175
1188
'project_id ' => 'ticket.project_id ' ,
1176
1189
'keyword_category ' => 'ticket.category_id ' ,
@@ -1182,42 +1195,41 @@ public static function getTotalTicketsCurrentUser()
1182
1195
];
1183
1196
1184
1197
foreach ($ keywords as $ keyword => $ sqlLabel ) {
1185
- if (! empty ($ _GET [$ keyword ])) {
1198
+ if (isset ($ _GET [$ keyword ])) {
1186
1199
$ data = Database::escape_string (trim ($ _GET [$ keyword ]));
1187
- $ sql .= " AND $ sqlLabel = ' $ data' " ;
1200
+ if ($ data !== '' ) {
1201
+ $ sql .= " AND $ sqlLabel = ' $ data' " ;
1202
+ }
1188
1203
}
1189
1204
}
1190
1205
1191
- // Search advanced
1206
+ // Advanced search: date range and course
1192
1207
$ keyword_start_date_start = isset ($ _GET ['keyword_start_date_start ' ]) ? Database::escape_string (trim ($ _GET ['keyword_start_date_start ' ])) : '' ;
1193
1208
$ keyword_start_date_end = isset ($ _GET ['keyword_start_date_end ' ]) ? Database::escape_string (trim ($ _GET ['keyword_start_date_end ' ])) : '' ;
1194
- $ keyword_range = isset ($ _GET ['keyword_dates ' ]) ? Database::escape_string (trim ($ _GET ['keyword_dates ' ])) : '' ;
1195
1209
$ keyword_course = isset ($ _GET ['keyword_course ' ]) ? Database::escape_string (trim ($ _GET ['keyword_course ' ])) : '' ;
1210
+ $ keyword_range = !empty ($ keyword_start_date_start ) && !empty ($ keyword_start_date_end );
1196
1211
1197
- if ($ keyword_range == false && $ keyword_start_date_start != '' ) {
1212
+ if (! $ keyword_range && $ keyword_start_date_start != = '' ) {
1198
1213
$ sql .= " AND ticket.start_date >= ' $ keyword_start_date_start' " ;
1199
1214
}
1200
- if ($ keyword_range && $ keyword_start_date_start != '' && $ keyword_start_date_end != '' ) {
1201
- $ sql .= " AND ticket.start_date >= ' $ keyword_start_date_start'
1202
- AND ticket.start_date <= ' $ keyword_start_date_end' " ;
1215
+ if ($ keyword_range ) {
1216
+ $ sql .= " AND ticket.start_date >= ' $ keyword_start_date_start' AND ticket.start_date <= ' $ keyword_start_date_end' " ;
1203
1217
}
1204
- if ($ keyword_course != '' ) {
1218
+ if ($ keyword_course !== '' ) {
1205
1219
$ course_table = Database::get_main_table (TABLE_MAIN_COURSE );
1206
1220
$ sql .= " AND ticket.course_id IN (
1207
- SELECT id
1208
- FROM $ course_table
1209
- WHERE (
1210
- title LIKE '% $ keyword_course%' OR
1211
- code LIKE '% $ keyword_course%' OR
1212
- visual_code LIKE '% $ keyword_course%'
1213
- )
1214
- ) " ;
1221
+ SELECT id FROM $ course_table
1222
+ WHERE (
1223
+ title LIKE '% $ keyword_course%' OR
1224
+ code LIKE '% $ keyword_course%' OR
1225
+ visual_code LIKE '% $ keyword_course%'
1226
+ )
1227
+ ) " ;
1215
1228
}
1216
1229
1217
1230
$ res = Database::query ($ sql );
1218
1231
$ obj = Database::fetch_object ($ res );
1219
-
1220
- return (int ) $ obj ->total ;
1232
+ return $ obj ? (int ) $ obj ->total : 0 ;
1221
1233
}
1222
1234
1223
1235
/**
@@ -2018,6 +2030,27 @@ public static function getStatusIdFromCode($code)
2018
2030
return 0 ;
2019
2031
}
2020
2032
2033
+ /**
2034
+ * Returns the numeric priority ID from its code (e.g. 'NRM', 'HGH').
2035
+ *
2036
+ * @param string $code
2037
+ *
2038
+ * @return int
2039
+ */
2040
+ public static function getPriorityIdFromCode ($ code )
2041
+ {
2042
+ $ item = Database::getManager ()
2043
+ ->getRepository ('ChamiloTicketBundle:Priority ' )
2044
+ ->findOneBy (['code ' => $ code ])
2045
+ ;
2046
+
2047
+ if ($ item ) {
2048
+ return $ item ->getId ();
2049
+ }
2050
+
2051
+ return 0 ;
2052
+ }
2053
+
2021
2054
/**
2022
2055
* @return array
2023
2056
*/
@@ -2642,4 +2675,4 @@ public static function notifiyTicketUpdated(int $ticketId, int $categoryId, stri
2642
2675
}
2643
2676
}
2644
2677
}
2645
- }
2678
+ }
0 commit comments