* @version $Id: exercise.lib.php 22247 2009-07-20 15:57:25Z ivantcholakov $
* Modified by Hubert Borderiou 2011-10-21 Question Category
*/
/**
* Code
*/
// The initialization class for the online editor is needed here.
require_once dirname(__FILE__).'/../inc/lib/fckeditor/fckeditor.php';
$TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
$TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
$TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
$TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER);
$TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
$main_user_table = Database::get_main_table(TABLE_MAIN_USER);
$main_course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
$TBL_TRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
$TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
/**
* Shows a question
*
* @param int question id
* @param bool if true only show the questions, no exercise title
* @param bool origin i.e = learnpath
* @param int current item from the list of questions
* @param int number of total questions
* */
function showQuestion($questionId, $only_questions = false, $origin = false, $current_item = '', $show_title = true, $freeze = false, $user_choice = array()) {
// Text direction for the current language
$is_ltr_text_direction = api_get_text_direction() != 'rtl';
$remind_question = 1;
// Change false to true in the following line to enable answer hinting.
$debug_mark_answer = api_is_allowed_to_edit() && false;
// Reads question information
if (!$objQuestionTmp = Question::read($questionId)) {
// Question not found
return false;
}
$answerType = $objQuestionTmp->selectType();
$pictureName = $objQuestionTmp->selectPicture();
$html = '';
if ($answerType != HOT_SPOT && $answerType != HOT_SPOT_DELINEATION) {
// Question is not a hotspot
if (!$only_questions) {
$questionDescription = $objQuestionTmp->selectDescription();
if ($show_title) {
Testcategory::displayCategoryAndTitle($objQuestionTmp->id); //
echo Display::div($current_item.'. '.$objQuestionTmp->selectTitle(), array('class'=>'question_title'));
}
if (!empty($questionDescription)) {
echo Display::div($questionDescription, array('class'=>'question_description'));
}
//@deprecated
if (!empty($pictureName)) {
//echo "";
}
}
echo '
';
// construction of the Answer object (also gets all answers details)
$objAnswerTmp = new Answer($questionId);
$nbrAnswers = $objAnswerTmp->selectNbrAnswers();
$course_id = api_get_course_int_id();
$quiz_question_options = Question::readQuestionOption($questionId, $course_id);
// For "matching" type here, we need something a little bit special
// because the match between the suggestions and the answers cannot be
// done easily (suggestions and answers are in the same table), so we
// have to go through answers first (elems with "correct" value to 0).
$select_items = array();
//This will contain the number of answers on the left side. We call them
// suggestions here, for the sake of comprehensions, while the ones
// on the right side are called answers
$num_suggestions = 0;
if ($answerType == MATCHING) {
$x = 1; //iterate through answers
$letter = 'A'; //mark letters for each answer
$answer_matching = $cpt1 = array();
$answer_suggestions = $nbrAnswers;
for ($answerId=1; $answerId <= $nbrAnswers; $answerId++) {
$answerCorrect = $objAnswerTmp->isCorrect($answerId);
$numAnswer = $objAnswerTmp->selectAutoId($answerId);
$answer=$objAnswerTmp->selectAnswer($answerId);
if ($answerCorrect==0) {
// options (A, B, C, ...) that will be put into the list-box
// have the "correct" field set to 0 because they are answer
$cpt1[$x] = $letter;
$answer_matching[$x]=$objAnswerTmp->selectAnswerByAutoId($numAnswer);
$x++; $letter++;
}
}
$i = 1;
$select_items[0]['id'] = 0;
$select_items[0]['letter'] = '--';
$select_items[0]['answer'] = '';
foreach ($answer_matching as $id => $value) {
$select_items[$i]['id'] = $value['id'];
$select_items[$i]['letter'] = $cpt1[$id];
$select_items[$i]['answer'] = $value['answer'];
$i ++;
}
$num_suggestions = ($nbrAnswers - $x) + 1;
} elseif ($answerType == FREE_ANSWER) {
$fck_content = isset($user_choice[0]) && !empty($user_choice[0]['answer']) ? $user_choice[0]['answer']:null;
$oFCKeditor = new FCKeditor("choice[".$questionId."]") ;
$oFCKeditor->ToolbarSet = 'TestFreeAnswer';
$oFCKeditor->Width = '100%';
$oFCKeditor->Height = '200';
$oFCKeditor->Value = $fck_content;
$s .= '
';
$s .= $oFCKeditor->CreateHtml();
$s .= '
';
}
// Now navigate through the possible answers, using the max number of
// answers for the question as a limiter
$lines_count = 1; // a counter for matching-type answers
$question_list = array();
if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE || $answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
$header = '';
$header .= Display::tag('th', get_lang('Options'));
foreach ($objQuestionTmp->options as $key=>$item) {
$header .= Display::tag('th', $item);
}
$s.= Display::tag('tr',$header, array('style'=>'text-align:left;'));
}
$matching_correct_answer = 0;
$user_choice_array = array();
if (!empty($user_choice)) {
foreach($user_choice as $item) {
$user_choice_array[] = $item['answer'];
}
}
for ($answerId=1; $answerId <= $nbrAnswers; $answerId++) {
$answer = $objAnswerTmp->selectAnswer($answerId);
$answerCorrect = $objAnswerTmp->isCorrect($answerId);
$numAnswer = $objAnswerTmp->selectAutoId($answerId);
// Unique answer
if ($answerType == UNIQUE_ANSWER || $answerType == UNIQUE_ANSWER_NO_OPTION) {
// set $debug_mark_answer to true at function start to
// show the correct answer with a suffix '-x'
$help = $selected = '';
if ($debug_mark_answer) {
if ($answerCorrect) {
$help = 'x-';
$selected = 'checked';
}
}
$input_id = 'choice-'.$questionId.'-'.$answerId;
if (isset($user_choice[0]['answer']) && $user_choice[0]['answer'] == $numAnswer ) {
$attributes = array('id' =>$input_id, 'class'=>'checkbox','checked'=>1, 'selected'=>1);
} else {
$attributes = array('id' =>$input_id, 'class'=>'checkbox');
}
$answer = Security::remove_XSS($answer, STUDENT);
$s .= Display::input('hidden','choice2['.$questionId.']','0');
//@todo fix $is_ltr_text_direction
//
';
} elseif ($answerType == FILL_IN_BLANKS) {
/*
// splits text and weightings that are joined with the character '::'
list($answer) = explode('::',$answer);
//getting the matches
$answer = api_ereg_replace('\[[^]]+\]','',($answer));
$answer = api_preg_replace('/\[[^]]+\]/', Display::input('text', "choice[$questionId][]", '', $attributes), $answer);
api_preg_match_all('/\[[^]]+\]/', $answer, $fill_list);
if (isset($user_choice[0]['answer'])) {
api_preg_match_all('/\[[^]]+\]/', $user_choice[0]['answer'], $user_fill_list);
$user_fill_list = $user_fill_list[0];
}*/
list($answer) = explode('::',$answer);
api_preg_match_all('/\[[^]]+\]/', $answer, $teacher_answer_list);
if (isset($user_choice[0]['answer'])) {
api_preg_match_all('/\[[^]]+\]/', $user_choice[0]['answer'], $student_answer_list);
$student_answer_list = $student_answer_list[0];
}
//var_dump($teacher_answer_list, $student_answer_list);
if (!empty($teacher_answer_list) && !empty($student_answer_list)) {
$teacher_answer_list = $teacher_answer_list[0];
$i = 0;
foreach($teacher_answer_list as $teacher_item) {
$value = null;
if (isset($student_answer_list[$i]) && !empty($student_answer_list[$i])) {
//Cleaning student answer list
$value = strip_tags($student_answer_list[$i]);
$value = api_substr($value,1, api_strlen($value)-2);
$value = explode('/', $value);
if (!empty($value[0])) {
$value = trim($value[0]);
$value = str_replace(' ', '', $value);
}
$answer = api_preg_replace('/\['.$teacher_item.'+\]/', Display::input('text', "choice[$questionId][]", $value), $answer);
}
$i++;
}
} else {
$answer = api_preg_replace('/\[[^]]+\]/', Display::input('text', "choice[$questionId][]", '', $attributes), $answer);
}
$s .= '
'.$answer.'
';
} elseif ($answerType == MATCHING) {
// matching type, showing suggestions and answers
// TODO: replace $answerId by $numAnswer
if ($answerCorrect != 0) {
// only show elements to be answered (not the contents of
// the select boxes, who are corrrect = 0)
$s .= '
';
$lines_count++;
//if the left side of the "matching" has been completely
// shown but the right side still has values to show...
if (($lines_count -1) == $num_suggestions) {
// if it remains answers to shown at the right side
while (isset($select_items[$lines_count])) {
$s .= '
";
$lines_count++;
} // end while()
} // end if()
$matching_correct_answer++;
}
}
} // end for()
$s .= '
';
$s .= '';
// destruction of the Answer object
unset($objAnswerTmp);
// destruction of the Question object
unset($objQuestionTmp);
if ($origin != 'export') {
echo $s;
} else {
return $s;
}
} elseif ($answerType == HOT_SPOT || $answerType == HOT_SPOT_DELINEATION) {
// Question is a HOT_SPOT
//checking document/images visibility
if (api_is_platform_admin() || api_is_course_admin()) {
require_once api_get_path(LIBRARY_PATH).'document.lib.php';
$course = api_get_course_info();
$doc_id = DocumentManager::get_document_id($course, '/images/'.$pictureName);
if (is_numeric($doc_id)) {
$images_folder_visibility = api_get_item_visibility($course,'document', $doc_id, api_get_session_id());
if (!$images_folder_visibility) {
//This message is shown only to the course/platform admin if the image is set to visibility = false
Display::display_warning_message(get_lang('ChangeTheVisibilityOfTheCurrentImage'));
}
}
}
$questionName = $objQuestionTmp->selectTitle();
$questionDescription = $objQuestionTmp->selectDescription();
if ($freeze) {
echo Display::img($objQuestionTmp->selectPicturePath());
return;
}
// Get the answers, make a list
$objAnswerTmp = new Answer($questionId);
$nbrAnswers = $objAnswerTmp->selectNbrAnswers();
// get answers of hotpost
$answers_hotspot = array();
for ($answerId=1;$answerId <= $nbrAnswers;$answerId++) {
$answers = $objAnswerTmp->selectAnswerByAutoId($objAnswerTmp->selectAutoId($answerId));
$answers_hotspot[$answers['id']] = $objAnswerTmp->selectAnswer($answerId);
}
// display answers of hotpost order by id
$answer_list = '
'.get_lang('HotspotZones').'
';
if (!empty($answers_hotspot)) {
ksort($answers_hotspot);
foreach ($answers_hotspot as $key => $value) {
$answer_list .= '
';
// if the float look like 10.00 we show only 10
$my_res = $results[$i]['exresult'];
$my_total = $results[$i]['exweight'];
if (!$results[$i]['propagate_neg'] && $my_res < 0) {
$my_res = 0;
}
$ex = show_score($my_res, $my_total);
$result_list = $ex;
$html_link = '';
if ($is_allowedToEdit || $is_tutor) {
if (in_array($results[$i]['excruid'], $teacher_id_list)) {
$html_link.= Display::return_icon('teachers.gif', get_lang('Teacher'));
}
if ($revised) {
$html_link.= "".Display :: return_icon('edit.png', get_lang('Edit'), array(), 22);
$html_link.= ' ';
} else {
$html_link.="".Display :: return_icon('quiz.gif', get_lang('Qualify'));
$html_link.=' ';
}
$html_link.="";
if ($is_allowedToEdit) {
if ($filter==2){
$html_link.=' ' .Display :: return_icon('history.gif', get_lang('ViewHistoryChange')).'';
}
}
if (api_is_platform_admin() || $is_tutor) {
$html_link.=' '.Display :: return_icon('delete.png', get_lang('Delete')).'';
$html_link.=' ';
}
} else {
$attempt_url = api_get_path(WEB_CODE_PATH).'exercice/result.php?'.api_get_cidreq().'&id='.$results[$i]['exid'].'&id_session='.api_get_session_id().'&height=500&width=750';
$attempt_link = Display::url(get_lang('Show'), $attempt_url, array('class'=>'thickbox'))." ";
$html_link.= $attempt_link;
if ($revised) {
$html_link.= Display::span(get_lang('Validated'), array('class'=>'label_tag notice'));
} else {
$html_link.= Display::span(get_lang('NotValidated'), array('class'=>'label_tag notice'));
}
}
$more_details_list = $html_link;
if ($is_allowedToEdit || $is_tutor) {
$list_info[] = array($user_first_name,$user_last_name,$user_login,$user_groups,$quiz_name_list,$duration_list,$date_list,$result_list,$more_details_list);
} else {
$list_info[] = array($quiz_name_list,$duration_list,$date_list,$result_list,$more_details_list);
}
}
}
}
} else {
//echo $hpsql; var_dump($hpsql);
$hpresults = getManyResultsXCol($hpsql, 6);
// Print HotPotatoes test results.
if (is_array($hpresults)) {
for ($i = 0; $i < sizeof($hpresults); $i++) {
$hp_title = GetQuizName($hpresults[$i][3], $documentPath);
if ($hp_title == '') {
$hp_title = basename($hpresults[$i][3]);
}
//var_dump($hpresults[$i]);
$hp_date = api_get_local_time($hpresults[$i][6], null, date_default_timezone_get());
$hp_result = round(($hpresults[$i][4] / ($hpresults[$i][5] != 0 ? $hpresults[$i][5] : 1)) * 100, 2).'% ('.$hpresults[$i][4].' / '.$hpresults[$i][5].')';
if ($is_allowedToEdit || $is_tutor) {
$list_info[] = array($hpresults[$i][0], $hpresults[$i][1], $hpresults[$i][2], '', $hp_title, '-', $hp_date , $hp_result , '-');
} else {
$list_info[] = array($hp_title, '-', $hp_date , $hp_result , '-');
}
}
}
}
return $list_info;
}
/**
* Converts the score with the exercise_max_note and exercise_min_score the platform settings + formats the results using the float_format function
*
* @param float score
* @param float weight
* @param bool show porcentage or not
* @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) {
if (is_null($score) && is_null($weight)) {
return '-';
}
$html = '';
$score_rounded = $score;
$max_note = api_get_setting('exercise_max_score');
$min_note = api_get_setting('exercise_min_score');
if ($use_platform_settings) {
if ($max_note != '' && $min_note != '') {
if (!empty($weight) && intval($weight) != 0) {
$score = $min_note + ($max_note - $min_note) * $score /$weight;
} else {
$score = $min_note;
}
$weight = $max_note;
}
}
$score_rounded = float_format($score, 1);
$weight = float_format($weight, 1);
if ($show_percentage) {
$parent = '(' . $score_rounded . ' / ' . $weight . ')';
$html = float_format(($score / ($weight != 0 ? $weight : 1)) * 100, 1) . "% $parent";
} else {
$html = $score_rounded . ' / ' . $weight;
}
return $html;
}
/**
* Converts a numeric value in a percentage example 0.66666 to 66.67 %
* @param $value
* @return float Converted number
*/
function convert_to_percentage($value) {
$return = '-';
if ($value != '') {
$return = float_format($value * 100, 1).' %';
}
return $return;
}
/**
* Converts a score/weight values to the platform scale
* @param float score
* @param float weight
* @return float the score rounded converted to the new range
*/
function convert_score($score, $weight) {
$max_note = api_get_setting('exercise_max_score');
$min_note = api_get_setting('exercise_min_score');
if ($score != '' && $weight != '') {
if ($max_note != '' && $min_note != '') {
if (!empty($weight)) {
$score = $min_note + ($max_note - $min_note) * $score / $weight;
} else {
$score = $min_note;
}
}
}
$score_rounded = float_format($score, 1);
return $score_rounded;
}
/**
* Getting all active exercises from a course from a session (if a session_id is provided we will show all the exercises in the course + all exercises in the session)
* @param array course data
* @param int session id
* @return array array with exercise data
*/
function get_all_exercises($course_info = null, $session_id = 0, $check_dates = false) {
$TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST);
$course_id = api_get_course_int_id();
if (!empty($course_info) && !empty($course_info['real_id'])) {
$course_id = $course_info['real_id'];
}
if ($session_id == -1) {
$session_id = 0;
}
if ($session_id == 0) {
$conditions = array('where'=>array('active = ? AND session_id = ? AND c_id = ? '=>array('1', $session_id, $course_id)), 'order'=>'title');
} else {
//All exercises
$conditions = array('where'=>array('active = ? AND (session_id = 0 OR session_id = ? ) AND c_id = ? ' => array('1', $session_id, $course_id)), 'order'=>'title');
}
return Database::select('*',$TBL_EXERCICES, $conditions);
}
/**
* Getting all active exercises from a course from a session (if a session_id is provided we will show all the exercises in the course + all exercises in the session)
* @param array course data
* @param int session id
* @param int course c_id
* @return array array with exercise data
* modified by Hubert Borderiou
*/
function get_all_exercises_for_course_id($course_info = null, $session_id = 0, $course_id=0) {
$TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST);
if ($session_id == -1) {
$session_id = 0;
}
if ($session_id == 0) {
$conditions = array('where'=>array('active = ? AND session_id = ? AND c_id = ?'=>array('1', $session_id, $course_id)), 'order'=>'title');
} else {
//All exercises
$conditions = array('where'=>array('active = ? AND (session_id = 0 OR session_id = ? ) AND c_id=?' =>array('1', $session_id, $course_id)), 'order'=>'title');
}
return Database::select('*',$TBL_EXERCICES, $conditions);
}
/**
* Gets the position of the score based in a given score (result/weight) and the exe_id based in the user list
* (NO Exercises in LPs )
* @param float user score to be compared *attention* $my_score = score/weight and not just the score
* @param int exe id of the exercise (this is necesary because if 2 students have the same score the one with the minor exe_id will have a best position, just to be fair and FIFO)
* @param int exercise id
* @param string course code
* @param int session id
* @return int the position of the user between his friends in a course (or course within a session)
*/
function get_exercise_result_ranking($my_score, $my_exe_id, $exercise_id, $course_code, $session_id = 0, $user_list, $return_string = true) {
//No score given we return
if (is_null($my_score)) {
return '-';
}
if (empty($user_list)) {
return '-';
}
$best_attempts = array();
foreach($user_list as $user_data) {
$user_id = $user_data['user_id'];
$best_attempts[$user_id]= get_best_attempt_by_user($user_id, $exercise_id, $course_code, $session_id);
}
$position_data = array();
if (empty($best_attempts)) {
return 1;
} else {
$position = 1;
$my_ranking = array();
foreach($best_attempts as $user_id => $result) {
if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
$my_ranking[$user_id] = $result['exe_result']/$result['exe_weighting'];
} else {
$my_ranking[$user_id] = 0;
}
}
//if (!empty($my_ranking)) {
asort($my_ranking);
$position = count($my_ranking);
if (!empty($my_ranking)) {
foreach($my_ranking as $user_id => $ranking) {
if ($my_score >= $ranking) {
if ($my_score == $ranking) {
$exe_id = $best_attempts[$user_id]['exe_id'];
if ($my_exe_id < $exe_id) {
$position--;
}
} else {
$position--;
}
}
}
}
//}
$return_value = array('position'=>$position, 'count'=>count($my_ranking));
//var_dump($my_score, $my_ranking);
if ($return_string) {
if (!empty($position) && !empty($my_ranking)) {
$return_value = $position.'/'.count($my_ranking);
} else {
$return_value = '-';
}
}
return $return_value;
}
}
/**
* Gets the position of the score based in a given score (result/weight) and the exe_id based in all attempts
* (NO Exercises in LPs ) old funcionality by attempt
* @param float user score to be compared attention => score/weight
* @param int exe id of the exercise (this is necesary because if 2 students have the same score the one with the minor exe_id will have a best position, just to be fair and FIFO)
* @param int exercise id
* @param string course code
* @param int session id
* @return int the position of the user between his friends in a course (or course within a session)
*/
function get_exercise_result_ranking_by_attempt($my_score, $my_exe_id, $exercise_id, $course_code, $session_id = 0, $return_string = true) {
if (empty($session_id)) {
$session_id = 0;
}
if (is_null($my_score)) {
return '-';
}
$user_results = get_all_exercise_results($exercise_id, $course_code, $session_id, false);
$position_data = array();
if (empty($user_results)) {
return 1;
} else {
$position = 1;
$my_ranking = array();
foreach($user_results as $result) {
//print_r($result);
if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
$my_ranking[$result['exe_id']] = $result['exe_result']/$result['exe_weighting'];
} else {
$my_ranking[$result['exe_id']] = 0;
}
}
asort($my_ranking);
$position = count($my_ranking);
if (!empty($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--;
}
}
}
}
$return_value = array('position'=>$position, 'count'=>count($my_ranking));
//var_dump($my_score, $my_ranking);
if ($return_string) {
if (!empty($position) && !empty($my_ranking)) {
return $position.'/'.count($my_ranking);
}
}
return $return_value;
}
}
/*
* Get the best attempt in a exercise (NO Exercises in LPs )
*/
function get_best_attempt_in_course($exercise_id, $course_code, $session_id) {
$user_results = get_all_exercise_results($exercise_id, $course_code, $session_id, false);
$best_score_data = array();
$best_score = 0;
if (!empty($user_results)) {
foreach($user_results as $result) {
if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
$score = $result['exe_result']/$result['exe_weighting'];
if ($score >= $best_score) {
$best_score = $score;
$best_score_data = $result;
}
}
}
}
return $best_score_data;
}
/*
* Get the best score in a exercise (NO Exercises in LPs )
*/
function get_best_attempt_by_user($user_id, $exercise_id, $course_code, $session_id) {
$user_results = get_all_exercise_results($exercise_id, $course_code, $session_id, false);
$best_score_data = array();
$best_score = 0;
if (!empty($user_results)) {
foreach($user_results as $result) {
if ($result['exe_user_id'] != $user_id) continue;
if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
$score = $result['exe_result']/$result['exe_weighting'];
if ($score >= $best_score) {
$best_score = $score;
$best_score_data = $result;
}
}
}
}
return $best_score_data;
}
/**
* Get average score (NO Exercises in LPs )
* @param int exercise id
* @param string course code
* @param int session id
* @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();
$avg_score = 0;
if (!empty($user_results)) {
foreach($user_results as $result) {
if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
$score = $result['exe_result']/$result['exe_weighting'];
$avg_score +=$score;
}
}
$avg_score = float_format($avg_score / count($user_results), 1);
}
return $avg_score;
}
/**
* Get average score by score (NO Exercises in LPs )
* @param int exercise id
* @param string course code
* @param int session id
* @return float Average score
*/
function get_average_score_by_course($course_code, $session_id) {
$user_results = get_all_exercise_results_by_course($course_code, $session_id, false);
//echo $course_code.' - '.$session_id.' ';
$avg_score = 0;
if (!empty($user_results)) {
foreach($user_results as $result) {
if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
$score = $result['exe_result']/$result['exe_weighting'];
//var_dump($score);
$avg_score +=$score;
}
}
//We asume that all exe_weighting
//$avg_score = show_score( $avg_score / count($user_results) , $result['exe_weighting']);
$avg_score = ($avg_score / count($user_results));
}
//var_dump($avg_score);
return $avg_score;
}
function get_average_score_by_course_by_user($user_id, $course_code, $session_id) {
$user_results = get_all_exercise_results_by_user($user_id, $course_code, $session_id);
$avg_score = 0;
if (!empty($user_results)) {
foreach($user_results as $result) {
if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
$score = $result['exe_result']/$result['exe_weighting'];
$avg_score +=$score;
}
}
//We asume that all exe_weighting
//$avg_score = show_score( $avg_score / count($user_results) , $result['exe_weighting']);
$avg_score = ($avg_score / count($user_results));
}
return $avg_score;
}
/**
* Get average score by score (NO Exercises in LPs )
* @param int exercise id
* @param string course code
* @param int session id
* @return float Best average score
*/
function get_best_average_score_by_exercise($exercise_id, $course_code, $session_id, $user_count) {
$user_results = get_best_exercise_results_by_user($exercise_id, $course_code, $session_id);
$avg_score = 0;
if (!empty($user_results)) {
foreach($user_results as $result) {
if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
$score = $result['exe_result']/$result['exe_weighting'];
$avg_score +=$score;
}
}
//We asume that all exe_weighting
//$avg_score = show_score( $avg_score / count($user_results) , $result['exe_weighting']);
//$avg_score = ($avg_score / count($user_results));
if(!empty($user_count)) {
$avg_score = float_format($avg_score / $user_count, 1) * 100;
} else {
$avg_score = 0;
}
}
return $avg_score;
}
function get_exercises_to_be_taken($course_code, $session_id) {
$course_info = api_get_course_info($course_code);
$exercises = get_all_exercises($course_info, $session_id);
$result = array();
$now = time() + 15*24*60*60;
foreach($exercises as $exercise_item) {
if (isset($exercise_item['end_time']) && !empty($exercise_item['end_time']) && $exercise_item['end_time'] != '0000-00-00 00:00:00' && api_strtotime($exercise_item['end_time']) < $now) {
$result[] = $exercise_item;
}
}
return $result;
}
/**
* Get student results (only in completed exercises) stats by question
* @param int question id
* @param int exercise id
* @param string course code
* @param int session id
*
* */
function get_student_stats_by_question($question_id, $exercise_id, $course_code, $session_id) {
$track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
$track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
$question_id = intval($question_id);
$exercise_id = intval($exercise_id);
$course_code = Database::escape_string($course_code);
$session_id = intval($session_id);
$sql = "SELECT count(exe_user_id) as users, MAX(marks) as max , MIN(marks) as min, AVG(marks) as average
FROM $track_exercises e INNER JOIN $track_attempt a ON (a.exe_id = e.exe_id)
WHERE exe_exo_id = $exercise_id AND
course_code = '$course_code' AND
e.session_id = $session_id AND
question_id = $question_id AND status = '' LIMIT 1";
$result = Database::query($sql);
$return = array();
if ($result) {
$return = Database::fetch_array($result, 'ASSOC');
}
return $return;
}
// ---------------------------------------------------------
// return the HTML code for a menu with students group
// @input : $in_name : is the name and the id of the