survey.lib.php 139 KB

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