Advertisement
Guest User

exercise.lib.php

a guest
Apr 18th, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 92.62 KB | None | 0 0
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4.  * Exercise library
  5.  * @todo convert this lib into a static class
  6.  *
  7.  * shows a question and its answers
  8.  * @package chamilo.exercise
  9.  * @author Olivier Brouckaert <oli.brouckaert@skynet.be>
  10.  * @version $Id: exercise.lib.php 22247 2009-07-20 15:57:25Z ivantcholakov $
  11.  * Modified by Hubert Borderiou 2011-10-21 Question Category
  12.  */
  13. /**
  14.  * Code
  15.  */
  16. // The initialization class for the online editor is needed here.
  17. require_once dirname(__FILE__).'/../inc/lib/fckeditor/fckeditor.php';
  18.  
  19. /**
  20.  * Shows a question
  21.  *
  22.  * @param int   question id
  23.  * @param bool  if true only show the questions, no exercise title
  24.  * @param bool  origin i.e = learnpath
  25.  * @param int   current item from the list of questions
  26.  * @param int   number of total questions
  27.  * */
  28. function showQuestion($questionId, $only_questions = false, $origin = false, $current_item = '', $show_title = true, $freeze = false, $user_choice = array(), $show_comment = false, $exercise_feedback = null, $show_answers = false) {
  29.  
  30.     // Text direction for the current language
  31.     $is_ltr_text_direction = api_get_text_direction() != 'rtl';
  32.  
  33.     // Change false to true in the following line to enable answer hinting
  34.     $debug_mark_answer = $show_answers; //api_is_allowed_to_edit() && false;
  35.  
  36.     // Reads question information
  37.     if (!$objQuestionTmp = Question::read($questionId)) {
  38.         // Question not found
  39.         return false;
  40.     }
  41.  
  42.     if ($exercise_feedback != EXERCISE_FEEDBACK_TYPE_END) {
  43.         $show_comment = false;
  44.     }
  45.  
  46.     $answerType    = $objQuestionTmp->selectType();
  47.     $pictureName   = $objQuestionTmp->selectPicture();
  48.  
  49.     if ($answerType != HOT_SPOT && $answerType != HOT_SPOT_DELINEATION) {
  50.         // Question is not a hotspot
  51.  
  52.         if (!$only_questions) {
  53.             $questionDescription = $objQuestionTmp->selectDescription();
  54.             if ($show_title) {
  55.                 Testcategory::displayCategoryAndTitle($objQuestionTmp->id); //
  56.                 echo Display::div($current_item.'. '.$objQuestionTmp->selectTitle(), array('class'=>'question_title'));
  57.             }
  58.             if (!empty($questionDescription)) {
  59.                 echo Display::div($questionDescription, array('class'=>'question_description'));
  60.             }
  61.         }
  62.  
  63.         if (in_array($answerType, array(FREE_ANSWER, ORAL_EXPRESSION)) && $freeze) {
  64.             return '';
  65.         }
  66.  
  67.         echo '<div class="question_options">';
  68.  
  69.         $s = '';
  70.  
  71.         // construction of the Answer object (also gets all answers details)
  72.         $objAnswerTmp = new Answer($questionId);
  73.  
  74.         $nbrAnswers     = $objAnswerTmp->selectNbrAnswers();
  75.         $course_id      = api_get_course_int_id();
  76.         $quiz_question_options = Question::readQuestionOption($questionId, $course_id);
  77.  
  78.         // For "matching" type here, we need something a little bit special
  79.         // because the match between the suggestions and the answers cannot be
  80.         // done easily (suggestions and answers are in the same table), so we
  81.         // have to go through answers first (elems with "correct" value to 0).
  82.         $select_items = array();
  83.         //This will contain the number of answers on the left side. We call them
  84.         // suggestions here, for the sake of comprehensions, while the ones
  85.         // on the right side are called answers
  86.         $num_suggestions = 0;
  87.  
  88.         if ($answerType == MATCHING) {
  89.             $s .= '<table class="data_table">';
  90.  
  91.             $x = 1; //iterate through answers
  92.             $letter = 'A'; //mark letters for each answer
  93.             $answer_matching = $cpt1 = array();
  94.  
  95.             for ($answerId=1; $answerId <= $nbrAnswers; $answerId++) {
  96.                 $answerCorrect = $objAnswerTmp->isCorrect($answerId);
  97.                 $numAnswer = $objAnswerTmp->selectAutoId($answerId);
  98.                 $answer=$objAnswerTmp->selectAnswer($answerId);
  99.                 if ($answerCorrect==0) {
  100.                     // options (A, B, C, ...) that will be put into the list-box
  101.                     // have the "correct" field set to 0 because they are answer
  102.                     $cpt1[$x] = $letter;
  103.                     $answer_matching[$x]=$objAnswerTmp->selectAnswerByAutoId($numAnswer);
  104.                     $x++; $letter++;
  105.                 }
  106.             }
  107.             $i = 1;
  108.  
  109.             $select_items[0]['id'] = 0;
  110.             $select_items[0]['letter'] = '--';
  111.             $select_items[0]['answer'] = '';
  112.  
  113.             foreach ($answer_matching as $id => $value) {
  114.                 $select_items[$i]['id']     = $value['id'];
  115.                 $select_items[$i]['letter'] = $cpt1[$id];
  116.                 $select_items[$i]['answer'] = $value['answer'];
  117.                 $i ++;
  118.             }
  119.             $num_suggestions = ($nbrAnswers - $x) + 1;
  120.  
  121.         } elseif ($answerType == FREE_ANSWER) {
  122.             $fck_content = isset($user_choice[0]) && !empty($user_choice[0]['answer']) ? $user_choice[0]['answer']:null;
  123.  
  124.             $oFCKeditor = new FCKeditor("choice[".$questionId."]") ;
  125.  
  126.             $oFCKeditor->ToolbarSet = 'TestFreeAnswer';
  127.             $oFCKeditor->Width      = '100%';
  128.             $oFCKeditor->Height     = '200';
  129.             $oFCKeditor->Value      = $fck_content;
  130.             $s .= $oFCKeditor->CreateHtml();
  131.         } elseif ($answerType == ORAL_EXPRESSION) {
  132.             //Add nanog
  133.             if (api_get_setting('enable_nanogong') == 'true') {
  134.                 require_once api_get_path(LIBRARY_PATH).'nanogong.lib.php';
  135.                 //@todo pass this as a parameter
  136.                 global $exercise_stat_info, $exerciseId, $exe_id;
  137.  
  138.                 if (!empty($exercise_stat_info)) {
  139.                     $params = array(
  140.                         'exercise_id'   => $exercise_stat_info['exe_exo_id'],
  141.                         'exe_id'        => $exercise_stat_info['exe_id'],
  142.                         'question_id'   => $questionId
  143.                     );
  144.                 } else {
  145.                     $params = array(
  146.                         'exercise_id'   => $exerciseId,
  147.                         'exe_id'        => 'temp_exe',
  148.                         'question_id'   => $questionId
  149.                     );
  150.                 }
  151.                 $nano = new Nanogong($params);
  152.                 echo $nano->show_button();
  153.             }
  154.  
  155.             $oFCKeditor = new FCKeditor("choice[".$questionId."]") ;
  156.             $oFCKeditor->ToolbarSet = 'TestFreeAnswer';
  157.             $oFCKeditor->Width  = '100%';
  158.             $oFCKeditor->Height = '150';
  159.             $oFCKeditor->ToolbarStartExpanded = false;
  160.             $oFCKeditor->Value  = '' ;
  161.             $s .= $oFCKeditor->CreateHtml();
  162.         }
  163.  
  164.         // Now navigate through the possible answers, using the max number of
  165.         // answers for the question as a limiter
  166.         $lines_count = 1; // a counter for matching-type answers
  167.  
  168.         if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE || $answerType ==  MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
  169.             $header = Display::tag('th', get_lang('Options'));
  170.             foreach ($objQuestionTmp->options as $key=>$item) {
  171.                 $header .= Display::tag('th', $item);
  172.             }
  173.             if ($show_comment) {
  174.                 $header .= Display::tag('th', get_lang('Feedback'));
  175.             }
  176.             $s .= '<table class="data_table">';
  177.             $s .= Display::tag('tr', $header, array('style'=>'text-align:left;'));
  178.         }
  179.  
  180.         if ($show_comment) {
  181.             if (in_array($answerType, array(MULTIPLE_ANSWER, MULTIPLE_ANSWER_COMBINATION, UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION, GLOBAL_MULTIPLE_ANSWER))) {
  182.                 $header = Display::tag('th', get_lang('Options'));
  183.                 if ($exercise_feedback == EXERCISE_FEEDBACK_TYPE_END) {
  184.                     $header .= Display::tag('th', get_lang('Feedback'));
  185.                 }
  186.                 $s .= '<table class="data_table">';
  187.                 $s.= Display::tag('tr',$header, array('style'=>'text-align:left;'));
  188.             }
  189.         }
  190.  
  191.         $matching_correct_answer = 0;
  192.         $user_choice_array = array();
  193.         if (!empty($user_choice)) {
  194.             foreach($user_choice as $item) {
  195.                 $user_choice_array[] = $item['answer'];
  196.             }
  197.         }
  198.  
  199.         for ($answerId=1; $answerId <= $nbrAnswers; $answerId++) {
  200.             $answer          = $objAnswerTmp->selectAnswer($answerId);
  201.             $answerCorrect   = $objAnswerTmp->isCorrect($answerId);
  202.             $numAnswer       = $objAnswerTmp->selectAutoId($answerId);
  203.             $comment         = $objAnswerTmp->selectComment($answerId);
  204.  
  205.             $attributes = array();
  206.  
  207.             // Unique answer
  208.             if ($answerType == UNIQUE_ANSWER || $answerType == UNIQUE_ANSWER_NO_OPTION) {
  209.                 $input_id = 'choice-'.$questionId.'-'.$answerId;
  210.                 if (isset($user_choice[0]['answer']) && $user_choice[0]['answer'] == $numAnswer ) {
  211.                     $attributes = array('id' =>$input_id, 'checked'=>1, 'selected'=>1);
  212.                 } else {
  213.                     $attributes = array('id' =>$input_id);
  214.                 }
  215.  
  216.                 if ($debug_mark_answer) {
  217.                     if ($answerCorrect) {
  218.                         $attributes['checked'] = 1;
  219.                         $attributes['selected'] = 1;
  220.                     }
  221.                 }
  222.  
  223.                 $answer = Security::remove_XSS($answer, STUDENT);
  224.  
  225.                 $s .= Display::input('hidden','choice2['.$questionId.']','0');
  226.  
  227.                 $answer_input = '<label class="radio">';
  228.                 $answer_input .= Display::input('radio', 'choice['.$questionId.']', $numAnswer, $attributes);
  229.                 $answer_input .= $answer;
  230.                 $answer_input .= '</label>';
  231.  
  232.                 if ($show_comment) {
  233.                     $s .= '<tr><td>';
  234.                     $s .= $answer_input;
  235.                     $s .= '</td>';
  236.                     $s .= '<td>';
  237.                     $s .= $comment;
  238.                     $s .= '</td>';
  239.                     $s .= '</tr>';
  240.                 } else {
  241.                     $s .= $answer_input;
  242.                 }
  243.  
  244.             } elseif ($answerType == MULTIPLE_ANSWER || $answerType == MULTIPLE_ANSWER_TRUE_FALSE || $answerType == GLOBAL_MULTIPLE_ANSWER) {
  245.                 $input_id = 'choice-'.$questionId.'-'.$answerId;
  246.                 $answer = Security::remove_XSS($answer, STUDENT);
  247.  
  248.                 if (in_array($numAnswer, $user_choice_array)) {
  249.                     $attributes = array('id' =>$input_id, 'checked'=>1, 'selected'=>1);
  250.                 } else {
  251.                     $attributes = array('id' =>$input_id);
  252.                 }
  253.  
  254.                 if ($debug_mark_answer) {
  255.                     if ($answerCorrect) {
  256.                         $attributes['checked'] = 1;
  257.                         $attributes['selected'] = 1;
  258.                     }
  259.                 }
  260.  
  261.                 if ($answerType == MULTIPLE_ANSWER || $answerType == GLOBAL_MULTIPLE_ANSWER) {
  262.                     $s .= '<input type="hidden" name="choice2['.$questionId.']" value="0" />';
  263.  
  264.                     $answer_input = '<label class="checkbox">';
  265.                     $answer_input .= Display::input('checkbox', 'choice['.$questionId.']['.$numAnswer.']', $numAnswer, $attributes);
  266.                     $answer_input .= $answer;
  267.                     $answer_input .= '</label>';
  268.  
  269.                     if ($show_comment) {
  270.                         $s .= '<tr><td>';
  271.                         $s .= $answer_input;
  272.                         $s .= '</td>';
  273.                         $s .= '<td>';
  274.                         $s .= $comment;
  275.                         $s .= '</td>';
  276.                         $s .='</tr>';
  277.                     } else {
  278.                         $s .= $answer_input;
  279.                     }
  280.                 } elseif ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
  281.  
  282.                     $my_choice = array();
  283.                     if (!empty($user_choice_array)) {
  284.                         foreach ($user_choice_array as $item) {
  285.                             $item = explode(':', $item);
  286.                             $my_choice[$item[0]] = $item[1];
  287.                         }
  288.                     }
  289.  
  290.                     $s .= '<tr>';
  291.                     $s .= Display::tag('td', $answer);
  292.  
  293.                     if (!empty($quiz_question_options)) {
  294.                         foreach ($quiz_question_options as $id => $item) {
  295.  
  296.                             if (isset($my_choice[$numAnswer]) && $id == $my_choice[$numAnswer]) {
  297.                                 $attributes = array('checked'=>1, 'selected'=>1);
  298.                             } else {
  299.                                 $attributes = array();
  300.                             }
  301.  
  302.                             if ($debug_mark_answer) {
  303.                                 if ($id == $answerCorrect) {
  304.                                     $attributes['checked'] = 1;
  305.                                     $attributes['selected'] = 1;
  306.                                 }
  307.                             }
  308.                             $s .= Display::tag('td', Display::input('radio', 'choice['.$questionId.']['.$numAnswer.']', $id, $attributes), array('style'=>''));
  309.                         }
  310.                     }
  311.  
  312.                     if ($show_comment) {
  313.                         $s .= '<td>';
  314.                         $s .= $comment;
  315.                         $s .= '</td>';
  316.                     }
  317.                     $s.='</tr>';
  318.                 }
  319.             } elseif ($answerType == MULTIPLE_ANSWER_COMBINATION) {
  320.                 // multiple answers
  321.                 $input_id = 'choice-'.$questionId.'-'.$answerId;
  322.  
  323.                 if (in_array($numAnswer, $user_choice_array)) {
  324.                     $attributes = array('id'=>$input_id, 'checked'=>1, 'selected'=>1);
  325.                 } else {
  326.                     $attributes = array('id'=>$input_id);
  327.                 }
  328.  
  329.                 if ($debug_mark_answer) {
  330.                     if ($answerCorrect) {
  331.                         $attributes['checked'] = 1;
  332.                         $attributes['selected'] = 1;
  333.                     }
  334.                 }
  335.  
  336.                 $answer = Security::remove_XSS($answer, STUDENT);
  337.                 $answer_input = '<input type="hidden" name="choice2['.$questionId.']" value="0" />';
  338.                 $answer_input .= '<label class="checkbox">';
  339.                 $answer_input .= Display::input('checkbox', 'choice['.$questionId.']['.$numAnswer.']', 1, $attributes);
  340.                 $answer_input .= $answer;
  341.                 $answer_input .= '</label>';
  342.  
  343.                 if ($show_comment) {
  344.                     $s.= '<tr>';
  345.                     $s .= '<td>';
  346.                     $s.= $answer_input;
  347.                     $s .= '</td>';
  348.                     $s .= '<td>';
  349.                     $s .= $comment;
  350.                     $s .= '</td>';
  351.                     $s.= '</tr>';
  352.                 } else {
  353.                     $s.= $answer_input;
  354.                 }
  355.  
  356.             } elseif ($answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
  357.                 $s .= '<input type="hidden" name="choice2['.$questionId.']" value="0" />';
  358.  
  359.                 $my_choice = array();
  360.                 if (!empty($user_choice_array)) {
  361.                     foreach ($user_choice_array as $item) {
  362.                         $item = explode(':', $item);
  363.                         $my_choice[$item[0]] = $item[1];
  364.                     }
  365.                 }
  366.                 $answer = Security::remove_XSS($answer, STUDENT);
  367.                 $s .='<tr>';
  368.                 $s .= Display::tag('td', $answer);
  369.  
  370.                 foreach ($objQuestionTmp->options as $key => $item) {
  371.                     if (isset($my_choice[$numAnswer]) && $key == $my_choice[$numAnswer]) {
  372.                         $attributes = array('checked' => 1, 'selected' => 1);
  373.                     } else {
  374.                         $attributes = array();
  375.                     }
  376.  
  377.                     if ($debug_mark_answer) {
  378.                         if ($key == $answerCorrect) {
  379.                             $attributes['checked']  = 1;
  380.                             $attributes['selected'] = 1;
  381.                         }
  382.                     }
  383.                     $s .= Display::tag('td', Display::input('radio', 'choice['.$questionId.']['.$numAnswer.']', $key, $attributes));
  384.                 }
  385.  
  386.                 if ($show_comment) {
  387.                     $s .= '<td>';
  388.                     $s .= $comment;
  389.                     $s .= '</td>';
  390.                 }
  391.                 $s.='</tr>';
  392.  
  393.             } elseif ($answerType == FILL_IN_BLANKS) {
  394.                 list($answer) = explode('::', $answer);
  395.  
  396.                 //Correct answer
  397.                 api_preg_match_all('/\[[^]]+\]/', $answer, $correct_answer_list);
  398.  
  399.                 //Student's answezr
  400.                 if (isset($user_choice[0]['answer'])) {
  401.                     api_preg_match_all('/\[[^]]+\]/', $user_choice[0]['answer'], $student_answer_list);
  402.                     $student_answer_list = $student_answer_list[0];
  403.                 }
  404.  
  405.                 //If debug
  406.                 if ($debug_mark_answer) {
  407.                     $student_answer_list = $correct_answer_list[0];
  408.                 }
  409.  
  410.                 if (!empty($correct_answer_list) && !empty($student_answer_list)) {
  411.                     $correct_answer_list = $correct_answer_list[0];
  412.                     $i = 0;
  413.  
  414.                     foreach ($correct_answer_list as $correct_item) {
  415.                         $value = null;
  416.                         if (isset($student_answer_list[$i]) && !empty($student_answer_list[$i])) {
  417.  
  418.                             //Cleaning student answer list
  419.                             $value = strip_tags($student_answer_list[$i]);
  420.                             $value = api_substr($value, 1, api_strlen($value)-2);
  421.                             $value = explode('/', $value);
  422.  
  423.                             if (!empty($value[0])) {
  424.                                 $value = str_replace('&nbsp;', '',  trim($value[0]));
  425.                             }
  426.                             $correct_item = preg_quote($correct_item);
  427.                             $correct_item = api_preg_replace('|/|', '\/', $correct_item);   // to prevent error if there is a / in the text to find
  428.                             $answer = api_preg_replace('/'.$correct_item.'/', Display::input('text', "choice[$questionId][]", $value), $answer, 1);
  429.                         }
  430.                         $i++;
  431.                     }
  432.                 } else {
  433.                     $answer = api_preg_replace('/\[[^]]+\]/', Display::input('text', "choice[$questionId][]", '', $attributes), $answer);
  434.                 }
  435.                 $s .= $answer;
  436.             } elseif ($answerType == MATCHING) {
  437.                 // matching type, showing suggestions and answers
  438.                 // TODO: replace $answerId by $numAnswer
  439.  
  440.                 if ($answerCorrect != 0) {
  441.                     // only show elements to be answered (not the contents of
  442.                     // the select boxes, who are corrrect = 0)
  443.                     $s .= '<tr><td width="45%" valign="top">';
  444.                     $parsed_answer = $answer;
  445.                     //left part questions
  446.                     $s .= ' <span style="float:left; width:8%;"><b>'.$lines_count.'</b>.&nbsp;</span>
  447.                             <span style="float:left; width:92%;">'.$parsed_answer.'</span></td>';
  448.                     //middle part (matches selects)
  449.  
  450.                     $s .= '<td width="10%" valign="top" align="center">&nbsp;&nbsp;
  451.                             <select name="choice['.$questionId.']['.$numAnswer.']">';
  452.  
  453.                     // fills the list-box
  454.                     foreach ($select_items as $key=>$val) {
  455.                         // set $debug_mark_answer to true at function start to
  456.                         // show the correct answer with a suffix '-x'
  457.                         $selected = '';
  458.                         if ($debug_mark_answer) {
  459.                             if ($val['id'] == $answerCorrect) {
  460.                                 $selected = 'selected="selected"';
  461.                             }
  462.                         }
  463.                         if (isset($user_choice[$matching_correct_answer]) && $val['id'] == $user_choice[$matching_correct_answer]['answer']) {
  464.                             $selected = 'selected="selected"';
  465.                         }
  466.                         $s .= '<option value="'.$val['id'].'" '.$selected.'>'.$val['letter'].$help.'</option>';
  467.  
  468.                     }  // end foreach()
  469.  
  470.                     $s .= '</select></td>';
  471.                     //print_r($select_items);
  472.                     //right part (answers)
  473.                     $s.='<td width="45%" valign="top" >';
  474.                     if (isset($select_items[$lines_count])) {
  475.                         $s.='<span style="float:left; width:5%;"><b>'.$select_items[$lines_count]['letter'].'.</b></span>'.
  476.                              '<span style="float:left; width:95%;">'.$select_items[$lines_count]['answer'].'</span>';
  477.                     } else {
  478.                         $s.='&nbsp;';
  479.                     }
  480.                     $s .= '</td>';
  481.                     $s .= '</tr>';
  482.                     $lines_count++;
  483.                     //if the left side of the "matching" has been completely
  484.                     // shown but the right side still has values to show...
  485.                     if (($lines_count -1) == $num_suggestions) {
  486.                         // if it remains answers to shown at the right side
  487.                         while (isset($select_items[$lines_count])) {
  488.                             $s .= '<tr>
  489.                                   <td colspan="2"></td>
  490.                                   <td valign="top">';
  491.                             $s.='<b>'.$select_items[$lines_count]['letter'].'.</b> '.$select_items[$lines_count]['answer'];
  492.                             $s.="</td>
  493.                             </tr>";
  494.                             $lines_count++;
  495.                         }   // end while()
  496.                     }  // end if()
  497.                     $matching_correct_answer++;
  498.                 }
  499.             }
  500.         }   // end for()
  501.  
  502.         if ($show_comment) {
  503.             $s .= '</table>';
  504.         } else {
  505.             if ($answerType == MATCHING || $answerType == UNIQUE_ANSWER_NO_OPTION || $answerType == MULTIPLE_ANSWER_TRUE_FALSE ||
  506.                 $answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
  507.                 $s .= '</table>';
  508.             }
  509.         }
  510.  
  511.  
  512.  
  513.         $s .= '</div>';
  514.  
  515.         // destruction of the Answer object
  516.         unset($objAnswerTmp);
  517.  
  518.         // destruction of the Question object
  519.         unset($objQuestionTmp);
  520.  
  521.         if ($origin != 'export') {
  522.             echo $s;
  523.         } else {
  524.             return $s;
  525.         }
  526.     } elseif ($answerType == HOT_SPOT || $answerType == HOT_SPOT_DELINEATION) {
  527.         // Question is a HOT_SPOT
  528.         //checking document/images visibility
  529.         if (api_is_platform_admin() || api_is_course_admin()) {
  530.             require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  531.             $course = api_get_course_info();
  532.             $doc_id = DocumentManager::get_document_id($course, '/images/'.$pictureName);
  533.             if (is_numeric($doc_id)) {
  534.                 $images_folder_visibility = api_get_item_visibility($course,'document', $doc_id, api_get_session_id());
  535.                 if (!$images_folder_visibility) {
  536.                     //This message is shown only to the course/platform admin if the image is set to visibility = false
  537.                     Display::display_warning_message(get_lang('ChangeTheVisibilityOfTheCurrentImage'));
  538.                 }
  539.             }
  540.         }
  541.         $questionName         = $objQuestionTmp->selectTitle();
  542.         $questionDescription  = $objQuestionTmp->selectDescription();
  543.  
  544.         if ($freeze) {
  545.             echo Display::img($objQuestionTmp->selectPicturePath());
  546.             return;
  547.         }
  548.  
  549.         // Get the answers, make a list
  550.         $objAnswerTmp         = new Answer($questionId);
  551.         $nbrAnswers           = $objAnswerTmp->selectNbrAnswers();
  552.  
  553.         // get answers of hotpost
  554.         $answers_hotspot = array();
  555.         for ($answerId=1;$answerId <= $nbrAnswers;$answerId++) {
  556.             $answers = $objAnswerTmp->selectAnswerByAutoId($objAnswerTmp->selectAutoId($answerId));
  557.             $answers_hotspot[$answers['id']] = $objAnswerTmp->selectAnswer($answerId);
  558.         }
  559.  
  560.         // display answers of hotpost order by id
  561.         $answer_list = '<div style="padding: 10px; margin-left: 0px; border: 1px solid #A4A4A4; height: 408px; width: 200px;"><b>'.get_lang('HotspotZones').'</b><dl>';
  562.         if (!empty($answers_hotspot)) {
  563.             ksort($answers_hotspot);
  564.             foreach ($answers_hotspot as $key => $value) {
  565.                 $answer_list .= '<dt>'.$key.'.- '.$value.'</dt><br />';
  566.             }
  567.         }
  568.         $answer_list .= '</dl></div>';
  569.  
  570.         if ($answerType == HOT_SPOT_DELINEATION) {
  571.             $answer_list='';
  572.             $swf_file = 'hotspot_delineation_user';
  573.             $swf_height = 405;
  574.         } else {
  575.             $swf_file = 'hotspot_user';
  576.             $swf_height = 436;
  577.         }
  578.  
  579.         if (!$only_questions) {
  580.             if ($show_title) {
  581.                 Testcategory::displayCategoryAndTitle($objQuestionTmp->id);
  582.                 echo '<div class="question_title">'.$current_item.'. '.$questionName.'</div>';
  583.             }
  584.             //@todo I need to the get the feedback type
  585.             echo '<input type="hidden" name="hidden_hotspot_id" value="'.$questionId.'" />';
  586.             echo '<table class="exercise_questions" >
  587.                   <tr>
  588.                     <td valign="top" colspan="2">';
  589.             echo $questionDescription;
  590.             echo '</td></tr>';
  591.         }
  592.         $canClick = isset($_GET['editQuestion']) ? '0' : (isset($_GET['modifyAnswers']) ? '0' : '1');
  593.  
  594.         $s .= '<script type="text/javascript" src="../plugin/hotspot/JavaScriptFlashGateway.js"></script>
  595.                         <script src="../plugin/hotspot/hotspot.js" type="text/javascript" ></script>
  596.                         <script type="text/javascript">
  597.                         <!--
  598.                         // Globals
  599.                         // Major version of Flash required
  600.                         var requiredMajorVersion = 7;
  601.                         // Minor version of Flash required
  602.                         var requiredMinorVersion = 0;
  603.                         // Minor version of Flash required
  604.                         var requiredRevision = 0;
  605.                         // the version of javascript supported
  606.                         var jsVersion = 1.0;
  607.                         // -->
  608.                         </script>
  609.                         <script language="VBScript" type="text/vbscript">
  610.                         <!-- // Visual basic helper required to detect Flash Player ActiveX control version information
  611.                         Function VBGetSwfVer(i)
  612.                           on error resume next
  613.                           Dim swControl, swVersion
  614.                           swVersion = 0
  615.  
  616.                           set swControl = CreateObject("ShockwaveFlash.ShockwaveFlash." + CStr(i))
  617.                           if (IsObject(swControl)) then
  618.                             swVersion = swControl.GetVariable("$version")
  619.                           end if
  620.                           VBGetSwfVer = swVersion
  621.                         End Function
  622.                         // -->
  623.                         </script>
  624.  
  625.                         <script language="JavaScript1.1" type="text/javascript">
  626.                         <!-- // Detect Client Browser type
  627.                         var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
  628.                         var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
  629.                         var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
  630.                         jsVersion = 1.1;
  631.                         // JavaScript helper required to detect Flash Player PlugIn version information
  632.                         function JSGetSwfVer(i) {
  633.                             // NS/Opera version >= 3 check for Flash plugin in plugin array
  634.                             if (navigator.plugins != null && navigator.plugins.length > 0) {
  635.                                 if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
  636.                                     var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
  637.                                     var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
  638.                                     descArray = flashDescription.split(" ");
  639.                                     tempArrayMajor = descArray[2].split(".");
  640.                                     versionMajor = tempArrayMajor[0];
  641.                                     versionMinor = tempArrayMajor[1];
  642.                                     if ( descArray[3] != "" ) {
  643.                                         tempArrayMinor = descArray[3].split("r");
  644.                                     } else {
  645.                                         tempArrayMinor = descArray[4].split("r");
  646.                                     }
  647.                                     versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
  648.                                     flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
  649.                                 } else {
  650.                                     flashVer = -1;
  651.                                 }
  652.                             }
  653.                             // MSN/WebTV 2.6 supports Flash 4
  654.                             else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
  655.                             // WebTV 2.5 supports Flash 3
  656.                             else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
  657.                             // older WebTV supports Flash 2
  658.                             else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
  659.                             // Can\'t detect in all other cases
  660.                             else
  661.                             {
  662.                                 flashVer = -1;
  663.                             }
  664.                             return flashVer;
  665.                         }
  666.                         // When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
  667.  
  668.                         function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision) {
  669.                             reqVer = parseFloat(reqMajorVer + "." + reqRevision);
  670.                             // loop backwards through the versions until we find the newest version
  671.                             for (i=25;i>0;i--) {
  672.                                 if (isIE && isWin && !isOpera) {
  673.                                     versionStr = VBGetSwfVer(i);
  674.                                 } else {
  675.                                     versionStr = JSGetSwfVer(i);
  676.                                 }
  677.                                 if (versionStr == -1 ) {
  678.                                     return false;
  679.                                 } else if (versionStr != 0) {
  680.                                     if(isIE && isWin && !isOpera) {
  681.                                         tempArray         = versionStr.split(" ");
  682.                                         tempString        = tempArray[1];
  683.                                         versionArray      = tempString .split(",");
  684.                                     } else {
  685.                                         versionArray      = versionStr.split(".");
  686.                                     }
  687.                                     versionMajor      = versionArray[0];
  688.                                     versionMinor      = versionArray[1];
  689.                                     versionRevision   = versionArray[2];
  690.  
  691.                                     versionString     = versionMajor + "." + versionRevision;   // 7.0r24 == 7.24
  692.                                     versionNum        = parseFloat(versionString);
  693.                                     // is the major.revision >= requested major.revision AND the minor version >= requested minor
  694.                                     if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
  695.                                         return true;
  696.                                     } else {
  697.                                         return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
  698.                                     }
  699.                                 }
  700.                             }
  701.                         }
  702.                         // -->
  703.                         </script>';
  704.         $s .= '<tr><td valign="top" colspan="2" width="520"><table><tr><td width="520">
  705.                     <script>
  706.                         <!--
  707.                         // Version check based upon the values entered above in "Globals"
  708.                         var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
  709.  
  710.                         // Check to see if the version meets the requirements for playback
  711.                         if (hasReqestedVersion) {  // if we\'ve detected an acceptable version
  712.                             var oeTags = \'<object type="application/x-shockwave-flash" data="../plugin/hotspot/'.$swf_file.'.swf?modifyAnswers='.$questionId.'&amp;canClick:'.$canClick.'" width="600" height="'.$swf_height.'">\'
  713.                                         + \'<param name="wmode" value="transparent">\'
  714.                                         + \'<param name="movie" value="../plugin/hotspot/'.$swf_file.'.swf?modifyAnswers='.$questionId.'&amp;canClick:'.$canClick.'" />\'
  715.                                         + \'<\/object>\';
  716.                             document.write(oeTags);   // embed the Flash Content SWF when all tests are passed
  717.                         } else {  // flash is too old or we can\'t detect the plugin
  718.                             var alternateContent = "Error<br \/>"
  719.                                 + "Hotspots requires Macromedia Flash 7.<br \/>"
  720.                                 + "<a href=\"http://www.macromedia.com/go/getflash/\">Get Flash<\/a>";
  721.                             document.write(alternateContent);  // insert non-flash content
  722.                         }
  723.                         // -->
  724.                     </script>
  725.                     </td>
  726.                     <td valign="top" align="left">'.$answer_list.'</td></tr>
  727.                     </table>
  728.         </td></tr>';
  729.         echo $s;
  730.         echo '</table>';
  731.     }
  732.     return $nbrAnswers;
  733. }
  734.  
  735. function get_exercise_track_exercise_info($exe_id) {
  736.     $TBL_EXERCICES          = Database::get_course_table(TABLE_QUIZ_TEST);
  737.     $TBL_TRACK_EXERCICES    = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  738.     $TBL_COURSE             = Database::get_main_table(TABLE_MAIN_COURSE);
  739.     $exe_id = intval($exe_id);
  740.     $result = array();
  741.     if (!empty($exe_id)) {
  742.        $sql_fb_type = "SELECT q.*, tee.*
  743.                        FROM $TBL_EXERCICES as q
  744.                        INNER JOIN $TBL_TRACK_EXERCICES as tee
  745.                        ON q.id=tee.exe_exo_id
  746.                        INNER JOIN $TBL_COURSE c
  747.                        ON c.code = tee.exe_cours_id
  748.                        WHERE tee.exe_id=$exe_id
  749.                        AND q.c_id=c.id";
  750.  
  751.        $res_fb_type = Database::query($sql_fb_type);
  752.        $result      = Database::fetch_array($res_fb_type, 'ASSOC');
  753.     }
  754.     return $result;
  755. }
  756.  
  757.  
  758. /**
  759.  * Validates the time control key
  760.  */
  761. function exercise_time_control_is_valid($exercise_id, $lp_id = 0 , $lp_item_id = 0) {
  762.     $course_id = api_get_course_int_id();
  763.     $exercise_id = intval($exercise_id);
  764.     $TBL_EXERCICES =  Database::get_course_table(TABLE_QUIZ_TEST);
  765.     $sql    = "SELECT expired_time FROM $TBL_EXERCICES WHERE c_id = $course_id AND id = $exercise_id";
  766.     $result = Database::query($sql);
  767.     $row    = Database::fetch_array($result, 'ASSOC');
  768.     if (!empty($row['expired_time']) ) {
  769.         $current_expired_time_key = get_time_control_key($exercise_id, $lp_id, $lp_item_id);
  770.         if (isset($_SESSION['expired_time'][$current_expired_time_key])) {
  771.             $current_time = time();
  772.             $expired_time = api_strtotime($_SESSION['expired_time'][$current_expired_time_key], 'UTC');
  773.             $total_time_allowed = $expired_time + 30;
  774.             //error_log('expired time converted + 30: '.$total_time_allowed);
  775.             //error_log('$current_time: '.$current_time);
  776.             if ($total_time_allowed < $current_time) {
  777.                 return false;
  778.             }
  779.             return true;
  780.         } else {
  781.             return false;
  782.         }
  783.     } else {
  784.         return true;
  785.     }
  786. }
  787.  
  788. /**
  789.     Deletes the time control token
  790. */
  791. function exercise_time_control_delete($exercise_id,  $lp_id = 0 , $lp_item_id = 0) {
  792.     $current_expired_time_key = get_time_control_key($exercise_id, $lp_id, $lp_item_id);
  793.     unset($_SESSION['expired_time'][$current_expired_time_key]);
  794. }
  795.  
  796. /**
  797.     Generates the time control key
  798. */
  799. function get_time_control_key($exercise_id, $lp_id = 0, $lp_item_id = 0) {
  800.     $exercise_id = intval($exercise_id);
  801.     $lp_id = intval($lp_id);
  802.     $lp_item_id = intval($lp_item_id);
  803.     return api_get_course_int_id().'_'.api_get_session_id().'_'.$exercise_id.'_'.api_get_user_id().'_'.$lp_id.'_'.$lp_item_id;
  804. }
  805.  
  806. /**
  807.  * Get session time control
  808.  */
  809. function get_session_time_control_key($exercise_id, $lp_id = 0, $lp_item_id = 0) {
  810.     $return_value = 0;
  811.     $time_control_key = get_time_control_key($exercise_id, $lp_id, $lp_item_id);
  812.     if (isset($_SESSION['expired_time']) && isset($_SESSION['expired_time'][$time_control_key])) {
  813.         $return_value = $_SESSION['expired_time'][$time_control_key];
  814.     }
  815.     return $return_value;
  816. }
  817.  
  818. /**
  819.  * Gets count of exam results
  820.  * @todo this function should be moved in a library  + no global calls
  821.  */
  822. function get_count_exam_results($exercise_id, $extra_where_conditions) {
  823.     $count = get_exam_results_data(null, null, null, null, $exercise_id, $extra_where_conditions, true);
  824.     return $count;
  825. }
  826.  
  827. function get_count_exam_hotpotatoes_results($in_hotpot_path) {
  828.     return get_exam_results_hotpotatoes_data(0, 0, '', '', $in_hotpot_path, true, '');
  829. }
  830.  
  831. //function get_exam_results_hotpotatoes_data($from, $number_of_items, $column, $direction, $exercise_id, $extra_where_conditions = null, $get_count = false) {
  832. function get_exam_results_hotpotatoes_data($in_from, $in_number_of_items, $in_column, $in_direction, $in_hotpot_path, $in_get_count = false, $where_condition = null) {
  833.  
  834.     $tab_res = array();
  835.     $course_code = api_get_course_id();
  836.     // by default in_column = 1 If parameters given, it is the name of the column witch is the bdd field name
  837.     if ($in_column == 1) {
  838.         $in_column = 'firstname';
  839.     }
  840.  
  841.     $TBL_TRACK_HOTPOTATOES      = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  842.     $TBL_GROUP_REL_USER         = Database :: get_course_table(TABLE_GROUP_USER);
  843.     $TBL_GROUP                  = Database :: get_course_table(TABLE_GROUP);
  844.     $TBL_USER                   = Database :: get_main_table(TABLE_MAIN_USER);
  845.  
  846.     $sql .= "SELECT * FROM $TBL_TRACK_HOTPOTATOES thp JOIN $TBL_USER u ON thp.exe_user_id = u.user_id WHERE thp.exe_cours_id = '$course_code' AND exe_name LIKE '$in_hotpot_path%'";
  847.  
  848.     // just count how many answers
  849.     if ($in_get_count) {
  850.         $res = Database::query($sql);
  851.         return Database::num_rows($res);
  852.     }
  853.  
  854.     // get a number of sorted results
  855.     $sql .= " $where_condition ORDER BY $in_column $in_direction  LIMIT $in_from, $in_number_of_items";
  856.  
  857.     $res = Database::query($sql);
  858.     while ($data = Database::fetch_array($res)) {
  859.         $tab_one_res = array();
  860.         $tab_one_res['firstname'] = $data['firstname'];
  861.         $tab_one_res['lastname'] = $data['lastname'];
  862.         $tab_one_res['username'] = $data['username'];
  863.         $tab_one_res['group_name'] = implode("<br/>",GroupManager::get_user_group_name($data['user_id']));
  864.         $tab_one_res['exe_date'] = $data['exe_date'];
  865.         $tab_one_res['score'] = $data['exe_result'].'/'.$data['exe_weighting'];
  866.         $tab_one_res['actions'] = "";
  867.         $tab_res[] = $tab_one_res;
  868.     }
  869.     return $tab_res;
  870. }
  871.  
  872. /**
  873.  * Gets the exam'data results
  874.  * @todo this function should be moved in a library  + no global calls
  875.  */
  876. function get_exam_results_data($from, $number_of_items, $column, $direction, $exercise_id, $extra_where_conditions = null, $get_count = false) {
  877.  
  878.     //@todo replace all this globals
  879.     global $documentPath, $filter;
  880.  
  881.     if (empty($extra_where_conditions)) {
  882.         $extra_where_conditions = "1 = 1 ";
  883.     }
  884.  
  885.     $course_id = api_get_course_int_id();
  886.     $course_code = api_get_course_id();
  887.  
  888.     $is_allowedToEdit           = api_is_allowed_to_edit(null,true) || api_is_allowed_to_edit(true) || api_is_drh();
  889.  
  890.     $TBL_USER                   = Database :: get_main_table(TABLE_MAIN_USER);
  891.     $TBL_EXERCICES              = Database :: get_course_table(TABLE_QUIZ_TEST);
  892.     $TBL_GROUP_REL_USER         = Database :: get_course_table(TABLE_GROUP_USER);
  893.     $TBL_GROUP                  = Database :: get_course_table(TABLE_GROUP);
  894.  
  895.     $TBL_TRACK_EXERCICES        = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  896.     $TBL_TRACK_HOTPOTATOES      = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  897.     $TBL_TRACK_ATTEMPT_RECORDING= Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  898.  
  899.     $session_id_and = ' AND te.session_id = '.api_get_session_id().' ';
  900.  
  901.     $exercise_id = intval($exercise_id);
  902.  
  903.     $exercise_where = '';
  904.     if (!empty($exercise_id)) {
  905.         $exercise_where .= ' AND te.exe_exo_id = '.$exercise_id.'  ';
  906.     }
  907.  
  908.     $hotpotatoe_where = '';
  909.     if (!empty($_GET['path'])) {
  910.         $hotpotatoe_path = Database::escape_string($_GET['path']);
  911.         $hotpotatoe_where .= ' AND exe_name = "'.$hotpotatoe_path.'"  ';
  912.     }
  913.  
  914.     // sql for chamilo-type tests for teacher / tutor view
  915.     $sql_inner_join_tbl_track_exercices = " (
  916.                                            SELECT DISTINCT ttte.*, if(tr.exe_id,1, 0) as revised
  917.                                            FROM $TBL_TRACK_EXERCICES ttte LEFT JOIN $TBL_TRACK_ATTEMPT_RECORDING tr
  918.                                            ON (ttte.exe_id = tr.exe_id)
  919.                                            WHERE exe_cours_id = '$course_code' AND
  920.                                                  exe_exo_id = $exercise_id AND
  921.                                                  ttte.session_id = ".api_get_session_id()."
  922.                                            )";
  923.     if ($is_allowedToEdit) {
  924.         //Teacher view
  925.         if (isset($_GET['gradebook']) && $_GET['gradebook'] == 'view') {
  926.             //$exercise_where_query = ' te.exe_exo_id = ce.id AND ';
  927.         }
  928.  
  929.         $sqlFromOption                      = "";
  930.         $sqlWhereOption                     = "";           // for hpsql
  931.  
  932.         //@todo fix to work with COURSE_RELATION_TYPE_RRHH in both queries
  933.  
  934.         //Hack in order to filter groups
  935.         $sql_inner_join_tbl_user = '';
  936.  
  937.         if (strpos($extra_where_conditions, 'group_id')) {
  938.             $sql_inner_join_tbl_user = "
  939.            (
  940.                SELECT u.user_id, firstname, lastname, email, username, g.name as group_name, g.id as group_id
  941.                FROM $TBL_USER u
  942.                INNER JOIN $TBL_GROUP_REL_USER gru ON ( gru.user_id = u.user_id AND gru.c_id=".$course_id.")
  943.                INNER JOIN $TBL_GROUP g ON (gru.group_id = g.id AND g.c_id=".$course_id.")
  944.            )";
  945.  
  946.         }
  947.  
  948.         if (strpos($extra_where_conditions, 'group_all')) {
  949.  
  950.             $extra_where_conditions = str_replace("AND (  group_id = 'group_all'  )", '', $extra_where_conditions);
  951.             $extra_where_conditions = str_replace("AND group_id = 'group_all'", '', $extra_where_conditions);
  952.             $extra_where_conditions = str_replace("group_id = 'group_all' AND", '', $extra_where_conditions);
  953.  
  954.             $sql_inner_join_tbl_user = "
  955.            (
  956.                SELECT u.user_id, firstname, lastname, email, username, '' as group_name, '' as group_id
  957.                FROM $TBL_USER u
  958.            )";
  959.             $sql_inner_join_tbl_user = null;
  960.         }
  961.  
  962.         if (strpos($extra_where_conditions, 'group_none')) {
  963.             $extra_where_conditions = str_replace("AND (  group_id = 'group_none'  )", "AND (  group_id is null  )", $extra_where_conditions);
  964.             $extra_where_conditions = str_replace("AND group_id = 'group_none'", "AND (  group_id is null  )", $extra_where_conditions);
  965.             $sql_inner_join_tbl_user = "
  966.            (
  967.                SELECT u.user_id, firstname, lastname, email, username, g.name as group_name, g.id as group_id
  968.                FROM $TBL_USER u
  969.                LEFT OUTER JOIN $TBL_GROUP_REL_USER gru ON ( gru.user_id = u.user_id AND gru.c_id=".$course_id." )
  970.                LEFT OUTER JOIN $TBL_GROUP g ON (gru.group_id = g.id AND g.c_id = ".$course_id.")
  971.            )";
  972.         }
  973.  
  974.         //All
  975.         $is_empty_sql_inner_join_tbl_user = false;
  976.  
  977.         if (empty($sql_inner_join_tbl_user)) {
  978.             $is_empty_sql_inner_join_tbl_user = true;
  979.              $sql_inner_join_tbl_user = "
  980.            (
  981.                SELECT u.user_id, firstname, lastname, email, username, ' ' as group_name, '' as group_id
  982.                FROM $TBL_USER u
  983.            )";
  984.         }
  985.  
  986.  
  987.         $sqlFromOption = " , $TBL_GROUP_REL_USER AS gru ";
  988.         $sqlWhereOption = "  AND gru.c_id = ".api_get_course_int_id()." AND gru.user_id = user.user_id ";
  989.  
  990.         $first_and_last_name = api_is_western_name_order() ? "firstname, lastname" : "lastname, firstname";
  991.  
  992.         if ($get_count) {
  993.             $sql_select = "SELECT count(te.exe_id) ";
  994.         } else {
  995.             $sql_select = "SELECT DISTINCT
  996.                    user_id,
  997.                    $first_and_last_name,
  998.                    ce.title,
  999.                    username,
  1000.                    te.exe_result,
  1001.                    te.exe_weighting,
  1002.                    te.exe_date,
  1003.                    te.exe_id,
  1004.                    email as exemail,
  1005.                    te.start_date,
  1006.                    steps_counter,
  1007.                    exe_user_id,
  1008.                    te.exe_duration,
  1009.                    propagate_neg,
  1010.                    revised,
  1011.                    group_name,
  1012.                    group_id,
  1013.                    orig_lp_id";
  1014.         }
  1015.  
  1016.         $sql = " $sql_select
  1017.                FROM $TBL_EXERCICES AS ce
  1018.                INNER JOIN $sql_inner_join_tbl_track_exercices AS te ON (te.exe_exo_id = ce.id)
  1019.                INNER JOIN $sql_inner_join_tbl_user  AS user ON (user.user_id = exe_user_id)
  1020.                WHERE $extra_where_conditions AND
  1021.                    te.status != 'incomplete'
  1022.                    AND te.exe_cours_id='" . api_get_course_id() . "' $session_id_and
  1023.                    AND ce.active <>-1
  1024.                    AND ce.c_id=".api_get_course_int_id()."
  1025.                    $exercise_where ";
  1026.  
  1027.         // sql for hotpotatoes tests for teacher / tutor view
  1028.  
  1029.         if ($get_count) {
  1030.             $hpsql_select = "SELECT count(username)";
  1031.         } else {
  1032.             $hpsql_select = "SELECT
  1033.                    $first_and_last_name ,
  1034.                    username,
  1035.                    tth.exe_name,
  1036.                    tth.exe_result ,
  1037.                    tth.exe_weighting,
  1038.                    tth.exe_date";
  1039.         }
  1040.  
  1041.         $hpsql = " $hpsql_select
  1042.                FROM
  1043.                    $TBL_TRACK_HOTPOTATOES tth,
  1044.                    $TBL_USER user
  1045.                    $sqlFromOption
  1046.                WHERE
  1047.                    user.user_id=tth.exe_user_id
  1048.                    AND tth.exe_cours_id = '" . api_get_course_id()."'
  1049.                    $hotpotatoe_where
  1050.                    $sqlWhereOption
  1051.                     AND $where_condition
  1052.                ORDER BY
  1053.                    tth.exe_cours_id ASC,
  1054.                    tth.exe_date DESC";
  1055.     }
  1056.  
  1057.     if ($get_count) {
  1058.         $resx = Database::query($sql);
  1059.         $rowx = Database::fetch_row($resx,'ASSOC');
  1060.         return $rowx[0];
  1061.     }
  1062.  
  1063.     $teacher_list = CourseManager::get_teacher_list_from_course_code(api_get_course_id());
  1064.     $teacher_id_list = array();
  1065.     foreach ($teacher_list as $teacher) {
  1066.         $teacher_id_list[] = $teacher['user_id'];
  1067.     }
  1068.  
  1069.     //Simple exercises
  1070.     if (empty($hotpotatoe_where)) {
  1071.         $column             = !empty($column) ? Database::escape_string($column) : null;
  1072.         $from               = intval($from);
  1073.         $number_of_items    = intval($number_of_items);
  1074.  
  1075.         if (!empty($column)) {
  1076.             $sql .= " ORDER BY $column $direction ";
  1077.         }
  1078.         $sql .= " LIMIT $from, $number_of_items";
  1079.  
  1080.         $results = array();
  1081.         $resx = Database::query($sql);
  1082.         while ($rowx = Database::fetch_array($resx,'ASSOC')) {
  1083.             $results[] = $rowx;
  1084.         }
  1085.  
  1086.         $list_info = array();
  1087.  
  1088.         $group_list = GroupManager::get_group_list();
  1089.         $clean_group_list = array();
  1090.  
  1091.         if (!empty($group_list)) {
  1092.             foreach ($group_list as $group) {
  1093.                 $clean_group_list[$group['id']] = $group['name'];
  1094.             }
  1095.         }
  1096.  
  1097.         $lp_list_obj = new learnpathList(api_get_user_id());
  1098.         $lp_list = $lp_list_obj->get_flat_list();
  1099.  
  1100.         if (is_array($results)) {
  1101.  
  1102.             $users_array_id = array();
  1103.             if ($_GET['gradebook'] == 'view') {
  1104.                 $from_gradebook = true;
  1105.             }
  1106.             $sizeof = count($results);
  1107.  
  1108.             $user_list_id = array ();
  1109.  
  1110.             $locked = api_resource_is_locked_by_gradebook($exercise_id, LINK_EXERCISE);
  1111.  
  1112.             //Looping results
  1113.             for ($i = 0; $i < $sizeof; $i++) {
  1114.                 $revised = $results[$i]['revised'];
  1115.  
  1116.                 if ($from_gradebook && ($is_allowedToEdit)) {
  1117.                     if (in_array($results[$i]['username'] . $results[$i]['firstname'] . $results[$i]['lastname'], $users_array_id)) {
  1118.                         continue;
  1119.                     }
  1120.                     $users_array_id[] = $results[$i]['username'] . $results[$i]['firstname'] . $results[$i]['lastname'];
  1121.                 }
  1122.  
  1123.                 $lp_obj = isset($results[$i]['orig_lp_id']) && isset($lp_list[$results[$i]['orig_lp_id']]) ? $lp_list[$results[$i]['orig_lp_id']] : null;
  1124.                 $lp_name = null;
  1125.  
  1126.                 if ($lp_obj) {
  1127.                     $url = api_get_path(WEB_CODE_PATH).'newscorm/lp_controller.php?'.api_get_cidreq().'&action=view&lp_id='.$results[$i]['orig_lp_id'];
  1128.                     $lp_name =  Display::url($lp_obj['lp_name'], $url, array('target' => '_blank'));
  1129.                 }
  1130.  
  1131.                 //Add all groups by user
  1132.                 $group_name_list = null;
  1133.  
  1134.                 if ($is_empty_sql_inner_join_tbl_user) {
  1135.                     $group_list = GroupManager::get_group_ids(api_get_course_int_id(), $results[$i]['user_id']);
  1136.  
  1137.                     foreach ($group_list as $id) {
  1138.                         $group_name_list .= $clean_group_list[$id].'<br/>';
  1139.                     }
  1140.                     $results[$i]['group_name'] = $group_name_list;
  1141.                 }
  1142.  
  1143.                 $results[$i]['exe_duration'] =  !empty($results[$i]['exe_duration']) ? round($results[$i]['exe_duration'] / 60) : 0;
  1144.  
  1145.                 $user_list_id[] = $results[$i]['exe_user_id'];
  1146.                 $id = $results[$i]['exe_id'];
  1147.  
  1148.                 $dt = api_convert_and_format_date($results[$i]['exe_weighting']);
  1149.  
  1150.                 // we filter the results if we have the permission to
  1151.                 if (isset($results[$i]['results_disabled'])) {
  1152.                     $result_disabled = intval($results[$i]['results_disabled']);
  1153.                 }  else {
  1154.                     $result_disabled = 0;
  1155.                 }
  1156.  
  1157.                 if ($result_disabled == 0) {
  1158.  
  1159.                     $my_res     = $results[$i]['exe_result'];
  1160.                     $my_total   = $results[$i]['exe_weighting'];
  1161.  
  1162.                     $results[$i]['start_date']  =   api_get_local_time($results[$i]['start_date']);
  1163.                     $results[$i]['exe_date']    =   api_get_local_time($results[$i]['exe_date']);
  1164.  
  1165.                     if (!$results[$i]['propagate_neg'] && $my_res < 0) {
  1166.                         $my_res = 0;
  1167.                     }
  1168.                     $score = show_score($my_res, $my_total);
  1169.  
  1170.                     $actions = '';
  1171.                     if ($is_allowedToEdit) {
  1172.                         if (isset($teacher_id_list)) {
  1173.                             if (in_array($results[$i]['exe_user_id'], $teacher_id_list)) {
  1174.                                 $actions .= Display::return_icon('teachers.gif', get_lang('Teacher'));
  1175.                             }
  1176.                         }
  1177.                         if ($revised) {
  1178.                             $actions .= "<a href='exercise_show.php?".api_get_cidreq()."&action=edit&id=$id'>".Display :: return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL);
  1179.                             $actions .= '&nbsp;';
  1180.                         } else {
  1181.                             $actions .="<a href='exercise_show.php?".api_get_cidreq()."&action=qualify&id=$id'>".Display :: return_icon('quiz.gif', get_lang('Qualify'));
  1182.                             $actions .='&nbsp;';
  1183.                         }
  1184.                         $actions .="</a>";
  1185.  
  1186.                         if ($filter == 2) {
  1187.                             $actions .=' <a href="exercise_history.php?'.api_get_cidreq().'&exe_id=' . $id . '">' .Display :: return_icon('history.gif', get_lang('ViewHistoryChange')).'</a>';
  1188.                         }
  1189.  
  1190.                         //Admin can always delete the attempt
  1191.                         if ($locked == false || api_is_platform_admin()) {
  1192.                             $ip = TrackingUserLog::get_ip_from_user_event($results[$i]['exe_user_id'], $results[$i]['exe_date'], false);
  1193.                             $actions .= '<a href="http://www.whatsmyip.org/ip-geo-location/?ip='.$ip.'" target="_blank"><img src="'.api_get_path(WEB_CODE_PATH).'img/icons/22/info.png" title="'.$ip.'" /></a>';
  1194.                             $actions .=' <a href="exercise_report.php?'.api_get_cidreq().'&filter_by_user='.intval($_GET['filter_by_user']).'&filter=' . $filter . '&exerciseId='.$exercise_id.'&delete=delete&did=' . $id . '" onclick="javascript:if(!confirm(\'' . sprintf(get_lang('DeleteAttempt'), $user, $dt) . '\')) return false;">'.Display :: return_icon('delete.png', get_lang('Delete')).'</a>';
  1195.                             $actions .='&nbsp;';
  1196.                         }
  1197.  
  1198.                     } else {
  1199.                         $attempt_url    = api_get_path(WEB_CODE_PATH).'exercice/result.php?'.api_get_cidreq().'&id='.$results[$i]['exe_id'].'&id_session='.api_get_session_id().'&height=500&width=750';
  1200.                         $attempt_link   = Display::url(get_lang('Show'), $attempt_url, array('class'=>'ajax btn'));
  1201.                         $actions .= $attempt_link;
  1202.                     }
  1203.  
  1204.                     if ($revised) {
  1205.                         $revised = Display::label(get_lang('Validated'), 'success');
  1206.                     } else {
  1207.                         $revised = Display::label(get_lang('NotValidated'), 'info');
  1208.                     }
  1209.  
  1210.                     if ($is_allowedToEdit) {
  1211.                         $results[$i]['status']  =  $revised;
  1212.                         $results[$i]['score']   =  $score;
  1213.                         $results[$i]['lp']      =  $lp_name;
  1214.                         $results[$i]['actions'] =  $actions;
  1215.                         $list_info[] = $results[$i];
  1216.                     } else {
  1217.                         $results[$i]['status']  =  $revised;
  1218.                         $results[$i]['score']   =  $score;
  1219.                         $results[$i]['actions'] =  $actions;
  1220.                         $list_info[] = $results[$i];
  1221.                     }
  1222.                 }
  1223.             }
  1224.         }
  1225.     } else {
  1226.         //echo $hpsql; var_dump($hpsql);
  1227.         $hpresults = getManyResultsXCol($hpsql, 6);
  1228.  
  1229.         // Print HotPotatoes test results.
  1230.         if (is_array($hpresults)) {
  1231.  
  1232.             for ($i = 0; $i < sizeof($hpresults); $i++) {
  1233.                 $hp_title = GetQuizName($hpresults[$i][3], $documentPath);
  1234.                 if ($hp_title == '') {
  1235.                     $hp_title = basename($hpresults[$i][3]);
  1236.                 }
  1237.                 //var_dump($hpresults[$i]);
  1238.  
  1239.                 $hp_date = api_get_local_time($hpresults[$i][6], null, date_default_timezone_get());
  1240.                 $hp_result = round(($hpresults[$i][4] / ($hpresults[$i][5] != 0 ? $hpresults[$i][5] : 1)) * 100, 2).'% ('.$hpresults[$i][4].' / '.$hpresults[$i][5].')';
  1241.                 if ($is_allowedToEdit) {
  1242.                     $list_info[] = array($hpresults[$i][0], $hpresults[$i][1], $hpresults[$i][2], '',  $hp_title, '-',  $hp_date , $hp_result , '-');
  1243.                 } else {
  1244.                     $list_info[] = array($hp_title, '-', $hp_date , $hp_result , '-');
  1245.                 }
  1246.             }
  1247.         }
  1248.     }
  1249.  
  1250.     return $list_info;
  1251. }
  1252.  
  1253.  
  1254. /**
  1255.  * Converts the score with the exercise_max_note and exercise_min_score the platform settings + formats the results using the float_format function
  1256.  *
  1257.  * @param   float   score
  1258.  * @param   float   weight
  1259.  * @param   bool    show porcentage or not
  1260.  * @param   bool    use or not the platform settings
  1261.  * @return  string  an html with the score modified
  1262.  */
  1263. function show_score($score, $weight, $show_percentage = true, $use_platform_settings = true, $show_only_percentage = false) {
  1264.     if (is_null($score) && is_null($weight)) {
  1265.         return '-';
  1266.     }
  1267.  
  1268.     $max_note =  api_get_setting('exercise_max_score');
  1269.     $min_note =  api_get_setting('exercise_min_score');
  1270.  
  1271.     if ($use_platform_settings) {
  1272.         if ($max_note != '' && $min_note != '') {
  1273.             if (!empty($weight) && intval($weight) != 0) {
  1274.                $score = $min_note + ($max_note - $min_note) * $score / $weight;
  1275.             } else {
  1276.                $score = $min_note;
  1277.             }
  1278.             $weight = $max_note;
  1279.         }
  1280.     }
  1281.     $percentage = (100 * $score)/ ($weight != 0 ? $weight : 1);
  1282.  
  1283.     //Formats values
  1284.     $percentage = float_format($percentage, 1);
  1285.     $score = float_format($score, 1);
  1286.     $weight = float_format($weight, 1);
  1287.  
  1288.     $html  = null;
  1289.     if ($show_percentage) {
  1290.         $parent = '(' . $score . ' / ' . $weight . ')';
  1291.         $html = $percentage."%  $parent";
  1292.         if ($show_only_percentage) {
  1293.             $html = $percentage."% ";
  1294.         }
  1295.     } else {
  1296.         $html = $score . ' / ' . $weight;
  1297.     }
  1298.     $html  = Display::span($html, array('class' => 'score_exercise'));
  1299.     return $html;
  1300. }
  1301.  
  1302. function is_success_exercise_result($score, $weight, $pass_percentage) {
  1303.     $percentage = float_format(($score / ($weight != 0 ? $weight : 1)) * 100, 1);
  1304.     if (isset($pass_percentage) && !empty($pass_percentage)) {
  1305.         if ($percentage >= $pass_percentage) {
  1306.             return true;
  1307.         }
  1308.     }
  1309.     return false;
  1310. }
  1311.  
  1312. function show_success_message($score, $weight, $pass_percentage) {
  1313.     $res = "";
  1314.     if (is_pass_pourcentage_enabled($pass_percentage)) {
  1315.         $is_success = is_success_exercise_result($score, $weight, $pass_percentage);
  1316.  
  1317.         $icon = '';
  1318.         if ($is_success) {
  1319.             $html = get_lang('CongratulationsYouPassedTheTest');
  1320.             $icon = Display::return_icon('completed.png', get_lang('Correct'), array(), ICON_SIZE_MEDIUM);
  1321.         } else {
  1322.             //$html .= Display::return_message(get_lang('YouDidNotReachTheMinimumScore'), 'warning');
  1323.             $html = get_lang('YouDidNotReachTheMinimumScore');
  1324.             $icon = Display::return_icon('warning.png', get_lang('Wrong'), array(), ICON_SIZE_MEDIUM);
  1325.         }
  1326.         $html = Display::tag('h4', $html);
  1327.         $html .= Display::tag('h5', $icon, array('style' => 'width:40px; padding:2px 10px 0px 0px'));
  1328.         $res = $html;
  1329.     }
  1330.     return $res;
  1331. }
  1332.  
  1333. /**
  1334.  * Return true if pass_pourcentage activated (we use the pass pourcentage feature
  1335.  * return false if pass_percentage = 0 (we don't use the pass pourcentage feature
  1336.  * @param $in_pass_pourcentage
  1337.  * @return boolean
  1338.  * In this version, pass_percentage and show_success_message are disabled if
  1339.  * pass_percentage is set to 0
  1340.  */
  1341. function is_pass_pourcentage_enabled($in_pass_pourcentage) {
  1342.     return $in_pass_pourcentage > 0;
  1343. }
  1344.  
  1345. /**
  1346.  * Converts a numeric value in a percentage example 0.66666 to 66.67 %
  1347.  * @param $value
  1348.  * @return float Converted number
  1349.  */
  1350. function convert_to_percentage($value) {
  1351.     $return = '-';
  1352.     if ($value != '') {
  1353.         $return = float_format($value * 100, 1).' %';
  1354.     }
  1355.     return $return;
  1356. }
  1357.  
  1358. /**
  1359.  * Converts a score/weight values to the platform scale
  1360.  * @param   float   score
  1361.  * @param   float   weight
  1362.  * @return  float   the score rounded converted to the new range
  1363.  */
  1364. function convert_score($score, $weight) {
  1365.     $max_note =  api_get_setting('exercise_max_score');
  1366.     $min_note =  api_get_setting('exercise_min_score');
  1367.  
  1368.     if ($score != '' && $weight != '') {
  1369.         if ($max_note != '' && $min_note != '') {
  1370.            if (!empty($weight)) {
  1371.                $score   = $min_note + ($max_note - $min_note) * $score / $weight;
  1372.            } else {
  1373.                $score   = $min_note;
  1374.            }
  1375.         }
  1376.     }
  1377.     $score_rounded  = float_format($score, 1);
  1378.     return $score_rounded;
  1379. }
  1380.  
  1381. /**
  1382.  * 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)
  1383.  * @param   array   course data
  1384.  * @param   int     session id
  1385.  * @return  array   array with exercise data
  1386.  */
  1387. function get_all_exercises($course_info = null, $session_id = 0, $check_publication_dates = false) {
  1388.     $TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST);
  1389.     $course_id = api_get_course_int_id();
  1390.  
  1391.     if (!empty($course_info) && !empty($course_info['real_id'])) {
  1392.         $course_id = $course_info['real_id'];
  1393.     }
  1394.  
  1395.     if ($session_id == -1) {
  1396.         $session_id  = 0;
  1397.     }
  1398.  
  1399.     $now = api_get_utc_datetime();
  1400.     $time_conditions = '';
  1401.  
  1402.     if ($check_publication_dates) {
  1403.         $time_conditions = " AND ((start_time <> '0000-00-00 00:00:00' AND start_time < '$now'  AND end_time <> '0000-00-00 00:00:00' AND end_time > '$now' )  OR "; //start and end are set
  1404.         $time_conditions .= " (start_time <> '0000-00-00 00:00:00' AND start_time < '$now'  AND end_time = '0000-00-00 00:00:00') OR "; // only start is set
  1405.         $time_conditions .= " (start_time = '0000-00-00 00:00:00'   AND end_time <> '0000-00-00 00:00:00'  AND end_time > '$now') OR   "; // only end is set
  1406.         $time_conditions .= " (start_time = '0000-00-00 00:00:00'   AND end_time =  '0000-00-00 00:00:00'))  "; // nothing is set
  1407.     }
  1408.  
  1409.     if ($session_id == 0) {
  1410.         $conditions = array('where'=>array('active = ? AND session_id = ? AND c_id = ? '.$time_conditions => array('1', $session_id, $course_id)), 'order'=>'title');
  1411.     } else {
  1412.         //All exercises
  1413.         $conditions = array('where'=>array('active = ? AND  (session_id = 0 OR session_id = ? ) AND c_id = ? '.$time_conditions => array('1', $session_id, $course_id)), 'order'=>'title');
  1414.     }
  1415.     return Database::select('*',$TBL_EXERCICES, $conditions);
  1416. }
  1417.  
  1418.  
  1419. /**
  1420.  * 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)
  1421.  * @param   array   course data
  1422.  * @param   int     session id
  1423.  * @param       int         course c_id
  1424.  * @return  array   array with exercise data
  1425.  * modified by Hubert Borderiou
  1426.  */
  1427. function get_all_exercises_for_course_id($course_info = null, $session_id = 0, $course_id=0) {
  1428.     $TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST);
  1429.     if ($session_id == -1) {
  1430.         $session_id  = 0;
  1431.     }
  1432.     if ($session_id == 0) {
  1433.         $conditions = array('where'=>array('active = ? AND session_id = ? AND c_id = ?'=>array('1', $session_id, $course_id)), 'order'=>'title');
  1434.     } else {
  1435.         //All exercises
  1436.         $conditions = array('where'=>array('active = ? AND (session_id = 0 OR session_id = ? ) AND c_id=?' =>array('1', $session_id, $course_id)), 'order'=>'title');
  1437.     }
  1438.     return Database::select('*',$TBL_EXERCICES, $conditions);
  1439. }
  1440.  
  1441. /**
  1442.  * Gets the position of the score based in a given score (result/weight) and the exe_id based in the user list
  1443.  * (NO Exercises in LPs )
  1444.  * @param   float   user score to be compared *attention* $my_score = score/weight and not just the score
  1445.  * @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)
  1446.  * @param   int     exercise id
  1447.  * @param   string  course code
  1448.  * @param   int     session id
  1449.  * @return  int     the position of the user between his friends in a course (or course within a session)
  1450.  */
  1451. function get_exercise_result_ranking($my_score, $my_exe_id, $exercise_id, $course_code, $session_id = 0, $user_list = array(), $return_string = true) {
  1452.     //No score given we return
  1453.     if (is_null($my_score)) {
  1454.         return '-';
  1455.     }
  1456.     if (empty($user_list)) {
  1457.         return '-';
  1458.     }
  1459.  
  1460.     $best_attempts = array();
  1461.     foreach ($user_list as $user_data) {
  1462.         $user_id = $user_data['user_id'];
  1463.         $best_attempts[$user_id]= get_best_attempt_by_user($user_id, $exercise_id, $course_code, $session_id);
  1464.     }
  1465.  
  1466.     if (empty($best_attempts)) {
  1467.         return 1;
  1468.     } else {
  1469.         $position = 1;
  1470.         $my_ranking = array();
  1471.         foreach($best_attempts as $user_id => $result) {
  1472.             if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1473.                 $my_ranking[$user_id] = $result['exe_result']/$result['exe_weighting'];
  1474.             } else {
  1475.                 $my_ranking[$user_id] = 0;
  1476.             }
  1477.         }
  1478.         //if (!empty($my_ranking)) {
  1479.             asort($my_ranking);
  1480.             $position = count($my_ranking);
  1481.             if (!empty($my_ranking)) {
  1482.                 foreach ($my_ranking as $user_id => $ranking) {
  1483.                     if ($my_score >= $ranking) {
  1484.                         if ($my_score == $ranking) {
  1485.                             $exe_id = $best_attempts[$user_id]['exe_id'];
  1486.                             if ($my_exe_id < $exe_id) {
  1487.                                 $position--;
  1488.                             }
  1489.                         } else {
  1490.                           $position--;
  1491.                         }
  1492.                     }
  1493.                 }
  1494.             }
  1495.         //}
  1496.         $return_value = array('position'=>$position, 'count'=>count($my_ranking));
  1497.         //var_dump($my_score, $my_ranking);
  1498.         if ($return_string) {
  1499.             if (!empty($position) && !empty($my_ranking)) {
  1500.                $return_value = $position.'/'.count($my_ranking);
  1501.             } else {
  1502.                 $return_value = '-';
  1503.             }
  1504.         }
  1505.         return $return_value;
  1506.     }
  1507. }
  1508.  
  1509. /**
  1510.  * Gets the position of the score based in a given score (result/weight) and the exe_id based in all attempts
  1511.  * (NO Exercises in LPs ) old funcionality by attempt
  1512.  * @param   float   user score to be compared attention => score/weight
  1513.  * @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)
  1514.  * @param   int     exercise id
  1515.  * @param   string  course code
  1516.  * @param   int     session id
  1517.  * @return  int     the position of the user between his friends in a course (or course within a session)
  1518.  */
  1519. function get_exercise_result_ranking_by_attempt($my_score, $my_exe_id, $exercise_id, $course_code, $session_id = 0, $return_string = true) {
  1520.     if (empty($session_id)) {
  1521.         $session_id = 0;
  1522.     }
  1523.     if (is_null($my_score)) {
  1524.         return '-';
  1525.     }
  1526.     $user_results = get_all_exercise_results($exercise_id, $course_code, $session_id, false);
  1527.     $position_data = array();
  1528.     if (empty($user_results)) {
  1529.         return 1;
  1530.     } else {
  1531.         $position = 1;
  1532.         $my_ranking = array();
  1533.         foreach($user_results as $result) {
  1534.             //print_r($result);
  1535.             if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1536.                 $my_ranking[$result['exe_id']] = $result['exe_result']/$result['exe_weighting'];
  1537.             } else {
  1538.                 $my_ranking[$result['exe_id']] = 0;
  1539.             }
  1540.         }
  1541.         asort($my_ranking);
  1542.         $position = count($my_ranking);
  1543.         if (!empty($my_ranking)) {
  1544.             foreach($my_ranking as $exe_id=>$ranking) {
  1545.                 if ($my_score >= $ranking) {
  1546.                     if ($my_score == $ranking) {
  1547.                         if ($my_exe_id < $exe_id) {
  1548.                             $position--;
  1549.                         }
  1550.                     } else {
  1551.                       $position--;
  1552.                     }
  1553.                 }
  1554.             }
  1555.         }
  1556.         $return_value = array('position'=>$position, 'count'=>count($my_ranking));
  1557.         //var_dump($my_score, $my_ranking);
  1558.         if ($return_string) {
  1559.             if (!empty($position) && !empty($my_ranking)) {
  1560.                return $position.'/'.count($my_ranking);
  1561.             }
  1562.         }
  1563.         return $return_value;
  1564.     }
  1565. }
  1566.  
  1567.  
  1568. /*
  1569.  *  Get the best attempt in a exercise (NO Exercises in LPs )
  1570.  */
  1571.  
  1572. function get_best_attempt_in_course($exercise_id, $course_code, $session_id) {
  1573.     $user_results = get_all_exercise_results($exercise_id, $course_code, $session_id, false);
  1574.     $best_score_data = array();
  1575.     $best_score = 0;
  1576.     if (!empty($user_results)) {
  1577.         foreach($user_results as $result) {
  1578.             if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1579.                 $score = $result['exe_result']/$result['exe_weighting'];
  1580.                 if ($score >= $best_score) {
  1581.                     $best_score = $score;
  1582.                     $best_score_data = $result;
  1583.                 }
  1584.             }
  1585.         }
  1586.     }
  1587.     return $best_score_data;
  1588. }
  1589.  
  1590. /*
  1591.  *  Get the best score in a exercise (NO Exercises in LPs )
  1592.  */
  1593. function get_best_attempt_by_user($user_id, $exercise_id, $course_code, $session_id) {
  1594.     $user_results = get_all_exercise_results($exercise_id, $course_code, $session_id, false, $user_id);
  1595.     $best_score_data = array();
  1596.     $best_score = 0;
  1597.     if (!empty($user_results)) {
  1598.         foreach($user_results as $result) {
  1599.             if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1600.                 $score = $result['exe_result']/$result['exe_weighting'];
  1601.                 if ($score >= $best_score) {
  1602.                     $best_score = $score;
  1603.                     $best_score_data = $result;
  1604.                 }
  1605.             }
  1606.         }
  1607.     }
  1608.     return $best_score_data;
  1609. }
  1610.  
  1611.  
  1612.  
  1613.  
  1614. /**
  1615.  * Get average score (NO Exercises in LPs )
  1616.  * @param   int exercise id
  1617.  * @param   string  course code
  1618.  * @param   int session id
  1619.  * @return  float   Average score
  1620.  */
  1621. function get_average_score($exercise_id, $course_code, $session_id) {
  1622.     $user_results = get_all_exercise_results($exercise_id, $course_code, $session_id);
  1623.     $avg_score = 0;
  1624.     if (!empty($user_results)) {
  1625.         foreach($user_results as $result) {
  1626.             if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1627.                 $score = $result['exe_result']/$result['exe_weighting'];
  1628.                 $avg_score +=$score;
  1629.             }
  1630.         }
  1631.         $avg_score = float_format($avg_score / count($user_results), 1);
  1632.     }
  1633.     return $avg_score;
  1634. }
  1635.  
  1636. /**
  1637.  * Get average score by score (NO Exercises in LPs )
  1638.  * @param   int exercise id
  1639.  * @param   string  course code
  1640.  * @param   int session id
  1641.  * @return  float   Average score
  1642.  */
  1643. function get_average_score_by_course($course_code, $session_id) {
  1644.     $user_results = get_all_exercise_results_by_course($course_code, $session_id, false);
  1645.     //echo $course_code.' - '.$session_id.'<br />';
  1646.     $avg_score = 0;
  1647.     if (!empty($user_results)) {
  1648.         foreach($user_results as $result) {
  1649.             if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1650.                 $score = $result['exe_result']/$result['exe_weighting'];
  1651.                 //var_dump($score);
  1652.                 $avg_score +=$score;
  1653.             }
  1654.         }
  1655.         //We asume that all exe_weighting
  1656.         //$avg_score = show_score( $avg_score / count($user_results) , $result['exe_weighting']);
  1657.         $avg_score = ($avg_score / count($user_results));
  1658.     }
  1659.     //var_dump($avg_score);
  1660.     return $avg_score;
  1661. }
  1662.  
  1663. function get_average_score_by_course_by_user($user_id, $course_code, $session_id) {
  1664.     $user_results = get_all_exercise_results_by_user($user_id, $course_code, $session_id);
  1665.     $avg_score = 0;
  1666.     if (!empty($user_results)) {
  1667.         foreach($user_results as $result) {
  1668.             if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1669.                 $score = $result['exe_result']/$result['exe_weighting'];
  1670.                 $avg_score +=$score;
  1671.             }
  1672.         }
  1673.         //We asume that all exe_weighting
  1674.         //$avg_score = show_score( $avg_score / count($user_results) , $result['exe_weighting']);
  1675.         $avg_score = ($avg_score / count($user_results));
  1676.     }
  1677.     return $avg_score;
  1678. }
  1679.  
  1680.  
  1681. /**
  1682.  * Get average score by score (NO Exercises in LPs )
  1683.  * @param   int     exercise id
  1684.  * @param   string  course code
  1685.  * @param   int     session id
  1686.  * @return  float   Best average score
  1687.  */
  1688. function get_best_average_score_by_exercise($exercise_id, $course_code, $session_id, $user_count) {
  1689.     $user_results = get_best_exercise_results_by_user($exercise_id, $course_code, $session_id);
  1690.     $avg_score = 0;
  1691.     if (!empty($user_results)) {
  1692.         foreach($user_results as $result) {
  1693.             if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1694.                 $score = $result['exe_result']/$result['exe_weighting'];
  1695.                 $avg_score +=$score;
  1696.             }
  1697.         }
  1698.         //We asume that all exe_weighting
  1699.         //$avg_score = show_score( $avg_score / count($user_results) , $result['exe_weighting']);
  1700.         //$avg_score = ($avg_score / count($user_results));
  1701.         if(!empty($user_count)) {
  1702.             $avg_score = float_format($avg_score / $user_count, 1) * 100;
  1703.         } else {
  1704.             $avg_score = 0;
  1705.         }
  1706.     }
  1707.     return $avg_score;
  1708. }
  1709.  
  1710. function get_exercises_to_be_taken($course_code, $session_id) {
  1711.     $course_info = api_get_course_info($course_code);
  1712.     $exercises = get_all_exercises($course_info, $session_id);
  1713.     $result = array();
  1714.     $now = time() + 15*24*60*60;
  1715.     foreach($exercises as $exercise_item) {
  1716.         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'], 'UTC') < $now) {
  1717.             $result[] = $exercise_item;
  1718.         }
  1719.     }
  1720.     return $result;
  1721. }
  1722.  
  1723. /**
  1724.  * Get student results (only in completed exercises) stats by question
  1725.  * @param   int     question id
  1726.  * @param   int     exercise id
  1727.  * @param   string  course code
  1728.  * @param   int     session id
  1729.  *
  1730.  * */
  1731. function get_student_stats_by_question($question_id,  $exercise_id, $course_code, $session_id) {
  1732.     $track_exercises    = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1733.     $track_attempt      = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1734.  
  1735.     $question_id        = intval($question_id);
  1736.     $exercise_id        = intval($exercise_id);
  1737.     $course_code        = Database::escape_string($course_code);
  1738.     $session_id         = intval($session_id);
  1739.  
  1740.     $sql = "SELECT MAX(marks) as max , MIN(marks) as min, AVG(marks) as average
  1741.             FROM $track_exercises e INNER JOIN $track_attempt a ON (a.exe_id = e.exe_id)
  1742.             WHERE   exe_exo_id      = $exercise_id AND
  1743.                     course_code     = '$course_code' AND
  1744.                     e.session_id    = $session_id AND
  1745.                     question_id     = $question_id AND status = '' LIMIT 1";
  1746.     $result = Database::query($sql);
  1747.     $return = array();
  1748.     if ($result) {
  1749.         $return = Database::fetch_array($result, 'ASSOC');
  1750.  
  1751.     }
  1752.     return $return;
  1753. }
  1754.  
  1755. function get_number_students_question_with_answer_count($question_id, $exercise_id, $course_code, $session_id) {
  1756.     $track_exercises    = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1757.     $track_attempt      = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1758.     $course_user        = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1759.  
  1760.     $question_id        = intval($question_id);
  1761.     $exercise_id        = intval($exercise_id);
  1762.     $course_code        = Database::escape_string($course_code);
  1763.     $session_id         = intval($session_id);
  1764.  
  1765.  
  1766.     $sql = "SELECT DISTINCT exe_user_id
  1767.             FROM $track_exercises e INNER JOIN $track_attempt a ON (a.exe_id = e.exe_id) INNER JOIN $course_user cu
  1768.                ON cu.course_code = a.course_code AND cu.user_id  = exe_user_id
  1769.             WHERE   exe_exo_id      = $exercise_id AND
  1770.                     a.course_code   = '$course_code' AND
  1771.                     e.session_id    = $session_id AND
  1772.                     question_id     = $question_id AND
  1773.                    answer          <> '0' AND
  1774.                    cu.status       = ".STUDENT." AND
  1775.                    relation_type  <> 2 AND
  1776.                    e.status        = ''";
  1777.     $result = Database::query($sql);
  1778.     $return = 0;
  1779.     if ($result) {
  1780.         $return = Database::num_rows($result);
  1781.     }
  1782.     return $return;
  1783. }
  1784.  
  1785. function get_number_students_answer_hotspot_count($answer_id, $question_id,  $exercise_id, $course_code, $session_id) {
  1786.     $track_exercises    = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1787.     $track_hotspot      = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  1788.     $course_user        = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1789.  
  1790.     $question_id        = intval($question_id);
  1791.     $answer_id          = intval($answer_id);
  1792.     $exercise_id        = intval($exercise_id);
  1793.     $course_code        = Database::escape_string($course_code);
  1794.     $session_id         = intval($session_id);
  1795.  
  1796.     $sql = "SELECT DISTINCT exe_user_id
  1797.             FROM $track_exercises e INNER JOIN $track_hotspot a ON (a.hotspot_exe_id = e.exe_id) INNER JOIN $course_user cu
  1798.                ON cu.course_code = a.hotspot_course_code AND cu.user_id  = exe_user_id
  1799.             WHERE   exe_exo_id              = $exercise_id AND
  1800.                     a.hotspot_course_code   = '$course_code' AND
  1801.                     e.session_id            = $session_id AND
  1802.                    hotspot_answer_id       = $answer_id AND
  1803.                     hotspot_question_id     = $question_id AND
  1804.                    cu.status               = ".STUDENT." AND
  1805.                    hotspot_correct         =  1 AND
  1806.                    relation_type           <> 2 AND
  1807.                    e.status                = ''";
  1808.  
  1809.     $result = Database::query($sql);
  1810.     $return = 0;
  1811.     if ($result) {
  1812.         $return = Database::num_rows($result);
  1813.     }
  1814.     return $return;
  1815. }
  1816.  
  1817.  
  1818. function get_number_students_answer_count($answer_id, $question_id, $exercise_id, $course_code, $session_id, $question_type = null, $correct_answer = null, $current_answer = null) {
  1819.     $track_exercises    = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1820.     $track_attempt      = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1821.     $course_user        = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1822.  
  1823.     $question_id        = intval($question_id);
  1824.     $answer_id          = intval($answer_id);
  1825.     $exercise_id        = intval($exercise_id);
  1826.     $course_code        = Database::escape_string($course_code);
  1827.     $session_id         = intval($session_id);
  1828.  
  1829.     switch ($question_type) {
  1830.         case FILL_IN_BLANKS:
  1831.             $answer_condition = "";
  1832.             $select_condition = " e.exe_id, answer ";
  1833.             break;
  1834.         case MATCHING:
  1835.         default:
  1836.             $answer_condition = " answer = $answer_id AND ";
  1837.             $select_condition = " DISTINCT exe_user_id ";
  1838.     }
  1839.  
  1840.     $sql = "SELECT $select_condition
  1841.             FROM $track_exercises e INNER JOIN $track_attempt a ON (a.exe_id = e.exe_id) INNER JOIN $course_user cu
  1842.                ON cu.course_code = a.course_code AND cu.user_id  = exe_user_id
  1843.             WHERE   exe_exo_id      = $exercise_id AND
  1844.                     a.course_code   = '$course_code' AND
  1845.                     e.session_id    = $session_id AND
  1846.                    $answer_condition
  1847.                     question_id     = $question_id AND
  1848.                    cu.status        = ".STUDENT." AND
  1849.                    relation_type <> 2 AND
  1850.                    e.status = ''";
  1851.     //var_dump($sql);
  1852.     $result = Database::query($sql);
  1853.     $return = 0;
  1854.     if ($result) {
  1855.         $good_answers = 0;
  1856.         switch ($question_type) {
  1857.             case FILL_IN_BLANKS:
  1858.                 while ($row = Database::fetch_array($result, 'ASSOC')) {
  1859.                     $fill_blank = check_fill_in_blanks($correct_answer, $row['answer']);
  1860.                     if (isset($fill_blank[$current_answer]) && $fill_blank[$current_answer] == 1 ) {
  1861.                         $good_answers++;
  1862.                     }
  1863.                 }
  1864.                 return $good_answers;
  1865.                 break;
  1866.             case MATCHING:
  1867.             default:
  1868.                 $return = Database::num_rows($result);
  1869.         }
  1870.     }
  1871.     return $return;
  1872. }
  1873.  
  1874.  
  1875. function check_fill_in_blanks($answer, $user_answer) {
  1876.     // the question is encoded like this
  1877.     // [A] B [C] D [E] F::10,10,10@1
  1878.     // number 1 before the "@" means that is a switchable fill in blank question
  1879.     // [A] B [C] D [E] F::10,10,10@ or  [A] B [C] D [E] F::10,10,10
  1880.     // means that is a normal fill blank question
  1881.     // first we explode the "::"
  1882.     $pre_array = explode('::', $answer);
  1883.     // is switchable fill blank or not
  1884.     $last = count($pre_array) - 1;
  1885.     $is_set_switchable = explode('@', $pre_array[$last]);
  1886.     $switchable_answer_set = false;
  1887.     if (isset ($is_set_switchable[1]) && $is_set_switchable[1] == 1) {
  1888.         $switchable_answer_set = true;
  1889.     }
  1890.     $answer = '';
  1891.     for ($k = 0; $k < $last; $k++) {
  1892.         $answer .= $pre_array[$k];
  1893.     }
  1894.     // splits weightings that are joined with a comma
  1895.     $answerWeighting = explode(',', $is_set_switchable[0]);
  1896.  
  1897.     // we save the answer because it will be modified
  1898.     //$temp = $answer;
  1899.     $temp = $answer;
  1900.  
  1901.     $answer = '';
  1902.     $j = 0;
  1903.     //initialise answer tags
  1904.     $user_tags = $correct_tags = $real_text = array ();
  1905.     // the loop will stop at the end of the text
  1906.     while (1) {
  1907.         // quits the loop if there are no more blanks (detect '[')
  1908.         if (($pos = api_strpos($temp, '[')) === false) {
  1909.             // adds the end of the text
  1910.             $answer = $temp;
  1911.             /* // Deprecated code
  1912.              // TeX parsing - replacement of texcode tags
  1913.             $texstring = api_parse_tex($texstring);
  1914.             $answer = str_replace("{texcode}", $texstring, $answer);
  1915.             */
  1916.             $real_text[] = $answer;
  1917.             break; //no more "blanks", quit the loop
  1918.         }
  1919.         // adds the piece of text that is before the blank
  1920.         //and ends with '[' into a general storage array
  1921.         $real_text[] = api_substr($temp, 0, $pos +1);
  1922.         $answer .= api_substr($temp, 0, $pos +1);
  1923.         //take the string remaining (after the last "[" we found)
  1924.         $temp = api_substr($temp, $pos +1);
  1925.         // quit the loop if there are no more blanks, and update $pos to the position of next ']'
  1926.         if (($pos = api_strpos($temp, ']')) === false) {
  1927.             // adds the end of the text
  1928.             $answer .= $temp;
  1929.             break;
  1930.         }
  1931.  
  1932.         $str = $user_answer;
  1933.  
  1934.         preg_match_all('#\[([^[]*)\]#', $str, $arr);
  1935.         $str = str_replace('\r\n', '', $str);
  1936.         $choice = $arr[1];
  1937.  
  1938.         $tmp = api_strrpos($choice[$j],' / ');
  1939.         $choice[$j] = api_substr($choice[$j],0,$tmp);
  1940.         $choice[$j] = trim($choice[$j]);
  1941.  
  1942.         //Needed to let characters ' and " to work as part of an answer
  1943.         $choice[$j] = stripslashes($choice[$j]);
  1944.  
  1945.         $user_tags[] = api_strtolower($choice[$j]);
  1946.         //put the contents of the [] answer tag into correct_tags[]
  1947.         $correct_tags[] = api_strtolower(api_substr($temp, 0, $pos));
  1948.         $j++;
  1949.         $temp = api_substr($temp, $pos +1);
  1950.     }
  1951.  
  1952.     $answer = '';
  1953.     $real_correct_tags = $correct_tags;
  1954.     $chosen_list = array();
  1955.  
  1956.     $good_answer = array();
  1957.  
  1958.     for ($i = 0; $i < count($real_correct_tags); $i++) {
  1959.         if (!$switchable_answer_set) {
  1960.             //needed to parse ' and " characters
  1961.             $user_tags[$i] = stripslashes($user_tags[$i]);
  1962.             if ($correct_tags[$i] == $user_tags[$i]) {
  1963.                 $good_answer[$correct_tags[$i]] = 1;
  1964.             } elseif (!empty ($user_tags[$i])) {
  1965.                 $good_answer[$correct_tags[$i]] = 0;
  1966.             } else {
  1967.                 $good_answer[$correct_tags[$i]] = 0;
  1968.             }
  1969.         } else {
  1970.             // switchable fill in the blanks
  1971.             if (in_array($user_tags[$i], $correct_tags)) {
  1972.                 $correct_tags = array_diff($correct_tags, $chosen_list);
  1973.                 $good_answer[$correct_tags[$i]] = 1;
  1974.             } elseif (!empty ($user_tags[$i])) {
  1975.                 $good_answer[$correct_tags[$i]] = 0;
  1976.             } else {
  1977.                 $good_answer[$correct_tags[$i]] = 0;
  1978.             }
  1979.         }
  1980.         // adds the correct word, followed by ] to close the blank
  1981.         $answer .= ' / <font color="green"><b>' . $real_correct_tags[$i] . '</b></font>]';
  1982.         if (isset ($real_text[$i +1])) {
  1983.             $answer .= $real_text[$i +1];
  1984.         }
  1985.     }
  1986.     return $good_answer;
  1987. }
  1988.  
  1989.  
  1990. function get_number_students_finish_exercise($exercise_id, $course_code, $session_id) {
  1991.     $track_exercises    = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1992.     $track_attempt      = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1993.  
  1994.     $exercise_id        = intval($exercise_id);
  1995.     $course_code        = Database::escape_string($course_code);
  1996.     $session_id         = intval($session_id);
  1997.  
  1998.     $sql = "SELECT DISTINCT exe_user_id
  1999.             FROM $track_exercises e INNER JOIN $track_attempt a ON (a.exe_id = e.exe_id)
  2000.             WHERE   exe_exo_id      = $exercise_id AND
  2001.                     course_code     = '$course_code' AND
  2002.                     e.session_id    = $session_id AND
  2003.                     status = ''";
  2004.     $result = Database::query($sql);
  2005.     $return = 0;
  2006.     if ($result) {
  2007.         $return = Database::num_rows($result);
  2008.  
  2009.     }
  2010.     return $return;
  2011. }
  2012.  
  2013.  
  2014.  
  2015. /**
  2016. // return the HTML code for a menu with students group
  2017. // @input : $in_name : is the name and the id of the <select>
  2018. //          $in_default : default value for option
  2019. // @return : the html code of the <select>
  2020. */
  2021. function displayGroupMenu($in_name, $in_default, $in_onchange="") {
  2022.     // check the default value of option
  2023.     $tabSelected = array($in_default => " selected='selected' ");
  2024.     $res = "";
  2025.     $res .= "<select name='$in_name' id='$in_name' onchange='".$in_onchange."' >";
  2026.     $res .= "<option value='-1'".$tabSelected["-1"].">-- ".get_lang('AllGroups')." --</option>";
  2027.     $res .= "<option value='0'".$tabSelected["0"].">- ".get_lang('NotInAGroup')." -</option>";
  2028.     $tabGroups = GroupManager::get_group_list();
  2029.     $currentCatId = 0;
  2030.     for ($i=0; $i < count($tabGroups); $i++) {
  2031.         $tabCategory = GroupManager::get_category_from_group($tabGroups[$i]["id"]);
  2032.         if ($tabCategory["id"] != $currentCatId) {
  2033.             $res .= "<option value='-1' disabled='disabled'>".$tabCategory["title"]."</option>";
  2034.             $currentCatId = $tabCategory["id"];
  2035.         }
  2036.         $res .= "<option ".$tabSelected[$tabGroups[$i]["id"]]."style='margin-left:40px' value='".$tabGroups[$i]["id"]."'>".$tabGroups[$i]["name"]."</option>";
  2037.     }
  2038.     $res .= "</select>";
  2039.     return $res;
  2040. }
  2041.  
  2042.  
  2043. /**
  2044.  * Return a list of group for user with user_id=in_userid separated with in_separator
  2045.  * @deprecated ?
  2046.  */
  2047. function displayGroupsForUser($in_separator, $in_userid) {
  2048.     $res = implode($in_separator, GroupManager::get_user_group_name($in_userid));
  2049.     if ($res == "") {
  2050.         $res = "<div style='text-align:center'>-</div>";
  2051.     }
  2052.     return $res;
  2053. }
  2054.  
  2055. function create_chat_exercise_session($exe_id) {
  2056.     if (!isset($_SESSION['current_exercises'])) {
  2057.         $_SESSION['current_exercises'] = array();
  2058.     }
  2059.     $_SESSION['current_exercises'][$exe_id] = true;
  2060. }
  2061.  
  2062. function delete_chat_exercise_session($exe_id) {
  2063.     if (isset($_SESSION['current_exercises'])) {
  2064.         $_SESSION['current_exercises'][$exe_id] = false;
  2065.     }
  2066. }
  2067.  
  2068.  
  2069. /**
  2070.  * Display the exercise results
  2071.  * @param obj   exercise obj
  2072.  * @param int   attempt id (exe_id)
  2073.  * @param bool  save users results (true) or just show the results (false)
  2074.  */
  2075. function display_question_list_by_attempt($objExercise, $exe_id, $save_user_result = false) {
  2076.     global $origin, $debug;
  2077.  
  2078.     //Getting attempt info
  2079.     $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exe_id);
  2080.  
  2081.     //Getting question list
  2082.     $question_list = array();
  2083.     if (!empty($exercise_stat_info['data_tracking'])) {
  2084.         $question_list = explode(',', $exercise_stat_info['data_tracking']);
  2085.     } else {
  2086.         //Try getting the question list only if save result is off
  2087.         if ($save_user_result == false) {
  2088.             $question_list = $objExercise->get_validated_question_list();
  2089.         }
  2090.         error_log("Data tracking is empty! exe_id: $exe_id");
  2091.     }
  2092.  
  2093.     $counter = 1;
  2094.     $total_score = $total_weight = 0;
  2095.  
  2096.     $exercise_content = null;
  2097.  
  2098.     //Hide results
  2099.     $show_results     = false;
  2100.     $show_only_score  = false;
  2101.  
  2102.     if ($objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS) {
  2103.         $show_results = true;
  2104.     }
  2105.  
  2106.     if (in_array($objExercise->results_disabled, array(RESULT_DISABLE_SHOW_SCORE_ONLY, RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES))) {
  2107.         $show_only_score = true;
  2108.     }
  2109.  
  2110.     if ($show_results || $show_only_score) {
  2111.         $user_info   = api_get_user_info($exercise_stat_info['exe_user_id']);
  2112.         //Shows exercise header
  2113.         echo $objExercise->show_exercise_result_header($user_info['complete_name'], api_convert_and_format_date($exercise_stat_info['start_date'], DATE_TIME_FORMAT_LONG), $exercise_stat_info['duration']);
  2114.     }
  2115.  
  2116.     // Display text when test is finished #4074 and for LP #4227
  2117.     $end_of_message = $objExercise->selectTextWhenFinished();
  2118.     if (!empty($end_of_message)) {
  2119.         Display::display_normal_message($end_of_message, false);
  2120.         echo "<div class='clear'>&nbsp;</div>";
  2121.     }
  2122.  
  2123.     $question_list_answers = array();
  2124.     $media_list = array();
  2125.     $category_list = array();
  2126.  
  2127.     // Loop over all question to show results for each of them, one by one
  2128.     if (!empty($question_list)) {
  2129.         if ($debug) { error_log('Looping question_list '.print_r($question_list,1));}
  2130.         foreach ($question_list as $questionId) {
  2131.  
  2132.             // creates a temporary Question object
  2133.             $objQuestionTmp = Question :: read($questionId);
  2134.  
  2135.             //this variable commes from exercise_submit_modal.php
  2136.             ob_start();
  2137.  
  2138.             // We're inside *one* question. Go through each possible answer for this question
  2139.             $result = $objExercise->manage_answer($exercise_stat_info['exe_id'], $questionId, null, 'exercise_result', array(), $save_user_result, true, $show_results, $objExercise->selectPropagateNeg(), $hotspot_delineation_result);
  2140.             if (empty($result)) {
  2141.                 continue;
  2142.             }
  2143.  
  2144.             $total_score     += $result['score'];
  2145.             $total_weight    += $result['weight'];
  2146.  
  2147.             $question_list_answers[] = array(
  2148.                 'question' => $result['open_question'],
  2149.                 'answer' => $result['open_answer'],
  2150.                 'answer_type' => $result['answer_type']
  2151.             );
  2152.  
  2153.             $my_total_score  = $result['score'];
  2154.             $my_total_weight = $result['weight'];
  2155.  
  2156.  
  2157.             //Category report
  2158.             $category_was_added_for_this_test = false;
  2159.  
  2160.             if (isset($objQuestionTmp->category) && !empty($objQuestionTmp->category)) {
  2161.                 $category_list[$objQuestionTmp->category]['score'] += $my_total_score;
  2162.                 $category_list[$objQuestionTmp->category]['total'] += $my_total_weight;
  2163.                 $category_was_added_for_this_test = true;
  2164.             }
  2165.  
  2166.             if (isset($objQuestionTmp->category_list) && !empty($objQuestionTmp->category_list)) {
  2167.                 foreach($objQuestionTmp->category_list as $category_id) {
  2168.                     $category_list[$category_id]['score'] += $my_total_score;
  2169.                     $category_list[$category_id]['total'] += $my_total_weight;
  2170.                     $category_was_added_for_this_test = true;
  2171.                 }
  2172.             }
  2173.  
  2174.             //No category for this question!
  2175.             if ($category_was_added_for_this_test == false) {
  2176.                 $category_list['none']['score'] += $my_total_score;
  2177.                 $category_list['none']['total'] += $my_total_weight;
  2178.             }
  2179.  
  2180.             if ($objExercise->selectPropagateNeg() == 0 && $my_total_score < 0) {
  2181.                 $my_total_score = 0;
  2182.             }
  2183.  
  2184.             $comnt = null;
  2185.             if ($show_results) {
  2186.                 $comnt = get_comments($exe_id, $questionId);
  2187.                 if (!empty($comnt)) {
  2188.                     echo '<b>'.get_lang('Feedback').'</b>';
  2189.                     echo '<div id="question_feedback">'.$comnt.'</div>';
  2190.                 }
  2191.             }
  2192.  
  2193.             $score = array();
  2194.             if ($show_results) {
  2195.                 $score['result']    = get_lang('Score')." : ".show_score($my_total_score, $my_total_weight, false, true);
  2196.                 $score['pass']      = $my_total_score >= $my_total_weight ? true : false;
  2197.                 $score['score']     = $my_total_score;
  2198.                 $score['weight']    = $my_total_weight;
  2199.                 $score['comments']  = $comnt;
  2200.             }
  2201.  
  2202.             $contents = ob_get_clean();
  2203.  
  2204.             $question_content = '<div class="question_row">';
  2205.  
  2206.             if ($show_results) {
  2207.                 $show_media = false;
  2208.                 /*if ($objQuestionTmp->parent_id != 0 && !in_array($objQuestionTmp->parent_id, $media_list)) {
  2209.                     $show_media = true;
  2210.                     $media_list[] = $objQuestionTmp->parent_id;
  2211.                 }*/
  2212.                 //Shows question title an description
  2213.                 $question_content .= $objQuestionTmp->return_header(null, $counter, $score);
  2214.             }
  2215.             $counter++;
  2216.  
  2217.             $question_content .= $contents;
  2218.             $question_content .= '</div>';
  2219.  
  2220.             $exercise_content .= $question_content;
  2221.  
  2222.         } // end foreach() block that loops over all questions
  2223.     }
  2224.  
  2225.     $total_score_text = null;
  2226.  
  2227.     if ($origin != 'learnpath') {
  2228.         if ($show_results || $show_only_score) {
  2229.             $total_score_text .= '<div class="question_row">';
  2230.             $total_score_text .= get_question_ribbon($objExercise, $total_score, $total_weight, true);
  2231.             $total_score_text .= '</div>';
  2232.         }
  2233.     }
  2234.  
  2235.     if (!empty($category_list) && ($show_results || $show_only_score) ) {
  2236.         //Adding total
  2237.         $category_list['total'] = array('score' => $total_score, 'total' => $total_weight);
  2238.         echo Testcategory::get_stats_table_by_attempt($objExercise->id, $category_list);
  2239.     }
  2240.  
  2241.     echo $total_score_text;
  2242.     echo $exercise_content;
  2243.     if (!$show_only_score) {
  2244.         echo $total_score_text;
  2245.     }
  2246.  
  2247.     if ($save_user_result) {
  2248.  
  2249.         // Tracking of results
  2250.         $learnpath_id           = $exercise_stat_info['orig_lp_id'];
  2251.         $learnpath_item_id      = $exercise_stat_info['orig_lp_item_id'];
  2252.         $learnpath_item_view_id = $exercise_stat_info['orig_lp_item_view_id'];
  2253.  
  2254.         if (api_is_allowed_to_session_edit()) {
  2255.             update_event_exercice($exercise_stat_info['exe_id'], $objExercise->selectId(), $total_score, $total_weight, api_get_session_id(), $learnpath_id, $learnpath_item_id, $learnpath_item_view_id, $exercise_stat_info['exe_duration'], $question_list, '', array(), $end_date);
  2256.         }
  2257.  
  2258.         // Send notification ..
  2259.         if (!api_is_allowed_to_edit(null,true)) {
  2260.             $objExercise->send_notification_for_open_questions($question_list_answers, $origin, $exe_id);
  2261.             $objExercise->send_notification_for_oral_questions($question_list_answers, $origin, $exe_id);
  2262.         }
  2263.     }
  2264. }
  2265.  
  2266.  
  2267. function get_question_ribbon($objExercise, $score, $weight, $check_pass_percentage = false) {
  2268.     $ribbon = '<div class="ribbon">';
  2269.     if ($check_pass_percentage) {
  2270.         $is_success = is_success_exercise_result($score, $weight, $objExercise->selectPassPercentage());
  2271.         // Color the final test score if pass_percentage activated
  2272.         $ribbon_total_success_or_error = "";
  2273.         if (is_pass_pourcentage_enabled($objExercise->selectPassPercentage())) {
  2274.             if ($is_success) {
  2275.                 $ribbon_total_success_or_error = ' ribbon-total-success';
  2276.             } else {
  2277.                 $ribbon_total_success_or_error = ' ribbon-total-error';
  2278.             }
  2279.         }
  2280.         $ribbon .= '<div class="rib rib-total '.$ribbon_total_success_or_error.'">';
  2281.     } else {
  2282.         $ribbon .= '<div class="rib rib-total">';
  2283.     }
  2284.     $ribbon .= '<h3>'.get_lang('YourTotalScore').":&nbsp;";
  2285.     $ribbon .= show_score($score, $weight, false, true);
  2286.     $ribbon .= '</h3>';
  2287.     $ribbon .= '</div>';
  2288.     if ($check_pass_percentage) {
  2289.         $ribbon .= show_success_message($score, $weight, $objExercise->selectPassPercentage());
  2290.     }
  2291.  
  2292.  
  2293.     $ribbon .= '</div>';
  2294.     return $ribbon;
  2295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement