From 0152bcb97abbdd587b0460da160d36204d1040de Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Fri, 6 Jun 2014 11:03:45 +0200 Subject: [PATCH] Minor - format code. --- main/gradebook/lib/fe/flatviewtable.class.php | 8 ++- .../lib/flatview_data_generator.class.php | 20 ++++-- .../lib/gradebook_data_generator.class.php | 58 +++++++-------- .../lib/gradebook_functions_users.inc.php | 60 +++++++++------- .../lib/results_data_generator.class.php | 70 ++++++++++--------- 5 files changed, 120 insertions(+), 96 deletions(-) diff --git a/main/gradebook/lib/fe/flatviewtable.class.php b/main/gradebook/lib/fe/flatviewtable.class.php index 9aaab91bba..df62421740 100755 --- a/main/gradebook/lib/fe/flatviewtable.class.php +++ b/main/gradebook/lib/fe/flatviewtable.class.php @@ -501,7 +501,13 @@ class FlatViewTable extends SortableTable $column++; } - $data_array = $this->datagen->get_data($users_sorting, $from, $this->per_page, $this->offset, $selectlimit); + $data_array = $this->datagen->get_data( + $users_sorting, + $from, + $this->per_page, + $this->offset, + $selectlimit + ); $table_data = array(); foreach ($data_array as $user_row) { diff --git a/main/gradebook/lib/flatview_data_generator.class.php b/main/gradebook/lib/flatview_data_generator.class.php index 58ff8d07bc..fdbc560b4c 100755 --- a/main/gradebook/lib/flatview_data_generator.class.php +++ b/main/gradebook/lib/flatview_data_generator.class.php @@ -152,6 +152,10 @@ class FlatViewDataGenerator return $headers; } + /** + * @param int $id + * @return int + */ public function get_max_result_by_link($id) { $max = 0; @@ -198,7 +202,7 @@ class FlatViewDataGenerator $ignore_score_color = false, $show_all = false ) { - // do some checks on users/items counts, redefine if invalid values + // Do some checks on users/items counts, redefine if invalid values if (!isset($users_count)) { $users_count = count ($this->users) - $users_start; } @@ -362,7 +366,9 @@ class FlatViewDataGenerator $temp_score = Display::tip($temp_score, $real_score); } - if (!isset($this->params['only_total_category']) || (isset($this->params['only_total_category']) && $this->params['only_total_category'] == false)) { + if (!isset($this->params['only_total_category']) || + (isset($this->params['only_total_category']) && $this->params['only_total_category'] == false) + ) { if (!$show_all) { $row[] = $temp_score.' '; } else { @@ -409,7 +415,9 @@ class FlatViewDataGenerator $temp_score = Display::tip($temp_score, $complete_score); } - if (!isset($this->params['only_total_category']) || (isset($this->params['only_total_category']) && $this->params['only_total_category'] == false)) { + if (!isset($this->params['only_total_category']) || + (isset($this->params['only_total_category']) && $this->params['only_total_category'] == false) + ) { if (!$show_all) { if (in_array($item->get_type() , array(LINK_EXERCISE, LINK_DROPBOX, LINK_STUDENTPUBLICATION, LINK_LEARNPATH, LINK_FORUM_THREAD, LINK_ATTENDANCE, LINK_SURVEY, LINK_HOTPOTATOES))) { @@ -445,9 +453,9 @@ class FlatViewDataGenerator } } unset($score); - //var_dump($row);exit; $data[] = $row; } + return $data; } @@ -457,7 +465,9 @@ class FlatViewDataGenerator public function get_evaluation_sumary_results ($session_id = null) { $usertable = array(); - foreach ($this->users as $user) { $usertable[] = $user; } + foreach ($this->users as $user) { + $usertable[] = $user; + } $selected_users = $usertable; // generate actual data array for all selected users diff --git a/main/gradebook/lib/gradebook_data_generator.class.php b/main/gradebook/lib/gradebook_data_generator.class.php index 231a4b2739..2159ffeaa8 100755 --- a/main/gradebook/lib/gradebook_data_generator.class.php +++ b/main/gradebook/lib/gradebook_data_generator.class.php @@ -1,10 +1,7 @@ items); } - + /** * Get actual array data * @return array 2-dimensional array - each array contains the elements: @@ -87,7 +83,7 @@ class GradebookDataGenerator if ($count < 0) { $count = 0; } - + $allitems = $this->items; // sort array if ($sorting & self :: GDG_SORT_TYPE) { @@ -112,10 +108,10 @@ class GradebookDataGenerator $user_id = api_get_user_id(); $course_code = api_get_course_id(); $status_user = api_get_status_of_user_in_course($user_id, $course_code); - + // generate the data to display $data = array(); - + foreach ($visibleitems as $item) { $row = array (); $row[] = $item; @@ -125,13 +121,13 @@ class GradebookDataGenerator $row[] = $item->get_weight(); /*if (api_is_allowed_to_edit(null, true)) { $row[] = $this->build_date_column($item); - }*/ + }*/ if (count($this->evals_links) > 0) { - if (!api_is_allowed_to_edit() || $status_user != 1 ) { + if (!api_is_allowed_to_edit() || $status_user != 1 ) { $row[] = $this->build_result_column($item, $ignore_score_color); $row[] = $item; } - } + } $data[] = $row; } return $data; @@ -142,7 +138,7 @@ class GradebookDataGenerator * returns an empty string. This only works with categories. * @param object Item */ - function get_certificate_link($item) + public function get_certificate_link($item) { if (is_a($item, 'Category')) { if($item->is_certificate_available(api_get_user_id())) { @@ -152,20 +148,21 @@ class GradebookDataGenerator } return ''; } + // Sort functions // Make sure to only use functions as defined in the GradebookItem interface ! - function sort_by_name($item1, $item2) + public function sort_by_name($item1, $item2) { return api_strnatcmp($item1->get_name(), $item2->get_name()); } - - function sort_by_id($item1, $item2) + + public function sort_by_id($item1, $item2) { return api_strnatcmp($item1->get_id(), $item2->get_id()); - } + } - function sort_by_type($item1, $item2) + public function sort_by_type($item1, $item2) { if ($item1->get_item_type() == $item2->get_item_type()) { return $this->sort_by_name($item1,$item2); @@ -174,7 +171,7 @@ class GradebookDataGenerator } } - function sort_by_description($item1, $item2) + public function sort_by_description($item1, $item2) { $result = api_strcmp($item1->get_description(), $item2->get_description()); if ($result == 0) { @@ -183,7 +180,7 @@ class GradebookDataGenerator return $result; } - function sort_by_weight($item1, $item2) + public function sort_by_weight($item1, $item2) { if ($item1->get_weight() == $item2->get_weight()) { return $this->sort_by_name($item1,$item2); @@ -192,7 +189,7 @@ class GradebookDataGenerator } } - function sort_by_date($item1, $item2) + public function sort_by_date($item1, $item2) { if (is_int($item1->get_date())) { $timestamp1 = $item1->get_date(); @@ -204,13 +201,13 @@ class GradebookDataGenerator $timestamp1 = null; } } - + if(is_int($item2->get_date())) { $timestamp2 = $item2->get_date(); } else { $timestamp2 = api_strtotime($item2->get_date(), 'UTC'); } - + if ($timestamp1 == $timestamp2) { return $this->sort_by_name($item1,$item2); } else { @@ -218,13 +215,12 @@ class GradebookDataGenerator } } - // Other functions private function build_result_column($item, $ignore_score_color) { - $scoredisplay = ScoreDisplay :: instance(); + $scoredisplay = ScoreDisplay :: instance(); $score = $item->calc_score(api_get_user_id()); - - if (!empty($score)) { + + if (!empty($score)) { switch ($item->get_item_type()) { // category case 'C' : @@ -241,7 +237,7 @@ class GradebookDataGenerator case 'E' : case 'L' : $displaytype = SCORE_DIV_PERCENT; - if ($ignore_score_color) { + if ($ignore_score_color) { $displaytype |= SCORE_IGNORE_SPLIT; } return $scoredisplay->display_score($score, SCORE_DIV_PERCENT_WITH_CUSTOM); @@ -256,7 +252,7 @@ class GradebookDataGenerator if (!isset($date) || empty($date)) { return ''; } else { - if(is_int($date)) { + if (is_int($date)) { return api_convert_and_format_date($date); } else { return api_format_date($date); diff --git a/main/gradebook/lib/gradebook_functions_users.inc.php b/main/gradebook/lib/gradebook_functions_users.inc.php index c5f9ca5ade..eca7d0d1ac 100755 --- a/main/gradebook/lib/gradebook_functions_users.inc.php +++ b/main/gradebook/lib/gradebook_functions_users.inc.php @@ -5,11 +5,13 @@ * @author Julio Montoya adding security functions * @package chamilo.gradebook */ + /** * returns users within a course given by param - * @param $course_id + * @param int $course_id */ -function get_users_in_course($course_id) { +function get_users_in_course($course_id) +{ $tbl_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER); $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER); $tbl_user = Database :: get_main_table(TABLE_MAIN_USER); @@ -26,34 +28,36 @@ function get_users_in_course($course_id) { AND scru.status=0 AND scru.course_code='$course_id' AND id_session ='$current_session' $order_clause "; } else { - $sql = 'SELECT user.user_id, user.username, lastname, firstname, official_code - FROM '.$tbl_course_user.' as course_rel_user, '.$tbl_user.' as user - WHERE course_rel_user.user_id=user.user_id AND - course_rel_user.status='.STUDENT.' AND + $sql = 'SELECT user.user_id, user.username, lastname, firstname, official_code + FROM '.$tbl_course_user.' as course_rel_user, '.$tbl_user.' as user + WHERE course_rel_user.user_id=user.user_id AND + course_rel_user.status='.STUDENT.' AND course_rel_user.course_code = "'.$course_id.'" '.$order_clause; - } + } $result = Database::query($sql); return get_user_array_from_sql_result($result); } -function get_user_array_from_sql_result($result) { - $a_students = array(); - while ($user = Database::fetch_array($result)) { - if (!array_key_exists($user['user_id'], $a_students)) { - $a_current_student = array (); - $a_current_student[] = $user['user_id']; - $a_current_student[] = $user['username']; - $a_current_student[] = $user['lastname']; - $a_current_student[] = $user['firstname']; - $a_current_student[] = $user['official_code']; - $a_students['STUD'.$user['user_id']] = $a_current_student; - } - } +function get_user_array_from_sql_result($result) +{ + $a_students = array(); + while ($user = Database::fetch_array($result)) { + if (!array_key_exists($user['user_id'], $a_students)) { + $a_current_student = array (); + $a_current_student[] = $user['user_id']; + $a_current_student[] = $user['username']; + $a_current_student[] = $user['lastname']; + $a_current_student[] = $user['firstname']; + $a_current_student[] = $user['official_code']; + $a_students['STUD'.$user['user_id']] = $a_current_student; + } + } return $a_students; } -function get_all_users ($evals = array(), $links = array()) { +function get_all_users ($evals = array(), $links = array()) +{ $coursecodes = array(); $users = array(); @@ -69,15 +73,15 @@ function get_all_users ($evals = array(), $links = array()) { $tbl_user = Database :: get_main_table(TABLE_MAIN_USER); $tbl_res = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT); - $sql = 'SELECT user.user_id,lastname, firstname, user.official_code - FROM '.$tbl_res.' as res, '.$tbl_user.' as user + $sql = 'SELECT user.user_id,lastname, firstname, user.official_code + FROM '.$tbl_res.' as res, '.$tbl_user.' as user WHERE res.evaluation_id = '.intval($eval->get_id()) .' AND res.user_id = user.user_id'; $result = Database::query($sql); $users = array_merge($users, get_user_array_from_sql_result($result)); } } - + foreach ($links as $link) { // links are always in a course $coursecode = $link->get_course_code(); @@ -94,7 +98,8 @@ function get_all_users ($evals = array(), $links = array()) { * Search students matching a given last name and/or first name * @author Bert Steppé */ -function find_students($mask= '') { +function find_students($mask= '') +{ // students shouldn't be here // don't search if mask empty if (!api_is_allowed_to_edit() || empty ($mask)) { return null; @@ -127,10 +132,11 @@ function find_students($mask= '') { * @deprecated replace this function with the api_get_user_info() * @return array All user information as an associative array */ -function get_user_info_from_id($userid) { +function get_user_info_from_id($userid) +{ $user_table= Database :: get_main_table(TABLE_MAIN_USER); $sql= 'SELECT * FROM ' . $user_table . ' WHERE user_id=' . intval($userid); $res= Database::query($sql); $user= Database::fetch_array($res, 'ASSOC'); return $user; -} \ No newline at end of file +} diff --git a/main/gradebook/lib/results_data_generator.class.php b/main/gradebook/lib/results_data_generator.class.php index dc075d479c..c91a7bee80 100755 --- a/main/gradebook/lib/results_data_generator.class.php +++ b/main/gradebook/lib/results_data_generator.class.php @@ -1,18 +1,15 @@ evaluation = $evaluation; $this->results = (isset($results) ? $results : array()); } - /** * Get total number of results (rows) */ - public function get_total_results_count () { + public function get_total_results_count () + { return count($this->results); } - /** * Get actual array data * @return array 2-dimensional array - each array contains the elements: @@ -58,8 +53,8 @@ class ResultsDataGenerator * 4 ['score'] : student's score * 5 ['display'] : custom score display (only if custom scoring enabled) */ - public function get_data ($sorting = 0, $start = 0, $count = null, $ignore_score_color = false, $pdf=false) { - + public function get_data ($sorting = 0, $start = 0, $count = null, $ignore_score_color = false, $pdf=false) + { // do some checks on count, redefine if invalid value $number_decimals = api_get_setting('gradebook_number_decimals'); if (!isset($count)) { @@ -86,13 +81,19 @@ class ResultsDataGenerator } else { $user['score'] = $this->get_score_display($result->get_score(),true, $ignore_score_color); } - $user['percentage_score'] = intval($scoredisplay->display_score(array($result->get_score(), $this->evaluation->get_max()), SCORE_PERCENT, SCORE_BOTH, true)); - if ($pdf && $number_decimals == null){ + $user['percentage_score'] = intval($scoredisplay->display_score( + array($result->get_score(), $this->evaluation->get_max()), + SCORE_PERCENT, + SCORE_BOTH, + true + ) + ); + if ($pdf && $number_decimals == null){ $user['scoreletter'] = $result->get_score(); - } - if ($scoredisplay->is_custom()) { - $user['display'] = $this->get_score_display($result->get_score(), false, $ignore_score_color); - } + } + if ($scoredisplay->is_custom()) { + $user['display'] = $this->get_score_display($result->get_score(), false, $ignore_score_color); + } $table[] = $user; } @@ -102,7 +103,7 @@ class ResultsDataGenerator usort($table, array('ResultsDataGenerator', 'sort_by_last_name')); } elseif ($sorting & self :: RDG_SORT_FIRSTNAME) { usort($table, array('ResultsDataGenerator', 'sort_by_first_name')); - } elseif ($sorting & self :: RDG_SORT_SCORE) { + } elseif ($sorting & self :: RDG_SORT_SCORE) { usort($table, array('ResultsDataGenerator', 'sort_by_score')); } elseif ($sorting & self :: RDG_SORT_MASK) { usort($table, array('ResultsDataGenerator', 'sort_by_mask')); @@ -110,7 +111,7 @@ class ResultsDataGenerator if ($sorting & self :: RDG_SORT_DESC) { $table = array_reverse($table); } - $return = array_slice($table, $start, $count); + $return = array_slice($table, $start, $count); return $return; } @@ -122,28 +123,32 @@ class ResultsDataGenerator * @param bool Whether we want to ignore the score color * @result string The score as we want to show it */ - private function get_score_display ($score, $realscore, $ignore_score_color = false) { + private function get_score_display ($score, $realscore, $ignore_score_color = false) + { if ($score != null) { $scoredisplay = ScoreDisplay :: instance(); $type = SCORE_CUSTOM; if ($realscore === true) { - $type = SCORE_DIV_PERCENT ; - } + $type = SCORE_DIV_PERCENT ; + } return $scoredisplay->display_score(array($score, $this->evaluation->get_max()), $type, SCORE_BOTH, $ignore_score_color); } - return ''; + return ''; } // Sort functions - used internally - function sort_by_last_name($item1, $item2) { + function sort_by_last_name($item1, $item2) + { return api_strcmp($item1['lastname'], $item2['lastname']); } - function sort_by_first_name($item1, $item2) { + function sort_by_first_name($item1, $item2) + { return api_strcmp($item1['firstname'], $item2['firstname']); } - function sort_by_score($item1, $item2) { + function sort_by_score($item1, $item2) + { if ($item1['percentage_score'] == $item2['percentage_score']) { return 0; } else { @@ -151,7 +156,8 @@ class ResultsDataGenerator } } - function sort_by_mask ($item1, $item2) { + function sort_by_mask ($item1, $item2) + { $score1 = (isset($item1['score']) ? array($item1['score'],$this->evaluation->get_max()) : null); $score2 = (isset($item2['score']) ? array($item2['score'],$this->evaluation->get_max()) : null); return ScoreDisplay :: compare_scores_by_custom_display($score1, $score2);