survey.lib.php 136 KB

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