Преглед изворни кода

improved querys for course an user reporting

Cristian Fasanando пре 15 година
родитељ
комит
93a8fc60e4
3 измењених фајлова са 424 додато и 322 уклоњено
  1. 162 235
      main/inc/lib/tracking.lib.php
  2. 136 5
      main/mySpace/index.php
  3. 126 82
      main/tracking/courseLog.php

+ 162 - 235
main/inc/lib/tracking.lib.php

@@ -243,120 +243,83 @@ class Tracking {
 
 	/**
 	 * This function gets the score average from all tests in a course by student
-	 * @param int $student_id - User id
+	 * @param int $student_id - or array for multiples User id (array(0=>1,1=>2))
 	 * @param string $course_code - Course id
 	 * @return string value (number %) Which represents a round integer about the score average.
 	 */
 	function get_avg_student_exercise_score($student_id, $course_code) {
 
 		// protect datas
-		$student_id = Database::escape_string($student_id);
 		$course_code = Database::escape_string($course_code);
-
 		// get the informations of the course
 		$a_course = CourseManager :: get_course_information($course_code);
-
-		if(!empty($a_course['db_name']))
-		{
+		if(!empty($a_course['db_name'])) {
 			// table definition
 			$tbl_course_quiz = Database::get_course_table(TABLE_QUIZ_TEST,$a_course['db_name']);
-			$tbl_stats_exercise = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
-
-			//get the list of exercises
-			$sql = "SELECT id, title FROM $tbl_course_quiz WHERE active <> -1";
-			$rs = Database::query($sql, __FILE__, __LINE__);
-			$count_exe = Database::num_rows($rs);
-
-			if ($count_exe > 0) {
-				$quiz_avg_total_score = 0;
-				while($quiz = Database::fetch_array($rs)) {
-
-					// get the score and max score from track_e_exercise
-					$sql = 'SELECT exe_result , exe_weighting
-							FROM '.$tbl_stats_exercise.'
-							WHERE exe_exo_id = '.(int)$quiz['id'].'
-							AND exe_user_id = '.(int)$student_id.'
-							AND orig_lp_id = 0
-							AND exe_cours_id = "'.Database::escape_string($course_code).'"
-							AND orig_lp_item_id = 0
-							ORDER BY exe_date DESC';
-
-					$rsAttempt = Database::query($sql, __FILE__, __LINE__);
-					$nb_attempts = 0;
-
-					$quiz_avg_score = 0;
-					while ($attempt = Database::fetch_array($rsAttempt)) {
-						$nb_attempts++;
-						$exe_weight=$attempt['exe_weighting'];
-						if ($exe_weight >0) {
-							$quiz_avg_score += round(($attempt['exe_result']/$exe_weight*100),2);
-						}
-					}
-					if($nb_attempts>0) {
-						$quiz_avg_score = $quiz_avg_score / $nb_attempts;
-		            }
-
-		           	$quiz_avg_total_score += $quiz_avg_score;
+			$tbl_stats_exercise = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);			
+			$count_quiz = Database::fetch_row(Database::query("SELECT count(id) FROM $tbl_course_quiz WHERE active <> -1",__FILE__,__LINE__));
+			$quiz_avg_total_score = 0;
+			if (!empty($count_quiz[0]) && !empty($student_id)) {				
+				$condition_user = "";
+				if (is_array($student_id)) {
+					$condition_user = " AND exe_user_id IN (".implode(',',$student_id).") ";
+				} else {
+					$condition_user = " AND exe_user_id = '$student_id' ";
 				}
-				return $quiz_avg_total_score/$count_exe;
-			}
-		}
-		else
-		{
-			return null;
-		}
-
+				$sql = "SELECT SUM(exe_result/exe_weighting*100) as avg_score 
+						FROM $tbl_stats_exercise
+						WHERE exe_exo_id IN (SELECT id FROM $tbl_course_quiz WHERE active <> -1) 
+						$condition_user
+						AND orig_lp_id = 0
+						AND exe_cours_id = '$course_code' 
+						AND orig_lp_item_id = 0
+						ORDER BY exe_date DESC";				
+				$res = Database::query($sql, __FILE__, __LINE__);
+				$row = Database::fetch_array($res);				
+				$quiz_avg_score = 0;
+				if (!empty($row['avg_score'])) {
+					$quiz_avg_score = round($row['avg_score'],2);
+				}								
+				$count_attempt = Database::fetch_row(Database::query("SELECT count(*) FROM $tbl_stats_exercise WHERE exe_exo_id IN (SELECT id FROM $tbl_course_quiz WHERE active <> -1) $condition_user AND orig_lp_id = 0 AND exe_cours_id = '$course_code' AND orig_lp_item_id = 0 ORDER BY exe_date DESC",__FILE__,__LINE__));																
+				if(!empty($count_attempt[0])) {
+					$quiz_avg_score = $quiz_avg_score / $count_attempt[0];
+		        }
+		        $quiz_avg_total_score = $quiz_avg_score;				
+				return $quiz_avg_total_score/$count_quiz[0];				
+			} 		
+		}		
+		return null;		
 	}
 
-
-	function get_avg_student_progress($student_id, $course_code) {
-		require_once (api_get_path(LIBRARY_PATH) . 'course.lib.php');
-
+	function get_avg_student_progress($student_id, $course_code) {		
 		// protect datas
-		$student_id = intval($student_id);
 		$course_code = addslashes($course_code);
-
 		// get the informations of the course
 		$a_course = CourseManager :: get_course_information($course_code);
-
-		if(!empty($a_course['db_name']))
-		{
+		if (!empty($a_course['db_name'])) {
 			// table definition
-			$tbl_course_lp_view = Database :: get_course_table(TABLE_LP_VIEW, $a_course['db_name']);
-			$tbl_course_lp_view_item = Database :: get_course_table(TABLE_LP_ITEM_VIEW, $a_course['db_name']);
-			$tbl_course_lp_item = Database :: get_course_table(TABLE_LP_ITEM, $a_course['db_name']);
-			$tbl_course_lp = Database :: get_course_table(TABLE_LP_MAIN, $a_course['db_name']);
-
-			//get the list of learning paths
-			$sql = 'SELECT id FROM ' . $tbl_course_lp;
-			$rs = Database::query($sql, __FILE__, __LINE__);
-			$nb_lp = Database::num_rows($rs);
+			$tbl_course_lp_view = Database :: get_course_table(TABLE_LP_VIEW, $a_course['db_name']);	
+			$tbl_course_lp = Database :: get_course_table(TABLE_LP_MAIN, $a_course['db_name']);		
+			$count_lp = Database::fetch_row(Database::query("SELECT count(id) FROM $tbl_course_lp",__FILE__,__LINE__));
 			$avg_progress = 0;
-
-			if ($nb_lp > 0) {
-				while ($lp = Database :: fetch_array($rs)) {
-					// get the progress in learning pathes
-					$sqlProgress = "SELECT progress
-													FROM " . $tbl_course_lp_view . " AS lp_view
-													WHERE lp_view.user_id = " . $student_id . "
-													AND lp_view.lp_id = " . $lp['id'] . "
-												   ";
-					$resultItem = Database::query($sqlProgress, __FILE__, __LINE__);
-					if(Database::num_rows($resultItem)>0)
-					{
-						$avg_progress += Database::result($resultItem, 0, 0);
-					}
-				}
-				$avg_progress = round($avg_progress / $nb_lp, 1);
-			}
-
-			return $avg_progress;
-		}
-		else
-		{
-			return null;
+			if (!empty($count_lp[0]) && !empty($student_id)) {				
+				$condition_user = "";
+				if (is_array($student_id)) {			
+					$condition_user = " lp_view.user_id IN (".implode(',',$student_id).") AND ";				
+				} else {
+					$condition_user = " lp_view.user_id = '$student_id' AND ";
+				}								
+				$sqlProgress = "SELECT SUM(progress) FROM $tbl_course_lp_view AS lp_view WHERE $condition_user lp_view.lp_id IN (SELECT id FROM $tbl_course_lp)";				
+				$resultItem  = Database::query($sqlProgress, __FILE__, __LINE__);
+				$rowItem = Database::fetch_row($resultItem);				
+				$avg_progress = round($rowItem[0] / $count_lp[0], 1);				
+				return $avg_progress;
+			} 
 		}
+		return null;
 	}
+
+
 	/**
 	 * This function gets:
 	 * 1. The score average from all SCORM Test items in all LP in a course-> All the answers / All the max score.
@@ -377,65 +340,71 @@ class Tracking {
 		$tbl_stats_exercices = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
 		$tbl_stats_attempts= Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
 		$course = CourseManager :: get_course_information($course_code);
+		
 		if (!empty($course['db_name'])) {
-
+	
 			$tbl_quiz_questions= Database :: get_course_table(TABLE_QUIZ_QUESTION,$course['db_name']);
 			$lp_table = Database :: get_course_table(TABLE_LP_MAIN,$course['db_name']);
 			$lp_item_table = Database  :: get_course_table(TABLE_LP_ITEM,$course['db_name']);
 			$lp_view_table = Database  :: get_course_table(TABLE_LP_VIEW,$course['db_name']);
 			$lp_item_view_table = Database  :: get_course_table(TABLE_LP_ITEM_VIEW,$course['db_name']);
-
-			$sql_course_lp = 'SELECT id FROM '.$lp_table;
-			if(count($lp_ids)!=0) {
-				$sql_course_lp.=' WHERE id IN ('.implode(',',$lp_ids).')';
+			
+			$condition_lp = "";
+			if(count($lp_ids) > 0) {
+				$condition_lp =" WHERE id IN(".implode(',',$lp_ids).") ";
 			}
-
-			$sql_result_lp = Database::query($sql_course_lp, __FILE__, __LINE__);
+	
+			$count_row_lp = Database::fetch_row(Database::query("SELECT count(*) FROM $lp_table $condition_lp",__FILE__,__LINE__));
+				
 			$lp_scorm_score_total = 0;
 			$lp_scorm_weighting_total = 0;
 			$lp_scorm_result_score_total = 0;
 			$lp_scorm_loop=0;
 			$lp_count = 0;
 			$progress = 0;
-
-			if(Database::num_rows($sql_result_lp)>0){
-				//Scorm test
-				while($a_learnpath = Database::fetch_array($sql_result_lp)) {
-
-					//We get the last view id of this LP (with the higher id)
-					$sql='SELECT max(id) as id FROM '.$lp_view_table.'
-						  WHERE lp_id='.$a_learnpath['id'].' AND user_id="'.intval($student_id).'"';
-					$rs_last_lp_view_id = Database::query($sql, __FILE__, __LINE__);
-					$lp_view_id = Database::result($rs_last_lp_view_id,0,'id'); // THE view
-
-					if ($lp_view_id != '') {
+			
+			if (!empty($count_row_lp[0]) && !empty($student_id)) {
+				
+				$condition_user1 = "";				
+				if (is_array($student_id)) {
+					$condition_user1 =" AND user_id IN (".implode(',',$student_id).") ";	
+				} else {
+					$condition_user1 =" AND user_id = '$student_id' ";
+				}
+				
+				$sql = "SELECT max(id) as id, lp_id, user_id FROM $lp_view_table WHERE lp_id IN (SELECT id FROM $lp_table $condition_lp) $condition_user1 GROUP BY lp_id,user_id";
+				$rs_last_lp_view_id = Database::query($sql, __FILE__, __LINE__);
+				
+				$score_of_scorm_calculate = 0;
+				if (Database::num_rows($rs_last_lp_view_id) > 0) {
+					while ($row_lp_view = Database::fetch_array($rs_last_lp_view_id)) {
+						
+						$lp_view_id = $row_lp_view['id'];					
 						// we get the progress
 						$sql='SELECT progress FROM '.$lp_view_table.' WHERE id="'.$lp_view_id.'"';
+	
 						$rs = Database::query($sql, __FILE__, __LINE__);
 						$progress = Database::result($rs,0,'progress');
-
+	
 						// item's list of an scorm
 						$sql_max_score='SELECT lp_iv.score as score,lp_i.max_score
 								FROM '.$lp_item_view_table.' as lp_iv
 								INNER JOIN '.$lp_item_table.' as lp_i
 									ON lp_i.id = lp_iv.lp_item_id
 									AND lp_i.item_type="sco"
-								WHERE lp_view_id="'.$lp_view_id.'"';
+								WHERE lp_view_id="'.$lp_view_id.'"';		
 
-						//$rs = Database::query($sql, __FILE__, __LINE__);
-						//$sql_max_score='SELECT max_score FROM '.$lp_item_view_table.' WHERE lp_view_id="'.$lp_view_id.'" ';
 						$res_max_score=Database::query($sql_max_score,__FILE__,__LINE__);
 						$count_total_loop=0;
 						$num_rows_max_score=Database::num_rows($res_max_score);
-
+	
 						if ($num_rows_max_score==1) {
 							while ($row_max_score=Database::fetch_array($res_max_score)) {
-								//echo $row_max_score['score'].'  - '.$row_max_score['max_score'];
 								if ($row_max_score['max_score']==0) {
 	                                //when there's no max score, we assume 100 as the max score, as the SCORM 1.2 says that the value should always be between 0 and 100.
 	                       			$lp_scorm_result_score_total+=($row_max_score['score']/100);
 									$current_value = $row_max_score['score']/100;
-
+	
 								} else {
 									$lp_scorm_result_score_total+=($row_max_score['score']/$row_max_score['max_score']);
 									$current_value = $row_max_score['score']/$row_max_score['max_score'];
@@ -443,10 +412,7 @@ class Tracking {
 								$count_total_loop++;
 							}
 						} elseif ($num_rows_max_score > 1) {
-							//echo ' ---- <br>';
 							while ($row_max_score=Database::fetch_array($res_max_score)) {
-								//echo $row_max_score['score'].'  - '.$row_max_score['max_score'];
-								//echo '<br>';
 								if ($row_max_score['max_score']==0) {
 									$lp_scorm_result_score_total+=($row_max_score['score']/100);
 									$current_value = $row_max_score['score']/100;
@@ -458,126 +424,90 @@ class Tracking {
 								$count_total_loop++;
 							}
 						}
-
+	
 						if ($num_rows_max_score > 0 && ($progress > 0 || $current_value > 0 )) {
 							$lp_count++;
 						}
-
+	
 						if ($count_total_loop==0) {
 							$count_total_loop=1;
 						}
 						$score_of_scorm_calculate=round((($lp_scorm_result_score_total/$count_total_loop)*100),2);
-
-						} else {
-							$score_of_scorm_calculate = 0;
+										
 					}
-				}
-
-				//The next call to a MySQL fetch function, such as mysql_fetch_assoc(), would return that row.
-				mysql_data_seek($sql_result_lp,0);
-
-				if ($lp_count==0) {
-                	$lp_count=1;
-                }
-
+				} 
+										
 				if(count($lp_ids)==0 ) {
-					$score_of_scorm_calculate=round((($score_of_scorm_calculate/$lp_count)),2);
+					$score_of_scorm_calculate=round((($score_of_scorm_calculate/$count_row_lp[0])),2);
 				}
-
+	
 				$lp_scorm_score_total = $score_of_scorm_calculate;
-
-				//Quizz in a LP
-				while($a_learnpath = Database::fetch_array($sql_result_lp)) {
-					//we got the maxscore this is wrong
-					/*
-					echo $sql = 'SELECT id as item_id, max_score
-							FROM '.$lp_item_table.' AS lp_item
-							WHERE lp_id='.$a_learnpath['id'].'
-							AND item_type="quiz"';
-					*/
-
-					//Path is the exercise id
-					$sql = 'SELECT path, id as item_id, max_score
-					FROM '.$lp_item_table.' AS lp_item
-					WHERE lp_id='.$a_learnpath['id'].'
-					AND item_type="quiz"';
-
-					$rsItems = Database::query($sql, __FILE__, __LINE__);
-
-					//We get the last view id of this LP
-					$sql = "SELECT id FROM $lp_view_table  WHERE user_id = '".intval($student_id)."' and lp_id='".intval($a_learnpath['id'])."'";
-					//$sql='SELECT max(id) as id FROM '.$lp_view_table.' WHERE lp_id='.$a_learnpath['id'].' AND user_id="'.intval($student_id).'"';
-					$rs_last_lp_view_id = Database::query($sql, __FILE__, __LINE__);
-					$lp_view_id = intval(Database::result($rs_last_lp_view_id,0,'id'));
-
-					$total_score = $total_weighting = 0;
-					if ($lp_view_id!=0) {
-						while ($item = Database :: fetch_array($rsItems, 'ASSOC')) {
-
-							// we take the score from a LP because we have lp_view_id
-							$sql = "SELECT score FROM $lp_item_view_table WHERE lp_item_id = '".(int)$item['item_id']."' and lp_view_id = '".(int)$lp_view_id."'
-							ORDER BY view_count DESC limit 1";
-
-							/*$sql = 'SELECT score as student_score
-									FROM '.$lp_item_view_table.' as lp_view_item
-									WHERE lp_view_item.lp_item_id = '.$item['item_id'].'
-									AND lp_view_id = "'.$lp_view_id.'" ';*/
-							$rsScores = Database::query($sql, __FILE__, __LINE__);
-
-							// Real max score - this was implemented because of the random exercises
-					 		$sql_last_attempt = 'SELECT exe_id FROM '. $tbl_stats_exercices. ' ' .
-					 							'WHERE exe_exo_id="' .$item['path']. '" AND exe_user_id="' . $student_id . '" AND orig_lp_id = "'.$a_learnpath['id'].'" AND orig_lp_item_id = "'.$item['item_id'].'" AND exe_cours_id="' . $course_code . '" ORDER BY exe_date DESC limit 1';
-
-							$resultLastAttempt = Database::query($sql_last_attempt, __FILE__, __LINE__);
-							$num = Database :: num_rows($resultLastAttempt);
-							if ($num > 0){
-								if ($num > 1){
-									while ($rowLA = Database :: fetch_row($resultLastAttempt)) {
-										$id_last_attempt = $rowLA[0];
-									}
-								} else {
-									$id_last_attempt = Database :: result($resultLastAttempt, 0, 0);
+				
+				$condition_user2 = "";				
+				if (is_array($student_id)) {
+					$condition_user2 =" lp_view.user_id IN (".implode(',',$student_id).") AND ";	
+				} else {
+					$condition_user2 =" lp_view.user_id = '$student_id' AND ";
+				}
+	
+				//Path is the exercise id
+				$sql = "SELECT lp_view.id as lp_view_id, lp_view.lp_id as lp_id, lp_item.path, lp_item.id as item_id, lp_item.max_score, lp_view.user_id as user_id 
+				FROM $lp_view_table lp_view 
+				INNER JOIN $lp_item_table lp_item ON lp_item.lp_id = lp_view.lp_id AND item_type='".TOOL_QUIZ."'
+				WHERE $condition_user2 lp_view.lp_id IN (SELECT id FROM $lp_table $condition_lp)";;
+				
+				$rsItems = Database::query($sql, __FILE__, __LINE__);
+				
+				$total_score = $total_weighting = 0;
+				if (Database::num_rows($rsItems) > 0) {
+					while ($item = Database::fetch_array($rsItems)) {
+						
+						$lp_view_id = $item['lp_view_id'];
+						$lp_item_id = $item['item_id'];
+						$lp_id = $item['lp_id'];
+						$user_id = $item['user_id'];
+						
+						// we take the score from a LP because we have lp_view_id
+						$sql = "SELECT score FROM $lp_item_view_table WHERE lp_item_id = '$lp_item_id' and lp_view_id = '$lp_view_id'
+						ORDER BY view_count DESC limit 1";
+
+						$rsScores = Database::query($sql, __FILE__, __LINE__);
+						// Real max score - this was implemented because of the random exercises
+				 		$sql_last_attempt = 'SELECT exe_id FROM '. $tbl_stats_exercices. ' ' .
+				 							'WHERE exe_exo_id="' .$item['path']. '" AND exe_user_id="' . $user_id . '" AND orig_lp_id = "'.$lp_id.'" AND orig_lp_item_id = "'.$lp_item_id.'" AND exe_cours_id="' . $course_code . '" ORDER BY exe_date DESC limit 1';
+
+						$resultLastAttempt = Database::query($sql_last_attempt, __FILE__, __LINE__);
+						$num = Database :: num_rows($resultLastAttempt);
+						if ($num > 0){
+							if ($num > 1){
+								while ($rowLA = Database :: fetch_row($resultLastAttempt)) {
+									$id_last_attempt = $rowLA[0];
 								}
+							} else {
+								$id_last_attempt = Database :: result($resultLastAttempt, 0, 0);
 							}
+						}
 
-							$sql = "SELECT SUM(t.ponderation) as maxscore from ( SELECT distinct question_id, marks,ponderation FROM $tbl_stats_attempts as at " .
-						  	"INNER JOIN  $tbl_quiz_questions as q  on(q.id = at.question_id) where exe_id ='$id_last_attempt' ) as t";
-
-							$result = Database::query($sql, __FILE__, __LINE__);
-							$row_max_score = Database :: fetch_array($result);
-							$maxscore = $row_max_score['maxscore'];
-							if ($maxscore=='') {
-								$maxscore = $item['max_score'];
-							}
-							// not right!
-							/*if(Database::num_rows($rsScores)>0) {
-								$total_score += Database::result($rsScores, 0, 0);
-								//echo $total_weighting += $item['max_score'];
-								$total_weighting += $maxscore;
-
-								if($total_weighting>0) {
-									//echo ($total_score/$total_weighting)*100;
-									$lp_scorm_score_total += ($total_score/$total_weighting)*100;
-									$lp_scorm_weighting_total+=100;
-								}
-							}*/
-
-							if(Database::num_rows($rsScores)>0) {
-								$total_score = Database::result($rsScores, 0, 0);
-								//echo $total_weighting += $item['max_score'];
-								$total_weighting += $maxscore;
-								if($total_weighting>0 && $maxscore>0) {
-									//echo $total_score.' -  '.$maxscore; echo '<br>';
-									//echo $lp_scorm_score_total += ($total_score/$total_weighting)*100;
-									$lp_scorm_score_total += ($total_score/$maxscore)*100;
-									$lp_scorm_weighting_total+=100;
-								}
+						$sql = "SELECT SUM(t.ponderation) as maxscore from ( SELECT distinct question_id, marks,ponderation FROM $tbl_stats_attempts as at " .
+					  	"INNER JOIN  $tbl_quiz_questions as q  on(q.id = at.question_id) where exe_id ='$id_last_attempt' ) as t";
+
+						$result = Database::query($sql, __FILE__, __LINE__);
+						$row_max_score = Database :: fetch_array($result);
+						$maxscore = $row_max_score['maxscore'];
+						if ($maxscore=='') {
+							$maxscore = $item['max_score'];
+						}						
+						if(Database::num_rows($rsScores)>0) {
+							$total_score = Database::result($rsScores, 0, 0);
+							$total_weighting += $maxscore;
+							if($total_weighting>0 && $maxscore>0) {
+								$lp_scorm_score_total += ($total_score/$maxscore)*100;
+								$lp_scorm_weighting_total+=100;
 							}
-						}
+						}							
 					}
-				}
-			}
-
+				}									
+			}			
 			$totalScore = $lp_scorm_score_total;
 			$pourcentageScore = 0;
 			if($lp_scorm_weighting_total>0) {
@@ -589,11 +519,10 @@ class Tracking {
 				return $score_of_scorm_calculate;
 			} else {
 				return null;
-			}
-		} else {
-			return null;
-		}
+			}			
+		}	
 	}
+	
 
 	/**
 	 * gets the list of students followed by coach
@@ -619,7 +548,7 @@ class Tracking {
 		if ($_configuration['multiple_access_urls']==true) {
 			$tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
 			$access_url_id = api_get_current_access_url_id();
-			if ($access_url_id != -1){
+			if ($access_url_id != -1) {
 				$sql = 'SELECT scu.id_session, scu.course_code
 						FROM ' . $tbl_session_course_user . ' scu INNER JOIN '.$tbl_session_rel_access_url.'  sru
 						ON (scu.id_session=sru.session_id)
@@ -989,7 +918,6 @@ class Tracking {
 							FROM ' . $tbl_item_property . '
 							WHERE insert_user_id=' . $student_id . '
 							AND tool="work"';
-
 			$rs = Database::query($sql, __LINE__, __FILE__);
 			return Database::num_rows($rs);
 		}
@@ -1297,8 +1225,8 @@ class Tracking {
 	 * @param string the course id
 	 */
 	function get_average_test_scorm_and_lp ($user_id,$course_id) {
-				//the score inside the Reporting table
-
+		
+		//the score inside the Reporting table
 		$course_info=api_get_course_info($course_id);
 		$lp_table = Database :: get_course_table(TABLE_LP_MAIN,$course_info['dbName']);
 		$lp_view_table = Database  :: get_course_table(TABLE_LP_VIEW,$course_info['dbName']);
@@ -1358,12 +1286,11 @@ class Tracking {
 			$average_data1=0;
 			$count_loop++;
 		}
-
 		if ((int)$count_loop > 0) {
 			$avg_student_score = round(($average_data_sum / $count_loop * 100), 2);
 		}
-
 		return $avg_student_score;
 	}
+				
 }
 ?>

