exercise.lib.php 112 KB

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