survey.lib.php 188 KB

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