Browse Source

Adding category report see #5716

Julio Montoya 12 years ago
parent
commit
109a234db5

+ 6 - 8
main/exercice/exercise.class.php

@@ -1587,12 +1587,11 @@ class Exercise {
 
 	public function show_button($question_id, $questionNum) {	
 		global $origin, $safe_lp_id, $safe_lp_item_id, $safe_lp_item_view_id;
-		$nbrQuestions = count($this->get_validated_question_list());		
+        $question_list = $this->get_validated_question_list();
+		$nbrQuestions = count($question_list);
 		 
-		$html = $label = '';
-		$confirmation_alert = $this->type == ALL_ON_ONE_PAGE? " onclick=\"javascript:if(!confirm('".get_lang("ConfirmYourChoice")."')) return false;\" ":"";
-		$hotspot_get = isset($_POST['hotspot']) ? Security::remove_XSS($_POST['hotspot']):null;
-		$all_button = '';
+		$all_button = $html = $label = '';		
+		$hotspot_get = isset($_POST['hotspot']) ? Security::remove_XSS($_POST['hotspot']):null;		 
 	
 		if ($this->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT && $this->type == ONE_PER_PAGE) {            
 			$html .='<a href="exercise_submit_modal.php?learnpath_id='.$safe_lp_id.'&learnpath_item_id='.$safe_lp_item_id.'&learnpath_item_view_id='.$safe_lp_item_view_id.'&origin='.$origin.'&hotspot='.$hotspot_get.'&nbrQuestions='.$nbrQuestions.'&num='.$questionNum.'&exerciseType='.$this->type.'&exerciseId='.$this->id.'&placeValuesBeforeTB_=savedValues&TB_iframe=true&height=480&width=640&modal=true" title="" class="thickbox btn">';
@@ -1617,9 +1616,8 @@ class Exercise {
 					$label = get_lang('NextQuestion');
 					$class = 'btn btn-primary';
 				}
-				if ($this->type == ONE_PER_PAGE) {	
-
-					if ($questionNum != 1) {
+				if ($this->type == ONE_PER_PAGE) {
+					if ($questionNum != 1) {                        
 						$prev_question = $questionNum - 2;
 						$all_button .= '<a href="javascript://" class="btn" onclick="previous_question_and_save('.$prev_question.', '.$question_id.' ); ">'.get_lang('PreviousQuestion').'</a>';
 					}

+ 51 - 16
main/exercice/exercise.lib.php

@@ -857,7 +857,6 @@ function get_exam_results_hotpotatoes_data($in_from, $in_number_of_items, $in_co
  * @todo this function should be moved in a library  + no global calls 
  */
 function get_exam_results_data($from, $number_of_items, $column, $direction, $exercise_id, $extra_where_conditions = null, $get_count = false) {
-    
                         
     //@todo replace all this globals
     global $documentPath, $filter;
@@ -1231,7 +1230,7 @@ function get_exam_results_data($from, $number_of_items, $column, $direction, $ex
  * @param	 bool	use or not the platform settings
  * @return  string  an html with the score modified
  */
-function show_score($score, $weight, $show_percentage = true, $use_platform_settings = true) {
+function show_score($score, $weight, $show_percentage = true, $use_platform_settings = true, $show_only_percentage = false) {
     if (is_null($score) && is_null($weight)) {
         return '-';
     }
@@ -1242,11 +1241,11 @@ function show_score($score, $weight, $show_percentage = true, $use_platform_sett
     if ($use_platform_settings) {
         if ($max_note != '' && $min_note != '') {        
             if (!empty($weight) && intval($weight) != 0) {
-    	       $score        = $min_note + ($max_note - $min_note) * $score /$weight;
+    	       $score = $min_note + ($max_note - $min_note) * $score /$weight;
             } else {
-               $score        = $min_note;
+               $score = $min_note;
             }            
-            $weight         = $max_note;
+            $weight = $max_note;
         }
     }
     $score_rounded = float_format($score, 1);    
@@ -1254,10 +1253,13 @@ function show_score($score, $weight, $show_percentage = true, $use_platform_sett
     
     $percentage = float_format(($score / ($weight != 0 ? $weight : 1)) * 100, 1);
     
-    $html  = '';
+    $html  = null;
     if ($show_percentage) {        
         $parent = '(' . $score_rounded . ' / ' . $weight . ')';
-        $html = $percentage." %  $parent";	
+        $html = $percentage."%  $parent";
+        if ($show_only_percentage) {
+            $html = $percentage."% ";
+        }
     } else {    
     	$html = $score_rounded . ' / ' . $weight;
     }  
@@ -1587,8 +1589,7 @@ function get_best_attempt_by_user($user_id, $exercise_id, $course_code, $session
  * @return 	float	Average score
  */
 function get_average_score($exercise_id, $course_code, $session_id) { 
-    $user_results = get_all_exercise_results($exercise_id, $course_code, $session_id);
-    $avg_score_data = array();    
+    $user_results = get_all_exercise_results($exercise_id, $course_code, $session_id);    
     $avg_score = 0;
     if (!empty($user_results)) {        
         foreach($user_results as $result) {
@@ -2046,8 +2047,8 @@ function display_question_list_by_attempt($objExercise, $exe_id, $save_user_resu
     
     //Getting attempt info
     $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exe_id);
-    $question_list = array();
     
+    $question_list = array();    
     if (!empty($exercise_stat_info['data_tracking'])) {
         $question_list = explode(',', $exercise_stat_info['data_tracking']);
     } else {        
@@ -2079,7 +2080,7 @@ function display_question_list_by_attempt($objExercise, $exe_id, $save_user_resu
         $user_info   = api_get_user_info($exercise_stat_info['exe_user_id']);
         //Shows exercise header        
         $exercise_date = $exercise_stat_info['start_date'];        
-        echo $objExercise->show_exercise_result_header(api_get_person_name($user_info['firstName'], $user_info['lastName']), api_convert_and_format_date($exercise_date, DATE_TIME_FORMAT_LONG));
+        echo $objExercise->show_exercise_result_header($user_info['complete_name'], api_convert_and_format_date($exercise_date, DATE_TIME_FORMAT_LONG));
     }
     
     if ($save_user_result) {    
@@ -2094,6 +2095,8 @@ function display_question_list_by_attempt($objExercise, $exe_id, $save_user_resu
     }
     
     $question_list_answers = array();
+    
+    $category_list = array();
 
     // Loop over all question to show results for each of them, one by one
     if (!empty($question_list)) {
@@ -2105,7 +2108,6 @@ function display_question_list_by_attempt($objExercise, $exe_id, $save_user_resu
 
             //this variable commes from exercise_submit_modal.php
 
-            //$hotspot_delineation_result = $_SESSION['hotspot_delineation_result'][$objExercise->selectId()][$quesId];
             ob_start();
             
             // We're inside *one* question. Go through each possible answer for this question        
@@ -2114,11 +2116,38 @@ function display_question_list_by_attempt($objExercise, $exe_id, $save_user_resu
             $total_score     += $result['score'];
             $total_weight    += $result['weight'];
             
-            $question_list_answers[] = array('question' => $result['open_question'], 'answer' => $result['open_answer']);            
+            $question_list_answers[] = array(
+                'question' => $result['open_question'], 
+                'answer' => $result['open_answer']
+            );
             
             $my_total_score  = $result['score'];
-            $my_total_weight = $result['weight'];   
-
+            $my_total_weight = $result['weight'];
+            
+            
+            //Category report
+            $category_was_added_for_this_test = false;
+            
+            if (isset($objQuestionTmp->category) && !empty($objQuestionTmp->category)) {
+                $category_list[$objQuestionTmp->category]['score'] += $my_total_score;
+                $category_list[$objQuestionTmp->category]['total'] += $my_total_weight;
+                $category_was_added_for_this_test = true;
+            } 
+            
+            if (isset($objQuestionTmp->category_list) && !empty($objQuestionTmp->category_list)) {
+                foreach($objQuestionTmp->category_list as $category_id) {
+                    $category_list[$category_id]['score'] += $my_total_score;
+                    $category_list[$category_id]['total'] += $my_total_weight;
+                    $category_was_added_for_this_test = true;
+                }
+            }
+            
+            //No category for this question!
+            if ($category_was_added_for_this_test == false) {
+                $category_list['none']['score'] += $my_total_score;
+                $category_list['none']['total'] += $my_total_weight;
+            }            
+   
             if ($objExercise->selectPropagateNeg() == 0 && $my_total_score < 0) {
                 $my_total_score = 0;
             }
@@ -2195,7 +2224,13 @@ function display_question_list_by_attempt($objExercise, $exe_id, $save_user_resu
             $total_score_text .= '</div>';
             $total_score_text .= '</div>';
         }
-    }
+    }    
+    
+    if (!empty($category_list) && ($show_results || $show_only_score) ) {
+        //Adding total        
+        $category_list['total'] = array('score' => $total_score, 'total' => $total_weight);
+        echo Testcategory::get_stats_table_by_attempt($objExercise->id, $category_list);
+    }    
     
     echo $total_score_text;   
     echo $exercise_content;    

+ 35 - 12
main/exercice/exercise_show.php

@@ -167,8 +167,6 @@ $show_only_total_score  = false;
 
 // Avoiding the "Score 0/0" message  when the exe_id is not set
 if (!empty($track_exercise_info)) {
-	$exerciseTitle			= $track_exercise_info['title'];
-	$exerciseDescription	= $track_exercise_info['description'];
 	// if the results_disabled of the Quiz is 1 when block the script
 	$result_disabled		= $track_exercise_info['results_disabled'];
 	
@@ -234,9 +232,7 @@ $query = "SELECT attempts.question_id, answer FROM ".$TBL_TRACK_ATTEMPT." as att
 				    AND questions.c_id = ".api_get_course_int_id()."
 		  WHERE attempts.exe_id='".Database::escape_string($id)."' $user_restriction
 		  GROUP BY quizz_rel_questions.question_order, attempts.question_id";
-
-			//GROUP BY questions.position, attempts.question_id";
-
+          //GROUP BY questions.position, attempts.question_id";
 $result = Database::query($query);	
 $questionList = array();
 $exerciseResult = array();
@@ -271,6 +267,8 @@ $counter = 1;
 
 $exercise_content = null;
 
+$category_list = array();
+
 foreach ($questionList as $questionId) {
 	
 	$choice = $exerciseResult[$questionId];
@@ -281,6 +279,7 @@ foreach ($questionList as $questionId) {
 	$objQuestionTmp 	= Question::read($questionId);	
 	$questionWeighting	= $objQuestionTmp->selectWeighting();
 	$answerType			= $objQuestionTmp->selectType();
+
 	
 	// Start buffer
     ob_start();
@@ -472,7 +471,6 @@ foreach ($questionList as $questionId) {
                     <td colspan="2">
                         <object type="application/x-shockwave-flash" data="../plugin/hotspot/hotspot_solution.swf?modifyAnswers='.$questionId.'&exe_id='.$id.'&from_db=1" width="556" height="350">
                             <param name="movie" value="../plugin/hotspot/hotspot_solution.swf?modifyAnswers='.$questionId.'&exe_id='.$id.'&from_db=1" />
-
                         </object>
                     </td>
                 </tr>
@@ -506,7 +504,7 @@ foreach ($questionList as $questionId) {
 			echo '<br />';
             
             echo '<div id="feedback_'.$name.'" style="width:100%">';
-			$comnt = trim(get_comments($id,$questionId));
+			$comnt = trim(get_comments($id, $questionId));
 			if (empty($comnt)) {
 				echo '<br />';
 			} else {
@@ -550,7 +548,7 @@ foreach ($questionList as $questionId) {
 					echo '<option '.(($i==$questionScore)?"selected='selected'":'').'>'.$i.'</option>';
 				}
 				echo '</select>';
-				echo '</form><br/ ></div>';
+				echo '</form><br /></div>';
                 
 				if ($questionScore == -1 ) {
 					$questionScore = 0;
@@ -566,11 +564,31 @@ foreach ($questionList as $questionId) {
 				 $questionScore = 0;
 			}
 		}    	
-	}
+	}   
     
     $my_total_score  = $questionScore;
-	$my_total_weight = $questionWeighting;   
+	$my_total_weight = $questionWeighting;
     $totalWeighting += $questionWeighting;
+    
+    if (isset($objQuestionTmp->category) && !empty($objQuestionTmp->category)) {
+        $category_list[$objQuestionTmp->category]['score'] += $my_total_score;
+        $category_list[$objQuestionTmp->category]['total'] += $my_total_weight;
+        $category_was_added_for_this_test = true;
+    } 
+
+    if (isset($objQuestionTmp->category_list) && !empty($objQuestionTmp->category_list)) {
+        foreach($objQuestionTmp->category_list as $category_id) {
+            $category_list[$category_id]['score'] += $my_total_score;
+            $category_list[$category_id]['total'] += $my_total_weight;
+            $category_was_added_for_this_test = true;
+        }
+    }
+    
+    //No category for this question!
+    if ($category_was_added_for_this_test == false) {
+        $category_list['none']['score'] += $my_total_score;
+        $category_list['none']['total'] += $my_total_weight;
+    }    
 	
     if ($objExercise->selectPropagateNeg() == 0 && $my_total_score < 0) {
         $my_total_score = 0;
@@ -631,6 +649,12 @@ if ($origin!='learnpath' || ($origin == 'learnpath' && isset($_GET['fb_type'])))
 	}
 }
 
+if (!empty($category_list) && ($show_results || $show_only_total_score)) {
+    //Adding total        
+    $category_list['total'] = array('score' => $my_total_score_temp, 'total' => $totalWeighting);
+    echo Testcategory::get_stats_table_by_attempt($objExercise->id, $category_list);
+}    
+
 echo $total_score_text;
 echo $exercise_content;
 echo $total_score_text;
@@ -642,8 +666,7 @@ if (is_array($arrid) && is_array($arrmarks)) {
 
 if ($is_allowedToEdit && $locked == false && !api_is_drh()) {
 	if (in_array($origin, array('tracking_course','user_course','correct_exercise_in_lp'))) {        
-		echo ' <form name="myform" id="myform" action="exercise_report.php?exerciseId='.$exercise_id.'&filter=2&comments=update&exeid='.$id.'&origin='.$origin.'&details=true&course='.Security::remove_XSS($_GET['cidReq']).$fromlink.'" method="post">';
-		//echo ' <input type = "hidden" name="totalWeighting" value="'.$totalWeighting.'">';
+		echo ' <form name="myform" id="myform" action="exercise_report.php?exerciseId='.$exercise_id.'&filter=2&comments=update&exeid='.$id.'&origin='.$origin.'&details=true&course='.Security::remove_XSS($_GET['cidReq']).$fromlink.'" method="post">';		
 		echo '<input type = "hidden" name="lp_item_id"       value="'.$learnpath_id.'">';
 		echo '<input type = "hidden" name="lp_item_view_id"  value="'.$lp_item_view_id.'">';
 		echo '<input type = "hidden" name="student_id"       value="'.$student_id.'">';

+ 56 - 5
main/exercice/testcategory.class.php

@@ -234,25 +234,24 @@ class Testcategory {
 			}
 		}
 		return $tabcat;
-	}
-	
+	}	
 	
 	/**
 	 * return the list of differents categories NAME for a test
 	 * input : test_id
 	 * return : array of string
 	 * hubert.borderiou 07-04-2011
+     * @author function rewrote by jmontoya
 	 */
 	public static function getListOfCategoriesNameForTest($in_testid) {
 		$tabcatName = array();
-		$tabcatID = getListOfCategoriesNameForTest($in_testid);
+		$tabcatID = self::getListOfCategoriesIDForTest($in_testid);
 		for ($i=0; $i < count($tabcatID); $i++) {
 			$cat = new Testcategory($tabcatID[$i]);
-			$tabcatName[] = $cat->name;
+			$tabcatName[$cat->id] = $cat->name;
 		}
 		return $tabcatName;
 	}
-	
 	/**
 	 * return the number of differents categories for a test
 	 * input : test_id
@@ -463,5 +462,57 @@ class Testcategory {
 		}
         return $res_num_max;
     }
+    
+    /**     
+     * Returns a category summary report
+     * @params int exercise id
+     * @params array prefilled array with the category_id, score, and weight example: array(1 => array('score' => '10', 'total' => 20));
+     */
+    public static function get_stats_table_by_attempt($exercise_id, $category_list = array()) {
+        if (empty($category_list)) {
+            return null;
+        }          
+        $category_name_list = Testcategory::getListOfCategoriesNameForTest($exercise_id);
+                
+        $table = new HTML_Table(array('class' => 'data_table'));
+        $table->setHeaderContents(0, 0, get_lang('Categories'));
+        $table->setHeaderContents(0, 1, get_lang('AbsoluteScore'));
+        $table->setHeaderContents(0, 2, get_lang('RelativeScore'));
+        $row = 1;
+        
+        $none_category = array();
+        if (isset($category_list['none'])) {
+            $none_category = $category_list['none'];                    
+            unset($category_list['none']);            
+        }
+        
+        $total = array();
+        if (isset($category_list['total'])) {
+            $total = $category_list['total'];
+            unset($category_list['total']);
+        }
+        if (count($category_list) > 1) {
+            foreach ($category_list as $category_id => $category_item) {            
+                $table->setCellContents($row, 0, $category_name_list[$category_id]);
+                $table->setCellContents($row, 1, show_score($category_item['score'], $category_item['total'], false));            
+                $table->setCellContents($row, 2, show_score($category_item['score'], $category_item['total'], true, false, true));
+                $row++;
+            }
+
+            if (!empty($none_category)) {
+                $table->setCellContents($row, 0, get_lang('None'));
+                $table->setCellContents($row, 1, show_score($none_category['score'], $none_category['total'], false));            
+                $table->setCellContents($row, 2, show_score($none_category['score'], $none_category['total'], true, false, true));
+                $row++;
+            }
+            if (!empty($total)) {
+                $table->setCellContents($row, 0, get_lang('Total'));
+                $table->setCellContents($row, 1, show_score($total['score'], $total['total'], false));
+                $table->setCellContents($row, 2, show_score($total['score'], $total['total'], true, false, true));
+            }
+            return $table->toHtml();
+        }
+        return null;
+    }
 }
 endif;