-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatePoll.php
60 lines (55 loc) · 1.77 KB
/
createPoll.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
require_once 'libs/models/option.php';
require_once 'libs/common.php';
require_once 'libs/models/poll.php';
require_once 'libs/tietokantayhteys.php';
if (!isLoggedIn()) {
$_SESSION['message'] = "You need to be logged in to create a poll";
header('Location: index.php');
exit;
}
$new_poll = new Poll();
$new_poll->setTopic($_POST['topic']);
$new_poll->setDescription($_POST['description']);
$new_poll->setStartDate(date("Y-m-d"));
$new_poll->setEndDate($_POST['end_date']);
$new_poll->setUserId($_SESSION['user_id']);
$options = array();
foreach ($_POST['options'] as $option) {
if ($option != "") {
$new_option = new Option();
$new_option->setOptionName($option);
$new_option->setVoteCount(0);
$options[] = $new_option;
}
}
/* tarkistetaan ovatko kaikki annetut vaihtoehdot oikein */
$options_valid = true;
foreach ($options as $option) {
if (!$option->isValid()) {
$options_valid = false;
}
}
/* validoidaan äänestyksen tiedot, vaihtoehtojen määrä ja muoto */
if ($new_poll->isValid() and (sizeof($options) > 1) and $options_valid ) {
$new_poll->addIntoDatabase();
foreach ($options as $option) {
if ($option->isValid()) {
$option->setPollId($new_poll->getId());
$option->addIntoDatabase();
}
}
$_SESSION['message'] = "Poll created successfully";
header('Location: index.php');
}else {
$errors = array();
if (sizeof($options) <= 1) {
$errors += array("You need to give at least 2 options");
}
$errors += $new_poll->getErrors();
foreach ($options as $option) {
if (!$option->isValid()) {
$errors += $option->getErrors();
}}
naytaNakyma('newpoll.php', array('poll'=> $new_poll, 'errors'=>$errors));
}