survey.lib.php 208 KB

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