diff --git a/index.php b/index.php index b5f5b50..314832f 100644 --- a/index.php +++ b/index.php @@ -57,9 +57,9 @@ $messagestable = new html_table(); $messagestable->attributes['class'] = 'generaltable boxaligncenter'; $messagestable->cellpadding = 5; $messagestable->id = 'messagestable'; -$messagestable->head = array(get_string('fullname'), get_string('totalsended', 'report_messages'), +$messagestable->head = array(get_string('fullname'), get_string('role', 'report_messages'), get_string('totalsended', 'report_messages'), get_string('totalreceived','report_messages'), $strviewdetail); -$messagestable->align = array('left', 'right', 'right', 'center'); +$messagestable->align = array('left', 'right', 'right', 'right', 'center'); $messagestable->data = array(); $userlist = get_enrolled_users($context, '', 0, 'u.id'); @@ -69,19 +69,27 @@ $fullname = $DB->sql_fullname('u.firstname', 'u.lastname'); $sql = " select u.id, $fullname as fullname, u.picture, u.firstname, u.lastname, u.imagealt, u.email, (select count(*) from {message} where useridfrom=u.id and useridto<>u.id and useridto $in_user) as totalsend, - (select count(*) from {message} where useridto=u.id and useridfrom<>u.id and useridfrom $in_user) as totalreceive + (select count(*) from {message_read} where useridfrom=u.id and useridto<>u.id and useridto $in_user) as totalsendread, + (select count(*) from {message} where useridto=u.id and useridfrom<>u.id and useridfrom $in_user) as totalreceive, + (select count(*) from {message_read} where useridto=u.id and useridfrom<>u.id and useridfrom $in_user) as totalreceiveread from {user} u where u.id $in_user order by fullname "; -$data = $DB->get_records_sql($sql, array_merge($param_users,$param_users,$param_users)); +$data = $DB->get_records_sql($sql, array_merge($param_users,$param_users,$param_users,$param_users,$param_users)); foreach ($data as $user) { + $roles = role_fix_names(get_user_roles($context, $user->id)); + $str_roles = ""; + foreach ($roles as $role) { + $str_roles .= $role->localname.", "; + } + $str_roles = substr($str_roles, 0, -2); $upic = $OUTPUT->user_picture($user); $ulink = "wwwroot}/user/view.php?id={$user->id}&course={$course->id}\">{$upic}"; $link = "wwwroot/report/messages/user.php?id=$user->id&course=$course->id\">$strviewdetail"; - $messagestable->data[] = array($ulink.' '.$user->fullname, $user->totalsend, $user->totalreceive, $link); + $messagestable->data[] = array($ulink.' '.$user->fullname, $str_roles, $user->totalsend+$user->totalsendread, $user->totalreceive+$user->totalreceiveread, $link); } echo html_writer::table($messagestable); diff --git a/lang/en/report_messages.php b/lang/en/report_messages.php index 76c436c..b42dc76 100644 --- a/lang/en/report_messages.php +++ b/lang/en/report_messages.php @@ -32,5 +32,6 @@ $string['reporttitle'] = 'Message report to user {$a->fullname}'; $string['exchangedmessages'] = '{$a->userlink} {$a->total_messages} messages exchanged with {$a->fullname}'; $string['message'] = 'Message body'; $string['viewdetail'] = 'View details'; +$string['role'] = 'Role'; $string['totalsended'] = 'Total sended messages'; $string['totalreceived'] = 'Total received messages'; diff --git a/lang/pt_br/report_messages.php b/lang/pt_br/report_messages.php index a0c4dea..c5645a0 100644 --- a/lang/pt_br/report_messages.php +++ b/lang/pt_br/report_messages.php @@ -32,5 +32,6 @@ $string['reporttitle'] = 'Relatório de mensagens do usuário {$a->fullname}'; $string['exchangedmessages'] = '{$a->userlink} {$a->total_messages} mensagens trocadas com {$a->fullname}'; $string['message'] = 'Texto da mensagem'; $string['viewdetail'] = 'Ver detalhes'; +$string['role'] = 'Papel'; $string['totalsended'] = 'Total de mensagens enviadas'; $string['totalreceived'] = 'Total de mensagens recebidas'; diff --git a/user.php b/user.php index 5bc34b7..1e05243 100644 --- a/user.php +++ b/user.php @@ -59,31 +59,40 @@ $PAGE->set_heading($course->fullname); echo $OUTPUT->header(); echo '

'.get_string('reporttitle','report_messages',array('fullname'=>fullname($user))).'

'; -$from = $DB->get_records_sql('select distinct m.useridfrom as id from {message} m where m.useridto = ? and m.useridfrom <> ?', array($user->id, $user->id)); -$to = $DB->get_records_sql('select distinct m.useridto as id from {message} m where m.useridfrom = ? and m.useridto <> ?', array($user->id, $user->id)); +$sql = " + (SELECT DISTINCT m.useridfrom as id + FROM {message} m + WHERE m.useridto = ? AND m.useridfrom <> ?) + + UNION + + (SELECT DISTINCT mr.useridfrom as id + FROM {message_read} mr + WHERE mr.useridto = ? AND mr.useridfrom <> ?) + + UNION + + (SELECT distinct m.useridto as id + FROM {message} m + WHERE m.useridfrom = ? AND m.useridto <> ?) + + UNION + + (SELECT distinct mr.useridto as id + FROM {message_read} mr + WHERE mr.useridfrom = ? AND mr.useridto <> ?) +"; +$userlist = $DB->get_records_sql($sql, array($user->id, $user->id, $user->id, $user->id,$user->id, $user->id, $user->id, $user->id)); +list($in_user, $param_users) = $DB->get_in_or_equal(array_keys($userlist), SQL_PARAMS_QM); $contacts = array(); $sqlfullname = $DB->sql_fullname('u.firstname','u.lastname'); $sql = " - select u.id, $sqlfullname as fullname, count(*) as total_messages, u.picture, u.firstname, u.lastname, u.imagealt, u.email - from {user} u, {message} m - where u.id = ? and - ((m.useridfrom = ? and m.useridto = ?) or - (m.useridfrom = ? and m.useridto = ?)) - group by u.id, u.username, u.firstname, u.lastname, u.email + select u.*, $sqlfullname as fullname + from {user} u + where u.id {$in_user} "; -foreach ($from as $u) { - if (is_enrolled($coursecontext, $u)) { - $contacts[$u->id] = $DB->get_record_sql($sql, array($u->id, $u->id, $user->id, $user->id, $u->id)); - } -} - - -foreach ($to as $u) { - if (is_enrolled($coursecontext, $u)) { - $contacts[$u->id] = $DB->get_record_sql($sql, array($u->id, $u->id, $user->id, $user->id, $u->id)); - } -} +$contacts = $DB->get_records_sql($sql, $param_users); if (empty($contacts)) { echo $OUTPUT->notification(get_string('nothingtodisplay')); @@ -91,27 +100,36 @@ if (empty($contacts)) { $sqlfromname = $DB->sql_fullname('fu.firstname','fu.lastname'); $sqltoname = $DB->sql_fullname('tu.firstname','tu.lastname'); $sql = " - select m.timecreated, $sqlfromname as fromuser, $sqltoname as touser, m.fullmessage, m.fullmessageformat + (select m.timecreated, $sqlfromname as fromuser, $sqltoname as touser, m.fullmessage, m.fullmessageformat from {message} m inner join {user} fu on fu.id = m.useridfrom inner join {user} tu on tu.id = m.useridto where (m.useridfrom = ? and m.useridto = ?) or (m.useridfrom = ? and m.useridto = ?) - order by m.timecreated desc + order by m.timecreated desc) + + UNION + + (select mr.timecreated, $sqlfromname as fromuser, $sqltoname as touser, mr.fullmessage, mr.fullmessageformat + from {message_read} mr + inner join {user} fu on fu.id = mr.useridfrom + inner join {user} tu on tu.id = mr.useridto + where (mr.useridfrom = ? and mr.useridto = ?) or + (mr.useridfrom = ? and mr.useridto = ?) + order by mr.timecreated desc) "; foreach ($contacts as $contact) { + $messages = $DB->get_records_sql($sql, array($user->id, $contact->id, $contact->id, $user->id, $user->id, $contact->id, $contact->id, $user->id)); echo $OUTPUT->box_start(); $upic = $OUTPUT->user_picture($contact); $ulink = "wwwroot}/user/view.php?id={$contact->id}&course={$course->id}\">{$upic}"; - $a = array('userlink'=>$ulink, 'total_messages'=>$contact->total_messages, 'fullname'=>$contact->fullname); + $a = array('userlink'=>$ulink, 'total_messages'=>count($messages), 'fullname'=>$contact->fullname); $head = get_string('exchangedmessages', 'report_messages', $a); echo "

$head

"; - $table = new html_table(); - $tabl->attributes['class'] = 'generaltable boxaligncenter'; + $table->attributes['class'] = 'generaltable boxaligncenter'; $table->head = array(get_string('date'), get_string('from'), get_string('to'), get_string('message', 'report_messages')); - $messages = $DB->get_records_sql($sql, array($user->id, $contact->id, $contact->id, $user->id)); $table->data = array(); foreach ($messages as $message) { $table->data[] = array(userdate($message->timecreated), $message->fromuser, $message->touser, format_text($message->fullmessage, $message->fullmessageformat));