exercise.class.php 344 KB

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