survey.lib.php 185 KB

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