survey.lib.php 232 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. $config['survey']['debug'] = false;
  4. $htmlHeadXtra[] = '<script>
  5. function setFocus(){
  6. $("#search_title").focus();
  7. }
  8. $(document).ready(function () {
  9. setFocus();
  10. });
  11. </script>';
  12. /**
  13. * Class survey_manager
  14. * @package chamilo.survey
  15. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University:
  16. * cleanup, refactoring and rewriting large parts (if not all) of the code
  17. * @author Julio Montoya <gugli100@gmail.com>, Personality Test modification
  18. * and rewriting large parts of the code
  19. * @author cfasanando
  20. * @todo move this file to inc/lib
  21. * @todo use consistent naming for the functions (save vs store for instance)
  22. */
  23. class survey_manager
  24. {
  25. /**
  26. * @param $code
  27. *
  28. * @return string
  29. */
  30. public static function generate_unique_code($code)
  31. {
  32. if (empty($code)) {
  33. return false;
  34. }
  35. $course_id = api_get_course_int_id();
  36. $table_survey = Database::get_course_table(TABLE_SURVEY);
  37. $code = Database::escape_string($code);
  38. $num = 0;
  39. $new_code = $code;
  40. while (true) {
  41. $sql = "SELECT * FROM $table_survey
  42. WHERE code = '$new_code' AND c_id = $course_id";
  43. $result = Database::query($sql);
  44. if (Database::num_rows($result)) {
  45. $num++;
  46. $new_code = $code . $num;
  47. } else {
  48. break;
  49. }
  50. }
  51. return $code.$num;
  52. }
  53. /**
  54. * Deletes all survey invitations of a user
  55. * @param int $user_id
  56. *
  57. * @return boolean
  58. * @assert ('') === false
  59. */
  60. public static function delete_all_survey_invitations_by_user($user_id)
  61. {
  62. $user_id = intval($user_id);
  63. if (empty($user_id)) {
  64. return false;
  65. }
  66. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  67. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  68. $sql = "SELECT survey_invitation_id, survey_code
  69. FROM $table_survey_invitation WHERE user = '$user_id' AND c_id <> 0 ";
  70. $result = Database::query($sql);
  71. while ($row = Database::fetch_array($result ,'ASSOC')){
  72. $survey_invitation_id = $row['survey_invitation_id'];
  73. $survey_code = $row['survey_code'];
  74. $sql2 = "DELETE FROM $table_survey_invitation
  75. WHERE survey_invitation_id = '$survey_invitation_id' AND c_id <> 0";
  76. if (Database::query($sql2)) {
  77. $sql3 = "UPDATE $table_survey SET
  78. invited = invited-1
  79. WHERE c_id <> 0 AND code ='$survey_code'";
  80. Database::query($sql3);
  81. }
  82. }
  83. }
  84. /**
  85. *
  86. * @param string $course_code
  87. * @param int $session_id
  88. * @return type
  89. * @assert ('') === false
  90. */
  91. public static function get_surveys($course_code, $session_id = 0)
  92. {
  93. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  94. if (empty($course_code)) {
  95. return false;
  96. }
  97. $course_info = api_get_course_info($course_code);
  98. $session_condition = api_get_session_condition($session_id, true, true);
  99. $sql = "SELECT * FROM $table_survey
  100. WHERE c_id = {$course_info['real_id']} $session_condition ";
  101. $result = Database::query($sql);
  102. $result = Database::store_result($result, 'ASSOC');
  103. return $result;
  104. }
  105. /**
  106. * Retrieves all the survey information
  107. *
  108. * @param integer $survey_id the id of the survey
  109. * @param boolean $shared this parameter determines if
  110. * we have to get the information of a survey from the central (shared) database or from the
  111. * course database
  112. * @param string course code optional
  113. *
  114. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  115. * @version February 2007
  116. * @assert ('') === false
  117. *
  118. * @todo this is the same function as in create_new_survey.php
  119. */
  120. public static function get_survey($survey_id, $shared = 0, $course_code = '', $simple_return = false)
  121. {
  122. // Table definition
  123. if (!empty($course_code)) {
  124. $my_course_id = $course_code;
  125. } else if (isset($_GET['course'])) {
  126. $my_course_id = Security::remove_XSS($_GET['course']);
  127. } else {
  128. $my_course_id = api_get_course_id();
  129. }
  130. $my_course_info = api_get_course_info($my_course_id);
  131. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  132. if ($shared != 0) {
  133. $table_survey = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  134. $sql = "SELECT * FROM $table_survey
  135. WHERE survey_id='".intval($survey_id)."' ";
  136. } else {
  137. $sql = "SELECT * FROM $table_survey
  138. WHERE
  139. survey_id='".intval($survey_id)."' AND
  140. c_id = ".$my_course_info['real_id'];
  141. }
  142. $result = Database::query($sql);
  143. $return = array();
  144. if (Database::num_rows($result)> 0) {
  145. $return = Database::fetch_array($result,'ASSOC');
  146. if ($simple_return) {
  147. return $return;
  148. }
  149. // We do this (temporarily) to have the array match the quickform elements immediately
  150. // idealiter the fields in the db match the quickform fields
  151. $return['survey_code'] = $return['code'];
  152. $return['survey_title'] = $return['title'];
  153. $return['survey_subtitle'] = $return['subtitle'];
  154. $return['survey_language'] = $return['lang'];
  155. $return['start_date'] = $return['avail_from'];
  156. $return['end_date'] = $return['avail_till'];
  157. $return['survey_share'] = $return['is_shared'];
  158. $return['survey_introduction'] = $return['intro'];
  159. $return['survey_thanks'] = $return['surveythanks'];
  160. $return['survey_type'] = $return['survey_type'];
  161. $return['one_question_per_page']= $return['one_question_per_page'];
  162. $return['show_form_profile'] = $return['show_form_profile'];
  163. $return['input_name_list'] = isset($return['input_name_list']) ? $return['input_name_list'] : null;
  164. $return['shuffle'] = $return['shuffle'];
  165. $return['parent_id'] = $return['parent_id'];
  166. $return['survey_version'] = $return['survey_version'];
  167. $return['anonymous'] = $return['anonymous'];
  168. }
  169. return $return;
  170. }
  171. /**
  172. * This function stores a survey in the database.
  173. *
  174. * @param array $values
  175. *
  176. * @return array $return the type of return message that has to be displayed and the message in it
  177. *
  178. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  179. * @version February 2007
  180. */
  181. public static function store_survey($values)
  182. {
  183. $_user = api_get_user_info();
  184. $course_id = api_get_course_int_id();
  185. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  186. $shared_survey_id = 0;
  187. if (!isset($values['survey_id'])) {
  188. // Check if the code doesn't soon exists in this language
  189. $sql = 'SELECT 1 FROM '.$table_survey.'
  190. WHERE
  191. c_id = '.$course_id.' AND
  192. code="'.Database::escape_string($values['survey_code']).'" AND
  193. lang="'.Database::escape_string($values['survey_language']).'"';
  194. $rs = Database::query($sql);
  195. if (Database::num_rows($rs) > 0) {
  196. $return['message'] = 'ThisSurveyCodeSoonExistsInThisLanguage';
  197. $return['type'] = 'error';
  198. $return['id'] = isset($values['survey_id']) ? $values['survey_id'] : 0;
  199. return $return;
  200. }
  201. if (!isset($values['anonymous'])) {
  202. $values['anonymous'] = 0;
  203. }
  204. $values['anonymous'] = intval($values['anonymous']);
  205. $additional['columns'] = '';
  206. $additional['values'] = '';
  207. if ($values['anonymous'] == 0) {
  208. // Input_name_list
  209. $additional['columns'] .= ', show_form_profile';
  210. $values['show_form_profile'] = isset($values['show_form_profile']) ? $values['show_form_profile'] : null;
  211. $additional['values'] .= ",'".Database::escape_string($values['show_form_profile'])."'";
  212. if ($values['show_form_profile'] == 1) {
  213. // Input_name_list
  214. $fields = explode(',', $values['input_name_list']);
  215. $field_values = '';
  216. foreach ($fields as & $field) {
  217. if ($field != '') {
  218. if ($values[$field] == '') {
  219. $values[$field] = 0;
  220. }
  221. $field_values.= $field.':'.$values[$field].'@';
  222. }
  223. }
  224. $additional['columns'] .= ', form_fields';
  225. $additional['values'] .= ",'".Database::escape_string($field_values)."'";
  226. } else {
  227. $additional['columns'] .= ', form_fields';
  228. $additional['values'] .= ",''";
  229. }
  230. } else {
  231. // Input_name_list
  232. $additional['columns'] .= ', show_form_profile';
  233. $additional['values'] .= ",'0'";
  234. $additional['columns'] .= ', form_fields';
  235. $additional['values'] .= ",''";
  236. }
  237. if ($values['survey_type'] == 1) {
  238. $additional['columns'] .= ', survey_type';
  239. $additional['values'] .= ",'1'";
  240. $additional['columns'] .= ', shuffle';
  241. $additional['values'] .= ",'".Database::escape_string($values['shuffle'])."'";
  242. $additional['columns'] .= ', one_question_per_page';
  243. $additional['values'] .= ",'".Database::escape_string($values['one_question_per_page'])."'";
  244. $additional['columns'] .= ', parent_id';
  245. $additional['values'] .= ",'".Database::escape_string($values['parent_id'])."'";
  246. // Logic for versioning surveys
  247. if (!empty($values['parent_id'])) {
  248. $additional['columns'] .= ', survey_version';
  249. $sql = 'SELECT survey_version
  250. FROM '.$table_survey.'
  251. WHERE
  252. c_id = '.$course_id.' AND
  253. parent_id = '.intval($values['parent_id']).'
  254. ORDER BY survey_version DESC
  255. LIMIT 1';
  256. $rs = Database::query($sql);
  257. if (Database::num_rows($rs) === 0) {
  258. $sql = 'SELECT survey_version FROM '.$table_survey.'
  259. WHERE
  260. c_id = '.$course_id.' AND
  261. survey_id = '.intval($values['parent_id']);
  262. $rs = Database::query($sql);
  263. $getversion = Database::fetch_array($rs, 'ASSOC');
  264. if (empty($getversion['survey_version'])) {
  265. $additional['values'] .= ",'".++$getversion['survey_version']."'";
  266. } else {
  267. $additional['values'] .= ",'".$getversion['survey_version'].".1'";
  268. }
  269. } else {
  270. $row = Database::fetch_array($rs, 'ASSOC');
  271. $pos = api_strpos($row['survey_version']);
  272. if ($pos === false) {
  273. $row['survey_version'] = $row['survey_version'] + 1;
  274. $additional['values'] .= ",'".$row['survey_version']."'";
  275. } else {
  276. $getlast = api_split('\.', $row['survey_version']);
  277. $lastversion = array_pop($getlast);
  278. $lastversion = $lastversion + 1;
  279. $add = implode('.', $getlast);
  280. if ($add != '') {
  281. $insertnewversion = $add.'.'.$lastversion;
  282. } else {
  283. $insertnewversion = $lastversion;
  284. }
  285. $additional['values'] .= ",'".$insertnewversion."'";
  286. }
  287. }
  288. }
  289. }
  290. $course_id = api_get_course_int_id();
  291. $sql = "INSERT INTO $table_survey (c_id, code, title, subtitle, author, lang, avail_from, avail_till, is_shared, template, intro, surveythanks, creation_date, anonymous".$additional['columns'].", session_id) VALUES (
  292. $course_id,
  293. '".Database::escape_string(strtolower(generate_course_code(api_substr($values['survey_code'],0))))."',
  294. '".Database::escape_string($values['survey_title'])."',
  295. '".Database::escape_string($values['survey_subtitle'])."',
  296. '".intval($_user['user_id'])."',
  297. '".Database::escape_string($values['survey_language'])."',
  298. '".Database::escape_string($values['start_date'])."',
  299. '".Database::escape_string($values['end_date'])."',
  300. '".Database::escape_string($shared_survey_id)."',
  301. '".Database::escape_string('template')."',
  302. '".Database::escape_string($values['survey_introduction'])."',
  303. '".Database::escape_string($values['survey_thanks'])."',
  304. '".api_get_utc_datetime()."',
  305. '".Database::escape_string($values['anonymous'])."'".$additional['values'].",
  306. ".api_get_session_id()."
  307. )";
  308. Database::query($sql);
  309. $survey_id = Database::insert_id();
  310. if ($survey_id > 0) {
  311. // Insert into item_property
  312. api_item_property_update(api_get_course_info(), TOOL_SURVEY, $survey_id, 'SurveyAdded', api_get_user_id());
  313. }
  314. if ($values['survey_type'] == 1 && !empty($values['parent_id'])) {
  315. survey_manager::copy_survey($values['parent_id'], $survey_id);
  316. }
  317. $return['message'] = 'SurveyCreatedSuccesfully';
  318. $return['type'] = 'confirmation';
  319. $return['id'] = $survey_id;
  320. } else {
  321. // Check whether the code doesn't soon exists in this language
  322. $sql = 'SELECT 1 FROM '.$table_survey.'
  323. WHERE
  324. c_id = '.$course_id.' AND
  325. code = "'.Database::escape_string($values['survey_code']).'" AND
  326. lang = "'.Database::escape_string($values['survey_language']).'" AND
  327. survey_id !='.intval($values['survey_id']);
  328. $rs = Database::query($sql);
  329. if (Database::num_rows($rs) > 0) {
  330. $return['message'] = 'ThisSurveyCodeSoonExistsInThisLanguage';
  331. $return['type'] = 'error';
  332. $return['id'] = isset($values['survey_id']) ? $values['survey_id'] : 0;
  333. return $return;
  334. }
  335. if (!isset($values['anonymous']) ||
  336. isset($values['anonymous']) && $values['anonymous'] == ''
  337. ) {
  338. $values['anonymous'] = 0;
  339. }
  340. $values['shuffle'] = isset($values['shuffle']) ? $values['shuffle'] : null;
  341. $values['one_question_per_page'] = isset($values['one_question_per_page']) ? $values['one_question_per_page'] : null;
  342. $values['show_form_profile'] = isset($values['show_form_profile']) ? $values['show_form_profile'] : null;
  343. $additionalsets = ", shuffle = '".Database::escape_string($values['shuffle'])."'";
  344. $additionalsets .= ", one_question_per_page = '".Database::escape_string($values['one_question_per_page'])."'";
  345. if ($values['anonymous'] == 0) {
  346. $additionalsets .= ", show_form_profile = '".Database::escape_string($values['show_form_profile'])."'";
  347. if ($values['show_form_profile'] == 1) {
  348. $fields = explode(',',$values['input_name_list']);
  349. $field_values = '';
  350. foreach ($fields as & $field) {
  351. if ($field != '') {
  352. if ($values[$field] == '') {
  353. $values[$field] = 0;
  354. }
  355. $field_values.= $field.':'.$values[$field].'@';
  356. }
  357. }
  358. $additionalsets .= ", form_fields = '".Database::escape_string($field_values)."'";
  359. } else {
  360. $additionalsets .= ", form_fields = '' ";
  361. }
  362. } else {
  363. $additionalsets .= ", show_form_profile = '0'";
  364. $additionalsets .= ", form_fields = '' ";
  365. }
  366. $sql = "UPDATE $table_survey SET
  367. title = '".Database::escape_string($values['survey_title'])."',
  368. subtitle = '".Database::escape_string($values['survey_subtitle'])."',
  369. author = '".Database::escape_string($_user['user_id'])."',
  370. lang = '".Database::escape_string($values['survey_language'])."',
  371. avail_from = '".Database::escape_string($values['start_date'])."',
  372. avail_till = '".Database::escape_string($values['end_date'])."',
  373. is_shared = '".Database::escape_string($shared_survey_id)."',
  374. template = '".Database::escape_string('template')."',
  375. intro = '".Database::escape_string($values['survey_introduction'])."',
  376. surveythanks = '".Database::escape_string($values['survey_thanks'])."',
  377. anonymous = '".Database::escape_string($values['anonymous'])."'".$additionalsets."
  378. WHERE c_id = $course_id AND survey_id = '".intval($values['survey_id'])."'";
  379. Database::query($sql);
  380. // Update into item_property (update)
  381. api_item_property_update(api_get_course_info(), TOOL_SURVEY, $values['survey_id'], 'SurveyUpdated', api_get_user_id());
  382. $return['message'] = 'SurveyUpdatedSuccesfully';
  383. $return['type'] = 'confirmation';
  384. $return['id'] = $values['survey_id'];
  385. }
  386. return $return;
  387. }
  388. /**
  389. * This function stores a shared survey in the central database.
  390. *
  391. * @param array $values
  392. * @return array $return the type of return message that has to be displayed and the message in it
  393. *
  394. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  395. * @version February 2007
  396. */
  397. public function store_shared_survey($values)
  398. {
  399. $_user = api_get_user_info();
  400. $_course = api_get_course_info();
  401. // Table definitions
  402. $table_survey = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY);
  403. if (!$values['survey_id'] ||
  404. !is_numeric($values['survey_id']) ||
  405. $values['survey_share']['survey_share'] == 'true'
  406. ) {
  407. $sql = "INSERT INTO $table_survey (code, title, subtitle, author, lang, template, intro, surveythanks, creation_date, course_code) VALUES (
  408. '".Database::escape_string($values['survey_code'])."',
  409. '".Database::escape_string($values['survey_title'])."',
  410. '".Database::escape_string($values['survey_subtitle'])."',
  411. '".intval($_user['user_id'])."',
  412. '".Database::escape_string($values['survey_language'])."',
  413. '".Database::escape_string('template')."',
  414. '".Database::escape_string($values['survey_introduction'])."',
  415. '".Database::escape_string($values['survey_thanks'])."',
  416. '".date('Y-m-d H:i:s')."',
  417. '".$_course['id']."')";
  418. Database::query($sql);
  419. $return = Database::insert_id();
  420. } else {
  421. $sql = "UPDATE $table_survey SET
  422. code = '".Database::escape_string($values['survey_code'])."',
  423. title = '".Database::escape_string($values['survey_title'])."',
  424. subtitle = '".Database::escape_string($values['survey_subtitle'])."',
  425. author = '".intval($_user['user_id'])."',
  426. lang = '".Database::escape_string($values['survey_language'])."',
  427. template = '".Database::escape_string('template')."',
  428. intro = '".Database::escape_string($values['survey_introduction'])."',
  429. surveythanks = '".Database::escape_string($values['survey_thanks'])."'
  430. WHERE survey_id = '".Database::escape_string($values['survey_share']['survey_share'])."'";
  431. Database::query($sql);
  432. $return = $values['survey_share']['survey_share'];
  433. }
  434. return $return;
  435. }
  436. /**
  437. * This function deletes a survey (and also all the question in that survey
  438. *
  439. * @param int $survey_id id of the survey that has to be deleted
  440. * @return true
  441. *
  442. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  443. * @version January 2007
  444. */
  445. public static function delete_survey($survey_id, $shared = false, $course_id = '')
  446. {
  447. // Database table definitions
  448. if (empty($course_id)) {
  449. $course_id = api_get_course_int_id();
  450. }
  451. $survey_id = intval($survey_id);
  452. if (empty($survey_id)) {
  453. return false;
  454. }
  455. $course_info = api_get_course_info_by_id($course_id);
  456. $course_id = $course_info['real_id'];
  457. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  458. $table_survey_question_group = Database :: get_course_table(TABLE_SURVEY_QUESTION_GROUP);
  459. if ($shared) {
  460. $table_survey = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY);
  461. // Deleting the survey
  462. $sql = "DELETE FROM $table_survey WHERE survey_id='".$survey_id."'";
  463. Database::query($sql);
  464. } else {
  465. $sql = "DELETE FROM $table_survey WHERE c_id = $course_id AND survey_id='".$survey_id."'";
  466. Database::query($sql);
  467. }
  468. // Deleting groups of this survey
  469. $sql = "DELETE FROM $table_survey_question_group WHERE c_id = $course_id AND survey_id='".$survey_id."'";
  470. Database::query($sql);
  471. // Deleting the questions of the survey
  472. survey_manager::delete_all_survey_questions($survey_id, $shared);
  473. // Update into item_property (delete)
  474. api_item_property_update($course_info, TOOL_SURVEY, $survey_id, 'SurveyDeleted', api_get_user_id());
  475. return true;
  476. }
  477. /**
  478. * @param int $survey_id
  479. * @param int $new_survey_id
  480. *
  481. * @return bool
  482. */
  483. public static function copy_survey($survey_id, $new_survey_id = null)
  484. {
  485. $course_id = api_get_course_int_id();
  486. // Database table definitions
  487. $table_survey = Database::get_course_table(TABLE_SURVEY);
  488. $table_survey_question_group = Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP);
  489. $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
  490. $table_survey_options = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  491. $survey_id = intval($survey_id);
  492. // Get groups
  493. $survey_data = self::get_survey($survey_id, 0, null, true);
  494. if (empty($survey_data)) {
  495. return true;
  496. }
  497. if (empty($new_survey_id)) {
  498. $params = $survey_data;
  499. $params['code'] = self::generate_unique_code($params['code']);
  500. $params['c_id'] = $course_id;
  501. unset($params['survey_id']);
  502. $params['session_id'] = api_get_session_id();
  503. $params['title'] = $params['title'] . ' ' . get_lang('Copy');
  504. Database::insert($table_survey, $params);
  505. $new_survey_id = Database::insert_id();
  506. // Insert into item_property
  507. api_item_property_update(api_get_course_info(), TOOL_SURVEY, $new_survey_id, 'SurveyAdded', api_get_user_id());
  508. } else {
  509. $new_survey_id = intval($new_survey_id);
  510. }
  511. $sql = "SELECT * FROM $table_survey_question_group
  512. WHERE c_id = $course_id AND survey_id='".$survey_id."'";
  513. $res = Database::query($sql);
  514. while($row = Database::fetch_array($res, 'ASSOC')) {
  515. $params = array(
  516. 'c_id' => $course_id,
  517. 'name' => $row['name'],
  518. 'description' => $row['description'],
  519. 'survey_id' => $new_survey_id
  520. );
  521. $insertId = Database::insert($table_survey_question_group, $params);
  522. $group_id[$row['id']] = $insertId;
  523. }
  524. // Get questions
  525. $sql = "SELECT * FROM $table_survey_question
  526. WHERE c_id = $course_id AND survey_id='".$survey_id."'";
  527. $res = Database::query($sql);
  528. while ($row = Database::fetch_array($res, 'ASSOC')) {
  529. $params = array(
  530. 'c_id' => $course_id,
  531. 'survey_id' => $new_survey_id,
  532. 'survey_question' => $row['survey_question'],
  533. 'survey_question_comment' => $row['survey_question_comment'],
  534. 'type' => $row['type'],
  535. 'display' => $row['display'],
  536. 'sort' => $row['sort'],
  537. 'shared_question_id' => $row['shared_question_id'],
  538. 'max_value' => $row['max_value'],
  539. 'survey_group_pri' => $row['survey_group_pri'],
  540. 'survey_group_sec1' => $row['survey_group_sec1'],
  541. 'survey_group_sec2' => $row['survey_group_sec2']
  542. );
  543. $insertId = Database::insert($table_survey_question, $params);
  544. $question_id[$row['question_id']] = $insertId;
  545. }
  546. // Get questions options
  547. $sql = "SELECT * FROM $table_survey_options
  548. WHERE c_id = $course_id AND survey_id='".$survey_id."'";
  549. $res = Database::query($sql);
  550. while ($row = Database::fetch_array($res ,'ASSOC')) {
  551. $params = array(
  552. 'c_id' => $course_id,
  553. 'question_id' => $question_id[$row['question_id']],
  554. 'survey_id' => $new_survey_id,
  555. 'option_text' => $row['option_text'],
  556. 'sort' => $row['sort'],
  557. 'value' => $row['value']
  558. );
  559. Database::insert($table_survey_options, $params);
  560. }
  561. return true;
  562. }
  563. /**
  564. * This function duplicates a survey (and also all the question in that survey
  565. *
  566. * @param int $survey_id id of the survey that has to be duplicated
  567. * @return true
  568. *
  569. * @author Eric Marguin <e.marguin@elixir-interactive.com>, Elixir Interactive
  570. * @version October 2007
  571. */
  572. public static function empty_survey($survey_id)
  573. {
  574. // Database table definitions
  575. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  576. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  577. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  578. $course_id = api_get_course_int_id();
  579. $datas = survey_manager::get_survey($survey_id);
  580. $session_where = '';
  581. if (api_get_session_id() != 0) {
  582. $session_where = ' AND session_id = "'.api_get_session_id().'" ';
  583. }
  584. $sql = 'DELETE FROM '.$table_survey_invitation.'
  585. WHERE c_id = '.$course_id.' AND survey_code = "'.Database::escape_string($datas['code']).'" '.$session_where.' ';
  586. Database::query($sql);
  587. $sql = 'DELETE FROM '.$table_survey_answer.'
  588. WHERE c_id = '.$course_id.' AND survey_id='.intval($survey_id);
  589. Database::query($sql);
  590. $sql = 'UPDATE '.$table_survey.' SET invited=0, answered=0
  591. WHERE c_id = '.$course_id.' AND survey_id='.intval($survey_id);
  592. Database::query($sql);
  593. return true;
  594. }
  595. /**
  596. * This function recalculates the number of people who have taken the survey (=filled at least one question)
  597. *
  598. * @param int $survey_id the id of the survey somebody
  599. * @return true
  600. *
  601. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  602. * @version February 2007
  603. */
  604. public static function update_survey_answered($survey_data, $user, $survey_code)
  605. {
  606. // Database table definitions
  607. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  608. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  609. $survey_id = $survey_data['survey_id'];
  610. $course_id = $survey_data['c_id'];
  611. $session_id = $survey_data['session_id'];
  612. // Getting a list with all the people who have filled the survey
  613. $people_filled = survey_manager::get_people_who_filled_survey($survey_id, false, $course_id);
  614. $number = intval(count($people_filled));
  615. // Storing this value in the survey table
  616. $sql = "UPDATE $table_survey
  617. SET answered = $number
  618. WHERE
  619. c_id = $course_id AND
  620. survey_id = ".intval($survey_id);
  621. Database::query($sql);
  622. // Storing that the user has finished the survey.
  623. $sql = "UPDATE $table_survey_invitation SET answered='1'
  624. WHERE
  625. c_id = $course_id AND
  626. session_id='".$session_id."' AND
  627. user='".Database::escape_string($user)."' AND
  628. survey_code='".Database::escape_string($survey_code)."'";
  629. Database::query($sql);
  630. }
  631. /**
  632. * Gets a complete structure of a survey
  633. * (all survey information, all question information of all the questions
  634. * and all the options of all the questions.
  635. *
  636. * @param integer $survey_id the id of the survey
  637. * @param boolean $shared this parameter determines if we have to get the
  638. * information of a survey from the central (shared) database or from the
  639. * course database
  640. *
  641. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  642. * @version February 2007
  643. */
  644. public static function get_complete_survey_structure($survey_id, $shared = 0)
  645. {
  646. $structure = survey_manager::get_survey($survey_id, $shared);
  647. $structure['questions'] = survey_manager::get_questions($survey_id);
  648. }
  649. /***
  650. * SURVEY QUESTION FUNCTIONS
  651. */
  652. /**
  653. * This function return the "icon" of the question type
  654. *
  655. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  656. * @version February 2007
  657. */
  658. public static function icon_question($type)
  659. {
  660. // the possible question types
  661. $possible_types = array(
  662. 'personality', 'yesno', 'multiplechoice', 'multipleresponse', 'open', 'dropdown', 'comment', 'pagebreak', 'percentage', 'score'
  663. );
  664. // the images array
  665. $icon_question = array(
  666. 'yesno' => 'yesno.gif',
  667. 'personality' => 'yesno.gif',
  668. 'multiplechoice' => 'mcua.png',
  669. 'multipleresponse' => 'mcma.png',
  670. 'open' => 'open_answer.png',
  671. 'dropdown' => 'dropdown.gif',
  672. 'percentage' => 'percentagequestion.gif',
  673. 'score' => 'scorequestion.gif',
  674. 'comment' => 'commentquestion.gif',
  675. 'pagebreak' => 'page_end.gif',
  676. );
  677. if (in_array($type, $possible_types)) {
  678. return $icon_question[$type];
  679. } else {
  680. return false;
  681. }
  682. }
  683. /**
  684. * This function retrieves all the information of a question
  685. *
  686. * @param integer $question_id the id of the question
  687. * @return array
  688. *
  689. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  690. * @version January 2007
  691. *
  692. * @todo one sql call should do the trick
  693. */
  694. public static function get_question($question_id, $shared = false)
  695. {
  696. // Table definitions
  697. $tbl_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  698. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  699. $course_id = api_get_course_int_id();
  700. $sql = "SELECT * FROM $tbl_survey_question
  701. WHERE c_id = $course_id AND question_id='".intval($question_id)."'
  702. ORDER BY `sort` ";
  703. $sqlOption = " SELECT * FROM $table_survey_question_option
  704. WHERE c_id = $course_id AND question_id='".intval($question_id)."'
  705. ORDER BY `sort` ";
  706. if ($shared) {
  707. $tbl_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  708. $table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  709. $sql = "SELECT * FROM $tbl_survey_question
  710. WHERE question_id='".intval($question_id)."'
  711. ORDER BY `sort` ";
  712. $sqlOption = "SELECT * FROM $table_survey_question_option
  713. WHERE question_id='".intval($question_id)."'
  714. ORDER BY `sort` ";
  715. }
  716. // Getting the information of the question
  717. $result = Database::query($sql);
  718. $row = Database::fetch_array($result,'ASSOC');
  719. $return['survey_id'] = $row['survey_id'];
  720. $return['question_id'] = $row['question_id'];
  721. $return['type'] = $row['type'];
  722. $return['question'] = $row['survey_question'];
  723. $return['horizontalvertical'] = $row['display'];
  724. $return['shared_question_id'] = $row['shared_question_id'];
  725. $return['maximum_score'] = $row['max_value'];
  726. if ($row['survey_group_pri'] != 0) {
  727. $return['assigned'] = $row['survey_group_pri'];
  728. $return['choose'] = 1;
  729. } else {
  730. $return['assigned1'] = $row['survey_group_sec1'];
  731. $return['assigned2'] = $row['survey_group_sec2'];
  732. $return['choose'] = 2;
  733. }
  734. // Getting the information of the question options
  735. $result = Database::query($sqlOption);
  736. while ($row = Database::fetch_array($result, 'ASSOC')) {
  737. /** @todo this should be renamed to options instead of answers */
  738. $return['answers'][] = $row['option_text'];
  739. $return['values'][] = $row['value'];
  740. /** @todo this can be done more elegantly (used in reporting) */
  741. $return['answersid'][] = $row['question_option_id'];
  742. }
  743. return $return;
  744. }
  745. /**
  746. * This function gets all the question of any given survey
  747. *
  748. * @param integer $survey_id the id of the survey
  749. * @return array containing all the questions of the survey
  750. *
  751. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  752. * @version February 2007
  753. *
  754. * @todo one sql call should do the trick
  755. */
  756. public static function get_questions($survey_id, $course_id = '')
  757. {
  758. // Table definitions
  759. $tbl_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  760. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  761. if (empty($course_id)) {
  762. $course_id = api_get_course_int_id();
  763. }
  764. $return = array();
  765. // Getting the information of the question
  766. $sql = "SELECT * FROM $tbl_survey_question
  767. WHERE c_id = $course_id AND survey_id='".intval($survey_id)."'";
  768. $result = Database::query($sql);
  769. while ($row = Database::fetch_array($result, 'ASSOC')) {
  770. $return[$row['question_id']]['survey_id'] = $row['survey_id'];
  771. $return[$row['question_id']]['question_id'] = $row['question_id'];
  772. $return[$row['question_id']]['type'] = $row['type'];
  773. $return[$row['question_id']]['question'] = $row['survey_question'];
  774. $return[$row['question_id']]['horizontalvertical'] = $row['display'];
  775. $return[$row['question_id']]['maximum_score'] = $row['max_value'];
  776. $return[$row['question_id']]['sort'] = $row['sort'];
  777. }
  778. // Getting the information of the question options
  779. $sql = "SELECT * FROM $table_survey_question_option
  780. WHERE c_id = $course_id AND survey_id='".intval($survey_id)."'";
  781. $result = Database::query($sql);
  782. while ($row = Database::fetch_array($result, 'ASSOC')) {
  783. $return[$row['question_id']]['answers'][] = $row['option_text'];
  784. }
  785. return $return;
  786. }
  787. /**
  788. * This function saves a question in the database.
  789. * This can be either an update of an existing survey or storing a new survey
  790. * @param array $survey_data
  791. * @param array $form_content all the information of the form
  792. *
  793. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  794. * @version January 2007
  795. */
  796. public static function save_question($survey_data, $form_content)
  797. {
  798. if (strlen($form_content['question']) > 1) {
  799. // Checks length of the question
  800. $empty_answer = false;
  801. if ($survey_data['survey_type'] == 1) {
  802. if (empty($form_content['choose'])) {
  803. $return_message = 'PleaseChooseACondition';
  804. return $return_message;
  805. }
  806. if (($form_content['choose']==2)&&($form_content['assigned1'] == $form_content['assigned2'])) {
  807. $return_message = 'ChooseDifferentCategories';
  808. return $return_message;
  809. }
  810. }
  811. if ($form_content['type'] != 'percentage') {
  812. for ($i = 0; $i < count($form_content['answers']); $i++) {
  813. if (strlen($form_content['answers'][$i]) < 1) {
  814. $empty_answer = true;
  815. break;
  816. }
  817. }
  818. }
  819. if ($form_content['type'] == 'score') {
  820. if (strlen($form_content['maximum_score']) < 1) {
  821. $empty_answer = true;
  822. }
  823. }
  824. $additional = array();
  825. $course_id = api_get_course_int_id();
  826. if (!$empty_answer) {
  827. // Table definitions
  828. $tbl_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  829. // Getting all the information of the survey
  830. $survey_data = survey_manager::get_survey($form_content['survey_id']);
  831. // Storing the question in the shared database
  832. if (is_numeric($survey_data['survey_share']) && $survey_data['survey_share'] != 0) {
  833. $shared_question_id = survey_manager::save_shared_question($form_content, $survey_data);
  834. $form_content['shared_question_id'] = $shared_question_id;
  835. }
  836. // Storing a new question
  837. if ($form_content['question_id'] == '' || !is_numeric($form_content['question_id'])) {
  838. // Finding the max sort order of the questions in the given survey
  839. $sql = "SELECT max(sort) AS max_sort
  840. FROM $tbl_survey_question
  841. WHERE c_id = $course_id AND survey_id='".intval($form_content['survey_id'])."'";
  842. $result = Database::query($sql);
  843. $row = Database::fetch_array($result,'ASSOC');
  844. $max_sort = $row['max_sort'];
  845. // Some variables defined for survey-test type
  846. $additional['column'] = '';
  847. $additional['value'] = '';
  848. if (isset($_POST['choose'])) {
  849. if ($_POST['choose'] == 1) {
  850. $additional['column'] = ',survey_group_pri';
  851. $additional['value'] = ",'" . Database::escape_string($_POST['assigned']) . "'";
  852. } elseif ($_POST['choose'] == 2) {
  853. $additional['column'] = ',survey_group_sec1, survey_group_sec2';
  854. $additional['value'] = ",'" . Database::escape_string($_POST['assigned1']) . "'" . ",'" . Database::escape_string($_POST['assigned2']) . "'";
  855. }
  856. }
  857. // Adding the question to the survey_question table
  858. $sql = "INSERT INTO $tbl_survey_question
  859. (c_id, survey_id,survey_question,survey_question_comment,type,display, sort, shared_question_id, max_value".$additional['column'].")
  860. VALUES (
  861. $course_id,
  862. '".Database::escape_string($form_content['survey_id'])."',
  863. '".Database::escape_string($form_content['question'])."',
  864. '".Database::escape_string($form_content['question_comment'])."',
  865. '".Database::escape_string($form_content['type'])."',
  866. '".Database::escape_string($form_content['horizontalvertical'])."',
  867. '".Database::escape_string($max_sort+1)."',
  868. '".Database::escape_string($form_content['shared_question_id'])."',
  869. '".Database::escape_string($form_content['maximum_score'])."'".
  870. $additional['value']."
  871. )";
  872. Database::query($sql);
  873. $question_id = Database::insert_id();
  874. $form_content['question_id'] = $question_id;
  875. $return_message = 'QuestionAdded';
  876. } else {
  877. // Updating an existing question
  878. $additionalsets = '';
  879. if (isset($_POST['choose'])) {
  880. if ($_POST['choose'] == 1) {
  881. $additionalsets = ',survey_group_pri = \''.Database::escape_string($_POST['assigned']).'\', survey_group_sec1 = \'0\', survey_group_sec2 = \'0\' ';
  882. } elseif ($_POST['choose'] == 2) {
  883. $additionalsets = ',survey_group_pri = \'0\', survey_group_sec1 = \''.Database::escape_string($_POST['assigned1']).'\', survey_group_sec2 = \''.Database::escape_string($_POST['assigned2']).'\' ';
  884. }
  885. }
  886. $setadditionals = $additional['set'][1].$additional['set'][2].$additional['set'][3];
  887. // Adding the question to the survey_question table
  888. $sql = "UPDATE $tbl_survey_question SET
  889. survey_question = '".Database::escape_string($form_content['question'])."',
  890. survey_question_comment = '".Database::escape_string($form_content['question_comment'])."',
  891. display = '".Database::escape_string($form_content['horizontalvertical'])."',
  892. max_value = '".Database::escape_string($form_content['maximum_score'])."'" .
  893. $additionalsets."
  894. WHERE c_id = $course_id AND question_id = '".intval($form_content['question_id'])."'";
  895. Database::query($sql);
  896. $return_message = 'QuestionUpdated';
  897. }
  898. if (!empty($form_content['survey_id'])) {
  899. //Updating survey
  900. api_item_property_update(
  901. api_get_course_info(),
  902. TOOL_SURVEY,
  903. $form_content['survey_id'],
  904. 'SurveyUpdated',
  905. api_get_user_id()
  906. );
  907. }
  908. // Storing the options of the question
  909. survey_manager::save_question_options($form_content, $survey_data);
  910. } else {
  911. $return_message = 'PleasFillAllAnswer';
  912. }
  913. } else {
  914. $return_message = 'PleaseEnterAQuestion';
  915. }
  916. return $return_message;
  917. }
  918. /**
  919. * This function saves the question in the shared database
  920. *
  921. * @param array $form_content all the information of the form
  922. * @param array $survey_data all the information of the survey
  923. *
  924. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  925. * @version February 2007
  926. *
  927. * @todo editing of a shared question
  928. */
  929. public function save_shared_question($form_content, $survey_data)
  930. {
  931. $_course = api_get_course_info();
  932. // Table definitions
  933. $tbl_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  934. // Storing a new question
  935. if ($form_content['shared_question_id'] == '' || !is_numeric($form_content['shared_question_id'])) {
  936. // Finding the max sort order of the questions in the given survey
  937. $sql = "SELECT max(sort) AS max_sort FROM $tbl_survey_question
  938. WHERE survey_id='".intval($survey_data['survey_share'])."'
  939. AND code='".Database::escape_string($_course['id'])."'";
  940. $result = Database::query($sql);
  941. $row = Database::fetch_array($result,'ASSOC');
  942. $max_sort = $row['max_sort'];
  943. // Adding the question to the survey_question table
  944. $sql = "INSERT INTO $tbl_survey_question (survey_id, survey_question, survey_question_comment, type, display, sort, code) VALUES (
  945. '".Database::escape_string($survey_data['survey_share'])."',
  946. '".Database::escape_string($form_content['question'])."',
  947. '".Database::escape_string($form_content['question_comment'])."',
  948. '".Database::escape_string($form_content['type'])."',
  949. '".Database::escape_string($form_content['horizontalvertical'])."',
  950. '".Database::escape_string($max_sort+1)."',
  951. '".Database::escape_string($_course['id'])."')";
  952. Database::query($sql);
  953. $shared_question_id = Database::insert_id();
  954. } else {
  955. // Updating an existing question
  956. // adding the question to the survey_question table
  957. $sql = "UPDATE $tbl_survey_question SET
  958. survey_question = '".Database::escape_string($form_content['question'])."',
  959. survey_question_comment = '".Database::escape_string($form_content['question_comment'])."',
  960. display = '".Database::escape_string($form_content['horizontalvertical'])."'
  961. WHERE
  962. question_id = '".intval($form_content['shared_question_id'])."' AND
  963. code = '".Database::escape_string($_course['id'])."'";
  964. Database::query($sql);
  965. $shared_question_id = $form_content['shared_question_id'];
  966. }
  967. return $shared_question_id;
  968. }
  969. /**
  970. * This functions moves a question of a survey up or down
  971. *
  972. * @param string $direction
  973. * @param integer $survey_question_id
  974. * @param integer $survey_id
  975. *
  976. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  977. * @version January 2007
  978. */
  979. public static function move_survey_question($direction, $survey_question_id, $survey_id)
  980. {
  981. // Table definition
  982. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  983. $course_id = api_get_course_int_id();
  984. if ($direction == 'moveup') {
  985. $sort = 'DESC';
  986. }
  987. if ($direction == 'movedown') {
  988. $sort = 'ASC';
  989. }
  990. // Finding the two questions that needs to be swapped
  991. $sql = "SELECT * FROM $table_survey_question
  992. WHERE c_id = $course_id AND survey_id='".Database::escape_string($survey_id)."'
  993. ORDER BY sort $sort";
  994. $result = Database::query($sql);
  995. $found = false;
  996. while ($row = Database::fetch_array($result, 'ASSOC')) {
  997. if ($found) {
  998. $question_id_two = $row['question_id'];
  999. $question_sort_two = $row['sort'];
  1000. $found = false;
  1001. }
  1002. if ($row['question_id'] == $survey_question_id) {
  1003. $found = true;
  1004. $question_id_one = $row['question_id'];
  1005. $question_sort_one = $row['sort'];
  1006. }
  1007. }
  1008. $sql1 = "UPDATE $table_survey_question SET sort = '".Database::escape_string($question_sort_two)."'
  1009. WHERE c_id = $course_id AND question_id='".intval($question_id_one)."'";
  1010. Database::query($sql1);
  1011. $sql2 = "UPDATE $table_survey_question SET sort = '".Database::escape_string($question_sort_one)."'
  1012. WHERE c_id = $course_id AND question_id='".intval($question_id_two)."'";
  1013. Database::query($sql2);
  1014. }
  1015. /**
  1016. * This function deletes all the questions of a given survey
  1017. * This function is normally only called when a survey is deleted
  1018. *
  1019. * @param int $survey_id the id of the survey that has to be deleted
  1020. * @return true
  1021. *
  1022. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1023. * @version January 2007
  1024. */
  1025. public static function delete_all_survey_questions($survey_id, $shared = false)
  1026. {
  1027. $course_id = api_get_course_int_id();
  1028. // Table definitions
  1029. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  1030. $course_condition = " c_id = $course_id AND ";
  1031. if ($shared) {
  1032. $course_condition = "";
  1033. $table_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  1034. }
  1035. $sql = "DELETE FROM $table_survey_question
  1036. WHERE $course_condition survey_id='".intval($survey_id)."'";
  1037. // Deleting the survey questions
  1038. Database::query($sql);
  1039. // Deleting all the options of the questions of the survey
  1040. survey_manager::delete_all_survey_questions_options($survey_id, $shared);
  1041. // Deleting all the answers on this survey
  1042. survey_manager::delete_all_survey_answers($survey_id);
  1043. }
  1044. /**
  1045. * This function deletes a survey question and all its options
  1046. *
  1047. * @param integer $survey_id the id of the survey
  1048. * @param integer $question_id the id of the question
  1049. * @param integer $shared
  1050. *
  1051. * @todo also delete the answers to this question
  1052. *
  1053. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1054. * @version March 2007
  1055. */
  1056. public static function delete_survey_question($survey_id, $question_id, $shared = false)
  1057. {
  1058. $course_id = api_get_course_int_id();
  1059. // Table definitions
  1060. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  1061. if ($shared) {
  1062. survey_manager::delete_shared_survey_question($survey_id, $question_id);
  1063. }
  1064. // Deleting the survey questions
  1065. $sql = "DELETE FROM $table_survey_question
  1066. WHERE
  1067. c_id = $course_id AND
  1068. survey_id='".intval($survey_id)."' AND
  1069. question_id='".intval($question_id)."'";
  1070. Database::query($sql);
  1071. // Deleting the options of the question of the survey
  1072. survey_manager::delete_survey_question_option($survey_id, $question_id, $shared);
  1073. }
  1074. /**
  1075. * This function deletes a shared survey question from the main database and all its options
  1076. *
  1077. * @param int $question_id the id of the question
  1078. * @param int $shared
  1079. *
  1080. * @todo delete all the options of this question
  1081. *
  1082. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1083. * @version March 2007
  1084. */
  1085. public static function delete_shared_survey_question($survey_id, $question_id)
  1086. {
  1087. // Table definitions
  1088. $table_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  1089. $table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  1090. // First we have to get the shared_question_id
  1091. $question_data = survey_manager::get_question($question_id);
  1092. // Deleting the survey questions
  1093. $sql = "DELETE FROM $table_survey_question
  1094. WHERE question_id='".intval($question_data['shared_question_id'])."'";
  1095. Database::query($sql);
  1096. // Deleting the options of the question of the survey question
  1097. $sql = "DELETE FROM $table_survey_question_option
  1098. WHERE question_id='".intval($question_data['shared_question_id'])."'";
  1099. Database::query($sql);
  1100. }
  1101. /**
  1102. * This function stores the options of the questions in the table
  1103. *
  1104. * @param array $form_content
  1105. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1106. * @version January 2007
  1107. *
  1108. * @todo writing the update statement when editing a question
  1109. */
  1110. public static function save_question_options($form_content, $survey_data)
  1111. {
  1112. $course_id = api_get_course_int_id();
  1113. // A percentage question type has options 1 -> 100
  1114. if ($form_content['type'] == 'percentage') {
  1115. for($i = 1; $i < 101; $i++) {
  1116. $form_content['answers'][] = $i;
  1117. }
  1118. }
  1119. if (is_numeric($survey_data['survey_share']) && $survey_data['survey_share'] != 0) {
  1120. survey_manager::save_shared_question_options($form_content, $survey_data);
  1121. }
  1122. // Table definition
  1123. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  1124. // We are editing a question so we first have to remove all the existing options from the database
  1125. if (is_numeric($form_content['question_id'])) {
  1126. $sql = "DELETE FROM $table_survey_question_option
  1127. WHERE c_id = $course_id AND question_id = '".intval($form_content['question_id'])."'";
  1128. Database::query($sql);
  1129. }
  1130. $counter = 1;
  1131. if (is_array($form_content['answers'])) {
  1132. //foreach ($form_content['answers'] as $key => $answer) {
  1133. for ($i = 0; $i < count($form_content['answers']); $i++) {
  1134. $sql = "INSERT INTO $table_survey_question_option (c_id, question_id, survey_id, option_text, value,sort) VALUES (
  1135. $course_id,
  1136. '".intval($form_content['question_id'])."',
  1137. '".intval($form_content['survey_id'])."',
  1138. '".Database::escape_string($form_content['answers'][$i])."',
  1139. '".Database::escape_string($form_content['values'][$i])."',
  1140. '".Database::escape_string($counter)."')";
  1141. Database::query($sql);
  1142. $counter++;
  1143. }
  1144. }
  1145. }
  1146. /**
  1147. * This function stores the options of the questions in the shared table
  1148. *
  1149. * @param array $form_content
  1150. *
  1151. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1152. * @version February 2007
  1153. *
  1154. * @todo writing the update statement when editing a question
  1155. */
  1156. public function save_shared_question_options($form_content, $survey_data)
  1157. {
  1158. if (is_array($form_content) && is_array($form_content['answers'])) {
  1159. // Table defintion
  1160. $table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  1161. // We are editing a question so we first have to remove all the existing options from the database
  1162. $sql = "DELETE FROM $table_survey_question_option WHERE question_id = '".Database::escape_string($form_content['shared_question_id'])."'";
  1163. Database::query($sql);
  1164. $counter = 1;
  1165. foreach ($form_content['answers'] as & $answer) {
  1166. $sql = "INSERT INTO $table_survey_question_option (question_id, survey_id, option_text, sort) VALUES (
  1167. '".Database::escape_string($form_content['shared_question_id'])."',
  1168. '".Database::escape_string($survey_data['is_shared'])."',
  1169. '".Database::escape_string($answer)."',
  1170. '".Database::escape_string($counter)."')";
  1171. Database::query($sql);
  1172. $counter++;
  1173. }
  1174. }
  1175. }
  1176. /**
  1177. * This function deletes all the options of the questions of a given survey
  1178. * This function is normally only called when a survey is deleted
  1179. *
  1180. * @param $survey_id the id of the survey that has to be deleted
  1181. * @return true
  1182. *
  1183. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1184. * @version January 2007
  1185. */
  1186. public static function delete_all_survey_questions_options($survey_id, $shared = false)
  1187. {
  1188. // Table definitions
  1189. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  1190. $course_id = api_get_course_int_id();
  1191. $course_condition = " c_id = $course_id AND ";
  1192. if ($shared) {
  1193. $course_condition = "";
  1194. $table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  1195. }
  1196. $sql = "DELETE FROM $table_survey_question_option
  1197. WHERE $course_condition survey_id='".Database::escape_string($survey_id)."'";
  1198. // Deleting the options of the survey questions
  1199. Database::query($sql);
  1200. return true;
  1201. }
  1202. /**
  1203. * This function deletes the options of a given question
  1204. *
  1205. * @param int $survey_id
  1206. * @param int $question_id
  1207. * @param int $shared
  1208. *
  1209. * @return bool
  1210. *
  1211. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1212. * @author Julio Montoya
  1213. * @version March 2007
  1214. */
  1215. public static function delete_survey_question_option($survey_id, $question_id, $shared = false)
  1216. {
  1217. $course_id = api_get_course_int_id();
  1218. $course_condition = " c_id = $course_id AND ";
  1219. // Table definitions
  1220. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  1221. if ($shared) {
  1222. $course_condition = "";
  1223. $table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  1224. }
  1225. // Deleting the options of the survey questions
  1226. $sql = "DELETE from $table_survey_question_option
  1227. WHERE
  1228. $course_condition survey_id='".Database::escape_string($survey_id)."' AND
  1229. question_id='".Database::escape_string($question_id)."'";
  1230. Database::query($sql);
  1231. return true;
  1232. }
  1233. /**
  1234. * SURVEY ANSWERS FUNCTIONS
  1235. */
  1236. /**
  1237. * This function deletes all the answers anyone has given on this survey
  1238. * This function is normally only called when a survey is deleted
  1239. *
  1240. * @param $survey_id the id of the survey that has to be deleted
  1241. * @return true
  1242. *
  1243. * @todo write the function
  1244. *
  1245. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1246. * @version January 2007,december 2008
  1247. */
  1248. public static function delete_all_survey_answers($survey_id)
  1249. {
  1250. $course_id = api_get_course_int_id();
  1251. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  1252. $survey_id = intval($survey_id);
  1253. Database::query("DELETE FROM $table_survey_answer WHERE c_id = $course_id AND survey_id=$survey_id");
  1254. return true;
  1255. }
  1256. /**
  1257. * @param int $user_id
  1258. * @param int $survey_id
  1259. * @param int $course_id
  1260. * @return bool
  1261. */
  1262. public static function is_user_filled_survey($user_id, $survey_id, $course_id)
  1263. {
  1264. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  1265. $user_id = intval($user_id);
  1266. $course_id = intval($course_id);
  1267. $survey_id = intval($survey_id);
  1268. $sql = "SELECT DISTINCT user FROM $table_survey_answer
  1269. WHERE
  1270. c_id = $course_id AND
  1271. user = $user_id AND
  1272. survey_id = $survey_id";
  1273. $result = Database::query($sql);
  1274. if (Database::num_rows($result)) {
  1275. return true;
  1276. }
  1277. return false;
  1278. }
  1279. /**
  1280. * This function gets all the persons who have filled the survey
  1281. *
  1282. * @param integer $survey_id
  1283. * @return array
  1284. *
  1285. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1286. * @version February 2007
  1287. */
  1288. static function get_people_who_filled_survey($survey_id, $all_user_info = false, $course_id = null)
  1289. {
  1290. // Database table definition
  1291. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  1292. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  1293. // Variable initialisation
  1294. $return = array();
  1295. if (empty($course_id)) {
  1296. $course_id = api_get_course_int_id();
  1297. } else {
  1298. $course_id = intval($course_id);
  1299. }
  1300. if ($all_user_info) {
  1301. $order_clause = api_sort_by_first_name() ? ' ORDER BY user.firstname, user.lastname' : ' ORDER BY user.lastname, user.firstname';
  1302. $sql = "SELECT DISTINCT
  1303. answered_user.user as invited_user, user.firstname, user.lastname, user.user_id
  1304. FROM $table_survey_answer answered_user
  1305. LEFT JOIN $table_user as user ON answered_user.user = user.user_id
  1306. WHERE
  1307. answered_user.c_id = $course_id AND
  1308. survey_id= '".Database::escape_string($survey_id)."' ".
  1309. $order_clause;
  1310. } else {
  1311. $sql = "SELECT DISTINCT user FROM $table_survey_answer
  1312. WHERE c_id = $course_id AND survey_id= '".Database::escape_string($survey_id)."' ";
  1313. }
  1314. $res = Database::query($sql);
  1315. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1316. if ($all_user_info) {
  1317. $return[] = $row;
  1318. } else {
  1319. $return[] = $row['user'];
  1320. }
  1321. }
  1322. return $return;
  1323. }
  1324. static function survey_generation_hash_available()
  1325. {
  1326. if (extension_loaded('mcrypt')) {
  1327. return true;
  1328. }
  1329. return false;
  1330. }
  1331. static function generate_survey_hash($survey_id, $course_id, $session_id, $group_id)
  1332. {
  1333. $hash = hash('sha512', api_get_security_key().'_'.$course_id.'_'.$session_id.'_'.$group_id.'_'.$survey_id);
  1334. return $hash;
  1335. }
  1336. static function validate_survey_hash($survey_id, $course_id, $session_id, $group_id, $hash)
  1337. {
  1338. $survey_generated_hash = self::generate_survey_hash($survey_id, $course_id, $session_id, $group_id);
  1339. if ($survey_generated_hash == $hash) {
  1340. return true;
  1341. }
  1342. return false;
  1343. }
  1344. static function generate_survey_link($survey_id, $course_id, $session_id, $group_id)
  1345. {
  1346. $code = self::generate_survey_hash($survey_id, $course_id, $session_id, $group_id);
  1347. return api_get_path(WEB_CODE_PATH).'survey/link.php?h='.$code.'&i='.$survey_id.'&c='.intval($course_id).'&s='.intval($session_id).'&g='.$group_id;
  1348. }
  1349. }
  1350. /**
  1351. * Class survey_question
  1352. */
  1353. class survey_question
  1354. {
  1355. // The html code of the form
  1356. public $html;
  1357. /**
  1358. * This function does the generic part of any survey question: the question field
  1359. *
  1360. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1361. * @version January 2007
  1362. *
  1363. * @todo the form_text has to become a wysiwyg editor or adding a question_comment field
  1364. * @todo consider adding a question_comment form element
  1365. */
  1366. function create_form($survey_data, $form_content)
  1367. {
  1368. $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : null;
  1369. $questionId = isset($_GET['question_id']) ? Security::remove_XSS($_GET['question_id']) : null;
  1370. $tool_name = Display::return_icon(
  1371. survey_manager::icon_question(Security::remove_XSS($_GET['type'])),
  1372. get_lang(ucfirst(Security::remove_XSS($_GET['type']))),
  1373. array('align' => 'middle', 'height' => '22px')
  1374. ).' ';
  1375. if ($action == 'add') {
  1376. $tool_name .= get_lang('AddQuestion');
  1377. }
  1378. if ($action == 'edit') {
  1379. $tool_name .= get_lang('EditQuestion');
  1380. }
  1381. if ($_GET['type'] == 'yesno') {
  1382. $tool_name .= ': '.get_lang('YesNo');
  1383. } else if ($_GET['type'] == 'multiplechoice') {
  1384. $tool_name .= ': '.get_lang('UniqueSelect');
  1385. } else {
  1386. $tool_name .= ': '.get_lang(api_ucfirst(Security::remove_XSS($_GET['type'])));
  1387. }
  1388. $formContent = isset($form_content['question']) ? $form_content['question'] : null;
  1389. $sharedQuestionId = isset($form_content['shared_question_id']) ? $form_content['shared_question_id'] : null;
  1390. $url = api_get_self().'?action='.$action.'&type='.Security::remove_XSS($_GET['type']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'&question_id='.$questionId;
  1391. $this->html .= '<form class="form-horizontal" id="question_form" name="question_form" method="post" action="'.$url.'">';
  1392. $this->html .= '<legend>'.$tool_name.'</legend>';
  1393. $this->html .= ' <input type="hidden" name="survey_id" id="survey_id" value="'.Security::remove_XSS($_GET['survey_id']).'"/>';
  1394. $this->html .= ' <input type="hidden" name="question_id" id="question_id" value="'.$questionId.'"/>';
  1395. $this->html .= ' <input type="hidden" name="shared_question_id" id="shared_question_id" value="'.Security::remove_XSS($sharedQuestionId).'"/>';
  1396. $this->html .= ' <input type="hidden" name="type" id="type" value="'.Security::remove_XSS($_GET['type']).'"/>';
  1397. // question field
  1398. $this->html .= ' <div class="control-group">';
  1399. $this->html .= ' <label class="control-label">';
  1400. $this->html .= ' <span class="form_required">*</span> '.get_lang('Question');
  1401. $this->html .= ' </label>';
  1402. $this->html .= ' <div class="controls">';
  1403. $this->html .= api_return_html_area('question', Security::remove_XSS(stripslashes($formContent), STUDENT), '', '', null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120'));
  1404. $this->html .= ' </div>';
  1405. $this->html .= ' </div>';
  1406. if ($survey_data['survey_type'] == 1) {
  1407. $table_survey_question_group = Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP);
  1408. $sql = 'SELECT id,name FROM '.$table_survey_question_group.' WHERE survey_id = '.(int)$_GET['survey_id'].' ORDER BY name';
  1409. $rs = Database::query($sql);
  1410. $glist = null;
  1411. while ($row = Database::fetch_array($rs, 'NUM')) {
  1412. $glist .= '<option value="'.$row[0].'" >'.$row[1].'</option>';
  1413. }
  1414. $grouplist = $grouplist1 = $grouplist2 = $glist;
  1415. if (!empty($form_content['assigned'])) {
  1416. $grouplist = str_replace('<option value="'.$form_content['assigned'].'"','<option value="'.$form_content['assigned'].'" selected',$glist);
  1417. }
  1418. if (!empty($form_content['assigned1'])) {
  1419. $grouplist1 = str_replace('<option value="'.$form_content['assigned1'].'"','<option value="'.$form_content['assigned1'].'" selected',$glist);
  1420. }
  1421. if (!empty($form_content['assigned2'])) {
  1422. $grouplist2 = str_replace('<option value="'.$form_content['assigned2'].'"','<option value="'.$form_content['assigned2'].'" selected',$glist);
  1423. }
  1424. $this->html .= ' <tr><td colspan="">
  1425. <fieldset style="border:1px solid black"><legend>'.get_lang('Condition').'</legend>
  1426. <b>'.get_lang('Primary').'</b><br />
  1427. '.'<input type="radio" name="choose" value="1" '.(($form_content['choose'] == 1) ? 'checked' : '').
  1428. '><select name="assigned">'.$grouplist.'</select><br />';
  1429. $this->html .= '
  1430. <b>'.get_lang('Secondary').'</b><br />
  1431. '.'<input type="radio" name="choose" value="2" '.(($form_content['choose']==2)?'checked':'').
  1432. '><select name="assigned1">'.$grouplist1.'</select> '.
  1433. '<select name="assigned2">'.$grouplist2.'</select>'
  1434. .'</fieldset><br />';
  1435. }
  1436. return $this->html;
  1437. }
  1438. /**
  1439. * This functions displays the form after the html variable has correctly been finished
  1440. * (adding a submit button, closing the table and closing the form)
  1441. *
  1442. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1443. * @version January 2007
  1444. *
  1445. */
  1446. function render_form()
  1447. {
  1448. if (isset($_GET['question_id']) and !empty($_GET['question_id'])) {
  1449. $class = 'save';
  1450. $text = get_lang('ModifyQuestionSurvey');
  1451. } else {
  1452. $class = 'add';
  1453. $text = get_lang('CreateQuestionSurvey');
  1454. }
  1455. if ($_GET['type'] == 'yesno' || $_GET['type'] == 'open'|| $_GET['type'] == 'percentage' || $_GET['type'] == 'comment' || $_GET['type'] == 'pagebreak') {
  1456. $this->html .= ' <div class="control-group">';
  1457. $this->html .= ' <div class="controls">';
  1458. }
  1459. $this->html .= ' <button class="'.$class.'"type="submit" name="save_question">'.$text.'</button>';
  1460. $this->html .= ' </div>';
  1461. $this->html .= ' </div>';
  1462. //$this->html .= ' </table>';
  1463. $this->html .= '</form>';
  1464. echo $this->html;
  1465. }
  1466. /**
  1467. * This function handles the actions on a question and its answers
  1468. *
  1469. * @todo consider using $form_content instead of $_POST
  1470. *
  1471. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1472. * @version January 2007
  1473. */
  1474. public function handle_action($survey_data, $form_content)
  1475. {
  1476. $course_id = api_get_course_int_id();
  1477. global $config;
  1478. // Moving an answer up
  1479. if (isset($_POST['move_up']) && $_POST['move_up']) {
  1480. foreach ($_POST['move_up'] as $key => & $value) {
  1481. $id1 = $key;
  1482. $content1 = $form_content['answers'][$id1];
  1483. $id2 = $key - 1;
  1484. $content2 = $form_content['answers'][$id2];
  1485. $form_content['answers'][$id1] = $content2;
  1486. $form_content['answers'][$id2] = $content1;
  1487. }
  1488. }
  1489. // Moving an answer down
  1490. if (isset($_POST['move_down']) && $_POST['move_down']) {
  1491. foreach ($_POST['move_down'] as $key => & $value) {
  1492. $id1 = $key;
  1493. $content1 = $form_content['answers'][$id1];
  1494. $id2 = $key + 1;
  1495. $content2 = $form_content['answers'][$id2];
  1496. $form_content['answers'][$id1] = $content2;
  1497. $form_content['answers'][$id2] = $content1;
  1498. }
  1499. }
  1500. // Adding an answer
  1501. if (isset($_POST['add_answer'])) {
  1502. $form_content['answers'][] = '';
  1503. }
  1504. // Removing an answer
  1505. if (isset($_POST['remove_answer'])) {
  1506. $max_answer = count($form_content['answers']);
  1507. unset($form_content['answers'][$max_answer - 1]);
  1508. }
  1509. // Saving a question
  1510. if (isset($_POST['save_question'])) {
  1511. $message = survey_manager::save_question(
  1512. $survey_data,
  1513. $form_content
  1514. );
  1515. if ($message == 'QuestionAdded' || $message == 'QuestionUpdated' ) {
  1516. $sql='SELECT COUNT(*) FROM '.Database :: get_course_table(TABLE_SURVEY_QUESTION).' WHERE c_id = '.$course_id.' AND survey_id = '.intval($_GET['survey_id']);
  1517. $res = Database :: fetch_array (Database::query($sql));
  1518. if ($config['survey']['debug']) {
  1519. Display :: display_header();
  1520. Display :: display_confirmation_message($message.'<br />'.get_lang('ReturnTo').' <a href="'.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.intval($_GET['survey_id']).'">'.get_lang('Survey').'</a>', false);
  1521. } else {
  1522. header('Location: '.api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.intval($_GET['survey_id']).'&message='.$message);
  1523. exit;
  1524. }
  1525. } else {
  1526. if ($message == 'PleaseEnterAQuestion' || $message == 'PleasFillAllAnswer'|| $message == 'PleaseChooseACondition'|| $message == 'ChooseDifferentCategories') {
  1527. $_SESSION['temp_user_message'] = $form_content['question'];
  1528. $_SESSION['temp_horizontalvertical'] = $form_content['horizontalvertical'];
  1529. $_SESSION['temp_sys_message'] = $message;
  1530. $_SESSION['temp_answers'] = $form_content['answers'];
  1531. $_SESSION['temp_values'] = $form_content['values'];
  1532. header('location: '.api_get_path(WEB_CODE_PATH).'survey/question.php?'.api_get_cidreq().'&question_id='.intval($_GET['question_id']).'&survey_id='.intval($_GET['survey_id']).'&action='.Security::remove_XSS($_GET['action']).'&type='.Security::remove_XSS($_GET['type']).'');
  1533. exit;
  1534. }
  1535. }
  1536. }
  1537. /**
  1538. * This solution is a little bit strange but I could not find a different solution.
  1539. */
  1540. if ($_POST['delete_answer']) {
  1541. foreach ($_POST['delete_answer'] as $key => & $value) {
  1542. unset($form_content['answers'][$key]);
  1543. $deleted = $key;
  1544. }
  1545. foreach ($form_content['answers'] as $key => & $value) {
  1546. if ($key > $deleted) {
  1547. $form_content['answers'][$key - 1] = $form_content['answers'][$key];
  1548. unset($form_content['answers'][$key]);
  1549. }
  1550. }
  1551. }
  1552. return $form_content;
  1553. }
  1554. /**
  1555. * This functions adds two buttons. One to add an option, one to remove an option
  1556. *
  1557. * @param string $form_content
  1558. * @return string code
  1559. *
  1560. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1561. * @author Julio Montoya - 2013
  1562. * @version January 2007
  1563. */
  1564. function add_remove_buttons($form_content)
  1565. {
  1566. if (count($form_content['answers']) <= 2) {
  1567. $remove_answer_attribute = 'disabled="disabled"';
  1568. }
  1569. $return = ' <div class="row">';
  1570. $return .= ' <div class="formw">';
  1571. $return .= ' <input type="hidden" name="is_executable" id="is_executable" value="-" />';
  1572. $return .= ' <button class="minus" type="submit" name="remove_answer" "'.$remove_answer_attribute.'">'.get_lang('RemoveAnswer').' </button>';
  1573. $return .= ' <button class="plus" type="submit" name="add_answer">'.get_lang('AddAnswer').'</button>';
  1574. return $return;
  1575. }
  1576. /**
  1577. * Render the question. In this case this starts with the form tag
  1578. *
  1579. * @param string $form_content
  1580. *
  1581. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1582. * @version January 2007
  1583. */
  1584. function render_question($form_content)
  1585. {
  1586. $this->html = '<form id="question" name="question" method="post" action="'.api_get_self().'?survey_id='.intval($_GET['survey_id']).'">';
  1587. echo $this->html;
  1588. }
  1589. }
  1590. class ch_yesno extends survey_question
  1591. {
  1592. /**
  1593. * This function creates the form elements for the yesno questions
  1594. *
  1595. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1596. * @version January 2007
  1597. */
  1598. function create_form($survey_data, $form_content)
  1599. {
  1600. $this->html = parent::create_form($survey_data, $form_content);
  1601. // Horizontal or vertical
  1602. $this->html .= ' <div class="control-group">';
  1603. $this->html .= ' <label class="control-label">';
  1604. $this->html .= get_lang('DisplayAnswersHorVert');
  1605. $this->html .= ' </label>';
  1606. $this->html .= ' <div class="controls">';
  1607. $this->html .= ' <input name="horizontalvertical" type="radio" value="horizontal" ';
  1608. if (empty($form_content['horizontalvertical']) or $form_content['horizontalvertical'] == 'horizontal') {
  1609. $this->html .= 'checked="checked"';
  1610. }
  1611. $this->html .= '/>'.get_lang('Horizontal').'<br />';
  1612. $this->html .= ' <input name="horizontalvertical" type="radio" value="vertical" ';
  1613. if (isset($form_content['horizontalvertical']) && $form_content['horizontalvertical'] == 'vertical') {
  1614. $this->html .= 'checked="checked"';
  1615. }
  1616. $this->html .= ' />'.get_lang('Vertical').'';
  1617. $this->html .= ' </div>';
  1618. $this->html .= ' </div>';
  1619. // The options
  1620. $this->html .= ' <div class="row">';
  1621. $this->html .= ' <label class="control-label">';
  1622. $this->html .= get_lang('AnswerOptions');
  1623. $this->html .= ' </label>';
  1624. $this->html .= ' <div class="formw">';
  1625. $this->html .= ' <table>';
  1626. $this->html .= ' <tr>';
  1627. $this->html .= ' <td align="right"><label for="answers[0]">1</label></td>';
  1628. $this->html .= ' <td width="550">'.api_return_html_area('answers[0]', stripslashes($form_content['answers'][0]), '', '', null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120')).'</td>';
  1629. $this->html .= ' <td><input style="width:22px" src="../img/icons/22/down.png" type="image" class="down" value="move_down[0]" name="move_down[0]"/></td>';
  1630. $this->html .= ' </tr>';
  1631. $this->html .= ' <tr>';
  1632. $this->html .= ' <td align="right"><label for="answers[1]">2</label></td>';
  1633. //$this->html .= ' <td><input type="text" name="answers[1]" id="answers[1]" value="'.$form_content['answers'][1].'" /></td>';
  1634. $this->html .= ' <td width="550">'.api_return_html_area('answers[1]', stripslashes($form_content['answers'][1]), '', '', null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120')).'</td>';
  1635. $this->html .= ' <td><input style="width:22px" type="image" src="../img/icons/22/up.png" value="move_up[1]" name="move_up[1]" /></td>';
  1636. $this->html .= ' </tr>';
  1637. $this->html .= ' </table>';
  1638. $this->html .= ' </div>';
  1639. $this->html .= ' </div>';
  1640. }
  1641. /**
  1642. * Render the yes not question type
  1643. *
  1644. * @param string $form_content
  1645. *
  1646. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1647. * @version January 2007
  1648. */
  1649. function render_question($form_content, $answers = array())
  1650. {
  1651. if (is_array($form_content['options'])) { // Check if data is correct
  1652. foreach ($form_content['options'] as $key => & $value) {
  1653. if ($form_content['display'] == 'vertical') {
  1654. $this->html .= '<label class="radio">';
  1655. } else {
  1656. $this->html .= '<label class="radio inline">';
  1657. }
  1658. $value_to_show = $value;
  1659. if (substr_count($value, '<p>') == 1) {
  1660. $value_to_show = substr($value, 3, (strlen($value) - 7));
  1661. }
  1662. $this->html .= '<input name="question'.$form_content['question_id'].'" type="radio" value="'.$key.'"';
  1663. if (is_array($answers)) {
  1664. if (in_array($key,$answers)) {
  1665. $this->html .= 'checked="checked"';
  1666. }
  1667. }
  1668. $this->html .= '/>';
  1669. $this->html .= $value_to_show.'</label>';
  1670. if ($form_content['display'] == 'vertical') {
  1671. //$this->html .= '<br />';
  1672. }
  1673. }
  1674. }
  1675. echo '<div class="survey_question_wrapper">';
  1676. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  1677. echo $this->html;
  1678. echo '</div>';
  1679. }
  1680. }
  1681. class ch_multiplechoice extends survey_question
  1682. {
  1683. /**
  1684. * This function creates the form elements for the multiple choice questions
  1685. *
  1686. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1687. * @version January 2007
  1688. */
  1689. function create_form($survey_data, $form_content)
  1690. {
  1691. $this->html = parent::create_form($survey_data, $form_content);
  1692. // Horizontal or vertical
  1693. $this->html .= ' <div class="row">';
  1694. $this->html .= ' <label class="control-label">';
  1695. $this->html .= get_lang('DisplayAnswersHorVert');
  1696. $this->html .= ' </label>';
  1697. $this->html .= ' <div class="formw">';
  1698. $this->html .= ' <input name="horizontalvertical" type="radio" value="horizontal" ';
  1699. if (empty($form_content['horizontalvertical']) or $form_content['horizontalvertical'] == 'horizontal') {
  1700. $this->html .= 'checked="checked"';
  1701. }
  1702. $this->html .= '/>'.get_lang('Horizontal').'</label><br />';
  1703. $this->html .= ' <input name="horizontalvertical" type="radio" value="vertical" ';
  1704. if (isset($form_content['horizontalvertical']) && $form_content['horizontalvertical'] == 'vertical') {
  1705. $this->html .= 'checked="checked"';
  1706. }
  1707. $this->html .= ' />'.get_lang('Vertical').'</label>';
  1708. $this->html .= ' </div>';
  1709. $this->html .= ' </div>';
  1710. // The Options
  1711. $this->html .= ' <div class="row">';
  1712. $this->html .= ' <label class="control-label">';
  1713. $this->html .= get_lang('AnswerOptions');
  1714. $this->html .= ' </label>';
  1715. $this->html .= ' <div class="formw">';
  1716. $total_number_of_answers = count($form_content['answers']);
  1717. $this->html .= ' <table>';
  1718. if (is_array($form_content['answers'])) { // Check if data is correct
  1719. foreach ($form_content['answers'] as $key => $value) {
  1720. $this->html .= ' <tr>';
  1721. $this->html .= ' <td align="right"><label for="answers['.$key.']">'.($key+1).'</label></td>';
  1722. //$this->html .= ' <td><input type="text" name="answers['.$key.']" id="answers['.$key.']" value="'.$form_content['answers'][$key].'" /></td>';
  1723. $this->html .= ' <td width="550">'.api_return_html_area('answers['.$key.']', api_html_entity_decode(stripslashes($form_content['answers'][$key]), ENT_QUOTES), '', '', null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120')).'</td>';
  1724. $this->html .= ' <td>';
  1725. if ($key<$total_number_of_answers-1) {
  1726. $this->html .= ' <input style="width:22px" type="image" src="../img/icons/22/down.png" value="move_down['.$key.']" name="move_down['.$key.']"/>';
  1727. }
  1728. if ($key>0) {
  1729. $this->html .= ' <input style="width:22px" type="image" src="../img/icons/22/up.png" value="move_up['.$key.']" name="move_up['.$key.']"/>';
  1730. }
  1731. if ($total_number_of_answers> 2) {
  1732. $this->html .= ' <input style="width:22px" type="image" src="../img/icons/22/delete.png" value="delete_answer['.$key.']" name="delete_answer['.$key.']"/>';
  1733. }
  1734. $this->html .= ' </td>';
  1735. $this->html .= ' </tr>';
  1736. }
  1737. }
  1738. // The buttons for adding or removing
  1739. $this->html .= ' </table>';
  1740. $this->html .= ' </div>';
  1741. $this->html .= ' </div>';
  1742. $this->html .= parent :: add_remove_buttons($form_content);
  1743. }
  1744. /**
  1745. * render the multiple choice question type
  1746. *
  1747. * @param string $form_content
  1748. *
  1749. * @todo it would make more sense to consider yesno as a special case of multiplechoice and not the other way around
  1750. *
  1751. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1752. * @version January 2007
  1753. */
  1754. function render_question($form_content, $answers = array())
  1755. {
  1756. $question = new ch_yesno();
  1757. $question->render_question($form_content, $answers);
  1758. }
  1759. }
  1760. class ch_personality extends survey_question
  1761. {
  1762. /**
  1763. * This function creates the form elements for the multiple response questions
  1764. *
  1765. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1766. * @version January 2007
  1767. */
  1768. function create_form($survey_data, $form_content)
  1769. {
  1770. $this->html = parent::create_form($survey_data, $form_content);
  1771. $this->html .= ' <tr>';
  1772. $this->html .= ' <td colspan="2"><strong>'.get_lang('DisplayAnswersHorVert').'</strong></td>';
  1773. $this->html .= ' </tr>';
  1774. // Horizontal or vertical
  1775. $this->html .= ' <tr>';
  1776. $this->html .= ' <td align="right" valign="top">&nbsp;</td>';
  1777. $this->html .= ' <td>';
  1778. $this->html .= ' <input name="horizontalvertical" type="radio" value="horizontal" ';
  1779. if (empty($form_content['horizontalvertical']) || $form_content['horizontalvertical'] == 'horizontal') {
  1780. $this->html .= 'checked="checked"';
  1781. }
  1782. $this->html .= '/>'.get_lang('Horizontal').'</label><br />';
  1783. $this->html .= ' <input name="horizontalvertical" type="radio" value="vertical" ';
  1784. if (isset($form_content['horizontalvertical']) && $form_content['horizontalvertical'] == 'vertical') {
  1785. $this->html .= 'checked="checked"';
  1786. }
  1787. $this->html .= ' />'.get_lang('Vertical').'</label>';
  1788. $this->html .= ' </td>';
  1789. $this->html .= ' <td>&nbsp;</td>';
  1790. $this->html .= ' </tr>';
  1791. $this->html .= ' <tr>
  1792. <td colspan="">&nbsp;</td>
  1793. </tr>';
  1794. // The options
  1795. $this->html .= ' <tr>';
  1796. $this->html .= ' <td colspan="3"><strong>'.get_lang('AnswerOptions').'</strong></td>';
  1797. $this->html .= ' </tr>';
  1798. $total_number_of_answers = count($form_content['answers']);
  1799. $question_values = array();
  1800. // Values of question options
  1801. if (is_array($form_content['values'])) { // Check if data is correct
  1802. foreach ($form_content['values'] as $key => & $value) {
  1803. $question_values [] = '<input size="3" type="text" id="values['.$key.']" name="values['.$key.']" value="'.$value.'" />';
  1804. }
  1805. }
  1806. $count = 0;
  1807. if (is_array($form_content['answers'])) {
  1808. foreach ($form_content['answers'] as $key => & $value) {
  1809. $this->html .= ' <tr>';
  1810. $this->html .= ' <td align="right"><label for="answers['.$key.']">'.($key+1).'</label></td>';
  1811. //$this->html .= ' <td><input type="text" name="answers['.$key.']" id="answers['.$key.']" value="'.$form_content['answers'][$key].'" /></td>';
  1812. $this->html .= ' <td width="550">'.api_return_html_area('answers['.$key.']', api_html_entity_decode(stripslashes($form_content['answers'][$key])), '', '', null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120')).'</td>';
  1813. $this->html .= ' <td>';
  1814. if ($total_number_of_answers > 2) {
  1815. $this->html .= $question_values[$count];
  1816. }
  1817. if ($key < $total_number_of_answers - 1) {
  1818. $this->html .= ' <input type="image" style="width:22px" src="../img/icons/22/down.png" value="move_down['.$key.']" name="move_down['.$key.']"/>';
  1819. }
  1820. if ($key > 0) {
  1821. $this->html .= ' <input type="image" style="width:22px" src="../img/icons/22/up.png" value="move_up['.$key.']" name="move_up['.$key.']"/>';
  1822. }
  1823. if ($total_number_of_answers > 2) {
  1824. $this->html .= ' <input type="image" style="width:22px" src="../img/icons/22/delete.png" value="delete_answer['.$key.']" name="delete_answer['.$key.']"/>';
  1825. }
  1826. $this->html .= ' </td>';
  1827. $this->html .= ' </tr>';
  1828. $count++;
  1829. }
  1830. }
  1831. // The buttons for adding or removing
  1832. //$this->html .= parent :: add_remove_buttons($form_content);
  1833. }
  1834. /**
  1835. * Render the multiple response question type
  1836. *
  1837. * @param string $form_content
  1838. *
  1839. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1840. * @version January 2007
  1841. */
  1842. function render_question($form_content, $answers = array()) {
  1843. $question = new ch_yesno();
  1844. $question->render_question($form_content, $answers);
  1845. }
  1846. }
  1847. class ch_multipleresponse extends survey_question
  1848. {
  1849. /**
  1850. * This function creates the form elements for the multiple response questions
  1851. *
  1852. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1853. * @version January 2007
  1854. */
  1855. function create_form($survey_data, $form_content) {
  1856. $this->html = parent::create_form($survey_data, $form_content);
  1857. // Horizontal or vertical
  1858. $this->html .= ' <div class="row">';
  1859. $this->html .= ' <label class="control-label">';
  1860. $this->html .= get_lang('DisplayAnswersHorVert');
  1861. $this->html .= ' </label>';
  1862. $this->html .= ' <div class="formw">';
  1863. $this->html .= ' <input name="horizontalvertical" type="radio" value="horizontal" ';
  1864. if (empty($form_content['horizontalvertical']) || $form_content['horizontalvertical'] == 'horizontal') {
  1865. $this->html .= 'checked="checked"';
  1866. }
  1867. $this->html .= '/>'.get_lang('Horizontal').'</label><br />';
  1868. $this->html .= ' <input name="horizontalvertical" type="radio" value="vertical" ';
  1869. if (isset($form_content['horizontalvertical']) && $form_content['horizontalvertical'] == 'vertical') {
  1870. $this->html .= 'checked="checked"';
  1871. }
  1872. $this->html .= ' />'.get_lang('Vertical').'</label>';
  1873. $this->html .= ' </div>';
  1874. $this->html .= ' </div>';
  1875. // The options
  1876. $this->html .= ' <div class="row">';
  1877. $this->html .= ' <label class="control-label">';
  1878. $this->html .= get_lang('AnswerOptions');
  1879. $this->html .= ' </label>';
  1880. $this->html .= ' <div class="formw">';
  1881. $total_number_of_answers = count($form_content['answers']);
  1882. $this->html .= ' <table>';
  1883. if (is_array($form_content['answers'])) { // Values of question options
  1884. foreach ($form_content['answers'] as $key => & $value) {
  1885. $this->html .= ' <tr>';
  1886. $this->html .= ' <td align="right"><label for="answers['.$key.']">'.($key + 1).'</label></td>';
  1887. //$this->html .= ' <td><input type="text" name="answers['.$key.']" id="answers['.$key.']" value="'.$form_content['answers'][$key].'" /></td>';
  1888. $this->html .= ' <td width="550">'.api_return_html_area('answers['.$key.']', api_html_entity_decode(stripslashes($form_content['answers'][$key]), ENT_QUOTES), '', '', null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120')).'</td>';
  1889. $this->html .= ' <td>';
  1890. if ($key<$total_number_of_answers - 1) {
  1891. $this->html .= ' <input type="image" style="width:22px" src="../img/icons/22/down.png" value="move_down['.$key.']" name="move_down['.$key.']"/>';
  1892. }
  1893. if ($key > 0) {
  1894. $this->html .= ' <input type="image" style="width:22px" src="../img/icons/22/up.png" value="move_up['.$key.']" name="move_up['.$key.']"/>';
  1895. }
  1896. if ($total_number_of_answers > 2) {
  1897. $this->html .= ' <input type="image" style="width:22px" src="../img/icons/22/delete.png" value="delete_answer['.$key.']" name="delete_answer['.$key.']"/>';
  1898. }
  1899. $this->html .= ' </td>';
  1900. $this->html .= ' </tr>';
  1901. }
  1902. }
  1903. // The buttons for adding or removing
  1904. $this->html .= ' </table>';
  1905. $this->html .= ' </div>';
  1906. $this->html .= ' </div>';
  1907. $this->html .= parent :: add_remove_buttons($form_content);
  1908. }
  1909. /**
  1910. * Render the multiple response question type
  1911. *
  1912. * @param unknown_type $form_content
  1913. *
  1914. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1915. * @version January 2007
  1916. */
  1917. function render_question($form_content, $answers = array())
  1918. {
  1919. foreach ($form_content['options'] as $key => & $value) {
  1920. if ($form_content['display'] == 'vertical') {
  1921. $this->html .= '<label class="checkbox"><input name="question'.$form_content['question_id'].'[]" type="checkbox" value="'.$key.'"';
  1922. } else {
  1923. $this->html .= '<label class="checkbox inline"><input name="question'.$form_content['question_id'].'[]" type="checkbox" value="'.$key.'"';
  1924. }
  1925. if (is_array($answers)) {
  1926. if (in_array($key, $answers)) {
  1927. $this->html .= 'checked="checked"';
  1928. }
  1929. }
  1930. if (substr_count($value, '<p>') == 1) {
  1931. $this->html .= '/>'.substr($value, 3, (strlen($value) - 7)).'</label>';
  1932. if ($form_content['display'] == 'vertical') {
  1933. $this->html .= '<br />';
  1934. }
  1935. } else {
  1936. $this->html .= '/>'.$value.'</label>';
  1937. }
  1938. }
  1939. echo '<div class="survey_question_wrapper">';
  1940. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  1941. echo '<div class="control-group">';
  1942. echo $this->html;
  1943. echo '</div>';
  1944. echo '</div>';
  1945. }
  1946. }
  1947. class ch_dropdown extends survey_question
  1948. {
  1949. /**
  1950. * This function creates the form elements for the dropdown questions
  1951. *
  1952. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1953. * @version January 2007
  1954. */
  1955. function create_form($survey_data, $form_content)
  1956. {
  1957. $this->html = parent::create_form($survey_data, $form_content);
  1958. // The answers
  1959. $this->html .= ' <div class="row">';
  1960. $this->html .= ' <label class="control-label">';
  1961. $this->html .= get_lang('AnswerOptions');
  1962. $this->html .= ' </label>';
  1963. $this->html .= ' <div class="formw">';
  1964. $total_number_of_answers = count($form_content['answers']);
  1965. $this->html .= ' <table>';
  1966. foreach ($form_content['answers'] as $key => & $value) {
  1967. $this->html .= ' <tr>';
  1968. $this->html .= ' <td align="right"><label for="answers['.$key.']">'.($key + 1).'</label></td>';
  1969. $this->html .= ' <td><input type="text" name="answers['.$key.']" id="answers['.$key.']" value="'.stripslashes($form_content['answers'][$key]).'" /></td>';
  1970. $this->html .= ' <td>';
  1971. if ($key < $total_number_of_answers - 1) {
  1972. $this->html .= ' <input type="image" style="width:22px" src="../img/icons/22/down.png" value="move_down['.$key.']" name="move_down['.$key.']"/>';
  1973. }
  1974. if ($key > 0) {
  1975. $this->html .= ' <input type="image" style="width:22px" src="../img/icons/22/up.png" value="move_up['.$key.']" name="move_up['.$key.']"/>';
  1976. }
  1977. if ($total_number_of_answers> 2) {
  1978. $this->html .= ' <input type="image" style="width:22px" src="../img/icons/22/delete.png" value="delete_answer['.$key.']" name="delete_answer['.$key.']"/>';
  1979. }
  1980. $this->html .= ' </td>';
  1981. $this->html .= ' </tr>';
  1982. }
  1983. // The buttons for adding or removing
  1984. $this->html .= ' </table>';
  1985. $this->html .= ' </div>';
  1986. $this->html .= ' </div>';
  1987. $this->html .= parent :: add_remove_buttons($form_content);
  1988. }
  1989. /**
  1990. * Render the dropdown question type
  1991. *
  1992. * @param unknown_type $form_content
  1993. *
  1994. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1995. * @version January 2007
  1996. */
  1997. function render_question($form_content, $answers = array())
  1998. {
  1999. foreach ($form_content['options'] as $key => & $value) {
  2000. $this->html .= '<option value="'.$key.'" ';
  2001. if (is_array($answers)) {
  2002. if (in_array($key, $answers)) {
  2003. $this->html .= 'selected="selected"';
  2004. }
  2005. }
  2006. $this->html .= '>'.$value.'</option>';
  2007. }
  2008. echo '<div class="survey_question_wrapper">';
  2009. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  2010. echo '<div class="survey_question_options">';
  2011. echo '<select name="question'.$form_content['question_id'].'" id="select">';
  2012. echo $this->html;
  2013. echo '</select>';
  2014. echo '</div>';
  2015. echo '</div>';
  2016. /*
  2017. <option value="test">test</option>
  2018. */
  2019. }
  2020. }
  2021. class ch_open extends survey_question
  2022. {
  2023. /**
  2024. * This function creates the form elements for the open questions
  2025. *
  2026. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2027. * @version January 2007
  2028. *
  2029. * @todo add a limit for the number of characters that can be type
  2030. * @todo add a checkbox weither the answer is a textarea or a wysiwyg editor
  2031. */
  2032. function create_form($survey_data, $form_content)
  2033. {
  2034. $this->html = parent::create_form($survey_data, $form_content);
  2035. }
  2036. /**
  2037. * render the open question type
  2038. *
  2039. * @param unknown_type $form_content
  2040. *
  2041. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2042. * @version January 2007
  2043. */
  2044. function render_question($form_content, $answers = array())
  2045. {
  2046. echo '<div class="survey_question_wrapper">';
  2047. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  2048. echo '<div class="survey_question_options">';
  2049. if (is_array($answers)) {
  2050. $content = implode('', $answers);
  2051. } else {
  2052. $content = $answers;
  2053. }
  2054. echo '<label for="question'.$form_content['question_id'].'"></label>
  2055. <textarea name="question'.$form_content['question_id'].'" id="textarea" style="width: 400px; height: 130px;">'.$content.'</textarea>';
  2056. echo '</div>';
  2057. echo '</div>';
  2058. }
  2059. }
  2060. class ch_comment extends survey_question
  2061. {
  2062. /**
  2063. * This function creates the form elements for a comment.
  2064. * A comment is nothing more than a block of text that the user can read
  2065. *
  2066. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2067. * @version January 2007
  2068. *
  2069. * @param array $form_content
  2070. */
  2071. function create_form($survey_data, $form_content)
  2072. {
  2073. $this->html = parent::create_form($survey_data, $form_content);
  2074. }
  2075. /**
  2076. * Render the comment "question" type
  2077. *
  2078. * @param unknown_type $form_content
  2079. *
  2080. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2081. * @version January 2007
  2082. */
  2083. function render_question($form_content)
  2084. {
  2085. echo '<div class="survey_question_wrapper">';
  2086. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  2087. echo '</div>';
  2088. }
  2089. }
  2090. class ch_pagebreak extends survey_question
  2091. {
  2092. /**
  2093. * This function creates the form elements for a comment.
  2094. * A comment is nothing more than a block of text that the user can read
  2095. *
  2096. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2097. * @version January 2007
  2098. *
  2099. * @param array $form_content
  2100. */
  2101. function create_form($survey_data, $form_content)
  2102. {
  2103. $this->html = parent::create_form($survey_data, $form_content);
  2104. }
  2105. }
  2106. class ch_percentage extends survey_question
  2107. {
  2108. function create_form($survey_data, $form_content)
  2109. {
  2110. $this->html = parent::create_form($survey_data, $form_content);
  2111. }
  2112. function render_question($form_content, $answers = array()) {
  2113. $this->html .= '<option value="--">--</option>';
  2114. foreach ($form_content['options'] as $key => & $value) {
  2115. $this->html .= '<option value="'.$key.'" ';
  2116. if (is_array($answers)) {
  2117. if (in_array($key, $answers)) {
  2118. $this->html .= 'selected="selected"';
  2119. }
  2120. }
  2121. $this->html .= '>'.$value.'</option>';
  2122. }
  2123. echo '<div class="survey_question_wrapper">';
  2124. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  2125. echo '<div class="survey_question_options">';
  2126. echo '<select name="question'.$form_content['question_id'].'" id="select">';
  2127. echo $this->html;
  2128. echo '</select>';
  2129. echo '</div>';
  2130. echo '</div>';
  2131. }
  2132. }
  2133. class ch_score extends survey_question
  2134. {
  2135. function create_form($survey_data, $form_content)
  2136. {
  2137. $this->html = parent::create_form($survey_data, $form_content);
  2138. // The maximum score that can be given
  2139. $this->html .= ' <div class="control-group">';
  2140. $this->html .= ' <label class="control-label">';
  2141. $this->html .= ' <span class="form_required">*</span>'.get_lang('MaximumScore');
  2142. $this->html .= ' </label>';
  2143. $this->html .= ' <div class="controls">';
  2144. $this->html .= ' <input type="text" name="maximum_score" value="'.$form_content['maximum_score'].'">';
  2145. $this->html .= ' </div>';
  2146. $this->html .= ' </div>';
  2147. // The answers
  2148. $this->html .= ' <div class="row">';
  2149. $this->html .= ' <label class="control-label">';
  2150. $this->html .= get_lang('AnswerOptions');
  2151. $this->html .= ' </label>';
  2152. $this->html .= ' <div class="formw">';
  2153. $total_number_of_answers = count($form_content['answers']);
  2154. $this->html .= ' <table>';
  2155. foreach ($form_content['answers'] as $key => & $value) {
  2156. $this->html .= ' <tr>';
  2157. $this->html .= ' <td align="right"><label for="answers['.$key.']">'.($key+1).'</label></td>';
  2158. //$this->html .= ' <td><input type="text" name="answers['.$key.']" id="answers['.$key.']" value="'.$form_content['answers'][$key].'" /></td>';
  2159. $this->html .= ' <td width="550">'.api_return_html_area('answers['.$key.']', stripslashes($form_content['answers'][$key]), '', '', null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120')).'</td>';
  2160. $this->html .= ' <td>';
  2161. if ($key<$total_number_of_answers - 1) {
  2162. $this->html .= ' <input type="image" style="width:22px" src="../img/icons/22/down.png" value="move_down['.$key.']" name="move_down['.$key.']"/>';
  2163. }
  2164. if ($key > 0) {
  2165. $this->html .= ' <input type="image" style="width:22px" src="../img/icons/22/up.png" value="move_up['.$key.']" name="move_up['.$key.']"/>';
  2166. }
  2167. if ($total_number_of_answers > 2) {
  2168. $this->html .= ' <input type="image" style="width:22px" src="../img/icons/22/delete.png" value="delete_answer['.$key.']" name="delete_answer['.$key.']"/>';
  2169. }
  2170. $this->html .= ' </td>';
  2171. $this->html .= ' </tr>';
  2172. }
  2173. // The buttons for adding or removing
  2174. $this->html .= ' </table>';
  2175. $this->html .= ' </div>';
  2176. $this->html .= ' </div>';
  2177. $this->html .= parent :: add_remove_buttons($form_content);
  2178. }
  2179. function render_question($form_content, $answers = array())
  2180. {
  2181. /*
  2182. echo '<div style="border: 1px solid red;">';
  2183. echo '<pre>';
  2184. print_r($answers);
  2185. echo '</pre></div>';
  2186. */
  2187. $this->html = '<table>';
  2188. foreach ($form_content['options'] as $key => & $value) {
  2189. $this->html .= '<tr>
  2190. <td>'.$value.'</td>';
  2191. $this->html .= ' <td>';
  2192. $this->html .= '<select name="question'.$form_content['question_id'].'['.$key.']">';
  2193. $this->html .= '<option value="--">--</option>';
  2194. for ($i=1; $i <= $form_content['maximum_score']; $i++) {
  2195. $this->html .= '<option value="'.$i.'"';
  2196. if ($answers[$key] == $i) {
  2197. $this->html .= 'selected="selected" ';
  2198. }
  2199. $this->html .= '>'.$i.'</option>';
  2200. }
  2201. $this->html .= '</select>';
  2202. $this->html .= ' </td>';
  2203. $this->html .= '</tr>';
  2204. }
  2205. $this->html .= '</table>';
  2206. echo '<div class="survey_question_wrapper">';
  2207. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  2208. echo '<div class="survey_question_options">';
  2209. //echo '<select name="question'.$form_content['question_id'].'" id="select">';
  2210. echo $this->html;
  2211. //echo '</select>';
  2212. echo '</div>';
  2213. echo '</div>';
  2214. }
  2215. }
  2216. /**
  2217. * This class offers a series of general utility functions for survey querying and display
  2218. * @package chamilo.survey
  2219. */
  2220. class SurveyUtil
  2221. {
  2222. /**
  2223. * Checks whether the given survey has a pagebreak question as the first or the last question.
  2224. * If so, break the current process, displaying an error message
  2225. * @param integer Survey ID (database ID)
  2226. * @param boolean Optional. Whether to continue the current process or exit when breaking condition found. Defaults to true (do not break).
  2227. * @return void
  2228. */
  2229. static function check_first_last_question($survey_id, $continue = true)
  2230. {
  2231. // Table definitions
  2232. $tbl_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  2233. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  2234. $course_id = api_get_course_int_id();
  2235. // Getting the information of the question
  2236. $sql = "SELECT * FROM $tbl_survey_question WHERE c_id = $course_id AND survey_id='".Database::escape_string($survey_id)."' ORDER BY sort ASC";
  2237. $result = Database::query($sql);
  2238. $total = Database::num_rows($result);
  2239. $counter = 1;
  2240. $error = false;
  2241. while ($row = Database::fetch_array($result, 'ASSOC')) {
  2242. if ($counter == 1 && $row['type'] == 'pagebreak') {
  2243. Display::display_error_message(get_lang('PagebreakNotFirst'), false);
  2244. $error = true;
  2245. }
  2246. if ($counter == $total && $row['type'] == 'pagebreak') {
  2247. Display::display_error_message(get_lang('PagebreakNotLast'), false);
  2248. $error = true;
  2249. }
  2250. $counter++;
  2251. }
  2252. if (!$continue && $error) {
  2253. Display::display_footer();
  2254. exit;
  2255. }
  2256. }
  2257. /**
  2258. * This function removes an (or multiple) answer(s) of a user on a question of a survey
  2259. *
  2260. * @param mixed The user id or email of the person who fills the survey
  2261. * @param integer The survey id
  2262. * @param integer The question id
  2263. * @param integer The option id
  2264. *
  2265. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2266. * @version January 2007
  2267. */
  2268. static function remove_answer($user, $survey_id, $question_id, $course_id) {
  2269. $course_id = intval($course_id);
  2270. // table definition
  2271. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  2272. $sql = "DELETE FROM $table_survey_answer
  2273. WHERE c_id = $course_id AND
  2274. user = '".Database::escape_string($user)."'
  2275. AND survey_id = '".Database::escape_string($survey_id)."'
  2276. AND question_id = '".Database::escape_string($question_id)."'";
  2277. Database::query($sql);
  2278. }
  2279. /**
  2280. * This function stores an answer of a user on a question of a survey
  2281. *
  2282. * @param mixed The user id or email of the person who fills the survey
  2283. * @param integer Survey id
  2284. * @param integer Question id
  2285. * @param integer Option id
  2286. * @param string Option value
  2287. * @param array Survey data settings
  2288. *
  2289. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2290. * @version January 2007
  2291. */
  2292. static function store_answer($user, $survey_id, $question_id, $option_id, $option_value, $survey_data)
  2293. {
  2294. // Table definition
  2295. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  2296. // Make the survey anonymous
  2297. if ($survey_data['anonymous'] == 1) {
  2298. if (!isset($_SESSION['surveyuser'])) {
  2299. $user = md5($user.time());
  2300. $_SESSION['surveyuser'] = $user;
  2301. } else {
  2302. $user = $_SESSION['surveyuser'];
  2303. }
  2304. }
  2305. $course_id = $survey_data['c_id'];
  2306. $sql = "INSERT INTO $table_survey_answer (c_id, user, survey_id, question_id, option_id, value) VALUES (
  2307. $course_id,
  2308. '".Database::escape_string($user)."',
  2309. '".Database::escape_string($survey_id)."',
  2310. '".Database::escape_string($question_id)."',
  2311. '".Database::escape_string($option_id)."',
  2312. '".Database::escape_string($option_value)."'
  2313. )";
  2314. Database::query($sql);
  2315. }
  2316. /**
  2317. * This function checks the parameters that are used in this page
  2318. *
  2319. * @return string The header, an error and the footer if any parameter fails, else it returns true
  2320. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2321. * @version February 2007
  2322. */
  2323. static function check_parameters($people_filled)
  2324. {
  2325. $error = false;
  2326. // Getting the survey data
  2327. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  2328. // $_GET['survey_id'] has to be numeric
  2329. if (!is_numeric($_GET['survey_id'])) {
  2330. $error = get_lang('IllegalSurveyId');
  2331. }
  2332. // $_GET['action']
  2333. $allowed_actions = array('overview', 'questionreport', 'userreport', 'comparativereport', 'completereport','deleteuserreport');
  2334. if (isset($_GET['action']) && !in_array($_GET['action'], $allowed_actions)) {
  2335. $error = get_lang('ActionNotAllowed');
  2336. }
  2337. // User report
  2338. if (isset($_GET['action']) && $_GET['action'] == 'userreport') {
  2339. if ($survey_data['anonymous'] == 0) {
  2340. foreach ($people_filled as $key => & $value) {
  2341. $people_filled_userids[] = $value['invited_user'];
  2342. }
  2343. } else {
  2344. $people_filled_userids = $people_filled;
  2345. }
  2346. if (isset($_GET['user']) && !in_array($_GET['user'], $people_filled_userids)) {
  2347. $error = get_lang('UnknowUser');
  2348. }
  2349. }
  2350. // Question report
  2351. if (isset($_GET['action']) && $_GET['action'] == 'questionreport') {
  2352. if (isset($_GET['question']) && !is_numeric($_GET['question'])) {
  2353. $error = get_lang('UnknowQuestion');
  2354. }
  2355. }
  2356. if ($error) {
  2357. $tool_name = get_lang('Reporting');
  2358. Display::display_header($tool_name);
  2359. Display::display_error_message(get_lang('Error').': '.$error, false);
  2360. Display::display_footer();
  2361. exit;
  2362. } else {
  2363. return true;
  2364. }
  2365. }
  2366. /**
  2367. * This function deals with the action handling
  2368. * @return void
  2369. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2370. * @version February 2007
  2371. */
  2372. public static function handle_reporting_actions($survey_data, $people_filled)
  2373. {
  2374. $action = isset($_GET['action']) ? $_GET['action'] : null;
  2375. // Getting the number of question
  2376. $temp_questions_data = survey_manager::get_questions($_GET['survey_id']);
  2377. // Sorting like they should be displayed and removing the non-answer question types (comment and pagebreak)
  2378. $my_temp_questions_data = $temp_questions_data == null ? array() : $temp_questions_data;
  2379. $questions_data = array();
  2380. foreach ($my_temp_questions_data as $key => & $value) {
  2381. if ($value['type'] != 'comment' && $value['type'] != 'pagebreak') {
  2382. $questions_data[$value['sort']] = $value;
  2383. }
  2384. }
  2385. // Counting the number of questions that are relevant for the reporting
  2386. $survey_data['number_of_questions'] = count($questions_data);
  2387. if ($action == 'questionreport') {
  2388. SurveyUtil::display_question_report($survey_data);
  2389. }
  2390. if ($action == 'userreport') {
  2391. SurveyUtil::display_user_report($people_filled, $survey_data);
  2392. }
  2393. if ($action == 'comparativereport') {
  2394. SurveyUtil::display_comparative_report();
  2395. }
  2396. if ($action == 'completereport') {
  2397. SurveyUtil::display_complete_report($survey_data);
  2398. }
  2399. if ($action == 'deleteuserreport') {
  2400. SurveyUtil::delete_user_report($_GET['survey_id'], $_GET['user']);
  2401. //SurveyUtil::display_user_report(); //Could work but looks a bit clunky
  2402. }
  2403. }
  2404. /**
  2405. * This function deletes the report of an user who wants to retake the survey
  2406. * @param integer survey_id
  2407. * @param integer user_id
  2408. * @return void
  2409. * @author Christian Fasanando Flores <christian.fasanando@dokeos.com>
  2410. * @version November 2008
  2411. */
  2412. function delete_user_report($survey_id, $user_id)
  2413. {
  2414. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  2415. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  2416. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  2417. $course_id = api_get_course_int_id();
  2418. if (!empty($survey_id) && !empty($user_id)) {
  2419. // delete data from survey_answer by user_id and survey_id
  2420. $sql = "DELETE FROM $table_survey_answer
  2421. WHERE c_id = $course_id AND survey_id = '".(int)$survey_id."' AND user = '".(int)$user_id."'";
  2422. Database::query($sql);
  2423. // update field answered from survey_invitation by user_id and survey_id
  2424. $sql = "UPDATE $table_survey_invitation SET answered = '0'
  2425. WHERE
  2426. c_id = $course_id AND
  2427. survey_code = (
  2428. SELECT code FROM $table_survey
  2429. WHERE
  2430. c_id = $course_id AND
  2431. survey_id = '".(int)$survey_id."'
  2432. ) AND
  2433. user = '".(int)$user_id."'";
  2434. $result = Database::query($sql);
  2435. }
  2436. if ($result !== false) {
  2437. $message = get_lang('SurveyUserAnswersHaveBeenRemovedSuccessfully').'<br />
  2438. <a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action=userreport&survey_id='.Security::remove_XSS($survey_id).'">'.get_lang('GoBack').'</a>';
  2439. Display::display_confirmation_message($message, false);
  2440. }
  2441. }
  2442. /**
  2443. * This function displays the user report which is basically nothing more than a one-page display of all the questions
  2444. * of the survey that is filled with the answers of the person who filled the survey.
  2445. *
  2446. * @return string html code of the one-page survey with the answers of the selected user
  2447. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2448. * @version February 2007 - Updated March 2008
  2449. */
  2450. static function display_user_report($people_filled, $survey_data)
  2451. {
  2452. // Database table definitions
  2453. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  2454. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  2455. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  2456. // Actions bar
  2457. echo '<div class="actions">';
  2458. echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?survey_id='.Security::remove_XSS($_GET['survey_id']).'">'.
  2459. Display::return_icon('back.png',get_lang('BackTo').' '.get_lang('ReportingOverview'),'',ICON_SIZE_MEDIUM).'</a>';
  2460. if (isset($_GET['user'])) {
  2461. if (api_is_allowed_to_edit()) {
  2462. // The delete link
  2463. echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action=deleteuserreport&amp;survey_id='.Security::remove_XSS($_GET['survey_id']).'&amp;user='.Security::remove_XSS($_GET['user']).'" >'.
  2464. Display::return_icon('delete.png', get_lang('Delete'),'',ICON_SIZE_MEDIUM).'</a>';
  2465. }
  2466. // Export the user report
  2467. echo '<a href="javascript: void(0);" onclick="document.form1a.submit();">'.
  2468. Display::return_icon('export_csv.png', get_lang('ExportAsCSV'),'',ICON_SIZE_MEDIUM).'</a> ';
  2469. echo '<a href="javascript: void(0);" onclick="document.form1b.submit();">'.
  2470. Display::return_icon('export_excel.png', get_lang('ExportAsXLS'),'',ICON_SIZE_MEDIUM).'</a> ';
  2471. echo '<form id="form1a" name="form1a" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'&user_id='.Security::remove_XSS($_GET['user']).'">';
  2472. echo '<input type="hidden" name="export_report" value="export_report">';
  2473. echo '<input type="hidden" name="export_format" value="csv">';
  2474. echo '</form>';
  2475. echo '<form id="form1b" name="form1b" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'&user_id='.Security::remove_XSS($_GET['user']).'">';
  2476. echo '<input type="hidden" name="export_report" value="export_report">';
  2477. echo '<input type="hidden" name="export_format" value="xls">';
  2478. echo '</form>';
  2479. echo '<form id="form2" name="form2" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'">';
  2480. }
  2481. echo '</div>';
  2482. // Step 1: selection of the user
  2483. echo "<script>
  2484. function jumpMenu(targ,selObj,restore) {
  2485. eval(targ+\".location='\"+selObj.options[selObj.selectedIndex].value+\"'\");
  2486. if (restore) selObj.selectedIndex=0;
  2487. }
  2488. </script>";
  2489. echo get_lang('SelectUserWhoFilledSurvey').'<br />';
  2490. echo '<select name="user" onchange="jumpMenu(\'parent\',this,0)">';
  2491. echo '<option value="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action='.Security::remove_XSS($_GET['action']).'&amp;survey_id='.Security::remove_XSS($_GET['survey_id']).'">'.get_lang('SelectUser').'</option>';
  2492. foreach ($people_filled as $key => & $person) {
  2493. if ($survey_data['anonymous'] == 0) {
  2494. $name = api_get_person_name($person['firstname'], $person['lastname']);
  2495. $id = $person['user_id'];
  2496. if ($id == '') {
  2497. $id = $person['invited_user'];
  2498. $name = $person['invited_user'];
  2499. }
  2500. } else {
  2501. $name = get_lang('Anonymous') . ' ' . ($key + 1);
  2502. $id = $person;
  2503. }
  2504. echo '<option value="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'&user='.Security::remove_XSS($id).'" ';
  2505. if (isset($_GET['user']) && $_GET['user'] == $id) {
  2506. echo 'selected="selected"';
  2507. }
  2508. echo '>'.$name.'</option>';
  2509. }
  2510. echo '</select>';
  2511. $course_id = api_get_course_int_id();
  2512. // Step 2: displaying the survey and the answer of the selected users
  2513. if (isset($_GET['user'])) {
  2514. Display::display_normal_message(get_lang('AllQuestionsOnOnePage'), false);
  2515. // Getting all the questions and options
  2516. $sql = "SELECT
  2517. survey_question.question_id,
  2518. survey_question.survey_id,
  2519. survey_question.survey_question,
  2520. survey_question.display,
  2521. survey_question.max_value,
  2522. survey_question.sort,
  2523. survey_question.type,
  2524. survey_question_option.question_option_id,
  2525. survey_question_option.option_text,
  2526. survey_question_option.sort as option_sort
  2527. FROM $table_survey_question survey_question
  2528. LEFT JOIN $table_survey_question_option survey_question_option
  2529. ON
  2530. survey_question.question_id = survey_question_option.question_id AND
  2531. survey_question_option.c_id = $course_id
  2532. WHERE
  2533. survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."' AND
  2534. survey_question.c_id = $course_id
  2535. ORDER BY survey_question.sort, survey_question_option.sort ASC";
  2536. $result = Database::query($sql);
  2537. while ($row = Database::fetch_array($result, 'ASSOC')) {
  2538. if($row['type'] != 'pagebreak') {
  2539. $questions[$row['sort']]['question_id'] = $row['question_id'];
  2540. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  2541. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  2542. $questions[$row['sort']]['display'] = $row['display'];
  2543. $questions[$row['sort']]['type'] = $row['type'];
  2544. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  2545. $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text'];
  2546. }
  2547. }
  2548. // Getting all the answers of the user
  2549. $sql = "SELECT * FROM $table_survey_answer
  2550. WHERE
  2551. c_id = $course_id AND
  2552. survey_id = '".Database::escape_string($_GET['survey_id'])."' AND
  2553. user = '".Database::escape_string($_GET['user'])."'";
  2554. $result = Database::query($sql);
  2555. while ($row = Database::fetch_array($result, 'ASSOC')) {
  2556. $answers[$row['question_id']][] = $row['option_id'];
  2557. $all_answers[$row['question_id']][] = $row;
  2558. }
  2559. // Displaying all the questions
  2560. $second_parameter=array();
  2561. foreach ($questions as & $question) {
  2562. // If the question type is a scoring then we have to format the answers differently
  2563. if ($question['type'] == 'score') {
  2564. if (is_array($question) && is_array($all_answers)) {
  2565. foreach ($all_answers[$question['question_id']] as $key => & $answer_array) {
  2566. $second_parameter[$answer_array['option_id']] = $answer_array['value'];
  2567. }
  2568. }
  2569. } else {
  2570. $second_parameter = $answers[$question['question_id']];
  2571. if ($question['type'] == 'open') {
  2572. $second_parameter = array();
  2573. $second_parameter[] = $all_answers[$question['question_id']][0]['option_id'];
  2574. }
  2575. }
  2576. $ch_type = 'ch_'.$question['type'];
  2577. $display = new $ch_type;
  2578. $display->render_question($question, $second_parameter);
  2579. }
  2580. }
  2581. }
  2582. /**
  2583. * This function displays the report by question.
  2584. *
  2585. * It displays a table with all the options of the question and the number of users who have answered positively on the option.
  2586. * The number of users who answered positive on a given option is expressed in an absolute number, in a percentage of the total
  2587. * and graphically using bars
  2588. * By clicking on the absolute number you get a list with the persons who have answered this.
  2589. * You can then click on the name of the person and you will then go to the report by user where you see all the
  2590. * answers of that user.
  2591. *
  2592. * @param array All the survey data
  2593. * @return string html code that displays the report by question
  2594. * @todo allow switching between horizontal and vertical.
  2595. * @todo multiple response: percentage are probably not OK
  2596. * @todo the question and option text have to be shortened and should expand when the user clicks on it.
  2597. * @todo the pagebreak and comment question types should not be shown => removed from $survey_data before
  2598. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2599. * @version February 2007 - Updated March 2008
  2600. */
  2601. public static function display_question_report($survey_data)
  2602. {
  2603. $course_id = api_get_course_int_id();
  2604. // Database table definitions
  2605. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  2606. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  2607. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  2608. // Determining the offset of the sql statement (the n-th question of the survey)
  2609. if (!isset($_GET['question'])) {
  2610. $offset = 0;
  2611. } else {
  2612. $offset = intval($_GET['question']);
  2613. }
  2614. $currentQuestion = isset($_GET['question']) ? $_GET['question'] : 0;
  2615. $question = array();
  2616. echo '<div class="actions">';
  2617. echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?survey_id='.Security::remove_XSS($_GET['survey_id']).'">'.
  2618. Display::return_icon('back.png',get_lang('BackTo').' '.get_lang('ReportingOverview'),'',ICON_SIZE_MEDIUM).'</a>';
  2619. echo '</div>';
  2620. if ($survey_data['number_of_questions'] > 0) {
  2621. echo '<div id="question_report_questionnumbers">'.get_lang('GoToQuestion').': ';
  2622. for ($i = 1; $i <= $survey_data['number_of_questions']; $i++) {
  2623. if ($offset != $i - 1) {
  2624. echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action=questionreport&amp;survey_id='.Security::remove_XSS($_GET['survey_id']).'&amp;question='.($i-1).'">'.$i.'</a>';
  2625. } else {
  2626. echo $i;
  2627. }
  2628. if ($i < $survey_data['number_of_questions']) {
  2629. echo ' | ';
  2630. }
  2631. }
  2632. echo '</div>';
  2633. // Getting the question information
  2634. $sql = "SELECT * FROM $table_survey_question
  2635. WHERE
  2636. c_id = $course_id AND
  2637. survey_id='".Database::escape_string($_GET['survey_id'])."' AND
  2638. type<>'pagebreak' AND type<>'comment'
  2639. ORDER BY sort ASC
  2640. LIMIT ".$offset.",1";
  2641. $result = Database::query($sql);
  2642. $question = Database::fetch_array($result);
  2643. // Navigate through the questions (next and previous)
  2644. if ($currentQuestion != 0 ) {
  2645. echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action='.Security::remove_XSS($_GET['action']).'&amp;survey_id='.$_GET['survey_id'].'&amp;question='.Security::remove_XSS($offset-1).'">'.
  2646. Display::return_icon('action_prev.png', get_lang('PreviousQuestion'), array('align' => 'middle')).' '.get_lang('PreviousQuestion').'</a> ';
  2647. } else {
  2648. echo Display::return_icon('action_prev.png', get_lang('PreviousQuestion'), array('align' => 'middle')).' '.get_lang('PreviousQuestion').' ';
  2649. }
  2650. echo ' | ';
  2651. if ($currentQuestion < ($survey_data['number_of_questions'] - 1)) {
  2652. echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action='.Security::remove_XSS($_GET['action']).'&amp;survey_id='.Security::remove_XSS($_GET['survey_id']).'&amp;question='.Security::remove_XSS($offset+1).'">'.get_lang('NextQuestion').' '.Display::return_icon('action_next.png', get_lang('NextQuestion'), array('align' => 'middle')).'</a>';
  2653. } else {
  2654. echo get_lang('NextQuestion').' '.Display::return_icon('action_next.png', get_lang('NextQuestion'), array('align' => 'middle'));
  2655. }
  2656. }
  2657. echo isset($question['survey_question']) ? $question['survey_question'] : null;
  2658. if ($question['type'] == 'score') {
  2659. /** @todo This function should return the options as this is needed further in the code */
  2660. $options = SurveyUtil::display_question_report_score($survey_data, $question, $offset);
  2661. } elseif ($question['type'] == 'open') {
  2662. /** @todo Also get the user who has answered this */
  2663. $sql = "SELECT * FROM $table_survey_answer
  2664. WHERE
  2665. c_id = $course_id AND
  2666. survey_id='".Database::escape_string($_GET['survey_id'])."' AND
  2667. question_id = '".Database::escape_string($question['question_id'])."'";
  2668. $result = Database::query($sql);
  2669. while ($row = Database::fetch_array($result)) {
  2670. echo $row['option_id'].'<hr noshade="noshade" size="1" />';
  2671. }
  2672. } else {
  2673. // Getting the options
  2674. $sql = "SELECT * FROM $table_survey_question_option
  2675. WHERE
  2676. c_id = $course_id AND survey_id='".Database::escape_string($_GET['survey_id'])."' AND
  2677. question_id = '".Database::escape_string($question['question_id'])."'
  2678. ORDER BY sort ASC";
  2679. $result = Database::query($sql);
  2680. while ($row = Database::fetch_array($result)) {
  2681. $options[$row['question_option_id']] = $row;
  2682. }
  2683. // Getting the answers
  2684. $sql = "SELECT *, count(answer_id) as total FROM $table_survey_answer
  2685. WHERE
  2686. c_id = $course_id AND
  2687. survey_id='".Database::escape_string($_GET['survey_id'])."' AND
  2688. question_id = '".Database::escape_string($question['question_id'])."'
  2689. GROUP BY option_id, value";
  2690. $result = Database::query($sql);
  2691. $number_of_answers = 0;
  2692. $data = array();
  2693. while ($row = Database::fetch_array($result)) {
  2694. $number_of_answers += $row['total'];
  2695. $data[$row['option_id']] = $row;
  2696. }
  2697. // displaying the table: headers
  2698. echo '<table class="data_table">';
  2699. echo ' <tr>';
  2700. echo ' <th>&nbsp;</th>';
  2701. echo ' <th>'.get_lang('AbsoluteTotal').'</th>';
  2702. echo ' <th>'.get_lang('Percentage').'</th>';
  2703. echo ' <th>'.get_lang('VisualRepresentation').'</th>';
  2704. echo ' <tr>';
  2705. // Displaying the table: the content
  2706. if (is_array($options)) {
  2707. foreach ($options as $key => & $value) {
  2708. $absolute_number = null;
  2709. if (isset($data[$value['question_option_id']])) {
  2710. $absolute_number = $data[$value['question_option_id']]['total'];
  2711. }
  2712. if ($question['type'] == 'percentage' && empty($absolute_number)) {
  2713. continue;
  2714. }
  2715. if ($number_of_answers == 0) {
  2716. $answers_number = 0;
  2717. } else {
  2718. $answers_number = $absolute_number/$number_of_answers*100;
  2719. }
  2720. echo ' <tr>';
  2721. echo ' <td>'.$value['option_text'].'</td>';
  2722. echo ' <td align="right"><a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action='.Security::remove_XSS($_GET['action']).'&amp;survey_id='.Security::remove_XSS($_GET['survey_id']).'&amp;question='.Security::remove_XSS($offset).'&amp;viewoption='.$value['question_option_id'].'">'.$absolute_number.'</a></td>';
  2723. echo ' <td align="right">'.round($answers_number, 2).' %</td>';
  2724. echo ' <td align="right">';
  2725. $size = $answers_number*2;
  2726. if ($size > 0) {
  2727. echo '<div style="border:1px solid #264269; background-color:#aecaf4; height:10px; width:'.$size.'px">&nbsp;</div>';
  2728. }
  2729. echo ' </td>';
  2730. echo ' </tr>';
  2731. }
  2732. }
  2733. // displaying the table: footer (totals)
  2734. echo ' <tr>';
  2735. echo ' <td style="border-top:1px solid black;"><b>'.get_lang('Total').'</b></td>';
  2736. echo ' <td style="border-top:1px solid black;" align="right"><b>'.($number_of_answers==0?'0':$number_of_answers).'</b></td>';
  2737. echo ' <td style="border-top:1px solid black;">&nbsp;</td>';
  2738. echo ' <td style="border-top:1px solid black;">&nbsp;</td>';
  2739. echo ' </tr>';
  2740. echo '</table>';
  2741. }
  2742. if (isset($_GET['viewoption'])) {
  2743. echo get_lang('PeopleWhoAnswered').': '.$options[Security::remove_XSS($_GET['viewoption'])]['option_text'].'<br />';
  2744. if (is_numeric($_GET['value'])) {
  2745. $sql_restriction = "AND value='".Database::escape_string($_GET['value'])."'";
  2746. }
  2747. $sql = "SELECT user FROM $table_survey_answer WHERE c_id = $course_id AND option_id = '".Database::escape_string($_GET['viewoption'])."' $sql_restriction";
  2748. $result = Database::query($sql);
  2749. while ($row = Database::fetch_array($result)) {
  2750. $user_info = api_get_user_info($row['user']);
  2751. echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action=userreport&survey_id='.Security::remove_XSS($_GET['survey_id']).'&user='.$row['user'].'">'.$user_info['complete_name'].'</a><br />';
  2752. }
  2753. }
  2754. }
  2755. /**
  2756. * Display score data about a survey question
  2757. * @param array Question info
  2758. * @param integer The offset of results shown
  2759. * @return void (direct output)
  2760. */
  2761. public static function display_question_report_score($survey_data, $question, $offset)
  2762. {
  2763. $course_id = api_get_course_int_id();
  2764. // Database table definitions
  2765. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  2766. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  2767. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  2768. $course_id = api_get_course_int_id();
  2769. // Getting the options
  2770. $sql = "SELECT * FROM $table_survey_question_option
  2771. WHERE
  2772. c_id = $course_id AND
  2773. survey_id='".Database::escape_string($_GET['survey_id'])."' AND
  2774. question_id = '".Database::escape_string($question['question_id'])."'
  2775. ORDER BY sort ASC";
  2776. $result = Database::query($sql);
  2777. while ($row = Database::fetch_array($result)) {
  2778. $options[$row['question_option_id']] = $row;
  2779. }
  2780. // Getting the answers
  2781. $sql = "SELECT *, count(answer_id) as total FROM $table_survey_answer
  2782. WHERE
  2783. c_id = $course_id AND
  2784. survey_id='".Database::escape_string($_GET['survey_id'])."' AND
  2785. question_id = '".Database::escape_string($question['question_id'])."'
  2786. GROUP BY option_id, value";
  2787. $result = Database::query($sql);
  2788. while ($row = Database::fetch_array($result)) {
  2789. $number_of_answers += $row['total'];
  2790. $data[$row['option_id']][$row['value']] = $row;
  2791. }
  2792. // Displaying the table: headers
  2793. echo '<table class="data_table">';
  2794. echo ' <tr>';
  2795. echo ' <th>&nbsp;</th>';
  2796. echo ' <th>'.get_lang('Score').'</th>';
  2797. echo ' <th>'.get_lang('AbsoluteTotal').'</th>';
  2798. echo ' <th>'.get_lang('Percentage').'</th>';
  2799. echo ' <th>'.get_lang('VisualRepresentation').'</th>';
  2800. echo ' <tr>';
  2801. // Displaying the table: the content
  2802. foreach ($options as $key => & $value) {
  2803. for ($i = 1; $i <= $question['max_value']; $i++) {
  2804. $absolute_number = $data[$value['question_option_id']][$i]['total'];
  2805. echo ' <tr>';
  2806. echo ' <td>'.$value['option_text'].'</td>';
  2807. echo ' <td>'.$i.'</td>';
  2808. echo ' <td><a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action='.Security::remove_XSS($_GET['action']).'&amp;survey_id='.Security::remove_XSS($_GET['survey_id']).'&amp;question='.Security::remove_XSS($offset).'&amp;viewoption='.$value['question_option_id'].'&amp;value='.$i.'">'.$absolute_number.'</a></td>';
  2809. echo ' <td>'.round($absolute_number/$number_of_answers*100, 2).' %</td>';
  2810. echo ' <td>';
  2811. $size = ($absolute_number/$number_of_answers*100*2);
  2812. if ($size > 0) {
  2813. echo ' <div style="border:1px solid #264269; background-color:#aecaf4; height:10px; width:'.$size.'px">&nbsp;</div>';
  2814. }
  2815. echo ' </td>';
  2816. echo ' </tr>';
  2817. }
  2818. }
  2819. // Displaying the table: footer (totals)
  2820. echo ' <tr>';
  2821. echo ' <td style="border-top:1px solid black"><b>'.get_lang('Total').'</b></td>';
  2822. echo ' <td style="border-top:1px solid black">&nbsp;</td>';
  2823. echo ' <td style="border-top:1px solid black"><b>'.$number_of_answers.'</b></td>';
  2824. echo ' <td style="border-top:1px solid black">&nbsp;</td>';
  2825. echo ' <td style="border-top:1px solid black">&nbsp;</td>';
  2826. echo ' </tr>';
  2827. echo '</table>';
  2828. }
  2829. /**
  2830. * This functions displays the complete reporting
  2831. * @return string HTML code
  2832. * @todo open questions are not in the complete report yet.
  2833. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2834. * @version February 2007
  2835. */
  2836. public static function display_complete_report($survey_data)
  2837. {
  2838. // Database table definitions
  2839. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  2840. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  2841. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  2842. // Actions bar
  2843. echo '<div class="actions">';
  2844. echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?survey_id='.Security::remove_XSS($_GET['survey_id']).'">
  2845. '.Display::return_icon('back.png',get_lang('BackTo').' '.get_lang('ReportingOverview'),'',ICON_SIZE_MEDIUM).'</a>';
  2846. echo '<a class="survey_export_link" href="javascript: void(0);" onclick="document.form1a.submit();">
  2847. '.Display::return_icon('export_csv.png',get_lang('ExportAsCSV'),'',ICON_SIZE_MEDIUM).'</a>';
  2848. echo '<a class="survey_export_link" href="javascript: void(0);" onclick="document.form1b.submit();">
  2849. '.Display::return_icon('export_excel.png',get_lang('ExportAsXLS'),'',ICON_SIZE_MEDIUM).'</a>';
  2850. echo '</div>';
  2851. // The form
  2852. echo '<form id="form1a" name="form1a" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'">';
  2853. echo '<input type="hidden" name="export_report" value="export_report">';
  2854. echo '<input type="hidden" name="export_format" value="csv">';
  2855. echo '</form>';
  2856. echo '<form id="form1b" name="form1b" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'">';
  2857. echo '<input type="hidden" name="export_report" value="export_report">';
  2858. echo '<input type="hidden" name="export_format" value="xls">';
  2859. echo '</form>';
  2860. echo '<form id="form2" name="form2" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'">';
  2861. // The table
  2862. echo '<br /><table class="data_table" border="1">';
  2863. // Getting the number of options per question
  2864. echo ' <tr>';
  2865. echo ' <th>';
  2866. if (isset($_POST['submit_question_filter']) && $_POST['submit_question_filter'] ||
  2867. isset($_POST['export_report']) && $_POST['export_report']) {
  2868. echo '<button class="cancel" type="submit" name="reset_question_filter" value="'.get_lang('ResetQuestionFilter').'">'.get_lang('ResetQuestionFilter').'</button>';
  2869. }
  2870. echo '<button class="save" type="submit" name="submit_question_filter" value="'.get_lang('SubmitQuestionFilter').'">'.get_lang('SubmitQuestionFilter').'</button>';
  2871. echo '</th>';
  2872. $display_extra_user_fields = false;
  2873. if (!(isset($_POST['submit_question_filter']) && $_POST['submit_question_filter'] ||
  2874. isset($_POST['export_report']) && $_POST['export_report']) || !empty($_POST['fields_filter'])) {
  2875. // Show user fields section with a big th colspan that spans over all fields
  2876. $extra_user_fields = UserManager::get_extra_fields(0, 0, 5, 'ASC', false, true);
  2877. $num = count($extra_user_fields);
  2878. if ($num > 0 ) {
  2879. echo '<th '.($num>0?' colspan="'.$num.'"':'').'>';
  2880. echo '<label><input type="checkbox" name="fields_filter" value="1" checked="checked"/> ';
  2881. echo get_lang('UserFields');
  2882. echo '</label>';
  2883. echo '</th>';
  2884. $display_extra_user_fields = true;
  2885. }
  2886. }
  2887. $course_id = api_get_course_int_id();
  2888. // Get all the questions ordered by the "sort" column
  2889. // <hub> modify the query to display open questions too
  2890. // $sql = "SELECT q.question_id, q.type, q.survey_question, count(o.question_option_id) as number_of_options
  2891. // FROM $table_survey_question q LEFT JOIN $table_survey_question_option o
  2892. // ON q.question_id = o.question_id
  2893. // WHERE q.question_id = o.question_id
  2894. // AND q.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  2895. // GROUP BY q.question_id
  2896. // ORDER BY q.sort ASC";
  2897. $sql = "SELECT q.question_id, q.type, q.survey_question, count(o.question_option_id) as number_of_options
  2898. FROM $table_survey_question q LEFT JOIN $table_survey_question_option o
  2899. ON q.question_id = o.question_id
  2900. WHERE q.survey_id = '".Database::escape_string($_GET['survey_id'])."' AND
  2901. q.c_id = $course_id AND
  2902. o.c_id = $course_id
  2903. GROUP BY q.question_id
  2904. ORDER BY q.sort ASC";
  2905. // </hub>
  2906. $result = Database::query($sql);
  2907. while ($row = Database::fetch_array($result)) {
  2908. // We show the questions if
  2909. // 1. there is no question filter and the export button has not been clicked
  2910. // 2. there is a quesiton filter but the question is selected for display
  2911. //if (!($_POST['submit_question_filter'] || $_POST['export_report']) || in_array($row['question_id'], $_POST['questions_filter'])) {
  2912. if (!(isset($_POST['submit_question_filter']) && $_POST['submit_question_filter']) ||
  2913. (is_array($_POST['questions_filter']) &&
  2914. in_array($row['question_id'], $_POST['questions_filter']))) {
  2915. // We do not show comment and pagebreak question types
  2916. if ($row['type'] != 'comment' && $row['type'] != 'pagebreak') {
  2917. echo ' <th';
  2918. // <hub> modified tst to include percentage
  2919. if ($row['number_of_options'] > 0 && $row['type'] != 'percentage') {
  2920. // </hub>
  2921. echo ' colspan="'.$row['number_of_options'].'"';
  2922. }
  2923. echo '>';
  2924. echo '<label><input type="checkbox" name="questions_filter[]" value="'.$row['question_id'].'" checked="checked"/> ';
  2925. echo $row['survey_question'];
  2926. echo '</label>';
  2927. echo '</th>';
  2928. }
  2929. // No column at all if it's not a question
  2930. }
  2931. $questions[$row['question_id']] = $row;
  2932. }
  2933. echo ' </tr>';
  2934. // Getting all the questions and options
  2935. echo ' <tr>';
  2936. echo ' <th>&nbsp;</th>'; // the user column
  2937. if (!(isset($_POST['submit_question_filter']) && $_POST['submit_question_filter'] ||
  2938. isset($_POST['export_report']) && $_POST['export_report']) || !empty($_POST['fields_filter'])) {
  2939. //show the fields names for user fields
  2940. foreach($extra_user_fields as & $field) {
  2941. echo '<th>'.$field[3].'</th>';
  2942. }
  2943. }
  2944. // cells with option (none for open question)
  2945. $sql = "SELECT sq.question_id, sq.survey_id,
  2946. sq.survey_question, sq.display,
  2947. sq.sort, sq.type, sqo.question_option_id,
  2948. sqo.option_text, sqo.sort as option_sort
  2949. FROM $table_survey_question sq
  2950. LEFT JOIN $table_survey_question_option sqo
  2951. ON sq.question_id = sqo.question_id
  2952. WHERE
  2953. sq.survey_id = '".Database::escape_string($_GET['survey_id'])."' AND
  2954. sq.c_id = $course_id AND
  2955. sqo.c_id = $course_id
  2956. ORDER BY sq.sort ASC, sqo.sort ASC";
  2957. $result = Database::query($sql);
  2958. $display_percentage_header = 1; // in order to display only once the cell option (and not 100 times)
  2959. while ($row = Database::fetch_array($result)) {
  2960. // We show the options if
  2961. // 1. there is no question filter and the export button has not been clicked
  2962. // 2. there is a question filter but the question is selected for display
  2963. //if (!($_POST['submit_question_filter'] || $_POST['export_report']) || in_array($row['question_id'], $_POST['questions_filter'])) {
  2964. if (!(isset($_POST['submit_question_filter']) && $_POST['submit_question_filter']) || (is_array($_POST['questions_filter']) &&
  2965. in_array($row['question_id'], $_POST['questions_filter']))) {
  2966. // <hub> modif 05-05-2010
  2967. // we do not show comment and pagebreak question types
  2968. if ($row['type'] == 'open') {
  2969. echo '<th>&nbsp;-&nbsp;</th>';
  2970. $possible_answers[$row['question_id']][$row['question_option_id']] = $row['question_option_id'];
  2971. $display_percentage_header = 1;
  2972. } else if ($row['type'] == 'percentage' && $display_percentage_header) {
  2973. echo '<th>&nbsp;%&nbsp;</th>';
  2974. $possible_answers[$row['question_id']][$row['question_option_id']] = $row['question_option_id'];
  2975. $display_percentage_header = 0;
  2976. } else if ($row['type'] == 'percentage') {
  2977. $possible_answers[$row['question_id']][$row['question_option_id']] = $row['question_option_id'];
  2978. } else if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak' AND $row['type'] <> 'percentage') {
  2979. echo '<th>';
  2980. echo $row['option_text'];
  2981. echo '</th>';
  2982. $possible_answers[$row['question_id']][$row['question_option_id']] = $row['question_option_id'];
  2983. $display_percentage_header = 1;
  2984. }
  2985. //no column at all if the question was not a question
  2986. // </hub>
  2987. }
  2988. }
  2989. echo ' </tr>';
  2990. // Getting all the answers of the users
  2991. $old_user = '';
  2992. $answers_of_user = array();
  2993. $sql = "SELECT * FROM $table_survey_answer WHERE c_id = $course_id AND survey_id='".Database::escape_string($_GET['survey_id'])."' ORDER BY user ASC";
  2994. $result = Database::query($sql);
  2995. $i = 1;
  2996. while ($row = Database::fetch_array($result)) {
  2997. if ($old_user != $row['user'] && $old_user != '') {
  2998. $userParam = $old_user;
  2999. if ($survey_data['anonymous'] != 0) {
  3000. $userParam = $i;
  3001. $i++;
  3002. }
  3003. SurveyUtil::display_complete_report_row($survey_data, $possible_answers, $answers_of_user, $userParam, $questions, $display_extra_user_fields);
  3004. $answers_of_user=array();
  3005. }
  3006. if ($questions[$row['question_id']]['type'] != 'open') {
  3007. $answers_of_user[$row['question_id']][$row['option_id']] = $row;
  3008. } else {
  3009. $answers_of_user[$row['question_id']][0] = $row;
  3010. }
  3011. $old_user = $row['user'];
  3012. }
  3013. $userParam = $old_user;
  3014. if ($survey_data['anonymous'] != 0) {
  3015. $userParam = $i;
  3016. $i++;
  3017. }
  3018. SurveyUtil::display_complete_report_row($survey_data, $possible_answers, $answers_of_user, $userParam, $questions, $display_extra_user_fields);
  3019. // This is to display the last user
  3020. echo '</table>';
  3021. echo '</form>';
  3022. }
  3023. /**
  3024. * This function displays a row (= a user and his/her answers) in the table of the complete report.
  3025. *
  3026. * @param array $survey_data
  3027. * @param array Possible options
  3028. * @param array User answers
  3029. * @param mixed User ID or user details string
  3030. * @param boolean Whether to show extra user fields or not
  3031. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3032. * @version February 2007 - Updated March 2008
  3033. */
  3034. static function display_complete_report_row($survey_data, $possible_options, $answers_of_user, $user, $questions, $display_extra_user_fields = false)
  3035. {
  3036. $user = Security::remove_XSS($user);
  3037. echo '<tr>';
  3038. if ($survey_data['anonymous'] == 0) {
  3039. if (intval($user) !== 0) {
  3040. $sql = 'SELECT firstname, lastname FROM '.Database::get_main_table(TABLE_MAIN_USER).' WHERE user_id='.intval($user);
  3041. $rs = Database::query($sql);
  3042. if ($row = Database::fetch_array($rs)) {
  3043. $user_displayed = api_get_person_name($row['firstname'], $row['lastname']);
  3044. } else {
  3045. $user_displayed = '-';
  3046. }
  3047. echo '<th><a href="'.api_get_self().'?action=userreport&survey_id='.Security::remove_XSS($_GET['survey_id']).'&user='.$user.'">'.$user_displayed.'</a></th>'; // the user column
  3048. } else {
  3049. echo '<th>'.$user.'</th>'; // the user column
  3050. }
  3051. } else {
  3052. echo '<th>' . get_lang('Anonymous') . ' ' . $user . '</th>';
  3053. }
  3054. if ($display_extra_user_fields) {
  3055. // Show user fields data, if any, for this user
  3056. $user_fields_values = UserManager::get_extra_user_data(intval($user), false, false, false, true);
  3057. foreach ($user_fields_values as & $value) {
  3058. echo '<td align="center">'.$value.'</td>';
  3059. }
  3060. }
  3061. if (is_array($possible_options)) {
  3062. // <hub> modified to display open answers and percentage
  3063. foreach ($possible_options as $question_id => & $possible_option) {
  3064. if ($questions[$question_id]['type'] == 'open') {
  3065. echo '<td align="center">';
  3066. echo $answers_of_user[$question_id]['0']['option_id'];
  3067. echo '</td>';
  3068. } else {
  3069. foreach ($possible_option as $option_id => & $value) {
  3070. if ($questions[$question_id]['type'] == 'percentage') {
  3071. if (!empty($answers_of_user[$question_id][$option_id])) {
  3072. echo "<td align='center'>";
  3073. echo $answers_of_user[$question_id][$option_id]['value'];
  3074. echo "</td>";
  3075. }
  3076. }
  3077. else {
  3078. echo '<td align="center">';
  3079. if (!empty($answers_of_user[$question_id][$option_id])) {
  3080. if ($answers_of_user[$question_id][$option_id]['value'] != 0) {
  3081. echo $answers_of_user[$question_id][$option_id]['value'];
  3082. }
  3083. else {
  3084. echo 'v';
  3085. }
  3086. }
  3087. } // </hub>
  3088. }
  3089. }
  3090. }
  3091. }
  3092. echo '</tr>';
  3093. }
  3094. /**
  3095. * Quite similar to display_complete_report(), returns an HTML string
  3096. * that can be used in a csv file
  3097. * @todo consider merging this function with display_complete_report
  3098. * @return string The contents of a csv file
  3099. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3100. * @version February 2007
  3101. */
  3102. static function export_complete_report($survey_data, $user_id = 0)
  3103. {
  3104. // Database table definitions
  3105. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  3106. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  3107. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  3108. // The first column
  3109. $return = ';';
  3110. // Show extra fields blank space (enough for extra fields on next line)
  3111. $extra_user_fields = UserManager::get_extra_fields(0, 0, 5, 'ASC', false, true);
  3112. $num = count($extra_user_fields);
  3113. $return .= str_repeat(';', $num);
  3114. $course_id = api_get_course_int_id();
  3115. $sql = "SELECT questions.question_id, questions.type, questions.survey_question, count(options.question_option_id) as number_of_options
  3116. FROM $table_survey_question questions
  3117. LEFT JOIN $table_survey_question_option options
  3118. ON questions.question_id = options.question_id AND options.c_id = $course_id
  3119. WHERE
  3120. questions.survey_id = '".Database::escape_string($_GET['survey_id'])."' AND
  3121. questions.c_id = $course_id
  3122. GROUP BY questions.question_id
  3123. ORDER BY questions.sort ASC";
  3124. $result = Database::query($sql);
  3125. while ($row = Database::fetch_array($result)) {
  3126. // We show the questions if
  3127. // 1. there is no question filter and the export button has not been clicked
  3128. // 2. there is a quesiton filter but the question is selected for display
  3129. if (!($_POST['submit_question_filter']) ||
  3130. (is_array($_POST['questions_filter']) &&
  3131. in_array($row['question_id'], $_POST['questions_filter']))
  3132. ) {
  3133. // We do not show comment and pagebreak question types
  3134. if ($row['type'] != 'comment' && $row['type'] != 'pagebreak') {
  3135. if ($row['number_of_options'] == 0 && $row['type'] == 'open') {
  3136. $return .= str_replace("\r\n",' ', api_html_entity_decode(strip_tags($row['survey_question']), ENT_QUOTES)).';';
  3137. } else {
  3138. for ($ii = 0; $ii < $row['number_of_options']; $ii++) {
  3139. $return .= str_replace("\r\n",' ', api_html_entity_decode(strip_tags($row['survey_question']), ENT_QUOTES)).';';
  3140. }
  3141. }
  3142. }
  3143. }
  3144. }
  3145. $return .= "\n";
  3146. // Getting all the questions and options
  3147. $return .= ';';
  3148. // Show extra field values
  3149. //if (!($_POST['submit_question_filter'] || $_POST['export_report']) || !empty($_POST['fields_filter'])) {
  3150. // Show the fields names for user fields
  3151. if (!empty($extra_user_fields)) {
  3152. foreach ($extra_user_fields as & $field) {
  3153. $return .= '"'.str_replace("\r\n",' ',api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES)).'";';
  3154. }
  3155. }
  3156. //}
  3157. $sql = "SELECT
  3158. survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type,
  3159. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  3160. FROM $table_survey_question survey_question
  3161. LEFT JOIN $table_survey_question_option survey_question_option
  3162. ON survey_question.question_id = survey_question_option.question_id AND survey_question_option.c_id = $course_id
  3163. WHERE
  3164. survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."' AND
  3165. survey_question.c_id = $course_id
  3166. ORDER BY survey_question.sort ASC, survey_question_option.sort ASC";
  3167. $result = Database::query($sql);
  3168. $possible_answers = array();
  3169. $possible_answers_type = array();
  3170. while ($row = Database::fetch_array($result)) {
  3171. // We show the options if
  3172. // 1. there is no question filter and the export button has not been clicked
  3173. // 2. there is a quesiton filter but the question is selected for display
  3174. if (!($_POST['submit_question_filter']) || (is_array($_POST['questions_filter']) && in_array($row['question_id'], $_POST['questions_filter']))) {
  3175. // We do not show comment and pagebreak question types
  3176. if ($row['type'] != 'comment' && $row['type'] != 'pagebreak') {
  3177. $row['option_text'] = str_replace(array("\r","\n"),array('',''),$row['option_text']);
  3178. $return .= api_html_entity_decode(strip_tags($row['option_text']), ENT_QUOTES).';';
  3179. $possible_answers[$row['question_id']][$row['question_option_id']] = $row['question_option_id'];
  3180. $possible_answers_type[$row['question_id']] = $row['type'];
  3181. }
  3182. }
  3183. }
  3184. $return .= "\n";
  3185. // Getting all the answers of the users
  3186. $old_user = '';
  3187. $answers_of_user = array();
  3188. $sql = "SELECT * FROM $table_survey_answer
  3189. WHERE c_id = $course_id AND survey_id='".Database::escape_string($_GET['survey_id'])."'";
  3190. if ($user_id != 0) {
  3191. $sql .= "AND user='".Database::escape_string($user_id)."' ";
  3192. }
  3193. $sql .= "ORDER BY user ASC";
  3194. $open_question_iterator = 1;
  3195. $result = Database::query($sql);
  3196. while ($row = Database::fetch_array($result)) {
  3197. if ($old_user != $row['user'] && $old_user != '') {
  3198. $return .= SurveyUtil::export_complete_report_row($survey_data, $possible_answers, $answers_of_user, $old_user, true);
  3199. $answers_of_user=array();
  3200. }
  3201. if($possible_answers_type[$row['question_id']] == 'open') {
  3202. $temp_id = 'open'.$open_question_iterator;
  3203. $answers_of_user[$row['question_id']][$temp_id] = $row;
  3204. $open_question_iterator++;
  3205. } else {
  3206. $answers_of_user[$row['question_id']][$row['option_id']] = $row;
  3207. }
  3208. $old_user = $row['user'];
  3209. }
  3210. $return .= SurveyUtil::export_complete_report_row($survey_data, $possible_answers, $answers_of_user, $old_user, true); // This is to display the last user
  3211. return $return;
  3212. }
  3213. /**
  3214. * Add a line to the csv file
  3215. *
  3216. * @param array Possible answers
  3217. * @param array User's answers
  3218. * @param mixed User ID or user details as string - Used as a string in the result string
  3219. * @param boolean Whether to display user fields or not
  3220. * @return string One line of the csv file
  3221. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3222. * @version February 2007
  3223. */
  3224. static function export_complete_report_row($survey_data, $possible_options, $answers_of_user, $user, $display_extra_user_fields = false)
  3225. {
  3226. $return = '';
  3227. if ($survey_data['anonymous'] == 0) {
  3228. if (intval($user) !== 0) {
  3229. $sql = 'SELECT firstname, lastname FROM '.Database::get_main_table(TABLE_MAIN_USER).' WHERE user_id='.intval($user);
  3230. $rs = Database::query($sql);
  3231. if ($row = Database::fetch_array($rs)) {
  3232. $user_displayed = api_get_person_name($row['firstname'], $row['lastname']);
  3233. } else {
  3234. $user_displayed = '-';
  3235. }
  3236. $return .= $user_displayed.';';
  3237. } else {
  3238. $return .= $user.';';
  3239. }
  3240. } else {
  3241. $return .= '-;'; // The user column
  3242. }
  3243. if ($display_extra_user_fields) {
  3244. // Show user fields data, if any, for this user
  3245. $user_fields_values = UserManager::get_extra_user_data($user,false,false, false, true);
  3246. foreach ($user_fields_values as & $value) {
  3247. $return .= '"'.str_replace('"', '""', api_html_entity_decode(strip_tags($value), ENT_QUOTES)).'";';
  3248. }
  3249. }
  3250. if (is_array($possible_options)) {
  3251. foreach ($possible_options as $question_id => $possible_option) {
  3252. if (is_array($possible_option) && count($possible_option) > 0) {
  3253. foreach ($possible_option as $option_id => & $value) {
  3254. $my_answer_of_user = ($answers_of_user[$question_id] == null) ? array() : $answers_of_user[$question_id];
  3255. $key = array_keys($my_answer_of_user);
  3256. if (substr($key[0], 0, 4) == 'open') {
  3257. $return .= '"'.str_replace('"', '""', api_html_entity_decode(strip_tags($answers_of_user[$question_id][$key[0]]['option_id']), ENT_QUOTES)).'"';
  3258. } elseif (!empty($answers_of_user[$question_id][$option_id])) {
  3259. //$return .= 'v';
  3260. if ($answers_of_user[$question_id][$option_id]['value'] != 0) {
  3261. $return .= $answers_of_user[$question_id][$option_id]['value'];
  3262. } else {
  3263. $return .= 'v';
  3264. }
  3265. }
  3266. $return .= ';';
  3267. }
  3268. }
  3269. }
  3270. }
  3271. $return .= "\n";
  3272. return $return;
  3273. }
  3274. /**
  3275. * Quite similar to display_complete_report(), returns an HTML string
  3276. * that can be used in a csv file
  3277. * @todo consider merging this function with display_complete_report
  3278. * @return string The contents of a csv file
  3279. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3280. * @version February 2007
  3281. */
  3282. static function export_complete_report_xls($survey_data, $filename, $user_id = 0)
  3283. {
  3284. require_once api_get_path(LIBRARY_PATH).'pear/Spreadsheet_Excel_Writer/Writer.php';
  3285. $workbook = new Spreadsheet_Excel_Writer();
  3286. $workbook ->setTempDir(api_get_path(SYS_ARCHIVE_PATH));
  3287. $workbook->send($filename);
  3288. $workbook->setVersion(8); // BIFF8
  3289. $worksheet =& $workbook->addWorksheet('Report 1');
  3290. $worksheet->setInputEncoding(api_get_system_encoding());
  3291. $line = 0;
  3292. $column = 1; // Skip the first column (row titles)
  3293. // Show extra fields blank space (enough for extra fields on next line)
  3294. $display_extra_user_fields = false;
  3295. //if (!empty($_REQUEST['fields_filter'])) {
  3296. // Show user fields section with a big th colspan that spans over all fields
  3297. $extra_user_fields = UserManager::get_extra_fields(0, 0, 5, 'ASC', false, true);
  3298. $num = count($extra_user_fields);
  3299. for ($i = 0; $i < $num; $i++) {
  3300. $worksheet->write($line, $column, '');
  3301. $column++;
  3302. }
  3303. $display_extra_user_fields = true;
  3304. //}
  3305. // Database table definitions
  3306. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  3307. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  3308. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  3309. $course_id = api_get_course_int_id();
  3310. // First line (questions)
  3311. $sql = "SELECT questions.question_id, questions.type, questions.survey_question, count(options.question_option_id) as number_of_options
  3312. FROM $table_survey_question questions
  3313. LEFT JOIN $table_survey_question_option options
  3314. ON questions.question_id = options.question_id AND options.c_id = $course_id
  3315. WHERE
  3316. questions.survey_id = '".Database::escape_string($_GET['survey_id'])."' AND
  3317. questions.c_id = $course_id
  3318. GROUP BY questions.question_id
  3319. ORDER BY questions.sort ASC";
  3320. $result = Database::query($sql);
  3321. while ($row = Database::fetch_array($result)) {
  3322. // We show the questions if
  3323. // 1. there is no question filter and the export button has not been clicked
  3324. // 2. there is a quesiton filter but the question is selected for display
  3325. if (!($_POST['submit_question_filter']) || (is_array($_POST['questions_filter']) && in_array($row['question_id'], $_POST['questions_filter']))) {
  3326. // We do not show comment and pagebreak question types
  3327. if ($row['type'] != 'comment' && $row['type'] != 'pagebreak') {
  3328. if ($row['number_of_options'] == 0 && $row['type'] == 'open') {
  3329. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['survey_question']), ENT_QUOTES));
  3330. $column ++;
  3331. } else {
  3332. for ($ii = 0; $ii < $row['number_of_options']; $ii ++) {
  3333. $worksheet->write($line, $column, api_html_entity_decode(strip_tags($row['survey_question']), ENT_QUOTES));
  3334. $column ++;
  3335. }
  3336. }
  3337. }
  3338. }
  3339. }
  3340. $line++;
  3341. $column = 1;
  3342. // Show extra field values
  3343. if ($display_extra_user_fields) {
  3344. // Show the fields names for user fields
  3345. foreach ($extra_user_fields as & $field) {
  3346. $worksheet->write($line, $column, api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES));
  3347. $column++;
  3348. }
  3349. }
  3350. // Getting all the questions and options (second line)
  3351. $sql = "SELECT survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type,
  3352. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  3353. FROM $table_survey_question survey_question
  3354. LEFT JOIN $table_survey_question_option survey_question_option
  3355. ON survey_question.question_id = survey_question_option.question_id AND survey_question_option.c_id = $course_id
  3356. WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."' AND
  3357. survey_question.c_id = $course_id
  3358. ORDER BY survey_question.sort ASC, survey_question_option.sort ASC";
  3359. $result = Database::query($sql);
  3360. $possible_answers = array();
  3361. $possible_answers_type = array();
  3362. while ($row = Database::fetch_array($result)) {
  3363. // We show the options if
  3364. // 1. there is no question filter and the export button has not been clicked
  3365. // 2. there is a quesiton filter but the question is selected for display
  3366. if (!($_POST['submit_question_filter']) || (is_array($_POST['questions_filter']) && in_array($row['question_id'], $_POST['questions_filter']))) {
  3367. // We do not show comment and pagebreak question types
  3368. if ($row['type'] != 'comment' && $row['type'] != 'pagebreak') {
  3369. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['option_text']), ENT_QUOTES));
  3370. $possible_answers[$row['question_id']][$row['question_option_id']] = $row['question_option_id'];
  3371. $possible_answers_type[$row['question_id']] = $row['type'];
  3372. $column++;
  3373. }
  3374. }
  3375. }
  3376. // Getting all the answers of the users
  3377. $line ++;
  3378. $column = 0;
  3379. $old_user = '';
  3380. $answers_of_user = array();
  3381. $sql = "SELECT * FROM $table_survey_answer WHERE c_id = $course_id AND survey_id='".Database::escape_string($_GET['survey_id'])."' ";
  3382. if ($user_id != 0) {
  3383. $sql .= "AND user='".Database::escape_string($user_id)."' ";
  3384. }
  3385. $sql .= "ORDER BY user ASC";
  3386. $open_question_iterator = 1;
  3387. $result = Database::query($sql);
  3388. while ($row = Database::fetch_array($result)) {
  3389. if ($old_user != $row['user'] && $old_user != '') {
  3390. $return = SurveyUtil::export_complete_report_row_xls($survey_data, $possible_answers, $answers_of_user, $old_user, true);
  3391. foreach ($return as $elem) {
  3392. $worksheet->write($line, $column, $elem);
  3393. $column++;
  3394. }
  3395. $answers_of_user = array();
  3396. $line++;
  3397. $column = 0;
  3398. }
  3399. if ($possible_answers_type[$row['question_id']] == 'open') {
  3400. $temp_id = 'open'.$open_question_iterator;
  3401. $answers_of_user[$row['question_id']][$temp_id] = $row;
  3402. $open_question_iterator++;
  3403. } else {
  3404. $answers_of_user[$row['question_id']][$row['option_id']] = $row;
  3405. }
  3406. $old_user = $row['user'];
  3407. }
  3408. $return = SurveyUtil::export_complete_report_row_xls($survey_data, $possible_answers, $answers_of_user, $old_user, true); // this is to display the last user
  3409. foreach ($return as $elem) {
  3410. $worksheet->write($line, $column, $elem);
  3411. $column++;
  3412. }
  3413. $workbook->close();
  3414. return null;
  3415. }
  3416. /**
  3417. * Add a line to the csv file
  3418. *
  3419. * @param array Possible answers
  3420. * @param array User's answers
  3421. * @param mixed User ID or user details as string - Used as a string in the result string
  3422. * @param boolean Whether to display user fields or not
  3423. * @return string One line of the csv file
  3424. */
  3425. static function export_complete_report_row_xls($survey_data, $possible_options, $answers_of_user, $user, $display_extra_user_fields = false)
  3426. {
  3427. $return = array();
  3428. if ($survey_data['anonymous'] == 0) {
  3429. if (intval($user) !== 0) {
  3430. $sql = 'SELECT firstname, lastname FROM '.Database::get_main_table(TABLE_MAIN_USER).' WHERE user_id='.intval($user);
  3431. $rs = Database::query($sql);
  3432. if($row = Database::fetch_array($rs)) {
  3433. $user_displayed = api_get_person_name($row['firstname'], $row['lastname']);
  3434. } else {
  3435. $user_displayed = '-';
  3436. }
  3437. //echo ' <th><a href="'.api_get_self().'?action=userreport&survey_id='.Security::remove_XSS($_GET['survey_id']).'&user='.$user.'">'.$user_displayed.'</a></th>'; // the user column
  3438. $return[] = $user_displayed;
  3439. } else {
  3440. //echo ' <th>'.$user.'</th>'; // The user column
  3441. $return[] = $user;
  3442. }
  3443. } else {
  3444. $return[] = '-'; // The user column
  3445. }
  3446. if ($display_extra_user_fields) {
  3447. //show user fields data, if any, for this user
  3448. $user_fields_values = UserManager::get_extra_user_data(intval($user),false,false, false, true);
  3449. foreach($user_fields_values as $value) {
  3450. $return[] = api_html_entity_decode(strip_tags($value), ENT_QUOTES);
  3451. }
  3452. }
  3453. if (is_array($possible_options)) {
  3454. foreach ($possible_options as $question_id => & $possible_option) {
  3455. if (is_array($possible_option) && count($possible_option) > 0) {
  3456. foreach ($possible_option as $option_id => & $value) {
  3457. $my_answers_of_user = ($answers_of_user[$question_id]==null) ? array() : $answers_of_user[$question_id];
  3458. $key = array_keys($my_answers_of_user);
  3459. if (substr($key[0], 0, 4) == 'open') {
  3460. $return[] = api_html_entity_decode(strip_tags($answers_of_user[$question_id][$key[0]]['option_id']), ENT_QUOTES);
  3461. } elseif (!empty($answers_of_user[$question_id][$option_id])) {
  3462. //$return .= 'v';
  3463. if ($answers_of_user[$question_id][$option_id]['value'] != 0) {
  3464. $return[] = $answers_of_user[$question_id][$option_id]['value'];
  3465. } else {
  3466. $return[] = 'v';
  3467. }
  3468. } else {
  3469. $return[] = '';
  3470. }
  3471. }
  3472. }
  3473. }
  3474. }
  3475. return $return;
  3476. }
  3477. /**
  3478. * This function displays the comparative report which allows you to compare two questions
  3479. * A comparative report creates a table where one question is on the x axis and a second question is on the y axis.
  3480. * In the intersection is the number of people who have answerd positive on both options.
  3481. *
  3482. * @return string HTML code
  3483. *
  3484. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3485. * @version February 2007
  3486. */
  3487. static function display_comparative_report() {
  3488. // Allowed question types for comparative report
  3489. $allowed_question_types = array('yesno', 'multiplechoice', 'multipleresponse', 'dropdown', 'percentage', 'score');
  3490. // Getting all the questions
  3491. $questions = survey_manager::get_questions($_GET['survey_id']);
  3492. // Actions bar
  3493. echo '<div class="actions">';
  3494. echo '<a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?survey_id='.Security::remove_XSS($_GET['survey_id']).'">'.Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('ReportingOverview'),'',ICON_SIZE_MEDIUM).'</a>';
  3495. echo '</div>';
  3496. // Displaying an information message that only the questions with predefined answers can be used in a comparative report
  3497. Display::display_normal_message(get_lang('OnlyQuestionsWithPredefinedAnswers'), false);
  3498. // The form for selecting the axis of the table
  3499. echo '<form id="form1" name="form1" method="get" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'&xaxis='.Security::remove_XSS($_GET['xaxis']).'&y='.Security::remove_XSS($_GET['yaxis']).'">';
  3500. // Survey_id
  3501. echo '<input type="hidden" name="action" value="'.Security::remove_XSS($_GET['action']).'"/>';
  3502. echo '<input type="hidden" name="survey_id" value="'.Security::remove_XSS($_GET['survey_id']).'"/>';
  3503. // X axis
  3504. echo get_lang('SelectXAxis').': ';
  3505. echo '<select name="xaxis">';
  3506. echo '<option value="">---</option>';
  3507. foreach ($questions as $key => & $question) {
  3508. if (is_array($allowed_question_types)) {
  3509. if (in_array($question['type'], $allowed_question_types)) {
  3510. echo '<option value="'.$question['question_id'].'"';
  3511. if ($_GET['xaxis'] == $question['question_id']) {
  3512. echo ' selected="selected"';
  3513. }
  3514. echo '">'.api_substr(strip_tags($question['question']), 0, 50).'</option>';
  3515. }
  3516. }
  3517. }
  3518. echo '</select><br /><br />';
  3519. // Y axis
  3520. echo get_lang('SelectYAxis').': ';
  3521. echo '<select name="yaxis">';
  3522. echo '<option value="">---</option>';
  3523. foreach ($questions as $key => & $question) {
  3524. if (in_array($question['type'], $allowed_question_types)) {
  3525. echo '<option value="'.$question['question_id'].'"';
  3526. if ($_GET['yaxis'] == $question['question_id']) {
  3527. echo ' selected="selected"';
  3528. }
  3529. echo '">'.api_substr(strip_tags($question['question']), 0, 50).'</option>';
  3530. }
  3531. }
  3532. echo '</select><br /><br />';
  3533. echo '<button class="save" type="submit" name="Submit" value="Submit">'.get_lang('CompareQuestions').'</button>';
  3534. echo '</form>';
  3535. // Getting all the information of the x axis
  3536. if (isset($_GET['xaxis']) && is_numeric($_GET['xaxis'])) {
  3537. $question_x = survey_manager::get_question($_GET['xaxis']);
  3538. }
  3539. // Getting all the information of the y axis
  3540. if (isset($_GET['yaxis']) && is_numeric($_GET['yaxis'])) {
  3541. $question_y = survey_manager::get_question($_GET['yaxis']);
  3542. }
  3543. if (isset($_GET['xaxis']) && is_numeric($_GET['xaxis']) && isset($_GET['yaxis']) && is_numeric($_GET['yaxis'])) {
  3544. // Getting the answers of the two questions
  3545. $answers_x = SurveyUtil::get_answers_of_question_by_user($_GET['survey_id'], $_GET['xaxis']);
  3546. $answers_y = SurveyUtil::get_answers_of_question_by_user($_GET['survey_id'], $_GET['yaxis']);
  3547. // Displaying the table
  3548. echo '<table border="1" class="data_table">';
  3549. // The header
  3550. echo ' <tr>';
  3551. for ($ii = 0; $ii <= count($question_x['answers']); $ii++) {
  3552. if ($ii == 0) {
  3553. echo ' <th>&nbsp;</th>';
  3554. } else {
  3555. if ($question_x['type'] == 'score') {
  3556. for ($x = 1; $x <= $question_x['maximum_score']; $x++) {
  3557. echo ' <th>'.$question_x['answers'][($ii-1)].'<br />'.$x.'</th>';
  3558. }
  3559. $x = '';
  3560. } else {
  3561. echo ' <th>'.$question_x['answers'][($ii-1)].'</th>';
  3562. }
  3563. }
  3564. }
  3565. echo ' </tr>';
  3566. // The main part
  3567. for ($ij = 0; $ij < count($question_y['answers']); $ij++) {
  3568. // The Y axis is a scoring question type so we have more rows than the options (actually options * maximum score)
  3569. if ($question_y['type'] == 'score') {
  3570. for ($y = 1; $y <= $question_y['maximum_score']; $y++) {
  3571. echo ' <tr>';
  3572. for ($ii = 0; $ii <= count($question_x['answers']); $ii++) {
  3573. if ($question_x['type'] == 'score') {
  3574. for ($x = 1; $x <= $question_x['maximum_score']; $x++) {
  3575. if ($ii == 0) {
  3576. echo ' <th>'.$question_y['answers'][($ij)].' '.$y.'</th>';
  3577. break;
  3578. } else {
  3579. echo ' <td align="center">';
  3580. echo SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], $x, $y);
  3581. echo '</td>';
  3582. }
  3583. }
  3584. } else {
  3585. if ($ii == 0) {
  3586. echo ' <th>'.$question_y['answers'][($ij)].' '.$y.'</th>';
  3587. } else {
  3588. echo ' <td align="center">';
  3589. echo SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], 0, $y);
  3590. echo '</td>';
  3591. }
  3592. }
  3593. }
  3594. echo ' </tr>';
  3595. }
  3596. }
  3597. // The Y axis is NOT a score question type so the number of rows = the number of options
  3598. else {
  3599. echo ' <tr>';
  3600. for ($ii = 0; $ii <= count($question_x['answers']); $ii++) {
  3601. if ($question_x['type'] == 'score') {
  3602. for ($x = 1; $x <= $question_x['maximum_score']; $x++) {
  3603. if ($ii == 0) {
  3604. echo ' <th>'.$question_y['answers'][($ij)].'</th>';
  3605. break;
  3606. } else {
  3607. echo ' <td align="center">';
  3608. echo SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], $x, 0);
  3609. echo '</td>';
  3610. }
  3611. }
  3612. } else {
  3613. if ($ii == 0) {
  3614. echo ' <th>'.$question_y['answers'][($ij)].'</th>';
  3615. } else {
  3616. echo ' <td align="center">';
  3617. echo SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)]);
  3618. echo '</td>';
  3619. }
  3620. }
  3621. }
  3622. echo ' </tr>';
  3623. }
  3624. }
  3625. echo '</table>';
  3626. }
  3627. }
  3628. /**
  3629. * Get all the answers of a question grouped by user
  3630. *
  3631. * @param integer Survey ID
  3632. * @param integer Question ID
  3633. * @return Array Array containing all answers of all users, grouped by user
  3634. *
  3635. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3636. * @version February 2007 - Updated March 2008
  3637. */
  3638. static function get_answers_of_question_by_user($survey_id, $question_id) {
  3639. $course_id = api_get_course_int_id();
  3640. // Database table definitions
  3641. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  3642. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  3643. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  3644. $sql = "SELECT * FROM $table_survey_answer
  3645. WHERE c_id = $course_id AND survey_id='".Database::escape_string($survey_id)."'
  3646. AND question_id='".Database::escape_string($question_id)."'
  3647. ORDER BY USER ASC";
  3648. $result = Database::query($sql);
  3649. while ($row = Database::fetch_array($result)) {
  3650. if ($row['value'] == 0) {
  3651. $return[$row['user']][] = $row['option_id'];
  3652. } else {
  3653. $return[$row['user']][] = $row['option_id'].'*'.$row['value'];
  3654. }
  3655. }
  3656. return $return;
  3657. }
  3658. /**
  3659. * Count the number of users who answer positively on both options
  3660. *
  3661. * @param array All answers of the x axis
  3662. * @param array All answers of the y axis
  3663. * @param integer x axis value (= the option_id of the first question)
  3664. * @param integer y axis value (= the option_id of the second question)
  3665. * @return integer Number of users who have answered positively to both options
  3666. *
  3667. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3668. * @version February 2007
  3669. */
  3670. static function comparative_check($answers_x, $answers_y, $option_x, $option_y, $value_x = 0, $value_y = 0) {
  3671. if ($value_x == 0) {
  3672. $check_x = $option_x;
  3673. } else {
  3674. $check_x = $option_x.'*'.$value_x;
  3675. }
  3676. if ($value_y == 0) {
  3677. $check_y = $option_y;
  3678. } else {
  3679. $check_y = $option_y.'*'.$value_y;
  3680. }
  3681. $counter = 0;
  3682. if (is_array($answers_x)) {
  3683. foreach ($answers_x as $user => & $answers) {
  3684. // Check if the user has given $option_x as answer
  3685. if (in_array($check_x, $answers)) {
  3686. // Check if the user has given $option_y as an answer
  3687. if (in_array($check_y, $answers_y[$user])) {
  3688. $counter++;
  3689. }
  3690. }
  3691. }
  3692. }
  3693. return $counter;
  3694. }
  3695. /**
  3696. * Get all the information about the invitations of a certain survey
  3697. *
  3698. * @return array Lines of invitation [user, code, date, empty element]
  3699. *
  3700. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3701. * @version January 2007
  3702. *
  3703. * @todo use survey_id parameter instead of $_GET
  3704. */
  3705. static function get_survey_invitations_data() {
  3706. $course_id = api_get_course_int_id();
  3707. // Database table definition
  3708. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  3709. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  3710. $sql = "SELECT
  3711. survey_invitation.user as col1,
  3712. survey_invitation.invitation_code as col2,
  3713. survey_invitation.invitation_date as col3,
  3714. '' as col4
  3715. FROM $table_survey_invitation survey_invitation
  3716. LEFT JOIN $table_user user
  3717. ON survey_invitation.user = user.user_id
  3718. WHERE
  3719. survey_invitation.c_id = $course_id AND
  3720. survey_invitation.survey_id = '".Database::escape_string($_GET['survey_id'])."' AND
  3721. session_id='".api_get_session_id()."' ";
  3722. $res = Database::query($sql);
  3723. while ($row = Database::fetch_array($res)) {
  3724. $survey_invitation_data[] = $row;
  3725. }
  3726. return $survey_invitation_data;
  3727. }
  3728. /**
  3729. * Get the total number of survey invitations for a given survey (through $_GET['survey_id'])
  3730. *
  3731. * @return integer Total number of survey invitations
  3732. *
  3733. * @todo use survey_id parameter instead of $_GET
  3734. *
  3735. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3736. * @version January 2007
  3737. */
  3738. static function get_number_of_survey_invitations()
  3739. {
  3740. $course_id = api_get_course_int_id();
  3741. // Database table definition
  3742. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  3743. $sql = "SELECT count(user) AS total
  3744. FROM $table_survey_invitation
  3745. WHERE
  3746. c_id = $course_id AND
  3747. survey_id='".Database::escape_string($_GET['survey_id'])."' AND
  3748. session_id='".api_get_session_id()."' ";
  3749. $res = Database::query($sql);
  3750. $row = Database::fetch_array($res,'ASSOC');
  3751. return $row['total'];
  3752. }
  3753. /**
  3754. * Save the invitation mail
  3755. *
  3756. * @param string Text of the e-mail
  3757. * @param integer Whether the mail contents are for invite mail (0, default) or reminder mail (1)
  3758. *
  3759. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3760. * @version January 2007
  3761. */
  3762. static function save_invite_mail($mailtext, $mail_subject, $reminder = 0) {
  3763. $course_id = api_get_course_int_id();
  3764. // Database table definition
  3765. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  3766. // Reminder or not
  3767. if ($reminder == 0) {
  3768. $mail_field = 'invite_mail';
  3769. } else {
  3770. $mail_field = 'reminder_mail';
  3771. }
  3772. $sql = "UPDATE $table_survey
  3773. SET
  3774. mail_subject='".Database::escape_string($mail_subject)."',
  3775. $mail_field = '".Database::escape_string($mailtext)."'
  3776. WHERE c_id = $course_id AND survey_id = '".Database::escape_string($_GET['survey_id'])."'";
  3777. Database::query($sql);
  3778. }
  3779. /**
  3780. * This function saves all the invitations of course users and additional users in the database
  3781. * and sends the invitations by email
  3782. *
  3783. * @param array Users array can be both a list of course uids AND a list of additional emailaddresses
  3784. * @param string Title of the invitation, used as the title of the mail
  3785. * @param string Text of the invitation, used as the text of the mail.
  3786. * The text has to contain a **link** string or this will automatically be added to the end
  3787. *
  3788. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3789. * @author Julio Montoya - Adding auto-generated link support
  3790. * @version January 2007
  3791. *
  3792. */
  3793. static function save_invitations($users_array, $invitation_title, $invitation_text, $reminder = 0, $sendmail = 0, $remindUnAnswered = 0)
  3794. {
  3795. if (!is_array($users_array)) return 0; // Should not happen
  3796. // Getting the survey information
  3797. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  3798. $survey_invitations = SurveyUtil::get_invitations($survey_data['survey_code']);
  3799. $already_invited = SurveyUtil::get_invited_users($survey_data['code']);
  3800. // Remind unanswered is a special version of remind all reminder
  3801. $exclude_users = array();
  3802. if ($remindUnAnswered == 1) { // Remind only unanswered users
  3803. $reminder = 1;
  3804. $exclude_users = survey_manager::get_people_who_filled_survey($_GET['survey_id']);
  3805. }
  3806. $counter = 0; // Nr of invitations "sent" (if sendmail option)
  3807. $course_id = api_get_course_int_id();
  3808. $session_id = api_get_session_id();
  3809. foreach ($users_array as $key=>$value) {
  3810. if (!isset($value) || $value == '') continue;
  3811. // Skip user if reminding only unanswered people
  3812. if (in_array($value, $exclude_users)) continue;
  3813. // Get the unique invitation code if we already have it
  3814. if ($reminder == 1 && array_key_exists($value, $survey_invitations)) {
  3815. $invitation_code = $survey_invitations[$value]['invitation_code'];
  3816. } else {
  3817. $invitation_code = md5($value.microtime());
  3818. }
  3819. $new_user = false; // User not already invited
  3820. // Store the invitation if user_id not in $already_invited['course_users'] OR email is not in $already_invited['additional_users']
  3821. $addit_users_array = isset($already_invited['additional_users']) && !empty($already_invited['additional_users']) ? explode(';', $already_invited['additional_users']) : array();
  3822. $my_alredy_invited = ($already_invited['course_users'] == null) ? array() : $already_invited['course_users'];
  3823. if ((is_numeric($value) && !in_array($value, $my_alredy_invited)) || (!is_numeric($value) && !in_array($value, $addit_users_array))) {
  3824. $new_user = true;
  3825. if (!array_key_exists($value, $survey_invitations)) {
  3826. $params = array(
  3827. 'c_id' => $course_id,
  3828. 'session_id' => $session_id,
  3829. 'user' => $value,
  3830. 'survey_code' => $survey_data['code'],
  3831. 'invitation_code' => $invitation_code,
  3832. 'invitation_date' => api_get_utc_datetime()
  3833. );
  3834. self::save_invitation($params);
  3835. }
  3836. }
  3837. // Send the email if checkboxed
  3838. if (($new_user || $reminder == 1) && $sendmail != 0) {
  3839. // Make a change for absolute url
  3840. if (isset($invitation_text)) {
  3841. $invitation_text = api_html_entity_decode($invitation_text, ENT_QUOTES);
  3842. $invitation_text = str_replace('src="../../', 'src="'.api_get_path(WEB_PATH), $invitation_text);
  3843. $invitation_text = trim(stripslashes($invitation_text));
  3844. }
  3845. SurveyUtil::send_invitation_mail($value, $invitation_code, $invitation_title, $invitation_text);
  3846. $counter++;
  3847. }
  3848. }
  3849. return $counter; // Number of invitations sent
  3850. }
  3851. static function save_invitation($params)
  3852. {
  3853. // Database table to store the invitations data
  3854. $table_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION);
  3855. if (!empty($params['c_id']) && !empty($params['user']) && !empty($params['survey_code'])) {
  3856. return Database::insert($table_survey_invitation, $params);
  3857. }
  3858. return false;
  3859. }
  3860. /**
  3861. * Send the invitation by mail.
  3862. *
  3863. * @param int invitedUser - the userId (course user) or emailaddress of additional user
  3864. * $param string $invitation_code - the unique invitation code for the URL
  3865. * @return void
  3866. */
  3867. static function send_invitation_mail($invitedUser, $invitation_code, $invitation_title, $invitation_text)
  3868. {
  3869. $_user = api_get_user_info();
  3870. $_course = api_get_course_info();
  3871. // Replacing the **link** part with a valid link for the user
  3872. $survey_link = api_get_path(WEB_CODE_PATH).'survey/fillsurvey.php?course='.$_course['code'].'&invitationcode='.$invitation_code;
  3873. $text_link = '<a href="'.$survey_link.'">'.get_lang('ClickHereToAnswerTheSurvey')."</a><br />\r\n<br />\r\n".get_lang('OrCopyPasteTheFollowingUrl')." <br />\r\n ".$survey_link;
  3874. $replace_count = 0;
  3875. $full_invitation_text = api_str_ireplace('**link**', $text_link ,$invitation_text, $replace_count);
  3876. if ($replace_count < 1) {
  3877. $full_invitation_text = $full_invitation_text."<br />\r\n<br />\r\n".$text_link;
  3878. }
  3879. // Sending the mail
  3880. $sender_name = api_get_person_name($_user['firstName'], $_user['lastName'], null, PERSON_NAME_EMAIL_ADDRESS);
  3881. $sender_email = $_user['mail'];
  3882. $sender_user_id = api_get_user_id();
  3883. $replyto = array();
  3884. if (api_get_setting('survey_email_sender_noreply') == 'noreply') {
  3885. $noreply = api_get_setting('noreply_email_address');
  3886. if (!empty($noreply)) {
  3887. $replyto['Reply-to'] = $noreply;
  3888. $sender_name = $noreply;
  3889. $sender_email = $noreply;
  3890. $sender_user_id = null;
  3891. }
  3892. }
  3893. // Optionally: finding the e-mail of the course user
  3894. if (is_numeric($invitedUser)) {
  3895. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  3896. $sql = "SELECT firstname, lastname, email FROM $table_user
  3897. WHERE user_id='".Database::escape_string($invitedUser)."'";
  3898. $result = Database::query($sql);
  3899. $row = Database::fetch_array($result);
  3900. $recipient_email = $row['email'];
  3901. $recipient_name = api_get_person_name($row['firstname'], $row['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  3902. MessageManager::send_message(
  3903. $invitedUser,
  3904. $invitation_title,
  3905. $full_invitation_text,
  3906. null,
  3907. null,
  3908. null,
  3909. null,
  3910. null,
  3911. null,
  3912. $sender_user_id
  3913. );
  3914. } else {
  3915. /** @todo check if the address is a valid email */
  3916. $recipient_email = $invitedUser;
  3917. @api_mail_html(
  3918. $recipient_name,
  3919. $recipient_email,
  3920. $invitation_title,
  3921. $full_invitation_text,
  3922. $sender_name,
  3923. $sender_email,
  3924. $replyto
  3925. );
  3926. }
  3927. }
  3928. /**
  3929. * This function recalculates the number of users who have been invited and updates the survey table with this value.
  3930. *
  3931. * @param string Survey code
  3932. * @return void
  3933. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3934. * @version January 2007
  3935. */
  3936. static function update_count_invited($survey_code)
  3937. {
  3938. $course_id = api_get_course_int_id();
  3939. // Database table definition
  3940. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  3941. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  3942. // Counting the number of people that are invited
  3943. $sql = "SELECT count(user) as total FROM $table_survey_invitation
  3944. WHERE c_id = $course_id AND survey_code = '".Database::escape_string($survey_code)."'";
  3945. $result = Database::query($sql);
  3946. $row = Database::fetch_array($result);
  3947. $total_invited = $row['total'];
  3948. // Updating the field in the survey table
  3949. $sql = "UPDATE $table_survey
  3950. SET invited = '".Database::escape_string($total_invited)."'
  3951. WHERE c_id = $course_id AND code = '".Database::escape_string($survey_code)."'";
  3952. Database::query($sql);
  3953. }
  3954. /**
  3955. * This function gets all the invited users for a given survey code.
  3956. *
  3957. * @param string Survey code
  3958. * @param string optional - course database
  3959. * @return array Array containing the course users and additional users (non course users)
  3960. *
  3961. * @todo consider making $defaults['additional_users'] also an array
  3962. *
  3963. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3964. * @author Julio Montoya, adding c_id fixes - Dec 2012
  3965. * @version January 2007
  3966. */
  3967. static function get_invited_users($survey_code, $course_code = '', $session_id = 0)
  3968. {
  3969. if (!empty($course_code)) {
  3970. $course_info = api_get_course_info($course_code);
  3971. $course_id = $course_info['real_id'];
  3972. } else {
  3973. $course_id = api_get_course_int_id();
  3974. }
  3975. if (empty($session_id)) {
  3976. $session_id = api_get_session_id();
  3977. }
  3978. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  3979. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  3980. // Selecting all the invitations of this survey AND the additional emailaddresses (the left join)
  3981. $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname' : ' ORDER BY lastname, firstname';
  3982. $sql = "SELECT user
  3983. FROM $table_survey_invitation as table_invitation
  3984. WHERE
  3985. table_invitation.c_id = $course_id AND
  3986. survey_code='".Database::escape_string($survey_code)."' AND
  3987. session_id = $session_id
  3988. ";
  3989. $defaults = array();
  3990. $defaults['course_users'] = array();
  3991. $defaults['additional_users'] = array();
  3992. $result = Database::query($sql);
  3993. while ($row = Database::fetch_array($result)) {
  3994. if (is_numeric($row['user'])) {
  3995. $defaults['course_users'][] = $row['user'];
  3996. } else {
  3997. if (!empty($row['user'])) {
  3998. $defaults['additional_users'][] = $row['user'];
  3999. }
  4000. }
  4001. }
  4002. if (!empty($defaults['course_users'])) {
  4003. $user_ids = implode("','", $defaults['course_users']);
  4004. $sql = "SELECT user_id FROM $table_user WHERE user_id IN ('$user_ids') $order_clause";
  4005. $result = Database::query($sql);
  4006. $fixed_users = array();
  4007. while ($row = Database::fetch_array($result)) {
  4008. $fixed_users[] = $row['user_id'];
  4009. }
  4010. $defaults['course_users'] = $fixed_users;
  4011. }
  4012. if (!empty($defaults['additional_users'])) {
  4013. $defaults['additional_users'] = implode(';', $defaults['additional_users']);
  4014. }
  4015. //error_log(print_r($defaults, 1));
  4016. return $defaults;
  4017. }
  4018. /**
  4019. * Get all the invitations
  4020. *
  4021. * @param string Survey code
  4022. * @return array Database rows matching the survey code
  4023. *
  4024. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4025. * @version September 2007
  4026. */
  4027. static function get_invitations($survey_code)
  4028. {
  4029. $course_id = api_get_course_int_id();
  4030. // Database table definition
  4031. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  4032. $sql = "SELECT * FROM $table_survey_invitation
  4033. WHERE
  4034. c_id = $course_id AND
  4035. survey_code = '".Database::escape_string($survey_code)."'";
  4036. $result = Database::query($sql);
  4037. $return = array();
  4038. while ($row = Database::fetch_array($result)) {
  4039. $return[$row['user']] = $row;
  4040. }
  4041. return $return;
  4042. }
  4043. /**
  4044. * This function displays the form for searching a survey
  4045. *
  4046. * @return void (direct output)
  4047. *
  4048. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4049. * @version January 2007
  4050. *
  4051. * @todo use quickforms
  4052. * @todo consider moving this to surveymanager.inc.lib.php
  4053. */
  4054. static function display_survey_search_form()
  4055. {
  4056. echo '<form class="form-horizontal" method="get" action="'.api_get_path(WEB_CODE_PATH).'survey/survey_list.php?search=advanced">';
  4057. echo '<legend>'.get_lang('SearchASurvey').'</legend>';
  4058. echo ' <div class="control-group">
  4059. <label class="control-label">
  4060. '.get_lang('Title').'
  4061. </label>
  4062. <div class="controls">
  4063. <input type="text" id="search_title" name="keyword_title"/>
  4064. </div>
  4065. </div>';
  4066. echo ' <div class="control-group">
  4067. <label class="control-label">
  4068. '.get_lang('Code').'
  4069. </label>
  4070. <div class="controls">
  4071. <input type="text" name="keyword_code"/>
  4072. </div>
  4073. </div>';
  4074. echo ' <div class="control-group">
  4075. <label class="control-label">
  4076. '.get_lang('Language').'
  4077. </label>
  4078. <div class="controls">';
  4079. echo ' <select name="keyword_language"><option value="%">'.get_lang('All').'</option>';
  4080. $languages = api_get_languages();
  4081. foreach ($languages['name'] as $index => & $name) {
  4082. echo '<option value="'.$languages['folder'][$index].'">'.$name.'</option>';
  4083. }
  4084. echo ' </select>';
  4085. echo ' </div>
  4086. </div>';
  4087. echo '<input type="hidden" name="cidReq" value="'.api_get_course_id().'"/>';
  4088. echo ' <div class="control-group">
  4089. <div class="controls">
  4090. <button class="search" type="submit" name="do_search">'.get_lang('Search').'</button>
  4091. </div>
  4092. </div>';
  4093. echo '</form>';
  4094. echo '<div style="clear: both;margin-bottom: 10px;"></div>';
  4095. }
  4096. /**
  4097. * Show table only visible by DRH users
  4098. */
  4099. public static function displaySurveyListForDrh()
  4100. {
  4101. $parameters = array();
  4102. $parameters['cidReq'] = api_get_course_id();
  4103. // Create a sortable table with survey-data
  4104. $table = new SortableTable('surveys', 'get_number_of_surveys', 'get_survey_data_drh', 2);
  4105. $table->set_additional_parameters($parameters);
  4106. $table->set_header(0, '', false);
  4107. $table->set_header(1, get_lang('SurveyName'));
  4108. $table->set_header(2, get_lang('SurveyCode'));
  4109. $table->set_header(3, get_lang('NumberOfQuestions'));
  4110. $table->set_header(4, get_lang('Author'));
  4111. $table->set_header(5, get_lang('AvailableFrom'));
  4112. $table->set_header(6, get_lang('AvailableUntil'));
  4113. $table->set_header(7, get_lang('Invite'));
  4114. $table->set_header(8, get_lang('Anonymous'));
  4115. $table->set_header(9, get_lang('Modify'), false, 'width="150"');
  4116. $table->set_column_filter(8, 'anonymous_filter');
  4117. $table->set_column_filter(9, 'modify_filter_drh');
  4118. $table->display();
  4119. }
  4120. /**
  4121. * This function displays the sortable table with all the surveys
  4122. *
  4123. * @return void (direct output)
  4124. *
  4125. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4126. * @version January 2007
  4127. */
  4128. static function display_survey_list()
  4129. {
  4130. $parameters = array();
  4131. $parameters['cidReq'] = api_get_course_id();
  4132. if (isset($_GET['do_search']) && $_GET['do_search']) {
  4133. $message = get_lang('DisplaySearchResults').'<br />';
  4134. $message .= '<a href="'.api_get_self().'?'.api_get_cidreq().'">'.get_lang('DisplayAll').'</a>';
  4135. Display::display_normal_message($message, false);
  4136. }
  4137. // Create a sortable table with survey-data
  4138. $table = new SortableTable('surveys', 'get_number_of_surveys', 'get_survey_data', 2);
  4139. $table->set_additional_parameters($parameters);
  4140. $table->set_header(0, '', false);
  4141. $table->set_header(1, get_lang('SurveyName'));
  4142. $table->set_header(2, get_lang('SurveyCode'));
  4143. $table->set_header(3, get_lang('NumberOfQuestions'));
  4144. $table->set_header(4, get_lang('Author'));
  4145. //$table->set_header(5, get_lang('Language'));
  4146. //$table->set_header(6, get_lang('Shared'));
  4147. $table->set_header(5, get_lang('AvailableFrom'));
  4148. $table->set_header(6, get_lang('AvailableUntil'));
  4149. $table->set_header(7, get_lang('Invite'));
  4150. $table->set_header(8, get_lang('Anonymous'));
  4151. $table->set_header(9, get_lang('Modify'), false, 'width="150"');
  4152. $table->set_column_filter(8, 'anonymous_filter');
  4153. $table->set_column_filter(9, 'modify_filter');
  4154. $table->set_form_actions(array('delete' => get_lang('DeleteSurvey')));
  4155. $table->display();
  4156. }
  4157. function display_survey_list_for_coach()
  4158. {
  4159. $parameters = array();
  4160. $parameters['cidReq']=api_get_course_id();
  4161. if (isset($_GET['do_search'])) {
  4162. $message = get_lang('DisplaySearchResults').'<br />';
  4163. $message .= '<a href="'.api_get_self().'?'.api_get_cidreq().'">'.get_lang('DisplayAll').'</a>';
  4164. Display::display_normal_message($message, false);
  4165. }
  4166. // Create a sortable table with survey-data
  4167. $table = new SortableTable('surveys_coach', 'get_number_of_surveys_for_coach', 'get_survey_data_for_coach', 2);
  4168. $table->set_additional_parameters($parameters);
  4169. $table->set_header(0, '', false);
  4170. $table->set_header(1, get_lang('SurveyName'));
  4171. $table->set_header(2, get_lang('SurveyCode'));
  4172. $table->set_header(3, get_lang('NumberOfQuestions'));
  4173. $table->set_header(4, get_lang('Author'));
  4174. //$table->set_header(5, get_lang('Language'));
  4175. //$table->set_header(6, get_lang('Shared'));
  4176. $table->set_header(5, get_lang('AvailableFrom'));
  4177. $table->set_header(6, get_lang('AvailableUntil'));
  4178. $table->set_header(7, get_lang('Invite'));
  4179. $table->set_header(8, get_lang('Anonymous'));
  4180. $table->set_header(9, get_lang('Modify'), false, 'width="130"');
  4181. $table->set_column_filter(8, 'anonymous_filter');
  4182. $table->set_column_filter(9, 'modify_filter_for_coach');
  4183. $table->display();
  4184. }
  4185. /**
  4186. * This function changes the modify column of the sortable table
  4187. *
  4188. * @param integer $survey_id the id of the survey
  4189. * @param bool $drh
  4190. * @return string html code that are the actions that can be performed on any survey
  4191. *
  4192. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4193. * @version January 2007
  4194. */
  4195. static function modify_filter($survey_id, $drh = false)
  4196. {
  4197. $survey_id = Security::remove_XSS($survey_id);
  4198. $return = '';
  4199. if ($drh) {
  4200. return '<a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.
  4201. Display::return_icon('stats.png', get_lang('Reporting'),'',ICON_SIZE_SMALL).'</a>';
  4202. }
  4203. // Coach can see that only if the survey is in his session
  4204. if (api_is_allowed_to_edit() ||
  4205. api_is_element_in_the_session(TOOL_SURVEY, $survey_id)
  4206. ) {
  4207. $return .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/create_new_survey.php?'.api_get_cidreq().'&amp;action=edit&amp;survey_id='.$survey_id.'">'.Display::return_icon('edit.png', get_lang('Edit'),'',ICON_SIZE_SMALL).'</a>';
  4208. if (survey_manager::survey_generation_hash_available()) {
  4209. $return .= Display::url(
  4210. Display::return_icon('new_link.png', get_lang('GenerateSurveyAccessLink'),'',ICON_SIZE_SMALL),
  4211. api_get_path(WEB_CODE_PATH).'survey/generate_link.php?survey_id='.$survey_id.'&'.api_get_cidreq()
  4212. );
  4213. }
  4214. $return .= Display::url(
  4215. Display::return_icon('copy.png', get_lang('DuplicateSurvey'), '', ICON_SIZE_SMALL),
  4216. 'survey_list.php?action=copy_survey&survey_id='.$survey_id.'&'.api_get_cidreq()
  4217. );
  4218. $return .= ' <a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq().'&amp;action=empty&amp;survey_id='.$survey_id.'" onclick="javascript: if(!confirm(\''.addslashes(api_htmlentities(get_lang("EmptySurvey").'?')).'\')) return false;">'.
  4219. Display::return_icon('clean.png', get_lang('EmptySurvey'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
  4220. }
  4221. $return .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/preview.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.
  4222. Display::return_icon('preview_view.png', get_lang('Preview'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
  4223. $return .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_invite.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.
  4224. Display::return_icon('mail_send.png', get_lang('Publish'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
  4225. $return .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.
  4226. Display::return_icon('stats.png', get_lang('Reporting'),'',ICON_SIZE_SMALL).'</a>';
  4227. if (api_is_allowed_to_edit() ||
  4228. api_is_element_in_the_session(TOOL_SURVEY, $survey_id)
  4229. ) {
  4230. $return .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq().'&amp;action=delete&amp;survey_id='.$survey_id.'" onclick="javascript: if(!confirm(\''.addslashes(api_htmlentities(get_lang("DeleteSurvey").'?', ENT_QUOTES)).'\')) return false;">'.
  4231. Display::return_icon('delete.png', get_lang('Delete'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
  4232. }
  4233. return $return;
  4234. }
  4235. static function modify_filter_for_coach($survey_id)
  4236. {
  4237. $survey_id = Security::remove_XSS($survey_id);
  4238. //$return = '<a href="create_new_survey.php?'.api_get_cidreq().'&amp;action=edit&amp;survey_id='.$survey_id.'">'.Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
  4239. //$return .= '<a href="survey_list.php?'.api_get_cidreq().'&amp;action=delete&amp;survey_id='.$survey_id.'" onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang("DeleteSurvey").'?', ENT_QUOTES)).'\')) return false;">'.Display::return_icon('delete.gif', get_lang('Delete')).'</a>';
  4240. //$return .= '<a href="create_survey_in_another_language.php?id_survey='.$survey_id.'">'.Display::return_icon('copy.gif', get_lang('Copy')).'</a>';
  4241. //$return .= '<a href="survey.php?survey_id='.$survey_id.'">'.Display::return_icon('add.gif', get_lang('Add')).'</a>';
  4242. $return = '<a href="'.api_get_path(WEB_CODE_PATH).'survey/preview.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.Display::return_icon('preview_view.png', get_lang('Preview'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
  4243. $return .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_invite.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.Display::return_icon('mail_send.png', get_lang('Publish'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
  4244. $return .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq().'&amp;action=empty&amp;survey_id='.$survey_id.'" onclick="javascript: if(!confirm(\''.addslashes(api_htmlentities(get_lang("EmptySurvey").'?', ENT_QUOTES)).'\')) return false;">'.Display::return_icon('clean.png', get_lang('EmptySurvey'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
  4245. //$return .= '<a href="reporting.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.Display::return_icon('statistics.gif', get_lang('Reporting')).'</a>';
  4246. return $return;
  4247. }
  4248. /**
  4249. * Returns "yes" when given parameter is one, "no" for any other value
  4250. * @param integer Whether anonymous or not
  4251. * @return string "Yes" or "No" in the current language
  4252. */
  4253. static function anonymous_filter($anonymous)
  4254. {
  4255. if ($anonymous == 1) {
  4256. return get_lang('Yes');
  4257. } else {
  4258. return get_lang('No');
  4259. }
  4260. }
  4261. /**
  4262. * This function handles the search restriction for the SQL statements
  4263. *
  4264. * @return string Part of a SQL statement or false on error
  4265. *
  4266. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4267. * @version January 2007
  4268. */
  4269. static function survey_search_restriction()
  4270. {
  4271. if (isset($_GET['do_search'])) {
  4272. if ($_GET['keyword_title'] != '') {
  4273. $search_term[] = 'title like "%" \''.Database::escape_string($_GET['keyword_title']).'\' "%"';
  4274. }
  4275. if ($_GET['keyword_code'] != '') {
  4276. $search_term[] = 'code =\''.Database::escape_string($_GET['keyword_code']).'\'';
  4277. }
  4278. if ($_GET['keyword_language'] != '%') {
  4279. $search_term[] = 'lang =\''.Database::escape_string($_GET['keyword_language']).'\'';
  4280. }
  4281. $my_search_term = ($search_term == null) ? array() : $search_term;
  4282. $search_restriction = implode(' AND ', $my_search_term);
  4283. return $search_restriction;
  4284. } else {
  4285. return false;
  4286. }
  4287. }
  4288. /**
  4289. * This function calculates the total number of surveys
  4290. *
  4291. * @return integer Total number of surveys
  4292. *
  4293. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4294. * @version January 2007
  4295. */
  4296. static function get_number_of_surveys()
  4297. {
  4298. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  4299. $course_id = api_get_course_int_id();
  4300. $search_restriction = SurveyUtil::survey_search_restriction();
  4301. if ($search_restriction) {
  4302. $search_restriction = 'WHERE c_id = '.$course_id.' AND '.$search_restriction;
  4303. } else {
  4304. $search_restriction = "WHERE c_id = $course_id";
  4305. }
  4306. $sql = "SELECT count(survey_id) AS total_number_of_items
  4307. FROM ".$table_survey.' '.$search_restriction;
  4308. $res = Database::query($sql);
  4309. $obj = Database::fetch_object($res);
  4310. return $obj->total_number_of_items;
  4311. }
  4312. static function get_number_of_surveys_for_coach()
  4313. {
  4314. // Ugly fix
  4315. require_once api_get_path(LIBRARY_PATH).'surveymanager.lib.php';
  4316. $survey_tree = new SurveyTree();
  4317. return count($survey_tree->get_last_children_from_branch($survey_tree->surveylist));
  4318. }
  4319. /**
  4320. * This function gets all the survey data that is to be displayed in the sortable table
  4321. *
  4322. * @param int $from
  4323. * @param int $number_of_items
  4324. * @param int $column
  4325. * @param string $direction
  4326. * @param bool $isDrh
  4327. * @return unknown
  4328. *
  4329. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4330. * @author Julio Montoya <gugli100@gmail.com>, Beeznest - Adding intvals
  4331. * @version January 2007
  4332. */
  4333. static function get_survey_data($from, $number_of_items, $column, $direction, $isDrh = false)
  4334. {
  4335. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  4336. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  4337. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  4338. $_user = api_get_user_info();
  4339. // Searching
  4340. $search_restriction = SurveyUtil::survey_search_restriction();
  4341. if ($search_restriction) {
  4342. $search_restriction = ' AND '.$search_restriction;
  4343. }
  4344. $from = intval($from);
  4345. $number_of_items = intval($number_of_items);
  4346. $column = intval($column);
  4347. if (!in_array(strtolower($direction), array('asc', 'desc'))) {
  4348. $direction = 'asc';
  4349. }
  4350. // Condition for the session
  4351. $session_id = api_get_session_id();
  4352. $condition_session = api_get_session_condition($session_id);
  4353. $course_id = api_get_course_int_id();
  4354. $sql = "SELECT
  4355. survey.survey_id AS col0,
  4356. survey.title AS col1,
  4357. survey.code AS col2,
  4358. count(survey_question.question_id) AS col3,
  4359. ".(api_is_western_name_order() ? "CONCAT(user.firstname, ' ', user.lastname)" : "CONCAT(user.lastname, ' ', user.firstname)")." AS col4,
  4360. survey.avail_from AS col5,
  4361. survey.avail_till AS col6,
  4362. survey.invited AS col7,
  4363. survey.anonymous AS col8,
  4364. survey.survey_id AS col9,
  4365. survey.session_id AS session_id,
  4366. survey.answered,
  4367. survey.invited
  4368. FROM $table_survey survey
  4369. LEFT JOIN $table_survey_question survey_question
  4370. ON (survey.survey_id = survey_question.survey_id AND survey_question.c_id = $course_id)
  4371. LEFT JOIN $table_user user
  4372. ON (survey.author = user.user_id)
  4373. WHERE survey.c_id = $course_id
  4374. $search_restriction
  4375. $condition_session ";
  4376. $sql .= " GROUP BY survey.survey_id";
  4377. $sql .= " ORDER BY col$column $direction ";
  4378. $sql .= " LIMIT $from,$number_of_items";
  4379. $res = Database::query($sql);
  4380. $surveys = array();
  4381. $array = array();
  4382. while ($survey = Database::fetch_array($res)) {
  4383. $array[0] = $survey[0];
  4384. $array[1] = Display::url(
  4385. $survey[1],
  4386. api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey[0].'&'.api_get_cidreq()
  4387. );
  4388. // Validation when belonging to a session
  4389. $session_img = api_get_session_image($survey['session_id'], $_user['status']);
  4390. $array[2] = $survey[2] . $session_img;
  4391. $array[3] = $survey[3];
  4392. $array[4] = $survey[4];
  4393. $array[5] = $survey[5];
  4394. $array[6] = $survey[6];
  4395. $array[7] =
  4396. Display::url(
  4397. $survey['answered'],
  4398. api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=answered&survey_id='.$survey[0].'&'.api_get_cidreq()
  4399. ).' / '.
  4400. Display::url(
  4401. $survey['invited'],
  4402. api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=invited&survey_id='.$survey[0].'&'.api_get_cidreq()
  4403. );
  4404. $array[8] = $survey[8];
  4405. $array[9] = $survey[9];
  4406. if ($isDrh) {
  4407. $array[1] = $survey[1];
  4408. $array[7] = strip_tags($array[7]);
  4409. }
  4410. $surveys[] = $array;
  4411. }
  4412. return $surveys;
  4413. }
  4414. static function get_survey_data_for_coach($from, $number_of_items, $column, $direction)
  4415. {
  4416. require_once api_get_path(LIBRARY_PATH).'surveymanager.lib.php';
  4417. $survey_tree = new SurveyTree();
  4418. $last_version_surveys = $survey_tree->get_last_children_from_branch($survey_tree->surveylist);
  4419. $list = array();
  4420. foreach ($last_version_surveys as & $survey) {
  4421. $list[]=$survey['id'];
  4422. }
  4423. if (count($list) > 0) {
  4424. $list_condition = " AND survey.survey_id IN (".implode(',',$list).") ";
  4425. } else {
  4426. $list_condition = '';
  4427. }
  4428. $from = intval($from);
  4429. $number_of_items = intval($number_of_items);
  4430. $column = intval($column);
  4431. if (!in_array(strtolower($direction), array('asc', 'desc'))) {
  4432. $direction = 'asc';
  4433. }
  4434. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  4435. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  4436. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  4437. $course_id = api_get_course_int_id();
  4438. //IF(is_shared<>0,'V','-') AS col6,
  4439. $sql = "SELECT ".
  4440. "survey.survey_id AS col0, ".
  4441. "survey.title AS col1, ".
  4442. "survey.code AS col2, ".
  4443. "count(survey_question.question_id) AS col3, ".
  4444. (api_is_western_name_order() ? "CONCAT(user.firstname, ' ', user.lastname)" : "CONCAT(user.lastname, ' ', user.firstname)")." AS col4, ".
  4445. "survey.avail_from AS col5, ".
  4446. "survey.avail_till AS col6, ".
  4447. "CONCAT('<a href=\"survey_invitation.php?view=answered&amp;survey_id=',survey.survey_id,'\">',survey.answered,'</a> / <a href=\"survey_invitation.php?view=invited&amp;survey_id=',survey.survey_id,'\">',survey.invited, '</a>') AS col7, ".
  4448. "survey.anonymous AS col8, ".
  4449. "survey.survey_id AS col9 ".
  4450. "FROM $table_survey survey ".
  4451. "LEFT JOIN $table_survey_question survey_question ON (survey.survey_id = survey_question.survey_id AND survey.c_id = survey_question.c_id) ".
  4452. ", $table_user user
  4453. WHERE survey.author = user.user_id AND survey.c_id = $course_id $list_condition ";
  4454. $sql .= " GROUP BY survey.survey_id";
  4455. $sql .= " ORDER BY col$column $direction ";
  4456. $sql .= " LIMIT $from,$number_of_items";
  4457. $res = Database::query($sql);
  4458. $surveys = array();
  4459. while ($survey = Database::fetch_array($res)) {
  4460. $surveys[] = $survey;
  4461. }
  4462. return $surveys;
  4463. }
  4464. /**
  4465. * Display all the active surveys for the given course user
  4466. *
  4467. * @param int $user_id
  4468. *
  4469. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4470. * @version April 2007
  4471. */
  4472. public static function survey_list_user($user_id)
  4473. {
  4474. $_course = api_get_course_info();
  4475. $course_id = api_get_course_int_id();
  4476. $user_id = intval($user_id);
  4477. $sessionId = api_get_session_id();
  4478. // Database table definitions
  4479. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  4480. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  4481. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  4482. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  4483. $all_question_id = array();
  4484. $sql = 'SELECT question_id FROM '.$table_survey_question." WHERE c_id = $course_id";
  4485. $result = Database::query($sql);
  4486. while($row = Database::fetch_array($result, 'ASSOC')) {
  4487. $all_question_id[] = $row;
  4488. }
  4489. $count = 0;
  4490. for ($i = 0; $i < count($all_question_id); $i++) {
  4491. $sql = 'SELECT COUNT(*) as count
  4492. FROM '.$table_survey_answer.'
  4493. WHERE
  4494. c_id = '.$course_id.' AND
  4495. question_id='.intval($all_question_id[$i]['question_id']).' AND
  4496. user = '.$user_id;
  4497. $result = Database::query($sql);
  4498. while ($row = Database::fetch_array($result, 'ASSOC')) {
  4499. if ($row['count'] == 0) {
  4500. $count++;
  4501. break;
  4502. }
  4503. }
  4504. if ($count > 0) {
  4505. $link_add = true;
  4506. break;
  4507. }
  4508. }
  4509. echo '<table class="data_table">';
  4510. echo '<tr>';
  4511. echo ' <th>'.get_lang('SurveyName').'</th>';
  4512. echo ' <th>'.get_lang('Anonymous').'</th>';
  4513. echo '</tr>';
  4514. $sql = "SELECT * FROM $table_survey survey, $table_survey_invitation survey_invitation
  4515. WHERE
  4516. survey_invitation.user = $user_id AND
  4517. survey.code = survey_invitation.survey_code AND
  4518. survey.avail_from <= '".date('Y-m-d H:i:s')."' AND
  4519. survey.avail_till >= '".date('Y-m-d H:i:s')."' AND
  4520. survey.c_id = $course_id AND
  4521. survey.session_id = $sessionId AND
  4522. survey_invitation.c_id = $course_id
  4523. ";
  4524. $result = Database::query($sql);
  4525. $counter = 0;
  4526. while ($row = Database::fetch_array($result, 'ASSOC')) {
  4527. // Get the user into survey answer table (user or anonymus)
  4528. $sql = "SELECT user FROM $table_survey_answer
  4529. WHERE c_id = $course_id AND survey_id = (
  4530. SELECT survey_id from $table_survey
  4531. WHERE code ='".Database::escape_string($row['code'])." AND c_id = $course_id'
  4532. )
  4533. ";
  4534. $result_answer = Database::query($sql);
  4535. $row_answer = Database::fetch_array($result_answer,'ASSOC');
  4536. echo '<tr>';
  4537. if ($row['answered'] == 0) {
  4538. echo '<td><a href="'.api_get_path(WEB_CODE_PATH).'survey/fillsurvey.php?course='.$_course['sysCode'].'&amp;invitationcode='.$row['invitation_code'].'&amp;cidReq='.$_course['sysCode'].'">'.$row['title'].'</a></td>';
  4539. } else {
  4540. echo '<td>'.$row['title'].'</td>';
  4541. }
  4542. echo '<td>';
  4543. echo ($row['anonymous'] == 1) ? get_lang('Yes') : get_lang('No');
  4544. echo '</td>';
  4545. echo '</tr>';
  4546. if ($row['anonymous'] == 1) {
  4547. $current_user_id = $_SESSION['surveyuser'];
  4548. } else {
  4549. $current_user_id = api_get_user_id();
  4550. }
  4551. $link_available = self::show_link_available(api_get_user_id(),$row['code'],$current_user_id);
  4552. //todo check this link
  4553. if ($link_add === true && $link_available === true) {
  4554. //echo '<tr><td><a href="fillsurvey.php?user_id='.api_get_user_id().'&amp;course='.$_course['sysCode'].'&amp;invitationcode='.$row['invitation_code'].'&amp;cidReq='.$_course['sysCode'].'">'.get_lang('CompleteTheSurveysQuestions').'</a></td><td></td></tr>';
  4555. }
  4556. }
  4557. echo '</table>';
  4558. }
  4559. /**
  4560. * Creates a multi array with the user fields that we can show. We look the visibility with the api_get_setting function
  4561. * The username is always NOT able to change it.
  4562. * @author Julio Montoya Armas <gugli100@gmail.com>, Chamilo: Personality Test modification
  4563. * @return array[value_name][name]
  4564. * array[value_name][visibilty]
  4565. */
  4566. static function make_field_list()
  4567. {
  4568. // LAST NAME and FIRST NAME
  4569. $field_list_array = array();
  4570. $field_list_array['lastname']['name'] = get_lang('Lastname');
  4571. $field_list_array['firstname']['name'] = get_lang('Firstname');
  4572. if (api_get_setting('profile', 'name') != 'true') {
  4573. $field_list_array['firstname']['visibility'] = 0;
  4574. $field_list_array['lastname']['visibility'] = 0;
  4575. } else {
  4576. $field_list_array['firstname']['visibility'] = 1;
  4577. $field_list_array['lastname']['visibility'] = 1;
  4578. }
  4579. $field_list_array['username']['name'] = get_lang('Username');
  4580. $field_list_array['username']['visibility'] = 0;
  4581. // OFFICIAL CODE
  4582. $field_list_array['official_code']['name'] = get_lang('OfficialCode');
  4583. if (api_get_setting('profile', 'officialcode') != 'true') {
  4584. $field_list_array['official_code']['visibility'] = 1;
  4585. } else {
  4586. $field_list_array['official_code']['visibility'] = 0;
  4587. }
  4588. // EMAIL
  4589. $field_list_array['email']['name'] = get_lang('Email');
  4590. if (api_get_setting('profile', 'email') != 'true') {
  4591. $field_list_array['email']['visibility'] = 1;
  4592. } else {
  4593. $field_list_array['email']['visibility'] = 0;
  4594. }
  4595. // PHONE
  4596. $field_list_array['phone']['name'] = get_lang('Phone');
  4597. if (api_get_setting('profile', 'phone') != 'true') {
  4598. $field_list_array['phone']['visibility'] = 0;
  4599. } else {
  4600. $field_list_array['phone']['visibility'] = 1;
  4601. }
  4602. // LANGUAGE
  4603. $field_list_array['language']['name'] = get_lang('Language');
  4604. if (api_get_setting('profile', 'language') != 'true') {
  4605. $field_list_array['language']['visibility'] = 0;
  4606. } else {
  4607. $field_list_array['language']['visibility'] = 1;
  4608. }
  4609. // EXTRA FIELDS
  4610. $extra = UserManager::get_extra_fields(0, 50, 5, 'ASC');
  4611. foreach ($extra as $id => $field_details) {
  4612. if ($field_details[6] == 0) {
  4613. continue;
  4614. }
  4615. switch ($field_details[2]) {
  4616. case UserManager::USER_FIELD_TYPE_TEXT:
  4617. $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3];
  4618. if ($field_details[7] == 0) {
  4619. $field_list_array['extra_'.$field_details[1]]['visibility'] = 0;
  4620. } else {
  4621. $field_list_array['extra_'.$field_details[1]]['visibility'] = 1;
  4622. }
  4623. break;
  4624. case UserManager::USER_FIELD_TYPE_TEXTAREA:
  4625. $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3];
  4626. if ($field_details[7] == 0) {
  4627. $field_list_array['extra_'.$field_details[1]]['visibility'] = 0;
  4628. } else {
  4629. $field_list_array['extra_'.$field_details[1]]['visibility'] = 1;
  4630. }
  4631. break;
  4632. case UserManager::USER_FIELD_TYPE_RADIO:
  4633. $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3];
  4634. if ($field_details[7] == 0) {
  4635. $field_list_array['extra_'.$field_details[1]]['visibility'] = 0;
  4636. } else {
  4637. $field_list_array['extra_'.$field_details[1]]['visibility'] = 1;
  4638. }
  4639. break;
  4640. case UserManager::USER_FIELD_TYPE_SELECT:
  4641. $get_lang_variables = false;
  4642. if (in_array($field_details[1], array('mail_notify_message', 'mail_notify_invitation', 'mail_notify_group_message'))) {
  4643. $get_lang_variables = true;
  4644. }
  4645. if ($get_lang_variables) {
  4646. $field_list_array['extra_'.$field_details[1]]['name'] = get_lang($field_details[3]);
  4647. } else {
  4648. $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3];
  4649. }
  4650. if ($field_details[7] == 0) {
  4651. $field_list_array['extra_'.$field_details[1]]['visibility'] = 0;
  4652. } else {
  4653. $field_list_array['extra_'.$field_details[1]]['visibility'] = 1;
  4654. }
  4655. break;
  4656. case UserManager::USER_FIELD_TYPE_SELECT_MULTIPLE:
  4657. $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3];
  4658. if ($field_details[7] == 0) {
  4659. $field_list_array['extra_'.$field_details[1]]['visibility'] = 0;
  4660. } else {
  4661. $field_list_array['extra_'.$field_details[1]]['visibility'] = 1;
  4662. }
  4663. break;
  4664. case UserManager::USER_FIELD_TYPE_DATE:
  4665. $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3];
  4666. if ($field_details[7] == 0) {
  4667. $field_list_array['extra_'.$field_details[1]]['visibility'] = 0;
  4668. } else {
  4669. $field_list_array['extra_'.$field_details[1]]['visibility'] = 1;
  4670. }
  4671. break;
  4672. case UserManager::USER_FIELD_TYPE_DATETIME:
  4673. $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3];
  4674. if ($field_details[7] == 0) {
  4675. $field_list_array['extra_'.$field_details[1]]['visibility'] = 0;
  4676. } else {
  4677. $field_list_array['extra_'.$field_details[1]]['visibility'] = 1;
  4678. }
  4679. break;
  4680. case UserManager::USER_FIELD_TYPE_DOUBLE_SELECT:
  4681. $field_list_array['extra_'.$field_details[1]]['name'] = $field_details[3];
  4682. if ($field_details[7] == 0) {
  4683. $field_list_array['extra_'.$field_details[1]]['visibility'] = 0;
  4684. } else {
  4685. $field_list_array['extra_'.$field_details[1]]['visibility']=1;
  4686. }
  4687. break;
  4688. case UserManager::USER_FIELD_TYPE_DIVIDER:
  4689. //$form->addElement('static',$field_details[1], '<br /><strong>'.$field_details[3].'</strong>');
  4690. break;
  4691. }
  4692. }
  4693. return $field_list_array;
  4694. }
  4695. /**
  4696. * @author Isaac Flores Paz <florespaz@bidsoftperu.com>
  4697. * @param int $user_id - User ID
  4698. * @param int $user_id_answer - User in survey answer table (user id or anonymus)
  4699. * @return boolean
  4700. */
  4701. static function show_link_available($user_id, $survey_code, $user_answer)
  4702. {
  4703. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  4704. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  4705. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  4706. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  4707. $survey_code = Database::escape_string($survey_code);
  4708. $user_id = intval($user_id);
  4709. $user_answer = Database::escape_string($user_answer);
  4710. $course_id = api_get_course_int_id();
  4711. $sql = 'SELECT COUNT(*) as count FROM '.$table_survey_invitation.'
  4712. WHERE user='.$user_id.' AND survey_code="'.$survey_code.'" AND answered="1" AND c_id = '.$course_id.' ';
  4713. $sql2 = 'SELECT COUNT(*) as count FROM '.$table_survey.' s INNER JOIN '.$table_survey_question.' q ON s.survey_id=q.survey_id
  4714. WHERE s.code="'.$survey_code.'" AND q.type NOT IN("pagebreak","comment") AND s.c_id = '.$course_id.' AND q.c_id = '.$course_id.' ';
  4715. $sql3 = 'SELECT COUNT(DISTINCT question_id) as count FROM '.$table_survey_answer.'
  4716. WHERE survey_id=(SELECT survey_id FROM '.$table_survey.'
  4717. WHERE code="'.$survey_code.'" AND c_id = '.$course_id.' ) AND user="'.$user_answer.'" AND c_id = '.$course_id.' ';
  4718. $result = Database::query($sql);
  4719. $result2 = Database::query($sql2);
  4720. $result3 = Database::query($sql3);
  4721. $row = Database::fetch_array($result, 'ASSOC');
  4722. $row2 = Database::fetch_array($result2, 'ASSOC');
  4723. $row3 = Database::fetch_array($result3, 'ASSOC');
  4724. if ($row['count'] == 1 && $row3['count'] != $row2['count']) {
  4725. return true;
  4726. } else {
  4727. return false;
  4728. }
  4729. }
  4730. /**
  4731. * Set a flag to the current survey as answered by the current user
  4732. * @param string $surveyCode The survey code
  4733. * @param int $courseId The course ID
  4734. */
  4735. public static function flagSurveyAsAnswered($surveyCode, $courseId)
  4736. {
  4737. $currenUserId = api_get_user_id();
  4738. $flag = sprintf("%s-%s-%d", $courseId, $surveyCode, $currenUserId);
  4739. if (!isset($_SESSION['filled_surveys'])) {
  4740. $_SESSION['filled_surveys'] = array();
  4741. }
  4742. $_SESSION['filled_surveys'][] = $flag;
  4743. }
  4744. /**
  4745. * Check whether a survey was answered by the current user
  4746. * @param string $surveyCode The survey code
  4747. * @param int $courseId The course ID
  4748. * @return boolean
  4749. */
  4750. public static function isSurveyAnsweredFlagged($surveyCode, $courseId)
  4751. {
  4752. $currenUserId = api_get_user_id();
  4753. $flagToCheck = sprintf("%s-%s-%d", $courseId, $surveyCode, $currenUserId);
  4754. if (!isset($_SESSION['filled_surveys'])) {
  4755. return false;
  4756. }
  4757. if (!is_array($_SESSION['filled_surveys'])) {
  4758. return false;
  4759. }
  4760. foreach ($_SESSION['filled_surveys'] as $flag) {
  4761. if ($flagToCheck != $flag) {
  4762. continue;
  4763. }
  4764. return true;
  4765. }
  4766. return false;
  4767. }
  4768. }