Added feature for read comments in grouped events

This commit is contained in:
Jose Gonzalez 2021-10-13 14:58:31 +02:00
parent a8937abb65
commit 3f28a98744
2 changed files with 78 additions and 34 deletions

View File

@ -83,10 +83,10 @@ $in_process_event = get_parameter('in_process_event', 0);
$validate_event = get_parameter('validate_event', 0); $validate_event = get_parameter('validate_event', 0);
$delete_event = get_parameter('delete_event', 0); $delete_event = get_parameter('delete_event', 0);
$get_event_filters = get_parameter('get_event_filters', 0); $get_event_filters = get_parameter('get_event_filters', 0);
$get_comments = get_parameter('get_comments', 0); $get_comments = (bool) get_parameter('get_comments', false);
$get_events_fired = (bool) get_parameter('get_events_fired'); $get_events_fired = (bool) get_parameter('get_events_fired');
$get_id_source_event = get_parameter('get_id_source_event'); $get_id_source_event = get_parameter('get_id_source_event');
if ($get_comments) { if ($get_comments === true) {
$event = get_parameter('event', false); $event = get_parameter('event', false);
$filter = get_parameter('filter', false); $filter = get_parameter('filter', false);
@ -94,6 +94,8 @@ if ($get_comments) {
return __('Failed to retrieve comments'); return __('Failed to retrieve comments');
} }
$eventsGrouped = [];
if ($filter['group_rep'] == 1) { if ($filter['group_rep'] == 1) {
$events = events_get_all( $events = events_get_all(
['te.*'], ['te.*'],
@ -119,23 +121,39 @@ if ($get_comments) {
// True for show comments of validated events. // True for show comments of validated events.
true true
); );
if ($events !== false) { if ($events !== false) {
$event = $events[0]; $event = $events[0];
} }
} else { } else {
$events = events_get_event( // Consider if the event is grouped.
$event['id_evento'], if (isset($event['event_rep']) === true && $event['event_rep'] > 0) {
false, $eventsGrouped = db_get_all_rows_sql(
$meta, sprintf(
$history 'SELECT `user_comment`
); FROM `tevento`
WHERE `id_agente` = "%d" AND `data` = "%d" AND `estado` = "%d"',
$event['id_agente'],
$event['data'],
$event['estado']
)
);
} else {
$events = events_get_event(
$event['id_evento'],
false,
$meta,
$history
);
if ($events !== false) { if ($events !== false) {
$event = $events; $event = $events;
}
} }
} }
echo events_page_comments($event, true); // End of get_comments.
echo events_page_comments($event, true, $eventsGrouped);
return; return;
} }

View File

