Conditions | 17 |
Paths | 576 |
Total Lines | 106 |
Code Lines | 75 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
90 | public static function load($id = null, $user_id = null, $evaluation_id = null) |
||
91 | { |
||
92 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
93 | $tbl_grade_results = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT); |
||
94 | $tbl_course_rel_course = Database::get_main_table(TABLE_MAIN_COURSE_USER); |
||
95 | $tbl_session_rel_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
96 | $sessionId = api_get_session_id(); |
||
97 | $list_user_course_list = []; |
||
98 | |||
99 | if (is_null($id) && is_null($user_id) && !is_null($evaluation_id)) { |
||
100 | // Verified_if_exist_evaluation |
||
101 | $sql = 'SELECT COUNT(*) AS count |
||
102 | FROM '.$tbl_grade_results.' |
||
103 | WHERE evaluation_id="'.Database::escape_string($evaluation_id).'"'; |
||
104 | |||
105 | $result = Database::query($sql); |
||
106 | $existEvaluation = Database::result($result, 0, 0); |
||
107 | |||
108 | if (0 != $existEvaluation) { |
||
109 | if ($sessionId) { |
||
110 | $sql = 'SELECT c_id, user_id as user_id, status |
||
111 | FROM '.$tbl_session_rel_course_user.' |
||
112 | WHERE |
||
113 | status= 0 AND |
||
114 | c_id = "'.api_get_course_int_id().'" AND |
||
115 | session_id = '.$sessionId; |
||
116 | } else { |
||
117 | $sql = 'SELECT c_id, user_id, status |
||
118 | FROM '.$tbl_course_rel_course.' |
||
119 | WHERE status ="'.STUDENT.'" AND c_id = "'.api_get_course_int_id().'" '; |
||
120 | } |
||
121 | |||
122 | $res_course_rel_user = Database::query($sql); |
||
123 | while ($row_course_rel_user = Database::fetch_array($res_course_rel_user, 'ASSOC')) { |
||
124 | $list_user_course_list[] = $row_course_rel_user; |
||
125 | } |
||
126 | $current_date = api_get_utc_datetime(); |
||
127 | for ($i = 0; $i < count($list_user_course_list); $i++) { |
||
|
|||
128 | $sql_verified = 'SELECT COUNT(*) AS count |
||
129 | FROM '.$tbl_grade_results.' |
||
130 | WHERE |
||
131 | user_id="'.intval($list_user_course_list[$i]['user_id']).'" AND |
||
132 | evaluation_id="'.intval($evaluation_id).'";'; |
||
133 | $res_verified = Database::query($sql_verified); |
||
134 | $info_verified = Database::result($res_verified, 0, 0); |
||
135 | if (0 == $info_verified) { |
||
136 | $sql_insert = 'INSERT INTO '.$tbl_grade_results.'(user_id,evaluation_id,created_at,score) |
||
137 | VALUES ("'.intval($list_user_course_list[$i]['user_id']).'","'.intval($evaluation_id).'","'.$current_date.'",0);'; |
||
138 | Database::query($sql_insert); |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | |||
144 | $userIdList = []; |
||
145 | foreach ($list_user_course_list as $data) { |
||
146 | $userIdList[] = $data['user_id']; |
||
147 | } |
||
148 | $userIdListToString = implode("', '", $userIdList); |
||
149 | |||
150 | $sql = "SELECT lastname, gr.id, gr.user_id, gr.evaluation_id, gr.created_at, gr.score |
||
151 | FROM $tbl_grade_results gr |
||
152 | INNER JOIN $tbl_user u |
||
153 | ON gr.user_id = u.user_id "; |
||
154 | |||
155 | if (!empty($userIdList)) { |
||
156 | $sql .= " AND u.user_id IN ('$userIdListToString')"; |
||
157 | } |
||
158 | |||
159 | $paramcount = 0; |
||
160 | if (!empty($id)) { |
||
161 | $sql .= ' WHERE gr.id = '.intval($id); |
||
162 | $paramcount++; |
||
163 | } |
||
164 | if (!empty($user_id)) { |
||
165 | if (0 != $paramcount) { |
||
166 | $sql .= ' AND'; |
||
167 | } else { |
||
168 | $sql .= ' WHERE'; |
||
169 | } |
||
170 | $sql .= ' gr.user_id = '.intval($user_id); |
||
171 | $paramcount++; |
||
172 | } |
||
173 | if (!empty($evaluation_id)) { |
||
174 | if (0 != $paramcount) { |
||
175 | $sql .= ' AND'; |
||
176 | } else { |
||
177 | $sql .= ' WHERE'; |
||
178 | } |
||
179 | $sql .= ' gr.evaluation_id = '.intval($evaluation_id); |
||
180 | } |
||
181 | $sql .= ' ORDER BY u.lastname, u.firstname'; |
||
182 | |||
183 | $result = Database::query($sql); |
||
184 | $allres = []; |
||
185 | while ($data = Database::fetch_array($result)) { |
||
186 | $res = new Result(); |
||
187 | $res->set_id($data['id']); |
||
188 | $res->set_user_id($data['user_id']); |
||
189 | $res->set_evaluation_id($data['evaluation_id']); |
||
190 | $res->set_date(api_get_local_time($data['created_at'])); |
||
191 | $res->set_score($data['score']); |
||
192 | $allres[] = $res; |
||
193 | } |
||
194 | |||
195 | return $allres; |
||
196 | } |
||
324 |
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: