Completed
Push — master ( 58560a...52adce )
by Patrick
03:00
created

FlipsideCAPTCHA::getCaptchField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 11
rs 9.4285
c 1
b 1
f 0
1
<?php
2
require_once('Autoload.php');
3
class FlipsideCAPTCHA implements JsonSerializable
4
{
5
    public  $random_id;
6
    private $validIDs;
7
8
    public static function get_valid_captcha_ids()
9
    {
10
        $dataset = DataSetFactory::getDataSetByName('profiles');
11
        $datatable = $dataset['captcha'];
12
        $data = $datatable->read(false, array('id'));
13
        $count = count($data);
14
        for($i = 0; $i < $count; $i++)
15
        {
16
            $data[$i] = $data[$i]['id'];
17
        }
18
        return $data;
19
    }
20
21
    public static function get_all()
22
    {
23
        $res = array();
24
        $ids = FlipsideCAPTCHA::get_valid_captcha_ids();
25
        $count = count($ids);
26
        for($i = 0; $i < $count; $i++)
27
        {
28
            $captcha = new FlipsideCAPTCHA();
29
            $captcha->random_id = $ids[$i]; 
30
            array_push($res, $captcha);
31
        }
32
        return $res;
33
    }
34
35
    public static function save_new_captcha($question, $hint, $answer)
36
    {
37
        $dataset = DataSetFactory::getDataSetByName('profiles');
38
        $datatable = $dataset['captcha'];
39
        return $datatable->create(array('question'=>$question, 'hint'=>$hint, 'answer'=>$answer));
40
    }
41
42
    public function __construct()
43
    {
44
        $this->validIDs = FlipsideCAPTCHA::get_valid_captcha_ids();
45
        $this->random_id = mt_rand(0, count($this->validIDs) - 1);
46
        $this->random_id = $this->validIDs[$this->random_id];
47
    }
48
49
    protected function getCaptchField($fieldName)
50
    {
51
        $dataset = DataSetFactory::getDataSetByName('profiles');
52
        $datatable = $dataset['captcha'];
53
        $data = $datatable->read(new \Data\Filter('id eq '.$this->random_id), array($fieldName));
54
        if(empty($data))
55
        {
56
            return false;
57
        }
58
        return $data[0][$fieldName];
59
    }
60
61
    public function get_question()
62
    {
63
        return $this->getCaptchField('question');
64
    }
65
66
    public function get_hint()
67
    {
68
        return $this->getCaptchField('hint');
69
    }
70
71
    private function get_answer()
72
    {
73
        return $this->getCaptchField('answer');
74
    }
75
76
    public function is_answer_right($answer)
77
    {
78
        return strcasecmp($this->get_answer(), $answer) === 0;
79
    }
80
81
    public function draw_captcha($explination = true, $return = false, $ownForm = false)
82
    {
83
        $string = '';
84
85
        if($ownForm)
86
        {
87
            $string .= '<form id="flipcaptcha" name="flipcaptcha">';
88
        }
89
90
        $string .= '<label for="captcha" class="col-sm-2 control-label">'.$this->get_question().'</label><div class="col-sm-10"><input class="form-control" type="text" id="captcha" name="captcha" placeholder="'.$this->get_hint().'" required/></div>';
91
        if($ownForm)
92
        {
93
            $string .= '</form>';
94
        }
95
        if($explination)
96
        {
97
            $string .= '<div class="col-sm-10">The answer to this question may be found in the Burning Flipside Survival Guide. It may be found <a href="http://www.burningflipside.com/sg">here</a>.</div>';
98
        }
99
        
100
        if(!$return)
101
        {
102
            echo $string;
103
        }
104
        return $string;
105
    }
106
107
    public function self_json_encode()
108
    {
109
        return json_encode($this->jsonSerialize());
110
    }
111
112
    public function jsonSerialize()
113
    {
114
        $res = array();
115
        $res['id'] = $this->random_id;
116
        $res['question'] = $this->get_question();
117
        $res['hint'] = $this->get_hint();
118
        $res['answer'] = $this->get_answer();
119
        return $res;
120
    }
121
}
122
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
123