survey.lib.php 179 KB

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