survey_seahorse

Software Engineering Project - Fall 2018
Log | Files | Refs | README

takesurvey.php (5201B)


      1 <?php
      2 include_once 'res/session.php';
      3 include_once 'config.php';
      4 include 'res/navbar.php';
      5 
      6 $uid = $_SESSION['uid'];
      7 
      8 if (empty($_GET)) {
      9     $code = mysqli_real_escape_string($connect, $_POST['survey-code']);
     10     $sql = "SELECT * FROM survey WHERE access_code='$code'";
     11 } else {
     12     $code = mysqli_real_escape_string($connect, $_GET['sid']);
     13     $sql = "SELECT * FROM survey WHERE survey_id ='$code'";
     14 }
     15 
     16 if (empty($code)) {
     17     header("Location: ../findsurvey.php?find=error");
     18     exit();
     19 } elseif (empty($uid)) {
     20     header("Location: login.php?sid=" . $_GET['sid']);
     21 } else {
     22     //    $sql = "SELECT * FROM survey WHERE access_code='$code'";
     23     $result = mysqli_query($connect, $sql);
     24     $check = mysqli_num_rows($result);
     25 
     26     $block = $_SESSION['blocked'];
     27 
     28     if ($check < 1) {
     29         header("Location: ../findsurvey.php?find=error");
     30         exit();
     31     } elseif ($block == 50 || $block == 51) {
     32         header("Location: res/nope.php");
     33         exit();
     34     }else {
     35         echo '<link rel="stylesheet" href="res/style.css">';
     36         echo  '<div class="take-survey">';
     37         $row = mysqli_fetch_assoc($result);
     38         $_SESSION['sid'] = $row['survey_id'];
     39         $sid = $row['survey_id'];
     40         $title = $row['title'];
     41         $number_questions = $row['number_questions'];
     42         $type = ord($row['type']);
     43         $expire_date = $row['expiration_date'];
     44         $one_shot = ord($row['one_shot']);
     45         
     46 
     47         if ($one_shot == 49) {
     48             if ($type == 48) {
     49                 $sql = "SELECT user_id FROM answer_numeric WHERE user_id=$uid AND survey_id=$sid;";
     50             } elseif ($type == 49) {
     51                 $sql = "SELECT user_id FROM answer_bool WHERE user_id=$uid AND survey_id=$sid;";
     52             } elseif ($type == 50) {
     53                 $sql = "SELECT user_id FROM answer_text WHERE user_id=$uid AND survey_id=$sid;";
     54             }
     55             $result = mysqli_query($connect, $sql);
     56             $num_rows = mysqli_num_rows($result);
     57             if ($num_rows > 0) {
     58                 $_SESSION['taken'] = 1;
     59         echo "<h3> NOTICE: Survey already taken. Your answers will not be counted. </h3>";
     60             } else {
     61                 $_SESSION['taken'] = 0;
     62 
     63             }
     64         }
     65 
     66      if ($expire_date < date("Y-m-d")) {
     67         $_SESSION['expired'] = 1;
     68         echo "<h3> NOTICE: This survey is expired. Your answers will not be counted. </h3>";
     69      } else {
     70         $_SESSION['expired'] = 0;
     71      }
     72 
     73 
     74         echo "<form method = 'POST' action = 'res/submit-survey.php'>";
     75         echo  "<h1> $title </h1> <br>";
     76 
     77         if ($type == 48) {
     78             for($counter = 0; $counter < $number_questions; $counter++) {
     79                 $sql = "SELECT question_content FROM question WHERE survey_id=$sid and question_number=$counter+1;";
     80                 $result = mysqli_query($connect, $sql);
     81                 $q = mysqli_fetch_assoc($result);
     82                 echo  $q['question_content'], "<br>";
     83                 echo " <select name = 'answers[]'>
     84                     <option value='1'>1</option>
     85                     <option value='2'>2</option>
     86                     <option value='3'>3</option>
     87                     <option value='4'>4</option>
     88                     <option selected='selected' value='5'>5</option>
     89                     <option value='6'>6</option>
     90                     <option value='7'>7</option>
     91                     <option value='8'>8</option>
     92                     <option value='9'>9</option>
     93                     <option value='10'>10</option>
     94                     </select> ";
     95                 echo "<p> <span id='demo'</span> </p>";
     96             }
     97             echo "<input type='submit' name ='numeric' type = 'submit' value = 'Submit'/>";
     98         } elseif ($type == 49) {
     99             for($counter = 0; $counter < $number_questions; $counter++) {
    100                 $sql = "SELECT question_content FROM question WHERE survey_id=$sid and question_number=$counter+1;";
    101                 $result = mysqli_query($connect, $sql);
    102                 $q = mysqli_fetch_assoc($result);
    103                 echo  $q['question_content'], "<br>";
    104                 echo " <select name = 'answers[]'>
    105                     <option selected = 'selected' value='Yes'>Yes</option>
    106                     <option value='No'>No</option>
    107                     </select> ";
    108                 echo "<p> <span id='demo'</span> </p>";
    109             }
    110             echo "<intput type='submit' name ='bool' type = 'submit' value = 'Submit'/>";
    111         } elseif ($type == 50) {
    112             for($counter = 0; $counter < $number_questions; $counter++) {
    113                 $sql = "SELECT question_content FROM question WHERE survey_id=$sid and question_number=$counter+1;";
    114                 $result = mysqli_query($connect, $sql);
    115                 $q = mysqli_fetch_assoc($result);
    116                 echo  $q['question_content'], "<br> <br>";
    117                 echo " <form name = 'answers[]'>
    118                     <input type = 'text' placeholder = 'Answer'></input>
    119                     </form> ";
    120                 echo "<p> <span id='demo'</span> </p>";
    121             }
    122             echo "<input type='submit' name ='text' type = 'submit' value = 'Submit'/>";
    123         }
    124         echo "</form>";
    125     }
    126 }
    127 ?>