survey.lib.php 186 KB

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