From 8fb8b9e81070f15fcbc91c4c81c2b7924d5f7342 Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Thu, 6 Jan 2011 15:45:19 +0100 Subject: [PATCH] Adding MyQCM view functional. Needs query improvements see BT#1631 --- main/exercice/exercise.lib.php | 39 ++++++++++++ main/inc/lib/display.lib.php | 39 ++++++++++-- main/session/index.php | 113 +++++++++++++++++++++------------ 3 files changed, 145 insertions(+), 46 deletions(-) diff --git a/main/exercice/exercise.lib.php b/main/exercice/exercise.lib.php index ce89113119..914765ddbc 100755 --- a/main/exercice/exercise.lib.php +++ b/main/exercice/exercise.lib.php @@ -1156,4 +1156,43 @@ function get_all_exercises($course_info = null, $session_id = 0) { return Database::select('*',$TBL_EXERCICES, $conditions); } +/** + * Gets the position of the score based in my score (result/weight) and the exe_id + */ +function get_exercise_result_ranking($my_score, $my_exe_id, $exercise_id, $course_code, $session_id = 0) { + //Getting all exercise results + $user_results = get_all_exercise_results($exercise_id, $course_code, $session_id); + if (empty($user_results)) { + return 1; + } else { + $ranking = 1; + $my_ranking = array(); + foreach($user_results as $result) { + //print_r($result); + if (empty($result['exe_weighting']) || $result['exe_weighting'] == '0.00') { + $my_ranking[$result['exe_id']] = 0; + } else { + $my_ranking[$result['exe_id']] = $result['exe_result']/$result['exe_weighting']; + } + } + asort($my_ranking); + $position = count($my_ranking); + + foreach($my_ranking as $exe_id=>$ranking) { + if ($my_score >= $ranking) { + if ($my_score == $ranking) { + if ($my_exe_id < $exe_id) { + $position--; + } + } else { + $position--; + } + } + } + //echo $position; + return $position; + } + +} + diff --git a/main/inc/lib/display.lib.php b/main/inc/lib/display.lib.php index 91df1213a4..569e15c53d 100755 --- a/main/inc/lib/display.lib.php +++ b/main/inc/lib/display.lib.php @@ -814,12 +814,15 @@ class Display { * @param array Visible columns (you should use get_lang). An array in which we place the names of the columns. This is the text that appears in the head of the grid (Header layer). Example: colname {name:'date', index:'date', width:120, align:'right'}, * @param array the column model : Array which describes the parameters of the columns.This is the most important part of the grid. For a full description of all valid values see colModel API. See the url above. * @param array extra parameters + * @param array data that will be loaded * @return string the js code * */ - public static function grid_js($div_id, $url, $column_names, $column_model, $extra_params) { + public static function grid_js($div_id, $url, $column_names, $column_model, $extra_params, $data = array()) { $obj = new stdClass(); - $obj->url = $url; + + if (!empty($url)) + $obj->url = $url; $obj->colNames = $column_names; $obj->colModel = $column_model; $obj->pager = $div_id.'_pager'; @@ -832,14 +835,20 @@ class Display { if (!empty($extra_params['sortname'])) { $obj->sortname = $extra_params['sortname']; } - $obj->sortorder = 'desc'; + //$obj->sortorder = 'desc'; if (!empty($extra_params['sortorder'])) { $obj->sortorder = $extra_params['sortorder']; } if (!empty($extra_params['rowList'])) { $obj->rowList = $extra_params['rowList']; - } + } + $obj->rowNum = 10; + if (!empty($extra_params['rowNum'])) { + $obj->rowNum = $extra_params['rowNum']; + } + + //height: 'auto', $obj->viewrecords = 'true'; if (!empty($extra_params['viewrecords'])) @@ -850,10 +859,28 @@ class Display { $obj->$key = $element; } } + + if (!empty($data)) { + $data_var = $div_id.'_data'; + $json.=' var '.$data_var.' = '.json_encode($data).';'; + /* $json.='for(var i=0;i<='.$data_var.'.length;i++) + jQuery("#'.$div_id.'").jqGrid(\'addRowData\',i+1,'.$data_var.'[i]);';*/ + $obj->data = $data_var; + $obj->datatype = 'local'; + $json.="\n"; + } + + $json_encode = json_encode($obj); + if (!empty($data)) { + //Converts the "data":"js_variable" to "data":js_variable + $json_encode = str_replace('"data":"'.$data_var.'"','"data":'.$data_var.'',$json_encode); + } - $json = '$("#'.$div_id.'").jqGrid('; - $json .= json_encode($obj); + $json .= '$("#'.$div_id.'").jqGrid('; + $json .= $json_encode; $json .= ');'; + + $json.="\n"; return $json; /* diff --git a/main/session/index.php b/main/session/index.php index cfc910a4f3..2fc128cfe0 100644 --- a/main/session/index.php +++ b/main/session/index.php @@ -78,29 +78,29 @@ foreach ($course_list as $item) { } -//Getting all sessions where I'm subscribed - +//Getting all sessions where I'm subscribed $new_session_list = UserManager::get_personal_session_course_list(api_get_user_id()); //echo '
';
+
 $my_session_list = array();
 $final_array = array();
 
 if (!empty($new_session_list)) {
     foreach($new_session_list as $item) {
         $my_session_id = $item['id_session'];    
-        if (isset($my_session_id) && !in_array($my_session_id, $my_session_list)) {
+        if (isset($my_session_id) && !in_array($my_session_id, $my_session_list) && $session_id == $my_session_id) {
         	$final_array[$my_session_id]['name'] = $item['session_name'];
             
-         
+            //Get all courses by session where I'm subscribed
             $my_course_list = UserManager::get_courses_list_by_session(api_get_user_id(), $my_session_id );            
             $course = array();        
             foreach ($my_course_list as $my_course) {
             
-                $course_info    = api_get_course_info($my_course['code']);            
+                $course_info   = api_get_course_info($my_course['code']);
+                //Getting all exercises from the current course            
                 $exercise_list = get_all_exercises($course_info);
-           
-                
+                           
                 //Exercises we skip
                 /*if (empty($exercise_list)) {
                     continue;
@@ -112,11 +112,11 @@ if (!empty($new_session_list)) {
                         $exercise = new Exercise($course_info['real_id']);
                         $exercise->read($exercise_item['id']);                                    
                         $exercise_course_list[$exercise_item['id']] = $exercise;
+                        //Reading all Exercise results by user, exercise_id, code, and session
                         $user_results = get_all_exercise_results_by_user(api_get_user_id(), $exercise_item['id'], $my_course['code'], $my_session_id);
                         //print_r($user_results);
                         
-                        $course['exercises'][$exercise_item['id']]['name'] =  $exercise->exercise;
-                            
+                        $course['exercises'][$exercise_item['id']]['name'] =  $exercise->exercise;                            
                         $course['exercises'][$exercise_item['id']]['data'] =  $user_results;      
                         //print_r($user_results);          
                     }
@@ -131,14 +131,20 @@ if (!empty($new_session_list)) {
 //print_r($final_array); exit;
 require_once api_get_path(LIBRARY_PATH).'pear/HTML/Table.php';
 $html = '';
-foreach($final_array as $session_data) {    
+//Final data to be show
+$my_real_array =array();
+foreach($final_array as $session_data) {
+    //Session name    
 	$html .=Display::tag('h1',$session_data['name']);
     $course_list = $session_data['data'];         
-    foreach ($course_list as $course_data) {        
+    foreach ($course_list as $my_course_code=>$course_data) {
+        //Course table        
         $table = new HTML_Table(array('class' => 'data_table'));
         $row = 0;
         $column = 0;
-        $header_names = array(get_lang('Course'),get_lang('Exercise'),get_lang('Attempt'),get_lang('Results'),get_lang('Score'), get_lang('Ranking'));
+        
+        //Course headers
+        /*$header_names = array(get_lang('Course'),get_lang('Exercise'),get_lang('Attempt'),get_lang('Results'),get_lang('Score'), get_lang('Ranking'));
         foreach ($header_names as $item) {
             $table->setHeaderContents($row, $column, $item);
             $column++;
@@ -146,16 +152,38 @@ foreach($final_array as $session_data) {
         $row = 1;
         $column = 0;
         $table->setCellContents($row, $column, $course_data['name']);
-        $column++;           
-        if (!empty($course_data['exercises'])) {            
-            foreach ($course_data['exercises'] as $exercise_data) {                                                  
+        $column++;*/           
+        if (!empty($course_data['exercises'])) {
+            //Exercises            
+            foreach ($course_data['exercises'] as $my_exercise_id => $exercise_data) {
+                //Exercise results                              
+                $counter = 1;                    
                 foreach ($exercise_data['data'] as $exercise_result) {                    
                     $my_exercise_result = array($exercise_data['name'], $exercise_result['exe_id']);
                     $column = 1;
-                    foreach ($my_exercise_result as $data) {                                                
+                    //$exercise_result['exe_id']
+                    /*print_r($exercise_result);
+                    exe_weighting
+                    exe_result
+                    exe_id*/
+                    $score          = $exercise_result['exe_result'].' / '.$exercise_result['exe_weighting'];
+                    $platform_score = show_score($exercise_result['exe_result'], $exercise_result['exe_weighting'], false);
+                    if (!empty($exercise_result['exe_weighting'])) {
+                        $my_score = $exercise_result['exe_result']/$exercise_result['exe_weighting'];
+                    } else {
+                    	$my_score = 0;
+                    }
+                    $position       = get_exercise_result_ranking($my_score, $exercise_result['exe_id'], $my_exercise_id,  $my_course_code,$session_id);
+                    $my_real_array[]= array('course'=>$course_data['name'], 'exercise'=>$exercise_data['name'],'attempt'=>$counter,'result'=>$score,'note'=>$platform_score,'position'=>$position);
+                    $counter++;   
+                    foreach ($my_exercise_result as $data) {    
+                        
+                        //$my_real_array[]= array('session'=>$session_data['name'],'course'=>$course_data['name'], 'exercise'=>$exercise_data['name'],'result'=>$exercise_result['exe_id'])  ;
+                                                     
                         $table->setCellContents($row, $column, $data);                        
                         //$table->updateCellAttributes($row, $column, 'align="center"');
-                        $column++;                        
+                        $column++;   
+                                          
                     }
                     $row++;
                 }               
@@ -164,18 +192,7 @@ foreach($final_array as $session_data) {
         $html .=$table->toHtml();
     }
 }     
-
-
-
-//print_r($my_session_list);
-
-//Exercise list
-/*
-$exercise_grid_url            = api_get_path(WEB_AJAX_PATH).'course_home.ajax.php?a=session_courses_lp_default&session_id='.$session_id;
-$exercise_grid_columns        =array(get_lang('Session'), get_lang(''))
-$exercise_grid_column_model
-$exercise_grid_settings       =
-*/
+//echo '
';print_r($my_real_array) ;
 
 //Default grid settings
 $url            = api_get_path(WEB_AJAX_PATH).'course_home.ajax.php?a=session_courses_lp_default&session_id='.$session_id;
@@ -202,31 +219,47 @@ $extra_params_week['grouping'] = 'true';
 $extra_params_week['groupingView'] = array('groupField'=>array('week'),
                                             'groupColumnShow'=>'false',
                                             'groupText' => array('Week {0} - {1} Item(s)'));
+//MyQCM grid
+$column_exercise        = array(get_lang('Course'),get_lang('Exercise'), get_lang('Attempt').' #', get_lang('Result'), get_lang('Note'), get_lang('Position'));
+$column_exercise_model  = array(array('name'=>'course',     'index'=>'course',    'width'=>'450', 'align'=>'left','sortable'=>'false'),
+                                array('name'=>'exercise',   'index'=>'exercise',  'width'=>'250', 'align'=>'left', 'sortable'=>'false'),
+                                array('name'=>'attempt',    'index'=>'attempt',   'width'=>'50', 'align'=>'center', 'sortable'=>'false'),
+                                array('name'=>'result',     'index'=>'result',    'width'=>'50', 'align'=>'center', 'sortable'=>'false'),
+                                array('name'=>'note',       'index'=>'note',      'width'=>'50', 'align'=>'center', 'sortable'=>'false'),
+                                array('name'=>'position',   'index'=>'position',  'width'=>'50', 'align'=>'center', 'sortable'=>'false')
+                                );                        
+$extra_params_exercise['grouping'] = 'true';
+$extra_params_exercise['groupingView'] = array('groupField'=>array('course'),'groupColumnShow'=>'false','groupText' => array('Course {0} - {1} Item(s)'));
+                                           
 ?>