exercise.lib.php 93 KB

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