zombie_report.class.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. /**
  12. * @return ZombieReport
  13. */
  14. static function create($additional_parameters = array())
  15. {
  16. return new self($additional_parameters);
  17. }
  18. protected $additional_parameters = array();
  19. function __construct($additional_parameters = array())
  20. {
  21. $this->additional_parameters = $additional_parameters;
  22. }
  23. function get_additional_parameters()
  24. {
  25. return $this->additional_parameters;
  26. }
  27. function get_parameters()
  28. {
  29. $result = array(
  30. 'name' => 'zombie_report_parameters',
  31. 'method' => 'GET',
  32. 'attributes' => array('class' => 'well form-horizontal form-search'),
  33. 'items' => array(
  34. array(
  35. 'name' => 'ceiling',
  36. 'label' => get_lang('LastAccess'),
  37. 'type' => 'date_picker',
  38. 'default' => $this->get_ceiling('Y-m-d'),
  39. 'rules' => array(
  40. // array(
  41. // 'type' => 'required',
  42. // 'message' => get_lang('Required')
  43. // ),
  44. array(
  45. 'type' => 'date',
  46. 'message' => get_lang('Date')
  47. )
  48. )
  49. ),
  50. array(
  51. 'name' => 'active_only',
  52. 'label' => get_lang('ActiveOnly'),
  53. 'type' => 'checkbox',
  54. 'default' => $this->get_active_only()
  55. ),
  56. array(
  57. 'name' => 'submit_button',
  58. 'type' => 'button',
  59. 'value' => get_lang('Search'),
  60. 'attributes' => array('class' => 'search')
  61. )
  62. )
  63. );
  64. $additional_parameters = $this->get_additional_parameters();
  65. foreach ($additional_parameters as $key => $value) {
  66. $result['items'][] = array(
  67. 'type' => 'hidden',
  68. 'name' => $key,
  69. 'value' => $value
  70. );
  71. }
  72. return $result;
  73. }
  74. protected $parameters_form = null;
  75. /**
  76. *
  77. * @return FormValidator
  78. */
  79. function get_parameters_form()
  80. {
  81. $parameters = $this->get_parameters();
  82. if (empty($parameters)) {
  83. return null;
  84. }
  85. if (empty($this->parameters_form)) {
  86. $this->parameters_form = FormValidator::create($parameters);
  87. }
  88. return $this->parameters_form;
  89. }
  90. function display_parameters($return = false)
  91. {
  92. $form = $this->get_parameters_form();
  93. if (empty($form)) {
  94. return '';
  95. }
  96. $result = $form->return_form();
  97. if ($return) {
  98. return $result;
  99. } else {
  100. echo $result;
  101. }
  102. }
  103. function is_valid()
  104. {
  105. $form = $this->get_parameters_form();
  106. if (empty($form)) {
  107. return true;
  108. }
  109. return $form->isSubmitted() == false || $form->validate();
  110. }
  111. function get_ceiling($format = null)
  112. {
  113. $result = Request::get('ceiling');
  114. $result = $result ? $result : ZombieManager::last_year();
  115. $result = is_array($result) && count($result) == 1 ? reset($result) : $result;
  116. $result = is_array($result) ? mktime(0, 0, 0, $result['F'], $result['d'], $result['Y']) : $result;
  117. $result = is_numeric($result) ? (int) $result : $result;
  118. $result = is_string($result) ? strtotime($result) : $result;
  119. if ($format) {
  120. $result = date($format, $result);
  121. }
  122. return $result;
  123. }
  124. function get_active_only()
  125. {
  126. $result = Request::get('active_only', false);
  127. $result = $result === 'true' ? true : $result;
  128. $result = $result === 'false' ? false : $result;
  129. $result = (bool) $result;
  130. return $result;
  131. }
  132. function get_action()
  133. {
  134. /**
  135. * todo check token
  136. */
  137. $check = Security::check_token('post');
  138. Security::clear_token();
  139. if (!$check) {
  140. return 'display';
  141. }
  142. return Request::post('action', 'display');
  143. }
  144. function perform_action()
  145. {
  146. $ids = Request::post('id');
  147. if (empty($ids)) {
  148. return $ids;
  149. }
  150. $action = $this->get_action();
  151. $f = array($this, 'action_' . $action);
  152. if (is_callable($f)) {
  153. return call_user_func($f, $ids);
  154. }
  155. return false;
  156. }
  157. function action_deactivate($ids)
  158. {
  159. return UserManager::deactivate_users($ids);
  160. }
  161. function action_activate($ids)
  162. {
  163. return UserManager::activate_users($ids);
  164. }
  165. function action_delete($ids)
  166. {
  167. return UserManager::delete_users($ids);
  168. }
  169. function count()
  170. {
  171. if (!$this->is_valid()) {
  172. return 0;
  173. }
  174. $ceiling = $this->get_ceiling();
  175. $active_only = $this->get_active_only();
  176. $items = ZombieManager::listZombies($ceiling, $active_only);
  177. return count($items);
  178. }
  179. function get_data($from, $count, $column, $direction)
  180. {
  181. if (!$this->is_valid()) {
  182. return array();
  183. }
  184. $ceiling = $this->get_ceiling();
  185. $active_only = $this->get_active_only();
  186. $items = ZombieManager::listZombies($ceiling, $active_only, $count, $from, $column, $direction);
  187. $result = array();
  188. foreach ($items as $item) {
  189. $row = array();
  190. $row[] = $item['user_id'];
  191. $row[] = $item['code'];
  192. $row[] = $item['firstname'];
  193. $row[] = $item['lastname'];
  194. $row[] = $item['username'];
  195. $row[] = $item['email'];
  196. $row[] = $item['status'];
  197. $row[] = $item['auth_source'];
  198. $row[] = api_format_date($item['registration_date'], DATE_FORMAT_SHORT);
  199. $row[] = api_format_date($item['login_date'], DATE_FORMAT_SHORT);
  200. $row[] = $item['active'];
  201. $result[] = $row;
  202. }
  203. return $result;
  204. }
  205. function display_data($return = false)
  206. {
  207. $count = array($this, 'count');
  208. $data = array($this, 'get_data');
  209. $parameters = array();
  210. $parameters['sec_token'] = Security::get_token();
  211. $parameters['ceiling'] = $this->get_ceiling();
  212. $parameters['active_only'] = $this->get_active_only() ? 'true' : 'false';
  213. $additional_parameters = $this->get_additional_parameters();
  214. $parameters = array_merge($additional_parameters, $parameters);
  215. $table = new SortableTable('users', $count, $data, 1, 50);
  216. $table->set_additional_parameters($parameters);
  217. $col = 0;
  218. $table->set_header($col++, '', false);
  219. $table->set_header($col++, get_lang('Code'));
  220. $table->set_header($col++, get_lang('FirstName'));
  221. $table->set_header($col++, get_lang('LastName'));
  222. $table->set_header($col++, get_lang('LoginName'));
  223. $table->set_header($col++, get_lang('Email'));
  224. $table->set_header($col++, get_lang('Profile'));
  225. $table->set_header($col++, get_lang('AuthenticationSource'));
  226. $table->set_header($col++, get_lang('RegisteredDate'));
  227. $table->set_header($col++, get_lang('LastAccess'), false);
  228. $table->set_header($col++, get_lang('Active'), false);
  229. $table->set_column_filter(5, array($this, 'format_email'));
  230. $table->set_column_filter(6, array($this, 'format_status'));
  231. $table->set_column_filter(10, array($this, 'format_active'));
  232. $table->set_form_actions(array(
  233. 'activate' => get_lang('Activate'),
  234. 'deactivate' => get_lang('Deactivate'),
  235. 'delete' => get_lang('Delete')
  236. ));
  237. if ($return) {
  238. return $table->return_table();
  239. } else {
  240. echo $table->return_table();
  241. }
  242. }
  243. /**
  244. * Table formatter for the active column.
  245. *
  246. * @param string $active
  247. * @return string
  248. */
  249. function format_active($active)
  250. {
  251. $active = ($active == '1');
  252. if ($active) {
  253. $image = 'accept';
  254. $text = get_lang('Yes');
  255. } else {
  256. $image = 'error';
  257. $text = get_lang('No');
  258. }
  259. $result = Display::return_icon($image . '.png', $text);
  260. return $result;
  261. }
  262. function format_status($status)
  263. {
  264. $statusname = api_get_status_langvars();
  265. return $statusname[$status];
  266. }
  267. function format_email($email)
  268. {
  269. return Display :: encrypted_mailto_link($email, $email);
  270. }
  271. function display($return = false)
  272. {
  273. $result = $this->display_parameters($return);
  274. if ($this->perform_action()) {
  275. if ($return) {
  276. $result .= Display::return_confirmation_message(get_lang('Done'));
  277. } else {
  278. Display::display_confirmation_message(get_lang('Done'));
  279. }
  280. }
  281. $result .= $this->display_data($return);
  282. if ($return) {
  283. return $result;
  284. }
  285. }
  286. }