survey.lib.php 187 KB

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