survey.lib.php 198 KB

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