Issues (379)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

class/assessment_perguntas.php (49 issues)

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 41 and the first side effect is on line 32.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
// $Id: assessment_perguntas.php,v 1.10 2007/03/24 14:41:41 marcellobrandao Exp $
3
//  ------------------------------------------------------------------------ //
4
//                XOOPS - PHP Content Management System                      //
5
//                    Copyright (c) 2000 XOOPS.org                           //
6
//                       <http://www.xoops.org/>                             //
7
//  ------------------------------------------------------------------------ //
8
//  This program is free software; you can redistribute it and/or modify     //
9
//  it under the terms of the GNU General Public License as published by     //
10
//  the Free Software Foundation; either version 2 of the License, or        //
11
//  (at your option) any later version.                                      //
12
//                                                                           //
13
//  You may not change or alter any portion of this comment or credits       //
14
//  of supporting developers from this source code or any supporting         //
15
//  source code which is considered copyrighted (c) material of the          //
16
//  original comment or credit authors.                                      //
17
//                                                                           //
18
//  This program is distributed in the hope that it will be useful,          //
19
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21
//  GNU General Public License for more details.                             //
22
//                                                                           //
23
//  You should have received a copy of the GNU General Public License        //
24
//  along with this program; if not, write to the Free Software              //
25
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26
//  ------------------------------------------------------------------------ //
27
// assessment_perguntas.php,v 1
28
//  ---------------------------------------------------------------- //
29
// Author: Marcello Brandao                                            //
30
// ----------------------------------------------------------------- //
31
32
include_once XOOPS_ROOT_PATH . '/kernel/object.php';
33
include_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
34
//include_once("../class/assessment_respostas.php");
35
36
/**
37
 * assessment_perguntas class.
38
 * $this class is responsible for providing data access mechanisms to the data source
39
 * of XOOPS user class objects.
40
 */
41
class assessment_perguntas extends XoopsObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
42
{
43
    public $db;
44
45
    // constructor
46
47
    /**
48
     * @param null $id
49
     * @return assessment_perguntas
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
50
     */
51
    public function __construct($id = null)
52
    {
53
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
54
        $this->initVar('cod_pergunta', XOBJ_DTYPE_INT, null, false, 10);
55
        $this->initVar('cod_prova', XOBJ_DTYPE_INT, null, false, 10);
56
        $this->initVar('titulo', XOBJ_DTYPE_TXTBOX, null, false);
57
        $this->initVar('data_criacao', XOBJ_DTYPE_TXTBOX, null, false);
58
        $this->initVar('data_update', XOBJ_DTYPE_TXTBOX, null, false);
59
        $this->initVar('uid_elaborador', XOBJ_DTYPE_TXTBOX, null, false);
60
        $this->initVar('ordem', XOBJ_DTYPE_INT, null, false, 10);
61 View Code Duplication
        if (!empty($id)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
            if (is_array($id)) {
63
                $this->assignVars($id);
64
            } else {
65
                $this->load((int)$id);
66
            }
67
        } else {
68
            $this->setNew();
69
        }
70
    }
71
72
    /**
73
     * @param $id
74
     */
75 View Code Duplication
    public function load($id)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        $sql   = 'SELECT * FROM ' . $this->db->prefix('assessment_perguntas') . ' WHERE cod_pergunta=' . $id;
78
        $myrow = $this->db->fetchArray($this->db->query($sql));
79
        $this->assignVars($myrow);
80
        if (!$myrow) {
81
            $this->setNew();
82
        }
83
    }
84
85
    /**
86
     * @param array  $criteria
87
     * @param bool   $asobject
88
     * @param string $sort
89
     * @param string $order
90
     * @param int    $limit
91
     * @param int    $start
92
     *
93
     * @return array
94
     */
95 View Code Duplication
    public function getAllassessment_perguntass($criteria = array(), $asobject = false, $sort = 'cod_pergunta', $order = 'ASC', $limit = 0, $start = 0)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97
        $db          = XoopsDatabaseFactory::getDatabaseConnection();
98
        $ret         = array();
99
        $where_query = '';
100
        if (is_array($criteria) && count($criteria) > 0) {
101
            $where_query = ' WHERE';
102
            foreach ($criteria as $c) {
103
                $where_query .= " $c AND";
104
            }
105
            $where_query = substr($where_query, 0, -4);
106
        } elseif (!is_array($criteria) && $criteria) {
107
            $where_query = ' WHERE ' . $criteria;
108
        }
