survey.lib.php 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos SPRL
  6. Copyright (c) 2006-2008 Patrick Cool
  7. For a full list of contributors, see "credits.txt".
  8. The full license can be read in "license.txt".
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. See the GNU General Public License for more details.
  14. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  15. Mail: info@dokeos.com
  16. ==============================================================================
  17. */
  18. /**
  19. * @package dokeos.survey
  20. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts (if not all) of the code
  21. * @version $Id: survey.lib.php 14943 2008-04-17 20:42:54Z juliomontoya $
  22. *
  23. * @todo move this file to inc/lib
  24. * @todo use consistent naming for the functions (save vs store for instance)
  25. */
  26. $config['survey']['debug'] = false;
  27. require_once(api_get_path(LIBRARY_PATH).'usermanager.lib.php');
  28. class survey_manager
  29. {
  30. /******************************************************************************************************
  31. SURVEY FUNCTIONS
  32. *****************************************************************************************************/
  33. /**
  34. * This function retrieves all the survey information
  35. *
  36. * @param integer $survey_id the id of the survey
  37. * @param boolean $shared this parameter determines if we have to get the information of a survey from the central (shared) database or from the
  38. * course database
  39. *
  40. * @return array
  41. *
  42. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  43. * @version February 2007
  44. *
  45. * @todo this is the same function as in create_new_survey.php
  46. */
  47. function get_survey($survey_id,$shared=0)
  48. {
  49. global $_course;
  50. // table definition
  51. $table_survey = Database :: get_course_table(TABLE_SURVEY, $_course['db_name']);
  52. if ($shared<>0)
  53. {
  54. $table_survey = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  55. }
  56. $sql = "SELECT * FROM $table_survey WHERE survey_id='".Database::escape_string($survey_id)."'";
  57. $result = api_sql_query($sql, __FILE__, __LINE__);
  58. $return = Database::fetch_array($result,'ASSOC');
  59. // we do this (temporarily) to have the array match the quickform elements immediately
  60. // idealiter the fields in the db match the quickform fields
  61. $return['survey_code'] = $return['code'];
  62. $return['survey_title'] = $return['title'];
  63. $return['survey_subtitle'] = $return['subtitle'];
  64. $return['survey_language'] = $return['lang'];
  65. $return['start_date'] = $return['avail_from'];
  66. $return['end_date'] = $return['avail_till'];
  67. $return['survey_share'] = $return['is_shared'];
  68. $return['survey_introduction'] = $return['intro'];
  69. $return['survey_thanks'] = $return['surveythanks'];
  70. return $return;
  71. }
  72. /**
  73. * This function stores a survey in the database.
  74. *
  75. * @param array $values
  76. * @return array $return the type of return message that has to be displayed and the message in it
  77. *
  78. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  79. * @version February 2007
  80. */
  81. function store_survey($values)
  82. {
  83. global $_user;
  84. // table defnitions
  85. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  86. /*if ($values['survey_share']['survey_share'] !== '0')
  87. {
  88. $shared_survey_id = survey_manager::store_shared_survey($values);
  89. }*/
  90. $shared_survey_id=0;
  91. if (!$values['survey_id'] OR !is_numeric($values['survey_id']))
  92. {
  93. if ($values['anonymous']=='')
  94. {
  95. $values['anonymous']=0;
  96. }
  97. $sql = "INSERT INTO $table_survey (code, title, subtitle, author, lang, avail_from, avail_till, is_shared, template, intro, surveythanks, creation_date, anonymous) VALUES (
  98. '".Database::escape_string($values['survey_code'])."',
  99. '".Database::escape_string($values['survey_title'])."',
  100. '".Database::escape_string($values['survey_subtitle'])."',
  101. '".Database::escape_string($_user['user_id'])."',
  102. '".Database::escape_string($values['survey_language'])."',
  103. '".Database::escape_string($values['start_date'])."',
  104. '".Database::escape_string($values['end_date'])."',
  105. '".Database::escape_string($shared_survey_id)."',
  106. '".Database::escape_string('template')."',
  107. '".Database::escape_string($values['survey_introduction'])."',
  108. '".Database::escape_string($values['survey_thanks'])."',
  109. '".date('Y-m-d H:i:s')."',
  110. '".Database::escape_string($values['anonymous'])."'
  111. )";
  112. $result = api_sql_query($sql, __FILE__, __LINE__);
  113. $survey_id = Database::insert_id();
  114. //$return['message'] = get_lang('SurveyCreatedSuccesfully').'<br />'.get_lang('YouCanNowAddQuestionToYourSurvey').': ';
  115. //$return['message'] .= '<a href="survey.php?survey_id='.$survey_id.'">'.get_lang('ClickHere').'</a>';
  116. $return['message'] = 'SurveyCreatedSuccesfully';
  117. $return['type'] = 'confirmation';
  118. $return['id'] = $survey_id;
  119. }
  120. else
  121. {
  122. if ($values['anonymous']=='')
  123. {
  124. $values['anonymous']=0;
  125. }
  126. $sql = "UPDATE $table_survey SET
  127. code = '".Database::escape_string($values['survey_code'])."',
  128. title = '".Database::escape_string($values['survey_title'])."',
  129. subtitle = '".Database::escape_string($values['survey_subtitle'])."',
  130. author = '".Database::escape_string($_user['user_id'])."',
  131. lang = '".Database::escape_string($values['survey_language'])."',
  132. avail_from = '".Database::escape_string($values['start_date'])."',
  133. avail_till = '".Database::escape_string($values['end_date'])."',
  134. is_shared = '".Database::escape_string($shared_survey_id)."',
  135. template = '".Database::escape_string('template')."',
  136. intro = '".Database::escape_string($values['survey_introduction'])."',
  137. surveythanks = '".Database::escape_string($values['survey_thanks'])."',
  138. anonymous = '".Database::escape_string($values['anonymous'])."'
  139. WHERE survey_id = '".Database::escape_string($values['survey_id'])."'";
  140. $result = api_sql_query($sql, __FILE__, __LINE__);
  141. //$return['message'] = get_lang('SurveyUpdatedSuccesfully').'<br />'.get_lang('YouCanNowAddQuestionToYourSurvey').': ';
  142. //$return['message'] .= '<a href="survey.php?survey_id='.$values['survey_id'].'">'.get_lang('Here').'</a>';
  143. //$return['message'] .= get_lang('OrReturnToSurveyOverview').'<a href="survey_list.php">'.get_lang('Here').'</a>';
  144. $return['message'] = 'SurveyUpdatedSuccesfully';
  145. $return['type'] = 'confirmation';
  146. $return['id'] = $values['survey_id'];
  147. }
  148. return $return;
  149. }
  150. /**
  151. * This function stores a shared survey in the central database.
  152. *
  153. * @param array $values
  154. * @return array $return the type of return message that has to be displayed and the message in it
  155. *
  156. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  157. * @version February 2007
  158. */
  159. function store_shared_survey($values)
  160. {
  161. global $_user;
  162. global $_course;
  163. // table defnitions
  164. $table_survey = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY);
  165. if (!$values['survey_id'] OR !is_numeric($values['survey_id']) OR $values['survey_share']['survey_share'] == 'true')
  166. {
  167. $sql = "INSERT INTO $table_survey (code, title, subtitle, author, lang, template, intro, surveythanks, creation_date, course_code) VALUES (
  168. '".Database::escape_string($values['survey_code'])."',
  169. '".Database::escape_string($values['survey_title'])."',
  170. '".Database::escape_string($values['survey_subtitle'])."',
  171. '".Database::escape_string($_user['user_id'])."',
  172. '".Database::escape_string($values['survey_language'])."',
  173. '".Database::escape_string('template')."',
  174. '".Database::escape_string($values['survey_introduction'])."',
  175. '".Database::escape_string($values['survey_thanks'])."',
  176. '".date('Y-m-d H:i:s')."',
  177. '".$_course['id']."')";
  178. $result = api_sql_query($sql, __FILE__, __LINE__);
  179. $return = Database::insert_id();
  180. }
  181. else
  182. {
  183. $sql = "UPDATE $table_survey SET
  184. code = '".Database::escape_string($values['survey_code'])."',
  185. title = '".Database::escape_string($values['survey_title'])."',
  186. subtitle = '".Database::escape_string($values['survey_subtitle'])."',
  187. author = '".Database::escape_string($_user['user_id'])."',
  188. lang = '".Database::escape_string($values['survey_language'])."',
  189. template = '".Database::escape_string('template')."',
  190. intro = '".Database::escape_string($values['survey_introduction'])."',
  191. surveythanks = '".Database::escape_string($values['survey_thanks'])."'
  192. WHERE survey_id = '".Database::escape_string($values['survey_share']['survey_share'])."'";
  193. $result = api_sql_query($sql, __FILE__, __LINE__);
  194. $return = $values['survey_share']['survey_share'];
  195. }
  196. return $return;
  197. }
  198. /**
  199. * This function deletes a survey (and also all the question in that survey
  200. *
  201. * @param $survey_id the id of the survey that has to be deleted
  202. * @return true
  203. *
  204. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  205. * @version January 2007
  206. */
  207. function delete_survey($survey_id, $shared=false)
  208. {
  209. // Database table definitions
  210. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  211. if ($shared)
  212. {
  213. $table_survey = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY);
  214. }
  215. // deleting the survey
  216. $sql = "DELETE from $table_survey WHERE survey_id='".Database::escape_string($survey_id)."'";
  217. $res = api_sql_query($sql, __FILE__, __LINE__);
  218. // deleting the questions of the survey
  219. survey_manager::delete_all_survey_questions($survey_id, $shared);
  220. return true;
  221. }
  222. /**
  223. * This function duplicates a survey (and also all the question in that survey
  224. *
  225. * @param $survey_id the id of the survey that has to be duplicated
  226. * @return true
  227. *
  228. * @author Eric Marguin <e.marguin@elixir-interactive.com>, Elixir Interactive
  229. * @version October 2007
  230. */
  231. function empty_survey($survey_id)
  232. {
  233. // Database table definitions
  234. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  235. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  236. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  237. $datas = survey_manager::get_survey($survey_id);
  238. $sql = 'DELETE FROM '.$table_survey_invitation.' WHERE survey_code = "'.Database::escape_string($datas['code']).'"';
  239. api_sql_query($sql, __FILE__, __LINE__);
  240. $sql = 'DELETE FROM '.$table_survey_answer.' WHERE survey_id='.intval($survey_id);
  241. api_sql_query($sql, __FILE__, __LINE__);
  242. $sql = 'UPDATE '.$table_survey.' SET invited=0, answered=0 WHERE survey_id='.intval($survey_id);
  243. api_sql_query($sql, __FILE__, __LINE__);
  244. return true;
  245. }
  246. /**
  247. * This function recalculates the number of people who have taken the survey (=filled at least one question)
  248. *
  249. * @param $survey_id the id of the survey somebody
  250. * @return true
  251. *
  252. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  253. * @version February 2007
  254. */
  255. function update_survey_answered($survey_id, $user, $survey_code)
  256. {
  257. global $_course;
  258. // Database table definitions
  259. $table_survey = Database :: get_course_table(TABLE_SURVEY, $_course['db_name']);
  260. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION, $_course['db_name']);
  261. // getting a list with all the people who have filled the survey
  262. $people_filled = survey_manager::get_people_who_filled_survey($survey_id);
  263. $number = count($people_filled);
  264. // storing this value in the survey table
  265. $sql = "UPDATE $table_survey SET answered = '".Database::escape_string($number)."' WHERE survey_id = '".Database::escape_string($survey_id)."'";
  266. $res = api_sql_query($sql, __FILE__, __LINE__);
  267. // storing that the user has finished the survey.
  268. $sql = "UPDATE $table_survey_invitation SET answered='1' WHERE user='".Database::escape_string($user)."' AND survey_code='".Database::escape_string($survey_code)."'";
  269. $res = api_sql_query($sql, __FILE__, __LINE__);
  270. }
  271. /**
  272. * This function gets a complete structure of a survey (all survey information, all question information
  273. * of all the questions and all the options of all the questions.
  274. *
  275. * @param integer $survey_id the id of the survey
  276. * @param boolean $shared this parameter determines if we have to get the information of a survey from the central (shared) database or from the
  277. * course database
  278. *
  279. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  280. * @version February 2007
  281. */
  282. function get_complete_survey_structure($survey_id, $shared=0)
  283. {
  284. // Database table definitions
  285. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  286. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  287. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  288. if ($shared<>0)
  289. {
  290. $table_survey = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  291. $table_survey_question = Database :: get_course_table(TABLE_SHARED_SURVEY_QUESTION);
  292. $table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  293. }
  294. $structure = survey_manager::get_survey($survey_id, $shared);
  295. $structure['questions'] = survey_manager::get_questions($survey_id);
  296. }
  297. /******************************************************************************************************
  298. SURVEY QUESTION FUNCTIONS
  299. *****************************************************************************************************/
  300. /**
  301. * This function return the "icon" of the question type
  302. *
  303. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  304. * @version February 2007
  305. */
  306. function icon_question($type)
  307. {
  308. // the possible question types
  309. $possible_types = array('yesno', 'multiplechoice', 'multipleresponse', 'open', 'dropdown', 'comment', 'pagebreak', 'percentage', 'score');
  310. // the images array
  311. $icon_question = array(
  312. 'yesno' => 'yesno.gif',
  313. 'multiplechoice' => 'mcua.gif',
  314. 'multipleresponse' => 'mcma.gif',
  315. 'open' => 'open_answer.gif',
  316. 'dropdown' => 'dropdown.gif',
  317. 'percentage' => 'percentagequestion.gif',
  318. 'score' => 'scorequestion.gif',
  319. 'comment' => 'commentquestion.gif',
  320. 'pagebreak' => 'page_end.gif',
  321. );
  322. if (in_array($type, $possible_types))
  323. {
  324. return $icon_question[$type];
  325. }
  326. else
  327. {
  328. return false;
  329. }
  330. }
  331. /**
  332. * This function retrieves all the information of a question
  333. *
  334. * @param integer $question_id the id of the question
  335. * @return array
  336. *
  337. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  338. * @version January 2007
  339. *
  340. * @todo one sql call should do the trick
  341. */
  342. function get_question($question_id, $shared=false)
  343. {
  344. // table definitions
  345. $tbl_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  346. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  347. if ($shared)
  348. {
  349. $tbl_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  350. $table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  351. }
  352. // getting the information of the question
  353. $sql = "SELECT * FROM $tbl_survey_question WHERE question_id='".Database::escape_string($question_id)."' ORDER BY `sort`";
  354. $result = api_sql_query($sql, __FILE__, __LINE__);
  355. $row = Database::fetch_array($result,'ASSOC');
  356. $return['survey_id'] = $row['survey_id'];
  357. $return['question_id'] = $row['question_id'];
  358. $return['type'] = $row['type'];
  359. $return['question'] = $row['survey_question'];
  360. $return['horizontalvertical'] = $row['display'];
  361. $return['shared_question_id'] = $row['shared_question_id'];
  362. $return['maximum_score'] = $row['max_value'];
  363. // getting the information of the question options
  364. $sql = "SELECT * FROM $table_survey_question_option WHERE question_id='".Database::escape_string($question_id)."' ORDER BY `sort` ";
  365. $result = api_sql_query($sql, __FILE__, __LINE__);
  366. while ($row = Database::fetch_array($result,'ASSOC'))
  367. {
  368. /** @todo this should be renamed to options instead of answers */
  369. $return['answers'][] = $row['option_text'];
  370. /** @todo this can be done more elegantly (used in reporting) */
  371. $return['answersid'][] = $row['question_option_id'];
  372. }
  373. return $return;
  374. }
  375. /**
  376. * This function gets all the question of any given survey
  377. *
  378. * @param integer $survey_id the id of the survey
  379. * @return array containing all the questions of the survey
  380. *
  381. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  382. * @version February 2007
  383. *
  384. * @todo one sql call should do the trick
  385. */
  386. function get_questions($survey_id)
  387. {
  388. // table definitions
  389. $tbl_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  390. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  391. // getting the information of the question
  392. $sql = "SELECT * FROM $tbl_survey_question WHERE survey_id='".Database::escape_string($survey_id)."'";
  393. $result = api_sql_query($sql, __FILE__, __LINE__);
  394. while ($row = Database::fetch_array($result,'ASSOC'))
  395. {
  396. $return[$row['question_id']]['survey_id'] = $row['survey_id'];
  397. $return[$row['question_id']]['question_id'] = $row['question_id'];
  398. $return[$row['question_id']]['type'] = $row['type'];
  399. $return[$row['question_id']]['question'] = $row['survey_question'];
  400. $return[$row['question_id']]['horizontalvertical'] = $row['display'];
  401. $return[$row['question_id']]['maximum_score'] = $row['max_value'];
  402. $return[$row['question_id']]['sort'] = $row['sort'];
  403. }
  404. // getting the information of the question options
  405. $sql = "SELECT * FROM $table_survey_question_option WHERE survey_id='".Database::escape_string($survey_id)."'";
  406. $result = api_sql_query($sql, __FILE__, __LINE__);
  407. while ($row = Database::fetch_array($result,'ASSOC'))
  408. {
  409. $return[$row['question_id']]['answers'][] = $row['option_text'];
  410. }
  411. return $return;
  412. }
  413. /**
  414. * This function saves a question in the database.
  415. * This can be either an update of an existing survey or storing a new survey
  416. *
  417. * @param array $form_content all the information of the form
  418. *
  419. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  420. * @version January 2007
  421. */
  422. function save_question($form_content)
  423. {
  424. if (strlen($form_content['question'])>1)
  425. { //checks lenght of the question
  426. $empty_answer=false;
  427. if ($form_content['type'] != 'percentage')
  428. {
  429. for($i=0;$i<count($form_content['answers']);$i++)
  430. {
  431. if (strlen($form_content['answers'][$i])<1)
  432. {
  433. $empty_answer=true;
  434. break;
  435. }
  436. }
  437. }
  438. if ($form_content['type'] == 'score' )
  439. {
  440. if (strlen($form_content['maximum_score'])<1)
  441. {
  442. $empty_answer=true;
  443. }
  444. }
  445. if (!$empty_answer)
  446. {
  447. global $_course;
  448. // table definitions
  449. $table_survey = Database :: get_course_table(TABLE_SURVEY, $_course['db_name']);
  450. $tbl_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION, $_course['db_name']);
  451. // getting all the information of the survey
  452. $survey_data = survey_manager::get_survey($form_content['survey_id']);
  453. // storing the question in the shared database
  454. if (is_numeric($survey_data['survey_share']) AND $survey_data['survey_share'] <> 0)
  455. {
  456. $shared_question_id = survey_manager::save_shared_question($form_content, $survey_data);
  457. $form_content['shared_question_id'] = $shared_question_id;
  458. }
  459. // storing a new question
  460. if ($form_content['question_id'] == '' OR !is_numeric($form_content['question_id']))
  461. {
  462. // finding the max sort order of the questions in the given survey
  463. $sql = "SELECT max(sort) AS max_sort FROM $tbl_survey_question WHERE survey_id='".Database::escape_string($form_content['survey_id'])."'";
  464. $result = api_sql_query($sql, __FILE__, __LINE__);
  465. $row = Database::fetch_array($result,'ASSOC');
  466. $max_sort = $row['max_sort'];
  467. // adding the question to the survey_question table
  468. $sql = "INSERT INTO $tbl_survey_question (survey_id,survey_question,survey_question_comment,type,display, sort, shared_question_id, max_value) VALUES (
  469. '".Database::escape_string($form_content['survey_id'])."',
  470. '".Database::escape_string($form_content['question'])."',
  471. '".Database::escape_string($form_content['question_comment'])."',
  472. '".Database::escape_string($form_content['type'])."',
  473. '".Database::escape_string($form_content['horizontalvertical'])."',
  474. '".Database::escape_string($max_sort+1)."',
  475. '".Database::escape_string($form_content['shared_question_id'])."',
  476. '".Database::escape_string($form_content['maximum_score'])."'
  477. )";
  478. $result = api_sql_query($sql, __FILE__, __LINE__);
  479. $question_id = Database::insert_id();
  480. $form_content['question_id'] = $question_id;
  481. $return_message = 'QuestionAdded';
  482. }
  483. // updating an existing question
  484. else
  485. {
  486. // adding the question to the survey_question table
  487. $sql = "UPDATE $tbl_survey_question SET
  488. survey_question = '".Database::escape_string($form_content['question'])."',
  489. survey_question_comment = '".Database::escape_string($form_content['question_comment'])."',
  490. display = '".Database::escape_string($form_content['horizontalvertical'])."',
  491. max_value = '".Database::escape_string($form_content['maximum_score'])."'
  492. WHERE question_id = '".Database::escape_string($form_content['question_id'])."'";
  493. $result = api_sql_query($sql, __FILE__, __LINE__);
  494. $return_message = 'QuestionUpdated';
  495. }
  496. // storing the options of the question
  497. $message_options=survey_manager::save_question_options($form_content, $survey_data);
  498. }
  499. else
  500. {
  501. $return_message='PleasFillAllAnswer';
  502. }
  503. }
  504. else
  505. {
  506. $return_message='PleaseEnterAQuestion';
  507. }
  508. return $return_message;
  509. }
  510. /**
  511. * This function saves the question in the shared database
  512. *
  513. * @param array $form_content all the information of the form
  514. * @param array $survey_data all the information of the survey
  515. *
  516. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  517. * @version February 2007
  518. *
  519. * @todo editing of a shared question
  520. */
  521. function save_shared_question($form_content, $survey_data)
  522. {
  523. global $_course;
  524. // table definitions
  525. $tbl_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  526. // storing a new question
  527. if ($form_content['shared_question_id'] == '' OR !is_numeric($form_content['shared_question_id']))
  528. {
  529. // finding the max sort order of the questions in the given survey
  530. $sql = "SELECT max(sort) AS max_sort FROM $tbl_survey_question
  531. WHERE survey_id='".Database::escape_string($survey_data['survey_share'])."'
  532. AND code='".Database::escape_string($_course['id'])."'";
  533. $result = api_sql_query($sql, __FILE__, __LINE__);
  534. $row = Database::fetch_array($result,'ASSOC');
  535. $max_sort = $row['max_sort'];
  536. // adding the question to the survey_question table
  537. $sql = "INSERT INTO $tbl_survey_question (survey_id, survey_question, survey_question_comment, type, display, sort, code) VALUES (
  538. '".Database::escape_string($survey_data['survey_share'])."',
  539. '".Database::escape_string($form_content['question'])."',
  540. '".Database::escape_string($form_content['question_comment'])."',
  541. '".Database::escape_string($form_content['type'])."',
  542. '".Database::escape_string($form_content['horizontalvertical'])."',
  543. '".Database::escape_string($max_sort+1)."',
  544. '".Database::escape_string($_course['id'])."')";
  545. $result = api_sql_query($sql, __FILE__, __LINE__);
  546. $shared_question_id = Database::insert_id();
  547. }
  548. // updating an existing question
  549. else
  550. {
  551. // adding the question to the survey_question table
  552. $sql = "UPDATE $tbl_survey_question SET
  553. survey_question = '".Database::escape_string($form_content['question'])."',
  554. survey_question_comment = '".Database::escape_string($form_content['question_comment'])."',
  555. display = '".Database::escape_string($form_content['horizontalvertical'])."'
  556. WHERE question_id = '".Database::escape_string($form_content['shared_question_id'])."'
  557. AND code='".Database::escape_string($_course['id'])."'";
  558. $result = api_sql_query($sql, __FILE__, __LINE__);
  559. $shared_question_id = $form_content['shared_question_id'];
  560. }
  561. return $shared_question_id;
  562. }
  563. /**
  564. * This functions moves a question of a survey up or down
  565. *
  566. * @param string $direction
  567. * @param integer $survey_question_id
  568. * @param integer $survey_id
  569. *
  570. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  571. * @version January 2007
  572. */
  573. function move_survey_question($direction, $survey_question_id, $survey_id)
  574. {
  575. // table definition
  576. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  577. if ($direction == 'moveup')
  578. {
  579. $sort = 'DESC';
  580. }
  581. if ($direction == 'movedown')
  582. {
  583. $sort = 'ASC';
  584. }
  585. // finding the two questions that needs to be swapped
  586. $sql = "SELECT * FROM $table_survey_question WHERE survey_id='".Database::escape_string($survey_id)."' ORDER BY sort $sort";
  587. $result = api_sql_query($sql, __FILE__, __LINE__);
  588. $found = false;
  589. while ($row = Database::fetch_array($result,'ASSOC'))
  590. {
  591. if ($found == true)
  592. {
  593. $question_id_two = $row['question_id'];
  594. $question_sort_two = $row['sort'];
  595. $found = false;
  596. }
  597. if ($row['question_id'] == $survey_question_id)
  598. {
  599. $found = true;
  600. $question_id_one = $row['question_id'];
  601. $question_sort_one = $row['sort'];
  602. }
  603. }
  604. $sql1 = "UPDATE $table_survey_question SET sort = '".Database::escape_string($question_sort_two)."' WHERE question_id='".Database::escape_string($question_id_one)."'";
  605. $result = api_sql_query($sql1, __FILE__, __LINE__);
  606. $sql2 = "UPDATE $table_survey_question SET sort = '".Database::escape_string($question_sort_one)."' WHERE question_id='".Database::escape_string($question_id_two)."'";
  607. $result = api_sql_query($sql2, __FILE__, __LINE__);
  608. }
  609. /**
  610. * This function deletes all the questions of a given survey
  611. * This function is normally only called when a survey is deleted
  612. *
  613. * @param $survey_id the id of the survey that has to be deleted
  614. * @return true
  615. *
  616. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  617. * @version January 2007
  618. */
  619. function delete_all_survey_questions($survey_id, $shared=false)
  620. {
  621. // table definitions
  622. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  623. if ($shared)
  624. {
  625. $table_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  626. }
  627. // deleting the survey questions
  628. $sql = "DELETE from $table_survey_question WHERE survey_id='".Database::escape_string($survey_id)."'";
  629. $res = api_sql_query($sql, __FILE__, __LINE__);
  630. // deleting all the options of the questions of the survey
  631. survey_manager::delete_all_survey_questions_options($survey_id, $shared);
  632. // deleting all the answers on this survey
  633. survey_manager::delete_all_survey_answers($survey_id);
  634. }
  635. /**
  636. * This function deletes a survey question and all its options
  637. *
  638. * @param integer $survey_id the id of the survey
  639. * @param integer $question_id the id of the question
  640. * @param integer $shared
  641. *
  642. * @todo also delete the answers to this question
  643. *
  644. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  645. * @version March 2007
  646. */
  647. function delete_survey_question($survey_id, $question_id, $shared=false)
  648. {
  649. // table definitions
  650. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  651. if ($shared)
  652. {
  653. survey_manager::delete_shared_survey_question($survey_id, $question_id);
  654. }
  655. // deleting the survey questions
  656. $sql = "DELETE from $table_survey_question WHERE survey_id='".Database::escape_string($survey_id)."' AND question_id='".Database::escape_string($question_id)."'";
  657. $res = api_sql_query($sql, __FILE__, __LINE__);
  658. // deleting the options of the question of the survey
  659. survey_manager::delete_survey_question_option($survey_id, $question_id, $shared);
  660. }
  661. /**
  662. * This function deletes a shared survey question from the main database and all its options
  663. *
  664. * @param integer $question_id the id of the question
  665. * @param integer $shared
  666. *
  667. * @todo delete all the options of this question
  668. *
  669. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  670. * @version March 2007
  671. */
  672. function delete_shared_survey_question($survey_id, $question_id)
  673. {
  674. // table definitions
  675. $table_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  676. $table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  677. // first we have to get the shared_question_id
  678. $question_data = survey_manager::get_question($question_id);
  679. // deleting the survey questions
  680. $sql = "DELETE FROM $table_survey_question WHERE question_id='".Database::escape_string($question_data['shared_question_id'])."'";
  681. $res = api_sql_query($sql, __FILE__, __LINE__);
  682. // deleting the options of the question of the survey question
  683. $sql = "DELETE FROM $table_survey_question_option WHERE question_id='".Database::escape_string($question_data['shared_question_id'])."'";
  684. $res = api_sql_query($sql, __FILE__, __LINE__);
  685. }
  686. /******************************************************************************************************
  687. SURVEY QUESTION OPTIONS FUNCTIONS
  688. *****************************************************************************************************/
  689. /**
  690. * This function stores the options of the questions in the table
  691. *
  692. * @param array $form_content
  693. * @return
  694. *
  695. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  696. * @version January 2007
  697. *
  698. * @todo writing the update statement when editing a question
  699. */
  700. function save_question_options($form_content, $survey_data)
  701. {
  702. // a percentage question type has options 1 -> 100
  703. if ($form_content['type'] == 'percentage')
  704. {
  705. for($i=1;$i<101;$i++)
  706. {
  707. $form_content['answers'][] = $i;
  708. }
  709. }
  710. if (is_numeric($survey_data['survey_share']) AND $survey_data['survey_share'] <> 0)
  711. {
  712. survey_manager::save_shared_question_options($form_content, $survey_data);
  713. }
  714. // table defintion
  715. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  716. // we are editing a question so we first have to remove all the existing options from the database
  717. if (is_numeric($form_content['question_id']))
  718. {
  719. $sql = "DELETE FROM $table_survey_question_option WHERE question_id = '".Database::escape_string($form_content['question_id'])."'";
  720. $result = api_sql_query($sql, __FILE__, __LINE__);
  721. }
  722. $counter=1;
  723. if(is_array($form_content['answers']))
  724. {
  725. foreach ($form_content['answers'] as $key=>$answer)
  726. {
  727. $sql = "INSERT INTO $table_survey_question_option (question_id, survey_id, option_text, sort) VALUES (
  728. '".Database::escape_string($form_content['question_id'])."',
  729. '".Database::escape_string($form_content['survey_id'])."',
  730. '".Database::escape_string($answer)."',
  731. '".Database::escape_string($counter)."')";
  732. $result = api_sql_query($sql, __FILE__, __LINE__);
  733. $counter++;
  734. }
  735. }
  736. }
  737. /**
  738. * This function stores the options of the questions in the shared table
  739. *
  740. * @param array $form_content
  741. * @return
  742. *
  743. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  744. * @version February 2007
  745. *
  746. * @todo writing the update statement when editing a question
  747. */
  748. function save_shared_question_options($form_content, $survey_data)
  749. {
  750. if (is_array($form_content))
  751. {
  752. // table defintion
  753. $table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  754. // we are editing a question so we first have to remove all the existing options from the database
  755. $sql = "DELETE FROM $table_survey_question_option WHERE question_id = '".Database::escape_string($form_content['shared_question_id'])."'";
  756. $result = api_sql_query($sql, __FILE__, __LINE__);
  757. $counter = 1;
  758. foreach ($form_content['answers'] as $key=>$answer)
  759. {
  760. $sql = "INSERT INTO $table_survey_question_option (question_id, survey_id, option_text, sort) VALUES (
  761. '".Database::escape_string($form_content['shared_question_id'])."',
  762. '".Database::escape_string($survey_data['is_shared'])."',
  763. '".Database::escape_string($answer)."',
  764. '".Database::escape_string($counter)."')";
  765. $result = api_sql_query($sql, __FILE__, __LINE__);
  766. $counter++;
  767. }
  768. }
  769. }
  770. /*
  771. if (is_numeric($survey_data['survey_share']) AND $survey_data['survey_share'] <> 0)
  772. {
  773. $form_content = survey_manager::save_shared_question($form_content, $survey_data);
  774. }
  775. */
  776. /**
  777. * This function deletes all the options of the questions of a given survey
  778. * This function is normally only called when a survey is deleted
  779. *
  780. * @param $survey_id the id of the survey that has to be deleted
  781. * @return true
  782. *
  783. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  784. * @version January 2007
  785. */
  786. function delete_all_survey_questions_options($survey_id, $shared=false)
  787. {
  788. // table definitions
  789. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  790. if ($shared)
  791. {
  792. $table_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  793. }
  794. // deleting the options of the survey questions
  795. $sql = "DELETE from $table_survey_question_option WHERE survey_id='".Database::escape_string($survey_id)."'";
  796. $res = api_sql_query($sql, __FILE__, __LINE__);
  797. return true;
  798. }
  799. /**
  800. * This function deletes the options of a given question
  801. *
  802. * @param unknown_type $survey_id
  803. * @param unknown_type $question_id
  804. * @param unknown_type $shared
  805. * @return unknown
  806. *
  807. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  808. * @version March 2007
  809. */
  810. function delete_survey_question_option($survey_id, $question_id, $shared=false)
  811. {
  812. // table definitions
  813. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  814. if ($shared)
  815. {
  816. $table_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  817. }
  818. // deleting the options of the survey questions
  819. $sql = "DELETE from $table_survey_question_option WHERE survey_id='".Database::escape_string($survey_id)."' AND question_id='".Database::escape_string($question_id)."'";
  820. $res = api_sql_query($sql, __FILE__, __LINE__);
  821. return true;
  822. }
  823. /******************************************************************************************************
  824. SURVEY ANSWERS FUNCTIONS
  825. *****************************************************************************************************/
  826. /**
  827. * This function deletes all the answers anyone has given on this survey
  828. * This function is normally only called when a survey is deleted
  829. *
  830. * @param $survey_id the id of the survey that has to be deleted
  831. * @return true
  832. *
  833. * @todo write the function
  834. *
  835. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  836. * @version January 2007
  837. */
  838. function delete_all_survey_answers($survey_id)
  839. {
  840. return true;
  841. }
  842. /**
  843. * This function gets all the persons who have filled the survey
  844. *
  845. * @param integer $survey_id
  846. * @return array
  847. *
  848. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  849. * @version February 2007
  850. */
  851. function get_people_who_filled_survey($survey_id, $all_user_info = false)
  852. {
  853. global $_course;
  854. // Database table definition
  855. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER, $_course['db_name']);
  856. $table_user = Database :: get_main_table('user');
  857. // variable initialisation
  858. $return = array();
  859. // getting the survey information
  860. $survey_data = survey_manager::get_survey($survey_id);
  861. if ($all_user_info)
  862. {
  863. $sql = "SELECT DISTINCT answered_user.user as invited_user, user.firstname, user.lastname, user.user_id
  864. FROM $table_survey_answer answered_user
  865. LEFT JOIN $table_user as user
  866. ON answered_user.user = user.user_id
  867. WHERE survey_id= '".Database::escape_string($survey_data['survey_id'])."'";
  868. }
  869. else
  870. {
  871. $sql = "SELECT DISTINCT user FROM $table_survey_answer WHERE survey_id= '".Database::escape_string($survey_data['survey_id'])."'";
  872. }
  873. $res = api_sql_query($sql, __FILE__, __LINE__);
  874. while ($row = Database::fetch_array($res,'ASSOC'))
  875. {
  876. if ($all_user_info)
  877. {
  878. $return[] = $row;
  879. }
  880. else
  881. {
  882. $return[] = $row['user'];
  883. }
  884. }
  885. return $return;
  886. }
  887. }
  888. class question
  889. {
  890. // the html code of the form
  891. public $html;
  892. /**
  893. * This function does the generic part of any survey question: the question field
  894. *
  895. * @return unknown
  896. */
  897. /**
  898. * This function does the generic part of any survey question: the question field
  899. *
  900. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  901. * @version January 2007
  902. *
  903. * @todo the form_text has to become a wysiwyg editor or adding a question_comment field
  904. * @todo consider adding a question_comment form element
  905. */
  906. function create_form($form_content)
  907. {
  908. global $fck_attribute;
  909. $this->html = '<form id="question_form" name="question_form" method="post" action="'.api_get_self().'?action='.$_GET['action'].'&type='.$_GET['type'].'&survey_id='.$_GET['survey_id'].'&question_id='.$_GET['question_id'].'">';
  910. $this->html .= ' <input type="hidden" name="survey_id" id="survey_id" value="'.$_GET['survey_id'].'"/>';
  911. $this->html .= ' <input type="hidden" name="question_id" id="question_id" value="'.$_GET['question_id'].'"/>';
  912. $this->html .= ' <input type="hidden" name="shared_question_id" id="shared_question_id" value="'.$form_content['shared_question_id'].'"/>';
  913. $this->html .= ' <input type="hidden" name="type" id="type" value="'.$_GET['type'].'"/>';
  914. $this->html .= '<table>';
  915. $this->html .= ' <tr>';
  916. $this->html .= ' <td colspan="3"><strong>'.get_lang('Question').'</strong></td>';
  917. $this->html .= ' </tr>';
  918. $this->html .= ' <tr>';
  919. //$this->html .= ' <td><label for="question">'.get_lang('Question').'</label></td>';
  920. $fck_attribute['Width'] = '100%';
  921. $fck_attribute['Height'] = '100';
  922. $fck_attribute['ToolbarSet'] = 'Survey';
  923. //$this->html .= ' <td><input type="text" name="question" id="question" value="'.$form_content['question'].'"/></td>';
  924. $this->html .= ' <td colspan="3" width="500">'.api_return_html_area('question', html_entity_decode(stripslashes($form_content['question']))).'</td>';
  925. $this->html .= ' </tr>';
  926. /*
  927. $this->html .= ' <tr>';
  928. $this->html .= ' <td><label for="question_comment">'.get_lang('QuestionComment').'</label></td>';
  929. $this->html .= ' <td><input type="text" name="question_comment" id="question_comment" value="'.$form_content['question_comment'].'"/></td>';
  930. $this->html .= ' <td>&nbsp;</td>';
  931. $this->html .= ' </tr>';
  932. */
  933. $this->html .=' <tr>
  934. <td colspan="">&nbsp</td>
  935. </tr>';
  936. return $this->html;
  937. }
  938. /**
  939. * This functions displays the form after the html variable has correctly been finished
  940. * (adding a submit button, closing the table and closing the form)
  941. *
  942. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  943. * @version January 2007
  944. *
  945. */
  946. function render_form()
  947. {
  948. $this->html .= ' <tr>';
  949. $this->html .= ' <td>&nbsp;</td>';
  950. $this->html .= ' <td> <input type="submit" name="save_question" value="'.get_lang('SaveQuestion').'" /></td>';
  951. $this->html .= ' <td>&nbsp;</td>';
  952. $this->html .= ' </tr>';
  953. $this->html .= '</table>';
  954. $this->html .= '</form>';
  955. echo $this->html;
  956. }
  957. /**
  958. * This function handles the actions on a question and its answers
  959. *
  960. * @todo consider using $form_content instead of $_POST
  961. *
  962. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  963. * @version January 2007
  964. */
  965. function handle_action($form_content)
  966. {
  967. global $config;
  968. // moving an answer up
  969. if ($_POST['move_up'])
  970. {
  971. foreach ($_POST['move_up'] as $key=>$value)
  972. {
  973. $id1 = $key;
  974. $content1 = $form_content['answers'][$id1];
  975. $id2 = $key-1;
  976. $content2 = $form_content['answers'][$id2];
  977. $form_content['answers'][$id1] = $content2;
  978. $form_content['answers'][$id2] = $content1;
  979. }
  980. }
  981. // moving an answer down
  982. if ($_POST['move_down'])
  983. {
  984. foreach ($_POST['move_down'] as $key=>$value)
  985. {
  986. $id1 = $key;
  987. $content1 = $form_content['answers'][$id1];
  988. $id2 = $key+1;
  989. $content2 = $form_content['answers'][$id2];
  990. $form_content['answers'][$id1] = $content2;
  991. $form_content['answers'][$id2] = $content1;
  992. }
  993. }
  994. // adding an answer
  995. if ($_POST['add_answer'])
  996. {
  997. $form_content['answers'][]='';
  998. }
  999. // removing an answer
  1000. if ($_POST['remove_answer'])
  1001. {
  1002. $max_answer = count($form_content['answers']);
  1003. unset($form_content['answers'][$max_answer-1]);
  1004. }
  1005. // saving a question
  1006. if ($_POST['save_question'])
  1007. {
  1008. $message = survey_manager::save_question($form_content);
  1009. if ($message == 'QuestionAdded' || $message == 'QuestionUpdated' ) {
  1010. if ($config['survey']['debug'])
  1011. {
  1012. Display :: display_header();
  1013. Display :: display_confirmation_message($message.'<br />'.get_lang('ReturnTo').' <a href="survey.php?survey_id='.$_GET['survey_id'].'">'.get_lang('Survey').'</a>', false);
  1014. }
  1015. else
  1016. {
  1017. header('location:survey.php?survey_id='.Security::remove_XSS($_GET['survey_id']).'&message='.$message);
  1018. }
  1019. }
  1020. else
  1021. {
  1022. if ($message == 'PleaseEnterAQuestion' || $message=='PleasFillAllAnswer'){
  1023. $_SESSION['temp_user_message']=$form_content['question'];
  1024. $_SESSION['temp_sys_message']=$message;
  1025. $_SESSION['temp_answers']=$form_content['answers'];
  1026. header('location:question.php?'.api_get_cidreq().'&survey_id='.Security::remove_XSS($_GET['survey_id']).'&action='.Security::remove_XSS($_GET['action']).'&type='.Security::remove_XSS($_GET['type']).'');
  1027. }
  1028. }
  1029. }
  1030. /**
  1031. * This solution is a little bit strange but I could not find a different solution.
  1032. */
  1033. if ($_POST['delete_answer'])
  1034. {
  1035. foreach ($_POST['delete_answer'] as $key=>$value)
  1036. {
  1037. unset($form_content['answers'][$key]);
  1038. $deleted = $key;
  1039. }
  1040. foreach ($form_content['answers'] as $key=>$value)
  1041. {
  1042. if ($key>$deleted)
  1043. {
  1044. $form_content['answers'][$key-1] = $form_content['answers'][$key];
  1045. unset($form_content['answers'][$key]);
  1046. }
  1047. }
  1048. }
  1049. return $form_content;
  1050. }
  1051. /**
  1052. * This functions adds two buttons. One to add an option, one to remove an option
  1053. *
  1054. * @param unknown_type $form_content
  1055. * @return html code
  1056. *
  1057. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1058. * @version January 2007
  1059. */
  1060. function add_remove_buttons($form_content)
  1061. {
  1062. if (count($form_content['answers'])<=2)
  1063. {
  1064. $remove_answer_attribute = 'disabled="disabled"';
  1065. }
  1066. $return .= ' <tr>';
  1067. $return .= ' <td align="right">&nbsp;</td>';
  1068. $return .= ' <td colspan="2">';
  1069. $return .= ' <input type="submit" name="remove_answer" value="'.get_lang('RemoveAnswer').'" '.$remove_answer_attribute.' />';
  1070. $return .= ' <input type="submit" name="add_answer" value="'.get_lang('AddAnswer').'" />';
  1071. $return .= ' </td>';
  1072. $return .= ' </tr>';
  1073. return $return;
  1074. }
  1075. /**
  1076. * render the question. In this case this starts with the form tag
  1077. *
  1078. * @param unknown_type $form_content
  1079. *
  1080. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1081. * @version January 2007
  1082. */
  1083. function render_question($form_content)
  1084. {
  1085. $this->html = '<form id="question" name="question" method="post" action="'.api_get_self().'?survey_id='.$_GET['survey_id'].'">';
  1086. echo $this->html;
  1087. }
  1088. }
  1089. class yesno extends question
  1090. {
  1091. /**
  1092. * This function creates the form elements for the yesno questions
  1093. *
  1094. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1095. * @version January 2007
  1096. */
  1097. function create_form($form_content)
  1098. {
  1099. $this->html = parent::create_form($form_content);
  1100. // Horizontal or vertical
  1101. $this->html .= ' <tr>';
  1102. $this->html .= ' <td colspan="2"><strong>'.get_lang('DisplayAnswersHorVert').'</strong></td>';
  1103. $this->html .= ' </tr>';
  1104. $this->html .= ' <tr>';
  1105. $this->html .= ' <td align="right" valign="top">&nbsp;</td>';
  1106. $this->html .= ' <td>';
  1107. $this->html .= ' <input name="horizontalvertical" type="radio" value="horizontal" ';
  1108. if ($form_content['horizontalvertical'] == 'horizontal')
  1109. {
  1110. $this->html .= 'checked="checked"';
  1111. }
  1112. $this->html .= '/>'.get_lang('Horizontal').'</label><br />';
  1113. $this->html .= ' <input name="horizontalvertical" type="radio" value="vertical" ';
  1114. if ($form_content['horizontalvertical'] == 'vertical')
  1115. {
  1116. $this->html .= 'checked="checked"';
  1117. }
  1118. $this->html .= ' />'.get_lang('Vertical').'</label>';
  1119. $this->html .= ' </td>';
  1120. $this->html .= ' <td>&nbsp;</td>';
  1121. $this->html .= ' </tr>';
  1122. $this->html .=' <tr>
  1123. <td colspan="">&nbsp</td>
  1124. </tr>';
  1125. // The options
  1126. $this->html .= ' <tr>';
  1127. $this->html .= ' <td colspan="3"><strong>'.get_lang('AnswerOptions').'</strong></td>';
  1128. $this->html .= ' </tr>';
  1129. $this->html .= ' <tr>';
  1130. $this->html .= ' <td align="right"><label for="answers[0]">1</label></td>';
  1131. //$this->html .= ' <td><input type="text" name="answers[0]" id="answers[0]" value="'.$form_content['answers'][0].'" /></td>';
  1132. $this->html .= ' <td>'.api_return_html_area('answers[0]', stripslashes($form_content['answers'][0])).'</td>';
  1133. $this->html .= ' <td><input type="image" src="../img/down.gif" value="move_down[0]" name="move_down[0]"/></td>';
  1134. $this->html .= ' </tr>';
  1135. $this->html .= ' <tr>';
  1136. $this->html .= ' <td align="right"><label for="answers[1]">2</label></td>';
  1137. //$this->html .= ' <td><input type="text" name="answers[1]" id="answers[1]" value="'.$form_content['answers'][1].'" /></td>';
  1138. $this->html .= ' <td>'.api_return_html_area('answers[1]', stripslashes($form_content['answers'][1])).'</td>';
  1139. $this->html .= ' <td><input type="image" src="../img/up.gif" value="move_up[1]" name="move_up[1]" /></td>';
  1140. $this->html .= ' </tr>';
  1141. }
  1142. /**
  1143. * Render the yes not question type
  1144. *
  1145. * @param unknown_type $form_content
  1146. *
  1147. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1148. * @version January 2007
  1149. */
  1150. function render_question($form_content, $answers=array())
  1151. {
  1152. foreach ($form_content['options'] as $key=>$value)
  1153. {
  1154. $this->html .= '<label><input name="question'.$form_content['question_id'].'" type="radio" value="'.$key.'"';
  1155. if (is_array($answers))
  1156. {
  1157. if (in_array($key,$answers))
  1158. {
  1159. $this->html .= 'checked="checked"';
  1160. }
  1161. }
  1162. $this->html .= '/>'.$value.'</label>';
  1163. if ($form_content['display'] == 'vertical')
  1164. {
  1165. $this->html .= '<br />';
  1166. }
  1167. }
  1168. echo '<div class="survey_question_wrapper">';
  1169. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  1170. echo '<div class="survey_question_options">';
  1171. echo $this->html;
  1172. echo '</div>';
  1173. echo '</div>';
  1174. }
  1175. }
  1176. class multiplechoice extends question
  1177. {
  1178. /**
  1179. * This function creates the form elements for the multiple choice questions
  1180. *
  1181. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1182. * @version January 2007
  1183. */
  1184. function create_form($form_content)
  1185. {
  1186. $this->html = parent::create_form($form_content);
  1187. $this->html .= ' <tr>';
  1188. $this->html .= ' <td colspan="2"><strong>'.get_lang('DisplayAnswersHorVert').'</strong></td>';
  1189. $this->html .= ' </tr>';
  1190. // Horizontal or vertical
  1191. $this->html .= ' <tr>';
  1192. $this->html .= ' <td align="right" valign="top">&nbsp;</td>';
  1193. $this->html .= ' <td>';
  1194. $this->html .= ' <input name="horizontalvertical" type="radio" value="horizontal" ';
  1195. if ($form_content['horizontalvertical'] == 'horizontal')
  1196. {
  1197. $this->html .= 'checked="checked"';
  1198. }
  1199. $this->html .= '/>'.get_lang('Horizontal').'</label><br />';
  1200. $this->html .= ' <input name="horizontalvertical" type="radio" value="vertical" ';
  1201. if ($form_content['horizontalvertical'] == 'vertical')
  1202. {
  1203. $this->html .= 'checked="checked"';
  1204. }
  1205. $this->html .= ' />'.get_lang('Vertical').'</label>'; $this->html .= ' </td>';
  1206. $this->html .= ' <td>&nbsp;</td>';
  1207. $this->html .= ' </tr>';
  1208. $this->html .=' <tr>
  1209. <td colspan="">&nbsp</td>
  1210. </tr>';
  1211. // The Options
  1212. $this->html .= ' <tr>';
  1213. $this->html .= ' <td colspan="3"><strong>'.get_lang('AnswerOptions').'</strong></td>';
  1214. $this->html .= ' </tr>';
  1215. $total_number_of_answers = count($form_content['answers']);
  1216. foreach ($form_content['answers'] as $key=>$value)
  1217. {
  1218. $this->html .= ' <tr>';
  1219. $this->html .= ' <td align="right"><label for="answers['.$key.']">'.($key+1).'</label></td>';
  1220. //$this->html .= ' <td><input type="text" name="answers['.$key.']" id="answers['.$key.']" value="'.$form_content['answers'][$key].'" /></td>';
  1221. $this->html .= ' <td width="500">'.api_return_html_area('answers['.$key.']', html_entity_decode(stripslashes($form_content['answers'][$key]))).'</td>';
  1222. $this->html .= ' <td>';
  1223. if ($key<$total_number_of_answers-1)
  1224. {
  1225. $this->html .= ' <input type="image" src="../img/down.gif" value="move_down['.$key.']" name="move_down['.$key.']"/>';
  1226. }
  1227. else
  1228. {
  1229. $this->html .= ' <img src="../img/spacer.gif" alt="'.get_lang('Empty').'" title="'.get_lang('Empty').'" />';
  1230. }
  1231. if ($key>0)
  1232. {
  1233. $this->html .= ' <input type="image" src="../img/up.gif" value="move_up['.$key.']" name="move_up['.$key.']"/>';
  1234. }
  1235. else
  1236. {
  1237. $this->html .= ' <img src="../img/spacer.gif" alt="'.get_lang('Empty').'" title="'.get_lang('Empty').'" />';
  1238. }
  1239. if ($total_number_of_answers> 2)
  1240. {
  1241. $this->html .= ' <input type="image" src="../img/delete.gif" value="delete_answer['.$key.']" name="delete_answer['.$key.']"/>';
  1242. }
  1243. $this->html .= ' </td>';
  1244. $this->html .= ' </tr>';
  1245. }
  1246. // The buttons for adding or removing
  1247. $this->html .= parent :: add_remove_buttons($form_content);
  1248. }
  1249. /**
  1250. * render the multiple choice question type
  1251. *
  1252. * @param unknown_type $form_content
  1253. *
  1254. * @todo it would make more sense to consider yesno as a special case of multiplechoice and not the other way around
  1255. *
  1256. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1257. * @version January 2007
  1258. */
  1259. function render_question($form_content, $answers=array())
  1260. {
  1261. $question = new yesno();
  1262. $question->render_question($form_content, $answers);
  1263. }
  1264. }
  1265. class multipleresponse extends question
  1266. {
  1267. /**
  1268. * This function creates the form elements for the multiple response questions
  1269. *
  1270. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1271. * @version January 2007
  1272. */
  1273. function create_form($form_content)
  1274. {
  1275. $this->html = parent::create_form($form_content);
  1276. $this->html .= ' <tr>';
  1277. $this->html .= ' <td colspan="2"><strong>'.get_lang('DisplayAnswersHorVert').'</strong></td>';
  1278. $this->html .= ' </tr>';
  1279. // Horizontal or vertical
  1280. $this->html .= ' <tr>';
  1281. $this->html .= ' <td align="right" valign="top">&nbsp;</td>';
  1282. $this->html .= ' <td>';
  1283. $this->html .= ' <input name="horizontalvertical" type="radio" value="horizontal" ';
  1284. if ($form_content['horizontalvertical'] == 'horizontal')
  1285. {
  1286. $this->html .= 'checked="checked"';
  1287. }
  1288. $this->html .= '/>'.get_lang('Horizontal').'</label><br />';
  1289. $this->html .= ' <input name="horizontalvertical" type="radio" value="vertical" ';
  1290. if ($form_content['horizontalvertical'] == 'vertical')
  1291. {
  1292. $this->html .= 'checked="checked"';
  1293. }
  1294. $this->html .= ' />'.get_lang('Vertical').'</label>'; $this->html .= ' </td>';
  1295. $this->html .= ' <td>&nbsp;</td>';
  1296. $this->html .= ' </tr>';
  1297. $this->html .=' <tr>
  1298. <td colspan="">&nbsp</td>
  1299. </tr>';
  1300. // The options
  1301. $this->html .= ' <tr>';
  1302. $this->html .= ' <td colspan="3"><strong>'.get_lang('AnswerOptions').'</strong></td>';
  1303. $this->html .= ' </tr>';
  1304. $total_number_of_answers = count($form_content['answers']);
  1305. foreach ($form_content['answers'] as $key=>$value)
  1306. {
  1307. $this->html .= ' <tr>';
  1308. $this->html .= ' <td align="right"><label for="answers['.$key.']">'.($key+1).'</label></td>';
  1309. //$this->html .= ' <td><input type="text" name="answers['.$key.']" id="answers['.$key.']" value="'.$form_content['answers'][$key].'" /></td>';
  1310. $this->html .= ' <td width="500">'.api_return_html_area('answers['.$key.']', html_entity_decode(stripslashes($form_content['answers'][$key]))).'</td>';
  1311. $this->html .= ' <td>';
  1312. if ($key<$total_number_of_answers-1)
  1313. {
  1314. $this->html .= ' <input type="image" src="../img/down.gif" value="move_down['.$key.']" name="move_down['.$key.']"/>';
  1315. }
  1316. else
  1317. {
  1318. $this->html .= ' <img src="../img/spacer.gif" alt="'.get_lang('Empty').'" title="'.get_lang('Empty').'" />';
  1319. }
  1320. if ($key>0)
  1321. {
  1322. $this->html .= ' <input type="image" src="../img/up.gif" value="move_up['.$key.']" name="move_up['.$key.']"/>';
  1323. }
  1324. else
  1325. {
  1326. $this->html .= ' <img src="../img/spacer.gif" alt="'.get_lang('Empty').'" title="'.get_lang('Empty').'" />';
  1327. }
  1328. if ($total_number_of_answers> 2)
  1329. {
  1330. $this->html .= ' <input type="image" src="../img/delete.gif" value="delete_answer['.$key.']" name="delete_answer['.$key.']"/>';
  1331. }
  1332. $this->html .= ' </td>';
  1333. $this->html .= ' </tr>';
  1334. }
  1335. // The buttons for adding or removing
  1336. $this->html .= parent :: add_remove_buttons($form_content);
  1337. }
  1338. /**
  1339. * Render the multiple response question type
  1340. *
  1341. * @param unknown_type $form_content
  1342. *
  1343. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1344. * @version January 2007
  1345. */
  1346. function render_question($form_content, $answers=array())
  1347. {
  1348. foreach ($form_content['options'] as $key=>$value)
  1349. {
  1350. $this->html .= '<label><input name="question'.$form_content['question_id'].'[]" type="checkbox" value="'.$key.'"';
  1351. if (is_array($answers))
  1352. {
  1353. if (in_array($key,$answers))
  1354. {
  1355. $this->html .= 'checked="checked"';
  1356. }
  1357. }
  1358. $this->html .= ' />'.$value.'</label>';
  1359. if ($form_content['display'] == 'vertical')
  1360. {
  1361. $this->html .= '<br />';
  1362. }
  1363. }
  1364. echo '<div class="survey_question_wrapper">';
  1365. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  1366. echo '<div class="survey_question_options">';
  1367. echo $this->html;
  1368. echo '</div>';
  1369. }
  1370. }
  1371. class dropdown extends question
  1372. {
  1373. /**
  1374. * This function creates the form elements for the dropdown questions
  1375. *
  1376. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1377. * @version January 2007
  1378. */
  1379. function create_form($form_content)
  1380. {
  1381. $this->html = parent::create_form($form_content);
  1382. // The answers
  1383. $this->html .= ' <tr>';
  1384. $this->html .= ' <td colspan="3"><strong>'.get_lang('AnswerOptions').'</strong></td>';
  1385. $this->html .= ' </tr>';
  1386. $total_number_of_answers = count($form_content['answers']);
  1387. foreach ($form_content['answers'] as $key=>$value)
  1388. {
  1389. $this->html .= ' <tr>';
  1390. $this->html .= ' <td align="right"><label for="answers['.$key.']">'.($key+1).'</label></td>';
  1391. $this->html .= ' <td><input type="text" name="answers['.$key.']" id="answers['.$key.']" value="'.stripslashes(htmlentities($form_content['answers'][$key])).'" /></td>';
  1392. $this->html .= ' <td>';
  1393. if ($key<$total_number_of_answers-1)
  1394. {
  1395. $this->html .= ' <input type="image" src="../img/down.gif" value="move_down['.$key.']" name="move_down['.$key.']"/>';
  1396. }
  1397. if ($key>0)
  1398. {
  1399. $this->html .= ' <input type="image" src="../img/up.gif" value="move_up['.$key.']" name="move_up['.$key.']"/>';
  1400. }
  1401. if ($total_number_of_answers> 2)
  1402. {
  1403. $this->html .= ' <input type="image" src="../img/delete.gif" value="delete_answer['.$key.']" name="delete_answer['.$key.']"/>';
  1404. }
  1405. $this->html .= ' </td>';
  1406. $this->html .= ' </tr>';
  1407. }
  1408. // The buttons for adding or removing
  1409. $this->html .= parent :: add_remove_buttons($form_content);
  1410. }
  1411. /**
  1412. * Render the dropdown question type
  1413. *
  1414. * @param unknown_type $form_content
  1415. *
  1416. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1417. * @version January 2007
  1418. */
  1419. function render_question($form_content, $answers=array())
  1420. {
  1421. foreach ($form_content['options'] as $key=>$value)
  1422. {
  1423. $this->html .= '<option value="'.$key.'" ';
  1424. if (is_array($answers))
  1425. {
  1426. if (in_array($key,$answers))
  1427. {
  1428. $this->html .= 'selected="selected"';
  1429. }
  1430. }
  1431. $this->html .= '>'.$value.'</option>';
  1432. }
  1433. echo '<div class="survey_question_wrapper">';
  1434. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  1435. echo '<div class="survey_question_options">';
  1436. echo '<select name="question'.$form_content['question_id'].'" id="select">';
  1437. echo $this->html;
  1438. echo '</select>';
  1439. echo '</div>';
  1440. /*
  1441. <option value="test">test</option>
  1442. */
  1443. }
  1444. }
  1445. class open extends question
  1446. {
  1447. /**
  1448. * This function creates the form elements for the open questions
  1449. *
  1450. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1451. * @version January 2007
  1452. *
  1453. * @todo add a limit for the number of characters that can be type
  1454. * @todo add a checkbox weither the answer is a textarea or a wysiwyg editor
  1455. */
  1456. function create_form($form_content)
  1457. {
  1458. $this->html = parent::create_form($form_content);
  1459. }
  1460. /**
  1461. * render the open question type
  1462. *
  1463. * @param unknown_type $form_content
  1464. *
  1465. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1466. * @version January 2007
  1467. */
  1468. function render_question($form_content, $answers=array())
  1469. {
  1470. echo '<div class="survey_question_wrapper">';
  1471. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  1472. echo '<div class="survey_question_options">';
  1473. if (is_array($answers))
  1474. {
  1475. $content = implode('',$answers);
  1476. }
  1477. else
  1478. {
  1479. $content = $answers;
  1480. }
  1481. echo '<label for="question'.$form_content['question_id'].'"></label><textarea name="question'.$form_content['question_id'].'" id="textarea" style="width: 400px; height: 130px;">'.$content.'</textarea>';
  1482. echo '</div>';
  1483. }
  1484. }
  1485. class comment extends question
  1486. {
  1487. /**
  1488. * This function creates the form elements for a comment.
  1489. * A comment is nothing more than a block of text that the user can read
  1490. *
  1491. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1492. * @version January 2007
  1493. *
  1494. * @param array $form_content
  1495. */
  1496. function create_form($form_content)
  1497. {
  1498. $this->html = parent::create_form($form_content);
  1499. }
  1500. /**
  1501. * Render the comment "question" type
  1502. *
  1503. * @param unknown_type $form_content
  1504. *
  1505. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1506. * @version January 2007
  1507. */
  1508. function render_question($form_content)
  1509. {
  1510. echo '<div class="survey_question_wrapper">';
  1511. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  1512. echo '</div>';
  1513. echo "\n";
  1514. }
  1515. }
  1516. class pagebreak extends question
  1517. {
  1518. /**
  1519. * This function creates the form elements for a comment.
  1520. * A comment is nothing more than a block of text that the user can read
  1521. *
  1522. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1523. * @version January 2007
  1524. *
  1525. * @param array $form_content
  1526. */
  1527. function create_form($form_content)
  1528. {
  1529. $this->html = parent::create_form($form_content);
  1530. }
  1531. }
  1532. class percentage extends question
  1533. {
  1534. function create_form($form_content)
  1535. {
  1536. $this->html = parent::create_form($form_content);
  1537. }
  1538. function render_question($form_content, $answers=array())
  1539. {
  1540. $this->html .= '<option value="--">--</option>';
  1541. foreach ($form_content['options'] as $key=>$value)
  1542. {
  1543. $this->html .= '<option value="'.$key.'" ';
  1544. if (is_array($answers))
  1545. {
  1546. if (in_array($key,$answers))
  1547. {
  1548. $this->html .= 'selected="selected"';
  1549. }
  1550. }
  1551. $this->html .= '>'.$value.'</option>';
  1552. }
  1553. echo '<div class="survey_question_wrapper">';
  1554. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  1555. echo '<div class="survey_question_options">';
  1556. echo '<select name="question'.$form_content['question_id'].'" id="select">';
  1557. echo $this->html;
  1558. echo '</select>';
  1559. echo '</div>';
  1560. }
  1561. }
  1562. class score extends question
  1563. {
  1564. function create_form($form_content)
  1565. {
  1566. $this->html = parent::create_form($form_content);
  1567. // the maximum score that can be given
  1568. $this->html .= ' <tr>';
  1569. $this->html .= ' <td colspan="3"><strong>'.get_lang('MaximumScore').'</strong></td>';
  1570. $this->html .= ' </tr>';
  1571. $this->html .= ' <tr>
  1572. <td colspan="3"><input type="text" name="maximum_score" value="'.$form_content['maximum_score'].'"></td>
  1573. </tr>';
  1574. // The answers
  1575. $this->html .= ' <tr>';
  1576. $this->html .= ' <td colspan="3"><strong>'.get_lang('AnswerOptions').'</strong></td>';
  1577. $this->html .= ' </tr>';
  1578. $total_number_of_answers = count($form_content['answers']);
  1579. foreach ($form_content['answers'] as $key=>$value)
  1580. {
  1581. $this->html .= ' <tr>';
  1582. $this->html .= ' <td align="right"><label for="answers['.$key.']">'.($key+1).'</label></td>';
  1583. //$this->html .= ' <td><input type="text" name="answers['.$key.']" id="answers['.$key.']" value="'.$form_content['answers'][$key].'" /></td>';
  1584. $this->html .= ' <td width="500">'.api_return_html_area('answers['.$key.']', stripslashes($form_content['answers'][$key])).'</td>';
  1585. $this->html .= ' <td>';
  1586. if ($key<$total_number_of_answers-1)
  1587. {
  1588. $this->html .= ' <input type="image" src="../img/down.gif" value="move_down['.$key.']" name="move_down['.$key.']"/>';
  1589. }
  1590. if ($key>0)
  1591. {
  1592. $this->html .= ' <input type="image" src="../img/up.gif" value="move_up['.$key.']" name="move_up['.$key.']"/>';
  1593. }
  1594. if ($total_number_of_answers> 2)
  1595. {
  1596. $this->html .= ' <input type="image" src="../img/delete.gif" value="delete_answer['.$key.']" name="delete_answer['.$key.']"/>';
  1597. }
  1598. $this->html .= ' </td>';
  1599. $this->html .= ' </tr>';
  1600. }
  1601. // The buttons for adding or removing
  1602. $this->html .= parent :: add_remove_buttons($form_content);
  1603. }
  1604. function render_question($form_content, $answers=array())
  1605. {
  1606. /*
  1607. echo '<div style="border: 1px solid red;">';
  1608. echo '<pre>';
  1609. print_r($answers);
  1610. echo '</pre></div>';
  1611. */
  1612. $this->html = '<table>';
  1613. foreach ($form_content['options'] as $key=>$value)
  1614. {
  1615. $this->html .= '<tr>
  1616. <td>'.$value.'</td>';
  1617. $this->html .= ' <td>';
  1618. $this->html .= '<select name="question'.$form_content['question_id'].'['.$key.']">';
  1619. $this->html .= '<option value="--">--</option>';
  1620. for($i=1; $i<=$form_content['maximum_score']; $i++)
  1621. {
  1622. $this->html .= '<option value="'.$i.'"';
  1623. if ($answers[$key] == $i)
  1624. {
  1625. $this->html .= 'selected="selected" ';
  1626. }
  1627. $this->html .= '>'.$i.'</option>';
  1628. }
  1629. $this->html .= '</select>';
  1630. $this->html .= ' </td>';
  1631. $this->html .= '</tr>';
  1632. }
  1633. $this->html .= '</table>';
  1634. echo '<div class="survey_question_wrapper">';
  1635. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  1636. echo '<div class="survey_question_options">';
  1637. //echo '<select name="question'.$form_content['question_id'].'" id="select">';
  1638. echo $this->html;
  1639. //echo '</select>';
  1640. echo '</div>';
  1641. }
  1642. }
  1643. /**
  1644. * This class offers a series of general utility functions for survey querying and display
  1645. * @package dokeos.survey
  1646. */
  1647. class SurveyUtil {
  1648. /**
  1649. * Checks whether the given survey has a pagebreak question as the first or the last question.
  1650. * If so, break the current process, displaying an error message
  1651. * @param integer Survey ID (database ID)
  1652. * @param boolean Optional. Whether to continue the current process or exit when breaking condition found. Defaults to true (do not break).
  1653. * @return void
  1654. */
  1655. function check_first_last_question($survey_id, $continue=true)
  1656. {
  1657. // table definitions
  1658. $tbl_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  1659. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  1660. // getting the information of the question
  1661. $sql = "SELECT * FROM $tbl_survey_question WHERE survey_id='".Database::escape_string($survey_id)."' ORDER BY sort ASC";
  1662. $result = api_sql_query($sql, __FILE__, __LINE__);
  1663. $total = Database::num_rows($result);
  1664. $counter=1;
  1665. $error = false;
  1666. while ($row = Database::fetch_array($result,'ASSOC'))
  1667. {
  1668. if ($counter == 1 AND $row['type'] == 'pagebreak')
  1669. {
  1670. Display::display_error_message(get_lang('PagebreakNotFirst'), false);
  1671. $error = true;
  1672. }
  1673. if ($counter == $total AND $row['type'] == 'pagebreak')
  1674. {
  1675. Display::display_error_message(get_lang('PagebreakNotLast'), false);
  1676. $error = true;
  1677. }
  1678. $counter++;
  1679. }
  1680. if (!$continue AND $error)
  1681. {
  1682. Display::display_footer();
  1683. exit;
  1684. }
  1685. }
  1686. /**
  1687. * This function removes an (or multiple) answer(s) of a user on a question of a survey
  1688. *
  1689. * @param mixed The user id or email of the person who fills the survey
  1690. * @param integer The survey id
  1691. * @param integer The question id
  1692. * @param integer The option id
  1693. *
  1694. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1695. * @version January 2007
  1696. */
  1697. function remove_answer($user, $survey_id, $question_id)
  1698. {
  1699. global $_course;
  1700. // table definition
  1701. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER, $_course['db_name']);
  1702. $sql = "DELETE FROM $table_survey_answer
  1703. WHERE user = '".Database::escape_string($user)."'
  1704. AND survey_id = '".Database::escape_string($survey_id)."'
  1705. AND question_id = '".Database::escape_string($question_id)."'";
  1706. $result = api_sql_query($sql, __FILE__, __LINE__);
  1707. }
  1708. /**
  1709. * This function stores an answer of a user on a question of a survey
  1710. *
  1711. * @param mixed The user id or email of the person who fills the survey
  1712. * @param integer Survey id
  1713. * @param integer Question id
  1714. * @param integer Option id
  1715. * @param string Option value
  1716. * @param array Survey data settings
  1717. *
  1718. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1719. * @version January 2007
  1720. */
  1721. function store_answer($user, $survey_id, $question_id, $option_id, $option_value, $survey_data)
  1722. {
  1723. global $_course;
  1724. global $types;
  1725. // table definition
  1726. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER, $_course['db_name']);
  1727. // make the survey anonymous
  1728. if ($survey_data['anonymous'] == 1)
  1729. {
  1730. if (!$_SESSION['surveyuser'])
  1731. {
  1732. $user = md5($user.time());
  1733. $_SESSION['surveyuser'] = $user;
  1734. }
  1735. else
  1736. {
  1737. $user = $_SESSION['surveyuser'];
  1738. }
  1739. }
  1740. $sql = "INSERT INTO $table_survey_answer (user, survey_id, question_id, option_id, value) VALUES (
  1741. '".Database::escape_string($user)."',
  1742. '".Database::escape_string($survey_id)."',
  1743. '".Database::escape_string($question_id)."',
  1744. '".Database::escape_string($option_id)."',
  1745. '".Database::escape_string($option_value)."'
  1746. )";
  1747. $result = api_sql_query($sql, __FILE__, __LINE__);
  1748. }
  1749. /**
  1750. * This function checks the parameters that are used in this page
  1751. *
  1752. * @return string The header, an error and the footer if any parameter fails, else it returns true
  1753. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1754. * @version February 2007
  1755. */
  1756. function check_parameters()
  1757. {
  1758. $error = false;
  1759. // getting the survey data
  1760. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  1761. // $_GET['survey_id'] has to be numeric
  1762. if (!is_numeric($_GET['survey_id']))
  1763. {
  1764. $error = get_lang('IllegalSurveyId');
  1765. }
  1766. // $_GET['action']
  1767. $allowed_actions = array('overview', 'questionreport', 'userreport', 'comparativereport', 'completereport');
  1768. if (isset($_GET['action']) AND !in_array($_GET['action'], $allowed_actions))
  1769. {
  1770. $error = get_lang('ActionNotAllowed');
  1771. }
  1772. // user report
  1773. if ($_GET['action'] == 'userreport')
  1774. {
  1775. global $people_filled;
  1776. if ($survey_data['anonymous'] == 0)
  1777. {
  1778. $people_filled_full_data = true;
  1779. }
  1780. else
  1781. {
  1782. $people_filled_full_data = false;
  1783. }
  1784. $people_filled = survey_manager::get_people_who_filled_survey($_GET['survey_id'], $people_filled_full_data);
  1785. if ($survey_data['anonymous'] == 0)
  1786. {
  1787. foreach ($people_filled as $key=>$value)
  1788. {
  1789. $people_filled_userids[]=$value['invited_user'];
  1790. }
  1791. }
  1792. else
  1793. {
  1794. $people_filled_userids = $people_filled;
  1795. }
  1796. if (isset($_GET['user']) AND !in_array($_GET['user'], $people_filled_userids))
  1797. {
  1798. $error = get_lang('UnknowUser');
  1799. }
  1800. }
  1801. // question report
  1802. if ($_GET['action'] == 'questionreport')
  1803. {
  1804. if (isset($_GET['question'])AND !is_numeric($_GET['question']))
  1805. {
  1806. $error = get_lang('UnknowQuestion');
  1807. }
  1808. }
  1809. if ($error)
  1810. {
  1811. $tool_name = get_lang('Reporting');
  1812. Display::display_header($tool_name);
  1813. Display::display_error_message(get_lang('Error').': '.$error, false);
  1814. Display::display_footer();
  1815. exit;
  1816. }
  1817. else
  1818. {
  1819. return true;
  1820. }
  1821. }
  1822. /**
  1823. * This function deals with the action handling
  1824. * @return void
  1825. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1826. * @version February 2007
  1827. */
  1828. function handle_reporting_actions()
  1829. {
  1830. // getting the number of question
  1831. $temp_questions_data = survey_manager::get_questions($_GET['survey_id']);
  1832. // sorting like they should be displayed and removing the non-answer question types (comment and pagebreak)
  1833. foreach ($temp_questions_data as $key=>$value)
  1834. {
  1835. if ($value['type'] <> 'comment' AND $value['type']<>'pagebreak')
  1836. {
  1837. $questions_data[$value['sort']]=$value;
  1838. }
  1839. }
  1840. // counting the number of questions that are relevant for the reporting
  1841. $survey_data['number_of_questions'] = count($questions_data);
  1842. if ($_GET['action'] == 'questionreport')
  1843. {
  1844. SurveyUtil::display_question_report($survey_data);
  1845. }
  1846. if ($_GET['action'] == 'userreport')
  1847. {
  1848. SurveyUtil::display_user_report();
  1849. }
  1850. if ($_GET['action'] == 'comparativereport')
  1851. {
  1852. SurveyUtil::display_comparative_report();
  1853. }
  1854. if ($_GET['action'] == 'completereport')
  1855. {
  1856. SurveyUtil::display_complete_report();
  1857. }
  1858. }
  1859. /**
  1860. * This function displays the user report which is basically nothing more than a one-page display of all the questions
  1861. * of the survey that is filled with the answers of the person who filled the survey.
  1862. *
  1863. * @return string html code of the one-page survey with the answers of the selected user
  1864. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1865. * @version February 2007 - Updated March 2008
  1866. */
  1867. function display_user_report()
  1868. {
  1869. global $people_filled, $survey_data;
  1870. // Database table definitions
  1871. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  1872. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  1873. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  1874. // step 1: selection of the user
  1875. echo "<script language=\"JavaScript\" type=\"text/JavaScript\">
  1876. <!--
  1877. function jumpMenu(targ,selObj,restore)
  1878. {
  1879. eval(targ+\".location='\"+selObj.options[selObj.selectedIndex].value+\"'\");
  1880. if (restore) selObj.selectedIndex=0;
  1881. }
  1882. //-->
  1883. </script>
  1884. ";
  1885. echo get_lang('SelectUserWhoFilledSurvey').'<br />';
  1886. echo '<select name="user" onchange="jumpMenu(\'parent\',this,0)">';
  1887. echo '<option value="reporting.php?action='.Security::remove_XSS($_GET['action']).'&amp;survey_id='.Security::remove_XSS($_GET['survey_id']).'">'.get_lang('SelectUser').'</option>';
  1888. foreach ($people_filled as $key=>$person)
  1889. {
  1890. if ($survey_data['anonymous'] == 0)
  1891. {
  1892. $name = $person['firstname'].' '.$person['lastname'];
  1893. $id = $person['user_id'];
  1894. }
  1895. else
  1896. {
  1897. $name = $key+1;
  1898. $id = $person;
  1899. }
  1900. echo '<option value="reporting.php?action='.Security::remove_XSS($_GET['action']).'&amp;survey_id='.Security::remove_XSS($_GET['survey_id']).'&amp;user='.$id.'" ';
  1901. if ($_GET['user'] == $person)
  1902. {
  1903. echo 'selected="selected"';
  1904. }
  1905. echo '>'.$name.'</option>';
  1906. }
  1907. echo '</select>';
  1908. // step 2: displaying the survey and the answer of the selected users
  1909. if (isset($_GET['user']))
  1910. {
  1911. Display::display_normal_message(get_lang('AllQuestionsOnOnePage'), false);
  1912. // getting all the questions and options
  1913. $sql = "SELECT survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.max_value, survey_question.sort, survey_question.type,
  1914. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  1915. FROM $table_survey_question survey_question
  1916. LEFT JOIN $table_survey_question_option survey_question_option
  1917. ON survey_question.question_id = survey_question_option.question_id
  1918. WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  1919. ORDER BY survey_question.sort ASC";
  1920. $result = api_sql_query($sql, __FILE__, __LINE__);
  1921. while ($row = Database::fetch_array($result,'ASSOC'))
  1922. {
  1923. if($row['type'] <> 'pagebreak')
  1924. {
  1925. $questions[$row['sort']]['question_id'] = $row['question_id'];
  1926. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  1927. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  1928. $questions[$row['sort']]['display'] = $row['display'];
  1929. $questions[$row['sort']]['type'] = $row['type'];
  1930. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  1931. $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text'];
  1932. }
  1933. }
  1934. // getting all the answers of the user
  1935. $sql = "SELECT * FROM $table_survey_answer WHERE survey_id = '".Database::escape_string($_GET['survey_id'])."' AND user = '".Database::escape_string($_GET['user'])."'";
  1936. $result = api_sql_query($sql, __FILE__, __LINE__);
  1937. while ($row = Database::fetch_array($result,'ASSOC'))
  1938. {
  1939. $answers[$row['question_id']][] = $row['option_id'];
  1940. $all_answers[$row['question_id']][] = $row;
  1941. }
  1942. // displaying all the questions
  1943. $second_parameter=array();
  1944. foreach ($questions as $key=>$question)
  1945. {
  1946. // if the question type is a scoring then we have to format the answers differently
  1947. if ($question['type'] == 'score')
  1948. {
  1949. if (is_array($second_parameter) && is_array($question) &&is_array($all_answers))
  1950. {
  1951. foreach($all_answers[$question['question_id']] as $key=>$answer_array)
  1952. {
  1953. $second_parameter[$answer_array['option_id']] = $answer_array['value'];
  1954. }
  1955. }
  1956. }
  1957. else
  1958. {
  1959. $second_parameter = $answers[$question['question_id']];
  1960. if ($question['type'] == 'open')
  1961. {
  1962. $second_parameter = array();
  1963. $second_parameter[] = $all_answers[$question['question_id']][0]['option_id'];
  1964. }
  1965. }
  1966. $display = new $question['type'];
  1967. $display->render_question($question, $second_parameter);
  1968. }
  1969. }
  1970. }
  1971. /**
  1972. * This function displays the report by question.
  1973. *
  1974. * It displays a table with all the options of the question and the number of users who have answered positively on the option.
  1975. * The number of users who answered positive on a given option is expressed in an absolute number, in a percentage of the total
  1976. * and graphically using bars
  1977. * By clicking on the absolute number you get a list with the persons who have answered this.
  1978. * 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
  1979. * answers of that user.
  1980. *
  1981. * @param array All the survey data
  1982. * @return string html code that displays the report by question
  1983. * @todo allow switching between horizontal and vertical.
  1984. * @todo multiple response: percentage are probably not OK
  1985. * @todo the question and option text have to be shortened and should expand when the user clicks on it.
  1986. * @todo the pagebreak and comment question types should not be shown => removed from $survey_data before
  1987. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1988. * @version February 2007 - Updated March 2008
  1989. */
  1990. function display_question_report($survey_data)
  1991. {
  1992. // Database table definitions
  1993. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  1994. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  1995. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  1996. // determining the offset of the sql statement (the n-th question of the survey)
  1997. if (!isset($_GET['question']))
  1998. {
  1999. $offset = 0;
  2000. }
  2001. else
  2002. {
  2003. $offset = Database::escape_string($_GET['question']);
  2004. }
  2005. echo '<div id="question_report_questionnumbers">';
  2006. for($i=1; $i<=($survey_data['number_of_questions']); $i++ )
  2007. {
  2008. if ($offset <> $i-1)
  2009. {
  2010. echo '<a href="reporting.php?action=questionreport&amp;survey_id='.(int)$_GET['survey_id'].'&amp;question='.($i-1).'">'.$i.'</a>';
  2011. }
  2012. else
  2013. {
  2014. echo $i;
  2015. }
  2016. if ($i < $survey_data['number_of_questions'])
  2017. {
  2018. echo ' | ';
  2019. }
  2020. }
  2021. echo '</div>';
  2022. // getting the question information
  2023. $sql = "SELECT * FROM $table_survey_question WHERE survey_id='".Database::escape_string($_GET['survey_id'])."' AND type<>'pagebreak' AND type<>'comment' ORDER BY sort ASC LIMIT ".$offset.",1";
  2024. $result = api_sql_query($sql, __FILE__, __LINE__);
  2025. $question = Database::fetch_array($result);
  2026. // navigate through the questions (next and previous)
  2027. if ($_GET['question'] <> 0)
  2028. {
  2029. echo '<a href="reporting.php?action='.Security::remove_XSS($_GET['action']).'&amp;survey_id='.$_GET['survey_id'].'&amp;question='.Security::remove_XSS($offset-1).'"> &lt;&lt; '.get_lang('PreviousQuestion').'</a> ';
  2030. }
  2031. else
  2032. {
  2033. echo '&lt;&lt;'.get_lang('PreviousQuestion').' ';
  2034. }
  2035. echo ' | ';
  2036. if ($_GET['question'] < ($survey_data['number_of_questions']-1))
  2037. {
  2038. echo '<a href="reporting.php?action='.$_GET['action'].'&amp;survey_id='.Security::remove_XSS($_GET['survey_id']).'&amp;question='.Security::remove_XSS($offset+1).'">'.get_lang('NextQuestion').'&gt;&gt; </a>';
  2039. }
  2040. else
  2041. {
  2042. echo get_lang('NextQuestion'). '&gt;&gt;';
  2043. }
  2044. echo '<br />';
  2045. echo $question['survey_question'];
  2046. echo '<br />';
  2047. if ($question['type'] == 'score')
  2048. {
  2049. /** @todo this function should return the options as this is needed further in the code */
  2050. $options = SurveyUtil::display_question_report_score($survey_data, $question, $offset);
  2051. }
  2052. elseif ($question['type'] == 'open')
  2053. {
  2054. /** @todo also get the user who has answered this */
  2055. $sql = "SELECT * FROM $table_survey_answer WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'
  2056. AND question_id = '".Database::escape_string($question['question_id'])."'";
  2057. $result = api_sql_query($sql, __FILE__, __LINE__);
  2058. while ($row = Database::fetch_array($result))
  2059. {
  2060. echo $row['option_id'].'<hr noshade="noshade" size="1" />';
  2061. }
  2062. }
  2063. else
  2064. {
  2065. // getting the options
  2066. $sql = "SELECT * FROM $table_survey_question_option
  2067. WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'
  2068. AND question_id = '".Database::escape_string($question['question_id'])."'
  2069. ORDER BY sort ASC";
  2070. $result = api_sql_query($sql, __FILE__, __LINE__);
  2071. while ($row = Database::fetch_array($result))
  2072. {
  2073. $options[$row['question_option_id']] = $row;
  2074. }
  2075. // getting the answers
  2076. $sql = "SELECT *, count(answer_id) as total FROM $table_survey_answer
  2077. WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'
  2078. AND question_id = '".Database::escape_string($question['question_id'])."'
  2079. GROUP BY option_id, value";
  2080. $result = api_sql_query($sql, __FILE__, __LINE__);
  2081. while ($row = Database::fetch_array($result))
  2082. {
  2083. $number_of_answers += $row['total'];
  2084. $data[$row['option_id']] = $row;
  2085. }
  2086. //echo '<pre>';
  2087. //print_r($data);
  2088. //echo '<pre>';
  2089. // displaying the table: headers
  2090. echo '<table>';
  2091. echo ' <tr>';
  2092. echo ' <th>&nbsp;</th>';
  2093. echo ' <th>'.get_lang('AbsoluteTotal').'</th>';
  2094. echo ' <th>'.get_lang('Percentage').'</th>';
  2095. echo ' <th>'.get_lang('VisualRepresentation').'</th>';
  2096. echo ' <tr>';
  2097. // displaying the table: the content
  2098. foreach ($options as $key=>$value)
  2099. {
  2100. $absolute_number = $data[$value['question_option_id']]['total'];
  2101. if($number_of_answers == 0)
  2102. {
  2103. $answers_number = 0;
  2104. }
  2105. else
  2106. {
  2107. $answers_number = $absolute_number/$number_of_answers*100;
  2108. }
  2109. echo ' <tr>';
  2110. echo ' <td>'.$value['option_text'].'</td>';
  2111. echo ' <td align="right"><a href="reporting.php?action='.Security::remove_XSS($_GET['action']).'&amp;survey_id='.Security::remove_XSS($_GET['survey_id']).'&amp;question='.Security::remove_XSS($offset).'&amp;viewoption='.$value['question_option_id'].'">'.$absolute_number.'</a></td>';
  2112. echo ' <td align="right">'.round($answers_number, 2).' %</td>';
  2113. echo ' <td align="right">';
  2114. $size = $answers_number*2;
  2115. if ($size > 0)
  2116. {
  2117. echo '<div style="border:1px solid #264269; background-color:#aecaf4; height:10px; width:'.$size.'px">&nbsp;</div>';
  2118. }
  2119. echo ' </td>';
  2120. echo ' </tr>';
  2121. }
  2122. // displaying the table: footer (totals)
  2123. echo ' <tr>';
  2124. echo ' <td style="border-top:1px solid black;"><b>'.get_lang('Total').'</b></td>';
  2125. echo ' <td style="border-top:1px solid black;" align="right"><b>'.($number_of_answers==0?'0':$number_of_answers).'</b></td>';
  2126. echo ' <td style="border-top:1px solid black;">&nbsp;</td>';
  2127. echo ' <td style="border-top:1px solid black;">&nbsp;</td>';
  2128. echo ' </tr>';
  2129. echo '</table>';
  2130. }
  2131. if (isset($_GET['viewoption']))
  2132. {
  2133. echo get_lang('PeopleWhoAnswered').': '.$options[$_GET['viewoption']]['option_text'].'<br />';
  2134. if (is_numeric($_GET['value']))
  2135. {
  2136. $sql_restriction = "AND value='".Database::escape_string($_GET['value'])."'";
  2137. }
  2138. $sql = "SELECT user FROM $table_survey_answer WHERE option_id = '".Database::escape_string($_GET['viewoption'])."' $sql_restriction";
  2139. $result = api_sql_query($sql, __FILE__, __LINE__);
  2140. while ($row = Database::fetch_array($result))
  2141. {
  2142. echo '<a href="reporting.php?action=userreport&survey_id='.Security::remove_XSS($_GET['survey_id']).'&user='.$row['user'].'">'.$row['user'].'</a><br />';
  2143. }
  2144. }
  2145. }
  2146. /**
  2147. * Display score data about a survey question
  2148. * @param array Question info
  2149. * @param integer The offset of results shown
  2150. * @return void (direct output)
  2151. */
  2152. function display_question_report_score($survey_data, $question, $offset)
  2153. {
  2154. // Database table definitions
  2155. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  2156. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  2157. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  2158. // getting the options
  2159. $sql = "SELECT * FROM $table_survey_question_option
  2160. WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'
  2161. AND question_id = '".Database::escape_string($question['question_id'])."'
  2162. ORDER BY sort ASC";
  2163. $result = api_sql_query($sql, __FILE__, __LINE__);
  2164. while ($row = Database::fetch_array($result))
  2165. {
  2166. $options[$row['question_option_id']] = $row;
  2167. }
  2168. // getting the answers
  2169. $sql = "SELECT *, count(answer_id) as total FROM $table_survey_answer
  2170. WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'
  2171. AND question_id = '".Database::escape_string($question['question_id'])."'
  2172. GROUP BY option_id, value";
  2173. $result = api_sql_query($sql, __FILE__, __LINE__);
  2174. while ($row = Database::fetch_array($result))
  2175. {
  2176. $number_of_answers += $row['total'];
  2177. $data[$row['option_id']][$row['value']] = $row;
  2178. }
  2179. // displaying the table: headers
  2180. echo '<table>';
  2181. echo ' <tr>';
  2182. echo ' <th>&nbsp;</th>';
  2183. echo ' <th>'.get_lang('Score').'</th>';
  2184. echo ' <th>'.get_lang('AbsoluteTotal').'</th>';
  2185. echo ' <th>'.get_lang('Percentage').'</th>';
  2186. echo ' <th>'.get_lang('VisualRepresentation').'</th>';
  2187. echo ' <tr>';
  2188. // displaying the table: the content
  2189. foreach ($options as $key=>$value)
  2190. {
  2191. for ($i=1; $i<=$question['max_value']; $i++)
  2192. {
  2193. $absolute_number = $data[$value['question_option_id']][$i]['total'];
  2194. echo ' <tr>';
  2195. echo ' <td>'.$value['option_text'].'</td>';
  2196. echo ' <td>'.$i.'</td>';
  2197. echo ' <td><a href="reporting.php?action='.Security::remove_XSS($_GET['action']).'&amp;survey_id='.Security::remove_XSS($_GET['survey_id']).'&amp;question='.Security::remove_XSS($offset).'&amp;viewoption='.$value['question_option_id'].'&amp;value='.$i.'">'.$absolute_number.'</a></td>';
  2198. echo ' <td>'.round($absolute_number/$number_of_answers*100, 2).' %</td>';
  2199. echo ' <td>';
  2200. $size = ($absolute_number/$number_of_answers*100*2);
  2201. if ($size > 0)
  2202. {
  2203. echo ' <div style="border:1px solid #264269; background-color:#aecaf4; height:10px; width:'.$size.'px">&nbsp;</div>';
  2204. }
  2205. echo ' </td>';
  2206. echo ' </tr>';
  2207. }
  2208. }
  2209. // displaying the table: footer (totals)
  2210. echo ' <tr>';
  2211. echo ' <td style="border-top:1px solid black"><b>'.get_lang('Total').'</b></td>';
  2212. echo ' <td style="border-top:1px solid black">&nbsp;</td>';
  2213. echo ' <td style="border-top:1px solid black"><b>'.$number_of_answers.'</b></td>';
  2214. echo ' <td style="border-top:1px solid black">&nbsp;</td>';
  2215. echo ' <td style="border-top:1px solid black">&nbsp;</td>';
  2216. echo ' </tr>';
  2217. echo '</table>';
  2218. }
  2219. /**
  2220. * This functions displays the complete reporting
  2221. * @return string HTML code
  2222. * @todo open questions are not in the complete report yet.
  2223. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2224. * @version February 2007
  2225. */
  2226. function display_complete_report()
  2227. {
  2228. // Database table definitions
  2229. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  2230. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  2231. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  2232. // the form
  2233. echo '<form id="form1a" name="form1a" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'">';
  2234. echo '<input type="hidden" name="export_report" value="export_report">';
  2235. echo '<input type="hidden" name="export_format" value="csv">';
  2236. echo '</form>';
  2237. echo '<form id="form1b" name="form1b" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'">';
  2238. echo '<input type="hidden" name="export_report" value="export_report">';
  2239. echo '<input type="hidden" name="export_format" value="xls">';
  2240. echo '</form>';
  2241. echo '<form id="form2" name="form2" method="post" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'">';
  2242. echo '<a class="survey_export_link" href="#" onclick="document.form1a.submit();"><img align="absbottom" src="'.api_get_path(WEB_IMG_PATH).'excel.gif">&nbsp;'.get_lang('ExportAsCSV').'</a>';
  2243. echo '<a class="survey_export_link" href="#" onclick="document.form1b.submit();"><img align="absbottom" src="'.api_get_path(WEB_IMG_PATH).'excel.gif">&nbsp;'.get_lang('ExportAsXLS').'</a>';
  2244. // the table
  2245. echo '<br /><table class="data_table" border="1">';
  2246. // getting the number of options per question
  2247. echo ' <tr>';
  2248. echo ' <th>';
  2249. if ($_POST['submit_question_filter'] OR $_POST['export_report'])
  2250. {
  2251. echo ' <input type="submit" name="reset_question_filter" value="'.get_lang('ResetQuestionFilter').'" />';
  2252. }
  2253. echo ' <input type="submit" name="submit_question_filter" value="'.get_lang('SubmitQuestionFilter').'" />';
  2254. echo '</th>';
  2255. $display_extra_user_fields = false;
  2256. if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR !empty($_POST['fields_filter']))
  2257. {
  2258. //show user fields section with a big th colspan that spans over all fields
  2259. $extra_user_fields = UserManager::get_extra_fields(0,0,5,'ASC',false);
  2260. $num = count($extra_user_fields);
  2261. if($num > 0)
  2262. {
  2263. echo '<th '.($num>0?' colspan="'.$num.'"':'').'>';
  2264. echo '<label><input type="checkbox" name="fields_filter" value="1" checked="checked"/> ';
  2265. echo get_lang('UserFields');
  2266. echo '</label>';
  2267. echo '</th>';
  2268. $display_extra_user_fields = true;
  2269. }
  2270. }
  2271. // Get all the questions ordered by the "sort" column
  2272. $sql = "SELECT q.question_id, q.type, q.survey_question, count(o.question_option_id) as number_of_options
  2273. FROM $table_survey_question q LEFT JOIN $table_survey_question_option o
  2274. ON q.question_id = o.question_id
  2275. WHERE q.question_id = o.question_id
  2276. AND q.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  2277. GROUP BY q.question_id
  2278. ORDER BY q.sort ASC";
  2279. $result = api_sql_query($sql, __FILE__, __LINE__);
  2280. while ($row = Database::fetch_array($result))
  2281. {
  2282. // we show the questions if
  2283. // 1. there is no question filter and the export button has not been clicked
  2284. // 2. there is a quesiton filter but the question is selected for display
  2285. if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR in_array($row['question_id'], $_POST['questions_filter']))
  2286. {
  2287. // we do not show comment and pagebreak question types
  2288. if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
  2289. {
  2290. echo ' <th';
  2291. if ($row['number_of_options'] >0)
  2292. {
  2293. echo ' colspan="'.$row['number_of_options'].'"';
  2294. }
  2295. echo '>';
  2296. echo '<label><input type="checkbox" name="questions_filter[]" value="'.$row['question_id'].'" checked="checked"/> ';
  2297. echo $row['survey_question'];
  2298. echo '</label>';
  2299. echo '</th>';
  2300. }
  2301. //no column at all if it's not a question
  2302. }
  2303. $questions[$row['question_id']] = $row;
  2304. }
  2305. echo ' </tr>';
  2306. // getting all the questions and options
  2307. echo ' <tr>';
  2308. echo ' <th>&nbsp;</th>'; // the user column
  2309. if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR !empty($_POST['fields_filter']))
  2310. {
  2311. //show the fields names for user fields
  2312. foreach($extra_user_fields as $field)
  2313. {
  2314. echo '<th>'.$field[3].'</th>';
  2315. }
  2316. }
  2317. $sql = "SELECT sq.question_id, sq.survey_id,
  2318. sq.survey_question, sq.display,
  2319. sq.sort, sq.type, sqo.question_option_id,
  2320. sqo.option_text, sqo.sort as option_sort
  2321. FROM $table_survey_question sq
  2322. LEFT JOIN $table_survey_question_option sqo
  2323. ON sq.question_id = sqo.question_id
  2324. WHERE sq.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  2325. ORDER BY sq.sort ASC, sqo.sort ASC";
  2326. $result = api_sql_query($sql, __FILE__, __LINE__);
  2327. while ($row = Database::fetch_array($result))
  2328. {
  2329. // we show the options if
  2330. // 1. there is no question filter and the export button has not been clicked
  2331. // 2. there is a question filter but the question is selected for display
  2332. if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR in_array($row['question_id'], $_POST['questions_filter']))
  2333. {
  2334. // we do not show comment and pagebreak question types
  2335. if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak' AND $row['type'] <> 'open')
  2336. {
  2337. echo ' <th>';
  2338. echo $row['option_text'];
  2339. echo '</th>';
  2340. $possible_answers[$row['question_id']][$row['question_option_id']] =$row['question_option_id'];
  2341. }
  2342. //no column at all if the question was not a question
  2343. }
  2344. }
  2345. echo ' </tr>';
  2346. // getting all the answers of the users
  2347. $old_user='';
  2348. $answers_of_user = array();
  2349. $sql = "SELECT * FROM $table_survey_answer WHERE survey_id='".Database::escape_string($_GET['survey_id'])."' ORDER BY user ASC";
  2350. $result = api_sql_query($sql, __FILE__, __LINE__);
  2351. while ($row = Database::fetch_array($result))
  2352. {
  2353. if ($old_user <> $row['user'] AND $old_user<>'')
  2354. {
  2355. SurveyUtil::display_complete_report_row($possible_answers, $answers_of_user, $old_user, $questions, $display_extra_user_fields);
  2356. $answers_of_user=array();
  2357. }
  2358. if ($questions[$row['question_id']]['type']<> 'open')
  2359. {
  2360. $answers_of_user[$row['question_id']][$row['option_id']] = $row;
  2361. }
  2362. else
  2363. {
  2364. $answers_of_user[$row['question_id']][0] = $row;
  2365. }
  2366. $old_user = $row['user'];
  2367. }
  2368. SurveyUtil::display_complete_report_row($possible_answers, $answers_of_user, $old_user, $questions, $display_extra_user_fields); // this is to display the last user
  2369. echo '</table>';
  2370. echo '</form>';
  2371. }
  2372. /**
  2373. * This function displays a row (= a user and his/her answers) in the table of the complete report.
  2374. *
  2375. * @param array Possible options
  2376. * @param array User answers
  2377. * @param mixed User ID or user details string
  2378. * @param boolean Whether to show extra user fields or not
  2379. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2380. * @version February 2007 - Updated March 2008
  2381. */
  2382. function display_complete_report_row($possible_options, $answers_of_user, $user, $questions, $display_extra_user_fields=false)
  2383. {
  2384. global $survey_data;
  2385. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  2386. echo '<tr>';
  2387. if ($survey_data['anonymous'] == 0)
  2388. {
  2389. if(intval($user)!==0)
  2390. {
  2391. $sql = 'SELECT firstname, lastname FROM '.Database::get_main_table(TABLE_MAIN_USER).' WHERE user_id='.intval($user);
  2392. $rs = api_sql_query($sql, __FILE__, __LINE__);
  2393. if($row = Database::fetch_array($rs))
  2394. {
  2395. $user_displayed = $row['lastname'].' '.$row['firstname'];
  2396. }
  2397. else
  2398. {
  2399. $user_displayed = '-';
  2400. }
  2401. echo ' <th><a href="'.api_get_self().'?action=userreport&survey_id='.Security::remove_XSS($_GET['survey_id']).'&user='.$user.'">'.$user_displayed.'</a></th>'; // the user column
  2402. }
  2403. else
  2404. {
  2405. echo ' <th>'.$user.'</th>'; // the user column
  2406. }
  2407. }
  2408. else
  2409. {
  2410. echo '<th>-</th>';
  2411. }
  2412. if ($display_extra_user_fields)
  2413. {
  2414. //show user fields data, if any, for this user
  2415. $user_fields_values = UserManager::get_extra_user_data(intval($user),false,false);
  2416. foreach($user_fields_values as $value)
  2417. {
  2418. echo '<td align="center">'.$value.'</td>';
  2419. }
  2420. }
  2421. foreach ($possible_options as $question_id=>$possible_option)
  2422. {
  2423. if ($questions[$question_id]['type'] == 'open')
  2424. {
  2425. echo '<td align="center">';
  2426. echo $answers_of_user[$question_id]['0']['option_id'];
  2427. echo '</td>';
  2428. }
  2429. else
  2430. {
  2431. foreach ($possible_option as $option_id=>$value)
  2432. {
  2433. echo '<td align="center">';
  2434. if (!empty($answers_of_user[$question_id][$option_id]))
  2435. {
  2436. if ($answers_of_user[$question_id][$option_id]['value']<>0)
  2437. {
  2438. echo $answers_of_user[$question_id][$option_id]['value'];
  2439. }
  2440. else
  2441. {
  2442. echo 'v';
  2443. }
  2444. }
  2445. }
  2446. }
  2447. }
  2448. echo '</tr>';
  2449. }
  2450. /**
  2451. * Quite similar to display_complete_report(), returns an HTML string
  2452. * that can be used in a csv file
  2453. * @todo consider merging this function with display_complete_report
  2454. * @return string The contents of a csv file
  2455. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2456. * @version February 2007
  2457. */
  2458. function export_complete_report()
  2459. {
  2460. // Database table definitions
  2461. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  2462. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  2463. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  2464. // the first column
  2465. $return = ';';
  2466. //show extra fields blank space (enough for extra fields on next line)
  2467. $display_extra_user_fields = false;
  2468. if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR !empty($_POST['fields_filter']))
  2469. {
  2470. //show user fields section with a big th colspan that spans over all fields
  2471. $extra_user_fields = UserManager::get_extra_fields(0,0,5,'ASC',false);
  2472. $num = count($extra_user_fields);
  2473. $return .= str_repeat(';',$num);
  2474. $display_extra_user_fields = true;
  2475. }
  2476. $sql = "SELECT questions.question_id, questions.type, questions.survey_question, count(options.question_option_id) as number_of_options
  2477. FROM $table_survey_question questions LEFT JOIN $table_survey_question_option options
  2478. ON questions.question_id = options.question_id "
  2479. ." AND questions.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  2480. GROUP BY questions.question_id "
  2481. ." ORDER BY questions.sort ASC";
  2482. $result = api_sql_query($sql, __FILE__, __LINE__);
  2483. while ($row = Database::fetch_array($result))
  2484. {
  2485. // we show the questions if
  2486. // 1. there is no question filter and the export button has not been clicked
  2487. // 2. there is a quesiton filter but the question is selected for display
  2488. if (!($_POST['submit_question_filter']) OR (is_array($_POST['questions_filter']) && in_array($row['question_id'], $_POST['questions_filter'])))
  2489. {
  2490. // we do not show comment and pagebreak question types
  2491. if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
  2492. {
  2493. if($row['number_of_options'] == 0 && $row['type']=='open')
  2494. {
  2495. $return .= str_replace("\r\n",' ',html_entity_decode(strip_tags($row['survey_question']))).';';
  2496. }
  2497. else
  2498. {
  2499. for ($ii = 0; $ii < $row['number_of_options']; $ii ++)
  2500. {
  2501. $return .= str_replace("\r\n",' ',html_entity_decode(strip_tags($row['survey_question']))).';';
  2502. }
  2503. }
  2504. }
  2505. }
  2506. }
  2507. $return .= "\n";
  2508. // getting all the questions and options
  2509. $return .= ';';
  2510. //show extra field values
  2511. if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR !empty($_POST['fields_filter']))
  2512. {
  2513. //show the fields names for user fields
  2514. foreach($extra_user_fields as $field)
  2515. {
  2516. $return .= '"'.str_replace("\r\n",' ',html_entity_decode(strip_tags($field[3]))).'";';
  2517. }
  2518. }
  2519. $sql = "SELECT survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type,
  2520. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  2521. FROM $table_survey_question survey_question
  2522. LEFT JOIN $table_survey_question_option survey_question_option
  2523. ON survey_question.question_id = survey_question_option.question_id
  2524. WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  2525. ORDER BY survey_question.sort ASC, survey_question_option.sort ASC";
  2526. $result = api_sql_query($sql, __FILE__, __LINE__);
  2527. $possible_answers = array();
  2528. $possible_answers_type = array();
  2529. while ($row = Database::fetch_array($result))
  2530. {
  2531. // we show the options if
  2532. // 1. there is no question filter and the export button has not been clicked
  2533. // 2. there is a quesiton filter but the question is selected for display
  2534. if (!($_POST['submit_question_filter']) OR (is_array($_POST['questions_filter']) && in_array($row['question_id'], $_POST['questions_filter'])))
  2535. {
  2536. // we do not show comment and pagebreak question types
  2537. if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
  2538. {
  2539. $return .= html_entity_decode(strip_tags($row['option_text'])).';';
  2540. $possible_answers[$row['question_id']][$row['question_option_id']] =$row['question_option_id'];
  2541. $possible_answers_type[$row['question_id']] = $row['type'];
  2542. }
  2543. }
  2544. }
  2545. $return .= "\n";
  2546. // getting all the answers of the users
  2547. $old_user='';
  2548. $answers_of_user = array();
  2549. $sql = "SELECT * FROM $table_survey_answer WHERE survey_id='".Database::escape_string($_GET['survey_id'])."' ORDER BY user ASC";
  2550. $open_question_iterator = 1;
  2551. $result = api_sql_query($sql, __FILE__, __LINE__);
  2552. while ($row = Database::fetch_array($result))
  2553. {
  2554. if ($old_user <> $row['user'] AND $old_user <> '')
  2555. {
  2556. $return .= SurveyUtil::export_complete_report_row($possible_answers, $answers_of_user, $old_user, $display_extra_user_fields);
  2557. $answers_of_user=array();
  2558. }
  2559. if($possible_answers_type[$row['question_id']] == 'open')
  2560. {
  2561. $temp_id = 'open'.$open_question_iterator;
  2562. $answers_of_user[$row['question_id']][$temp_id] = $row;
  2563. $open_question_iterator++;
  2564. }
  2565. else
  2566. {
  2567. $answers_of_user[$row['question_id']][$row['option_id']] = $row;
  2568. }
  2569. $old_user = $row['user'];
  2570. }
  2571. $return .= SurveyUtil::export_complete_report_row($possible_answers, $answers_of_user, $old_user, $display_extra_user_fields); // this is to display the last user
  2572. return $return;
  2573. }
  2574. /**
  2575. * Add a line to the csv file
  2576. *
  2577. * @param array Possible answers
  2578. * @param array User's answers
  2579. * @param mixed User ID or user details as string - Used as a string in the result string
  2580. * @param boolean Whether to display user fields or not
  2581. * @return string One line of the csv file
  2582. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2583. * @version February 2007
  2584. */
  2585. function export_complete_report_row($possible_options, $answers_of_user, $user, $display_extra_user_fields=false)
  2586. {
  2587. $return = $user.';'; // the user column
  2588. if($display_extra_user_fields)
  2589. {
  2590. //show user fields data, if any, for this user
  2591. $user_fields_values = UserManager::get_extra_user_data(intval($user),false,false);
  2592. foreach($user_fields_values as $value)
  2593. {
  2594. $return .= '"'.str_replace('"','""',html_entity_decode(strip_tags($value))).'";';
  2595. }
  2596. }
  2597. if(is_array($possible_options))
  2598. {
  2599. foreach ($possible_options as $question_id=>$possible_option)
  2600. {
  2601. if(is_array($possible_option) && count($possible_option)>0)
  2602. {
  2603. foreach ($possible_option as $option_id=>$value)
  2604. {
  2605. $key = array_keys($answers_of_user[$question_id]);
  2606. if(substr($key[0],0,4)=='open')
  2607. {
  2608. $return .= '"'.str_replace('"','""',html_entity_decode(strip_tags($answers_of_user[$question_id][$key[0]]['option_id']))).'"';
  2609. }
  2610. elseif (!empty($answers_of_user[$question_id][$option_id]))
  2611. {
  2612. $return .= 'v';
  2613. }
  2614. $return .= ';';
  2615. }
  2616. }
  2617. }
  2618. }
  2619. $return .= "\n";
  2620. return $return;
  2621. }
  2622. /**
  2623. * Quite similar to display_complete_report(), returns an HTML string
  2624. * that can be used in a csv file
  2625. * @todo consider merging this function with display_complete_report
  2626. * @return string The contents of a csv file
  2627. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2628. * @version February 2007
  2629. */
  2630. function export_complete_report_xls($filename)
  2631. {
  2632. require_once(api_get_path(LIBRARY_PATH).'pear/Spreadsheet_Excel_Writer/Writer.php');
  2633. $workbook = new Spreadsheet_Excel_Writer();
  2634. $workbook->send($filename);
  2635. $worksheet =& $workbook->addWorksheet('Report 1');
  2636. $line = 0;
  2637. $column = 1; //skip the first column (row titles)
  2638. //show extra fields blank space (enough for extra fields on next line)
  2639. $display_extra_user_fields = false;
  2640. if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR !empty($_POST['fields_filter']))
  2641. {
  2642. //show user fields section with a big th colspan that spans over all fields
  2643. $extra_user_fields = UserManager::get_extra_fields(0,0,5,'ASC',false);
  2644. $num = count($extra_user_fields);
  2645. for($i=0;$i<$num;$i++)
  2646. {
  2647. $worksheet->write($line,$column,'');
  2648. $column ++;
  2649. }
  2650. $display_extra_user_fields = true;
  2651. }
  2652. // Database table definitions
  2653. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  2654. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  2655. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  2656. // First line (questions)
  2657. $sql = "SELECT questions.question_id, questions.type, questions.survey_question, count(options.question_option_id) as number_of_options
  2658. FROM $table_survey_question questions LEFT JOIN $table_survey_question_option options
  2659. ON questions.question_id = options.question_id "
  2660. ." AND questions.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  2661. GROUP BY questions.question_id "
  2662. ." ORDER BY questions.sort ASC";
  2663. $result = api_sql_query($sql, __FILE__, __LINE__);
  2664. while ($row = Database::fetch_array($result))
  2665. {
  2666. // we show the questions if
  2667. // 1. there is no question filter and the export button has not been clicked
  2668. // 2. there is a quesiton filter but the question is selected for display
  2669. if (!($_POST['submit_question_filter']) OR (is_array($_POST['questions_filter']) && in_array($row['question_id'], $_POST['questions_filter'])))
  2670. {
  2671. // we do not show comment and pagebreak question types
  2672. if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
  2673. {
  2674. if($row['number_of_options'] == 0 && $row['type']=='open')
  2675. {
  2676. $worksheet->write($line,$column,html_entity_decode(strip_tags($row['survey_question'])));
  2677. $column ++;
  2678. }
  2679. else
  2680. {
  2681. for ($ii = 0; $ii < $row['number_of_options']; $ii ++)
  2682. {
  2683. $worksheet->write($line,$column,html_entity_decode(strip_tags($row['survey_question'])));
  2684. $column ++;
  2685. }
  2686. }
  2687. }
  2688. }
  2689. }
  2690. $line++;
  2691. $column = 1;
  2692. //show extra field values
  2693. if ($display_extra_user_fields)
  2694. {
  2695. //show the fields names for user fields
  2696. foreach($extra_user_fields as $field)
  2697. {
  2698. $worksheet->write($line,$column,html_entity_decode(strip_tags($field[3])));
  2699. $column++;
  2700. }
  2701. }
  2702. // getting all the questions and options (second line)
  2703. $sql = "SELECT survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type,
  2704. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  2705. FROM $table_survey_question survey_question
  2706. LEFT JOIN $table_survey_question_option survey_question_option
  2707. ON survey_question.question_id = survey_question_option.question_id
  2708. WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  2709. ORDER BY survey_question.sort ASC, survey_question_option.sort ASC";
  2710. $result = api_sql_query($sql, __FILE__, __LINE__);
  2711. $possible_answers = array();
  2712. $possible_answers_type = array();
  2713. while ($row = Database::fetch_array($result))
  2714. {
  2715. // we show the options if
  2716. // 1. there is no question filter and the export button has not been clicked
  2717. // 2. there is a quesiton filter but the question is selected for display
  2718. if (!($_POST['submit_question_filter']) OR (is_array($_POST['questions_filter']) && in_array($row['question_id'], $_POST['questions_filter'])))
  2719. {
  2720. // we do not show comment and pagebreak question types
  2721. if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
  2722. {
  2723. $worksheet->write($line,$column,html_entity_decode(strip_tags($row['option_text'])));
  2724. $possible_answers[$row['question_id']][$row['question_option_id']] =$row['question_option_id'];
  2725. $possible_answers_type[$row['question_id']] = $row['type'];
  2726. $column++;
  2727. }
  2728. }
  2729. }
  2730. // getting all the answers of the users
  2731. $line ++;
  2732. $column = 0;
  2733. $old_user='';
  2734. $answers_of_user = array();
  2735. $sql = "SELECT * FROM $table_survey_answer " .
  2736. "WHERE survey_id='".Database::escape_string($_GET['survey_id'])."' " .
  2737. "ORDER BY user ASC";
  2738. $open_question_iterator = 1;
  2739. $result = api_sql_query($sql, __FILE__, __LINE__);
  2740. while ($row = Database::fetch_array($result))
  2741. {
  2742. if ($old_user <> $row['user'] AND $old_user <> '')
  2743. {
  2744. $return = SurveyUtil::export_complete_report_row_xls($possible_answers, $answers_of_user, $old_user, $display_extra_user_fields);
  2745. foreach($return as $elem)
  2746. {
  2747. $worksheet->write($line,$column,$elem);
  2748. $column++;
  2749. }
  2750. $answers_of_user=array();
  2751. $line++;
  2752. $column=0;
  2753. }
  2754. if($possible_answers_type[$row['question_id']] == 'open')
  2755. {
  2756. $temp_id = 'open'.$open_question_iterator;
  2757. $answers_of_user[$row['question_id']][$temp_id] = $row;
  2758. $open_question_iterator++;
  2759. }
  2760. else
  2761. {
  2762. $answers_of_user[$row['question_id']][$row['option_id']] = $row;
  2763. }
  2764. $old_user = $row['user'];
  2765. }
  2766. $return = SurveyUtil::export_complete_report_row_xls($possible_answers, $answers_of_user, $old_user); // this is to display the last user
  2767. foreach($return as $elem)
  2768. {
  2769. $worksheet->write($line,$column,$elem);
  2770. $column++;
  2771. }
  2772. $workbook->close();
  2773. return null;
  2774. }
  2775. /**
  2776. * Add a line to the csv file
  2777. *
  2778. * @param array Possible answers
  2779. * @param array User's answers
  2780. * @param mixed User ID or user details as string - Used as a string in the result string
  2781. * @param boolean Whether to display user fields or not
  2782. * @return string One line of the csv file
  2783. */
  2784. function export_complete_report_row_xls($possible_options, $answers_of_user, $user, $display_extra_user_fields=false)
  2785. {
  2786. $return = array();
  2787. $return[] = $user; // the user column
  2788. if($display_extra_user_fields)
  2789. {
  2790. //show user fields data, if any, for this user
  2791. $user_fields_values = UserManager::get_extra_user_data(intval($user),false,false);
  2792. foreach($user_fields_values as $value)
  2793. {
  2794. $return[] = html_entity_decode(strip_tags($value));
  2795. }
  2796. }
  2797. if(is_array($possible_options))
  2798. {
  2799. foreach ($possible_options as $question_id=>$possible_option)
  2800. {
  2801. if(is_array($possible_option) && count($possible_option)>0)
  2802. {
  2803. foreach ($possible_option as $option_id=>$value)
  2804. {
  2805. $key = array_keys($answers_of_user[$question_id]);
  2806. if(substr($key[0],0,4)=='open')
  2807. {
  2808. $return[] = html_entity_decode(strip_tags($answers_of_user[$question_id][$key[0]]['option_id']));
  2809. }
  2810. elseif (!empty($answers_of_user[$question_id][$option_id]))
  2811. {
  2812. $return[] = 'v';
  2813. }
  2814. else
  2815. {
  2816. $return[] = '';
  2817. }
  2818. }
  2819. }
  2820. }
  2821. }
  2822. return $return;
  2823. }
  2824. /**
  2825. * This function displays the comparative report which allows you to compare two questions
  2826. * A comparative report creates a table where one question is on the x axis and a second question is on the y axis.
  2827. * In the intersection is the number of people who have answerd positive on both options.
  2828. *
  2829. * @return string HTML code
  2830. *
  2831. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2832. * @version February 2007
  2833. */
  2834. function display_comparative_report()
  2835. {
  2836. // allowed question types for comparative report
  2837. $allowed_question_types = array('yesno', 'multiplechoice', 'multipleresponse', 'dropdown', 'percentage', 'score');
  2838. // getting all the questions
  2839. $questions = survey_manager::get_questions($_GET['survey_id']);
  2840. // displaying an information message that only the questions with predefined answers can be used in a comparative report
  2841. Display::display_normal_message(get_lang('OnlyQuestionsWithPredefinedAnswers'), false);
  2842. // The form for selecting the axis of the table
  2843. echo '<form id="form1" name="form1" method="get" action="'.api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&survey_id='.Security::remove_XSS($_GET['survey_id']).'&xaxis='.Security::remove_XSS($_GET['xaxis']).'&y='.Security::remove_XSS($_GET['yaxis']).'">';
  2844. // survey_id
  2845. echo '<input type="hidden" name="action" value="'.Security::remove_XSS($_GET['action']).'"/>';
  2846. echo '<input type="hidden" name="survey_id" value="'.(int)$_GET['survey_id'].'"/>';
  2847. // X axis
  2848. echo get_lang('SelectXAxis').': ';
  2849. echo '<select name="xaxis">';
  2850. echo '<option value="">---</option>';
  2851. foreach ($questions as $key=>$question)
  2852. {
  2853. if (in_array($question['type'], $allowed_question_types))
  2854. {
  2855. echo '<option value="'.$question['question_id'].'"';
  2856. if ($_GET['xaxis'] == $question['question_id'])
  2857. {
  2858. echo ' selected="selected"';
  2859. }
  2860. echo '">'.substr(strip_tags($question['question']), 0, 50).'</option>';
  2861. }
  2862. }
  2863. echo '</select><br /><br />';
  2864. // Y axis
  2865. echo get_lang('SelectYAxis').': ';
  2866. echo '<select name="yaxis">';
  2867. echo '<option value="">---</option>';
  2868. foreach ($questions as $key=>$question)
  2869. {
  2870. if (in_array($question['type'], $allowed_question_types))
  2871. {
  2872. echo '<option value="'.$question['question_id'].'"';
  2873. if ($_GET['yaxis'] == $question['question_id'])
  2874. {
  2875. echo ' selected="selected"';
  2876. }
  2877. echo '">'.substr(strip_tags($question['question']), 0, 50).'</option>';
  2878. }
  2879. }
  2880. echo '</select><br /><br />';
  2881. echo '<input type="submit" name="Submit" value="Submit" />';
  2882. echo '</form>';
  2883. // getting all the information of the x axis
  2884. if (isset($_GET['xaxis']) AND is_numeric($_GET['xaxis']))
  2885. {
  2886. $question_x = survey_manager::get_question($_GET['xaxis']);
  2887. }
  2888. // getting all the information of the y axis
  2889. if (isset($_GET['yaxis']) AND is_numeric($_GET['yaxis']))
  2890. {
  2891. $question_y = survey_manager::get_question($_GET['yaxis']);
  2892. }
  2893. if (isset($_GET['xaxis']) AND is_numeric($_GET['xaxis']) AND isset($_GET['yaxis']) AND is_numeric($_GET['yaxis']))
  2894. {
  2895. // getting the answers of the two questions
  2896. $answers_x = SurveyUtil::get_answers_of_question_by_user($_GET['survey_id'], $_GET['xaxis']);
  2897. $answers_y = SurveyUtil::get_answers_of_question_by_user($_GET['survey_id'], $_GET['yaxis']);
  2898. // displaying the table
  2899. echo '<table border="1" class="data_table">';
  2900. // the header
  2901. echo ' <tr>';
  2902. for ($ii=0; $ii<=count($question_x['answers']); $ii++)
  2903. {
  2904. if ($ii == 0)
  2905. {
  2906. echo ' <th>&nbsp;</th>';
  2907. }
  2908. else
  2909. {
  2910. if ($question_x['type']=='score')
  2911. {
  2912. for($x=1; $x<=$question_x['maximum_score']; $x++)
  2913. {
  2914. echo ' <th>'.$question_x['answers'][($ii-1)].'<br />'.$x.'</th>';
  2915. }
  2916. $x='';
  2917. }
  2918. else
  2919. {
  2920. echo ' <th>'.$question_x['answers'][($ii-1)].'</th>';
  2921. }
  2922. }
  2923. }
  2924. echo ' </tr>';
  2925. // the main part
  2926. for ($ij=0; $ij<count($question_y['answers']); $ij++)
  2927. {
  2928. // The Y axis is a scoring question type so we have more rows than the options (actually options * maximum score)
  2929. if ($question_y['type'] == 'score')
  2930. {
  2931. for($y=1; $y<=$question_y['maximum_score']; $y++)
  2932. {
  2933. echo ' <tr>';
  2934. for ($ii=0; $ii<=count($question_x['answers']); $ii++)
  2935. {
  2936. if ($question_x['type']=='score')
  2937. {
  2938. for($x=1; $x<=$question_x['maximum_score']; $x++)
  2939. {
  2940. if ($ii == 0)
  2941. {
  2942. echo ' <th>'.$question_y['answers'][($ij)].' '.$y.'</th>';
  2943. break;
  2944. }
  2945. else
  2946. {
  2947. echo ' <td align="center">';
  2948. echo SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], $x, $y);
  2949. echo '</td>';
  2950. }
  2951. }
  2952. }
  2953. else
  2954. {
  2955. if ($ii == 0)
  2956. {
  2957. echo ' <th>'.$question_y['answers'][($ij)].' '.$y.'</th>';
  2958. }
  2959. else
  2960. {
  2961. echo ' <td align="center">';
  2962. echo SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], 0, $y);
  2963. echo '</td>';
  2964. }
  2965. }
  2966. }
  2967. echo ' </tr>';
  2968. }
  2969. }
  2970. // The Y axis is NOT a score question type so the number of rows = the number of options
  2971. else
  2972. {
  2973. echo ' <tr>';
  2974. for ($ii=0; $ii<=count($question_x['answers']); $ii++)
  2975. {
  2976. if ($question_x['type']=='score')
  2977. {
  2978. for($x=1; $x<=$question_x['maximum_score']; $x++)
  2979. {
  2980. if ($ii == 0)
  2981. {
  2982. echo ' <th>'.$question_y['answers'][($ij)].'</th>';
  2983. break;
  2984. }
  2985. else
  2986. {
  2987. echo ' <td align="center">';
  2988. echo SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], $x, 0);
  2989. echo '</td>';
  2990. }
  2991. }
  2992. }
  2993. else
  2994. {
  2995. if ($ii == 0)
  2996. {
  2997. echo ' <th>'.$question_y['answers'][($ij)].'</th>';
  2998. }
  2999. else
  3000. {
  3001. echo ' <td align="center">';
  3002. echo SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)]);
  3003. echo '</td>';
  3004. }
  3005. }
  3006. }
  3007. echo ' </tr>';
  3008. }
  3009. }
  3010. echo '</table>';
  3011. }
  3012. }
  3013. /**
  3014. * Get all the answers of a question grouped by user
  3015. *
  3016. * @param integer Survey ID
  3017. * @param integer Question ID
  3018. * @return Array Array containing all answers of all users, grouped by user
  3019. *
  3020. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3021. * @version February 2007 - Updated March 2008
  3022. */
  3023. function get_answers_of_question_by_user($survey_id, $question_id)
  3024. {
  3025. // Database table definitions
  3026. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  3027. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  3028. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  3029. $sql = "SELECT * FROM $table_survey_answer
  3030. WHERE survey_id='".Database::escape_string($survey_id)."'
  3031. AND question_id='".Database::escape_string($question_id)."'
  3032. ORDER BY USER ASC";
  3033. $result = api_sql_query($sql, __FILE__, __LINE__);
  3034. while ($row = Database::fetch_array($result))
  3035. {
  3036. if ($row['value'] == 0)
  3037. {
  3038. $return[$row['user']][] = $row['option_id'];
  3039. }
  3040. else
  3041. {
  3042. $return[$row['user']][] = $row['option_id'].'*'.$row['value'];
  3043. }
  3044. }
  3045. return $return;
  3046. }
  3047. /**
  3048. * Count the number of users who answer positively on both options
  3049. *
  3050. * @param array All answers of the x axis
  3051. * @param array All answers of the y axis
  3052. * @param integer x axis value (= the option_id of the first question)
  3053. * @param integer y axis value (= the option_id of the second question)
  3054. * @return integer Number of users who have answered positively to both options
  3055. *
  3056. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3057. * @version February 2007
  3058. */
  3059. function comparative_check($answers_x, $answers_y, $option_x, $option_y, $value_x=0, $value_y=0)
  3060. {
  3061. if ($value_x==0)
  3062. {
  3063. $check_x = $option_x;
  3064. }
  3065. else
  3066. {
  3067. $check_x = $option_x.'*'.$value_x;
  3068. }
  3069. if ($value_y==0)
  3070. {
  3071. $check_y = $option_y;
  3072. }
  3073. else
  3074. {
  3075. $check_y = $option_y.'*'.$value_y;
  3076. }
  3077. $counter = 0;
  3078. foreach ($answers_x as $user => $answers)
  3079. {
  3080. // check if the user has given $option_x as answer
  3081. if (in_array($check_x, $answers))
  3082. {
  3083. // check if the user has given $option_y as an answer
  3084. if (in_array($check_y, $answers_y[$user]))
  3085. {
  3086. $counter++;
  3087. }
  3088. }
  3089. }
  3090. return $counter;
  3091. }
  3092. /**
  3093. * Get all the information about the invitations of a certain survey
  3094. *
  3095. * @return array Lines of invitation [user, code, date, empty element]
  3096. *
  3097. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3098. * @version January 2007
  3099. *
  3100. * @todo use survey_id parameter instead of $_GET
  3101. */
  3102. function get_survey_invitations_data()
  3103. {
  3104. // Database table definition
  3105. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  3106. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  3107. $sql = "SELECT
  3108. survey_invitation.user as col1,
  3109. survey_invitation.invitation_code as col2,
  3110. survey_invitation.invitation_date as col3,
  3111. '' as col4
  3112. FROM $table_survey_invitation survey_invitation
  3113. LEFT JOIN $table_user user ON survey_invitation.user = user.user_id
  3114. WHERE survey_invitation.survey_id = '".Database::escape_string($_GET['survey_id'])."'";
  3115. $res = api_sql_query($sql, __FILE__, __LINE__);
  3116. while ($row = Database::fetch_array($res))
  3117. {
  3118. $survey_invitation_data[] = $row;
  3119. }
  3120. return $survey_invitation_data;
  3121. }
  3122. /**
  3123. * Get the total number of survey invitations for a given survey (through $_GET['survey_id'])
  3124. *
  3125. * @return integer Total number of survey invitations
  3126. *
  3127. * @todo use survey_id parameter instead of $_GET
  3128. *
  3129. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3130. * @version January 2007
  3131. */
  3132. function get_number_of_survey_invitations()
  3133. {
  3134. // Database table definition
  3135. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  3136. $sql = "SELECT count(user) AS total FROM $table_survey_invitation WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'";
  3137. $res = api_sql_query($sql, __FILE__, __LINE__);
  3138. $row = Database::fetch_array($res,'ASSOC');
  3139. return $row['total'];
  3140. }
  3141. /**
  3142. * Save the invitation mail
  3143. *
  3144. * @param string Text of the e-mail
  3145. * @param integer Whether the mail contents are for invite mail (0, default) or reminder mail (1)
  3146. *
  3147. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3148. * @version January 2007
  3149. */
  3150. function save_invite_mail($mailtext, $reminder=0)
  3151. {
  3152. // Database table definition
  3153. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  3154. // reminder or not
  3155. if ($reminder == 0)
  3156. {
  3157. $mail_field = 'invite_mail';
  3158. }
  3159. else
  3160. {
  3161. $mail_field = 'reminder_mail';
  3162. }
  3163. $sql = "UPDATE $table_survey SET $mail_field = '".Database::escape_string($mailtext)."' WHERE survey_id = '".Database::escape_string($_GET['survey_id'])."'";
  3164. $result = api_sql_query($sql, __FILE__, __LINE__);
  3165. }
  3166. /**
  3167. * This function saves all the invitations of course users and additional users in the
  3168. *
  3169. * @param array Users array
  3170. * @param string Title of the invitation, used as the title of the mail
  3171. * @param string Text of the invitation, used as the text of the mail.
  3172. * The text has to contain a **link** string or this will automatically be added to the end
  3173. *
  3174. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3175. * @version January 2007
  3176. *
  3177. * @todo create the survey link
  3178. */
  3179. function save_invitations($users_array, $invitation_title, $invitation_text, $reminder=0, $sendmail = 0)
  3180. {
  3181. global $_user;
  3182. global $_course;
  3183. global $_configuration;
  3184. // getting the survey information
  3185. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  3186. // Database table definition
  3187. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  3188. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  3189. $already_invited = array();
  3190. $survey_invitations = array();
  3191. // get the people who are already invited
  3192. if ($reminder == 1)
  3193. {
  3194. $survey_invitations = SurveyUtil::get_invitations($survey_data['survey_code']);
  3195. }
  3196. else
  3197. {
  3198. $already_invited = SurveyUtil::get_invited_users($survey_data['code']);
  3199. }
  3200. $counter = 0;
  3201. if (is_array($users_array))
  3202. {
  3203. foreach ($users_array as $key=>$value)
  3204. {
  3205. if(isset($value))
  3206. {
  3207. // generating the unique code
  3208. if(is_array($survey_invitations))
  3209. {
  3210. if ($reminder == 1 AND array_key_exists($value,$survey_invitations))
  3211. {
  3212. $invitation_code = $survey_invitations[$value]['invitation_code'];
  3213. }
  3214. else
  3215. {
  3216. $invitation_code = md5($value.microtime());
  3217. }
  3218. }
  3219. $survey_link = '';
  3220. $full_invitation_text= '';
  3221. // storing the invitation (only if the user_id is not in $already_invited['course_users'] OR email is not in $already_invited['additional_users']
  3222. $add_users_array = explode(';',$already_invited['additional_users']);
  3223. if ((is_numeric($value) AND !in_array($value,$already_invited['course_users'])) OR (!is_numeric($value) AND !in_array($value,$add_users_array)))
  3224. {
  3225. if(is_array($survey_invitations))
  3226. {
  3227. if (!array_key_exists($value,$survey_invitations))
  3228. {
  3229. $sql = "INSERT INTO $table_survey_invitation (user, survey_code, invitation_code, invitation_date) VALUES
  3230. ('".Database::escape_string($value)."','".Database::escape_string($survey_data['code'])."','".Database::escape_string($invitation_code)."','".Database::escape_string(date('Y-m-d H:i:s'))."')";
  3231. $result = api_sql_query($sql, __FILE__, __LINE__);
  3232. }
  3233. }
  3234. // replacing the **link** part with a valid link for the user
  3235. $survey_link = $_configuration['root_web'].$_configuration['code_append'].'survey/'.'fillsurvey.php?course='.$_course['sysCode'].'&invitationcode='.$invitation_code;
  3236. $text_link = '<a href="'.$survey_link.'">'.get_lang('ClickHereToAnswerTheSurvey')."</a><br />\r\n<br />\r\n".get_lang('OrCopyPasteTheFollowingUrl')." <br />\r\n ".$survey_link;
  3237. $replace_count = 0;
  3238. $full_invitation_text = str_ireplace('**link**', $text_link ,$invitation_text, $replace_count);
  3239. if ($replace_count < 1)
  3240. {
  3241. $full_invitation_text = $full_invitation_text . "<br />\r\n<br />\r\n".$text_link;
  3242. }
  3243. // optionally: finding the e-mail of the course user
  3244. if (is_numeric($value))
  3245. {
  3246. $sql = "SELECT firstname, lastname, email FROM $table_user WHERE user_id='".Database::escape_string($value)."'";
  3247. $result = api_sql_query($sql, __FILE__, __LINE__);
  3248. $row = Database::fetch_array($result);
  3249. $recipient_email = $row['email'];
  3250. $recipient_name = $row['firstname'].' '.$row['lastname'];
  3251. }
  3252. else
  3253. {
  3254. /** @todo check if the $value is a valid email */
  3255. $recipient_email = $value;
  3256. }
  3257. // sending the mail
  3258. $sender_name = $_user['firstName'].' '.$_user['lastName'];
  3259. $sender_email = $_user['mail'];
  3260. $replyto = array();
  3261. if(api_get_setting('survey_email_sender_noreply')=='noreply')
  3262. {
  3263. $noreply = api_get_setting('noreply_email_address');
  3264. if(!empty($noreply))
  3265. {
  3266. $replyto['Reply-to'] = $noreply;
  3267. $sender_name = $noreply;
  3268. $sender_email = $noreply;
  3269. }
  3270. }
  3271. if ($sendmail<>0)
  3272. {
  3273. api_mail_html($recipient_name, $recipient_email, $invitation_title, $full_invitation_text, $sender_name, $sender_email, $replyto);
  3274. }
  3275. $counter++;
  3276. }
  3277. }
  3278. }
  3279. }
  3280. return $counter;
  3281. }
  3282. /**
  3283. * This function recalculates the number of users who have been invited and updates the survey table with this value.
  3284. *
  3285. * @param string Survey code
  3286. * @return void
  3287. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3288. * @version January 2007
  3289. */
  3290. function update_count_invited($survey_code)
  3291. {
  3292. // Database table definition
  3293. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  3294. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  3295. // counting the number of people that are invited
  3296. $sql = "SELECT count(user) as total FROM $table_survey_invitation WHERE survey_code = '".Database::escape_string($survey_code)."'";
  3297. $result = api_sql_query($sql, __FILE__, __LINE__);
  3298. $row = Database::fetch_array($result);
  3299. $total_invited = $row['total'];
  3300. // updating the field in the survey table
  3301. $sql = "UPDATE $table_survey SET invited = '".Database::escape_string($total_invited)."' WHERE code = '".Database::escape_string($survey_code)."'";
  3302. $result = api_sql_query($sql, __FILE__, __LINE__);
  3303. }
  3304. /**
  3305. * This function gets all the invited users for a given survey code.
  3306. *
  3307. * @param string Survey code
  3308. * @return array Array containing the course users and additional users (non course users)
  3309. *
  3310. * @todo consider making $defaults['additional_users'] also an array
  3311. *
  3312. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3313. * @version January 2007
  3314. */
  3315. function get_invited_users($survey_code)
  3316. {
  3317. // Database table definition
  3318. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  3319. // Selecting all the invitations of this survey
  3320. $sql = "SELECT user FROM $table_survey_invitation WHERE survey_code='".Database::escape_string($survey_code)."'";
  3321. $defaults = array();
  3322. $defaults['course_users'] = array();
  3323. $defaults['additional_users'] = '';
  3324. $result = api_sql_query($sql, __FILE__, __LINE__);
  3325. while ($row = Database::fetch_array($result))
  3326. {
  3327. if (is_numeric($row['user']))
  3328. {
  3329. $defaults['course_users'][] = $row['user'];
  3330. }
  3331. else
  3332. {
  3333. if (empty($defaults['additional_users']))
  3334. {
  3335. $defaults['additional_users'] = $row['user'];
  3336. }
  3337. else
  3338. {
  3339. $defaults['additional_users'] .= ';'.$row['user'];
  3340. }
  3341. }
  3342. }
  3343. return $defaults;
  3344. }
  3345. /**
  3346. * Get all the invitations
  3347. *
  3348. * @param string Survey code
  3349. * @return array Database rows matching the survey code
  3350. *
  3351. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3352. * @version September 2007
  3353. */
  3354. function get_invitations($survey_code)
  3355. {
  3356. // Database table definition
  3357. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  3358. $sql = "SELECT * FROM $table_survey_invitation WHERE survey_code = '".Database::escape_string($survey_code)."'";
  3359. $result = api_sql_query($sql, __FILE__, __LINE__);
  3360. $return = array();
  3361. while ($row = Database::fetch_array($result))
  3362. {
  3363. $return[$row['user']] = $row;
  3364. }
  3365. return $return;
  3366. }
  3367. /**
  3368. * This function displays the form for searching a survey
  3369. *
  3370. * @return void (direct output)
  3371. *
  3372. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3373. * @version January 2007
  3374. *
  3375. * @todo use quickforms
  3376. * @todo consider moving this to surveymanager.inc.lib.php
  3377. */
  3378. function display_survey_search_form()
  3379. {
  3380. echo '<form method="get" action="survey_list.php?search=advanced">';
  3381. echo '<table>
  3382. <tr>
  3383. <td>'.get_lang('Title').'</td>
  3384. <td><input type="text" name="keyword_title"/></td>
  3385. </tr>
  3386. <tr>
  3387. <td>'.get_lang('Code').'</td>
  3388. <td><input type="text" name="keyword_code"/></td>
  3389. </tr>
  3390. <tr>
  3391. <td>'.get_lang('Language').'</td>
  3392. <td>
  3393. <select name="keyword_language"><option value="%">'.get_lang('All').'</option>';
  3394. $languages = api_get_languages();
  3395. foreach ($languages['name'] as $index => $name)
  3396. {
  3397. echo '<option value="'.$languages['folder'][$index].'">'.$name.'</option>';
  3398. }
  3399. echo ' </select>
  3400. </td>
  3401. </tr>
  3402. <tr>
  3403. <td>&nbsp;</td>
  3404. <td><input type="submit" name="do_search" value="'.get_lang('Ok').'"/></td>
  3405. </tr>
  3406. </table>
  3407. </form>';
  3408. }
  3409. /**
  3410. * This function displays the sortable table with all the surveys
  3411. *
  3412. * @return void (direct output)
  3413. *
  3414. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3415. * @version January 2007
  3416. */
  3417. function display_survey_list()
  3418. {
  3419. $parameters = array();
  3420. if ($_GET['do_search'])
  3421. {
  3422. $message = get_lang('DisplaySearchResults').'<br />';
  3423. $message .= '<a href="'.api_get_self().'">'.get_lang('DisplayAll').'</a>';
  3424. Display::display_normal_message($message, false);
  3425. }
  3426. // Create a sortable table with survey-data
  3427. $table = new SortableTable('surveys', 'get_number_of_surveys', 'get_survey_data',2);
  3428. $table->set_additional_parameters($parameters);
  3429. $table->set_header(0, '', false);
  3430. $table->set_header(1, get_lang('SurveyName'));
  3431. $table->set_header(2, get_lang('SurveyCode'));
  3432. $table->set_header(3, get_lang('NumberOfQuestions'));
  3433. $table->set_header(4, get_lang('Author'));
  3434. $table->set_header(5, get_lang('Language'));
  3435. $table->set_header(6, get_lang('Shared'));
  3436. $table->set_header(7, get_lang('AvailableFrom'));
  3437. $table->set_header(8, get_lang('AvailableUntill'));
  3438. $table->set_header(9, get_lang('Invite'));
  3439. $table->set_header(10, get_lang('Anonymous'));
  3440. $table->set_header(11, get_lang('Modify'), false);
  3441. $table->set_column_filter(10, 'anonymous_filter');
  3442. $table->set_column_filter(11, 'modify_filter');
  3443. $table->set_form_actions(array ('delete' => get_lang('DeleteSurvey')));
  3444. $table->display();
  3445. }
  3446. /**
  3447. * This function changes the modify column of the sortable table
  3448. *
  3449. * @param integer $survey_id the id of the survey
  3450. * @return html code that are the actions that can be performed on any survey
  3451. *
  3452. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3453. * @version January 2007
  3454. */
  3455. function modify_filter($survey_id)
  3456. {
  3457. global $charset;
  3458. $survey_id = Security::remove_XSS($survey_id);
  3459. $return = '<a href="create_new_survey.php?'.api_get_cidreq().'&amp;action=edit&amp;survey_id='.$survey_id.'">'.Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
  3460. $return .= '<a href="survey_list.php?'.api_get_cidreq().'&amp;action=delete&amp;survey_id='.$survey_id.'" onclick="javascript:if(!confirm(\''.addslashes(htmlentities(get_lang("DeleteSurvey").'?',ENT_QUOTES,$charset)).'\')) return false;">'.Display::return_icon('delete.gif', get_lang('Delete')).'</a>';
  3461. //$return .= '<a href="create_survey_in_another_language.php?id_survey='.$survey_id.'">'.Display::return_icon('copy.gif', get_lang('Copy')).'</a>';
  3462. //$return .= '<a href="survey.php?survey_id='.$survey_id.'">'.Display::return_icon('add.gif', get_lang('Add')).'</a>';
  3463. $return .= '<a href="preview.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.Display::return_icon('preview.gif', get_lang('Preview')).'</a>';
  3464. $return .= '<a href="survey_invite.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.Display::return_icon('survey_publish.gif', get_lang('Publish')).'</a>';
  3465. $return .= '<a href="survey_list.php?'.api_get_cidreq().'&amp;action=empty&amp;survey_id='.$survey_id.'" onclick="javascript:if(!confirm(\''.addslashes(htmlentities(get_lang("EmptySurvey").'?')).'\')) return false;">'.Display::return_icon('empty.gif', get_lang('EmptySurvey')).'</a>';
  3466. $return .= '<a href="reporting.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.Display::return_icon('statistics.gif', get_lang('Reporting')).'</a>';
  3467. return $return;
  3468. }
  3469. /**
  3470. * Returns "yes" when given parameter is one, "no" for any other value
  3471. * @param integer Whether anonymous or not
  3472. * @return string "Yes" or "No" in the current language
  3473. */
  3474. function anonymous_filter($anonymous)
  3475. {
  3476. if ($anonymous == 1)
  3477. {
  3478. return get_lang('Yes');
  3479. }
  3480. else
  3481. {
  3482. return get_lang('No');
  3483. }
  3484. }
  3485. /**
  3486. * This function handles the search restriction for the SQL statements
  3487. *
  3488. * @return string Part of a SQL statement or false on error
  3489. *
  3490. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3491. * @version January 2007
  3492. */
  3493. function survey_search_restriction()
  3494. {
  3495. if ($_GET['do_search'])
  3496. {
  3497. if ($_GET['keyword_title']<>'')
  3498. {
  3499. $search_term[] = 'title =\''.Database::escape_string($_GET['keyword_title']).'\'';
  3500. }
  3501. if ($_GET['keyword_code']<>'')
  3502. {
  3503. $search_term[] = 'code =\''.Database::escape_string($_GET['keyword_code']).'\'';
  3504. }
  3505. if ($_GET['keyword_language']<>'%')
  3506. {
  3507. $search_term[] = 'lang =\''.Database::escape_string($_GET['keyword_language']).'\'';
  3508. }
  3509. $search_restriction = implode(' AND ', $search_term);
  3510. return $search_restriction;
  3511. }
  3512. else
  3513. {
  3514. return false;
  3515. }
  3516. }
  3517. /**
  3518. * This function calculates the total number of surveys
  3519. *
  3520. * @return integer Total number of surveys
  3521. *
  3522. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3523. * @version January 2007
  3524. */
  3525. function get_number_of_surveys()
  3526. {
  3527. global $table_survey;
  3528. $search_restriction = SurveyUtil::survey_search_restriction();
  3529. if ($search_restriction)
  3530. {
  3531. $search_restriction = 'WHERE '.$search_restriction;
  3532. }
  3533. $sql = "SELECT count(survey_id) AS total_number_of_items FROM ".$table_survey.' '.$search_restriction;
  3534. $res = api_sql_query($sql, __FILE__, __LINE__);
  3535. $obj = Database::fetch_object($res);
  3536. return $obj->total_number_of_items;
  3537. }
  3538. /**
  3539. * This function gets all the survey data that is to be displayed in the sortable table
  3540. *
  3541. * @param unknown_type $from
  3542. * @param unknown_type $number_of_items
  3543. * @param unknown_type $column
  3544. * @param unknown_type $direction
  3545. * @return unknown
  3546. *
  3547. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3548. * @version January 2007
  3549. */
  3550. function get_survey_data($from, $number_of_items, $column, $direction)
  3551. {
  3552. global $table_survey;
  3553. global $table_user;
  3554. global $table_survey_question;
  3555. // searching
  3556. $search_restriction = SurveyUtil::survey_search_restriction();
  3557. if ($search_restriction)
  3558. {
  3559. $search_restriction = ' AND '.$search_restriction;
  3560. }
  3561. $sql = "SELECT
  3562. survey.survey_id AS col0,
  3563. CONCAT('<a href=\"survey.php?survey_id=',survey.survey_id,'\">',survey.title,'</a>') AS col1,
  3564. survey.code AS col2,
  3565. count(survey_question.question_id) AS col3,
  3566. CONCAT(user.firstname, ' ', user.lastname) AS col4,
  3567. survey.lang AS col5,
  3568. IF(is_shared<>0,'V','-') AS col6,
  3569. survey.avail_from AS col7,
  3570. survey.avail_till AS col8,
  3571. CONCAT('<a href=\"survey_invitation.php?view=answered&amp;survey_id=',survey.survey_id,'\">',survey.answered,'</a> / <a href=\"survey_invitation.php?view=invited&amp;survey_id=',survey.survey_id,'\">',survey.invited, '</a>') AS col9,
  3572. survey.anonymous AS col10,
  3573. survey.survey_id AS col11
  3574. FROM $table_survey survey
  3575. LEFT JOIN $table_survey_question survey_question ON survey.survey_id = survey_question.survey_id
  3576. , $table_user user
  3577. WHERE survey.author = user.user_id
  3578. $search_restriction
  3579. ";
  3580. $sql .= " GROUP BY survey.survey_id";
  3581. $sql .= " ORDER BY col$column $direction ";
  3582. $sql .= " LIMIT $from,$number_of_items";
  3583. $res = api_sql_query($sql, __FILE__, __LINE__);
  3584. $surveys = array ();
  3585. while ($survey = Database::fetch_array($res))
  3586. {
  3587. $surveys[] = $survey;
  3588. }
  3589. return $surveys;
  3590. }
  3591. /**
  3592. * Display all the active surveys for the given course user
  3593. *
  3594. * @param unknown_type $user_id
  3595. *
  3596. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3597. * @version April 2007
  3598. */
  3599. function survey_list_user($user_id)
  3600. {
  3601. global $_course;
  3602. // Database table definitions
  3603. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  3604. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  3605. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  3606. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  3607. $sql = "SELECT * FROM $table_survey survey, $table_survey_invitation survey_invitation
  3608. WHERE survey_invitation.user = '".Database::escape_string($user_id)."'
  3609. AND survey.code = survey_invitation.survey_code
  3610. AND survey.avail_from <= '".date('Y-m-d H:i:s')."'
  3611. AND survey.avail_till >= '".date('Y-m-d H:i:s')."'
  3612. ";
  3613. $result = api_sql_query($sql, __FILE__, __LINE__);
  3614. echo '<table class="data_table">';
  3615. echo '<tr>';
  3616. echo ' <th>'.get_lang('SurveyName').'</th>';
  3617. echo ' <th>'.get_lang('Anonymous').'</th>';
  3618. echo '</tr>';
  3619. $counter = 0;
  3620. while ($row = Database::fetch_array($result,'ASSOC'))
  3621. {
  3622. echo '<tr>';
  3623. if ($row['answered'] == 0)
  3624. {
  3625. echo '<td><a href="fillsurvey.php?course='.$_course['sysCode'].'&amp;invitationcode='.$row['invitation_code'].'&amp;cidReq='.$_course['sysCode'].'">'.$row['title'].'</a></td>';
  3626. }
  3627. else
  3628. {
  3629. echo '<td>'.$row['title'].'</td>';
  3630. }
  3631. echo '<td>';
  3632. echo ($row['anonymous'] == 1)?get_lang('Yes'):get_lang('No');
  3633. echo '</td>';
  3634. echo '</tr>';
  3635. }
  3636. echo '</table>';
  3637. }
  3638. }
  3639. ?>