Advertisement
Guest User

Untitled

a guest
Apr 16th, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.47 KB | None | 0 0
  1. <?php
  2.  
  3. function add_frontend_scripts() {
  4.     if( ! is_admin() ) {
  5.  
  6.         wp_register_style( 'custom-stylesheet1', plugins_url('/css/quiz-style.css', __FILE__) );
  7.  
  8.         wp_enqueue_script('jquery');
  9.  
  10.         wp_enqueue_style( 'custom-stylesheet1' );
  11.     }
  12. }
  13. add_action('init', 'add_frontend_scripts');
  14. add_filter('the_content','folio3_survey');
  15.  
  16.  
  17. //Callback function
  18. function folio3_survey($content)
  19. {
  20.     if ( isset( $_POST['question1'] )) {
  21.  
  22.         $_data = array(
  23.             "questions" => array(1,2,3,4,5,6,7,8,9,10),
  24.             "option" => array($_POST['question1'],$_POST['question2'],$_POST['question3'],$_POST['question4'],$_POST['question5'],$_POST['question6'],$_POST['question7'],$_POST['question8'],$_POST['question9'],$_POST['question10'])
  25.         );
  26.         ob_start();
  27.         echo $content;
  28.  
  29.         $type_data = folio3_process_form($_data);
  30.         $implode_type_data = implode(",", $type_data);
  31.         $encode_implode_type_data = urlencode($implode_type_data);
  32.  
  33.         return ob_get_clean();
  34.     }
  35.     if (is_page( 'quiz' )) {
  36.  
  37.         global $wpdb;
  38.  
  39.         $randomFact = $wpdb->get_results( "SELECT * FROM folio3_survey_questions");
  40.  
  41.         $NumRows = count((array) $randomFact);
  42.         $RandNum = rand(0, $NumRows);
  43.         ob_start();
  44.         echo $content;
  45.         ?>
  46.  
  47.         <form method="post" class="not--started card" id="survey" action="" onsubmit="return validate()">
  48.             <!-- Form Goes Here -->
  49.         </form>
  50.  
  51.         <?php
  52.  
  53.         return ob_get_clean();
  54.     }
  55.     else {
  56.         return $content;
  57.     }
  58. }
  59.  
  60.  
  61. function folio3_process_form($_data)
  62. {
  63.     global $wpdb;
  64.     $sum_segment1=0;
  65.     $sum_segment2=0;
  66.     $sum_segment3=0;
  67.     $sum_segment4=0;
  68.  
  69.     for($i=0; $i<10; $i++)
  70.     {
  71.         $randomFact = $wpdb->get_results( "SELECT * FROM folio3_survey_option_values WHERE question_id =". $_data['questions'][$i] ." AND `option` = ".$_data['option'][$i]);
  72.         $sum_segment1=  $sum_segment1 + $randomFact[0]->option_segment1_value;
  73.         $sum_segment2=  $sum_segment2 + $randomFact[0]->option_segment2_value;
  74.         $sum_segment3=  $sum_segment3 + $randomFact[0]->option_segment3_value;
  75.         $sum_segment4=  $sum_segment4 + $randomFact[0]->option_segment4_value;
  76.     }
  77.     $sum_segment1 = exp($sum_segment1 + (-1.674));
  78.     $sum_segment2 = exp($sum_segment2 + (-0.489));
  79.     $sum_segment3 = exp($sum_segment3 + (2.223));
  80.     $sum_segment4 = exp($sum_segment4 + (-0.06));
  81.  
  82.     $probablity_segment1 = round((($sum_segment1/($sum_segment1+$sum_segment2+$sum_segment3+$sum_segment4)) * 100),0);
  83.     $probablity_segment2 = round((($sum_segment2/($sum_segment1+$sum_segment2+$sum_segment3+$sum_segment4)) * 100),0);
  84.     $probablity_segment3 = round((($sum_segment3/($sum_segment1+$sum_segment2+$sum_segment3+$sum_segment4)) * 100),0);
  85.     $probablity_segment4 = round((($sum_segment4/($sum_segment1+$sum_segment2+$sum_segment3+$sum_segment4)) * 100),0);
  86.  
  87.     $type_data = array();
  88.  
  89.     if ($probablity_segment1 >= 34.38):
  90.         $entrepreneur_type = "Explorer";
  91.         $type_data['type'] = $entrepreneur_type;
  92.         $type_data['url'] = "/explorer";
  93.         goto end;
  94.  
  95.         elseif ($probablity_segment4 >= 39.06):
  96.             $entrepreneur_type = "Driver";
  97.             $type_data['type'] = $entrepreneur_type;
  98.             $type_data['url'] = "/driver";
  99.             goto end;
  100.  
  101.             elseif ($probablity_segment2 >= 50.00):
  102.                 $entrepreneur_type = "Crusader";
  103.                 $type_data['type'] = $entrepreneur_type;
  104.                 $type_data['url'] = "/crusader";
  105.                 goto end;
  106.  
  107.             else:
  108.                 $entrepreneur_type = "Captain";
  109.                 $type_data['type'] = $entrepreneur_type;
  110.                 $type_data['url'] = "/captain";
  111.  
  112.             endif;
  113.  
  114.             end:
  115.             //Save to DB
  116.             $gender = $_POST['gender'];
  117.             $role = $_POST['role'];
  118.             $revenue = $_POST['annual_revenue'];
  119.             $industry = $_POST['work_your_company'];
  120.             $email = $_POST['email'];
  121.             $type = $type_data['type'];
  122.             $url = $type_data['url'];
  123.  
  124.             $wpdb->insert(  'quiz_entries',
  125.             array(
  126.                 'gender' => $gender,
  127.                 'email' => $email,
  128.                 'role' => $role,
  129.                 'revenue' => $revenue,
  130.                 'industry' => $industry,
  131.                 'type' => $type
  132.             )
  133.         );
  134.         return $type_data;
  135.     }
  136. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement