| Conditions | 17 |
| Paths | 576 |
| Total Lines | 107 |
| Code Lines | 76 |
| 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 |
||
| 92 | public static function load($id = null, $user_id = null, $evaluation_id = null) |
||
| 93 | { |
||
| 94 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 95 | $tbl_grade_results = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT); |
||
| 96 | $tbl_course_rel_course = Database::get_main_table(TABLE_MAIN_COURSE_USER); |
||
| 97 | $tbl_session_rel_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 98 | $sessionId = api_get_session_id(); |
||
| 99 | $list_user_course_list = []; |
||
| 100 | |||
| 101 | if (is_null($id) && is_null($user_id) && !is_null($evaluation_id)) { |
||
| 102 | // Verified_if_exist_evaluation |
||
| 103 | $sql = 'SELECT COUNT(*) AS count |
||
| 104 | FROM '.$tbl_grade_results.' |
||
| 105 | WHERE evaluation_id="'.Database::escape_string($evaluation_id).'"'; |
||
| 106 | |||
| 107 | $result = Database::query($sql); |
||
| 108 | $existEvaluation = Database::result($result, 0, 0); |
||
| 109 | |||
| 110 | if ($existEvaluation != 0) { |
||
| 111 | if ($sessionId) { |
||
| 112 | $sql = 'SELECT c_id, user_id as user_id, status |
||
| 113 | FROM '.$tbl_session_rel_course_user.' |
||
| 114 | WHERE |
||
| 115 | status= 0 AND |
||
| 116 | c_id = "'.api_get_course_int_id().'" AND |
||
| 117 | session_id = '.$sessionId; |
||
| 118 | } else { |
||
| 119 | $sql = 'SELECT c_id, user_id, status |
||
| 120 | FROM '.$tbl_course_rel_course.' |
||
| 121 | WHERE status ="'.STUDENT.'" AND c_id = "'.api_get_course_int_id().'" '; |
||
| 122 | } |
||
| 123 | |||
| 124 | $res_course_rel_user = Database::query($sql); |
||
| 125 | while ($row_course_rel_user = Database::fetch_array($res_course_rel_user, 'ASSOC')) { |
||
| 126 | $list_user_course_list[] = $row_course_rel_user; |
||
| 127 | } |
||
| 128 | $current_date = api_get_utc_datetime(); |
||
| 129 | for ($i = 0; $i < count($list_user_course_list); $i++) { |
||
|
|
|||
| 130 | $sql_verified = 'SELECT COUNT(*) AS count |
||
| 131 | FROM '.$tbl_grade_results.' |
||
| 132 | WHERE |
||
| 133 | user_id="'.intval($list_user_course_list[$i]['user_id']).'" AND |
||
| 134 | evaluation_id="'.intval($evaluation_id).'";'; |
||
| 135 | $res_verified = Database::query($sql_verified); |
||
| 136 | $info_verified = Database::result($res_verified, 0, 0); |
||
| 137 | if ($info_verified == 0) { |
||
| 138 | $sql_insert = 'INSERT INTO '.$tbl_grade_results.'(user_id,evaluation_id,created_at,score) |
||
| 139 | VALUES ("'.intval($list_user_course_list[$i]['user_id']).'","'.intval($evaluation_id).'","'.$current_date.'",0);'; |
||
| 140 | Database::query($sql_insert); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | $userIdList = []; |
||
| 147 | foreach ($list_user_course_list as $data) { |
||
| 148 | $userIdList[] = $data['user_id']; |
||
| 149 | } |
||
| 150 | $userIdListToString = implode("', '", $userIdList); |
||
| 151 | |||
| 152 | $sql = "SELECT lastname, gr.id, gr.user_id, gr.evaluation_id, gr.created_at, gr.score |
||
| 153 | FROM $tbl_grade_results gr |
||
| 154 | INNER JOIN $tbl_user u |
||
| 155 | ON gr.user_id = u.user_id "; |
||
| 156 | |||
| 157 | if (!empty($userIdList)) { |
||
| 158 | $sql .= " AND u.user_id IN ('$userIdListToString')"; |
||
| 159 | } |
||
| 160 | |||
| 161 | $paramcount = 0; |
||
| 162 | if (!empty($id)) { |
||
| 163 | $sql .= ' WHERE gr.id = '.intval($id); |
||
| 164 | $paramcount++; |
||
| 165 | } |
||
| 166 | if (!empty($user_id)) { |
||
| 167 | if ($paramcount != 0) { |
||
| 168 | $sql .= ' AND'; |
||
| 169 | } else { |
||
| 170 | $sql .= ' WHERE'; |
||
| 171 | } |
||
| 172 | $sql .= ' gr.user_id = '.intval($user_id); |
||
| 173 | $paramcount++; |
||
| 174 | } |
||
| 175 | if (!empty($evaluation_id)) { |
||
| 176 | if ($paramcount != 0) { |
||
| 177 | $sql .= ' AND'; |
||
| 178 | } else { |
||
| 179 | $sql .= ' WHERE'; |
||
| 180 | } |
||
| 181 | $sql .= ' gr.evaluation_id = '.intval($evaluation_id); |
||
| 182 | $paramcount++; |
||
| 183 | } |
||
| 184 | $sql .= ' ORDER BY u.lastname, u.firstname'; |
||
| 185 | |||
| 186 | $result = Database::query($sql); |
||
| 187 | $allres = []; |
||
| 188 | while ($data = Database::fetch_array($result)) { |
||
| 189 | $res = new Result(); |
||
| 190 | $res->set_id($data['id']); |
||
| 191 | $res->set_user_id($data['user_id']); |
||
| 192 | $res->set_evaluation_id($data['evaluation_id']); |
||
| 193 | $res->set_date(api_get_local_time($data['created_at'])); |
||
| 194 | $res->set_score($data['score']); |
||
| 195 | $allres[] = $res; |
||
| 196 | } |
||
| 197 | |||
| 198 | return $allres; |
||
| 199 | } |
||
| 316 |
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: