exercise.lib.php 213 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
  4. use Chamilo\CoreBundle\Entity\TrackEExercises;
  5. use ChamiloSession as Session;
  6. /**
  7. * Class ExerciseLib
  8. * shows a question and its answers.
  9. *
  10. * @author Olivier Brouckaert <oli.brouckaert@skynet.be> 2003-2004
  11. * @author Hubert Borderiou 2011-10-21
  12. * @author ivantcholakov2009-07-20
  13. * @author Julio Montoya
  14. */
  15. class ExerciseLib
  16. {
  17. /**
  18. * Shows a question.
  19. *
  20. * @param Exercise $exercise
  21. * @param int $questionId $questionId question id
  22. * @param bool $only_questions if true only show the questions, no exercise title
  23. * @param bool $origin i.e = learnpath
  24. * @param string $current_item current item from the list of questions
  25. * @param bool $show_title
  26. * @param bool $freeze
  27. * @param array $user_choice
  28. * @param bool $show_comment
  29. * @param bool $show_answers
  30. *
  31. * @throws \Exception
  32. *
  33. * @return bool|int
  34. */
  35. public static function showQuestion(
  36. $exercise,
  37. $questionId,
  38. $only_questions = false,
  39. $origin = false,
  40. $current_item = '',
  41. $show_title = true,
  42. $freeze = false,
  43. $user_choice = [],
  44. $show_comment = false,
  45. $show_answers = false,
  46. $show_icon = false
  47. ) {
  48. $course_id = $exercise->course_id;
  49. $exerciseId = $exercise->iId;
  50. if (empty($course_id)) {
  51. return '';
  52. }
  53. $course = $exercise->course;
  54. // Change false to true in the following line to enable answer hinting
  55. $debug_mark_answer = $show_answers;
  56. // Reads question information
  57. if (!$objQuestionTmp = Question::read($questionId, $course)) {
  58. // Question not found
  59. return false;
  60. }
  61. if ($exercise->getFeedbackType() != EXERCISE_FEEDBACK_TYPE_END) {
  62. $show_comment = false;
  63. }
  64. $answerType = $objQuestionTmp->selectType();
  65. $pictureName = $objQuestionTmp->getPictureFilename();
  66. $s = '';
  67. if ($answerType != HOT_SPOT &&
  68. $answerType != HOT_SPOT_DELINEATION &&
  69. $answerType != ANNOTATION
  70. ) {
  71. // Question is not a hotspot
  72. if (!$only_questions) {
  73. $questionDescription = $objQuestionTmp->selectDescription();
  74. if ($show_title) {
  75. if ($exercise->display_category_name) {
  76. TestCategory::displayCategoryAndTitle($objQuestionTmp->id);
  77. }
  78. $titleToDisplay = $objQuestionTmp->getTitleToDisplay($current_item);
  79. if ($answerType == READING_COMPREHENSION) {
  80. // In READING_COMPREHENSION, the title of the question
  81. // contains the question itself, which can only be
  82. // shown at the end of the given time, so hide for now
  83. $titleToDisplay = Display::div(
  84. $current_item.'. '.get_lang('ReadingComprehension'),
  85. ['class' => 'question_title']
  86. );
  87. }
  88. echo $titleToDisplay;
  89. }
  90. if (!empty($questionDescription) && $answerType != READING_COMPREHENSION) {
  91. echo Display::div(
  92. $questionDescription,
  93. ['class' => 'question_description']
  94. );
  95. }
  96. }
  97. if (in_array($answerType, [FREE_ANSWER, ORAL_EXPRESSION]) && $freeze) {
  98. return '';
  99. }
  100. echo '<div class="question_options">';
  101. // construction of the Answer object (also gets all answers details)
  102. $objAnswerTmp = new Answer($questionId, $course_id, $exercise);
  103. $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
  104. $quizQuestionOptions = Question::readQuestionOption($questionId, $course_id);
  105. // For "matching" type here, we need something a little bit special
  106. // because the match between the suggestions and the answers cannot be
  107. // done easily (suggestions and answers are in the same table), so we
  108. // have to go through answers first (elems with "correct" value to 0).
  109. $select_items = [];
  110. //This will contain the number of answers on the left side. We call them
  111. // suggestions here, for the sake of comprehensions, while the ones
  112. // on the right side are called answers
  113. $num_suggestions = 0;
  114. switch ($answerType) {
  115. case MATCHING:
  116. case DRAGGABLE:
  117. case MATCHING_DRAGGABLE:
  118. if ($answerType == DRAGGABLE) {
  119. $isVertical = $objQuestionTmp->extra == 'v';
  120. $s .= '
  121. <div class="col-md-12 ui-widget ui-helper-clearfix">
  122. <div class="clearfix">
  123. <ul class="exercise-draggable-answer '.($isVertical ? '' : 'list-inline').'"
  124. id="question-'.$questionId.'" data-question="'.$questionId.'">
  125. ';
  126. } else {
  127. $s .= '<div id="drag'.$questionId.'_question" class="drag_question">
  128. <table class="data_table">';
  129. }
  130. // Iterate through answers
  131. $x = 1;
  132. //mark letters for each answer
  133. $letter = 'A';
  134. $answer_matching = [];
  135. $cpt1 = [];
  136. for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
  137. $answerCorrect = $objAnswerTmp->isCorrect($answerId);
  138. $numAnswer = $objAnswerTmp->selectAutoId($answerId);
  139. if ($answerCorrect == 0) {
  140. // options (A, B, C, ...) that will be put into the list-box
  141. // have the "correct" field set to 0 because they are answer
  142. $cpt1[$x] = $letter;
  143. $answer_matching[$x] = $objAnswerTmp->selectAnswerByAutoId(
  144. $numAnswer
  145. );
  146. $x++;
  147. $letter++;
  148. }
  149. }
  150. $i = 1;
  151. $select_items[0]['id'] = 0;
  152. $select_items[0]['letter'] = '--';
  153. $select_items[0]['answer'] = '';
  154. foreach ($answer_matching as $id => $value) {
  155. $select_items[$i]['id'] = $value['id_auto'];
  156. $select_items[$i]['letter'] = $cpt1[$id];
  157. $select_items[$i]['answer'] = $value['answer'];
  158. $i++;
  159. }
  160. $user_choice_array_position = [];
  161. if (!empty($user_choice)) {
  162. foreach ($user_choice as $item) {
  163. $user_choice_array_position[$item['position']] = $item['answer'];
  164. }
  165. }
  166. $num_suggestions = ($nbrAnswers - $x) + 1;
  167. break;
  168. case FREE_ANSWER:
  169. $fck_content = isset($user_choice[0]) && !empty($user_choice[0]['answer']) ? $user_choice[0]['answer'] : null;
  170. $form = new FormValidator('free_choice_'.$questionId);
  171. $config = [
  172. 'ToolbarSet' => 'TestFreeAnswer',
  173. ];
  174. $form->addHtmlEditor(
  175. "choice[".$questionId."]",
  176. null,
  177. false,
  178. false,
  179. $config
  180. );
  181. $form->setDefaults(["choice[".$questionId."]" => $fck_content]);
  182. $s .= $form->returnForm();
  183. break;
  184. case ORAL_EXPRESSION:
  185. // Add nanog
  186. if (api_get_setting('enable_record_audio') === 'true') {
  187. //@todo pass this as a parameter
  188. global $exercise_stat_info;
  189. if (!empty($exercise_stat_info)) {
  190. $objQuestionTmp->initFile(
  191. api_get_session_id(),
  192. api_get_user_id(),
  193. $exercise_stat_info['exe_exo_id'],
  194. $exercise_stat_info['exe_id']
  195. );
  196. } else {
  197. $objQuestionTmp->initFile(
  198. api_get_session_id(),
  199. api_get_user_id(),
  200. $exerciseId,
  201. 'temp_exe'
  202. );
  203. }
  204. echo $objQuestionTmp->returnRecorder();
  205. }
  206. $form = new FormValidator('free_choice_'.$questionId);
  207. $config = ['ToolbarSet' => 'TestFreeAnswer'];
  208. $form->addHtml('<div id="'.'hide_description_'.$questionId.'_options" style="display: none;">');
  209. $form->addHtmlEditor(
  210. "choice[$questionId]",
  211. null,
  212. false,
  213. false,
  214. $config
  215. );
  216. $form->addHtml('</div>');
  217. $s .= $form->returnForm();
  218. break;
  219. }
  220. // Now navigate through the possible answers, using the max number of
  221. // answers for the question as a limiter
  222. $lines_count = 1; // a counter for matching-type answers
  223. if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE ||
  224. $answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE
  225. ) {
  226. $header = Display::tag('th', get_lang('Options'));
  227. foreach ($objQuestionTmp->options as $item) {
  228. if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
  229. if (in_array($item, $objQuestionTmp->options)) {
  230. $header .= Display::tag('th', get_lang($item));
  231. } else {
  232. $header .= Display::tag('th', $item);
  233. }
  234. } else {
  235. $header .= Display::tag('th', $item);
  236. }
  237. }
  238. if ($show_comment) {
  239. $header .= Display::tag('th', get_lang('Feedback'));
  240. }
  241. $s .= '<table class="table table-hover table-striped">';
  242. $s .= Display::tag(
  243. 'tr',
  244. $header,
  245. ['style' => 'text-align:left;']
  246. );
  247. } elseif ($answerType == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  248. $header = Display::tag('th', get_lang('Options'), ['width' => '50%']);
  249. echo "
  250. <script>
  251. function RadioValidator(question_id, answer_id)
  252. {
  253. var ShowAlert = '';
  254. var typeRadioB = '';
  255. var AllFormElements = window.document.getElementById('exercise_form').elements;
  256. for (i = 0; i < AllFormElements.length; i++) {
  257. if (AllFormElements[i].type == 'radio') {
  258. var ThisRadio = AllFormElements[i].name;
  259. var ThisChecked = 'No';
  260. var AllRadioOptions = document.getElementsByName(ThisRadio);
  261. for (x = 0; x < AllRadioOptions.length; x++) {
  262. if (AllRadioOptions[x].checked && ThisChecked == 'No') {
  263. ThisChecked = 'Yes';
  264. break;
  265. }
  266. }
  267. var AlreadySearched = ShowAlert.indexOf(ThisRadio);
  268. if (ThisChecked == 'No' && AlreadySearched == -1) {
  269. ShowAlert = ShowAlert + ThisRadio;
  270. }
  271. }
  272. }
  273. if (ShowAlert != '') {
  274. } else {
  275. $('.question-validate-btn').removeAttr('disabled');
  276. }
  277. }
  278. function handleRadioRow(event, question_id, answer_id) {
  279. var t = event.target;
  280. if (t && t.tagName == 'INPUT')
  281. return;
  282. while (t && t.tagName != 'TD') {
  283. t = t.parentElement;
  284. }
  285. var r = t.getElementsByTagName('INPUT')[0];
  286. r.click();
  287. RadioValidator(question_id, answer_id);
  288. }
  289. $(function() {
  290. var ShowAlert = '';
  291. var typeRadioB = '';
  292. var question_id = $('input[name=question_id]').val();
  293. var AllFormElements = window.document.getElementById('exercise_form').elements;
  294. for (i = 0; i < AllFormElements.length; i++) {
  295. if (AllFormElements[i].type == 'radio') {
  296. var ThisRadio = AllFormElements[i].name;
  297. var ThisChecked = 'No';
  298. var AllRadioOptions = document.getElementsByName(ThisRadio);
  299. for (x = 0; x < AllRadioOptions.length; x++) {
  300. if (AllRadioOptions[x].checked && ThisChecked == 'No') {
  301. ThisChecked = \"Yes\";
  302. break;
  303. }
  304. }
  305. var AlreadySearched = ShowAlert.indexOf(ThisRadio);
  306. if (ThisChecked == 'No' && AlreadySearched == -1) {
  307. ShowAlert = ShowAlert + ThisRadio;
  308. }
  309. }
  310. }
  311. if (ShowAlert != '') {
  312. $('.question-validate-btn').attr('disabled', 'disabled');
  313. } else {
  314. $('.question-validate-btn').removeAttr('disabled');
  315. }
  316. });
  317. </script>";
  318. foreach ($objQuestionTmp->optionsTitle as $item) {
  319. if (in_array($item, $objQuestionTmp->optionsTitle)) {
  320. $properties = [];
  321. if ($item === 'Answers') {
  322. $properties['colspan'] = 2;
  323. $properties['style'] = 'background-color: #F56B2A; color: #ffffff;';
  324. } elseif ($item == 'DegreeOfCertaintyThatMyAnswerIsCorrect') {
  325. $properties['colspan'] = 6;
  326. $properties['style'] = 'background-color: #330066; color: #ffffff;';
  327. }
  328. $header .= Display::tag('th', get_lang($item), $properties);
  329. } else {
  330. $header .= Display::tag('th', $item);
  331. }
  332. }
  333. if ($show_comment) {
  334. $header .= Display::tag('th', get_lang('Feedback'));
  335. }
  336. $s .= '<table class="data_table">';
  337. $s .= Display::tag('tr', $header, ['style' => 'text-align:left;']);
  338. // ajout de la 2eme ligne d'entête pour true/falss et les pourcentages de certitude
  339. $header1 = Display::tag('th', '&nbsp;');
  340. $cpt1 = 0;
  341. foreach ($objQuestionTmp->options as $item) {
  342. $colorBorder1 = ($cpt1 == (count($objQuestionTmp->options) - 1))
  343. ? '' : 'border-right: solid #FFFFFF 1px;';
  344. if ($item === 'True' || $item === 'False') {
  345. $header1 .= Display::tag(
  346. 'th',
  347. get_lang($item),
  348. ['style' => 'background-color: #F7C9B4; color: black;'.$colorBorder1]
  349. );
  350. } else {
  351. $header1 .= Display::tag(
  352. 'th',
  353. $item,
  354. ['style' => 'background-color: #e6e6ff; color: black;padding:5px; '.$colorBorder1]
  355. );
  356. }
  357. $cpt1++;
  358. }
  359. if ($show_comment) {
  360. $header1 .= Display::tag('th', '&nbsp;');
  361. }
  362. $s .= Display::tag('tr', $header1);
  363. // add explanation
  364. $header2 = Display::tag('th', '&nbsp;');
  365. $descriptionList = [
  366. get_lang('DegreeOfCertaintyIDeclareMyIgnorance'),
  367. get_lang('DegreeOfCertaintyIAmVeryUnsure'),
  368. get_lang('DegreeOfCertaintyIAmUnsure'),
  369. get_lang('DegreeOfCertaintyIAmPrettySure'),
  370. get_lang('DegreeOfCertaintyIAmSure'),
  371. get_lang('DegreeOfCertaintyIAmVerySure'),
  372. ];
  373. $counter2 = 0;
  374. foreach ($objQuestionTmp->options as $item) {
  375. if ($item == 'True' || $item == 'False') {
  376. $header2 .= Display::tag('td',
  377. '&nbsp;',
  378. ['style' => 'background-color: #F7E1D7; color: black;border-right: solid #FFFFFF 1px;']);
  379. } else {
  380. $color_border2 = ($counter2 == (count($objQuestionTmp->options) - 1)) ?
  381. '' : 'border-right: solid #FFFFFF 1px;font-size:11px;';
  382. $header2 .= Display::tag(
  383. 'td',
  384. nl2br($descriptionList[$counter2]),
  385. ['style' => 'background-color: #EFEFFC; color: black; width: 110px; text-align:center;
  386. vertical-align: top; padding:5px; '.$color_border2]);
  387. $counter2++;
  388. }
  389. }
  390. if ($show_comment) {
  391. $header2 .= Display::tag('th', '&nbsp;');
  392. }
  393. $s .= Display::tag('tr', $header2);
  394. }
  395. if ($show_comment) {
  396. if (in_array(
  397. $answerType,
  398. [
  399. MULTIPLE_ANSWER,
  400. MULTIPLE_ANSWER_COMBINATION,
  401. UNIQUE_ANSWER,
  402. UNIQUE_ANSWER_IMAGE,
  403. UNIQUE_ANSWER_NO_OPTION,
  404. GLOBAL_MULTIPLE_ANSWER,
  405. ]
  406. )) {
  407. $header = Display::tag('th', get_lang('Options'));
  408. if ($exercise->getFeedbackType() == EXERCISE_FEEDBACK_TYPE_END) {
  409. $header .= Display::tag('th', get_lang('Feedback'));
  410. }
  411. $s .= '<table class="table table-hover table-striped">';
  412. $s .= Display::tag(
  413. 'tr',
  414. $header,
  415. ['style' => 'text-align:left;']
  416. );
  417. }
  418. }
  419. $matching_correct_answer = 0;
  420. $userChoiceList = [];
  421. if (!empty($user_choice)) {
  422. foreach ($user_choice as $item) {
  423. $userChoiceList[] = $item['answer'];
  424. }
  425. }
  426. $hidingClass = '';
  427. if ($answerType == READING_COMPREHENSION) {
  428. $objQuestionTmp->setExerciseType($exercise->selectType());
  429. $objQuestionTmp->processText($objQuestionTmp->selectDescription());
  430. $hidingClass = 'hide-reading-answers';
  431. $s .= Display::div(
  432. $objQuestionTmp->selectTitle(),
  433. ['class' => 'question_title '.$hidingClass]
  434. );
  435. }
  436. for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
  437. $answer = $objAnswerTmp->selectAnswer($answerId);
  438. $answerCorrect = $objAnswerTmp->isCorrect($answerId);
  439. $numAnswer = $objAnswerTmp->selectAutoId($answerId);
  440. $comment = $objAnswerTmp->selectComment($answerId);
  441. $attributes = [];
  442. switch ($answerType) {
  443. case UNIQUE_ANSWER:
  444. case UNIQUE_ANSWER_NO_OPTION:
  445. case UNIQUE_ANSWER_IMAGE:
  446. case READING_COMPREHENSION:
  447. $input_id = 'choice-'.$questionId.'-'.$answerId;
  448. if (isset($user_choice[0]['answer']) && $user_choice[0]['answer'] == $numAnswer) {
  449. $attributes = [
  450. 'id' => $input_id,
  451. 'checked' => 1,
  452. 'selected' => 1,
  453. ];
  454. } else {
  455. $attributes = ['id' => $input_id];
  456. }
  457. if ($debug_mark_answer) {
  458. if ($answerCorrect) {
  459. $attributes['checked'] = 1;
  460. $attributes['selected'] = 1;
  461. }
  462. }
  463. if ($show_comment) {
  464. $s .= '<tr><td>';
  465. }
  466. if ($answerType == UNIQUE_ANSWER_IMAGE) {
  467. if ($show_comment) {
  468. if (empty($comment)) {
  469. $s .= '<div id="answer'.$questionId.$numAnswer.'"
  470. class="exercise-unique-answer-image" style="text-align: center">';
  471. } else {
  472. $s .= '<div id="answer'.$questionId.$numAnswer.'"
  473. class="exercise-unique-answer-image col-xs-6 col-sm-12"
  474. style="text-align: center">';
  475. }
  476. } else {
  477. $s .= '<div id="answer'.$questionId.$numAnswer.'"
  478. class="exercise-unique-answer-image col-xs-6 col-md-3"
  479. style="text-align: center">';
  480. }
  481. }
  482. $answer = Security::remove_XSS($answer, STUDENT);
  483. $s .= Display::input(
  484. 'hidden',
  485. 'choice2['.$questionId.']',
  486. '0'
  487. );
  488. $answer_input = null;
  489. $attributes['class'] = 'checkradios';
  490. if ($answerType == UNIQUE_ANSWER_IMAGE) {
  491. $attributes['class'] = '';
  492. $attributes['style'] = 'display: none;';
  493. $answer = '<div class="thumbnail">'.$answer.'</div>';
  494. }
  495. $answer_input .= '<label class="radio '.$hidingClass.'">';
  496. $answer_input .= Display::input(
  497. 'radio',
  498. 'choice['.$questionId.']',
  499. $numAnswer,
  500. $attributes
  501. );
  502. $answer_input .= $answer;
  503. $answer_input .= '</label>';
  504. if ($answerType == UNIQUE_ANSWER_IMAGE) {
  505. $answer_input .= "</div>";
  506. }
  507. if ($show_comment) {
  508. $s .= $answer_input;
  509. $s .= '</td>';
  510. $s .= '<td>';
  511. $s .= $comment;
  512. $s .= '</td>';
  513. $s .= '</tr>';
  514. } else {
  515. $s .= $answer_input;
  516. }
  517. break;
  518. case MULTIPLE_ANSWER:
  519. case MULTIPLE_ANSWER_TRUE_FALSE:
  520. case GLOBAL_MULTIPLE_ANSWER:
  521. case MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY:
  522. $input_id = 'choice-'.$questionId.'-'.$answerId;
  523. $answer = Security::remove_XSS($answer, STUDENT);
  524. if (in_array($numAnswer, $userChoiceList)) {
  525. $attributes = [
  526. 'id' => $input_id,
  527. 'checked' => 1,
  528. 'selected' => 1,
  529. ];
  530. } else {
  531. $attributes = ['id' => $input_id];
  532. }
  533. if ($debug_mark_answer) {
  534. if ($answerCorrect) {
  535. $attributes['checked'] = 1;
  536. $attributes['selected'] = 1;
  537. }
  538. }
  539. if ($answerType == MULTIPLE_ANSWER || $answerType == GLOBAL_MULTIPLE_ANSWER) {
  540. $s .= '<input type="hidden" name="choice2['.$questionId.']" value="0" />';
  541. $attributes['class'] = 'checkradios';
  542. $answer_input = '<label class="checkbox">';
  543. $answer_input .= Display::input(
  544. 'checkbox',
  545. 'choice['.$questionId.']['.$numAnswer.']',
  546. $numAnswer,
  547. $attributes
  548. );
  549. $answer_input .= $answer;
  550. $answer_input .= '</label>';
  551. if ($show_comment) {
  552. $s .= '<tr><td>';
  553. $s .= $answer_input;
  554. $s .= '</td>';
  555. $s .= '<td>';
  556. $s .= $comment;
  557. $s .= '</td>';
  558. $s .= '</tr>';
  559. } else {
  560. $s .= $answer_input;
  561. }
  562. } elseif ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
  563. $myChoice = [];
  564. if (!empty($userChoiceList)) {
  565. foreach ($userChoiceList as $item) {
  566. $item = explode(':', $item);
  567. if (!empty($item)) {
  568. $myChoice[$item[0]] = isset($item[1]) ? $item[1] : '';
  569. }
  570. }
  571. }
  572. $s .= '<tr>';
  573. $s .= Display::tag('td', $answer);
  574. if (!empty($quizQuestionOptions)) {
  575. foreach ($quizQuestionOptions as $id => $item) {
  576. if (isset($myChoice[$numAnswer]) && $id == $myChoice[$numAnswer]) {
  577. $attributes = [
  578. 'checked' => 1,
  579. 'selected' => 1,
  580. ];
  581. } else {
  582. $attributes = [];
  583. }
  584. if ($debug_mark_answer) {
  585. if ($id == $answerCorrect) {
  586. $attributes['checked'] = 1;
  587. $attributes['selected'] = 1;
  588. }
  589. }
  590. $s .= Display::tag(
  591. 'td',
  592. Display::input(
  593. 'radio',
  594. 'choice['.$questionId.']['.$numAnswer.']',
  595. $id,
  596. $attributes
  597. ),
  598. ['style' => '']
  599. );
  600. }
  601. }
  602. if ($show_comment) {
  603. $s .= '<td>';
  604. $s .= $comment;
  605. $s .= '</td>';
  606. }
  607. $s .= '</tr>';
  608. } elseif ($answerType == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  609. $myChoice = [];
  610. if (!empty($userChoiceList)) {
  611. foreach ($userChoiceList as $item) {
  612. $item = explode(':', $item);
  613. $myChoice[$item[0]] = $item[1];
  614. }
  615. }
  616. $myChoiceDegreeCertainty = [];
  617. if (!empty($userChoiceList)) {
  618. foreach ($userChoiceList as $item) {
  619. $item = explode(':', $item);
  620. $myChoiceDegreeCertainty[$item[0]] = $item[2];
  621. }
  622. }
  623. $s .= '<tr>';
  624. $s .= Display::tag('td', $answer);
  625. if (!empty($quizQuestionOptions)) {
  626. foreach ($quizQuestionOptions as $id => $item) {
  627. if (isset($myChoice[$numAnswer]) && $id == $myChoice[$numAnswer]) {
  628. $attributes = ['checked' => 1, 'selected' => 1];
  629. } else {
  630. $attributes = [];
  631. }
  632. $attributes['onChange'] = 'RadioValidator('.$questionId.', '.$numAnswer.')';
  633. // radio button selection
  634. if (isset($myChoiceDegreeCertainty[$numAnswer]) &&
  635. $id == $myChoiceDegreeCertainty[$numAnswer]
  636. ) {
  637. $attributes1 = ['checked' => 1, 'selected' => 1];
  638. } else {
  639. $attributes1 = [];
  640. }
  641. $attributes1['onChange'] = 'RadioValidator('.$questionId.', '.$numAnswer.')';
  642. if ($debug_mark_answer) {
  643. if ($id == $answerCorrect) {
  644. $attributes['checked'] = 1;
  645. $attributes['selected'] = 1;
  646. }
  647. }
  648. if ($item['name'] == 'True' || $item['name'] == 'False') {
  649. $s .= Display::tag('td',
  650. Display::input('radio',
  651. 'choice['.$questionId.']['.$numAnswer.']',
  652. $id,
  653. $attributes
  654. ),
  655. ['style' => 'text-align:center; background-color:#F7E1D7;',
  656. 'onclick' => 'handleRadioRow(event, '.
  657. $questionId.', '.
  658. $numAnswer.')',
  659. ]
  660. );
  661. } else {
  662. $s .= Display::tag('td',
  663. Display::input('radio',
  664. 'choiceDegreeCertainty['.$questionId.']['.$numAnswer.']',
  665. $id,
  666. $attributes1
  667. ),
  668. ['style' => 'text-align:center; background-color:#EFEFFC;',
  669. 'onclick' => 'handleRadioRow(event, '.
  670. $questionId.', '.
  671. $numAnswer.')',
  672. ]
  673. );
  674. }
  675. }
  676. }
  677. if ($show_comment) {
  678. $s .= '<td>';
  679. $s .= $comment;
  680. $s .= '</td>';
  681. }
  682. $s .= '</tr>';
  683. }
  684. break;
  685. case MULTIPLE_ANSWER_COMBINATION:
  686. // multiple answers
  687. $input_id = 'choice-'.$questionId.'-'.$answerId;
  688. if (in_array($numAnswer, $userChoiceList)) {
  689. $attributes = [
  690. 'id' => $input_id,
  691. 'checked' => 1,
  692. 'selected' => 1,
  693. ];
  694. } else {
  695. $attributes = ['id' => $input_id];
  696. }
  697. if ($debug_mark_answer) {
  698. if ($answerCorrect) {
  699. $attributes['checked'] = 1;
  700. $attributes['selected'] = 1;
  701. }
  702. }
  703. $answer = Security::remove_XSS($answer, STUDENT);
  704. $answer_input = '<input type="hidden" name="choice2['.$questionId.']" value="0" />';
  705. $answer_input .= '<label class="checkbox">';
  706. $answer_input .= Display::input(
  707. 'checkbox',
  708. 'choice['.$questionId.']['.$numAnswer.']',
  709. 1,
  710. $attributes
  711. );
  712. $answer_input .= $answer;
  713. $answer_input .= '</label>';
  714. if ($show_comment) {
  715. $s .= '<tr>';
  716. $s .= '<td>';
  717. $s .= $answer_input;
  718. $s .= '</td>';
  719. $s .= '<td>';
  720. $s .= $comment;
  721. $s .= '</td>';
  722. $s .= '</tr>';
  723. } else {
  724. $s .= $answer_input;
  725. }
  726. break;
  727. case MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE:
  728. $s .= '<input type="hidden" name="choice2['.$questionId.']" value="0" />';
  729. $myChoice = [];
  730. if (!empty($userChoiceList)) {
  731. foreach ($userChoiceList as $item) {
  732. $item = explode(':', $item);
  733. if (isset($item[1]) && isset($item[0])) {
  734. $myChoice[$item[0]] = $item[1];
  735. }
  736. }
  737. }
  738. $answer = Security::remove_XSS($answer, STUDENT);
  739. $s .= '<tr>';
  740. $s .= Display::tag('td', $answer);
  741. foreach ($objQuestionTmp->options as $key => $item) {
  742. if (isset($myChoice[$numAnswer]) && $key == $myChoice[$numAnswer]) {
  743. $attributes = [
  744. 'checked' => 1,
  745. 'selected' => 1,
  746. ];
  747. } else {
  748. $attributes = [];
  749. }
  750. if ($debug_mark_answer) {
  751. if ($key == $answerCorrect) {
  752. $attributes['checked'] = 1;
  753. $attributes['selected'] = 1;
  754. }
  755. }
  756. $s .= Display::tag(
  757. 'td',
  758. Display::input(
  759. 'radio',
  760. 'choice['.$questionId.']['.$numAnswer.']',
  761. $key,
  762. $attributes
  763. )
  764. );
  765. }
  766. if ($show_comment) {
  767. $s .= '<td>';
  768. $s .= $comment;
  769. $s .= '</td>';
  770. }
  771. $s .= '</tr>';
  772. break;
  773. case FILL_IN_BLANKS:
  774. // display the question, with field empty, for student to fill it,
  775. // or filled to display the answer in the Question preview of the exercise/admin.php page
  776. $displayForStudent = true;
  777. $listAnswerInfo = FillBlanks::getAnswerInfo($answer);
  778. // Correct answers
  779. $correctAnswerList = $listAnswerInfo['words'];
  780. // Student's answer
  781. $studentAnswerList = [];
  782. if (isset($user_choice[0]['answer'])) {
  783. $arrayStudentAnswer = FillBlanks::getAnswerInfo(
  784. $user_choice[0]['answer'],
  785. true
  786. );
  787. $studentAnswerList = $arrayStudentAnswer['student_answer'];
  788. }
  789. // If the question must be shown with the answer (in page exercise/admin.php)
  790. // for teacher preview set the student-answer to the correct answer
  791. if ($debug_mark_answer) {
  792. $studentAnswerList = $correctAnswerList;
  793. $displayForStudent = false;
  794. }
  795. if (!empty($correctAnswerList) && !empty($studentAnswerList)) {
  796. $answer = '';
  797. for ($i = 0; $i < count($listAnswerInfo['common_words']) - 1; $i++) {
  798. // display the common word
  799. $answer .= $listAnswerInfo['common_words'][$i];
  800. // display the blank word
  801. $correctItem = $listAnswerInfo['words'][$i];
  802. if (isset($studentAnswerList[$i])) {
  803. // If student already started this test and answered this question,
  804. // fill the blank with his previous answers
  805. // may be "" if student viewed the question, but did not fill the blanks
  806. $correctItem = $studentAnswerList[$i];
  807. }
  808. $attributes['style'] = 'width:'.$listAnswerInfo['input_size'][$i].'px';
  809. $answer .= FillBlanks::getFillTheBlankHtml(
  810. $current_item,
  811. $questionId,
  812. $correctItem,
  813. $attributes,
  814. $answer,
  815. $listAnswerInfo,
  816. $displayForStudent,
  817. $i
  818. );
  819. }
  820. // display the last common word
  821. $answer .= $listAnswerInfo['common_words'][$i];
  822. } else {
  823. // display empty [input] with the right width for student to fill it
  824. $answer = '';
  825. for ($i = 0; $i < count($listAnswerInfo['common_words']) - 1; $i++) {
  826. // display the common words
  827. $answer .= $listAnswerInfo['common_words'][$i];
  828. // display the blank word
  829. $attributes['style'] = 'width:'.$listAnswerInfo['input_size'][$i].'px';
  830. $answer .= FillBlanks::getFillTheBlankHtml(
  831. $current_item,
  832. $questionId,
  833. '',
  834. $attributes,
  835. $answer,
  836. $listAnswerInfo,
  837. $displayForStudent,
  838. $i
  839. );
  840. }
  841. // display the last common word
  842. $answer .= $listAnswerInfo['common_words'][$i];
  843. }
  844. $s .= $answer;
  845. break;
  846. case CALCULATED_ANSWER:
  847. /*
  848. * In the CALCULATED_ANSWER test
  849. * you mustn't have [ and ] in the textarea
  850. * you mustn't have @@ in the textarea
  851. * the text to find mustn't be empty or contains only spaces
  852. * the text to find mustn't contains HTML tags
  853. * the text to find mustn't contains char "
  854. */
  855. if ($origin !== null) {
  856. global $exe_id;
  857. $exe_id = (int) $exe_id;
  858. $trackAttempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  859. $sql = "SELECT answer FROM $trackAttempts
  860. WHERE exe_id = $exe_id AND question_id= $questionId";
  861. $rsLastAttempt = Database::query($sql);
  862. $rowLastAttempt = Database::fetch_array($rsLastAttempt);
  863. $answer = $rowLastAttempt['answer'];
  864. if (empty($answer)) {
  865. $_SESSION['calculatedAnswerId'][$questionId] = mt_rand(
  866. 1,
  867. $nbrAnswers
  868. );
  869. $answer = $objAnswerTmp->selectAnswer(
  870. $_SESSION['calculatedAnswerId'][$questionId]
  871. );
  872. }
  873. }
  874. list($answer) = explode('@@', $answer);
  875. // $correctAnswerList array of array with correct anwsers 0=> [0=>[\p] 1=>[plop]]
  876. api_preg_match_all(
  877. '/\[[^]]+\]/',
  878. $answer,
  879. $correctAnswerList
  880. );
  881. // get student answer to display it if student go back
  882. // to previous calculated answer question in a test
  883. if (isset($user_choice[0]['answer'])) {
  884. api_preg_match_all(
  885. '/\[[^]]+\]/',
  886. $answer,
  887. $studentAnswerList
  888. );
  889. $studentAnswerListToClean = $studentAnswerList[0];
  890. $studentAnswerList = [];
  891. $maxStudents = count($studentAnswerListToClean);
  892. for ($i = 0; $i < $maxStudents; $i++) {
  893. $answerCorrected = $studentAnswerListToClean[$i];
  894. $answerCorrected = api_preg_replace(
  895. '| / <font color="green"><b>.*$|',
  896. '',
  897. $answerCorrected
  898. );
  899. $answerCorrected = api_preg_replace(
  900. '/^\[/',
  901. '',
  902. $answerCorrected
  903. );
  904. $answerCorrected = api_preg_replace(
  905. '|^<font color="red"><s>|',
  906. '',
  907. $answerCorrected
  908. );
  909. $answerCorrected = api_preg_replace(
  910. '|</s></font>$|',
  911. '',
  912. $answerCorrected
  913. );
  914. $answerCorrected = '['.$answerCorrected.']';
  915. $studentAnswerList[] = $answerCorrected;
  916. }
  917. }
  918. // If display preview of answer in test view for exemple,
  919. // set the student answer to the correct answers
  920. if ($debug_mark_answer) {
  921. // contain the rights answers surronded with brackets
  922. $studentAnswerList = $correctAnswerList[0];
  923. }
  924. /*
  925. Split the response by bracket
  926. tabComments is an array with text surrounding the text to find
  927. we add a space before and after the answerQuestion to be sure to
  928. have a block of text before and after [xxx] patterns
  929. so we have n text to find ([xxx]) and n+1 block of texts before,
  930. between and after the text to find
  931. */
  932. $tabComments = api_preg_split(
  933. '/\[[^]]+\]/',
  934. ' '.$answer.' '
  935. );
  936. if (!empty($correctAnswerList) && !empty($studentAnswerList)) {
  937. $answer = '';
  938. $i = 0;
  939. foreach ($studentAnswerList as $studentItem) {
  940. // Remove surronding brackets
  941. $studentResponse = api_substr(
  942. $studentItem,
  943. 1,
  944. api_strlen($studentItem) - 2
  945. );
  946. $size = strlen($studentItem);
  947. $attributes['class'] = self::detectInputAppropriateClass(
  948. $size
  949. );
  950. $answer .= $tabComments[$i].
  951. Display::input(
  952. 'text',
  953. "choice[$questionId][]",
  954. $studentResponse,
  955. $attributes
  956. );
  957. $i++;
  958. }
  959. $answer .= $tabComments[$i];
  960. } else {
  961. // display exercise with empty input fields
  962. // every [xxx] are replaced with an empty input field
  963. foreach ($correctAnswerList[0] as $item) {
  964. $size = strlen($item);
  965. $attributes['class'] = self::detectInputAppropriateClass(
  966. $size
  967. );
  968. $answer = str_replace(
  969. $item,
  970. Display::input(
  971. 'text',
  972. "choice[$questionId][]",
  973. '',
  974. $attributes
  975. ),
  976. $answer
  977. );
  978. }
  979. }
  980. if ($origin !== null) {
  981. $s = $answer;
  982. break;
  983. } else {
  984. $s .= $answer;
  985. }
  986. break;
  987. case MATCHING:
  988. // matching type, showing suggestions and answers
  989. // TODO: replace $answerId by $numAnswer
  990. if ($answerCorrect != 0) {
  991. // only show elements to be answered (not the contents of
  992. // the select boxes, who are correct = 0)
  993. $s .= '<tr><td width="45%" valign="top">';
  994. $parsed_answer = $answer;
  995. // Left part questions
  996. $s .= '<p class="indent">'.$lines_count.'.&nbsp;'.$parsed_answer.'</p></td>';
  997. // Middle part (matches selects)
  998. // Id of select is # question + # of option
  999. $s .= '<td width="10%" valign="top" align="center">
  1000. <div class="select-matching">
  1001. <select
  1002. id="choice_id_'.$current_item.'_'.$lines_count.'"
  1003. name="choice['.$questionId.']['.$numAnswer.']">';
  1004. // fills the list-box
  1005. foreach ($select_items as $key => $val) {
  1006. // set $debug_mark_answer to true at function start to
  1007. // show the correct answer with a suffix '-x'
  1008. $selected = '';
  1009. if ($debug_mark_answer) {
  1010. if ($val['id'] == $answerCorrect) {
  1011. $selected = 'selected="selected"';
  1012. }
  1013. }
  1014. //$user_choice_array_position
  1015. if (isset($user_choice_array_position[$numAnswer]) &&
  1016. $val['id'] == $user_choice_array_position[$numAnswer]
  1017. ) {
  1018. $selected = 'selected="selected"';
  1019. }
  1020. $s .= '<option value="'.$val['id'].'" '.$selected.'>'.$val['letter'].'</option>';
  1021. }
  1022. $s .= '</select></div></td><td width="5%" class="separate">&nbsp;</td>';
  1023. $s .= '<td width="40%" valign="top" >';
  1024. if (isset($select_items[$lines_count])) {
  1025. $s .= '<div class="text-right">
  1026. <p class="indent">'.
  1027. $select_items[$lines_count]['letter'].'.&nbsp; '.
  1028. $select_items[$lines_count]['answer'].'
  1029. </p>
  1030. </div>';
  1031. } else {
  1032. $s .= '&nbsp;';
  1033. }
  1034. $s .= '</td>';
  1035. $s .= '</tr>';
  1036. $lines_count++;
  1037. // If the left side of the "matching" has been completely
  1038. // shown but the right side still has values to show...
  1039. if (($lines_count - 1) == $num_suggestions) {
  1040. // if it remains answers to shown at the right side
  1041. while (isset($select_items[$lines_count])) {
  1042. $s .= '<tr>
  1043. <td colspan="2"></td>
  1044. <td valign="top">';
  1045. $s .= '<b>'.$select_items[$lines_count]['letter'].'.</b> '.
  1046. $select_items[$lines_count]['answer'];
  1047. $s .= "</td>
  1048. </tr>";
  1049. $lines_count++;
  1050. }
  1051. }
  1052. $matching_correct_answer++;
  1053. }
  1054. break;
  1055. case DRAGGABLE:
  1056. if ($answerCorrect) {
  1057. $windowId = $questionId.'_'.$lines_count;
  1058. $s .= '<li class="touch-items" id="'.$windowId.'">';
  1059. $s .= Display::div(
  1060. $answer,
  1061. [
  1062. 'id' => "window_$windowId",
  1063. 'class' => "window{$questionId}_question_draggable exercise-draggable-answer-option",
  1064. ]
  1065. );
  1066. $draggableSelectOptions = [];
  1067. $selectedValue = 0;
  1068. $selectedIndex = 0;
  1069. if ($user_choice) {
  1070. foreach ($user_choice as $chosen) {
  1071. if ($answerCorrect != $chosen['answer']) {
  1072. continue;
  1073. }
  1074. $selectedValue = $chosen['answer'];
  1075. }
  1076. }
  1077. foreach ($select_items as $key => $select_item) {
  1078. $draggableSelectOptions[$select_item['id']] = $select_item['letter'];
  1079. }
  1080. foreach ($draggableSelectOptions as $value => $text) {
  1081. if ($value == $selectedValue) {
  1082. break;
  1083. }
  1084. $selectedIndex++;
  1085. }
  1086. $s .= Display::select(
  1087. "choice[$questionId][$numAnswer]",
  1088. $draggableSelectOptions,
  1089. $selectedValue,
  1090. [
  1091. 'id' => "window_{$windowId}_select",
  1092. 'class' => 'select_option hidden',
  1093. ],
  1094. false
  1095. );
  1096. if ($selectedValue && $selectedIndex) {
  1097. $s .= "
  1098. <script>
  1099. $(function() {
  1100. DraggableAnswer.deleteItem(
  1101. $('#{$questionId}_$lines_count'),
  1102. $('#drop_{$questionId}_{$selectedIndex}')
  1103. );
  1104. });
  1105. </script>
  1106. ";
  1107. }
  1108. if (isset($select_items[$lines_count])) {
  1109. $s .= Display::div(
  1110. Display::tag(
  1111. 'b',
  1112. $select_items[$lines_count]['letter']
  1113. ).$select_items[$lines_count]['answer'],
  1114. [
  1115. 'id' => "window_{$windowId}_answer",
  1116. 'class' => 'hidden',
  1117. ]
  1118. );
  1119. } else {
  1120. $s .= '&nbsp;';
  1121. }
  1122. $lines_count++;
  1123. if (($lines_count - 1) == $num_suggestions) {
  1124. while (isset($select_items[$lines_count])) {
  1125. $s .= Display::tag('b', $select_items[$lines_count]['letter']);
  1126. $s .= $select_items[$lines_count]['answer'];
  1127. $lines_count++;
  1128. }
  1129. }
  1130. $matching_correct_answer++;
  1131. $s .= '</li>';
  1132. }
  1133. break;
  1134. case MATCHING_DRAGGABLE:
  1135. if ($answerId == 1) {
  1136. echo $objAnswerTmp->getJs();
  1137. }
  1138. if ($answerCorrect != 0) {
  1139. $windowId = "{$questionId}_{$lines_count}";
  1140. $s .= <<<HTML
  1141. <tr>
  1142. <td width="45%">
  1143. <div id="window_{$windowId}"
  1144. class="window window_left_question window{$questionId}_question">
  1145. <strong>$lines_count.</strong>
  1146. $answer
  1147. </div>
  1148. </td>
  1149. <td width="10%">
  1150. HTML;
  1151. $draggableSelectOptions = [];
  1152. $selectedValue = 0;
  1153. $selectedIndex = 0;
  1154. if ($user_choice) {
  1155. foreach ($user_choice as $chosen) {
  1156. if ($numAnswer == $chosen['position']) {
  1157. $selectedValue = $chosen['answer'];
  1158. break;
  1159. }
  1160. }
  1161. }
  1162. foreach ($select_items as $key => $selectItem) {
  1163. $draggableSelectOptions[$selectItem['id']] = $selectItem['letter'];
  1164. }
  1165. foreach ($draggableSelectOptions as $value => $text) {
  1166. if ($value == $selectedValue) {
  1167. break;
  1168. }
  1169. $selectedIndex++;
  1170. }
  1171. $s .= Display::select(
  1172. "choice[$questionId][$numAnswer]",
  1173. $draggableSelectOptions,
  1174. $selectedValue,
  1175. [
  1176. 'id' => "window_{$windowId}_select",
  1177. 'class' => 'hidden',
  1178. ],
  1179. false
  1180. );
  1181. if (!empty($answerCorrect) && !empty($selectedValue)) {
  1182. // Show connect if is not freeze (question preview)
  1183. if (!$freeze) {
  1184. $s .= "
  1185. <script>
  1186. $(function() {
  1187. jsPlumb.ready(function() {
  1188. jsPlumb.connect({
  1189. source: 'window_$windowId',
  1190. target: 'window_{$questionId}_{$selectedIndex}_answer',
  1191. endpoint: ['Blank', {radius: 15}],
  1192. anchors: ['RightMiddle', 'LeftMiddle'],
  1193. paintStyle: {strokeStyle: '#8A8888', lineWidth: 8},
  1194. connector: [
  1195. MatchingDraggable.connectorType,
  1196. {curvines: MatchingDraggable.curviness}
  1197. ]
  1198. });
  1199. });
  1200. });
  1201. </script>
  1202. ";
  1203. }
  1204. }
  1205. $s .= '</td><td width="45%">';
  1206. if (isset($select_items[$lines_count])) {
  1207. $s .= <<<HTML
  1208. <div id="window_{$windowId}_answer" class="window window_right_question">
  1209. <strong>{$select_items[$lines_count]['letter']}.</strong>
  1210. {$select_items[$lines_count]['answer']}
  1211. </div>
  1212. HTML;
  1213. } else {
  1214. $s .= '&nbsp;';
  1215. }
  1216. $s .= '</td></tr>';
  1217. $lines_count++;
  1218. if (($lines_count - 1) == $num_suggestions) {
  1219. while (isset($select_items[$lines_count])) {
  1220. $s .= <<<HTML
  1221. <tr>
  1222. <td colspan="2"></td>
  1223. <td>
  1224. <strong>{$select_items[$lines_count]['letter']}</strong>
  1225. {$select_items[$lines_count]['answer']}
  1226. </td>
  1227. </tr>
  1228. HTML;
  1229. $lines_count++;
  1230. }
  1231. }
  1232. $matching_correct_answer++;
  1233. }
  1234. break;
  1235. }
  1236. }
  1237. if ($show_comment) {
  1238. $s .= '</table>';
  1239. } elseif (in_array(
  1240. $answerType,
  1241. [
  1242. MATCHING,
  1243. MATCHING_DRAGGABLE,
  1244. UNIQUE_ANSWER_NO_OPTION,
  1245. MULTIPLE_ANSWER_TRUE_FALSE,
  1246. MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE,
  1247. MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY,
  1248. ]
  1249. )) {
  1250. $s .= '</table>';
  1251. }
  1252. if ($answerType == DRAGGABLE) {
  1253. $isVertical = $objQuestionTmp->extra == 'v';
  1254. $s .= "</ul>";
  1255. $s .= "</div>";
  1256. $counterAnswer = 1;
  1257. $s .= $isVertical ? '' : '<div class="row">';
  1258. for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
  1259. $answerCorrect = $objAnswerTmp->isCorrect($answerId);
  1260. $windowId = $questionId.'_'.$counterAnswer;
  1261. if ($answerCorrect) {
  1262. $s .= $isVertical ? '<div class="row">' : '';
  1263. $s .= '
  1264. <div class="'.($isVertical ? 'col-md-12' : 'col-xs-12 col-sm-4 col-md-3 col-lg-2').'">
  1265. <div class="droppable-item">
  1266. <span class="number">'.$counterAnswer.'.</span>
  1267. <div id="drop_'.$windowId.'" class="droppable">&nbsp;</div>
  1268. </div>
  1269. </div>
  1270. ';
  1271. $s .= $isVertical ? '</div>' : '';
  1272. $counterAnswer++;
  1273. }
  1274. }
  1275. $s .= $isVertical ? '' : '</div>'; // row
  1276. $s .= '</div>'; // col-md-12 ui-widget ui-helper-clearfix
  1277. }
  1278. if (in_array($answerType, [MATCHING, MATCHING_DRAGGABLE])) {
  1279. $s .= '</div>'; //drag_question
  1280. }
  1281. $s .= '</div>'; //question_options row
  1282. // destruction of the Answer object
  1283. unset($objAnswerTmp);
  1284. // destruction of the Question object
  1285. unset($objQuestionTmp);
  1286. if ($origin == 'export') {
  1287. return $s;
  1288. }
  1289. echo $s;
  1290. } elseif ($answerType == HOT_SPOT || $answerType == HOT_SPOT_DELINEATION) {
  1291. global $exe_id;
  1292. // Question is a HOT_SPOT
  1293. // Checking document/images visibility
  1294. if (api_is_platform_admin() || api_is_course_admin()) {
  1295. $doc_id = $objQuestionTmp->getPictureId();
  1296. if (is_numeric($doc_id)) {
  1297. $images_folder_visibility = api_get_item_visibility(
  1298. $course,
  1299. 'document',
  1300. $doc_id,
  1301. api_get_session_id()
  1302. );
  1303. if (!$images_folder_visibility) {
  1304. // Show only to the course/platform admin if the image is set to visibility = false
  1305. echo Display::return_message(
  1306. get_lang('ChangeTheVisibilityOfTheCurrentImage'),
  1307. 'warning'
  1308. );
  1309. }
  1310. }
  1311. }
  1312. $questionDescription = $objQuestionTmp->selectDescription();
  1313. // Get the answers, make a list
  1314. $objAnswerTmp = new Answer($questionId, $course_id);
  1315. $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
  1316. // get answers of hotpost
  1317. $answers_hotspot = [];
  1318. for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
  1319. $answers = $objAnswerTmp->selectAnswerByAutoId(
  1320. $objAnswerTmp->selectAutoId($answerId)
  1321. );
  1322. $answers_hotspot[$answers['id']] = $objAnswerTmp->selectAnswer(
  1323. $answerId
  1324. );
  1325. }
  1326. $answerList = '';
  1327. $hotspotColor = 0;
  1328. if ($answerType != HOT_SPOT_DELINEATION) {
  1329. $answerList = '
  1330. <div class="well well-sm">
  1331. <h5 class="page-header">'.get_lang('HotspotZones').'</h5>
  1332. <ol>
  1333. ';
  1334. if (!empty($answers_hotspot)) {
  1335. Session::write("hotspot_ordered$questionId", array_keys($answers_hotspot));
  1336. foreach ($answers_hotspot as $value) {
  1337. $answerList .= '<li>';
  1338. if ($freeze) {
  1339. $answerList .= '<span class="hotspot-color-'.$hotspotColor
  1340. .' fa fa-square" aria-hidden="true"></span>'.PHP_EOL;
  1341. }
  1342. $answerList .= $value;
  1343. $answerList .= '</li>';
  1344. $hotspotColor++;
  1345. }
  1346. }
  1347. $answerList .= '
  1348. </ul>
  1349. </div>
  1350. ';
  1351. if ($freeze) {
  1352. $relPath = api_get_path(WEB_CODE_PATH);
  1353. echo "
  1354. <div class=\"row\">
  1355. <div class=\"col-sm-9\">
  1356. <div id=\"hotspot-preview-$questionId\"></div>
  1357. </div>
  1358. <div class=\"col-sm-3\">
  1359. $answerList
  1360. </div>
  1361. </div>
  1362. <script>
  1363. new ".($answerType == HOT_SPOT ? "HotspotQuestion" : "DelineationQuestion")."({
  1364. questionId: $questionId,
  1365. exerciseId: $exerciseId,
  1366. exeId: 0,
  1367. selector: '#hotspot-preview-$questionId',
  1368. for: 'preview',
  1369. relPath: '$relPath'
  1370. });
  1371. </script>
  1372. ";
  1373. return;
  1374. }
  1375. }
  1376. if (!$only_questions) {
  1377. if ($show_title) {
  1378. if ($exercise->display_category_name) {
  1379. TestCategory::displayCategoryAndTitle($objQuestionTmp->id);
  1380. }
  1381. echo $objQuestionTmp->getTitleToDisplay($current_item);
  1382. }
  1383. //@todo I need to the get the feedback type
  1384. echo <<<HOTSPOT
  1385. <input type="hidden" name="hidden_hotspot_id" value="$questionId" />
  1386. <div class="exercise_questions">
  1387. $questionDescription
  1388. <div class="row">
  1389. HOTSPOT;
  1390. }
  1391. $relPath = api_get_path(WEB_CODE_PATH);
  1392. $s .= "<div class=\"col-sm-8 col-md-9\">
  1393. <div class=\"hotspot-image\"></div>
  1394. <script>
  1395. $(function() {
  1396. new ".($answerType == HOT_SPOT_DELINEATION ? 'DelineationQuestion' : 'HotspotQuestion')."({
  1397. questionId: $questionId,
  1398. exerciseId: $exerciseId,
  1399. selector: '#question_div_' + $questionId + ' .hotspot-image',
  1400. for: 'user',
  1401. relPath: '$relPath'
  1402. });
  1403. });
  1404. </script>
  1405. </div>
  1406. <div class=\"col-sm-4 col-md-3\">
  1407. $answerList
  1408. </div>
  1409. ";
  1410. echo <<<HOTSPOT
  1411. $s
  1412. </div>
  1413. </div>
  1414. HOTSPOT;
  1415. } elseif ($answerType == ANNOTATION) {
  1416. global $exe_id;
  1417. $relPath = api_get_path(WEB_CODE_PATH);
  1418. if (api_is_platform_admin() || api_is_course_admin()) {
  1419. $docId = DocumentManager::get_document_id($course, '/images/'.$pictureName);
  1420. if ($docId) {
  1421. $images_folder_visibility = api_get_item_visibility(
  1422. $course,
  1423. 'document',
  1424. $docId,
  1425. api_get_session_id()
  1426. );
  1427. if (!$images_folder_visibility) {
  1428. echo Display::return_message(get_lang('ChangeTheVisibilityOfTheCurrentImage'), 'warning');
  1429. }
  1430. }
  1431. if ($freeze) {
  1432. echo Display::img(
  1433. api_get_path(WEB_COURSE_PATH).$course['path'].'/document/images/'.$pictureName,
  1434. $objQuestionTmp->selectTitle(),
  1435. ['width' => '600px']
  1436. );
  1437. return 0;
  1438. }
  1439. }
  1440. if (!$only_questions) {
  1441. if ($show_title) {
  1442. if ($exercise->display_category_name) {
  1443. TestCategory::displayCategoryAndTitle($objQuestionTmp->id);
  1444. }
  1445. echo $objQuestionTmp->getTitleToDisplay($current_item);
  1446. }
  1447. echo '
  1448. <input type="hidden" name="hidden_hotspot_id" value="'.$questionId.'" />
  1449. <div class="exercise_questions">
  1450. '.$objQuestionTmp->selectDescription().'
  1451. <div class="row">
  1452. <div class="col-sm-8 col-md-9">
  1453. <div id="annotation-canvas-'.$questionId.'" class="annotation-canvas center-block">
  1454. </div>
  1455. <script>
  1456. AnnotationQuestion({
  1457. questionId: '.$questionId.',
  1458. exerciseId: '.$exerciseId.',
  1459. relPath: \''.$relPath.'\',
  1460. courseId: '.$course_id.',
  1461. });
  1462. </script>
  1463. </div>
  1464. <div class="col-sm-4 col-md-3">
  1465. <div class="well well-sm" id="annotation-toolbar-'.$questionId.'">
  1466. <div class="btn-toolbar">
  1467. <div class="btn-group" data-toggle="buttons">
  1468. <label class="btn btn-default active"
  1469. aria-label="'.get_lang('AddAnnotationPath').'">
  1470. <input
  1471. type="radio" value="0"
  1472. name="'.$questionId.'-options" autocomplete="off" checked>
  1473. <span class="fa fa-pencil" aria-hidden="true"></span>
  1474. </label>
  1475. <label class="btn btn-default"
  1476. aria-label="'.get_lang('AddAnnotationText').'">
  1477. <input
  1478. type="radio" value="1"
  1479. name="'.$questionId.'-options" autocomplete="off">
  1480. <span class="fa fa-font fa-fw" aria-hidden="true"></span>
  1481. </label>
  1482. </div>
  1483. </div>
  1484. <ul class="list-unstyled"></ul>
  1485. </div>
  1486. </div>
  1487. </div>
  1488. </div>
  1489. ';
  1490. }
  1491. $objAnswerTmp = new Answer($questionId);
  1492. $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
  1493. unset($objAnswerTmp, $objQuestionTmp);
  1494. }
  1495. return $nbrAnswers;
  1496. }
  1497. /**
  1498. * @param int $exeId
  1499. *
  1500. * @return array
  1501. */
  1502. public static function get_exercise_track_exercise_info($exeId)
  1503. {
  1504. $quizTable = Database::get_course_table(TABLE_QUIZ_TEST);
  1505. $trackExerciseTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1506. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  1507. $exeId = (int) $exeId;
  1508. $result = [];
  1509. if (!empty($exeId)) {
  1510. $sql = " SELECT q.*, tee.*
  1511. FROM $quizTable as q
  1512. INNER JOIN $trackExerciseTable as tee
  1513. ON q.id = tee.exe_exo_id
  1514. INNER JOIN $courseTable c
  1515. ON c.id = tee.c_id
  1516. WHERE tee.exe_id = $exeId
  1517. AND q.c_id = c.id";
  1518. $sqlResult = Database::query($sql);
  1519. if (Database::num_rows($sqlResult)) {
  1520. $result = Database::fetch_array($sqlResult, 'ASSOC');
  1521. $result['duration_formatted'] = '';
  1522. if (!empty($result['exe_duration'])) {
  1523. $time = api_format_time($result['exe_duration'], 'js');
  1524. $result['duration_formatted'] = $time;
  1525. }
  1526. }
  1527. }
  1528. return $result;
  1529. }
  1530. /**
  1531. * Validates the time control key.
  1532. *
  1533. * @param int $exercise_id
  1534. * @param int $lp_id
  1535. * @param int $lp_item_id
  1536. *
  1537. * @return bool
  1538. */
  1539. public static function exercise_time_control_is_valid(
  1540. $exercise_id,
  1541. $lp_id = 0,
  1542. $lp_item_id = 0
  1543. ) {
  1544. $course_id = api_get_course_int_id();
  1545. $exercise_id = (int) $exercise_id;
  1546. $table = Database::get_course_table(TABLE_QUIZ_TEST);
  1547. $sql = "SELECT expired_time FROM $table
  1548. WHERE c_id = $course_id AND id = $exercise_id";
  1549. $result = Database::query($sql);
  1550. $row = Database::fetch_array($result, 'ASSOC');
  1551. if (!empty($row['expired_time'])) {
  1552. $current_expired_time_key = self::get_time_control_key(
  1553. $exercise_id,
  1554. $lp_id,
  1555. $lp_item_id
  1556. );
  1557. if (isset($_SESSION['expired_time'][$current_expired_time_key])) {
  1558. $current_time = time();
  1559. $expired_time = api_strtotime(
  1560. $_SESSION['expired_time'][$current_expired_time_key],
  1561. 'UTC'
  1562. );
  1563. $total_time_allowed = $expired_time + 30;
  1564. if ($total_time_allowed < $current_time) {
  1565. return false;
  1566. }
  1567. return true;
  1568. }
  1569. return false;
  1570. }
  1571. return true;
  1572. }
  1573. /**
  1574. * Deletes the time control token.
  1575. *
  1576. * @param int $exercise_id
  1577. * @param int $lp_id
  1578. * @param int $lp_item_id
  1579. */
  1580. public static function exercise_time_control_delete(
  1581. $exercise_id,
  1582. $lp_id = 0,
  1583. $lp_item_id = 0
  1584. ) {
  1585. $current_expired_time_key = self::get_time_control_key(
  1586. $exercise_id,
  1587. $lp_id,
  1588. $lp_item_id
  1589. );
  1590. unset($_SESSION['expired_time'][$current_expired_time_key]);
  1591. }
  1592. /**
  1593. * Generates the time control key.
  1594. *
  1595. * @param int $exercise_id
  1596. * @param int $lp_id
  1597. * @param int $lp_item_id
  1598. *
  1599. * @return string
  1600. */
  1601. public static function get_time_control_key(
  1602. $exercise_id,
  1603. $lp_id = 0,
  1604. $lp_item_id = 0
  1605. ) {
  1606. $exercise_id = (int) $exercise_id;
  1607. $lp_id = (int) $lp_id;
  1608. $lp_item_id = (int) $lp_item_id;
  1609. return
  1610. api_get_course_int_id().'_'.
  1611. api_get_session_id().'_'.
  1612. $exercise_id.'_'.
  1613. api_get_user_id().'_'.
  1614. $lp_id.'_'.
  1615. $lp_item_id;
  1616. }
  1617. /**
  1618. * Get session time control.
  1619. *
  1620. * @param int $exercise_id
  1621. * @param int $lp_id
  1622. * @param int $lp_item_id
  1623. *
  1624. * @return int
  1625. */
  1626. public static function get_session_time_control_key(
  1627. $exercise_id,
  1628. $lp_id = 0,
  1629. $lp_item_id = 0
  1630. ) {
  1631. $return_value = 0;
  1632. $time_control_key = self::get_time_control_key(
  1633. $exercise_id,
  1634. $lp_id,
  1635. $lp_item_id
  1636. );
  1637. if (isset($_SESSION['expired_time']) && isset($_SESSION['expired_time'][$time_control_key])) {
  1638. $return_value = $_SESSION['expired_time'][$time_control_key];
  1639. }
  1640. return $return_value;
  1641. }
  1642. /**
  1643. * Gets count of exam results.
  1644. *
  1645. * @param int $exerciseId
  1646. * @param array $conditions
  1647. * @param string $courseCode
  1648. * @param bool $showSession
  1649. *
  1650. * @return array
  1651. */
  1652. public static function get_count_exam_results($exerciseId, $conditions, $courseCode = '', $showSession = false)
  1653. {
  1654. $count = self::get_exam_results_data(
  1655. null,
  1656. null,
  1657. null,
  1658. null,
  1659. $exerciseId,
  1660. $conditions,
  1661. true,
  1662. $courseCode,
  1663. $showSession
  1664. );
  1665. return $count;
  1666. }
  1667. /**
  1668. * @param string $path
  1669. *
  1670. * @return int
  1671. */
  1672. public static function get_count_exam_hotpotatoes_results($path)
  1673. {
  1674. return self::get_exam_results_hotpotatoes_data(
  1675. 0,
  1676. 0,
  1677. '',
  1678. '',
  1679. $path,
  1680. true,
  1681. ''
  1682. );
  1683. }
  1684. /**
  1685. * @param int $in_from
  1686. * @param int $in_number_of_items
  1687. * @param int $in_column
  1688. * @param int $in_direction
  1689. * @param string $in_hotpot_path
  1690. * @param bool $in_get_count
  1691. * @param null $where_condition
  1692. *
  1693. * @return array|int
  1694. */
  1695. public static function get_exam_results_hotpotatoes_data(
  1696. $in_from,
  1697. $in_number_of_items,
  1698. $in_column,
  1699. $in_direction,
  1700. $in_hotpot_path,
  1701. $in_get_count = false,
  1702. $where_condition = null
  1703. ) {
  1704. $courseId = api_get_course_int_id();
  1705. // by default in_column = 1 If parameters given, it is the name of the column witch is the bdd field name
  1706. if ($in_column == 1) {
  1707. $in_column = 'firstname';
  1708. }
  1709. $in_hotpot_path = Database::escape_string($in_hotpot_path);
  1710. $in_direction = Database::escape_string($in_direction);
  1711. $in_column = Database::escape_string($in_column);
  1712. $in_number_of_items = intval($in_number_of_items);
  1713. $in_from = (int) $in_from;
  1714. $TBL_TRACK_HOTPOTATOES = Database::get_main_table(
  1715. TABLE_STATISTIC_TRACK_E_HOTPOTATOES
  1716. );
  1717. $TBL_USER = Database::get_main_table(TABLE_MAIN_USER);
  1718. $sql = "SELECT *, thp.id AS thp_id
  1719. FROM $TBL_TRACK_HOTPOTATOES thp
  1720. JOIN $TBL_USER u
  1721. ON thp.exe_user_id = u.user_id
  1722. WHERE
  1723. thp.c_id = $courseId AND
  1724. exe_name LIKE '$in_hotpot_path%'";
  1725. // just count how many answers
  1726. if ($in_get_count) {
  1727. $res = Database::query($sql);
  1728. return Database::num_rows($res);
  1729. }
  1730. // get a number of sorted results
  1731. $sql .= " $where_condition
  1732. ORDER BY $in_column $in_direction
  1733. LIMIT $in_from, $in_number_of_items";
  1734. $res = Database::query($sql);
  1735. $result = [];
  1736. $apiIsAllowedToEdit = api_is_allowed_to_edit();
  1737. $urlBase = api_get_path(WEB_CODE_PATH).
  1738. 'exercise/hotpotatoes_exercise_report.php?action=delete&'.
  1739. api_get_cidreq().'&id=';
  1740. while ($data = Database::fetch_array($res)) {
  1741. $actions = null;
  1742. if ($apiIsAllowedToEdit) {
  1743. $url = $urlBase.$data['thp_id'].'&path='.$data['exe_name'];
  1744. $actions = Display::url(
  1745. Display::return_icon('delete.png', get_lang('Delete')),
  1746. $url
  1747. );
  1748. }
  1749. $result[] = [
  1750. 'firstname' => $data['firstname'],
  1751. 'lastname' => $data['lastname'],
  1752. 'username' => $data['username'],
  1753. 'group_name' => implode(
  1754. '<br/>',
  1755. GroupManager::get_user_group_name($data['user_id'])
  1756. ),
  1757. 'exe_date' => $data['exe_date'],
  1758. 'score' => $data['exe_result'].' / '.$data['exe_weighting'],
  1759. 'actions' => $actions,
  1760. ];
  1761. }
  1762. return $result;
  1763. }
  1764. /**
  1765. * @param string $exercisePath
  1766. * @param int $userId
  1767. * @param int $courseId
  1768. * @param int $sessionId
  1769. *
  1770. * @return array
  1771. */
  1772. public static function getLatestHotPotatoResult(
  1773. $exercisePath,
  1774. $userId,
  1775. $courseId,
  1776. $sessionId
  1777. ) {
  1778. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  1779. $exercisePath = Database::escape_string($exercisePath);
  1780. $userId = (int) $userId;
  1781. $courseId = (int) $courseId;
  1782. $sql = "SELECT * FROM $table
  1783. WHERE
  1784. c_id = $courseId AND
  1785. exe_name LIKE '$exercisePath%' AND
  1786. exe_user_id = $userId
  1787. ORDER BY id
  1788. LIMIT 1";
  1789. $result = Database::query($sql);
  1790. $attempt = [];
  1791. if (Database::num_rows($result)) {
  1792. $attempt = Database::fetch_array($result, 'ASSOC');
  1793. }
  1794. return $attempt;
  1795. }
  1796. /**
  1797. * Gets the exam'data results.
  1798. *
  1799. * @todo this function should be moved in a library + no global calls
  1800. *
  1801. * @param int $from
  1802. * @param int $number_of_items
  1803. * @param int $column
  1804. * @param string $direction
  1805. * @param int $exercise_id
  1806. * @param null $extra_where_conditions
  1807. * @param bool $get_count
  1808. * @param string $courseCode
  1809. * @param bool $showSessionField
  1810. * @param bool $showExerciseCategories
  1811. * @param array $userExtraFieldsToAdd
  1812. * @param bool $useCommaAsDecimalPoint
  1813. * @param bool $roundValues
  1814. *
  1815. * @return array
  1816. */
  1817. public static function get_exam_results_data(
  1818. $from,
  1819. $number_of_items,
  1820. $column,
  1821. $direction,
  1822. $exercise_id,
  1823. $extra_where_conditions = null,
  1824. $get_count = false,
  1825. $courseCode = null,
  1826. $showSessionField = false,
  1827. $showExerciseCategories = false,
  1828. $userExtraFieldsToAdd = [],
  1829. $useCommaAsDecimalPoint = false,
  1830. $roundValues = false
  1831. ) {
  1832. //@todo replace all this globals
  1833. global $filter;
  1834. $courseCode = empty($courseCode) ? api_get_course_id() : $courseCode;
  1835. $courseInfo = api_get_course_info($courseCode);
  1836. if (empty($courseInfo)) {
  1837. return [];
  1838. }
  1839. $documentPath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document';
  1840. $course_id = $courseInfo['real_id'];
  1841. $sessionId = api_get_session_id();
  1842. $exercise_id = (int) $exercise_id;
  1843. $is_allowedToEdit =
  1844. api_is_allowed_to_edit(null, true) ||
  1845. api_is_allowed_to_edit(true) ||
  1846. api_is_drh() ||
  1847. api_is_student_boss() ||
  1848. api_is_session_admin();
  1849. $TBL_USER = Database::get_main_table(TABLE_MAIN_USER);
  1850. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  1851. $TBL_GROUP_REL_USER = Database::get_course_table(TABLE_GROUP_USER);
  1852. $TBL_GROUP = Database::get_course_table(TABLE_GROUP);
  1853. $TBL_TRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1854. $TBL_TRACK_HOTPOTATOES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  1855. $TBL_TRACK_ATTEMPT_RECORDING = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
  1856. $session_id_and = '';
  1857. $sessionCondition = '';
  1858. if (!$showSessionField) {
  1859. $session_id_and = " AND te.session_id = $sessionId ";
  1860. $sessionCondition = " AND ttte.session_id = $sessionId";
  1861. }
  1862. $exercise_where = '';
  1863. if (!empty($exercise_id)) {
  1864. $exercise_where .= ' AND te.exe_exo_id = '.$exercise_id.' ';
  1865. }
  1866. $hotpotatoe_where = '';
  1867. if (!empty($_GET['path'])) {
  1868. $hotpotatoe_path = Database::escape_string($_GET['path']);
  1869. $hotpotatoe_where .= ' AND exe_name = "'.$hotpotatoe_path.'" ';
  1870. }
  1871. // sql for chamilo-type tests for teacher / tutor view
  1872. $sql_inner_join_tbl_track_exercices = "
  1873. (
  1874. SELECT DISTINCT ttte.*, if(tr.exe_id,1, 0) as revised
  1875. FROM $TBL_TRACK_EXERCICES ttte
  1876. LEFT JOIN $TBL_TRACK_ATTEMPT_RECORDING tr
  1877. ON (ttte.exe_id = tr.exe_id)
  1878. WHERE
  1879. c_id = $course_id AND
  1880. exe_exo_id = $exercise_id
  1881. $sessionCondition
  1882. )";
  1883. if ($is_allowedToEdit) {
  1884. //@todo fix to work with COURSE_RELATION_TYPE_RRHH in both queries
  1885. // Hack in order to filter groups
  1886. $sql_inner_join_tbl_user = '';
  1887. if (strpos($extra_where_conditions, 'group_id')) {
  1888. $sql_inner_join_tbl_user = "
  1889. (
  1890. SELECT
  1891. u.user_id,
  1892. firstname,
  1893. lastname,
  1894. official_code,
  1895. email,
  1896. username,
  1897. g.name as group_name,
  1898. g.id as group_id
  1899. FROM $TBL_USER u
  1900. INNER JOIN $TBL_GROUP_REL_USER gru
  1901. ON (gru.user_id = u.user_id AND gru.c_id= $course_id )
  1902. INNER JOIN $TBL_GROUP g
  1903. ON (gru.group_id = g.id AND g.c_id= $course_id )
  1904. )";
  1905. }
  1906. if (strpos($extra_where_conditions, 'group_all')) {
  1907. $extra_where_conditions = str_replace(
  1908. "AND ( group_id = 'group_all' )",
  1909. '',
  1910. $extra_where_conditions
  1911. );
  1912. $extra_where_conditions = str_replace(
  1913. "AND group_id = 'group_all'",
  1914. '',
  1915. $extra_where_conditions
  1916. );
  1917. $extra_where_conditions = str_replace(
  1918. "group_id = 'group_all' AND",
  1919. '',
  1920. $extra_where_conditions
  1921. );
  1922. $sql_inner_join_tbl_user = "
  1923. (
  1924. SELECT
  1925. u.user_id,
  1926. firstname,
  1927. lastname,
  1928. official_code,
  1929. email,
  1930. username,
  1931. '' as group_name,
  1932. '' as group_id
  1933. FROM $TBL_USER u
  1934. )";
  1935. $sql_inner_join_tbl_user = null;
  1936. }
  1937. if (strpos($extra_where_conditions, 'group_none')) {
  1938. $extra_where_conditions = str_replace(
  1939. "AND ( group_id = 'group_none' )",
  1940. "AND ( group_id is null )",
  1941. $extra_where_conditions
  1942. );
  1943. $extra_where_conditions = str_replace(
  1944. "AND group_id = 'group_none'",
  1945. "AND ( group_id is null )",
  1946. $extra_where_conditions
  1947. );
  1948. $sql_inner_join_tbl_user = "
  1949. (
  1950. SELECT
  1951. u.user_id,
  1952. firstname,
  1953. lastname,
  1954. official_code,
  1955. email,
  1956. username,
  1957. g.name as group_name,
  1958. g.id as group_id
  1959. FROM $TBL_USER u
  1960. LEFT OUTER JOIN $TBL_GROUP_REL_USER gru
  1961. ON ( gru.user_id = u.user_id AND gru.c_id= $course_id )
  1962. LEFT OUTER JOIN $TBL_GROUP g
  1963. ON (gru.group_id = g.id AND g.c_id = $course_id )
  1964. )";
  1965. }
  1966. // All
  1967. $is_empty_sql_inner_join_tbl_user = false;
  1968. if (empty($sql_inner_join_tbl_user)) {
  1969. $is_empty_sql_inner_join_tbl_user = true;
  1970. $sql_inner_join_tbl_user = "
  1971. (
  1972. SELECT u.user_id, firstname, lastname, email, username, ' ' as group_name, '' as group_id, official_code
  1973. FROM $TBL_USER u
  1974. WHERE u.status NOT IN(".api_get_users_status_ignored_in_reports('string').")
  1975. )";
  1976. }
  1977. $sqlFromOption = " , $TBL_GROUP_REL_USER AS gru ";
  1978. $sqlWhereOption = " AND gru.c_id = $course_id AND gru.user_id = user.user_id ";
  1979. $first_and_last_name = api_is_western_name_order() ? "firstname, lastname" : "lastname, firstname";
  1980. if ($get_count) {
  1981. $sql_select = 'SELECT count(te.exe_id) ';
  1982. } else {
  1983. $sql_select = "SELECT DISTINCT
  1984. user_id,
  1985. $first_and_last_name,
  1986. official_code,
  1987. ce.title,
  1988. username,
  1989. te.exe_result,
  1990. te.exe_weighting,
  1991. te.exe_date,
  1992. te.exe_id,
  1993. te.session_id,
  1994. email as exemail,
  1995. te.start_date,
  1996. ce.expired_time,
  1997. steps_counter,
  1998. exe_user_id,
  1999. te.exe_duration,
  2000. te.status as completion_status,
  2001. propagate_neg,
  2002. revised,
  2003. group_name,
  2004. group_id,
  2005. orig_lp_id,
  2006. te.user_ip";
  2007. }
  2008. $sql = " $sql_select
  2009. FROM $TBL_EXERCICES AS ce
  2010. INNER JOIN $sql_inner_join_tbl_track_exercices AS te
  2011. ON (te.exe_exo_id = ce.id)
  2012. INNER JOIN $sql_inner_join_tbl_user AS user
  2013. ON (user.user_id = exe_user_id)
  2014. WHERE
  2015. te.c_id = $course_id $session_id_and AND
  2016. ce.active <> -1 AND
  2017. ce.c_id = $course_id
  2018. $exercise_where
  2019. $extra_where_conditions
  2020. ";
  2021. // sql for hotpotatoes tests for teacher / tutor view
  2022. if ($get_count) {
  2023. $hpsql_select = ' SELECT count(username) ';
  2024. } else {
  2025. $hpsql_select = " SELECT
  2026. $first_and_last_name ,
  2027. username,
  2028. official_code,
  2029. tth.exe_name,
  2030. tth.exe_result ,
  2031. tth.exe_weighting,
  2032. tth.exe_date";
  2033. }
  2034. $hpsql = " $hpsql_select
  2035. FROM
  2036. $TBL_TRACK_HOTPOTATOES tth,
  2037. $TBL_USER user
  2038. $sqlFromOption
  2039. WHERE
  2040. user.user_id=tth.exe_user_id
  2041. AND tth.c_id = $course_id
  2042. $hotpotatoe_where
  2043. $sqlWhereOption
  2044. AND user.status NOT IN (".api_get_users_status_ignored_in_reports('string').")
  2045. ORDER BY tth.c_id ASC, tth.exe_date DESC ";
  2046. }
  2047. if (empty($sql)) {
  2048. return false;
  2049. }
  2050. if ($get_count) {
  2051. $resx = Database::query($sql);
  2052. $rowx = Database::fetch_row($resx, 'ASSOC');
  2053. return $rowx[0];
  2054. }
  2055. $teacher_list = CourseManager::get_teacher_list_from_course_code($courseCode);
  2056. $teacher_id_list = [];
  2057. if (!empty($teacher_list)) {
  2058. foreach ($teacher_list as $teacher) {
  2059. $teacher_id_list[] = $teacher['user_id'];
  2060. }
  2061. }
  2062. $scoreDisplay = new ScoreDisplay();
  2063. $decimalSeparator = '.';
  2064. $thousandSeparator = ',';
  2065. if ($useCommaAsDecimalPoint) {
  2066. $decimalSeparator = ',';
  2067. $thousandSeparator = '';
  2068. }
  2069. $listInfo = [];
  2070. // Simple exercises
  2071. if (empty($hotpotatoe_where)) {
  2072. $column = !empty($column) ? Database::escape_string($column) : null;
  2073. $from = (int) $from;
  2074. $number_of_items = (int) $number_of_items;
  2075. if (!empty($column)) {
  2076. $sql .= " ORDER BY $column $direction ";
  2077. }
  2078. $sql .= " LIMIT $from, $number_of_items";
  2079. $results = [];
  2080. $resx = Database::query($sql);
  2081. while ($rowx = Database::fetch_array($resx, 'ASSOC')) {
  2082. $results[] = $rowx;
  2083. }
  2084. $group_list = GroupManager::get_group_list(null, $courseInfo);
  2085. $clean_group_list = [];
  2086. if (!empty($group_list)) {
  2087. foreach ($group_list as $group) {
  2088. $clean_group_list[$group['id']] = $group['name'];
  2089. }
  2090. }
  2091. $lp_list_obj = new LearnpathList(api_get_user_id());
  2092. $lp_list = $lp_list_obj->get_flat_list();
  2093. $oldIds = array_column($lp_list, 'lp_old_id', 'iid');
  2094. if (is_array($results)) {
  2095. $users_array_id = [];
  2096. $from_gradebook = false;
  2097. if (isset($_GET['gradebook']) && $_GET['gradebook'] == 'view') {
  2098. $from_gradebook = true;
  2099. }
  2100. $sizeof = count($results);
  2101. $locked = api_resource_is_locked_by_gradebook(
  2102. $exercise_id,
  2103. LINK_EXERCISE
  2104. );
  2105. $timeNow = strtotime(api_get_utc_datetime());
  2106. // Looping results
  2107. for ($i = 0; $i < $sizeof; $i++) {
  2108. $revised = $results[$i]['revised'];
  2109. if ($results[$i]['completion_status'] == 'incomplete') {
  2110. // If the exercise was incomplete, we need to determine
  2111. // if it is still into the time allowed, or if its
  2112. // allowed time has expired and it can be closed
  2113. // (it's "unclosed")
  2114. $minutes = $results[$i]['expired_time'];
  2115. if ($minutes == 0) {
  2116. // There's no time limit, so obviously the attempt
  2117. // can still be "ongoing", but the teacher should
  2118. // be able to choose to close it, so mark it as
  2119. // "unclosed" instead of "ongoing"
  2120. $revised = 2;
  2121. } else {
  2122. $allowedSeconds = $minutes * 60;
  2123. $timeAttemptStarted = strtotime($results[$i]['start_date']);
  2124. $secondsSinceStart = $timeNow - $timeAttemptStarted;
  2125. if ($secondsSinceStart > $allowedSeconds) {
  2126. $revised = 2; // mark as "unclosed"
  2127. } else {
  2128. $revised = 3; // mark as "ongoing"
  2129. }
  2130. }
  2131. }
  2132. if ($from_gradebook && ($is_allowedToEdit)) {
  2133. if (in_array(
  2134. $results[$i]['username'].$results[$i]['firstname'].$results[$i]['lastname'],
  2135. $users_array_id
  2136. )) {
  2137. continue;
  2138. }
  2139. $users_array_id[] = $results[$i]['username'].$results[$i]['firstname'].$results[$i]['lastname'];
  2140. }
  2141. $lp_obj = isset($results[$i]['orig_lp_id']) && isset($lp_list[$results[$i]['orig_lp_id']]) ? $lp_list[$results[$i]['orig_lp_id']] : null;
  2142. if (empty($lp_obj)) {
  2143. // Try to get the old id (id instead of iid)
  2144. $lpNewId = isset($results[$i]['orig_lp_id']) && isset($oldIds[$results[$i]['orig_lp_id']]) ? $oldIds[$results[$i]['orig_lp_id']] : null;
  2145. if ($lpNewId) {
  2146. $lp_obj = isset($lp_list[$lpNewId]) ? $lp_list[$lpNewId] : null;
  2147. }
  2148. }
  2149. $lp_name = null;
  2150. if ($lp_obj) {
  2151. $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq().'&action=view&lp_id='.$results[$i]['orig_lp_id'];
  2152. $lp_name = Display::url(
  2153. $lp_obj['lp_name'],
  2154. $url,
  2155. ['target' => '_blank']
  2156. );
  2157. }
  2158. // Add all groups by user
  2159. $group_name_list = '';
  2160. if ($is_empty_sql_inner_join_tbl_user) {
  2161. $group_list = GroupManager::get_group_ids(
  2162. api_get_course_int_id(),
  2163. $results[$i]['user_id']
  2164. );
  2165. foreach ($group_list as $id) {
  2166. if (isset($clean_group_list[$id])) {
  2167. $group_name_list .= $clean_group_list[$id].'<br/>';
  2168. }
  2169. }
  2170. $results[$i]['group_name'] = $group_name_list;
  2171. }
  2172. $results[$i]['exe_duration'] = !empty($results[$i]['exe_duration']) ? round($results[$i]['exe_duration'] / 60) : 0;
  2173. $id = $results[$i]['exe_id'];
  2174. $dt = api_convert_and_format_date($results[$i]['exe_weighting']);
  2175. // we filter the results if we have the permission to
  2176. $result_disabled = 0;
  2177. if (isset($results[$i]['results_disabled'])) {
  2178. $result_disabled = (int) $results[$i]['results_disabled'];
  2179. }
  2180. if ($result_disabled == 0) {
  2181. $my_res = $results[$i]['exe_result'];
  2182. $my_total = $results[$i]['exe_weighting'];
  2183. $results[$i]['start_date'] = api_get_local_time($results[$i]['start_date']);
  2184. $results[$i]['exe_date'] = api_get_local_time($results[$i]['exe_date']);
  2185. if (!$results[$i]['propagate_neg'] && $my_res < 0) {
  2186. $my_res = 0;
  2187. }
  2188. $score = self::show_score(
  2189. $my_res,
  2190. $my_total,
  2191. true,
  2192. true,
  2193. false,
  2194. false,
  2195. $decimalSeparator,
  2196. $thousandSeparator,
  2197. $roundValues
  2198. );
  2199. $actions = '<div class="pull-right">';
  2200. if ($is_allowedToEdit) {
  2201. if (isset($teacher_id_list)) {
  2202. if (in_array(
  2203. $results[$i]['exe_user_id'],
  2204. $teacher_id_list
  2205. )) {
  2206. $actions .= Display::return_icon('teacher.png', get_lang('Teacher'));
  2207. }
  2208. }
  2209. $revisedLabel = '';
  2210. switch ($revised) {
  2211. case 0:
  2212. $actions .= "<a href='exercise_show.php?".api_get_cidreq()."&action=qualify&id=$id'>".
  2213. Display:: return_icon(
  2214. 'quiz.png',
  2215. get_lang('Qualify')
  2216. );
  2217. $actions .= '</a>';
  2218. $revisedLabel = Display::label(
  2219. get_lang('NotValidated'),
  2220. 'info'
  2221. );
  2222. break;
  2223. case 1:
  2224. $actions .= "<a href='exercise_show.php?".api_get_cidreq()."&action=edit&id=$id'>".
  2225. Display:: return_icon(
  2226. 'edit.png',
  2227. get_lang('Edit'),
  2228. [],
  2229. ICON_SIZE_SMALL
  2230. );
  2231. $actions .= '</a>';
  2232. $revisedLabel = Display::label(
  2233. get_lang('Validated'),
  2234. 'success'
  2235. );
  2236. break;
  2237. case 2: //finished but not marked as such
  2238. $actions .= '<a href="exercise_report.php?'
  2239. .api_get_cidreq()
  2240. .'&exerciseId='
  2241. .$exercise_id
  2242. .'&a=close&id='
  2243. .$id
  2244. .'">'.
  2245. Display:: return_icon(
  2246. 'lock.png',
  2247. get_lang('MarkAttemptAsClosed'),
  2248. [],
  2249. ICON_SIZE_SMALL
  2250. );
  2251. $actions .= '</a>';
  2252. $revisedLabel = Display::label(
  2253. get_lang('Unclosed'),
  2254. 'warning'
  2255. );
  2256. break;
  2257. case 3: //still ongoing
  2258. $actions .= Display:: return_icon(
  2259. 'clock.png',
  2260. get_lang('AttemptStillOngoingPleaseWait'),
  2261. [],
  2262. ICON_SIZE_SMALL
  2263. );
  2264. $actions .= '';
  2265. $revisedLabel = Display::label(
  2266. get_lang('Ongoing'),
  2267. 'danger'
  2268. );
  2269. break;
  2270. }
  2271. if ($filter == 2) {
  2272. $actions .= ' <a href="exercise_history.php?'.api_get_cidreq().'&exe_id='.$id.'">'.
  2273. Display:: return_icon(
  2274. 'history.png',
  2275. get_lang('ViewHistoryChange')
  2276. ).'</a>';
  2277. }
  2278. // Admin can always delete the attempt
  2279. if (($locked == false || api_is_platform_admin()) && !api_is_student_boss()) {
  2280. $ip = Tracking::get_ip_from_user_event(
  2281. $results[$i]['exe_user_id'],
  2282. api_get_utc_datetime(),
  2283. false
  2284. );
  2285. $actions .= '<a href="http://www.whatsmyip.org/ip-geo-location/?ip='.$ip.'" target="_blank">'
  2286. .Display::return_icon('info.png', $ip)
  2287. .'</a>';
  2288. $recalculateUrl = api_get_path(WEB_CODE_PATH).'exercise/recalculate.php?'.
  2289. api_get_cidreq().'&'.
  2290. http_build_query([
  2291. 'id' => $id,
  2292. 'exercise' => $exercise_id,
  2293. 'user' => $results[$i]['exe_user_id'],
  2294. ]);
  2295. $actions .= Display::url(
  2296. Display::return_icon('reload.png', get_lang('RecalculateResults')),
  2297. $recalculateUrl,
  2298. [
  2299. 'data-exercise' => $exercise_id,
  2300. 'data-user' => $results[$i]['exe_user_id'],
  2301. 'data-id' => $id,
  2302. 'class' => 'exercise-recalculate',
  2303. ]
  2304. );
  2305. $filterByUser = isset($_GET['filter_by_user']) ? (int) $_GET['filter_by_user'] : 0;
  2306. $delete_link = '<a href="exercise_report.php?'.api_get_cidreq().'&filter_by_user='.$filterByUser.'&filter='.$filter.'&exerciseId='.$exercise_id.'&delete=delete&did='.$id.'"
  2307. onclick="javascript:if(!confirm(\''.sprintf(
  2308. addslashes(get_lang('DeleteAttempt')),
  2309. $results[$i]['username'],
  2310. $dt
  2311. ).'\')) return false;">';
  2312. $delete_link .= Display::return_icon(
  2313. 'delete.png',
  2314. addslashes(get_lang('Delete'))
  2315. ).'</a>';
  2316. if (api_is_drh() && !api_is_platform_admin()) {
  2317. $delete_link = null;
  2318. }
  2319. if (api_is_session_admin()) {
  2320. $delete_link = '';
  2321. }
  2322. if ($revised == 3) {
  2323. $delete_link = null;
  2324. }
  2325. $actions .= $delete_link;
  2326. }
  2327. } else {
  2328. $attempt_url = api_get_path(WEB_CODE_PATH).'exercise/result.php?'.api_get_cidreq().'&id='.$results[$i]['exe_id'].'&id_session='.$sessionId;
  2329. $attempt_link = Display::url(
  2330. get_lang('Show'),
  2331. $attempt_url,
  2332. [
  2333. 'class' => 'ajax btn btn-default',
  2334. 'data-title' => get_lang('Show'),
  2335. ]
  2336. );
  2337. $actions .= $attempt_link;
  2338. }
  2339. $actions .= '</div>';
  2340. if (!empty($userExtraFieldsToAdd)) {
  2341. foreach ($userExtraFieldsToAdd as $variable) {
  2342. $extraFieldValue = new ExtraFieldValue('user');
  2343. $values = $extraFieldValue->get_values_by_handler_and_field_variable(
  2344. $results[$i]['user_id'],
  2345. $variable
  2346. );
  2347. if (isset($values['value'])) {
  2348. $results[$i][$variable] = $values['value'];
  2349. }
  2350. }
  2351. }
  2352. $exeId = $results[$i]['exe_id'];
  2353. $results[$i]['id'] = $exeId;
  2354. $category_list = [];
  2355. if ($is_allowedToEdit) {
  2356. $sessionName = '';
  2357. $sessionStartAccessDate = '';
  2358. if (!empty($results[$i]['session_id'])) {
  2359. $sessionInfo = api_get_session_info($results[$i]['session_id']);
  2360. if (!empty($sessionInfo)) {
  2361. $sessionName = $sessionInfo['name'];
  2362. $sessionStartAccessDate = api_get_local_time($sessionInfo['access_start_date']);
  2363. }
  2364. }
  2365. $objExercise = new Exercise($course_id);
  2366. if ($showExerciseCategories) {
  2367. // Getting attempt info
  2368. $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exeId);
  2369. if (!empty($exercise_stat_info['data_tracking'])) {
  2370. $question_list = explode(',', $exercise_stat_info['data_tracking']);
  2371. if (!empty($question_list)) {
  2372. foreach ($question_list as $questionId) {
  2373. $objQuestionTmp = Question::read($questionId, $objExercise->course);
  2374. // We're inside *one* question. Go through each possible answer for this question
  2375. $result = $objExercise->manage_answer(
  2376. $exeId,
  2377. $questionId,
  2378. null,
  2379. 'exercise_result',
  2380. false,
  2381. false,
  2382. true,
  2383. false,
  2384. $objExercise->selectPropagateNeg(),
  2385. null,
  2386. true
  2387. );
  2388. $my_total_score = $result['score'];
  2389. $my_total_weight = $result['weight'];
  2390. // Category report
  2391. $category_was_added_for_this_test = false;
  2392. if (isset($objQuestionTmp->category) && !empty($objQuestionTmp->category)) {
  2393. if (!isset($category_list[$objQuestionTmp->category]['score'])) {
  2394. $category_list[$objQuestionTmp->category]['score'] = 0;
  2395. }
  2396. if (!isset($category_list[$objQuestionTmp->category]['total'])) {
  2397. $category_list[$objQuestionTmp->category]['total'] = 0;
  2398. }
  2399. $category_list[$objQuestionTmp->category]['score'] += $my_total_score;
  2400. $category_list[$objQuestionTmp->category]['total'] += $my_total_weight;
  2401. $category_was_added_for_this_test = true;
  2402. }
  2403. if (isset($objQuestionTmp->category_list) &&
  2404. !empty($objQuestionTmp->category_list)
  2405. ) {
  2406. foreach ($objQuestionTmp->category_list as $category_id) {
  2407. $category_list[$category_id]['score'] += $my_total_score;
  2408. $category_list[$category_id]['total'] += $my_total_weight;
  2409. $category_was_added_for_this_test = true;
  2410. }
  2411. }
  2412. // No category for this question!
  2413. if ($category_was_added_for_this_test == false) {
  2414. if (!isset($category_list['none']['score'])) {
  2415. $category_list['none']['score'] = 0;
  2416. }
  2417. if (!isset($category_list['none']['total'])) {
  2418. $category_list['none']['total'] = 0;
  2419. }
  2420. $category_list['none']['score'] += $my_total_score;
  2421. $category_list['none']['total'] += $my_total_weight;
  2422. }
  2423. }
  2424. }
  2425. }
  2426. }
  2427. foreach ($category_list as $categoryId => $result) {
  2428. $scoreToDisplay = self::show_score(
  2429. $result['score'],
  2430. $result['total'],
  2431. true,
  2432. true,
  2433. false,
  2434. false,
  2435. $decimalSeparator,
  2436. $thousandSeparator,
  2437. $roundValues
  2438. );
  2439. $results[$i]['category_'.$categoryId] = $scoreToDisplay;
  2440. $results[$i]['category_'.$categoryId.'_score_percentage'] = self::show_score(
  2441. $result['score'],
  2442. $result['total'],
  2443. true,
  2444. true,
  2445. true, // $show_only_percentage = false
  2446. true, // hide % sign
  2447. $decimalSeparator,
  2448. $thousandSeparator,
  2449. $roundValues
  2450. );
  2451. $results[$i]['category_'.$categoryId.'_only_score'] = $result['score'];
  2452. $results[$i]['category_'.$categoryId.'_total'] = $result['total'];
  2453. }
  2454. $results[$i]['session'] = $sessionName;
  2455. $results[$i]['session_access_start_date'] = $sessionStartAccessDate;
  2456. $results[$i]['status'] = $revisedLabel;
  2457. $results[$i]['score'] = $score;
  2458. $results[$i]['score_percentage'] = self::show_score(
  2459. $my_res,
  2460. $my_total,
  2461. true,
  2462. true,
  2463. true,
  2464. true,
  2465. $decimalSeparator,
  2466. $thousandSeparator,
  2467. $roundValues
  2468. );
  2469. if ($roundValues) {
  2470. $whole = floor($my_res); // 1
  2471. $fraction = $my_res - $whole; // .25
  2472. if ($fraction >= 0.5) {
  2473. $onlyScore = ceil($my_res);
  2474. } else {
  2475. $onlyScore = round($my_res);
  2476. }
  2477. } else {
  2478. $onlyScore = $scoreDisplay->format_score(
  2479. $my_res,
  2480. false,
  2481. $decimalSeparator,
  2482. $thousandSeparator
  2483. );
  2484. }
  2485. $results[$i]['only_score'] = $onlyScore;
  2486. if ($roundValues) {
  2487. $whole = floor($my_total); // 1
  2488. $fraction = $my_total - $whole; // .25
  2489. if ($fraction >= 0.5) {
  2490. $onlyTotal = ceil($my_total);
  2491. } else {
  2492. $onlyTotal = round($my_total);
  2493. }
  2494. } else {
  2495. $onlyTotal = $scoreDisplay->format_score(
  2496. $my_total,
  2497. false,
  2498. $decimalSeparator,
  2499. $thousandSeparator
  2500. );
  2501. }
  2502. $results[$i]['total'] = $onlyTotal;
  2503. $results[$i]['lp'] = $lp_name;
  2504. $results[$i]['actions'] = $actions;
  2505. $listInfo[] = $results[$i];
  2506. } else {
  2507. $results[$i]['status'] = $revisedLabel;
  2508. $results[$i]['score'] = $score;
  2509. $results[$i]['actions'] = $actions;
  2510. $listInfo[] = $results[$i];
  2511. }
  2512. }
  2513. }
  2514. }
  2515. } else {
  2516. $hpresults = [];
  2517. $res = Database::query($hpsql);
  2518. if ($res !== false) {
  2519. $i = 0;
  2520. while ($resA = Database::fetch_array($res, 'NUM')) {
  2521. for ($j = 0; $j < 6; $j++) {
  2522. $hpresults[$i][$j] = $resA[$j];
  2523. }
  2524. $i++;
  2525. }
  2526. }
  2527. // Print HotPotatoes test results.
  2528. if (is_array($hpresults)) {
  2529. for ($i = 0; $i < count($hpresults); $i++) {
  2530. $hp_title = GetQuizName($hpresults[$i][3], $documentPath);
  2531. if ($hp_title == '') {
  2532. $hp_title = basename($hpresults[$i][3]);
  2533. }
  2534. $hp_date = api_get_local_time(
  2535. $hpresults[$i][6],
  2536. null,
  2537. date_default_timezone_get()
  2538. );
  2539. $hp_result = round(($hpresults[$i][4] / ($hpresults[$i][5] != 0 ? $hpresults[$i][5] : 1)) * 100, 2);
  2540. $hp_result .= '% ('.$hpresults[$i][4].' / '.$hpresults[$i][5].')';
  2541. if ($is_allowedToEdit) {
  2542. $listInfo[] = [
  2543. $hpresults[$i][0],
  2544. $hpresults[$i][1],
  2545. $hpresults[$i][2],
  2546. '',
  2547. $hp_title,
  2548. '-',
  2549. $hp_date,
  2550. $hp_result,
  2551. '-',
  2552. ];
  2553. } else {
  2554. $listInfo[] = [
  2555. $hp_title,
  2556. '-',
  2557. $hp_date,
  2558. $hp_result,
  2559. '-',
  2560. ];
  2561. }
  2562. }
  2563. }
  2564. }
  2565. return $listInfo;
  2566. }
  2567. /**
  2568. * @param $score
  2569. * @param $weight
  2570. *
  2571. * @return array
  2572. */
  2573. public static function convertScoreToPlatformSetting($score, $weight)
  2574. {
  2575. $maxNote = api_get_setting('exercise_max_score');
  2576. $minNote = api_get_setting('exercise_min_score');
  2577. if ($maxNote != '' && $minNote != '') {
  2578. if (!empty($weight) && intval($weight) != 0) {
  2579. $score = $minNote + ($maxNote - $minNote) * $score / $weight;
  2580. } else {
  2581. $score = $minNote;
  2582. }
  2583. $weight = $maxNote;
  2584. }
  2585. return ['score' => $score, 'weight' => $weight];
  2586. }
  2587. /**
  2588. * Converts the score with the exercise_max_note and exercise_min_score
  2589. * the platform settings + formats the results using the float_format function.
  2590. *
  2591. * @param float $score
  2592. * @param float $weight
  2593. * @param bool $show_percentage show percentage or not
  2594. * @param bool $use_platform_settings use or not the platform settings
  2595. * @param bool $show_only_percentage
  2596. * @param bool $hidePercentageSign hide "%" sign
  2597. * @param string $decimalSeparator
  2598. * @param string $thousandSeparator
  2599. * @param bool $roundValues This option rounds the float values into a int using ceil()
  2600. *
  2601. * @return string an html with the score modified
  2602. */
  2603. public static function show_score(
  2604. $score,
  2605. $weight,
  2606. $show_percentage = true,
  2607. $use_platform_settings = true,
  2608. $show_only_percentage = false,
  2609. $hidePercentageSign = false,
  2610. $decimalSeparator = '.',
  2611. $thousandSeparator = ',',
  2612. $roundValues = false
  2613. ) {
  2614. if (is_null($score) && is_null($weight)) {
  2615. return '-';
  2616. }
  2617. if ($use_platform_settings) {
  2618. $result = self::convertScoreToPlatformSetting($score, $weight);
  2619. $score = $result['score'];
  2620. $weight = $result['weight'];
  2621. }
  2622. $percentage = (100 * $score) / ($weight != 0 ? $weight : 1);
  2623. // Formats values
  2624. $percentage = float_format($percentage, 1);
  2625. $score = float_format($score, 1);
  2626. $weight = float_format($weight, 1);
  2627. if ($roundValues) {
  2628. $whole = floor($percentage); // 1
  2629. $fraction = $percentage - $whole; // .25
  2630. // Formats values
  2631. if ($fraction >= 0.5) {
  2632. $percentage = ceil($percentage);
  2633. } else {
  2634. $percentage = round($percentage);
  2635. }
  2636. $whole = floor($score); // 1
  2637. $fraction = $score - $whole; // .25
  2638. if ($fraction >= 0.5) {
  2639. $score = ceil($score);
  2640. } else {
  2641. $score = round($score);
  2642. }
  2643. $whole = floor($weight); // 1
  2644. $fraction = $weight - $whole; // .25
  2645. if ($fraction >= 0.5) {
  2646. $weight = ceil($weight);
  2647. } else {
  2648. $weight = round($weight);
  2649. }
  2650. } else {
  2651. // Formats values
  2652. $percentage = float_format($percentage, 1, $decimalSeparator, $thousandSeparator);
  2653. $score = float_format($score, 1, $decimalSeparator, $thousandSeparator);
  2654. $weight = float_format($weight, 1, $decimalSeparator, $thousandSeparator);
  2655. }
  2656. if ($show_percentage) {
  2657. $percentageSign = '%';
  2658. if ($hidePercentageSign) {
  2659. $percentageSign = '';
  2660. }
  2661. $html = $percentage."$percentageSign ($score / $weight)";
  2662. if ($show_only_percentage) {
  2663. $html = $percentage.$percentageSign;
  2664. }
  2665. } else {
  2666. $html = $score.' / '.$weight;
  2667. }
  2668. // Over write score
  2669. $scoreBasedInModel = self::convertScoreToModel($percentage);
  2670. if (!empty($scoreBasedInModel)) {
  2671. $html = $scoreBasedInModel;
  2672. }
  2673. // Ignore other formats and use the configuratio['exercise_score_format'] value
  2674. // But also keep the round values settings.
  2675. $format = api_get_configuration_value('exercise_score_format');
  2676. if (!empty($format)) {
  2677. $html = ScoreDisplay::instance()->display_score([$score, $weight], $format);
  2678. }
  2679. $html = Display::span($html, ['class' => 'score_exercise']);
  2680. return $html;
  2681. }
  2682. /**
  2683. * @param array $model
  2684. * @param float $percentage
  2685. *
  2686. * @return string
  2687. */
  2688. public static function getModelStyle($model, $percentage)
  2689. {
  2690. $modelWithStyle = '<span class="'.$model['css_class'].'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>';
  2691. return $modelWithStyle;
  2692. }
  2693. /**
  2694. * @param float $percentage value between 0 and 100
  2695. *
  2696. * @return string
  2697. */
  2698. public static function convertScoreToModel($percentage)
  2699. {
  2700. $model = self::getCourseScoreModel();
  2701. if (!empty($model)) {
  2702. $scoreWithGrade = [];
  2703. foreach ($model['score_list'] as $item) {
  2704. if ($percentage >= $item['min'] && $percentage <= $item['max']) {
  2705. $scoreWithGrade = $item;
  2706. break;
  2707. }
  2708. }
  2709. if (!empty($scoreWithGrade)) {
  2710. return self::getModelStyle($scoreWithGrade, $percentage);
  2711. }
  2712. }
  2713. return '';
  2714. }
  2715. /**
  2716. * @return array
  2717. */
  2718. public static function getCourseScoreModel()
  2719. {
  2720. $modelList = self::getScoreModels();
  2721. if (empty($modelList)) {
  2722. return [];
  2723. }
  2724. $courseInfo = api_get_course_info();
  2725. if (!empty($courseInfo)) {
  2726. $scoreModelId = api_get_course_setting('score_model_id');
  2727. if ($scoreModelId != -1) {
  2728. $modelIdList = array_column($modelList['models'], 'id');
  2729. if (in_array($scoreModelId, $modelIdList)) {
  2730. foreach ($modelList['models'] as $item) {
  2731. if ($item['id'] == $scoreModelId) {
  2732. return $item;
  2733. }
  2734. }
  2735. }
  2736. }
  2737. }
  2738. return [];
  2739. }
  2740. /**
  2741. * @return array
  2742. */
  2743. public static function getScoreModels()
  2744. {
  2745. return api_get_configuration_value('score_grade_model');
  2746. }
  2747. /**
  2748. * @param float $score
  2749. * @param float $weight
  2750. * @param string $pass_percentage
  2751. *
  2752. * @return bool
  2753. */
  2754. public static function isSuccessExerciseResult($score, $weight, $pass_percentage)
  2755. {
  2756. $percentage = float_format(
  2757. ($score / ($weight != 0 ? $weight : 1)) * 100,
  2758. 1
  2759. );
  2760. if (isset($pass_percentage) && !empty($pass_percentage)) {
  2761. if ($percentage >= $pass_percentage) {
  2762. return true;
  2763. }
  2764. }
  2765. return false;
  2766. }
  2767. /**
  2768. * @param FormValidator $form
  2769. * @param string $name
  2770. * @param $weight
  2771. * @param $selected
  2772. *
  2773. * @return bool
  2774. */
  2775. public static function addScoreModelInput(
  2776. FormValidator $form,
  2777. $name,
  2778. $weight,
  2779. $selected
  2780. ) {
  2781. $model = self::getCourseScoreModel();
  2782. if (empty($model)) {
  2783. return false;
  2784. }
  2785. /** @var HTML_QuickForm_select $element */
  2786. $element = $form->createElement(
  2787. 'select',
  2788. $name,
  2789. get_lang('Qualification'),
  2790. [],
  2791. ['class' => 'exercise_mark_select']
  2792. );
  2793. foreach ($model['score_list'] as $item) {
  2794. $i = api_number_format($item['score_to_qualify'] / 100 * $weight, 2);
  2795. $label = self::getModelStyle($item, $i);
  2796. $attributes = [
  2797. 'class' => $item['css_class'],
  2798. ];
  2799. if ($selected == $i) {
  2800. $attributes['selected'] = 'selected';
  2801. }
  2802. $element->addOption($label, $i, $attributes);
  2803. }
  2804. $form->addElement($element);
  2805. }
  2806. /**
  2807. * @return string
  2808. */
  2809. public static function getJsCode()
  2810. {
  2811. // Filling the scores with the right colors.
  2812. $models = self::getCourseScoreModel();
  2813. $cssListToString = '';
  2814. if (!empty($models)) {
  2815. $cssList = array_column($models['score_list'], 'css_class');
  2816. $cssListToString = implode(' ', $cssList);
  2817. }
  2818. if (empty($cssListToString)) {
  2819. return '';
  2820. }
  2821. $js = <<<EOT
  2822. function updateSelect(element) {
  2823. var spanTag = element.parent().find('span.filter-option');
  2824. var value = element.val();
  2825. var selectId = element.attr('id');
  2826. var optionClass = $('#' + selectId + ' option[value="'+value+'"]').attr('class');
  2827. spanTag.removeClass('$cssListToString');
  2828. spanTag.addClass(optionClass);
  2829. }
  2830. $(function() {
  2831. // Loading values
  2832. $('.exercise_mark_select').on('loaded.bs.select', function() {
  2833. updateSelect($(this));
  2834. });
  2835. // On change
  2836. $('.exercise_mark_select').on('changed.bs.select', function() {
  2837. updateSelect($(this));
  2838. });
  2839. });
  2840. EOT;
  2841. return $js;
  2842. }
  2843. /**
  2844. * @param float $score
  2845. * @param float $weight
  2846. * @param string $pass_percentage
  2847. *
  2848. * @return string
  2849. */
  2850. public static function showSuccessMessage($score, $weight, $pass_percentage)
  2851. {
  2852. $res = '';
  2853. if (self::isPassPercentageEnabled($pass_percentage)) {
  2854. $isSuccess = self::isSuccessExerciseResult(
  2855. $score,
  2856. $weight,
  2857. $pass_percentage
  2858. );
  2859. if ($isSuccess) {
  2860. $html = get_lang('CongratulationsYouPassedTheTest');
  2861. $icon = Display::return_icon(
  2862. 'completed.png',
  2863. get_lang('Correct'),
  2864. [],
  2865. ICON_SIZE_MEDIUM
  2866. );
  2867. } else {
  2868. $html = get_lang('YouDidNotReachTheMinimumScore');
  2869. $icon = Display::return_icon(
  2870. 'warning.png',
  2871. get_lang('Wrong'),
  2872. [],
  2873. ICON_SIZE_MEDIUM
  2874. );
  2875. }
  2876. $html = Display::tag('h4', $html);
  2877. $html .= Display::tag(
  2878. 'h5',
  2879. $icon,
  2880. ['style' => 'width:40px; padding:2px 10px 0px 0px']
  2881. );
  2882. $res = $html;
  2883. }
  2884. return $res;
  2885. }
  2886. /**
  2887. * Return true if pass_pourcentage activated (we use the pass pourcentage feature
  2888. * return false if pass_percentage = 0 (we don't use the pass pourcentage feature.
  2889. *
  2890. * @param $value
  2891. *
  2892. * @return bool
  2893. * In this version, pass_percentage and show_success_message are disabled if
  2894. * pass_percentage is set to 0
  2895. */
  2896. public static function isPassPercentageEnabled($value)
  2897. {
  2898. return $value > 0;
  2899. }
  2900. /**
  2901. * Converts a numeric value in a percentage example 0.66666 to 66.67 %.
  2902. *
  2903. * @param $value
  2904. *
  2905. * @return float Converted number
  2906. */
  2907. public static function convert_to_percentage($value)
  2908. {
  2909. $return = '-';
  2910. if ($value != '') {
  2911. $return = float_format($value * 100, 1).' %';
  2912. }
  2913. return $return;
  2914. }
  2915. /**
  2916. * Getting all active exercises from a course from a session
  2917. * (if a session_id is provided we will show all the exercises in the course +
  2918. * all exercises in the session).
  2919. *
  2920. * @param array $course_info
  2921. * @param int $session_id
  2922. * @param bool $check_publication_dates
  2923. * @param string $search Search exercise name
  2924. * @param bool $search_all_sessions Search exercises in all sessions
  2925. * @param int 0 = only inactive exercises
  2926. * 1 = only active exercises,
  2927. * 2 = all exercises
  2928. * 3 = active <> -1
  2929. *
  2930. * @return array array with exercise data
  2931. */
  2932. public static function get_all_exercises(
  2933. $course_info = null,
  2934. $session_id = 0,
  2935. $check_publication_dates = false,
  2936. $search = '',
  2937. $search_all_sessions = false,
  2938. $active = 2
  2939. ) {
  2940. $course_id = api_get_course_int_id();
  2941. if (!empty($course_info) && !empty($course_info['real_id'])) {
  2942. $course_id = $course_info['real_id'];
  2943. }
  2944. if ($session_id == -1) {
  2945. $session_id = 0;
  2946. }
  2947. $now = api_get_utc_datetime();
  2948. $timeConditions = '';
  2949. if ($check_publication_dates) {
  2950. // Start and end are set
  2951. $timeConditions = " AND ((start_time <> '' AND start_time < '$now' AND end_time <> '' AND end_time > '$now' ) OR ";
  2952. // only start is set
  2953. $timeConditions .= " (start_time <> '' AND start_time < '$now' AND end_time is NULL) OR ";
  2954. // only end is set
  2955. $timeConditions .= " (start_time IS NULL AND end_time <> '' AND end_time > '$now') OR ";
  2956. // nothing is set
  2957. $timeConditions .= ' (start_time IS NULL AND end_time IS NULL)) ';
  2958. }
  2959. $needle_where = !empty($search) ? " AND title LIKE '?' " : '';
  2960. $needle = !empty($search) ? "%".$search."%" : '';
  2961. // Show courses by active status
  2962. $active_sql = '';
  2963. if ($active == 3) {
  2964. $active_sql = ' active <> -1 AND';
  2965. } else {
  2966. if ($active != 2) {
  2967. $active_sql = sprintf(' active = %d AND', $active);
  2968. }
  2969. }
  2970. if ($search_all_sessions == true) {
  2971. $conditions = [
  2972. 'where' => [
  2973. $active_sql.' c_id = ? '.$needle_where.$timeConditions => [
  2974. $course_id,
  2975. $needle,
  2976. ],
  2977. ],
  2978. 'order' => 'title',
  2979. ];
  2980. } else {
  2981. if (empty($session_id)) {
  2982. $conditions = [
  2983. 'where' => [
  2984. $active_sql.' (session_id = 0 OR session_id IS NULL) AND c_id = ? '.$needle_where.$timeConditions => [
  2985. $course_id,
  2986. $needle,
  2987. ],
  2988. ],
  2989. 'order' => 'title',
  2990. ];
  2991. } else {
  2992. $conditions = [
  2993. 'where' => [
  2994. $active_sql.' (session_id = 0 OR session_id IS NULL OR session_id = ? ) AND c_id = ? '.$needle_where.$timeConditions => [
  2995. $session_id,
  2996. $course_id,
  2997. $needle,
  2998. ],
  2999. ],
  3000. 'order' => 'title',
  3001. ];
  3002. }
  3003. }
  3004. $table = Database::get_course_table(TABLE_QUIZ_TEST);
  3005. return Database::select('*', $table, $conditions);
  3006. }
  3007. /**
  3008. * Getting all exercises (active only or all)
  3009. * from a course from a session
  3010. * (if a session_id is provided we will show all the exercises in the
  3011. * course + all exercises in the session).
  3012. *
  3013. * @param array course data
  3014. * @param int session id
  3015. * @param int course c_id
  3016. * @param bool $only_active_exercises
  3017. *
  3018. * @return array array with exercise data
  3019. * modified by Hubert Borderiou
  3020. */
  3021. public static function get_all_exercises_for_course_id(
  3022. $course_info = null,
  3023. $session_id = 0,
  3024. $course_id = 0,
  3025. $only_active_exercises = true
  3026. ) {
  3027. $table = Database::get_course_table(TABLE_QUIZ_TEST);
  3028. if ($only_active_exercises) {
  3029. // Only active exercises.
  3030. $sql_active_exercises = "active = 1 AND ";
  3031. } else {
  3032. // Not only active means visible and invisible NOT deleted (-2)
  3033. $sql_active_exercises = "active IN (1, 0) AND ";
  3034. }
  3035. if ($session_id == -1) {
  3036. $session_id = 0;
  3037. }
  3038. $params = [
  3039. $session_id,
  3040. $course_id,
  3041. ];
  3042. if (empty($session_id)) {
  3043. $conditions = [
  3044. 'where' => ["$sql_active_exercises (session_id = 0 OR session_id IS NULL) AND c_id = ?" => [$course_id]],
  3045. 'order' => 'title',
  3046. ];
  3047. } else {
  3048. // All exercises
  3049. $conditions = [
  3050. 'where' => ["$sql_active_exercises (session_id = 0 OR session_id IS NULL OR session_id = ? ) AND c_id=?" => $params],
  3051. 'order' => 'title',
  3052. ];
  3053. }
  3054. return Database::select('*', $table, $conditions);
  3055. }
  3056. /**
  3057. * Gets the position of the score based in a given score (result/weight)
  3058. * and the exe_id based in the user list
  3059. * (NO Exercises in LPs ).
  3060. *
  3061. * @param float $my_score user score to be compared *attention*
  3062. * $my_score = score/weight and not just the score
  3063. * @param int $my_exe_id exe id of the exercise
  3064. * (this is necessary because if 2 students have the same score the one
  3065. * with the minor exe_id will have a best position, just to be fair and FIFO)
  3066. * @param int $exercise_id
  3067. * @param string $course_code
  3068. * @param int $session_id
  3069. * @param array $user_list
  3070. * @param bool $return_string
  3071. *
  3072. * @return int the position of the user between his friends in a course
  3073. * (or course within a session)
  3074. */
  3075. public static function get_exercise_result_ranking(
  3076. $my_score,
  3077. $my_exe_id,
  3078. $exercise_id,
  3079. $course_code,
  3080. $session_id = 0,
  3081. $user_list = [],
  3082. $return_string = true
  3083. ) {
  3084. //No score given we return
  3085. if (is_null($my_score)) {
  3086. return '-';
  3087. }
  3088. if (empty($user_list)) {
  3089. return '-';
  3090. }
  3091. $best_attempts = [];
  3092. foreach ($user_list as $user_data) {
  3093. $user_id = $user_data['user_id'];
  3094. $best_attempts[$user_id] = self::get_best_attempt_by_user(
  3095. $user_id,
  3096. $exercise_id,
  3097. $course_code,
  3098. $session_id
  3099. );
  3100. }
  3101. if (empty($best_attempts)) {
  3102. return 1;
  3103. } else {
  3104. $position = 1;
  3105. $my_ranking = [];
  3106. foreach ($best_attempts as $user_id => $result) {
  3107. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  3108. $my_ranking[$user_id] = $result['exe_result'] / $result['exe_weighting'];
  3109. } else {
  3110. $my_ranking[$user_id] = 0;
  3111. }
  3112. }
  3113. //if (!empty($my_ranking)) {
  3114. asort($my_ranking);
  3115. $position = count($my_ranking);
  3116. if (!empty($my_ranking)) {
  3117. foreach ($my_ranking as $user_id => $ranking) {
  3118. if ($my_score >= $ranking) {
  3119. if ($my_score == $ranking && isset($best_attempts[$user_id]['exe_id'])) {
  3120. $exe_id = $best_attempts[$user_id]['exe_id'];
  3121. if ($my_exe_id < $exe_id) {
  3122. $position--;
  3123. }
  3124. } else {
  3125. $position--;
  3126. }
  3127. }
  3128. }
  3129. }
  3130. //}
  3131. $return_value = [
  3132. 'position' => $position,
  3133. 'count' => count($my_ranking),
  3134. ];
  3135. if ($return_string) {
  3136. if (!empty($position) && !empty($my_ranking)) {
  3137. $return_value = $position.'/'.count($my_ranking);
  3138. } else {
  3139. $return_value = '-';
  3140. }
  3141. }
  3142. return $return_value;
  3143. }
  3144. }
  3145. /**
  3146. * Gets the position of the score based in a given score (result/weight) and the exe_id based in all attempts
  3147. * (NO Exercises in LPs ) old functionality by attempt.
  3148. *
  3149. * @param float user score to be compared attention => score/weight
  3150. * @param int exe id of the exercise
  3151. * (this is necessary because if 2 students have the same score the one
  3152. * with the minor exe_id will have a best position, just to be fair and FIFO)
  3153. * @param int exercise id
  3154. * @param string course code
  3155. * @param int session id
  3156. * @param bool $return_string
  3157. *
  3158. * @return int the position of the user between his friends in a course (or course within a session)
  3159. */
  3160. public static function get_exercise_result_ranking_by_attempt(
  3161. $my_score,
  3162. $my_exe_id,
  3163. $exercise_id,
  3164. $courseId,
  3165. $session_id = 0,
  3166. $return_string = true
  3167. ) {
  3168. if (empty($session_id)) {
  3169. $session_id = 0;
  3170. }
  3171. if (is_null($my_score)) {
  3172. return '-';
  3173. }
  3174. $user_results = Event::get_all_exercise_results(
  3175. $exercise_id,
  3176. $courseId,
  3177. $session_id,
  3178. false
  3179. );
  3180. $position_data = [];
  3181. if (empty($user_results)) {
  3182. return 1;
  3183. } else {
  3184. $position = 1;
  3185. $my_ranking = [];
  3186. foreach ($user_results as $result) {
  3187. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  3188. $my_ranking[$result['exe_id']] = $result['exe_result'] / $result['exe_weighting'];
  3189. } else {
  3190. $my_ranking[$result['exe_id']] = 0;
  3191. }
  3192. }
  3193. asort($my_ranking);
  3194. $position = count($my_ranking);
  3195. if (!empty($my_ranking)) {
  3196. foreach ($my_ranking as $exe_id => $ranking) {
  3197. if ($my_score >= $ranking) {
  3198. if ($my_score == $ranking) {
  3199. if ($my_exe_id < $exe_id) {
  3200. $position--;
  3201. }
  3202. } else {
  3203. $position--;
  3204. }
  3205. }
  3206. }
  3207. }
  3208. $return_value = [
  3209. 'position' => $position,
  3210. 'count' => count($my_ranking),
  3211. ];
  3212. if ($return_string) {
  3213. if (!empty($position) && !empty($my_ranking)) {
  3214. return $position.'/'.count($my_ranking);
  3215. }
  3216. }
  3217. return $return_value;
  3218. }
  3219. }
  3220. /**
  3221. * Get the best attempt in a exercise (NO Exercises in LPs ).
  3222. *
  3223. * @param int $exercise_id
  3224. * @param int $courseId
  3225. * @param int $session_id
  3226. *
  3227. * @return array
  3228. */
  3229. public static function get_best_attempt_in_course($exercise_id, $courseId, $session_id)
  3230. {
  3231. $user_results = Event::get_all_exercise_results(
  3232. $exercise_id,
  3233. $courseId,
  3234. $session_id,
  3235. false
  3236. );
  3237. $best_score_data = [];
  3238. $best_score = 0;
  3239. if (!empty($user_results)) {
  3240. foreach ($user_results as $result) {
  3241. if (!empty($result['exe_weighting']) &&
  3242. intval($result['exe_weighting']) != 0
  3243. ) {
  3244. $score = $result['exe_result'] / $result['exe_weighting'];
  3245. if ($score >= $best_score) {
  3246. $best_score = $score;
  3247. $best_score_data = $result;
  3248. }
  3249. }
  3250. }
  3251. }
  3252. return $best_score_data;
  3253. }
  3254. /**
  3255. * Get the best score in a exercise (NO Exercises in LPs ).
  3256. *
  3257. * @param int $user_id
  3258. * @param int $exercise_id
  3259. * @param int $courseId
  3260. * @param int $session_id
  3261. *
  3262. * @return array
  3263. */
  3264. public static function get_best_attempt_by_user(
  3265. $user_id,
  3266. $exercise_id,
  3267. $courseId,
  3268. $session_id
  3269. ) {
  3270. $user_results = Event::get_all_exercise_results(
  3271. $exercise_id,
  3272. $courseId,
  3273. $session_id,
  3274. false,
  3275. $user_id
  3276. );
  3277. $best_score_data = [];
  3278. $best_score = 0;
  3279. if (!empty($user_results)) {
  3280. foreach ($user_results as $result) {
  3281. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  3282. $score = $result['exe_result'] / $result['exe_weighting'];
  3283. if ($score >= $best_score) {
  3284. $best_score = $score;
  3285. $best_score_data = $result;
  3286. }
  3287. }
  3288. }
  3289. }
  3290. return $best_score_data;
  3291. }
  3292. /**
  3293. * Get average score (NO Exercises in LPs ).
  3294. *
  3295. * @param int exercise id
  3296. * @param int $courseId
  3297. * @param int session id
  3298. *
  3299. * @return float Average score
  3300. */
  3301. public static function get_average_score($exercise_id, $courseId, $session_id)
  3302. {
  3303. $user_results = Event::get_all_exercise_results(
  3304. $exercise_id,
  3305. $courseId,
  3306. $session_id
  3307. );
  3308. $avg_score = 0;
  3309. if (!empty($user_results)) {
  3310. foreach ($user_results as $result) {
  3311. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  3312. $score = $result['exe_result'] / $result['exe_weighting'];
  3313. $avg_score += $score;
  3314. }
  3315. }
  3316. $avg_score = float_format($avg_score / count($user_results), 1);
  3317. }
  3318. return $avg_score;
  3319. }
  3320. /**
  3321. * Get average score by score (NO Exercises in LPs ).
  3322. *
  3323. * @param int $courseId
  3324. * @param int session id
  3325. *
  3326. * @return float Average score
  3327. */
  3328. public static function get_average_score_by_course($courseId, $session_id)
  3329. {
  3330. $user_results = Event::get_all_exercise_results_by_course(
  3331. $courseId,
  3332. $session_id,
  3333. false
  3334. );
  3335. $avg_score = 0;
  3336. if (!empty($user_results)) {
  3337. foreach ($user_results as $result) {
  3338. if (!empty($result['exe_weighting']) && intval(
  3339. $result['exe_weighting']
  3340. ) != 0
  3341. ) {
  3342. $score = $result['exe_result'] / $result['exe_weighting'];
  3343. $avg_score += $score;
  3344. }
  3345. }
  3346. // We assume that all exe_weighting
  3347. $avg_score = $avg_score / count($user_results);
  3348. }
  3349. return $avg_score;
  3350. }
  3351. /**
  3352. * @param int $user_id
  3353. * @param int $courseId
  3354. * @param int $session_id
  3355. *
  3356. * @return float|int
  3357. */
  3358. public static function get_average_score_by_course_by_user(
  3359. $user_id,
  3360. $courseId,
  3361. $session_id
  3362. ) {
  3363. $user_results = Event::get_all_exercise_results_by_user(
  3364. $user_id,
  3365. $courseId,
  3366. $session_id
  3367. );
  3368. $avg_score = 0;
  3369. if (!empty($user_results)) {
  3370. foreach ($user_results as $result) {
  3371. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  3372. $score = $result['exe_result'] / $result['exe_weighting'];
  3373. $avg_score += $score;
  3374. }
  3375. }
  3376. // We assume that all exe_weighting
  3377. $avg_score = ($avg_score / count($user_results));
  3378. }
  3379. return $avg_score;
  3380. }
  3381. /**
  3382. * Get average score by score (NO Exercises in LPs ).
  3383. *
  3384. * @param int $exercise_id
  3385. * @param int $courseId
  3386. * @param int $session_id
  3387. * @param int $user_count
  3388. *
  3389. * @return float Best average score
  3390. */
  3391. public static function get_best_average_score_by_exercise(
  3392. $exercise_id,
  3393. $courseId,
  3394. $session_id,
  3395. $user_count
  3396. ) {
  3397. $user_results = Event::get_best_exercise_results_by_user(
  3398. $exercise_id,
  3399. $courseId,
  3400. $session_id
  3401. );
  3402. $avg_score = 0;
  3403. if (!empty($user_results)) {
  3404. foreach ($user_results as $result) {
  3405. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  3406. $score = $result['exe_result'] / $result['exe_weighting'];
  3407. $avg_score += $score;
  3408. }
  3409. }
  3410. // We asumme that all exe_weighting
  3411. if (!empty($user_count)) {
  3412. $avg_score = float_format($avg_score / $user_count, 1) * 100;
  3413. } else {
  3414. $avg_score = 0;
  3415. }
  3416. }
  3417. return $avg_score;
  3418. }
  3419. /**
  3420. * Get average score by score (NO Exercises in LPs ).
  3421. *
  3422. * @param int $exercise_id
  3423. * @param int $courseId
  3424. * @param int $session_id
  3425. *
  3426. * @return float Best average score
  3427. */
  3428. public static function getBestScoreByExercise(
  3429. $exercise_id,
  3430. $courseId,
  3431. $session_id
  3432. ) {
  3433. $user_results = Event::get_best_exercise_results_by_user(
  3434. $exercise_id,
  3435. $courseId,
  3436. $session_id
  3437. );
  3438. $avg_score = 0;
  3439. if (!empty($user_results)) {
  3440. foreach ($user_results as $result) {
  3441. if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
  3442. $score = $result['exe_result'] / $result['exe_weighting'];
  3443. $avg_score += $score;
  3444. }
  3445. }
  3446. }
  3447. return $avg_score;
  3448. }
  3449. /**
  3450. * @param string $course_code
  3451. * @param int $session_id
  3452. *
  3453. * @return array
  3454. */
  3455. public static function get_exercises_to_be_taken($course_code, $session_id)
  3456. {
  3457. $course_info = api_get_course_info($course_code);
  3458. $exercises = self::get_all_exercises($course_info, $session_id);
  3459. $result = [];
  3460. $now = time() + 15 * 24 * 60 * 60;
  3461. foreach ($exercises as $exercise_item) {
  3462. if (isset($exercise_item['end_time']) &&
  3463. !empty($exercise_item['end_time']) &&
  3464. api_strtotime($exercise_item['end_time'], 'UTC') < $now
  3465. ) {
  3466. $result[] = $exercise_item;
  3467. }
  3468. }
  3469. return $result;
  3470. }
  3471. /**
  3472. * Get student results (only in completed exercises) stats by question.
  3473. *
  3474. * @param int $question_id
  3475. * @param int $exercise_id
  3476. * @param string $course_code
  3477. * @param int $session_id
  3478. *
  3479. * @return array
  3480. */
  3481. public static function get_student_stats_by_question(
  3482. $question_id,
  3483. $exercise_id,
  3484. $course_code,
  3485. $session_id
  3486. ) {
  3487. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  3488. $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  3489. $question_id = (int) $question_id;
  3490. $exercise_id = (int) $exercise_id;
  3491. $course_code = Database::escape_string($course_code);
  3492. $session_id = (int) $session_id;
  3493. $courseId = api_get_course_int_id($course_code);
  3494. $sql = "SELECT MAX(marks) as max, MIN(marks) as min, AVG(marks) as average
  3495. FROM $track_exercises e
  3496. INNER JOIN $track_attempt a
  3497. ON (
  3498. a.exe_id = e.exe_id AND
  3499. e.c_id = a.c_id AND
  3500. e.session_id = a.session_id
  3501. )
  3502. WHERE
  3503. exe_exo_id = $exercise_id AND
  3504. a.c_id = $courseId AND
  3505. e.session_id = $session_id AND
  3506. question_id = $question_id AND
  3507. status = ''
  3508. LIMIT 1";
  3509. $result = Database::query($sql);
  3510. $return = [];
  3511. if ($result) {
  3512. $return = Database::fetch_array($result, 'ASSOC');
  3513. }
  3514. return $return;
  3515. }
  3516. /**
  3517. * Get the correct answer count for a fill blanks question.
  3518. *
  3519. * @param int $question_id
  3520. * @param int $exercise_id
  3521. *
  3522. * @return array
  3523. */
  3524. public static function getNumberStudentsFillBlanksAnswerCount(
  3525. $question_id,
  3526. $exercise_id
  3527. ) {
  3528. $listStudentsId = [];
  3529. $listAllStudentInfo = CourseManager::get_student_list_from_course_code(
  3530. api_get_course_id(),
  3531. true
  3532. );
  3533. foreach ($listAllStudentInfo as $i => $listStudentInfo) {
  3534. $listStudentsId[] = $listStudentInfo['user_id'];
  3535. }
  3536. $listFillTheBlankResult = FillBlanks::getFillTheBlankResult(
  3537. $exercise_id,
  3538. $question_id,
  3539. $listStudentsId,
  3540. '1970-01-01',
  3541. '3000-01-01'
  3542. );
  3543. $arrayCount = [];
  3544. foreach ($listFillTheBlankResult as $resultCount) {
  3545. foreach ($resultCount as $index => $count) {
  3546. //this is only for declare the array index per answer
  3547. $arrayCount[$index] = 0;
  3548. }
  3549. }
  3550. foreach ($listFillTheBlankResult as $resultCount) {
  3551. foreach ($resultCount as $index => $count) {
  3552. $count = ($count === 0) ? 1 : 0;
  3553. $arrayCount[$index] += $count;
  3554. }
  3555. }
  3556. return $arrayCount;
  3557. }
  3558. /**
  3559. * Get the number of questions with answers.
  3560. *
  3561. * @param int $question_id
  3562. * @param int $exercise_id
  3563. * @param string $course_code
  3564. * @param int $session_id
  3565. * @param string $questionType
  3566. *
  3567. * @return int
  3568. */
  3569. public static function get_number_students_question_with_answer_count(
  3570. $question_id,
  3571. $exercise_id,
  3572. $course_code,
  3573. $session_id,
  3574. $questionType = ''
  3575. ) {
  3576. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  3577. $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  3578. $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  3579. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  3580. $courseUserSession = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  3581. $question_id = intval($question_id);
  3582. $exercise_id = intval($exercise_id);
  3583. $courseId = api_get_course_int_id($course_code);
  3584. $session_id = intval($session_id);
  3585. if ($questionType == FILL_IN_BLANKS) {
  3586. $listStudentsId = [];
  3587. $listAllStudentInfo = CourseManager::get_student_list_from_course_code(
  3588. api_get_course_id(),
  3589. true
  3590. );
  3591. foreach ($listAllStudentInfo as $i => $listStudentInfo) {
  3592. $listStudentsId[] = $listStudentInfo['user_id'];
  3593. }
  3594. $listFillTheBlankResult = FillBlanks::getFillTheBlankResult(
  3595. $exercise_id,
  3596. $question_id,
  3597. $listStudentsId,
  3598. '1970-01-01',
  3599. '3000-01-01'
  3600. );
  3601. return FillBlanks::getNbResultFillBlankAll($listFillTheBlankResult);
  3602. }
  3603. if (empty($session_id)) {
  3604. $courseCondition = "
  3605. INNER JOIN $courseUser cu
  3606. ON cu.c_id = c.id AND cu.user_id = exe_user_id";
  3607. $courseConditionWhere = " AND relation_type <> 2 AND cu.status = ".STUDENT;
  3608. } else {
  3609. $courseCondition = "
  3610. INNER JOIN $courseUserSession cu
  3611. ON cu.c_id = c.id AND cu.user_id = exe_user_id";
  3612. $courseConditionWhere = " AND cu.status = 0 ";
  3613. }
  3614. $sql = "SELECT DISTINCT exe_user_id
  3615. FROM $track_exercises e
  3616. INNER JOIN $track_attempt a
  3617. ON (
  3618. a.exe_id = e.exe_id AND
  3619. e.c_id = a.c_id AND
  3620. e.session_id = a.session_id
  3621. )
  3622. INNER JOIN $courseTable c
  3623. ON (c.id = a.c_id)
  3624. $courseCondition
  3625. WHERE
  3626. exe_exo_id = $exercise_id AND
  3627. a.c_id = $courseId AND
  3628. e.session_id = $session_id AND
  3629. question_id = $question_id AND
  3630. answer <> '0' AND
  3631. e.status = ''
  3632. $courseConditionWhere
  3633. ";
  3634. $result = Database::query($sql);
  3635. $return = 0;
  3636. if ($result) {
  3637. $return = Database::num_rows($result);
  3638. }
  3639. return $return;
  3640. }
  3641. /**
  3642. * Get number of answers to hotspot questions.
  3643. *
  3644. * @param int $answer_id
  3645. * @param int $question_id
  3646. * @param int $exercise_id
  3647. * @param string $course_code
  3648. * @param int $session_id
  3649. *
  3650. * @return int
  3651. */
  3652. public static function get_number_students_answer_hotspot_count(
  3653. $answer_id,
  3654. $question_id,
  3655. $exercise_id,
  3656. $course_code,
  3657. $session_id
  3658. ) {
  3659. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  3660. $track_hotspot = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  3661. $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  3662. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  3663. $courseUserSession = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  3664. $question_id = (int) $question_id;
  3665. $answer_id = (int) $answer_id;
  3666. $exercise_id = (int) $exercise_id;
  3667. $course_code = Database::escape_string($course_code);
  3668. $session_id = (int) $session_id;
  3669. if (empty($session_id)) {
  3670. $courseCondition = "
  3671. INNER JOIN $courseUser cu
  3672. ON cu.c_id = c.id AND cu.user_id = exe_user_id";
  3673. $courseConditionWhere = " AND relation_type <> 2 AND cu.status = ".STUDENT;
  3674. } else {
  3675. $courseCondition = "
  3676. INNER JOIN $courseUserSession cu
  3677. ON cu.c_id = c.id AND cu.user_id = exe_user_id";
  3678. $courseConditionWhere = ' AND cu.status = 0 ';
  3679. }
  3680. $sql = "SELECT DISTINCT exe_user_id
  3681. FROM $track_exercises e
  3682. INNER JOIN $track_hotspot a
  3683. ON (a.hotspot_exe_id = e.exe_id)
  3684. INNER JOIN $courseTable c
  3685. ON (hotspot_course_code = c.code)
  3686. $courseCondition
  3687. WHERE
  3688. exe_exo_id = $exercise_id AND
  3689. a.hotspot_course_code = '$course_code' AND
  3690. e.session_id = $session_id AND
  3691. hotspot_answer_id = $answer_id AND
  3692. hotspot_question_id = $question_id AND
  3693. hotspot_correct = 1 AND
  3694. e.status = ''
  3695. $courseConditionWhere
  3696. ";
  3697. $result = Database::query($sql);
  3698. $return = 0;
  3699. if ($result) {
  3700. $return = Database::num_rows($result);
  3701. }
  3702. return $return;
  3703. }
  3704. /**
  3705. * @param int $answer_id
  3706. * @param int $question_id
  3707. * @param int $exercise_id
  3708. * @param string $course_code
  3709. * @param int $session_id
  3710. * @param string $question_type
  3711. * @param string $correct_answer
  3712. * @param string $current_answer
  3713. *
  3714. * @return int
  3715. */
  3716. public static function get_number_students_answer_count(
  3717. $answer_id,
  3718. $question_id,
  3719. $exercise_id,
  3720. $course_code,
  3721. $session_id,
  3722. $question_type = null,
  3723. $correct_answer = null,
  3724. $current_answer = null
  3725. ) {
  3726. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  3727. $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  3728. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  3729. $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  3730. $courseUserSession = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  3731. $question_id = (int) $question_id;
  3732. $answer_id = (int) $answer_id;
  3733. $exercise_id = (int) $exercise_id;
  3734. $courseId = api_get_course_int_id($course_code);
  3735. $session_id = (int) $session_id;
  3736. switch ($question_type) {
  3737. case FILL_IN_BLANKS:
  3738. $answer_condition = '';
  3739. $select_condition = ' e.exe_id, answer ';
  3740. break;
  3741. case MATCHING:
  3742. case MATCHING_DRAGGABLE:
  3743. default:
  3744. $answer_condition = " answer = $answer_id AND ";
  3745. $select_condition = ' DISTINCT exe_user_id ';
  3746. }
  3747. if (empty($session_id)) {
  3748. $courseCondition = "
  3749. INNER JOIN $courseUser cu
  3750. ON cu.c_id = c.id AND cu.user_id = exe_user_id";
  3751. $courseConditionWhere = " AND relation_type <> 2 AND cu.status = ".STUDENT;
  3752. } else {
  3753. $courseCondition = "
  3754. INNER JOIN $courseUserSession cu
  3755. ON cu.c_id = a.c_id AND cu.user_id = exe_user_id";
  3756. $courseConditionWhere = ' AND cu.status = 0 ';
  3757. }
  3758. $sql = "SELECT $select_condition
  3759. FROM $track_exercises e
  3760. INNER JOIN $track_attempt a
  3761. ON (
  3762. a.exe_id = e.exe_id AND
  3763. e.c_id = a.c_id AND
  3764. e.session_id = a.session_id
  3765. )
  3766. INNER JOIN $courseTable c
  3767. ON c.id = a.c_id
  3768. $courseCondition
  3769. WHERE
  3770. exe_exo_id = $exercise_id AND
  3771. a.c_id = $courseId AND
  3772. e.session_id = $session_id AND
  3773. $answer_condition
  3774. question_id = $question_id AND
  3775. e.status = ''
  3776. $courseConditionWhere
  3777. ";
  3778. $result = Database::query($sql);
  3779. $return = 0;
  3780. if ($result) {
  3781. $good_answers = 0;
  3782. switch ($question_type) {
  3783. case FILL_IN_BLANKS:
  3784. while ($row = Database::fetch_array($result, 'ASSOC')) {
  3785. $fill_blank = self::check_fill_in_blanks(
  3786. $correct_answer,
  3787. $row['answer'],
  3788. $current_answer
  3789. );
  3790. if (isset($fill_blank[$current_answer]) && $fill_blank[$current_answer] == 1) {
  3791. $good_answers++;
  3792. }
  3793. }
  3794. return $good_answers;
  3795. break;
  3796. case MATCHING:
  3797. case MATCHING_DRAGGABLE:
  3798. default:
  3799. $return = Database::num_rows($result);
  3800. }
  3801. }
  3802. return $return;
  3803. }
  3804. /**
  3805. * @param array $answer
  3806. * @param string $user_answer
  3807. *
  3808. * @return array
  3809. */
  3810. public static function check_fill_in_blanks($answer, $user_answer, $current_answer)
  3811. {
  3812. // the question is encoded like this
  3813. // [A] B [C] D [E] F::10,10,10@1
  3814. // number 1 before the "@" means that is a switchable fill in blank question
  3815. // [A] B [C] D [E] F::10,10,10@ or [A] B [C] D [E] F::10,10,10
  3816. // means that is a normal fill blank question
  3817. // first we explode the "::"
  3818. $pre_array = explode('::', $answer);
  3819. // is switchable fill blank or not
  3820. $last = count($pre_array) - 1;
  3821. $is_set_switchable = explode('@', $pre_array[$last]);
  3822. $switchable_answer_set = false;
  3823. if (isset($is_set_switchable[1]) && $is_set_switchable[1] == 1) {
  3824. $switchable_answer_set = true;
  3825. }
  3826. $answer = '';
  3827. for ($k = 0; $k < $last; $k++) {
  3828. $answer .= $pre_array[$k];
  3829. }
  3830. // splits weightings that are joined with a comma
  3831. $answerWeighting = explode(',', $is_set_switchable[0]);
  3832. // we save the answer because it will be modified
  3833. //$temp = $answer;
  3834. $temp = $answer;
  3835. $answer = '';
  3836. $j = 0;
  3837. //initialise answer tags
  3838. $user_tags = $correct_tags = $real_text = [];
  3839. // the loop will stop at the end of the text
  3840. while (1) {
  3841. // quits the loop if there are no more blanks (detect '[')
  3842. if (($pos = api_strpos($temp, '[')) === false) {
  3843. // adds the end of the text
  3844. $answer = $temp;
  3845. $real_text[] = $answer;
  3846. break; //no more "blanks", quit the loop
  3847. }
  3848. // adds the piece of text that is before the blank
  3849. //and ends with '[' into a general storage array
  3850. $real_text[] = api_substr($temp, 0, $pos + 1);
  3851. $answer .= api_substr($temp, 0, $pos + 1);
  3852. //take the string remaining (after the last "[" we found)
  3853. $temp = api_substr($temp, $pos + 1);
  3854. // quit the loop if there are no more blanks, and update $pos to the position of next ']'
  3855. if (($pos = api_strpos($temp, ']')) === false) {
  3856. // adds the end of the text
  3857. $answer .= $temp;
  3858. break;
  3859. }
  3860. $str = $user_answer;
  3861. preg_match_all('#\[([^[]*)\]#', $str, $arr);
  3862. $str = str_replace('\r\n', '', $str);
  3863. $choices = $arr[1];
  3864. $choice = [];
  3865. $check = false;
  3866. $i = 0;
  3867. foreach ($choices as $item) {
  3868. if ($current_answer === $item) {
  3869. $check = true;
  3870. }
  3871. if ($check) {
  3872. $choice[] = $item;
  3873. $i++;
  3874. }
  3875. if ($i == 3) {
  3876. break;
  3877. }
  3878. }
  3879. $tmp = api_strrpos($choice[$j], ' / ');
  3880. if ($tmp !== false) {
  3881. $choice[$j] = api_substr($choice[$j], 0, $tmp);
  3882. }
  3883. $choice[$j] = trim($choice[$j]);
  3884. //Needed to let characters ' and " to work as part of an answer
  3885. $choice[$j] = stripslashes($choice[$j]);
  3886. $user_tags[] = api_strtolower($choice[$j]);
  3887. //put the contents of the [] answer tag into correct_tags[]
  3888. $correct_tags[] = api_strtolower(api_substr($temp, 0, $pos));
  3889. $j++;
  3890. $temp = api_substr($temp, $pos + 1);
  3891. }
  3892. $answer = '';
  3893. $real_correct_tags = $correct_tags;
  3894. $chosen_list = [];
  3895. $good_answer = [];
  3896. for ($i = 0; $i < count($real_correct_tags); $i++) {
  3897. if (!$switchable_answer_set) {
  3898. //needed to parse ' and " characters
  3899. $user_tags[$i] = stripslashes($user_tags[$i]);
  3900. if ($correct_tags[$i] == $user_tags[$i]) {
  3901. $good_answer[$correct_tags[$i]] = 1;
  3902. } elseif (!empty($user_tags[$i])) {
  3903. $good_answer[$correct_tags[$i]] = 0;
  3904. } else {
  3905. $good_answer[$correct_tags[$i]] = 0;
  3906. }
  3907. } else {
  3908. // switchable fill in the blanks
  3909. if (in_array($user_tags[$i], $correct_tags)) {
  3910. $correct_tags = array_diff($correct_tags, $chosen_list);
  3911. $good_answer[$correct_tags[$i]] = 1;
  3912. } elseif (!empty($user_tags[$i])) {
  3913. $good_answer[$correct_tags[$i]] = 0;
  3914. } else {
  3915. $good_answer[$correct_tags[$i]] = 0;
  3916. }
  3917. }
  3918. // adds the correct word, followed by ] to close the blank
  3919. $answer .= ' / <font color="green"><b>'.$real_correct_tags[$i].'</b></font>]';
  3920. if (isset($real_text[$i + 1])) {
  3921. $answer .= $real_text[$i + 1];
  3922. }
  3923. }
  3924. return $good_answer;
  3925. }
  3926. /**
  3927. * @param int $exercise_id
  3928. * @param string $course_code
  3929. * @param int $session_id
  3930. *
  3931. * @return int
  3932. */
  3933. public static function get_number_students_finish_exercise(
  3934. $exercise_id,
  3935. $course_code,
  3936. $session_id
  3937. ) {
  3938. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  3939. $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  3940. $exercise_id = (int) $exercise_id;
  3941. $course_code = Database::escape_string($course_code);
  3942. $session_id = (int) $session_id;
  3943. $sql = "SELECT DISTINCT exe_user_id
  3944. FROM $track_exercises e
  3945. INNER JOIN $track_attempt a
  3946. ON (a.exe_id = e.exe_id)
  3947. WHERE
  3948. exe_exo_id = $exercise_id AND
  3949. course_code = '$course_code' AND
  3950. e.session_id = $session_id AND
  3951. status = ''";
  3952. $result = Database::query($sql);
  3953. $return = 0;
  3954. if ($result) {
  3955. $return = Database::num_rows($result);
  3956. }
  3957. return $return;
  3958. }
  3959. /**
  3960. * Return an HTML select menu with the student groups.
  3961. *
  3962. * @param string $name is the name and the id of the <select>
  3963. * @param string $default default value for option
  3964. * @param string $onchange
  3965. *
  3966. * @return string the html code of the <select>
  3967. */
  3968. public static function displayGroupMenu($name, $default, $onchange = "")
  3969. {
  3970. // check the default value of option
  3971. $tabSelected = [$default => " selected='selected' "];
  3972. $res = "";
  3973. $res .= "<select name='$name' id='$name' onchange='".$onchange."' >";
  3974. $res .= "<option value='-1'".$tabSelected["-1"].">-- ".get_lang(
  3975. 'AllGroups'
  3976. )." --</option>";
  3977. $res .= "<option value='0'".$tabSelected["0"].">- ".get_lang(
  3978. 'NotInAGroup'
  3979. )." -</option>";
  3980. $tabGroups = GroupManager::get_group_list();
  3981. $currentCatId = 0;
  3982. $countGroups = count($tabGroups);
  3983. for ($i = 0; $i < $countGroups; $i++) {
  3984. $tabCategory = GroupManager::get_category_from_group(
  3985. $tabGroups[$i]['iid']
  3986. );
  3987. if ($tabCategory["id"] != $currentCatId) {
  3988. $res .= "<option value='-1' disabled='disabled'>".$tabCategory["title"]."</option>";
  3989. $currentCatId = $tabCategory["id"];
  3990. }
  3991. $res .= "<option ".$tabSelected[$tabGroups[$i]["id"]]."style='margin-left:40px' value='".
  3992. $tabGroups[$i]["id"]."'>".
  3993. $tabGroups[$i]["name"].
  3994. "</option>";
  3995. }
  3996. $res .= "</select>";
  3997. return $res;
  3998. }
  3999. /**
  4000. * @param int $exe_id
  4001. */
  4002. public static function create_chat_exercise_session($exe_id)
  4003. {
  4004. if (!isset($_SESSION['current_exercises'])) {
  4005. $_SESSION['current_exercises'] = [];
  4006. }
  4007. $_SESSION['current_exercises'][$exe_id] = true;
  4008. }
  4009. /**
  4010. * @param int $exe_id
  4011. */
  4012. public static function delete_chat_exercise_session($exe_id)
  4013. {
  4014. if (isset($_SESSION['current_exercises'])) {
  4015. $_SESSION['current_exercises'][$exe_id] = false;
  4016. }
  4017. }
  4018. /**
  4019. * Display the exercise results.
  4020. *
  4021. * @param Exercise $objExercise
  4022. * @param int $exeId
  4023. * @param bool $save_user_result save users results (true) or just show the results (false)
  4024. * @param string $remainingMessage
  4025. */
  4026. public static function displayQuestionListByAttempt(
  4027. $objExercise,
  4028. $exeId,
  4029. $save_user_result = false,
  4030. $remainingMessage = ''
  4031. ) {
  4032. $origin = api_get_origin();
  4033. $courseCode = api_get_course_id();
  4034. $sessionId = api_get_session_id();
  4035. // Getting attempt info
  4036. $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exeId);
  4037. // Getting question list
  4038. $question_list = [];
  4039. $studentInfo = [];
  4040. if (!empty($exercise_stat_info['data_tracking'])) {
  4041. $studentInfo = api_get_user_info($exercise_stat_info['exe_user_id']);
  4042. $question_list = explode(',', $exercise_stat_info['data_tracking']);
  4043. } else {
  4044. // Try getting the question list only if save result is off
  4045. if ($save_user_result == false) {
  4046. $question_list = $objExercise->get_validated_question_list();
  4047. }
  4048. if (in_array(
  4049. $objExercise->getFeedbackType(),
  4050. [EXERCISE_FEEDBACK_TYPE_DIRECT, EXERCISE_FEEDBACK_TYPE_POPUP]
  4051. )) {
  4052. $question_list = $objExercise->get_validated_question_list();
  4053. }
  4054. }
  4055. if ($objExercise->getResultAccess()) {
  4056. if ($objExercise->hasResultsAccess($exercise_stat_info) === false) {
  4057. echo Display::return_message(
  4058. sprintf(get_lang('YouPassedTheLimitOfXMinutesToSeeTheResults'), $objExercise->getResultsAccess())
  4059. );
  4060. return false;
  4061. }
  4062. if (!empty($objExercise->getResultAccess())) {
  4063. $url = api_get_path(WEB_CODE_PATH).'exercise/overview.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id;
  4064. echo $objExercise->returnTimeLeftDiv();
  4065. echo $objExercise->showSimpleTimeControl(
  4066. $objExercise->getResultAccessTimeDiff($exercise_stat_info),
  4067. $url
  4068. );
  4069. }
  4070. }
  4071. $counter = 1;
  4072. $total_score = $total_weight = 0;
  4073. $exercise_content = null;
  4074. // Hide results
  4075. $show_results = false;
  4076. $show_only_score = false;
  4077. if (in_array($objExercise->results_disabled,
  4078. [
  4079. RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
  4080. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
  4081. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
  4082. ]
  4083. )) {
  4084. $show_results = true;
  4085. }
  4086. if (in_array(
  4087. $objExercise->results_disabled,
  4088. [
  4089. RESULT_DISABLE_SHOW_SCORE_ONLY,
  4090. RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES,
  4091. RESULT_DISABLE_RANKING,
  4092. ]
  4093. )
  4094. ) {
  4095. $show_only_score = true;
  4096. }
  4097. // Not display expected answer, but score, and feedback
  4098. $show_all_but_expected_answer = false;
  4099. if ($objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_ONLY &&
  4100. $objExercise->getFeedbackType() == EXERCISE_FEEDBACK_TYPE_END
  4101. ) {
  4102. $show_all_but_expected_answer = true;
  4103. $show_results = true;
  4104. $show_only_score = false;
  4105. }
  4106. $showTotalScoreAndUserChoicesInLastAttempt = true;
  4107. $showTotalScore = true;
  4108. $showQuestionScore = true;
  4109. if (in_array(
  4110. $objExercise->results_disabled,
  4111. [
  4112. RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT,
  4113. RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK,
  4114. ])
  4115. ) {
  4116. $show_only_score = true;
  4117. $show_results = true;
  4118. $numberAttempts = 0;
  4119. if ($objExercise->attempts > 0) {
  4120. $attempts = Event::getExerciseResultsByUser(
  4121. api_get_user_id(),
  4122. $objExercise->id,
  4123. api_get_course_int_id(),
  4124. api_get_session_id(),
  4125. $exercise_stat_info['orig_lp_id'],
  4126. $exercise_stat_info['orig_lp_item_id'],
  4127. 'desc'
  4128. );
  4129. if ($attempts) {
  4130. $numberAttempts = count($attempts);
  4131. }
  4132. if ($save_user_result) {
  4133. $numberAttempts++;
  4134. }
  4135. $showTotalScore = false;
  4136. $showTotalScoreAndUserChoicesInLastAttempt = false;
  4137. if ($numberAttempts >= $objExercise->attempts) {
  4138. $showTotalScore = true;
  4139. $show_results = true;
  4140. $show_only_score = false;
  4141. $showTotalScoreAndUserChoicesInLastAttempt = true;
  4142. }
  4143. }
  4144. if ($objExercise->results_disabled ==
  4145. RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK
  4146. ) {
  4147. $show_only_score = false;
  4148. $show_results = true;
  4149. $show_all_but_expected_answer = false;
  4150. $showTotalScore = false;
  4151. $showQuestionScore = false;
  4152. if ($numberAttempts >= $objExercise->attempts) {
  4153. $showTotalScore = true;
  4154. $showQuestionScore = true;
  4155. }
  4156. }
  4157. }
  4158. if (($show_results || $show_only_score) && $origin !== 'embeddable') {
  4159. if (isset($exercise_stat_info['exe_user_id'])) {
  4160. if (!empty($studentInfo)) {
  4161. // Shows exercise header
  4162. echo $objExercise->showExerciseResultHeader(
  4163. $studentInfo,
  4164. $exercise_stat_info
  4165. );
  4166. }
  4167. }
  4168. }
  4169. // Display text when test is finished #4074 and for LP #4227
  4170. $endOfMessage = $objExercise->getTextWhenFinished();
  4171. if (!empty($endOfMessage)) {
  4172. echo Display::return_message($endOfMessage, 'normal', false);
  4173. echo "<div class='clear'>&nbsp;</div>";
  4174. }
  4175. $question_list_answers = [];
  4176. $media_list = [];
  4177. $category_list = [];
  4178. $loadChoiceFromSession = false;
  4179. $fromDatabase = true;
  4180. $exerciseResult = null;
  4181. $exerciseResultCoordinates = null;
  4182. $delineationResults = null;
  4183. if (in_array(
  4184. $objExercise->getFeedbackType(),
  4185. [EXERCISE_FEEDBACK_TYPE_DIRECT, EXERCISE_FEEDBACK_TYPE_POPUP]
  4186. )) {
  4187. $loadChoiceFromSession = true;
  4188. $fromDatabase = false;
  4189. $exerciseResult = Session::read('exerciseResult');
  4190. $exerciseResultCoordinates = Session::read('exerciseResultCoordinates');
  4191. $delineationResults = Session::read('hotspot_delineation_result');
  4192. $delineationResults = isset($delineationResults[$objExercise->id]) ? $delineationResults[$objExercise->id] : null;
  4193. }
  4194. $countPendingQuestions = 0;
  4195. $result = [];
  4196. // Loop over all question to show results for each of them, one by one
  4197. if (!empty($question_list)) {
  4198. foreach ($question_list as $questionId) {
  4199. // Creates a temporary Question object
  4200. $objQuestionTmp = Question::read($questionId, $objExercise->course);
  4201. // This variable came from exercise_submit_modal.php
  4202. ob_start();
  4203. $choice = null;
  4204. $delineationChoice = null;
  4205. if ($loadChoiceFromSession) {
  4206. $choice = isset($exerciseResult[$questionId]) ? $exerciseResult[$questionId] : null;
  4207. $delineationChoice = isset($delineationResults[$questionId]) ? $delineationResults[$questionId] : null;
  4208. }
  4209. // We're inside *one* question. Go through each possible answer for this question
  4210. $result = $objExercise->manage_answer(
  4211. $exeId,
  4212. $questionId,
  4213. $choice,
  4214. 'exercise_result',
  4215. $exerciseResultCoordinates,
  4216. $save_user_result,
  4217. $fromDatabase,
  4218. $show_results,
  4219. $objExercise->selectPropagateNeg(),
  4220. $delineationChoice,
  4221. $showTotalScoreAndUserChoicesInLastAttempt
  4222. );
  4223. if (empty($result)) {
  4224. continue;
  4225. }
  4226. $total_score += $result['score'];
  4227. $total_weight += $result['weight'];
  4228. $question_list_answers[] = [
  4229. 'question' => $result['open_question'],
  4230. 'answer' => $result['open_answer'],
  4231. 'answer_type' => $result['answer_type'],
  4232. 'generated_oral_file' => $result['generated_oral_file'],
  4233. ];
  4234. $my_total_score = $result['score'];
  4235. $my_total_weight = $result['weight'];
  4236. // Category report
  4237. $category_was_added_for_this_test = false;
  4238. if (isset($objQuestionTmp->category) && !empty($objQuestionTmp->category)) {
  4239. if (!isset($category_list[$objQuestionTmp->category]['score'])) {
  4240. $category_list[$objQuestionTmp->category]['score'] = 0;
  4241. }
  4242. if (!isset($category_list[$objQuestionTmp->category]['total'])) {
  4243. $category_list[$objQuestionTmp->category]['total'] = 0;
  4244. }
  4245. $category_list[$objQuestionTmp->category]['score'] += $my_total_score;
  4246. $category_list[$objQuestionTmp->category]['total'] += $my_total_weight;
  4247. $category_was_added_for_this_test = true;
  4248. }
  4249. if (isset($objQuestionTmp->category_list) && !empty($objQuestionTmp->category_list)) {
  4250. foreach ($objQuestionTmp->category_list as $category_id) {
  4251. $category_list[$category_id]['score'] += $my_total_score;
  4252. $category_list[$category_id]['total'] += $my_total_weight;
  4253. $category_was_added_for_this_test = true;
  4254. }
  4255. }
  4256. // No category for this question!
  4257. if ($category_was_added_for_this_test == false) {
  4258. if (!isset($category_list['none']['score'])) {
  4259. $category_list['none']['score'] = 0;
  4260. }
  4261. if (!isset($category_list['none']['total'])) {
  4262. $category_list['none']['total'] = 0;
  4263. }
  4264. $category_list['none']['score'] += $my_total_score;
  4265. $category_list['none']['total'] += $my_total_weight;
  4266. }
  4267. if ($objExercise->selectPropagateNeg() == 0 && $my_total_score < 0) {
  4268. $my_total_score = 0;
  4269. }
  4270. $comnt = null;
  4271. if ($show_results) {
  4272. $comnt = Event::get_comments($exeId, $questionId);
  4273. $teacherAudio = self::getOralFeedbackAudio(
  4274. $exeId,
  4275. $questionId,
  4276. api_get_user_id()
  4277. );
  4278. if (!empty($comnt) || $teacherAudio) {
  4279. echo '<b>'.get_lang('Feedback').'</b>';
  4280. }
  4281. if (!empty($comnt)) {
  4282. echo self::getFeedbackText($comnt);
  4283. }
  4284. if ($teacherAudio) {
  4285. echo $teacherAudio;
  4286. }
  4287. }
  4288. $score = [];
  4289. if ($show_results) {
  4290. $scorePassed = $my_total_score >= $my_total_weight;
  4291. if (function_exists('bccomp')) {
  4292. $compareResult = bccomp($my_total_score, $my_total_weight, 3);
  4293. $scorePassed = $compareResult === 1 || $compareResult === 0;
  4294. }
  4295. $score = [
  4296. 'result' => self::show_score(
  4297. $my_total_score,
  4298. $my_total_weight,
  4299. false
  4300. ),
  4301. 'pass' => $scorePassed,
  4302. 'score' => $my_total_score,
  4303. 'weight' => $my_total_weight,
  4304. 'comments' => $comnt,
  4305. 'user_answered' => $result['user_answered'],
  4306. ];
  4307. }
  4308. if (in_array($objQuestionTmp->type, [FREE_ANSWER, ORAL_EXPRESSION, ANNOTATION])) {
  4309. $reviewScore = [
  4310. 'score' => $my_total_score,
  4311. 'comments' => Event::get_comments($exeId, $questionId),
  4312. ];
  4313. $check = $objQuestionTmp->isQuestionWaitingReview($reviewScore);
  4314. if ($check === false) {
  4315. $countPendingQuestions++;
  4316. }
  4317. }
  4318. $contents = ob_get_clean();
  4319. $question_content = '';
  4320. if ($show_results) {
  4321. $question_content = '<div class="question_row_answer">';
  4322. if ($showQuestionScore == false) {
  4323. $score = [];
  4324. }
  4325. // Shows question title an description
  4326. $question_content .= $objQuestionTmp->return_header(
  4327. $objExercise,
  4328. $counter,
  4329. $score
  4330. );
  4331. }
  4332. $counter++;
  4333. $question_content .= $contents;
  4334. if ($show_results) {
  4335. $question_content .= '</div>';
  4336. }
  4337. if ($objExercise->showExpectedChoice()) {
  4338. $exercise_content .= Display::div(
  4339. Display::panel($question_content),
  4340. ['class' => 'question-panel']
  4341. );
  4342. } else {
  4343. // $show_all_but_expected_answer should not happen at
  4344. // the same time as $show_results
  4345. if ($show_results && !$show_only_score) {
  4346. $exercise_content .= Display::div(
  4347. Display::panel($question_content),
  4348. ['class' => 'question-panel']
  4349. );
  4350. }
  4351. }
  4352. } // end foreach() block that loops over all questions
  4353. }
  4354. $totalScoreText = null;
  4355. $certificateBlock = '';
  4356. if (($show_results || $show_only_score) && $showTotalScore) {
  4357. if ($result['answer_type'] == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  4358. echo '<h1 style="text-align : center; margin : 20px 0;">'.get_lang('YourResults').'</h1><br />';
  4359. }
  4360. $totalScoreText .= '<div class="question_row_score">';
  4361. if ($result['answer_type'] == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  4362. $totalScoreText .= self::getQuestionDiagnosisRibbon(
  4363. $objExercise,
  4364. $total_score,
  4365. $total_weight,
  4366. true
  4367. );
  4368. } else {
  4369. $pluginEvaluation = QuestionOptionsEvaluationPlugin::create();
  4370. if ('true' === $pluginEvaluation->get(QuestionOptionsEvaluationPlugin::SETTING_ENABLE)) {
  4371. $formula = $pluginEvaluation->getFormulaForExercise($objExercise->selectId());
  4372. if (!empty($formula)) {
  4373. $total_score = $pluginEvaluation->getResultWithFormula($exeId, $formula);
  4374. $total_weight = $pluginEvaluation->getMaxScore();
  4375. }
  4376. }
  4377. $totalScoreText .= self::getTotalScoreRibbon(
  4378. $objExercise,
  4379. $total_score,
  4380. $total_weight,
  4381. true,
  4382. $countPendingQuestions
  4383. );
  4384. }
  4385. $totalScoreText .= '</div>';
  4386. if (!empty($studentInfo)) {
  4387. $certificateBlock = self::generateAndShowCertificateBlock(
  4388. $total_score,
  4389. $total_weight,
  4390. $objExercise,
  4391. $studentInfo['id'],
  4392. $courseCode,
  4393. $sessionId
  4394. );
  4395. }
  4396. }
  4397. if ($result['answer_type'] == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  4398. $chartMultiAnswer = MultipleAnswerTrueFalseDegreeCertainty::displayStudentsChartResults(
  4399. $exeId,
  4400. $objExercise
  4401. );
  4402. echo $chartMultiAnswer;
  4403. }
  4404. if (!empty($category_list) && ($show_results || $show_only_score)) {
  4405. // Adding total
  4406. $category_list['total'] = [
  4407. 'score' => $total_score,
  4408. 'total' => $total_weight,
  4409. ];
  4410. echo TestCategory::get_stats_table_by_attempt(
  4411. $objExercise->id,
  4412. $category_list
  4413. );
  4414. }
  4415. if ($show_all_but_expected_answer) {
  4416. $exercise_content .= Display::return_message(get_lang('ExerciseWithFeedbackWithoutCorrectionComment'));
  4417. }
  4418. // Remove audio auto play from questions on results page - refs BT#7939
  4419. $exercise_content = preg_replace(
  4420. ['/autoplay[\=\".+\"]+/', '/autostart[\=\".+\"]+/'],
  4421. '',
  4422. $exercise_content
  4423. );
  4424. echo $totalScoreText;
  4425. echo $certificateBlock;
  4426. // Ofaj change BT#11784
  4427. if (api_get_configuration_value('quiz_show_description_on_results_page') &&
  4428. !empty($objExercise->description)
  4429. ) {
  4430. echo Display::div($objExercise->description, ['class' => 'exercise_description']);
  4431. }
  4432. echo $exercise_content;
  4433. if (!$show_only_score) {
  4434. echo $totalScoreText;
  4435. }
  4436. if ($save_user_result) {
  4437. // Tracking of results
  4438. if ($exercise_stat_info) {
  4439. $learnpath_id = $exercise_stat_info['orig_lp_id'];
  4440. $learnpath_item_id = $exercise_stat_info['orig_lp_item_id'];
  4441. $learnpath_item_view_id = $exercise_stat_info['orig_lp_item_view_id'];
  4442. if (api_is_allowed_to_session_edit()) {
  4443. Event::updateEventExercise(
  4444. $exercise_stat_info['exe_id'],
  4445. $objExercise->selectId(),
  4446. $total_score,
  4447. $total_weight,
  4448. api_get_session_id(),
  4449. $learnpath_id,
  4450. $learnpath_item_id,
  4451. $learnpath_item_view_id,
  4452. $exercise_stat_info['exe_duration'],
  4453. $question_list
  4454. );
  4455. $allowStats = api_get_configuration_value('allow_gradebook_stats');
  4456. if ($allowStats) {
  4457. $objExercise->generateStats(
  4458. $objExercise->selectId(),
  4459. api_get_course_info(),
  4460. api_get_session_id()
  4461. );
  4462. }
  4463. }
  4464. }
  4465. // Send notification at the end
  4466. if (!api_is_allowed_to_edit(null, true) &&
  4467. !api_is_excluded_user_type()
  4468. ) {
  4469. $objExercise->send_mail_notification_for_exam(
  4470. 'end',
  4471. $question_list_answers,
  4472. $origin,
  4473. $exeId,
  4474. $total_score,
  4475. $total_weight
  4476. );
  4477. }
  4478. }
  4479. if (in_array(
  4480. $objExercise->selectResultsDisabled(),
  4481. [RESULT_DISABLE_RANKING, RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING]
  4482. )) {
  4483. echo Display::page_header(get_lang('Ranking'), null, 'h4');
  4484. echo self::displayResultsInRanking(
  4485. $objExercise->iId,
  4486. api_get_user_id(),
  4487. api_get_course_int_id(),
  4488. api_get_session_id()
  4489. );
  4490. }
  4491. if (!empty($remainingMessage)) {
  4492. echo Display::return_message($remainingMessage, 'normal', false);
  4493. }
  4494. }
  4495. /**
  4496. * Display the ranking of results in a exercise.
  4497. *
  4498. * @param int $exerciseId
  4499. * @param int $currentUserId
  4500. * @param int $courseId
  4501. * @param int $sessionId
  4502. *
  4503. * @return string
  4504. */
  4505. public static function displayResultsInRanking($exerciseId, $currentUserId, $courseId, $sessionId = 0)
  4506. {
  4507. $data = self::exerciseResultsInRanking($exerciseId, $courseId, $sessionId);
  4508. $table = new HTML_Table(['class' => 'table table-hover table-bordered']);
  4509. $table->setHeaderContents(0, 0, get_lang('Position'), ['class' => 'text-right']);
  4510. $table->setHeaderContents(0, 1, get_lang('Username'));
  4511. $table->setHeaderContents(0, 2, get_lang('Score'), ['class' => 'text-right']);
  4512. $table->setHeaderContents(0, 3, get_lang('Date'), ['class' => 'text-center']);
  4513. foreach ($data as $r => $item) {
  4514. if (!isset($item[1])) {
  4515. continue;
  4516. }
  4517. $selected = $item[1]->getId() == $currentUserId;
  4518. foreach ($item as $c => $value) {
  4519. $table->setCellContents($r + 1, $c, $value);
  4520. $attrClass = '';
  4521. if (in_array($c, [0, 2])) {
  4522. $attrClass = 'text-right';
  4523. } elseif (3 == $c) {
  4524. $attrClass = 'text-center';
  4525. }
  4526. if ($selected) {
  4527. $attrClass .= ' warning';
  4528. }
  4529. $table->setCellAttributes($r + 1, $c, ['class' => $attrClass]);
  4530. }
  4531. }
  4532. return $table->toHtml();
  4533. }
  4534. /**
  4535. * Get the ranking for results in a exercise.
  4536. * Function used internally by ExerciseLib::displayResultsInRanking.
  4537. *
  4538. * @param int $exerciseId
  4539. * @param int $courseId
  4540. * @param int $sessionId
  4541. *
  4542. * @return array
  4543. */
  4544. public static function exerciseResultsInRanking($exerciseId, $courseId, $sessionId = 0)
  4545. {
  4546. $em = Database::getManager();
  4547. $dql = 'SELECT DISTINCT te.exeUserId FROM ChamiloCoreBundle:TrackEExercises te WHERE te.exeExoId = :id AND te.cId = :cId';
  4548. $dql .= api_get_session_condition($sessionId, true, false, 'te.sessionId');
  4549. $result = $em
  4550. ->createQuery($dql)
  4551. ->setParameters(['id' => $exerciseId, 'cId' => $courseId])
  4552. ->getScalarResult();
  4553. $data = [];
  4554. /** @var TrackEExercises $item */
  4555. foreach ($result as $item) {
  4556. $bestAttemp = self::get_best_attempt_by_user($item['exeUserId'], $exerciseId, $courseId, $sessionId = 0);
  4557. $data[] = $bestAttemp;
  4558. }
  4559. usort(
  4560. $data,
  4561. function ($a, $b) {
  4562. if ($a['exe_result'] != $b['exe_result']) {
  4563. return $a['exe_result'] > $b['exe_result'] ? -1 : 1;
  4564. }
  4565. if ($a['exe_date'] != $b['exe_date']) {
  4566. return $a['exe_date'] < $b['exe_date'] ? -1 : 1;
  4567. }
  4568. return 0;
  4569. }
  4570. );
  4571. // flags to display the same position in case of tie
  4572. $lastScore = $data[0]['exe_result'];
  4573. $position = 1;
  4574. $data = array_map(
  4575. function ($item) use (&$lastScore, &$position) {
  4576. if ($item['exe_result'] < $lastScore) {
  4577. $position++;
  4578. }
  4579. $lastScore = $item['exe_result'];
  4580. return [
  4581. $position,
  4582. api_get_user_entity($item['exe_user_id']),
  4583. self::show_score($item['exe_result'], $item['exe_weighting'], true, true, true),
  4584. api_convert_and_format_date($item['exe_date'], DATE_TIME_FORMAT_SHORT),
  4585. ];
  4586. },
  4587. $data
  4588. );
  4589. return $data;
  4590. }
  4591. /**
  4592. * Get a special ribbon on top of "degree of certainty" questions (
  4593. * variation from getTotalScoreRibbon() for other question types).
  4594. *
  4595. * @param Exercise $objExercise
  4596. * @param float $score
  4597. * @param float $weight
  4598. * @param bool $checkPassPercentage
  4599. *
  4600. * @return string
  4601. */
  4602. public static function getQuestionDiagnosisRibbon($objExercise, $score, $weight, $checkPassPercentage = false)
  4603. {
  4604. $displayChartDegree = true;
  4605. $ribbon = $displayChartDegree ? '<div class="ribbon">' : '';
  4606. if ($checkPassPercentage) {
  4607. $isSuccess = self::isSuccessExerciseResult(
  4608. $score, $weight, $objExercise->selectPassPercentage()
  4609. );
  4610. // Color the final test score if pass_percentage activated
  4611. $ribbonTotalSuccessOrError = '';
  4612. if (self::isPassPercentageEnabled($objExercise->selectPassPercentage())) {
  4613. if ($isSuccess) {
  4614. $ribbonTotalSuccessOrError = ' ribbon-total-success';
  4615. } else {
  4616. $ribbonTotalSuccessOrError = ' ribbon-total-error';
  4617. }
  4618. }
  4619. $ribbon .= $displayChartDegree ? '<div class="rib rib-total '.$ribbonTotalSuccessOrError.'">' : '';
  4620. } else {
  4621. $ribbon .= $displayChartDegree ? '<div class="rib rib-total">' : '';
  4622. }
  4623. if ($displayChartDegree) {
  4624. $ribbon .= '<h3>'.get_lang('YourTotalScore').':&nbsp;';
  4625. $ribbon .= self::show_score($score, $weight, false, true);
  4626. $ribbon .= '</h3>';
  4627. $ribbon .= '</div>';
  4628. }
  4629. if ($checkPassPercentage) {
  4630. $ribbon .= self::showSuccessMessage(
  4631. $score,
  4632. $weight,
  4633. $objExercise->selectPassPercentage()
  4634. );
  4635. }
  4636. $ribbon .= $displayChartDegree ? '</div>' : '';
  4637. return $ribbon;
  4638. }
  4639. /**
  4640. * @param Exercise $objExercise
  4641. * @param float $score
  4642. * @param float $weight
  4643. * @param bool $checkPassPercentage
  4644. * @param int $countPendingQuestions
  4645. *
  4646. * @return string
  4647. */
  4648. public static function getTotalScoreRibbon(
  4649. Exercise $objExercise,
  4650. $score,
  4651. $weight,
  4652. $checkPassPercentage = false,
  4653. $countPendingQuestions = 0
  4654. ) {
  4655. $hide = (int) $objExercise->getPageConfigurationAttribute('hide_total_score');
  4656. if ($hide === 1) {
  4657. return '';
  4658. }
  4659. $passPercentage = $objExercise->selectPassPercentage();
  4660. $ribbon = '<div class="title-score">';
  4661. if ($checkPassPercentage) {
  4662. $isSuccess = self::isSuccessExerciseResult(
  4663. $score,
  4664. $weight,
  4665. $passPercentage
  4666. );
  4667. // Color the final test score if pass_percentage activated
  4668. $class = '';
  4669. if (self::isPassPercentageEnabled($passPercentage)) {
  4670. if ($isSuccess) {
  4671. $class = ' ribbon-total-success';
  4672. } else {
  4673. $class = ' ribbon-total-error';
  4674. }
  4675. }
  4676. $ribbon .= '<div class="total '.$class.'">';
  4677. } else {
  4678. $ribbon .= '<div class="total">';
  4679. }
  4680. $ribbon .= '<h3>'.get_lang('YourTotalScore').':&nbsp;';
  4681. $ribbon .= self::show_score($score, $weight, false, true);
  4682. $ribbon .= '</h3>';
  4683. $ribbon .= '</div>';
  4684. if ($checkPassPercentage) {
  4685. $ribbon .= self::showSuccessMessage(
  4686. $score,
  4687. $weight,
  4688. $passPercentage
  4689. );
  4690. }
  4691. $ribbon .= '</div>';
  4692. if (!empty($countPendingQuestions)) {
  4693. $ribbon .= '<br />';
  4694. $ribbon .= Display::return_message(
  4695. sprintf(
  4696. get_lang('TempScoreXQuestionsNotCorrectedYet'),
  4697. $countPendingQuestions
  4698. ),
  4699. 'warning'
  4700. );
  4701. }
  4702. return $ribbon;
  4703. }
  4704. /**
  4705. * @param int $countLetter
  4706. *
  4707. * @return mixed
  4708. */
  4709. public static function detectInputAppropriateClass($countLetter)
  4710. {
  4711. $limits = [
  4712. 0 => 'input-mini',
  4713. 10 => 'input-mini',
  4714. 15 => 'input-medium',
  4715. 20 => 'input-xlarge',
  4716. 40 => 'input-xlarge',
  4717. 60 => 'input-xxlarge',
  4718. 100 => 'input-xxlarge',
  4719. 200 => 'input-xxlarge',
  4720. ];
  4721. foreach ($limits as $size => $item) {
  4722. if ($countLetter <= $size) {
  4723. return $item;
  4724. }
  4725. }
  4726. return $limits[0];
  4727. }
  4728. /**
  4729. * @param int $senderId
  4730. * @param array $course_info
  4731. * @param string $test
  4732. * @param string $url
  4733. *
  4734. * @return string
  4735. */
  4736. public static function getEmailNotification($senderId, $course_info, $test, $url)
  4737. {
  4738. $teacher_info = api_get_user_info($senderId);
  4739. $from_name = api_get_person_name(
  4740. $teacher_info['firstname'],
  4741. $teacher_info['lastname'],
  4742. null,
  4743. PERSON_NAME_EMAIL_ADDRESS
  4744. );
  4745. $view = new Template('', false, false, false, false, false, false);
  4746. $view->assign('course_title', Security::remove_XSS($course_info['name']));
  4747. $view->assign('test_title', Security::remove_XSS($test));
  4748. $view->assign('url', $url);
  4749. $view->assign('teacher_name', $from_name);
  4750. $template = $view->get_template('mail/exercise_result_alert_body.tpl');
  4751. return $view->fetch($template);
  4752. }
  4753. /**
  4754. * @return string
  4755. */
  4756. public static function getNotCorrectedYetText()
  4757. {
  4758. return Display::return_message(get_lang('notCorrectedYet'), 'warning');
  4759. }
  4760. /**
  4761. * @param string $message
  4762. *
  4763. * @return string
  4764. */
  4765. public static function getFeedbackText($message)
  4766. {
  4767. return Display::return_message($message, 'warning', false);
  4768. }
  4769. /**
  4770. * Get the recorder audio component for save a teacher audio feedback.
  4771. *
  4772. * @param int $attemptId
  4773. * @param int $questionId
  4774. * @param int $userId
  4775. *
  4776. * @return string
  4777. */
  4778. public static function getOralFeedbackForm($attemptId, $questionId, $userId)
  4779. {
  4780. $view = new Template('', false, false, false, false, false, false);
  4781. $view->assign('user_id', $userId);
  4782. $view->assign('question_id', $questionId);
  4783. $view->assign('directory', "/../exercises/teacher_audio/$attemptId/");
  4784. $view->assign('file_name', "{$questionId}_{$userId}");
  4785. $template = $view->get_template('exercise/oral_expression.tpl');
  4786. return $view->fetch($template);
  4787. }
  4788. /**
  4789. * Get the audio componen for a teacher audio feedback.
  4790. *
  4791. * @param int $attemptId
  4792. * @param int $questionId
  4793. * @param int $userId
  4794. *
  4795. * @return string
  4796. */
  4797. public static function getOralFeedbackAudio($attemptId, $questionId, $userId)
  4798. {
  4799. $courseInfo = api_get_course_info();
  4800. $sessionId = api_get_session_id();
  4801. $groupId = api_get_group_id();
  4802. $sysCourseDir = api_get_path(SYS_COURSE_PATH).$courseInfo['path'];
  4803. $webCourseDir = api_get_path(WEB_COURSE_PATH).$courseInfo['path'];
  4804. $fileName = "{$questionId}_{$userId}".DocumentManager::getDocumentSuffix($courseInfo, $sessionId, $groupId);
  4805. $filePath = null;
  4806. $relFilePath = "/exercises/teacher_audio/$attemptId/$fileName";
  4807. if (file_exists($sysCourseDir.$relFilePath.'.ogg')) {
  4808. $filePath = $webCourseDir.$relFilePath.'.ogg';
  4809. } elseif (file_exists($sysCourseDir.$relFilePath.'.wav.wav')) {
  4810. $filePath = $webCourseDir.$relFilePath.'.wav.wav';
  4811. } elseif (file_exists($sysCourseDir.$relFilePath.'.wav')) {
  4812. $filePath = $webCourseDir.$relFilePath.'.wav';
  4813. }
  4814. if (!$filePath) {
  4815. return '';
  4816. }
  4817. return Display::tag(
  4818. 'audio',
  4819. null,
  4820. ['src' => $filePath]
  4821. );
  4822. }
  4823. /**
  4824. * @return array
  4825. */
  4826. public static function getNotificationSettings()
  4827. {
  4828. $emailAlerts = [
  4829. 2 => get_lang('SendEmailToTeacherWhenStudentStartQuiz'),
  4830. 1 => get_lang('SendEmailToTeacherWhenStudentEndQuiz'), // default
  4831. 3 => get_lang('SendEmailToTeacherWhenStudentEndQuizOnlyIfOpenQuestion'),
  4832. 4 => get_lang('SendEmailToTeacherWhenStudentEndQuizOnlyIfOralQuestion'),
  4833. ];
  4834. return $emailAlerts;
  4835. }
  4836. /**
  4837. * Get the additional actions added in exercise_additional_teacher_modify_actions configuration.
  4838. *
  4839. * @param int $exerciseId
  4840. * @param int $iconSize
  4841. *
  4842. * @return string
  4843. */
  4844. public static function getAdditionalTeacherActions($exerciseId, $iconSize = ICON_SIZE_SMALL)
  4845. {
  4846. $additionalActions = api_get_configuration_value('exercise_additional_teacher_modify_actions') ?: [];
  4847. $actions = [];
  4848. foreach ($additionalActions as $additionalAction) {
  4849. $actions[] = call_user_func(
  4850. $additionalAction,
  4851. $exerciseId,
  4852. $iconSize
  4853. );
  4854. }
  4855. return implode(PHP_EOL, $actions);
  4856. }
  4857. /**
  4858. * @param DateTime $time
  4859. * @param int $userId
  4860. * @param int $courseId
  4861. * @param int $sessionId
  4862. *
  4863. * @throws \Doctrine\ORM\Query\QueryException
  4864. *
  4865. * @return int
  4866. */
  4867. public static function countAnsweredQuestionsByUserAfterTime(DateTime $time, $userId, $courseId, $sessionId)
  4868. {
  4869. $em = Database::getManager();
  4870. $time = api_get_utc_datetime($time->format('Y-m-d H:i:s'), false, true);
  4871. $result = $em
  4872. ->createQuery('
  4873. SELECT COUNT(ea) FROM ChamiloCoreBundle:TrackEAttempt ea
  4874. WHERE ea.userId = :user AND ea.cId = :course AND ea.sessionId = :session
  4875. AND ea.tms > :time
  4876. ')
  4877. ->setParameters(['user' => $userId, 'course' => $courseId, 'session' => $sessionId, 'time' => $time])
  4878. ->getSingleScalarResult();
  4879. return $result;
  4880. }
  4881. /**
  4882. * @param int $userId
  4883. * @param int $numberOfQuestions
  4884. * @param int $courseId
  4885. * @param int $sessionId
  4886. *
  4887. * @throws \Doctrine\ORM\Query\QueryException
  4888. *
  4889. * @return bool
  4890. */
  4891. public static function isQuestionsLimitPerDayReached($userId, $numberOfQuestions, $courseId, $sessionId)
  4892. {
  4893. $questionsLimitPerDay = (int) api_get_course_setting('quiz_question_limit_per_day');
  4894. if ($questionsLimitPerDay <= 0) {
  4895. return false;
  4896. }
  4897. $midnightTime = ChamiloApi::getServerMidnightTime();
  4898. $answeredQuestionsCount = self::countAnsweredQuestionsByUserAfterTime(
  4899. $midnightTime,
  4900. $userId,
  4901. $courseId,
  4902. $sessionId
  4903. );
  4904. return ($answeredQuestionsCount + $numberOfQuestions) > $questionsLimitPerDay;
  4905. }
  4906. /**
  4907. * Check if an exercise complies with the requirements to be embedded in the mobile app or a video.
  4908. * By making sure it is set on one question per page and it only contains unique-answer or multiple-answer questions
  4909. * or unique-answer image. And that the exam does not have immediate feedback.
  4910. *
  4911. * @param array $exercise Exercise info
  4912. *
  4913. * @throws \Doctrine\ORM\Query\QueryException
  4914. *
  4915. * @return bool
  4916. */
  4917. public static function isQuizEmbeddable(array $exercise)
  4918. {
  4919. $em = Database::getManager();
  4920. if (ONE_PER_PAGE != $exercise['type'] ||
  4921. in_array($exercise['feedback_type'], [EXERCISE_FEEDBACK_TYPE_DIRECT, EXERCISE_FEEDBACK_TYPE_POPUP])
  4922. ) {
  4923. return false;
  4924. }
  4925. $countAll = $em
  4926. ->createQuery('SELECT COUNT(qq)
  4927. FROM ChamiloCourseBundle:CQuizQuestion qq
  4928. INNER JOIN ChamiloCourseBundle:CQuizRelQuestion qrq
  4929. WITH qq.iid = qrq.questionId
  4930. WHERE qrq.exerciceId = :id'
  4931. )
  4932. ->setParameter('id', $exercise['iid'])
  4933. ->getSingleScalarResult();
  4934. $countOfAllowed = $em
  4935. ->createQuery('SELECT COUNT(qq)
  4936. FROM ChamiloCourseBundle:CQuizQuestion qq
  4937. INNER JOIN ChamiloCourseBundle:CQuizRelQuestion qrq
  4938. WITH qq.iid = qrq.questionId
  4939. WHERE qrq.exerciceId = :id AND qq.type IN (:types)'
  4940. )
  4941. ->setParameters(
  4942. [
  4943. 'id' => $exercise['iid'],
  4944. 'types' => [UNIQUE_ANSWER, MULTIPLE_ANSWER, UNIQUE_ANSWER_IMAGE],
  4945. ]
  4946. )
  4947. ->getSingleScalarResult();
  4948. return $countAll === $countOfAllowed;
  4949. }
  4950. /**
  4951. * Generate a certificate linked to current quiz and.
  4952. * Return the HTML block with links to download and view the certificate.
  4953. *
  4954. * @param float $totalScore
  4955. * @param float $totalWeight
  4956. * @param Exercise $objExercise
  4957. * @param int $studentId
  4958. * @param string $courseCode
  4959. * @param int $sessionId
  4960. *
  4961. * @return string
  4962. */
  4963. public static function generateAndShowCertificateBlock(
  4964. $totalScore,
  4965. $totalWeight,
  4966. Exercise $objExercise,
  4967. $studentId,
  4968. $courseCode,
  4969. $sessionId = 0
  4970. ) {
  4971. if (!api_get_configuration_value('quiz_generate_certificate_ending') ||
  4972. !self::isSuccessExerciseResult($totalScore, $totalWeight, $objExercise->selectPassPercentage())
  4973. ) {
  4974. return '';
  4975. }
  4976. /** @var Category $category */
  4977. $category = Category::load(null, null, $courseCode, null, null, $sessionId, 'ORDER By id');
  4978. if (empty($category)) {
  4979. return '';
  4980. }
  4981. /** @var Category $category */
  4982. $category = $category[0];
  4983. $categoryId = $category->get_id();
  4984. $link = LinkFactory::load(
  4985. null,
  4986. null,
  4987. $objExercise->selectId(),
  4988. null,
  4989. $courseCode,
  4990. $categoryId
  4991. );
  4992. if (empty($link)) {
  4993. return '';
  4994. }
  4995. $resourceDeletedMessage = $category->show_message_resource_delete($courseCode);
  4996. if (false !== $resourceDeletedMessage || api_is_allowed_to_edit() || api_is_excluded_user_type()) {
  4997. return '';
  4998. }
  4999. $certificate = Category::generateUserCertificate($categoryId, $studentId);
  5000. if (!is_array($certificate)) {
  5001. return '';
  5002. }
  5003. return Category::getDownloadCertificateBlock($certificate);
  5004. }
  5005. }