exercise.lib.php 111 KB

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