109
        if (!$asobject) {
110
            $sql    = 'SELECT cod_pergunta FROM ' . $db->prefix('assessment_perguntas') . "$where_query ORDER BY $sort $order";
111
            $result = $db->query($sql, $limit, $start);
112
            while ($myrow = $db->fetchArray($result)) {
113
                $ret[] = $myrow['assessment_perguntas_id'];
114
            }
115
        } else {
116
            $sql    = 'SELECT * FROM ' . $db->prefix('assessment_perguntas') . "$where_query ORDER BY $sort $order";
117
            $result = $db->query($sql, $limit, $start);
118
            while ($myrow = $db->fetchArray($result)) {
119
                $ret[] = new assessment_perguntas($myrow);
120
            }
121
        }
122
123
        return $ret;
124
    }
125
}
126
127
// -------------------------------------------------------------------------
128
// ------------------assessment_perguntas user handler class -------------------
129
// -------------------------------------------------------------------------
130
131
/**
132
 * assessment_perguntashandler class.
133
 * This class provides simple mecanisme for assessment_perguntas object
134
 */
135
class Xoopsassessment_perguntasHandler extends XoopsPersistableObjectHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
136
{
137
    /**
138
     * create a new assessment_perguntas
139
     *
140
     * @param bool $isNew flag the new objects as "new"?
141
     *
142
     * @return object assessment_perguntas
143
     */
144
    public function &create($isNew = true)
145
    {
146
        $assessment_perguntas = new assessment_perguntas();
147
        if ($isNew) {
148
            $assessment_perguntas->setNew();
149
        } //hack consertando
150
        else {
151
            $assessment_perguntas->unsetNew();
152
        }
153
154
        //fim do hack para consertar
155
        return $assessment_perguntas;
156
    }
157
158
    /**
159
     * retrieve a assessment_perguntas
160
     *
161
     * @param  mixed $id     ID
162
     * @param  array $fields fields to fetch
0 ignored issues
show
Should the type for parameter $fields not be array|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
163
     * @return XoopsObject {@link XoopsObject}
0 ignored issues
show
Should the return type not be false|assessment_perguntas?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
164
     */
165 View Code Duplication
    public function get($id = null, $fields = null)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
166
    {
167
        $sql = 'SELECT * FROM ' . $this->db->prefix('assessment_perguntas') . ' WHERE cod_pergunta=' . $id;
168
        if (!$result = $this->db->query($sql)) {
169
            return false;
170
        }
171
172
        $numrows = $this->db->getRowsNum($result);
173
        if ($numrows == 1) {
174
            $assessment_perguntas = new assessment_perguntas();
175
            $assessment_perguntas->assignVars($this->db->fetchArray($result));
176
177
            return $assessment_perguntas;
178
        }
179
180
        return false;
181
    }
182
183
    /**
184
     * insert a new assessment_perguntas in the database
185
     *
186
     * @param XoopsObject $assessment_perguntas reference to the {@link assessment_perguntas} object
187
     * @param bool        $force
188
     *
189
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
190
     */
191 View Code Duplication
    public function insert(XoopsObject $assessment_perguntas, $force = false)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
192
    {
193
        global $xoopsConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
194
        if (get_class($assessment_perguntas) != 'assessment_perguntas') {
195
            return false;
196
        }
197
        if (!$assessment_perguntas->isDirty()) {
198
            return true;
199
        }
200
        if (!$assessment_perguntas->cleanVars()) {
201
            return false;
202
        }
203
        foreach ($assessment_perguntas->cleanVars as $k => $v) {
204
            ${$k} = $v;
205
        }
206
        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
207
        if ($assessment_perguntas->isNew()) {
208
            // ajout/modification d'un assessment_perguntas
209
            $assessment_perguntas = new assessment_perguntas();
210
            $format               = 'INSERT INTO %s (cod_pergunta, cod_prova, titulo, data_criacao, data_update, uid_elaborador,ordem)';
211
            $format               .= 'VALUES (%u, %u, %s, %s, %s, %s, %u)';
212
            $sql                  = sprintf($format, $this->db->prefix('assessment_perguntas'), $cod_pergunta, $cod_prova, $this->db->quoteString($titulo), $now, $now, $this->db->quoteString($uid_elaborador), $ordem);
0 ignored issues
show
The variable $cod_pergunta seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
The variable $cod_prova does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $titulo does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $uid_elaborador does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $ordem does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
213
            $force                = true;
214
        } else {
215
            $format = 'UPDATE %s SET ';
216
            $format .= 'cod_pergunta=%u, cod_prova=%u, titulo=%s, data_criacao=%s, data_update=%s, uid_elaborador=%s, ordem=%u';
217
            $format .= ' WHERE cod_pergunta = %u';
218
            $sql    = sprintf($format, $this->db->prefix('assessment_perguntas'), $cod_pergunta, $cod_prova, $this->db->quoteString($titulo), $now, $now, $this->db->quoteString($uid_elaborador), $ordem, $cod_pergunta);
0 ignored issues
show
The variable $cod_pergunta seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
219
        }
220
        if (false != $force) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
221
            $result = $this->db->queryF($sql);
222
        } else {
223
            $result = $this->db->query($sql);
224
        }
225
        if (!$result) {
226
            return false;
227
        }
228
        if (empty($cod_pergunta)) {
0 ignored issues
show
The variable $cod_pergunta seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
229
            $cod_pergunta = $this->db->getInsertId();
230
        }
231
        $assessment_perguntas->assignVar('cod_pergunta', $cod_pergunta);
232
233
        return true;
234
    }
