survey.lib.php 152 KB

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