survey.lib.php 176 KB

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