@ -1619,14 +1619,14 @@ function events_get_events($filter=false, $fields=false)
*/ */
function events_get_event($id, $fields=false, $meta=false, $history=false) function events_get_event($id, $fields=false, $meta=false, $history=false)
{ {
if (empty($id)) { if (empty($id) === true) {
return false; return false;
} }
global $config; global $config;
if (is_array($fields)) { if (is_array($fields) === true) {
if (! in_array('id_grupo', $fields)) { if (in_array('id_grupo', $fields) === false) {
$fields[] = 'id_grupo'; $fields[] = 'id_grupo';
} }
} }
@ -1634,7 +1634,7 @@ function events_get_event($id, $fields=false, $meta=false, $history=false)
$table = events_get_events_table($meta, $history); $table = events_get_events_table($meta, $history);
$event = db_get_row($table, 'id_evento', $id, $fields); $event = db_get_row($table, 'id_evento', $id, $fields);
if (! check_acl($config['id_user'], $event['id_grupo'], 'ER')) { if ((bool) check_acl($config['id_user'], $event['id_grupo'], 'ER') === false) {
return false; return false;
} }
@ -2255,7 +2255,7 @@ function events_comment(
// If comments are not stored in json, the format is old. // If comments are not stored in json, the format is old.
$event_comments_array = json_decode($event_comments[0]['user_comment']); $event_comments_array = json_decode($event_comments[0]['user_comment']);
if (empty($event_comments_array)) { if (empty($event_comments_array) === true) {
$comments_format = 'old'; $comments_format = 'old';
} else { } else {
$comments_format = 'new'; $comments_format = 'new';
@ -2268,6 +2268,7 @@ function events_comment(
$comment_for_json['action'] = $action; $comment_for_json['action'] = $action;
$comment_for_json['id_user'] = $config['id_user']; $comment_for_json['id_user'] = $config['id_user'];
$comment_for_json['utimestamp'] = time(); $comment_for_json['utimestamp'] = time();
$comment_for_json['event_id'] = $first_event;
$event_comments_array[] = $comment_for_json; $event_comments_array[] = $comment_for_json;
@ -2282,11 +2283,11 @@ function events_comment(
break; break;
case 'old': case 'old':
// Give old ugly format to comment. TODO: Change this method for // Give old ugly format to comment.
// aux table or json. // Change this method for aux table or json.
$comment = str_replace(["\r\n", "\r", "\n"], '<br>', $comment); $comment = str_replace(["\r\n", "\r", "\n"], '<br>', $comment);
if ($comment != '') { if ($comment !== '') {
$commentbox = '<div class="comment_box">'.io_safe_input($comment).'</div>'; $commentbox = '<div class="comment_box">'.io_safe_input($comment).'</div>';
} else { } else {
$commentbox = ''; $commentbox = '';
@ -5082,11 +5083,13 @@ function events_page_general($event)
/** /**
* Generate 'comments' page for event viewer. * Generate 'comments' page for event viewer.
* *
* @param array $event Event. * @param array $event Event.
* @param boolean $ajax If the query come from AJAX.
* @param boolean $grouped If the event must shown comments grouped.
* *
* @return string HTML. * @return string HTML.
*/ */
function events_page_comments($event, $ajax=false) function events_page_comments($event, $ajax=false, $groupedComments=[])
{ {
// Comments. // Comments.
global $config; global $config;
@ -5097,23 +5100,36 @@ function events_page_comments($event, $ajax=false)
$table_comments->head = []; $table_comments->head = [];
$table_comments->class = 'table_modal_alternate'; $table_comments->class = 'table_modal_alternate';
$comments = ($event['user_comment'] ?? ''); $comments = (empty($groupedComments) === true) ? $event['user_comment'] : $groupedComments;
if (empty($comments)) { if (empty($comments) === true) {
$table_comments->style[0] = 'text-align:center;'; $table_comments->style[0] = 'text-align:center;';
$table_comments->colspan[0][0] = 2; $table_comments->colspan[0][0] = 2;
$data = []; $data = [];
$data[0] = __('There are no comments'); $data[0] = __('There are no comments');
$table_comments->data[] = $data; $table_comments->data[] = $data;
} else { } else {
if (is_array($comments)) { if (is_array($comments) === true) {
$comments_array = [];
foreach ($comments as $comm) { foreach ($comments as $comm) {
if (empty($comm)) { if (empty($comm) === true) {
continue; continue;
} }
// If exists user_comments, come from grouped events and must be handled like this.
if (isset($comm['user_comment']) === true) {
$comm = $comm['user_comment'];
}
$comments_array[] = io_safe_output(json_decode($comm, true)); $comments_array[] = io_safe_output(json_decode($comm, true));
} }
usort(
$comments_array,
function ($a, $b) {
return ($a[(count($a) - 1)]['utimestamp'] < $b[(count($b) - 1)]['utimestamp']);
}
);
} else { } else {
$comments = str_replace(["\n", '&#x0a;'], '<br>', $comments); $comments = str_replace(["\n", '&#x0a;'], '<br>', $comments);
// If comments are not stored in json, the format is old. // If comments are not stored in json, the format is old.
@ -5122,22 +5138,32 @@ function events_page_comments($event, $ajax=false)
foreach ($comments_array as $comm) { foreach ($comments_array as $comm) {
// Show the comments more recent first. // Show the comments more recent first.
if (is_array($comm)) { if (is_array($comm) === true) {
$comm = array_reverse($comm); $comm = array_reverse($comm);
} }
if (empty($comm)) { $comments_format = (empty($comm) === true ? 'old' : 'new');
$comments_format = 'old';
} else {
$comments_format = 'new';
}
switch ($comments_format) { switch ($comments_format) {
case 'new': case 'new':
foreach ($comm as $c) { foreach ($comm as $c) {
$data[0] = '<b>'.$c['action'].' by '.$c['id_user'].'</b>'; $eventIdExplanation = (empty($groupedComments) === false) ? sprintf(' (#%d)', $c['event_id']) : '';
$data[0] .= '<br><br><i>'.date($config['date_format'], $c['utimestamp']).'</i>';
$data[0] = sprintf(
'<b>%s %s %s%s</b>',
$c['action'],
__('by'),
$c['id_user'],
$eventIdExplanation
);
$data[0] .= sprintf(
'<br><br><i>%s</i>',
date($config['date_format'], $c['utimestamp'])
);
$data[1] = '<p class="break_word">'.stripslashes(str_replace(['\n', '\r'], '<br/>', $c['comment'])).'</p>'; $data[1] = '<p class="break_word">'.stripslashes(str_replace(['\n', '\r'], '<br/>', $c['comment'])).'</p>';
$table_comments->data[] = $data; $table_comments->data[] = $data;
} }
break; break;
@ -5227,7 +5253,7 @@ function events_page_comments($event, $ajax=false)
); );
} }
if ($ajax) { if ($ajax === true) {
return $comments_form.html_print_table($table_comments, true); return $comments_form.html_print_table($table_comments, true);
} }