admin.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. $cidReset = true;
  4. require_once __DIR__.'/../../main/inc/global.inc.php';
  5. $plugin = WhispeakAuthPlugin::create();
  6. api_protect_admin_script(true);
  7. $plugin->protectTool();
  8. /**
  9. * Return the wsids for the users in $usersIds.
  10. *
  11. * @param array $userIds
  12. *
  13. * @return array
  14. */
  15. function findWsIds(array $userIds)
  16. {
  17. $wsIds = [];
  18. foreach ($userIds as $userId) {
  19. $extraFieldValue = WhispeakAuthPlugin::getAuthUidValue($userId);
  20. if (!$extraFieldValue) {
  21. continue;
  22. }
  23. $wsId = $extraFieldValue->getValue();
  24. if (empty($wsId)) {
  25. continue;
  26. }
  27. $wsIds[] = $wsId;
  28. }
  29. return $wsIds;
  30. }
  31. /**
  32. * Group Whispeak results by external_user_id.
  33. *
  34. * @param array $results
  35. *
  36. * @return array
  37. */
  38. function groupResults(array $results)
  39. {
  40. $groups = [];
  41. foreach ($results as $row) {
  42. $index = array_shift($row);
  43. $groups[$index][] = array_values($row);
  44. }
  45. return $groups;
  46. }
  47. $form = new FormValidator('frm_filter', 'GET');
  48. $slctUsers = $form->addSelectAjax(
  49. 'users',
  50. get_lang('Users'),
  51. [],
  52. [
  53. 'url' => api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=get_user_like',
  54. 'id' => 'user_id',
  55. 'multiple' => true,
  56. ]
  57. );
  58. $form->addDatePicker('date', get_lang('Date'));
  59. $form->addButtonSearch(get_lang('Search'));
  60. $results = [];
  61. if ($form->validate()) {
  62. $formValues = $form->exportValues();
  63. $userIds = $formValues['users'] ?: [];
  64. $date = api_get_utc_datetime($formValues['date'], true, true);
  65. $wsIds = findWsIds($formValues['users'] ?: []);
  66. try {
  67. $results = WhispeakAuthRequest::getUsersInfos($plugin, $wsIds, [], $date);
  68. } catch (Exception $exception) {
  69. api_not_allowed(
  70. true,
  71. Display::return_message($exception->getMessage(), 'error')
  72. );
  73. }
  74. }
  75. $results = groupResults($results);
  76. $pageContent = '';
  77. foreach ($results as $wsId => $activities) {
  78. $extraFieldValue = new ExtraFieldValue('user');
  79. $value = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
  80. WhispeakAuthPlugin::EXTRAFIELD_AUTH_UID,
  81. $wsId
  82. );
  83. if (empty($value)) {
  84. continue;
  85. }
  86. $user = api_get_user_entity($value['item_id']);
  87. $slctUsers->addOption($user->getCompleteNameWithUsername(), $user->getId());
  88. $table = new SortableTableFromArray($activities, 3);
  89. $table->setTotalNumberOfItems(count($activities));
  90. $table->set_header(
  91. 0,
  92. $plugin->get_lang('ActivityId'),
  93. false,
  94. ['class' => 'text-center'],
  95. ['class' => 'text-center']
  96. );
  97. $table->set_header(
  98. 1,
  99. $plugin->get_lang('Quality'),
  100. false,
  101. ['class' => 'text-center'],
  102. ['class' => 'text-center']
  103. );
  104. $table->set_header(
  105. 2,
  106. get_lang('Result'),
  107. false,
  108. ['class' => 'text-center'],
  109. ['class' => 'text-center']
  110. );
  111. $table->set_header(
  112. 3,
  113. get_lang('DateTime'),
  114. true,
  115. ['class' => 'text-center'],
  116. ['class' => 'text-center']
  117. );
  118. $table->set_column_filter(
  119. 0,
  120. function ($id) {
  121. return "<code>$id</code>";
  122. }
  123. );
  124. $table->set_column_filter(
  125. 1,
  126. function ($quality) use ($plugin) {
  127. if (empty($quality)) {
  128. return '';
  129. }
  130. $quality = ucfirst($quality);
  131. return $plugin->get_lang("AudioQuality$quality");
  132. }
  133. );
  134. $table->set_column_filter(
  135. 2,
  136. function ($result) use ($plugin) {
  137. return $result
  138. ? Display::span($plugin->get_lang('Success'), ['class' => 'text-success'])
  139. : Display::span($plugin->get_lang('Failed'), ['class' => 'text-danger']);
  140. }
  141. );
  142. $table->set_column_filter(
  143. 3,
  144. function ($date) {
  145. return api_convert_and_format_date($date, DATE_TIME_FORMAT_LONG_24H);
  146. }
  147. );
  148. $pageContent .= Display::page_header(
  149. $user->getCompleteNameWithUsername(),
  150. $wsId
  151. );
  152. $pageContent .= $table->return_table();
  153. }
  154. $template = new Template($plugin->get_title());
  155. $template->assign(
  156. 'content',
  157. $form->returnForm().PHP_EOL.$pageContent
  158. );
  159. $template->display_one_col_template();