PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/formidable/pro/classes/controllers/FrmProStatisticsController.php

https://github.com/rafapires/festival-de-ideias
PHP | 1312 lines | 1039 code | 248 blank | 25 comment | 264 complexity | b8db12893ad4963ecd6bf642bf710409 MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. class FrmProStatisticsController{
  3. function FrmProStatisticsController(){
  4. add_action('admin_menu', array( &$this, 'menu' ), 24);
  5. add_action('admin_init', array(&$this, 'admin_js'));
  6. add_shortcode('frm-graph', array(&$this, 'graph_shortcode'));
  7. add_shortcode('frm-stats', array(&$this, 'stats_shortcode'));
  8. }
  9. function menu(){
  10. global $frm_settings;
  11. add_submenu_page('formidable', 'Formidable | '. __('Reports', 'formidable'), __('Reports', 'formidable'), 'frm_view_reports', 'formidable-reports', array(&$this, 'show'));
  12. add_action('admin_head-'. sanitize_title($frm_settings->menu) .'_page_formidable-reports', array(&$this, 'head'));
  13. }
  14. function admin_js(){
  15. if (isset($_GET) and isset($_GET['page']) and $_GET['page'] == 'formidable-reports'){
  16. wp_enqueue_script('swfobject');
  17. wp_enqueue_script('json2');
  18. }
  19. }
  20. function head(){
  21. require_once(FRMPRO_PATH . '/js/ofc-library/open-flash-chart-object.php');
  22. require_once(FRMPRO_PATH . '/js/ofc-library/open-flash-chart.php');
  23. }
  24. function show(){
  25. global $frmdb, $frm_form, $frm_field, $frm_entry_meta, $frm_entry, $wpdb;
  26. if (!isset($_GET['form'])){
  27. require_once(FRMPRO_VIEWS_PATH.'/frmpro-statistics/show.php');
  28. return;
  29. }
  30. $form = $frm_form->getOne($_GET['form']);
  31. //$form_options = maybe_unserialize($form->options);
  32. $fields = $frm_field->getAll("fi.type not in ('divider','captcha','break','rte','textarea','file','grid','html') and fi.form_id=".$form->id, 'field_order ASC');
  33. $js = '';
  34. $data = array();
  35. $odd = true;
  36. $colors = array('#EF8C08', '#21759B', '#1C9E05');
  37. foreach ($fields as $field){
  38. $data[$field->id] = $this->get_graph($field, array('colors' => $colors, 'bg_color' => '#FFFFFF', 'odd' => $odd));
  39. $js .= 'swfobject.embedSWF("'.FRMPRO_URL.'/js/open-flash-chart.swf", "chart_'.$field->id.'",
  40. "650", "400", "9.0.0", "expressInstall.swf", {"get-data":"get_data_'.$field->id.'"},{"wmode":"transparent"});';
  41. $odd = $odd ? false : true;
  42. }
  43. $data['time'] = $this->get_daily_entries($form, $colors);
  44. $js .= 'swfobject.embedSWF("'.FRMPRO_URL.'/js/open-flash-chart.swf","chart_time","900","300","9.0.0","expressInstall.swf", {"get-data":"get_data_time"},{"wmode":"transparent"});';
  45. //$data['hour'] = $this->get_daily_entries($form, $colors, 'HOUR'); //TODO: Fix timezone
  46. //$js .= 'swfobject.embedSWF("'.FRMPRO_URL.'/js/open-flash-chart.swf","chart_hour","900","300","9.0.0","expressInstall.swf", {"get-data":"get_data_hour"} );';
  47. $data['month'] = $this->get_daily_entries($form, $colors, 'MONTH');
  48. $js .= 'swfobject.embedSWF("'.FRMPRO_URL.'/js/open-flash-chart.swf","chart_month","900","300","9.0.0","expressInstall.swf", {"get-data":"get_data_month"},{"wmode":"transparent"});';
  49. //$data['year'] = $this->get_daily_entries($form, $colors, 'YEAR');
  50. //$js .= 'swfobject.embedSWF("'.FRMPRO_URL.'/js/open-flash-chart.swf","chart_year","900","300","9.0.0","expressInstall.swf", {"get-data":"get_data_year"} );';
  51. require(FRMPRO_VIEWS_PATH.'/frmpro-statistics/head.php');
  52. require(FRMPRO_VIEWS_PATH.'/frmpro-statistics/show.php');
  53. }
  54. function get_graph($field, $args){
  55. $defaults = array(
  56. 'ids' => false,
  57. 'colors' => array('#EF8C08', '#21759B', '#1C9E05'), 'grid_color' => '#f7e8bf', 'bg_color' => '#FFFFFF',
  58. 'odd' => false, 'truncate' => 40, 'truncate_label' => 15, 'response_count' => 10,
  59. 'user_id' => false, 'type' => 'default', 'x_axis' => false, 'data_type' => 'count',
  60. 'limit' => '', 'x_start' => '', 'x_end' => '', 'show_key' => false, 'min' => '', 'max' => '',
  61. 'include_zero' => false, 'width' => 400, 'height' => 400, 'wrap' => 15
  62. );
  63. $args = wp_parse_args($args, $defaults);
  64. $vals = $this->get_graph_values($field, $args);
  65. extract($args);
  66. extract($vals);
  67. $title = new title( preg_replace("/&#?[a-z0-9]{2,8};/i", "", FrmAppHelper::truncate($field->name, $truncate, 0)) );
  68. $chart = new open_flash_chart();
  69. $chart->set_title( $title );
  70. $bar = new bar_glass();
  71. $x = new x_axis();
  72. $y = new y_axis();
  73. $x_labels = new x_axis_labels();
  74. if (in_array($field->type, array('select', 'checkbox', 'radio', '10radio', 'scale')) and (!isset($x_inputs) or !$x_inputs)){
  75. $x_labels->rotate(340);
  76. //$x_labels->set_colour( '#A2ACBA' );
  77. //$x->set_colour( '#A2ACBA' );
  78. }else if ($field->type == 'user_id'){
  79. if(!$pie)
  80. $x_labels->rotate(340);
  81. }else{
  82. $x_labels->rotate(340);
  83. }
  84. $pie = ($type == 'default') ? $pie : (($type == 'pie') ? true : false);
  85. if ($pie){
  86. $bar = new pie();
  87. $bar->set_alpha(0.6);
  88. $bar->set_start_angle( 35 );
  89. $bar->add_animation( new pie_fade() );
  90. $bar->set_tooltip( '#val# (#percent#)' );
  91. $bar->set_colours( $colors );
  92. $pie_values = array();
  93. foreach ($values as $val_key => $val){
  94. if($val)
  95. $pie_values[] = new pie_value($val, wordwrap($labels[$val_key], $wrap, "n")." (". round(($val/$total_count) *100) . "%)");
  96. }
  97. $bar->set_values( $pie_values );
  98. }else{
  99. $color = $odd ? current($colors) : next($colors);
  100. if(!$color)
  101. $color = reset($colors);
  102. if($type == 'line')
  103. $bar = new line();
  104. else if($type == 'hbar')
  105. $bar = new hbar($color);
  106. else if($type == 'area')
  107. $bar = new area();
  108. else if($type == 'bar_flat')
  109. $bar = new bar($color);
  110. else
  111. $bar = new bar_glass($color);
  112. $bar->set_colour( $color);
  113. $bar->set_values( $values );
  114. if($show_key)
  115. $bar->set_key( stripslashes($field->name), $show_key );
  116. $x_labels->set_labels( $labels );
  117. $x->set_labels( $x_labels );
  118. $x->set_grid_colour( $grid_color );
  119. $y->set_grid_colour( $grid_color );
  120. if($combine_dates and !strpos($width, '%') and ((count($labels) * 30) > (int)$width))
  121. $x_labels->visible_steps(ceil((count($labels) * 30) / (int)$width));
  122. $set_max = $max;
  123. if (!empty($values) and empty($max)){
  124. $max = abs(max($values)*1.2);
  125. if ($max < 3) $max = 3;
  126. }
  127. foreach($ids as $f_id){
  128. $new_max = abs(max($f_values[$f_id])*1.2);
  129. if($set_max != $max and $new_max > $max)
  130. $max = $new_max;
  131. unset($f_id);
  132. unset($new_max);
  133. }
  134. $bars = array();
  135. foreach($f_values as $f_id => $f_vals){
  136. if($type == 'line')
  137. $bars[$f_id] = new line();
  138. else if($type == 'hbar')
  139. $bars[$f_id] = new hbar($color);
  140. else if($type == 'area')
  141. $bars[$f_id] = new area();
  142. else if($type == 'bar_flat')
  143. $bars[$f_id] = new bar();
  144. else
  145. $bars[$f_id] = new bar_glass();
  146. $color = next($colors);
  147. if(!$color)
  148. $color = reset($colors);
  149. $bars[$f_id]->set_colour( $color );
  150. $bars[$f_id]->set_values( $f_vals );
  151. if($show_key)
  152. $bars[$f_id]->set_key( stripslashes($fields[$f_id]->name), $show_key );
  153. unset($f_id);
  154. }
  155. if(isset($max) and !empty($max)){
  156. $step = ceil($max/10);
  157. if(empty($min))
  158. $min = 0;
  159. $y->set_range($min, $max, $step);
  160. }
  161. }
  162. $chart->add_element( $bar );
  163. if(isset($bars) and !empty($bars)){
  164. foreach($bars as $f_bar)
  165. $chart->add_element( $f_bar );
  166. }
  167. $chart->set_bg_colour( $bg_color );
  168. if(!$pie){
  169. $chart->set_y_axis( $y );
  170. $chart->set_x_axis( $x );
  171. }
  172. return $chart->toPrettyString();
  173. }
  174. function get_google_graph($field, $args){
  175. $defaults = array(
  176. 'ids' => false,
  177. 'colors' => '', 'grid_color' => '#CCC', 'bg_color' => '#FFFFFF',
  178. 'odd' => false, 'truncate' => 40, 'truncate_label' => 15, 'response_count' => 10,
  179. 'user_id' => false, 'type' => 'default', 'x_axis' => false, 'data_type' => 'count',
  180. 'limit' => '', 'x_start' => '', 'x_end' => '', 'show_key' => false, 'min' => '', 'max' => '',
  181. 'include_zero' => false, 'width' => 400, 'height' => 400, 'allowed_col_types' => array('string', 'number')
  182. );
  183. $args = wp_parse_args($args, $defaults);
  184. $vals = $this->get_graph_values($field, $args);
  185. extract($vals);
  186. extract($args);
  187. $pie = ($type == 'default') ? $pie : (($type == 'pie') ? true : false);
  188. if ($pie){
  189. $type = 'pie';
  190. $cols = array('Field' => array('type' => 'string'), 'Entries' => array('type' => 'number')); //map each array position in rows array
  191. foreach ($values as $val_key => $val){
  192. if($val)
  193. $rows[] = array($labels[$val_key], $val);
  194. }
  195. }else{
  196. if(!isset($options['hAxis']))
  197. $options['hAxis'] = array();
  198. $options['vAxis'] = array('gridlines' => array('color' => $grid_color));
  199. if($combine_dates and !strpos($width, '%') and ((count($labels) * 50) > (int)$width))
  200. $options['hAxis']['showTextEvery'] = (ceil((count($labels) * 50) / (int)$width));
  201. $options['hAxis']['slantedText'] = true;
  202. $options['hAxis']['slantedTextAngle'] = 20;
  203. $rn_order = array();
  204. foreach($labels as $lkey => $l){
  205. if(isset($x_field) and $x_field and $x_field->type == 'number'){
  206. $l = (float)$l;
  207. $rn_order[] = $l;
  208. }
  209. $row = array($l, $values[$lkey]);
  210. foreach($f_values as $f_id => $f_vals)
  211. $row[] = isset($f_vals[$lkey]) ? $f_vals[$lkey] : 0;
  212. $rows[] = $row;
  213. unset($lkey);
  214. unset($l);
  215. }
  216. if(isset($max) and !empty($max))
  217. $options['vAxis']['maxValue'] = $max;
  218. if(!empty($min))
  219. $options['vAxis']['minValue'] = $min;
  220. }
  221. if(isset($rn_order) and !empty($rn_order)){
  222. asort($rn_order);
  223. $sorted_rows = array();
  224. foreach($rn_order as $rk => $rv)
  225. $sorted_rows[] = $rows[$rk];
  226. $rows = $sorted_rows;
  227. }
  228. $options['backgroundColor'] = $bg_color;
  229. if($type == 'bar' or $type == 'bar_flat' or $type == 'bar_glass')
  230. $type = 'column';
  231. else if($type == 'hbar')
  232. $type = 'bar';
  233. $allowed_types = array('pie', 'line', 'column', 'area', 'SteppedArea', 'geo');
  234. if(!in_array($type, $allowed_types))
  235. $type = 'column';
  236. $options = apply_filters('frm_google_chart', $options, compact('rows', 'cols', 'type', 'atts'));
  237. return $this->convert_to_google($rows, $cols, $options, $type);
  238. }
  239. function get_graph_values($field, $args){
  240. global $frm_entry_meta, $frm_field, $frmdb, $wpdb;
  241. $values = $labels = $f_values = $f_labels = $rows = $cols = array();
  242. $pie = false;
  243. extract($args);
  244. $show_key = (int)$show_key;
  245. if($show_key and $show_key < 5)
  246. $show_key = 10;
  247. $options = array('width' => $width, 'height' => $height, 'legend' => 'none');
  248. if(!empty($colors))
  249. $options['colors'] = $colors;
  250. $options['title'] = preg_replace("/&#?[a-z0-9]{2,8};/i", "", FrmAppHelper::truncate($field->name, $truncate, 0));
  251. if($show_key)
  252. $options['legend'] = array('position' => 'right', 'textStyle' => array('fontSize' => $show_key));
  253. $fields = $f_inputs = array();
  254. $fields[$field->id] = $field;
  255. if($ids){
  256. $ids = explode(',', $ids);
  257. foreach($ids as $id_key => $f){
  258. $ids[$id_key] = $f = trim($f);
  259. if(!$f or empty($f)){
  260. unset($ids[$id_key]);
  261. continue;
  262. }
  263. if($add_field = $frm_field->getOne($f)){
  264. $fields[$add_field->id] = $add_field;
  265. $ids[$id_key] = $add_field->id;
  266. }
  267. unset($f);
  268. unset($id_key);
  269. }
  270. }else{
  271. $ids = array();
  272. }
  273. $cols['xaxis'] = array('type' => 'string');
  274. if($x_axis){
  275. $x_field = $frm_field->getOne($x_axis);
  276. $query = $x_query = "SELECT meta_value, item_id FROM $frmdb->entry_metas em";
  277. if(!$x_field)
  278. $x_query = "SELECT id, {$x_axis} FROM $frmdb->entries e";
  279. if($user_id){
  280. $query .= " LEFT JOIN $frmdb->entries e ON (e.id=em.item_id)";
  281. if($x_field)
  282. $x_query .= " LEFT JOIN $frmdb->entries e ON (e.id=em.item_id)";
  283. }
  284. if($x_field){
  285. if(isset($allowed_col_types))
  286. $cols['xaxis'] = array('type' => ((in_array($x_field->type, $allowed_col_types)) ? $x_field->type : 'string'), 'id' => $x_field->id);
  287. $options['hAxis'] = array('title' => stripslashes($x_field->name));
  288. $x_query .= " WHERE em.field_id='{$x_field->id}'";
  289. if(!empty($x_start)){
  290. if($x_field->type == 'date')
  291. $x_start = date('Y-m-d', strtotime($x_start));
  292. $x_query .= " and meta_value >= '$x_start'";
  293. }
  294. if(!empty($x_end)){
  295. if($x_field->type == 'date')
  296. $x_end = date('Y-m-d', strtotime($x_end));
  297. $x_query .= " and meta_value <= '$x_end'";
  298. }
  299. }else{
  300. $cols['xaxis'] = array('type' => 'string');
  301. $x_query .= " WHERE form_id=". $field->form_id;
  302. if(!empty($x_start)){
  303. if(in_array($x_axis, array('created_at', 'updated_at')))
  304. $x_start = date('Y-m-d', strtotime($x_start));
  305. $x_query .= " and e.{$x_axis} >= '$x_start'";
  306. }
  307. if(!empty($x_end)){
  308. if(in_array($x_axis, array('created_at', 'updated_at')))
  309. $x_end = date('Y-m-d', strtotime($x_end)) .' 23:59:59';
  310. $x_query .= " and e.{$x_axis} <= '$x_end'";
  311. }
  312. }
  313. $q = array();
  314. foreach($fields as $f_id => $f){
  315. if($f_id != $field->id)
  316. $q[$f_id] = $query ." WHERE em.field_id='{$f_id}'". ( ($user_id) ? " AND user_id='$user_id'" : '');
  317. unset($f);
  318. unset($f_id);
  319. }
  320. $query .= " WHERE em.field_id='{$field->id}'";
  321. if($user_id){
  322. $query .= " AND user_id='$user_id'";
  323. $x_query .= " AND user_id='$user_id'";
  324. }
  325. $inputs = $wpdb->get_results($query, ARRAY_A);
  326. $x_inputs = $wpdb->get_results($x_query, ARRAY_A);
  327. if(!$x_inputs)
  328. $x_inputs = array('id' => '0');
  329. unset($query);
  330. unset($x_query);
  331. foreach($q as $f_id => $query){
  332. $f_inputs[$f_id] = $wpdb->get_results($query, ARRAY_A);
  333. unset($query);
  334. }
  335. unset($q);
  336. }else{
  337. if($user_id)
  338. $inputs = $wpdb->get_col("SELECT meta_value FROM $frmdb->entry_metas em LEFT JOIN $frmdb->entries e ON (e.id=em.item_id) WHERE em.field_id='{$field->id}' AND user_id='$user_id'");
  339. else
  340. $inputs = $frm_entry_meta->get_entry_metas_for_field($field->id);
  341. foreach($fields as $f_id => $f){
  342. if($f_id != $field->id)
  343. $f_inputs[$f_id] = $wpdb->get_col("SELECT meta_value FROM $frmdb->entry_metas em LEFT JOIN $frmdb->entries e ON (e.id=em.item_id) WHERE em.field_id='{$f_id}'". ( ($user_id) ? " AND user_id='$user_id'" : ''));
  344. unset($f_id);
  345. unset($f);
  346. }
  347. }
  348. $inputs = array_map('maybe_unserialize', $inputs);
  349. $inputs = stripslashes_deep($inputs);
  350. foreach($f_inputs as $f_id => $f){
  351. $f = array_map('maybe_unserialize', $f);
  352. $f_inputs[$f_id] = stripslashes_deep($f);
  353. unset($f_id);
  354. unset($f);
  355. }
  356. if(isset($allowed_col_types)){
  357. //add columns for each field
  358. foreach($fields as $f_id => $f){
  359. $cols[stripslashes($f->name)] = array('type' => ((in_array($f->type, $allowed_col_types)) ? $f->type : 'number'), 'id' => $f->id);
  360. unset($f);
  361. unset($f_id);
  362. }
  363. unset($allowed_col_types);
  364. }
  365. $field_options = maybe_unserialize($field->options);
  366. $field->field_options = maybe_unserialize($field->field_options);
  367. global $frm_posts;
  368. if($user_id){
  369. $form_posts = $frmdb->get_records($frmdb->entries, array('form_id' => $field->form_id, 'post_id >' => 1, 'user_id' => $user_id), '', '', 'id,post_id');
  370. }else if($frm_posts and isset($frm_posts[$field->form_id])){
  371. $form_posts = $frm_posts[$field->form_id];
  372. }else{
  373. $form_posts = $frmdb->get_records($frmdb->entries, array('form_id' => $field->form_id, 'post_id >' => 1), '', '', 'id,post_id');
  374. $frm_posts = array($field->form_id => $form_posts);
  375. }
  376. if(!empty($form_posts)){
  377. if(isset($field->field_options['post_field']) and $field->field_options['post_field'] != ''){
  378. if($field->field_options['post_field'] == 'post_category'){
  379. $field_options = FrmProFieldsHelper::get_category_options($field);
  380. }else if($field->field_options['post_field'] == 'post_custom' and $field->field_options['custom_field'] != ''){
  381. //check custom fields
  382. foreach($form_posts as $form_post){
  383. $meta_value = get_post_meta($form_post->post_id, $field->field_options['custom_field'], true);
  384. if($meta_value){
  385. if($x_axis)
  386. $inputs[] = array('meta_value' => $meta_value, 'item_id' => $form_post->id);
  387. else
  388. $inputs[] = $meta_value;
  389. }
  390. }
  391. }else{ //if field is post field
  392. if($field->field_options['post_field'] == 'post_status')
  393. $field_options = FrmProFieldsHelper::get_status_options($field);
  394. foreach($form_posts as $form_post){
  395. $post_value = $wpdb->get_var("SELECT ". $field->field_options['post_field'] ." FROM $wpdb->posts WHERE ID=".$form_post->post_id);
  396. if($post_value){
  397. if($x_axis)
  398. $inputs[] = array('meta_value' => $post_value, 'item_id' => $form_post->id);
  399. else
  400. $inputs[] = $post_value;
  401. }
  402. }
  403. }
  404. }
  405. }
  406. if($field->type == 'data'){
  407. foreach($inputs as $k => $i){
  408. if(is_numeric($i)){
  409. if(is_array($inputs[$k]) and isset($inputs[$k]['meta_value'])){
  410. $inputs[$k]['meta_value'] = FrmProFieldsHelper::get_data_value($inputs[$k]['meta_value'], $field, array('truncate' => 'truncate_label'));
  411. }else{
  412. $inputs[$k] = FrmProFieldsHelper::get_data_value($inputs[$k], $field, array('truncate' => 'truncate_label'));
  413. }
  414. }
  415. unset($k);
  416. unset($i);
  417. }
  418. }
  419. if(isset($x_inputs) and $x_inputs){
  420. $x_temp = array();
  421. foreach($x_inputs as $x_input){
  422. if($x_field)
  423. $x_temp[$x_input['item_id']] = $x_input['meta_value'];
  424. else
  425. $x_temp[$x_input['id']] = $x_input[$x_axis];
  426. }
  427. $x_inputs = apply_filters('frm_graph_value', $x_temp, ($x_field ? $x_field : $x_axis), $args);
  428. unset($x_temp);
  429. unset($x_input);
  430. }
  431. if($x_axis and $inputs){
  432. $y_temp = array();
  433. foreach($inputs as $input)
  434. $y_temp[$input['item_id']] = $input['meta_value'];
  435. foreach($ids as $f_id){
  436. if(!isset($f_values[$f_id]))
  437. $f_values[$f_id] = array();
  438. $f_values[$f_id][key($y_temp)] = 0;
  439. unset($f_id);
  440. }
  441. $inputs = $y_temp;
  442. unset($y_temp);
  443. unset($input);
  444. }
  445. $inputs = apply_filters('frm_graph_value', $inputs, $field, $args);
  446. foreach($f_inputs as $f_id => $f){
  447. $temp = array();
  448. foreach($f as $input){
  449. if(is_array($input)){
  450. $temp[$input['item_id']] = $input['meta_value'];
  451. foreach($ids as $d){
  452. if(!isset($f_values[$d][$input['item_id']]))
  453. $f_values[$d][$input['item_id']] = 0;
  454. unset($d);
  455. }
  456. }else{
  457. $temp[] = $input;
  458. }
  459. unset($input);
  460. }
  461. $f_inputs[$f_id] = apply_filters('frm_graph_value', $temp, $fields[$f_id], $args);
  462. unset($temp);
  463. unset($input);
  464. unset($f);
  465. }
  466. if (in_array($field->type, array('select', 'checkbox', 'radio', '10radio', 'scale')) and (!isset($x_inputs) or !$x_inputs)){
  467. if($limit == '') $limit = 10;
  468. $field_opt_count = count($field_options);
  469. if($field_options){
  470. foreach ($field_options as $opt_key => $opt){
  471. $field_val = apply_filters('frm_field_value_saved', $opt, $opt_key, $field->field_options);
  472. $opt = apply_filters('frm_field_label_seen', $opt, $opt_key, $field);
  473. $count = 0;
  474. if(empty($opt))
  475. continue;
  476. $opt = stripslashes_deep($opt);
  477. foreach ($inputs as $in){
  478. if (FrmAppHelper::check_selected($in, $field_val)){
  479. if($data_type == 'total')
  480. $count += $field_val;
  481. else
  482. $count++;
  483. }
  484. }
  485. $new_val = FrmAppHelper::truncate($opt, $truncate_label, 2);
  486. if($count > 0 or $field_opt_count < $limit or (!$count and $include_zero)){
  487. $labels[$new_val] = $new_val;
  488. $values[$new_val] = $count;
  489. }
  490. unset($count);
  491. foreach($f_inputs as $f_id => $f){
  492. foreach($f as $in){
  493. if(!isset($f_values[$f_id]))
  494. $f_values[$f_id] = array();
  495. if(!isset($f_values[$f_id][$new_val]))
  496. $f_values[$f_id][$new_val] = 0;
  497. if (FrmAppHelper::check_selected($in, $field_val)){
  498. if($data_type == 'total')
  499. $f_values[$f_id][$new_val] += $field_val;
  500. else
  501. $f_values[$f_id][$new_val]++;
  502. }
  503. unset($in);
  504. }
  505. unset($f_id);
  506. unset($f);
  507. }
  508. }
  509. if($limit != 10 and count($values) > $limit){
  510. $ordered_vals = $values;
  511. arsort($ordered_vals);
  512. $l_count = 0;
  513. foreach($ordered_vals as $vkey => $v){
  514. $l_count++;
  515. if($l_count > $limit){
  516. unset($values[$vkey]);
  517. unset($labels[$vkey]);
  518. }
  519. unset($vkey);
  520. unset($v);
  521. }
  522. unset($l_count);
  523. unset($ordered_vals);
  524. }
  525. }
  526. if (!in_array($field->type, array('checkbox', '10radio', 'scale'))) //and count($field_options) == 2
  527. $pie = true;
  528. }else if ($field->type == 'user_id'){
  529. $form = $frmdb->get_one_record($frmdb->forms, array('id' => $field->form_id));
  530. $form_options = maybe_unserialize($form->options);
  531. $id_count = array_count_values($inputs);
  532. if ($form->editable and (isset($form_options['single_entry']) and $form_options['single_entry'] and isset($form_options['single_entry_type']) and $form_options['single_entry_type'] == 'user')){
  533. //if only one response per user, do a pie chart of users who have submitted the form
  534. $users_of_blog = (function_exists('get_users')) ? get_users() : get_users_of_blog();
  535. $total_users = count( $users_of_blog );
  536. unset($users_of_blog);
  537. $id_count = count($id_count);
  538. $not_completed = (int)$total_users - (int)$id_count;
  539. $labels = array(__('Completed', 'formidable'), __('Not Completed', 'formidable'));
  540. $values = array($id_count, $not_completed);
  541. $pie = true;
  542. }else{
  543. //arsort($id_count);
  544. foreach ($id_count as $val => $count){
  545. $user_info = get_userdata($val);
  546. $labels[] = ($user_info) ? $user_info->display_name : __('Deleted User', 'formidable');
  547. $values[] = $count;
  548. }
  549. if (count($labels) < 10)
  550. $pie = true;
  551. }
  552. }else{
  553. if(isset($x_inputs) and $x_inputs){
  554. $calc_array = array();
  555. foreach ($inputs as $entry_id => $in){
  556. $entry_id = (int)$entry_id;
  557. if(!isset($values[$entry_id]))
  558. $values[$entry_id] = 0;
  559. $labels[$entry_id] = (isset($x_inputs[$entry_id])) ? $x_inputs[$entry_id] : '';
  560. if(!isset($calc_array[$entry_id]))
  561. $calc_array[$entry_id] = array('count' => 0);
  562. if($data_type == 'total' or $data_type == 'average'){
  563. $values[$entry_id] += (float)$in;
  564. $calc_array[$entry_id]['total'] = $values[$entry_id];
  565. $calc_array[$entry_id]['count']++;
  566. }else{
  567. $values[$entry_id]++;
  568. }
  569. unset($entry_id);
  570. unset($in);
  571. }
  572. if($data_type == 'average'){
  573. foreach($calc_array as $entry_id => $calc){
  574. $values[$entry_id] = ($calc['total'] / $calc['count']);
  575. unset($entry_id);
  576. unset($calc);
  577. }
  578. }
  579. $calc_array = array();
  580. foreach($f_inputs as $f_id => $f){
  581. if(!isset($calc_array[$f_id]))
  582. $calc_array[$f_id] = array();
  583. foreach($f as $entry_id => $in){
  584. $entry_id = (int)$entry_id;
  585. if(!isset($labels[$entry_id])){
  586. $labels[$entry_id] = (isset($x_inputs[$entry_id])) ? $x_inputs[$entry_id] : '';
  587. $values[$entry_id] = 0;
  588. }
  589. if(!isset($calc_array[$f_id][$entry_id]))
  590. $calc_array[$f_id][$entry_id] = array('count' => 0);
  591. if(!isset($f_values[$f_id][$entry_id]))
  592. $f_values[$f_id][$entry_id] = 0;
  593. if($data_type == 'total' or $data_type == 'average'){
  594. $f_values[$f_id][$entry_id] += (float)$in;
  595. $calc_array[$f_id][$entry_id]['total'] = $f_values[$f_id][$entry_id];
  596. $calc_array[$f_id][$entry_id]['count']++;
  597. }else{
  598. $f_values[$f_id][$entry_id]++;
  599. }
  600. unset($entry_id);
  601. unset($in);
  602. }
  603. unset($f_id);
  604. unset($f);
  605. }
  606. if($data_type == 'average'){
  607. foreach($calc_array as $f_id => $calc){
  608. foreach($calc as $entry_id => $c){
  609. $f_values[$f_id][$entry_id] = ($c['total'] / $c['count']);
  610. unset($entry_id);
  611. unset($c);
  612. }
  613. unset($calc);
  614. unset($f_id);
  615. }
  616. }
  617. unset($calc_array);
  618. }else{
  619. if(is_array(reset($inputs))){
  620. $id_count = array_map('implode', $inputs);
  621. $id_count = array_count_values(array_map('strtolower', $id_count));
  622. }else{
  623. $id_count = array_count_values(array_map('strtolower', $inputs));
  624. arsort($id_count);
  625. }
  626. $i = 0;
  627. foreach ($id_count as $val => $count){
  628. if ($i < $response_count){
  629. if ($field->type == 'user_id'){
  630. $user_info = get_userdata($val);
  631. $new_val = $user_info->display_name;
  632. }else{
  633. $new_val = ucwords($val);
  634. }
  635. $labels[$new_val] = $new_val;
  636. $values[$new_val] = $count;
  637. }
  638. $i++;
  639. }
  640. foreach($f_inputs as $f_id => $f){
  641. $id_count = array_count_values(array_map('strtolower', $f));
  642. arsort($id_count);
  643. $i = 0;
  644. foreach ($id_count as $val => $count){
  645. if ($i < $response_count){
  646. if ($field->type == 'user_id'){
  647. $user_info = get_userdata($val);
  648. $new_val = $user_info->display_name;
  649. }else{
  650. $new_val = ucwords($val);
  651. }
  652. $position = array_search($new_val, $labels);
  653. if(!$position){
  654. end($labels);
  655. $position = key($labels);
  656. $labels[$new_val] = $new_val;
  657. $values[$new_val] = 0;
  658. }
  659. $f_values[$f_id][$new_val] = $count;
  660. }
  661. $i++;
  662. }
  663. unset($f_id);
  664. unset($f);
  665. }
  666. }
  667. }
  668. if(isset($x_inputs) and $x_inputs){
  669. $used_vals = $calc_array = array();
  670. foreach($labels as $l_key => $label){
  671. if(empty($label) and (!empty($x_start) or !empty($x_end))){
  672. unset($values[$l_key]);
  673. unset($labels[$l_key]);
  674. continue;
  675. }
  676. if(in_array($x_axis, array('created_at', 'updated_at'))){
  677. if($type == 'pie')
  678. $labels[$l_key] = $label = $inputs[$l_key];
  679. else
  680. $labels[$l_key] = $label = date('Y-m-d', strtotime($label));
  681. }
  682. if(isset($used_vals[$label])){
  683. $values[$l_key] += $values[$used_vals[$label]];
  684. unset($values[$used_vals[$label]]);
  685. foreach($ids as $f_id){
  686. if(!isset($f_values[$f_id][$l_key]))
  687. $f_values[$f_id][$l_key] = 0;
  688. if(!isset($f_values[$f_id][$used_vals[$label]]))
  689. $f_values[$f_id][$used_vals[$label]] = 0;
  690. $f_values[$f_id][$l_key] += $f_values[$f_id][$used_vals[$label]];
  691. unset($f_values[$f_id][$used_vals[$label]]);
  692. unset($f_id);
  693. }
  694. unset($labels[$used_vals[$label]]);
  695. }
  696. $used_vals[$label] = $l_key;
  697. if($data_type == 'average'){
  698. if(!isset($calc_array[$label]))
  699. $calc_array[$label] = 0;
  700. $calc_array[$label]++;
  701. }
  702. unset($label);
  703. unset($l_key);
  704. }
  705. if(!empty($calc_array)){
  706. foreach($calc_array as $label => $calc){
  707. if(isset($used_vals[$label])){
  708. $values[$used_vals[$label]] = ($values[$used_vals[$label]] / $calc);
  709. foreach($ids as $f_id){
  710. $f_values[$f_id][$used_vals[$label]] = ($f_values[$f_id][$used_vals[$label]] / $calc);
  711. unset($f_id);
  712. }
  713. }
  714. unset($label);
  715. unset($calc);
  716. }
  717. }
  718. unset($used_vals);
  719. }
  720. $combine_dates = false;
  721. if((isset($x_field) and $x_field and $x_field->type == 'date') or in_array($x_axis, array('created_at', 'updated_at')))
  722. $combine_dates = apply_filters('frm_combine_dates', true, $x_field);
  723. if($combine_dates){
  724. if($include_zero){
  725. $start_timestamp = (empty($x_start)) ? time() : strtotime($x_start);
  726. $end_timestamp = (empty($x_end)) ? time() : strtotime($x_end);
  727. $dates_array = array();
  728. // Get the dates array
  729. for($e = $start_timestamp; $e <= $end_timestamp; $e += 60*60*24)
  730. $dates_array[] = date('Y-m-d', $e);
  731. unset($e);
  732. // Add the zero count days
  733. foreach($dates_array as $date_str){
  734. if(!in_array($date_str, $labels)){
  735. $labels[$date_str] = $date_str;
  736. $values[$date_str] = 0;
  737. foreach($ids as $f_id){
  738. if(!isset($f_values[$f_id][$date_str]))
  739. $f_values[$f_id][$date_str] = 0;
  740. }
  741. }
  742. }
  743. unset($dates_array);
  744. unset($start_timestamp);
  745. unset($end_timestamp);
  746. }
  747. asort($labels);
  748. global $frmpro_settings;
  749. foreach($labels as $l_key => $l){
  750. if((isset($x_field) and $x_field and $x_field->type == 'date') or in_array($x_axis, array('created_at', 'updated_at'))){
  751. if ($type != 'pie' and preg_match('/^\d{4}-\d{2}-\d{2}$/', $l)){
  752. global $frmpro_settings;
  753. $labels[$l_key] = FrmProAppHelper::convert_date($l, 'Y-m-d', $frmpro_settings->date_format);
  754. }
  755. }
  756. unset($l_key);
  757. unset($l);
  758. }
  759. $values = FrmProAppHelper::sort_by_array($values, array_keys($labels));
  760. foreach($ids as $f_id){
  761. $f_values[$f_id] = FrmProAppHelper::sort_by_array($f_values[$f_id], array_keys($labels));
  762. $f_values[$f_id] = FrmProAppHelper::reset_keys($f_values[$f_id]);
  763. ksort($f_values[$f_id]);
  764. unset($f_id);
  765. }
  766. }else{
  767. if(isset($x_inputs) and $x_inputs){
  768. foreach($labels as $l_key => $l){
  769. foreach($ids as $f_id){
  770. //do a last check to make sure all bars/lines have a value for each label
  771. if(!isset($f_values[$f_id][$l_key]))
  772. $f_values[$f_id][$l_key] = 0;
  773. unset($fid);
  774. }
  775. unset($l_key);
  776. unset($l);
  777. }
  778. }
  779. foreach($ids as $f_id){
  780. $f_values[$f_id] = FrmProAppHelper::reset_keys($f_values[$f_id]);
  781. ksort($f_values[$f_id]);
  782. unset($f_id);
  783. }
  784. ksort($labels);
  785. ksort($values);
  786. }
  787. $labels = FrmProAppHelper::reset_keys($labels);
  788. $values = FrmProAppHelper::reset_keys($values);
  789. $return = array('total_count' => count($inputs), 'f_values' => $f_values, 'labels' => $labels,
  790. 'values' => $values, 'pie' => $pie, 'combine_dates' => $combine_dates, 'ids' => $ids, 'cols' => $cols,
  791. 'rows' => $rows, 'options' => $options, 'fields' => $fields
  792. );
  793. if(isset($x_inputs))
  794. $return['x_inputs'] = $x_inputs;
  795. return $return;
  796. }
  797. function convert_to_google($rows, $cols, $options, $type){
  798. $gcontent = '';
  799. if(!empty($cols)){
  800. foreach((array)$cols as $col_name => $col){
  801. $gcontent .= "data.addColumn('". $col['type'] ."','{$col_name}');";
  802. unset($col_name);
  803. unset($col);
  804. }
  805. }
  806. if(!empty($rows)){
  807. if($type == 'table'){
  808. $last = end($rows);
  809. $count = $last[0]+1;
  810. $gcontent .= "data.addRows($count);\n";
  811. foreach($rows as $row){
  812. $gcontent .= "data.setCell(". implode(',', $row). ");"; //data.setCell(0, 0, 'Mike');
  813. unset($row);
  814. }
  815. }else{
  816. $gcontent .= "data.addRows(". json_encode($rows). ");\n";
  817. }
  818. }
  819. if(!empty($options))
  820. $gcontent .= "var options=". json_encode($options) ."\n";
  821. return compact('gcontent', 'type');
  822. }
  823. function get_daily_entries($form, $colors=false, $type="DATE"){
  824. global $wpdb, $frmdb;
  825. if(!$colors)
  826. $colors = array('#EF8C08', '#21759B', '#1C9E05');
  827. $type = strtoupper($type);
  828. //Chart for Entries Submitted
  829. $values = array();
  830. $labels = array();
  831. if($type == 'HOUR')
  832. $start_timestamp = strtotime('-48 hours');
  833. else if($type == 'MONTH')
  834. $start_timestamp = strtotime('-1 year');
  835. else if($type == 'YEAR')
  836. $start_timestamp = strtotime('-10 years');
  837. else
  838. $start_timestamp = strtotime('-1 month');
  839. $end_timestamp = time();
  840. if($type == 'HOUR'){
  841. $query = "SELECT en.created_at as endate,COUNT(*) as encount FROM $frmdb->entries en WHERE en.created_at >= '".date("Y-n-j H", $start_timestamp).":00:00' AND en.form_id=$form->id GROUP BY endate";
  842. }else{
  843. $query = "SELECT DATE(en.created_at) as endate,COUNT(*) as encount FROM $frmdb->entries en WHERE en.created_at >= '".date("Y-n-j", $start_timestamp)." 00:00:00' AND en.form_id=$form->id GROUP BY $type(en.created_at)";
  844. }
  845. $entries_array = $wpdb->get_results($query);
  846. $temp_array = $counts_array = $dates_array = array();
  847. // Refactor Array for use later on
  848. foreach($entries_array as $e){
  849. $e_key = $e->endate;
  850. if($type == 'HOUR')
  851. $e_key = date('Y-m-d H', strtotime($e->endate)) .':00:00';
  852. else if($type == 'MONTH')
  853. $e_key = date('Y-m', strtotime($e->endate)) .'-01';
  854. else if($type == 'YEAR')
  855. $e_key = date('Y', strtotime($e->endate)) .'-01-01';
  856. $temp_array[$e_key] = $e->encount;
  857. }
  858. // Get the dates array
  859. if($type == 'HOUR'){
  860. for($e = $start_timestamp; $e <= $end_timestamp; $e += 60*60){
  861. if(!in_array(date('Y-m-d H', $e) .':00:00' , $dates_array))
  862. $dates_array[] = date('Y-m-d H', $e) .':00:00';
  863. }
  864. $date_format = get_option('time_format');
  865. }else if($type == 'MONTH'){
  866. for($e = $start_timestamp; $e <= $end_timestamp; $e += 60*60*24*25){
  867. if(!in_array(date('Y-m', $e) .'-01', $dates_array))
  868. $dates_array[] = date('Y-m', $e) .'-01';
  869. }
  870. $date_format = 'F Y';
  871. }else if($type == 'YEAR'){
  872. for($e = $start_timestamp; $e <= $end_timestamp; $e += 60*60*24*364){
  873. if(!in_array(date('Y', $e) .'-01-01', $dates_array))
  874. $dates_array[] = date('Y', $e) .'-01-01';
  875. }
  876. $date_format = 'Y';
  877. }else{
  878. for($e = $start_timestamp; $e <= $end_timestamp; $e += 60*60*24)
  879. $dates_array[] = date("Y-m-d", $e);
  880. $date_format = get_option('date_format');
  881. }
  882. // Make sure counts array is in order and includes zero click days
  883. foreach($dates_array as $date_str){
  884. if(isset($temp_array[$date_str]))
  885. $counts_array[$date_str] = $temp_array[$date_str];
  886. else
  887. $counts_array[$date_str] = 0;
  888. }
  889. foreach ($counts_array as $date => $count){
  890. $labels[] = date_i18n($date_format, strtotime($date));
  891. $values[] = (int)$count;
  892. }
  893. if($type == 'MONTH')
  894. $title = __('Monthly Entries', 'formidable');
  895. else if($type == 'YEAR')
  896. $title = __('Yearly Entries', 'formidable');
  897. else if($type == 'HOUR')
  898. $title = __('Hourly Entries', 'formidable');
  899. else
  900. $title = __('Daily Entries', 'formidable');
  901. $title = new title( $title );
  902. $line_1_default_dot = new dot();
  903. $line_1_default_dot->colour($colors[0]);
  904. $line_1_default_dot->tooltip('#x_label#<br>#val# Entries');
  905. $line_1 = new line();
  906. $line_1->set_default_dot_style($line_1_default_dot);
  907. $line_1->set_values( $values );
  908. $line_1->set_colour( $colors[1] );
  909. $chart = new open_flash_chart();
  910. $chart->set_title( $title );
  911. $chart->set_bg_colour( '#FFFFFF' );
  912. $x = new x_axis();
  913. $x_labels = new x_axis_labels();
  914. $x_labels->rotate(340);
  915. $x_labels->set_labels( $labels );
  916. if(count($labels) > 15)
  917. $x_labels->visible_steps(2);
  918. $x->set_labels($x_labels);
  919. $chart->set_x_axis( $x );
  920. $y = new y_axis();
  921. if (!empty($values)){
  922. $max = max($values)+1;
  923. $step = ceil($max/10);
  924. $y->set_range(0, $max, $step);
  925. }
  926. $chart->add_element( $line_1 );
  927. $chart->set_y_axis( $y );
  928. return $chart->toPrettyString();
  929. }
  930. function google_graph($atts){
  931. $type = isset($atts['type']) ? $atts['type'] : 'default';
  932. $defaults = array(
  933. 'id' => false, 'id2' => false, 'id3' => false, 'id4' => false, 'ids' => false,
  934. 'include_js' => true, 'colors' => '', 'grid_color' => '#CCC',
  935. 'height' => 400, 'width' => 400, 'truncate_label' => 7,
  936. 'bg_color' => '#FFFFFF', 'truncate' => 40, 'response_count' => 10, 'user_id' => false,
  937. 'type' => 'default', 'x_axis' => false, 'data_type' => 'count', 'limit' => '',
  938. 'x_start' => '', 'x_end' => '', 'show_key' => false, 'min' => '', 'max' => '',
  939. 'include_zero' => false
  940. );
  941. if($type == 'geo'){
  942. $defaults['truncate_label'] = 100;
  943. $defaults['width'] = 600;
  944. }
  945. extract(shortcode_atts($defaults, $atts));
  946. if (!$id) return;
  947. global $frm_field, $frm_google_chart;
  948. if(!$ids and ($id2 or $id3 or $id4)){
  949. $ids = array($id2, $id3, $id4);
  950. $ids = implode(',', $ids);
  951. }
  952. $x_axis = (!$x_axis or $x_axis == 'false') ? false : $x_axis;
  953. $user_id = FrmProAppHelper::get_user_id_param($user_id);
  954. $html = $js = $js_content2 = '';
  955. $fields = $frm_field->getAll("fi.id in ($id)");
  956. if(!empty($colors))
  957. $colors = explode(',', $colors);
  958. $js_content = '<script type="text/javascript">';
  959. if($include_js and !$frm_google_chart){
  960. $js_content = '<script type="text/javascript" src="https://www.google.com/jsapi"></script>';
  961. $js_content .= '<script type="text/javascript">';
  962. $js_content .= "google.load('visualization', '1.0', {'packages':['". ($type == 'geo' ? 'geochart' : 'corechart')."']});\n";
  963. if($type != 'geo')
  964. $frm_google_chart = true;
  965. }else if($type == 'geo'){
  966. $js_content .= "google.load('visualization', '1', {'packages': ['geochart']});\n";
  967. }
  968. global $frm_gr_count;
  969. if(!$frm_gr_count)
  970. $frm_gr_count = 0;
  971. foreach ($fields as $field){
  972. $data = $this->get_google_graph($field, compact('ids', 'colors', 'grid_color', 'bg_color', 'truncate', 'truncate_label', 'response_count', 'user_id', 'type', 'x_axis', 'data_type', 'limit', 'x_start', 'x_end', 'show_key', 'min', 'max', 'include_zero', 'width', 'height'));
  973. $frm_gr_count++;
  974. $this_id = $field->id .'_'. $frm_gr_count;
  975. $html .= '<div id="chart_'. $this_id .'" style="height:'.$height.';width:'.$width.'"></div>';
  976. $js_content2 .= "google.setOnLoadCallback(get_data_{$this_id});\n";
  977. $js_content2 .= "function get_data_{$this_id}(){var data=new google.visualization.DataTable();";
  978. $js_content2 .= $data['gcontent'];
  979. $js_content2 .= "var chart=new google.visualization.". ucfirst($data['type']) ."Chart(document.getElementById('chart_{$this_id}')); chart.draw(data, options);}";
  980. }
  981. $js_content .= $js . $js_content2;
  982. $js_content .= '</script>';
  983. return

Large files files are truncated, but you can click here to view the full file