235
236
    /**
237
     * delete a assessment_perguntas from the database
238
     *
239
     * @param XoopsObject $assessment_perguntas reference to the assessment_perguntas to delete
240
     * @param bool        $force
241
     *
242
     * @return bool FALSE if failed.
243
     */
244 View Code Duplication
    public function delete(XoopsObject $assessment_perguntas, $force = false)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
245
    {
246
        if (get_class($assessment_perguntas) != 'assessment_perguntas') {
247
            return false;
248
        }
249
        $sql = sprintf('DELETE FROM %s WHERE cod_pergunta = %u', $this->db->prefix('assessment_perguntas'), $assessment_perguntas->getVar('cod_pergunta'));
250
        if (false != $force) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
251
            $result = $this->db->queryF($sql);
252
        } else {
253
            $result = $this->db->query($sql);
254
        }
255
        if (!$result) {
256
            return false;
257
        }
258
259
        return true;
260
    }
261
262
    /**
263
     * retrieve assessment_perguntass from the database
264
     *
265
     * @param CriteriaElement $criteria  {@link CriteriaElement} conditions to be met
0 ignored issues
show
Should the type for parameter $criteria not be null|CriteriaElement?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
266
     * @param bool            $id_as_key use the UID as key for the array?
267
     *
268
     * @param bool            $as_object
269
     * @return array array of <a href='psi_element://assessment_perguntas'>assessment_perguntas</a> objects
270
     *                                   objects
271
     */
272 View Code Duplication
    public function &getObjects(CriteriaElement $criteria = null, $id_as_key = false, $as_object = true)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
273
    {
274
        $ret   = array();
275
        $limit = $start = 0;
276
        $sql   = 'SELECT * FROM ' . $this->db->prefix('assessment_perguntas');
277
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
278
            $sql .= ' ' . $criteria->renderWhere();
279
            if ($criteria->getSort() != '') {
280
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
281
            }
282
            $limit = $criteria->getLimit();
283
            $start = $criteria->getStart();
284
        }
285
        $result = $this->db->query($sql, $limit, $start);
286
        if (!$result) {
287
            return $ret;
288
        }
289
        while ($myrow = $this->db->fetchArray($result)) {
290
            $assessment_perguntas = new assessment_perguntas();
291
            $assessment_perguntas->assignVars($myrow);
292
            if (!$id_as_key) {
293
                $ret[] = $assessment_perguntas;
294
            } else {
295
                $ret[$myrow['cod_pergunta']] = $assessment_perguntas;
296
            }
297
            unset($assessment_perguntas);
298
        }
299
300
        return $ret;
301
    }
302
303
    /**
304
     * retrieve assessment_perguntass from the database
305
     *
306
     * @param object $criteria  {@link CriteriaElement} conditions to be met
0 ignored issues
show
Should the type for parameter $criteria not be object|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
307
     * @param bool   $id_as_key use the UID as key for the array?
308
     *
309
     * @return array array of {@link assessment_perguntas} objects
310
     */
311
    public function &getCodObjects($criteria = null, $id_as_key = false)