+ 136 - 5
main/mySpace/index.php

@@ -48,7 +48,8 @@ $tbl_session_course 		= Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
 $tbl_session_user 			= Database :: get_main_table(TABLE_MAIN_SESSION_USER);
 $tbl_session_course_user 	= Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
 $tbl_admin					= Database :: get_main_table(TABLE_MAIN_ADMIN);
-
+$tbl_track_cours_access 	= Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
+	
 
 /********************
  * FUNCTIONS
@@ -438,10 +439,16 @@ if ($view == 'coach') {
 
 echo '<div class="clear">&nbsp;</div>';
 
+
+
+
 if (api_is_allowed_to_create_course() && $view == 'teacher') {
+	
+	
 	if ($nb_teacher_courses) {
 
-		$table = new SortableTable('tracking_list_course', 'count_teacher_courses');
+		$table = new SortableTable('courses', 'get_number_of_courses' ,'get_course_data');
+						
 		$parameters['view'] = 'teacher';
 		$parameters['class'] = 'data_table';
 
@@ -466,9 +473,11 @@ if (api_is_allowed_to_create_course() && $view == 'teacher') {
 			get_lang('AvgMessages', ''),
 			get_lang('AvgAssignments', '')
 		);
+		
+		$table->display();
+		
 
-		$a_course_students = array();
-
+		/*
 		foreach ($courses as $course) {
 			$course_code = $course['course_code'];
 			$avg_assignments_in_course = $avg_messages_in_course = $nb_students_in_course = $avg_progress_in_course = $avg_score_in_course = $avg_time_spent_in_course = $avg_score_in_exercise = 0;
@@ -527,6 +536,9 @@ if (api_is_allowed_to_create_course() && $view == 'teacher') {
 				$avg_assignments_in_course = null;
 			}
 
+
+
+
 			$table_row = array();
 			$table_row[] = $course['title'];
 			$table_row[] = $nb_students_in_course;
@@ -556,10 +568,14 @@ if (api_is_allowed_to_create_course() && $view == 'teacher') {
 		}
 		$table -> updateColAttributes(0, array('align' => 'left'));
 		$table -> updateColAttributes(7, array('align' => 'center'));
-		$table -> display();
+		*/
+		//$table -> display();
 	}
 }
 
