Issues (2128)

main/inc/lib/surveymanager.lib.php (2 issues)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Manage the "versioning" of a conditional survey.
6
 *
7
 *	@package chamilo.survey
8
 */
9
class SurveyTree
10
{
11
    public $surveylist;
12
    public $plainsurveylist;
13
    public $numbersurveys;
14
15
    /**
16
     * Sets the surveylist and the plainsurveylist.
17
     */
18
    public function __construct()
19
    {
20
        // Database table definitions
21
        $table_survey = Database::get_course_table(TABLE_SURVEY);
22
        $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
23
        $table_user = Database::get_main_table(TABLE_MAIN_USER);
24
25
        // searching
26
        $search_restriction = SurveyUtil::survey_search_restriction();
27
        if ($search_restriction) {
28
            $search_restriction = ' AND '.$search_restriction;
29
        }
30
31
        $course_id = api_get_course_int_id();
32
33
        $sql = "SELECT
34
                    survey.survey_id,
35
                    survey.parent_id,
36
                    survey_version,
37
                    survey.code as name
38
				FROM $table_survey survey
39
				LEFT JOIN $table_survey_question  survey_question
40
				ON survey.survey_id = survey_question.survey_id , $table_user user
41
				WHERE
42
					survey.c_id =  $course_id AND
43
					survey_question.c_id = $course_id AND
44
					survey.author = user.user_id
45
				GROUP BY survey.survey_id";
46
47
        $res = Database::query($sql);
48
        $refs = [];
49
        $list = [];
50
        $plain_array = [];
51
52
        while ($survey = Database::fetch_array($res, 'ASSOC')) {
53
            $plain_array[$survey['survey_id']] = $survey;
54
            $surveys_parents[] = $survey['survey_version'];
55
            $thisref = &$refs[$survey['survey_id']];
56
            $thisref['parent_id'] = $survey['parent_id'];
57
            $thisref['name'] = $survey['name'];
58
            $thisref['id'] = $survey['survey_id'];
59
            $thisref['survey_version'] = $survey['survey_version'];
60
            if ($survey['parent_id'] == 0) {
61
                $list[$survey['survey_id']] = &$thisref;
62
            } else {
63
                $refs[$survey['parent_id']]['children'][$survey['survey_id']] = &$thisref;
64
            }
65
        }
66
        $this->surveylist = $list;
67
        $this->plainsurveylist = $plain_array;
68
    }
69
70
    /**
71
     * This function gets the parent id of a survey.
72
     *
73
     * @param int $id survey id
74
     *
75
     * @return int survey parent id
76
     *
77
     * @author Julio Montoya <[email protected]>, Dokeos
78
     *
79
     * @version September 2008
80
     */
81
    public function getParentId($id)
82
    {
83
        $node = $this->plainsurveylist[$id];
84
        if (is_array($node) && !empty($node['parent_id'])) {
85
            return $node['parent_id'];
86
        } else {
87
            return -1;
88
        }
89
    }
90
91
    /**
92
     * This function creates a list of all surveys id.
93
     *
94
     * @param array $list of nodes
95
     *
96
     * @return array with the structure survey_id => survey_name
97
     *
98
     * @author Julio Montoya <[email protected]>
99
     *
100
     * @version September 2008
101
     */
102
    public function createList($list)
103
    {
104
        $result = [];
105
        if (is_array($list)) {
106
            foreach ($list as $key => $node) {
107
                if (isset($node['children']) && is_array($node['children'])) {
108
                    $result[$key] = $node['name'];
109
                    $re = self::createList($node['children']);
0 ignored issues
show
Bug Best Practice introduced by
The method SurveyTree::createList() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
                    /** @scrutinizer ignore-call */ 
110
                    $re = self::createList($node['children']);
Loading history...
110
                    if (!empty($re)) {
111
                        if (is_array($re)) {
112
                            foreach ($re as $key => $r) {
0 ignored issues
show
Comprehensibility Bug introduced by
$key is overwriting a variable from outer foreach loop.
Loading history...
113
                                $result[$key] = ''.$r;
114
                            }
115
                        } else {
116
                            $result[] = $re;
117
                        }
118
                    }
119
                } else {
120
                    $result[$key] = $node['name'];
121
                }
122
            }
123
        }
124
125
        return $result;
126
    }
127
}
128