exercise.lib.php 70 KB

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