zombie_report.class.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /**
  3. * Description of zombie_report.
  4. *
  5. * @copyright (c) 2012 University of Geneva
  6. * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
  7. * @author Laurent Opprecht <laurent@opprecht.info>
  8. */
  9. class ZombieReport implements Countable
  10. {
  11. protected $additional_parameters = [];
  12. protected $parameters_form = null;
  13. public function __construct($additional_parameters = [])
  14. {
  15. $this->additional_parameters = $additional_parameters;
  16. }
  17. /**
  18. * @return ZombieReport
  19. */
  20. public static function create($additional_parameters = [])
  21. {
  22. return new self($additional_parameters);
  23. }
  24. public function get_additional_parameters()
  25. {
  26. return $this->additional_parameters;
  27. }
  28. public function get_parameters()
  29. {
  30. $result = [
  31. 'items' => [
  32. [
  33. 'name' => 'ceiling',
  34. 'label' => get_lang('Latest access'),
  35. 'type' => 'date_picker',
  36. 'default' => $this->get_ceiling('Y-m-d'),
  37. 'rules' => [
  38. [
  39. 'type' => 'date',
  40. 'message' => get_lang('Date'),
  41. ],
  42. ],
  43. ],
  44. [
  45. 'name' => 'active_only',
  46. 'label' => get_lang('active only'),
  47. 'type' => 'checkbox',
  48. 'default' => $this->get_active_only(),
  49. ],
  50. [
  51. 'name' => 'submit_button',
  52. 'type' => 'button',
  53. 'value' => get_lang('Search'),
  54. 'attributes' => ['class' => 'search'],
  55. ],
  56. ],
  57. ];
  58. return $result;
  59. }
  60. /**
  61. * @return FormValidator
  62. */
  63. public function get_parameters_form()
  64. {
  65. $form = new FormValidator(
  66. 'zombie_report_parameters',
  67. 'get',
  68. null,
  69. null,
  70. ['class' => 'well form-horizontal form-search']
  71. );
  72. $form->addDatePicker('ceiling', get_lang('Latest access'));
  73. $form->addCheckBox('active_only', get_lang('active only'));
  74. $form->addButtonSearch(get_lang('Search'));
  75. $params = [
  76. 'active_only' => $this->get_active_only(),
  77. 'ceiling' => $this->get_ceiling('Y-m-d'),
  78. ];
  79. $form->setDefaults($params);
  80. $additional = $this->get_additional_parameters();
  81. foreach ($additional as $key => $value) {
  82. $value = Security::remove_XSS($value);
  83. $form->addHidden($key, $value);
  84. }
  85. return $form;
  86. }
  87. public function display_parameters($return = false)
  88. {
  89. $form = $this->get_parameters_form();
  90. $result = $form->returnForm();
  91. if ($return) {
  92. return $result;
  93. } else {
  94. echo $result;
  95. }
  96. }
  97. public function is_valid()
  98. {
  99. $form = $this->get_parameters_form();
  100. return $form->isSubmitted() == false || $form->validate();
  101. }
  102. public function get_ceiling($format = null)
  103. {
  104. $result = Request::get('ceiling');
  105. $result = $result ? $result : ZombieManager::last_year();
  106. $result = is_array($result) && count($result) == 1 ? reset($result) : $result;
  107. $result = is_array($result) ? mktime(0, 0, 0, $result['F'], $result['d'], $result['Y']) : $result;
  108. $result = is_numeric($result) ? (int) $result : $result;
  109. $result = is_string($result) ? strtotime($result) : $result;
  110. if ($format) {
  111. $result = date($format, $result);
  112. }
  113. return $result;
  114. }
  115. public function get_active_only()
  116. {
  117. $result = Request::get('active_only', false);
  118. $result = $result === 'true' ? true : $result;
  119. $result = $result === 'false' ? false : $result;
  120. $result = (bool) $result;
  121. return $result;
  122. }
  123. public function get_action()
  124. {
  125. /**
  126. * todo check token.
  127. */
  128. $check = Security::check_token('post');
  129. Security::clear_token();
  130. if (!$check) {
  131. return 'display';
  132. }
  133. return Request::post('action', 'display');
  134. }
  135. public function perform_action()
  136. {
  137. $ids = Request::post('id');
  138. if (empty($ids)) {
  139. return $ids;
  140. }
  141. $action = $this->get_action();
  142. switch ($action) {
  143. case 'activate':
  144. return UserManager::activate_users($ids);
  145. break;
  146. case 'deactivate':
  147. return UserManager::deactivate_users($ids);
  148. break;
  149. case 'delete':
  150. return UserManager::delete_users($ids);
  151. }
  152. return false;
  153. }
  154. public function count()
  155. {
  156. $ceiling = $this->get_ceiling();
  157. $active_only = $this->get_active_only();
  158. $items = ZombieManager::listZombies($ceiling, $active_only, null, null);
  159. return count($items);
  160. }
  161. public function get_data($from, $count, $column, $direction)
  162. {
  163. $ceiling = $this->get_ceiling();
  164. $active_only = $this->get_active_only();
  165. $items = ZombieManager::listZombies($ceiling, $active_only, $from, $count, $column, $direction);
  166. $result = [];
  167. foreach ($items as $item) {
  168. $row = [];
  169. $row[] = $item['user_id'];
  170. $row[] = $item['official_code'];
  171. $row[] = $item['firstname'];
  172. $row[] = $item['lastname'];
  173. $row[] = $item['username'];
  174. $row[] = $item['email'];
  175. $row[] = $item['status'];
  176. $row[] = $item['auth_source'];
  177. $row[] = api_format_date($item['registration_date'], DATE_FORMAT_SHORT);
  178. $row[] = api_format_date($item['login_date'], DATE_FORMAT_SHORT);
  179. $row[] = $item['active'];
  180. $result[] = $row;
  181. }
  182. return $result;
  183. }
  184. public function display_data($return = false)
  185. {
  186. $count = [$this, 'count'];
  187. $data = [$this, 'get_data'];
  188. $parameters = [];
  189. $parameters['sec_token'] = Security::get_token();
  190. $parameters['ceiling'] = $this->get_ceiling();
  191. $parameters['active_only'] = $this->get_active_only() ? 'true' : 'false';
  192. $additional_parameters = $this->get_additional_parameters();
  193. $parameters = array_merge($additional_parameters, $parameters);
  194. $table = new SortableTable('zombie_users', $count, $data, 1, 50);
  195. $table->set_additional_parameters($parameters);
  196. $col = 0;
  197. $table->set_header($col++, '', false);
  198. $table->set_header($col++, get_lang('Code'));
  199. $table->set_header($col++, get_lang('First name'));
  200. $table->set_header($col++, get_lang('Last name'));
  201. $table->set_header($col++, get_lang('Login'));
  202. $table->set_header($col++, get_lang('e-mail'));
  203. $table->set_header($col++, get_lang('Profile'));
  204. $table->set_header($col++, get_lang('Authentication source'));
  205. $table->set_header($col++, get_lang('Registered date'));
  206. $table->set_header($col++, get_lang('Latest access'), false);
  207. $table->set_header($col, get_lang('active'), false);
  208. $table->set_column_filter(5, [$this, 'format_email']);
  209. $table->set_column_filter(6, [$this, 'format_status']);
  210. $table->set_column_filter(10, [$this, 'format_active']);
  211. $table->set_form_actions([
  212. 'activate' => get_lang('Activate'),
  213. 'deactivate' => get_lang('Deactivate'),
  214. 'delete' => get_lang('Delete'),
  215. ]);
  216. if ($return) {
  217. return $table->return_table();
  218. } else {
  219. echo $table->return_table();
  220. }
  221. }
  222. /**
  223. * Table formatter for the active column.
  224. *
  225. * @param string $active
  226. *
  227. * @return string
  228. */
  229. public function format_active($active)
  230. {
  231. $active = $active == '1';
  232. if ($active) {
  233. $image = 'accept';
  234. $text = get_lang('Yes');
  235. } else {
  236. $image = 'error';
  237. $text = get_lang('No');
  238. }
  239. $result = Display::return_icon($image.'.png', $text);
  240. return $result;
  241. }
  242. public function format_status($status)
  243. {
  244. $statusname = api_get_status_langvars();
  245. return $statusname[$status];
  246. }
  247. public function format_email($email)
  248. {
  249. return Display::encrypted_mailto_link($email, $email);
  250. }
  251. public function display($return = false)
  252. {
  253. $result = $this->display_parameters($return);
  254. $valid = $this->perform_action();
  255. if ($valid) {
  256. echo Display::return_message(get_lang('Update successful'), 'confirmation');
  257. }
  258. $result .= $this->display_data($return);
  259. if ($return) {
  260. return $result;
  261. }
  262. }
  263. }