312
    {
313
        $ret   = array();
314
        $limit = $start = 0;
315
        $sql   = 'SELECT cod_pergunta FROM ' . $this->db->prefix('assessment_perguntas');
316
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
317
            $sql .= ' ' . $criteria->renderWhere();
318
            if ($criteria->getSort() != '') {
319
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
320
            }
321
            $limit = $criteria->getLimit();
322
            $start = $criteria->getStart();
323
        }
324
        $result = $this->db->query($sql, $limit, $start);
325
        while ($myrow = $this->db->fetchArray($result)) {
326
            $ret[] = $myrow['cod_pergunta'];
327
        }
328
329
        return $ret;
330
    }
331
332
    /**
333
     * count assessment_perguntass matching a condition
334
     *
335
     * @param CriteriaElement $criteria {@link CriteriaElement} to match
0 ignored issues
show
Should the type for parameter $criteria not be null|CriteriaElement?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
336
     *
337
     * @return int count of assessment_perguntass
338
     */
339 View Code Duplication
    public function getCount(CriteriaElement $criteria = null)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
340
    {
341
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('assessment_perguntas');
342
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
343
            $sql .= ' ' . $criteria->renderWhere();
344
        }
345
        $result = $this->db->query($sql);
346
        if (!$result) {
347
            return 0;
348
        }
349
        list($count) = $this->db->fetchRow($result);
350
351
        return $count;
352
    }
353
354
    /**
355
     * delete assessment_perguntass matching a set of conditions
356
     *
357
     * @param CriteriaElement $criteria {@link CriteriaElement}
0 ignored issues
show
Should the type for parameter $criteria not be null|CriteriaElement?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
358
     *
359
     * @param bool            $force
360
     * @param bool            $asObject
361
     * @return bool FALSE if deletion failed
362
     */
363 View Code Duplication
    public function deleteAll(CriteriaElement $criteria = null, $force = true, $asObject = false)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
364
    {
365
        $sql = 'DELETE FROM ' . $this->db->prefix('assessment_perguntas');
366
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
367
            $sql .= ' ' . $criteria->renderWhere();
368
        }
369
        if (!$result = $this->db->query($sql)) {
370
            return false;
371
        }
372
373
        return true;
374
    }
375
376
    /**
377
     * cria form de inser��o e edi��o de pergunta
378
     *
379
     * @param string $action caminho para arquivo que ...
380
     * @param null   $prova
381
     * @return bool FALSE if failed
382
     * @internal param object $assessment_prova <a href='psi_element://assessment_pprova'>assessment_pprova</a>
383
     *
384
     */
385
    public function renderFormCadastrar($action, $prova = null)
