Completed
Push — master ( 9c4346...4bd980 )
by Patrick
06:39
created

class.FlipsideCAPTCHA.php (5 issues)

Severity

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::get_data_set('profiles');
0 ignored issues
show
Deprecated Code introduced by
The method DataSetFactory::get_data_set() has been deprecated with message: 2.0.0 Utilize the getDataSetByName() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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++)
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::get_data_set('profiles');
0 ignored issues
show
Deprecated Code introduced by
The method DataSetFactory::get_data_set() has been deprecated with message: 2.0.0 Utilize the getDataSetByName() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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::get_data_set('profiles');
0 ignored issues
show
Deprecated Code introduced by
The method DataSetFactory::get_data_set() has been deprecated with message: 2.0.0 Utilize the getDataSetByName() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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::get_data_set('profiles');
0 ignored issues
show
Deprecated Code introduced by
The method DataSetFactory::get_data_set() has been deprecated with message: 2.0.0 Utilize the getDataSetByName() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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::get_data_set('profiles');
0 ignored issues
show
Deprecated Code introduced by
The method DataSetFactory::get_data_set() has been deprecated with message: 2.0.0 Utilize the getDataSetByName() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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