exercise.lib.php 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673
  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. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  20. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  21. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  22. $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER);
  23. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  24. $main_user_table = Database::get_main_table(TABLE_MAIN_USER);
  25. $main_course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  26. $TBL_TRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  27. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);*/
  28. /**
  29. * Shows a question
  30. *
  31. * @param int question id
  32. * @param bool if true only show the questions, no exercise title
  33. * @param bool origin i.e = learnpath
  34. * @param int current item from the list of questions
  35. * @param int number of total questions
  36. * */
  37. function showQuestion($questionId, $only_questions = false, $origin = false, $current_item = '', $show_title = true, $freeze = false, $user_choice = array()) {
  38. // Text direction for the current language
  39. $is_ltr_text_direction = api_get_text_direction() != 'rtl';
  40. $remind_question = 1;
  41. // Change false to true in the following line to enable answer hinting.
  42. $debug_mark_answer = api_is_allowed_to_edit() && false;
  43. // Reads question information
  44. if (!$objQuestionTmp = Question::read($questionId)) {
  45. // Question not found
  46. return false;
  47. }
  48. $answerType = $objQuestionTmp->selectType();
  49. $pictureName = $objQuestionTmp->selectPicture();
  50. $html = '';
  51. if ($answerType != HOT_SPOT && $answerType != HOT_SPOT_DELINEATION) {
  52. // Question is not a hotspot
  53. if (!$only_questions) {
  54. $questionDescription = $objQuestionTmp->selectDescription();
  55. if ($show_title) {
  56. Testcategory::displayCategoryAndTitle($objQuestionTmp->id); //
  57. echo Display::div($current_item.'. '.$objQuestionTmp->selectTitle(), array('class'=>'question_title'));
  58. }
  59. if (!empty($questionDescription)) {
  60. echo Display::div($questionDescription, array('class'=>'question_description'));
  61. }
  62. //@deprecated
  63. if (!empty($pictureName)) {
  64. //echo "<img src='../document/download.php?doc_url=%2Fimages%2F'".$pictureName."' border='0'>";
  65. }
  66. }
  67. if (in_array($answerType, array(FREE_ANSWER, ORAL_EXPRESSION)) && $freeze) {
  68. return '';
  69. }
  70. echo '<div class="question_options">';
  71. //$s .= '<table width="720" class="exercise_options" style="width: 720px;'.$option_ie.' background-color:#fff;">';
  72. $s = '';
  73. $s .= '<table class="exercise_options">';
  74. // construction of the Answer object (also gets all answers details)
  75. $objAnswerTmp = new Answer($questionId);
  76. $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
  77. $course_id = api_get_course_int_id();
  78. $quiz_question_options = Question::readQuestionOption($questionId, $course_id);
  79. // For "matching" type here, we need something a little bit special
  80. // because the match between the suggestions and the answers cannot be
  81. // done easily (suggestions and answers are in the same table), so we
  82. // have to go through answers first (elems with "correct" value to 0).
  83. $select_items = array();
  84. //This will contain the number of answers on the left side. We call them
  85. // suggestions here, for the sake of comprehensions, while the ones
  86. // on the right side are called answers
  87. $num_suggestions = 0;
  88. if ($answerType == MATCHING) {
  89. $x = 1; //iterate through answers
  90. $letter = 'A'; //mark letters for each answer
  91. $answer_matching = $cpt1 = array();
  92. for ($answerId=1; $answerId <= $nbrAnswers; $answerId++) {
  93. $answerCorrect = $objAnswerTmp->isCorrect($answerId);
  94. $numAnswer = $objAnswerTmp->selectAutoId($answerId);
  95. $answer=$objAnswerTmp->selectAnswer($answerId);
  96. if ($answerCorrect==0) {
  97. // options (A, B, C, ...) that will be put into the list-box
  98. // have the "correct" field set to 0 because they are answer
  99. $cpt1[$x] = $letter;
  100. $answer_matching[$x]=$objAnswerTmp->selectAnswerByAutoId($numAnswer);
  101. $x++; $letter++;
  102. }
  103. }
  104. $i = 1;
  105. $select_items[0]['id'] = 0;
  106. $select_items[0]['letter'] = '--';
  107. $select_items[0]['answer'] = '';
  108. foreach ($answer_matching as $id => $value) {
  109. $select_items[$i]['id'] = $value['id'];
  110. $select_items[$i]['letter'] = $cpt1[$id];
  111. $select_items[$i]['answer'] = $value['answer'];
  112. $i ++;
  113. }
  114. $num_suggestions = ($nbrAnswers - $x) + 1;
  115. } elseif ($answerType == FREE_ANSWER) {
  116. $fck_content = isset($user_choice[0]) && !empty($user_choice[0]['answer']) ? $user_choice[0]['answer']:null;
  117. $oFCKeditor = new FCKeditor("choice[".$questionId."]") ;
  118. $oFCKeditor->ToolbarSet = 'TestFreeAnswer';
  119. $oFCKeditor->Width = '100%';
  120. $oFCKeditor->Height = '200';
  121. $oFCKeditor->Value = $fck_content;
  122. $s .= '<tr><td colspan="3">';
  123. $s .= $oFCKeditor->CreateHtml();
  124. $s .= '</td></tr>';
  125. } elseif ($answerType == ORAL_EXPRESSION) {
  126. //Add nanog
  127. if (api_get_setting('enable_nanogong') == 'true') {
  128. require_once api_get_path(LIBRARY_PATH).'nanogong.lib.php';
  129. //@todo pass this as a parameter
  130. global $exercise_stat_info, $exerciseId,$exe_id;
  131. if (!empty($exercise_stat_info)) {
  132. $params = array(
  133. 'exercise_id' => $exercise_stat_info['exe_exo_id'],
  134. 'exe_id' => $exercise_stat_info['exe_id'],
  135. 'question_id' => $questionId
  136. );
  137. } else {
  138. $params = array(
  139. 'exercise_id' => $exerciseId,
  140. 'exe_id' => 'temp_exe',
  141. 'question_id' => $questionId
  142. );
  143. }
  144. $nano = new Nanogong($params);
  145. echo $nano->show_button();
  146. }
  147. $oFCKeditor = new FCKeditor("choice[".$questionId."]") ;
  148. $oFCKeditor->ToolbarSet = 'TestFreeAnswer';
  149. $oFCKeditor->Width = '100%';
  150. $oFCKeditor->Height = '150';
  151. $oFCKeditor->ToolbarStartExpanded = false;
  152. $oFCKeditor->Value = '' ;
  153. $s .= '<tr><td colspan="3">';
  154. $s .= $oFCKeditor->CreateHtml();
  155. $s .= '</td></tr>';
  156. }
  157. // Now navigate through the possible answers, using the max number of
  158. // answers for the question as a limiter
  159. $lines_count = 1; // a counter for matching-type answers
  160. if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE || $answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
  161. $header = '';
  162. $header .= Display::tag('th', get_lang('Options'));
  163. foreach ($objQuestionTmp->options as $key=>$item) {
  164. $header .= Display::tag('th', $item);
  165. }
  166. $s.= Display::tag('tr',$header, array('style'=>'text-align:left;'));
  167. }
  168. $matching_correct_answer = 0;
  169. $user_choice_array = array();
  170. if (!empty($user_choice)) {
  171. foreach($user_choice as $item) {
  172. $user_choice_array[] = $item['answer'];
  173. }
  174. }
  175. for ($answerId=1; $answerId <= $nbrAnswers; $answerId++) {
  176. $answer = $objAnswerTmp->selectAnswer($answerId);
  177. $answerCorrect = $objAnswerTmp->isCorrect($answerId);
  178. $numAnswer = $objAnswerTmp->selectAutoId($answerId);
  179. // Unique answer
  180. if ($answerType == UNIQUE_ANSWER || $answerType == UNIQUE_ANSWER_NO_OPTION) {
  181. // set $debug_mark_answer to true at function start to
  182. // show the correct answer with a suffix '-x'
  183. $help = $selected = '';
  184. if ($debug_mark_answer) {
  185. if ($answerCorrect) {
  186. $help = 'x-';
  187. $selected = 'checked';
  188. }
  189. }
  190. $input_id = 'choice-'.$questionId.'-'.$answerId;
  191. if (isset($user_choice[0]['answer']) && $user_choice[0]['answer'] == $numAnswer ) {
  192. $attributes = array('id' =>$input_id, 'class'=>'checkbox','checked'=>1, 'selected'=>1);
  193. } else {
  194. $attributes = array('id' =>$input_id, 'class'=>'checkbox');
  195. }
  196. $answer = Security::remove_XSS($answer, STUDENT);
  197. $s .= Display::input('hidden','choice2['.$questionId.']','0');
  198. //@todo fix $is_ltr_text_direction
  199. //<p style="float: '.($is_ltr_text_direction ? 'left' : 'right').'; padding-'.($is_ltr_text_direction ? 'right' : 'left').': 4px;">
  200. //$s .= '<div style="margin-'.($is_ltr_text_direction ? 'left' : 'right').': 24px;">'.
  201. $s .= '<tr><td colspan="3">';
  202. $s .= '<span class="question_answer">';
  203. $s .= Display::input('radio', 'choice['.$questionId.']', $numAnswer, $attributes);
  204. $s .= Display::tag('label', $answer, array('for'=>$input_id)).'</span>';
  205. $s .= '</td></tr>';
  206. } elseif ($answerType == MULTIPLE_ANSWER || $answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
  207. // multiple answers
  208. // set $debug_mark_answer to true at function start to
  209. // show the correct answer with a suffix '-x'
  210. $help = $selected = '';
  211. if ($debug_mark_answer) {
  212. if ($answerCorrect) {
  213. $help = 'x-';
  214. $selected = 'checked="checked"';
  215. }
  216. }
  217. $input_id = 'choice-'.$questionId.'-'.$answerId;
  218. $answer = Security::remove_XSS($answer, STUDENT);
  219. if (in_array($numAnswer, $user_choice_array)) {
  220. $attributes = array('id' =>$input_id, 'class'=>'checkbox','checked'=>1, 'selected'=>1);
  221. } else {
  222. $attributes = array('id' =>$input_id, 'class'=>'checkbox');
  223. }
  224. $answer = Security::remove_XSS($answer, STUDENT);
  225. if ($answerType == MULTIPLE_ANSWER) {
  226. $s .= '<input type="hidden" name="choice2['.$questionId.']" value="0" />';
  227. $s .= '<tr><td colspan="3">';
  228. $s .= '<span class="question_answer">';
  229. if ($debug_mark_answer) {
  230. if ($answerCorrect) {
  231. //$options['checked'] = 'checked';
  232. }
  233. }
  234. $s .= Display::tag('span', Display::input('checkbox', 'choice['.$questionId.']['.$numAnswer.']', $numAnswer, $attributes));
  235. $s .= Display::tag('label', $answer, array('for'=>$input_id)).'</span></td></tr>';
  236. } elseif ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
  237. $my_choice = array();
  238. if (!empty($user_choice_array)) {
  239. foreach ($user_choice_array as $item) {
  240. $item = explode(':', $item);
  241. $my_choice[$item[0]] = $item[1];
  242. }
  243. }
  244. $s .='<tr>';
  245. $s .= Display::tag('td', $answer);
  246. if (!empty($quiz_question_options)) {
  247. foreach ($quiz_question_options as $id=>$item) {
  248. if (isset($my_choice[$numAnswer]) && $id == $my_choice[$numAnswer]) {
  249. $attributes = array('class'=>'checkbox','checked'=>1, 'selected'=>1);
  250. } else {
  251. $attributes = array('class'=>'checkbox');
  252. }
  253. $s .= Display::tag('td', Display::input('radio', 'choice['.$questionId.']['.$numAnswer.']', $id, $attributes));
  254. }
  255. }
  256. $s.='<tr>';
  257. }
  258. } elseif ($answerType == MULTIPLE_ANSWER_COMBINATION) {
  259. // multiple answers
  260. // set $debug_mark_answer to true at function start to
  261. // show the correct answer with a suffix '-x'
  262. $help = $selected = '';
  263. if ($debug_mark_answer) {
  264. if ($answerCorrect) {
  265. $help = 'x-';
  266. $selected = 'checked="checked"';
  267. }
  268. }
  269. $input_id = 'choice-'.$questionId.'-'.$answerId;
  270. if (in_array($numAnswer, $user_choice_array)) {
  271. $attributes = array('id'=>$input_id, 'class'=>'checkbox','checked'=>1, 'selected'=>1);
  272. } else {
  273. $attributes = array('id'=>$input_id, 'class'=>'checkbox');
  274. }
  275. $answer = Security::remove_XSS($answer, STUDENT);
  276. $s .= '<input type="hidden" name="choice2['.$questionId.']" value="0" />'.
  277. '<tr><td colspan="3">';
  278. $s .= '<span class="question_answer">';
  279. $s .= Display::tag('span', Display::input('checkbox', 'choice['.$questionId.']['.$numAnswer.']', 1, $attributes));
  280. $s .= Display::tag('label', $answer, array('for'=>$input_id)).'</span></td></tr>';
  281. } elseif ($answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
  282. // multiple answers
  283. // set $debug_mark_answer to true at function start to
  284. // show the correct answer with a suffix '-x'
  285. $s .= '<input type="hidden" name="choice2['.$questionId.']" value="0" />';
  286. $my_choice = array();
  287. if (!empty($user_choice_array)) {
  288. foreach ($user_choice_array as $item) {
  289. $item = explode(':', $item);
  290. $my_choice[$item[0]] = $item[1];
  291. }
  292. }
  293. $answer = Security::remove_XSS($answer, STUDENT);
  294. $s .='<tr>';
  295. $s .= Display::tag('td', $answer);
  296. foreach ($objQuestionTmp->options as $key => $item) {
  297. //$options['value'] = $key;
  298. if (isset($my_choice[$numAnswer]) && $key == $my_choice[$numAnswer]) {
  299. $attributes = array('class'=>'checkbox','checked'=>1, 'selected'=>1);
  300. } else {
  301. $attributes = array('class'=>'checkbox');
  302. }
  303. $s .= Display::tag('td', Display::input('radio','choice['.$questionId.']['.$numAnswer.']', $key, $attributes));
  304. }
  305. $s.='<tr>';
  306. } elseif ($answerType == FILL_IN_BLANKS) {
  307. /*
  308. // splits text and weightings that are joined with the character '::'
  309. list($answer) = explode('::',$answer);
  310. //getting the matches
  311. $answer = api_ereg_replace('\[[^]]+\]','<input type="text" name="choice['.$questionId.'][]" size="10" />',($answer));
  312. $answer = api_preg_replace('/\[[^]]+\]/', Display::input('text', "choice[$questionId][]", '', $attributes), $answer);
  313. api_preg_match_all('/\[[^]]+\]/', $answer, $fill_list);
  314. if (isset($user_choice[0]['answer'])) {
  315. api_preg_match_all('/\[[^]]+\]/', $user_choice[0]['answer'], $user_fill_list);
  316. $user_fill_list = $user_fill_list[0];
  317. }*/
  318. list($answer) = explode('::',$answer);
  319. api_preg_match_all('/\[[^]]+\]/', $answer, $teacher_answer_list);
  320. if (isset($user_choice[0]['answer'])) {
  321. api_preg_match_all('/\[[^]]+\]/', $user_choice[0]['answer'], $student_answer_list);
  322. $student_answer_list = $student_answer_list[0];
  323. }
  324. if (!empty($teacher_answer_list) && !empty($student_answer_list)) {
  325. $teacher_answer_list = $teacher_answer_list[0];
  326. $i = 0;
  327. foreach($teacher_answer_list as $teacher_item) {
  328. $value = null;
  329. if (isset($student_answer_list[$i]) && !empty($student_answer_list[$i])) {
  330. //Cleaning student answer list
  331. $value = strip_tags($student_answer_list[$i]);
  332. $value = api_substr($value,1, api_strlen($value)-2);
  333. $value = explode('/', $value);
  334. if (!empty($value[0])) {
  335. $value = trim($value[0]);
  336. $value = str_replace('&nbsp;', '', $value);
  337. }
  338. $answer = api_preg_replace('/\['.$teacher_item.'+\]/', Display::input('text', "choice[$questionId][]", $value), $answer);
  339. }
  340. $i++;
  341. }
  342. } else {
  343. $answer = api_preg_replace('/\[[^]]+\]/', Display::input('text', "choice[$questionId][]", '', $attributes), $answer);
  344. }
  345. $s .= '<tr><td colspan="3">'.$answer.'</td></tr>';
  346. } elseif ($answerType == MATCHING) {
  347. // matching type, showing suggestions and answers
  348. // TODO: replace $answerId by $numAnswer
  349. if ($answerCorrect != 0) {
  350. // only show elements to be answered (not the contents of
  351. // the select boxes, who are corrrect = 0)
  352. $s .= '<tr><td width="45%" valign="top">';
  353. $parsed_answer = $answer;
  354. //left part questions
  355. $s .= ' <span style="float:left; width:8%;"><b>'.$lines_count.'</b>.&nbsp;</span>
  356. <span style="float:left; width:92%;">'.$parsed_answer.'</span></td>';
  357. //middle part (matches selects)
  358. $s .= '<td width="10%" valign="top" align="center">&nbsp;&nbsp;
  359. <select name="choice['.$questionId.']['.$numAnswer.']">';
  360. // fills the list-box
  361. foreach ($select_items as $key=>$val) {
  362. // set $debug_mark_answer to true at function start to
  363. // show the correct answer with a suffix '-x'
  364. $help = $selected = '';
  365. if ($debug_mark_answer) {
  366. if ($val['id'] == $answerCorrect) {
  367. $help = '-x';
  368. //$selected = 'selected="selected"';
  369. }
  370. }
  371. if (isset($user_choice[$matching_correct_answer]) && $val['id'] == $user_choice[$matching_correct_answer]['answer']) {
  372. $selected = 'selected="selected"';
  373. }
  374. $s .= '<option value="'.$val['id'].'" '.$selected.'>'.$val['letter'].$help.'</option>';
  375. } // end foreach()
  376. $s .= '</select></td>';
  377. //print_r($select_items);
  378. //right part (answers)
  379. $s.='<td width="45%" valign="top" >';
  380. if (isset($select_items[$lines_count])) {
  381. $s.='<span style="float:left; width:5%;"><b>'.$select_items[$lines_count]['letter'].'.</b></span>'.
  382. '<span style="float:left; width:95%;">'.$select_items[$lines_count]['answer'].'</span>';
  383. } else {
  384. $s.='&nbsp;';
  385. }
  386. $s .= '</td>';
  387. $s .= '</tr>';
  388. $lines_count++;
  389. //if the left side of the "matching" has been completely
  390. // shown but the right side still has values to show...
  391. if (($lines_count -1) == $num_suggestions) {
  392. // if it remains answers to shown at the right side
  393. while (isset($select_items[$lines_count])) {
  394. $s .= '<tr>
  395. <td colspan="2"></td>
  396. <td valign="top">';
  397. $s.='<b>'.$select_items[$lines_count]['letter'].'.</b> '.$select_items[$lines_count]['answer'];
  398. $s.="</td>
  399. </tr>";
  400. $lines_count++;
  401. } // end while()
  402. } // end if()
  403. $matching_correct_answer++;
  404. }
  405. }
  406. } // end for()
  407. $s .= '</table>';
  408. $s .= '</div>';
  409. // destruction of the Answer object
  410. unset($objAnswerTmp);
  411. // destruction of the Question object
  412. unset($objQuestionTmp);
  413. if ($origin != 'export') {
  414. echo $s;
  415. } else {
  416. return $s;
  417. }
  418. } elseif ($answerType == HOT_SPOT || $answerType == HOT_SPOT_DELINEATION) {
  419. // Question is a HOT_SPOT
  420. //checking document/images visibility
  421. if (api_is_platform_admin() || api_is_course_admin()) {
  422. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  423. $course = api_get_course_info();
  424. $doc_id = DocumentManager::get_document_id($course, '/images/'.$pictureName);
  425. if (is_numeric($doc_id)) {
  426. $images_folder_visibility = api_get_item_visibility($course,'document', $doc_id, api_get_session_id());
  427. if (!$images_folder_visibility) {
  428. //This message is shown only to the course/platform admin if the image is set to visibility = false
  429. Display::display_warning_message(get_lang('ChangeTheVisibilityOfTheCurrentImage'));
  430. }
  431. }
  432. }
  433. $questionName = $objQuestionTmp->selectTitle();
  434. $questionDescription = $objQuestionTmp->selectDescription();
  435. if ($freeze) {
  436. echo Display::img($objQuestionTmp->selectPicturePath());
  437. return;
  438. }
  439. // Get the answers, make a list
  440. $objAnswerTmp = new Answer($questionId);
  441. $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
  442. // get answers of hotpost
  443. $answers_hotspot = array();
  444. for ($answerId=1;$answerId <= $nbrAnswers;$answerId++) {
  445. $answers = $objAnswerTmp->selectAnswerByAutoId($objAnswerTmp->selectAutoId($answerId));
  446. $answers_hotspot[$answers['id']] = $objAnswerTmp->selectAnswer($answerId);
  447. }
  448. // display answers of hotpost order by id
  449. $answer_list = '<div style="padding: 10px; margin-left: 0px; border: 1px solid #A4A4A4; height: 408px; width: 200px;"><b>'.get_lang('HotspotZones').'</b><dl>';
  450. if (!empty($answers_hotspot)) {
  451. ksort($answers_hotspot);
  452. foreach ($answers_hotspot as $key => $value) {
  453. $answer_list .= '<dt>'.$key.'.- '.$value.'</dt><br />';
  454. }
  455. }
  456. $answer_list .= '</dl></div>';
  457. if ($answerType == HOT_SPOT_DELINEATION) {
  458. $answer_list='';
  459. $swf_file = 'hotspot_delineation_user';
  460. $swf_height = 405;
  461. } else {
  462. $swf_file = 'hotspot_user';
  463. $swf_height = 436;
  464. }
  465. if (!$only_questions) {
  466. if ($show_title) {
  467. Testcategory::displayCategoryAndTitle($objQuestionTmp->id);
  468. echo '<div class="question_title">'.$current_item.'. '.$questionName.'</div>';
  469. }
  470. //@todo I need to the get the feedback type
  471. echo '<input type="hidden" name="hidden_hotspot_id" value="'.$questionId.'" />';
  472. echo '<table class="exercise_questions" >
  473. <tr>
  474. <td valign="top" colspan="2">';
  475. echo $questionDescription;
  476. echo '</td></tr>';
  477. }
  478. $canClick = isset($_GET['editQuestion']) ? '0' : (isset($_GET['modifyAnswers']) ? '0' : '1');
  479. $s .= '<script type="text/javascript" src="../plugin/hotspot/JavaScriptFlashGateway.js"></script>
  480. <script src="../plugin/hotspot/hotspot.js" type="text/javascript" ></script>
  481. <script type="text/javascript">
  482. <!--
  483. // Globals
  484. // Major version of Flash required
  485. var requiredMajorVersion = 7;
  486. // Minor version of Flash required
  487. var requiredMinorVersion = 0;
  488. // Minor version of Flash required
  489. var requiredRevision = 0;
  490. // the version of javascript supported
  491. var jsVersion = 1.0;
  492. // -->
  493. </script>
  494. <script language="VBScript" type="text/vbscript">
  495. <!-- // Visual basic helper required to detect Flash Player ActiveX control version information
  496. Function VBGetSwfVer(i)
  497. on error resume next
  498. Dim swControl, swVersion
  499. swVersion = 0
  500. set swControl = CreateObject("ShockwaveFlash.ShockwaveFlash." + CStr(i))
  501. if (IsObject(swControl)) then
  502. swVersion = swControl.GetVariable("$version")
  503. end if
  504. VBGetSwfVer = swVersion
  505. End Function
  506. // -->
  507. </script>
  508. <script language="JavaScript1.1" type="text/javascript">
  509. <!-- // Detect Client Browser type
  510. var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
  511. var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
  512. var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
  513. jsVersion = 1.1;
  514. // JavaScript helper required to detect Flash Player PlugIn version information
  515. function JSGetSwfVer(i) {
  516. // NS/Opera version >= 3 check for Flash plugin in plugin array
  517. if (navigator.plugins != null && navigator.plugins.length > 0) {
  518. if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
  519. var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
  520. var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
  521. descArray = flashDescription.split(" ");
  522. tempArrayMajor = descArray[2].split(".");
  523. versionMajor = tempArrayMajor[0];
  524. versionMinor = tempArrayMajor[1];
  525. if ( descArray[3] != "" ) {
  526. tempArrayMinor = descArray[3].split("r");
  527. } else {
  528. tempArrayMinor = descArray[4].split("r");
  529. }
  530. versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
  531. flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
  532. } else {
  533. flashVer = -1;
  534. }
  535. }
  536. // MSN/WebTV 2.6 supports Flash 4
  537. else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
  538. // WebTV 2.5 supports Flash 3
  539. else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
  540. // older WebTV supports Flash 2
  541. else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
  542. // Can\'t detect in all other cases
  543. else
  544. {
  545. flashVer = -1;
  546. }
  547. return flashVer;
  548. }
  549. // When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
  550. function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision) {
  551. reqVer = parseFloat(reqMajorVer + "." + reqRevision);
  552. // loop backwards through the versions until we find the newest version
  553. for (i=25;i>0;i--) {
  554. if (isIE && isWin && !isOpera) {
  555. versionStr = VBGetSwfVer(i);
  556. } else {
  557. versionStr = JSGetSwfVer(i);
  558. }
  559. if (versionStr == -1 ) {
  560. return false;
  561. } else if (versionStr != 0) {
  562. if(isIE && isWin && !isOpera) {
  563. tempArray = versionStr.split(" ");
  564. tempString = tempArray[1];
  565. versionArray = tempString .split(",");
  566. } else {
  567. versionArray = versionStr.split(".");
  568. }
  569. versionMajor = versionArray[0];
  570. versionMinor = versionArray[1];
  571. versionRevision = versionArray[2];
  572. versionString = versionMajor + "." + versionRevision; // 7.0r24 == 7.24
  573. versionNum = parseFloat(versionString);
  574. // is the major.revision >= requested major.revision AND the minor version >= requested minor
  575. if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
  576. return true;
  577. } else {
  578. return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
  579. }
  580. }
  581. }
  582. }
  583. // -->
  584. </script>';
  585. $s .= '<tr><td valign="top" colspan="2" width="520"><table><tr><td width="520">
  586. <script language="JavaScript" type="text/javascript">
  587. <!--
  588. // Version check based upon the values entered above in "Globals"
  589. var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
  590. // Check to see if the version meets the requirements for playback
  591. if (hasReqestedVersion) { // if we\'ve detected an acceptable version
  592. var oeTags = \'<object type="application/x-shockwave-flash" data="../plugin/hotspot/'.$swf_file.'.swf?modifyAnswers='.$questionId.'&amp;canClick:'.$canClick.'" width="600" height="'.$swf_height.'">\'
  593. + \'<param name="wmode" value="transparent">\'
  594. + \'<param name="movie" value="../plugin/hotspot/'.$swf_file.'.swf?modifyAnswers='.$questionId.'&amp;canClick:'.$canClick.'" />\'
  595. + \'<\/object>\';
  596. document.write(oeTags); // embed the Flash Content SWF when all tests are passed
  597. } else { // flash is too old or we can\'t detect the plugin
  598. var alternateContent = "Error<br \/>"
  599. + "Hotspots requires Macromedia Flash 7.<br \/>"
  600. + "<a href=\"http://www.macromedia.com/go/getflash/\">Get Flash<\/a>";
  601. document.write(alternateContent); // insert non-flash content
  602. }
  603. // -->
  604. </script>
  605. </td>
  606. <td valign="top" align="left">'.$answer_list.'</td></tr>
  607. </table>
  608. </td></tr>';
  609. echo $s;
  610. }
  611. echo '</table>';
  612. return $nbrAnswers;
  613. }
  614. function get_exercise_track_exercise_info($exe_id) {
  615. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  616. $TBL_TRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  617. $exe_id = intval($exe_id);
  618. $result = array();
  619. if (!empty($exe_id)) {
  620. $sql_fb_type = 'SELECT * FROM '.$TBL_EXERCICES.' as e INNER JOIN '.$TBL_TRACK_EXERCICES.' as te ON (e.id=te.exe_exo_id) WHERE te.exe_id='.$exe_id;
  621. $res_fb_type = Database::query($sql_fb_type);
  622. $result = Database::fetch_array($res_fb_type, 'ASSOC');
  623. }
  624. return $result;
  625. }
  626. /**
  627. * Validates the time control key
  628. */
  629. function exercise_time_control_is_valid($exercise_id) {
  630. $course_id = api_get_course_int_id();
  631. $exercise_id = intval($exercise_id);
  632. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  633. $sql = "SELECT expired_time FROM $TBL_EXERCICES WHERE c_id = $course_id AND id = $exercise_id";
  634. $result = Database::query($sql);
  635. $row = Database::fetch_array($result, 'ASSOC');
  636. if (!empty($row['expired_time']) ) {
  637. $current_expired_time_key = get_time_control_key($exercise_id);
  638. if (isset($_SESSION['expired_time'][$current_expired_time_key])) {
  639. $current_time = time();
  640. $expired_time = api_strtotime($_SESSION['expired_time'][$current_expired_time_key], 'UTC');
  641. $total_time_allowed = $expired_time + 30;
  642. //error_log('expired time converted + 30: '.$total_time_allowed);
  643. //error_log('$current_time: '.$current_time);
  644. if ($total_time_allowed < $current_time) {
  645. return false;
  646. }
  647. return true;
  648. } else {
  649. return false;
  650. }
  651. } else {
  652. return true;
  653. }
  654. }
  655. /**
  656. Deletes the time control token
  657. */
  658. function exercise_time_control_delete($exercise_id) {
  659. $current_expired_time_key = get_time_control_key($exercise_id);
  660. unset($_SESSION['expired_time'][$current_expired_time_key]);
  661. }
  662. /**
  663. Generates the time control key
  664. */
  665. function generate_time_control_key($exercise_id) {
  666. $exercise_id = intval($exercise_id);
  667. return api_get_course_int_id().'_'.api_get_session_id().'_'.$exercise_id.'_'.api_get_user_id();
  668. }
  669. /**
  670. Returns the time controller key
  671. @todo this function is the same as generate_time_control_key
  672. */
  673. function get_time_control_key($exercise_id) {
  674. $exercise_id = intval($exercise_id);
  675. return api_get_course_int_id().'_'.api_get_session_id().'_'.$exercise_id.'_'.api_get_user_id();
  676. }
  677. /**
  678. * @todo use this function instead of get_time_control_key
  679. */
  680. function get_session_time_control_key($exercise_id) {
  681. $time_control_key = get_time_control_key($exercise_id);
  682. $return_value = $_SESSION['expired_time'][$time_control_key];
  683. return $return_value;
  684. }
  685. /**
  686. * Gets count of exam results
  687. * @todo this function should be moved in a library + no global calls
  688. * Modified by hubert borderiou 08-11-2011
  689. */
  690. function get_count_exam_results($exercise_id) {
  691. // I know it's bad to add a static integer here... but
  692. // it factorise function get_exam_results_data
  693. // and I think it worths it.
  694. //This is not bad for the hardcoded value, this is bad because you call 2 times get_exam_results_data()! jm
  695. //@todo use a real count select I know the sql constructions are
  696. $tabres = get_exam_results_data(0, 9999999, 0, "ASC", $exercise_id);
  697. return count($tabres);
  698. }
  699. /**
  700. * Gets the exam'data results
  701. * @todo this function should be moved in a library + no global calls
  702. */
  703. function get_exam_results_data($from, $number_of_items, $column, $direction, $exercise_id, $extra_where_conditions = null) {
  704. //@todo replace all this globals
  705. global $documentPath, $filter;
  706. if (empty($extra_where_conditions)) {
  707. $extra_where_conditions = "1 = 1 ";
  708. }
  709. $is_allowedToEdit = api_is_allowed_to_edit(null,true);
  710. $is_tutor = api_is_allowed_to_edit(true);
  711. $TBL_USER = Database :: get_main_table(TABLE_MAIN_USER);
  712. $TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST);
  713. $TBL_GROUP_REL_USER = Database :: get_course_table(TABLE_GROUP_USER);
  714. $TBL_GROUP = Database :: get_course_table(TABLE_GROUP);
  715. $TBL_TRACK_EXERCICES = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  716. $TBL_TRACK_HOTPOTATOES = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  717. $TBL_TRACK_ATTEMPT_RECORDING= Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  718. $session_id_and = ' AND te.session_id = ' . api_get_session_id() . ' ';
  719. $exercise_id = intval($exercise_id);
  720. $exercise_where = '';
  721. if (!empty($exercise_id)) {
  722. $exercise_where .= ' AND te.exe_exo_id = '.$exercise_id.' ';
  723. }
  724. $hotpotatoe_where = '';
  725. if (!empty($_GET['path'])) {
  726. $hotpotatoe_path = Database::escape_string($_GET['path']);
  727. $hotpotatoe_where .= ' AND exe_name = "'.$hotpotatoe_path.'" ';
  728. }
  729. // sql for chamilo-type tests for teacher / tutor view
  730. $sql_inner_join_tbl_track_exercices = "(SELECT ttte.*, if(tr.exe_id,1, 0) as revised FROM $TBL_TRACK_EXERCICES ttte LEFT JOIN $TBL_TRACK_ATTEMPT_RECORDING tr ON (ttte.exe_id = tr.exe_id) )";
  731. if ($is_allowedToEdit || $is_tutor) {
  732. //Teacher view
  733. if (isset($_GET['gradebook']) && $_GET['gradebook'] == 'view') {
  734. //$exercise_where_query = ' te.exe_exo_id = ce.id AND ';
  735. }
  736. $sqlFromOption = "";
  737. $sqlWhereOption = ""; // for hpsql
  738. $sql_inner_join_tbl_user = "";
  739. //@todo fix to work with COURSE_RELATION_TYPE_RRHH in both queries
  740. //Hack in order to filter groups
  741. $sql_inner_join_tbl_user = '';
  742. if (strpos($extra_where_conditions, 'group_id')) {
  743. $sql_inner_join_tbl_user = "
  744. (
  745. SELECT u.user_id, firstname, lastname, email, username, g.name as group_name, g.id as group_id
  746. FROM $TBL_USER u
  747. INNER JOIN $TBL_GROUP_REL_USER gru ON ( gru.user_id = u.user_id AND gru.c_id=".api_get_course_int_id().")
  748. INNER JOIN $TBL_GROUP g ON (gru.group_id = g.id)
  749. )";
  750. }
  751. if (strpos($extra_where_conditions, 'group_all')) {
  752. $extra_where_conditions = str_replace("AND ( group_id = 'group_all' )", '', $extra_where_conditions);
  753. $sql_inner_join_tbl_user = "
  754. (
  755. SELECT u.user_id, firstname, lastname, email, username, ' ' as group_name
  756. FROM $TBL_USER u
  757. )";
  758. }
  759. if (strpos($extra_where_conditions, 'group_none')) {
  760. $extra_where_conditions = str_replace("AND ( group_id = 'group_none' )", "AND ( group_id is null )", $extra_where_conditions);
  761. $sql_inner_join_tbl_user = "
  762. (
  763. SELECT u.user_id, firstname, lastname, email, username, g.name as group_name, g.id as group_id
  764. FROM $TBL_USER u
  765. LEFT OUTER JOIN $TBL_GROUP_REL_USER gru ON ( gru.user_id = u.user_id AND gru.c_id=".api_get_course_int_id()." )
  766. LEFT OUTER JOIN $TBL_GROUP g ON (gru.group_id = g.id AND g.c_id = ".api_get_course_int_id().")
  767. )";
  768. }
  769. //All
  770. if (empty($sql_inner_join_tbl_user)) {
  771. $sql_inner_join_tbl_user = "
  772. (
  773. SELECT u.user_id, firstname, lastname, email, username, ' ' as group_name
  774. FROM $TBL_USER u
  775. )";
  776. }
  777. $sqlFromOption = " , $TBL_GROUP_REL_USER AS gru ";
  778. $sqlWhereOption = " AND gru.c_id = ".api_get_course_int_id()." AND gru.user_id = user.user_id ";
  779. $first_and_last_name = api_is_western_name_order() ? "firstname, lastname" : "lastname, firstname";
  780. $sql = "SELECT DISTINCT
  781. user_id,
  782. $first_and_last_name,
  783. ce.title,
  784. username,
  785. te.exe_result,
  786. te.exe_weighting,
  787. te.exe_date,
  788. te.exe_id,
  789. email as exemail,
  790. te.start_date,
  791. steps_counter,
  792. exe_user_id,
  793. te.exe_duration,
  794. propagate_neg,
  795. revised, group_name
  796. FROM
  797. $TBL_EXERCICES AS ce
  798. INNER JOIN $sql_inner_join_tbl_track_exercices AS te ON (te.exe_exo_id = ce.id)
  799. INNER JOIN $sql_inner_join_tbl_user AS user ON (user.user_id = exe_user_id)
  800. WHERE $extra_where_conditions AND
  801. te.status != 'incomplete'
  802. AND te.exe_cours_id='" . api_get_course_id() . "' $session_id_and
  803. AND ce.active <>-1
  804. AND orig_lp_id = 0
  805. AND orig_lp_item_id = 0
  806. AND ce.c_id=".api_get_course_int_id()."
  807. $exercise_where ";
  808. //var_dump($sql );
  809. // sql for hotpotatoes tests for teacher / tutor view
  810. $hpsql = "SELECT
  811. $first_and_last_name ,
  812. username,
  813. tth.exe_name,
  814. tth.exe_result ,
  815. tth.exe_weighting,
  816. tth.exe_date
  817. FROM
  818. $TBL_TRACK_HOTPOTATOES tth,
  819. $TBL_USER user
  820. $sqlFromOption
  821. WHERE
  822. user.user_id=tth.exe_user_id
  823. AND tth.exe_cours_id = '" . api_get_course_id()."'
  824. $hotpotatoe_where
  825. $sqlWhereOption
  826. AND $where_condition
  827. ORDER BY
  828. tth.exe_cours_id ASC,
  829. tth.exe_date DESC";
  830. } else {
  831. //any view is proposed to the student, they should see the results in the overview.php page
  832. exit;
  833. // Student view
  834. $sql = "SELECT DISTINCT
  835. te.exe_duration,
  836. te.start_date,
  837. firstname,
  838. lastname,
  839. te.exe_result,
  840. te.exe_weighting,
  841. te.exe_date,
  842. te.exe_id,
  843. email as exemail,
  844. steps_counter,
  845. exe_user_id,
  846. ce.results_disabled,
  847. propagate_neg,
  848. revised
  849. FROM
  850. $TBL_EXERCICES AS ce
  851. INNER JOIN $sql_inner_join_tbl_track_exercices AS te ON (te.exe_exo_id = ce.id)
  852. INNER JOIN $TBL_USER AS user ON (user.user_id = exe_user_id)
  853. WHERE $extra_where_conditions AND
  854. te.status != 'incomplete'
  855. AND te.exe_cours_id = '".api_get_course_id()."'
  856. AND te.exe_user_id = ".api_get_user_id()." $session_id_and
  857. AND ce.active <>-1
  858. AND orig_lp_id = 0
  859. AND orig_lp_item_id = 0
  860. AND ce.c_id = ".api_get_course_int_id()."
  861. $exercise_where";
  862. $hpsql = "SELECT '', '', '', exe_name, exe_result , exe_weighting, exe_date
  863. FROM $TBL_TRACK_HOTPOTATOES
  864. WHERE exe_user_id = '" . api_get_user_id() . "' AND exe_cours_id = '" . api_get_course_id() . "' $hotpotatoe_where
  865. ORDER BY exe_cours_id ASC, exe_date DESC";
  866. }
  867. $teacher_list = CourseManager::get_teacher_list_from_course_code(api_get_course_id());
  868. $teacher_id_list = array();
  869. foreach ($teacher_list as $teacher) {
  870. $teacher_id_list[] = $teacher['user_id'];
  871. }
  872. if (empty($hotpotatoe_where)) {
  873. $column = empty($column) ? : Database::escape_string($column);
  874. $from = intval($from);
  875. $number_of_items = intval($number_of_items);
  876. $sql .= " ORDER BY $column $direction ";
  877. $sql .= " LIMIT $from, $number_of_items";
  878. //var_dump($sql);
  879. $results = array();
  880. $resx = Database::query($sql);
  881. while ($rowx = Database::fetch_array($resx,'ASSOC')) {
  882. $results[] = $rowx;
  883. }
  884. $list_info = array();
  885. // Print test results.
  886. $lang_nostartdate = get_lang('NoStartDate') . ' / ';
  887. if (is_array($results)) {
  888. $users_array_id = array();
  889. if ($_GET['gradebook'] == 'view') {
  890. $from_gradebook = true;
  891. }
  892. $sizeof = count($results);
  893. $user_list_id = array ();
  894. $duration_list = '';
  895. for ($i = 0; $i < $sizeof; $i++) {
  896. $revised = $results[$i]['revised'];
  897. if ($from_gradebook && ($is_allowedToEdit || $is_tutor)) {
  898. if (in_array($results[$i]['username'] . $results[$i]['firstname'] . $results[$i]['lastname'], $users_array_id)) {
  899. continue;
  900. }
  901. $users_array_id[] = $results[$i]['username'] . $results[$i]['firstname'] . $results[$i]['lastname'];
  902. }
  903. if ($is_allowedToEdit || $is_tutor) {
  904. $user = $results[$i]['firstname'] . $results[$i]['lastname'];
  905. $user_groups = displayGroupsForUser('<br/>', $results[$i]['user_id']);
  906. } else {
  907. $user = $results[$i]['firstname'] . $results[$i]['lastname'];
  908. }
  909. $user_list_id[] = $results[$i]['exe_user_id'];
  910. $id = $results[$i]['exe_id'];
  911. $dt = api_convert_and_format_date($results[$i]['exe_weighting']);
  912. // we filter the results if we have the permission to
  913. if (isset ($results[$i]['results_disabled']))
  914. $result_disabled = intval($results[$i]['results_disabled']);
  915. else
  916. $result_disabled = 0;
  917. if ($result_disabled == 0) {
  918. if ($is_allowedToEdit || $is_tutor) {
  919. $user = $results[$i]['firstname'] . $results[$i]['lastname'];
  920. $start_date = $results[$i]['start_date'];
  921. } else {
  922. $start_date = $results[$i]['start_date'];
  923. }
  924. if ($start_date != "0000-00-00 00:00:00") {
  925. $start_date_timestamp = api_strtotime($start_date, 'UTC');
  926. $exe_date_timestamp = api_strtotime($results[$i]['exe_date'], 'UTC');
  927. $my_duration = ceil((($exe_date_timestamp - $start_date_timestamp) / 60));
  928. //var_dump($start_date .' - '.$results[$i]['exdate'].' - '.$my_duration);
  929. if ($my_duration == 1 ) {
  930. $duration_list = $my_duration . ' ' . get_lang('MinMinute');
  931. } else {
  932. $duration_list = $my_duration. ' ' . get_lang('MinMinutes');
  933. }
  934. if ($results[$i]['exstep'] > 1) {
  935. $duration_list = ' ( ' . $results[$i]['steps_counter'] . ' ' . get_lang('Steps') . ' )';
  936. }
  937. } else {
  938. $duration_list = get_lang('NoLogOfDuration');
  939. }
  940. // Date conversion
  941. // there are already a duration test period calculated??
  942. //echo '<td>'.sprintf(get_lang('DurationFormat'), $duration).'</td>';
  943. // if the float look like 10.00 we show only 10
  944. $my_res = $results[$i]['exe_result'];
  945. $my_total = $results[$i]['exe_weighting'];
  946. $results[$i]['start_date'] = api_get_local_time($results[$i]['start_date']);
  947. $results[$i]['exe_date'] = api_get_local_time($results[$i]['exe_date']);
  948. if (!$results[$i]['propagate_neg'] && $my_res < 0) {
  949. $my_res = 0;
  950. }
  951. $score = show_score($my_res, $my_total);
  952. $actions = '';
  953. if ($is_allowedToEdit || $is_tutor) {
  954. if (in_array($results[$i]['exe_user_id'], $teacher_id_list)) {
  955. $actions .= Display::return_icon('teachers.gif', get_lang('Teacher'));
  956. }
  957. if ($revised) {
  958. $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);
  959. $actions .= '&nbsp;';
  960. } else {
  961. $actions .="<a href='exercise_show.php?".api_get_cidreq()."&action=qualify&id=$id'>".Display :: return_icon('quiz.gif', get_lang('Qualify'));
  962. $actions .='&nbsp;';
  963. }
  964. $actions .="</a>";
  965. if ($is_allowedToEdit) {
  966. if ($filter==2){
  967. $actions .=' <a href="exercise_history.php?'.api_get_cidreq().'&exe_id=' . $id . '">' .Display :: return_icon('history.gif', get_lang('ViewHistoryChange')).'</a>';
  968. }
  969. }
  970. if (api_is_platform_admin() || $is_tutor) {
  971. $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>';
  972. $actions .='&nbsp;';
  973. }
  974. } else {
  975. $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';
  976. $attempt_link = Display::url(get_lang('Show'), $attempt_url, array('class'=>'thickbox a_button white small'));
  977. $actions .= $attempt_link;
  978. }
  979. if ($revised) {
  980. $revised = Display::span(get_lang('Validated'), array('class'=>'label_tag success'));
  981. } else {
  982. $revised = Display::span(get_lang('NotValidated'), array('class'=>'label_tag notice'));
  983. }
  984. if ($is_allowedToEdit || $is_tutor) {
  985. $results[$i]['status'] = $revised;
  986. $results[$i]['score'] = $score;
  987. $results[$i]['actions'] = $actions;
  988. $list_info[] = $results[$i];
  989. } else {
  990. $results[$i]['status'] = $revised;
  991. $results[$i]['score'] = $score;
  992. $results[$i]['actions'] = $actions;
  993. $list_info[] = $results[$i];
  994. }
  995. }
  996. }
  997. }
  998. } else {
  999. //echo $hpsql; var_dump($hpsql);
  1000. $hpresults = getManyResultsXCol($hpsql, 6);
  1001. // Print HotPotatoes test results.
  1002. if (is_array($hpresults)) {
  1003. for ($i = 0; $i < sizeof($hpresults); $i++) {
  1004. $hp_title = GetQuizName($hpresults[$i][3], $documentPath);
  1005. if ($hp_title == '') {
  1006. $hp_title = basename($hpresults[$i][3]);
  1007. }
  1008. //var_dump($hpresults[$i]);
  1009. $hp_date = api_get_local_time($hpresults[$i][6], null, date_default_timezone_get());
  1010. $hp_result = round(($hpresults[$i][4] / ($hpresults[$i][5] != 0 ? $hpresults[$i][5] : 1)) * 100, 2).'% ('.$hpresults[$i][4].' / '.$hpresults[$i][5].')';
  1011. if ($is_allowedToEdit || $is_tutor) {
  1012. $list_info[] = array($hpresults[$i][0], $hpresults[$i][1], $hpresults[$i][2], '', $hp_title, '-', $hp_date , $hp_result , '-');
  1013. } else {
  1014. $list_info[] = array($hp_title, '-', $hp_date , $hp_result , '-');
  1015. }
  1016. }
  1017. }
  1018. }
  1019. //var_dump($list_info);
  1020. return $list_info;
  1021. }
  1022. /**
  1023. * Converts the score with the exercise_max_note and exercise_min_score the platform settings + formats the results using the float_format function
  1024. *
  1025. * @param float score
  1026. * @param float weight
  1027. * @param bool show porcentage or not
  1028. * @param bool use or not the platform settings
  1029. * @return string an html with the score modified
  1030. */
  1031. function show_score($score, $weight, $show_percentage = true, $use_platform_settings = true) {
  1032. if (is_null($score) && is_null($weight)) {
  1033. return '-';
  1034. }
  1035. $html = '';
  1036. $score_rounded = $score;
  1037. $max_note = api_get_setting('exercise_max_score');
  1038. $min_note = api_get_setting('exercise_min_score');
  1039. if ($use_platform_settings) {
  1040. if ($max_note != '' && $min_note != '') {
  1041. if (!empty($weight) && intval($weight) != 0) {
  1042. $score = $min_note + ($max_note - $min_note) * $score /$weight;
  1043. } else {
  1044. $score = $min_note;
  1045. }
  1046. $weight = $max_note;
  1047. }
  1048. }
  1049. $score_rounded = float_format($score, 1);
  1050. $weight = float_format($weight, 1);
  1051. if ($show_percentage) {
  1052. $parent = '(' . $score_rounded . ' / ' . $weight . ')';
  1053. $html = float_format(($score / ($weight != 0 ? $weight : 1)) * 100, 1) . "% $parent";
  1054. } else {
  1055. $html = $score_rounded . ' / ' . $weight;
  1056. }
  1057. return $html;
  1058. }
  1059. /**
  1060. * Converts a numeric value in a percentage example 0.66666 to 66.67 %
  1061. * @param $value
  1062. * @return float Converted number
  1063. */
  1064. function convert_to_percentage($value) {
  1065. $return = '-';
  1066. if ($value != '') {
  1067. $return = float_format($value * 100, 1).' %';
  1068. }
  1069. return $return;
  1070. }
  1071. /**
  1072. * Converts a score/weight values to the platform scale
  1073. * @param float score
  1074. * @param float weight
  1075. * @return float the score rounded converted to the new range
  1076. */
  1077. function convert_score($score, $weight) {
  1078. $max_note = api_get_setting('exercise_max_score');
  1079. $min_note = api_get_setting('exercise_min_score');
  1080. if ($score != '' && $weight != '') {
  1081. if ($max_note != '' && $min_note != '') {
  1082. if (!empty($weight)) {
  1083. $score = $min_note + ($max_note - $min_note) * $score / $weight;
  1084. } else {
  1085. $score = $min_note;
  1086. }
  1087. }
  1088. }
  1089. $score_rounded = float_format($score, 1);
  1090. return $score_rounded;
  1091. }
  1092. /**
  1093. * 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)
  1094. * @param array course data
  1095. * @param int session id
  1096. * @return array array with exercise data
  1097. */
  1098. function get_all_exercises($course_info = null, $session_id = 0, $check_publication_dates = false) {
  1099. $TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST);
  1100. $course_id = api_get_course_int_id();
  1101. if (!empty($course_info) && !empty($course_info['real_id'])) {
  1102. $course_id = $course_info['real_id'];
  1103. }
  1104. if ($session_id == -1) {
  1105. $session_id = 0;
  1106. }
  1107. $now = api_get_utc_datetime();
  1108. $time_conditions = '';
  1109. if ($check_publication_dates) {
  1110. $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
  1111. $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
  1112. $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
  1113. $time_conditions .= " (start_time = '0000-00-00 00:00:00' AND end_time = '0000-00-00 00:00:00')) "; // nothing is set
  1114. }
  1115. if ($session_id == 0) {
  1116. $conditions = array('where'=>array('active = ? AND session_id = ? AND c_id = ? '.$time_conditions => array('1', $session_id, $course_id)), 'order'=>'title');
  1117. } else {
  1118. //All exercises
  1119. $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');
  1120. }
  1121. return Database::select('*',$TBL_EXERCICES, $conditions);
  1122. }
  1123. /**
  1124. * 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)
  1125. * @param array course data
  1126. * @param int session id
  1127. * @param int course c_id
  1128. * @return array array with exercise data
  1129. * modified by Hubert Borderiou
  1130. */
  1131. function get_all_exercises_for_course_id($course_info = null, $session_id = 0, $course_id=0) {
  1132. $TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST);
  1133. if ($session_id == -1) {
  1134. $session_id = 0;
  1135. }
  1136. if ($session_id == 0) {
  1137. $conditions = array('where'=>array('active = ? AND session_id = ? AND c_id = ?'=>array('1', $session_id, $course_id)), 'order'=>'title');
  1138. } else {
  1139. //All exercises
  1140. $conditions = array('where'=>array('active = ? AND (session_id = 0 OR session_id = ? ) AND c_id=?' =>array('1', $session_id, $course_id)), 'order'=>'title');
  1141. }
  1142. return Database::select('*',$TBL_EXERCICES, $conditions);
  1143. }
  1144. /**
  1145. * Gets the position of the score based in a given score (result/weight) and the exe_id based in the user list
  1146. * (NO Exercises in LPs )
  1147. * @param float user score to be compared *attention* $my_score = score/weight and not just the score
  1148. * @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)
  1149. * @param int exercise id
  1150. * @param string course code
  1151. * @param int session id
  1152. * @return int the position of the user between his friends in a course (or course within a session)
  1153. */
  1154. function get_exercise_result_ranking($my_score, $my_exe_id, $exercise_id, $course_code, $session_id = 0, $user_list, $return_string = true) {
  1155. //No score given we return
  1156. if (is_null($my_score)) {
  1157. return '-';
  1158. }
  1159. if (empty($user_list)) {
  1160. return '-';
  1161. }
  1162. $best_attempts = array();
  1163. foreach($user_list as $user_data) {
  1164. $user_id = $user_data['user_id'];
  1165. $best_attempts[$user_id]= get_best_attempt_by_user($user_id, $exercise_id, $course_code, $session_id);
  1166. }
  1167. $position_data = array();
  1168. if (empty($best_attempts)) {
  1169. return 1;
  1170. } else {
  1171. $position = 1;
  1172. $my_ranking = array();
  1173. foreach($best_attempts as $user_id => $result) {
  1174. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1175. $my_ranking[$user_id] = $result['exe_result']/$result['exe_weighting'];
  1176. } else {
  1177. $my_ranking[$user_id] = 0;
  1178. }
  1179. }
  1180. //if (!empty($my_ranking)) {
  1181. asort($my_ranking);
  1182. $position = count($my_ranking);
  1183. if (!empty($my_ranking)) {
  1184. foreach($my_ranking as $user_id => $ranking) {
  1185. if ($my_score >= $ranking) {
  1186. if ($my_score == $ranking) {
  1187. $exe_id = $best_attempts[$user_id]['exe_id'];
  1188. if ($my_exe_id < $exe_id) {
  1189. $position--;
  1190. }
  1191. } else {
  1192. $position--;
  1193. }
  1194. }
  1195. }
  1196. }
  1197. //}
  1198. $return_value = array('position'=>$position, 'count'=>count($my_ranking));
  1199. //var_dump($my_score, $my_ranking);
  1200. if ($return_string) {
  1201. if (!empty($position) && !empty($my_ranking)) {
  1202. $return_value = $position.'/'.count($my_ranking);
  1203. } else {
  1204. $return_value = '-';
  1205. }
  1206. }
  1207. return $return_value;
  1208. }
  1209. }
  1210. /**
  1211. * Gets the position of the score based in a given score (result/weight) and the exe_id based in all attempts
  1212. * (NO Exercises in LPs ) old funcionality by attempt
  1213. * @param float user score to be compared attention => score/weight
  1214. * @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)
  1215. * @param int exercise id
  1216. * @param string course code
  1217. * @param int session id
  1218. * @return int the position of the user between his friends in a course (or course within a session)
  1219. */
  1220. function get_exercise_result_ranking_by_attempt($my_score, $my_exe_id, $exercise_id, $course_code, $session_id = 0, $return_string = true) {
  1221. if (empty($session_id)) {
  1222. $session_id = 0;
  1223. }
  1224. if (is_null($my_score)) {
  1225. return '-';
  1226. }
  1227. $user_results = get_all_exercise_results($exercise_id, $course_code, $session_id, false);
  1228. $position_data = array();
  1229. if (empty($user_results)) {
  1230. return 1;
  1231. } else {
  1232. $position = 1;
  1233. $my_ranking = array();
  1234. foreach($user_results as $result) {
  1235. //print_r($result);
  1236. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1237. $my_ranking[$result['exe_id']] = $result['exe_result']/$result['exe_weighting'];
  1238. } else {
  1239. $my_ranking[$result['exe_id']] = 0;
  1240. }
  1241. }
  1242. asort($my_ranking);
  1243. $position = count($my_ranking);
  1244. if (!empty($my_ranking)) {
  1245. foreach($my_ranking as $exe_id=>$ranking) {
  1246. if ($my_score >= $ranking) {
  1247. if ($my_score == $ranking) {
  1248. if ($my_exe_id < $exe_id) {
  1249. $position--;
  1250. }
  1251. } else {
  1252. $position--;
  1253. }
  1254. }
  1255. }
  1256. }
  1257. $return_value = array('position'=>$position, 'count'=>count($my_ranking));
  1258. //var_dump($my_score, $my_ranking);
  1259. if ($return_string) {
  1260. if (!empty($position) && !empty($my_ranking)) {
  1261. return $position.'/'.count($my_ranking);
  1262. }
  1263. }
  1264. return $return_value;
  1265. }
  1266. }
  1267. /*
  1268. * Get the best attempt in a exercise (NO Exercises in LPs )
  1269. */
  1270. function get_best_attempt_in_course($exercise_id, $course_code, $session_id) {
  1271. $user_results = get_all_exercise_results($exercise_id, $course_code, $session_id, false);
  1272. $best_score_data = array();
  1273. $best_score = 0;
  1274. if (!empty($user_results)) {
  1275. foreach($user_results as $result) {
  1276. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1277. $score = $result['exe_result']/$result['exe_weighting'];
  1278. if ($score >= $best_score) {
  1279. $best_score = $score;
  1280. $best_score_data = $result;
  1281. }
  1282. }
  1283. }
  1284. }
  1285. return $best_score_data;
  1286. }
  1287. /*
  1288. * Get the best score in a exercise (NO Exercises in LPs )
  1289. */
  1290. function get_best_attempt_by_user($user_id, $exercise_id, $course_code, $session_id) {
  1291. $user_results = get_all_exercise_results($exercise_id, $course_code, $session_id, false);
  1292. $best_score_data = array();
  1293. $best_score = 0;
  1294. if (!empty($user_results)) {
  1295. foreach($user_results as $result) {
  1296. if ($result['exe_user_id'] != $user_id) continue;
  1297. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1298. $score = $result['exe_result']/$result['exe_weighting'];
  1299. if ($score >= $best_score) {
  1300. $best_score = $score;
  1301. $best_score_data = $result;
  1302. }
  1303. }
  1304. }
  1305. }
  1306. return $best_score_data;
  1307. }
  1308. /**
  1309. * Get average score (NO Exercises in LPs )
  1310. * @param int exercise id
  1311. * @param string course code
  1312. * @param int session id
  1313. * @return float Average score
  1314. */
  1315. function get_average_score($exercise_id, $course_code, $session_id) {
  1316. $user_results = get_all_exercise_results($exercise_id, $course_code, $session_id);
  1317. $avg_score_data = array();
  1318. $avg_score = 0;
  1319. if (!empty($user_results)) {
  1320. foreach($user_results as $result) {
  1321. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1322. $score = $result['exe_result']/$result['exe_weighting'];
  1323. $avg_score +=$score;
  1324. }
  1325. }
  1326. $avg_score = float_format($avg_score / count($user_results), 1);
  1327. }
  1328. return $avg_score;
  1329. }
  1330. /**
  1331. * Get average score by score (NO Exercises in LPs )
  1332. * @param int exercise id
  1333. * @param string course code
  1334. * @param int session id
  1335. * @return float Average score
  1336. */
  1337. function get_average_score_by_course($course_code, $session_id) {
  1338. $user_results = get_all_exercise_results_by_course($course_code, $session_id, false);
  1339. //echo $course_code.' - '.$session_id.'<br />';
  1340. $avg_score = 0;
  1341. if (!empty($user_results)) {
  1342. foreach($user_results as $result) {
  1343. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1344. $score = $result['exe_result']/$result['exe_weighting'];
  1345. //var_dump($score);
  1346. $avg_score +=$score;
  1347. }
  1348. }
  1349. //We asume that all exe_weighting
  1350. //$avg_score = show_score( $avg_score / count($user_results) , $result['exe_weighting']);
  1351. $avg_score = ($avg_score / count($user_results));
  1352. }
  1353. //var_dump($avg_score);
  1354. return $avg_score;
  1355. }
  1356. function get_average_score_by_course_by_user($user_id, $course_code, $session_id) {
  1357. $user_results = get_all_exercise_results_by_user($user_id, $course_code, $session_id);
  1358. $avg_score = 0;
  1359. if (!empty($user_results)) {
  1360. foreach($user_results as $result) {
  1361. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1362. $score = $result['exe_result']/$result['exe_weighting'];
  1363. $avg_score +=$score;
  1364. }
  1365. }
  1366. //We asume that all exe_weighting
  1367. //$avg_score = show_score( $avg_score / count($user_results) , $result['exe_weighting']);
  1368. $avg_score = ($avg_score / count($user_results));
  1369. }
  1370. return $avg_score;
  1371. }
  1372. /**
  1373. * Get average score by score (NO Exercises in LPs )
  1374. * @param int exercise id
  1375. * @param string course code
  1376. * @param int session id
  1377. * @return float Best average score
  1378. */
  1379. function get_best_average_score_by_exercise($exercise_id, $course_code, $session_id, $user_count) {
  1380. $user_results = get_best_exercise_results_by_user($exercise_id, $course_code, $session_id);
  1381. $avg_score = 0;
  1382. if (!empty($user_results)) {
  1383. foreach($user_results as $result) {
  1384. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  1385. $score = $result['exe_result']/$result['exe_weighting'];
  1386. $avg_score +=$score;
  1387. }
  1388. }
  1389. //We asume that all exe_weighting
  1390. //$avg_score = show_score( $avg_score / count($user_results) , $result['exe_weighting']);
  1391. //$avg_score = ($avg_score / count($user_results));
  1392. if(!empty($user_count)) {
  1393. $avg_score = float_format($avg_score / $user_count, 1) * 100;
  1394. } else {
  1395. $avg_score = 0;
  1396. }
  1397. }
  1398. return $avg_score;
  1399. }
  1400. function get_exercises_to_be_taken($course_code, $session_id) {
  1401. $course_info = api_get_course_info($course_code);
  1402. $exercises = get_all_exercises($course_info, $session_id);
  1403. $result = array();
  1404. $now = time() + 15*24*60*60;
  1405. foreach($exercises as $exercise_item) {
  1406. 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) {
  1407. $result[] = $exercise_item;
  1408. }
  1409. }
  1410. return $result;
  1411. }
  1412. /**
  1413. * Get student results (only in completed exercises) stats by question
  1414. * @param int question id
  1415. * @param int exercise id
  1416. * @param string course code
  1417. * @param int session id
  1418. *
  1419. * */
  1420. function get_student_stats_by_question($question_id, $exercise_id, $course_code, $session_id) {
  1421. $track_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1422. $track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1423. $question_id = intval($question_id);
  1424. $exercise_id = intval($exercise_id);
  1425. $course_code = Database::escape_string($course_code);
  1426. $session_id = intval($session_id);
  1427. $sql = "SELECT count(exe_user_id) as users, MAX(marks) as max , MIN(marks) as min, AVG(marks) as average
  1428. FROM $track_exercises e INNER JOIN $track_attempt a ON (a.exe_id = e.exe_id)
  1429. WHERE exe_exo_id = $exercise_id AND
  1430. course_code = '$course_code' AND
  1431. e.session_id = $session_id AND
  1432. question_id = $question_id AND status = '' LIMIT 1";
  1433. $result = Database::query($sql);
  1434. $return = array();
  1435. if ($result) {
  1436. $return = Database::fetch_array($result, 'ASSOC');
  1437. }
  1438. return $return;
  1439. }
  1440. // ---------------------------------------------------------
  1441. // return the HTML code for a menu with students group
  1442. // @input : $in_name : is the name and the id of the <select>
  1443. // $in_default : default value for option
  1444. // @return : the html code of the <select>
  1445. // ---------------------------------------------------------
  1446. function displayGroupMenu($in_name, $in_default, $in_onchange="") {
  1447. // check the default value of option
  1448. $tabSelected = array($in_default => " selected='selected' ");
  1449. $res = "";
  1450. $res .= "<select name='$in_name' id='$in_name' onchange='".$in_onchange."' >";
  1451. $res .= "<option value='-1'".$tabSelected["-1"].">-- ".get_lang('AllGroups')." --</option>";
  1452. $res .= "<option value='0'".$tabSelected["0"].">- ".get_lang('NotInAGroup')." -</option>";
  1453. $tabGroups = GroupManager::get_group_list();
  1454. $currentCatId = 0;
  1455. for ($i=0; $i < count($tabGroups); $i++) {
  1456. $tabCategory = GroupManager::get_category_from_group($tabGroups[$i]["id"]);
  1457. if ($tabCategory["id"] != $currentCatId) {
  1458. $res .= "<option value='-1' disabled='disabled'>".$tabCategory["title"]."</option>";
  1459. $currentCatId = $tabCategory["id"];
  1460. }
  1461. $res .= "<option ".$tabSelected[$tabGroups[$i]["id"]]."style='margin-left:40px' value='".$tabGroups[$i]["id"]."'>".$tabGroups[$i]["name"]."</option>";
  1462. }
  1463. $res .= "</select>";
  1464. return $res;
  1465. }
  1466. // ------------------------------------------------------
  1467. // return a list of group for user with user_id=in_userid
  1468. // separated with in_separator
  1469. // --------------------------------------------------
  1470. function displayGroupsForUser($in_separator, $in_userid) {
  1471. $res = implode($in_separator, GroupManager::get_user_group_name($in_userid));
  1472. if ($res == "") {
  1473. $res = "<div style='text-align:center'>-</div>";
  1474. }
  1475. return $res;
  1476. }