survey.lib.php 130 KB

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