exercise.lib.php 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Exercise library
  5. * shows a question and its answers
  6. * @package dokeos.exercise
  7. * @author Olivier Brouckaert <oli.brouckaert@skynet.be>
  8. * @version $Id: exercise.lib.php 22247 2009-07-20 15:57:25Z ivantcholakov $
  9. */
  10. // The initialization class for the online editor is needed here.
  11. require_once dirname(__FILE__).'/../inc/lib/fckeditor/fckeditor.php';
  12. /**
  13. * Shows a question
  14. *
  15. * @param int question id
  16. * @param bool if true only show the questions, no exercise title
  17. * @param bool origin i.e = learnpath
  18. * @param int current item from the list of questions
  19. * @param int number of total questions
  20. * */
  21. function showQuestion($questionId, $only_questions = false, $origin = false, $current_item = '', $show_title = true, $freeze = false) {
  22. // Text direction for the current language
  23. $is_ltr_text_direction = api_get_text_direction() != 'rtl';
  24. // Change false to true in the following line to enable answer hinting.
  25. $debug_mark_answer = api_is_allowed_to_edit() && false;
  26. // Reads question information
  27. if (!$objQuestionTmp = Question::read($questionId)) {
  28. // Question not found
  29. return false;
  30. }
  31. $answerType = $objQuestionTmp->selectType();
  32. $pictureName = $objQuestionTmp->selectPicture();
  33. if ($answerType != HOT_SPOT) {
  34. // Question is not a hotspot
  35. if (!$only_questions) {
  36. $questionName = $objQuestionTmp->selectTitle();
  37. $questionDescription = $objQuestionTmp->selectDescription();
  38. $questionName = text_filter($questionName);
  39. if ($show_title) {
  40. $s='<div id="question_title" class="sectiontitle">'.get_lang('Question').' ';
  41. $s.=$current_item;
  42. //@todo I need the get the feedback type
  43. //if($answerType != 1)
  44. //$s.=' / '.$total_item;
  45. echo $s;
  46. echo ' : ';
  47. echo $questionName.'</div>';
  48. }
  49. $s='';
  50. $s.='<table class="exercise_questions" style="margin:4px 4px 4px 0px; padding:2px;">
  51. <tr><td valign="top" colspan="2">';
  52. $questionDescription=text_filter($questionDescription);
  53. $s.=$questionDescription;
  54. $s.='</td></tr></table>';
  55. if (!empty($pictureName)) {
  56. $s.="<tr>
  57. <td align='center' colspan='2'><img src='../document/download.php?doc_url=%2Fimages%2F'".$pictureName."' border='0'></td>
  58. </tr>";
  59. }
  60. $s.= '</table>';
  61. }
  62. $s .= '<div class="rounded exercise_questions" style="width: 720px; padding: 3px;">';
  63. $option_ie = '';
  64. /*
  65. if (!ereg("MSIE",$_SERVER["HTTP_USER_AGENT"])) {
  66. $s .= '<div class="rounded exercise_questions" style="width: 720px; padding: 3px;">';
  67. } else {
  68. $option_ie="margin-left:10px";
  69. }*/
  70. if ($answerType == FREE_ANSWER && $freeze) {
  71. return '';
  72. }
  73. $s .= '<table width="720" class="exercise_options" style="width: 720px;'.$option_ie.' background-color:#fff;">';
  74. // construction of the Answer object (also gets all answers details)
  75. $objAnswerTmp = new Answer($questionId);
  76. $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
  77. $quiz_question_options = Question::readQuestionOption($questionId);
  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. if ($answerType == MATCHING) {
  88. $x = 1; //iterate through answers
  89. $letter = 'A'; //mark letters for each answer
  90. $answer_matching = $cpt1 = array();
  91. $answer_suggestions = $nbrAnswers;
  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. foreach ($answer_matching as $id => $value) {
  106. $select_items[$i]['id'] = $value['id'];
  107. $select_items[$i]['letter'] = $cpt1[$id];
  108. $select_items[$i]['answer'] = $value['answer'];
  109. $i ++;
  110. }
  111. $num_suggestions = ($nbrAnswers - $x) + 1;
  112. } elseif ($answerType == FREE_ANSWER) {
  113. $oFCKeditor = new FCKeditor("choice[".$questionId."]") ;
  114. $oFCKeditor->ToolbarSet = 'TestFreeAnswer';
  115. $oFCKeditor->Width = '100%';
  116. $oFCKeditor->Height = '200';
  117. $oFCKeditor->Value = '' ;
  118. $s .= '<tr><td colspan="3">';
  119. $s .= $oFCKeditor->CreateHtml();
  120. $s .= '</td></tr>';
  121. }
  122. ?>
  123. <style>
  124. #questions {
  125. width:40%;
  126. height:50px;
  127. float:left;
  128. padding:5px;
  129. }
  130. #options {
  131. width:40%;
  132. float:left;
  133. padding:5px;
  134. }
  135. .question_item {
  136. height:50px;
  137. padding:5px;
  138. margin:10px 0px 10px 0px;
  139. }
  140. .option_item {
  141. width:150px;
  142. padding:3px;
  143. margin:10px;
  144. }
  145. </style>
  146. <script>
  147. $(function() {
  148. /*
  149. var $options = $( "#options" );
  150. $( "div", $options ).draggable({
  151. revert: "invalid", // when not dropped, the item will revert back to its initial position
  152. cursor: "move",
  153. });
  154. var $question_1 = $( "#question_1" );
  155. $question_1.droppable({
  156. accept: "#options div",
  157. activeClass: "ui-state-hover",
  158. hoverClass: "ui-state-active",
  159. drop: function( event, ui ) {
  160. //$( this ).addClass( "ui-state-highlight" );
  161. }
  162. });
  163. var $question_2 = $( "#question_2" );
  164. $question_2.droppable({
  165. accept: "#options div",
  166. hoverClass: "ui-state-active",
  167. drop: function( event, ui ) {
  168. //$( this ).addClass( "ui-state-highlight" );
  169. }
  170. });
  171. $options.droppable({
  172. accept: "#options div",
  173. hoverClass: "ui-state-active",
  174. drop: function( event, ui ) {
  175. }
  176. });*/
  177. });
  178. </script>
  179. <?php
  180. // Now navigate through the possible answers, using the max number of
  181. // answers for the question as a limiter
  182. $lines_count=1; // a counter for matching-type answers
  183. $question_list = array();
  184. if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE || $answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
  185. $header .= Display::tag('th', get_lang('Options'));
  186. foreach ($objQuestionTmp->options as $key=>$item) {
  187. $header .= Display::tag('th', $item);
  188. }
  189. $s.=Display::tag('tr',$header, array('style'=>'text-align:left;'));
  190. }
  191. for ($answerId=1;$answerId <= $nbrAnswers;$answerId++) {
  192. $answer = $objAnswerTmp->selectAnswer($answerId);
  193. $answerCorrect = $objAnswerTmp->isCorrect($answerId);
  194. $numAnswer = $objAnswerTmp->selectAutoId($answerId);
  195. if ($answerType == FILL_IN_BLANKS) {
  196. // splits text and weightings that are joined with the character '::'
  197. list($answer) = explode('::',$answer);
  198. // because [] is parsed here we follow this procedure:
  199. $answer = text_filter($answer);
  200. //getting the matches
  201. $answer = api_ereg_replace('\[[^]]+\]','<input type="text" name="choice['.$questionId.'][]" size="10" />',($answer));
  202. }
  203. // Unique answer
  204. if ($answerType == UNIQUE_ANSWER || $answerType == UNIQUE_ANSWER_NO_OPTION) {
  205. // set $debug_mark_answer to true at function start to
  206. // show the correct answer with a suffix '-x'
  207. $help = $selected = '';
  208. if ($debug_mark_answer) {
  209. if ($answerCorrect) {
  210. $help = 'x-';
  211. $selected = 'checked';
  212. }
  213. }
  214. $answer = text_filter($answer);
  215. $answer = Security::remove_XSS($answer, STUDENT);
  216. $s .= Display::input('hidden','choice2['.$questionId.']','0').
  217. '<tr><td colspan="3"><div class="u-m-answer"><p style="float: '.($is_ltr_text_direction ? 'left' : 'right').'; padding-'.($is_ltr_text_direction ? 'right' : 'left').': 4px;">'.
  218. '<span>'.Display::input('radio','choice['.$questionId.']', $numAnswer, array('class'=>'checkbox','selected'=>$selected)).'</span></p>'.
  219. '<div style="margin-'.($is_ltr_text_direction ? 'left' : 'right').': 24px;">'.
  220. $answer.
  221. '</div></div></td></tr>';
  222. } elseif ($answerType == MULTIPLE_ANSWER || $answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
  223. // multiple answers
  224. // set $debug_mark_answer to true at function start to
  225. // show the correct answer with a suffix '-x'
  226. $help = $selected = '';
  227. if ($debug_mark_answer) {
  228. if ($answerCorrect) {
  229. $help = 'x-';
  230. $selected = 'checked="checked"';
  231. }
  232. }
  233. $answer = text_filter($answer);
  234. $answer = Security::remove_XSS($answer, STUDENT);
  235. if ($answerType == MULTIPLE_ANSWER) {
  236. $s .= '<input type="hidden" name="choice2['.$questionId.']" value="0" />';
  237. $s .= '<tr><td colspan="3"><div class="u-m-answer"><p style="float: '.($is_ltr_text_direction ? 'left' : 'right').'; padding-'.($is_ltr_text_direction ? 'right' : 'left').': 4px;">';
  238. $options = array('type'=>'checkbox','name'=>'choice['.$questionId.']['.$numAnswer.']', 'class'=>'checkbox');
  239. if ($debug_mark_answer) {
  240. if ($answerCorrect) {
  241. $options['checked'] = 'checked';
  242. }
  243. }
  244. $s .= Display::tag('span', Display::tag('input','',$options ));
  245. $s .= '</p>';
  246. $s .= '<div style="margin-'.($is_ltr_text_direction ? 'left' : 'right').': 24px;">'.
  247. $answer.
  248. '</div></div></td></tr>';
  249. } elseif ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
  250. $options = array('type'=>'radio','name'=>'choice['.$questionId.']['.$numAnswer.']', 'class'=>'checkbox');
  251. $s .='<tr>';
  252. $s .= Display::tag('td', $answer);
  253. if (!empty($quiz_question_options)) {
  254. foreach ($quiz_question_options as $id=>$item) {
  255. $options['value'] = $id;
  256. $s .= Display::tag('td', Display::tag('input','',$options ));
  257. }
  258. }
  259. $s.='<tr>';
  260. }
  261. } elseif ($answerType == MULTIPLE_ANSWER_COMBINATION) {
  262. // multiple answers
  263. // set $debug_mark_answer to true at function start to
  264. // show the correct answer with a suffix '-x'
  265. $help = $selected = '';
  266. if ($debug_mark_answer) {
  267. if ($answerCorrect) {
  268. $help = 'x-';
  269. $selected = 'checked="checked"';
  270. }
  271. }
  272. $answer = text_filter($answer);
  273. $answer = Security::remove_XSS($answer, STUDENT);
  274. $s .= '<input type="hidden" name="choice2['.$questionId.']" value="0" />'.
  275. '<tr><td colspan="3"><div class="u-m-answer"><p style="float: '.($is_ltr_text_direction ? 'left' : 'right').'; padding-'.($is_ltr_text_direction ? 'right' : 'left').': 4px;">'.
  276. '<span><input class="checkbox" type="checkbox" name="choice['.$questionId.']['.$numAnswer.']" value="1" '.$selected.' /></span></p>'.
  277. '<div style="margin-'.($is_ltr_text_direction ? 'left' : 'right').': 24px;">'.
  278. $answer.
  279. '</div></div></td></tr>';
  280. } elseif ($answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
  281. // multiple answers
  282. // set $debug_mark_answer to true at function start to
  283. // show the correct answer with a suffix '-x'
  284. $help = $selected = '';
  285. if ($debug_mark_answer) {
  286. if ($answerCorrect) {
  287. $help = 'x-';
  288. $selected = 'checked="checked"';
  289. }
  290. }
  291. $answer = text_filter($answer);
  292. $answer = Security::remove_XSS($answer, STUDENT);
  293. $options = array('type'=>'radio','name'=>'choice['.$questionId.']['.$numAnswer.']', 'class'=>'checkbox');
  294. $s .='<tr>';
  295. $s .= Display::tag('td', $answer);
  296. foreach ($objQuestionTmp->options as $key=>$item) {
  297. $options['value'] = $key;
  298. $s .= Display::tag('td', Display::tag('input','',$options ));
  299. }
  300. $s.='<tr>';
  301. } elseif ($answerType == FILL_IN_BLANKS) {
  302. // fill in blanks
  303. $s .= '<tr><td colspan="3">'.$answer.'</td></tr>';
  304. } else {
  305. // matching type, showing suggestions and answers
  306. // TODO: replace $answerId by $numAnswer
  307. if ($answerCorrect != 0) {
  308. // only show elements to be answered (not the contents of
  309. // the select boxes, who are corrrect = 0)
  310. $s .= '<tr><td width="45%" valign="top" >';
  311. $parsed_answer = text_filter($answer);
  312. $question_list[] = $parsed_answer;
  313. //left part questions
  314. $s .= ' <span style="float:left; width:8%;"><b>'.$lines_count.'</b>.&nbsp;</span>
  315. <span style="float:left; width:92%;">'.$parsed_answer.'</span></td>';
  316. //middle part (matches selects)
  317. $s .= '<td width="10%" valign="top" align="center">&nbsp;&nbsp;
  318. <select name="choice['.$questionId.']['.$numAnswer.']">
  319. <option value="0">--</option>';
  320. // fills the list-box
  321. foreach ($select_items as $key=>$val) {
  322. // set $debug_mark_answer to true at function start to
  323. // show the correct answer with a suffix '-x'
  324. $help = $selected = '';
  325. if ($debug_mark_answer) {
  326. if ($val['id'] == $answerCorrect) {
  327. $help = '-x';
  328. $selected = 'selected="selected"';
  329. }
  330. }
  331. $s.='<option value="'.$val['id'].'" '.$selected.'>'.$val['letter'].$help.'</option>';
  332. } // end foreach()
  333. $s .= '</select>&nbsp;&nbsp;</td>';
  334. //print_r($select_items);
  335. //right part (answers)
  336. $s.='<td width="45%" valign="top" >';
  337. if (isset($select_items[$lines_count])) {
  338. $s.='<span style="float:left; width:5%;"><b>'.$select_items[$lines_count]['letter'].'.</b></span>'.
  339. '<span style="float:left; width:95%;">'.$select_items[$lines_count]['answer'].'</span>';
  340. } else {
  341. $s.='&nbsp;';
  342. }
  343. $s .= '</td>';
  344. $s .= '</tr>';
  345. $lines_count++;
  346. //if the left side of the "matching" has been completely
  347. // shown but the right side still has values to show...
  348. if (($lines_count -1) == $num_suggestions) {
  349. // if it remains answers to shown at the right side
  350. while (isset($select_items[$lines_count])) {
  351. $s .= '<tr>
  352. <td colspan="2">&nbsp;</td>
  353. <td valign="top">';
  354. $s.='<b>'.$select_items[$lines_count]['letter'].'.</b> '.$select_items[$lines_count]['answer'];
  355. $s.="</td>
  356. </tr>";
  357. $lines_count++;
  358. } // end while()
  359. } // end if()
  360. }
  361. }
  362. } // end for()
  363. /*
  364. //Adding divs for the new MATCHING interface
  365. if ($answerType == MATCHING && !$freeze) {
  366. echo '<div id="questions">';
  367. echo Display::tag('h2','Questions');
  368. $i = 1;
  369. foreach ($question_list as $key=>$val) {
  370. echo Display::tag('div', Display::tag('p',$val), array('id'=>'question_'.$i, 'class'=>'question_item ui-widget-header'));
  371. $i++;
  372. }
  373. echo '</div>';
  374. echo Display::tag('h2','Options');
  375. echo '<div id="options" class=" ui-widget-header">';
  376. foreach ($select_items as $key=>$val) {
  377. echo Display::tag('div', Display::tag('p',$val['answer']), array('id'=>'option_'.$i, 'class'=>'option_item ui-widget-content'));
  378. }
  379. echo '</ul>';
  380. }*/
  381. $s .= '</table>';
  382. $s .= '</div><br />';
  383. // destruction of the Answer object
  384. unset($objAnswerTmp);
  385. // destruction of the Question object
  386. unset($objQuestionTmp);
  387. if ($origin != 'export') {
  388. echo $s;
  389. } else {
  390. return($s);
  391. }
  392. } elseif ($answerType == HOT_SPOT) {
  393. // Question is a HOT_SPOT
  394. //checking document/images visibility
  395. if (api_is_platform_admin() || api_is_course_admin()) {
  396. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  397. $course = api_get_course_info();
  398. $doc_id = DocumentManager::get_document_id($course, '/images/'.$pictureName);
  399. if (is_numeric($doc_id)) {
  400. $images_folder_visibility = api_get_item_visibility($course,'document', $doc_id, api_get_session_id());
  401. if (!$images_folder_visibility) {
  402. //This message is shown only to the course/platform admin if the image is set to visibility = false
  403. Display::display_warning_message(get_lang('ChangeTheVisibilityOfTheCurrentImage'));
  404. }
  405. }
  406. }
  407. $questionName = $objQuestionTmp->selectTitle();
  408. $questionDescription = $objQuestionTmp->selectDescription();
  409. if ($freeze) {
  410. echo Display::img($objQuestionTmp->selectPicturePath());
  411. return;
  412. }
  413. // Get the answers, make a list
  414. $objAnswerTmp = new Answer($questionId);
  415. $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
  416. // get answers of hotpost
  417. $answers_hotspot = array();
  418. for ($answerId=1;$answerId <= $nbrAnswers;$answerId++) {
  419. $answers = $objAnswerTmp->selectAnswerByAutoId($objAnswerTmp->selectAutoId($answerId));
  420. $answers_hotspot[$answers['id']] = $objAnswerTmp->selectAnswer($answerId);
  421. }
  422. // display answers of hotpost order by id
  423. $answer_list = '<div style="padding: 10px; margin-left: 0px; border: 1px solid #A4A4A4; height: 408px; width: 200px;"><b>'.get_lang('HotspotZones').'</b><dl>';
  424. if (!empty($answers_hotspot)) {
  425. ksort($answers_hotspot);
  426. foreach ($answers_hotspot as $key => $value) {
  427. $answer_list .= '<dt>'.$key.'.- '.$value.'</dt><br />';
  428. }
  429. }
  430. $answer_list .= '</dl></div>';
  431. if (!$only_questions) {
  432. if ($show_title) {
  433. echo '<div id="question_title" class="sectiontitle">'.get_lang('Question').' '.$current_item.' : '.$questionName.'</div>';
  434. }
  435. //@todo I need to the get the feedback type
  436. //if($answerType == 2)
  437. // $s.=' / '.$total_item;
  438. echo '<input type="hidden" name="hidden_hotspot_id" value="'.$questionId.'" />';
  439. echo '<table class="exercise_questions" >
  440. <tr>
  441. <td valign="top" colspan="2">';
  442. echo $questionDescription=text_filter($questionDescription);
  443. echo '</td></tr>';
  444. }
  445. $canClick = isset($_GET['editQuestion']) ? '0' : (isset($_GET['modifyAnswers']) ? '0' : '1');
  446. $s .= '<script language="JavaScript" type="text/javascript" src="../plugin/hotspot/JavaScriptFlashGateway.js"></script>
  447. <script src="../plugin/hotspot/hotspot.js" type="text/javascript" language="JavaScript"></script>
  448. <script language="JavaScript" type="text/javascript">
  449. <!--
  450. // Globals
  451. // Major version of Flash required
  452. var requiredMajorVersion = 7;
  453. // Minor version of Flash required
  454. var requiredMinorVersion = 0;
  455. // Minor version of Flash required
  456. var requiredRevision = 0;
  457. // the version of javascript supported
  458. var jsVersion = 1.0;
  459. // -->
  460. </script>
  461. <script language="VBScript" type="text/vbscript">
  462. <!-- // Visual basic helper required to detect Flash Player ActiveX control version information
  463. Function VBGetSwfVer(i)
  464. on error resume next
  465. Dim swControl, swVersion
  466. swVersion = 0
  467. set swControl = CreateObject("ShockwaveFlash.ShockwaveFlash." + CStr(i))
  468. if (IsObject(swControl)) then
  469. swVersion = swControl.GetVariable("$version")
  470. end if
  471. VBGetSwfVer = swVersion
  472. End Function
  473. // -->
  474. </script>
  475. <script language="JavaScript1.1" type="text/javascript">
  476. <!-- // Detect Client Browser type
  477. var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
  478. var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
  479. var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
  480. jsVersion = 1.1;
  481. // JavaScript helper required to detect Flash Player PlugIn version information
  482. function JSGetSwfVer(i) {
  483. // NS/Opera version >= 3 check for Flash plugin in plugin array
  484. if (navigator.plugins != null && navigator.plugins.length > 0) {
  485. if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
  486. var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
  487. var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
  488. descArray = flashDescription.split(" ");
  489. tempArrayMajor = descArray[2].split(".");
  490. versionMajor = tempArrayMajor[0];
  491. versionMinor = tempArrayMajor[1];
  492. if ( descArray[3] != "" ) {
  493. tempArrayMinor = descArray[3].split("r");
  494. } else {
  495. tempArrayMinor = descArray[4].split("r");
  496. }
  497. versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
  498. flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
  499. } else {
  500. flashVer = -1;
  501. }
  502. }
  503. // MSN/WebTV 2.6 supports Flash 4
  504. else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
  505. // WebTV 2.5 supports Flash 3
  506. else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
  507. // older WebTV supports Flash 2
  508. else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
  509. // Can\'t detect in all other cases
  510. else
  511. {
  512. flashVer = -1;
  513. }
  514. return flashVer;
  515. }
  516. // When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
  517. function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision) {
  518. reqVer = parseFloat(reqMajorVer + "." + reqRevision);
  519. // loop backwards through the versions until we find the newest version
  520. for (i=25;i>0;i--) {
  521. if (isIE && isWin && !isOpera) {
  522. versionStr = VBGetSwfVer(i);
  523. } else {
  524. versionStr = JSGetSwfVer(i);
  525. }
  526. if (versionStr == -1 ) {
  527. return false;
  528. } else if (versionStr != 0) {
  529. if(isIE && isWin && !isOpera) {
  530. tempArray = versionStr.split(" ");
  531. tempString = tempArray[1];
  532. versionArray = tempString .split(",");
  533. } else {
  534. versionArray = versionStr.split(".");
  535. }
  536. versionMajor = versionArray[0];
  537. versionMinor = versionArray[1];
  538. versionRevision = versionArray[2];
  539. versionString = versionMajor + "." + versionRevision; // 7.0r24 == 7.24
  540. versionNum = parseFloat(versionString);
  541. // is the major.revision >= requested major.revision AND the minor version >= requested minor
  542. if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
  543. return true;
  544. } else {
  545. return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
  546. }
  547. }
  548. }
  549. }
  550. // -->
  551. </script>';
  552. $s .= '<tr><td valign="top" colspan="2" width="520"><table><tr><td width="520">
  553. <script language="JavaScript" type="text/javascript">
  554. <!--
  555. // Version check based upon the values entered above in "Globals"
  556. var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
  557. // Check to see if the version meets the requirements for playback
  558. if (hasReqestedVersion) { // if we\'ve detected an acceptable version
  559. var oeTags = \'<object type="application/x-shockwave-flash" data="../plugin/hotspot/hotspot_user.swf?modifyAnswers='.$questionId.'&amp;canClick:'.$canClick.'" width="560" height="436">\'
  560. + \'<param name="movie" value="../plugin/hotspot/hotspot_user.swf?modifyAnswers='.$questionId.'&amp;canClick:'.$canClick.'" />\'
  561. + \'<\/object>\';
  562. document.write(oeTags); // embed the Flash Content SWF when all tests are passed
  563. } else { // flash is too old or we can\'t detect the plugin
  564. var alternateContent = "Error<br \/>"
  565. + "Hotspots requires Macromedia Flash 7.<br \/>"
  566. + "<a href=\"http://www.macromedia.com/go/getflash/\">Get Flash<\/a>";
  567. document.write(alternateContent); // insert non-flash content
  568. }
  569. // -->
  570. </script>
  571. </td>
  572. <td valign="top" align="left">'.$answer_list.'</td></tr>
  573. </table>
  574. </td></tr>';
  575. echo $s;
  576. }
  577. echo '</table><br />';
  578. return $nbrAnswers;
  579. }
  580. function get_exercise_track_exercise_info($exe_id) {
  581. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  582. $TBL_TRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  583. $exe_id = intval($exe_id);
  584. $result = array();
  585. if (!empty($exe_id)) {
  586. $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;
  587. $res_fb_type = Database::query($sql_fb_type);
  588. $result = Database::fetch_array($res_fb_type, 'ASSOC');
  589. }
  590. return $result;
  591. }
  592. /**
  593. * Validates the time control key
  594. */
  595. function exercise_time_control_is_valid($exercise_id) {
  596. //Fast check
  597. $exercise_id = intval($exercise_id);
  598. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  599. $sql = "SELECT expired_time FROM $TBL_EXERCICES WHERE id = $exercise_id";
  600. $result = Database::query($sql);
  601. $row = Database::fetch_array($result, 'ASSOC');
  602. if (!empty($row['expired_time']) ) {
  603. $current_expired_time_key = get_time_control_key($exercise_id);
  604. if (isset($_SESSION['expired_time'][$current_expired_time_key])) {
  605. $current_time = time();
  606. $expired_time = api_strtotime($_SESSION['expired_time'][$current_expired_time_key], 'UTC');
  607. $total_time_allowed = $expired_time + 30;
  608. //error_log('expired time converted + 30: '.$total_time_allowed);
  609. //error_log('$current_time: '.$current_time);
  610. if ($total_time_allowed < $current_time) {
  611. return false;
  612. }
  613. return true;
  614. } else {
  615. return false;
  616. }
  617. } else {
  618. return true;
  619. }
  620. }
  621. /**
  622. Deletes the time control token
  623. */
  624. function exercise_time_control_delete($exercise_id) {
  625. $current_expired_time_key = get_time_control_key($exercise_id);
  626. unset($_SESSION['expired_time'][$current_expired_time_key]);
  627. }
  628. /**
  629. Generates the time control key
  630. */
  631. function generate_time_control_key($exercise_id) {
  632. $exercise_id = intval($exercise_id);
  633. return api_get_course_int_id().'_'.api_get_session_id().'_'.$exercise_id.'_'.api_get_user_id();
  634. }
  635. /**
  636. Returns the time controller key
  637. @todo this function is the same as generate_time_control_key
  638. */
  639. function get_time_control_key($exercise_id) {
  640. $exercise_id = intval($exercise_id);
  641. return api_get_course_int_id().'_'.api_get_session_id().'_'.$exercise_id.'_'.api_get_user_id();
  642. }
  643. /**
  644. * @todo use this function instead of get_time_control_key
  645. */
  646. function get_session_time_control_key($exercise_id) {
  647. $time_control_key = get_time_control_key($exercise_id);
  648. $return_value = $_SESSION['expired_time'][$time_control_key];
  649. return $return_value;
  650. }
  651. /**
  652. * Gets count of exam results
  653. * @todo this function should be moved in a library + no global calls
  654. */
  655. function get_count_exam_results($exercise_id = null) {
  656. //@todo replace all this globals
  657. global $is_allowedToEdit, $is_tutor,$TBL_USER, $TBL_EXERCICES,$TBL_TRACK_EXERCICES, $TBL_TRACK_HOTPOTATOES, $TBL_TRACK_ATTEMPT_RECORDING,$filter_by_not_revised,$filter_by_revised,$documentPath;
  658. $session_id_and = ' AND te.session_id = ' . api_get_session_id() . ' ';
  659. $exercise_id = intval($_GET['exerciseId']);
  660. $exercise_where = '';
  661. if (!empty($exercise_id)) {
  662. $exercise_where = ' AND te.exe_exo_id = '.$exercise_id.' ';
  663. }
  664. $hotpotatoe_where = '';
  665. if (!empty($_GET['path'])) {
  666. $hotpotatoe_path = Database::escape_string($_GET['path']);
  667. $hotpotatoe_where .= ' AND exe_name = "'.$hotpotatoe_path.'" ';
  668. }
  669. $user_id_hotpotato_and = ' AND exe_user_id = ' . api_get_user_id() . ' ';
  670. if ($is_allowedToEdit || $is_tutor) {
  671. $user_id_and = '';
  672. if (!empty ($_POST['filter_by_user'])) {
  673. if ($_POST['filter_by_user'] == 'all') {
  674. $user_id_and = " AND user_id like '%'";
  675. } else {
  676. $user_id_and = " AND user_id = '" . Database :: escape_string((int) $_POST['filter_by_user']) . "' ";
  677. }
  678. }
  679. if ($_GET['gradebook'] == 'view') {
  680. $exercise_where_query = 'te.exe_exo_id =ce.id AND ';
  681. }
  682. //@todo fix to work with COURSE_RELATION_TYPE_RRHH in both queries
  683. /*$sql="SELECT ".(api_is_western_name_order() ? "firstname as userpart1, lastname col1" : "lastname as userpart1, firstname as col1").", ce.title as extitle, te.exe_result as exresult ,
  684. te.exe_weighting as exweight, te.exe_date as exdate, te.exe_id as exid, email as exemail, te.start_date as exstart, steps_counter as exstep,cuser.user_id as excruid,te.exe_duration as exduration
  685. FROM $TBL_EXERCICES AS ce , $TBL_TRACK_EXERCICES AS te, $TBL_USER AS user,$tbl_course_rel_user AS cuser
  686. WHERE user.user_id=cuser.user_id AND cuser.relation_type<>".COURSE_RELATION_TYPE_RRHH." AND te.exe_exo_id = ce.id AND te.status != 'incomplete' AND cuser.user_id=te.exe_user_id AND te.exe_cours_id='" . Database :: escape_string($_cid) . "'
  687. $user_id_and $session_id_and AND ce.active <>-1 AND orig_lp_id = 0 AND orig_lp_item_id = 0
  688. AND cuser.course_code=te.exe_cours_id ORDER BY col1, te.exe_cours_id ASC, ce.title ASC, te.exe_date DESC";*/
  689. $sql="SELECT DISTINCT te.exe_id
  690. FROM $TBL_EXERCICES AS ce INNER JOIN $TBL_TRACK_EXERCICES AS te ON (te.exe_exo_id = ce.id) INNER JOIN $TBL_USER AS user ON (user.user_id = exe_user_id)
  691. WHERE te.status != 'incomplete' AND te.exe_cours_id='" . api_get_course_id() . "' $user_id_and $session_id_and AND ce.active <>-1 AND orig_lp_id = 0 AND orig_lp_item_id = 0 $exercise_where ";
  692. //Seems that the $TBL_TRACK_HOTPOTATOES does not have an exercise id that's weird ... we are removing $exercise_where
  693. $hpsql="SELECT count(*) as count
  694. FROM $TBL_TRACK_HOTPOTATOES tth, $TBL_USER tu
  695. WHERE tu.user_id=tth.exe_user_id AND tth.exe_cours_id = '" . api_get_course_id() . "' $user_id_hotpotato_and $hotpotatoe_where
  696. ORDER BY tth.exe_cours_id ASC, tth.exe_date DESC";
  697. } else {
  698. // get only this user's results
  699. $user_id_and = ' AND te.exe_user_id = ' . api_get_user_id() . ' ';
  700. /*$sql="SELECT ".(api_is_western_name_order() ? "firstname as col0, lastname col1" : "lastname as col0, firstname as col1").", ce.title as extitle, te.exe_result as exresult, " .
  701. "te.exe_weighting as exweight, te.exe_date as exdate, te.exe_id as exid, email as exemail, " .
  702. "te.start_date as exstart, steps_counter as exstep, cuser.user_id as excruid, te.exe_duration as exduration, ce.results_disabled as exdisabled
  703. FROM $TBL_EXERCICES AS ce , $TBL_TRACK_EXERCICES AS te, $TBL_USER AS user,$tbl_course_rel_user AS cuser
  704. WHERE user.user_id=cuser.user_id AND te.exe_exo_id = ce.id AND te.status != 'incomplete' AND cuser.user_id=te.exe_user_id
  705. AND te.exe_cours_id='" . Database :: escape_string($_cid) . "'
  706. AND cuser.relation_type<>".COURSE_RELATION_TYPE_RRHH." $user_id_and $session_id_and AND ce.active <>-1 AND" .
  707. " orig_lp_id = 0 AND orig_lp_item_id = 0 AND cuser.course_code=te.exe_cours_id ORDER BY col1, te.exe_cours_id ASC, ce.title ASC, te.exe_date DESC";*/
  708. $sql="SELECT count(*) as count
  709. FROM $TBL_EXERCICES AS ce INNER JOIN $TBL_TRACK_EXERCICES AS te ON (te.exe_exo_id = ce.id) INNER JOIN $TBL_USER AS user ON (user.user_id = exe_user_id)
  710. WHERE te.status != 'incomplete' AND te.exe_cours_id='" . api_get_course_id() . "' $user_id_and $session_id_and AND ce.active <>-1 AND orig_lp_id = 0 AND orig_lp_item_id = 0 $exercise_where";
  711. $hpsql = "SELECT count(*) as count
  712. FROM $TBL_TRACK_HOTPOTATOES
  713. WHERE exe_user_id = '" . api_get_user_id() . "' AND exe_cours_id = '" . api_get_course_id() . "' $user_id_hotpotato_and $hotpotatoe_where
  714. ORDER BY exe_cours_id ASC, exe_date DESC";
  715. }
  716. $count = 0;
  717. //Chamilo results
  718. if (empty($hotpotatoe_where)) {
  719. $resx = Database::query($sql);
  720. if (Database::num_rows($resx) > 0) {
  721. if ($is_allowedToEdit || $is_tutor) {
  722. while ($rowx = Database::fetch_array($resx,'ASSOC')) {
  723. $results[] = $rowx;
  724. }
  725. //Special modification to show corretly the pagination
  726. if (is_array($results)) {
  727. if ($_GET['gradebook'] == 'view') {
  728. $filter_by_no_revised = true;
  729. $from_gradebook = true;
  730. }
  731. $sizeof = count($results);
  732. $user_list_id = array ();
  733. $user_last_name = '';
  734. $user_first_name = '';
  735. $quiz_name_list = '';
  736. $duration_list = '';
  737. $date_list = '';
  738. $result_list = '';
  739. $more_details_list = '';
  740. for ($i = 0; $i < $sizeof; $i++) {
  741. $revised = false;
  742. $sql_exe = 'SELECT exe_id FROM ' . $TBL_TRACK_ATTEMPT_RECORDING . ' WHERE author != "" AND exe_id = ' . Database :: escape_string($results[$i]['exe_id']) .' LIMIT 1';
  743. $query = Database::query($sql_exe);
  744. if (Database :: num_rows($query) > 0) {
  745. $revised = true;
  746. }
  747. if ($filter_by_not_revised && $revised) {
  748. continue;
  749. }
  750. if ($filter_by_revised && !$revised) {
  751. continue;
  752. }
  753. $count++;
  754. }
  755. }
  756. } else {
  757. $rowx = Database::fetch_array($resx,'ASSOC');
  758. $count = $rowx['count'];
  759. }
  760. }
  761. } else {
  762. //Hotpotatoes results
  763. $hpres = Database::query($hpsql);
  764. if (Database::num_rows($hpres) > 0) {
  765. $rowx = Database::fetch_array($hpres,'ASSOC');
  766. $count += $rowx['count'];
  767. }
  768. }
  769. return $count;
  770. }
  771. /**
  772. * Gets the exam'data results
  773. * @todo this function should be moved in a library + no global calls
  774. */
  775. function get_exam_results_data($from, $number_of_items, $column, $direction) {
  776. //@todo replace all this globals
  777. global $is_allowedToEdit, $is_tutor,$TBL_USER, $TBL_EXERCICES,$TBL_TRACK_HOTPOTATOES, $TBL_TRACK_EXERCICES, $TBL_TRACK_ATTEMPT_RECORDING,$filter_by_not_revised,$filter_by_revised,$documentPath,$filter;
  778. $session_id_and = ' AND te.session_id = ' . api_get_session_id() . ' ';
  779. $exercise_id = intval($_GET['exerciseId']);
  780. $exercise_where = '';
  781. if (!empty($exercise_id)) {
  782. $exercise_where .= ' AND te.exe_exo_id = '.$exercise_id.' ';
  783. }
  784. $hotpotatoe_where = '';
  785. if (!empty($_GET['path'])) {
  786. $hotpotatoe_path = Database::escape_string($_GET['path']);
  787. $hotpotatoe_where .= ' AND exe_name = "'.$hotpotatoe_path.'" ';
  788. }
  789. if ($is_allowedToEdit || $is_tutor) {
  790. $user_id_and = '';
  791. if (!empty ($_POST['filter_by_user'])) {
  792. if ($_POST['filter_by_user'] == 'all') {
  793. $user_id_and = " AND user_id like '%'";
  794. } else {
  795. $user_id_and = " AND user_id = '" . intval($_POST['filter_by_user']) . "' ";
  796. }
  797. }
  798. if ($_GET['gradebook'] == 'view') {
  799. $exercise_where_query = ' te.exe_exo_id =ce.id AND ';
  800. }
  801. //@todo fix to work with COURSE_RELATION_TYPE_RRHH in both queries
  802. /*$sql="SELECT ".(api_is_western_name_order() ? "firstname as col0, lastname col1" : "lastname as col0, firstname as col1").", ce.title as extitle, te.exe_result as exresult ,
  803. te.exe_weighting as exweight, te.exe_date as exdate, te.exe_id as exid, email as exemail, te.start_date as exstart, steps_counter as exstep,cuser.user_id as excruid,te.exe_duration as exduration
  804. FROM $TBL_EXERCICES AS ce , $TBL_TRACK_EXERCICES AS te, $TBL_USER AS user,$tbl_course_rel_user AS cuser
  805. WHERE user.user_id=cuser.user_id AND cuser.relation_type<>".COURSE_RELATION_TYPE_RRHH." AND te.exe_exo_id = ce.id AND te.status != 'incomplete' AND cuser.user_id=te.exe_user_id AND te.exe_cours_id='" . Database :: escape_string(api_get_course_id()) . "'
  806. $user_id_and $session_id_and AND ce.active <>-1 AND orig_lp_id = 0 AND orig_lp_item_id = 0
  807. AND cuser.course_code=te.exe_cours_id ORDER BY col1, te.exe_cours_id ASC, ce.title ASC, te.exe_date DESC";*/
  808. $sql="SELECT ".(api_is_western_name_order() ? "firstname as col0, lastname col1" : "lastname as col0, firstname as col1").", ce.title as col2, te.exe_result as exresult , te.exe_weighting as exweight,
  809. te.exe_date as exdate, te.exe_id as exid, email as exemail, te.start_date as col4, steps_counter as exstep, exe_user_id as excruid,te.exe_duration as exduration, propagate_neg
  810. FROM $TBL_EXERCICES AS ce INNER JOIN $TBL_TRACK_EXERCICES AS te ON (te.exe_exo_id = ce.id) INNER JOIN $TBL_USER AS user ON (user.user_id = exe_user_id)
  811. WHERE te.status != 'incomplete' AND te.exe_cours_id='" . api_get_course_id() . "' $user_id_and $session_id_and AND ce.active <>-1 AND orig_lp_id = 0 AND orig_lp_item_id = 0 $exercise_where ";
  812. $hpsql="SELECT ".(api_is_western_name_order() ? "firstname as col0, lastname col1" : "lastname as col0, firstname as col1").", tth.exe_name, tth.exe_result , tth.exe_weighting, tth.exe_date
  813. FROM $TBL_TRACK_HOTPOTATOES tth, $TBL_USER tu
  814. WHERE tu.user_id=tth.exe_user_id AND tth.exe_cours_id = '" . api_get_course_id()."' $hotpotatoe_where
  815. ORDER BY tth.exe_cours_id ASC, tth.exe_date DESC";
  816. } else {
  817. // get only this user's results
  818. $user_id_and = ' AND te.exe_user_id = ' . api_get_user_id() . ' ';
  819. /*$sql="SELECT ".(api_is_western_name_order() ? "firstname as col0, lastname col1" : "lastname as col0, firstname as col1").", ce.title as extitle, te.exe_result as exresult, " .
  820. "te.exe_weighting as exweight, te.exe_date as exdate, te.exe_id as exid, email as exemail, " .
  821. "te.start_date as exstart, steps_counter as exstep, cuser.user_id as excruid, te.exe_duration as exduration, ce.results_disabled as exdisabled
  822. FROM $TBL_EXERCICES AS ce , $TBL_TRACK_EXERCICES AS te, $TBL_USER AS user,$tbl_course_rel_user AS cuser
  823. WHERE user.user_id=cuser.user_id AND te.exe_exo_id = ce.id AND te.status != 'incomplete' AND cuser.user_id=te.exe_user_id
  824. AND te.exe_cours_id='" . Database :: escape_string($_cid) . "'
  825. AND cuser.relation_type<>".COURSE_RELATION_TYPE_RRHH." $user_id_and $session_id_and AND ce.active <>-1 AND" .
  826. " orig_lp_id = 0 AND orig_lp_item_id = 0 AND cuser.course_code=te.exe_cours_id ORDER BY col1, te.exe_cours_id ASC, ce.title ASC, te.exe_date DESC";*/
  827. $sql="SELECT ce.title as col0, ce.title as col1, te.exe_duration as exduration, firstname, lastname, te.exe_result as exresult, te.exe_weighting as exweight, te.exe_date as exdate, te.exe_id as exid, email as exemail, " .
  828. "te.start_date as col2, steps_counter as exstep, exe_user_id as excruid, ce.results_disabled as exdisabled, propagate_neg
  829. FROM $TBL_EXERCICES AS ce INNER JOIN $TBL_TRACK_EXERCICES AS te ON (te.exe_exo_id = ce.id) INNER JOIN $TBL_USER AS user ON (user.user_id = exe_user_id)
  830. WHERE te.status != 'incomplete' AND te.exe_cours_id='" . api_get_course_id() . "' $user_id_and $session_id_and AND ce.active <>-1 AND orig_lp_id = 0 AND orig_lp_item_id = 0 $exercise_where";
  831. $hpsql = "SELECT '', '', exe_name, exe_result , exe_weighting, exe_date
  832. FROM $TBL_TRACK_HOTPOTATOES
  833. WHERE exe_user_id = '" . api_get_user_id() . "' AND exe_cours_id = '" . api_get_course_id() . "' $hotpotatoe_where
  834. ORDER BY exe_cours_id ASC, exe_date DESC";
  835. }
  836. if (empty($hotpotatoe_where)) {
  837. $column = intval($column);
  838. $from = intval($from);
  839. $number_of_items = intval($number_of_items);
  840. $sql .= " ORDER BY col$column $direction ";
  841. $sql .= " LIMIT $from, $number_of_items";
  842. $results = array();
  843. $resx = Database::query($sql);
  844. while ($rowx = Database::fetch_array($resx,'ASSOC')) {
  845. $results[] = $rowx;
  846. }
  847. $list_info = array();
  848. // Print test results.
  849. $lang_nostartdate = get_lang('NoStartDate') . ' / ';
  850. if (is_array($results)) {
  851. $users_array_id = array ();
  852. if ($_GET['gradebook'] == 'view') {
  853. $filter_by_no_revised = true;
  854. $from_gradebook = true;
  855. }
  856. $sizeof = count($results);
  857. $user_list_id = array ();
  858. $user_last_name = '';
  859. $user_first_name = '';
  860. $quiz_name_list = '';
  861. $duration_list = '';
  862. $date_list = '';
  863. $result_list = '';
  864. $more_details_list = '';
  865. for ($i = 0; $i < $sizeof; $i++) {
  866. $revised = false;
  867. $sql_exe = 'SELECT exe_id FROM ' . $TBL_TRACK_ATTEMPT_RECORDING . ' WHERE author != "" AND exe_id = ' . Database :: escape_string($results[$i]['exid']) .' LIMIT 1';
  868. $query = Database::query($sql_exe);
  869. if (Database :: num_rows($query) > 0) {
  870. $revised = true;
  871. }
  872. if ($filter_by_not_revised && $revised) {
  873. continue;
  874. }
  875. if ($filter_by_revised && !$revised) {
  876. continue;
  877. }
  878. if ($from_gradebook && ($is_allowedToEdit || $is_tutor)) {
  879. if (in_array($results[$i]['col2'] . $results[$i]['col0'] . $results[$i]['col1'], $users_array_id)) {
  880. continue;
  881. }
  882. $users_array_id[] = $results[$i]['col2'] . $results[$i]['col0'] . $results[$i]['col1'];
  883. }
  884. if ($is_allowedToEdit || $is_tutor) {
  885. $user_first_name = $results[$i]['col0'];
  886. $user_last_name = $results[$i]['col1'];
  887. $user = $results[$i]['col0'] . $results[$i]['col1'];
  888. $test = $results[$i]['col2'];
  889. } else {
  890. $user_first_name = $results[$i]['firstname'];
  891. $user_last_name = $results[$i]['lastname'];
  892. $user = $results[$i]['firstname'] . $results[$i]['lastname'];
  893. $test = $results[$i]['col0'];
  894. }
  895. $user_list_id[] = $results[$i]['excruid'];
  896. $id = $results[$i]['exid'];
  897. $quiz_name_list = $test;
  898. $dt = api_convert_and_format_date($results[$i]['exweight'], null, date_default_timezone_get());
  899. $res = $results[$i]['exresult'];
  900. $duration = intval($results[$i]['exduration']);
  901. // we filter the results if we have the permission to
  902. if (isset ($results[$i]['exdisabled']))
  903. $result_disabled = intval($results[$i]['exdisabled']);
  904. else
  905. $result_disabled = 0;
  906. if ($result_disabled == 0) {
  907. $add_start_date = $lang_nostartdate;
  908. if ($is_allowedToEdit || $is_tutor) {
  909. $user = $results[$i]['col0'] . $results[$i]['col1'];
  910. $date_value = $results[$i]['col4'];
  911. } else {
  912. $date_value = $results[$i]['col2'];
  913. }
  914. if ($date_value != "0000-00-00 00:00:00") {
  915. //echo ceil((($results[$i][4] - $results[$i][7]) / 60)) . ' ' . get_lang('MinMinutes');
  916. $exe_date_timestamp = api_strtotime($results[$i]['exdate'], date_default_timezone_get());
  917. $start_date_timestamp = api_strtotime($date_value, date_default_timezone_get());
  918. $my_duration = ceil((($exe_date_timestamp - $start_date_timestamp) / 60));
  919. if ($my_duration == 1 ) {
  920. $duration_list = $my_duration . ' ' . get_lang('MinMinute');
  921. } else {
  922. $duration_list = $my_duration. ' ' . get_lang('MinMinutes');
  923. }
  924. if ($results[$i]['exstep'] > 1) {
  925. //echo ' ( ' . $results[$i][8] . ' ' . get_lang('Steps') . ' )';
  926. $duration_list = ' ( ' . $results[$i]['exstep'] . ' ' . get_lang('Steps') . ' )';
  927. }
  928. $add_start_date = api_convert_and_format_date($date_value, null, date_default_timezone_get()) . ' / ';
  929. } else {
  930. $duration_list = get_lang('NoLogOfDuration');
  931. //echo get_lang('NoLogOfDuration');
  932. }
  933. // Date conversion
  934. $date_list = api_get_local_time($results[$i]['col4']). ' / ' . api_get_local_time($results[$i]['exdate']);
  935. // there are already a duration test period calculated??
  936. //echo '<td>'.sprintf(get_lang('DurationFormat'), $duration).'</td>';
  937. // if the float look like 10.00 we show only 10
  938. $my_res = $results[$i]['exresult'];
  939. $my_total = $results[$i]['exweight'];
  940. if (!$results[$i]['propagate_neg'] && $my_res < 0) {
  941. $my_res = 0;
  942. }
  943. $ex = show_score($my_res, $my_total);
  944. $result_list = $ex;
  945. $html_link = '';
  946. if ($is_allowedToEdit || $is_tutor) {
  947. if ($revised) {
  948. $html_link.= "<a href='exercise_show.php?".api_get_cidreq()."&action=edit&id=$id'>".Display :: return_icon('edit.gif', get_lang('Edit'));
  949. $html_link.= '&nbsp;';
  950. } else {
  951. $html_link.="<a href='exercise_show.php?".api_get_cidreq()."&action=qualify&id=$id'>".Display :: return_icon('quiz.gif', get_lang('Qualify'));
  952. $html_link.='&nbsp;';
  953. }
  954. $html_link.="</a>";
  955. if (api_is_platform_admin() || $is_tutor) {
  956. $html_link.=' <a href="exercice.php?'.api_get_cidreq().'&show=result&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>';
  957. $html_link.='&nbsp;';
  958. }
  959. if ($is_allowedToEdit) {
  960. if ($filter==2){
  961. $html_link.=' <a href="exercice_history.php?'.api_get_cidreq().'&exe_id=' . $id . '">' .Display :: return_icon('history.gif', get_lang('ViewHistoryChange')).'</a>';
  962. }
  963. }
  964. } else {
  965. if ($revised) {
  966. $html_link.="<a href='exercise_show.php?".api_get_cidreq()."&id=$id'>" . get_lang('Show') . "</a> ";
  967. } else {
  968. $html_link.='&nbsp;' . get_lang('NoResult');
  969. }
  970. }
  971. $more_details_list = $html_link;
  972. if ($is_allowedToEdit || $is_tutor) {
  973. $list_info [] = array($user_first_name,$user_last_name,$quiz_name_list,$duration_list,$date_list,$result_list,$more_details_list);
  974. } else {
  975. $list_info [] = array($quiz_name_list,$duration_list,$date_list,$result_list,$more_details_list);
  976. }
  977. }
  978. }
  979. }
  980. } else {
  981. $hpresults = getManyResultsXCol($hpsql, 6);
  982. // Print HotPotatoes test results.
  983. if (is_array($hpresults)) {
  984. for ($i = 0; $i < sizeof($hpresults); $i++) {
  985. $hp_title = GetQuizName($hpresults[$i][2], $documentPath);
  986. if ($hp_title == '') {
  987. $hp_title = basename($hpresults[$i][2]);
  988. }
  989. //$hp_date = api_convert_and_format_date($hpresults[$i][4], null, date_default_timezone_get());
  990. $hp_date = api_get_local_time($hpresults[$i][5], null, date_default_timezone_get());
  991. $hp_result = round(($hpresults[$i][3] / ($hpresults[$i][4] != 0 ? $hpresults[$i][4] : 1)) * 100, 2).'% ('.$hpresults[$i][3].' / '.$hpresults[$i][4].')';
  992. if ($is_allowedToEdit || $is_tutor) {
  993. $list_info[] = array($hpresults[$i][0], $hpresults[$i][1], $hp_title, '-', $hp_date , $hp_result , '-');
  994. } else {
  995. $list_info[] = array($hp_title, '-', $hp_date , $hp_result , '-');
  996. }
  997. }
  998. }
  999. }
  1000. return $list_info;
  1001. }
  1002. /**
  1003. * Converts the score with the exercise_max_note and exercise_min_score the platform settings + formats the results using the float_format function
  1004. *
  1005. * @param float score
  1006. * @param float weight
  1007. * @param bool show porcentage or not
  1008. * @param bool use or not the platform settings
  1009. * @return string an html with the score modified
  1010. */
  1011. function show_score($score, $weight, $show_porcentage = true, $use_platform_settings = true) {
  1012. $html = '';
  1013. $score_rounded = $score;
  1014. $max_note = api_get_setting('exercise_max_score');
  1015. $min_note = api_get_setting('exercise_min_score');
  1016. if ($use_platform_settings) {
  1017. if ($max_note != '' && $min_note != '') {
  1018. if (!empty($weight) && intval($weight) != 0) {
  1019. $score = $min_note + ($max_note - $min_note) * $score /$weight;
  1020. } else {
  1021. $score = $min_note;
  1022. }
  1023. $score_rounded = float_format($score, 1);
  1024. $weight = $max_note;
  1025. }
  1026. }
  1027. if ($show_porcentage) {
  1028. $html = float_format(($score / ($weight != 0 ? $weight : 1)) * 100, 1) . '% (' . $score_rounded . ' / ' . $weight . ')';
  1029. } else {
  1030. $weight = float_format($weight, 1);
  1031. $html = $score_rounded . ' / ' . $weight;
  1032. }
  1033. return $html;
  1034. }
  1035. /**
  1036. * Converts a score to the platform scale
  1037. * @param float score
  1038. * @param float weight
  1039. * @return float the score rounded converted to the new range
  1040. */
  1041. function convert_score($score, $weight) {
  1042. if ($score != '' && $weight != '') {
  1043. $max_note = api_get_setting('exercise_max_score');
  1044. $min_note = api_get_setting('exercise_min_score');
  1045. if ($max_note != '' && $min_note != '') {
  1046. if (!empty($weight)) {
  1047. $score = $min_note + ($max_note - $min_note) * $score /$weight;
  1048. } else {
  1049. $score = $min_note;
  1050. }
  1051. }
  1052. }
  1053. $score_rounded = round($score, 2);
  1054. return $score_rounded;
  1055. }
  1056. /**
  1057. * Getting all exercises from a course from a session (if a session_id is provided we will show all the exercise)
  1058. * @param array course data
  1059. * @param int session id
  1060. * @return array exercise data
  1061. */
  1062. function get_all_exercises($course_info = null, $session_id = 0) {
  1063. if(!empty($course_info)) {
  1064. $TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST,$course_info['db_name']);
  1065. } else {
  1066. $TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST);
  1067. }
  1068. if ($session_id == -1) {
  1069. $session_id = 0;
  1070. }
  1071. //var_dump($session_id);
  1072. if ($session_id == 0) {
  1073. $conditions = array('where'=>array('active <> ? AND session_id = ? '=>array('-1', $session_id)), 'order'=>'title');
  1074. } else {
  1075. //All exercises
  1076. $conditions = array('where'=>array('active <> ? AND (session_id = 0 OR session_id = ? )' =>array('-1', $session_id)), 'order'=>'title');
  1077. }
  1078. //var_dump($conditions);
  1079. return Database::select('*',$TBL_EXERCICES, $conditions);
  1080. }
  1081. /**
  1082. * Gets the position of the score based in a given score (result/weight) and the exe_id
  1083. * @param float user score to be compared
  1084. * @param int exe id of the exercise (this is necesary becase if 2 students have the same score the one with the minor exe_id will have a best position)
  1085. * @param int exercise id
  1086. * @param string course code
  1087. * @param int session id
  1088. * @return int the position of the user between his friends in a course (or course within a session)
  1089. */
  1090. function get_exercise_result_ranking($my_score, $my_exe_id, $exercise_id, $course_code, $session_id = 0) {
  1091. //Getting all exercise results
  1092. if (empty($session_id)) {
  1093. $session_id = 0;
  1094. }
  1095. $user_results = get_all_exercise_results($exercise_id, $course_code, $session_id);
  1096. if (empty($user_results)) {
  1097. return 1;
  1098. } else {
  1099. $position = 1;
  1100. $my_ranking = array();
  1101. foreach($user_results as $result) {
  1102. //print_r($result);
  1103. if (empty($result['exe_weighting']) || $result['exe_weighting'] == '0.00') {
  1104. $my_ranking[$result['exe_id']] = 0;
  1105. } else {
  1106. $my_ranking[$result['exe_id']] = $result['exe_result']/$result['exe_weighting'];
  1107. }
  1108. }
  1109. asort($my_ranking);
  1110. $position = count($my_ranking);
  1111. if (!empty($my_ranking))
  1112. foreach($my_ranking as $exe_id=>$ranking) {
  1113. if ($my_score >= $ranking) {
  1114. if ($my_score == $ranking) {
  1115. if ($my_exe_id < $exe_id) {
  1116. $position--;
  1117. }
  1118. } else {
  1119. $position--;
  1120. }
  1121. }
  1122. }
  1123. return $position;
  1124. }
  1125. }