Completed
Push — master ( 12d1e3...236763 )
by Patrick
03:09
created

class.FlipsideCAPTCHA.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
        for($i = 0; $i < count($ids); $i++)
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
26
        {
27
            $captcha = new FlipsideCAPTCHA();
28
            $captcha->random_id = $ids[$i]; 
29
            array_push($res, $captcha);
30
        }
31
        return $res;
32
    }
33
34
    public static function save_new_captcha($question, $hint, $answer)
35
    {
36
        $dataset = DataSetFactory::getDataSetByName('profiles');
37
        $datatable = $dataset['captcha'];
38
        return $datatable->create(array('question'=>$question,'hint'=>$hint,'answer'=>$answer));
39
    }
40
41
    public function __construct()
42
    {
43
        $this->validIDs = FlipsideCAPTCHA::get_valid_captcha_ids();
44
        $this->random_id = mt_rand(0, count($this->validIDs)-1);
45
        $this->random_id = $this->validIDs[$this->random_id];
46
    }
47
48 View Code Duplication
    public function get_question()
49
    {
50
        $dataset = DataSetFactory::getDataSetByName('profiles');
51
        $datatable = $dataset['captcha'];
52
        $data = $datatable->read(new \Data\Filter('id eq '.$this->random_id), array('question'));
53
        if($data === false)
54
        {
55
            return false;
56
        }
57
        return $data[0]['question'];
58
    }
59
60 View Code Duplication
    public function get_hint()
61
    {
62
        $dataset = DataSetFactory::getDataSetByName('profiles');
63
        $datatable = $dataset['captcha'];
64
        $data = $datatable->read(new \Data\Filter('id eq '.$this->random_id), array('hint'));
65
        if($data === false)
66
        {
67
            return false;
68
        }
69
        return $data[0]['hint'];
70
    }
71
72 View Code Duplication
    private function get_answer()
73
    {
74
        $dataset = DataSetFactory::getDataSetByName('profiles');
75
        $datatable = $dataset['captcha'];
76
        $data = $datatable->read(new \Data\Filter('id eq '.$this->random_id), array('answer'));
77
        if($data === false)
78
        {
79
            return false;
80
        }
81
        return $data[0]['answer'];
82
    }
83
84
    public function is_answer_right($answer)
85
    {
86
        return strcasecmp($this->get_answer(),$answer) == 0;
87
    }
88
89
    public function draw_captcha($explination=true, $return=false, $ownForm=false)
90
    {
91
        $string = '';
92
93
        if($ownForm)
94
        {
95
            $string.= '<form id="flipcaptcha" name="flipcaptcha">';
96
        }
97
98
        $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>';
99
        if($ownForm)
100
        {
101
            $string.='</form>';
102
        }
103
        if($explination)
104
        {
105
            $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>';
106
        }
107
        
108
        if(!$return)
109
        {
110
            echo $string;
111
        }
112
        return $string;
113
    }
114
115
    public function self_json_encode()
116
    {
117
        return json_encode($this->jsonSerialize());
118
    }
119
120
    public function jsonSerialize()
121
    {
122
        $res = array();
123
        $res['id'] = $this->random_id;
124
        $res['question'] = $this->get_question();
125
        $res['hint'] = $this->get_hint();
126
        $res['answer'] = $this->get_answer();
127
        return $res;
128
    }
129
}
130
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
131
?>
132