survey.lib.php 186 KB

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