exercise.class.php 390 KB

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