zombie_report.class.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 = new FormValidator(
  87. $parameters['name'],
  88. $parameters['method'],
  89. null,
  90. null,
  91. $parameters['attributes']
  92. );
  93. }
  94. return $this->parameters_form;
  95. }
  96. function display_parameters($return = false)
  97. {
  98. $form = $this->get_parameters_form();
  99. if (empty($form)) {
  100. return '';
  101. }
  102. $result = $form->returnForm();
  103. if ($return) {
  104. return $result;
  105. } else {
  106. echo $result;
  107. }
  108. }
  109. function is_valid()
  110. {
  111. $form = $this->get_parameters_form();
  112. if (empty($form)) {
  113. return true;
  114. }
  115. return $form->isSubmitted() == false || $form->validate();
  116. }
  117. function get_ceiling($format = null)
  118. {
  119. $result = Request::get('ceiling');
  120. $result = $result ? $result : ZombieManager::last_year();
  121. $result = is_array($result) && count($result) == 1 ? reset($result) : $result;
  122. $result = is_array($result) ? mktime(0, 0, 0, $result['F'], $result['d'], $result['Y']) : $result;
  123. $result = is_numeric($result) ? (int) $result : $result;
  124. $result = is_string($result) ? strtotime($result) : $result;
  125. if ($format) {
  126. $result = date($format, $result);
  127. }
  128. return $result;
  129. }
  130. function get_active_only()
  131. {
  132. $result = Request::get('active_only', false);
  133. $result = $result === 'true' ? true : $result;
  134. $result = $result === 'false' ? false : $result;
  135. $result = (bool) $result;
  136. return $result;
  137. }
  138. function get_action()
  139. {
  140. /**
  141. * todo check token
  142. */
  143. $check = Security::check_token('post');
  144. Security::clear_token();
  145. if (!$check) {
  146. return 'display';
  147. }
  148. return Request::post('action', 'display');
  149. }
  150. function perform_action()
  151. {
  152. $ids = Request::post('id');
  153. if (empty($ids)) {
  154. return $ids;
  155. }
  156. $action = $this->get_action();
  157. $f = array($this, 'action_'.$action);
  158. if (is_callable($f)) {
  159. return call_user_func($f, $ids);
  160. }
  161. return false;
  162. }
  163. function action_deactivate($ids)
  164. {
  165. return UserManager::deactivate_users($ids);
  166. }
  167. function action_activate($ids)
  168. {
  169. return UserManager::activate_users($ids);
  170. }
  171. function action_delete($ids)
  172. {
  173. return UserManager::delete_users($ids);
  174. }
  175. function count()
  176. {
  177. if (!$this->is_valid()) {
  178. return 0;
  179. }
  180. $ceiling = $this->get_ceiling();
  181. $active_only = $this->get_active_only();
  182. $items = ZombieManager::listZombies($ceiling, $active_only);
  183. return count($items);
  184. }
  185. function get_data($from, $count, $column, $direction)
  186. {
  187. if (!$this->is_valid()) {
  188. return array();
  189. }
  190. $ceiling = $this->get_ceiling();
  191. $active_only = $this->get_active_only();
  192. $items = ZombieManager::listZombies($ceiling, $active_only, $count, $from, $column, $direction);
  193. $result = array();
  194. foreach ($items as $item) {
  195. $row = array();
  196. $row[] = $item['user_id'];
  197. $row[] = $item['code'];
  198. $row[] = $item['firstname'];
  199. $row[] = $item['lastname'];
  200. $row[] = $item['username'];
  201. $row[] = $item['email'];
  202. $row[] = $item['status'];
  203. $row[] = $item['auth_source'];
  204. $row[] = api_format_date($item['registration_date'], DATE_FORMAT_SHORT);
  205. $row[] = api_format_date($item['login_date'], DATE_FORMAT_SHORT);
  206. $row[] = $item['active'];
  207. $result[] = $row;
  208. }
  209. return $result;
  210. }
  211. function display_data($return = false)
  212. {
  213. $count = array($this, 'count');
  214. $data = array($this, 'get_data');
  215. $parameters = array();
  216. $parameters['sec_token'] = Security::get_token();
  217. $parameters['ceiling'] = $this->get_ceiling();
  218. $parameters['active_only'] = $this->get_active_only() ? 'true' : 'false';
  219. $additional_parameters = $this->get_additional_parameters();
  220. $parameters = array_merge($additional_parameters, $parameters);
  221. $table = new SortableTable('users', $count, $data, 1, 50);
  222. $table->set_additional_parameters($parameters);
  223. $col = 0;
  224. $table->set_header($col++, '', false);
  225. $table->set_header($col++, get_lang('Code'));
  226. $table->set_header($col++, get_lang('FirstName'));
  227. $table->set_header($col++, get_lang('LastName'));
  228. $table->set_header($col++, get_lang('LoginName'));
  229. $table->set_header($col++, get_lang('Email'));
  230. $table->set_header($col++, get_lang('Profile'));
  231. $table->set_header($col++, get_lang('AuthenticationSource'));
  232. $table->set_header($col++, get_lang('RegisteredDate'));
  233. $table->set_header($col++, get_lang('LastAccess'), false);
  234. $table->set_header($col++, get_lang('Active'), false);
  235. $table->set_column_filter(5, array($this, 'format_email'));
  236. $table->set_column_filter(6, array($this, 'format_status'));
  237. $table->set_column_filter(10, array($this, 'format_active'));
  238. $table->set_form_actions(array(
  239. 'activate' => get_lang('Activate'),
  240. 'deactivate' => get_lang('Deactivate'),
  241. 'delete' => get_lang('Delete')
  242. ));
  243. if ($return) {
  244. return $table->return_table();
  245. } else {
  246. echo $table->return_table();
  247. }
  248. }
  249. /**
  250. * Table formatter for the active column.
  251. *
  252. * @param string $active
  253. * @return string
  254. */
  255. function format_active($active)
  256. {
  257. $active = ($active == '1');
  258. if ($active) {
  259. $image = 'accept';
  260. $text = get_lang('Yes');
  261. } else {
  262. $image = 'error';
  263. $text = get_lang('No');
  264. }
  265. $result = Display::return_icon($image.'.png', $text);
  266. return $result;
  267. }
  268. function format_status($status)
  269. {
  270. $statusname = api_get_status_langvars();
  271. return $statusname[$status];
  272. }
  273. function format_email($email)
  274. {
  275. return Display::encrypted_mailto_link($email, $email);
  276. }
  277. function display($return = false)
  278. {
  279. $result = $this->display_parameters($return);
  280. if ($this->perform_action()) {
  281. if ($return) {
  282. $result .= Display::return_message(get_lang('Done'), 'confirmation');
  283. } else {
  284. echo Display::return_message(get_lang('Done'), 'confirmation');
  285. }
  286. }
  287. $result .= $this->display_data($return);
  288. if ($return) {
  289. return $result;
  290. }
  291. }
  292. }