zombie_report.class.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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' => 'datepickerdate',
  38. 'default' => $this->get_ceiling(),
  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' => 'style_submit_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()
  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. return $result;
  120. }
  121. function get_active_only()
  122. {
  123. $result = Request::get('active_only', false);
  124. $result = $result === 'true' ? true : $result;
  125. $result = $result === 'false' ? false : $result;
  126. $result = (bool) $result;
  127. return $result;
  128. }
  129. function get_action()
  130. {
  131. /**
  132. * todo check token
  133. */
  134. $check = Security::check_token('post');
  135. Security::clear_token();
  136. if (!$check) {
  137. return 'display';
  138. }
  139. return Request::post('action', 'display');
  140. }
  141. function perform_action()
  142. {
  143. $ids = Request::post('id');
  144. if (empty($ids)) {
  145. return $ids;
  146. }
  147. $action = $this->get_action();
  148. $f = array($this, 'action_' . $action);
  149. if (is_callable($f)) {
  150. return call_user_func($f, $ids);
  151. }
  152. return false;
  153. }
  154. function action_deactivate($ids)
  155. {
  156. return UserManager::deactivate_users($ids);
  157. }
  158. function action_activate($ids)
  159. {
  160. return UserManager::activate_users($ids);
  161. }
  162. function action_delete($ids)
  163. {
  164. return UserManager::delete_users($ids);
  165. }
  166. function count()
  167. {
  168. if (!$this->is_valid()) {
  169. return 0;
  170. }
  171. $ceiling = $this->get_ceiling();
  172. $active_only = $this->get_active_only();
  173. $items = ZombieManager::list_zombies($ceiling, $active_only);
  174. return count($items);
  175. }
  176. function get_data($from, $count, $column, $direction)
  177. {
  178. if (!$this->is_valid()) {
  179. return array();
  180. }
  181. $ceiling = $this->get_ceiling();
  182. $active_only = $this->get_active_only();
  183. $items = ZombieManager::list_zombies($ceiling, $active_only)->limit($count, $from)->orderby($column, $direction);
  184. $result = array();
  185. foreach ($items as $item) {
  186. $row = array();
  187. $row[] = $item['user_id'];
  188. $row[] = $item['code'];
  189. $row[] = $item['firstname'];
  190. $row[] = $item['lastname'];
  191. $row[] = $item['username'];
  192. $row[] = $item['email'];
  193. $row[] = $item['status'];
  194. $row[] = $item['auth_source'];
  195. $row[] = api_format_date($item['registration_date'], DATE_FORMAT_SHORT);
  196. $row[] = api_format_date($item['login_date'], DATE_FORMAT_SHORT);
  197. $row[] = $item['active'];
  198. $result[] = $row;
  199. }
  200. return $result;
  201. }
  202. function display_data($return = false)
  203. {
  204. $count = array($this, 'count');
  205. $data = array($this, 'get_data');
  206. $parameters = array();
  207. $parameters['sec_token'] = Security::get_token();
  208. $parameters['ceiling'] = $this->get_ceiling();
  209. $parameters['active_only'] = $this->get_active_only() ? 'true' : 'false';
  210. $additional_parameters = $this->get_additional_parameters();
  211. $parameters = array_merge($additional_parameters, $parameters);
  212. $table = new SortableTable('users', $count, $data, 1, 50);
  213. $table->set_additional_parameters($parameters);
  214. $col = 0;
  215. $table->set_header($col++, '', false);
  216. $table->set_header($col++, get_lang('Code'));
  217. $table->set_header($col++, get_lang('FirstName'));
  218. $table->set_header($col++, get_lang('LastName'));
  219. $table->set_header($col++, get_lang('LoginName'));
  220. $table->set_header($col++, get_lang('Email'));
  221. $table->set_header($col++, get_lang('Profile'));
  222. $table->set_header($col++, get_lang('AuthenticationSource'));
  223. $table->set_header($col++, get_lang('RegisteredDate'));
  224. $table->set_header($col++, get_lang('LastAccess'), false);
  225. $table->set_header($col++, get_lang('Active'), false);
  226. $table->set_column_filter(5, array($this, 'format_email'));
  227. $table->set_column_filter(6, array($this, 'format_status'));
  228. $table->set_column_filter(10, array($this, 'format_active'));
  229. $table->set_form_actions(array(
  230. 'activate' => get_lang('Activate'),
  231. 'deactivate' => get_lang('Deactivate'),
  232. 'delete' => get_lang('Delete')
  233. ));
  234. if ($return) {
  235. return $table->return_table();
  236. } else {
  237. echo $table->return_table();
  238. }
  239. }
  240. /**
  241. * Table formatter for the active column.
  242. *
  243. * @param string $active
  244. * @return string
  245. */
  246. function format_active($active)
  247. {
  248. $active = ($active == '1');
  249. if ($active) {
  250. $image = 'accept';
  251. $text = get_lang('Yes');
  252. } else {
  253. $image = 'error';
  254. $text = get_lang('No');
  255. }
  256. $result = Display::return_icon($image . '.png', $text);
  257. return $result;
  258. }
  259. function format_status($status)
  260. {
  261. $statusname = api_get_status_langvars();
  262. return $statusname[$status];
  263. }
  264. function format_email($email)
  265. {
  266. return Display :: encrypted_mailto_link($email, $email);
  267. }
  268. function display($return = false)
  269. {
  270. $result = $this->display_parameters($return);
  271. if ($this->perform_action()) {
  272. if ($return) {
  273. $result .= Display::return_confirmation_message(get_lang('Done'));
  274. } else {
  275. Display::display_confirmation_message(get_lang('Done'));
  276. }
  277. }
  278. $result .= $this->display_data($return);
  279. if ($return) {
  280. return $result;
  281. }
  282. }
  283. }