exercise.lib.php 163 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Class ExerciseLib
  6. * shows a question and its answers
  7. * @author Olivier Brouckaert <oli.brouckaert@skynet.be>
  8. * @author Hubert Borderiou 2011-10-21
  9. * @author ivantcholakov2009-07-20
  10. *
  11. */
  12. class ExerciseLib
  13. {
  14. /**
  15. * Shows a question
  16. *
  17. * @param int $questionId $questionId question id
  18. * @param bool $only_questions if true only show the questions, no exercise title
  19. * @param bool $origin i.e = learnpath
  20. * @param string $current_item current item from the list of questions
  21. * @param bool $show_title
  22. * @param bool $freeze
  23. * @param array $user_choice
  24. * @param bool $show_comment
  25. * @param null $exercise_feedback
  26. * @param bool $show_answers
  27. * @return bool|int
  28. */
  29. public static function showQuestion(
  30. $questionId,
  31. $only_questions = false,
  32. $origin = false,
  33. $current_item = '',
  34. $show_title = true,
  35. $freeze = false,
  36. $user_choice = array(),
  37. $show_comment = false,
  38. $exercise_feedback = null,
  39. $show_answers = false,
  40. $show_icon = false
  41. ) {
  42. $course_id = api_get_course_int_id();
  43. $course = api_get_course_info_by_id($course_id);
  44. // Change false to true in the following line to enable answer hinting
  45. $debug_mark_answer = $show_answers;
  46. // Reads question information
  47. if (!$objQuestionTmp = Question::read($questionId)) {
  48. // Question not found
  49. return false;
  50. }
  51. if ($exercise_feedback != EXERCISE_FEEDBACK_TYPE_END) {
  52. $show_comment = false;
  53. }
  54. $answerType = $objQuestionTmp->selectType();
  55. $pictureName = $objQuestionTmp->getPictureFilename();
  56. $s = '';
  57. if ($answerType != HOT_SPOT && $answerType != HOT_SPOT_DELINEATION && $answerType != ANNOTATION) {
  58. // Question is not a hotspot
  59. if (!$only_questions) {
  60. $questionDescription = $objQuestionTmp->selectDescription();
  61. if ($show_title) {
  62. TestCategory::displayCategoryAndTitle($objQuestionTmp->id);
  63. $titleToDisplay = null;
  64. if ($answerType == READING_COMPREHENSION) {
  65. // In READING_COMPREHENSION, the title of the question
  66. // contains the question itself, which can only be
  67. // shown at the end of the given time, so hide for now
  68. $titleToDisplay = Display::div(
  69. $current_item.'. '.get_lang('ReadingComprehension'),
  70. ['class' => 'question_title']
  71. );
  72. } else {
  73. $titleToDisplay = $objQuestionTmp->getTitleToDisplay($current_item);
  74. }
  75. echo $titleToDisplay;
  76. }
  77. if (!empty($questionDescription) && $answerType != READING_COMPREHENSION) {
  78. echo Display::div(
  79. $questionDescription,
  80. array('class' => 'question_description')
  81. );
  82. }
  83. }
  84. if (in_array($answerType, array(FREE_ANSWER, ORAL_EXPRESSION)) &&
  85. $freeze
  86. ) {
  87. return '';
  88. }
  89. echo '<div class="question_options">';
  90. // construction of the Answer object (also gets all answers details)
  91. $objAnswerTmp = new Answer($questionId);
  92. $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
  93. $quiz_question_options = Question::readQuestionOption(
  94. $questionId,
  95. $course_id
  96. );
  97. // For "matching" type here, we need something a little bit special
  98. // because the match between the suggestions and the answers cannot be
  99. // done easily (suggestions and answers are in the same table), so we
  100. // have to go through answers first (elems with "correct" value to 0).
  101. $select_items = array();
  102. //This will contain the number of answers on the left side. We call them
  103. // suggestions here, for the sake of comprehensions, while the ones
  104. // on the right side are called answers
  105. $num_suggestions = 0;
  106. if (in_array($answerType, [MATCHING, DRAGGABLE, MATCHING_DRAGGABLE])) {
  107. if ($answerType == DRAGGABLE) {
  108. $isVertical = $objQuestionTmp->extra == 'v';
  109. $s .= '
  110. <div class="col-md-12 ui-widget ui-helper-clearfix">
  111. <div class="clearfix">
  112. <ul class="exercise-draggable-answer '.($isVertical ? '' : 'list-inline').'"
  113. id="question-'.$questionId.'" data-question="'.$questionId.'">
  114. ';
  115. } else {
  116. $s .= '<div id="drag'.$questionId.'_question" class="drag_question">
  117. <table class="data_table">';
  118. }
  119. // Iterate through answers
  120. $x = 1;
  121. //mark letters for each answer
  122. $letter = 'A';
  123. $answer_matching = array();
  124. $cpt1 = array();
  125. for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
  126. $answerCorrect = $objAnswerTmp->isCorrect($answerId);
  127. $numAnswer = $objAnswerTmp->selectAutoId($answerId);
  128. if ($answerCorrect == 0) {
  129. // options (A, B, C, ...) that will be put into the list-box
  130. // have the "correct" field set to 0 because they are answer
  131. $cpt1[$x] = $letter;
  132. $answer_matching[$x] = $objAnswerTmp->selectAnswerByAutoId(
  133. $numAnswer
  134. );
  135. $x++;
  136. $letter++;
  137. }
  138. }
  139. $i = 1;
  140. $select_items[0]['id'] = 0;
  141. $select_items[0]['letter'] = '--';
  142. $select_items[0]['answer'] = '';
  143. foreach ($answer_matching as $id => $value) {
  144. $select_items[$i]['id'] = $value['id_auto'];
  145. $select_items[$i]['letter'] = $cpt1[$id];
  146. $select_items[$i]['answer'] = $value['answer'];
  147. $i++;
  148. }
  149. $user_choice_array_position = array();
  150. if (!empty($user_choice)) {
  151. foreach ($user_choice as $item) {
  152. $user_choice_array_position[$item['position']] = $item['answer'];
  153. }
  154. }
  155. $num_suggestions = ($nbrAnswers - $x) + 1;
  156. } elseif ($answerType == FREE_ANSWER) {
  157. $fck_content = isset($user_choice[0]) && !empty($user_choice[0]['answer']) ? $user_choice[0]['answer'] : null;
  158. $form = new FormValidator('free_choice_'.$questionId);
  159. $config = array(
  160. 'ToolbarSet' => 'TestFreeAnswer'
  161. );
  162. $form->addHtmlEditor(
  163. "choice[".$questionId."]",
  164. null,
  165. false,
  166. false,
  167. $config
  168. );
  169. $form->setDefaults(
  170. array("choice[".$questionId."]" => $fck_content)
  171. );
  172. $s .= $form->returnForm();
  173. } elseif ($answerType == ORAL_EXPRESSION) {
  174. // Add nanog
  175. if (api_get_setting('enable_record_audio') == 'true') {
  176. //@todo pass this as a parameter
  177. global $exercise_stat_info, $exerciseId, $exe_id;
  178. if (!empty($exercise_stat_info)) {
  179. $objQuestionTmp->initFile(
  180. api_get_session_id(),
  181. api_get_user_id(),
  182. $exercise_stat_info['exe_exo_id'],
  183. $exercise_stat_info['exe_id']
  184. );
  185. } else {
  186. $objQuestionTmp->initFile(
  187. api_get_session_id(),
  188. api_get_user_id(),
  189. $exerciseId,
  190. 'temp_exe'
  191. );
  192. }
  193. echo $objQuestionTmp->returnRecorder();
  194. }
  195. $form = new FormValidator('free_choice_'.$questionId);
  196. $config = array(
  197. 'ToolbarSet' => 'TestFreeAnswer'
  198. );
  199. $form->addHtmlEditor(
  200. "choice[".$questionId."]",
  201. null,
  202. false,
  203. false,
  204. $config
  205. );
  206. $s .= $form->returnForm();
  207. }
  208. // Now navigate through the possible answers, using the max number of
  209. // answers for the question as a limiter
  210. $lines_count = 1; // a counter for matching-type answers
  211. if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE ||
  212. $answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE
  213. ) {
  214. $header = Display::tag('th', get_lang('Options'));
  215. foreach ($objQuestionTmp->options as $item) {
  216. if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
  217. if (in_array($item, $objQuestionTmp->options)) {
  218. $header .= Display::tag('th', get_lang($item));
  219. } else {
  220. $header .= Display::tag('th', $item);
  221. }
  222. } else {
  223. $header .= Display::tag('th', $item);
  224. }
  225. }
  226. if ($show_comment) {
  227. $header .= Display::tag('th', get_lang('Feedback'));
  228. }
  229. $s .= '<table class="table table-hover table-striped">';
  230. $s .= Display::tag(
  231. 'tr',
  232. $header,
  233. array('style' => 'text-align:left;')
  234. );
  235. }
  236. if ($show_comment) {
  237. if (
  238. in_array(
  239. $answerType,
  240. array(
  241. MULTIPLE_ANSWER,
  242. MULTIPLE_ANSWER_COMBINATION,
  243. UNIQUE_ANSWER,
  244. UNIQUE_ANSWER_IMAGE,
  245. UNIQUE_ANSWER_NO_OPTION,
  246. GLOBAL_MULTIPLE_ANSWER
  247. )
  248. )
  249. ) {
  250. $header = Display::tag('th', get_lang('Options'));
  251. if ($exercise_feedback == EXERCISE_FEEDBACK_TYPE_END) {
  252. $header .= Display::tag('th', get_lang('Feedback'));
  253. }
  254. $s .= '<table class="table table-hover table-striped">';
  255. $s .= Display::tag(
  256. 'tr',
  257. $header,
  258. array('style' => 'text-align:left;')
  259. );
  260. }
  261. }
  262. $matching_correct_answer = 0;
  263. $user_choice_array = array();
  264. if (!empty($user_choice)) {
  265. foreach ($user_choice as $item) {
  266. $user_choice_array[] = $item['answer'];
  267. }
  268. }
  269. $hidingClass = '';
  270. if ($answerType == READING_COMPREHENSION) {
  271. $objQuestionTmp->processText(
  272. $objQuestionTmp->selectDescription()
  273. );
  274. $hidingClass = 'hide-reading-answers';
  275. }
  276. if ($answerType == READING_COMPREHENSION) {
  277. $s .= Display::div(
  278. $objQuestionTmp->selectTitle(),
  279. ['class' => 'question_title '.$hidingClass]
  280. );
  281. }
  282. for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
  283. $answer = $objAnswerTmp->selectAnswer($answerId);
  284. $answerCorrect = $objAnswerTmp->isCorrect($answerId);
  285. $numAnswer = $objAnswerTmp->selectAutoId($answerId);
  286. $comment = $objAnswerTmp->selectComment($answerId);
  287. $attributes = array();
  288. switch ($answerType) {
  289. case UNIQUE_ANSWER:
  290. //no break
  291. case UNIQUE_ANSWER_NO_OPTION:
  292. //no break
  293. case UNIQUE_ANSWER_IMAGE:
  294. //no break
  295. case READING_COMPREHENSION:
  296. $input_id = 'choice-'.$questionId.'-'.$answerId;
  297. if (isset($user_choice[0]['answer']) && $user_choice[0]['answer'] == $numAnswer) {
  298. $attributes = array(
  299. 'id' => $input_id,
  300. 'checked' => 1,
  301. 'selected' => 1
  302. );
  303. } else {
  304. $attributes = array('id' => $input_id);
  305. }
  306. if ($debug_mark_answer) {
  307. if ($answerCorrect) {
  308. $attributes['checked'] = 1;
  309. $attributes['selected'] = 1;
  310. }
  311. }
  312. if ($show_comment) {
  313. $s .= '<tr><td>';
  314. }
  315. if ($answerType == UNIQUE_ANSWER_IMAGE) {
  316. if ($show_comment) {
  317. if (empty($comment)) {
  318. $s .= '<div id="answer'.$questionId.$numAnswer.'" '
  319. . 'class="exercise-unique-answer-image" style="text-align: center">';
  320. } else {
  321. $s .= '<div id="answer'.$questionId.$numAnswer.'" '
  322. . 'class="exercise-unique-answer-image col-xs-6 col-sm-12" style="text-align: center">';
  323. }
  324. } else {
  325. $s .= '<div id="answer'.$questionId.$numAnswer.'" '
  326. . 'class="exercise-unique-answer-image col-xs-6 col-md-3" style="text-align: center">';
  327. }
  328. }
  329. $answer = Security::remove_XSS($answer, STUDENT);
  330. $s .= Display::input(
  331. 'hidden',
  332. 'choice2['.$questionId.']',
  333. '0'
  334. );
  335. $answer_input = null;
  336. if ($answerType == UNIQUE_ANSWER_IMAGE) {
  337. $attributes['style'] = 'display: none;';
  338. $answer = '<div class="thumbnail">'.$answer.'</div>';
  339. }
  340. $attributes['class'] = 'checkradios';
  341. $answer_input .= '<label class="radio '.$hidingClass.'">';
  342. $answer_input .= Display::input(
  343. 'radio',
  344. 'choice['.$questionId.']',
  345. $numAnswer,
  346. $attributes
  347. );
  348. $answer_input .= $answer;
  349. $answer_input .= '</label>';
  350. if ($answerType == UNIQUE_ANSWER_IMAGE) {
  351. $answer_input .= "</div>";
  352. }
  353. if ($show_comment) {
  354. $s .= $answer_input;
  355. $s .= '</td>';
  356. $s .= '<td>';
  357. $s .= $comment;
  358. $s .= '</td>';
  359. $s .= '</tr>';
  360. } else {
  361. $s .= $answer_input;
  362. }
  363. break;
  364. case MULTIPLE_ANSWER:
  365. //no break
  366. case MULTIPLE_ANSWER_TRUE_FALSE:
  367. //no break
  368. case GLOBAL_MULTIPLE_ANSWER:
  369. $input_id = 'choice-'.$questionId.'-'.$answerId;
  370. $answer = Security::remove_XSS($answer, STUDENT);
  371. if (in_array($numAnswer, $user_choice_array)) {
  372. $attributes = array(
  373. 'id' => $input_id,
  374. 'checked' => 1,
  375. 'selected' => 1
  376. );
  377. } else {
  378. $attributes = array('id' => $input_id);
  379. }
  380. if ($debug_mark_answer) {
  381. if ($answerCorrect) {
  382. $attributes['checked'] = 1;
  383. $attributes['selected'] = 1;
  384. }
  385. }
  386. if ($answerType == MULTIPLE_ANSWER || $answerType == GLOBAL_MULTIPLE_ANSWER) {
  387. $s .= '<input type="hidden" name="choice2['.$questionId.']" value="0" />';
  388. $attributes['class'] = 'checkradios';
  389. $answer_input = '<label class="checkbox">';
  390. $answer_input .= Display::input(
  391. 'checkbox',
  392. 'choice['.$questionId.']['.$numAnswer.']',
  393. $numAnswer,
  394. $attributes
  395. );
  396. $answer_input .= $answer;
  397. $answer_input .= '</label>';
  398. if ($show_comment) {
  399. $s .= '<tr><td>';
  400. $s .= $answer_input;
  401. $s .= '</td>';
  402. $s .= '<td>';
  403. $s .= $comment;
  404. $s .= '</td>';
  405. $s .= '</tr>';
  406. } else {
  407. $s .= $answer_input;
  408. }
  409. } elseif ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
  410. $my_choice = array();
  411. if (!empty($user_choice_array)) {
  412. foreach ($user_choice_array as $item) {
  413. $item = explode(':', $item);
  414. $my_choice[$item[0]] = $item[1];
  415. }
  416. }
  417. $s .= '<tr>';
  418. $s .= Display::tag('td', $answer);
  419. if (!empty($quiz_question_options)) {
  420. foreach ($quiz_question_options as $id => $item) {
  421. if (isset($my_choice[$numAnswer]) && $id == $my_choice[$numAnswer]) {
  422. $attributes = array(
  423. 'checked' => 1,
  424. 'selected' => 1
  425. );
  426. } else {
  427. $attributes = array();
  428. }
  429. if ($debug_mark_answer) {
  430. if ($id == $answerCorrect) {
  431. $attributes['checked'] = 1;
  432. $attributes['selected'] = 1;
  433. }
  434. }
  435. $s .= Display::tag(
  436. 'td',
  437. Display::input(
  438. 'radio',
  439. 'choice['.$questionId.']['.$numAnswer.']',
  440. $id,
  441. $attributes
  442. ),
  443. array('style' => '')
  444. );
  445. }
  446. }
  447. if ($show_comment) {
  448. $s .= '<td>';
  449. $s .= $comment;
  450. $s .= '</td>';
  451. }
  452. $s .= '</tr>';
  453. }
  454. break;
  455. case MULTIPLE_ANSWER_COMBINATION:
  456. // multiple answers
  457. $input_id = 'choice-'.$questionId.'-'.$answerId;
  458. if (in_array($numAnswer, $user_choice_array)) {
  459. $attributes = array(
  460. 'id' => $input_id,
  461. 'checked' => 1,
  462. 'selected' => 1
  463. );
  464. } else {
  465. $attributes = array('id' => $input_id);
  466. }
  467. if ($debug_mark_answer) {
  468. if ($answerCorrect) {
  469. $attributes['checked'] = 1;
  470. $attributes['selected'] = 1;
  471. }
  472. }
  473. $answer = Security::remove_XSS($answer, STUDENT);
  474. $answer_input = '<input type="hidden" name="choice2['.$questionId.']" value="0" />';
  475. $answer_input .= '<label class="checkbox">';
  476. $answer_input .= Display::input(
  477. 'checkbox',
  478. 'choice['.$questionId.']['.$numAnswer.']',
  479. 1,
  480. $attributes
  481. );
  482. $answer_input .= $answer;
  483. $answer_input .= '</label>';
  484. if ($show_comment) {
  485. $s .= '<tr>';
  486. $s .= '<td>';
  487. $s .= $answer_input;
  488. $s .= '</td>';
  489. $s .= '<td>';
  490. $s .= $comment;
  491. $s .= '</td>';
  492. $s .= '</tr>';
  493. } else {
  494. $s .= $answer_input;
  495. }
  496. break;
  497. case MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE:
  498. $s .= '<input type="hidden" name="choice2['.$questionId.']" value="0" />';
  499. $my_choice = array();
  500. if (!empty($user_choice_array)) {
  501. foreach ($user_choice_array as $item) {
  502. $item = explode(':', $item);
  503. if (isset($item[1]) && isset($item[0])) {
  504. $my_choice[$item[0]] = $item[1];
  505. }
  506. }
  507. }
  508. $answer = Security::remove_XSS($answer, STUDENT);
  509. $s .= '<tr>';
  510. $s .= Display::tag('td', $answer);
  511. foreach ($objQuestionTmp->options as $key => $item) {
  512. if (isset($my_choice[$numAnswer]) && $key == $my_choice[$numAnswer]) {
  513. $attributes = array(
  514. 'checked' => 1,
  515. 'selected' => 1
  516. );
  517. } else {
  518. $attributes = array();
  519. }
  520. if ($debug_mark_answer) {
  521. if ($key == $answerCorrect) {
  522. $attributes['checked'] = 1;
  523. $attributes['selected'] = 1;
  524. }
  525. }
  526. $s .= Display::tag(
  527. 'td',
  528. Display::input(
  529. 'radio',
  530. 'choice['.$questionId.']['.$numAnswer.']',
  531. $key,
  532. $attributes
  533. )
  534. );
  535. }
  536. if ($show_comment) {
  537. $s .= '<td>';
  538. $s .= $comment;
  539. $s .= '</td>';
  540. }
  541. $s .= '</tr>';
  542. break;
  543. case FILL_IN_BLANKS:
  544. // display the question, with field empty, for student to fill it,
  545. // or filled to display the answer in the Question preview of the exercise/admin.php page
  546. $displayForStudent = true;
  547. $listAnswerInfo = FillBlanks::getAnswerInfo($answer);
  548. list($answer) = explode('::', $answer);
  549. // Correct answers
  550. $correctAnswerList = $listAnswerInfo['tabwords'];
  551. // Student's answer
  552. $studentAnswerList = array();
  553. if (isset($user_choice[0]['answer'])) {
  554. $arrayStudentAnswer = FillBlanks::getAnswerInfo($user_choice[0]['answer'], true);
  555. $studentAnswerList = $arrayStudentAnswer['studentanswer'];
  556. }
  557. // If the question must be shown with the answer (in page exercise/admin.php) for teacher preview
  558. // set the student-answer to the correct answer
  559. if ($debug_mark_answer) {
  560. $studentAnswerList = $correctAnswerList;
  561. $displayForStudent = false;
  562. }
  563. if (!empty($correctAnswerList) && !empty($studentAnswerList)) {
  564. $answer = '';
  565. for ($i = 0; $i < count($listAnswerInfo['commonwords']) - 1; $i++) {
  566. // display the common word
  567. $answer .= $listAnswerInfo['commonwords'][$i];
  568. // display the blank word
  569. $correctItem = $listAnswerInfo['tabwords'][$i];
  570. if (isset($studentAnswerList[$i])) {
  571. // If student already started this test and answered this question,
  572. // fill the blank with his previous answers
  573. // may be "" if student viewed the question, but did not fill the blanks
  574. $correctItem = $studentAnswerList[$i];
  575. }
  576. $attributes['style'] = "width:".$listAnswerInfo['tabinputsize'][$i]."px";
  577. $answer .= FillBlanks::getFillTheBlankHtml(
  578. $current_item,
  579. $questionId,
  580. $correctItem,
  581. $attributes,
  582. $answer,
  583. $listAnswerInfo,
  584. $displayForStudent,
  585. $i
  586. );
  587. }
  588. // display the last common word
  589. $answer .= $listAnswerInfo['commonwords'][$i];
  590. } else {
  591. // display empty [input] with the right width for student to fill it
  592. $answer = '';
  593. for ($i = 0; $i < count($listAnswerInfo['commonwords']) - 1; $i++) {
  594. // display the common words
  595. $answer .= $listAnswerInfo['commonwords'][$i];
  596. // display the blank word
  597. $attributes["style"] = "width:".$listAnswerInfo['tabinputsize'][$i]."px";
  598. $answer .= FillBlanks::getFillTheBlankHtml(
  599. $current_item,
  600. $questionId,
  601. '',
  602. $attributes,
  603. $answer,
  604. $listAnswerInfo,
  605. $displayForStudent,
  606. $i
  607. );
  608. }
  609. // display the last common word
  610. $answer .= $listAnswerInfo['commonwords'][$i];
  611. }
  612. $s .= $answer;
  613. break;
  614. case CALCULATED_ANSWER:
  615. /*
  616. * In the CALCULATED_ANSWER test
  617. * you mustn't have [ and ] in the textarea
  618. * you mustn't have @@ in the textarea
  619. * the text to find mustn't be empty or contains only spaces
  620. * the text to find mustn't contains HTML tags
  621. * the text to find mustn't contains char "
  622. */
  623. if ($origin !== null) {
  624. global $exe_id;
  625. $trackAttempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  626. $sql = 'SELECT answer FROM '.$trackAttempts.'
  627. WHERE exe_id=' . $exe_id.' AND question_id='.$questionId;
  628. $rsLastAttempt = Database::query($sql);
  629. $rowLastAttempt = Database::fetch_array($rsLastAttempt);
  630. $answer = $rowLastAttempt['answer'];
  631. if (empty($answer)) {
  632. $_SESSION['calculatedAnswerId'][$questionId] = mt_rand(
  633. 1,
  634. $nbrAnswers
  635. );
  636. $answer = $objAnswerTmp->selectAnswer(
  637. $_SESSION['calculatedAnswerId'][$questionId]
  638. );
  639. }
  640. }
  641. list($answer) = explode('@@', $answer);
  642. // $correctAnswerList array of array with correct anwsers 0=> [0=>[\p] 1=>[plop]]
  643. api_preg_match_all(
  644. '/\[[^]]+\]/',
  645. $answer,
  646. $correctAnswerList
  647. );
  648. // get student answer to display it if student go back to previous calculated answer question in a test
  649. if (isset($user_choice[0]['answer'])) {
  650. api_preg_match_all(
  651. '/\[[^]]+\]/',
  652. $answer,
  653. $studentAnswerList
  654. );
  655. $studentAnswerListTobecleaned = $studentAnswerList[0];
  656. $studentAnswerList = array();
  657. for ($i = 0; $i < count(
  658. $studentAnswerListTobecleaned
  659. ); $i++) {
  660. $answerCorrected = $studentAnswerListTobecleaned[$i];
  661. $answerCorrected = api_preg_replace(
  662. '| / <font color="green"><b>.*$|',
  663. '',
  664. $answerCorrected
  665. );
  666. $answerCorrected = api_preg_replace(
  667. '/^\[/',
  668. '',
  669. $answerCorrected
  670. );
  671. $answerCorrected = api_preg_replace(
  672. '|^<font color="red"><s>|',
  673. '',
  674. $answerCorrected
  675. );
  676. $answerCorrected = api_preg_replace(
  677. '|</s></font>$|',
  678. '',
  679. $answerCorrected
  680. );
  681. $answerCorrected = '['.$answerCorrected.']';
  682. $studentAnswerList[] = $answerCorrected;
  683. }
  684. }
  685. // If display preview of answer in test view for exemple, set the student answer to the correct answers
  686. if ($debug_mark_answer) {
  687. // contain the rights answers surronded with brackets
  688. $studentAnswerList = $correctAnswerList[0];
  689. }
  690. /*
  691. Split the response by bracket
  692. tabComments is an array with text surrounding the text to find
  693. we add a space before and after the answerQuestion to be sure to
  694. have a block of text before and after [xxx] patterns
  695. so we have n text to find ([xxx]) and n+1 block of texts before,
  696. between and after the text to find
  697. */
  698. $tabComments = api_preg_split(
  699. '/\[[^]]+\]/',
  700. ' '.$answer.' '
  701. );
  702. if (!empty($correctAnswerList) && !empty($studentAnswerList)) {
  703. $answer = "";
  704. $i = 0;
  705. foreach ($studentAnswerList as $studentItem) {
  706. // remove surronding brackets
  707. $studentResponse = api_substr(
  708. $studentItem,
  709. 1,
  710. api_strlen($studentItem) - 2
  711. );
  712. $size = strlen($studentItem);
  713. $attributes['class'] = self::detectInputAppropriateClass(
  714. $size
  715. );
  716. $answer .= $tabComments[$i].
  717. Display::input(
  718. 'text',
  719. "choice[$questionId][]",
  720. $studentResponse,
  721. $attributes
  722. );
  723. $i++;
  724. }
  725. $answer .= $tabComments[$i];
  726. } else {
  727. // display exercise with empty input fields
  728. // every [xxx] are replaced with an empty input field
  729. foreach ($correctAnswerList[0] as $item) {
  730. $size = strlen($item);
  731. $attributes['class'] = self::detectInputAppropriateClass(
  732. $size
  733. );
  734. $answer = str_replace(
  735. $item,
  736. Display::input(
  737. 'text',
  738. "choice[$questionId][]",
  739. '',
  740. $attributes
  741. ),
  742. $answer
  743. );
  744. }
  745. }
  746. if ($origin !== null) {
  747. $s = $answer;
  748. break;
  749. } else {
  750. $s .= $answer;
  751. }
  752. break;
  753. case MATCHING:
  754. // matching type, showing suggestions and answers
  755. // TODO: replace $answerId by $numAnswer
  756. if ($answerCorrect != 0) {
  757. // only show elements to be answered (not the contents of
  758. // the select boxes, who are correct = 0)
  759. $s .= '<tr><td width="45%" valign="top">';
  760. $parsed_answer = $answer;
  761. // Left part questions
  762. $s .= '<p class="indent">'.$lines_count.'.&nbsp;'.$parsed_answer.'</p></td>';
  763. // Middle part (matches selects)
  764. // Id of select is # question + # of option
  765. $s .= '<td width="10%" valign="top" align="center">
  766. <div class="select-matching">
  767. <select id="choice_id_'.$current_item.'_'.$lines_count.'" name="choice['.$questionId.']['.$numAnswer.']">';
  768. // fills the list-box
  769. foreach ($select_items as $key => $val) {
  770. // set $debug_mark_answer to true at function start to
  771. // show the correct answer with a suffix '-x'
  772. $selected = '';
  773. if ($debug_mark_answer) {
  774. if ($val['id'] == $answerCorrect) {
  775. $selected = 'selected="selected"';
  776. }
  777. }
  778. //$user_choice_array_position
  779. if (isset($user_choice_array_position[$numAnswer]) && $val['id'] == $user_choice_array_position[$numAnswer]) {
  780. $selected = 'selected="selected"';
  781. }
  782. $s .= '<option value="'.$val['id'].'" '.$selected.'>'.$val['letter'].'</option>';
  783. } // end foreach()
  784. $s .= '</select></div></td><td width="5%" class="separate">&nbsp;</td>';
  785. $s .= '<td width="40%" valign="top" >';
  786. if (isset($select_items[$lines_count])) {
  787. $s .= '<div class="text-right"><p class="indent">'.$select_items[$lines_count]['letter'].'.&nbsp; '.$select_items[$lines_count]['answer'].'</p></div>';
  788. } else {
  789. $s .= '&nbsp;';
  790. }
  791. $s .= '</td>';
  792. $s .= '</tr>';
  793. $lines_count++;
  794. //if the left side of the "matching" has been completely
  795. // shown but the right side still has values to show...
  796. if (($lines_count - 1) == $num_suggestions) {
  797. // if it remains answers to shown at the right side
  798. while (isset($select_items[$lines_count])) {
  799. $s .= '<tr>
  800. <td colspan="2"></td>
  801. <td valign="top">';
  802. $s .= '<b>'.$select_items[$lines_count]['letter'].'.</b> '.$select_items[$lines_count]['answer'];
  803. $s .= "</td>
  804. </tr>";
  805. $lines_count++;
  806. } // end while()
  807. } // end if()
  808. $matching_correct_answer++;
  809. }
  810. break;
  811. case DRAGGABLE:
  812. if ($answerCorrect) {
  813. $parsed_answer = $answer;
  814. /*$lines_count = '';
  815. $data = $objAnswerTmp->getAnswerByAutoId($numAnswer);
  816. $data = $objAnswerTmp->getAnswerByAutoId($data['correct']);
  817. $lines_count = $data['answer'];*/
  818. $windowId = $questionId.'_'.$lines_count;
  819. $s .= '<li class="touch-items" id="'.$windowId.'">';
  820. $s .= Display::div(
  821. $parsed_answer,
  822. [
  823. 'id' => "window_$windowId",
  824. 'class' => "window{$questionId}_question_draggable exercise-draggable-answer-option"
  825. ]
  826. );
  827. $draggableSelectOptions = [];
  828. $selectedValue = 0;
  829. $selectedIndex = 0;
  830. if ($user_choice) {
  831. foreach ($user_choice as $chosen) {
  832. if ($answerCorrect != $chosen['answer']) {
  833. continue;
  834. }
  835. $selectedValue = $chosen['answer'];
  836. }
  837. }
  838. foreach ($select_items as $key => $select_item) {
  839. $draggableSelectOptions[$select_item['id']] = $select_item['letter'];
  840. }
  841. foreach ($draggableSelectOptions as $value => $text) {
  842. if ($value == $selectedValue) {
  843. break;
  844. }
  845. $selectedIndex++;
  846. }
  847. $s .= Display::select(
  848. "choice[$questionId][$numAnswer]",
  849. $draggableSelectOptions,
  850. $selectedValue,
  851. [
  852. 'id' => "window_{$windowId}_select",
  853. 'class' => 'select_option hidden',
  854. ],
  855. false
  856. );
  857. if ($selectedValue && $selectedIndex) {
  858. $s .= "
  859. <script>
  860. $(function() {
  861. DraggableAnswer.deleteItem(
  862. $('#{$questionId}_$lines_count'),
  863. $('#drop_{$questionId}_{$selectedIndex}')
  864. );
  865. });
  866. </script>
  867. ";
  868. }
  869. if (isset($select_items[$lines_count])) {
  870. $s .= Display::div(
  871. Display::tag(
  872. 'b',
  873. $select_items[$lines_count]['letter']
  874. ).$select_items[$lines_count]['answer'],
  875. [
  876. 'id' => "window_{$windowId}_answer",
  877. 'class' => 'hidden'
  878. ]
  879. );
  880. } else {
  881. $s .= '&nbsp;';
  882. }
  883. $lines_count++;
  884. if (($lines_count - 1) == $num_suggestions) {
  885. while (isset($select_items[$lines_count])) {
  886. $s .= Display::tag('b', $select_items[$lines_count]['letter']);
  887. $s .= $select_items[$lines_count]['answer'];
  888. $lines_count++;
  889. }
  890. }
  891. $matching_correct_answer++;
  892. $s .= '</li>';
  893. }
  894. break;
  895. case MATCHING_DRAGGABLE:
  896. if ($answerId == 1) {
  897. echo $objAnswerTmp->getJs();
  898. }
  899. if ($answerCorrect != 0) {
  900. $parsed_answer = strip_tags($answer);
  901. $windowId = "{$questionId}_{$lines_count}";
  902. $s .= <<<HTML
  903. <tr>
  904. <td widht="45%">
  905. <div id="window_{$windowId}" class="window window_left_question window{$questionId}_question">
  906. <p><strong>$lines_count.</strong> $parsed_answer</p>
  907. </div>
  908. </td>
  909. <td width="10%">
  910. HTML;
  911. $draggableSelectOptions = [];
  912. $selectedValue = 0;
  913. $selectedIndex = 0;
  914. if ($user_choice) {
  915. foreach ($user_choice as $chosen) {
  916. if ($answerCorrect != $chosen['answer']) {
  917. continue;
  918. }
  919. $selectedValue = $chosen['answer'];
  920. }
  921. }
  922. foreach ($select_items as $key => $select_item) {
  923. $draggableSelectOptions[$select_item['id']] = $select_item['letter'];
  924. }
  925. foreach ($draggableSelectOptions as $value => $text) {
  926. if ($value == $selectedValue) {
  927. break;
  928. }
  929. $selectedIndex++;
  930. }
  931. $s .= Display::select(
  932. "choice[$questionId][$numAnswer]",
  933. $draggableSelectOptions,
  934. $selectedValue,
  935. [
  936. 'id' => "window_{$windowId}_select",
  937. 'class' => 'hidden'
  938. ],
  939. false
  940. );
  941. if (!empty($answerCorrect) && !empty($selectedValue)) {
  942. // Show connect if is not freeze (question preview)
  943. if (!$freeze) {
  944. $s .= "
  945. <script>
  946. $(document).on('ready', function () {
  947. jsPlumb.ready(function() {
  948. jsPlumb.connect({
  949. source: 'window_$windowId',
  950. target: 'window_{$questionId}_{$selectedIndex}_answer',
  951. endpoint: ['Blank', {radius: 15}],
  952. anchors: ['RightMiddle', 'LeftMiddle'],
  953. paintStyle: {strokeStyle: '#8A8888', lineWidth: 8},
  954. connector: [
  955. MatchingDraggable.connectorType,
  956. {curvines: MatchingDraggable.curviness}
  957. ]
  958. });
  959. });
  960. });
  961. </script>
  962. ";
  963. }
  964. }
  965. $s .= <<<HTML
  966. </td>
  967. <td width="45%" class="point_end">
  968. HTML;
  969. if (isset($select_items[$lines_count])) {
  970. $panswer = strip_tags($select_items[$lines_count]['answer']);
  971. $s .= <<<HTML
  972. <div id="window_{$windowId}_answer" class="window window_right_question">
  973. <p><strong>{$select_items[$lines_count]['letter']}.</strong> {$panswer}</p>
  974. </div>
  975. HTML;
  976. } else {
  977. $s .= '&nbsp;';
  978. }
  979. $s .= '</td></tr>';
  980. $lines_count++;
  981. if (($lines_count - 1) == $num_suggestions) {
  982. while (isset($select_items[$lines_count])) {
  983. $s .= <<<HTML
  984. <tr>
  985. <td colspan="2"></td>
  986. <td>
  987. <strong>{$select_items[$lines_count]['letter']}</strong>
  988. {$select_items[$lines_count]['answer']}
  989. </td>
  990. </tr>
  991. HTML;
  992. $lines_count++;
  993. }
  994. }
  995. $matching_correct_answer++;
  996. }
  997. break;
  998. }
  999. } // end for()
  1000. if ($show_comment) {
  1001. $s .= '</table>';
  1002. } elseif (in_array(
  1003. $answerType,
  1004. [
  1005. MATCHING,
  1006. MATCHING_DRAGGABLE,
  1007. UNIQUE_ANSWER_NO_OPTION,
  1008. MULTIPLE_ANSWER_TRUE_FALSE,
  1009. MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE,
  1010. ]
  1011. )) {
  1012. $s .= '</table>';
  1013. }
  1014. if ($answerType == DRAGGABLE) {
  1015. $isVertical = $objQuestionTmp->extra == 'v';
  1016. $s .= "</ul>";
  1017. $s .= "</div>"; //clearfix
  1018. $counterAnswer = 1;
  1019. $s .= $isVertical ? '' : '<div class="row">';
  1020. $count = 0;
  1021. for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
  1022. $answerCorrect = $objAnswerTmp->isCorrect($answerId);
  1023. $windowId = $questionId.'_'.$counterAnswer;
  1024. if ($answerCorrect) {
  1025. $count++;
  1026. $s .= $isVertical ? '<div class="row">' : '';
  1027. $s .= '
  1028. <div class="'.($isVertical ? 'col-md-12' : 'col-xs-12 col-sm-4 col-md-3 col-lg-2').'">
  1029. <div class="droppable-item">
  1030. <span class="number">'.$count.'.</span>
  1031. <div id="drop_'.$windowId.'" class="droppable">&nbsp;</div>
  1032. </div>
  1033. </div>
  1034. ';
  1035. $s .= $isVertical ? '</div>' : '';
  1036. $counterAnswer++;
  1037. }
  1038. }
  1039. $s .= $isVertical ? '' : '</div>'; // row
  1040. $s .= '</div>'; // col-md-12 ui-widget ui-helper-clearfix
  1041. }
  1042. if (in_array($answerType, [MATCHING, MATCHING_DRAGGABLE])) {
  1043. $s .= '</div>'; //drag_question
  1044. }
  1045. $s .= '</div>'; //question_options row
  1046. // destruction of the Answer object
  1047. unset($objAnswerTmp);
  1048. // destruction of the Question object
  1049. unset($objQuestionTmp);
  1050. if ($origin == 'export') {
  1051. return $s;
  1052. }
  1053. echo $s;
  1054. } elseif ($answerType == HOT_SPOT || $answerType == HOT_SPOT_DELINEATION) {
  1055. global $exerciseId, $exe_id;
  1056. // Question is a HOT_SPOT
  1057. //checking document/images visibility
  1058. if (api_is_platform_admin() || api_is_course_admin()) {
  1059. $doc_id = $objQuestionTmp->getPictureId();
  1060. if (is_numeric($doc_id)) {
  1061. $images_folder_visibility = api_get_item_visibility(
  1062. $course,
  1063. 'document',
  1064. $doc_id,
  1065. api_get_session_id()
  1066. );
  1067. if (!$images_folder_visibility) {
  1068. //This message is shown only to the course/platform admin if the image is set to visibility = false
  1069. echo Display::return_message(
  1070. get_lang('ChangeTheVisibilityOfTheCurrentImage'),
  1071. 'warning'
  1072. );
  1073. }
  1074. }
  1075. }
  1076. $questionName = $objQuestionTmp->selectTitle();
  1077. $questionDescription = $objQuestionTmp->selectDescription();
  1078. // Get the answers, make a list
  1079. $objAnswerTmp = new Answer($questionId);
  1080. $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
  1081. // get answers of hotpost
  1082. $answers_hotspot = array();
  1083. for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
  1084. $answers = $objAnswerTmp->selectAnswerByAutoId(
  1085. $objAnswerTmp->selectAutoId($answerId)
  1086. );
  1087. $answers_hotspot[$answers['id']] = $objAnswerTmp->selectAnswer(
  1088. $answerId
  1089. );
  1090. }
  1091. $answerList = '';
  1092. $hotspotColor = 0;
  1093. if ($answerType != HOT_SPOT_DELINEATION) {
  1094. $answerList = '
  1095. <div class="well well-sm">
  1096. <h5 class="page-header">' . get_lang('HotspotZones').'</h5>
  1097. <ol>
  1098. ';
  1099. if (!empty($answers_hotspot)) {
  1100. Session::write("hotspot_ordered$questionId", array_keys($answers_hotspot));
  1101. foreach ($answers_hotspot as $value) {
  1102. $answerList .= "<li>";
  1103. if ($freeze) {
  1104. $answerList .= '<span class="hotspot-color-'.$hotspotColor
  1105. .' fa fa-square" aria-hidden="true"></span>'.PHP_EOL;
  1106. }
  1107. $answerList .= $value;
  1108. $answerList .= "</li>";
  1109. $hotspotColor++;
  1110. }
  1111. }
  1112. $answerList .= '
  1113. </ul>
  1114. </div>
  1115. ';
  1116. if ($freeze) {
  1117. $relPath = api_get_path(WEB_CODE_PATH);
  1118. echo "
  1119. <div class=\"row\">
  1120. <div class=\"col-sm-9\">
  1121. <div id=\"hotspot-preview-$questionId\"></div>
  1122. </div>
  1123. <div class=\"col-sm-3\">
  1124. $answerList
  1125. </div>
  1126. </div>
  1127. <script>
  1128. new ".($answerType == HOT_SPOT ? "HotspotQuestion" : "DelineationQuestion")."({
  1129. questionId: $questionId,
  1130. exerciseId: $exerciseId,
  1131. selector: '#hotspot-preview-$questionId',
  1132. for: 'preview',
  1133. relPath: '$relPath'
  1134. });
  1135. </script>
  1136. ";
  1137. return;
  1138. }
  1139. }
  1140. if (!$only_questions) {
  1141. if ($show_title) {
  1142. TestCategory::displayCategoryAndTitle($objQuestionTmp->id);
  1143. echo $objQuestionTmp->getTitleToDisplay($current_item);
  1144. }
  1145. //@todo I need to the get the feedback type
  1146. echo <<<HOTSPOT
  1147. <input type="hidden" name="hidden_hotspot_id" value="$questionId" />
  1148. <div class="exercise_questions">
  1149. $questionDescription
  1150. <div class="row">
  1151. HOTSPOT;
  1152. }
  1153. $relPath = api_get_path(WEB_CODE_PATH);
  1154. $s .= "
  1155. <div class=\"col-sm-8 col-md-9\">
  1156. <div class=\"hotspot-image\"></div>
  1157. <script>
  1158. $(document).on('ready', function () {
  1159. new " . ($answerType == HOT_SPOT_DELINEATION ? 'DelineationQuestion' : 'HotspotQuestion')."({
  1160. questionId: $questionId,
  1161. exerciseId: $exe_id,
  1162. selector: '#question_div_' + $questionId + ' .hotspot-image',
  1163. for: 'user',
  1164. relPath: '$relPath'
  1165. });
  1166. });
  1167. </script>
  1168. </div>
  1169. <div class=\"col-sm-4 col-md-3\">
  1170. $answerList
  1171. </div>
  1172. ";
  1173. echo <<<HOTSPOT
  1174. $s
  1175. </div>
  1176. </div>
  1177. HOTSPOT;
  1178. } elseif ($answerType == ANNOTATION) {
  1179. global $exe_id;
  1180. $relPath = api_get_path(WEB_CODE_PATH);
  1181. if (api_is_platform_admin() || api_is_course_admin()) {
  1182. $docId = DocumentManager::get_document_id($course, '/images/'.$pictureName);
  1183. if ($docId) {
  1184. $images_folder_visibility = api_get_item_visibility(
  1185. $course,
  1186. 'document',
  1187. $docId,
  1188. api_get_session_id()
  1189. );
  1190. if (!$images_folder_visibility) {
  1191. echo Display::return_message(get_lang('ChangeTheVisibilityOfTheCurrentImage'), 'warning');
  1192. }
  1193. }
  1194. if ($freeze) {
  1195. echo Display::img(
  1196. api_get_path(WEB_COURSE_PATH).$course['path'].'/document/images/'.$pictureName,
  1197. $objQuestionTmp->selectTitle(),
  1198. ['width' => '600px']
  1199. );
  1200. return 0;
  1201. }
  1202. }
  1203. if (!$only_questions) {
  1204. if ($show_title) {
  1205. TestCategory::displayCategoryAndTitle($objQuestionTmp->id);
  1206. echo $objQuestionTmp->getTitleToDisplay($current_item);
  1207. }
  1208. echo '
  1209. <input type="hidden" name="hidden_hotspot_id" value="'.$questionId.'" />
  1210. <div class="exercise_questions">
  1211. '.$objQuestionTmp->selectDescription().'
  1212. <div class="row">
  1213. <div class="col-sm-8 col-md-9">
  1214. <div id="annotation-canvas-'.$questionId.'" class="annotation-canvas center-block">
  1215. </div>
  1216. <script>
  1217. AnnotationQuestion({
  1218. questionId: '.$questionId.',
  1219. exerciseId: '.$exe_id.',
  1220. relPath: \''.$relPath.'\'
  1221. });
  1222. </script>
  1223. </div>
  1224. <div class="col-sm-4 col-md-3">
  1225. <div class="well well-sm" id="annotation-toolbar-'.$questionId.'">
  1226. <div class="btn-toolbar">
  1227. <div class="btn-group" data-toggle="buttons">
  1228. <label class="btn btn-default active"
  1229. aria-label="'.get_lang('AddAnnotationPath').'">
  1230. <input type="radio" value="0" name="'.$questionId.'-options" autocomplete="off" checked>
  1231. <span class="fa fa-pencil" aria-hidden="true"></span>
  1232. </label>
  1233. <label class="btn btn-default"
  1234. aria-label="'.get_lang('AddAnnotationText').'">
  1235. <input type="radio" value="1" name="'.$questionId.'-options" autocomplete="off">
  1236. <span class="fa fa-font fa-fw" aria-hidden="true"></span>
  1237. </label>
  1238. </div>
  1239. </div>
  1240. <ul class="list-unstyled"></ul>
  1241. </div>
  1242. </div>
  1243. </div>
  1244. </div>
  1245. ';
  1246. }
  1247. $objAnswerTmp = new Answer($questionId);
  1248. $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
  1249. unset($objAnswerTmp, $objQuestionTmp);
  1250. }
  1251. return $nbrAnswers;
  1252. }
  1253. /**
  1254. * @param int $exe_id
  1255. * @return array
  1256. */
  1257. public static function get_exercise_track_exercise_info($exe_id)
  1258. {
  1259. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  1260. $TBL_TRACK_EXERCICES = Database::get_main_table(
  1261. TABLE_STATISTIC_TRACK_E_EXERCISES
  1262. );
  1263. $TBL_COURSE = Database::get_main_table(TABLE_MAIN_COURSE);
  1264. $exe_id = intval($exe_id);
  1265. $result = array();
  1266. if (!empty($exe_id)) {
  1267. $sql = " SELECT q.*, tee.*
  1268. FROM $TBL_EXERCICES as q
  1269. INNER JOIN $TBL_TRACK_EXERCICES as tee
  1270. ON q.id = tee.exe_exo_id
  1271. INNER JOIN $TBL_COURSE c
  1272. ON c.id = tee.c_id
  1273. WHERE tee.exe_id = $exe_id
  1274. AND q.c_id = c.id";
  1275. $res_fb_type = Database::query($sql);
  1276. $result = Database::fetch_array($res_fb_type, 'ASSOC');
  1277. }
  1278. return $result;
  1279. }
  1280. /**
  1281. * Validates the time control key
  1282. * @param int $exercise_id
  1283. * @param int $lp_id
  1284. * @param int $lp_item_id
  1285. * @return bool
  1286. */
  1287. public static function exercise_time_control_is_valid(
  1288. $exercise_id,
  1289. $lp_id = 0,
  1290. $lp_item_id = 0
  1291. ) {
  1292. $course_id = api_get_course_int_id();
  1293. $exercise_id = intval($exercise_id);
  1294. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  1295. $sql = "SELECT expired_time FROM $TBL_EXERCICES
  1296. WHERE c_id = $course_id AND id = $exercise_id";
  1297. $result = Database::query($sql);
  1298. $row = Database::fetch_array($result, 'ASSOC');
  1299. if (!empty($row['expired_time'])) {
  1300. $current_expired_time_key = self::get_time_control_key(
  1301. $exercise_id,
  1302. $lp_id,
  1303. $lp_item_id
  1304. );
  1305. if (isset($_SESSION['expired_time'][$current_expired_time_key])) {
  1306. $current_time = time();
  1307. $expired_time = api_strtotime(
  1308. $_SESSION['expired_time'][$current_expired_time_key],
  1309. 'UTC'
  1310. );
  1311. $total_time_allowed = $expired_time + 30;
  1312. if ($total_time_allowed < $current_time) {
  1313. return false;
  1314. }
  1315. return true;
  1316. } else {
  1317. return false;
  1318. }
  1319. } else {
  1320. return true;
  1321. }
  1322. }
  1323. /**
  1324. * Deletes the time control token
  1325. *
  1326. * @param int $exercise_id
  1327. * @param int $lp_id
  1328. * @param int $lp_item_id
  1329. */
  1330. public static function exercise_time_control_delete(
  1331. $exercise_id,
  1332. $lp_id = 0,
  1333. $lp_item_id = 0
  1334. ) {
  1335. $current_expired_time_key = self::get_time_control_key(
  1336. $exercise_id,
  1337. $lp_id,
  1338. $lp_item_id
  1339. );
  1340. unset($_SESSION['expired_time'][$current_expired_time_key]);
  1341. }
  1342. /**
  1343. * Generates the time control key
  1344. */
  1345. public static function get_time_control_key(
  1346. $exercise_id,
  1347. $lp_id = 0,
  1348. $lp_item_id = 0
  1349. ) {
  1350. $exercise_id = intval($exercise_id);
  1351. $lp_id = intval($lp_id);
  1352. $lp_item_id = intval($lp_item_id);
  1353. return
  1354. api_get_course_int_id().'_'.
  1355. api_get_session_id().'_'.
  1356. $exercise_id.'_'.
  1357. api_get_user_id().'_'.
  1358. $lp_id.'_'.
  1359. $lp_item_id;
  1360. }
  1361. /**
  1362. * Get session time control
  1363. *
  1364. * @param int $exercise_id
  1365. * @param int $lp_id
  1366. * @param int $lp_item_id
  1367. * @return int
  1368. */
  1369. public static function get_session_time_control_key(
  1370. $exercise_id,
  1371. $lp_id = 0,
  1372. $lp_item_id = 0
  1373. ) {
  1374. $return_value = 0;
  1375. $time_control_key = self::get_time_control_key(
  1376. $exercise_id,
  1377. $lp_id,
  1378. $lp_item_id
  1379. );
  1380. if (isset($_SESSION['expired_time']) && isset($_SESSION['expired_time'][$time_control_key])) {
  1381. $return_value = $_SESSION['expired_time'][$time_control_key];
  1382. }
  1383. return $return_value;
  1384. }
  1385. /**
  1386. * Gets count of exam results
  1387. * @todo this function should be moved in a library + no global calls
  1388. */
  1389. public static function get_count_exam_results($exercise_id, $extra_where_conditions)
  1390. {
  1391. $count = self::get_exam_results_data(
  1392. null,
  1393. null,
  1394. null,
  1395. null,
  1396. $exercise_id,
  1397. $extra_where_conditions,
  1398. true
  1399. );
  1400. return $count;
  1401. }
  1402. /**
  1403. * @param string $in_hotpot_path
  1404. * @return int
  1405. */
  1406. public static function get_count_exam_hotpotatoes_results($in_hotpot_path)
  1407. {
  1408. return self::get_exam_results_hotpotatoes_data(
  1409. 0,
  1410. 0,
  1411. '',
  1412. '',
  1413. $in_hotpot_path,
  1414. true,
  1415. ''
  1416. );
  1417. }
  1418. /**
  1419. * @param int $in_from
  1420. * @param int $in_number_of_items
  1421. * @param int $in_column
  1422. * @param int $in_direction
  1423. * @param string $in_hotpot_path
  1424. * @param bool $in_get_count
  1425. * @param null $where_condition
  1426. * @return array|int
  1427. */
  1428. public static function get_exam_results_hotpotatoes_data(
  1429. $in_from,
  1430. $in_number_of_items,
  1431. $in_column,
  1432. $in_direction,
  1433. $in_hotpot_path,
  1434. $in_get_count = false,
  1435. $where_condition = null
  1436. ) {
  1437. $courseId = api_get_course_int_id();
  1438. // by default in_column = 1 If parameters given, it is the name of the column witch is the bdd field name
  1439. if ($in_column == 1) {
  1440. $in_column = 'firstname';
  1441. }
  1442. $in_hotpot_path = Database::escape_string($in_hotpot_path);
  1443. $in_direction = Database::escape_string($in_direction);
  1444. $in_column = Database::escape_string($in_column);
  1445. $in_number_of_items = intval($in_number_of_items);
  1446. $in_from = intval($in_from);
  1447. $TBL_TRACK_HOTPOTATOES = Database::get_main_table(
  1448. TABLE_STATISTIC_TRACK_E_HOTPOTATOES
  1449. );
  1450. $TBL_USER = Database::get_main_table(TABLE_MAIN_USER);
  1451. $sql = "SELECT * FROM $TBL_TRACK_HOTPOTATOES thp
  1452. JOIN $TBL_USER u ON thp.exe_user_id = u.user_id
  1453. WHERE thp.c_id = $courseId AND exe_name LIKE '$in_hotpot_path%'";
  1454. // just count how many answers
  1455. if ($in_get_count) {
  1456. $res = Database::query($sql);
  1457. return Database::num_rows($res);
  1458. }
  1459. // get a number of sorted results
  1460. $sql .= " $where_condition
  1461. ORDER BY $in_column $in_direction
  1462. LIMIT $in_from, $in_number_of_items";
  1463. $res = Database::query($sql);
  1464. $result = array();
  1465. $apiIsAllowedToEdit = api_is_allowed_to_edit();
  1466. $urlBase = api_get_path(WEB_CODE_PATH).
  1467. 'exercise/hotpotatoes_exercise_report.php?action=delete&'.
  1468. api_get_cidreq().'&id=';
  1469. while ($data = Database::fetch_array($res)) {
  1470. $actions = null;
  1471. if ($apiIsAllowedToEdit) {
  1472. $url = $urlBase.$data['id'].'&path='.$data['exe_name'];
  1473. $actions = Display::url(
  1474. Display::return_icon('delete.png', get_lang('Delete')),
  1475. $url
  1476. );
  1477. }
  1478. $result[] = array(
  1479. 'firstname' => $data['firstname'],
  1480. 'lastname' => $data['lastname'],
  1481. 'username' => $data['username'],
  1482. 'group_name' => implode(
  1483. "<br/>",
  1484. GroupManager::get_user_group_name($data['user_id'])
  1485. ),
  1486. 'exe_date' => $data['exe_date'],
  1487. 'score' => $data['exe_result'].' / '.$data['exe_weighting'],
  1488. 'actions' => $actions,
  1489. );
  1490. }
  1491. return $result;
  1492. }
  1493. /**
  1494. * @param string $exercisePath
  1495. * @param int $userId
  1496. * @param int $courseId
  1497. * @param int $sessionId
  1498. *
  1499. * @return array
  1500. */
  1501. public static function getLatestHotPotatoResult(
  1502. $exercisePath,
  1503. $userId,
  1504. $courseId,
  1505. $sessionId
  1506. ) {
  1507. $table = Database::get_main_table(
  1508. TABLE_STATISTIC_TRACK_E_HOTPOTATOES
  1509. );
  1510. $exercisePath = Database::escape_string($exercisePath);
  1511. $userId = intval($userId);
  1512. $sql = "SELECT * FROM $table
  1513. WHERE
  1514. c_id = $courseId AND
  1515. exe_name LIKE '$exercisePath%' AND
  1516. exe_user_id = $userId
  1517. ORDER BY id
  1518. LIMIT 1";
  1519. $result = Database::query($sql);
  1520. $attempt = array();
  1521. if (Database::num_rows($result)) {
  1522. $attempt = Database::fetch_array($result, 'ASSOC');
  1523. }
  1524. return $attempt;
  1525. }
  1526. /**
  1527. * Gets the exam'data results
  1528. * @todo this function should be moved in a library + no global calls
  1529. * @param int $from
  1530. * @param int $number_of_items
  1531. * @param int $column
  1532. * @param string $direction
  1533. * @param int $exercise_id
  1534. * @param null $extra_where_conditions
  1535. * @param bool $get_count
  1536. * @param string $courseCode
  1537. * @return array
  1538. */
  1539. public static function get_exam_results_data(
  1540. $from,
  1541. $number_of_items,
  1542. $column,
  1543. $direction,
  1544. $exercise_id,
  1545. $extra_where_conditions = null,
  1546. $get_count = false,
  1547. $courseCode = null
  1548. ) {
  1549. //@todo replace all this globals
  1550. global $documentPath, $filter;
  1551. $courseCode = empty($courseCode) ? api_get_course_id() : $courseCode;
  1552. $courseInfo = api_get_course_info($courseCode);
  1553. $course_id = $courseInfo['real_id'];
  1554. $sessionId = api_get_session_id();
  1555. $is_allowedToEdit = api_is_allowed_to_edit(null, true) || api_is_allowed_to_edit(true) || api_is_drh() || api_is_student_boss();
  1556. $TBL_USER = Database::get_main_table(TABLE_MAIN_USER);
  1557. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  1558. $TBL_GROUP_REL_USER = Database::get_course_table(TABLE_GROUP_USER);
  1559. $TBL_GROUP = Database::get_course_table(TABLE_GROUP);
  1560. $TBL_TRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1561. $TBL_TRACK_HOTPOTATOES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  1562. $TBL_TRACK_ATTEMPT_RECORDING = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1563. $session_id_and = ' AND te.session_id = '.$sessionId.' ';
  1564. $exercise_id = intval($exercise_id);
  1565. $exercise_where = '';
  1566. if (!empty($exercise_id)) {
  1567. $exercise_where .= ' AND te.exe_exo_id = '.$exercise_id.' ';
  1568. }
  1569. $hotpotatoe_where = '';
  1570. if (!empty($_GET['path'])) {
  1571. $hotpotatoe_path = Database::escape_string($_GET['path']);
  1572. $hotpotatoe_where .= ' AND exe_name = "'.$hotpotatoe_path.'" ';
  1573. }
  1574. // sql for chamilo-type tests for teacher / tutor view
  1575. $sql_inner_join_tbl_track_exercices = "
  1576. (
  1577. SELECT DISTINCT ttte.*, if(tr.exe_id,1, 0) as revised
  1578. FROM $TBL_TRACK_EXERCICES ttte LEFT JOIN $TBL_TRACK_ATTEMPT_RECORDING tr
  1579. ON (ttte.exe_id = tr.exe_id)
  1580. WHERE
  1581. c_id = $course_id AND
  1582. exe_exo_id = $exercise_id AND
  1583. ttte.session_id = ".$sessionId."
  1584. )";
  1585. if ($is_allowedToEdit) {
  1586. //@todo fix to work with COURSE_RELATION_TYPE_RRHH in both queries
  1587. // Hack in order to filter groups
  1588. $sql_inner_join_tbl_user = '';
  1589. if (strpos($extra_where_conditions, 'group_id')) {
  1590. $sql_inner_join_tbl_user = "
  1591. (
  1592. SELECT
  1593. u.user_id,
  1594. firstname,
  1595. lastname,
  1596. official_code,
  1597. email,
  1598. username,
  1599. g.name as group_name,
  1600. g.id as group_id
  1601. FROM $TBL_USER u
  1602. INNER JOIN $TBL_GROUP_REL_USER gru
  1603. ON (gru.user_id = u.user_id AND gru.c_id=".$course_id.")
  1604. INNER JOIN $TBL_GROUP g
  1605. ON (gru.group_id = g.id AND g.c_id=".$course_id.")
  1606. )";
  1607. }
  1608. if (strpos($extra_where_conditions, 'group_all')) {
  1609. $extra_where_conditions = str_replace(
  1610. "AND ( group_id = 'group_all' )",
  1611. '',
  1612. $extra_where_conditions
  1613. );
  1614. $extra_where_conditions = str_replace(
  1615. "AND group_id = 'group_all'",
  1616. '',
  1617. $extra_where_conditions
  1618. );
  1619. $extra_where_conditions = str_replace(
  1620. "group_id = 'group_all' AND",
  1621. '',
  1622. $extra_where_conditions
  1623. );
  1624. $sql_inner_join_tbl_user = "
  1625. (
  1626. SELECT
  1627. u.user_id,
  1628. firstname,
  1629. lastname,
  1630. official_code,
  1631. email,
  1632. username,
  1633. '' as group_name,
  1634. '' as group_id
  1635. FROM $TBL_USER u
  1636. )";
  1637. $sql_inner_join_tbl_user = null;
  1638. }
  1639. if (strpos($extra_where_conditions, 'group_none')) {
  1640. $extra_where_conditions = str_replace(
  1641. "AND ( group_id = 'group_none' )",
  1642. "AND ( group_id is null )",
  1643. $extra_where_conditions
  1644. );
  1645. $extra_where_conditions = str_replace(
  1646. "AND group_id = 'group_none'",
  1647. "AND ( group_id is null )",
  1648. $extra_where_conditions
  1649. );
  1650. $sql_inner_join_tbl_user = "
  1651. (
  1652. SELECT
  1653. u.user_id,
  1654. firstname,
  1655. lastname,
  1656. official_code,
  1657. email,
  1658. username,
  1659. g.name as group_name,
  1660. g.id as group_id
  1661. FROM $TBL_USER u
  1662. LEFT OUTER JOIN $TBL_GROUP_REL_USER gru
  1663. ON ( gru.user_id = u.user_id AND gru.c_id=".$course_id." )
  1664. LEFT OUTER JOIN $TBL_GROUP g
  1665. ON (gru.group_id = g.id AND g.c_id = ".$course_id.")
  1666. )";
  1667. }
  1668. // All
  1669. $is_empty_sql_inner_join_tbl_user = false;
  1670. if (empty($sql_inner_join_tbl_user)) {
  1671. $is_empty_sql_inner_join_tbl_user = true;
  1672. $sql_inner_join_tbl_user = "
  1673. (
  1674. SELECT u.user_id, firstname, lastname, email, username, ' ' as group_name, '' as group_id, official_code
  1675. FROM $TBL_USER u
  1676. WHERE u.status NOT IN(".api_get_users_status_ignored_in_reports('string').")
  1677. )";
  1678. }
  1679. $sqlFromOption = " , $TBL_GROUP_REL_USER AS gru ";
  1680. $sqlWhereOption = " AND gru.c_id = ".$course_id." AND gru.user_id = user.user_id ";
  1681. $first_and_last_name = api_is_western_name_order() ? "firstname, lastname" : "lastname, firstname";
  1682. if ($get_count) {
  1683. $sql_select = "SELECT count(te.exe_id) ";
  1684. } else {
  1685. $sql_select = "SELECT DISTINCT
  1686. user_id,
  1687. $first_and_last_name,
  1688. official_code,
  1689. ce.title,
  1690. username,
  1691. te.exe_result,
  1692. te.exe_weighting,
  1693. te.exe_date,
  1694. te.exe_id,
  1695. email as exemail,
  1696. te.start_date,
  1697. ce.expired_time,
  1698. steps_counter,
  1699. exe_user_id,
  1700. te.exe_duration,
  1701. te.status as completion_status,
  1702. propagate_neg,
  1703. revised,
  1704. group_name,
  1705. group_id,
  1706. orig_lp_id,
  1707. te.user_ip";
  1708. }
  1709. $sql = " $sql_select
  1710. FROM $TBL_EXERCICES AS ce
  1711. INNER JOIN $sql_inner_join_tbl_track_exercices AS te
  1712. ON (te.exe_exo_id = ce.id)
  1713. INNER JOIN $sql_inner_join_tbl_user AS user
  1714. ON (user.user_id = exe_user_id)
  1715. WHERE
  1716. te.c_id = ".$course_id." $session_id_and AND
  1717. ce.active <>-1 AND
  1718. ce.c_id = ".$course_id."
  1719. $exercise_where
  1720. $extra_where_conditions
  1721. ";
  1722. // sql for hotpotatoes tests for teacher / tutor view
  1723. if ($get_count) {
  1724. $hpsql_select = "SELECT count(username)";
  1725. } else {
  1726. $hpsql_select = "SELECT
  1727. $first_and_last_name ,
  1728. username,
  1729. official_code,
  1730. tth.exe_name,
  1731. tth.exe_result ,
  1732. tth.exe_weighting,
  1733. tth.exe_date";
  1734. }
  1735. $hpsql = " $hpsql_select
  1736. FROM
  1737. $TBL_TRACK_HOTPOTATOES tth,
  1738. $TBL_USER user
  1739. $sqlFromOption
  1740. WHERE
  1741. user.user_id=tth.exe_user_id
  1742. AND tth.c_id = ".$course_id."
  1743. $hotpotatoe_where
  1744. $sqlWhereOption
  1745. AND user.status NOT IN(".api_get_users_status_ignored_in_reports('string').")
  1746. ORDER BY
  1747. tth.c_id ASC,
  1748. tth.exe_date DESC";
  1749. }
  1750. if ($get_count) {
  1751. $resx = Database::query($sql);
  1752. $rowx = Database::fetch_row($resx, 'ASSOC');
  1753. return $rowx[0];
  1754. }
  1755. $teacher_list = CourseManager::get_teacher_list_from_course_code(
  1756. $courseCode
  1757. );
  1758. $teacher_id_list = array();
  1759. if (!empty($teacher_list)) {
  1760. foreach ($teacher_list as $teacher) {
  1761. $teacher_id_list[] = $teacher['user_id'];
  1762. }
  1763. }
  1764. $list_info = array();
  1765. // Simple exercises
  1766. if (empty($hotpotatoe_where)) {
  1767. $column = !empty($column) ? Database::escape_string($column) : null;
  1768. $from = intval($from);
  1769. $number_of_items = intval($number_of_items);
  1770. if (!empty($column)) {
  1771. $sql .= " ORDER BY $column $direction ";
  1772. }
  1773. $sql .= " LIMIT $from, $number_of_items";
  1774. $results = array();
  1775. $resx = Database::query($sql);
  1776. while ($rowx = Database::fetch_array($resx, 'ASSOC')) {
  1777. $results[] = $rowx;
  1778. }
  1779. $group_list = GroupManager::get_group_list(null, $courseCode);
  1780. $clean_group_list = array();
  1781. if (!empty($group_list)) {
  1782. foreach ($group_list as $group) {
  1783. $clean_group_list[$group['id']] = $group['name'];
  1784. }
  1785. }
  1786. $lp_list_obj = new LearnpathList(api_get_user_id());
  1787. $lp_list = $lp_list_obj->get_flat_list();
  1788. $oldIds = array_column($lp_list, 'lp_old_id', 'iid');
  1789. if (is_array($results)) {
  1790. $users_array_id = array();
  1791. $from_gradebook = false;
  1792. if (isset($_GET['gradebook']) && $_GET['gradebook'] == 'view') {
  1793. $from_gradebook = true;
  1794. }
  1795. $sizeof = count($results);
  1796. $user_list_id = array();
  1797. $locked = api_resource_is_locked_by_gradebook(
  1798. $exercise_id,
  1799. LINK_EXERCISE
  1800. );
  1801. $timeNow = strtotime(api_get_utc_datetime());
  1802. // Looping results
  1803. for ($i = 0; $i < $sizeof; $i++) {
  1804. $revised = $results[$i]['revised'];
  1805. if ($results[$i]['completion_status'] == 'incomplete') {
  1806. // If the exercise was incomplete, we need to determine
  1807. // if it is still into the time allowed, or if its
  1808. // allowed time has expired and it can be closed
  1809. // (it's "unclosed")
  1810. $minutes = $results[$i]['expired_time'];
  1811. if ($minutes == 0) {
  1812. // There's no time limit, so obviously the attempt
  1813. // can still be "ongoing", but the teacher should
  1814. // be able to choose to close it, so mark it as
  1815. // "unclosed" instead of "ongoing"
  1816. $revised = 2;
  1817. } else {
  1818. $allowedSeconds = $minutes * 60;
  1819. $timeAttemptStarted = strtotime($results[$i]['start_date']);
  1820. $secondsSinceStart = $timeNow - $timeAttemptStarted;
  1821. if ($secondsSinceStart > $allowedSeconds) {
  1822. $revised = 2; // mark as "unclosed"
  1823. } else {
  1824. $revised = 3; // mark as "ongoing"
  1825. }
  1826. }
  1827. }
  1828. if ($from_gradebook && ($is_allowedToEdit)) {
  1829. if (in_array(
  1830. $results[$i]['username'].$results[$i]['firstname'].$results[$i]['lastname'],
  1831. $users_array_id
  1832. )) {
  1833. continue;
  1834. }
  1835. $users_array_id[] = $results[$i]['username'].$results[$i]['firstname'].$results[$i]['lastname'];
  1836. }
  1837. $lp_obj = isset($results[$i]['orig_lp_id']) && isset($lp_list[$results[$i]['orig_lp_id']]) ? $lp_list[$results[$i]['orig_lp_id']] : null;
  1838. if (empty($lp_obj)) {
  1839. // Try to get the old id (id instead of iid)
  1840. $lpNewId = isset($results[$i]['orig_lp_id']) && isset($oldIds[$results[$i]['orig_lp_id']]) ? $oldIds[$results[$i]['orig_lp_id']] : null;
  1841. if ($lpNewId) {
  1842. $lp_obj = isset($lp_list[$lpNewId]) ? $lp_list[$lpNewId] : null;
  1843. }
  1844. }
  1845. $lp_name = null;
  1846. if ($lp_obj) {
  1847. $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq().'&action=view&lp_id='.$results[$i]['orig_lp_id'];
  1848. $lp_name = Display::url(
  1849. $lp_obj['lp_name'],
  1850. $url,
  1851. array('target' => '_blank')
  1852. );
  1853. }
  1854. // Add all groups by user
  1855. $group_name_list = null;
  1856. if ($is_empty_sql_inner_join_tbl_user) {
  1857. $group_list = GroupManager::get_group_ids(
  1858. api_get_course_int_id(),
  1859. $results[$i]['user_id']
  1860. );
  1861. foreach ($group_list as $id) {
  1862. $group_name_list .= $clean_group_list[$id].'<br/>';
  1863. }
  1864. $results[$i]['group_name'] = $group_name_list;
  1865. }
  1866. $results[$i]['exe_duration'] = !empty($results[$i]['exe_duration']) ? round($results[$i]['exe_duration'] / 60) : 0;
  1867. $user_list_id[] = $results[$i]['exe_user_id'];
  1868. $id = $results[$i]['exe_id'];
  1869. $dt = api_convert_and_format_date($results[$i]['exe_weighting']);
  1870. // we filter the results if we have the permission to
  1871. if (isset($results[$i]['results_disabled'])) {
  1872. $result_disabled = intval(
  1873. $results[$i]['results_disabled']
  1874. );
  1875. } else {
  1876. $result_disabled = 0;
  1877. }
  1878. if ($result_disabled == 0) {
  1879. $my_res = $results[$i]['exe_result'];
  1880. $my_total = $results[$i]['exe_weighting'];
  1881. $results[$i]['start_date'] = api_get_local_time(
  1882. $results[$i]['start_date']
  1883. );
  1884. $results[$i]['exe_date'] = api_get_local_time(
  1885. $results[$i]['exe_date']
  1886. );
  1887. if (!$results[$i]['propagate_neg'] && $my_res < 0) {
  1888. $my_res = 0;
  1889. }
  1890. $score = self::show_score($my_res, $my_total);
  1891. $actions = '<div class="pull-right">';
  1892. if ($is_allowedToEdit) {
  1893. if (isset($teacher_id_list)) {
  1894. if (in_array(
  1895. $results[$i]['exe_user_id'],
  1896. $teacher_id_list
  1897. )) {
  1898. $actions .= Display::return_icon(
  1899. 'teacher.png',
  1900. get_lang('Teacher')
  1901. );
  1902. }
  1903. }
  1904. $revisedLabel = '';
  1905. switch ($revised) {
  1906. case 0:
  1907. $actions .= "<a href='exercise_show.php?".api_get_cidreq()."&action=qualify&id=$id'>".
  1908. Display:: return_icon(
  1909. 'quiz.png',
  1910. get_lang('Qualify')
  1911. );
  1912. $actions .= '</a>';
  1913. $revisedLabel = Display::label(
  1914. get_lang('NotValidated'),
  1915. 'info'
  1916. );
  1917. break;
  1918. case 1:
  1919. $actions .= "<a href='exercise_show.php?".api_get_cidreq()."&action=edit&id=$id'>".
  1920. Display:: return_icon(
  1921. 'edit.png',
  1922. get_lang('Edit'),
  1923. array(),
  1924. ICON_SIZE_SMALL
  1925. );
  1926. $actions .= '</a>';
  1927. $revisedLabel = Display::label(
  1928. get_lang('Validated'),
  1929. 'success'
  1930. );
  1931. break;
  1932. case 2: //finished but not marked as such
  1933. $actions .= '<a href="exercise_report.php?'
  1934. . api_get_cidreq()
  1935. . '&exerciseId='
  1936. . $exercise_id
  1937. . '&a=close&id='
  1938. . $id
  1939. . '">'.
  1940. Display:: return_icon(
  1941. 'lock.png',
  1942. get_lang('MarkAttemptAsClosed'),
  1943. array(),
  1944. ICON_SIZE_SMALL
  1945. );
  1946. $actions .= '</a>';
  1947. $revisedLabel = Display::label(
  1948. get_lang('Unclosed'),
  1949. 'warning'
  1950. );
  1951. break;
  1952. case 3: //still ongoing
  1953. $actions .= "".
  1954. Display:: return_icon(
  1955. 'clock.png',
  1956. get_lang('AttemptStillOngoingPleaseWait'),
  1957. array(),
  1958. ICON_SIZE_SMALL
  1959. );
  1960. $actions .= '';
  1961. $revisedLabel = Display::label(
  1962. get_lang('Ongoing'),
  1963. 'danger'
  1964. );
  1965. break;
  1966. }
  1967. if ($filter == 2) {
  1968. $actions .= ' <a href="exercise_history.php?'.api_get_cidreq().'&exe_id='.$id.'">'.
  1969. Display:: return_icon(
  1970. 'history.png',
  1971. get_lang('ViewHistoryChange')
  1972. ).'</a>';
  1973. }
  1974. //Admin can always delete the attempt
  1975. if (($locked == false || api_is_platform_admin()) && !api_is_student_boss()) {
  1976. $ip = TrackingUserLog::get_ip_from_user_event(
  1977. $results[$i]['exe_user_id'],
  1978. api_get_utc_datetime(),
  1979. false
  1980. );
  1981. $actions .= '<a href="http://www.whatsmyip.org/ip-geo-location/?ip='.$ip.'" target="_blank">'
  1982. . Display::return_icon('info.png', $ip)
  1983. .'</a>';
  1984. $recalculateUrl = api_get_path(WEB_CODE_PATH).'exercise/recalculate.php?'.
  1985. api_get_cidreq().'&'.
  1986. http_build_query([
  1987. 'id' => $id,
  1988. 'exercise' => $exercise_id,
  1989. 'user' => $results[$i]['exe_user_id']
  1990. ]);
  1991. $actions .= Display::url(
  1992. Display::return_icon('reload.png', get_lang('RecalculateResults')),
  1993. $recalculateUrl,
  1994. [
  1995. 'data-exercise' => $exercise_id,
  1996. 'data-user' => $results[$i]['exe_user_id'],
  1997. 'data-id' => $id,
  1998. 'class' => 'exercise-recalculate'
  1999. ]
  2000. );
  2001. $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.'"
  2002. onclick="javascript:if(!confirm(\'' . sprintf(
  2003. get_lang('DeleteAttempt'),
  2004. $results[$i]['username'],
  2005. $dt
  2006. ).'\')) return false;">'.Display:: return_icon(
  2007. 'delete.png',
  2008. get_lang('Delete')
  2009. ).'</a>';
  2010. $delete_link = utf8_encode($delete_link);
  2011. if (api_is_drh() && !api_is_platform_admin()) {
  2012. $delete_link = null;
  2013. }
  2014. if ($revised == 3) {
  2015. $delete_link = null;
  2016. }
  2017. $actions .= $delete_link;
  2018. }
  2019. } else {
  2020. $attempt_url = api_get_path(WEB_CODE_PATH).'exercise/result.php?'.api_get_cidreq().'&id='.$results[$i]['exe_id'].'&id_session='.$sessionId;
  2021. $attempt_link = Display::url(
  2022. get_lang('Show'),
  2023. $attempt_url,
  2024. [
  2025. 'class' => 'ajax btn btn-default',
  2026. 'data-title' => get_lang('Show')
  2027. ]
  2028. );
  2029. $actions .= $attempt_link;
  2030. }
  2031. $actions .= '</div>';
  2032. $results[$i]['id'] = $results[$i]['exe_id'];
  2033. if ($is_allowedToEdit) {
  2034. $results[$i]['status'] = $revisedLabel;
  2035. $results[$i]['score'] = $score;
  2036. $results[$i]['lp'] = $lp_name;
  2037. $results[$i]['actions'] = $actions;
  2038. $list_info[] = $results[$i];
  2039. } else {
  2040. $results[$i]['status'] = $revisedLabel;
  2041. $results[$i]['score'] = $score;
  2042. $results[$i]['actions'] = $actions;
  2043. $list_info[] = $results[$i];
  2044. }
  2045. }
  2046. }
  2047. }
  2048. } else {
  2049. $hpresults = StatsUtils::getManyResultsXCol($hpsql, 6);
  2050. // Print HotPotatoes test results.
  2051. if (is_array($hpresults)) {
  2052. for ($i = 0; $i < sizeof($hpresults); $i++) {
  2053. $hp_title = GetQuizName($hpresults[$i][3], $documentPath);
  2054. if ($hp_title == '') {
  2055. $hp_title = basename($hpresults[$i][3]);
  2056. }
  2057. $hp_date = api_get_local_time(
  2058. $hpresults[$i][6],
  2059. null,
  2060. date_default_timezone_get()
  2061. );
  2062. $hp_result = round(($hpresults[$i][4] / ($hpresults[$i][5] != 0 ? $hpresults[$i][5] : 1)) * 100, 2)
  2063. .'% ('.$hpresults[$i][4].' / '.$hpresults[$i][5].')';
  2064. if ($is_allowedToEdit) {
  2065. $list_info[] = array(
  2066. $hpresults[$i][0],
  2067. $hpresults[$i][1],
  2068. $hpresults[$i][2],
  2069. '',
  2070. $hp_title,
  2071. '-',
  2072. $hp_date,
  2073. $hp_result,
  2074. '-'
  2075. );
  2076. } else {
  2077. $list_info[] = array(
  2078. $hp_title,
  2079. '-',
  2080. $hp_date,
  2081. $hp_result,
  2082. '-'
  2083. );
  2084. }
  2085. }
  2086. }
  2087. }
  2088. return $list_info;
  2089. }
  2090. /**
  2091. * Converts the score with the exercise_max_note and exercise_min_score
  2092. * the platform settings + formats the results using the float_format function
  2093. *
  2094. * @param float $score
  2095. * @param float $weight
  2096. * @param bool $show_percentage show percentage or not
  2097. * @param bool $use_platform_settings use or not the platform settings
  2098. * @param bool $show_only_percentage
  2099. * @return string an html with the score modified
  2100. */
  2101. public static function show_score(
  2102. $score,
  2103. $weight,
  2104. $show_percentage = true,
  2105. $use_platform_settings = true,
  2106. $show_only_percentage = false
  2107. ) {
  2108. if (is_null($score) && is_null($weight)) {
  2109. return '-';
  2110. }
  2111. $max_note = api_get_setting('exercise_max_score');
  2112. $min_note = api_get_setting('exercise_min_score');
  2113. if ($use_platform_settings) {
  2114. if ($max_note != '' && $min_note != '') {
  2115. if (!empty($weight) && intval($weight) != 0) {
  2116. $score = $min_note + ($max_note - $min_note) * $score / $weight;
  2117. } else {
  2118. $score = $min_note;
  2119. }
  2120. $weight = $max_note;
  2121. }
  2122. }
  2123. $percentage = (100 * $score) / ($weight != 0 ? $weight : 1);
  2124. // Formats values
  2125. $percentage = float_format($percentage, 1);
  2126. $score = float_format($score, 1);
  2127. $weight = float_format($weight, 1);
  2128. $html = null;
  2129. if ($show_percentage) {
  2130. $parent = '('.$score.' / '.$weight.')';
  2131. $html = $percentage."% $parent";
  2132. if ($show_only_percentage) {
  2133. $html = $percentage."% ";
  2134. }
  2135. } else {
  2136. $html = $score.' / '.$weight;
  2137. }
  2138. $html = Display::span($html, array('class' => 'score_exercise'));
  2139. return $html;
  2140. }
  2141. /**
  2142. * @param float $score
  2143. * @param float $weight
  2144. * @param string $pass_percentage
  2145. * @return bool
  2146. */
  2147. public static function isSuccessExerciseResult($score, $weight, $pass_percentage)
  2148. {
  2149. $percentage = float_format(
  2150. ($score / ($weight != 0 ? $weight : 1)) * 100,
  2151. 1
  2152. );
  2153. if (isset($pass_percentage) && !empty($pass_percentage)) {
  2154. if ($percentage >= $pass_percentage) {
  2155. return true;
  2156. }
  2157. }
  2158. return false;
  2159. }
  2160. /**
  2161. * @param float $score
  2162. * @param float $weight
  2163. * @param string $pass_percentage
  2164. * @return string
  2165. */
  2166. public static function showSuccessMessage($score, $weight, $pass_percentage)
  2167. {
  2168. $res = '';
  2169. if (self::isPassPercentageEnabled($pass_percentage)) {
  2170. $isSuccess = self::isSuccessExerciseResult(
  2171. $score,
  2172. $weight,
  2173. $pass_percentage
  2174. );
  2175. if ($isSuccess) {
  2176. $html = get_lang('CongratulationsYouPassedTheTest');
  2177. $icon = Display::return_icon(
  2178. 'completed.png',
  2179. get_lang('Correct'),
  2180. array(),
  2181. ICON_SIZE_MEDIUM
  2182. );
  2183. } else {
  2184. //$html .= Display::return_message(get_lang('YouDidNotReachTheMinimumScore'), 'warning');
  2185. $html = get_lang('YouDidNotReachTheMinimumScore');
  2186. $icon = Display::return_icon(
  2187. 'warning.png',
  2188. get_lang('Wrong'),
  2189. array(),
  2190. ICON_SIZE_MEDIUM
  2191. );
  2192. }
  2193. $html = Display::tag('h4', $html);
  2194. $html .= Display::tag(
  2195. 'h5',
  2196. $icon,
  2197. array('style' => 'width:40px; padding:2px 10px 0px 0px')
  2198. );
  2199. $res = $html;
  2200. }
  2201. return $res;
  2202. }
  2203. /**
  2204. * Return true if pass_pourcentage activated (we use the pass pourcentage feature
  2205. * return false if pass_percentage = 0 (we don't use the pass pourcentage feature
  2206. * @param $value
  2207. * @return boolean
  2208. * In this version, pass_percentage and show_success_message are disabled if
  2209. * pass_percentage is set to 0
  2210. */
  2211. public static function isPassPercentageEnabled($value)
  2212. {
  2213. return $value > 0;
  2214. }
  2215. /**
  2216. * Converts a numeric value in a percentage example 0.66666 to 66.67 %
  2217. * @param $value
  2218. * @return float Converted number
  2219. */
  2220. public static function convert_to_percentage($value)
  2221. {
  2222. $return = '-';
  2223. if ($value != '') {
  2224. $return = float_format($value * 100, 1).' %';
  2225. }
  2226. return $return;
  2227. }
  2228. /**
  2229. * Converts a score/weight values to the platform scale
  2230. * @param float $score
  2231. * @param float $weight
  2232. * @deprecated seem not to be used
  2233. * @return float the score rounded converted to the new range
  2234. */
  2235. public static function convert_score($score, $weight)
  2236. {
  2237. $max_note = api_get_setting('exercise_max_score');
  2238. $min_note = api_get_setting('exercise_min_score');
  2239. if ($score != '' && $weight != '') {
  2240. if ($max_note != '' && $min_note != '') {
  2241. if (!empty($weight)) {
  2242. $score = $min_note + ($max_note - $min_note) * $score / $weight;
  2243. } else {
  2244. $score = $min_note;
  2245. }
  2246. }
  2247. }
  2248. $score_rounded = float_format($score, 1);
  2249. return $score_rounded;
  2250. }
  2251. /**
  2252. * Getting all active exercises from a course from a session
  2253. * (if a session_id is provided we will show all the exercises in the course +
  2254. * all exercises in the session)
  2255. * @param array $course_info
  2256. * @param int $session_id
  2257. * @param boolean $check_publication_dates
  2258. * @param string $search Search exercise name
  2259. * @param boolean $search_all_sessions Search exercises in all sessions
  2260. * @param int 0 = only inactive exercises
  2261. * 1 = only active exercises,
  2262. * 2 = all exercises
  2263. * 3 = active <> -1
  2264. * @return array array with exercise data
  2265. */
  2266. public static function get_all_exercises(
  2267. $course_info = null,
  2268. $session_id = 0,
  2269. $check_publication_dates = false,
  2270. $search = '',
  2271. $search_all_sessions = false,
  2272. $active = 2
  2273. ) {
  2274. $course_id = api_get_course_int_id();
  2275. if (!empty($course_info) && !empty($course_info['real_id'])) {
  2276. $course_id = $course_info['real_id'];
  2277. }
  2278. if ($session_id == -1) {
  2279. $session_id = 0;
  2280. }
  2281. $now = api_get_utc_datetime();
  2282. $time_conditions = '';
  2283. if ($check_publication_dates) {
  2284. //start and end are set
  2285. $time_conditions = " AND ((start_time <> '' AND start_time < '$now' AND end_time <> '' AND end_time > '$now' ) OR ";
  2286. // only start is set
  2287. $time_conditions .= " (start_time <> '' AND start_time < '$now' AND end_time is NULL) OR ";
  2288. // only end is set
  2289. $time_conditions .= " (start_time IS NULL AND end_time <> '' AND end_time > '$now') OR ";
  2290. // nothing is set
  2291. $time_conditions .= " (start_time IS NULL AND end_time IS NULL)) ";
  2292. }
  2293. $needle_where = !empty($search) ? " AND title LIKE '?' " : '';
  2294. $needle = !empty($search) ? "%".$search."%" : '';
  2295. // Show courses by active status
  2296. $active_sql = '';
  2297. if ($active == 3) {
  2298. $active_sql = ' active <> -1 AND';
  2299. } else {
  2300. if ($active != 2) {
  2301. $active_sql = sprintf(' active = %d AND', $active);
  2302. }
  2303. }
  2304. if ($search_all_sessions == true) {
  2305. $conditions = array(
  2306. 'where' => array(
  2307. $active_sql.' c_id = ? '.$needle_where.$time_conditions => array(
  2308. $course_id,
  2309. $needle
  2310. )
  2311. ),
  2312. 'order' => 'title'
  2313. );
  2314. } else {
  2315. if ($session_id == 0) {
  2316. $conditions = array(
  2317. 'where' => array(
  2318. $active_sql.' session_id = ? AND c_id = ? '.$needle_where.$time_conditions => array(
  2319. $session_id,
  2320. $course_id,
  2321. $needle
  2322. )
  2323. ),
  2324. 'order' => 'title'
  2325. );
  2326. } else {
  2327. $conditions = array(
  2328. 'where' => array(
  2329. $active_sql.' (session_id = 0 OR session_id = ? ) AND c_id = ? '.$needle_where.$time_conditions => array(
  2330. $session_id,
  2331. $course_id,
  2332. $needle
  2333. )
  2334. ),
  2335. 'order' => 'title'
  2336. );
  2337. }
  2338. }
  2339. $table = Database::get_course_table(TABLE_QUIZ_TEST);
  2340. return Database::select('*', $table, $conditions);
  2341. }
  2342. /**
  2343. * Get exercise information by id
  2344. * @param int $exerciseId Exercise Id
  2345. * @param int $courseId The course ID (necessary as c_quiz.id is not unique)
  2346. * @return array Exercise info
  2347. */
  2348. public static function get_exercise_by_id($exerciseId = 0, $courseId = 0)
  2349. {
  2350. $table = Database::get_course_table(TABLE_QUIZ_TEST);
  2351. if (empty($courseId)) {
  2352. $courseId = api_get_course_int_id();
  2353. } else {
  2354. $courseId = intval($courseId);
  2355. }
  2356. $conditions = array(
  2357. 'where' => array(
  2358. 'id = ?' => array($exerciseId),
  2359. ' AND c_id = ? ' => $courseId
  2360. )
  2361. );
  2362. return Database::select('*', $table, $conditions);
  2363. }
  2364. /**
  2365. * Getting all exercises (active only or all)
  2366. * from a course from a session
  2367. * (if a session_id is provided we will show all the exercises in the
  2368. * course + all exercises in the session)
  2369. * @param array course data
  2370. * @param int session id
  2371. * @param int course c_id
  2372. * @param boolean $only_active_exercises
  2373. * @return array array with exercise data
  2374. * modified by Hubert Borderiou
  2375. */
  2376. public static function get_all_exercises_for_course_id(
  2377. $course_info = null,
  2378. $session_id = 0,
  2379. $course_id = 0,
  2380. $only_active_exercises = true
  2381. ) {
  2382. $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST);
  2383. if ($only_active_exercises) {
  2384. // Only active exercises.
  2385. $sql_active_exercises = "active = 1 AND ";
  2386. } else {
  2387. // Not only active means visible and invisible NOT deleted (-2)
  2388. $sql_active_exercises = "active IN (1, 0) AND ";
  2389. }
  2390. if ($session_id == -1) {
  2391. $session_id = 0;
  2392. }
  2393. $params = array(
  2394. $session_id,
  2395. $course_id
  2396. );
  2397. if ($session_id == 0) {
  2398. $conditions = array(
  2399. 'where' => array("$sql_active_exercises session_id = ? AND c_id = ?" => $params),
  2400. 'order' => 'title'
  2401. );
  2402. } else {
  2403. // All exercises
  2404. $conditions = array(
  2405. 'where' => array("$sql_active_exercises (session_id = 0 OR session_id = ? ) AND c_id=?" => $params),
  2406. 'order' => 'title'
  2407. );
  2408. }
  2409. return Database::select('*', $TBL_EXERCISES, $conditions);
  2410. }
  2411. /**
  2412. * Gets the position of the score based in a given score (result/weight)
  2413. * and the exe_id based in the user list
  2414. * (NO Exercises in LPs )
  2415. * @param float $my_score user score to be compared *attention*
  2416. * $my_score = score/weight and not just the score
  2417. * @param int $my_exe_id exe id of the exercise
  2418. * (this is necessary because if 2 students have the same score the one
  2419. * with the minor exe_id will have a best position, just to be fair and FIFO)
  2420. * @param int $exercise_id
  2421. * @param string $course_code
  2422. * @param int $session_id
  2423. * @param array $user_list
  2424. * @param bool $return_string
  2425. *
  2426. * @return int the position of the user between his friends in a course
  2427. * (or course within a session)
  2428. */
  2429. public static function get_exercise_result_ranking(
  2430. $my_score,
  2431. $my_exe_id,
  2432. $exercise_id,
  2433. $course_code,
  2434. $session_id = 0,
  2435. $user_list = array(),
  2436. $return_string = true
  2437. ) {
  2438. //No score given we return
  2439. if (is_null($my_score)) {
  2440. return '-';
  2441. }
  2442. if (empty($user_list)) {
  2443. return '-';
  2444. }
  2445. $best_attempts = array();
  2446. foreach ($user_list as $user_data) {
  2447. $user_id = $user_data['user_id'];
  2448. $best_attempts[$user_id] = self::get_best_attempt_by_user(
  2449. $user_id,
  2450. $exercise_id,
  2451. $course_code,
  2452. $session_id
  2453. );
  2454. }
  2455. if (empty($best_attempts)) {
  2456. return 1;
  2457. } else {
  2458. $position = 1;
  2459. $my_ranking = array();
  2460. foreach ($best_attempts as $user_id => $result) {
  2461. if (!empty($result['exe_weighting']) && intval(
  2462. $result['exe_weighting']
  2463. ) != 0
  2464. ) {
  2465. $my_ranking[$user_id] = $result['exe_result'] / $result['exe_weighting'];
  2466. } else {
  2467. $my_ranking[$user_id] = 0;
  2468. }
  2469. }
  2470. //if (!empty($my_ranking)) {
  2471. asort($my_ranking);
  2472. $position = count($my_ranking);
  2473. if (!empty($my_ranking)) {
  2474. foreach ($my_ranking as $user_id => $ranking) {
  2475. if ($my_score >= $ranking) {
  2476. if ($my_score == $ranking && isset($best_attempts[$user_id]['exe_id'])) {
  2477. $exe_id = $best_attempts[$user_id]['exe_id'];
  2478. if ($my_exe_id < $exe_id) {
  2479. $position--;
  2480. }
  2481. } else {
  2482. $position--;
  2483. }
  2484. }
  2485. }
  2486. }
  2487. //}
  2488. $return_value = array(
  2489. 'position' => $position,
  2490. 'count' => count($my_ranking)
  2491. );
  2492. if ($return_string) {
  2493. if (!empty($position) && !empty($my_ranking)) {
  2494. $return_value = $position.'/'.count($my_ranking);
  2495. } else {
  2496. $return_value = '-';
  2497. }
  2498. }
  2499. return $return_value;
  2500. }
  2501. }
  2502. /**
  2503. * Gets the position of the score based in a given score (result/weight) and the exe_id based in all attempts
  2504. * (NO Exercises in LPs ) old functionality by attempt
  2505. * @param float user score to be compared attention => score/weight
  2506. * @param int exe id of the exercise
  2507. * (this is necessary because if 2 students have the same score the one
  2508. * with the minor exe_id will have a best position, just to be fair and FIFO)
  2509. * @param int exercise id
  2510. * @param string course code
  2511. * @param int session id
  2512. * @param bool $return_string
  2513. * @return int the position of the user between his friends in a course (or course within a session)
  2514. */
  2515. public static function get_exercise_result_ranking_by_attempt(
  2516. $my_score,
  2517. $my_exe_id,
  2518. $exercise_id,
  2519. $courseId,
  2520. $session_id = 0,
  2521. $return_string = true
  2522. ) {
  2523. if (empty($session_id)) {
  2524. $session_id = 0;
  2525. }
  2526. if (is_null($my_score)) {
  2527. return '-';
  2528. }
  2529. $user_results = Event::get_all_exercise_results(
  2530. $exercise_id,
  2531. $courseId,
  2532. $session_id,
  2533. false
  2534. );
  2535. $position_data = array();
  2536. if (empty($user_results)) {
  2537. return 1;
  2538. } else {
  2539. $position = 1;
  2540. $my_ranking = array();
  2541. foreach ($user_results as $result) {
  2542. //print_r($result);
  2543. if (!empty($result['exe_weighting']) && intval(
  2544. $result['exe_weighting']
  2545. ) != 0
  2546. ) {
  2547. $my_ranking[$result['exe_id']] = $result['exe_result'] / $result['exe_weighting'];
  2548. } else {
  2549. $my_ranking[$result['exe_id']] = 0;
  2550. }
  2551. }
  2552. asort($my_ranking);
  2553. $position = count($my_ranking);
  2554. if (!empty($my_ranking)) {
  2555. foreach ($my_ranking as $exe_id => $ranking) {
  2556. if ($my_score >= $ranking) {
  2557. if ($my_score == $ranking) {
  2558. if ($my_exe_id < $exe_id) {
  2559. $position--;
  2560. }
  2561. } else {
  2562. $position--;
  2563. }
  2564. }
  2565. }
  2566. }
  2567. $return_value = array(
  2568. 'position' => $position,
  2569. 'count' => count($my_ranking)
  2570. );
  2571. if ($return_string) {
  2572. if (!empty($position) && !empty($my_ranking)) {
  2573. return $position.'/'.count($my_ranking);
  2574. }
  2575. }
  2576. return $return_value;
  2577. }
  2578. }
  2579. /**
  2580. * Get the best attempt in a exercise (NO Exercises in LPs )
  2581. * @param int $exercise_id
  2582. * @param int $courseId
  2583. * @param int $session_id
  2584. *
  2585. * @return array
  2586. */
  2587. public static function get_best_attempt_in_course($exercise_id, $courseId, $session_id)
  2588. {
  2589. $user_results = Event::get_all_exercise_results(
  2590. $exercise_id,
  2591. $courseId,
  2592. $session_id,
  2593. false
  2594. );
  2595. $best_score_data = array();
  2596. $best_score = 0;
  2597. if (!empty($user_results)) {
  2598. foreach ($user_results as $result) {
  2599. if (!empty($result['exe_weighting']) &&
  2600. intval($result['exe_weighting']) != 0
  2601. ) {
  2602. $score = $result['exe_result'] / $result['exe_weighting'];
  2603. if ($score >= $best_score) {
  2604. $best_score = $score;
  2605. $best_score_data = $result;
  2606. }
  2607. }
  2608. }
  2609. }
  2610. return $best_score_data;
  2611. }
  2612. /**
  2613. * Get the best score in a exercise (NO Exercises in LPs )
  2614. * @param int $user_id
  2615. * @param int $exercise_id
  2616. * @param int $courseId
  2617. * @param int $session_id
  2618. *
  2619. * @return array
  2620. */
  2621. public static function get_best_attempt_by_user(
  2622. $user_id,
  2623. $exercise_id,
  2624. $courseId,
  2625. $session_id
  2626. )
  2627. {
  2628. $user_results = Event::get_all_exercise_results(
  2629. $exercise_id,
  2630. $courseId,
  2631. $session_id,
  2632. false,
  2633. $user_id
  2634. );
  2635. $best_score_data = array();
  2636. $best_score = 0;
  2637. if (!empty($user_results)) {
  2638. foreach ($user_results as $result) {
  2639. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  2640. $score = $result['exe_result'] / $result['exe_weighting'];
  2641. if ($score >= $best_score) {
  2642. $best_score = $score;
  2643. $best_score_data = $result;
  2644. }
  2645. }
  2646. }
  2647. }
  2648. return $best_score_data;
  2649. }
  2650. /**
  2651. * Get average score (NO Exercises in LPs )
  2652. * @param int exercise id
  2653. * @param int $courseId
  2654. * @param int session id
  2655. * @return float Average score
  2656. */
  2657. public static function get_average_score($exercise_id, $courseId, $session_id)
  2658. {
  2659. $user_results = Event::get_all_exercise_results(
  2660. $exercise_id,
  2661. $courseId,
  2662. $session_id
  2663. );
  2664. $avg_score = 0;
  2665. if (!empty($user_results)) {
  2666. foreach ($user_results as $result) {
  2667. if (!empty($result['exe_weighting']) && intval(
  2668. $result['exe_weighting']
  2669. ) != 0
  2670. ) {
  2671. $score = $result['exe_result'] / $result['exe_weighting'];
  2672. $avg_score += $score;
  2673. }
  2674. }
  2675. $avg_score = float_format($avg_score / count($user_results), 1);
  2676. }
  2677. return $avg_score;
  2678. }
  2679. /**
  2680. * Get average score by score (NO Exercises in LPs )
  2681. * @param int exercise id
  2682. * @param int $courseId
  2683. * @param int session id
  2684. * @return float Average score
  2685. */
  2686. public static function get_average_score_by_course($courseId, $session_id)
  2687. {
  2688. $user_results = Event::get_all_exercise_results_by_course(
  2689. $courseId,
  2690. $session_id,
  2691. false
  2692. );
  2693. //echo $course_code.' - '.$session_id.'<br />';
  2694. $avg_score = 0;
  2695. if (!empty($user_results)) {
  2696. foreach ($user_results as $result) {
  2697. if (!empty($result['exe_weighting']) && intval(
  2698. $result['exe_weighting']
  2699. ) != 0
  2700. ) {
  2701. $score = $result['exe_result'] / $result['exe_weighting'];
  2702. $avg_score += $score;
  2703. }
  2704. }
  2705. //We asume that all exe_weighting
  2706. $avg_score = ($avg_score / count($user_results));
  2707. }
  2708. return $avg_score;
  2709. }
  2710. /**
  2711. * @param int $user_id
  2712. * @param int $courseId
  2713. * @param int $session_id
  2714. *
  2715. * @return float|int
  2716. */
  2717. public static function get_average_score_by_course_by_user(
  2718. $user_id,
  2719. $courseId,
  2720. $session_id
  2721. )
  2722. {
  2723. $user_results = Event::get_all_exercise_results_by_user(
  2724. $user_id,
  2725. $courseId,
  2726. $session_id
  2727. );
  2728. $avg_score = 0;
  2729. if (!empty($user_results)) {
  2730. foreach ($user_results as $result) {
  2731. if (!empty($result['exe_weighting']) && intval(
  2732. $result['exe_weighting']
  2733. ) != 0
  2734. ) {
  2735. $score = $result['exe_result'] / $result['exe_weighting'];
  2736. $avg_score += $score;
  2737. }
  2738. }
  2739. // We asumme that all exe_weighting
  2740. $avg_score = ($avg_score / count($user_results));
  2741. }
  2742. return $avg_score;
  2743. }
  2744. /**
  2745. * Get average score by score (NO Exercises in LPs )
  2746. * @param int exercise id
  2747. * @param int $courseId
  2748. * @param int session id
  2749. * @return float Best average score
  2750. */
  2751. public static function get_best_average_score_by_exercise(
  2752. $exercise_id,
  2753. $courseId,
  2754. $session_id,
  2755. $user_count
  2756. )
  2757. {
  2758. $user_results = Event::get_best_exercise_results_by_user(
  2759. $exercise_id,
  2760. $courseId,
  2761. $session_id
  2762. );
  2763. $avg_score = 0;
  2764. if (!empty($user_results)) {
  2765. foreach ($user_results as $result) {
  2766. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  2767. $score = $result['exe_result'] / $result['exe_weighting'];
  2768. $avg_score += $score;
  2769. }
  2770. }
  2771. //We asumme that all exe_weighting
  2772. //$avg_score = show_score( $avg_score / count($user_results) , $result['exe_weighting']);
  2773. //$avg_score = ($avg_score / count($user_results));
  2774. if (!empty($user_count)) {
  2775. $avg_score = float_format($avg_score / $user_count, 1) * 100;
  2776. } else {
  2777. $avg_score = 0;
  2778. }
  2779. }
  2780. return $avg_score;
  2781. }
  2782. /**
  2783. * @param string $course_code
  2784. * @param int $session_id
  2785. *
  2786. * @return array
  2787. */
  2788. public static function get_exercises_to_be_taken($course_code, $session_id)
  2789. {
  2790. $course_info = api_get_course_info($course_code);
  2791. $exercises = self::get_all_exercises($course_info, $session_id);
  2792. $result = array();
  2793. $now = time() + 15 * 24 * 60 * 60;
  2794. foreach ($exercises as $exercise_item) {
  2795. if (isset($exercise_item['end_time']) &&
  2796. !empty($exercise_item['end_time']) &&
  2797. api_strtotime($exercise_item['end_time'], 'UTC') < $now
  2798. ) {
  2799. $result[] = $exercise_item;
  2800. }
  2801. }
  2802. return $result;
  2803. }
  2804. /**
  2805. * Get student results (only in completed exercises) stats by question
  2806. * @param int $question_id
  2807. * @param int $exercise_id
  2808. * @param string $course_code
  2809. * @param int $session_id
  2810. *
  2811. **/
  2812. public static function get_student_stats_by_question(
  2813. $question_id,
  2814. $exercise_id,
  2815. $course_code,
  2816. $session_id
  2817. ) {
  2818. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  2819. $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  2820. $question_id = intval($question_id);
  2821. $exercise_id = intval($exercise_id);
  2822. $course_code = Database::escape_string($course_code);
  2823. $session_id = intval($session_id);
  2824. $courseId = api_get_course_int_id($course_code);
  2825. $sql = "SELECT MAX(marks) as max, MIN(marks) as min, AVG(marks) as average
  2826. FROM $track_exercises e
  2827. INNER JOIN $track_attempt a
  2828. ON (
  2829. a.exe_id = e.exe_id AND
  2830. e.c_id = a.c_id AND
  2831. e.session_id = a.session_id
  2832. )
  2833. WHERE
  2834. exe_exo_id = $exercise_id AND
  2835. a.c_id = $courseId AND
  2836. e.session_id = $session_id AND
  2837. question_id = $question_id AND
  2838. status = ''
  2839. LIMIT 1";
  2840. $result = Database::query($sql);
  2841. $return = array();
  2842. if ($result) {
  2843. $return = Database::fetch_array($result, 'ASSOC');
  2844. }
  2845. return $return;
  2846. }
  2847. /**
  2848. * Get the correct answer count for a fill blanks question
  2849. *
  2850. * @param int $question_id
  2851. * @param int $exercise_id
  2852. * @return int
  2853. */
  2854. public static function getNumberStudentsFillBlanksAnwserCount(
  2855. $question_id,
  2856. $exercise_id
  2857. )
  2858. {
  2859. $listStudentsId = [];
  2860. $listAllStudentInfo = CourseManager::get_student_list_from_course_code(
  2861. api_get_course_id(),
  2862. true
  2863. );
  2864. foreach ($listAllStudentInfo as $i => $listStudentInfo) {
  2865. $listStudentsId[] = $listStudentInfo['user_id'];
  2866. }
  2867. $listFillTheBlankResult = FillBlanks::getFillTheBlankTabResult(
  2868. $exercise_id,
  2869. $question_id,
  2870. $listStudentsId,
  2871. '1970-01-01',
  2872. '3000-01-01'
  2873. );
  2874. $arrayCount = [];
  2875. foreach ($listFillTheBlankResult as $resultCount) {
  2876. foreach ($resultCount as $index => $count) {
  2877. //this is only for declare the array index per answer
  2878. $arrayCount[$index] = 0;
  2879. }
  2880. }
  2881. foreach ($listFillTheBlankResult as $resultCount) {
  2882. foreach ($resultCount as $index => $count) {
  2883. $count = ($count === 0) ? 1 : 0;
  2884. $arrayCount[$index] += $count;
  2885. }
  2886. }
  2887. return $arrayCount;
  2888. }
  2889. /**
  2890. * @param int $question_id
  2891. * @param int $exercise_id
  2892. * @param string $course_code
  2893. * @param int $session_id
  2894. * @param string $questionType
  2895. * @return int
  2896. */
  2897. public static function get_number_students_question_with_answer_count(
  2898. $question_id,
  2899. $exercise_id,
  2900. $course_code,
  2901. $session_id,
  2902. $questionType = ''
  2903. ) {
  2904. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  2905. $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  2906. $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2907. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  2908. $courseUserSession = Database::get_main_table(
  2909. TABLE_MAIN_SESSION_COURSE_USER
  2910. );
  2911. $question_id = intval($question_id);
  2912. $exercise_id = intval($exercise_id);
  2913. $courseId = api_get_course_int_id($course_code);
  2914. $session_id = intval($session_id);
  2915. if ($questionType == FILL_IN_BLANKS) {
  2916. $listStudentsId = array();
  2917. $listAllStudentInfo = CourseManager::get_student_list_from_course_code(
  2918. api_get_course_id(),
  2919. true
  2920. );
  2921. foreach ($listAllStudentInfo as $i => $listStudentInfo) {
  2922. $listStudentsId[] = $listStudentInfo['user_id'];
  2923. }
  2924. $listFillTheBlankResult = FillBlanks::getFillTheBlankTabResult(
  2925. $exercise_id,
  2926. $question_id,
  2927. $listStudentsId,
  2928. '1970-01-01',
  2929. '3000-01-01'
  2930. );
  2931. return FillBlanks::getNbResultFillBlankAll($listFillTheBlankResult);
  2932. }
  2933. if (empty($session_id)) {
  2934. $courseCondition = "
  2935. INNER JOIN $courseUser cu
  2936. ON cu.c_id = c.id AND cu.user_id = exe_user_id";
  2937. $courseConditionWhere = " AND relation_type <> 2 AND cu.status = ".STUDENT;
  2938. } else {
  2939. $courseCondition = "
  2940. INNER JOIN $courseUserSession cu
  2941. ON cu.c_id = c.id AND cu.user_id = exe_user_id";
  2942. $courseConditionWhere = " AND cu.status = 0 ";
  2943. }
  2944. $sql = "SELECT DISTINCT exe_user_id
  2945. FROM $track_exercises e
  2946. INNER JOIN $track_attempt a
  2947. ON (
  2948. a.exe_id = e.exe_id AND
  2949. e.c_id = a.c_id AND
  2950. e.session_id = a.session_id
  2951. )
  2952. INNER JOIN $courseTable c
  2953. ON (c.id = a.c_id)
  2954. $courseCondition
  2955. WHERE
  2956. exe_exo_id = $exercise_id AND
  2957. a.c_id = $courseId AND
  2958. e.session_id = $session_id AND
  2959. question_id = $question_id AND
  2960. answer <> '0' AND
  2961. e.status = ''
  2962. $courseConditionWhere
  2963. ";
  2964. $result = Database::query($sql);
  2965. $return = 0;
  2966. if ($result) {
  2967. $return = Database::num_rows($result);
  2968. }
  2969. return $return;
  2970. }
  2971. /**
  2972. * @param int $answer_id
  2973. * @param int $question_id
  2974. * @param int $exercise_id
  2975. * @param string $course_code
  2976. * @param int $session_id
  2977. *
  2978. * @return int
  2979. */
  2980. public static function get_number_students_answer_hotspot_count(
  2981. $answer_id,
  2982. $question_id,
  2983. $exercise_id,
  2984. $course_code,
  2985. $session_id
  2986. )
  2987. {
  2988. $track_exercises = Database::get_main_table(
  2989. TABLE_STATISTIC_TRACK_E_EXERCISES
  2990. );
  2991. $track_hotspot = Database::get_main_table(
  2992. TABLE_STATISTIC_TRACK_E_HOTSPOT
  2993. );
  2994. $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2995. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  2996. $courseUserSession = Database::get_main_table(
  2997. TABLE_MAIN_SESSION_COURSE_USER
  2998. );
  2999. $question_id = intval($question_id);
  3000. $answer_id = intval($answer_id);
  3001. $exercise_id = intval($exercise_id);
  3002. $course_code = Database::escape_string($course_code);
  3003. $session_id = intval($session_id);
  3004. if (empty($session_id)) {
  3005. $courseCondition = "
  3006. INNER JOIN $courseUser cu
  3007. ON cu.c_id = c.id AND cu.user_id = exe_user_id";
  3008. $courseConditionWhere = " AND relation_type <> 2 AND cu.status = ".STUDENT;
  3009. } else {
  3010. $courseCondition = "
  3011. INNER JOIN $courseUserSession cu
  3012. ON cu.c_id = c.id AND cu.user_id = exe_user_id";
  3013. $courseConditionWhere = " AND cu.status = 0 ";
  3014. }
  3015. $sql = "SELECT DISTINCT exe_user_id
  3016. FROM $track_exercises e
  3017. INNER JOIN $track_hotspot a
  3018. ON (a.hotspot_exe_id = e.exe_id)
  3019. INNER JOIN $courseTable c
  3020. ON (hotspot_course_code = c.code)
  3021. $courseCondition
  3022. WHERE
  3023. exe_exo_id = $exercise_id AND
  3024. a.hotspot_course_code = '$course_code' AND
  3025. e.session_id = $session_id AND
  3026. hotspot_answer_id = $answer_id AND
  3027. hotspot_question_id = $question_id AND
  3028. hotspot_correct = 1 AND
  3029. e.status = ''
  3030. $courseConditionWhere
  3031. ";
  3032. $result = Database::query($sql);
  3033. $return = 0;
  3034. if ($result) {
  3035. $return = Database::num_rows($result);
  3036. }
  3037. return $return;
  3038. }
  3039. /**
  3040. * @param int $answer_id
  3041. * @param int $question_id
  3042. * @param int $exercise_id
  3043. * @param string $course_code
  3044. * @param int $session_id
  3045. * @param string $question_type
  3046. * @param string $correct_answer
  3047. * @param string $current_answer
  3048. * @return int
  3049. */
  3050. public static function get_number_students_answer_count(
  3051. $answer_id,
  3052. $question_id,
  3053. $exercise_id,
  3054. $course_code,
  3055. $session_id,
  3056. $question_type = null,
  3057. $correct_answer = null,
  3058. $current_answer = null
  3059. ) {
  3060. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  3061. $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  3062. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  3063. $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  3064. $courseUserSession = Database::get_main_table(
  3065. TABLE_MAIN_SESSION_COURSE_USER
  3066. );
  3067. $question_id = intval($question_id);
  3068. $answer_id = intval($answer_id);
  3069. $exercise_id = intval($exercise_id);
  3070. $courseId = api_get_course_int_id($course_code);
  3071. $course_code = Database::escape_string($course_code);
  3072. $session_id = intval($session_id);
  3073. switch ($question_type) {
  3074. case FILL_IN_BLANKS:
  3075. $answer_condition = "";
  3076. $select_condition = " e.exe_id, answer ";
  3077. break;
  3078. case MATCHING:
  3079. //no break
  3080. case MATCHING_DRAGGABLE:
  3081. //no break
  3082. default:
  3083. $answer_condition = " answer = $answer_id AND ";
  3084. $select_condition = " DISTINCT exe_user_id ";
  3085. }
  3086. if (empty($session_id)) {
  3087. $courseCondition = "
  3088. INNER JOIN $courseUser cu
  3089. ON cu.c_id = c.id AND cu.user_id = exe_user_id";
  3090. $courseConditionWhere = " AND relation_type <> 2 AND cu.status = ".STUDENT;
  3091. } else {
  3092. $courseCondition = "
  3093. INNER JOIN $courseUserSession cu
  3094. ON cu.c_id = a.c_id AND cu.user_id = exe_user_id";
  3095. $courseConditionWhere = " AND cu.status = 0 ";
  3096. }
  3097. $sql = "SELECT $select_condition
  3098. FROM $track_exercises e
  3099. INNER JOIN $track_attempt a
  3100. ON (
  3101. a.exe_id = e.exe_id AND
  3102. e.c_id = a.c_id AND
  3103. e.session_id = a.session_id
  3104. )
  3105. INNER JOIN $courseTable c
  3106. ON c.id = a.c_id
  3107. $courseCondition
  3108. WHERE
  3109. exe_exo_id = $exercise_id AND
  3110. a.c_id = $courseId AND
  3111. e.session_id = $session_id AND
  3112. $answer_condition
  3113. question_id = $question_id AND
  3114. e.status = ''
  3115. $courseConditionWhere
  3116. ";
  3117. $result = Database::query($sql);
  3118. $return = 0;
  3119. if ($result) {
  3120. $good_answers = 0;
  3121. switch ($question_type) {
  3122. case FILL_IN_BLANKS:
  3123. while ($row = Database::fetch_array($result, 'ASSOC')) {
  3124. $fill_blank = self::check_fill_in_blanks(
  3125. $correct_answer,
  3126. $row['answer'],
  3127. $current_answer
  3128. );
  3129. if (isset($fill_blank[$current_answer]) && $fill_blank[$current_answer] == 1) {
  3130. $good_answers++;
  3131. }
  3132. }
  3133. return $good_answers;
  3134. break;
  3135. case MATCHING:
  3136. //no break
  3137. case MATCHING_DRAGGABLE:
  3138. //no break
  3139. default:
  3140. $return = Database::num_rows($result);
  3141. }
  3142. }
  3143. return $return;
  3144. }
  3145. /**
  3146. * @param array $answer
  3147. * @param string $user_answer
  3148. * @return array
  3149. */
  3150. public static function check_fill_in_blanks($answer, $user_answer, $current_answer)
  3151. {
  3152. // the question is encoded like this
  3153. // [A] B [C] D [E] F::10,10,10@1
  3154. // number 1 before the "@" means that is a switchable fill in blank question
  3155. // [A] B [C] D [E] F::10,10,10@ or [A] B [C] D [E] F::10,10,10
  3156. // means that is a normal fill blank question
  3157. // first we explode the "::"
  3158. $pre_array = explode('::', $answer);
  3159. // is switchable fill blank or not
  3160. $last = count($pre_array) - 1;
  3161. $is_set_switchable = explode('@', $pre_array[$last]);
  3162. $switchable_answer_set = false;
  3163. if (isset ($is_set_switchable[1]) && $is_set_switchable[1] == 1) {
  3164. $switchable_answer_set = true;
  3165. }
  3166. $answer = '';
  3167. for ($k = 0; $k < $last; $k++) {
  3168. $answer .= $pre_array[$k];
  3169. }
  3170. // splits weightings that are joined with a comma
  3171. $answerWeighting = explode(',', $is_set_switchable[0]);
  3172. // we save the answer because it will be modified
  3173. //$temp = $answer;
  3174. $temp = $answer;
  3175. $answer = '';
  3176. $j = 0;
  3177. //initialise answer tags
  3178. $user_tags = $correct_tags = $real_text = array();
  3179. // the loop will stop at the end of the text
  3180. while (1) {
  3181. // quits the loop if there are no more blanks (detect '[')
  3182. if (($pos = api_strpos($temp, '[')) === false) {
  3183. // adds the end of the text
  3184. $answer = $temp;
  3185. $real_text[] = $answer;
  3186. break; //no more "blanks", quit the loop
  3187. }
  3188. // adds the piece of text that is before the blank
  3189. //and ends with '[' into a general storage array
  3190. $real_text[] = api_substr($temp, 0, $pos + 1);
  3191. $answer .= api_substr($temp, 0, $pos + 1);
  3192. //take the string remaining (after the last "[" we found)
  3193. $temp = api_substr($temp, $pos + 1);
  3194. // quit the loop if there are no more blanks, and update $pos to the position of next ']'
  3195. if (($pos = api_strpos($temp, ']')) === false) {
  3196. // adds the end of the text
  3197. $answer .= $temp;
  3198. break;
  3199. }
  3200. $str = $user_answer;
  3201. preg_match_all('#\[([^[]*)\]#', $str, $arr);
  3202. $str = str_replace('\r\n', '', $str);
  3203. $choices = $arr[1];
  3204. $choice = [];
  3205. $check = false;
  3206. $i = 0;
  3207. foreach ($choices as $item) {
  3208. if ($current_answer === $item) {
  3209. $check = true;
  3210. }
  3211. if ($check) {
  3212. $choice[] = $item;
  3213. $i++;
  3214. }
  3215. if ($i == 3) {
  3216. break;
  3217. }
  3218. }
  3219. $tmp = api_strrpos($choice[$j], ' / ');
  3220. if ($tmp !== false) {
  3221. $choice[$j] = api_substr($choice[$j], 0, $tmp);
  3222. }
  3223. $choice[$j] = trim($choice[$j]);
  3224. //Needed to let characters ' and " to work as part of an answer
  3225. $choice[$j] = stripslashes($choice[$j]);
  3226. $user_tags[] = api_strtolower($choice[$j]);
  3227. //put the contents of the [] answer tag into correct_tags[]
  3228. $correct_tags[] = api_strtolower(api_substr($temp, 0, $pos));
  3229. $j++;
  3230. $temp = api_substr($temp, $pos + 1);
  3231. }
  3232. $answer = '';
  3233. $real_correct_tags = $correct_tags;
  3234. $chosen_list = array();
  3235. $good_answer = array();
  3236. for ($i = 0; $i < count($real_correct_tags); $i++) {
  3237. if (!$switchable_answer_set) {
  3238. //needed to parse ' and " characters
  3239. $user_tags[$i] = stripslashes($user_tags[$i]);
  3240. if ($correct_tags[$i] == $user_tags[$i]) {
  3241. $good_answer[$correct_tags[$i]] = 1;
  3242. } elseif (!empty ($user_tags[$i])) {
  3243. $good_answer[$correct_tags[$i]] = 0;
  3244. } else {
  3245. $good_answer[$correct_tags[$i]] = 0;
  3246. }
  3247. } else {
  3248. // switchable fill in the blanks
  3249. if (in_array($user_tags[$i], $correct_tags)) {
  3250. $correct_tags = array_diff($correct_tags, $chosen_list);
  3251. $good_answer[$correct_tags[$i]] = 1;
  3252. } elseif (!empty ($user_tags[$i])) {
  3253. $good_answer[$correct_tags[$i]] = 0;
  3254. } else {
  3255. $good_answer[$correct_tags[$i]] = 0;
  3256. }
  3257. }
  3258. // adds the correct word, followed by ] to close the blank
  3259. $answer .= ' / <font color="green"><b>'.$real_correct_tags[$i].'</b></font>]';
  3260. if (isset ($real_text[$i + 1])) {
  3261. $answer .= $real_text[$i + 1];
  3262. }
  3263. }
  3264. return $good_answer;
  3265. }
  3266. /**
  3267. * @param int $exercise_id
  3268. * @param string $course_code
  3269. * @param int $session_id
  3270. * @return int
  3271. */
  3272. public static function get_number_students_finish_exercise(
  3273. $exercise_id,
  3274. $course_code,
  3275. $session_id
  3276. ) {
  3277. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  3278. $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  3279. $exercise_id = intval($exercise_id);
  3280. $course_code = Database::escape_string($course_code);
  3281. $session_id = intval($session_id);
  3282. $sql = "SELECT DISTINCT exe_user_id
  3283. FROM $track_exercises e
  3284. INNER JOIN $track_attempt a
  3285. ON (a.exe_id = e.exe_id)
  3286. WHERE
  3287. exe_exo_id = $exercise_id AND
  3288. course_code = '$course_code' AND
  3289. e.session_id = $session_id AND
  3290. status = ''";
  3291. $result = Database::query($sql);
  3292. $return = 0;
  3293. if ($result) {
  3294. $return = Database::num_rows($result);
  3295. }
  3296. return $return;
  3297. }
  3298. /**
  3299. * @param string $in_name is the name and the id of the <select>
  3300. * @param string $in_default default value for option
  3301. * @param string $in_onchange
  3302. * @return string the html code of the <select>
  3303. */
  3304. public static function displayGroupMenu($in_name, $in_default, $in_onchange = "")
  3305. {
  3306. // check the default value of option
  3307. $tabSelected = array($in_default => " selected='selected' ");
  3308. $res = "";
  3309. $res .= "<select name='$in_name' id='$in_name' onchange='".$in_onchange."' >";
  3310. $res .= "<option value='-1'".$tabSelected["-1"].">-- ".get_lang(
  3311. 'AllGroups'
  3312. )." --</option>";
  3313. $res .= "<option value='0'".$tabSelected["0"].">- ".get_lang(
  3314. 'NotInAGroup'
  3315. )." -</option>";
  3316. $tabGroups = GroupManager::get_group_list();
  3317. $currentCatId = 0;
  3318. for ($i = 0; $i < count($tabGroups); $i++) {
  3319. $tabCategory = GroupManager::get_category_from_group(
  3320. $tabGroups[$i]['iid']
  3321. );
  3322. if ($tabCategory["id"] != $currentCatId) {
  3323. $res .= "<option value='-1' disabled='disabled'>".$tabCategory["title"]."</option>";
  3324. $currentCatId = $tabCategory["id"];
  3325. }
  3326. $res .= "<option ".$tabSelected[$tabGroups[$i]["id"]]."style='margin-left:40px' value='".$tabGroups[$i]["id"]."'>".$tabGroups[$i]["name"]."</option>";
  3327. }
  3328. $res .= "</select>";
  3329. return $res;
  3330. }
  3331. /**
  3332. * @param int $exe_id
  3333. */
  3334. public static function create_chat_exercise_session($exe_id)
  3335. {
  3336. if (!isset($_SESSION['current_exercises'])) {
  3337. $_SESSION['current_exercises'] = array();
  3338. }
  3339. $_SESSION['current_exercises'][$exe_id] = true;
  3340. }
  3341. /**
  3342. * @param int $exe_id
  3343. */
  3344. public static function delete_chat_exercise_session($exe_id)
  3345. {
  3346. if (isset($_SESSION['current_exercises'])) {
  3347. $_SESSION['current_exercises'][$exe_id] = false;
  3348. }
  3349. }
  3350. /**
  3351. * Display the exercise results
  3352. * @param Exercise $objExercise
  3353. * @param int $exe_id
  3354. * @param bool $save_user_result save users results (true) or just show the results (false)
  3355. * @param string $remainingMessage
  3356. */
  3357. public static function displayQuestionListByAttempt(
  3358. $objExercise,
  3359. $exe_id,
  3360. $save_user_result = false,
  3361. $remainingMessage = ''
  3362. ) {
  3363. $origin = api_get_origin();
  3364. // Getting attempt info
  3365. $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id(
  3366. $exe_id
  3367. );
  3368. // Getting question list
  3369. $question_list = array();
  3370. if (!empty($exercise_stat_info['data_tracking'])) {
  3371. $question_list = explode(',', $exercise_stat_info['data_tracking']);
  3372. } else {
  3373. // Try getting the question list only if save result is off
  3374. if ($save_user_result == false) {
  3375. $question_list = $objExercise->get_validated_question_list();
  3376. }
  3377. if ($objExercise->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  3378. $question_list = $objExercise->get_validated_question_list();
  3379. }
  3380. }
  3381. $counter = 1;
  3382. $total_score = $total_weight = 0;
  3383. $exercise_content = null;
  3384. // Hide results
  3385. $show_results = false;
  3386. $show_only_score = false;
  3387. if ($objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS) {
  3388. $show_results = true;
  3389. }
  3390. if (in_array(
  3391. $objExercise->results_disabled,
  3392. array(
  3393. RESULT_DISABLE_SHOW_SCORE_ONLY,
  3394. RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES
  3395. )
  3396. )
  3397. ) {
  3398. $show_only_score = true;
  3399. }
  3400. // Not display expected answer, but score, and feedback
  3401. $show_all_but_expected_answer = false;
  3402. if ($objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_ONLY &&
  3403. $objExercise->feedback_type == EXERCISE_FEEDBACK_TYPE_END
  3404. ) {
  3405. $show_all_but_expected_answer = true;
  3406. $show_results = true;
  3407. $show_only_score = false;
  3408. }
  3409. $showTotalScoreAndUserChoicesInLastAttempt = true;
  3410. if ($objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  3411. $show_only_score = true;
  3412. $show_results = true;
  3413. if ($objExercise->attempts > 0) {
  3414. $attempts = Event::getExerciseResultsByUser(
  3415. api_get_user_id(),
  3416. $objExercise->id,
  3417. api_get_course_int_id(),
  3418. api_get_session_id(),
  3419. $exercise_stat_info['orig_lp_id'],
  3420. $exercise_stat_info['orig_lp_item_id'],
  3421. 'desc'
  3422. );
  3423. if ($attempts) {
  3424. $numberAttempts = count($attempts);
  3425. } else {
  3426. $numberAttempts = 0;
  3427. }
  3428. if ($save_user_result) {
  3429. $numberAttempts++;
  3430. }
  3431. if ($numberAttempts >= $objExercise->attempts) {
  3432. $show_results = true;
  3433. $show_only_score = false;
  3434. $showTotalScoreAndUserChoicesInLastAttempt = true;
  3435. } else {
  3436. $showTotalScoreAndUserChoicesInLastAttempt = false;
  3437. }
  3438. }
  3439. }
  3440. if ($show_results || $show_only_score) {
  3441. if (isset($exercise_stat_info['exe_user_id'])) {
  3442. $user_info = api_get_user_info($exercise_stat_info['exe_user_id']);
  3443. if ($user_info) {
  3444. // Shows exercise header
  3445. echo $objExercise->show_exercise_result_header(
  3446. $user_info,
  3447. api_convert_and_format_date(
  3448. $exercise_stat_info['start_date'],
  3449. DATE_TIME_FORMAT_LONG
  3450. ),
  3451. $exercise_stat_info['duration'],
  3452. $exercise_stat_info['user_ip']
  3453. );
  3454. }
  3455. }
  3456. }
  3457. // Display text when test is finished #4074 and for LP #4227
  3458. $end_of_message = $objExercise->selectTextWhenFinished();
  3459. if (!empty($end_of_message)) {
  3460. echo Display::return_message($end_of_message, 'normal', false);
  3461. echo "<div class='clear'>&nbsp;</div>";
  3462. }
  3463. $question_list_answers = array();
  3464. $media_list = array();
  3465. $category_list = array();
  3466. $loadChoiceFromSession = false;
  3467. $fromDatabase = true;
  3468. $exerciseResult = null;
  3469. $exerciseResultCoordinates = null;
  3470. $delineationResults = null;
  3471. if ($objExercise->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  3472. $loadChoiceFromSession = true;
  3473. $fromDatabase = false;
  3474. $exerciseResult = Session::read('exerciseResult');
  3475. $exerciseResultCoordinates = Session::read('exerciseResultCoordinates');
  3476. $delineationResults = Session::read('hotspot_delineation_result');
  3477. $delineationResults = isset($delineationResults[$objExercise->id]) ? $delineationResults[$objExercise->id] : null;
  3478. }
  3479. $countPendingQuestions = 0;
  3480. // Loop over all question to show results for each of them, one by one
  3481. if (!empty($question_list)) {
  3482. foreach ($question_list as $questionId) {
  3483. // creates a temporary Question object
  3484. $objQuestionTmp = Question::read($questionId);
  3485. // This variable came from exercise_submit_modal.php
  3486. ob_start();
  3487. $choice = null;
  3488. $delineationChoice = null;
  3489. if ($loadChoiceFromSession) {
  3490. $choice = isset($exerciseResult[$questionId]) ? $exerciseResult[$questionId] : null;
  3491. $delineationChoice = isset($delineationResults[$questionId]) ? $delineationResults[$questionId] : null;
  3492. }
  3493. // We're inside *one* question. Go through each possible answer for this question
  3494. $result = $objExercise->manage_answer(
  3495. $exe_id,
  3496. $questionId,
  3497. $choice,
  3498. 'exercise_result',
  3499. $exerciseResultCoordinates,
  3500. $save_user_result,
  3501. $fromDatabase,
  3502. $show_results,
  3503. $objExercise->selectPropagateNeg(),
  3504. $delineationChoice,
  3505. $showTotalScoreAndUserChoicesInLastAttempt
  3506. );
  3507. if (empty($result)) {
  3508. continue;
  3509. }
  3510. $total_score += $result['score'];
  3511. $total_weight += $result['weight'];
  3512. $question_list_answers[] = array(
  3513. 'question' => $result['open_question'],
  3514. 'answer' => $result['open_answer'],
  3515. 'answer_type' => $result['answer_type']
  3516. );
  3517. $my_total_score = $result['score'];
  3518. $my_total_weight = $result['weight'];
  3519. // Category report
  3520. $category_was_added_for_this_test = false;
  3521. if (isset($objQuestionTmp->category) && !empty($objQuestionTmp->category)) {
  3522. if (!isset($category_list[$objQuestionTmp->category]['score'])) {
  3523. $category_list[$objQuestionTmp->category]['score'] = 0;
  3524. }
  3525. if (!isset($category_list[$objQuestionTmp->category]['total'])) {
  3526. $category_list[$objQuestionTmp->category]['total'] = 0;
  3527. }
  3528. $category_list[$objQuestionTmp->category]['score'] += $my_total_score;
  3529. $category_list[$objQuestionTmp->category]['total'] += $my_total_weight;
  3530. $category_was_added_for_this_test = true;
  3531. }
  3532. if (isset($objQuestionTmp->category_list) && !empty($objQuestionTmp->category_list)) {
  3533. foreach ($objQuestionTmp->category_list as $category_id) {
  3534. $category_list[$category_id]['score'] += $my_total_score;
  3535. $category_list[$category_id]['total'] += $my_total_weight;
  3536. $category_was_added_for_this_test = true;
  3537. }
  3538. }
  3539. // No category for this question!
  3540. if ($category_was_added_for_this_test == false) {
  3541. if (!isset($category_list['none']['score'])) {
  3542. $category_list['none']['score'] = 0;
  3543. }
  3544. if (!isset($category_list['none']['total'])) {
  3545. $category_list['none']['total'] = 0;
  3546. }
  3547. $category_list['none']['score'] += $my_total_score;
  3548. $category_list['none']['total'] += $my_total_weight;
  3549. }
  3550. if ($objExercise->selectPropagateNeg() == 0 &&
  3551. $my_total_score < 0
  3552. ) {
  3553. $my_total_score = 0;
  3554. }
  3555. $comnt = null;
  3556. if ($show_results) {
  3557. $comnt = Event::get_comments($exe_id, $questionId);
  3558. if (!empty($comnt)) {
  3559. echo '<b>'.get_lang('Feedback').'</b>';
  3560. echo ExerciseLib::getFeedbackText($comnt);
  3561. }
  3562. }
  3563. if ($show_results) {
  3564. $score = [
  3565. 'result' => self::show_score(
  3566. $my_total_score,
  3567. $my_total_weight,
  3568. false,
  3569. true
  3570. ),
  3571. 'pass' => $my_total_score >= $my_total_weight ? true : false,
  3572. 'score' => $my_total_score,
  3573. 'weight' => $my_total_weight,
  3574. 'comments' => $comnt,
  3575. ];
  3576. } else {
  3577. $score = [];
  3578. }
  3579. if (in_array($objQuestionTmp->type, [FREE_ANSWER, ORAL_EXPRESSION])) {
  3580. $check = $objQuestionTmp->isQuestionWaitingReview($score);
  3581. if ($check === false) {
  3582. $countPendingQuestions++;
  3583. }
  3584. }
  3585. $contents = ob_get_clean();
  3586. $question_content = '';
  3587. if ($show_results) {
  3588. $question_content = '<div class="question_row_answer">';
  3589. // Shows question title an description
  3590. $question_content .= $objQuestionTmp->return_header(
  3591. $objExercise,
  3592. $counter,
  3593. $score
  3594. );
  3595. }
  3596. $counter++;
  3597. $question_content .= $contents;
  3598. if ($show_results) {
  3599. $question_content .= '</div>';
  3600. }
  3601. $exercise_content .= $question_content;
  3602. } // end foreach() block that loops over all questions
  3603. }
  3604. $total_score_text = null;
  3605. if ($show_results || $show_only_score) {
  3606. $total_score_text .= '<div class="question_row_score">';
  3607. $total_score_text .= self::getTotalScoreRibbon(
  3608. $objExercise,
  3609. $total_score,
  3610. $total_weight,
  3611. true,
  3612. $countPendingQuestions
  3613. );
  3614. $total_score_text .= '</div>';
  3615. }
  3616. if (!empty($category_list) && ($show_results || $show_only_score)) {
  3617. // Adding total
  3618. $category_list['total'] = array(
  3619. 'score' => $total_score,
  3620. 'total' => $total_weight
  3621. );
  3622. echo TestCategory::get_stats_table_by_attempt(
  3623. $objExercise->id,
  3624. $category_list
  3625. );
  3626. }
  3627. if ($show_all_but_expected_answer) {
  3628. $exercise_content .= "<div class='normal-message'>".get_lang(
  3629. "ExerciseWithFeedbackWithoutCorrectionComment"
  3630. )."</div>";
  3631. }
  3632. // Remove audio auto play from questions on results page - refs BT#7939
  3633. $exercise_content = preg_replace(
  3634. ['/autoplay[\=\".+\"]+/', '/autostart[\=\".+\"]+/'],
  3635. '',
  3636. $exercise_content
  3637. );
  3638. echo $total_score_text;
  3639. // Ofaj change BT#11784
  3640. if (!empty($objExercise->description)) {
  3641. echo Display::div($objExercise->description, array('class'=>'exercise_description'));
  3642. }
  3643. echo $exercise_content;
  3644. if (!$show_only_score) {
  3645. echo $total_score_text;
  3646. }
  3647. if (!empty($remainingMessage)) {
  3648. echo Display::return_message($remainingMessage, 'normal', false);
  3649. }
  3650. if ($save_user_result) {
  3651. // Tracking of results
  3652. if ($exercise_stat_info) {
  3653. $learnpath_id = $exercise_stat_info['orig_lp_id'];
  3654. $learnpath_item_id = $exercise_stat_info['orig_lp_item_id'];
  3655. $learnpath_item_view_id = $exercise_stat_info['orig_lp_item_view_id'];
  3656. if (api_is_allowed_to_session_edit()) {
  3657. Event::update_event_exercise(
  3658. $exercise_stat_info['exe_id'],
  3659. $objExercise->selectId(),
  3660. $total_score,
  3661. $total_weight,
  3662. api_get_session_id(),
  3663. $learnpath_id,
  3664. $learnpath_item_id,
  3665. $learnpath_item_view_id,
  3666. $exercise_stat_info['exe_duration'],
  3667. $question_list,
  3668. '',
  3669. array()
  3670. );
  3671. }
  3672. }
  3673. // Send notification at the end
  3674. if (!api_is_allowed_to_edit(null, true) &&
  3675. !api_is_excluded_user_type()
  3676. ) {
  3677. $objExercise->send_mail_notification_for_exam(
  3678. 'end',
  3679. $question_list_answers,
  3680. $origin,
  3681. $exe_id,
  3682. $total_score,
  3683. $total_weight
  3684. );
  3685. }
  3686. }
  3687. }
  3688. /**
  3689. * @param string $class
  3690. * @param string $scoreLabel
  3691. * @param string $result
  3692. *
  3693. * @return string
  3694. */
  3695. public static function getQuestionRibbon($class, $scoreLabel, $result, $array)
  3696. {
  3697. // ofaj
  3698. $html = null;
  3699. $hideLabel = api_get_configuration_value('exercise_hide_label');
  3700. $label = '<div class="rib rib-'.$class.'">
  3701. <h3>'.$scoreLabel.'</h3>
  3702. </div>
  3703. <h4>'.get_lang('Score').': '.$result.'</h4>';
  3704. if ($hideLabel === true) {
  3705. $answerUsed = (int)$array['used'];
  3706. $answerMissing = (int)$array['missing'] - $answerUsed;
  3707. for ($i = 1; $i <= $answerUsed; $i++) {
  3708. $html.= '<span class="score-img">'.Display::return_icon('attempt-check.png',null,null,ICON_SIZE_SMALL).'</span>';
  3709. }
  3710. for ($i = 1; $i <= $answerMissing; $i++) {
  3711. $html.= '<span class="score-img">'.Display::return_icon('attempt-nocheck.png',null,null,ICON_SIZE_SMALL).'</span>';
  3712. }
  3713. $label = '<div class="score-title">'.get_lang('CorrectAnswers').': '.$result.'</div>';
  3714. $label .= '<div class="score-limits">';
  3715. $label .= $html;
  3716. $label .= '</div>';
  3717. }
  3718. return '<div class="ribbon">
  3719. '.$label.'
  3720. </div>'
  3721. ;
  3722. }
  3723. /**
  3724. * @param Exercise $objExercise
  3725. * @param float $score
  3726. * @param float $weight
  3727. * @param bool $checkPassPercentage
  3728. * @param int $countPendingQuestions
  3729. * @return string
  3730. */
  3731. public static function getTotalScoreRibbon(
  3732. $objExercise,
  3733. $score,
  3734. $weight,
  3735. $checkPassPercentage = false,
  3736. $countPendingQuestions = 0
  3737. ) {
  3738. $passPercentage = $objExercise->selectPassPercentage();
  3739. $ribbon = '<div class="title-score">';
  3740. if ($checkPassPercentage) {
  3741. $isSuccess = self::isSuccessExerciseResult(
  3742. $score,
  3743. $weight,
  3744. $passPercentage
  3745. );
  3746. // Color the final test score if pass_percentage activated
  3747. $class = '';
  3748. if (self::isPassPercentageEnabled($passPercentage)) {
  3749. if ($isSuccess) {
  3750. $class = ' ribbon-total-success';
  3751. } else {
  3752. $class = ' ribbon-total-error';
  3753. }
  3754. }
  3755. $ribbon .= '<div class="total '.$class.'">';
  3756. } else {
  3757. $ribbon .= '<div class="total">';
  3758. }
  3759. $ribbon .= '<h3>'.get_lang('YourTotalScore').":&nbsp;";
  3760. $ribbon .= self::show_score($score, $weight, false, true);
  3761. $ribbon .= '</h3>';
  3762. $ribbon .= '</div>';
  3763. if ($checkPassPercentage) {
  3764. $ribbon .= self::showSuccessMessage(
  3765. $score,
  3766. $weight,
  3767. $passPercentage
  3768. );
  3769. }
  3770. $ribbon .= '</div>';
  3771. if (!empty($countPendingQuestions)) {
  3772. $ribbon .= '<br />';
  3773. $ribbon .= Display::return_message(
  3774. sprintf(
  3775. get_lang('TempScoreXQuestionsNotCorrectedYet'),
  3776. $countPendingQuestions
  3777. ),
  3778. 'warning'
  3779. );
  3780. }
  3781. return $ribbon;
  3782. }
  3783. /**
  3784. * @param int $countLetter
  3785. * @return mixed
  3786. */
  3787. public static function detectInputAppropriateClass($countLetter)
  3788. {
  3789. $limits = array(
  3790. 0 => 'input-mini',
  3791. 10 => 'input-mini',
  3792. 15 => 'input-medium',
  3793. 20 => 'input-xlarge',
  3794. 40 => 'input-xlarge',
  3795. 60 => 'input-xxlarge',
  3796. 100 => 'input-xxlarge',
  3797. 200 => 'input-xxlarge',
  3798. );
  3799. foreach ($limits as $size => $item) {
  3800. if ($countLetter <= $size) {
  3801. return $item;
  3802. }
  3803. }
  3804. return $limits[0];
  3805. }
  3806. /**
  3807. * @param int $senderId
  3808. * @param array $course_info
  3809. * @param string $test
  3810. * @param string $url
  3811. *
  3812. * @return string
  3813. */
  3814. public static function getEmailNotification($senderId, $course_info, $test, $url)
  3815. {
  3816. $teacher_info = api_get_user_info($senderId);
  3817. $from_name = api_get_person_name(
  3818. $teacher_info['firstname'],
  3819. $teacher_info['lastname'],
  3820. null,
  3821. PERSON_NAME_EMAIL_ADDRESS
  3822. );
  3823. $message = '<p>'.get_lang('DearStudentEmailIntroduction').'</p><p>'.get_lang('AttemptVCC');
  3824. $message .= '<h3>'.get_lang('CourseName').'</h3><p>'.Security::remove_XSS($course_info['name']).'';
  3825. $message .= '<h3>'.get_lang('Exercise').'</h3><p>'.Security::remove_XSS($test);
  3826. $message .= '<p>'.get_lang('ClickLinkToViewComment').' <br /><a href="#url#">#url#</a><br />';
  3827. $message .= '<p>'.get_lang('Regards').'</p>';
  3828. $message .= $from_name;
  3829. $message = str_replace("#test#", Security::remove_XSS($test), $message);
  3830. $message = str_replace("#url#", $url, $message);
  3831. return $message;
  3832. }
  3833. /**
  3834. * @return string
  3835. */
  3836. public static function getNotCorrectedYetText()
  3837. {
  3838. return Display::return_message(get_lang('notCorrectedYet'), 'warning');
  3839. }
  3840. /**
  3841. * @param string $message
  3842. * @return string
  3843. */
  3844. public static function getFeedbackText($message)
  3845. {
  3846. // Old style
  3847. //return '<div id="question_feedback">'.$message.'</div>';
  3848. return Display::return_message($message, 'warning', false);
  3849. }
  3850. }