exercise.class.php 341 KB

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