386
    {
387
        $form              = new XoopsThemeForm(_AM_ASSESSMENT_CADASTRAR . ' ' . _AM_ASSESSMENT_PERGUNTA, 'form_pergunta', $action, 'post', true);
388
        $campo_titulo      = new XoopsFormTextArea(_AM_ASSESSMENT_TITULO, 'campo_titulo', '', 2, 50);
389
        $campo_ordem       = new XoopsFormText(_AM_ASSESSMENT_ORDEM, 'campo_ordem', 3, 3, '0');
390
        $cod_prova         = $prova->getVar('cod_prova');
0 ignored issues
show
The method getVar cannot be called on $prova (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
391
        $titulo_prova      = $prova->getVar('titulo');
0 ignored issues
show
The method getVar cannot be called on $prova (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
392
        $campo_prova_label = new XoopsFormLabel(_AM_ASSESSMENT_PROVA, $titulo_prova);
393
        $campo_prova_valor = new XoopsFormHidden('campo_cod_prova', $cod_prova);
394
        $campo_resposta1   = new XoopsFormTextArea(_AM_ASSESSMENT_RESPOSTA . ' 1 <br />(correta)', 'campo_resposta1', '', 2, 50);
395
        $campo_resposta1->setExtra('style="background-color:#ECFFEC"');
396
        $campo_resposta2 = new XoopsFormTextArea(_AM_ASSESSMENT_RESPOSTA . ' 2 - (errada)', 'campo_resposta2', '', 2, 50);
397
        $campo_resposta2->setExtra('style="background-color:#FFF0F0"');
398
        $campo_resposta3 = new XoopsFormTextArea(_AM_ASSESSMENT_RESPOSTA . ' 3 - (errada)', 'campo_resposta3', '', 2, 50);
399
        $campo_resposta3->setExtra('style="background-color:#FFF0F0"');
400
        $campo_resposta4 = new XoopsFormTextArea(_AM_ASSESSMENT_RESPOSTA . ' 4 - (errada)', 'campo_resposta4', '', 2, 50);
401
        $campo_resposta4->setExtra('style="background-color:#FFF0F0"');
402
        $campo_resposta5 = new XoopsFormTextArea(_AM_ASSESSMENT_RESPOSTA . ' 5 - (errada)', 'campo_resposta5', '', 2, 50);
403
        $campo_resposta5->setExtra('style="background-color:#FFF0F0"');
404
        $botao_enviar = new XoopsFormButton(_AM_ASSESSMENT_CADASTRAR, 'botao_submit', _SUBMIT, 'submit');
405
        $form->addElement($campo_prova_label);
406
        $form->addElement($campo_prova_valor);
407
        $form->addElement($campo_ordem, true);
408
        $form->addElement($campo_titulo, true);
409
        $form->addElement($campo_resposta1, true);
410
        $form->addElement($campo_resposta2, true);
411
        $form->addElement($campo_resposta3, true);
412
        $form->addElement($campo_resposta4, true);
413
        $form->addElement($campo_resposta5, true);
414
        $form->addElement($botao_enviar);
415
        $form->display();
416
417
        return true;
418
    }
419
420
    /**
421
     * @param       $action
422
     * @param       $pergunta
423
     * @param array $respostas
424
     *
425
     * @return bool
426
     */
427
    public function renderFormEditar($action, $pergunta, $respostas = array())
428
    {
429
        $cod_prova    = $pergunta->getVar('cod_prova');
0 ignored issues
show
$cod_prova is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
430
        $titulo       = $pergunta->getVar('titulo');
431
        $cod_pergunta = $pergunta->getVar('cod_pergunta');
432
        $ordem        = $pergunta->getVar('ordem');
433
434
        $form = new XoopsThemeForm(_AM_ASSESSMENT_EDITAR . ' ' . _AM_ASSESSMENT_PERGUNTA, 'form_pergunta', $action, 'post', true);
435
436
        $campo_ordem = new XoopsFormText(_AM_ASSESSMENT_ORDEM, 'campo_ordem', 3, 3, $ordem);
437
        $form->addElement($campo_ordem, true);
438
        $campo_titulo = new XoopsFormTextArea(_AM_ASSESSMENT_PERGUNTA, 'campo_titulo', $titulo, 2, 50);
439
        $form->addElement($campo_titulo, true);
440
        $botao_enviar       = new XoopsFormButton('', 'botao_submit', _AM_ASSESSMENT_SALVARALTERACOES, 'submit');
441
        $campo_cod_pergunta = new XoopsFormHidden('campo_cod_pergunta', $cod_pergunta);
442
443
        $i = 1;
444
        foreach ($respostas as $resposta) {
445
            $titulo_resposta            = $resposta->getVar('titulo');
446
            $cod_resposta               = $resposta->getVar('cod_resposta');
447
            $nome_campo_titulo_resposta = 'campo_resposta' . $i;
448
            if ($resposta->getVar('iscerta') == 1) {
449
                $resposta_correta = new XoopsFormTextArea(_AM_ASSESSMENT_RESPCORRETA . $i, $nome_campo_titulo_resposta, $titulo_resposta, 2, 50);
450
                $resposta_correta->setExtra('style="background-color:#ECFFEC"');
451
                $cod_resposta_correta = new XoopsFormHidden('campo_cod_resp1', $cod_resposta);
452
                $form->addElement($cod_resposta_correta, true);
453
                $form->addElement($resposta_correta, true);
454
            } else {
455
                $vetor_respostas_erradas[$i] = new XoopsFormTextArea(_AM_ASSESSMENT_RESPOSTA . $i, $nome_campo_titulo_resposta, $titulo_resposta, 2, 50);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$vetor_respostas_erradas was never initialized. Although not strictly required by PHP, it is generally a good practice to add $vetor_respostas_erradas = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
456
                $vetor_respostas_erradas[$i]->setExtra('style="background-color:#FFF0F0"');
0 ignored issues
show
The variable $vetor_respostas_erradas does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
457
                $vetor_cod_respostas_erradas[$i] = new XoopsFormHidden('campo_cod_resp' . $i, $cod_resposta);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$vetor_cod_respostas_erradas was never initialized. Although not strictly required by PHP, it is generally a good practice to add $vetor_cod_respostas_erradas = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
458
                $form->addElement($vetor_respostas_erradas[$i], true);
459
                $form->addElement($vetor_cod_respostas_erradas[$i], true);
0 ignored issues
show
The variable $vetor_cod_respostas_erradas does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
460
            }
461
            ++$i;
462
        }
463
464
        $form->addElement($campo_prova_valor, true);
0 ignored issues
show
The variable $campo_prova_valor does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
465
        $form->addElement($campo_cod_pergunta, true);
466
467
        $form->addElement($botao_enviar);
468
        $form->display();
469
470
        return true;
471
    }
472
473
    /**
474
     * @param       $action
475
     * @param       $pergunta
476
     * @param array $respostas
477
     * @param int   $param_cod_resposta
478
     *
479
     * @return XoopsThemeForm
480
     */
481
    public function renderFormResponder($action, $pergunta, $respostas = array(), $param_cod_resposta = 0)
0 ignored issues
show
renderFormResponder uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
482
    {
483
        global $_GET;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
484
        $start        = $_GET['start'];
485
        $cod_prova    = $pergunta->getVar('cod_prova');
0 ignored issues
show
$cod_prova is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
486
        $titulo       = $pergunta->getVar('titulo');
487
        $cod_pergunta = $pergunta->getVar('cod_pergunta');
488
        $form         = new XoopsThemeForm("$titulo", 'form_resposta', $action, 'post', true);
489
490
        $botao_enviar       = new XoopsFormButton('', 'botao_submit', _SUBMIT, 'submit');
491
        $campo_cod_pergunta = new XoopsFormHidden('cod_pergunta', $cod_pergunta);
492
        $campo_start        = new XoopsFormHidden('start', $start);
493
        $campo_respostas    = new XoopsFormRadio(_MA_ASSESSMENT_RESPOSTA, 'cod_resposta', '','<br/>');
494
        shuffle($respostas);
495
496
        $campo_respostas->setValue($param_cod_resposta);
497
        foreach ($respostas as $resposta) {
498
            $titulo_resposta = $resposta->getVar('titulo');
499
            $cod_resposta    = $resposta->getVar('cod_resposta');
500
            $campo_respostas->addOption($cod_resposta, $titulo_resposta . '<br />');
501
        }
502
        //$form->addElement($campo_prova_valor,true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
89% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
503
        $form->addElement($campo_cod_pergunta);
504
        $form->addElement($campo_respostas, true);
505
        $form->addElement($campo_start);
506
507
        $form->addElement($botao_enviar);
508
509
        //$form->display();
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
510
        return $form;
511
    }
512
513
    /**
514
     * @param $db
515
     *
516
     * @return mixed
517
     */
518
    public function pegarultimocodigo(&$db)
519
    {
520
        return $db->getInsertId();
521
    }
522
523
    /**
524
     * Copia as perguntas e salva elas ligadas � prova clone
525
     *
526
     * @param object $criteria {@link CriteriaElement} to match
527
     *
528
     * @param        $cod_prova
529
     * @return int count of assessment_perguntass
0 ignored issues
show
Should the return type not be integer|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
530
     */
531
    public function clonarPerguntas($criteria, $cod_prova)
532
    {
533
        global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
534
        $fabrica_de_respostas = new Xoopsassessment_respostasHandler($xoopsDB);
535
        $perguntas            = $this->getObjects($criteria);
536
        foreach ($perguntas as $pergunta) {
537
            $cod_pergunta = $pergunta->getVar('cod_pergunta');
538
            $pergunta->setVar('cod_prova', $cod_prova);
539
            $pergunta->setVar('cod_pergunta', 0);
540
            $pergunta->setNew();
541
            $this->insert($pergunta);
542
            $cod_pergunta_clone = $xoopsDB->getInsertId();
543
544
            $criteria_pergunta = new Criteria('cod_pergunta', $cod_pergunta);
545
            $respostas         = $fabrica_de_respostas->getObjects($criteria_pergunta);
546
547
            foreach ($respostas as $resposta) {
548
                $resposta->setVar('cod_pergunta', $cod_pergunta_clone);
549
                $resposta->setVar('cod_resposta', 0);
550
                $resposta->setNew();
551
                $fabrica_de_respostas->insert($resposta);
552
            }
553
        }
554
    }
555
}
556