+
+
+
 if ($is_platform_admin && $view == 'admin') {
 	echo '<a href="'.api_get_self().'?view=admin&amp;display=coaches">'.get_lang('DisplayCoaches').'</a> | ';
 	echo '<a href="'.api_get_self().'?view=admin&amp;display=useroverview">'.get_lang('DisplayUserOverview').'</a>';
@@ -1206,3 +1222,118 @@ function get_user_overview_export_extra_fields($user_id) {
 	$extra_data = UserManager::get_extra_user_data($user_id, true);
 	return $extra_data;
 }
+/**
+ * Get number of courses for sortable with pagination 
+ * @return int
+ */
+function get_number_of_courses() {
+	global $courses;	
+	return count($courses);
+}
+/**
+ * Get data for courses list in sortable with pagination 
+ * @return array
+ */
+function get_course_data($from, $number_of_items, $column, $direction) {
+	
+	global $courses, $csv_content, $charset ;
+	global $tbl_course, $tbl_course_user, $tbl_track_cours_access, $tbl_session_course_user;
+	
+	$a_course_students  = array();	
+	$course_data = array();	
+	$arr_course = $courses;	
+	foreach ($arr_course as &$cours) {			
+		$cours = "'{$cours[course_code]}'";
+	}
+	
+	// get all courses with limit
+	$sql = "SELECT course.code as col1, course.title as col2,count(course_rel_user.user_id) as col3 				
+			FROM $tbl_course course 
+			LEFT JOIN $tbl_course_user course_rel_user ON course_rel_user.course_code=course.code AND course_rel_user.status='5'
+			WHERE course.code IN (".implode(',',$arr_course).")
+			GROUP BY col1, col2"; 	
+	if (!in_array($direction, array('ASC','DESC'))) $direction = 'ASC';
+	
+    $column = intval($column);
+    $from = intval($from);
+    $number_of_items = intval($number_of_items);
+	$sql .= " ORDER BY col$column $direction ";
+	$sql .= " LIMIT $from,$number_of_items";
+
+	$res = Database::query($sql, __FILE__, __LINE__);				
+	while ($row_course = Database::fetch_row($res)) {
+
+		$course_code = $row_course[0];
+		$course_info = api_get_course_info($course_code);
+		$avg_assignments_in_course = $avg_messages_in_course = $nb_students_in_course = $avg_progress_in_course = $avg_score_in_course = $avg_time_spent_in_course = $avg_score_in_exercise = 0;		
+		$tbl_item_property 		= Database :: get_course_table(TABLE_ITEM_PROPERTY, $course_info['dbName']);
+		$tbl_forum_post  		= Database :: get_course_table(TABLE_FORUM_POST, $course_info['dbName']);
+		$tbl_course_lp_view = Database :: get_course_table(TABLE_LP_VIEW, $course_info['dbName']);	
+		$tbl_course_lp = Database :: get_course_table(TABLE_LP_MAIN, $course_info['dbName']);
+		
+		// students directly subscribed to the course
+		$sql = "SELECT user_id FROM $tbl_course_user as course_rel_user WHERE course_rel_user.status='5' AND course_rel_user.course_code='$course_code'
+		  		UNION DISTINCT SELECT id_user as user_id FROM $tbl_session_course_user srcu WHERE  srcu. course_code='$course_code'";					
+		$rs = Database::query($sql, __FILE__, __LINE__);
+		$users = array();		
+		while ($row = Database::fetch_array($rs)) {		
+			$users[] = $row['user_id']; 							
+		}		
+		if (count($users) > 0) {
+			// get columns data for courses list
+			$sql = "SELECT count(prop.tool) as count_assignments,
+					count(post.poster_id) as count_messages,					
+					(SELECT SUM(UNIX_TIMESTAMP(logout_course_date)-UNIX_TIMESTAMP(login_course_date)) FROM $tbl_track_cours_access track_course WHERE track_course.user_id = course_rel_user.user_id AND track_course.course_code='$course_code') as time_espent		
+					FROM $tbl_course_user  course_rel_user
+					LEFT JOIN $tbl_item_property prop ON prop.insert_user_id = course_rel_user.user_id AND tool='work'
+					LEFT JOIN $tbl_forum_post post ON post.poster_id = course_rel_user.user_id												
+					WHERE course_rel_user.user_id IN (".implode(',',$users).") AND course_rel_user.course_code='$course_code'";																										
+			$rs_count = Database::query($sql, __FILE__, __LINE__);
+			$row_count = Database::fetch_array($rs_count);	
+			$nb_students_in_course = (!empty($row_course[2])?$row_course[2]:0);			
+			$avg_time_spent_in_course = (!empty($row_count['time_espent'])?$row_count['time_espent']:0);
+			$avg_messages_in_course = (!empty($row_count['count_messages'])?$row_count['count_messages']:0);
+			$avg_assignments_in_course = (!empty($row_count['count_assignments'])?$row_count['count_assignments']:0);
+			$avg_progress_in_course += Tracking::get_avg_student_progress($users, $course_code);		
+			$avg_score_in_course += Tracking :: get_avg_student_score($users, $course_code);
+			$avg_score_in_exercise += Tracking::get_avg_student_exercise_score($users, $course_code);		
+		}
+		
+		if ($nb_students_in_course > 0) {
+			$avg_time_spent_in_course = api_time_to_hms($avg_time_spent_in_course / $nb_students_in_course);
+			$avg_progress_in_course = round($avg_progress_in_course / $nb_students_in_course, 2);
+			$avg_score_in_course = round($avg_score_in_course / $nb_students_in_course, 2);
+			$avg_score_in_exercise = round($avg_score_in_exercise / $nb_students_in_course, 2);			
+		} else {
+			$avg_time_spent_in_course = null;
+			$avg_progress_in_course = null;
+			$avg_score_in_course = null;
+			$avg_score_in_exercise = null;
+			$avg_messages_in_course = null;
+			$avg_assignments_in_course = null;
+		}
+		$table_row = array();		
+		$table_row[] = $row_course[1];
+		$table_row[] = $nb_students_in_course;
+		$table_row[] = $avg_time_spent_in_course;
+		$table_row[] = is_null($avg_progress_in_course) ? '' : $avg_progress_in_course.'%';
+		$table_row[] = is_null($avg_score_in_course) ? '' : $avg_score_in_course.'%';
+		$table_row[] = is_null($avg_score_in_exercise) ? '' : $avg_score_in_exercise.'%';
+		$table_row[] = $avg_messages_in_course;
+		$table_row[] = $avg_assignments_in_course;
+		//set the "from" value to know if I access the Reporting by the Dokeos tab or the course link
+		$table_row[] = '<center><a href="../tracking/courseLog.php?cidReq='.$course_code.'&studentlist=true&from=myspace"><img src="'.api_get_path(WEB_IMG_PATH).'2rightarrow.gif" border="0" /></a></center>';
+		$csv_content[] = array(
+			api_html_entity_decode($row_course[1], ENT_QUOTES, $charset),
+			$nb_students_in_course,
+			$avg_time_spent_in_course,
+			is_null($avg_progress_in_course) ? null : $avg_progress_in_course.'%',
+			is_null($avg_score_in_course) ? null : $avg_score_in_course.'%',
+			is_null($avg_score_in_exercise) ? null : $avg_score_in_exercise.'%',
+			$avg_messages_in_course,
+			$avg_assignments_in_course,
+		);
+		$course_data[] = $table_row;				
+	}
+	return $course_data;
+}

+ 126 - 82
main/tracking/courseLog.php

@@ -650,7 +650,23 @@ if ($_GET['studentlist'] == 'false') {
 	$tracking_direction = isset($_GET['tracking_direction']) ? $_GET['tracking_direction'] : 'DESC';
 
 	if (count($a_students) > 0) {
-		$table = new SortableTable('tracking', 'count_student_in_course', null, ($is_western_name_order xor $sort_by_first_name) ? 2 : 1);
+		
+	    if ($export_csv) {
+			$csv_content[] = array ();
+		}
+
+	    $all_datas = array();
+	    $course_code = $_course['id'];
+		
+		$user_ids = array_keys($a_students);
+		$table = new SortableTable('users', 'get_number_of_users', 'get_user_data', (api_is_western_name_order() xor api_sort_by_first_name()) ? 3 : 2);
+		
+		$parameters['cidReq'] 		= Security::remove_XSS($_GET['cidReq']);
+		$parameters['studentlist'] 	= Security::remove_XSS($_GET['studentlist']);
+		$parameters['from'] 	= Security::remove_XSS($_GET['myspace']);
+		
+		$table->set_additional_parameters($parameters);
+		
 		$table -> set_header(0, get_lang('OfficialCode'), false, 'align="center"');
 		if ($is_western_name_order) {
 			$table -> set_header(1, get_lang('FirstName'), false, 'align="center"');
@@ -666,86 +682,15 @@ if ($_GET['studentlist'] == 'false') {
 		$table -> set_header(7, get_lang('Messages'),false);
 		$table -> set_header(8, get_lang('FirstLogin'), false, 'align="center"');
 		$table -> set_header(9, get_lang('LatestLogin'), false, 'align="center"');				
-		if (isset($_GET['additional_profile_field']) AND is_numeric($_GET['additional_profile_field'])) {
-			$table -> set_header(10, get_lang('AdditionalProfileField'),false);
-			$table -> set_header(11, get_lang('Details'),false);
-        } else {
-        	$table -> set_header(10, get_lang('Details'),false);
-        }
-	    if ($export_csv) {
-			$csv_content[] = array ();
-		}
-
-	    $all_datas = array();
-	    $course_code = $_course['id'];
-		foreach ($a_students as $student_id => $student) {
-			$student_datas = UserManager :: get_user_info_by_id($student_id);
-			
-			$avg_time_spent = $avg_student_score = $avg_student_progress = $total_assignments = $total_messages = 0;
-			$nb_courses_student = 0;
-			$avg_time_spent = Tracking :: get_time_spent_on_the_course($student_id, $course_code);
-			$avg_student_score = Tracking :: get_average_test_scorm_and_lp($student_id, $course_code);
-			$avg_student_progress = Tracking :: get_avg_student_progress($student_id, $course_code);
-			$total_assignments = Tracking :: count_student_assignments($student_id, $course_code);
-			$total_messages = Tracking :: count_student_messages($student_id, $course_code);
-
-			$row = array();
-			$row[] = $student_datas['official_code'];
-			if ($is_western_name_order) {
-				$row[] = $student_datas['firstname'];
-				$row[] = $student_datas['lastname'];
-			} else {
-				$row[] = $student_datas['lastname'];
-				$row[] = $student_datas['firstname'];
-			}
-			$row[] = api_time_to_hms($avg_time_spent);
-			if (is_null($avg_student_score)) {$avg_student_score=0;}
-			if (is_null($avg_student_progress)) {$avg_student_progress=0;}
-			$row[] = $avg_student_progress.'%';
-			$row[] = $avg_student_score.'%';
-			$row[] = $total_assignments;
-			$row[] = $total_messages;
-			$row[] = Tracking :: get_first_connection_date_on_the_course($student_id, $course_code);
-			$row[] = Tracking :: get_last_connection_date_on_the_course($student_id, $course_code);
-
-			// we need to display an additional profile field
-			if (isset($_GET['additional_profile_field']) AND is_numeric($_GET['additional_profile_field'])) {
-				if (is_array($additional_user_profile_info[$student_id])) {
-					$row[]=implode(', ', $additional_user_profile_info[$student_id]);
-				} else {
-					$row[]='&nbsp;';
-				}
-			}
-			$row[] = '<center><a href="../mySpace/myStudents.php?student='.$student_id.'&details=true&course='.$course_code.'&origin=tracking_course"><img src="'.api_get_path(WEB_IMG_PATH).'2rightarrow.gif" border="0" /></a></center>';
-			if ($export_csv) {
-				$row[8] = strip_tags($row[8]);
-				$row[9] = strip_tags($row[9]);
-				unset($row[10]);
-				$csv_content[] = $row;
-			}
-			$all_datas[] = $row;
-		}		
-		$clean_order = array('ASC','DESC');
-		if(in_array($_GET['tracking_direction'],$clean_order) && $_GET['tracking_direction'] == 'ASC'){	
-			usort($all_datas, 'sort_users');
-		} else if (in_array($_GET['tracking_direction'],$clean_order) && $_GET['tracking_direction'] == 'DESC') {
-			usort($all_datas, 'sort_users_desc');
-		}
-		$page = $table->get_pager()->getCurrentPageID();
-		$all_datas = array_slice($all_datas, ($page-1)*$table -> per_page, $table -> per_page);
-		// if ($export_csv) { usort($csv_content, 'sort_users'); }
+		//if (isset($_GET['additional_profile_field']) AND is_numeric($_GET['additional_profile_field'])) {
+			$table -> set_header(10, get_lang('AdditionalProfileField'),false);			
+        /*} else {
+        	$table -> set_header(10, ,false);
+        }*/
+		$table -> set_header(11, get_lang('Details'),false);
+		$table->display();
+		
 		
-		foreach ($all_datas as $row) {
-			$table -> addRow($row,'align="right"');
-		}
-		$table -> setColAttributes(0, array('align' => 'left'));
-		$table -> setColAttributes(1, array('align' => 'left'));
-		$table -> setColAttributes(2, array('align' => 'left'));
-		$table -> setColAttributes(7, array('align' => 'right'));
-		$table -> setColAttributes(8, array('align' => 'center'));
-		$table -> setColAttributes(9, array('align' => 'center'));
-		$table -> display();
-
 	} else {
 		echo get_lang('NoUsersInCourseTracking');
 	}
@@ -907,7 +852,7 @@ function get_addtional_profile_information_of_field($field_id){
  * @since	Nov 2009
  * @version	1.8.6.2
  */
-function get_addtional_profile_information_of_field_by_user($field_id, $users){
+function get_addtional_profile_information_of_field_by_user($field_id, $users) {
 	// Database table definition
 	$table_user 				= Database::get_main_table(TABLE_MAIN_USER);
 	$table_user_field_values 	= Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);	
@@ -955,7 +900,6 @@ function get_addtional_profile_information_of_field_by_user($field_id, $users){
 	return $return;
 }
 
-
 /**
  * count the number of students in this course (used for SortableTable)
  */
@@ -971,3 +915,103 @@ function sort_users($a, $b) {
 function sort_users_desc($a, $b) {
 	return strcmp( trim(api_strtolower($b[$_SESSION['tracking_column']])), trim(api_strtolower($a[$_SESSION['tracking_column']])));
 }
+
+/**
+ * Get number of users for sortable with pagination 
+ * @return int
+ */
+function get_number_of_users() {		
+		global $user_ids;		
+		return count($user_ids);
+}
+/**
+ * Get data for users list in sortable with pagination 
+ * @return array
+ */
+function get_user_data($from, $number_of_items, $column, $direction) {
+	
+	global $user_ids, $course_code, $additional_user_profile_info, $export_csv, $is_western_name_order;
+	
+	$course_code = Database::escape_string($course_code);
+	$course_info = CourseManager :: get_course_information($course_code);
+	$tbl_track_cours_access = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
+	$tbl_user 				= Database :: get_main_table(TABLE_MAIN_USER);		
+	$tbl_item_property 		= Database :: get_course_table(TABLE_ITEM_PROPERTY, $course_info['db_name']);
+	$tbl_forum_post  		= Database :: get_course_table(TABLE_FORUM_POST, $course_info['db_name']);
+	$tbl_course_lp_view 	= Database :: get_course_table(TABLE_LP_VIEW, $course_info['db_name']);	
+	$tbl_course_lp 			= Database :: get_course_table(TABLE_LP_MAIN, $course_info['db_name']);
+
+	// get all users data from a course for sortable with limit
+	$condition_user = "";
+	if (is_array($user_ids)) {
+		$condition_user = " WHERE user.user_id IN (".implode(',',$user_ids).") "; 
+	} else {
+		$condition_user = " WHERE user.user_id = '$user_ids' ";
+	}			
+	$sql = "SELECT user.user_id as col0, 
+			user.official_code as col1, 
+			user.lastname as col2, 
+			user.firstname as col3, 
+			count(item_prop.tool) as col4,
+			count(post.post_id) as col5,
+			(SELECT login_course_date FROM $tbl_track_cours_access track_course WHERE track_course.user_id = user.user_id AND course_code = '$course_code' ORDER BY login_course_date DESC LIMIT 0,1) as col6,					
+			(SELECT SUM(UNIX_TIMESTAMP(logout_course_date)-UNIX_TIMESTAMP(login_course_date)) FROM $tbl_track_cours_access track_course WHERE track_course.user_id = user.user_id AND track_course.course_code='$course_code') as col7
+			FROM $tbl_user as user
+			LEFT JOIN $tbl_item_property item_prop ON item_prop.insert_user_id = user.user_id AND item_prop.tool='work'
+			LEFT JOIN $tbl_forum_post  post ON post.poster_id = user.user_id
+			$condition_user
+			GROUP BY col0, col1, col2, col3, col6";
+	if (!in_array($direction, array('ASC','DESC'))) {
+    	$direction = 'ASC';
+    }
+    $column = intval($column);
+    $from = intval($from);
+    $number_of_items = intval($number_of_items);
+	$sql .= " ORDER BY col$column $direction ";
+	$sql .= " LIMIT $from,$number_of_items";	
+	$res = Database::query($sql, __FILE__, __LINE__);	
+	$users = array ();
+    $t = time();
+   	$row = array();
+	while ($user = Database::fetch_row($res)) {
+		
+		$row[0] = $user[1];
+		if ($is_western_name_order) {
+			$row[1] = $user[3];
+			$row[2] = $user[2];
+		} else {
+			$row[1] = $user[2];
+			$row[2] = $user[3];
+		}
+		$row[3] = api_time_to_hms($user[7]);		
+		$avg_student_score = Tracking::get_average_test_scorm_and_lp($user[0], $course_code);
+		$avg_student_progress = Tracking::get_avg_student_progress($user[0], $course_code);			
+		if (empty($avg_student_score)) {$avg_student_score=0;}
+		if (empty($avg_student_progress)) {$avg_student_progress=0;}
+		$row[4] = $avg_student_progress.'%';
+		$row[5] = $avg_student_score.'%';
+		$row[6] = $user[4];
+		$row[7] = $user[5];
+		$row[8] = Tracking::get_first_connection_date_on_the_course($user[0], $course_code);
+		$row[9] = Tracking::get_last_connection_date_on_the_course($user[0], $course_code);
+				
+		// we need to display an additional profile field
+		if (isset($_GET['additional_profile_field']) AND is_numeric($_GET['additional_profile_field'])) {
+			if (is_array($additional_user_profile_info[$user[0]])) {
+				$row[10]=implode(', ', $additional_user_profile_info[$user[0]]);
+			} else {
+				$row[10]='&nbsp;';
+			}
+		}		
+		$row[11] = '<center><a href="../mySpace/myStudents.php?student='.$user[0].'&details=true&course='.$course_code.'&origin=tracking_course"><img src="'.api_get_path(WEB_IMG_PATH).'2rightarrow.gif" border="0" /></a></center>';
+		if ($export_csv) {
+			$row[8] = strip_tags($row[8]);
+			$row[9] = strip_tags($row[9]);
+			unset($row[10]);
+			$csv_content[] = $row;
+		}
+        // store columns in array $users
+        $users[] = array($row[0],$row[1],$row[2],$row[3],$row[4],$row[5],$row[6],$row[7],$row[8],$row[9],$row[10],$row[11]);
+	}
+	return $users;
+}