exercise.class.php 338 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\TrackEHotspot;
  4. use ChamiloSession as Session;
  5. /**
  6. * Class Exercise.
  7. *
  8. * Allows to instantiate an object of type Exercise
  9. *
  10. * @package chamilo.exercise
  11. *
  12. * @todo use getters and setters correctly
  13. *
  14. * @author Olivier Brouckaert
  15. * @author Julio Montoya Cleaning exercises
  16. * Modified by Hubert Borderiou #294
  17. */
  18. class Exercise
  19. {
  20. public $iId;
  21. public $id;
  22. public $name;
  23. public $title;
  24. public $exercise;
  25. public $description;
  26. public $sound;
  27. public $type; //ALL_ON_ONE_PAGE or ONE_PER_PAGE
  28. public $random;
  29. public $random_answers;
  30. public $active;
  31. public $timeLimit;
  32. public $attempts;
  33. public $feedback_type;
  34. public $end_time;
  35. public $start_time;
  36. public $questionList; // array with the list of this exercise's questions
  37. /* including question list of the media */
  38. public $questionListUncompressed;
  39. public $results_disabled;
  40. public $expired_time;
  41. public $course;
  42. public $course_id;
  43. public $propagate_neg;
  44. public $saveCorrectAnswers;
  45. public $review_answers;
  46. public $randomByCat;
  47. public $text_when_finished;
  48. public $display_category_name;
  49. public $pass_percentage;
  50. public $edit_exercise_in_lp = false;
  51. public $is_gradebook_locked = false;
  52. public $exercise_was_added_in_lp = false;
  53. public $lpList = [];
  54. public $force_edit_exercise_in_lp = false;
  55. public $categories;
  56. public $categories_grouping = true;
  57. public $endButton = 0;
  58. public $categoryWithQuestionList;
  59. public $mediaList;
  60. public $loadQuestionAJAX = false;
  61. // Notification send to the teacher.
  62. public $emailNotificationTemplate = null;
  63. // Notification send to the student.
  64. public $emailNotificationTemplateToUser = null;
  65. public $countQuestions = 0;
  66. public $fastEdition = false;
  67. public $modelType = 1;
  68. public $questionSelectionType = EX_Q_SELECTION_ORDERED;
  69. public $hideQuestionTitle = 0;
  70. public $scoreTypeModel = 0;
  71. public $categoryMinusOne = true; // Shows the category -1: See BT#6540
  72. public $globalCategoryId = null;
  73. public $onSuccessMessage = null;
  74. public $onFailedMessage = null;
  75. public $emailAlert;
  76. public $notifyUserByEmail = '';
  77. public $sessionId = 0;
  78. public $questionFeedbackEnabled = false;
  79. public $questionTypeWithFeedback;
  80. public $showPreviousButton;
  81. public $notifications;
  82. public $export = false;
  83. public $autolaunch;
  84. /**
  85. * Constructor of the class.
  86. *
  87. * @param int $courseId
  88. *
  89. * @author Olivier Brouckaert
  90. */
  91. public function __construct($courseId = 0)
  92. {
  93. $this->iId = 0;
  94. $this->id = 0;
  95. $this->exercise = '';
  96. $this->description = '';
  97. $this->sound = '';
  98. $this->type = ALL_ON_ONE_PAGE;
  99. $this->random = 0;
  100. $this->random_answers = 0;
  101. $this->active = 1;
  102. $this->questionList = [];
  103. $this->timeLimit = 0;
  104. $this->end_time = '';
  105. $this->start_time = '';
  106. $this->results_disabled = 1;
  107. $this->expired_time = 0;
  108. $this->propagate_neg = 0;
  109. $this->saveCorrectAnswers = 0;
  110. $this->review_answers = false;
  111. $this->randomByCat = 0;
  112. $this->text_when_finished = '';
  113. $this->display_category_name = 0;
  114. $this->pass_percentage = 0;
  115. $this->modelType = 1;
  116. $this->questionSelectionType = EX_Q_SELECTION_ORDERED;
  117. $this->endButton = 0;
  118. $this->scoreTypeModel = 0;
  119. $this->globalCategoryId = null;
  120. $this->notifications = [];
  121. if (!empty($courseId)) {
  122. $courseInfo = api_get_course_info_by_id($courseId);
  123. } else {
  124. $courseInfo = api_get_course_info();
  125. }
  126. $this->course_id = $courseInfo['real_id'];
  127. $this->course = $courseInfo;
  128. $this->sessionId = api_get_session_id();
  129. // ALTER TABLE c_quiz_question ADD COLUMN feedback text;
  130. $this->questionFeedbackEnabled = api_get_configuration_value('allow_quiz_question_feedback');
  131. $this->showPreviousButton = true;
  132. }
  133. /**
  134. * Reads exercise information from the data base.
  135. *
  136. * @author Olivier Brouckaert
  137. *
  138. * @param int $id - exercise Id
  139. * @param bool $parseQuestionList
  140. *
  141. * @return bool - true if exercise exists, otherwise false
  142. */
  143. public function read($id, $parseQuestionList = true)
  144. {
  145. $table = Database::get_course_table(TABLE_QUIZ_TEST);
  146. $tableLpItem = Database::get_course_table(TABLE_LP_ITEM);
  147. $id = (int) $id;
  148. if (empty($this->course_id)) {
  149. return false;
  150. }
  151. $sql = "SELECT * FROM $table
  152. WHERE c_id = ".$this->course_id." AND id = ".$id;
  153. $result = Database::query($sql);
  154. // if the exercise has been found
  155. if ($object = Database::fetch_object($result)) {
  156. $this->iId = $object->iid;
  157. $this->id = $id;
  158. $this->exercise = $object->title;
  159. $this->name = $object->title;
  160. $this->title = $object->title;
  161. $this->description = $object->description;
  162. $this->sound = $object->sound;
  163. $this->type = $object->type;
  164. if (empty($this->type)) {
  165. $this->type = ONE_PER_PAGE;
  166. }
  167. $this->random = $object->random;
  168. $this->random_answers = $object->random_answers;
  169. $this->active = $object->active;
  170. $this->results_disabled = $object->results_disabled;
  171. $this->attempts = $object->max_attempt;
  172. $this->feedback_type = $object->feedback_type;
  173. $this->sessionId = $object->session_id;
  174. $this->propagate_neg = $object->propagate_neg;
  175. $this->saveCorrectAnswers = $object->save_correct_answers;
  176. $this->randomByCat = $object->random_by_category;
  177. $this->text_when_finished = $object->text_when_finished;
  178. $this->display_category_name = $object->display_category_name;
  179. $this->pass_percentage = $object->pass_percentage;
  180. $this->is_gradebook_locked = api_resource_is_locked_by_gradebook($id, LINK_EXERCISE);
  181. $this->review_answers = (isset($object->review_answers) && $object->review_answers == 1) ? true : false;
  182. $this->globalCategoryId = isset($object->global_category_id) ? $object->global_category_id : null;
  183. $this->questionSelectionType = isset($object->question_selection_type) ? $object->question_selection_type : null;
  184. $this->hideQuestionTitle = isset($object->hide_question_title) ? (int) $object->hide_question_title : 0;
  185. $this->autolaunch = isset($object->autolaunch) ? (int) $object->autolaunch : 0;
  186. $this->notifications = [];
  187. if (!empty($object->notifications)) {
  188. $this->notifications = explode(',', $object->notifications);
  189. }
  190. if (isset($object->show_previous_button)) {
  191. $this->showPreviousButton = $object->show_previous_button == 1 ? true : false;
  192. }
  193. $sql = "SELECT lp_id, max_score
  194. FROM $tableLpItem
  195. WHERE
  196. c_id = {$this->course_id} AND
  197. item_type = '".TOOL_QUIZ."' AND
  198. path = '".$id."'";
  199. $result = Database::query($sql);
  200. if (Database::num_rows($result) > 0) {
  201. $this->exercise_was_added_in_lp = true;
  202. $this->lpList = Database::store_result($result, 'ASSOC');
  203. }
  204. $this->force_edit_exercise_in_lp = api_get_configuration_value('force_edit_exercise_in_lp');
  205. $this->edit_exercise_in_lp = true;
  206. if ($this->exercise_was_added_in_lp) {
  207. $this->edit_exercise_in_lp = $this->force_edit_exercise_in_lp == true;
  208. }
  209. if (!empty($object->end_time)) {
  210. $this->end_time = $object->end_time;
  211. }
  212. if (!empty($object->start_time)) {
  213. $this->start_time = $object->start_time;
  214. }
  215. // Control time
  216. $this->expired_time = $object->expired_time;
  217. // Checking if question_order is correctly set
  218. if ($parseQuestionList) {
  219. $this->setQuestionList(true);
  220. }
  221. //overload questions list with recorded questions list
  222. //load questions only for exercises of type 'one question per page'
  223. //this is needed only is there is no questions
  224. // @todo not sure were in the code this is used somebody mess with the exercise tool
  225. // @todo don't know who add that config and why $_configuration['live_exercise_tracking']
  226. /*global $_configuration, $questionList;
  227. if ($this->type == ONE_PER_PAGE && $_SERVER['REQUEST_METHOD'] != 'POST'
  228. && defined('QUESTION_LIST_ALREADY_LOGGED') &&
  229. isset($_configuration['live_exercise_tracking']) && $_configuration['live_exercise_tracking']
  230. ) {
  231. $this->questionList = $questionList;
  232. }*/
  233. return true;
  234. }
  235. return false;
  236. }
  237. /**
  238. * @return string
  239. */
  240. public function getCutTitle()
  241. {
  242. $title = $this->getUnformattedTitle();
  243. return cut($title, EXERCISE_MAX_NAME_SIZE);
  244. }
  245. /**
  246. * returns the exercise ID.
  247. *
  248. * @author Olivier Brouckaert
  249. *
  250. * @return int - exercise ID
  251. */
  252. public function selectId()
  253. {
  254. return $this->id;
  255. }
  256. /**
  257. * returns the exercise title.
  258. *
  259. * @author Olivier Brouckaert
  260. *
  261. * @param bool $unformattedText Optional. Get the title without HTML tags
  262. *
  263. * @return string - exercise title
  264. */
  265. public function selectTitle($unformattedText = false)
  266. {
  267. if ($unformattedText) {
  268. return $this->getUnformattedTitle();
  269. }
  270. return $this->exercise;
  271. }
  272. /**
  273. * returns the number of attempts setted.
  274. *
  275. * @return int - exercise attempts
  276. */
  277. public function selectAttempts()
  278. {
  279. return $this->attempts;
  280. }
  281. /** returns the number of FeedbackType *
  282. * 0=>Feedback , 1=>DirectFeedback, 2=>NoFeedback.
  283. *
  284. * @return int - exercise attempts
  285. */
  286. public function selectFeedbackType()
  287. {
  288. return $this->feedback_type;
  289. }
  290. /**
  291. * returns the time limit.
  292. *
  293. * @return int
  294. */
  295. public function selectTimeLimit()
  296. {
  297. return $this->timeLimit;
  298. }
  299. /**
  300. * returns the exercise description.
  301. *
  302. * @author Olivier Brouckaert
  303. *
  304. * @return string - exercise description
  305. */
  306. public function selectDescription()
  307. {
  308. return $this->description;
  309. }
  310. /**
  311. * returns the exercise sound file.
  312. *
  313. * @author Olivier Brouckaert
  314. *
  315. * @return string - exercise description
  316. */
  317. public function selectSound()
  318. {
  319. return $this->sound;
  320. }
  321. /**
  322. * returns the exercise type.
  323. *
  324. * @author Olivier Brouckaert
  325. *
  326. * @return int - exercise type
  327. */
  328. public function selectType()
  329. {
  330. return $this->type;
  331. }
  332. /**
  333. * @return int
  334. */
  335. public function getModelType()
  336. {
  337. return $this->modelType;
  338. }
  339. /**
  340. * @return int
  341. */
  342. public function selectEndButton()
  343. {
  344. return $this->endButton;
  345. }
  346. /**
  347. * @return string
  348. */
  349. public function getOnSuccessMessage()
  350. {
  351. return $this->onSuccessMessage;
  352. }
  353. /**
  354. * @return string
  355. */
  356. public function getOnFailedMessage()
  357. {
  358. return $this->onFailedMessage;
  359. }
  360. /**
  361. * @author hubert borderiou 30-11-11
  362. *
  363. * @return int : do we display the question category name for students
  364. */
  365. public function selectDisplayCategoryName()
  366. {
  367. return $this->display_category_name;
  368. }
  369. /**
  370. * @return int
  371. */
  372. public function selectPassPercentage()
  373. {
  374. return $this->pass_percentage;
  375. }
  376. /**
  377. * Modify object to update the switch display_category_name.
  378. *
  379. * @author hubert borderiou 30-11-11
  380. *
  381. * @param int $value is an integer 0 or 1
  382. */
  383. public function updateDisplayCategoryName($value)
  384. {
  385. $this->display_category_name = $value;
  386. }
  387. /**
  388. * @author hubert borderiou 28-11-11
  389. *
  390. * @return string html text : the text to display ay the end of the test
  391. */
  392. public function selectTextWhenFinished()
  393. {
  394. return $this->text_when_finished;
  395. }
  396. /**
  397. * @param string $text
  398. *
  399. * @author hubert borderiou 28-11-11
  400. */
  401. public function updateTextWhenFinished($text)
  402. {
  403. $this->text_when_finished = $text;
  404. }
  405. /**
  406. * return 1 or 2 if randomByCat.
  407. *
  408. * @author hubert borderiou
  409. *
  410. * @return int - quiz random by category
  411. */
  412. public function getRandomByCategory()
  413. {
  414. return $this->randomByCat;
  415. }
  416. /**
  417. * return 0 if no random by cat
  418. * return 1 if random by cat, categories shuffled
  419. * return 2 if random by cat, categories sorted by alphabetic order.
  420. *
  421. * @author hubert borderiou
  422. *
  423. * @return int - quiz random by category
  424. */
  425. public function isRandomByCat()
  426. {
  427. $res = EXERCISE_CATEGORY_RANDOM_DISABLED;
  428. if ($this->randomByCat == EXERCISE_CATEGORY_RANDOM_SHUFFLED) {
  429. $res = EXERCISE_CATEGORY_RANDOM_SHUFFLED;
  430. } elseif ($this->randomByCat == EXERCISE_CATEGORY_RANDOM_ORDERED) {
  431. $res = EXERCISE_CATEGORY_RANDOM_ORDERED;
  432. }
  433. return $res;
  434. }
  435. /**
  436. * return nothing
  437. * update randomByCat value for object.
  438. *
  439. * @param int $random
  440. *
  441. * @author hubert borderiou
  442. */
  443. public function updateRandomByCat($random)
  444. {
  445. $this->randomByCat = EXERCISE_CATEGORY_RANDOM_DISABLED;
  446. if (in_array(
  447. $random,
  448. [
  449. EXERCISE_CATEGORY_RANDOM_SHUFFLED,
  450. EXERCISE_CATEGORY_RANDOM_ORDERED,
  451. EXERCISE_CATEGORY_RANDOM_DISABLED,
  452. ]
  453. )) {
  454. $this->randomByCat = $random;
  455. }
  456. }
  457. /**
  458. * Tells if questions are selected randomly, and if so returns the draws.
  459. *
  460. * @author Carlos Vargas
  461. *
  462. * @return int - results disabled exercise
  463. */
  464. public function selectResultsDisabled()
  465. {
  466. return $this->results_disabled;
  467. }
  468. /**
  469. * tells if questions are selected randomly, and if so returns the draws.
  470. *
  471. * @author Olivier Brouckaert
  472. *
  473. * @return bool
  474. */
  475. public function isRandom()
  476. {
  477. $isRandom = false;
  478. // "-1" means all questions will be random
  479. if ($this->random > 0 || $this->random == -1) {
  480. $isRandom = true;
  481. }
  482. return $isRandom;
  483. }
  484. /**
  485. * returns random answers status.
  486. *
  487. * @author Juan Carlos Rana
  488. */
  489. public function getRandomAnswers()
  490. {
  491. return $this->random_answers;
  492. }
  493. /**
  494. * Same as isRandom() but has a name applied to values different than 0 or 1.
  495. *
  496. * @return int
  497. */
  498. public function getShuffle()
  499. {
  500. return $this->random;
  501. }
  502. /**
  503. * returns the exercise status (1 = enabled ; 0 = disabled).
  504. *
  505. * @author Olivier Brouckaert
  506. *
  507. * @return int - 1 if enabled, otherwise 0
  508. */
  509. public function selectStatus()
  510. {
  511. return $this->active;
  512. }
  513. /**
  514. * If false the question list will be managed as always if true
  515. * the question will be filtered
  516. * depending of the exercise settings (table c_quiz_rel_category).
  517. *
  518. * @param bool $status active or inactive grouping
  519. */
  520. public function setCategoriesGrouping($status)
  521. {
  522. $this->categories_grouping = (bool) $status;
  523. }
  524. /**
  525. * @return int
  526. */
  527. public function getHideQuestionTitle()
  528. {
  529. return $this->hideQuestionTitle;
  530. }
  531. /**
  532. * @param $value
  533. */
  534. public function setHideQuestionTitle($value)
  535. {
  536. $this->hideQuestionTitle = (int) $value;
  537. }
  538. /**
  539. * @return int
  540. */
  541. public function getScoreTypeModel()
  542. {
  543. return $this->scoreTypeModel;
  544. }
  545. /**
  546. * @param int $value
  547. */
  548. public function setScoreTypeModel($value)
  549. {
  550. $this->scoreTypeModel = (int) $value;
  551. }
  552. /**
  553. * @return int
  554. */
  555. public function getGlobalCategoryId()
  556. {
  557. return $this->globalCategoryId;
  558. }
  559. /**
  560. * @param int $value
  561. */
  562. public function setGlobalCategoryId($value)
  563. {
  564. if (is_array($value) && isset($value[0])) {
  565. $value = $value[0];
  566. }
  567. $this->globalCategoryId = (int) $value;
  568. }
  569. /**
  570. * @param int $start
  571. * @param int $limit
  572. * @param int $sidx
  573. * @param string $sord
  574. * @param array $whereCondition
  575. * @param array $extraFields
  576. *
  577. * @return array
  578. */
  579. public function getQuestionListPagination(
  580. $start,
  581. $limit,
  582. $sidx,
  583. $sord,
  584. $whereCondition = [],
  585. $extraFields = []
  586. ) {
  587. if (!empty($this->id)) {
  588. $category_list = TestCategory::getListOfCategoriesNameForTest(
  589. $this->id,
  590. false
  591. );
  592. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  593. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  594. $sql = "SELECT q.iid
  595. FROM $TBL_EXERCICE_QUESTION e
  596. INNER JOIN $TBL_QUESTIONS q
  597. ON (e.question_id = q.id AND e.c_id = ".$this->course_id." )
  598. WHERE e.exercice_id = '".$this->id."' ";
  599. $orderCondition = "ORDER BY question_order";
  600. if (!empty($sidx) && !empty($sord)) {
  601. if ($sidx == 'question') {
  602. if (in_array(strtolower($sord), ['desc', 'asc'])) {
  603. $orderCondition = " ORDER BY q.$sidx $sord";
  604. }
  605. }
  606. }
  607. $sql .= $orderCondition;
  608. $limitCondition = null;
  609. if (isset($start) && isset($limit)) {
  610. $start = intval($start);
  611. $limit = intval($limit);
  612. $limitCondition = " LIMIT $start, $limit";
  613. }
  614. $sql .= $limitCondition;
  615. $result = Database::query($sql);
  616. $questions = [];
  617. if (Database::num_rows($result)) {
  618. if (!empty($extraFields)) {
  619. $extraFieldValue = new ExtraFieldValue('question');
  620. }
  621. while ($question = Database::fetch_array($result, 'ASSOC')) {
  622. /** @var Question $objQuestionTmp */
  623. $objQuestionTmp = Question::read($question['iid']);
  624. $category_labels = TestCategory::return_category_labels(
  625. $objQuestionTmp->category_list,
  626. $category_list
  627. );
  628. if (empty($category_labels)) {
  629. $category_labels = "-";
  630. }
  631. // Question type
  632. list($typeImg, $typeExpl) = $objQuestionTmp->get_type_icon_html();
  633. $question_media = null;
  634. if (!empty($objQuestionTmp->parent_id)) {
  635. $objQuestionMedia = Question::read($objQuestionTmp->parent_id);
  636. $question_media = Question::getMediaLabel($objQuestionMedia->question);
  637. }
  638. $questionType = Display::tag(
  639. 'div',
  640. Display::return_icon($typeImg, $typeExpl, [], ICON_SIZE_MEDIUM).$question_media
  641. );
  642. $question = [
  643. 'id' => $question['iid'],
  644. 'question' => $objQuestionTmp->selectTitle(),
  645. 'type' => $questionType,
  646. 'category' => Display::tag(
  647. 'div',
  648. '<a href="#" style="padding:0px; margin:0px;">'.$category_labels.'</a>'
  649. ),
  650. 'score' => $objQuestionTmp->selectWeighting(),
  651. 'level' => $objQuestionTmp->level,
  652. ];
  653. if (!empty($extraFields)) {
  654. foreach ($extraFields as $extraField) {
  655. $value = $extraFieldValue->get_values_by_handler_and_field_id(
  656. $question['id'],
  657. $extraField['id']
  658. );
  659. $stringValue = null;
  660. if ($value) {
  661. $stringValue = $value['field_value'];
  662. }
  663. $question[$extraField['field_variable']] = $stringValue;
  664. }
  665. }
  666. $questions[] = $question;
  667. }
  668. }
  669. return $questions;
  670. }
  671. }
  672. /**
  673. * Get question count per exercise from DB (any special treatment).
  674. *
  675. * @return int
  676. */
  677. public function getQuestionCount()
  678. {
  679. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  680. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  681. $sql = "SELECT count(q.id) as count
  682. FROM $TBL_EXERCICE_QUESTION e
  683. INNER JOIN $TBL_QUESTIONS q
  684. ON (e.question_id = q.id AND e.c_id = q.c_id)
  685. WHERE
  686. e.c_id = {$this->course_id} AND
  687. e.exercice_id = ".$this->id;
  688. $result = Database::query($sql);
  689. $count = 0;
  690. if (Database::num_rows($result)) {
  691. $row = Database::fetch_array($result);
  692. $count = (int) $row['count'];
  693. }
  694. return $count;
  695. }
  696. /**
  697. * @return array
  698. */
  699. public function getQuestionOrderedListByName()
  700. {
  701. if (empty($this->course_id) || empty($this->id)) {
  702. return [];
  703. }
  704. $exerciseQuestionTable = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  705. $questionTable = Database::get_course_table(TABLE_QUIZ_QUESTION);
  706. // Getting question list from the order (question list drag n drop interface ).
  707. $sql = "SELECT e.question_id
  708. FROM $exerciseQuestionTable e
  709. INNER JOIN $questionTable q
  710. ON (e.question_id= q.id AND e.c_id = q.c_id)
  711. WHERE
  712. e.c_id = {$this->course_id} AND
  713. e.exercice_id = '".$this->id."'
  714. ORDER BY q.question";
  715. $result = Database::query($sql);
  716. $list = [];
  717. if (Database::num_rows($result)) {
  718. $list = Database::store_result($result, 'ASSOC');
  719. }
  720. return $list;
  721. }
  722. /**
  723. * Selecting question list depending in the exercise-category
  724. * relationship (category table in exercise settings).
  725. *
  726. * @param array $question_list
  727. * @param int $questionSelectionType
  728. *
  729. * @return array
  730. */
  731. public function getQuestionListWithCategoryListFilteredByCategorySettings(
  732. $question_list,
  733. $questionSelectionType
  734. ) {
  735. $result = [
  736. 'question_list' => [],
  737. 'category_with_questions_list' => [],
  738. ];
  739. // Order/random categories
  740. $cat = new TestCategory();
  741. // Setting category order.
  742. switch ($questionSelectionType) {
  743. case EX_Q_SELECTION_ORDERED: // 1
  744. case EX_Q_SELECTION_RANDOM: // 2
  745. // This options are not allowed here.
  746. break;
  747. case EX_Q_SELECTION_CATEGORIES_ORDERED_QUESTIONS_ORDERED: // 3
  748. $categoriesAddedInExercise = $cat->getCategoryExerciseTree(
  749. $this,
  750. $this->course['real_id'],
  751. 'title ASC',
  752. false,
  753. true
  754. );
  755. $questions_by_category = TestCategory::getQuestionsByCat(
  756. $this->id,
  757. $question_list,
  758. $categoriesAddedInExercise
  759. );
  760. $question_list = $this->pickQuestionsPerCategory(
  761. $categoriesAddedInExercise,
  762. $question_list,
  763. $questions_by_category,
  764. true,
  765. false
  766. );
  767. break;
  768. case EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_ORDERED: // 4
  769. case EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_ORDERED_NO_GROUPED: // 7
  770. $categoriesAddedInExercise = $cat->getCategoryExerciseTree(
  771. $this,
  772. $this->course['real_id'],
  773. null,
  774. true,
  775. true
  776. );
  777. $questions_by_category = TestCategory::getQuestionsByCat(
  778. $this->id,
  779. $question_list,
  780. $categoriesAddedInExercise
  781. );
  782. $question_list = $this->pickQuestionsPerCategory(
  783. $categoriesAddedInExercise,
  784. $question_list,
  785. $questions_by_category,
  786. true,
  787. false
  788. );
  789. break;
  790. case EX_Q_SELECTION_CATEGORIES_ORDERED_QUESTIONS_RANDOM: // 5
  791. $categoriesAddedInExercise = $cat->getCategoryExerciseTree(
  792. $this,
  793. $this->course['real_id'],
  794. 'title ASC',
  795. false,
  796. true
  797. );
  798. $questions_by_category = TestCategory::getQuestionsByCat(
  799. $this->id,
  800. $question_list,
  801. $categoriesAddedInExercise
  802. );
  803. $question_list = $this->pickQuestionsPerCategory(
  804. $categoriesAddedInExercise,
  805. $question_list,
  806. $questions_by_category,
  807. true,
  808. true
  809. );
  810. break;
  811. case EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_RANDOM: // 6
  812. case EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_RANDOM_NO_GROUPED:
  813. $categoriesAddedInExercise = $cat->getCategoryExerciseTree(
  814. $this,
  815. $this->course['real_id'],
  816. null,
  817. true,
  818. true
  819. );
  820. $questions_by_category = TestCategory::getQuestionsByCat(
  821. $this->id,
  822. $question_list,
  823. $categoriesAddedInExercise
  824. );
  825. $question_list = $this->pickQuestionsPerCategory(
  826. $categoriesAddedInExercise,
  827. $question_list,
  828. $questions_by_category,
  829. true,
  830. true
  831. );
  832. break;
  833. case EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_ORDERED_NO_GROUPED: // 7
  834. break;
  835. case EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_RANDOM_NO_GROUPED: // 8
  836. break;
  837. case EX_Q_SELECTION_CATEGORIES_ORDERED_BY_PARENT_QUESTIONS_ORDERED: // 9
  838. $categoriesAddedInExercise = $cat->getCategoryExerciseTree(
  839. $this,
  840. $this->course['real_id'],
  841. 'root ASC, lft ASC',
  842. false,
  843. true
  844. );
  845. $questions_by_category = TestCategory::getQuestionsByCat(
  846. $this->id,
  847. $question_list,
  848. $categoriesAddedInExercise
  849. );
  850. $question_list = $this->pickQuestionsPerCategory(
  851. $categoriesAddedInExercise,
  852. $question_list,
  853. $questions_by_category,
  854. true,
  855. false
  856. );
  857. break;
  858. case EX_Q_SELECTION_CATEGORIES_ORDERED_BY_PARENT_QUESTIONS_RANDOM: // 10
  859. $categoriesAddedInExercise = $cat->getCategoryExerciseTree(
  860. $this,
  861. $this->course['real_id'],
  862. 'root, lft ASC',
  863. false,
  864. true
  865. );
  866. $questions_by_category = TestCategory::getQuestionsByCat(
  867. $this->id,
  868. $question_list,
  869. $categoriesAddedInExercise
  870. );
  871. $question_list = $this->pickQuestionsPerCategory(
  872. $categoriesAddedInExercise,
  873. $question_list,
  874. $questions_by_category,
  875. true,
  876. true
  877. );
  878. break;
  879. }
  880. $result['question_list'] = isset($question_list) ? $question_list : [];
  881. $result['category_with_questions_list'] = isset($questions_by_category) ? $questions_by_category : [];
  882. $parentsLoaded = [];
  883. // Adding category info in the category list with question list:
  884. if (!empty($questions_by_category)) {
  885. $newCategoryList = [];
  886. $em = Database::getManager();
  887. foreach ($questions_by_category as $categoryId => $questionList) {
  888. $cat = new TestCategory();
  889. $cat = $cat->getCategory($categoryId);
  890. if ($cat) {
  891. $cat = (array) $cat;
  892. $cat['iid'] = $cat['id'];
  893. }
  894. $categoryParentInfo = null;
  895. // Parent is not set no loop here
  896. if (isset($cat['parent_id']) && !empty($cat['parent_id'])) {
  897. /** @var \Chamilo\CourseBundle\Entity\CQuizCategory $categoryEntity */
  898. if (!isset($parentsLoaded[$cat['parent_id']])) {
  899. $categoryEntity = $em->find('ChamiloCoreBundle:CQuizCategory', $cat['parent_id']);
  900. $parentsLoaded[$cat['parent_id']] = $categoryEntity;
  901. } else {
  902. $categoryEntity = $parentsLoaded[$cat['parent_id']];
  903. }
  904. $repo = $em->getRepository('ChamiloCoreBundle:CQuizCategory');
  905. $path = $repo->getPath($categoryEntity);
  906. $index = 0;
  907. if ($this->categoryMinusOne) {
  908. //$index = 1;
  909. }
  910. /** @var \Chamilo\CourseBundle\Entity\CQuizCategory $categoryParent */
  911. foreach ($path as $categoryParent) {
  912. $visibility = $categoryParent->getVisibility();
  913. if ($visibility == 0) {
  914. $categoryParentId = $categoryId;
  915. $categoryTitle = $cat['title'];
  916. if (count($path) > 1) {
  917. continue;
  918. }
  919. } else {
  920. $categoryParentId = $categoryParent->getIid();
  921. $categoryTitle = $categoryParent->getTitle();
  922. }
  923. $categoryParentInfo['id'] = $categoryParentId;
  924. $categoryParentInfo['iid'] = $categoryParentId;
  925. $categoryParentInfo['parent_path'] = null;
  926. $categoryParentInfo['title'] = $categoryTitle;
  927. $categoryParentInfo['name'] = $categoryTitle;
  928. $categoryParentInfo['parent_id'] = null;
  929. break;
  930. }
  931. }
  932. $cat['parent_info'] = $categoryParentInfo;
  933. $newCategoryList[$categoryId] = [
  934. 'category' => $cat,
  935. 'question_list' => $questionList,
  936. ];
  937. }
  938. $result['category_with_questions_list'] = $newCategoryList;
  939. }
  940. return $result;
  941. }
  942. /**
  943. * returns the array with the question ID list.
  944. *
  945. * @param bool $fromDatabase Whether the results should be fetched in the database or just from memory
  946. * @param bool $adminView Whether we should return all questions (admin view) or
  947. * just a list limited by the max number of random questions
  948. *
  949. * @author Olivier Brouckaert
  950. *
  951. * @return array - question ID list
  952. */
  953. public function selectQuestionList($fromDatabase = false, $adminView = false)
  954. {
  955. if ($fromDatabase && !empty($this->id)) {
  956. $nbQuestions = $this->getQuestionCount();
  957. $questionSelectionType = $this->getQuestionSelectionType();
  958. switch ($questionSelectionType) {
  959. case EX_Q_SELECTION_ORDERED:
  960. $questionList = $this->getQuestionOrderedList();
  961. break;
  962. case EX_Q_SELECTION_RANDOM:
  963. // Not a random exercise, or if there are not at least 2 questions
  964. if ($this->random == 0 || $nbQuestions < 2) {
  965. $questionList = $this->getQuestionOrderedList();
  966. } else {
  967. $questionList = $this->getRandomList($adminView);
  968. }
  969. break;
  970. default:
  971. $questionList = $this->getQuestionOrderedList();
  972. $result = $this->getQuestionListWithCategoryListFilteredByCategorySettings(
  973. $questionList,
  974. $questionSelectionType
  975. );
  976. $this->categoryWithQuestionList = $result['category_with_questions_list'];
  977. $questionList = $result['question_list'];
  978. break;
  979. }
  980. return $questionList;
  981. }
  982. return $this->questionList;
  983. }
  984. /**
  985. * returns the number of questions in this exercise.
  986. *
  987. * @author Olivier Brouckaert
  988. *
  989. * @return int - number of questions
  990. */
  991. public function selectNbrQuestions()
  992. {
  993. return count($this->questionList);
  994. }
  995. /**
  996. * @return int
  997. */
  998. public function selectPropagateNeg()
  999. {
  1000. return $this->propagate_neg;
  1001. }
  1002. /**
  1003. * @return int
  1004. */
  1005. public function selectSaveCorrectAnswers()
  1006. {
  1007. return $this->saveCorrectAnswers;
  1008. }
  1009. /**
  1010. * Selects questions randomly in the question list.
  1011. *
  1012. * @author Olivier Brouckaert
  1013. * @author Hubert Borderiou 15 nov 2011
  1014. *
  1015. * @param bool $adminView Whether we should return all
  1016. * questions (admin view) or just a list limited by the max number of random questions
  1017. *
  1018. * @return array - if the exercise is not set to take questions randomly, returns the question list
  1019. * without randomizing, otherwise, returns the list with questions selected randomly
  1020. */
  1021. public function getRandomList($adminView = false)
  1022. {
  1023. $quizRelQuestion = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  1024. $question = Database::get_course_table(TABLE_QUIZ_QUESTION);
  1025. $random = isset($this->random) && !empty($this->random) ? $this->random : 0;
  1026. // Random with limit
  1027. $randomLimit = " ORDER BY RAND() LIMIT $random";
  1028. // Random with no limit
  1029. if ($random == -1) {
  1030. $randomLimit = ' ORDER BY RAND() ';
  1031. }
  1032. // Admin see the list in default order
  1033. if ($adminView === true) {
  1034. // If viewing it as admin for edition, don't show it randomly, use title + id
  1035. $randomLimit = 'ORDER BY e.question_order';
  1036. }
  1037. $sql = "SELECT e.question_id
  1038. FROM $quizRelQuestion e
  1039. INNER JOIN $question q
  1040. ON (e.question_id= q.id AND e.c_id = q.c_id)
  1041. WHERE
  1042. e.c_id = {$this->course_id} AND
  1043. e.exercice_id = '".Database::escape_string($this->id)."'
  1044. $randomLimit ";
  1045. $result = Database::query($sql);
  1046. $questionList = [];
  1047. while ($row = Database::fetch_object($result)) {
  1048. $questionList[] = $row->question_id;
  1049. }
  1050. return $questionList;
  1051. }
  1052. /**
  1053. * returns 'true' if the question ID is in the question list.
  1054. *
  1055. * @author Olivier Brouckaert
  1056. *
  1057. * @param int $questionId - question ID
  1058. *
  1059. * @return bool - true if in the list, otherwise false
  1060. */
  1061. public function isInList($questionId)
  1062. {
  1063. $inList = false;
  1064. if (is_array($this->questionList)) {
  1065. $inList = in_array($questionId, $this->questionList);
  1066. }
  1067. return $inList;
  1068. }
  1069. /**
  1070. * If current exercise has a question.
  1071. *
  1072. * @param int $questionId
  1073. *
  1074. * @return int
  1075. */
  1076. public function hasQuestion($questionId)
  1077. {
  1078. $questionId = (int) $questionId;
  1079. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  1080. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  1081. $sql = "SELECT q.id
  1082. FROM $TBL_EXERCICE_QUESTION e
  1083. INNER JOIN $TBL_QUESTIONS q
  1084. ON (e.question_id = q.id AND e.c_id = q.c_id)
  1085. WHERE
  1086. q.id = $questionId AND
  1087. e.c_id = {$this->course_id} AND
  1088. e.exercice_id = ".$this->id;
  1089. $result = Database::query($sql);
  1090. return Database::num_rows($result) > 0;
  1091. }
  1092. /**
  1093. * changes the exercise title.
  1094. *
  1095. * @author Olivier Brouckaert
  1096. *
  1097. * @param string $title - exercise title
  1098. */
  1099. public function updateTitle($title)
  1100. {
  1101. $this->title = $this->exercise = $title;
  1102. }
  1103. /**
  1104. * changes the exercise max attempts.
  1105. *
  1106. * @param int $attempts - exercise max attempts
  1107. */
  1108. public function updateAttempts($attempts)
  1109. {
  1110. $this->attempts = $attempts;
  1111. }
  1112. /**
  1113. * changes the exercise feedback type.
  1114. *
  1115. * @param int $feedback_type
  1116. */
  1117. public function updateFeedbackType($feedback_type)
  1118. {
  1119. $this->feedback_type = $feedback_type;
  1120. }
  1121. /**
  1122. * changes the exercise description.
  1123. *
  1124. * @author Olivier Brouckaert
  1125. *
  1126. * @param string $description - exercise description
  1127. */
  1128. public function updateDescription($description)
  1129. {
  1130. $this->description = $description;
  1131. }
  1132. /**
  1133. * changes the exercise expired_time.
  1134. *
  1135. * @author Isaac flores
  1136. *
  1137. * @param int $expired_time The expired time of the quiz
  1138. */
  1139. public function updateExpiredTime($expired_time)
  1140. {
  1141. $this->expired_time = $expired_time;
  1142. }
  1143. /**
  1144. * @param $value
  1145. */
  1146. public function updatePropagateNegative($value)
  1147. {
  1148. $this->propagate_neg = $value;
  1149. }
  1150. /**
  1151. * @param $value int
  1152. */
  1153. public function updateSaveCorrectAnswers($value)
  1154. {
  1155. $this->saveCorrectAnswers = $value;
  1156. }
  1157. /**
  1158. * @param $value
  1159. */
  1160. public function updateReviewAnswers($value)
  1161. {
  1162. $this->review_answers = isset($value) && $value ? true : false;
  1163. }
  1164. /**
  1165. * @param $value
  1166. */
  1167. public function updatePassPercentage($value)
  1168. {
  1169. $this->pass_percentage = $value;
  1170. }
  1171. /**
  1172. * @param string $text
  1173. */
  1174. public function updateEmailNotificationTemplate($text)
  1175. {
  1176. $this->emailNotificationTemplate = $text;
  1177. }
  1178. /**
  1179. * @param string $text
  1180. */
  1181. public function setEmailNotificationTemplateToUser($text)
  1182. {
  1183. $this->emailNotificationTemplateToUser = $text;
  1184. }
  1185. /**
  1186. * @param string $value
  1187. */
  1188. public function setNotifyUserByEmail($value)
  1189. {
  1190. $this->notifyUserByEmail = $value;
  1191. }
  1192. /**
  1193. * @param int $value
  1194. */
  1195. public function updateEndButton($value)
  1196. {
  1197. $this->endButton = (int) $value;
  1198. }
  1199. /**
  1200. * @param string $value
  1201. */
  1202. public function setOnSuccessMessage($value)
  1203. {
  1204. $this->onSuccessMessage = $value;
  1205. }
  1206. /**
  1207. * @param string $value
  1208. */
  1209. public function setOnFailedMessage($value)
  1210. {
  1211. $this->onFailedMessage = $value;
  1212. }
  1213. /**
  1214. * @param $value
  1215. */
  1216. public function setModelType($value)
  1217. {
  1218. $this->modelType = (int) $value;
  1219. }
  1220. /**
  1221. * @param int $value
  1222. */
  1223. public function setQuestionSelectionType($value)
  1224. {
  1225. $this->questionSelectionType = (int) $value;
  1226. }
  1227. /**
  1228. * @return int
  1229. */
  1230. public function getQuestionSelectionType()
  1231. {
  1232. return $this->questionSelectionType;
  1233. }
  1234. /**
  1235. * @param array $categories
  1236. */
  1237. public function updateCategories($categories)
  1238. {
  1239. if (!empty($categories)) {
  1240. $categories = array_map('intval', $categories);
  1241. $this->categories = $categories;
  1242. }
  1243. }
  1244. /**
  1245. * changes the exercise sound file.
  1246. *
  1247. * @author Olivier Brouckaert
  1248. *
  1249. * @param string $sound - exercise sound file
  1250. * @param string $delete - ask to delete the file
  1251. */
  1252. public function updateSound($sound, $delete)
  1253. {
  1254. global $audioPath, $documentPath;
  1255. $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  1256. if ($sound['size'] && (strstr($sound['type'], 'audio') || strstr($sound['type'], 'video'))) {
  1257. $this->sound = $sound['name'];
  1258. if (@move_uploaded_file($sound['tmp_name'], $audioPath.'/'.$this->sound)) {
  1259. $sql = "SELECT 1 FROM $TBL_DOCUMENT
  1260. WHERE
  1261. c_id = ".$this->course_id." AND
  1262. path = '".str_replace($documentPath, '', $audioPath).'/'.$this->sound."'";
  1263. $result = Database::query($sql);
  1264. if (!Database::num_rows($result)) {
  1265. $id = add_document(
  1266. $this->course,
  1267. str_replace($documentPath, '', $audioPath).'/'.$this->sound,
  1268. 'file',
  1269. $sound['size'],
  1270. $sound['name']
  1271. );
  1272. api_item_property_update(
  1273. $this->course,
  1274. TOOL_DOCUMENT,
  1275. $id,
  1276. 'DocumentAdded',
  1277. api_get_user_id()
  1278. );
  1279. item_property_update_on_folder(
  1280. $this->course,
  1281. str_replace($documentPath, '', $audioPath),
  1282. api_get_user_id()
  1283. );
  1284. }
  1285. }
  1286. } elseif ($delete && is_file($audioPath.'/'.$this->sound)) {
  1287. $this->sound = '';
  1288. }
  1289. }
  1290. /**
  1291. * changes the exercise type.
  1292. *
  1293. * @author Olivier Brouckaert
  1294. *
  1295. * @param int $type - exercise type
  1296. */
  1297. public function updateType($type)
  1298. {
  1299. $this->type = $type;
  1300. }
  1301. /**
  1302. * sets to 0 if questions are not selected randomly
  1303. * if questions are selected randomly, sets the draws.
  1304. *
  1305. * @author Olivier Brouckaert
  1306. *
  1307. * @param int $random - 0 if not random, otherwise the draws
  1308. */
  1309. public function setRandom($random)
  1310. {
  1311. $this->random = $random;
  1312. }
  1313. /**
  1314. * sets to 0 if answers are not selected randomly
  1315. * if answers are selected randomly.
  1316. *
  1317. * @author Juan Carlos Rana
  1318. *
  1319. * @param int $random_answers - random answers
  1320. */
  1321. public function updateRandomAnswers($random_answers)
  1322. {
  1323. $this->random_answers = $random_answers;
  1324. }
  1325. /**
  1326. * enables the exercise.
  1327. *
  1328. * @author Olivier Brouckaert
  1329. */
  1330. public function enable()
  1331. {
  1332. $this->active = 1;
  1333. }
  1334. /**
  1335. * disables the exercise.
  1336. *
  1337. * @author Olivier Brouckaert
  1338. */
  1339. public function disable()
  1340. {
  1341. $this->active = 0;
  1342. }
  1343. /**
  1344. * Set disable results.
  1345. */
  1346. public function disable_results()
  1347. {
  1348. $this->results_disabled = true;
  1349. }
  1350. /**
  1351. * Enable results.
  1352. */
  1353. public function enable_results()
  1354. {
  1355. $this->results_disabled = false;
  1356. }
  1357. /**
  1358. * @param int $results_disabled
  1359. */
  1360. public function updateResultsDisabled($results_disabled)
  1361. {
  1362. $this->results_disabled = (int) $results_disabled;
  1363. }
  1364. /**
  1365. * updates the exercise in the data base.
  1366. *
  1367. * @param string $type_e
  1368. *
  1369. * @author Olivier Brouckaert
  1370. */
  1371. public function save($type_e = '')
  1372. {
  1373. $_course = $this->course;
  1374. $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST);
  1375. $id = $this->id;
  1376. $exercise = $this->exercise;
  1377. $description = $this->description;
  1378. $sound = $this->sound;
  1379. $type = $this->type;
  1380. $attempts = isset($this->attempts) ? $this->attempts : 0;
  1381. $feedback_type = isset($this->feedback_type) ? $this->feedback_type : 0;
  1382. $random = $this->random;
  1383. $random_answers = $this->random_answers;
  1384. $active = $this->active;
  1385. $propagate_neg = (int) $this->propagate_neg;
  1386. $saveCorrectAnswers = isset($this->saveCorrectAnswers) && $this->saveCorrectAnswers ? 1 : 0;
  1387. $review_answers = isset($this->review_answers) && $this->review_answers ? 1 : 0;
  1388. $randomByCat = (int) $this->randomByCat;
  1389. $text_when_finished = $this->text_when_finished;
  1390. $display_category_name = (int) $this->display_category_name;
  1391. $pass_percentage = (int) $this->pass_percentage;
  1392. $session_id = $this->sessionId;
  1393. // If direct we do not show results
  1394. $results_disabled = (int) $this->results_disabled;
  1395. if ($feedback_type == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  1396. $results_disabled = 0;
  1397. }
  1398. $expired_time = (int) $this->expired_time;
  1399. // Exercise already exists
  1400. if ($id) {
  1401. // we prepare date in the database using the api_get_utc_datetime() function
  1402. $start_time = null;
  1403. if (!empty($this->start_time)) {
  1404. $start_time = $this->start_time;
  1405. }
  1406. $end_time = null;
  1407. if (!empty($this->end_time)) {
  1408. $end_time = $this->end_time;
  1409. }
  1410. $params = [
  1411. 'title' => $exercise,
  1412. 'description' => $description,
  1413. ];
  1414. $paramsExtra = [];
  1415. if ($type_e != 'simple') {
  1416. $paramsExtra = [
  1417. 'sound' => $sound,
  1418. 'type' => $type,
  1419. 'random' => $random,
  1420. 'random_answers' => $random_answers,
  1421. 'active' => $active,
  1422. 'feedback_type' => $feedback_type,
  1423. 'start_time' => $start_time,
  1424. 'end_time' => $end_time,
  1425. 'max_attempt' => $attempts,
  1426. 'expired_time' => $expired_time,
  1427. 'propagate_neg' => $propagate_neg,
  1428. 'save_correct_answers' => $saveCorrectAnswers,
  1429. 'review_answers' => $review_answers,
  1430. 'random_by_category' => $randomByCat,
  1431. 'text_when_finished' => $text_when_finished,
  1432. 'display_category_name' => $display_category_name,
  1433. 'pass_percentage' => $pass_percentage,
  1434. 'results_disabled' => $results_disabled,
  1435. 'question_selection_type' => $this->getQuestionSelectionType(),
  1436. 'hide_question_title' => $this->getHideQuestionTitle(),
  1437. ];
  1438. $allow = api_get_configuration_value('allow_quiz_show_previous_button_setting');
  1439. if ($allow === true) {
  1440. $paramsExtra['show_previous_button'] = $this->showPreviousButton();
  1441. }
  1442. $allow = api_get_configuration_value('allow_notification_setting_per_exercise');
  1443. if ($allow === true) {
  1444. $notifications = $this->getNotifications();
  1445. $notifications = implode(',', $notifications);
  1446. $paramsExtra['notifications'] = $notifications;
  1447. }
  1448. }
  1449. $params = array_merge($params, $paramsExtra);
  1450. Database::update(
  1451. $TBL_EXERCISES,
  1452. $params,
  1453. ['c_id = ? AND id = ?' => [$this->course_id, $id]]
  1454. );
  1455. // update into the item_property table
  1456. api_item_property_update(
  1457. $_course,
  1458. TOOL_QUIZ,
  1459. $id,
  1460. 'QuizUpdated',
  1461. api_get_user_id()
  1462. );
  1463. if (api_get_setting('search_enabled') == 'true') {
  1464. $this->search_engine_edit();
  1465. }
  1466. } else {
  1467. // Creates a new exercise
  1468. // In this case of new exercise, we don't do the api_get_utc_datetime()
  1469. // for date because, bellow, we call function api_set_default_visibility()
  1470. // In this function, api_set_default_visibility,
  1471. // the Quiz is saved too, with an $id and api_get_utc_datetime() is done.
  1472. // If we do it now, it will be done twice (cf. https://support.chamilo.org/issues/6586)
  1473. $start_time = null;
  1474. if (!empty($this->start_time)) {
  1475. $start_time = $this->start_time;
  1476. }
  1477. $end_time = null;
  1478. if (!empty($this->end_time)) {
  1479. $end_time = $this->end_time;
  1480. }
  1481. $params = [
  1482. 'c_id' => $this->course_id,
  1483. 'start_time' => $start_time,
  1484. 'end_time' => $end_time,
  1485. 'title' => $exercise,
  1486. 'description' => $description,
  1487. 'sound' => $sound,
  1488. 'type' => $type,
  1489. 'random' => $random,
  1490. 'random_answers' => $random_answers,
  1491. 'active' => $active,
  1492. 'results_disabled' => $results_disabled,
  1493. 'max_attempt' => $attempts,
  1494. 'feedback_type' => $feedback_type,
  1495. 'expired_time' => $expired_time,
  1496. 'session_id' => $session_id,
  1497. 'review_answers' => $review_answers,
  1498. 'random_by_category' => $randomByCat,
  1499. 'text_when_finished' => $text_when_finished,
  1500. 'display_category_name' => $display_category_name,
  1501. 'pass_percentage' => $pass_percentage,
  1502. 'save_correct_answers' => (int) $saveCorrectAnswers,
  1503. 'propagate_neg' => $propagate_neg,
  1504. 'hide_question_title' => $this->getHideQuestionTitle(),
  1505. ];
  1506. $allow = api_get_configuration_value('allow_quiz_show_previous_button_setting');
  1507. if ($allow === true) {
  1508. $params['show_previous_button'] = $this->showPreviousButton();
  1509. }
  1510. $allow = api_get_configuration_value('allow_notification_setting_per_exercise');
  1511. if ($allow === true) {
  1512. $notifications = $this->getNotifications();
  1513. $params['notifications'] = '';
  1514. if (!empty($notifications)) {
  1515. $notifications = implode(',', $notifications);
  1516. $params['notifications'] = $notifications;
  1517. }
  1518. }
  1519. $this->id = $this->iId = Database::insert($TBL_EXERCISES, $params);
  1520. if ($this->id) {
  1521. $sql = "UPDATE $TBL_EXERCISES SET id = iid WHERE iid = {$this->id} ";
  1522. Database::query($sql);
  1523. $sql = "UPDATE $TBL_EXERCISES
  1524. SET question_selection_type= ".intval($this->getQuestionSelectionType())."
  1525. WHERE id = ".$this->id." AND c_id = ".$this->course_id;
  1526. Database::query($sql);
  1527. // insert into the item_property table
  1528. api_item_property_update(
  1529. $this->course,
  1530. TOOL_QUIZ,
  1531. $this->id,
  1532. 'QuizAdded',
  1533. api_get_user_id()
  1534. );
  1535. // This function save the quiz again, carefull about start_time
  1536. // and end_time if you remove this line (see above)
  1537. api_set_default_visibility(
  1538. $this->id,
  1539. TOOL_QUIZ,
  1540. null,
  1541. $this->course
  1542. );
  1543. if (api_get_setting('search_enabled') == 'true' && extension_loaded('xapian')) {
  1544. $this->search_engine_save();
  1545. }
  1546. }
  1547. }
  1548. $this->save_categories_in_exercise($this->categories);
  1549. // Updates the question position
  1550. $this->update_question_positions();
  1551. return $this->iId;
  1552. }
  1553. /**
  1554. * Updates question position.
  1555. *
  1556. * @return bool
  1557. */
  1558. public function update_question_positions()
  1559. {
  1560. $table = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  1561. // Fixes #3483 when updating order
  1562. $questionList = $this->selectQuestionList(true);
  1563. $this->id = (int) $this->id;
  1564. if (empty($this->id)) {
  1565. return false;
  1566. }
  1567. if (!empty($questionList)) {
  1568. foreach ($questionList as $position => $questionId) {
  1569. $position = (int) $position;
  1570. $questionId = (int) $questionId;
  1571. $sql = "UPDATE $table SET
  1572. question_order ='".$position."'
  1573. WHERE
  1574. c_id = ".$this->course_id." AND
  1575. question_id = ".$questionId." AND
  1576. exercice_id=".$this->id;
  1577. Database::query($sql);
  1578. }
  1579. }
  1580. return true;
  1581. }
  1582. /**
  1583. * Adds a question into the question list.
  1584. *
  1585. * @author Olivier Brouckaert
  1586. *
  1587. * @param int $questionId - question ID
  1588. *
  1589. * @return bool - true if the question has been added, otherwise false
  1590. */
  1591. public function addToList($questionId)
  1592. {
  1593. // checks if the question ID is not in the list
  1594. if (!$this->isInList($questionId)) {
  1595. // selects the max position
  1596. if (!$this->selectNbrQuestions()) {
  1597. $pos = 1;
  1598. } else {
  1599. if (is_array($this->questionList)) {
  1600. $pos = max(array_keys($this->questionList)) + 1;
  1601. }
  1602. }
  1603. $this->questionList[$pos] = $questionId;
  1604. return true;
  1605. }
  1606. return false;
  1607. }
  1608. /**
  1609. * removes a question from the question list.
  1610. *
  1611. * @author Olivier Brouckaert
  1612. *
  1613. * @param int $questionId - question ID
  1614. *
  1615. * @return bool - true if the question has been removed, otherwise false
  1616. */
  1617. public function removeFromList($questionId)
  1618. {
  1619. // searches the position of the question ID in the list
  1620. $pos = array_search($questionId, $this->questionList);
  1621. // question not found
  1622. if ($pos === false) {
  1623. return false;
  1624. } else {
  1625. // dont reduce the number of random question if we use random by category option, or if
  1626. // random all questions
  1627. if ($this->isRandom() && $this->isRandomByCat() == 0) {
  1628. if (count($this->questionList) >= $this->random && $this->random > 0) {
  1629. $this->random--;
  1630. $this->save();
  1631. }
  1632. }
  1633. // deletes the position from the array containing the wanted question ID
  1634. unset($this->questionList[$pos]);
  1635. return true;
  1636. }
  1637. }
  1638. /**
  1639. * deletes the exercise from the database
  1640. * Notice : leaves the question in the data base.
  1641. *
  1642. * @author Olivier Brouckaert
  1643. */
  1644. public function delete()
  1645. {
  1646. $table = Database::get_course_table(TABLE_QUIZ_TEST);
  1647. $sql = "UPDATE $table SET active='-1'
  1648. WHERE c_id = ".$this->course_id." AND id = ".intval($this->id);
  1649. Database::query($sql);
  1650. api_item_property_update(
  1651. $this->course,
  1652. TOOL_QUIZ,
  1653. $this->id,
  1654. 'QuizDeleted',
  1655. api_get_user_id()
  1656. );
  1657. api_item_property_update(
  1658. $this->course,
  1659. TOOL_QUIZ,
  1660. $this->id,
  1661. 'delete',
  1662. api_get_user_id()
  1663. );
  1664. Skill::deleteSkillsFromItem($this->iId, ITEM_TYPE_EXERCISE);
  1665. if (api_get_setting('search_enabled') == 'true' &&
  1666. extension_loaded('xapian')
  1667. ) {
  1668. $this->search_engine_delete();
  1669. }
  1670. }
  1671. /**
  1672. * Creates the form to create / edit an exercise.
  1673. *
  1674. * @param FormValidator $form
  1675. * @param string $type
  1676. */
  1677. public function createForm($form, $type = 'full')
  1678. {
  1679. if (empty($type)) {
  1680. $type = 'full';
  1681. }
  1682. // form title
  1683. if (!empty($_GET['exerciseId'])) {
  1684. $form_title = get_lang('ModifyExercise');
  1685. } else {
  1686. $form_title = get_lang('NewEx');
  1687. }
  1688. $form->addElement('header', $form_title);
  1689. // Title.
  1690. if (api_get_configuration_value('save_titles_as_html')) {
  1691. $form->addHtmlEditor(
  1692. 'exerciseTitle',
  1693. get_lang('ExerciseName'),
  1694. false,
  1695. false,
  1696. ['ToolbarSet' => 'Minimal']
  1697. );
  1698. } else {
  1699. $form->addElement(
  1700. 'text',
  1701. 'exerciseTitle',
  1702. get_lang('ExerciseName'),
  1703. ['id' => 'exercise_title']
  1704. );
  1705. }
  1706. $form->addElement('advanced_settings', 'advanced_params', get_lang('AdvancedParameters'));
  1707. $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
  1708. $editor_config = [
  1709. 'ToolbarSet' => 'TestQuestionDescription',
  1710. 'Width' => '100%',
  1711. 'Height' => '150',
  1712. ];
  1713. if (is_array($type)) {
  1714. $editor_config = array_merge($editor_config, $type);
  1715. }
  1716. $form->addHtmlEditor(
  1717. 'exerciseDescription',
  1718. get_lang('ExerciseDescription'),
  1719. false,
  1720. false,
  1721. $editor_config
  1722. );
  1723. $skillList = [];
  1724. if ($type == 'full') {
  1725. //Can't modify a DirectFeedback question
  1726. if ($this->selectFeedbackType() != EXERCISE_FEEDBACK_TYPE_DIRECT) {
  1727. // feedback type
  1728. $radios_feedback = [];
  1729. $radios_feedback[] = $form->createElement(
  1730. 'radio',
  1731. 'exerciseFeedbackType',
  1732. null,
  1733. get_lang('ExerciseAtTheEndOfTheTest'),
  1734. '0',
  1735. [
  1736. 'id' => 'exerciseType_0',
  1737. 'onclick' => 'check_feedback()',
  1738. ]
  1739. );
  1740. if (api_get_setting('enable_quiz_scenario') == 'true') {
  1741. // Can't convert a question from one feedback to another
  1742. // if there is more than 1 question already added
  1743. if ($this->selectNbrQuestions() == 0) {
  1744. $radios_feedback[] = $form->createElement(
  1745. 'radio',
  1746. 'exerciseFeedbackType',
  1747. null,
  1748. get_lang('DirectFeedback'),
  1749. '1',
  1750. [
  1751. 'id' => 'exerciseType_1',
  1752. 'onclick' => 'check_direct_feedback()',
  1753. ]
  1754. );
  1755. }
  1756. }
  1757. $radios_feedback[] = $form->createElement(
  1758. 'radio',
  1759. 'exerciseFeedbackType',
  1760. null,
  1761. get_lang('NoFeedback'),
  1762. '2',
  1763. ['id' => 'exerciseType_2']
  1764. );
  1765. $form->addGroup(
  1766. $radios_feedback,
  1767. null,
  1768. [
  1769. get_lang('FeedbackType'),
  1770. get_lang('FeedbackDisplayOptions'),
  1771. ]
  1772. );
  1773. // Type of results display on the final page
  1774. $this->setResultDisabledGroup($form);
  1775. // Type of questions disposition on page
  1776. $radios = [];
  1777. $radios[] = $form->createElement(
  1778. 'radio',
  1779. 'exerciseType',
  1780. null,
  1781. get_lang('SimpleExercise'),
  1782. '1',
  1783. [
  1784. 'onclick' => 'check_per_page_all()',
  1785. 'id' => 'option_page_all',
  1786. ]
  1787. );
  1788. $radios[] = $form->createElement(
  1789. 'radio',
  1790. 'exerciseType',
  1791. null,
  1792. get_lang('SequentialExercise'),
  1793. '2',
  1794. [
  1795. 'onclick' => 'check_per_page_one()',
  1796. 'id' => 'option_page_one',
  1797. ]
  1798. );
  1799. $form->addGroup($radios, null, get_lang('QuestionsPerPage'));
  1800. } else {
  1801. // if is Direct feedback but has not questions we can allow to modify the question type
  1802. if ($this->getQuestionCount() === 0) {
  1803. // feedback type
  1804. $radios_feedback = [];
  1805. $radios_feedback[] = $form->createElement(
  1806. 'radio',
  1807. 'exerciseFeedbackType',
  1808. null,
  1809. get_lang('ExerciseAtTheEndOfTheTest'),
  1810. '0',
  1811. ['id' => 'exerciseType_0', 'onclick' => 'check_feedback()']
  1812. );
  1813. if (api_get_setting('enable_quiz_scenario') == 'true') {
  1814. $radios_feedback[] = $form->createElement(
  1815. 'radio',
  1816. 'exerciseFeedbackType',
  1817. null,
  1818. get_lang('DirectFeedback'),
  1819. '1',
  1820. ['id' => 'exerciseType_1', 'onclick' => 'check_direct_feedback()']
  1821. );
  1822. }
  1823. $radios_feedback[] = $form->createElement(
  1824. 'radio',
  1825. 'exerciseFeedbackType',
  1826. null,
  1827. get_lang('NoFeedback'),
  1828. '2',
  1829. ['id' => 'exerciseType_2']
  1830. );
  1831. $form->addGroup(
  1832. $radios_feedback,
  1833. null,
  1834. [get_lang('FeedbackType'), get_lang('FeedbackDisplayOptions')]
  1835. );
  1836. $this->setResultDisabledGroup($form);
  1837. // Type of questions disposition on page
  1838. $radios = [];
  1839. $radios[] = $form->createElement('radio', 'exerciseType', null, get_lang('SimpleExercise'), '1');
  1840. $radios[] = $form->createElement(
  1841. 'radio',
  1842. 'exerciseType',
  1843. null,
  1844. get_lang('SequentialExercise'),
  1845. '2'
  1846. );
  1847. $form->addGroup($radios, null, get_lang('ExerciseType'));
  1848. } else {
  1849. $group = $this->setResultDisabledGroup($form);
  1850. $group->freeze();
  1851. // we force the options to the DirectFeedback exercisetype
  1852. $form->addElement('hidden', 'exerciseFeedbackType', EXERCISE_FEEDBACK_TYPE_DIRECT);
  1853. $form->addElement('hidden', 'exerciseType', ONE_PER_PAGE);
  1854. // Type of questions disposition on page
  1855. $radios[] = $form->createElement(
  1856. 'radio',
  1857. 'exerciseType',
  1858. null,
  1859. get_lang('SimpleExercise'),
  1860. '1',
  1861. [
  1862. 'onclick' => 'check_per_page_all()',
  1863. 'id' => 'option_page_all',
  1864. ]
  1865. );
  1866. $radios[] = $form->createElement(
  1867. 'radio',
  1868. 'exerciseType',
  1869. null,
  1870. get_lang('SequentialExercise'),
  1871. '2',
  1872. [
  1873. 'onclick' => 'check_per_page_one()',
  1874. 'id' => 'option_page_one',
  1875. ]
  1876. );
  1877. $type_group = $form->addGroup($radios, null, get_lang('QuestionsPerPage'));
  1878. $type_group->freeze();
  1879. }
  1880. }
  1881. $option = [
  1882. EX_Q_SELECTION_ORDERED => get_lang('OrderedByUser'),
  1883. // Defined by user
  1884. EX_Q_SELECTION_RANDOM => get_lang('Random'),
  1885. // 1-10, All
  1886. 'per_categories' => '--------'.get_lang('UsingCategories').'----------',
  1887. // Base (A 123 {3} B 456 {3} C 789{2} D 0{0}) --> Matrix {3, 3, 2, 0}
  1888. EX_Q_SELECTION_CATEGORIES_ORDERED_QUESTIONS_ORDERED => get_lang('OrderedCategoriesAlphabeticallyWithQuestionsOrdered'),
  1889. // A 123 B 456 C 78 (0, 1, all)
  1890. EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_ORDERED => get_lang('RandomCategoriesWithQuestionsOrdered'),
  1891. // C 78 B 456 A 123
  1892. EX_Q_SELECTION_CATEGORIES_ORDERED_QUESTIONS_RANDOM => get_lang('OrderedCategoriesAlphabeticallyWithRandomQuestions'),
  1893. // A 321 B 654 C 87
  1894. EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_RANDOM => get_lang('RandomCategoriesWithRandomQuestions'),
  1895. // C 87 B 654 A 321
  1896. //EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_ORDERED_NO_GROUPED => get_lang('RandomCategoriesWithQuestionsOrderedNoQuestionGrouped'),
  1897. /* B 456 C 78 A 123
  1898. 456 78 123
  1899. 123 456 78
  1900. */
  1901. //EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_RANDOM_NO_GROUPED => get_lang('RandomCategoriesWithRandomQuestionsNoQuestionGrouped'),
  1902. /*
  1903. A 123 B 456 C 78
  1904. B 456 C 78 A 123
  1905. B 654 C 87 A 321
  1906. 654 87 321
  1907. 165 842 73
  1908. */
  1909. //EX_Q_SELECTION_CATEGORIES_ORDERED_BY_PARENT_QUESTIONS_ORDERED => get_lang('OrderedCategoriesByParentWithQuestionsOrdered'),
  1910. //EX_Q_SELECTION_CATEGORIES_ORDERED_BY_PARENT_QUESTIONS_RANDOM => get_lang('OrderedCategoriesByParentWithQuestionsRandom'),
  1911. ];
  1912. $form->addElement(
  1913. 'select',
  1914. 'question_selection_type',
  1915. [get_lang('QuestionSelection')],
  1916. $option,
  1917. [
  1918. 'id' => 'questionSelection',
  1919. 'onchange' => 'checkQuestionSelection()',
  1920. ]
  1921. );
  1922. $displayMatrix = 'none';
  1923. $displayRandom = 'none';
  1924. $selectionType = $this->getQuestionSelectionType();
  1925. switch ($selectionType) {
  1926. case EX_Q_SELECTION_RANDOM:
  1927. $displayRandom = 'block';
  1928. break;
  1929. case $selectionType >= EX_Q_SELECTION_CATEGORIES_ORDERED_QUESTIONS_ORDERED:
  1930. $displayMatrix = 'block';
  1931. break;
  1932. }
  1933. $form->addElement(
  1934. 'html',
  1935. '<div id="hidden_random" style="display:'.$displayRandom.'">'
  1936. );
  1937. // Number of random question.
  1938. $max = ($this->id > 0) ? $this->getQuestionCount() : 10;
  1939. $option = range(0, $max);
  1940. $option[0] = get_lang('No');
  1941. $option[-1] = get_lang('AllQuestionsShort');
  1942. $form->addElement(
  1943. 'select',
  1944. 'randomQuestions',
  1945. [
  1946. get_lang('RandomQuestions'),
  1947. get_lang('RandomQuestionsHelp'),
  1948. ],
  1949. $option,
  1950. ['id' => 'randomQuestions']
  1951. );
  1952. $form->addElement('html', '</div>');
  1953. $form->addElement(
  1954. 'html',
  1955. '<div id="hidden_matrix" style="display:'.$displayMatrix.'">'
  1956. );
  1957. // Category selection.
  1958. $cat = new TestCategory();
  1959. $cat_form = $cat->returnCategoryForm($this);
  1960. if (empty($cat_form)) {
  1961. $cat_form = '<span class="label label-warning">'.get_lang('NoCategoriesDefined').'</span>';
  1962. }
  1963. $form->addElement('label', null, $cat_form);
  1964. $form->addElement('html', '</div>');
  1965. // Category name.
  1966. $radio_display_cat_name = [
  1967. $form->createElement('radio', 'display_category_name', null, get_lang('Yes'), '1'),
  1968. $form->createElement('radio', 'display_category_name', null, get_lang('No'), '0'),
  1969. ];
  1970. $form->addGroup($radio_display_cat_name, null, get_lang('QuestionDisplayCategoryName'));
  1971. // Random answers.
  1972. $radios_random_answers = [
  1973. $form->createElement('radio', 'randomAnswers', null, get_lang('Yes'), '1'),
  1974. $form->createElement('radio', 'randomAnswers', null, get_lang('No'), '0'),
  1975. ];
  1976. $form->addGroup($radios_random_answers, null, get_lang('RandomAnswers'));
  1977. // Hide question title.
  1978. $group = [
  1979. $form->createElement('radio', 'hide_question_title', null, get_lang('Yes'), '1'),
  1980. $form->createElement('radio', 'hide_question_title', null, get_lang('No'), '0'),
  1981. ];
  1982. $form->addGroup($group, null, get_lang('HideQuestionTitle'));
  1983. $allow = api_get_configuration_value('allow_quiz_show_previous_button_setting');
  1984. if ($allow === true) {
  1985. // Hide question title.
  1986. $group = [
  1987. $form->createElement(
  1988. 'radio',
  1989. 'show_previous_button',
  1990. null,
  1991. get_lang('Yes'),
  1992. '1'
  1993. ),
  1994. $form->createElement(
  1995. 'radio',
  1996. 'show_previous_button',
  1997. null,
  1998. get_lang('No'),
  1999. '0'
  2000. ),
  2001. ];
  2002. $form->addGroup($group, null, get_lang('ShowPreviousButton'));
  2003. }
  2004. // Attempts
  2005. $attempt_option = range(0, 10);
  2006. $attempt_option[0] = get_lang('Infinite');
  2007. $form->addElement(
  2008. 'select',
  2009. 'exerciseAttempts',
  2010. get_lang('ExerciseAttempts'),
  2011. $attempt_option,
  2012. ['id' => 'exerciseAttempts']
  2013. );
  2014. // Exercise time limit
  2015. $form->addElement(
  2016. 'checkbox',
  2017. 'activate_start_date_check',
  2018. null,
  2019. get_lang('EnableStartTime'),
  2020. ['onclick' => 'activate_start_date()']
  2021. );
  2022. $var = self::selectTimeLimit();
  2023. if (!empty($this->start_time)) {
  2024. $form->addElement('html', '<div id="start_date_div" style="display:block;">');
  2025. } else {
  2026. $form->addElement('html', '<div id="start_date_div" style="display:none;">');
  2027. }
  2028. $form->addElement('date_time_picker', 'start_time');
  2029. $form->addElement('html', '</div>');
  2030. $form->addElement(
  2031. 'checkbox',
  2032. 'activate_end_date_check',
  2033. null,
  2034. get_lang('EnableEndTime'),
  2035. ['onclick' => 'activate_end_date()']
  2036. );
  2037. if (!empty($this->end_time)) {
  2038. $form->addElement('html', '<div id="end_date_div" style="display:block;">');
  2039. } else {
  2040. $form->addElement('html', '<div id="end_date_div" style="display:none;">');
  2041. }
  2042. $form->addElement('date_time_picker', 'end_time');
  2043. $form->addElement('html', '</div>');
  2044. $display = 'block';
  2045. $form->addElement(
  2046. 'checkbox',
  2047. 'propagate_neg',
  2048. null,
  2049. get_lang('PropagateNegativeResults')
  2050. );
  2051. $form->addCheckBox(
  2052. 'save_correct_answers',
  2053. null,
  2054. get_lang('SaveTheCorrectAnswersForTheNextAttempt')
  2055. );
  2056. $form->addElement('html', '<div class="clear">&nbsp;</div>');
  2057. $form->addElement('checkbox', 'review_answers', null, get_lang('ReviewAnswers'));
  2058. $form->addElement('html', '<div id="divtimecontrol" style="display:'.$display.';">');
  2059. // Timer control
  2060. $form->addElement(
  2061. 'checkbox',
  2062. 'enabletimercontrol',
  2063. null,
  2064. get_lang('EnableTimerControl'),
  2065. [
  2066. 'onclick' => 'option_time_expired()',
  2067. 'id' => 'enabletimercontrol',
  2068. 'onload' => 'check_load_time()',
  2069. ]
  2070. );
  2071. $expired_date = (int) $this->selectExpiredTime();
  2072. if (($expired_date != '0')) {
  2073. $form->addElement('html', '<div id="timercontrol" style="display:block;">');
  2074. } else {
  2075. $form->addElement('html', '<div id="timercontrol" style="display:none;">');
  2076. }
  2077. $form->addText(
  2078. 'enabletimercontroltotalminutes',
  2079. get_lang('ExerciseTotalDurationInMinutes'),
  2080. false,
  2081. [
  2082. 'id' => 'enabletimercontroltotalminutes',
  2083. 'cols-size' => [2, 2, 8],
  2084. ]
  2085. );
  2086. $form->addElement('html', '</div>');
  2087. $form->addElement(
  2088. 'text',
  2089. 'pass_percentage',
  2090. [get_lang('PassPercentage'), null, '%'],
  2091. ['id' => 'pass_percentage']
  2092. );
  2093. $form->addRule('pass_percentage', get_lang('Numeric'), 'numeric');
  2094. $form->addRule('pass_percentage', get_lang('ValueTooSmall'), 'min_numeric_length', 0);
  2095. $form->addRule('pass_percentage', get_lang('ValueTooBig'), 'max_numeric_length', 100);
  2096. // add the text_when_finished textbox
  2097. $form->addHtmlEditor(
  2098. 'text_when_finished',
  2099. get_lang('TextWhenFinished'),
  2100. false,
  2101. false,
  2102. $editor_config
  2103. );
  2104. $allow = api_get_configuration_value('allow_notification_setting_per_exercise');
  2105. if ($allow === true) {
  2106. $settings = ExerciseLib::getNotificationSettings();
  2107. $group = [];
  2108. foreach ($settings as $itemId => $label) {
  2109. $group[] = $form->createElement(
  2110. 'checkbox',
  2111. 'notifications[]',
  2112. null,
  2113. $label,
  2114. ['value' => $itemId]
  2115. );
  2116. }
  2117. $form->addGroup($group, '', [get_lang('EmailNotifications')]);
  2118. }
  2119. $form->addCheckBox(
  2120. 'update_title_in_lps',
  2121. null,
  2122. get_lang('UpdateTitleInLps')
  2123. );
  2124. $defaults = [];
  2125. if (api_get_setting('search_enabled') === 'true') {
  2126. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  2127. $form->addElement('checkbox', 'index_document', '', get_lang('SearchFeatureDoIndexDocument'));
  2128. $form->addSelectLanguage('language', get_lang('SearchFeatureDocumentLanguage'));
  2129. $specific_fields = get_specific_field_list();
  2130. foreach ($specific_fields as $specific_field) {
  2131. $form->addElement('text', $specific_field['code'], $specific_field['name']);
  2132. $filter = [
  2133. 'c_id' => api_get_course_int_id(),
  2134. 'field_id' => $specific_field['id'],
  2135. 'ref_id' => $this->id,
  2136. 'tool_id' => "'".TOOL_QUIZ."'",
  2137. ];
  2138. $values = get_specific_field_values_list($filter, ['value']);
  2139. if (!empty($values)) {
  2140. $arr_str_values = [];
  2141. foreach ($values as $value) {
  2142. $arr_str_values[] = $value['value'];
  2143. }
  2144. $defaults[$specific_field['code']] = implode(', ', $arr_str_values);
  2145. }
  2146. }
  2147. }
  2148. $skillList = Skill::addSkillsToForm($form, ITEM_TYPE_EXERCISE, $this->iId);
  2149. $form->addElement('html', '</div>'); //End advanced setting
  2150. $form->addElement('html', '</div>');
  2151. }
  2152. // submit
  2153. if (isset($_GET['exerciseId'])) {
  2154. $form->addButtonSave(get_lang('ModifyExercise'), 'submitExercise');
  2155. } else {
  2156. $form->addButtonUpdate(get_lang('ProcedToQuestions'), 'submitExercise');
  2157. }
  2158. $form->addRule('exerciseTitle', get_lang('GiveExerciseName'), 'required');
  2159. if ($type == 'full') {
  2160. // rules
  2161. $form->addRule('exerciseAttempts', get_lang('Numeric'), 'numeric');
  2162. $form->addRule('start_time', get_lang('InvalidDate'), 'datetime');
  2163. $form->addRule('end_time', get_lang('InvalidDate'), 'datetime');
  2164. }
  2165. // defaults
  2166. if ($type == 'full') {
  2167. if ($this->id > 0) {
  2168. //if ($this->random > $this->selectNbrQuestions()) {
  2169. // $defaults['randomQuestions'] = $this->selectNbrQuestions();
  2170. //} else {
  2171. $defaults['randomQuestions'] = $this->random;
  2172. //}
  2173. $defaults['randomAnswers'] = $this->getRandomAnswers();
  2174. $defaults['exerciseType'] = $this->selectType();
  2175. $defaults['exerciseTitle'] = $this->get_formated_title();
  2176. $defaults['exerciseDescription'] = $this->selectDescription();
  2177. $defaults['exerciseAttempts'] = $this->selectAttempts();
  2178. $defaults['exerciseFeedbackType'] = $this->selectFeedbackType();
  2179. $defaults['results_disabled'] = $this->selectResultsDisabled();
  2180. $defaults['propagate_neg'] = $this->selectPropagateNeg();
  2181. $defaults['save_correct_answers'] = $this->selectSaveCorrectAnswers();
  2182. $defaults['review_answers'] = $this->review_answers;
  2183. $defaults['randomByCat'] = $this->getRandomByCategory();
  2184. $defaults['text_when_finished'] = $this->selectTextWhenFinished();
  2185. $defaults['display_category_name'] = $this->selectDisplayCategoryName();
  2186. $defaults['pass_percentage'] = $this->selectPassPercentage();
  2187. $defaults['question_selection_type'] = $this->getQuestionSelectionType();
  2188. $defaults['hide_question_title'] = $this->getHideQuestionTitle();
  2189. $defaults['show_previous_button'] = $this->showPreviousButton();
  2190. if (!empty($this->start_time)) {
  2191. $defaults['activate_start_date_check'] = 1;
  2192. }
  2193. if (!empty($this->end_time)) {
  2194. $defaults['activate_end_date_check'] = 1;
  2195. }
  2196. $defaults['start_time'] = !empty($this->start_time) ? api_get_local_time($this->start_time) : date('Y-m-d 12:00:00');
  2197. $defaults['end_time'] = !empty($this->end_time) ? api_get_local_time($this->end_time) : date('Y-m-d 12:00:00', time() + 84600);
  2198. // Get expired time
  2199. if ($this->expired_time != '0') {
  2200. $defaults['enabletimercontrol'] = 1;
  2201. $defaults['enabletimercontroltotalminutes'] = $this->expired_time;
  2202. } else {
  2203. $defaults['enabletimercontroltotalminutes'] = 0;
  2204. }
  2205. $defaults['skills'] = array_keys($skillList);
  2206. $defaults['notifications'] = $this->getNotifications();
  2207. } else {
  2208. $defaults['exerciseType'] = 2;
  2209. $defaults['exerciseAttempts'] = 0;
  2210. $defaults['randomQuestions'] = 0;
  2211. $defaults['randomAnswers'] = 0;
  2212. $defaults['exerciseDescription'] = '';
  2213. $defaults['exerciseFeedbackType'] = 0;
  2214. $defaults['results_disabled'] = 0;
  2215. $defaults['randomByCat'] = 0;
  2216. $defaults['text_when_finished'] = '';
  2217. $defaults['start_time'] = date('Y-m-d 12:00:00');
  2218. $defaults['display_category_name'] = 1;
  2219. $defaults['end_time'] = date('Y-m-d 12:00:00', time() + 84600);
  2220. $defaults['pass_percentage'] = '';
  2221. $defaults['end_button'] = $this->selectEndButton();
  2222. $defaults['question_selection_type'] = 1;
  2223. $defaults['hide_question_title'] = 0;
  2224. $defaults['show_previous_button'] = 1;
  2225. $defaults['on_success_message'] = null;
  2226. $defaults['on_failed_message'] = null;
  2227. }
  2228. } else {
  2229. $defaults['exerciseTitle'] = $this->selectTitle();
  2230. $defaults['exerciseDescription'] = $this->selectDescription();
  2231. }
  2232. if (api_get_setting('search_enabled') === 'true') {
  2233. $defaults['index_document'] = 'checked="checked"';
  2234. }
  2235. $form->setDefaults($defaults);
  2236. // Freeze some elements.
  2237. if ($this->id != 0 && $this->edit_exercise_in_lp == false) {
  2238. $elementsToFreeze = [
  2239. 'randomQuestions',
  2240. //'randomByCat',
  2241. 'exerciseAttempts',
  2242. 'propagate_neg',
  2243. 'enabletimercontrol',
  2244. 'review_answers',
  2245. ];
  2246. foreach ($elementsToFreeze as $elementName) {
  2247. /** @var HTML_QuickForm_element $element */
  2248. $element = $form->getElement($elementName);
  2249. $element->freeze();
  2250. }
  2251. }
  2252. }
  2253. /**
  2254. * function which process the creation of exercises.
  2255. *
  2256. * @param FormValidator $form
  2257. * @param string
  2258. *
  2259. * @return int c_quiz.iid
  2260. */
  2261. public function processCreation($form, $type = '')
  2262. {
  2263. $this->updateTitle(self::format_title_variable($form->getSubmitValue('exerciseTitle')));
  2264. $this->updateDescription($form->getSubmitValue('exerciseDescription'));
  2265. $this->updateAttempts($form->getSubmitValue('exerciseAttempts'));
  2266. $this->updateFeedbackType($form->getSubmitValue('exerciseFeedbackType'));
  2267. $this->updateType($form->getSubmitValue('exerciseType'));
  2268. $this->setRandom($form->getSubmitValue('randomQuestions'));
  2269. $this->updateRandomAnswers($form->getSubmitValue('randomAnswers'));
  2270. $this->updateResultsDisabled($form->getSubmitValue('results_disabled'));
  2271. $this->updateExpiredTime($form->getSubmitValue('enabletimercontroltotalminutes'));
  2272. $this->updatePropagateNegative($form->getSubmitValue('propagate_neg'));
  2273. $this->updateSaveCorrectAnswers($form->getSubmitValue('save_correct_answers'));
  2274. $this->updateRandomByCat($form->getSubmitValue('randomByCat'));
  2275. $this->updateTextWhenFinished($form->getSubmitValue('text_when_finished'));
  2276. $this->updateDisplayCategoryName($form->getSubmitValue('display_category_name'));
  2277. $this->updateReviewAnswers($form->getSubmitValue('review_answers'));
  2278. $this->updatePassPercentage($form->getSubmitValue('pass_percentage'));
  2279. $this->updateCategories($form->getSubmitValue('category'));
  2280. $this->updateEndButton($form->getSubmitValue('end_button'));
  2281. $this->setOnSuccessMessage($form->getSubmitValue('on_success_message'));
  2282. $this->setOnFailedMessage($form->getSubmitValue('on_failed_message'));
  2283. $this->updateEmailNotificationTemplate($form->getSubmitValue('email_notification_template'));
  2284. $this->setEmailNotificationTemplateToUser($form->getSubmitValue('email_notification_template_to_user'));
  2285. $this->setNotifyUserByEmail($form->getSubmitValue('notify_user_by_email'));
  2286. $this->setModelType($form->getSubmitValue('model_type'));
  2287. $this->setQuestionSelectionType($form->getSubmitValue('question_selection_type'));
  2288. $this->setHideQuestionTitle($form->getSubmitValue('hide_question_title'));
  2289. $this->sessionId = api_get_session_id();
  2290. $this->setQuestionSelectionType($form->getSubmitValue('question_selection_type'));
  2291. $this->setScoreTypeModel($form->getSubmitValue('score_type_model'));
  2292. $this->setGlobalCategoryId($form->getSubmitValue('global_category_id'));
  2293. $this->setShowPreviousButton($form->getSubmitValue('show_previous_button'));
  2294. $this->setNotifications($form->getSubmitValue('notifications'));
  2295. if ($form->getSubmitValue('activate_start_date_check') == 1) {
  2296. $start_time = $form->getSubmitValue('start_time');
  2297. $this->start_time = api_get_utc_datetime($start_time);
  2298. } else {
  2299. $this->start_time = null;
  2300. }
  2301. if ($form->getSubmitValue('activate_end_date_check') == 1) {
  2302. $end_time = $form->getSubmitValue('end_time');
  2303. $this->end_time = api_get_utc_datetime($end_time);
  2304. } else {
  2305. $this->end_time = null;
  2306. }
  2307. if ($form->getSubmitValue('enabletimercontrol') == 1) {
  2308. $expired_total_time = $form->getSubmitValue('enabletimercontroltotalminutes');
  2309. if ($this->expired_time == 0) {
  2310. $this->expired_time = $expired_total_time;
  2311. }
  2312. } else {
  2313. $this->expired_time = 0;
  2314. }
  2315. if ($form->getSubmitValue('randomAnswers') == 1) {
  2316. $this->random_answers = 1;
  2317. } else {
  2318. $this->random_answers = 0;
  2319. }
  2320. // Update title in all LPs that have this quiz added
  2321. if ($form->getSubmitValue('update_title_in_lps') == 1) {
  2322. $courseId = api_get_course_int_id();
  2323. $table = Database::get_course_table(TABLE_LP_ITEM);
  2324. $sql = "SELECT * FROM $table
  2325. WHERE
  2326. c_id = $courseId AND
  2327. item_type = 'quiz' AND
  2328. path = '".$this->id."'
  2329. ";
  2330. $result = Database::query($sql);
  2331. $items = Database::store_result($result);
  2332. if (!empty($items)) {
  2333. foreach ($items as $item) {
  2334. $itemId = $item['iid'];
  2335. $sql = "UPDATE $table SET title = '".$this->title."'
  2336. WHERE iid = $itemId AND c_id = $courseId ";
  2337. Database::query($sql);
  2338. }
  2339. }
  2340. }
  2341. $iId = $this->save($type);
  2342. if (!empty($iId)) {
  2343. Skill::saveSkills($form, ITEM_TYPE_EXERCISE, $iId);
  2344. }
  2345. }
  2346. public function search_engine_save()
  2347. {
  2348. if ($_POST['index_document'] != 1) {
  2349. return;
  2350. }
  2351. $course_id = api_get_course_id();
  2352. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  2353. $specific_fields = get_specific_field_list();
  2354. $ic_slide = new IndexableChunk();
  2355. $all_specific_terms = '';
  2356. foreach ($specific_fields as $specific_field) {
  2357. if (isset($_REQUEST[$specific_field['code']])) {
  2358. $sterms = trim($_REQUEST[$specific_field['code']]);
  2359. if (!empty($sterms)) {
  2360. $all_specific_terms .= ' '.$sterms;
  2361. $sterms = explode(',', $sterms);
  2362. foreach ($sterms as $sterm) {
  2363. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  2364. add_specific_field_value($specific_field['id'], $course_id, TOOL_QUIZ, $this->id, $sterm);
  2365. }
  2366. }
  2367. }
  2368. }
  2369. // build the chunk to index
  2370. $ic_slide->addValue("title", $this->exercise);
  2371. $ic_slide->addCourseId($course_id);
  2372. $ic_slide->addToolId(TOOL_QUIZ);
  2373. $xapian_data = [
  2374. SE_COURSE_ID => $course_id,
  2375. SE_TOOL_ID => TOOL_QUIZ,
  2376. SE_DATA => ['type' => SE_DOCTYPE_EXERCISE_EXERCISE, 'exercise_id' => (int) $this->id],
  2377. SE_USER => (int) api_get_user_id(),
  2378. ];
  2379. $ic_slide->xapian_data = serialize($xapian_data);
  2380. $exercise_description = $all_specific_terms.' '.$this->description;
  2381. $ic_slide->addValue("content", $exercise_description);
  2382. $di = new ChamiloIndexer();
  2383. isset($_POST['language']) ? $lang = Database::escape_string($_POST['language']) : $lang = 'english';
  2384. $di->connectDb(null, null, $lang);
  2385. $di->addChunk($ic_slide);
  2386. //index and return search engine document id
  2387. $did = $di->index();
  2388. if ($did) {
  2389. // save it to db
  2390. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  2391. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, search_did)
  2392. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  2393. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id, $did);
  2394. Database::query($sql);
  2395. }
  2396. }
  2397. public function search_engine_edit()
  2398. {
  2399. // update search enchine and its values table if enabled
  2400. if (api_get_setting('search_enabled') == 'true' && extension_loaded('xapian')) {
  2401. $course_id = api_get_course_id();
  2402. // actually, it consists on delete terms from db,
  2403. // insert new ones, create a new search engine document, and remove the old one
  2404. // get search_did
  2405. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  2406. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  2407. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
  2408. $res = Database::query($sql);
  2409. if (Database::num_rows($res) > 0) {
  2410. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  2411. $se_ref = Database::fetch_array($res);
  2412. $specific_fields = get_specific_field_list();
  2413. $ic_slide = new IndexableChunk();
  2414. $all_specific_terms = '';
  2415. foreach ($specific_fields as $specific_field) {
  2416. delete_all_specific_field_value($course_id, $specific_field['id'], TOOL_QUIZ, $this->id);
  2417. if (isset($_REQUEST[$specific_field['code']])) {
  2418. $sterms = trim($_REQUEST[$specific_field['code']]);
  2419. $all_specific_terms .= ' '.$sterms;
  2420. $sterms = explode(',', $sterms);
  2421. foreach ($sterms as $sterm) {
  2422. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  2423. add_specific_field_value($specific_field['id'], $course_id, TOOL_QUIZ, $this->id, $sterm);
  2424. }
  2425. }
  2426. }
  2427. // build the chunk to index
  2428. $ic_slide->addValue('title', $this->exercise);
  2429. $ic_slide->addCourseId($course_id);
  2430. $ic_slide->addToolId(TOOL_QUIZ);
  2431. $xapian_data = [
  2432. SE_COURSE_ID => $course_id,
  2433. SE_TOOL_ID => TOOL_QUIZ,
  2434. SE_DATA => ['type' => SE_DOCTYPE_EXERCISE_EXERCISE, 'exercise_id' => (int) $this->id],
  2435. SE_USER => (int) api_get_user_id(),
  2436. ];
  2437. $ic_slide->xapian_data = serialize($xapian_data);
  2438. $exercise_description = $all_specific_terms.' '.$this->description;
  2439. $ic_slide->addValue("content", $exercise_description);
  2440. $di = new ChamiloIndexer();
  2441. isset($_POST['language']) ? $lang = Database::escape_string($_POST['language']) : $lang = 'english';
  2442. $di->connectDb(null, null, $lang);
  2443. $di->remove_document($se_ref['search_did']);
  2444. $di->addChunk($ic_slide);
  2445. //index and return search engine document id
  2446. $did = $di->index();
  2447. if ($did) {
  2448. // save it to db
  2449. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=\'%s\'';
  2450. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
  2451. Database::query($sql);
  2452. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, search_did)
  2453. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  2454. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id, $did);
  2455. Database::query($sql);
  2456. }
  2457. } else {
  2458. $this->search_engine_save();
  2459. }
  2460. }
  2461. }
  2462. public function search_engine_delete()
  2463. {
  2464. // remove from search engine if enabled
  2465. if (api_get_setting('search_enabled') == 'true' && extension_loaded('xapian')) {
  2466. $course_id = api_get_course_id();
  2467. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  2468. $sql = 'SELECT * FROM %s
  2469. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level IS NULL
  2470. LIMIT 1';
  2471. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
  2472. $res = Database::query($sql);
  2473. if (Database::num_rows($res) > 0) {
  2474. $row = Database::fetch_array($res);
  2475. $di = new ChamiloIndexer();
  2476. $di->remove_document($row['search_did']);
  2477. unset($di);
  2478. $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
  2479. foreach ($this->questionList as $question_i) {
  2480. $sql = 'SELECT type FROM %s WHERE id=%s';
  2481. $sql = sprintf($sql, $tbl_quiz_question, $question_i);
  2482. $qres = Database::query($sql);
  2483. if (Database::num_rows($qres) > 0) {
  2484. $qrow = Database::fetch_array($qres);
  2485. $objQuestion = Question::getInstance($qrow['type']);
  2486. $objQuestion = Question::read((int) $question_i);
  2487. $objQuestion->search_engine_edit($this->id, false, true);
  2488. unset($objQuestion);
  2489. }
  2490. }
  2491. }
  2492. $sql = 'DELETE FROM %s
  2493. WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level IS NULL
  2494. LIMIT 1';
  2495. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_QUIZ, $this->id);
  2496. Database::query($sql);
  2497. // remove terms from db
  2498. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  2499. delete_all_values_for_item($course_id, TOOL_QUIZ, $this->id);
  2500. }
  2501. }
  2502. public function selectExpiredTime()
  2503. {
  2504. return $this->expired_time;
  2505. }
  2506. /**
  2507. * Cleans the student's results only for the Exercise tool (Not from the LP)
  2508. * The LP results are NOT deleted by default, otherwise put $cleanLpTests = true
  2509. * Works with exercises in sessions.
  2510. *
  2511. * @param bool $cleanLpTests
  2512. * @param string $cleanResultBeforeDate
  2513. *
  2514. * @return int quantity of user's exercises deleted
  2515. */
  2516. public function cleanResults($cleanLpTests = false, $cleanResultBeforeDate = null)
  2517. {
  2518. $sessionId = api_get_session_id();
  2519. $table_track_e_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  2520. $table_track_e_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  2521. $sql_where = ' AND
  2522. orig_lp_id = 0 AND
  2523. orig_lp_item_id = 0';
  2524. // if we want to delete results from LP too
  2525. if ($cleanLpTests) {
  2526. $sql_where = '';
  2527. }
  2528. // if we want to delete attempts before date $cleanResultBeforeDate
  2529. // $cleanResultBeforeDate must be a valid UTC-0 date yyyy-mm-dd
  2530. if (!empty($cleanResultBeforeDate)) {
  2531. $cleanResultBeforeDate = Database::escape_string($cleanResultBeforeDate);
  2532. if (api_is_valid_date($cleanResultBeforeDate)) {
  2533. $sql_where .= " AND exe_date <= '$cleanResultBeforeDate' ";
  2534. } else {
  2535. return 0;
  2536. }
  2537. }
  2538. $sql = "SELECT exe_id
  2539. FROM $table_track_e_exercises
  2540. WHERE
  2541. c_id = ".api_get_course_int_id()." AND
  2542. exe_exo_id = ".$this->id." AND
  2543. session_id = ".$sessionId." ".
  2544. $sql_where;
  2545. $result = Database::query($sql);
  2546. $exe_list = Database::store_result($result);
  2547. // deleting TRACK_E_ATTEMPT table
  2548. // check if exe in learning path or not
  2549. $i = 0;
  2550. if (is_array($exe_list) && count($exe_list) > 0) {
  2551. foreach ($exe_list as $item) {
  2552. $sql = "DELETE FROM $table_track_e_attempt
  2553. WHERE exe_id = '".$item['exe_id']."'";
  2554. Database::query($sql);
  2555. $i++;
  2556. }
  2557. }
  2558. // delete TRACK_E_EXERCISES table
  2559. $sql = "DELETE FROM $table_track_e_exercises
  2560. WHERE
  2561. c_id = ".api_get_course_int_id()." AND
  2562. exe_exo_id = ".$this->id." $sql_where AND
  2563. session_id = ".$sessionId;
  2564. Database::query($sql);
  2565. $this->generateStats($this->id, api_get_course_info(), $sessionId);
  2566. Event::addEvent(
  2567. LOG_EXERCISE_RESULT_DELETE,
  2568. LOG_EXERCISE_ID,
  2569. $this->id,
  2570. null,
  2571. null,
  2572. api_get_course_int_id(),
  2573. $sessionId
  2574. );
  2575. return $i;
  2576. }
  2577. /**
  2578. * Copies an exercise (duplicate all questions and answers).
  2579. */
  2580. public function copyExercise()
  2581. {
  2582. $exerciseObject = $this;
  2583. $categories = $exerciseObject->getCategoriesInExercise();
  2584. // Get all questions no matter the order/category settings
  2585. $questionList = $exerciseObject->getQuestionOrderedList();
  2586. // Force the creation of a new exercise
  2587. $exerciseObject->updateTitle($exerciseObject->selectTitle().' - '.get_lang('Copy'));
  2588. // Hides the new exercise
  2589. $exerciseObject->updateStatus(false);
  2590. $exerciseObject->updateId(0);
  2591. $exerciseObject->save();
  2592. $newId = $exerciseObject->selectId();
  2593. if ($newId && !empty($questionList)) {
  2594. // Question creation
  2595. foreach ($questionList as $oldQuestionId) {
  2596. $oldQuestionObj = Question::read($oldQuestionId);
  2597. $newQuestionId = $oldQuestionObj->duplicate();
  2598. if ($newQuestionId) {
  2599. $newQuestionObj = Question::read($newQuestionId);
  2600. if (isset($newQuestionObj) && $newQuestionObj) {
  2601. $newQuestionObj->addToList($newId);
  2602. if (!empty($oldQuestionObj->category)) {
  2603. $newQuestionObj->saveCategory($oldQuestionObj->category);
  2604. }
  2605. // This should be moved to the duplicate function
  2606. $newAnswerObj = new Answer($oldQuestionId);
  2607. $newAnswerObj->read();
  2608. $newAnswerObj->duplicate($newQuestionObj);
  2609. }
  2610. }
  2611. }
  2612. if (!empty($categories)) {
  2613. $newCategoryList = [];
  2614. foreach ($categories as $category) {
  2615. $newCategoryList[$category['category_id']] = $category['count_questions'];
  2616. }
  2617. $exerciseObject->save_categories_in_exercise($newCategoryList);
  2618. }
  2619. }
  2620. }
  2621. /**
  2622. * Changes the exercise status.
  2623. *
  2624. * @param string $status - exercise status
  2625. */
  2626. public function updateStatus($status)
  2627. {
  2628. $this->active = $status;
  2629. }
  2630. /**
  2631. * @param int $lp_id
  2632. * @param int $lp_item_id
  2633. * @param int $lp_item_view_id
  2634. * @param string $status
  2635. *
  2636. * @return array
  2637. */
  2638. public function get_stat_track_exercise_info(
  2639. $lp_id = 0,
  2640. $lp_item_id = 0,
  2641. $lp_item_view_id = 0,
  2642. $status = 'incomplete'
  2643. ) {
  2644. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  2645. if (empty($lp_id)) {
  2646. $lp_id = 0;
  2647. }
  2648. if (empty($lp_item_id)) {
  2649. $lp_item_id = 0;
  2650. }
  2651. if (empty($lp_item_view_id)) {
  2652. $lp_item_view_id = 0;
  2653. }
  2654. $condition = ' WHERE exe_exo_id = '."'".$this->id."'".' AND
  2655. exe_user_id = '."'".api_get_user_id()."'".' AND
  2656. c_id = '.api_get_course_int_id().' AND
  2657. status = '."'".Database::escape_string($status)."'".' AND
  2658. orig_lp_id = '."'".$lp_id."'".' AND
  2659. orig_lp_item_id = '."'".$lp_item_id."'".' AND
  2660. orig_lp_item_view_id = '."'".$lp_item_view_id."'".' AND
  2661. session_id = '."'".api_get_session_id()."' LIMIT 1"; //Adding limit 1 just in case
  2662. $sql_track = 'SELECT * FROM '.$track_exercises.$condition;
  2663. $result = Database::query($sql_track);
  2664. $new_array = [];
  2665. if (Database::num_rows($result) > 0) {
  2666. $new_array = Database::fetch_array($result, 'ASSOC');
  2667. $new_array['num_exe'] = Database::num_rows($result);
  2668. }
  2669. return $new_array;
  2670. }
  2671. /**
  2672. * Saves a test attempt.
  2673. *
  2674. * @param int $clock_expired_time clock_expired_time
  2675. * @param int int lp id
  2676. * @param int int lp item id
  2677. * @param int int lp item_view id
  2678. * @param array $questionList
  2679. * @param float $weight
  2680. *
  2681. * @return int
  2682. */
  2683. public function save_stat_track_exercise_info(
  2684. $clock_expired_time = 0,
  2685. $safe_lp_id = 0,
  2686. $safe_lp_item_id = 0,
  2687. $safe_lp_item_view_id = 0,
  2688. $questionList = [],
  2689. $weight = 0
  2690. ) {
  2691. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  2692. $safe_lp_id = intval($safe_lp_id);
  2693. $safe_lp_item_id = intval($safe_lp_item_id);
  2694. $safe_lp_item_view_id = intval($safe_lp_item_view_id);
  2695. if (empty($safe_lp_id)) {
  2696. $safe_lp_id = 0;
  2697. }
  2698. if (empty($safe_lp_item_id)) {
  2699. $safe_lp_item_id = 0;
  2700. }
  2701. if (empty($clock_expired_time)) {
  2702. $clock_expired_time = null;
  2703. }
  2704. $questionList = array_map('intval', $questionList);
  2705. $params = [
  2706. 'exe_exo_id' => $this->id,
  2707. 'exe_user_id' => api_get_user_id(),
  2708. 'c_id' => api_get_course_int_id(),
  2709. 'status' => 'incomplete',
  2710. 'session_id' => api_get_session_id(),
  2711. 'data_tracking' => implode(',', $questionList),
  2712. 'start_date' => api_get_utc_datetime(),
  2713. 'orig_lp_id' => $safe_lp_id,
  2714. 'orig_lp_item_id' => $safe_lp_item_id,
  2715. 'orig_lp_item_view_id' => $safe_lp_item_view_id,
  2716. 'exe_weighting' => $weight,
  2717. 'user_ip' => Database::escape_string(api_get_real_ip()),
  2718. 'exe_date' => api_get_utc_datetime(),
  2719. 'exe_result' => 0,
  2720. 'steps_counter' => 0,
  2721. 'exe_duration' => 0,
  2722. 'expired_time_control' => $clock_expired_time,
  2723. 'questions_to_check' => '',
  2724. ];
  2725. $id = Database::insert($track_exercises, $params);
  2726. return $id;
  2727. }
  2728. /**
  2729. * @param int $question_id
  2730. * @param int $questionNum
  2731. * @param array $questions_in_media
  2732. * @param string $currentAnswer
  2733. * @param array $myRemindList
  2734. *
  2735. * @return string
  2736. */
  2737. public function show_button(
  2738. $question_id,
  2739. $questionNum,
  2740. $questions_in_media = [],
  2741. $currentAnswer = '',
  2742. $myRemindList = []
  2743. ) {
  2744. global $origin, $safe_lp_id, $safe_lp_item_id, $safe_lp_item_view_id;
  2745. $nbrQuestions = $this->getQuestionCount();
  2746. $buttonList = [];
  2747. $html = $label = '';
  2748. $hotspot_get = isset($_POST['hotspot']) ? Security::remove_XSS($_POST['hotspot']) : null;
  2749. if ($this->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT && $this->type == ONE_PER_PAGE) {
  2750. $urlTitle = get_lang('ContinueTest');
  2751. if ($questionNum == count($this->questionList)) {
  2752. $urlTitle = get_lang('EndTest');
  2753. }
  2754. $html .= Display::url(
  2755. $urlTitle,
  2756. 'exercise_submit_modal.php?'.http_build_query([
  2757. 'learnpath_id' => $safe_lp_id,
  2758. 'learnpath_item_id' => $safe_lp_item_id,
  2759. 'learnpath_item_view_id' => $safe_lp_item_view_id,
  2760. 'origin' => $origin,
  2761. 'hotspot' => $hotspot_get,
  2762. 'nbrQuestions' => $nbrQuestions,
  2763. 'num' => $questionNum,
  2764. 'exerciseType' => $this->type,
  2765. 'exerciseId' => $this->id,
  2766. 'reminder' => empty($myRemindList) ? null : 2,
  2767. ]),
  2768. [
  2769. 'class' => 'ajax btn btn-default',
  2770. 'data-title' => $urlTitle,
  2771. 'data-size' => 'md',
  2772. ]
  2773. );
  2774. $html .= '<br />';
  2775. } else {
  2776. // User
  2777. if (api_is_allowed_to_session_edit()) {
  2778. $endReminderValue = false;
  2779. if (!empty($myRemindList)) {
  2780. $endValue = end($myRemindList);
  2781. if ($endValue == $question_id) {
  2782. $endReminderValue = true;
  2783. }
  2784. }
  2785. if ($this->type == ALL_ON_ONE_PAGE || $nbrQuestions == $questionNum || $endReminderValue) {
  2786. if ($this->review_answers) {
  2787. $label = get_lang('ReviewQuestions');
  2788. $class = 'btn btn-success';
  2789. } else {
  2790. $label = get_lang('EndTest');
  2791. $class = 'btn btn-warning';
  2792. }
  2793. } else {
  2794. $label = get_lang('NextQuestion');
  2795. $class = 'btn btn-primary';
  2796. }
  2797. // used to select it with jquery
  2798. $class .= ' question-validate-btn';
  2799. if ($this->type == ONE_PER_PAGE) {
  2800. if ($questionNum != 1) {
  2801. if ($this->showPreviousButton()) {
  2802. $prev_question = $questionNum - 2;
  2803. $showPreview = true;
  2804. if (!empty($myRemindList)) {
  2805. $beforeId = null;
  2806. for ($i = 0; $i < count($myRemindList); $i++) {
  2807. if (isset($myRemindList[$i]) && $myRemindList[$i] == $question_id) {
  2808. $beforeId = isset($myRemindList[$i - 1]) ? $myRemindList[$i - 1] : null;
  2809. break;
  2810. }
  2811. }
  2812. if (empty($beforeId)) {
  2813. $showPreview = false;
  2814. } else {
  2815. $num = 0;
  2816. foreach ($this->questionList as $originalQuestionId) {
  2817. if ($originalQuestionId == $beforeId) {
  2818. break;
  2819. }
  2820. $num++;
  2821. }
  2822. $prev_question = $num;
  2823. //$question_id = $beforeId;
  2824. }
  2825. }
  2826. if ($showPreview) {
  2827. $buttonList[] = Display::button(
  2828. 'previous_question_and_save',
  2829. get_lang('PreviousQuestion'),
  2830. [
  2831. 'type' => 'button',
  2832. 'class' => 'btn btn-default',
  2833. 'data-prev' => $prev_question,
  2834. 'data-question' => $question_id,
  2835. ]
  2836. );
  2837. }
  2838. }
  2839. }
  2840. // Next question
  2841. if (!empty($questions_in_media)) {
  2842. $buttonList[] = Display::button(
  2843. 'save_question_list',
  2844. $label,
  2845. [
  2846. 'type' => 'button',
  2847. 'class' => $class,
  2848. 'data-list' => implode(",", $questions_in_media),
  2849. ]
  2850. );
  2851. } else {
  2852. $buttonList[] = Display::button(
  2853. 'save_now',
  2854. $label,
  2855. ['type' => 'button', 'class' => $class, 'data-question' => $question_id]
  2856. );
  2857. }
  2858. $buttonList[] = '<span id="save_for_now_'.$question_id.'" class="exercise_save_mini_message"></span>&nbsp;';
  2859. $html .= implode(PHP_EOL, $buttonList);
  2860. } else {
  2861. if ($this->review_answers) {
  2862. $all_label = get_lang('ReviewQuestions');
  2863. $class = 'btn btn-success';
  2864. } else {
  2865. $all_label = get_lang('EndTest');
  2866. $class = 'btn btn-warning';
  2867. }
  2868. // used to select it with jquery
  2869. $class .= ' question-validate-btn';
  2870. $buttonList[] = Display::button(
  2871. 'validate_all',
  2872. $all_label,
  2873. ['type' => 'button', 'class' => $class]
  2874. );
  2875. $buttonList[] = '&nbsp;'.Display::span(null, ['id' => 'save_all_response']);
  2876. $html .= implode(PHP_EOL, $buttonList);
  2877. }
  2878. }
  2879. }
  2880. return $html;
  2881. }
  2882. /**
  2883. * So the time control will work.
  2884. *
  2885. * @param string $time_left
  2886. *
  2887. * @return string
  2888. */
  2889. public function showTimeControlJS($time_left)
  2890. {
  2891. $time_left = (int) $time_left;
  2892. $script = "redirectExerciseToResult();";
  2893. if ($this->type == ALL_ON_ONE_PAGE) {
  2894. $script = "save_now_all('validate');";
  2895. }
  2896. return "<script>
  2897. function openClockWarning() {
  2898. $('#clock_warning').dialog({
  2899. modal:true,
  2900. height:250,
  2901. closeOnEscape: false,
  2902. resizable: false,
  2903. buttons: {
  2904. '".addslashes(get_lang("EndTest"))."': function() {
  2905. $('#clock_warning').dialog('close');
  2906. }
  2907. },
  2908. close: function() {
  2909. send_form();
  2910. }
  2911. });
  2912. $('#clock_warning').dialog('open');
  2913. $('#counter_to_redirect').epiclock({
  2914. mode: $.epiclock.modes.countdown,
  2915. offset: {seconds: 5},
  2916. format: 's'
  2917. }).bind('timer', function () {
  2918. send_form();
  2919. });
  2920. }
  2921. function send_form() {
  2922. if ($('#exercise_form').length) {
  2923. $script
  2924. } else {
  2925. // In exercise_reminder.php
  2926. final_submit();
  2927. }
  2928. }
  2929. function onExpiredTimeExercise() {
  2930. $('#wrapper-clock').hide();
  2931. $('#expired-message-id').show();
  2932. // Fixes bug #5263
  2933. $('#num_current_id').attr('value', '".$this->selectNbrQuestions()."');
  2934. openClockWarning();
  2935. }
  2936. $(function() {
  2937. // time in seconds when using minutes there are some seconds lost
  2938. var time_left = parseInt(".$time_left.");
  2939. $('#exercise_clock_warning').epiclock({
  2940. mode: $.epiclock.modes.countdown,
  2941. offset: {seconds: time_left},
  2942. format: 'x:i:s',
  2943. renderer: 'minute'
  2944. }).bind('timer', function () {
  2945. onExpiredTimeExercise();
  2946. });
  2947. $('#submit_save').click(function () {});
  2948. });
  2949. </script>";
  2950. }
  2951. /**
  2952. * This function was originally found in the exercise_show.php.
  2953. *
  2954. * @param int $exeId
  2955. * @param int $questionId
  2956. * @param mixed $choice the user-selected option
  2957. * @param string $from function is called from 'exercise_show' or 'exercise_result'
  2958. * @param array $exerciseResultCoordinates the hotspot coordinates $hotspot[$question_id] = coordinates
  2959. * @param bool $saved_results save results in the DB or just show the reponse
  2960. * @param bool $from_database gets information from DB or from the current selection
  2961. * @param bool $show_result show results or not
  2962. * @param int $propagate_neg
  2963. * @param array $hotspot_delineation_result
  2964. * @param bool $showTotalScoreAndUserChoicesInLastAttempt
  2965. * @param bool $updateResults
  2966. *
  2967. * @todo reduce parameters of this function
  2968. *
  2969. * @return string html code
  2970. */
  2971. public function manage_answer(
  2972. $exeId,
  2973. $questionId,
  2974. $choice,
  2975. $from = 'exercise_show',
  2976. $exerciseResultCoordinates = [],
  2977. $saved_results = true,
  2978. $from_database = false,
  2979. $show_result = true,
  2980. $propagate_neg = 0,
  2981. $hotspot_delineation_result = [],
  2982. $showTotalScoreAndUserChoicesInLastAttempt = true,
  2983. $updateResults = false
  2984. ) {
  2985. $debug = false;
  2986. //needed in order to use in the exercise_attempt() for the time
  2987. global $learnpath_id, $learnpath_item_id;
  2988. require_once api_get_path(LIBRARY_PATH).'geometry.lib.php';
  2989. $em = Database::getManager();
  2990. $feedback_type = $this->selectFeedbackType();
  2991. $results_disabled = $this->selectResultsDisabled();
  2992. if ($debug) {
  2993. error_log("<------ manage_answer ------> ");
  2994. error_log('exe_id: '.$exeId);
  2995. error_log('$from: '.$from);
  2996. error_log('$saved_results: '.intval($saved_results));
  2997. error_log('$from_database: '.intval($from_database));
  2998. error_log('$show_result: '.intval($show_result));
  2999. error_log('$propagate_neg: '.$propagate_neg);
  3000. error_log('$exerciseResultCoordinates: '.print_r($exerciseResultCoordinates, 1));
  3001. error_log('$hotspot_delineation_result: '.print_r($hotspot_delineation_result, 1));
  3002. error_log('$learnpath_id: '.$learnpath_id);
  3003. error_log('$learnpath_item_id: '.$learnpath_item_id);
  3004. error_log('$choice: '.print_r($choice, 1));
  3005. }
  3006. $final_overlap = 0;
  3007. $final_missing = 0;
  3008. $final_excess = 0;
  3009. $overlap_color = 0;
  3010. $missing_color = 0;
  3011. $excess_color = 0;
  3012. $threadhold1 = 0;
  3013. $threadhold2 = 0;
  3014. $threadhold3 = 0;
  3015. $arrques = null;
  3016. $arrans = null;
  3017. $studentChoice = null;
  3018. $expectedAnswer = '';
  3019. $calculatedChoice = '';
  3020. $calculatedStatus = '';
  3021. $questionId = (int) $questionId;
  3022. $exeId = (int) $exeId;
  3023. $TBL_TRACK_ATTEMPT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  3024. $table_ans = Database::get_course_table(TABLE_QUIZ_ANSWER);
  3025. // Creates a temporary Question object
  3026. $course_id = $this->course_id;
  3027. $objQuestionTmp = Question::read($questionId, $this->course);
  3028. if ($objQuestionTmp === false) {
  3029. return false;
  3030. }
  3031. $questionName = $objQuestionTmp->selectTitle();
  3032. $questionWeighting = $objQuestionTmp->selectWeighting();
  3033. $answerType = $objQuestionTmp->selectType();
  3034. $quesId = $objQuestionTmp->selectId();
  3035. $extra = $objQuestionTmp->extra;
  3036. $next = 1; //not for now
  3037. $totalWeighting = 0;
  3038. $totalScore = 0;
  3039. // Extra information of the question
  3040. if ((
  3041. $answerType == MULTIPLE_ANSWER_TRUE_FALSE ||
  3042. $answerType == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY
  3043. )
  3044. && !empty($extra)
  3045. ) {
  3046. $extra = explode(':', $extra);
  3047. if ($debug) {
  3048. error_log(print_r($extra, 1));
  3049. }
  3050. // Fixes problems with negatives values using intval
  3051. $true_score = floatval(trim($extra[0]));
  3052. $false_score = floatval(trim($extra[1]));
  3053. $doubt_score = floatval(trim($extra[2]));
  3054. }
  3055. // Construction of the Answer object
  3056. $objAnswerTmp = new Answer($questionId, $course_id);
  3057. $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
  3058. if ($debug) {
  3059. error_log('Count of answers: '.$nbrAnswers);
  3060. error_log('$answerType: '.$answerType);
  3061. }
  3062. if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  3063. $choiceTmp = $choice;
  3064. $choice = isset($choiceTmp['choice']) ? $choiceTmp['choice'] : '';
  3065. $choiceDegreeCertainty = isset($choiceTmp['choiceDegreeCertainty']) ? $choiceTmp['choiceDegreeCertainty'] : '';
  3066. }
  3067. if ($answerType == FREE_ANSWER ||
  3068. $answerType == ORAL_EXPRESSION ||
  3069. $answerType == CALCULATED_ANSWER ||
  3070. $answerType == ANNOTATION
  3071. ) {
  3072. $nbrAnswers = 1;
  3073. }
  3074. $generatedFile = '';
  3075. if ($answerType == ORAL_EXPRESSION) {
  3076. $exe_info = Event::get_exercise_results_by_attempt($exeId);
  3077. $exe_info = isset($exe_info[$exeId]) ? $exe_info[$exeId] : null;
  3078. $objQuestionTmp->initFile(
  3079. api_get_session_id(),
  3080. isset($exe_info['exe_user_id']) ? $exe_info['exe_user_id'] : api_get_user_id(),
  3081. isset($exe_info['exe_exo_id']) ? $exe_info['exe_exo_id'] : $this->id,
  3082. isset($exe_info['exe_id']) ? $exe_info['exe_id'] : $exeId
  3083. );
  3084. // Probably this attempt came in an exercise all question by page
  3085. if ($feedback_type == 0) {
  3086. $objQuestionTmp->replaceWithRealExe($exeId);
  3087. }
  3088. $generatedFile = $objQuestionTmp->getFileUrl();
  3089. }
  3090. $user_answer = '';
  3091. // Get answer list for matching
  3092. $sql = "SELECT id_auto, id, answer
  3093. FROM $table_ans
  3094. WHERE c_id = $course_id AND question_id = $questionId";
  3095. $res_answer = Database::query($sql);
  3096. $answerMatching = [];
  3097. while ($real_answer = Database::fetch_array($res_answer)) {
  3098. $answerMatching[$real_answer['id_auto']] = $real_answer['answer'];
  3099. }
  3100. $real_answers = [];
  3101. $quiz_question_options = Question::readQuestionOption(
  3102. $questionId,
  3103. $course_id
  3104. );
  3105. $organs_at_risk_hit = 0;
  3106. $questionScore = 0;
  3107. $answer_correct_array = [];
  3108. $orderedHotspots = [];
  3109. if ($answerType == HOT_SPOT || $answerType == ANNOTATION) {
  3110. $orderedHotspots = $em->getRepository('ChamiloCoreBundle:TrackEHotspot')->findBy(
  3111. [
  3112. 'hotspotQuestionId' => $questionId,
  3113. 'cId' => $course_id,
  3114. 'hotspotExeId' => $exeId,
  3115. ],
  3116. ['hotspotAnswerId' => 'ASC']
  3117. );
  3118. }
  3119. if ($debug) {
  3120. error_log('Start answer loop ');
  3121. }
  3122. $userAnsweredQuestion = false;
  3123. for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
  3124. $answer = $objAnswerTmp->selectAnswer($answerId);
  3125. $answerComment = $objAnswerTmp->selectComment($answerId);
  3126. $answerCorrect = $objAnswerTmp->isCorrect($answerId);
  3127. $answerWeighting = (float) $objAnswerTmp->selectWeighting($answerId);
  3128. $answerAutoId = $objAnswerTmp->selectAutoId($answerId);
  3129. $answerIid = isset($objAnswerTmp->iid[$answerId]) ? $objAnswerTmp->iid[$answerId] : '';
  3130. $answer_correct_array[$answerId] = (bool) $answerCorrect;
  3131. if ($debug) {
  3132. error_log("answer auto id: $answerAutoId ");
  3133. error_log("answer correct: $answerCorrect ");
  3134. }
  3135. // Delineation
  3136. $delineation_cord = $objAnswerTmp->selectHotspotCoordinates(1);
  3137. $answer_delineation_destination = $objAnswerTmp->selectDestination(1);
  3138. switch ($answerType) {
  3139. case UNIQUE_ANSWER:
  3140. case UNIQUE_ANSWER_IMAGE:
  3141. case UNIQUE_ANSWER_NO_OPTION:
  3142. case READING_COMPREHENSION:
  3143. if ($from_database) {
  3144. $sql = "SELECT answer FROM $TBL_TRACK_ATTEMPT
  3145. WHERE
  3146. exe_id = '".$exeId."' AND
  3147. question_id= '".$questionId."'";
  3148. $result = Database::query($sql);
  3149. $choice = Database::result($result, 0, 'answer');
  3150. if ($userAnsweredQuestion === false) {
  3151. $userAnsweredQuestion = !empty($choice);
  3152. }
  3153. $studentChoice = $choice == $answerAutoId ? 1 : 0;
  3154. if ($studentChoice) {
  3155. $questionScore += $answerWeighting;
  3156. $totalScore += $answerWeighting;
  3157. }
  3158. } else {
  3159. $studentChoice = $choice == $answerAutoId ? 1 : 0;
  3160. if ($studentChoice) {
  3161. $questionScore += $answerWeighting;
  3162. $totalScore += $answerWeighting;
  3163. }
  3164. }
  3165. break;
  3166. case MULTIPLE_ANSWER_TRUE_FALSE:
  3167. if ($from_database) {
  3168. $choice = [];
  3169. $sql = "SELECT answer FROM $TBL_TRACK_ATTEMPT
  3170. WHERE
  3171. exe_id = $exeId AND
  3172. question_id = ".$questionId;
  3173. $result = Database::query($sql);
  3174. while ($row = Database::fetch_array($result)) {
  3175. $values = explode(':', $row['answer']);
  3176. $my_answer_id = isset($values[0]) ? $values[0] : '';
  3177. $option = isset($values[1]) ? $values[1] : '';
  3178. $choice[$my_answer_id] = $option;
  3179. }
  3180. }
  3181. $studentChoice = isset($choice[$answerAutoId]) ? $choice[$answerAutoId] : null;
  3182. if (!empty($studentChoice)) {
  3183. if ($studentChoice == $answerCorrect) {
  3184. $questionScore += $true_score;
  3185. } else {
  3186. if ($quiz_question_options[$studentChoice]['name'] == "Don't know" ||
  3187. $quiz_question_options[$studentChoice]['name'] == "DoubtScore"
  3188. ) {
  3189. $questionScore += $doubt_score;
  3190. } else {
  3191. $questionScore += $false_score;
  3192. }
  3193. }
  3194. } else {
  3195. // If no result then the user just hit don't know
  3196. $studentChoice = 3;
  3197. $questionScore += $doubt_score;
  3198. }
  3199. $totalScore = $questionScore;
  3200. break;
  3201. case MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY:
  3202. if ($from_database) {
  3203. $choice = [];
  3204. $choiceDegreeCertainty = [];
  3205. $sql = "SELECT answer
  3206. FROM $TBL_TRACK_ATTEMPT
  3207. WHERE
  3208. exe_id = $exeId AND question_id = $questionId";
  3209. $result = Database::query($sql);
  3210. while ($row = Database::fetch_array($result)) {
  3211. $ind = $row['answer'];
  3212. $values = explode(':', $ind);
  3213. $myAnswerId = $values[0];
  3214. $option = $values[1];
  3215. $percent = $values[2];
  3216. $choice[$myAnswerId] = $option;
  3217. $choiceDegreeCertainty[$myAnswerId] = $percent;
  3218. }
  3219. }
  3220. $studentChoice = isset($choice[$answerAutoId]) ? $choice[$answerAutoId] : null;
  3221. $studentChoiceDegree = isset($choiceDegreeCertainty[$answerAutoId]) ?
  3222. $choiceDegreeCertainty[$answerAutoId] : null;
  3223. // student score update
  3224. if (!empty($studentChoice)) {
  3225. if ($studentChoice == $answerCorrect) {
  3226. // correct answer and student is Unsure or PrettySur
  3227. if (isset($quiz_question_options[$studentChoiceDegree]) &&
  3228. $quiz_question_options[$studentChoiceDegree]['position'] >= 3 &&
  3229. $quiz_question_options[$studentChoiceDegree]['position'] < 9
  3230. ) {
  3231. $questionScore += $true_score;
  3232. } else {
  3233. // student ignore correct answer
  3234. $questionScore += $doubt_score;
  3235. }
  3236. } else {
  3237. // false answer and student is Unsure or PrettySur
  3238. if ($quiz_question_options[$studentChoiceDegree]['position'] >= 3
  3239. && $quiz_question_options[$studentChoiceDegree]['position'] < 9) {
  3240. $questionScore += $false_score;
  3241. } else {
  3242. // student ignore correct answer
  3243. $questionScore += $doubt_score;
  3244. }
  3245. }
  3246. }
  3247. $totalScore = $questionScore;
  3248. break;
  3249. case MULTIPLE_ANSWER: //2
  3250. if ($from_database) {
  3251. $choice = [];
  3252. $sql = "SELECT answer FROM ".$TBL_TRACK_ATTEMPT."
  3253. WHERE exe_id = '".$exeId."' AND question_id= '".$questionId."'";
  3254. $resultans = Database::query($sql);
  3255. while ($row = Database::fetch_array($resultans)) {
  3256. $choice[$row['answer']] = 1;
  3257. }
  3258. $studentChoice = isset($choice[$answerAutoId]) ? $choice[$answerAutoId] : null;
  3259. $real_answers[$answerId] = (bool) $studentChoice;
  3260. if ($studentChoice) {
  3261. $questionScore += $answerWeighting;
  3262. }
  3263. } else {
  3264. $studentChoice = isset($choice[$answerAutoId]) ? $choice[$answerAutoId] : null;
  3265. $real_answers[$answerId] = (bool) $studentChoice;
  3266. if (isset($studentChoice)) {
  3267. $questionScore += $answerWeighting;
  3268. }
  3269. }
  3270. $totalScore += $answerWeighting;
  3271. if ($debug) {
  3272. error_log("studentChoice: $studentChoice");
  3273. }
  3274. break;
  3275. case GLOBAL_MULTIPLE_ANSWER:
  3276. if ($from_database) {
  3277. $choice = [];
  3278. $sql = "SELECT answer FROM $TBL_TRACK_ATTEMPT
  3279. WHERE exe_id = '".$exeId."' AND question_id= '".$questionId."'";
  3280. $resultans = Database::query($sql);
  3281. while ($row = Database::fetch_array($resultans)) {
  3282. $choice[$row['answer']] = 1;
  3283. }
  3284. $studentChoice = isset($choice[$answerAutoId]) ? $choice[$answerAutoId] : null;
  3285. $real_answers[$answerId] = (bool) $studentChoice;
  3286. if ($studentChoice) {
  3287. $questionScore += $answerWeighting;
  3288. }
  3289. } else {
  3290. $studentChoice = isset($choice[$answerAutoId]) ? $choice[$answerAutoId] : null;
  3291. if (isset($studentChoice)) {
  3292. $questionScore += $answerWeighting;
  3293. }
  3294. $real_answers[$answerId] = (bool) $studentChoice;
  3295. }
  3296. $totalScore += $answerWeighting;
  3297. if ($debug) {
  3298. error_log("studentChoice: $studentChoice");
  3299. }
  3300. break;
  3301. case MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE:
  3302. if ($from_database) {
  3303. $choice = [];
  3304. $sql = "SELECT answer FROM ".$TBL_TRACK_ATTEMPT."
  3305. WHERE exe_id = $exeId AND question_id= ".$questionId;
  3306. $resultans = Database::query($sql);
  3307. while ($row = Database::fetch_array($resultans)) {
  3308. $result = explode(':', $row['answer']);
  3309. if (isset($result[0])) {
  3310. $my_answer_id = isset($result[0]) ? $result[0] : '';
  3311. $option = isset($result[1]) ? $result[1] : '';
  3312. $choice[$my_answer_id] = $option;
  3313. }
  3314. }
  3315. $studentChoice = isset($choice[$answerAutoId]) ? $choice[$answerAutoId] : '';
  3316. $real_answers[$answerId] = false;
  3317. if ($answerCorrect == $studentChoice) {
  3318. $real_answers[$answerId] = true;
  3319. }
  3320. } else {
  3321. $studentChoice = isset($choice[$answerAutoId]) ? $choice[$answerAutoId] : '';
  3322. $real_answers[$answerId] = false;
  3323. if ($answerCorrect == $studentChoice) {
  3324. $real_answers[$answerId] = true;
  3325. }
  3326. }
  3327. break;
  3328. case MULTIPLE_ANSWER_COMBINATION:
  3329. if ($from_database) {
  3330. $choice = [];
  3331. $sql = "SELECT answer FROM $TBL_TRACK_ATTEMPT
  3332. WHERE exe_id = $exeId AND question_id= $questionId";
  3333. $resultans = Database::query($sql);
  3334. while ($row = Database::fetch_array($resultans)) {
  3335. $choice[$row['answer']] = 1;
  3336. }
  3337. $studentChoice = isset($choice[$answerAutoId]) ? $choice[$answerAutoId] : null;
  3338. if ($answerCorrect == 1) {
  3339. $real_answers[$answerId] = false;
  3340. if ($studentChoice) {
  3341. $real_answers[$answerId] = true;
  3342. }
  3343. } else {
  3344. $real_answers[$answerId] = true;
  3345. if ($studentChoice) {
  3346. $real_answers[$answerId] = false;
  3347. }
  3348. }
  3349. } else {
  3350. $studentChoice = isset($choice[$answerAutoId]) ? $choice[$answerAutoId] : null;
  3351. if ($answerCorrect == 1) {
  3352. $real_answers[$answerId] = false;
  3353. if ($studentChoice) {
  3354. $real_answers[$answerId] = true;
  3355. }
  3356. } else {
  3357. $real_answers[$answerId] = true;
  3358. if ($studentChoice) {
  3359. $real_answers[$answerId] = false;
  3360. }
  3361. }
  3362. }
  3363. break;
  3364. case FILL_IN_BLANKS:
  3365. $str = '';
  3366. $answerFromDatabase = '';
  3367. if ($from_database) {
  3368. $sql = "SELECT answer
  3369. FROM $TBL_TRACK_ATTEMPT
  3370. WHERE
  3371. exe_id = $exeId AND
  3372. question_id= ".intval($questionId);
  3373. $result = Database::query($sql);
  3374. if ($debug) {
  3375. error_log($sql);
  3376. }
  3377. $str = $answerFromDatabase = Database::result($result, 0, 'answer');
  3378. }
  3379. // if ($saved_results == false && strpos($answerFromDatabase, 'font color') !== false) {
  3380. if (false) {
  3381. // the question is encoded like this
  3382. // [A] B [C] D [E] F::10,10,10@1
  3383. // number 1 before the "@" means that is a switchable fill in blank question
  3384. // [A] B [C] D [E] F::10,10,10@ or [A] B [C] D [E] F::10,10,10
  3385. // means that is a normal fill blank question
  3386. // first we explode the "::"
  3387. $pre_array = explode('::', $answer);
  3388. // is switchable fill blank or not
  3389. $last = count($pre_array) - 1;
  3390. $is_set_switchable = explode('@', $pre_array[$last]);
  3391. $switchable_answer_set = false;
  3392. if (isset($is_set_switchable[1]) && $is_set_switchable[1] == 1) {
  3393. $switchable_answer_set = true;
  3394. }
  3395. $answer = '';
  3396. for ($k = 0; $k < $last; $k++) {
  3397. $answer .= $pre_array[$k];
  3398. }
  3399. // splits weightings that are joined with a comma
  3400. $answerWeighting = explode(',', $is_set_switchable[0]);
  3401. // we save the answer because it will be modified
  3402. $temp = $answer;
  3403. $answer = '';
  3404. $j = 0;
  3405. //initialise answer tags
  3406. $user_tags = $correct_tags = $real_text = [];
  3407. // the loop will stop at the end of the text
  3408. while (1) {
  3409. // quits the loop if there are no more blanks (detect '[')
  3410. if ($temp == false || ($pos = api_strpos($temp, '[')) === false) {
  3411. // adds the end of the text
  3412. $answer = $temp;
  3413. $real_text[] = $answer;
  3414. break; //no more "blanks", quit the loop
  3415. }
  3416. // adds the piece of text that is before the blank
  3417. //and ends with '[' into a general storage array
  3418. $real_text[] = api_substr($temp, 0, $pos + 1);
  3419. $answer .= api_substr($temp, 0, $pos + 1);
  3420. //take the string remaining (after the last "[" we found)
  3421. $temp = api_substr($temp, $pos + 1);
  3422. // quit the loop if there are no more blanks, and update $pos to the position of next ']'
  3423. if (($pos = api_strpos($temp, ']')) === false) {
  3424. // adds the end of the text
  3425. $answer .= $temp;
  3426. break;
  3427. }
  3428. if ($from_database) {
  3429. $str = $answerFromDatabase;
  3430. api_preg_match_all('#\[([^[]*)\]#', $str, $arr);
  3431. $str = str_replace('\r\n', '', $str);
  3432. $choice = $arr[1];
  3433. if (isset($choice[$j])) {
  3434. $tmp = api_strrpos($choice[$j], ' / ');
  3435. $choice[$j] = api_substr($choice[$j], 0, $tmp);
  3436. $choice[$j] = trim($choice[$j]);
  3437. // Needed to let characters ' and " to work as part of an answer
  3438. $choice[$j] = stripslashes($choice[$j]);
  3439. } else {
  3440. $choice[$j] = null;
  3441. }
  3442. } else {
  3443. // This value is the user input, not escaped while correct answer is escaped by ckeditor
  3444. $choice[$j] = api_htmlentities(trim($choice[$j]));
  3445. }
  3446. $user_tags[] = $choice[$j];
  3447. // Put the contents of the [] answer tag into correct_tags[]
  3448. $correct_tags[] = api_substr($temp, 0, $pos);
  3449. $j++;
  3450. $temp = api_substr($temp, $pos + 1);
  3451. }
  3452. $answer = '';
  3453. $real_correct_tags = $correct_tags;
  3454. $chosen_list = [];
  3455. for ($i = 0; $i < count($real_correct_tags); $i++) {
  3456. if ($i == 0) {
  3457. $answer .= $real_text[0];
  3458. }
  3459. if (!$switchable_answer_set) {
  3460. // Needed to parse ' and " characters
  3461. $user_tags[$i] = stripslashes($user_tags[$i]);
  3462. if ($correct_tags[$i] == $user_tags[$i]) {
  3463. // gives the related weighting to the student
  3464. $questionScore += $answerWeighting[$i];
  3465. // increments total score
  3466. $totalScore += $answerWeighting[$i];
  3467. // adds the word in green at the end of the string
  3468. $answer .= $correct_tags[$i];
  3469. } elseif (!empty($user_tags[$i])) {
  3470. // else if the word entered by the student IS NOT the same as
  3471. // the one defined by the professor
  3472. // adds the word in red at the end of the string, and strikes it
  3473. $answer .= '<font color="red"><s>'.$user_tags[$i].'</s></font>';
  3474. } else {
  3475. // adds a tabulation if no word has been typed by the student
  3476. $answer .= ''; // remove &nbsp; that causes issue
  3477. }
  3478. } else {
  3479. // switchable fill in the blanks
  3480. if (in_array($user_tags[$i], $correct_tags)) {
  3481. $chosen_list[] = $user_tags[$i];
  3482. $correct_tags = array_diff($correct_tags, $chosen_list);
  3483. // gives the related weighting to the student
  3484. $questionScore += $answerWeighting[$i];
  3485. // increments total score
  3486. $totalScore += $answerWeighting[$i];
  3487. // adds the word in green at the end of the string
  3488. $answer .= $user_tags[$i];
  3489. } elseif (!empty($user_tags[$i])) {
  3490. // else if the word entered by the student IS NOT the same
  3491. // as the one defined by the professor
  3492. // adds the word in red at the end of the string, and strikes it
  3493. $answer .= '<font color="red"><s>'.$user_tags[$i].'</s></font>';
  3494. } else {
  3495. // adds a tabulation if no word has been typed by the student
  3496. $answer .= ''; // remove &nbsp; that causes issue
  3497. }
  3498. }
  3499. // adds the correct word, followed by ] to close the blank
  3500. $answer .= ' / <font color="green"><b>'.$real_correct_tags[$i].'</b></font>]';
  3501. if (isset($real_text[$i + 1])) {
  3502. $answer .= $real_text[$i + 1];
  3503. }
  3504. }
  3505. } else {
  3506. // insert the student result in the track_e_attempt table, field answer
  3507. // $answer is the answer like in the c_quiz_answer table for the question
  3508. // student data are choice[]
  3509. $listCorrectAnswers = FillBlanks::getAnswerInfo($answer);
  3510. $switchableAnswerSet = $listCorrectAnswers['switchable'];
  3511. $answerWeighting = $listCorrectAnswers['weighting'];
  3512. // user choices is an array $choice
  3513. // get existing user data in n the BDD
  3514. if ($from_database) {
  3515. $listStudentResults = FillBlanks::getAnswerInfo(
  3516. $answerFromDatabase,
  3517. true
  3518. );
  3519. $choice = $listStudentResults['student_answer'];
  3520. }
  3521. // loop other all blanks words
  3522. if (!$switchableAnswerSet) {
  3523. // not switchable answer, must be in the same place than teacher order
  3524. for ($i = 0; $i < count($listCorrectAnswers['words']); $i++) {
  3525. $studentAnswer = isset($choice[$i]) ? $choice[$i] : '';
  3526. $correctAnswer = $listCorrectAnswers['words'][$i];
  3527. if ($debug) {
  3528. error_log("Student answer: $i");
  3529. error_log($studentAnswer);
  3530. }
  3531. // This value is the user input, not escaped while correct answer is escaped by ckeditor
  3532. // Works with cyrillic alphabet and when using ">" chars see #7718 #7610 #7618
  3533. // ENT_QUOTES is used in order to transform ' to &#039;
  3534. if (!$from_database) {
  3535. $studentAnswer = FillBlanks::clearStudentAnswer($studentAnswer);
  3536. if ($debug) {
  3537. error_log("Student answer cleaned:");
  3538. error_log($studentAnswer);
  3539. }
  3540. }
  3541. $isAnswerCorrect = 0;
  3542. if (FillBlanks::isStudentAnswerGood($studentAnswer, $correctAnswer, $from_database)) {
  3543. // gives the related weighting to the student
  3544. $questionScore += $answerWeighting[$i];
  3545. // increments total score
  3546. $totalScore += $answerWeighting[$i];
  3547. $isAnswerCorrect = 1;
  3548. }
  3549. if ($debug) {
  3550. error_log("isAnswerCorrect $i: $isAnswerCorrect");
  3551. }
  3552. $studentAnswerToShow = $studentAnswer;
  3553. $type = FillBlanks::getFillTheBlankAnswerType($correctAnswer);
  3554. if ($debug) {
  3555. error_log("Fill in blank type: $type");
  3556. }
  3557. if ($type == FillBlanks::FILL_THE_BLANK_MENU) {
  3558. $listMenu = FillBlanks::getFillTheBlankMenuAnswers($correctAnswer, false);
  3559. if ($studentAnswer != '') {
  3560. foreach ($listMenu as $item) {
  3561. if (sha1($item) == $studentAnswer) {
  3562. $studentAnswerToShow = $item;
  3563. }
  3564. }
  3565. }
  3566. }
  3567. $listCorrectAnswers['student_answer'][$i] = $studentAnswerToShow;
  3568. $listCorrectAnswers['student_score'][$i] = $isAnswerCorrect;
  3569. }
  3570. } else {
  3571. // switchable answer
  3572. $listStudentAnswerTemp = $choice;
  3573. $listTeacherAnswerTemp = $listCorrectAnswers['words'];
  3574. // for every teacher answer, check if there is a student answer
  3575. for ($i = 0; $i < count($listStudentAnswerTemp); $i++) {
  3576. $studentAnswer = trim($listStudentAnswerTemp[$i]);
  3577. $studentAnswerToShow = $studentAnswer;
  3578. if ($debug) {
  3579. error_log("Student answer: $i");
  3580. error_log($studentAnswer);
  3581. }
  3582. $found = false;
  3583. for ($j = 0; $j < count($listTeacherAnswerTemp); $j++) {
  3584. $correctAnswer = $listTeacherAnswerTemp[$j];
  3585. $type = FillBlanks::getFillTheBlankAnswerType($correctAnswer);
  3586. if ($type == FillBlanks::FILL_THE_BLANK_MENU) {
  3587. $listMenu = FillBlanks::getFillTheBlankMenuAnswers($correctAnswer, false);
  3588. if (!empty($studentAnswer)) {
  3589. foreach ($listMenu as $key => $item) {
  3590. if ($key == $correctAnswer) {
  3591. $studentAnswerToShow = $item;
  3592. break;
  3593. }
  3594. }
  3595. }
  3596. }
  3597. if (!$found) {
  3598. if (FillBlanks::isStudentAnswerGood(
  3599. $studentAnswer,
  3600. $correctAnswer,
  3601. $from_database
  3602. )
  3603. ) {
  3604. $questionScore += $answerWeighting[$i];
  3605. $totalScore += $answerWeighting[$i];
  3606. $listTeacherAnswerTemp[$j] = '';
  3607. $found = true;
  3608. }
  3609. }
  3610. }
  3611. $listCorrectAnswers['student_answer'][$i] = $studentAnswerToShow;
  3612. if (!$found) {
  3613. $listCorrectAnswers['student_score'][$i] = 0;
  3614. } else {
  3615. $listCorrectAnswers['student_score'][$i] = 1;
  3616. }
  3617. }
  3618. }
  3619. $answer = FillBlanks::getAnswerInStudentAttempt($listCorrectAnswers);
  3620. }
  3621. break;
  3622. case CALCULATED_ANSWER:
  3623. $calculatedAnswerList = Session::read('calculatedAnswerId');
  3624. if (!empty($calculatedAnswerList)) {
  3625. $answer = $objAnswerTmp->selectAnswer($calculatedAnswerList[$questionId]);
  3626. $preArray = explode('@@', $answer);
  3627. $last = count($preArray) - 1;
  3628. $answer = '';
  3629. for ($k = 0; $k < $last; $k++) {
  3630. $answer .= $preArray[$k];
  3631. }
  3632. $answerWeighting = [$answerWeighting];
  3633. // we save the answer because it will be modified
  3634. $temp = $answer;
  3635. $answer = '';
  3636. $j = 0;
  3637. // initialise answer tags
  3638. $userTags = $correctTags = $realText = [];
  3639. // the loop will stop at the end of the text
  3640. while (1) {
  3641. // quits the loop if there are no more blanks (detect '[')
  3642. if ($temp == false || ($pos = api_strpos($temp, '[')) === false) {
  3643. // adds the end of the text
  3644. $answer = $temp;
  3645. $realText[] = $answer;
  3646. break; //no more "blanks", quit the loop
  3647. }
  3648. // adds the piece of text that is before the blank
  3649. // and ends with '[' into a general storage array
  3650. $realText[] = api_substr($temp, 0, $pos + 1);
  3651. $answer .= api_substr($temp, 0, $pos + 1);
  3652. // take the string remaining (after the last "[" we found)
  3653. $temp = api_substr($temp, $pos + 1);
  3654. // quit the loop if there are no more blanks, and update $pos to the position of next ']'
  3655. if (($pos = api_strpos($temp, ']')) === false) {
  3656. // adds the end of the text
  3657. $answer .= $temp;
  3658. break;
  3659. }
  3660. if ($from_database) {
  3661. $sql = "SELECT answer FROM ".$TBL_TRACK_ATTEMPT."
  3662. WHERE
  3663. exe_id = '".$exeId."' AND
  3664. question_id = ".intval($questionId);
  3665. $result = Database::query($sql);
  3666. $str = Database::result($result, 0, 'answer');
  3667. api_preg_match_all('#\[([^[]*)\]#', $str, $arr);
  3668. $str = str_replace('\r\n', '', $str);
  3669. $choice = $arr[1];
  3670. if (isset($choice[$j])) {
  3671. $tmp = api_strrpos($choice[$j], ' / ');
  3672. if ($tmp) {
  3673. $choice[$j] = api_substr($choice[$j], 0, $tmp);
  3674. } else {
  3675. $tmp = ltrim($tmp, '[');
  3676. $tmp = rtrim($tmp, ']');
  3677. }
  3678. $choice[$j] = trim($choice[$j]);
  3679. // Needed to let characters ' and " to work as part of an answer
  3680. $choice[$j] = stripslashes($choice[$j]);
  3681. } else {
  3682. $choice[$j] = null;
  3683. }
  3684. } else {
  3685. // This value is the user input not escaped while correct answer is escaped by ckeditor
  3686. $choice[$j] = api_htmlentities(trim($choice[$j]));
  3687. }
  3688. $userTags[] = $choice[$j];
  3689. // put the contents of the [] answer tag into correct_tags[]
  3690. $correctTags[] = api_substr($temp, 0, $pos);
  3691. $j++;
  3692. $temp = api_substr($temp, $pos + 1);
  3693. }
  3694. $answer = '';
  3695. $realCorrectTags = $correctTags;
  3696. $calculatedStatus = Display::label(get_lang('Incorrect'), 'danger');
  3697. $expectedAnswer = '';
  3698. $calculatedChoice = '';
  3699. for ($i = 0; $i < count($realCorrectTags); $i++) {
  3700. if ($i == 0) {
  3701. $answer .= $realText[0];
  3702. }
  3703. // Needed to parse ' and " characters
  3704. $userTags[$i] = stripslashes($userTags[$i]);
  3705. if ($correctTags[$i] == $userTags[$i]) {
  3706. // gives the related weighting to the student
  3707. $questionScore += $answerWeighting[$i];
  3708. // increments total score
  3709. $totalScore += $answerWeighting[$i];
  3710. // adds the word in green at the end of the string
  3711. $answer .= $correctTags[$i];
  3712. $calculatedChoice = $correctTags[$i];
  3713. } elseif (!empty($userTags[$i])) {
  3714. // else if the word entered by the student IS NOT the same as
  3715. // the one defined by the professor
  3716. // adds the word in red at the end of the string, and strikes it
  3717. $answer .= '<font color="red"><s>'.$userTags[$i].'</s></font>';
  3718. $calculatedChoice = $userTags[$i];
  3719. } else {
  3720. // adds a tabulation if no word has been typed by the student
  3721. $answer .= ''; // remove &nbsp; that causes issue
  3722. }
  3723. // adds the correct word, followed by ] to close the blank
  3724. if ($this->results_disabled != EXERCISE_FEEDBACK_TYPE_EXAM) {
  3725. $answer .= ' / <font color="green"><b>'.$realCorrectTags[$i].'</b></font>';
  3726. $calculatedStatus = Display::label(get_lang('Correct'), 'success');
  3727. $expectedAnswer = $realCorrectTags[$i];
  3728. }
  3729. $answer .= ']';
  3730. if (isset($realText[$i + 1])) {
  3731. $answer .= $realText[$i + 1];
  3732. }
  3733. }
  3734. } else {
  3735. if ($from_database) {
  3736. $sql = "SELECT *
  3737. FROM $TBL_TRACK_ATTEMPT
  3738. WHERE
  3739. exe_id = $exeId AND
  3740. question_id= ".intval($questionId);
  3741. $result = Database::query($sql);
  3742. $resultData = Database::fetch_array($result, 'ASSOC');
  3743. $answer = $resultData['answer'];
  3744. $questionScore = $resultData['marks'];
  3745. }
  3746. }
  3747. break;
  3748. case FREE_ANSWER:
  3749. if ($from_database) {
  3750. $sql = "SELECT answer, marks FROM $TBL_TRACK_ATTEMPT
  3751. WHERE
  3752. exe_id = $exeId AND
  3753. question_id= ".$questionId;
  3754. $result = Database::query($sql);
  3755. $data = Database::fetch_array($result);
  3756. $choice = $data['answer'];
  3757. $choice = str_replace('\r\n', '', $choice);
  3758. $choice = stripslashes($choice);
  3759. $questionScore = $data['marks'];
  3760. if ($questionScore == -1) {
  3761. $totalScore += 0;
  3762. } else {
  3763. $totalScore += $questionScore;
  3764. }
  3765. if ($questionScore == '') {
  3766. $questionScore = 0;
  3767. }
  3768. $arrques = $questionName;
  3769. $arrans = $choice;
  3770. } else {
  3771. $studentChoice = $choice;
  3772. if ($studentChoice) {
  3773. //Fixing negative puntation see #2193
  3774. $questionScore = 0;
  3775. $totalScore += 0;
  3776. }
  3777. }
  3778. break;
  3779. case ORAL_EXPRESSION:
  3780. if ($from_database) {
  3781. $query = "SELECT answer, marks
  3782. FROM $TBL_TRACK_ATTEMPT
  3783. WHERE
  3784. exe_id = $exeId AND
  3785. question_id = $questionId
  3786. ";
  3787. $resq = Database::query($query);
  3788. $row = Database::fetch_assoc($resq);
  3789. $choice = $row['answer'];
  3790. $choice = str_replace('\r\n', '', $choice);
  3791. $choice = stripslashes($choice);
  3792. $questionScore = $row['marks'];
  3793. if ($questionScore == -1) {
  3794. $totalScore += 0;
  3795. } else {
  3796. $totalScore += $questionScore;
  3797. }
  3798. $arrques = $questionName;
  3799. $arrans = $choice;
  3800. } else {
  3801. $studentChoice = $choice;
  3802. if ($studentChoice) {
  3803. //Fixing negative puntation see #2193
  3804. $questionScore = 0;
  3805. $totalScore += 0;
  3806. }
  3807. }
  3808. break;
  3809. case DRAGGABLE:
  3810. case MATCHING_DRAGGABLE:
  3811. case MATCHING:
  3812. if ($from_database) {
  3813. $sql = "SELECT id, answer, id_auto
  3814. FROM $table_ans
  3815. WHERE
  3816. c_id = $course_id AND
  3817. question_id = $questionId AND
  3818. correct = 0
  3819. ";
  3820. $result = Database::query($sql);
  3821. // Getting the real answer
  3822. $real_list = [];
  3823. while ($realAnswer = Database::fetch_array($result)) {
  3824. $real_list[$realAnswer['id_auto']] = $realAnswer['answer'];
  3825. }
  3826. $sql = "SELECT id, answer, correct, id_auto, ponderation
  3827. FROM $table_ans
  3828. WHERE
  3829. c_id = $course_id AND
  3830. question_id = $questionId AND
  3831. correct <> 0
  3832. ORDER BY id_auto";
  3833. $result = Database::query($sql);
  3834. $options = [];
  3835. while ($row = Database::fetch_array($result, 'ASSOC')) {
  3836. $options[] = $row;
  3837. }
  3838. $questionScore = 0;
  3839. $counterAnswer = 1;
  3840. foreach ($options as $a_answers) {
  3841. $i_answer_id = $a_answers['id']; //3
  3842. $s_answer_label = $a_answers['answer']; // your daddy - your mother
  3843. $i_answer_correct_answer = $a_answers['correct']; //1 - 2
  3844. $i_answer_id_auto = $a_answers['id_auto']; // 3 - 4
  3845. $sql = "SELECT answer FROM $TBL_TRACK_ATTEMPT
  3846. WHERE
  3847. exe_id = '$exeId' AND
  3848. question_id = '$questionId' AND
  3849. position = '$i_answer_id_auto'";
  3850. $result = Database::query($sql);
  3851. $s_user_answer = 0;
  3852. if (Database::num_rows($result) > 0) {
  3853. // rich - good looking
  3854. $s_user_answer = Database::result($result, 0, 0);
  3855. }
  3856. $i_answerWeighting = $a_answers['ponderation'];
  3857. $user_answer = '';
  3858. $status = Display::label(get_lang('Incorrect'), 'danger');
  3859. if (!empty($s_user_answer)) {
  3860. if ($answerType == DRAGGABLE) {
  3861. if ($s_user_answer == $i_answer_correct_answer) {
  3862. $questionScore += $i_answerWeighting;
  3863. $totalScore += $i_answerWeighting;
  3864. $user_answer = Display::label(get_lang('Correct'), 'success');
  3865. if ($this->showExpectedChoice()) {
  3866. $user_answer = $answerMatching[$i_answer_id_auto];
  3867. }
  3868. $status = Display::label(get_lang('Correct'), 'success');
  3869. } else {
  3870. $user_answer = Display::label(get_lang('Incorrect'), 'danger');
  3871. if ($this->showExpectedChoice()) {
  3872. $data = $options[$real_list[$s_user_answer] - 1];
  3873. $user_answer = $data['answer'];
  3874. }
  3875. }
  3876. } else {
  3877. if ($s_user_answer == $i_answer_correct_answer) {
  3878. $questionScore += $i_answerWeighting;
  3879. $totalScore += $i_answerWeighting;
  3880. $status = Display::label(get_lang('Correct'), 'success');
  3881. // Try with id
  3882. if (isset($real_list[$i_answer_id])) {
  3883. $user_answer = Display::span(
  3884. $real_list[$i_answer_id],
  3885. ['style' => 'color: #008000; font-weight: bold;']
  3886. );
  3887. }
  3888. // Try with $i_answer_id_auto
  3889. if (empty($user_answer)) {
  3890. if (isset($real_list[$i_answer_id_auto])) {
  3891. $user_answer = Display::span(
  3892. $real_list[$i_answer_id_auto],
  3893. ['style' => 'color: #008000; font-weight: bold;']
  3894. );
  3895. }
  3896. }
  3897. if (isset($real_list[$i_answer_correct_answer])) {
  3898. $user_answer = Display::span(
  3899. $real_list[$i_answer_correct_answer],
  3900. ['style' => 'color: #008000; font-weight: bold;']
  3901. );
  3902. }
  3903. } else {
  3904. $user_answer = Display::span(
  3905. $real_list[$s_user_answer],
  3906. ['style' => 'color: #FF0000; text-decoration: line-through;']
  3907. );
  3908. if ($this->showExpectedChoice()) {
  3909. if (isset($real_list[$s_user_answer])) {
  3910. $user_answer = Display::span($real_list[$s_user_answer]);
  3911. }
  3912. }
  3913. }
  3914. }
  3915. } elseif ($answerType == DRAGGABLE) {
  3916. $user_answer = Display::label(get_lang('Incorrect'), 'danger');
  3917. if ($this->showExpectedChoice()) {
  3918. $user_answer = '';
  3919. }
  3920. } else {
  3921. $user_answer = Display::span(
  3922. get_lang('Incorrect').' &nbsp;',
  3923. ['style' => 'color: #FF0000; text-decoration: line-through;']
  3924. );
  3925. if ($this->showExpectedChoice()) {
  3926. $user_answer = '';
  3927. }
  3928. }
  3929. if ($show_result) {
  3930. if ($this->showExpectedChoice() == false &&
  3931. $showTotalScoreAndUserChoicesInLastAttempt === false
  3932. ) {
  3933. $user_answer = '';
  3934. }
  3935. switch ($answerType) {
  3936. case MATCHING:
  3937. case MATCHING_DRAGGABLE:
  3938. echo '<tr>';
  3939. if ($this->results_disabled != RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER) {
  3940. echo '<td>'.$s_answer_label.'</td>';
  3941. echo '<td>'.$user_answer.'</td>';
  3942. } else {
  3943. echo '<td>'.$s_answer_label.'</td>';
  3944. $status = Display::label(get_lang('Correct'), 'success');
  3945. }
  3946. if ($this->showExpectedChoice()) {
  3947. echo '<td>';
  3948. if (in_array($answerType, [MATCHING, MATCHING_DRAGGABLE])) {
  3949. if (isset($real_list[$i_answer_correct_answer]) &&
  3950. $showTotalScoreAndUserChoicesInLastAttempt == true
  3951. ) {
  3952. echo Display::span(
  3953. $real_list[$i_answer_correct_answer]
  3954. );
  3955. }
  3956. }
  3957. echo '</td>';
  3958. echo '<td>'.$status.'</td>';
  3959. } else {
  3960. echo '<td>';
  3961. if (in_array($answerType, [MATCHING, MATCHING_DRAGGABLE])) {
  3962. if (isset($real_list[$i_answer_correct_answer]) &&
  3963. $showTotalScoreAndUserChoicesInLastAttempt === true
  3964. ) {
  3965. echo Display::span(
  3966. $real_list[$i_answer_correct_answer],
  3967. ['style' => 'color: #008000; font-weight: bold;']
  3968. );
  3969. }
  3970. }
  3971. echo '</td>';
  3972. }
  3973. echo '</tr>';
  3974. break;
  3975. case DRAGGABLE:
  3976. if ($showTotalScoreAndUserChoicesInLastAttempt == false) {
  3977. $s_answer_label = '';
  3978. }
  3979. echo '<tr>';
  3980. if ($this->showExpectedChoice()) {
  3981. if ($this->results_disabled != RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER) {
  3982. echo '<td>'.$user_answer.'</td>';
  3983. } else {
  3984. $status = Display::label(get_lang('Correct'), 'success');
  3985. }
  3986. echo '<td>'.$s_answer_label.'</td>';
  3987. echo '<td>'.$status.'</td>';
  3988. } else {
  3989. echo '<td>'.$s_answer_label.'</td>';
  3990. echo '<td>'.$user_answer.'</td>';
  3991. echo '<td>';
  3992. if (in_array($answerType, [MATCHING, MATCHING_DRAGGABLE])) {
  3993. if (isset($real_list[$i_answer_correct_answer]) &&
  3994. $showTotalScoreAndUserChoicesInLastAttempt === true
  3995. ) {
  3996. echo Display::span(
  3997. $real_list[$i_answer_correct_answer],
  3998. ['style' => 'color: #008000; font-weight: bold;']
  3999. );
  4000. }
  4001. }
  4002. echo '</td>';
  4003. }
  4004. echo '</tr>';
  4005. break;
  4006. }
  4007. }
  4008. $counterAnswer++;
  4009. }
  4010. break 2; // break the switch and the "for" condition
  4011. } else {
  4012. if ($answerCorrect) {
  4013. if (isset($choice[$answerAutoId]) &&
  4014. $answerCorrect == $choice[$answerAutoId]
  4015. ) {
  4016. $questionScore += $answerWeighting;
  4017. $totalScore += $answerWeighting;
  4018. $user_answer = Display::span($answerMatching[$choice[$answerAutoId]]);
  4019. } else {
  4020. if (isset($answerMatching[$choice[$answerAutoId]])) {
  4021. $user_answer = Display::span(
  4022. $answerMatching[$choice[$answerAutoId]],
  4023. ['style' => 'color: #FF0000; text-decoration: line-through;']
  4024. );
  4025. }
  4026. }
  4027. $matching[$answerAutoId] = $choice[$answerAutoId];
  4028. }
  4029. }
  4030. break;
  4031. case HOT_SPOT:
  4032. if ($from_database) {
  4033. $TBL_TRACK_HOTSPOT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  4034. // Check auto id
  4035. $sql = "SELECT hotspot_correct
  4036. FROM $TBL_TRACK_HOTSPOT
  4037. WHERE
  4038. hotspot_exe_id = $exeId AND
  4039. hotspot_question_id= $questionId AND
  4040. hotspot_answer_id = ".intval($answerAutoId)."
  4041. ORDER BY hotspot_id ASC";
  4042. $result = Database::query($sql);
  4043. if (Database::num_rows($result)) {
  4044. $studentChoice = Database::result(
  4045. $result,
  4046. 0,
  4047. 'hotspot_correct'
  4048. );
  4049. if ($studentChoice) {
  4050. $questionScore += $answerWeighting;
  4051. $totalScore += $answerWeighting;
  4052. }
  4053. } else {
  4054. // If answer.id is different:
  4055. $sql = "SELECT hotspot_correct
  4056. FROM $TBL_TRACK_HOTSPOT
  4057. WHERE
  4058. hotspot_exe_id = $exeId AND
  4059. hotspot_question_id= $questionId AND
  4060. hotspot_answer_id = ".intval($answerId)."
  4061. ORDER BY hotspot_id ASC";
  4062. $result = Database::query($sql);
  4063. if (Database::num_rows($result)) {
  4064. $studentChoice = Database::result(
  4065. $result,
  4066. 0,
  4067. 'hotspot_correct'
  4068. );
  4069. if ($studentChoice) {
  4070. $questionScore += $answerWeighting;
  4071. $totalScore += $answerWeighting;
  4072. }
  4073. } else {
  4074. // check answer.iid
  4075. if (!empty($answerIid)) {
  4076. $sql = "SELECT hotspot_correct
  4077. FROM $TBL_TRACK_HOTSPOT
  4078. WHERE
  4079. hotspot_exe_id = $exeId AND
  4080. hotspot_question_id= $questionId AND
  4081. hotspot_answer_id = ".intval($answerIid)."
  4082. ORDER BY hotspot_id ASC";
  4083. $result = Database::query($sql);
  4084. $studentChoice = Database::result(
  4085. $result,
  4086. 0,
  4087. 'hotspot_correct'
  4088. );
  4089. if ($studentChoice) {
  4090. $questionScore += $answerWeighting;
  4091. $totalScore += $answerWeighting;
  4092. }
  4093. }
  4094. }
  4095. }
  4096. } else {
  4097. if (!isset($choice[$answerAutoId]) && !isset($choice[$answerIid])) {
  4098. $choice[$answerAutoId] = 0;
  4099. $choice[$answerIid] = 0;
  4100. } else {
  4101. $studentChoice = $choice[$answerAutoId];
  4102. if (empty($studentChoice)) {
  4103. $studentChoice = $choice[$answerIid];
  4104. }
  4105. $choiceIsValid = false;
  4106. if (!empty($studentChoice)) {
  4107. $hotspotType = $objAnswerTmp->selectHotspotType($answerId);
  4108. $hotspotCoordinates = $objAnswerTmp->selectHotspotCoordinates($answerId);
  4109. $choicePoint = Geometry::decodePoint($studentChoice);
  4110. switch ($hotspotType) {
  4111. case 'square':
  4112. $hotspotProperties = Geometry::decodeSquare($hotspotCoordinates);
  4113. $choiceIsValid = Geometry::pointIsInSquare($hotspotProperties, $choicePoint);
  4114. break;
  4115. case 'circle':
  4116. $hotspotProperties = Geometry::decodeEllipse($hotspotCoordinates);
  4117. $choiceIsValid = Geometry::pointIsInEllipse($hotspotProperties, $choicePoint);
  4118. break;
  4119. case 'poly':
  4120. $hotspotProperties = Geometry::decodePolygon($hotspotCoordinates);
  4121. $choiceIsValid = Geometry::pointIsInPolygon($hotspotProperties, $choicePoint);
  4122. break;
  4123. }
  4124. }
  4125. $choice[$answerAutoId] = 0;
  4126. if ($choiceIsValid) {
  4127. $questionScore += $answerWeighting;
  4128. $totalScore += $answerWeighting;
  4129. $choice[$answerAutoId] = 1;
  4130. $choice[$answerIid] = 1;
  4131. }
  4132. }
  4133. }
  4134. break;
  4135. case HOT_SPOT_ORDER:
  4136. // @todo never added to chamilo
  4137. // for hotspot with fixed order
  4138. $studentChoice = $choice['order'][$answerId];
  4139. if ($studentChoice == $answerId) {
  4140. $questionScore += $answerWeighting;
  4141. $totalScore += $answerWeighting;
  4142. $studentChoice = true;
  4143. } else {
  4144. $studentChoice = false;
  4145. }
  4146. break;
  4147. case HOT_SPOT_DELINEATION:
  4148. // for hotspot with delineation
  4149. if ($from_database) {
  4150. // getting the user answer
  4151. $TBL_TRACK_HOTSPOT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  4152. $query = "SELECT hotspot_correct, hotspot_coordinate
  4153. FROM $TBL_TRACK_HOTSPOT
  4154. WHERE
  4155. hotspot_exe_id = '".$exeId."' AND
  4156. hotspot_question_id= '".$questionId."' AND
  4157. hotspot_answer_id='1'";
  4158. //by default we take 1 because it's a delineation
  4159. $resq = Database::query($query);
  4160. $row = Database::fetch_array($resq, 'ASSOC');
  4161. $choice = $row['hotspot_correct'];
  4162. $user_answer = $row['hotspot_coordinate'];
  4163. // THIS is very important otherwise the poly_compile will throw an error!!
  4164. // round-up the coordinates
  4165. $coords = explode('/', $user_answer);
  4166. $user_array = '';
  4167. foreach ($coords as $coord) {
  4168. list($x, $y) = explode(';', $coord);
  4169. $user_array .= round($x).';'.round($y).'/';
  4170. }
  4171. $user_array = substr($user_array, 0, -1);
  4172. } else {
  4173. if (!empty($studentChoice)) {
  4174. $newquestionList[] = $questionId;
  4175. }
  4176. if ($answerId === 1) {
  4177. $studentChoice = $choice[$answerId];
  4178. $questionScore += $answerWeighting;
  4179. if ($hotspot_delineation_result[1] == 1) {
  4180. $totalScore += $answerWeighting; //adding the total
  4181. }
  4182. }
  4183. }
  4184. $_SESSION['hotspot_coord'][1] = $delineation_cord;
  4185. $_SESSION['hotspot_dest'][1] = $answer_delineation_destination;
  4186. break;
  4187. case ANNOTATION:
  4188. if ($from_database) {
  4189. $sql = "SELECT answer, marks FROM $TBL_TRACK_ATTEMPT
  4190. WHERE
  4191. exe_id = $exeId AND
  4192. question_id= ".$questionId;
  4193. $resq = Database::query($sql);
  4194. $data = Database::fetch_array($resq);
  4195. $questionScore = empty($data['marks']) ? 0 : $data['marks'];
  4196. $totalScore += $questionScore == -1 ? 0 : $questionScore;
  4197. $arrques = $questionName;
  4198. break;
  4199. }
  4200. $studentChoice = $choice;
  4201. if ($studentChoice) {
  4202. $questionScore = 0;
  4203. $totalScore += 0;
  4204. }
  4205. break;
  4206. } // end switch Answertype
  4207. if ($show_result) {
  4208. if ($debug) {
  4209. error_log('Showing questions $from '.$from);
  4210. }
  4211. if ($from === 'exercise_result') {
  4212. //display answers (if not matching type, or if the answer is correct)
  4213. if (!in_array($answerType, [MATCHING, DRAGGABLE, MATCHING_DRAGGABLE]) ||
  4214. $answerCorrect
  4215. ) {
  4216. if (in_array(
  4217. $answerType,
  4218. [
  4219. UNIQUE_ANSWER,
  4220. UNIQUE_ANSWER_IMAGE,
  4221. UNIQUE_ANSWER_NO_OPTION,
  4222. MULTIPLE_ANSWER,
  4223. MULTIPLE_ANSWER_COMBINATION,
  4224. GLOBAL_MULTIPLE_ANSWER,
  4225. READING_COMPREHENSION,
  4226. ]
  4227. )) {
  4228. ExerciseShowFunctions::display_unique_or_multiple_answer(
  4229. $this,
  4230. $feedback_type,
  4231. $answerType,
  4232. $studentChoice,
  4233. $answer,
  4234. $answerComment,
  4235. $answerCorrect,
  4236. 0,
  4237. 0,
  4238. 0,
  4239. $results_disabled,
  4240. $showTotalScoreAndUserChoicesInLastAttempt,
  4241. $this->export
  4242. );
  4243. } elseif ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
  4244. ExerciseShowFunctions::display_multiple_answer_true_false(
  4245. $this,
  4246. $feedback_type,
  4247. $answerType,
  4248. $studentChoice,
  4249. $answer,
  4250. $answerComment,
  4251. $answerCorrect,
  4252. 0,
  4253. $questionId,
  4254. 0,
  4255. $results_disabled,
  4256. $showTotalScoreAndUserChoicesInLastAttempt
  4257. );
  4258. } elseif ($answerType == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  4259. ExerciseShowFunctions::displayMultipleAnswerTrueFalseDegreeCertainty(
  4260. $feedback_type,
  4261. $studentChoice,
  4262. $studentChoiceDegree,
  4263. $answer,
  4264. $answerComment,
  4265. $answerCorrect,
  4266. $questionId,
  4267. $results_disabled
  4268. );
  4269. } elseif ($answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE) {
  4270. ExerciseShowFunctions::display_multiple_answer_combination_true_false(
  4271. $this,
  4272. $feedback_type,
  4273. $answerType,
  4274. $studentChoice,
  4275. $answer,
  4276. $answerComment,
  4277. $answerCorrect,
  4278. 0,
  4279. 0,
  4280. 0,
  4281. $results_disabled,
  4282. $showTotalScoreAndUserChoicesInLastAttempt
  4283. );
  4284. } elseif ($answerType == FILL_IN_BLANKS) {
  4285. ExerciseShowFunctions::display_fill_in_blanks_answer(
  4286. $feedback_type,
  4287. $answer,
  4288. 0,
  4289. 0,
  4290. $results_disabled,
  4291. '',
  4292. $showTotalScoreAndUserChoicesInLastAttempt
  4293. );
  4294. } elseif ($answerType == CALCULATED_ANSWER) {
  4295. ExerciseShowFunctions::display_calculated_answer(
  4296. $this,
  4297. $feedback_type,
  4298. $answer,
  4299. 0,
  4300. 0,
  4301. $results_disabled,
  4302. $showTotalScoreAndUserChoicesInLastAttempt,
  4303. $expectedAnswer,
  4304. $calculatedChoice,
  4305. $calculatedStatus
  4306. );
  4307. } elseif ($answerType == FREE_ANSWER) {
  4308. ExerciseShowFunctions::display_free_answer(
  4309. $feedback_type,
  4310. $choice,
  4311. $exeId,
  4312. $questionId,
  4313. $questionScore,
  4314. $results_disabled
  4315. );
  4316. } elseif ($answerType == ORAL_EXPRESSION) {
  4317. // to store the details of open questions in an array to be used in mail
  4318. /** @var OralExpression $objQuestionTmp */
  4319. ExerciseShowFunctions::display_oral_expression_answer(
  4320. $feedback_type,
  4321. $choice,
  4322. 0,
  4323. 0,
  4324. $objQuestionTmp->getFileUrl(true),
  4325. $results_disabled,
  4326. $questionScore
  4327. );
  4328. } elseif ($answerType == HOT_SPOT) {
  4329. $correctAnswerId = 0;
  4330. /**
  4331. * @var int
  4332. * @var TrackEHotspot $hotspot
  4333. */
  4334. foreach ($orderedHotspots as $correctAnswerId => $hotspot) {
  4335. if ($hotspot->getHotspotAnswerId() == $answerAutoId) {
  4336. break;
  4337. }
  4338. }
  4339. // force to show whether the choice is correct or not
  4340. $showTotalScoreAndUserChoicesInLastAttempt = true;
  4341. ExerciseShowFunctions::display_hotspot_answer(
  4342. $feedback_type,
  4343. ++$correctAnswerId,
  4344. $answer,
  4345. $studentChoice,
  4346. $answerComment,
  4347. $results_disabled,
  4348. $correctAnswerId,
  4349. $showTotalScoreAndUserChoicesInLastAttempt
  4350. );
  4351. } elseif ($answerType == HOT_SPOT_ORDER) {
  4352. ExerciseShowFunctions::display_hotspot_order_answer(
  4353. $feedback_type,
  4354. $answerId,
  4355. $answer,
  4356. $studentChoice,
  4357. $answerComment
  4358. );
  4359. } elseif ($answerType == HOT_SPOT_DELINEATION) {
  4360. $user_answer = $_SESSION['exerciseResultCoordinates'][$questionId];
  4361. //round-up the coordinates
  4362. $coords = explode('/', $user_answer);
  4363. $user_array = '';
  4364. foreach ($coords as $coord) {
  4365. list($x, $y) = explode(';', $coord);
  4366. $user_array .= round($x).';'.round($y).'/';
  4367. }
  4368. $user_array = substr($user_array, 0, -1);
  4369. if ($next) {
  4370. $user_answer = $user_array;
  4371. // we compare only the delineation not the other points
  4372. $answer_question = $_SESSION['hotspot_coord'][1];
  4373. $answerDestination = $_SESSION['hotspot_dest'][1];
  4374. //calculating the area
  4375. $poly_user = convert_coordinates($user_answer, '/');
  4376. $poly_answer = convert_coordinates($answer_question, '|');
  4377. $max_coord = poly_get_max($poly_user, $poly_answer);
  4378. $poly_user_compiled = poly_compile($poly_user, $max_coord);
  4379. $poly_answer_compiled = poly_compile($poly_answer, $max_coord);
  4380. $poly_results = poly_result($poly_answer_compiled, $poly_user_compiled, $max_coord);
  4381. $overlap = $poly_results['both'];
  4382. $poly_answer_area = $poly_results['s1'];
  4383. $poly_user_area = $poly_results['s2'];
  4384. $missing = $poly_results['s1Only'];
  4385. $excess = $poly_results['s2Only'];
  4386. //$overlap = round(polygons_overlap($poly_answer,$poly_user));
  4387. // //this is an area in pixels
  4388. if ($debug > 0) {
  4389. error_log(__LINE__.' - Polygons results are '.print_r($poly_results, 1), 0);
  4390. }
  4391. if ($overlap < 1) {
  4392. //shortcut to avoid complicated calculations
  4393. $final_overlap = 0;
  4394. $final_missing = 100;
  4395. $final_excess = 100;
  4396. } else {
  4397. // the final overlap is the percentage of the initial polygon
  4398. // that is overlapped by the user's polygon
  4399. $final_overlap = round(((float) $overlap / (float) $poly_answer_area) * 100);
  4400. if ($debug > 1) {
  4401. error_log(__LINE__.' - Final overlap is '.$final_overlap, 0);
  4402. }
  4403. // the final missing area is the percentage of the initial polygon
  4404. // that is not overlapped by the user's polygon
  4405. $final_missing = 100 - $final_overlap;
  4406. if ($debug > 1) {
  4407. error_log(__LINE__.' - Final missing is '.$final_missing, 0);
  4408. }
  4409. // the final excess area is the percentage of the initial polygon's size
  4410. // that is covered by the user's polygon outside of the initial polygon
  4411. $final_excess = round((((float) $poly_user_area - (float) $overlap) / (float) $poly_answer_area) * 100);
  4412. if ($debug > 1) {
  4413. error_log(__LINE__.' - Final excess is '.$final_excess, 0);
  4414. }
  4415. }
  4416. //checking the destination parameters parsing the "@@"
  4417. $destination_items = explode(
  4418. '@@',
  4419. $answerDestination
  4420. );
  4421. $threadhold_total = $destination_items[0];
  4422. $threadhold_items = explode(
  4423. ';',
  4424. $threadhold_total
  4425. );
  4426. $threadhold1 = $threadhold_items[0]; // overlap
  4427. $threadhold2 = $threadhold_items[1]; // excess
  4428. $threadhold3 = $threadhold_items[2]; //missing
  4429. // if is delineation
  4430. if ($answerId === 1) {
  4431. //setting colors
  4432. if ($final_overlap >= $threadhold1) {
  4433. $overlap_color = true; //echo 'a';
  4434. }
  4435. //echo $excess.'-'.$threadhold2;
  4436. if ($final_excess <= $threadhold2) {
  4437. $excess_color = true; //echo 'b';
  4438. }
  4439. //echo '--------'.$missing.'-'.$threadhold3;
  4440. if ($final_missing <= $threadhold3) {
  4441. $missing_color = true; //echo 'c';
  4442. }
  4443. // if pass
  4444. if ($final_overlap >= $threadhold1 &&
  4445. $final_missing <= $threadhold3 &&
  4446. $final_excess <= $threadhold2
  4447. ) {
  4448. $next = 1; //go to the oars
  4449. $result_comment = get_lang('Acceptable');
  4450. $final_answer = 1; // do not update with update_exercise_attempt
  4451. } else {
  4452. $next = 0;
  4453. $result_comment = get_lang('Unacceptable');
  4454. $comment = $answerDestination = $objAnswerTmp->selectComment(1);
  4455. $answerDestination = $objAnswerTmp->selectDestination(1);
  4456. // checking the destination parameters parsing the "@@"
  4457. $destination_items = explode('@@', $answerDestination);
  4458. }
  4459. } elseif ($answerId > 1) {
  4460. if ($objAnswerTmp->selectHotspotType($answerId) == 'noerror') {
  4461. if ($debug > 0) {
  4462. error_log(__LINE__.' - answerId is of type noerror', 0);
  4463. }
  4464. //type no error shouldn't be treated
  4465. $next = 1;
  4466. continue;
  4467. }
  4468. if ($debug > 0) {
  4469. error_log(__LINE__.' - answerId is >1 so we\'re probably in OAR', 0);
  4470. }
  4471. $delineation_cord = $objAnswerTmp->selectHotspotCoordinates($answerId);
  4472. $poly_answer = convert_coordinates($delineation_cord, '|');
  4473. $max_coord = poly_get_max($poly_user, $poly_answer);
  4474. $poly_answer_compiled = poly_compile($poly_answer, $max_coord);
  4475. $overlap = poly_touch($poly_user_compiled, $poly_answer_compiled, $max_coord);
  4476. if ($overlap == false) {
  4477. //all good, no overlap
  4478. $next = 1;
  4479. continue;
  4480. } else {
  4481. if ($debug > 0) {
  4482. error_log(__LINE__.' - Overlap is '.$overlap.': OAR hit', 0);
  4483. }
  4484. $organs_at_risk_hit++;
  4485. //show the feedback
  4486. $next = 0;
  4487. $comment = $answerDestination = $objAnswerTmp->selectComment($answerId);
  4488. $answerDestination = $objAnswerTmp->selectDestination($answerId);
  4489. $destination_items = explode('@@', $answerDestination);
  4490. $try_hotspot = $destination_items[1];
  4491. $lp_hotspot = $destination_items[2];
  4492. $select_question_hotspot = $destination_items[3];
  4493. $url_hotspot = $destination_items[4];
  4494. }
  4495. }
  4496. } else {
  4497. // the first delineation feedback
  4498. if ($debug > 0) {
  4499. error_log(__LINE__.' first', 0);
  4500. }
  4501. }
  4502. } elseif (in_array($answerType, [MATCHING, MATCHING_DRAGGABLE])) {
  4503. echo '<tr>';
  4504. echo Display::tag('td', $answerMatching[$answerId]);
  4505. echo Display::tag(
  4506. 'td',
  4507. "$user_answer / ".Display::tag(
  4508. 'strong',
  4509. $answerMatching[$answerCorrect],
  4510. ['style' => 'color: #008000; font-weight: bold;']
  4511. )
  4512. );
  4513. echo '</tr>';
  4514. } elseif ($answerType == ANNOTATION) {
  4515. ExerciseShowFunctions::displayAnnotationAnswer(
  4516. $feedback_type,
  4517. $exeId,
  4518. $questionId,
  4519. $questionScore,
  4520. $results_disabled
  4521. );
  4522. }
  4523. }
  4524. } else {
  4525. if ($debug) {
  4526. error_log('Showing questions $from '.$from);
  4527. }
  4528. switch ($answerType) {
  4529. case UNIQUE_ANSWER:
  4530. case UNIQUE_ANSWER_IMAGE:
  4531. case UNIQUE_ANSWER_NO_OPTION:
  4532. case MULTIPLE_ANSWER:
  4533. case GLOBAL_MULTIPLE_ANSWER:
  4534. case MULTIPLE_ANSWER_COMBINATION:
  4535. case READING_COMPREHENSION:
  4536. if ($answerId == 1) {
  4537. ExerciseShowFunctions::display_unique_or_multiple_answer(
  4538. $this,
  4539. $feedback_type,
  4540. $answerType,
  4541. $studentChoice,
  4542. $answer,
  4543. $answerComment,
  4544. $answerCorrect,
  4545. $exeId,
  4546. $questionId,
  4547. $answerId,
  4548. $results_disabled,
  4549. $showTotalScoreAndUserChoicesInLastAttempt,
  4550. $this->export
  4551. );
  4552. } else {
  4553. ExerciseShowFunctions::display_unique_or_multiple_answer(
  4554. $this,
  4555. $feedback_type,
  4556. $answerType,
  4557. $studentChoice,
  4558. $answer,
  4559. $answerComment,
  4560. $answerCorrect,
  4561. $exeId,
  4562. $questionId,
  4563. '',
  4564. $results_disabled,
  4565. $showTotalScoreAndUserChoicesInLastAttempt,
  4566. $this->export
  4567. );
  4568. }
  4569. break;
  4570. case MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE:
  4571. if ($answerId == 1) {
  4572. ExerciseShowFunctions::display_multiple_answer_combination_true_false(
  4573. $this,
  4574. $feedback_type,
  4575. $answerType,
  4576. $studentChoice,
  4577. $answer,
  4578. $answerComment,
  4579. $answerCorrect,
  4580. $exeId,
  4581. $questionId,
  4582. $answerId,
  4583. $results_disabled,
  4584. $showTotalScoreAndUserChoicesInLastAttempt
  4585. );
  4586. } else {
  4587. ExerciseShowFunctions::display_multiple_answer_combination_true_false(
  4588. $this,
  4589. $feedback_type,
  4590. $answerType,
  4591. $studentChoice,
  4592. $answer,
  4593. $answerComment,
  4594. $answerCorrect,
  4595. $exeId,
  4596. $questionId,
  4597. '',
  4598. $results_disabled,
  4599. $showTotalScoreAndUserChoicesInLastAttempt
  4600. );
  4601. }
  4602. break;
  4603. case MULTIPLE_ANSWER_TRUE_FALSE:
  4604. if ($answerId == 1) {
  4605. ExerciseShowFunctions::display_multiple_answer_true_false(
  4606. $this,
  4607. $feedback_type,
  4608. $answerType,
  4609. $studentChoice,
  4610. $answer,
  4611. $answerComment,
  4612. $answerCorrect,
  4613. $exeId,
  4614. $questionId,
  4615. $answerId,
  4616. $results_disabled,
  4617. $showTotalScoreAndUserChoicesInLastAttempt
  4618. );
  4619. } else {
  4620. ExerciseShowFunctions::display_multiple_answer_true_false(
  4621. $this,
  4622. $feedback_type,
  4623. $answerType,
  4624. $studentChoice,
  4625. $answer,
  4626. $answerComment,
  4627. $answerCorrect,
  4628. $exeId,
  4629. $questionId,
  4630. '',
  4631. $results_disabled,
  4632. $showTotalScoreAndUserChoicesInLastAttempt
  4633. );
  4634. }
  4635. break;
  4636. case MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY:
  4637. if ($answerId == 1) {
  4638. ExerciseShowFunctions::displayMultipleAnswerTrueFalseDegreeCertainty(
  4639. $feedback_type,
  4640. $studentChoice,
  4641. $studentChoiceDegree,
  4642. $answer,
  4643. $answerComment,
  4644. $answerCorrect,
  4645. $questionId,
  4646. $results_disabled
  4647. );
  4648. } else {
  4649. ExerciseShowFunctions::displayMultipleAnswerTrueFalseDegreeCertainty(
  4650. $feedback_type,
  4651. $studentChoice,
  4652. $studentChoiceDegree,
  4653. $answer,
  4654. $answerComment,
  4655. $answerCorrect,
  4656. $questionId,
  4657. $results_disabled
  4658. );
  4659. }
  4660. break;
  4661. case FILL_IN_BLANKS:
  4662. ExerciseShowFunctions::display_fill_in_blanks_answer(
  4663. $feedback_type,
  4664. $answer,
  4665. $exeId,
  4666. $questionId,
  4667. $results_disabled,
  4668. $str,
  4669. $showTotalScoreAndUserChoicesInLastAttempt
  4670. );
  4671. break;
  4672. case CALCULATED_ANSWER:
  4673. ExerciseShowFunctions::display_calculated_answer(
  4674. $this,
  4675. $feedback_type,
  4676. $answer,
  4677. $exeId,
  4678. $questionId,
  4679. $results_disabled,
  4680. '',
  4681. $showTotalScoreAndUserChoicesInLastAttempt
  4682. );
  4683. break;
  4684. case FREE_ANSWER:
  4685. echo ExerciseShowFunctions::display_free_answer(
  4686. $feedback_type,
  4687. $choice,
  4688. $exeId,
  4689. $questionId,
  4690. $questionScore,
  4691. $results_disabled
  4692. );
  4693. break;
  4694. case ORAL_EXPRESSION:
  4695. echo '<tr>
  4696. <td valign="top">'.
  4697. ExerciseShowFunctions::display_oral_expression_answer(
  4698. $feedback_type,
  4699. $choice,
  4700. $exeId,
  4701. $questionId,
  4702. $objQuestionTmp->getFileUrl(),
  4703. $results_disabled,
  4704. $questionScore
  4705. ).'</td>
  4706. </tr>
  4707. </table>';
  4708. break;
  4709. case HOT_SPOT:
  4710. ExerciseShowFunctions::display_hotspot_answer(
  4711. $feedback_type,
  4712. $answerId,
  4713. $answer,
  4714. $studentChoice,
  4715. $answerComment,
  4716. $results_disabled,
  4717. $answerId,
  4718. $showTotalScoreAndUserChoicesInLastAttempt
  4719. );
  4720. break;
  4721. case HOT_SPOT_DELINEATION:
  4722. $user_answer = $user_array;
  4723. if ($next) {
  4724. $user_answer = $user_array;
  4725. // we compare only the delineation not the other points
  4726. $answer_question = $_SESSION['hotspot_coord'][1];
  4727. $answerDestination = $_SESSION['hotspot_dest'][1];
  4728. // calculating the area
  4729. $poly_user = convert_coordinates($user_answer, '/');
  4730. $poly_answer = convert_coordinates($answer_question, '|');
  4731. $max_coord = poly_get_max($poly_user, $poly_answer);
  4732. $poly_user_compiled = poly_compile($poly_user, $max_coord);
  4733. $poly_answer_compiled = poly_compile($poly_answer, $max_coord);
  4734. $poly_results = poly_result($poly_answer_compiled, $poly_user_compiled, $max_coord);
  4735. $overlap = $poly_results['both'];
  4736. $poly_answer_area = $poly_results['s1'];
  4737. $poly_user_area = $poly_results['s2'];
  4738. $missing = $poly_results['s1Only'];
  4739. $excess = $poly_results['s2Only'];
  4740. if ($debug > 0) {
  4741. error_log(__LINE__.' - Polygons results are '.print_r($poly_results, 1), 0);
  4742. }
  4743. if ($overlap < 1) {
  4744. //shortcut to avoid complicated calculations
  4745. $final_overlap = 0;
  4746. $final_missing = 100;
  4747. $final_excess = 100;
  4748. } else {
  4749. // the final overlap is the percentage of the initial polygon that is overlapped by the user's polygon
  4750. $final_overlap = round(((float) $overlap / (float) $poly_answer_area) * 100);
  4751. if ($debug > 1) {
  4752. error_log(__LINE__.' - Final overlap is '.$final_overlap, 0);
  4753. }
  4754. // the final missing area is the percentage of the initial polygon that is not overlapped by the user's polygon
  4755. $final_missing = 100 - $final_overlap;
  4756. if ($debug > 1) {
  4757. error_log(__LINE__.' - Final missing is '.$final_missing, 0);
  4758. }
  4759. // the final excess area is the percentage of the initial polygon's size that is covered by the user's polygon outside of the initial polygon
  4760. $final_excess = round((((float) $poly_user_area - (float) $overlap) / (float) $poly_answer_area) * 100);
  4761. if ($debug > 1) {
  4762. error_log(__LINE__.' - Final excess is '.$final_excess, 0);
  4763. }
  4764. }
  4765. // Checking the destination parameters parsing the "@@"
  4766. $destination_items = explode('@@', $answerDestination);
  4767. $threadhold_total = $destination_items[0];
  4768. $threadhold_items = explode(';', $threadhold_total);
  4769. $threadhold1 = $threadhold_items[0]; // overlap
  4770. $threadhold2 = $threadhold_items[1]; // excess
  4771. $threadhold3 = $threadhold_items[2]; //missing
  4772. // if is delineation
  4773. if ($answerId === 1) {
  4774. //setting colors
  4775. if ($final_overlap >= $threadhold1) {
  4776. $overlap_color = true; //echo 'a';
  4777. }
  4778. if ($final_excess <= $threadhold2) {
  4779. $excess_color = true; //echo 'b';
  4780. }
  4781. if ($final_missing <= $threadhold3) {
  4782. $missing_color = true; //echo 'c';
  4783. }
  4784. // if pass
  4785. if ($final_overlap >= $threadhold1 &&
  4786. $final_missing <= $threadhold3 &&
  4787. $final_excess <= $threadhold2
  4788. ) {
  4789. $next = 1; //go to the oars
  4790. $result_comment = get_lang('Acceptable');
  4791. $final_answer = 1; // do not update with update_exercise_attempt
  4792. } else {
  4793. $next = 0;
  4794. $result_comment = get_lang('Unacceptable');
  4795. $comment = $answerDestination = $objAnswerTmp->selectComment(1);
  4796. $answerDestination = $objAnswerTmp->selectDestination(1);
  4797. //checking the destination parameters parsing the "@@"
  4798. $destination_items = explode('@@', $answerDestination);
  4799. }
  4800. } elseif ($answerId > 1) {
  4801. if ($objAnswerTmp->selectHotspotType($answerId) == 'noerror') {
  4802. if ($debug > 0) {
  4803. error_log(__LINE__.' - answerId is of type noerror', 0);
  4804. }
  4805. //type no error shouldn't be treated
  4806. $next = 1;
  4807. break;
  4808. }
  4809. if ($debug > 0) {
  4810. error_log(__LINE__.' - answerId is >1 so we\'re probably in OAR', 0);
  4811. }
  4812. $delineation_cord = $objAnswerTmp->selectHotspotCoordinates($answerId);
  4813. $poly_answer = convert_coordinates($delineation_cord, '|');
  4814. $max_coord = poly_get_max($poly_user, $poly_answer);
  4815. $poly_answer_compiled = poly_compile($poly_answer, $max_coord);
  4816. $overlap = poly_touch($poly_user_compiled, $poly_answer_compiled, $max_coord);
  4817. if ($overlap == false) {
  4818. //all good, no overlap
  4819. $next = 1;
  4820. break;
  4821. } else {
  4822. if ($debug > 0) {
  4823. error_log(__LINE__.' - Overlap is '.$overlap.': OAR hit', 0);
  4824. }
  4825. $organs_at_risk_hit++;
  4826. //show the feedback
  4827. $next = 0;
  4828. $comment = $answerDestination = $objAnswerTmp->selectComment($answerId);
  4829. $answerDestination = $objAnswerTmp->selectDestination($answerId);
  4830. $destination_items = explode('@@', $answerDestination);
  4831. $try_hotspot = $destination_items[1];
  4832. $lp_hotspot = $destination_items[2];
  4833. $select_question_hotspot = $destination_items[3];
  4834. $url_hotspot = $destination_items[4];
  4835. }
  4836. }
  4837. } else {
  4838. // the first delineation feedback
  4839. if ($debug > 0) {
  4840. error_log(__LINE__.' first', 0);
  4841. }
  4842. }
  4843. break;
  4844. case HOT_SPOT_ORDER:
  4845. ExerciseShowFunctions::display_hotspot_order_answer(
  4846. $feedback_type,
  4847. $answerId,
  4848. $answer,
  4849. $studentChoice,
  4850. $answerComment
  4851. );
  4852. break;
  4853. case DRAGGABLE:
  4854. case MATCHING_DRAGGABLE:
  4855. case MATCHING:
  4856. echo '<tr>';
  4857. echo Display::tag('td', $answerMatching[$answerId]);
  4858. echo Display::tag(
  4859. 'td',
  4860. "$user_answer / ".Display::tag(
  4861. 'strong',
  4862. $answerMatching[$answerCorrect],
  4863. ['style' => 'color: #008000; font-weight: bold;']
  4864. )
  4865. );
  4866. echo '</tr>';
  4867. break;
  4868. case ANNOTATION:
  4869. ExerciseShowFunctions::displayAnnotationAnswer(
  4870. $feedback_type,
  4871. $exeId,
  4872. $questionId,
  4873. $questionScore,
  4874. $results_disabled
  4875. );
  4876. break;
  4877. }
  4878. }
  4879. }
  4880. if ($debug) {
  4881. error_log(' ------ ');
  4882. }
  4883. } // end for that loops over all answers of the current question
  4884. if ($debug) {
  4885. error_log('-- end answer loop --');
  4886. }
  4887. $final_answer = true;
  4888. foreach ($real_answers as $my_answer) {
  4889. if (!$my_answer) {
  4890. $final_answer = false;
  4891. }
  4892. }
  4893. //we add the total score after dealing with the answers
  4894. if ($answerType == MULTIPLE_ANSWER_COMBINATION ||
  4895. $answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE
  4896. ) {
  4897. if ($final_answer) {
  4898. //getting only the first score where we save the weight of all the question
  4899. $answerWeighting = $objAnswerTmp->selectWeighting(1);
  4900. $questionScore += $answerWeighting;
  4901. $totalScore += $answerWeighting;
  4902. }
  4903. }
  4904. //Fixes multiple answer question in order to be exact
  4905. //if ($answerType == MULTIPLE_ANSWER || $answerType == GLOBAL_MULTIPLE_ANSWER) {
  4906. /* if ($answerType == GLOBAL_MULTIPLE_ANSWER) {
  4907. $diff = @array_diff($answer_correct_array, $real_answers);
  4908. // All good answers or nothing works like exact
  4909. $counter = 1;
  4910. $correct_answer = true;
  4911. foreach ($real_answers as $my_answer) {
  4912. if ($debug)
  4913. error_log(" my_answer: $my_answer answer_correct_array[counter]: ".$answer_correct_array[$counter]);
  4914. if ($my_answer != $answer_correct_array[$counter]) {
  4915. $correct_answer = false;
  4916. break;
  4917. }
  4918. $counter++;
  4919. }
  4920. if ($debug) error_log(" answer_correct_array: ".print_r($answer_correct_array, 1)."");
  4921. if ($debug) error_log(" real_answers: ".print_r($real_answers, 1)."");
  4922. if ($debug) error_log(" correct_answer: ".$correct_answer);
  4923. if ($correct_answer == false) {
  4924. $questionScore = 0;
  4925. }
  4926. // This makes the result non exact
  4927. if (!empty($diff)) {
  4928. $questionScore = 0;
  4929. }
  4930. }*/
  4931. $extra_data = [
  4932. 'final_overlap' => $final_overlap,
  4933. 'final_missing' => $final_missing,
  4934. 'final_excess' => $final_excess,
  4935. 'overlap_color' => $overlap_color,
  4936. 'missing_color' => $missing_color,
  4937. 'excess_color' => $excess_color,
  4938. 'threadhold1' => $threadhold1,
  4939. 'threadhold2' => $threadhold2,
  4940. 'threadhold3' => $threadhold3,
  4941. ];
  4942. if ($from == 'exercise_result') {
  4943. // if answer is hotspot. To the difference of exercise_show.php,
  4944. // we use the results from the session (from_db=0)
  4945. // TODO Change this, because it is wrong to show the user
  4946. // some results that haven't been stored in the database yet
  4947. if ($answerType == HOT_SPOT || $answerType == HOT_SPOT_ORDER || $answerType == HOT_SPOT_DELINEATION) {
  4948. if ($debug) {
  4949. error_log('$from AND this is a hotspot kind of question ');
  4950. }
  4951. $my_exe_id = 0;
  4952. $from_database = 0;
  4953. if ($answerType == HOT_SPOT_DELINEATION) {
  4954. if (0) {
  4955. if ($overlap_color) {
  4956. $overlap_color = 'green';
  4957. } else {
  4958. $overlap_color = 'red';
  4959. }
  4960. if ($missing_color) {
  4961. $missing_color = 'green';
  4962. } else {
  4963. $missing_color = 'red';
  4964. }
  4965. if ($excess_color) {
  4966. $excess_color = 'green';
  4967. } else {
  4968. $excess_color = 'red';
  4969. }
  4970. if (!is_numeric($final_overlap)) {
  4971. $final_overlap = 0;
  4972. }
  4973. if (!is_numeric($final_missing)) {
  4974. $final_missing = 0;
  4975. }
  4976. if (!is_numeric($final_excess)) {
  4977. $final_excess = 0;
  4978. }
  4979. if ($final_overlap > 100) {
  4980. $final_overlap = 100;
  4981. }
  4982. $table_resume = '<table class="data_table">
  4983. <tr class="row_odd" >
  4984. <td></td>
  4985. <td ><b>'.get_lang('Requirements').'</b></td>
  4986. <td><b>'.get_lang('YourAnswer').'</b></td>
  4987. </tr>
  4988. <tr class="row_even">
  4989. <td><b>'.get_lang('Overlap').'</b></td>
  4990. <td>'.get_lang('Min').' '.$threadhold1.'</td>
  4991. <td><div style="color:'.$overlap_color.'">'
  4992. .(($final_overlap < 0) ? 0 : intval($final_overlap)).'</div></td>
  4993. </tr>
  4994. <tr>
  4995. <td><b>'.get_lang('Excess').'</b></td>
  4996. <td>'.get_lang('Max').' '.$threadhold2.'</td>
  4997. <td><div style="color:'.$excess_color.'">'
  4998. .(($final_excess < 0) ? 0 : intval($final_excess)).'</div></td>
  4999. </tr>
  5000. <tr class="row_even">
  5001. <td><b>'.get_lang('Missing').'</b></td>
  5002. <td>'.get_lang('Max').' '.$threadhold3.'</td>
  5003. <td><div style="color:'.$missing_color.'">'
  5004. .(($final_missing < 0) ? 0 : intval($final_missing)).'</div></td>
  5005. </tr>
  5006. </table>';
  5007. if ($next == 0) {
  5008. $try = $try_hotspot;
  5009. $lp = $lp_hotspot;
  5010. $destinationid = $select_question_hotspot;
  5011. $url = $url_hotspot;
  5012. } else {
  5013. //show if no error
  5014. //echo 'no error';
  5015. $comment = $answerComment = $objAnswerTmp->selectComment($nbrAnswers);
  5016. $answerDestination = $objAnswerTmp->selectDestination($nbrAnswers);
  5017. }
  5018. echo '<h1><div style="color:#333;">'.get_lang('Feedback').'</div></h1>
  5019. <p style="text-align:center">';
  5020. $message = '<p>'.get_lang('YourDelineation').'</p>';
  5021. $message .= $table_resume;
  5022. $message .= '<br />'.get_lang('ResultIs').' '.$result_comment.'<br />';
  5023. if ($organs_at_risk_hit > 0) {
  5024. $message .= '<p><b>'.get_lang('OARHit').'</b></p>';
  5025. }
  5026. $message .= '<p>'.$comment.'</p>';
  5027. echo $message;
  5028. } else {
  5029. echo $hotspot_delineation_result[0]; //prints message
  5030. $from_database = 1; // the hotspot_solution.swf needs this variable
  5031. }
  5032. //save the score attempts
  5033. if (1) {
  5034. //getting the answer 1 or 0 comes from exercise_submit_modal.php
  5035. $final_answer = $hotspot_delineation_result[1];
  5036. if ($final_answer == 0) {
  5037. $questionScore = 0;
  5038. }
  5039. // we always insert the answer_id 1 = delineation
  5040. Event::saveQuestionAttempt($questionScore, 1, $quesId, $exeId, 0);
  5041. //in delineation mode, get the answer from $hotspot_delineation_result[1]
  5042. $hotspotValue = (int) $hotspot_delineation_result[1] === 1 ? 1 : 0;
  5043. Event::saveExerciseAttemptHotspot(
  5044. $exeId,
  5045. $quesId,
  5046. 1,
  5047. $hotspotValue,
  5048. $exerciseResultCoordinates[$quesId]
  5049. );
  5050. } else {
  5051. if ($final_answer == 0) {
  5052. $questionScore = 0;
  5053. $answer = 0;
  5054. Event::saveQuestionAttempt($questionScore, $answer, $quesId, $exeId, 0);
  5055. if (is_array($exerciseResultCoordinates[$quesId])) {
  5056. foreach ($exerciseResultCoordinates[$quesId] as $idx => $val) {
  5057. Event::saveExerciseAttemptHotspot(
  5058. $exeId,
  5059. $quesId,
  5060. $idx,
  5061. 0,
  5062. $val
  5063. );
  5064. }
  5065. }
  5066. } else {
  5067. Event::saveQuestionAttempt($questionScore, $answer, $quesId, $exeId, 0);
  5068. if (is_array($exerciseResultCoordinates[$quesId])) {
  5069. foreach ($exerciseResultCoordinates[$quesId] as $idx => $val) {
  5070. $hotspotValue = (int) $choice[$idx] === 1 ? 1 : 0;
  5071. Event::saveExerciseAttemptHotspot(
  5072. $exeId,
  5073. $quesId,
  5074. $idx,
  5075. $hotspotValue,
  5076. $val
  5077. );
  5078. }
  5079. }
  5080. }
  5081. }
  5082. $my_exe_id = $exeId;
  5083. }
  5084. }
  5085. $relPath = api_get_path(WEB_CODE_PATH);
  5086. if ($answerType == HOT_SPOT || $answerType == HOT_SPOT_ORDER) {
  5087. // We made an extra table for the answers
  5088. if ($show_result) {
  5089. echo '</table></td></tr>';
  5090. echo "
  5091. <tr>
  5092. <td colspan=\"2\">
  5093. <p><em>".get_lang('HotSpot')."</em></p>
  5094. <div id=\"hotspot-solution-$questionId\"></div>
  5095. <script>
  5096. $(function() {
  5097. new HotspotQuestion({
  5098. questionId: $questionId,
  5099. exerciseId: {$this->id},
  5100. exeId: $exeId,
  5101. selector: '#hotspot-solution-$questionId',
  5102. for: 'solution',
  5103. relPath: '$relPath'
  5104. });
  5105. });
  5106. </script>
  5107. </td>
  5108. </tr>
  5109. ";
  5110. }
  5111. } elseif ($answerType == ANNOTATION) {
  5112. if ($show_result) {
  5113. echo '
  5114. <p><em>'.get_lang('Annotation').'</em></p>
  5115. <div id="annotation-canvas-'.$questionId.'"></div>
  5116. <script>
  5117. AnnotationQuestion({
  5118. questionId: parseInt('.$questionId.'),
  5119. exerciseId: parseInt('.$exeId.'),
  5120. relPath: \''.$relPath.'\',
  5121. courseId: parseInt('.$course_id.')
  5122. });
  5123. </script>
  5124. ';
  5125. }
  5126. }
  5127. //if ($origin != 'learnpath') {
  5128. if ($show_result && $answerType != ANNOTATION) {
  5129. echo '</table>';
  5130. }
  5131. // }
  5132. }
  5133. unset($objAnswerTmp);
  5134. $totalWeighting += $questionWeighting;
  5135. // Store results directly in the database
  5136. // For all in one page exercises, the results will be
  5137. // stored by exercise_results.php (using the session)
  5138. if ($saved_results) {
  5139. if ($debug) {
  5140. error_log("Save question results $saved_results");
  5141. error_log('choice: ');
  5142. error_log(print_r($choice, 1));
  5143. }
  5144. if (empty($choice)) {
  5145. $choice = 0;
  5146. }
  5147. // with certainty degree
  5148. if (empty($choiceDegreeCertainty)) {
  5149. $choiceDegreeCertainty = 0;
  5150. }
  5151. if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE ||
  5152. $answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE ||
  5153. $answerType == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY
  5154. ) {
  5155. if ($choice != 0) {
  5156. $reply = array_keys($choice);
  5157. $countReply = count($reply);
  5158. for ($i = 0; $i < $countReply; $i++) {
  5159. $chosenAnswer = $reply[$i];
  5160. if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
  5161. if ($choiceDegreeCertainty != 0) {
  5162. $replyDegreeCertainty = array_keys($choiceDegreeCertainty);
  5163. $answerDegreeCertainty = isset($replyDegreeCertainty[$i]) ? $replyDegreeCertainty[$i] : '';
  5164. $answerValue = isset($choiceDegreeCertainty[$answerDegreeCertainty]) ? $choiceDegreeCertainty[$answerDegreeCertainty] : '';
  5165. Event::saveQuestionAttempt(
  5166. $questionScore,
  5167. $chosenAnswer.':'.$choice[$chosenAnswer].':'.$answerValue,
  5168. $quesId,
  5169. $exeId,
  5170. $i,
  5171. $this->id,
  5172. $updateResults
  5173. );
  5174. }
  5175. } else {
  5176. Event::saveQuestionAttempt(
  5177. $questionScore,
  5178. $chosenAnswer.':'.$choice[$chosenAnswer],
  5179. $quesId,
  5180. $exeId,
  5181. $i,
  5182. $this->id,
  5183. $updateResults
  5184. );
  5185. }
  5186. if ($debug) {
  5187. error_log('result =>'.$questionScore.' '.$chosenAnswer.':'.$choice[$chosenAnswer]);
  5188. }
  5189. }
  5190. } else {
  5191. Event::saveQuestionAttempt(
  5192. $questionScore,
  5193. 0,
  5194. $quesId,
  5195. $exeId,
  5196. 0,
  5197. $this->id
  5198. );
  5199. }
  5200. } elseif ($answerType == MULTIPLE_ANSWER || $answerType == GLOBAL_MULTIPLE_ANSWER) {
  5201. if ($choice != 0) {
  5202. $reply = array_keys($choice);
  5203. if ($debug) {
  5204. error_log("reply ".print_r($reply, 1)."");
  5205. }
  5206. for ($i = 0; $i < sizeof($reply); $i++) {
  5207. $ans = $reply[$i];
  5208. Event::saveQuestionAttempt($questionScore, $ans, $quesId, $exeId, $i, $this->id);
  5209. }
  5210. } else {
  5211. Event::saveQuestionAttempt($questionScore, 0, $quesId, $exeId, 0, $this->id);
  5212. }
  5213. } elseif ($answerType == MULTIPLE_ANSWER_COMBINATION) {
  5214. if ($choice != 0) {
  5215. $reply = array_keys($choice);
  5216. for ($i = 0; $i < sizeof($reply); $i++) {
  5217. $ans = $reply[$i];
  5218. Event::saveQuestionAttempt($questionScore, $ans, $quesId, $exeId, $i, $this->id);
  5219. }
  5220. } else {
  5221. Event::saveQuestionAttempt(
  5222. $questionScore,
  5223. 0,
  5224. $quesId,
  5225. $exeId,
  5226. 0,
  5227. $this->id
  5228. );
  5229. }
  5230. } elseif (in_array($answerType, [MATCHING, DRAGGABLE, MATCHING_DRAGGABLE])) {
  5231. if (isset($matching)) {
  5232. foreach ($matching as $j => $val) {
  5233. Event::saveQuestionAttempt(
  5234. $questionScore,
  5235. $val,
  5236. $quesId,
  5237. $exeId,
  5238. $j,
  5239. $this->id
  5240. );
  5241. }
  5242. }
  5243. } elseif ($answerType == FREE_ANSWER) {
  5244. $answer = $choice;
  5245. Event::saveQuestionAttempt(
  5246. $questionScore,
  5247. $answer,
  5248. $quesId,
  5249. $exeId,
  5250. 0,
  5251. $this->id
  5252. );
  5253. } elseif ($answerType == ORAL_EXPRESSION) {
  5254. $answer = $choice;
  5255. Event::saveQuestionAttempt(
  5256. $questionScore,
  5257. $answer,
  5258. $quesId,
  5259. $exeId,
  5260. 0,
  5261. $this->id,
  5262. false,
  5263. $objQuestionTmp->getAbsoluteFilePath()
  5264. );
  5265. } elseif (
  5266. in_array(
  5267. $answerType,
  5268. [UNIQUE_ANSWER, UNIQUE_ANSWER_IMAGE, UNIQUE_ANSWER_NO_OPTION, READING_COMPREHENSION]
  5269. )
  5270. ) {
  5271. $answer = $choice;
  5272. Event::saveQuestionAttempt($questionScore, $answer, $quesId, $exeId, 0, $this->id);
  5273. } elseif ($answerType == HOT_SPOT || $answerType == ANNOTATION) {
  5274. $answer = [];
  5275. if (isset($exerciseResultCoordinates[$questionId]) && !empty($exerciseResultCoordinates[$questionId])) {
  5276. if ($debug) {
  5277. error_log('Checking result coordinates');
  5278. }
  5279. Database::delete(
  5280. Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT),
  5281. [
  5282. 'hotspot_exe_id = ? AND hotspot_question_id = ? AND c_id = ?' => [
  5283. $exeId,
  5284. $questionId,
  5285. api_get_course_int_id(),
  5286. ],
  5287. ]
  5288. );
  5289. foreach ($exerciseResultCoordinates[$questionId] as $idx => $val) {
  5290. $answer[] = $val;
  5291. $hotspotValue = (int) $choice[$idx] === 1 ? 1 : 0;
  5292. if ($debug) {
  5293. error_log('Hotspot value: '.$hotspotValue);
  5294. }
  5295. Event::saveExerciseAttemptHotspot(
  5296. $exeId,
  5297. $quesId,
  5298. $idx,
  5299. $hotspotValue,
  5300. $val,
  5301. false,
  5302. $this->id
  5303. );
  5304. }
  5305. } else {
  5306. if ($debug) {
  5307. error_log('Empty: exerciseResultCoordinates');
  5308. }
  5309. }
  5310. Event::saveQuestionAttempt($questionScore, implode('|', $answer), $quesId, $exeId, 0, $this->id);
  5311. } else {
  5312. Event::saveQuestionAttempt($questionScore, $answer, $quesId, $exeId, 0, $this->id);
  5313. }
  5314. }
  5315. if ($propagate_neg == 0 && $questionScore < 0) {
  5316. $questionScore = 0;
  5317. }
  5318. if ($saved_results) {
  5319. $statsTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  5320. $sql = "UPDATE $statsTable SET
  5321. exe_result = exe_result + ".floatval($questionScore)."
  5322. WHERE exe_id = $exeId";
  5323. Database::query($sql);
  5324. if ($debug) {
  5325. error_log($sql);
  5326. }
  5327. }
  5328. $return = [
  5329. 'score' => $questionScore,
  5330. 'weight' => $questionWeighting,
  5331. 'extra' => $extra_data,
  5332. 'open_question' => $arrques,
  5333. 'open_answer' => $arrans,
  5334. 'answer_type' => $answerType,
  5335. 'generated_oral_file' => $generatedFile,
  5336. 'user_answered' => $userAnsweredQuestion,
  5337. ];
  5338. return $return;
  5339. }
  5340. /**
  5341. * Sends a notification when a user ends an examn.
  5342. *
  5343. * @param string $type 'start' or 'end' of an exercise
  5344. * @param array $question_list_answers
  5345. * @param string $origin
  5346. * @param int $exe_id
  5347. * @param float $score
  5348. * @param float $weight
  5349. *
  5350. * @return bool
  5351. */
  5352. public function send_mail_notification_for_exam(
  5353. $type = 'end',
  5354. $question_list_answers,
  5355. $origin,
  5356. $exe_id,
  5357. $score = null,
  5358. $weight = null
  5359. ) {
  5360. $setting = api_get_course_setting('email_alert_manager_on_new_quiz');
  5361. if (empty($setting) && empty($this->getNotifications())) {
  5362. return false;
  5363. }
  5364. $settingFromExercise = $this->getNotifications();
  5365. if (!empty($settingFromExercise)) {
  5366. $setting = $settingFromExercise;
  5367. }
  5368. // Email configuration settings
  5369. $courseCode = api_get_course_id();
  5370. $courseInfo = api_get_course_info($courseCode);
  5371. if (empty($courseInfo)) {
  5372. return false;
  5373. }
  5374. $sessionId = api_get_session_id();
  5375. $sessionData = '';
  5376. if (!empty($sessionId)) {
  5377. $sessionInfo = api_get_session_info($sessionId);
  5378. if (!empty($sessionInfo)) {
  5379. $sessionData = '<tr>'
  5380. .'<td>'.get_lang('SessionName').'</td>'
  5381. .'<td>'.$sessionInfo['name'].'</td>'
  5382. .'</tr>';
  5383. }
  5384. }
  5385. $sendStart = false;
  5386. $sendEnd = false;
  5387. $sendEndOpenQuestion = false;
  5388. $sendEndOralQuestion = false;
  5389. foreach ($setting as $option) {
  5390. switch ($option) {
  5391. case 0:
  5392. return false;
  5393. break;
  5394. case 1: // End
  5395. if ($type == 'end') {
  5396. $sendEnd = true;
  5397. }
  5398. break;
  5399. case 2: // start
  5400. if ($type == 'start') {
  5401. $sendStart = true;
  5402. }
  5403. break;
  5404. case 3: // end + open
  5405. if ($type == 'end') {
  5406. $sendEndOpenQuestion = true;
  5407. }
  5408. break;
  5409. case 4: // end + oral
  5410. if ($type == 'end') {
  5411. $sendEndOralQuestion = true;
  5412. }
  5413. break;
  5414. }
  5415. }
  5416. $user_info = api_get_user_info(api_get_user_id());
  5417. $url = api_get_path(WEB_CODE_PATH).'exercise/exercise_show.php?'.
  5418. api_get_cidreq(true, true, 'qualify').'&id='.$exe_id.'&action=qualify';
  5419. if (!empty($sessionId)) {
  5420. $addGeneralCoach = true;
  5421. $setting = api_get_configuration_value('block_quiz_mail_notification_general_coach');
  5422. if ($setting === true) {
  5423. $addGeneralCoach = false;
  5424. }
  5425. $teachers = CourseManager::get_coach_list_from_course_code(
  5426. $courseCode,
  5427. $sessionId,
  5428. $addGeneralCoach
  5429. );
  5430. } else {
  5431. $teachers = CourseManager::get_teacher_list_from_course_code($courseCode);
  5432. }
  5433. if ($sendEndOpenQuestion) {
  5434. $this->sendNotificationForOpenQuestions(
  5435. $question_list_answers,
  5436. $origin,
  5437. $user_info,
  5438. $url,
  5439. $teachers
  5440. );
  5441. }
  5442. if ($sendEndOralQuestion) {
  5443. $this->sendNotificationForOralQuestions(
  5444. $question_list_answers,
  5445. $origin,
  5446. $exe_id,
  5447. $user_info,
  5448. $url,
  5449. $teachers
  5450. );
  5451. }
  5452. if (!$sendEnd && !$sendStart) {
  5453. return false;
  5454. }
  5455. $scoreLabel = '';
  5456. if ($sendEnd &&
  5457. api_get_configuration_value('send_score_in_exam_notification_mail_to_manager') == true
  5458. ) {
  5459. $notificationPercentage = api_get_configuration_value('send_notification_score_in_percentage');
  5460. $scoreLabel = ExerciseLib::show_score($score, $weight, $notificationPercentage, true);
  5461. $scoreLabel = "<tr>
  5462. <td>".get_lang('Score')."</td>
  5463. <td>&nbsp;$scoreLabel</td>
  5464. </tr>";
  5465. }
  5466. if ($sendEnd) {
  5467. $msg = get_lang('ExerciseAttempted').'<br /><br />';
  5468. } else {
  5469. $msg = get_lang('StudentStartExercise').'<br /><br />';
  5470. }
  5471. $msg .= get_lang('AttemptDetails').' : <br /><br />
  5472. <table>
  5473. <tr>
  5474. <td>'.get_lang('CourseName').'</td>
  5475. <td>#course#</td>
  5476. </tr>
  5477. '.$sessionData.'
  5478. <tr>
  5479. <td>'.get_lang('Exercise').'</td>
  5480. <td>&nbsp;#exercise#</td>
  5481. </tr>
  5482. <tr>
  5483. <td>'.get_lang('StudentName').'</td>
  5484. <td>&nbsp;#student_complete_name#</td>
  5485. </tr>
  5486. <tr>
  5487. <td>'.get_lang('StudentEmail').'</td>
  5488. <td>&nbsp;#email#</td>
  5489. </tr>
  5490. '.$scoreLabel.'
  5491. </table>';
  5492. $variables = [
  5493. '#email#' => $user_info['email'],
  5494. '#exercise#' => $this->exercise,
  5495. '#student_complete_name#' => $user_info['complete_name'],
  5496. '#course#' => Display::url(
  5497. $courseInfo['title'],
  5498. $courseInfo['course_public_url'].'?id_session='.$sessionId
  5499. ),
  5500. ];
  5501. if ($sendEnd) {
  5502. $msg .= '<br /><a href="#url#">'.get_lang('ClickToCommentAndGiveFeedback').'</a>';
  5503. $variables['#url#'] = $url;
  5504. }
  5505. $content = str_replace(array_keys($variables), array_values($variables), $msg);
  5506. if ($sendEnd) {
  5507. $subject = get_lang('ExerciseAttempted');
  5508. } else {
  5509. $subject = get_lang('StudentStartExercise');
  5510. }
  5511. if (!empty($teachers)) {
  5512. foreach ($teachers as $user_id => $teacher_data) {
  5513. MessageManager::send_message_simple(
  5514. $user_id,
  5515. $subject,
  5516. $content
  5517. );
  5518. }
  5519. }
  5520. }
  5521. /**
  5522. * @param array $user_data result of api_get_user_info()
  5523. * @param array $trackExerciseInfo result of get_stat_track_exercise_info
  5524. *
  5525. * @return string
  5526. */
  5527. public function showExerciseResultHeader(
  5528. $user_data,
  5529. $trackExerciseInfo
  5530. ) {
  5531. if (api_get_configuration_value('hide_user_info_in_quiz_result')) {
  5532. return '';
  5533. }
  5534. $start_date = null;
  5535. if (isset($trackExerciseInfo['start_date'])) {
  5536. $start_date = api_convert_and_format_date($trackExerciseInfo['start_date']);
  5537. }
  5538. $duration = isset($trackExerciseInfo['duration_formatted']) ? $trackExerciseInfo['duration_formatted'] : null;
  5539. $ip = isset($trackExerciseInfo['user_ip']) ? $trackExerciseInfo['user_ip'] : null;
  5540. if (!empty($user_data)) {
  5541. $userFullName = $user_data['complete_name'];
  5542. if (api_is_teacher() || api_is_platform_admin(true, true)) {
  5543. $userFullName = '<a href="'.$user_data['profile_url'].'" title="'.get_lang('GoToStudentDetails').'">'.
  5544. $user_data['complete_name'].'</a>';
  5545. }
  5546. $data = [
  5547. 'name_url' => $userFullName,
  5548. 'complete_name' => $user_data['complete_name'],
  5549. 'username' => $user_data['username'],
  5550. 'avatar' => $user_data['avatar_medium'],
  5551. 'url' => $user_data['profile_url'],
  5552. ];
  5553. if (!empty($user_data['official_code'])) {
  5554. $data['code'] = $user_data['official_code'];
  5555. }
  5556. }
  5557. // Description can be very long and is generally meant to explain
  5558. // rules *before* the exam. Leaving here to make display easier if
  5559. // necessary
  5560. /*
  5561. if (!empty($this->description)) {
  5562. $array[] = array('title' => get_lang("Description"), 'content' => $this->description);
  5563. }
  5564. */
  5565. if (!empty($start_date)) {
  5566. $data['start_date'] = $start_date;
  5567. }
  5568. if (!empty($duration)) {
  5569. $data['duration'] = $duration;
  5570. }
  5571. if (!empty($ip)) {
  5572. $data['ip'] = $ip;
  5573. }
  5574. if (api_get_configuration_value('save_titles_as_html')) {
  5575. $data['title'] = $this->get_formated_title().get_lang('Result');
  5576. } else {
  5577. $data['title'] = PHP_EOL.$this->exercise.' : '.get_lang('Result');
  5578. }
  5579. $tpl = new Template(null, false, false, false, false, false, false);
  5580. $tpl->assign('data', $data);
  5581. $layoutTemplate = $tpl->get_template('exercise/partials/result_exercise.tpl');
  5582. $content = $tpl->fetch($layoutTemplate);
  5583. return $content;
  5584. }
  5585. /**
  5586. * Returns the exercise result.
  5587. *
  5588. * @param int attempt id
  5589. *
  5590. * @return array
  5591. */
  5592. public function get_exercise_result($exe_id)
  5593. {
  5594. $result = [];
  5595. $track_exercise_info = ExerciseLib::get_exercise_track_exercise_info($exe_id);
  5596. if (!empty($track_exercise_info)) {
  5597. $totalScore = 0;
  5598. $objExercise = new Exercise();
  5599. $objExercise->read($track_exercise_info['exe_exo_id']);
  5600. if (!empty($track_exercise_info['data_tracking'])) {
  5601. $question_list = explode(',', $track_exercise_info['data_tracking']);
  5602. }
  5603. foreach ($question_list as $questionId) {
  5604. $question_result = $objExercise->manage_answer(
  5605. $exe_id,
  5606. $questionId,
  5607. '',
  5608. 'exercise_show',
  5609. [],
  5610. false,
  5611. true,
  5612. false,
  5613. $objExercise->selectPropagateNeg()
  5614. );
  5615. $totalScore += $question_result['score'];
  5616. }
  5617. if ($objExercise->selectPropagateNeg() == 0 && $totalScore < 0) {
  5618. $totalScore = 0;
  5619. }
  5620. $result = [
  5621. 'score' => $totalScore,
  5622. 'weight' => $track_exercise_info['exe_weighting'],
  5623. ];
  5624. }
  5625. return $result;
  5626. }
  5627. /**
  5628. * Checks if the exercise is visible due a lot of conditions
  5629. * visibility, time limits, student attempts
  5630. * Return associative array
  5631. * value : true if exercise visible
  5632. * message : HTML formatted message
  5633. * rawMessage : text message.
  5634. *
  5635. * @param int $lpId
  5636. * @param int $lpItemId
  5637. * @param int $lpItemViewId
  5638. * @param bool $filterByAdmin
  5639. *
  5640. * @return array
  5641. */
  5642. public function is_visible(
  5643. $lpId = 0,
  5644. $lpItemId = 0,
  5645. $lpItemViewId = 0,
  5646. $filterByAdmin = true
  5647. ) {
  5648. // 1. By default the exercise is visible
  5649. $isVisible = true;
  5650. $message = null;
  5651. // 1.1 Admins and teachers can access to the exercise
  5652. if ($filterByAdmin) {
  5653. if (api_is_platform_admin() || api_is_course_admin()) {
  5654. return ['value' => true, 'message' => ''];
  5655. }
  5656. }
  5657. // Deleted exercise.
  5658. if ($this->active == -1) {
  5659. return [
  5660. 'value' => false,
  5661. 'message' => Display::return_message(
  5662. get_lang('ExerciseNotFound'),
  5663. 'warning',
  5664. false
  5665. ),
  5666. 'rawMessage' => get_lang('ExerciseNotFound'),
  5667. ];
  5668. }
  5669. // Checking visibility in the item_property table.
  5670. $visibility = api_get_item_visibility(
  5671. api_get_course_info(),
  5672. TOOL_QUIZ,
  5673. $this->id,
  5674. api_get_session_id()
  5675. );
  5676. if ($visibility == 0 || $visibility == 2) {
  5677. $this->active = 0;
  5678. }
  5679. // 2. If the exercise is not active.
  5680. if (empty($lpId)) {
  5681. // 2.1 LP is OFF
  5682. if ($this->active == 0) {
  5683. return [
  5684. 'value' => false,
  5685. 'message' => Display::return_message(
  5686. get_lang('ExerciseNotFound'),
  5687. 'warning',
  5688. false
  5689. ),
  5690. 'rawMessage' => get_lang('ExerciseNotFound'),
  5691. ];
  5692. }
  5693. } else {
  5694. // 2.1 LP is loaded
  5695. if ($this->active == 0 &&
  5696. !learnpath::is_lp_visible_for_student($lpId, api_get_user_id())
  5697. ) {
  5698. return [
  5699. 'value' => false,
  5700. 'message' => Display::return_message(
  5701. get_lang('ExerciseNotFound'),
  5702. 'warning',
  5703. false
  5704. ),
  5705. 'rawMessage' => get_lang('ExerciseNotFound'),
  5706. ];
  5707. }
  5708. }
  5709. // 3. We check if the time limits are on
  5710. $limitTimeExists = false;
  5711. if (!empty($this->start_time) || !empty($this->end_time)) {
  5712. $limitTimeExists = true;
  5713. }
  5714. if ($limitTimeExists) {
  5715. $timeNow = time();
  5716. $existsStartDate = false;
  5717. $nowIsAfterStartDate = true;
  5718. $existsEndDate = false;
  5719. $nowIsBeforeEndDate = true;
  5720. if (!empty($this->start_time)) {
  5721. $existsStartDate = true;
  5722. }
  5723. if (!empty($this->end_time)) {
  5724. $existsEndDate = true;
  5725. }
  5726. // check if we are before-or-after end-or-start date
  5727. if ($existsStartDate && $timeNow < api_strtotime($this->start_time, 'UTC')) {
  5728. $nowIsAfterStartDate = false;
  5729. }
  5730. if ($existsEndDate & $timeNow >= api_strtotime($this->end_time, 'UTC')) {
  5731. $nowIsBeforeEndDate = false;
  5732. }
  5733. // lets check all cases
  5734. if ($existsStartDate && !$existsEndDate) {
  5735. // exists start date and dont exists end date
  5736. if ($nowIsAfterStartDate) {
  5737. // after start date, no end date
  5738. $isVisible = true;
  5739. $message = sprintf(
  5740. get_lang('ExerciseAvailableSinceX'),
  5741. api_convert_and_format_date($this->start_time)
  5742. );
  5743. } else {
  5744. // before start date, no end date
  5745. $isVisible = false;
  5746. $message = sprintf(
  5747. get_lang('ExerciseAvailableFromX'),
  5748. api_convert_and_format_date($this->start_time)
  5749. );
  5750. }
  5751. } elseif (!$existsStartDate && $existsEndDate) {
  5752. // doesnt exist start date, exists end date
  5753. if ($nowIsBeforeEndDate) {
  5754. // before end date, no start date
  5755. $isVisible = true;
  5756. $message = sprintf(
  5757. get_lang('ExerciseAvailableUntilX'),
  5758. api_convert_and_format_date($this->end_time)
  5759. );
  5760. } else {
  5761. // after end date, no start date
  5762. $isVisible = false;
  5763. $message = sprintf(
  5764. get_lang('ExerciseAvailableUntilX'),
  5765. api_convert_and_format_date($this->end_time)
  5766. );
  5767. }
  5768. } elseif ($existsStartDate && $existsEndDate) {
  5769. // exists start date and end date
  5770. if ($nowIsAfterStartDate) {
  5771. if ($nowIsBeforeEndDate) {
  5772. // after start date and before end date
  5773. $isVisible = true;
  5774. $message = sprintf(
  5775. get_lang('ExerciseIsActivatedFromXToY'),
  5776. api_convert_and_format_date($this->start_time),
  5777. api_convert_and_format_date($this->end_time)
  5778. );
  5779. } else {
  5780. // after start date and after end date
  5781. $isVisible = false;
  5782. $message = sprintf(
  5783. get_lang('ExerciseWasActivatedFromXToY'),
  5784. api_convert_and_format_date($this->start_time),
  5785. api_convert_and_format_date($this->end_time)
  5786. );
  5787. }
  5788. } else {
  5789. if ($nowIsBeforeEndDate) {
  5790. // before start date and before end date
  5791. $isVisible = false;
  5792. $message = sprintf(
  5793. get_lang('ExerciseWillBeActivatedFromXToY'),
  5794. api_convert_and_format_date($this->start_time),
  5795. api_convert_and_format_date($this->end_time)
  5796. );
  5797. }
  5798. // case before start date and after end date is impossible
  5799. }
  5800. } elseif (!$existsStartDate && !$existsEndDate) {
  5801. // doesnt exist start date nor end date
  5802. $isVisible = true;
  5803. $message = '';
  5804. }
  5805. }
  5806. // 4. We check if the student have attempts
  5807. if ($isVisible) {
  5808. $exerciseAttempts = $this->selectAttempts();
  5809. if ($exerciseAttempts > 0) {
  5810. $attemptCount = Event::get_attempt_count_not_finished(
  5811. api_get_user_id(),
  5812. $this->id,
  5813. $lpId,
  5814. $lpItemId,
  5815. $lpItemViewId
  5816. );
  5817. if ($attemptCount >= $exerciseAttempts) {
  5818. $message = sprintf(
  5819. get_lang('ReachedMaxAttempts'),
  5820. $this->name,
  5821. $exerciseAttempts
  5822. );
  5823. $isVisible = false;
  5824. }
  5825. }
  5826. }
  5827. $rawMessage = '';
  5828. if (!empty($message)) {
  5829. $rawMessage = $message;
  5830. $message = Display::return_message($message, 'warning', false);
  5831. }
  5832. return [
  5833. 'value' => $isVisible,
  5834. 'message' => $message,
  5835. 'rawMessage' => $rawMessage,
  5836. ];
  5837. }
  5838. /**
  5839. * @return bool
  5840. */
  5841. public function added_in_lp()
  5842. {
  5843. $TBL_LP_ITEM = Database::get_course_table(TABLE_LP_ITEM);
  5844. $sql = "SELECT max_score FROM $TBL_LP_ITEM
  5845. WHERE
  5846. c_id = {$this->course_id} AND
  5847. item_type = '".TOOL_QUIZ."' AND
  5848. path = '{$this->id}'";
  5849. $result = Database::query($sql);
  5850. if (Database::num_rows($result) > 0) {
  5851. return true;
  5852. }
  5853. return false;
  5854. }
  5855. /**
  5856. * Returns an array with this form.
  5857. *
  5858. * @example
  5859. * <code>
  5860. * array (size=3)
  5861. * 999 =>
  5862. * array (size=3)
  5863. * 0 => int 3422
  5864. * 1 => int 3423
  5865. * 2 => int 3424
  5866. * 100 =>
  5867. * array (size=2)
  5868. * 0 => int 3469
  5869. * 1 => int 3470
  5870. * 101 =>
  5871. * array (size=1)
  5872. * 0 => int 3482
  5873. * </code>
  5874. * The array inside the key 999 means the question list that belongs to the media id = 999,
  5875. * this case is special because 999 means "no media".
  5876. *
  5877. * @return array
  5878. */
  5879. public function getMediaList()
  5880. {
  5881. return $this->mediaList;
  5882. }
  5883. /**
  5884. * Is media question activated?
  5885. *
  5886. * @return bool
  5887. */
  5888. public function mediaIsActivated()
  5889. {
  5890. $mediaQuestions = $this->getMediaList();
  5891. $active = false;
  5892. if (isset($mediaQuestions) && !empty($mediaQuestions)) {
  5893. $media_count = count($mediaQuestions);
  5894. if ($media_count > 1) {
  5895. return true;
  5896. } elseif ($media_count == 1) {
  5897. if (isset($mediaQuestions[999])) {
  5898. return false;
  5899. } else {
  5900. return true;
  5901. }
  5902. }
  5903. }
  5904. return $active;
  5905. }
  5906. /**
  5907. * Gets question list from the exercise.
  5908. *
  5909. * @return array
  5910. */
  5911. public function getQuestionList()
  5912. {
  5913. return $this->questionList;
  5914. }
  5915. /**
  5916. * Question list with medias compressed like this.
  5917. *
  5918. * @example
  5919. * <code>
  5920. * array(
  5921. * question_id_1,
  5922. * question_id_2,
  5923. * media_id, <- this media id contains question ids
  5924. * question_id_3,
  5925. * )
  5926. * </code>
  5927. *
  5928. * @return array
  5929. */
  5930. public function getQuestionListWithMediasCompressed()
  5931. {
  5932. return $this->questionList;
  5933. }
  5934. /**
  5935. * Question list with medias uncompressed like this.
  5936. *
  5937. * @example
  5938. * <code>
  5939. * array(
  5940. * question_id,
  5941. * question_id,
  5942. * question_id, <- belongs to a media id
  5943. * question_id, <- belongs to a media id
  5944. * question_id,
  5945. * )
  5946. * </code>
  5947. *
  5948. * @return array
  5949. */
  5950. public function getQuestionListWithMediasUncompressed()
  5951. {
  5952. return $this->questionListUncompressed;
  5953. }
  5954. /**
  5955. * Sets the question list when the exercise->read() is executed.
  5956. *
  5957. * @param bool $adminView Whether to view the set the list of *all* questions or just the normal student view
  5958. */
  5959. public function setQuestionList($adminView = false)
  5960. {
  5961. // Getting question list.
  5962. $questionList = $this->selectQuestionList(true, $adminView);
  5963. $this->setMediaList($questionList);
  5964. $this->questionList = $this->transformQuestionListWithMedias(
  5965. $questionList,
  5966. false
  5967. );
  5968. $this->questionListUncompressed = $this->transformQuestionListWithMedias(
  5969. $questionList,
  5970. true
  5971. );
  5972. }
  5973. /**
  5974. * @params array question list
  5975. * @params bool expand or not question list (true show all questions,
  5976. * false show media question id instead of the question ids)
  5977. */
  5978. public function transformQuestionListWithMedias(
  5979. $question_list,
  5980. $expand_media_questions = false
  5981. ) {
  5982. $new_question_list = [];
  5983. if (!empty($question_list)) {
  5984. $media_questions = $this->getMediaList();
  5985. $media_active = $this->mediaIsActivated($media_questions);
  5986. if ($media_active) {
  5987. $counter = 1;
  5988. foreach ($question_list as $question_id) {
  5989. $add_question = true;
  5990. foreach ($media_questions as $media_id => $question_list_in_media) {
  5991. if ($media_id != 999 && in_array($question_id, $question_list_in_media)) {
  5992. $add_question = false;
  5993. if (!in_array($media_id, $new_question_list)) {
  5994. $new_question_list[$counter] = $media_id;
  5995. $counter++;
  5996. }
  5997. break;
  5998. }
  5999. }
  6000. if ($add_question) {
  6001. $new_question_list[$counter] = $question_id;
  6002. $counter++;
  6003. }
  6004. }
  6005. if ($expand_media_questions) {
  6006. $media_key_list = array_keys($media_questions);
  6007. foreach ($new_question_list as &$question_id) {
  6008. if (in_array($question_id, $media_key_list)) {
  6009. $question_id = $media_questions[$question_id];
  6010. }
  6011. }
  6012. $new_question_list = array_flatten($new_question_list);
  6013. }
  6014. } else {
  6015. $new_question_list = $question_list;
  6016. }
  6017. }
  6018. return $new_question_list;
  6019. }
  6020. /**
  6021. * Get question list depend on the random settings.
  6022. *
  6023. * @return array
  6024. */
  6025. public function get_validated_question_list()
  6026. {
  6027. $isRandomByCategory = $this->isRandomByCat();
  6028. if ($isRandomByCategory == 0) {
  6029. if ($this->isRandom()) {
  6030. return $this->getRandomList();
  6031. }
  6032. return $this->selectQuestionList();
  6033. }
  6034. if ($this->isRandom()) {
  6035. // USE question categories
  6036. // get questions by category for this exercise
  6037. // we have to choice $objExercise->random question in each array values of $tabCategoryQuestions
  6038. // key of $tabCategoryQuestions are the categopy id (0 for not in a category)
  6039. // value is the array of question id of this category
  6040. $questionList = [];
  6041. $tabCategoryQuestions = TestCategory::getQuestionsByCat($this->id);
  6042. $isRandomByCategory = $this->getRandomByCategory();
  6043. // We sort categories based on the term between [] in the head
  6044. // of the category's description
  6045. /* examples of categories :
  6046. * [biologie] Maitriser les mecanismes de base de la genetique
  6047. * [biologie] Relier les moyens de depenses et les agents infectieux
  6048. * [biologie] Savoir ou est produite l'enrgie dans les cellules et sous quelle forme
  6049. * [chimie] Classer les molles suivant leur pouvoir oxydant ou reacteur
  6050. * [chimie] Connaître la denition de la theoie acide/base selon Brönsted
  6051. * [chimie] Connaître les charges des particules
  6052. * We want that in the order of the groups defined by the term
  6053. * between brackets at the beginning of the category title
  6054. */
  6055. // If test option is Grouped By Categories
  6056. if ($isRandomByCategory == 2) {
  6057. $tabCategoryQuestions = TestCategory::sortTabByBracketLabel($tabCategoryQuestions);
  6058. }
  6059. foreach ($tabCategoryQuestions as $tabquestion) {
  6060. $number_of_random_question = $this->random;
  6061. if ($this->random == -1) {
  6062. $number_of_random_question = count($this->questionList);
  6063. }
  6064. $questionList = array_merge(
  6065. $questionList,
  6066. TestCategory::getNElementsFromArray(
  6067. $tabquestion,
  6068. $number_of_random_question
  6069. )
  6070. );
  6071. }
  6072. // shuffle the question list if test is not grouped by categories
  6073. if ($isRandomByCategory == 1) {
  6074. shuffle($questionList); // or not
  6075. }
  6076. return $questionList;
  6077. }
  6078. // Problem, random by category has been selected and
  6079. // we have no $this->isRandom number of question selected
  6080. // Should not happened
  6081. return [];
  6082. }
  6083. public function get_question_list($expand_media_questions = false)
  6084. {
  6085. $question_list = $this->get_validated_question_list();
  6086. $question_list = $this->transform_question_list_with_medias($question_list, $expand_media_questions);
  6087. return $question_list;
  6088. }
  6089. public function transform_question_list_with_medias($question_list, $expand_media_questions = false)
  6090. {
  6091. $new_question_list = [];
  6092. if (!empty($question_list)) {
  6093. $media_questions = $this->getMediaList();
  6094. $media_active = $this->mediaIsActivated($media_questions);
  6095. if ($media_active) {
  6096. $counter = 1;
  6097. foreach ($question_list as $question_id) {
  6098. $add_question = true;
  6099. foreach ($media_questions as $media_id => $question_list_in_media) {
  6100. if ($media_id != 999 && in_array($question_id, $question_list_in_media)) {
  6101. $add_question = false;
  6102. if (!in_array($media_id, $new_question_list)) {
  6103. $new_question_list[$counter] = $media_id;
  6104. $counter++;
  6105. }
  6106. break;
  6107. }
  6108. }
  6109. if ($add_question) {
  6110. $new_question_list[$counter] = $question_id;
  6111. $counter++;
  6112. }
  6113. }
  6114. if ($expand_media_questions) {
  6115. $media_key_list = array_keys($media_questions);
  6116. foreach ($new_question_list as &$question_id) {
  6117. if (in_array($question_id, $media_key_list)) {
  6118. $question_id = $media_questions[$question_id];
  6119. }
  6120. }
  6121. $new_question_list = array_flatten($new_question_list);
  6122. }
  6123. } else {
  6124. $new_question_list = $question_list;
  6125. }
  6126. }
  6127. return $new_question_list;
  6128. }
  6129. /**
  6130. * @param int $exe_id
  6131. *
  6132. * @return array
  6133. */
  6134. public function get_stat_track_exercise_info_by_exe_id($exe_id)
  6135. {
  6136. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  6137. $exe_id = (int) $exe_id;
  6138. $sql_track = "SELECT * FROM $table WHERE exe_id = $exe_id ";
  6139. $result = Database::query($sql_track);
  6140. $new_array = [];
  6141. if (Database::num_rows($result) > 0) {
  6142. $new_array = Database::fetch_array($result, 'ASSOC');
  6143. $start_date = api_get_utc_datetime($new_array['start_date'], true);
  6144. $end_date = api_get_utc_datetime($new_array['exe_date'], true);
  6145. $new_array['duration_formatted'] = '';
  6146. if (!empty($new_array['exe_duration']) && !empty($start_date) && !empty($end_date)) {
  6147. $time = api_format_time($new_array['exe_duration'], 'js');
  6148. $new_array['duration_formatted'] = $time;
  6149. }
  6150. }
  6151. return $new_array;
  6152. }
  6153. /**
  6154. * @param int $exeId
  6155. *
  6156. * @return bool
  6157. */
  6158. public function removeAllQuestionToRemind($exeId)
  6159. {
  6160. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  6161. $exeId = (int) $exeId;
  6162. if (empty($exeId)) {
  6163. return false;
  6164. }
  6165. $sql = "UPDATE $table
  6166. SET questions_to_check = ''
  6167. WHERE exe_id = $exeId ";
  6168. Database::query($sql);
  6169. return true;
  6170. }
  6171. /**
  6172. * @param int $exeId
  6173. * @param array $questionList
  6174. *
  6175. * @return bool
  6176. */
  6177. public function addAllQuestionToRemind($exeId, $questionList = [])
  6178. {
  6179. $exeId = (int) $exeId;
  6180. if (empty($questionList)) {
  6181. return false;
  6182. }
  6183. $questionListToString = implode(',', $questionList);
  6184. $questionListToString = Database::escape_string($questionListToString);
  6185. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  6186. $sql = "UPDATE $table
  6187. SET questions_to_check = '$questionListToString'
  6188. WHERE exe_id = $exeId";
  6189. Database::query($sql);
  6190. return true;
  6191. }
  6192. /**
  6193. * @param int $exe_id
  6194. * @param int $question_id
  6195. * @param string $action
  6196. */
  6197. public function editQuestionToRemind($exe_id, $question_id, $action = 'add')
  6198. {
  6199. $exercise_info = self::get_stat_track_exercise_info_by_exe_id($exe_id);
  6200. $question_id = (int) $question_id;
  6201. $exe_id = (int) $exe_id;
  6202. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  6203. if ($exercise_info) {
  6204. if (empty($exercise_info['questions_to_check'])) {
  6205. if ($action == 'add') {
  6206. $sql = "UPDATE $track_exercises
  6207. SET questions_to_check = '$question_id'
  6208. WHERE exe_id = $exe_id ";
  6209. Database::query($sql);
  6210. }
  6211. } else {
  6212. $remind_list = explode(',', $exercise_info['questions_to_check']);
  6213. $remind_list_string = '';
  6214. if ($action == 'add') {
  6215. if (!in_array($question_id, $remind_list)) {
  6216. $newRemindList = [];
  6217. $remind_list[] = $question_id;
  6218. $questionListInSession = Session::read('questionList');
  6219. if (!empty($questionListInSession)) {
  6220. foreach ($questionListInSession as $originalQuestionId) {
  6221. if (in_array($originalQuestionId, $remind_list)) {
  6222. $newRemindList[] = $originalQuestionId;
  6223. }
  6224. }
  6225. }
  6226. $remind_list_string = implode(',', $newRemindList);
  6227. }
  6228. } elseif ($action == 'delete') {
  6229. if (!empty($remind_list)) {
  6230. if (in_array($question_id, $remind_list)) {
  6231. $remind_list = array_flip($remind_list);
  6232. unset($remind_list[$question_id]);
  6233. $remind_list = array_flip($remind_list);
  6234. if (!empty($remind_list)) {
  6235. sort($remind_list);
  6236. array_filter($remind_list);
  6237. $remind_list_string = implode(',', $remind_list);
  6238. }
  6239. }
  6240. }
  6241. }
  6242. $value = Database::escape_string($remind_list_string);
  6243. $sql = "UPDATE $track_exercises
  6244. SET questions_to_check = '$value'
  6245. WHERE exe_id = $exe_id ";
  6246. Database::query($sql);
  6247. }
  6248. }
  6249. }
  6250. /**
  6251. * @param string $answer
  6252. *
  6253. * @return mixed
  6254. */
  6255. public function fill_in_blank_answer_to_array($answer)
  6256. {
  6257. api_preg_match_all('/\[[^]]+\]/', $answer, $teacher_answer_list);
  6258. $teacher_answer_list = $teacher_answer_list[0];
  6259. return $teacher_answer_list;
  6260. }
  6261. /**
  6262. * @param string $answer
  6263. *
  6264. * @return string
  6265. */
  6266. public function fill_in_blank_answer_to_string($answer)
  6267. {
  6268. $teacher_answer_list = $this->fill_in_blank_answer_to_array($answer);
  6269. $result = '';
  6270. if (!empty($teacher_answer_list)) {
  6271. $i = 0;
  6272. foreach ($teacher_answer_list as $teacher_item) {
  6273. $value = null;
  6274. //Cleaning student answer list
  6275. $value = strip_tags($teacher_item);
  6276. $value = api_substr($value, 1, api_strlen($value) - 2);
  6277. $value = explode('/', $value);
  6278. if (!empty($value[0])) {
  6279. $value = trim($value[0]);
  6280. $value = str_replace('&nbsp;', '', $value);
  6281. $result .= $value;
  6282. }
  6283. }
  6284. }
  6285. return $result;
  6286. }
  6287. /**
  6288. * @return string
  6289. */
  6290. public function return_time_left_div()
  6291. {
  6292. $html = '<div id="clock_warning" style="display:none">';
  6293. $html .= Display::return_message(
  6294. get_lang('ReachedTimeLimit'),
  6295. 'warning'
  6296. );
  6297. $html .= ' ';
  6298. $html .= sprintf(
  6299. get_lang('YouWillBeRedirectedInXSeconds'),
  6300. '<span id="counter_to_redirect" class="red_alert"></span>'
  6301. );
  6302. $html .= '</div>';
  6303. $html .= '<div id="exercise_clock_warning" class="count_down"></div>';
  6304. return $html;
  6305. }
  6306. /**
  6307. * Get categories added in the exercise--category matrix.
  6308. *
  6309. * @return array
  6310. */
  6311. public function getCategoriesInExercise()
  6312. {
  6313. $table = Database::get_course_table(TABLE_QUIZ_REL_CATEGORY);
  6314. if (!empty($this->id)) {
  6315. $sql = "SELECT * FROM $table
  6316. WHERE exercise_id = {$this->id} AND c_id = {$this->course_id} ";
  6317. $result = Database::query($sql);
  6318. $list = [];
  6319. if (Database::num_rows($result)) {
  6320. while ($row = Database::fetch_array($result, 'ASSOC')) {
  6321. $list[$row['category_id']] = $row;
  6322. }
  6323. return $list;
  6324. }
  6325. }
  6326. return [];
  6327. }
  6328. /**
  6329. * Get total number of question that will be parsed when using the category/exercise.
  6330. *
  6331. * @return int
  6332. */
  6333. public function getNumberQuestionExerciseCategory()
  6334. {
  6335. $table = Database::get_course_table(TABLE_QUIZ_REL_CATEGORY);
  6336. if (!empty($this->id)) {
  6337. $sql = "SELECT SUM(count_questions) count_questions
  6338. FROM $table
  6339. WHERE exercise_id = {$this->id} AND c_id = {$this->course_id}";
  6340. $result = Database::query($sql);
  6341. if (Database::num_rows($result)) {
  6342. $row = Database::fetch_array($result);
  6343. return $row['count_questions'];
  6344. }
  6345. }
  6346. return 0;
  6347. }
  6348. /**
  6349. * Save categories in the TABLE_QUIZ_REL_CATEGORY table.
  6350. *
  6351. * @param array $categories
  6352. */
  6353. public function save_categories_in_exercise($categories)
  6354. {
  6355. if (!empty($categories) && !empty($this->id)) {
  6356. $table = Database::get_course_table(TABLE_QUIZ_REL_CATEGORY);
  6357. $sql = "DELETE FROM $table
  6358. WHERE exercise_id = {$this->id} AND c_id = {$this->course_id}";
  6359. Database::query($sql);
  6360. if (!empty($categories)) {
  6361. foreach ($categories as $categoryId => $countQuestions) {
  6362. $params = [
  6363. 'c_id' => $this->course_id,
  6364. 'exercise_id' => $this->id,
  6365. 'category_id' => $categoryId,
  6366. 'count_questions' => $countQuestions,
  6367. ];
  6368. Database::insert($table, $params);
  6369. }
  6370. }
  6371. }
  6372. }
  6373. /**
  6374. * @param array $questionList
  6375. * @param int $currentQuestion
  6376. * @param array $conditions
  6377. * @param string $link
  6378. *
  6379. * @return string
  6380. */
  6381. public function progressExercisePaginationBar(
  6382. $questionList,
  6383. $currentQuestion,
  6384. $conditions,
  6385. $link
  6386. ) {
  6387. $mediaQuestions = $this->getMediaList();
  6388. $html = '<div class="exercise_pagination pagination pagination-mini"><ul>';
  6389. $counter = 0;
  6390. $nextValue = 0;
  6391. $wasMedia = false;
  6392. $before = 0;
  6393. $counterNoMedias = 0;
  6394. foreach ($questionList as $questionId) {
  6395. $isCurrent = $currentQuestion == ($counterNoMedias + 1) ? true : false;
  6396. if (!empty($nextValue)) {
  6397. if ($wasMedia) {
  6398. $nextValue = $nextValue - $before + 1;
  6399. }
  6400. }
  6401. if (isset($mediaQuestions) && isset($mediaQuestions[$questionId])) {
  6402. $fixedValue = $counterNoMedias;
  6403. $html .= Display::progressPaginationBar(
  6404. $nextValue,
  6405. $mediaQuestions[$questionId],
  6406. $currentQuestion,
  6407. $fixedValue,
  6408. $conditions,
  6409. $link,
  6410. true,
  6411. true
  6412. );
  6413. $counter += count($mediaQuestions[$questionId]) - 1;
  6414. $before = count($questionList);
  6415. $wasMedia = true;
  6416. $nextValue += count($questionList);
  6417. } else {
  6418. $html .= Display::parsePaginationItem(
  6419. $questionId,
  6420. $isCurrent,
  6421. $conditions,
  6422. $link,
  6423. $counter
  6424. );
  6425. $counter++;
  6426. $nextValue++;
  6427. $wasMedia = false;
  6428. }
  6429. $counterNoMedias++;
  6430. }
  6431. $html .= '</ul></div>';
  6432. return $html;
  6433. }
  6434. /**
  6435. * Shows a list of numbers that represents the question to answer in a exercise.
  6436. *
  6437. * @param array $categories
  6438. * @param int $current
  6439. * @param array $conditions
  6440. * @param string $link
  6441. *
  6442. * @return string
  6443. */
  6444. public function progressExercisePaginationBarWithCategories(
  6445. $categories,
  6446. $current,
  6447. $conditions = [],
  6448. $link = null
  6449. ) {
  6450. $html = null;
  6451. $counterNoMedias = 0;
  6452. $nextValue = 0;
  6453. $wasMedia = false;
  6454. $before = 0;
  6455. if (!empty($categories)) {
  6456. $selectionType = $this->getQuestionSelectionType();
  6457. $useRootAsCategoryTitle = false;
  6458. // Grouping questions per parent category see BT#6540
  6459. if (in_array(
  6460. $selectionType,
  6461. [
  6462. EX_Q_SELECTION_CATEGORIES_ORDERED_BY_PARENT_QUESTIONS_ORDERED,
  6463. EX_Q_SELECTION_CATEGORIES_ORDERED_BY_PARENT_QUESTIONS_RANDOM,
  6464. ]
  6465. )) {
  6466. $useRootAsCategoryTitle = true;
  6467. }
  6468. // If the exercise is set to only show the titles of the categories
  6469. // at the root of the tree, then pre-order the categories tree by
  6470. // removing children and summing their questions into the parent
  6471. // categories
  6472. if ($useRootAsCategoryTitle) {
  6473. // The new categories list starts empty
  6474. $newCategoryList = [];
  6475. foreach ($categories as $category) {
  6476. $rootElement = $category['root'];
  6477. if (isset($category['parent_info'])) {
  6478. $rootElement = $category['parent_info']['id'];
  6479. }
  6480. //$rootElement = $category['id'];
  6481. // If the current category's ancestor was never seen
  6482. // before, then declare it and assign the current
  6483. // category to it.
  6484. if (!isset($newCategoryList[$rootElement])) {
  6485. $newCategoryList[$rootElement] = $category;
  6486. } else {
  6487. // If it was already seen, then merge the previous with
  6488. // the current category
  6489. $oldQuestionList = $newCategoryList[$rootElement]['question_list'];
  6490. $category['question_list'] = array_merge($oldQuestionList, $category['question_list']);
  6491. $newCategoryList[$rootElement] = $category;
  6492. }
  6493. }
  6494. // Now use the newly built categories list, with only parents
  6495. $categories = $newCategoryList;
  6496. }
  6497. foreach ($categories as $category) {
  6498. $questionList = $category['question_list'];
  6499. // Check if in this category there questions added in a media
  6500. $mediaQuestionId = $category['media_question'];
  6501. $isMedia = false;
  6502. $fixedValue = null;
  6503. // Media exists!
  6504. if ($mediaQuestionId != 999) {
  6505. $isMedia = true;
  6506. $fixedValue = $counterNoMedias;
  6507. }
  6508. //$categoryName = $category['path']; << show the path
  6509. $categoryName = $category['name'];
  6510. if ($useRootAsCategoryTitle) {
  6511. if (isset($category['parent_info'])) {
  6512. $categoryName = $category['parent_info']['title'];
  6513. }
  6514. }
  6515. $html .= '<div class="row">';
  6516. $html .= '<div class="span2">'.$categoryName.'</div>';
  6517. $html .= '<div class="span8">';
  6518. if (!empty($nextValue)) {
  6519. if ($wasMedia) {
  6520. $nextValue = $nextValue - $before + 1;
  6521. }
  6522. }
  6523. $html .= Display::progressPaginationBar(
  6524. $nextValue,
  6525. $questionList,
  6526. $current,
  6527. $fixedValue,
  6528. $conditions,
  6529. $link,
  6530. $isMedia,
  6531. true
  6532. );
  6533. $html .= '</div>';
  6534. $html .= '</div>';
  6535. if ($mediaQuestionId == 999) {
  6536. $counterNoMedias += count($questionList);
  6537. } else {
  6538. $counterNoMedias++;
  6539. }
  6540. $nextValue += count($questionList);
  6541. $before = count($questionList);
  6542. if ($mediaQuestionId != 999) {
  6543. $wasMedia = true;
  6544. } else {
  6545. $wasMedia = false;
  6546. }
  6547. }
  6548. }
  6549. return $html;
  6550. }
  6551. /**
  6552. * Renders a question list.
  6553. *
  6554. * @param array $questionList (with media questions compressed)
  6555. * @param int $currentQuestion
  6556. * @param array $exerciseResult
  6557. * @param array $attemptList
  6558. * @param array $remindList
  6559. */
  6560. public function renderQuestionList(
  6561. $questionList,
  6562. $currentQuestion,
  6563. $exerciseResult,
  6564. $attemptList,
  6565. $remindList
  6566. ) {
  6567. $mediaQuestions = $this->getMediaList();
  6568. $i = 0;
  6569. // Normal question list render (medias compressed)
  6570. foreach ($questionList as $questionId) {
  6571. $i++;
  6572. // For sequential exercises
  6573. if ($this->type == ONE_PER_PAGE) {
  6574. // If it is not the right question, goes to the next loop iteration
  6575. if ($currentQuestion != $i) {
  6576. continue;
  6577. } else {
  6578. if ($this->feedback_type != EXERCISE_FEEDBACK_TYPE_DIRECT) {
  6579. // if the user has already answered this question
  6580. if (isset($exerciseResult[$questionId])) {
  6581. echo Display::return_message(
  6582. get_lang('AlreadyAnswered'),
  6583. 'normal'
  6584. );
  6585. break;
  6586. }
  6587. }
  6588. }
  6589. }
  6590. // The $questionList contains the media id we check
  6591. // if this questionId is a media question type
  6592. if (isset($mediaQuestions[$questionId]) &&
  6593. $mediaQuestions[$questionId] != 999
  6594. ) {
  6595. // The question belongs to a media
  6596. $mediaQuestionList = $mediaQuestions[$questionId];
  6597. $objQuestionTmp = Question::read($questionId);
  6598. $counter = 1;
  6599. if ($objQuestionTmp->type == MEDIA_QUESTION) {
  6600. echo $objQuestionTmp->show_media_content();
  6601. $countQuestionsInsideMedia = count($mediaQuestionList);
  6602. // Show questions that belongs to a media
  6603. if (!empty($mediaQuestionList)) {
  6604. // In order to parse media questions we use letters a, b, c, etc.
  6605. $letterCounter = 97;
  6606. foreach ($mediaQuestionList as $questionIdInsideMedia) {
  6607. $isLastQuestionInMedia = false;
  6608. if ($counter == $countQuestionsInsideMedia) {
  6609. $isLastQuestionInMedia = true;
  6610. }
  6611. $this->renderQuestion(
  6612. $questionIdInsideMedia,
  6613. $attemptList,
  6614. $remindList,
  6615. chr($letterCounter),
  6616. $currentQuestion,
  6617. $mediaQuestionList,
  6618. $isLastQuestionInMedia,
  6619. $questionList
  6620. );
  6621. $letterCounter++;
  6622. $counter++;
  6623. }
  6624. }
  6625. } else {
  6626. $this->renderQuestion(
  6627. $questionId,
  6628. $attemptList,
  6629. $remindList,
  6630. $i,
  6631. $currentQuestion,
  6632. null,
  6633. null,
  6634. $questionList
  6635. );
  6636. $i++;
  6637. }
  6638. } else {
  6639. // Normal question render.
  6640. $this->renderQuestion(
  6641. $questionId,
  6642. $attemptList,
  6643. $remindList,
  6644. $i,
  6645. $currentQuestion,
  6646. null,
  6647. null,
  6648. $questionList
  6649. );
  6650. }
  6651. // For sequential exercises.
  6652. if ($this->type == ONE_PER_PAGE) {
  6653. // quits the loop
  6654. break;
  6655. }
  6656. }
  6657. // end foreach()
  6658. if ($this->type == ALL_ON_ONE_PAGE) {
  6659. $exercise_actions = $this->show_button($questionId, $currentQuestion);
  6660. echo Display::div($exercise_actions, ['class' => 'exercise_actions']);
  6661. }
  6662. }
  6663. /**
  6664. * @param int $questionId
  6665. * @param array $attemptList
  6666. * @param array $remindList
  6667. * @param int $i
  6668. * @param int $current_question
  6669. * @param array $questions_in_media
  6670. * @param bool $last_question_in_media
  6671. * @param array $realQuestionList
  6672. * @param bool $generateJS
  6673. */
  6674. public function renderQuestion(
  6675. $questionId,
  6676. $attemptList,
  6677. $remindList,
  6678. $i,
  6679. $current_question,
  6680. $questions_in_media = [],
  6681. $last_question_in_media = false,
  6682. $realQuestionList,
  6683. $generateJS = true
  6684. ) {
  6685. // With this option on the question is loaded via AJAX
  6686. //$generateJS = true;
  6687. //$this->loadQuestionAJAX = true;
  6688. if ($generateJS && $this->loadQuestionAJAX) {
  6689. $url = api_get_path(WEB_AJAX_PATH).'exercise.ajax.php?a=get_question&id='.$questionId.'&'.api_get_cidreq();
  6690. $params = [
  6691. 'questionId' => $questionId,
  6692. 'attemptList' => $attemptList,
  6693. 'remindList' => $remindList,
  6694. 'i' => $i,
  6695. 'current_question' => $current_question,
  6696. 'questions_in_media' => $questions_in_media,
  6697. 'last_question_in_media' => $last_question_in_media,
  6698. ];
  6699. $params = json_encode($params);
  6700. $script = '<script>
  6701. $(function(){
  6702. var params = '.$params.';
  6703. $.ajax({
  6704. type: "GET",
  6705. async: false,
  6706. data: params,
  6707. url: "'.$url.'",
  6708. success: function(return_value) {
  6709. $("#ajaxquestiondiv'.$questionId.'").html(return_value);
  6710. }
  6711. });
  6712. });
  6713. </script>
  6714. <div id="ajaxquestiondiv'.$questionId.'"></div>';
  6715. echo $script;
  6716. } else {
  6717. global $origin;
  6718. $question_obj = Question::read($questionId);
  6719. $user_choice = isset($attemptList[$questionId]) ? $attemptList[$questionId] : null;
  6720. $remind_highlight = null;
  6721. // Hides questions when reviewing a ALL_ON_ONE_PAGE exercise
  6722. // see #4542 no_remind_highlight class hide with jquery
  6723. if ($this->type == ALL_ON_ONE_PAGE && isset($_GET['reminder']) && $_GET['reminder'] == 2) {
  6724. $remind_highlight = 'no_remind_highlight';
  6725. if (in_array($question_obj->type, Question::question_type_no_review())) {
  6726. return null;
  6727. }
  6728. }
  6729. $attributes = ['id' => 'remind_list['.$questionId.']'];
  6730. // Showing the question
  6731. $exercise_actions = null;
  6732. echo '<a id="questionanchor'.$questionId.'"></a><br />';
  6733. echo '<div id="question_div_'.$questionId.'" class="main_question '.$remind_highlight.'" >';
  6734. // Shows the question + possible answers
  6735. $showTitle = $this->getHideQuestionTitle() == 1 ? false : true;
  6736. echo $this->showQuestion(
  6737. $question_obj,
  6738. false,
  6739. $origin,
  6740. $i,
  6741. $showTitle,
  6742. false,
  6743. $user_choice,
  6744. false,
  6745. null,
  6746. false,
  6747. $this->getModelType(),
  6748. $this->categoryMinusOne
  6749. );
  6750. // Button save and continue
  6751. switch ($this->type) {
  6752. case ONE_PER_PAGE:
  6753. $exercise_actions .= $this->show_button(
  6754. $questionId,
  6755. $current_question,
  6756. null,
  6757. $remindList
  6758. );
  6759. break;
  6760. case ALL_ON_ONE_PAGE:
  6761. if (api_is_allowed_to_session_edit()) {
  6762. $button = [
  6763. Display::button(
  6764. 'save_now',
  6765. get_lang('SaveForNow'),
  6766. ['type' => 'button', 'class' => 'btn btn-primary', 'data-question' => $questionId]
  6767. ),
  6768. '<span id="save_for_now_'.$questionId.'" class="exercise_save_mini_message"></span>',
  6769. ];
  6770. $exercise_actions .= Display::div(
  6771. implode(PHP_EOL, $button),
  6772. ['class' => 'exercise_save_now_button']
  6773. );
  6774. }
  6775. break;
  6776. }
  6777. if (!empty($questions_in_media)) {
  6778. $count_of_questions_inside_media = count($questions_in_media);
  6779. if ($count_of_questions_inside_media > 1 && api_is_allowed_to_session_edit()) {
  6780. $button = [
  6781. Display::button(
  6782. 'save_now',
  6783. get_lang('SaveForNow'),
  6784. ['type' => 'button', 'class' => 'btn btn-primary', 'data-question' => $questionId]
  6785. ),
  6786. '<span id="save_for_now_'.$questionId.'" class="exercise_save_mini_message"></span>&nbsp;',
  6787. ];
  6788. $exercise_actions = Display::div(
  6789. implode(PHP_EOL, $button),
  6790. ['class' => 'exercise_save_now_button']
  6791. );
  6792. }
  6793. if ($last_question_in_media && $this->type == ONE_PER_PAGE) {
  6794. $exercise_actions = $this->show_button($questionId, $current_question, $questions_in_media);
  6795. }
  6796. }
  6797. // Checkbox review answers
  6798. if ($this->review_answers &&
  6799. !in_array($question_obj->type, Question::question_type_no_review())
  6800. ) {
  6801. $remind_question_div = Display::tag(
  6802. 'label',
  6803. Display::input(
  6804. 'checkbox',
  6805. 'remind_list['.$questionId.']',
  6806. '',
  6807. $attributes
  6808. ).get_lang('ReviewQuestionLater'),
  6809. [
  6810. 'class' => 'checkbox',
  6811. 'for' => 'remind_list['.$questionId.']',
  6812. ]
  6813. );
  6814. $exercise_actions .= Display::div(
  6815. $remind_question_div,
  6816. ['class' => 'exercise_save_now_button']
  6817. );
  6818. }
  6819. echo Display::div(' ', ['class' => 'clear']);
  6820. $paginationCounter = null;
  6821. if ($this->type == ONE_PER_PAGE) {
  6822. if (empty($questions_in_media)) {
  6823. $paginationCounter = Display::paginationIndicator(
  6824. $current_question,
  6825. count($realQuestionList)
  6826. );
  6827. } else {
  6828. if ($last_question_in_media) {
  6829. $paginationCounter = Display::paginationIndicator(
  6830. $current_question,
  6831. count($realQuestionList)
  6832. );
  6833. }
  6834. }
  6835. }
  6836. echo '<div class="row"><div class="pull-right">'.$paginationCounter.'</div></div>';
  6837. echo Display::div($exercise_actions, ['class' => 'form-actions']);
  6838. echo '</div>';
  6839. }
  6840. }
  6841. /**
  6842. * Returns an array of categories details for the questions of the current
  6843. * exercise.
  6844. *
  6845. * @return array
  6846. */
  6847. public function getQuestionWithCategories()
  6848. {
  6849. $categoryTable = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  6850. $categoryRelTable = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  6851. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  6852. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  6853. $sql = "SELECT DISTINCT cat.*
  6854. FROM $TBL_EXERCICE_QUESTION e
  6855. INNER JOIN $TBL_QUESTIONS q
  6856. ON (e.question_id = q.id AND e.c_id = q.c_id)
  6857. INNER JOIN $categoryRelTable catRel
  6858. ON (catRel.question_id = e.question_id AND catRel.c_id = e.c_id)
  6859. INNER JOIN $categoryTable cat
  6860. ON (cat.id = catRel.category_id AND cat.c_id = e.c_id)
  6861. WHERE
  6862. e.c_id = {$this->course_id} AND
  6863. e.exercice_id = ".intval($this->id);
  6864. $result = Database::query($sql);
  6865. $categoriesInExercise = [];
  6866. if (Database::num_rows($result)) {
  6867. $categoriesInExercise = Database::store_result($result, 'ASSOC');
  6868. }
  6869. return $categoriesInExercise;
  6870. }
  6871. /**
  6872. * Calculate the max_score of the quiz, depending of question inside, and quiz advanced option.
  6873. */
  6874. public function get_max_score()
  6875. {
  6876. $out_max_score = 0;
  6877. // list of question's id !!! the array key start at 1 !!!
  6878. $questionList = $this->selectQuestionList(true);
  6879. // test is randomQuestions - see field random of test
  6880. if ($this->random > 0 && $this->randomByCat == 0) {
  6881. $numberRandomQuestions = $this->random;
  6882. $questionScoreList = [];
  6883. foreach ($questionList as $questionId) {
  6884. $tmpobj_question = Question::read($questionId);
  6885. if (is_object($tmpobj_question)) {
  6886. $questionScoreList[] = $tmpobj_question->weighting;
  6887. }
  6888. }
  6889. rsort($questionScoreList);
  6890. // add the first $numberRandomQuestions value of score array to get max_score
  6891. for ($i = 0; $i < min($numberRandomQuestions, count($questionScoreList)); $i++) {
  6892. $out_max_score += $questionScoreList[$i];
  6893. }
  6894. } elseif ($this->random > 0 && $this->randomByCat > 0) {
  6895. // test is random by category
  6896. // get the $numberRandomQuestions best score question of each category
  6897. $numberRandomQuestions = $this->random;
  6898. $tab_categories_scores = [];
  6899. foreach ($questionList as $questionId) {
  6900. $question_category_id = TestCategory::getCategoryForQuestion($questionId);
  6901. if (!is_array($tab_categories_scores[$question_category_id])) {
  6902. $tab_categories_scores[$question_category_id] = [];
  6903. }
  6904. $tmpobj_question = Question::read($questionId);
  6905. if (is_object($tmpobj_question)) {
  6906. $tab_categories_scores[$question_category_id][] = $tmpobj_question->weighting;
  6907. }
  6908. }
  6909. // here we've got an array with first key, the category_id, second key, score of question for this cat
  6910. foreach ($tab_categories_scores as $tab_scores) {
  6911. rsort($tab_scores);
  6912. for ($i = 0; $i < min($numberRandomQuestions, count($tab_scores)); $i++) {
  6913. $out_max_score += $tab_scores[$i];
  6914. }
  6915. }
  6916. } else {
  6917. // standard test, just add each question score
  6918. foreach ($questionList as $questionId) {
  6919. $question = Question::read($questionId, $this->course);
  6920. $out_max_score += $question->weighting;
  6921. }
  6922. }
  6923. return $out_max_score;
  6924. }
  6925. /**
  6926. * @return string
  6927. */
  6928. public function get_formated_title()
  6929. {
  6930. if (api_get_configuration_value('save_titles_as_html')) {
  6931. }
  6932. return api_html_entity_decode($this->selectTitle());
  6933. }
  6934. /**
  6935. * @param string $title
  6936. *
  6937. * @return string
  6938. */
  6939. public static function get_formated_title_variable($title)
  6940. {
  6941. return api_html_entity_decode($title);
  6942. }
  6943. /**
  6944. * @return string
  6945. */
  6946. public function format_title()
  6947. {
  6948. return api_htmlentities($this->title);
  6949. }
  6950. /**
  6951. * @param string $title
  6952. *
  6953. * @return string
  6954. */
  6955. public static function format_title_variable($title)
  6956. {
  6957. return api_htmlentities($title);
  6958. }
  6959. /**
  6960. * @param int $courseId
  6961. * @param int $sessionId
  6962. *
  6963. * @return array exercises
  6964. */
  6965. public function getExercisesByCourseSession($courseId, $sessionId)
  6966. {
  6967. $courseId = (int) $courseId;
  6968. $sessionId = (int) $sessionId;
  6969. $tbl_quiz = Database::get_course_table(TABLE_QUIZ_TEST);
  6970. $sql = "SELECT * FROM $tbl_quiz cq
  6971. WHERE
  6972. cq.c_id = %s AND
  6973. (cq.session_id = %s OR cq.session_id = 0) AND
  6974. cq.active = 0
  6975. ORDER BY cq.id";
  6976. $sql = sprintf($sql, $courseId, $sessionId);
  6977. $result = Database::query($sql);
  6978. $rows = [];
  6979. while ($row = Database::fetch_array($result, 'ASSOC')) {
  6980. $rows[] = $row;
  6981. }
  6982. return $rows;
  6983. }
  6984. /**
  6985. * @param int $courseId
  6986. * @param int $sessionId
  6987. * @param array $quizId
  6988. *
  6989. * @return array exercises
  6990. */
  6991. public function getExerciseAndResult($courseId, $sessionId, $quizId = [])
  6992. {
  6993. if (empty($quizId)) {
  6994. return [];
  6995. }
  6996. $sessionId = (int) $sessionId;
  6997. $courseId = (int) $courseId;
  6998. $ids = is_array($quizId) ? $quizId : [$quizId];
  6999. $ids = array_map('intval', $ids);
  7000. $ids = implode(',', $ids);
  7001. $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  7002. if ($sessionId != 0) {
  7003. $sql = "SELECT * FROM $track_exercises te
  7004. INNER JOIN c_quiz cq ON cq.id = te.exe_exo_id AND te.c_id = cq.c_id
  7005. WHERE
  7006. te.id = %s AND
  7007. te.session_id = %s AND
  7008. cq.id IN (%s)
  7009. ORDER BY cq.id";
  7010. $sql = sprintf($sql, $courseId, $sessionId, $ids);
  7011. } else {
  7012. $sql = "SELECT * FROM $track_exercises te
  7013. INNER JOIN c_quiz cq ON cq.id = te.exe_exo_id AND te.c_id = cq.c_id
  7014. WHERE
  7015. te.id = %s AND
  7016. cq.id IN (%s)
  7017. ORDER BY cq.id";
  7018. $sql = sprintf($sql, $courseId, $ids);
  7019. }
  7020. $result = Database::query($sql);
  7021. $rows = [];
  7022. while ($row = Database::fetch_array($result, 'ASSOC')) {
  7023. $rows[] = $row;
  7024. }
  7025. return $rows;
  7026. }
  7027. /**
  7028. * @param $exeId
  7029. * @param $exercise_stat_info
  7030. * @param $remindList
  7031. * @param $currentQuestion
  7032. *
  7033. * @return int|null
  7034. */
  7035. public static function getNextQuestionId(
  7036. $exeId,
  7037. $exercise_stat_info,
  7038. $remindList,
  7039. $currentQuestion
  7040. ) {
  7041. $result = Event::get_exercise_results_by_attempt($exeId, 'incomplete');
  7042. if (isset($result[$exeId])) {
  7043. $result = $result[$exeId];
  7044. } else {
  7045. return null;
  7046. }
  7047. $data_tracking = $exercise_stat_info['data_tracking'];
  7048. $data_tracking = explode(',', $data_tracking);
  7049. // if this is the final question do nothing.
  7050. if ($currentQuestion == count($data_tracking)) {
  7051. return null;
  7052. }
  7053. $currentQuestion = $currentQuestion - 1;
  7054. if (!empty($result['question_list'])) {
  7055. $answeredQuestions = [];
  7056. foreach ($result['question_list'] as $question) {
  7057. if (!empty($question['answer'])) {
  7058. $answeredQuestions[] = $question['question_id'];
  7059. }
  7060. }
  7061. // Checking answered questions
  7062. $counterAnsweredQuestions = 0;
  7063. foreach ($data_tracking as $questionId) {
  7064. if (!in_array($questionId, $answeredQuestions)) {
  7065. if ($currentQuestion != $counterAnsweredQuestions) {
  7066. break;
  7067. }
  7068. }
  7069. $counterAnsweredQuestions++;
  7070. }
  7071. $counterRemindListQuestions = 0;
  7072. // Checking questions saved in the reminder list
  7073. if (!empty($remindList)) {
  7074. foreach ($data_tracking as $questionId) {
  7075. if (in_array($questionId, $remindList)) {
  7076. // Skip the current question
  7077. if ($currentQuestion != $counterRemindListQuestions) {
  7078. break;
  7079. }
  7080. }
  7081. $counterRemindListQuestions++;
  7082. }
  7083. if ($counterRemindListQuestions < $currentQuestion) {
  7084. return null;
  7085. }
  7086. if (!empty($counterRemindListQuestions)) {
  7087. if ($counterRemindListQuestions > $counterAnsweredQuestions) {
  7088. return $counterAnsweredQuestions;
  7089. } else {
  7090. return $counterRemindListQuestions;
  7091. }
  7092. }
  7093. }
  7094. return $counterAnsweredQuestions;
  7095. }
  7096. }
  7097. /**
  7098. * Gets the position of a questionId in the question list.
  7099. *
  7100. * @param $questionId
  7101. *
  7102. * @return int
  7103. */
  7104. public function getPositionInCompressedQuestionList($questionId)
  7105. {
  7106. $questionList = $this->getQuestionListWithMediasCompressed();
  7107. $mediaQuestions = $this->getMediaList();
  7108. $position = 1;
  7109. foreach ($questionList as $id) {
  7110. if (isset($mediaQuestions[$id]) && in_array($questionId, $mediaQuestions[$id])) {
  7111. $mediaQuestionList = $mediaQuestions[$id];
  7112. if (in_array($questionId, $mediaQuestionList)) {
  7113. return $position;
  7114. } else {
  7115. $position++;
  7116. }
  7117. } else {
  7118. if ($id == $questionId) {
  7119. return $position;
  7120. } else {
  7121. $position++;
  7122. }
  7123. }
  7124. }
  7125. return 1;
  7126. }
  7127. /**
  7128. * Get the correct answers in all attempts.
  7129. *
  7130. * @param int $learnPathId
  7131. * @param int $learnPathItemId
  7132. *
  7133. * @return array
  7134. */
  7135. public function getCorrectAnswersInAllAttempts($learnPathId = 0, $learnPathItemId = 0)
  7136. {
  7137. $attempts = Event::getExerciseResultsByUser(
  7138. api_get_user_id(),
  7139. $this->id,
  7140. api_get_course_int_id(),
  7141. api_get_session_id(),
  7142. $learnPathId,
  7143. $learnPathItemId,
  7144. 'asc'
  7145. );
  7146. $corrects = [];
  7147. foreach ($attempts as $attempt) {
  7148. foreach ($attempt['question_list'] as $answers) {
  7149. foreach ($answers as $answer) {
  7150. $objAnswer = new Answer($answer['question_id']);
  7151. switch ($objAnswer->getQuestionType()) {
  7152. case FILL_IN_BLANKS:
  7153. $isCorrect = FillBlanks::isCorrect($answer['answer']);
  7154. break;
  7155. case MATCHING:
  7156. case DRAGGABLE:
  7157. case MATCHING_DRAGGABLE:
  7158. $isCorrect = Matching::isCorrect(
  7159. $answer['position'],
  7160. $answer['answer'],
  7161. $answer['question_id']
  7162. );
  7163. break;
  7164. case ORAL_EXPRESSION:
  7165. $isCorrect = false;
  7166. break;
  7167. default:
  7168. $isCorrect = $objAnswer->isCorrectByAutoId($answer['answer']);
  7169. }
  7170. if ($isCorrect) {
  7171. $corrects[$answer['question_id']][] = $answer;
  7172. }
  7173. }
  7174. }
  7175. }
  7176. return $corrects;
  7177. }
  7178. /**
  7179. * @return bool
  7180. */
  7181. public function showPreviousButton()
  7182. {
  7183. $allow = api_get_configuration_value('allow_quiz_show_previous_button_setting');
  7184. if ($allow === false) {
  7185. return true;
  7186. }
  7187. return $this->showPreviousButton;
  7188. }
  7189. /**
  7190. * @param bool $showPreviousButton
  7191. *
  7192. * @return Exercise
  7193. */
  7194. public function setShowPreviousButton($showPreviousButton)
  7195. {
  7196. $this->showPreviousButton = $showPreviousButton;
  7197. return $this;
  7198. }
  7199. /**
  7200. * @param array $notifications
  7201. */
  7202. public function setNotifications($notifications)
  7203. {
  7204. $this->notifications = $notifications;
  7205. }
  7206. /**
  7207. * @return array
  7208. */
  7209. public function getNotifications()
  7210. {
  7211. return $this->notifications;
  7212. }
  7213. /**
  7214. * @return bool
  7215. */
  7216. public function showExpectedChoice()
  7217. {
  7218. return api_get_configuration_value('show_exercise_expected_choice');
  7219. }
  7220. /**
  7221. * @param string $class
  7222. * @param string $scoreLabel
  7223. * @param string $result
  7224. * @param array
  7225. *
  7226. * @return string
  7227. */
  7228. public function getQuestionRibbon($class, $scoreLabel, $result, $array)
  7229. {
  7230. if ($this->showExpectedChoice()) {
  7231. $html = null;
  7232. $hideLabel = api_get_configuration_value('exercise_hide_label');
  7233. $label = '<div class="rib rib-'.$class.'">
  7234. <h3>'.$scoreLabel.'</h3>
  7235. </div>';
  7236. if (!empty($result)) {
  7237. $label .= '<h4>'.get_lang('Score').': '.$result.'</h4>';
  7238. }
  7239. if ($hideLabel === true) {
  7240. $answerUsed = (int) $array['used'];
  7241. $answerMissing = (int) $array['missing'] - $answerUsed;
  7242. for ($i = 1; $i <= $answerUsed; $i++) {
  7243. $html .= '<span class="score-img">'.
  7244. Display::return_icon('attempt-check.png', null, null, ICON_SIZE_SMALL).
  7245. '</span>';
  7246. }
  7247. for ($i = 1; $i <= $answerMissing; $i++) {
  7248. $html .= '<span class="score-img">'.
  7249. Display::return_icon('attempt-nocheck.png', null, null, ICON_SIZE_SMALL).
  7250. '</span>';
  7251. }
  7252. $label = '<div class="score-title">'.get_lang('CorrectAnswers').': '.$result.'</div>';
  7253. $label .= '<div class="score-limits">';
  7254. $label .= $html;
  7255. $label .= '</div>';
  7256. }
  7257. return '<div class="ribbon">
  7258. '.$label.'
  7259. </div>'
  7260. ;
  7261. } else {
  7262. $html = '<div class="ribbon">
  7263. <div class="rib rib-'.$class.'">
  7264. <h3>'.$scoreLabel.'</h3>
  7265. </div>';
  7266. if (!empty($result)) {
  7267. $html .= '<h4>'.get_lang('Score').': '.$result.'</h4>';
  7268. }
  7269. $html .= '</div>';
  7270. return $html;
  7271. }
  7272. }
  7273. /**
  7274. * @return int
  7275. */
  7276. public function getAutoLaunch()
  7277. {
  7278. return $this->autolaunch;
  7279. }
  7280. /**
  7281. * Clean auto launch settings for all exercise in course/course-session.
  7282. */
  7283. public function enableAutoLaunch()
  7284. {
  7285. $table = Database::get_course_table(TABLE_QUIZ_TEST);
  7286. $sql = "UPDATE $table SET autolaunch = 1
  7287. WHERE iid = ".$this->iId;
  7288. Database::query($sql);
  7289. }
  7290. /**
  7291. * Clean auto launch settings for all exercise in course/course-session.
  7292. */
  7293. public function cleanCourseLaunchSettings()
  7294. {
  7295. $table = Database::get_course_table(TABLE_QUIZ_TEST);
  7296. $sql = "UPDATE $table SET autolaunch = 0
  7297. WHERE c_id = ".$this->course_id." AND session_id = ".$this->sessionId;
  7298. Database::query($sql);
  7299. }
  7300. /**
  7301. * Get the title without HTML tags.
  7302. *
  7303. * @return string
  7304. */
  7305. public function getUnformattedTitle()
  7306. {
  7307. return strip_tags(api_html_entity_decode($this->title));
  7308. }
  7309. /**
  7310. * @param int $start
  7311. * @param int $length
  7312. *
  7313. * @return array
  7314. */
  7315. public function getQuestionForTeacher($start = 0, $length = 10)
  7316. {
  7317. $start = (int) $start;
  7318. if ($start < 0) {
  7319. $start = 0;
  7320. }
  7321. $length = (int) $length;
  7322. $quizRelQuestion = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  7323. $question = Database::get_course_table(TABLE_QUIZ_QUESTION);
  7324. $sql = "SELECT DISTINCT e.question_id
  7325. FROM $quizRelQuestion e
  7326. INNER JOIN $question q
  7327. ON (e.question_id = q.iid AND e.c_id = q.c_id)
  7328. WHERE
  7329. e.c_id = {$this->course_id} AND
  7330. e.exercice_id = '".$this->id."'
  7331. ORDER BY question_order
  7332. LIMIT $start, $length
  7333. ";
  7334. $result = Database::query($sql);
  7335. $questionList = [];
  7336. while ($object = Database::fetch_object($result)) {
  7337. $questionList[] = $object->question_id;
  7338. }
  7339. return $questionList;
  7340. }
  7341. /**
  7342. * @param int $exerciseId
  7343. * @param array $courseInfo
  7344. * @param int $sessionId
  7345. *
  7346. * @throws \Doctrine\ORM\OptimisticLockException
  7347. *
  7348. * @return bool
  7349. */
  7350. public function generateStats($exerciseId, $courseInfo, $sessionId)
  7351. {
  7352. $allowStats = api_get_configuration_value('allow_gradebook_stats');
  7353. if (!$allowStats) {
  7354. return false;
  7355. }
  7356. if (empty($courseInfo)) {
  7357. return false;
  7358. }
  7359. $courseId = $courseInfo['real_id'];
  7360. $sessionId = (int) $sessionId;
  7361. $result = $this->read($exerciseId);
  7362. if (empty($result)) {
  7363. api_not_allowed(true);
  7364. }
  7365. $statusToFilter = empty($sessionId) ? STUDENT : 0;
  7366. $studentList = CourseManager::get_user_list_from_course_code(
  7367. api_get_course_id(),
  7368. $sessionId,
  7369. null,
  7370. null,
  7371. $statusToFilter
  7372. );
  7373. if (empty($studentList)) {
  7374. Display::addFlash(Display::return_message(get_lang('NoUsersInCourse')));
  7375. header('Location: '.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq());
  7376. exit;
  7377. }
  7378. $tblStats = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  7379. $studentIdList = [];
  7380. if (!empty($studentList)) {
  7381. $studentIdList = array_column($studentList, 'user_id');
  7382. }
  7383. if ($this->exercise_was_added_in_lp == false) {
  7384. $sql = "SELECT * FROM $tblStats
  7385. WHERE
  7386. exe_exo_id = $exerciseId AND
  7387. orig_lp_id = 0 AND
  7388. orig_lp_item_id = 0 AND
  7389. status <> 'incomplete' AND
  7390. session_id = $sessionId AND
  7391. c_id = $courseId
  7392. ";
  7393. } else {
  7394. $lpId = null;
  7395. if (!empty($this->lpList)) {
  7396. // Taking only the first LP
  7397. $lpId = current($this->lpList);
  7398. $lpId = $lpId['lp_id'];
  7399. }
  7400. $sql = "SELECT *
  7401. FROM $tblStats
  7402. WHERE
  7403. exe_exo_id = $exerciseId AND
  7404. orig_lp_id = $lpId AND
  7405. status <> 'incomplete' AND
  7406. session_id = $sessionId AND
  7407. c_id = $courseId ";
  7408. }
  7409. $sql .= ' ORDER BY exe_id DESC';
  7410. $studentCount = 0;
  7411. $sum = 0;
  7412. $bestResult = 0;
  7413. $weight = 0;
  7414. $sumResult = 0;
  7415. $result = Database::query($sql);
  7416. while ($data = Database::fetch_array($result, 'ASSOC')) {
  7417. // Only take into account users in the current student list.
  7418. if (!empty($studentIdList)) {
  7419. if (!in_array($data['exe_user_id'], $studentIdList)) {
  7420. continue;
  7421. }
  7422. }
  7423. if (!isset($students[$data['exe_user_id']])) {
  7424. if ($data['exe_weighting'] != 0) {
  7425. $students[$data['exe_user_id']] = $data['exe_result'];
  7426. $studentCount++;
  7427. if ($data['exe_result'] > $bestResult) {
  7428. $bestResult = $data['exe_result'];
  7429. }
  7430. $sum += $data['exe_result'] / $data['exe_weighting'];
  7431. $sumResult += $data['exe_result'];
  7432. $weight = $data['exe_weighting'];
  7433. }
  7434. }
  7435. }
  7436. $count = count($studentList);
  7437. $average = $sumResult / $count;
  7438. $em = Database::getManager();
  7439. $links = AbstractLink::getGradebookLinksFromItem(
  7440. $this->selectId(),
  7441. LINK_EXERCISE,
  7442. api_get_course_id(),
  7443. api_get_session_id()
  7444. );
  7445. if (!empty($links)) {
  7446. $repo = $em->getRepository('ChamiloCoreBundle:GradebookLink');
  7447. foreach ($links as $link) {
  7448. $linkId = $link['id'];
  7449. /** @var \Chamilo\CoreBundle\Entity\GradebookLink $exerciseLink */
  7450. $exerciseLink = $repo->find($linkId);
  7451. if ($exerciseLink) {
  7452. $exerciseLink
  7453. ->setUserScoreList($students)
  7454. ->setBestScore($bestResult)
  7455. ->setAverageScore($average)
  7456. ->setScoreWeight($this->get_max_score());
  7457. $em->persist($exerciseLink);
  7458. $em->flush();
  7459. }
  7460. }
  7461. }
  7462. }
  7463. /**
  7464. * Gets the question list ordered by the question_order setting (drag and drop).
  7465. *
  7466. * @return array
  7467. */
  7468. private function getQuestionOrderedList()
  7469. {
  7470. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  7471. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  7472. // Getting question_order to verify that the question
  7473. // list is correct and all question_order's were set
  7474. $sql = "SELECT DISTINCT count(e.question_order) as count
  7475. FROM $TBL_EXERCICE_QUESTION e
  7476. INNER JOIN $TBL_QUESTIONS q
  7477. ON (e.question_id = q.id AND e.c_id = q.c_id)
  7478. WHERE
  7479. e.c_id = {$this->course_id} AND
  7480. e.exercice_id = ".$this->id;
  7481. $result = Database::query($sql);
  7482. $row = Database::fetch_array($result);
  7483. $count_question_orders = $row['count'];
  7484. // Getting question list from the order (question list drag n drop interface).
  7485. $sql = "SELECT DISTINCT e.question_id, e.question_order
  7486. FROM $TBL_EXERCICE_QUESTION e
  7487. INNER JOIN $TBL_QUESTIONS q
  7488. ON (e.question_id = q.id AND e.c_id = q.c_id)
  7489. WHERE
  7490. e.c_id = {$this->course_id} AND
  7491. e.exercice_id = '".$this->id."'
  7492. ORDER BY question_order";
  7493. $result = Database::query($sql);
  7494. // Fills the array with the question ID for this exercise
  7495. // the key of the array is the question position
  7496. $temp_question_list = [];
  7497. $counter = 1;
  7498. $questionList = [];
  7499. while ($new_object = Database::fetch_object($result)) {
  7500. // Correct order.
  7501. $questionList[$new_object->question_order] = $new_object->question_id;
  7502. // Just in case we save the order in other array
  7503. $temp_question_list[$counter] = $new_object->question_id;
  7504. $counter++;
  7505. }
  7506. if (!empty($temp_question_list)) {
  7507. /* If both array don't match it means that question_order was not correctly set
  7508. for all questions using the default mysql order */
  7509. if (count($temp_question_list) != $count_question_orders) {
  7510. $questionList = $temp_question_list;
  7511. }
  7512. }
  7513. return $questionList;
  7514. }
  7515. /**
  7516. * Select N values from the questions per category array.
  7517. *
  7518. * @param array $categoriesAddedInExercise
  7519. * @param array $question_list
  7520. * @param array $questions_by_category per category
  7521. * @param bool $flatResult
  7522. * @param bool $randomizeQuestions
  7523. *
  7524. * @return array
  7525. */
  7526. private function pickQuestionsPerCategory(
  7527. $categoriesAddedInExercise,
  7528. $question_list,
  7529. &$questions_by_category,
  7530. $flatResult = true,
  7531. $randomizeQuestions = false
  7532. ) {
  7533. $addAll = true;
  7534. $categoryCountArray = [];
  7535. // Getting how many questions will be selected per category.
  7536. if (!empty($categoriesAddedInExercise)) {
  7537. $addAll = false;
  7538. // Parsing question according the category rel exercise settings
  7539. foreach ($categoriesAddedInExercise as $category_info) {
  7540. $category_id = $category_info['category_id'];
  7541. if (isset($questions_by_category[$category_id])) {
  7542. // How many question will be picked from this category.
  7543. $count = $category_info['count_questions'];
  7544. // -1 means all questions
  7545. $categoryCountArray[$category_id] = $count;
  7546. if ($count == -1) {
  7547. $categoryCountArray[$category_id] = 999;
  7548. }
  7549. }
  7550. }
  7551. }
  7552. if (!empty($questions_by_category)) {
  7553. $temp_question_list = [];
  7554. foreach ($questions_by_category as $category_id => &$categoryQuestionList) {
  7555. if (isset($categoryCountArray) && !empty($categoryCountArray)) {
  7556. $numberOfQuestions = 0;
  7557. if (isset($categoryCountArray[$category_id])) {
  7558. $numberOfQuestions = $categoryCountArray[$category_id];
  7559. }
  7560. }
  7561. if ($addAll) {
  7562. $numberOfQuestions = 999;
  7563. }
  7564. if (!empty($numberOfQuestions)) {
  7565. $elements = TestCategory::getNElementsFromArray(
  7566. $categoryQuestionList,
  7567. $numberOfQuestions,
  7568. $randomizeQuestions
  7569. );
  7570. if (!empty($elements)) {
  7571. $temp_question_list[$category_id] = $elements;
  7572. $categoryQuestionList = $elements;
  7573. }
  7574. }
  7575. }
  7576. if (!empty($temp_question_list)) {
  7577. if ($flatResult) {
  7578. $temp_question_list = array_flatten($temp_question_list);
  7579. }
  7580. $question_list = $temp_question_list;
  7581. }
  7582. }
  7583. return $question_list;
  7584. }
  7585. /**
  7586. * Changes the exercise id.
  7587. *
  7588. * @param int $id - exercise id
  7589. */
  7590. private function updateId($id)
  7591. {
  7592. $this->id = $id;
  7593. }
  7594. /**
  7595. * Sends a notification when a user ends an examn.
  7596. *
  7597. * @param array $question_list_answers
  7598. * @param string $origin
  7599. * @param array $user_info
  7600. * @param string $url_email
  7601. * @param array $teachers
  7602. */
  7603. private function sendNotificationForOpenQuestions(
  7604. $question_list_answers,
  7605. $origin,
  7606. $user_info,
  7607. $url_email,
  7608. $teachers
  7609. ) {
  7610. // Email configuration settings
  7611. $courseCode = api_get_course_id();
  7612. $courseInfo = api_get_course_info($courseCode);
  7613. $sessionId = api_get_session_id();
  7614. $sessionData = '';
  7615. if (!empty($sessionId)) {
  7616. $sessionInfo = api_get_session_info($sessionId);
  7617. if (!empty($sessionInfo)) {
  7618. $sessionData = '<tr>'
  7619. .'<td><em>'.get_lang('SessionName').'</em></td>'
  7620. .'<td>&nbsp;<b>'.$sessionInfo['name'].'</b></td>'
  7621. .'</tr>';
  7622. }
  7623. }
  7624. $msg = get_lang('OpenQuestionsAttempted').'<br /><br />'
  7625. .get_lang('AttemptDetails').' : <br /><br />'
  7626. .'<table>'
  7627. .'<tr>'
  7628. .'<td><em>'.get_lang('CourseName').'</em></td>'
  7629. .'<td>&nbsp;<b>#course#</b></td>'
  7630. .'</tr>'
  7631. .$sessionData
  7632. .'<tr>'
  7633. .'<td>'.get_lang('TestAttempted').'</td>'
  7634. .'<td>&nbsp;#exercise#</td>'
  7635. .'</tr>'
  7636. .'<tr>'
  7637. .'<td>'.get_lang('StudentName').'</td>'
  7638. .'<td>&nbsp;#firstName# #lastName#</td>'
  7639. .'</tr>'
  7640. .'<tr>'
  7641. .'<td>'.get_lang('StudentEmail').'</td>'
  7642. .'<td>&nbsp;#mail#</td>'
  7643. .'</tr>'
  7644. .'</table>';
  7645. $open_question_list = null;
  7646. foreach ($question_list_answers as $item) {
  7647. $question = $item['question'];
  7648. $answer = $item['answer'];
  7649. $answer_type = $item['answer_type'];
  7650. if (!empty($question) && !empty($answer) && $answer_type == FREE_ANSWER) {
  7651. $open_question_list .=
  7652. '<tr>'
  7653. .'<td width="220" valign="top" bgcolor="#E5EDF8">&nbsp;&nbsp;'.get_lang('Question').'</td>'
  7654. .'<td width="473" valign="top" bgcolor="#F3F3F3">'.$question.'</td>'
  7655. .'</tr>'
  7656. .'<tr>'
  7657. .'<td width="220" valign="top" bgcolor="#E5EDF8">&nbsp;&nbsp;'.get_lang('Answer').'</td>'
  7658. .'<td valign="top" bgcolor="#F3F3F3">'.$answer.'</td>'
  7659. .'</tr>';
  7660. }
  7661. }
  7662. if (!empty($open_question_list)) {
  7663. $msg .= '<p><br />'.get_lang('OpenQuestionsAttemptedAre').' :</p>'.
  7664. '<table width="730" height="136" border="0" cellpadding="3" cellspacing="3">';
  7665. $msg .= $open_question_list;
  7666. $msg .= '</table><br />';
  7667. $msg = str_replace('#exercise#', $this->exercise, $msg);
  7668. $msg = str_replace('#firstName#', $user_info['firstname'], $msg);
  7669. $msg = str_replace('#lastName#', $user_info['lastname'], $msg);
  7670. $msg = str_replace('#mail#', $user_info['email'], $msg);
  7671. $msg = str_replace(
  7672. '#course#',
  7673. Display::url($courseInfo['title'], $courseInfo['course_public_url'].'?id_session='.$sessionId),
  7674. $msg
  7675. );
  7676. if ($origin != 'learnpath') {
  7677. $msg .= '<br /><a href="#url#">'.get_lang('ClickToCommentAndGiveFeedback').'</a>';
  7678. }
  7679. $msg = str_replace('#url#', $url_email, $msg);
  7680. $subject = get_lang('OpenQuestionsAttempted');
  7681. if (!empty($teachers)) {
  7682. foreach ($teachers as $user_id => $teacher_data) {
  7683. MessageManager::send_message_simple(
  7684. $user_id,
  7685. $subject,
  7686. $msg
  7687. );
  7688. }
  7689. }
  7690. }
  7691. }
  7692. /**
  7693. * Send notification for oral questions.
  7694. *
  7695. * @param array $question_list_answers
  7696. * @param string $origin
  7697. * @param int $exe_id
  7698. * @param array $user_info
  7699. * @param string $url_email
  7700. * @param array $teachers
  7701. */
  7702. private function sendNotificationForOralQuestions(
  7703. $question_list_answers,
  7704. $origin,
  7705. $exe_id,
  7706. $user_info,
  7707. $url_email,
  7708. $teachers
  7709. ) {
  7710. // Email configuration settings
  7711. $courseCode = api_get_course_id();
  7712. $courseInfo = api_get_course_info($courseCode);
  7713. $oral_question_list = null;
  7714. foreach ($question_list_answers as $item) {
  7715. $question = $item['question'];
  7716. $file = $item['generated_oral_file'];
  7717. $answer = $item['answer'];
  7718. if ($answer == 0) {
  7719. $answer = '';
  7720. }
  7721. $answer_type = $item['answer_type'];
  7722. if (!empty($question) && (!empty($answer) || !empty($file)) && $answer_type == ORAL_EXPRESSION) {
  7723. if (!empty($file)) {
  7724. $file = Display::url($file, $file);
  7725. }
  7726. $oral_question_list .= '<br />
  7727. <table width="730" height="136" border="0" cellpadding="3" cellspacing="3">
  7728. <tr>
  7729. <td width="220" valign="top" bgcolor="#E5EDF8">&nbsp;&nbsp;'.get_lang('Question').'</td>
  7730. <td width="473" valign="top" bgcolor="#F3F3F3">'.$question.'</td>
  7731. </tr>
  7732. <tr>
  7733. <td width="220" valign="top" bgcolor="#E5EDF8">&nbsp;&nbsp;'.get_lang('Answer').'</td>
  7734. <td valign="top" bgcolor="#F3F3F3">'.$answer.$file.'</td>
  7735. </tr></table>';
  7736. }
  7737. }
  7738. if (!empty($oral_question_list)) {
  7739. $msg = get_lang('OralQuestionsAttempted').'<br /><br />
  7740. '.get_lang('AttemptDetails').' : <br /><br />
  7741. <table>
  7742. <tr>
  7743. <td><em>'.get_lang('CourseName').'</em></td>
  7744. <td>&nbsp;<b>#course#</b></td>
  7745. </tr>
  7746. <tr>
  7747. <td>'.get_lang('TestAttempted').'</td>
  7748. <td>&nbsp;#exercise#</td>
  7749. </tr>
  7750. <tr>
  7751. <td>'.get_lang('StudentName').'</td>
  7752. <td>&nbsp;#firstName# #lastName#</td>
  7753. </tr>
  7754. <tr>
  7755. <td>'.get_lang('StudentEmail').'</td>
  7756. <td>&nbsp;#mail#</td>
  7757. </tr>
  7758. </table>';
  7759. $msg .= '<br />'.sprintf(get_lang('OralQuestionsAttemptedAreX'), $oral_question_list).'<br />';
  7760. $msg1 = str_replace("#exercise#", $this->exercise, $msg);
  7761. $msg = str_replace("#firstName#", $user_info['firstname'], $msg1);
  7762. $msg1 = str_replace("#lastName#", $user_info['lastname'], $msg);
  7763. $msg = str_replace("#mail#", $user_info['email'], $msg1);
  7764. $msg = str_replace("#course#", $courseInfo['name'], $msg1);
  7765. if (!in_array($origin, ['learnpath', 'embeddable'])) {
  7766. $msg .= '<br /><a href="#url#">'.get_lang('ClickToCommentAndGiveFeedback').'</a>';
  7767. }
  7768. $msg1 = str_replace("#url#", $url_email, $msg);
  7769. $mail_content = $msg1;
  7770. $subject = get_lang('OralQuestionsAttempted');
  7771. if (!empty($teachers)) {
  7772. foreach ($teachers as $user_id => $teacher_data) {
  7773. MessageManager::send_message_simple(
  7774. $user_id,
  7775. $subject,
  7776. $mail_content
  7777. );
  7778. }
  7779. }
  7780. }
  7781. }
  7782. /**
  7783. * Returns an array with the media list.
  7784. *
  7785. * @param array question list
  7786. *
  7787. * @example there's 1 question with iid 5 that belongs to the media question with iid = 100
  7788. * <code>
  7789. * array (size=2)
  7790. * 999 =>
  7791. * array (size=3)
  7792. * 0 => int 7
  7793. * 1 => int 6
  7794. * 2 => int 3254
  7795. * 100 =>
  7796. * array (size=1)
  7797. * 0 => int 5
  7798. * </code>
  7799. */
  7800. private function setMediaList($questionList)
  7801. {
  7802. $mediaList = [];
  7803. /*
  7804. * Media feature is not activated in 1.11.x
  7805. if (!empty($questionList)) {
  7806. foreach ($questionList as $questionId) {
  7807. $objQuestionTmp = Question::read($questionId, $this->course_id);
  7808. // If a media question exists
  7809. if (isset($objQuestionTmp->parent_id) && $objQuestionTmp->parent_id != 0) {
  7810. $mediaList[$objQuestionTmp->parent_id][] = $objQuestionTmp->id;
  7811. } else {
  7812. // Always the last item
  7813. $mediaList[999][] = $objQuestionTmp->id;
  7814. }
  7815. }
  7816. }*/
  7817. $this->mediaList = $mediaList;
  7818. }
  7819. /**
  7820. * @param FormValidator $form
  7821. *
  7822. * @return HTML_QuickForm_group
  7823. */
  7824. private function setResultDisabledGroup(FormValidator $form)
  7825. {
  7826. $resultDisabledGroup = [];
  7827. $resultDisabledGroup[] = $form->createElement(
  7828. 'radio',
  7829. 'results_disabled',
  7830. null,
  7831. get_lang('ShowScoreAndRightAnswer'),
  7832. '0',
  7833. ['id' => 'result_disabled_0']
  7834. );
  7835. $resultDisabledGroup[] = $form->createElement(
  7836. 'radio',
  7837. 'results_disabled',
  7838. null,
  7839. get_lang('DoNotShowScoreNorRightAnswer'),
  7840. '1',
  7841. ['id' => 'result_disabled_1', 'onclick' => 'check_results_disabled()']
  7842. );
  7843. $resultDisabledGroup[] = $form->createElement(
  7844. 'radio',
  7845. 'results_disabled',
  7846. null,
  7847. get_lang('OnlyShowScore'),
  7848. '2',
  7849. ['id' => 'result_disabled_2', 'onclick' => 'check_results_disabled()']
  7850. );
  7851. if ($this->selectFeedbackType() == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  7852. $group = $form->addGroup(
  7853. $resultDisabledGroup,
  7854. null,
  7855. get_lang('ShowResultsToStudents')
  7856. );
  7857. return $group;
  7858. }
  7859. $resultDisabledGroup[] = $form->createElement(
  7860. 'radio',
  7861. 'results_disabled',
  7862. null,
  7863. get_lang('ShowScoreEveryAttemptShowAnswersLastAttempt'),
  7864. '4',
  7865. ['id' => 'result_disabled_4']
  7866. );
  7867. $resultDisabledGroup[] = $form->createElement(
  7868. 'radio',
  7869. 'results_disabled',
  7870. null,
  7871. get_lang('DontShowScoreOnlyWhenUserFinishesAllAttemptsButShowFeedbackEachAttempt'),
  7872. '5',
  7873. ['id' => 'result_disabled_5', 'onclick' => 'check_results_disabled()']
  7874. );
  7875. $resultDisabledGroup[] = $form->createElement(
  7876. 'radio',
  7877. 'results_disabled',
  7878. null,
  7879. get_lang('ExerciseRankingMode'),
  7880. '6',
  7881. ['id' => 'result_disabled_6']
  7882. );
  7883. $resultDisabledGroup[] = $form->createElement(
  7884. 'radio',
  7885. 'results_disabled',
  7886. null,
  7887. get_lang('ExerciseShowOnlyGlobalScoreAndCorrectAnswers'),
  7888. '7',
  7889. ['id' => 'result_disabled_7']
  7890. );
  7891. $group = $form->addGroup(
  7892. $resultDisabledGroup,
  7893. null,
  7894. get_lang('ShowResultsToStudents')
  7895. );
  7896. return $group;
  7897. }
  7898. }