survey.lib.php 188 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167
  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 void;
  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. foreach ($form_content['options'] as $key=>$value)
  1489. {
  1490. $this->html .= '<label><input name="question'.$form_content['question_id'].'" type="radio" value="'.$key.'"';
  1491. if (is_array($answers))
  1492. {
  1493. if (in_array($key,$answers))
  1494. {
  1495. $this->html .= 'checked="checked"';
  1496. }
  1497. }
  1498. if (substr_count($value,"<p>")==1) {
  1499. $this->html .= '/>'.substr($value,3,(strlen($value)-7)).'</label>';
  1500. if ($form_content['display'] == 'vertical') {
  1501. $this->html .= '<br />';
  1502. }
  1503. } else {
  1504. $this->html .= '/>'.$value.'</label>';
  1505. }
  1506. }
  1507. echo '<div class="survey_question_wrapper">';
  1508. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  1509. echo '<div class="survey_question_options">';
  1510. echo $this->html;
  1511. echo '</div>';
  1512. echo '</div>';
  1513. }
  1514. }
  1515. class multiplechoice extends question
  1516. {
  1517. /**
  1518. * This function creates the form elements for the multiple choice questions
  1519. *
  1520. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1521. * @version January 2007
  1522. */
  1523. function create_form($form_content)
  1524. {
  1525. global $charset;
  1526. $this->html = parent::create_form($form_content);
  1527. // Horizontal or vertical
  1528. $this->html .= ' <div class="row">';
  1529. $this->html .= ' <div class="label">';
  1530. $this->html .= get_lang('DisplayAnswersHorVert');
  1531. $this->html .= ' </div>';
  1532. $this->html .= ' <div class="formw">';
  1533. $this->html .= ' <input name="horizontalvertical" type="radio" value="horizontal" ';
  1534. if (empty($form_content['horizontalvertical']) or $form_content['horizontalvertical'] == 'horizontal') {
  1535. $this->html .= 'checked="checked"';
  1536. }
  1537. $this->html .= '/>'.get_lang('Horizontal').'</label><br />';
  1538. $this->html .= ' <input name="horizontalvertical" type="radio" value="vertical" ';
  1539. if ($form_content['horizontalvertical'] == 'vertical')
  1540. {
  1541. $this->html .= 'checked="checked"';
  1542. }
  1543. $this->html .= ' />'.get_lang('Vertical').'</label>';
  1544. $this->html .= ' </div>';
  1545. $this->html .= ' </div>';
  1546. // The Options
  1547. $this->html .= ' <div class="row">';
  1548. $this->html .= ' <div class="label">';
  1549. $this->html .= get_lang('AnswerOptions');
  1550. $this->html .= ' </div>';
  1551. $this->html .= ' <div class="formw">';
  1552. $total_number_of_answers = count($form_content['answers']);
  1553. $this->html .= ' <table>';
  1554. foreach ($form_content['answers'] as $key=>$value) {
  1555. $this->html .= ' <tr>';
  1556. $this->html .= ' <td align="right"><label for="answers['.$key.']">'.($key+1).'</label></td>';
  1557. //$this->html .= ' <td><input type="text" name="answers['.$key.']" id="answers['.$key.']" value="'.$form_content['answers'][$key].'" /></td>';
  1558. $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>';
  1559. $this->html .= ' <td>';
  1560. if ($key<$total_number_of_answers-1) {
  1561. $this->html .= ' <input type="image" src="../img/down.gif" value="move_down['.$key.']" name="move_down['.$key.']"/>';
  1562. }
  1563. if ($key>0) {
  1564. $this->html .= ' <input type="image" src="../img/up.gif" value="move_up['.$key.']" name="move_up['.$key.']"/>';
  1565. }
  1566. if ($total_number_of_answers> 2) {
  1567. $this->html .= ' <input type="image" src="../img/delete.gif" value="delete_answer['.$key.']" name="delete_answer['.$key.']"/>';
  1568. }
  1569. $this->html .= ' </td>';
  1570. $this->html .= ' </tr>';
  1571. }
  1572. // The buttons for adding or removing
  1573. $this->html .= ' </table>';
  1574. $this->html .= ' </div>';
  1575. $this->html .= ' </div>';
  1576. $this->html .= parent :: add_remove_buttons($form_content);
  1577. }
  1578. /**
  1579. * render the multiple choice question type
  1580. *
  1581. * @param unknown_type $form_content
  1582. *
  1583. * @todo it would make more sense to consider yesno as a special case of multiplechoice and not the other way around
  1584. *
  1585. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1586. * @version January 2007
  1587. */
  1588. function render_question($form_content, $answers=array())
  1589. {
  1590. $question = new yesno();
  1591. $question->render_question($form_content, $answers);
  1592. }
  1593. }
  1594. class personality extends question
  1595. {
  1596. /**
  1597. * This function creates the form elements for the multiple response questions
  1598. *
  1599. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1600. * @version January 2007
  1601. */
  1602. function create_form($form_content)
  1603. {
  1604. $this->html = parent::create_form($form_content);
  1605. $this->html .= ' <tr>';
  1606. $this->html .= ' <td colspan="2"><strong>'.get_lang('DisplayAnswersHorVert').'</strong></td>';
  1607. $this->html .= ' </tr>';
  1608. // Horizontal or vertical
  1609. $this->html .= ' <tr>';
  1610. $this->html .= ' <td align="right" valign="top">&nbsp;</td>';
  1611. $this->html .= ' <td>';
  1612. $this->html .= ' <input name="horizontalvertical" type="radio" value="horizontal" ';
  1613. if (empty($form_content['horizontalvertical']) or $form_content['horizontalvertical'] == 'horizontal') {
  1614. $this->html .= 'checked="checked"';
  1615. }
  1616. $this->html .= '/>'.get_lang('Horizontal').'</label><br />';
  1617. $this->html .= ' <input name="horizontalvertical" type="radio" value="vertical" ';
  1618. if ($form_content['horizontalvertical'] == 'vertical')
  1619. {
  1620. $this->html .= 'checked="checked"';
  1621. }
  1622. $this->html .= ' />'.get_lang('Vertical').'</label>'; $this->html .= ' </td>';
  1623. $this->html .= ' <td>&nbsp;</td>';
  1624. $this->html .= ' </tr>';
  1625. $this->html .=' <tr>
  1626. <td colspan="">&nbsp;</td>
  1627. </tr>';
  1628. // The options
  1629. $this->html .= ' <tr>';
  1630. $this->html .= ' <td colspan="3"><strong>'.get_lang('AnswerOptions').'</strong></td>';
  1631. $this->html .= ' </tr>';
  1632. $total_number_of_answers = count($form_content['answers']);
  1633. $question_values=array();
  1634. // values of question options
  1635. foreach ($form_content['values'] as $key=>$value)
  1636. {
  1637. $question_values [] = '<input size="3" type="text" id="values['.$key.']" name="values['.$key.']" value="'.$value.'" />';
  1638. }
  1639. $count=0;
  1640. foreach ($form_content['answers'] as $key=>$value)
  1641. {
  1642. $this->html .= ' <tr>';
  1643. $this->html .= ' <td align="right"><label for="answers['.$key.']">'.($key+1).'</label></td>';
  1644. //$this->html .= ' <td><input type="text" name="answers['.$key.']" id="answers['.$key.']" value="'.$form_content['answers'][$key].'" /></td>';
  1645. $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>';
  1646. $this->html .= ' <td>';
  1647. if ($total_number_of_answers> 2)
  1648. {
  1649. $this->html .=$question_values[$count];
  1650. }
  1651. if ($key<$total_number_of_answers-1)
  1652. {
  1653. $this->html .= ' <input type="image" src="../img/down.gif" value="move_down['.$key.']" name="move_down['.$key.']"/>';
  1654. }
  1655. if ($key>0)
  1656. {
  1657. $this->html .= ' <input type="image" src="../img/up.gif" value="move_up['.$key.']" name="move_up['.$key.']"/>';
  1658. }
  1659. if ($total_number_of_answers> 2)
  1660. {
  1661. $this->html .= ' <input type="image" src="../img/delete.gif" value="delete_answer['.$key.']" name="delete_answer['.$key.']"/>';
  1662. }
  1663. $this->html .= ' </td>';
  1664. $this->html .= ' </tr>';
  1665. $count++;
  1666. }
  1667. // The buttons for adding or removing
  1668. //$this->html .= parent :: add_remove_buttons($form_content);
  1669. }
  1670. /**
  1671. * Render the multiple response question type
  1672. *
  1673. * @param unknown_type $form_content
  1674. *
  1675. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1676. * @version January 2007
  1677. */
  1678. function render_question($form_content, $answers=array())
  1679. {
  1680. $question = new yesno();
  1681. $question->render_question($form_content, $answers);
  1682. }
  1683. }
  1684. class multipleresponse extends question
  1685. {
  1686. /**
  1687. * This function creates the form elements for the multiple response questions
  1688. *
  1689. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1690. * @version January 2007
  1691. */
  1692. function create_form($form_content)
  1693. {
  1694. global $charset;
  1695. $this->html = parent::create_form($form_content);
  1696. // Horizontal or vertical
  1697. $this->html .= ' <div class="row">';
  1698. $this->html .= ' <div class="label">';
  1699. $this->html .= get_lang('DisplayAnswersHorVert');
  1700. $this->html .= ' </div>';
  1701. $this->html .= ' <div class="formw">';
  1702. $this->html .= ' <input name="horizontalvertical" type="radio" value="horizontal" ';
  1703. if (empty($form_content['horizontalvertical']) or $form_content['horizontalvertical'] == 'horizontal') {
  1704. $this->html .= 'checked="checked"';
  1705. }
  1706. $this->html .= '/>'.get_lang('Horizontal').'</label><br />';
  1707. $this->html .= ' <input name="horizontalvertical" type="radio" value="vertical" ';
  1708. if ($form_content['horizontalvertical'] == 'vertical')
  1709. {
  1710. $this->html .= 'checked="checked"';
  1711. }
  1712. $this->html .= ' />'.get_lang('Vertical').'</label>';
  1713. $this->html .= ' </div>';
  1714. $this->html .= ' </div>';
  1715. // The options
  1716. $this->html .= ' <div class="row">';
  1717. $this->html .= ' <div class="label">';
  1718. $this->html .= get_lang('AnswerOptions');
  1719. $this->html .= ' </div>';
  1720. $this->html .= ' <div class="formw">';
  1721. $total_number_of_answers = count($form_content['answers']);
  1722. $this->html .= ' <table>';
  1723. foreach ($form_content['answers'] as $key=>$value) {
  1724. $this->html .= ' <tr>';
  1725. $this->html .= ' <td align="right"><label for="answers['.$key.']">'.($key+1).'</label></td>';
  1726. //$this->html .= ' <td><input type="text" name="answers['.$key.']" id="answers['.$key.']" value="'.$form_content['answers'][$key].'" /></td>';
  1727. $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>';
  1728. $this->html .= ' <td>';
  1729. if ($key<$total_number_of_answers-1) {
  1730. $this->html .= ' <input type="image" src="../img/down.gif" value="move_down['.$key.']" name="move_down['.$key.']"/>';
  1731. }
  1732. if ($key>0) {
  1733. $this->html .= ' <input type="image" src="../img/up.gif" value="move_up['.$key.']" name="move_up['.$key.']"/>';
  1734. }
  1735. if ($total_number_of_answers> 2) {
  1736. $this->html .= ' <input type="image" src="../img/delete.gif" value="delete_answer['.$key.']" name="delete_answer['.$key.']"/>';
  1737. }
  1738. $this->html .= ' </td>';
  1739. $this->html .= ' </tr>';
  1740. }
  1741. // The buttons for adding or removing
  1742. $this->html .= ' </table>';
  1743. $this->html .= ' </div>';
  1744. $this->html .= ' </div>';
  1745. $this->html .= parent :: add_remove_buttons($form_content);
  1746. }
  1747. /**
  1748. * Render the multiple response question type
  1749. *
  1750. * @param unknown_type $form_content
  1751. *
  1752. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1753. * @version January 2007
  1754. */
  1755. function render_question($form_content, $answers=array())
  1756. {
  1757. foreach ($form_content['options'] as $key=>$value)
  1758. {
  1759. $this->html .= '<label><input name="question'.$form_content['question_id'].'[]" type="checkbox" value="'.$key.'"';
  1760. if (is_array($answers))
  1761. {
  1762. if (in_array($key,$answers))
  1763. {
  1764. $this->html .= 'checked="checked"';
  1765. }
  1766. }
  1767. if (substr_count($value,"<p>")==1) {
  1768. $this->html .= '/>'.substr($value,3,(strlen($value)-7)).'</label>';
  1769. if ($form_content['display'] == 'vertical') {
  1770. $this->html .= '<br />';
  1771. }
  1772. } else {
  1773. $this->html .= '/>'.$value.'</label>';
  1774. }
  1775. }
  1776. echo '<div class="survey_question_wrapper">';
  1777. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  1778. echo '<div class="survey_question_options">';
  1779. echo $this->html;
  1780. echo '</div>';
  1781. echo '</div>';
  1782. }
  1783. }
  1784. class dropdown extends question
  1785. {
  1786. /**
  1787. * This function creates the form elements for the dropdown questions
  1788. *
  1789. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1790. * @version January 2007
  1791. */
  1792. function create_form($form_content)
  1793. {
  1794. global $charset;
  1795. $this->html = parent::create_form($form_content);
  1796. // The answers
  1797. $this->html .= ' <div class="row">';
  1798. $this->html .= ' <div class="label">';
  1799. $this->html .= get_lang('AnswerOptions');
  1800. $this->html .= ' </div>';
  1801. $this->html .= ' <div class="formw">';
  1802. $total_number_of_answers = count($form_content['answers']);
  1803. $this->html .= ' <table>';
  1804. foreach ($form_content['answers'] as $key=>$value)
  1805. {
  1806. $this->html .= ' <tr>';
  1807. $this->html .= ' <td align="right"><label for="answers['.$key.']">'.($key+1).'</label></td>';
  1808. $this->html .= ' <td><input type="text" name="answers['.$key.']" id="answers['.$key.']" value="'.stripslashes(api_htmlentities($form_content['answers'][$key], ENT_QUOTES, $charset)).'" /></td>';
  1809. $this->html .= ' <td>';
  1810. if ($key<$total_number_of_answers-1)
  1811. {
  1812. $this->html .= ' <input type="image" src="../img/down.gif" value="move_down['.$key.']" name="move_down['.$key.']"/>';
  1813. }
  1814. if ($key>0)
  1815. {
  1816. $this->html .= ' <input type="image" src="../img/up.gif" value="move_up['.$key.']" name="move_up['.$key.']"/>';
  1817. }
  1818. if ($total_number_of_answers> 2)
  1819. {
  1820. $this->html .= ' <input type="image" src="../img/delete.gif" value="delete_answer['.$key.']" name="delete_answer['.$key.']"/>';
  1821. }
  1822. $this->html .= ' </td>';
  1823. $this->html .= ' </tr>';
  1824. }
  1825. // The buttons for adding or removing
  1826. $this->html .= ' </table>';
  1827. $this->html .= ' </div>';
  1828. $this->html .= ' </div>';
  1829. $this->html .= parent :: add_remove_buttons($form_content);
  1830. }
  1831. /**
  1832. * Render the dropdown question type
  1833. *
  1834. * @param unknown_type $form_content
  1835. *
  1836. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1837. * @version January 2007
  1838. */
  1839. function render_question($form_content, $answers=array())
  1840. {
  1841. foreach ($form_content['options'] as $key=>$value)
  1842. {
  1843. $this->html .= '<option value="'.$key.'" ';
  1844. if (is_array($answers))
  1845. {
  1846. if (in_array($key,$answers))
  1847. {
  1848. $this->html .= 'selected="selected"';
  1849. }
  1850. }
  1851. $this->html .= '>'.$value.'</option>';
  1852. }
  1853. echo '<div class="survey_question_wrapper">';
  1854. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  1855. echo '<div class="survey_question_options">';
  1856. echo '<select name="question'.$form_content['question_id'].'" id="select">';
  1857. echo $this->html;
  1858. echo '</select>';
  1859. echo '</div>';
  1860. echo '</div>';
  1861. /*
  1862. <option value="test">test</option>
  1863. */
  1864. }
  1865. }
  1866. class open extends question
  1867. {
  1868. /**
  1869. * This function creates the form elements for the open questions
  1870. *
  1871. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1872. * @version January 2007
  1873. *
  1874. * @todo add a limit for the number of characters that can be type
  1875. * @todo add a checkbox weither the answer is a textarea or a wysiwyg editor
  1876. */
  1877. function create_form($form_content)
  1878. {
  1879. $this->html = parent::create_form($form_content);
  1880. }
  1881. /**
  1882. * render the open question type
  1883. *
  1884. * @param unknown_type $form_content
  1885. *
  1886. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1887. * @version January 2007
  1888. */
  1889. function render_question($form_content, $answers=array())
  1890. {
  1891. echo '<div class="survey_question_wrapper">';
  1892. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  1893. echo '<div class="survey_question_options">';
  1894. if (is_array($answers))
  1895. {
  1896. $content = implode('',$answers);
  1897. }
  1898. else
  1899. {
  1900. $content = $answers;
  1901. }
  1902. 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>';
  1903. echo '</div>';
  1904. echo '</div>';
  1905. }
  1906. }
  1907. class comment extends question
  1908. {
  1909. /**
  1910. * This function creates the form elements for a comment.
  1911. * A comment is nothing more than a block of text that the user can read
  1912. *
  1913. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1914. * @version January 2007
  1915. *
  1916. * @param array $form_content
  1917. */
  1918. function create_form($form_content)
  1919. {
  1920. $this->html = parent::create_form($form_content);
  1921. }
  1922. /**
  1923. * Render the comment "question" type
  1924. *
  1925. * @param unknown_type $form_content
  1926. *
  1927. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1928. * @version January 2007
  1929. */
  1930. function render_question($form_content)
  1931. {
  1932. echo '<div class="survey_question_wrapper">';
  1933. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  1934. echo '</div>';
  1935. echo "\n";
  1936. }
  1937. }
  1938. class pagebreak extends question
  1939. {
  1940. /**
  1941. * This function creates the form elements for a comment.
  1942. * A comment is nothing more than a block of text that the user can read
  1943. *
  1944. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1945. * @version January 2007
  1946. *
  1947. * @param array $form_content
  1948. */
  1949. function create_form($form_content)
  1950. {
  1951. $this->html = parent::create_form($form_content);
  1952. }
  1953. }
  1954. class percentage extends question
  1955. {
  1956. function create_form($form_content)
  1957. {
  1958. $this->html = parent::create_form($form_content);
  1959. }
  1960. function render_question($form_content, $answers=array())
  1961. {
  1962. $this->html .= '<option value="--">--</option>';
  1963. foreach ($form_content['options'] as $key=>$value)
  1964. {
  1965. $this->html .= '<option value="'.$key.'" ';
  1966. if (is_array($answers))
  1967. {
  1968. if (in_array($key,$answers))
  1969. {
  1970. $this->html .= 'selected="selected"';
  1971. }
  1972. }
  1973. $this->html .= '>'.$value.'</option>';
  1974. }
  1975. echo '<div class="survey_question_wrapper">';
  1976. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  1977. echo '<div class="survey_question_options">';
  1978. echo '<select name="question'.$form_content['question_id'].'" id="select">';
  1979. echo $this->html;
  1980. echo '</select>';
  1981. echo '</div>';
  1982. echo '</div>';
  1983. }
  1984. }
  1985. class score extends question
  1986. {
  1987. function create_form($form_content)
  1988. {
  1989. $this->html = parent::create_form($form_content);
  1990. // the maximum score that can be given
  1991. $this->html .= ' <div class="row">';
  1992. $this->html .= ' <div class="label">';
  1993. $this->html .= ' <span class="form_required">*</span>'.get_lang('MaximumScore');
  1994. $this->html .= ' </div>';
  1995. $this->html .= ' <div class="formw">';
  1996. $this->html .= ' <input type="text" name="maximum_score" value="'.$form_content['maximum_score'].'">';
  1997. $this->html .= ' </div>';
  1998. $this->html .= ' </div>';
  1999. // The answers
  2000. $this->html .= ' <div class="row">';
  2001. $this->html .= ' <div class="label">';
  2002. $this->html .= get_lang('AnswerOptions');
  2003. $this->html .= ' </div>';
  2004. $this->html .= ' <div class="formw">';
  2005. $total_number_of_answers = count($form_content['answers']);
  2006. $this->html .= ' <table>';
  2007. foreach ($form_content['answers'] as $key=>$value)
  2008. {
  2009. $this->html .= ' <tr>';
  2010. $this->html .= ' <td align="right"><label for="answers['.$key.']">'.($key+1).'</label></td>';
  2011. //$this->html .= ' <td><input type="text" name="answers['.$key.']" id="answers['.$key.']" value="'.$form_content['answers'][$key].'" /></td>';
  2012. $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>';
  2013. $this->html .= ' <td>';
  2014. if ($key<$total_number_of_answers-1)
  2015. {
  2016. $this->html .= ' <input type="image" src="../img/down.gif" value="move_down['.$key.']" name="move_down['.$key.']"/>';
  2017. }
  2018. if ($key>0)
  2019. {
  2020. $this->html .= ' <input type="image" src="../img/up.gif" value="move_up['.$key.']" name="move_up['.$key.']"/>';
  2021. }
  2022. if ($total_number_of_answers> 2)
  2023. {
  2024. $this->html .= ' <input type="image" src="../img/delete.gif" value="delete_answer['.$key.']" name="delete_answer['.$key.']"/>';
  2025. }
  2026. $this->html .= ' </td>';
  2027. $this->html .= ' </tr>';
  2028. }
  2029. // The buttons for adding or removing
  2030. $this->html .= ' </table>';
  2031. $this->html .= ' </div>';
  2032. $this->html .= ' </div>';
  2033. $this->html .= parent :: add_remove_buttons($form_content);
  2034. }
  2035. function render_question($form_content, $answers=array())
  2036. {
  2037. /*
  2038. echo '<div style="border: 1px solid red;">';
  2039. echo '<pre>';
  2040. print_r($answers);
  2041. echo '</pre></div>';
  2042. */
  2043. $this->html = '<table>';
  2044. foreach ($form_content['options'] as $key=>$value)
  2045. {
  2046. $this->html .= '<tr>
  2047. <td>'.$value.'</td>';
  2048. $this->html .= ' <td>';
  2049. $this->html .= '<select name="question'.$form_content['question_id'].'['.$key.']">';
  2050. $this->html .= '<option value="--">--</option>';
  2051. for($i=1; $i<=$form_content['maximum_score']; $i++)
  2052. {
  2053. $this->html .= '<option value="'.$i.'"';
  2054. if ($answers[$key] == $i)
  2055. {
  2056. $this->html .= 'selected="selected" ';
  2057. }
  2058. $this->html .= '>'.$i.'</option>';
  2059. }
  2060. $this->html .= '</select>';
  2061. $this->html .= ' </td>';
  2062. $this->html .= '</tr>';
  2063. }
  2064. $this->html .= '</table>';
  2065. echo '<div class="survey_question_wrapper">';
  2066. echo '<div class="survey_question">'.$form_content['survey_question'].'</div>';
  2067. echo '<div class="survey_question_options">';
  2068. //echo '<select name="question'.$form_content['question_id'].'" id="select">';
  2069. echo $this->html;
  2070. //echo '</select>';
  2071. echo '</div>';
  2072. echo '</div>';
  2073. }
  2074. }
  2075. /**
  2076. * This class offers a series of general utility functions for survey querying and display
  2077. * @package dokeos.survey
  2078. */
  2079. class SurveyUtil {
  2080. /**
  2081. * Checks whether the given survey has a pagebreak question as the first or the last question.
  2082. * If so, break the current process, displaying an error message
  2083. * @param integer Survey ID (database ID)
  2084. * @param boolean Optional. Whether to continue the current process or exit when breaking condition found. Defaults to true (do not break).
  2085. * @return void
  2086. */
  2087. function check_first_last_question($survey_id, $continue=true)
  2088. {
  2089. // table definitions
  2090. $tbl_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  2091. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  2092. // getting the information of the question
  2093. $sql = "SELECT * FROM $tbl_survey_question WHERE survey_id='".Database::escape_string($survey_id)."' ORDER BY sort ASC";
  2094. $result = Database::query($sql, __FILE__, __LINE__);
  2095. $total = Database::num_rows($result);
  2096. $counter=1;
  2097. $error = false;
  2098. while ($row = Database::fetch_array($result,'ASSOC'))
  2099. {
  2100. if ($counter == 1 AND $row['type'] == 'pagebreak')
  2101. {
  2102. Display::display_error_message(get_lang('PagebreakNotFirst'), false);
  2103. $error = true;
  2104. }
  2105. if ($counter == $total AND $row['type'] == 'pagebreak')
  2106. {
  2107. Display::display_error_message(get_lang('PagebreakNotLast'), false);
  2108. $error = true;
  2109. }
  2110. $counter++;
  2111. }
  2112. if (!$continue AND $error)
  2113. {
  2114. Display::display_footer();
  2115. exit;
  2116. }
  2117. }
  2118. /**
  2119. * This function removes an (or multiple) answer(s) of a user on a question of a survey
  2120. *
  2121. * @param mixed The user id or email of the person who fills the survey
  2122. * @param integer The survey id
  2123. * @param integer The question id
  2124. * @param integer The option id
  2125. *
  2126. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2127. * @version January 2007
  2128. */
  2129. function remove_answer($user, $survey_id, $question_id)
  2130. {
  2131. global $_course;
  2132. // table definition
  2133. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER, $_course['db_name']);
  2134. $sql = "DELETE FROM $table_survey_answer
  2135. WHERE user = '".Database::escape_string($user)."'
  2136. AND survey_id = '".Database::escape_string($survey_id)."'
  2137. AND question_id = '".Database::escape_string($question_id)."'";
  2138. $result = Database::query($sql, __FILE__, __LINE__);
  2139. }
  2140. /**
  2141. * This function stores an answer of a user on a question of a survey
  2142. *
  2143. * @param mixed The user id or email of the person who fills the survey
  2144. * @param integer Survey id
  2145. * @param integer Question id
  2146. * @param integer Option id
  2147. * @param string Option value
  2148. * @param array Survey data settings
  2149. *
  2150. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2151. * @version January 2007
  2152. */
  2153. function store_answer($user, $survey_id, $question_id, $option_id, $option_value, $survey_data)
  2154. {
  2155. global $_course;
  2156. global $types;
  2157. // table definition
  2158. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER, $_course['db_name']);
  2159. // make the survey anonymous
  2160. if ($survey_data['anonymous'] == 1)
  2161. {
  2162. if (!$_SESSION['surveyuser'])
  2163. {
  2164. $user = md5($user.time());
  2165. $_SESSION['surveyuser'] = $user;
  2166. }
  2167. else
  2168. {
  2169. $user = $_SESSION['surveyuser'];
  2170. }
  2171. }
  2172. $sql = "INSERT INTO $table_survey_answer (user, survey_id, question_id, option_id, value) VALUES (
  2173. '".Database::escape_string($user)."',
  2174. '".Database::escape_string($survey_id)."',
  2175. '".Database::escape_string($question_id)."',
  2176. '".Database::escape_string($option_id)."',
  2177. '".Database::escape_string($option_value)."'
  2178. )";
  2179. $result = Database::query($sql, __FILE__, __LINE__);
  2180. }
  2181. /**
  2182. * This function checks the parameters that are used in this page
  2183. *
  2184. * @return string The header, an error and the footer if any parameter fails, else it returns true
  2185. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2186. * @version February 2007
  2187. */
  2188. function check_parameters()
  2189. {
  2190. $error = false;
  2191. // getting the survey data
  2192. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  2193. // $_GET['survey_id'] has to be numeric
  2194. if (!is_numeric($_GET['survey_id']))
  2195. {
  2196. $error = get_lang('IllegalSurveyId');
  2197. }
  2198. // $_GET['action']
  2199. $allowed_actions = array('overview', 'questionreport', 'userreport', 'comparativereport', 'completereport','deleteuserreport');
  2200. if (isset($_GET['action']) AND !in_array($_GET['action'], $allowed_actions))
  2201. {
  2202. $error = get_lang('ActionNotAllowed');
  2203. }
  2204. // user report
  2205. if ($_GET['action'] == 'userreport')
  2206. {
  2207. global $people_filled;
  2208. if ($survey_data['anonymous'] == 0)
  2209. {
  2210. $people_filled_full_data = true;
  2211. }
  2212. else
  2213. {
  2214. $people_filled_full_data = false;
  2215. }
  2216. $people_filled = survey_manager::get_people_who_filled_survey($_GET['survey_id'], $people_filled_full_data);
  2217. if ($survey_data['anonymous'] == 0)
  2218. {
  2219. foreach ($people_filled as $key=>$value)
  2220. {
  2221. $people_filled_userids[]=$value['invited_user'];
  2222. }
  2223. }
  2224. else
  2225. {
  2226. $people_filled_userids = $people_filled;
  2227. }
  2228. if (isset($_GET['user']) AND !in_array($_GET['user'], $people_filled_userids))
  2229. {
  2230. $error = get_lang('UnknowUser');
  2231. }
  2232. }
  2233. // question report
  2234. if ($_GET['action'] == 'questionreport')
  2235. {
  2236. if (isset($_GET['question'])AND !is_numeric($_GET['question']))
  2237. {
  2238. $error = get_lang('UnknowQuestion');
  2239. }
  2240. }
  2241. if ($error)
  2242. {
  2243. $tool_name = get_lang('Reporting');
  2244. Display::display_header($tool_name);
  2245. Display::display_error_message(get_lang('Error').': '.$error, false);
  2246. Display::display_footer();
  2247. exit;
  2248. }
  2249. else
  2250. {
  2251. return true;
  2252. }
  2253. }
  2254. /**
  2255. * This function deals with the action handling
  2256. * @return void
  2257. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2258. * @version February 2007
  2259. */
  2260. function handle_reporting_actions()
  2261. {
  2262. // getting the number of question
  2263. $temp_questions_data = survey_manager::get_questions($_GET['survey_id']);
  2264. // sorting like they should be displayed and removing the non-answer question types (comment and pagebreak)
  2265. $my_temp_questions_data=($temp_questions_data==null) ? array() : $temp_questions_data;
  2266. foreach ($my_temp_questions_data as $key=>$value)
  2267. {
  2268. if ($value['type'] <> 'comment' AND $value['type']<>'pagebreak')
  2269. {
  2270. $questions_data[$value['sort']]=$value;
  2271. }
  2272. }
  2273. // counting the number of questions that are relevant for the reporting
  2274. $survey_data['number_of_questions'] = count($questions_data);
  2275. if ($_GET['action'] == 'questionreport')
  2276. {
  2277. SurveyUtil::display_question_report($survey_data);
  2278. }
  2279. if ($_GET['action'] == 'userreport')
  2280. {
  2281. SurveyUtil::display_user_report();
  2282. }
  2283. if ($_GET['action'] == 'comparativereport')
  2284. {
  2285. SurveyUtil::display_comparative_report();
  2286. }
  2287. if ($_GET['action'] == 'completereport')
  2288. {
  2289. SurveyUtil::display_complete_report();
  2290. }
  2291. if ($_GET['action'] == 'deleteuserreport')
  2292. {
  2293. SurveyUtil::delete_user_report($_GET['survey_id'],$_GET['user']);
  2294. //SurveyUtil::display_user_report(); //Could work but looks a bit clunky
  2295. }
  2296. }
  2297. /**
  2298. * This function deletes the report of an user who wants to retake the survey
  2299. * @param integer survey_id
  2300. * @param integer user_id
  2301. * @return void
  2302. * @author Christian Fasanando Flores <christian.fasanando@dokeos.com>
  2303. * @version November 2008
  2304. */
  2305. function delete_user_report($survey_id,$user_id) {
  2306. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  2307. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  2308. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  2309. if (!empty($survey_id) && !empty($user_id)) {
  2310. // delete data from survey_answer by user_id and survey_id
  2311. $sql = "DELETE FROM $table_survey_answer WHERE survey_id = '".(int)$survey_id."' AND user = '".(int)$user_id."'";
  2312. $result = Database::query($sql, __FILE__, __LINE__);
  2313. // update field answered from survey_invitation by user_id and survey_id
  2314. $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."'";
  2315. $result = Database::query($sql, __FILE__, __LINE__);
  2316. }
  2317. if($result !== false) {
  2318. $message = get_lang('SurveyUserAnswersHaveBeenRemovedSuccessfully').'<br />
  2319. <a href="reporting.php?action=userreport&survey_id='.Security::remove_XSS($survey_id).'">'.get_lang('GoBack').'</a>';
  2320. Display::display_confirmation_message($message, false);
  2321. }
  2322. }
  2323. /**
  2324. * This function displays the user report which is basically nothing more than a one-page display of all the questions
  2325. * of the survey that is filled with the answers of the person who filled the survey.
  2326. *
  2327. * @return string html code of the one-page survey with the answers of the selected user
  2328. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2329. * @version February 2007 - Updated March 2008
  2330. */
  2331. function display_user_report()
  2332. {
  2333. global $people_filled, $survey_data;
  2334. // Database table definitions
  2335. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  2336. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  2337. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  2338. // actions bar
  2339. echo '<div class="actions">';
  2340. 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>';
  2341. if (isset($_GET['user']))
  2342. {
  2343. // the delete link
  2344. 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>';
  2345. // export the user report
  2346. echo '<a href="javascript: void(0);" onclick="document.form1a.submit();">'.Display::return_icon('csv.gif', get_lang('ExportAsCSV')).' '.get_lang('ExportAsCSV').'</a> ';
  2347. echo '<a href="javascript: void(0);" onclick="document.form1b.submit();">'.Display::return_icon('excel.gif', get_lang('ExportAsXLS')).' '.get_lang('ExportAsXLS').'</a> ';
  2348. 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']).'">';
  2349. echo '<input type="hidden" name="export_report" value="export_report">';
  2350. echo '<input type="hidden" name="export_format" value="csv">';
  2351. echo '</form>';
  2352. 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']).'">';
  2353. echo '<input type="hidden" name="export_report" value="export_report">';
  2354. echo '<input type="hidden" name="export_format" value="xls">';
  2355. echo '</form>';
  2356. 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']).'">';
  2357. }
  2358. echo '</div>';
  2359. // step 1: selection of the user
  2360. echo "<script language=\"JavaScript\" type=\"text/JavaScript\">
  2361. <!--
  2362. function jumpMenu(targ,selObj,restore)
  2363. {
  2364. eval(targ+\".location='\"+selObj.options[selObj.selectedIndex].value+\"'\");
  2365. if (restore) selObj.selectedIndex=0;
  2366. }
  2367. //-->
  2368. </script>";
  2369. echo get_lang('SelectUserWhoFilledSurvey').'<br />';
  2370. echo '<select name="user" onchange="jumpMenu(\'parent\',this,0)">';
  2371. echo '<option value="reporting.php?action='.Security::remove_XSS($_GET['action']).'&amp;survey_id='.Security::remove_XSS($_GET['survey_id']).'">'.get_lang('SelectUser').'</option>';
  2372. foreach ($people_filled as $key=>$person)
  2373. {
  2374. if ($survey_data['anonymous'] == 0)
  2375. {
  2376. $name = api_get_person_name($person['firstname'], $person['lastname']);
  2377. $id = $person['user_id'];
  2378. if($id == ''){
  2379. $id = $person['invited_user'];
  2380. $name = $person['invited_user'];
  2381. }
  2382. }
  2383. else
  2384. {
  2385. $name = $key+1;
  2386. $id = $person;
  2387. }
  2388. 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).'" ';
  2389. if ($_GET['user'] == $id)
  2390. {
  2391. echo 'selected="selected"';
  2392. }
  2393. echo '>'.$name.'</option>';
  2394. }
  2395. echo '</select>';
  2396. // step 2: displaying the survey and the answer of the selected users
  2397. if (isset($_GET['user']))
  2398. {
  2399. Display::display_normal_message(get_lang('AllQuestionsOnOnePage'), false);
  2400. // getting all the questions and options
  2401. $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,
  2402. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  2403. FROM $table_survey_question survey_question
  2404. LEFT JOIN $table_survey_question_option survey_question_option
  2405. ON survey_question.question_id = survey_question_option.question_id
  2406. WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  2407. ORDER BY survey_question.sort, survey_question_option.sort ASC";
  2408. $result = Database::query($sql, __FILE__, __LINE__);
  2409. while ($row = Database::fetch_array($result,'ASSOC'))
  2410. {
  2411. if($row['type'] <> 'pagebreak')
  2412. {
  2413. $questions[$row['sort']]['question_id'] = $row['question_id'];
  2414. $questions[$row['sort']]['survey_id'] = $row['survey_id'];
  2415. $questions[$row['sort']]['survey_question'] = $row['survey_question'];
  2416. $questions[$row['sort']]['display'] = $row['display'];
  2417. $questions[$row['sort']]['type'] = $row['type'];
  2418. $questions[$row['sort']]['maximum_score'] = $row['max_value'];
  2419. $questions[$row['sort']]['options'][$row['question_option_id']] = $row['option_text'];
  2420. }
  2421. }
  2422. // getting all the answers of the user
  2423. $sql = "SELECT * FROM $table_survey_answer WHERE survey_id = '".Database::escape_string($_GET['survey_id'])."' AND user = '".Database::escape_string($_GET['user'])."'";
  2424. $result = Database::query($sql, __FILE__, __LINE__);
  2425. while ($row = Database::fetch_array($result,'ASSOC'))
  2426. {
  2427. $answers[$row['question_id']][] = $row['option_id'];
  2428. $all_answers[$row['question_id']][] = $row;
  2429. }
  2430. // displaying all the questions
  2431. $second_parameter=array();
  2432. foreach ($questions as $key=>$question)
  2433. {
  2434. // if the question type is a scoring then we have to format the answers differently
  2435. if ($question['type'] == 'score')
  2436. {
  2437. if (is_array($second_parameter) && is_array($question) &&is_array($all_answers))
  2438. {
  2439. foreach($all_answers[$question['question_id']] as $key=>$answer_array)
  2440. {
  2441. $second_parameter[$answer_array['option_id']] = $answer_array['value'];
  2442. }
  2443. }
  2444. }
  2445. else
  2446. {
  2447. $second_parameter = $answers[$question['question_id']];
  2448. if ($question['type'] == 'open')
  2449. {
  2450. $second_parameter = array();
  2451. $second_parameter[] = $all_answers[$question['question_id']][0]['option_id'];
  2452. }
  2453. }
  2454. $display = new $question['type'];
  2455. $display->render_question($question, $second_parameter);
  2456. }
  2457. }
  2458. }
  2459. /**
  2460. * This function displays the report by question.
  2461. *
  2462. * It displays a table with all the options of the question and the number of users who have answered positively on the option.
  2463. * The number of users who answered positive on a given option is expressed in an absolute number, in a percentage of the total
  2464. * and graphically using bars
  2465. * By clicking on the absolute number you get a list with the persons who have answered this.
  2466. * 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
  2467. * answers of that user.
  2468. *
  2469. * @param array All the survey data
  2470. * @return string html code that displays the report by question
  2471. * @todo allow switching between horizontal and vertical.
  2472. * @todo multiple response: percentage are probably not OK
  2473. * @todo the question and option text have to be shortened and should expand when the user clicks on it.
  2474. * @todo the pagebreak and comment question types should not be shown => removed from $survey_data before
  2475. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2476. * @version February 2007 - Updated March 2008
  2477. */
  2478. function display_question_report($survey_data)
  2479. {
  2480. // Database table definitions
  2481. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  2482. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  2483. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  2484. // determining the offset of the sql statement (the n-th question of the survey)
  2485. if (!isset($_GET['question']))
  2486. {
  2487. $offset = 0;
  2488. }
  2489. else
  2490. {
  2491. $offset = Database::escape_string($_GET['question']);
  2492. }
  2493. echo '<div class="actions">';
  2494. 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>';
  2495. if ($survey_data['number_of_questions'] > 0) {
  2496. echo '<div id="question_report_questionnumbers">'.get_lang('GoToQuestion').': ';
  2497. for($i=1; $i<=($survey_data['number_of_questions']); $i++ )
  2498. {
  2499. if ($offset <> $i-1)
  2500. {
  2501. echo '<a href="reporting.php?action=questionreport&amp;survey_id='.Security::remove_XSS($_GET['survey_id']).'&amp;question='.($i-1).'">'.$i.'</a>';
  2502. }
  2503. else
  2504. {
  2505. echo $i;
  2506. }
  2507. if ($i < $survey_data['number_of_questions'])
  2508. {
  2509. echo ' | ';
  2510. }
  2511. }
  2512. echo '</div>';
  2513. // getting the question information
  2514. $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";
  2515. $result = Database::query($sql, __FILE__, __LINE__);
  2516. $question = Database::fetch_array($result);
  2517. // navigate through the questions (next and previous)
  2518. if ($_GET['question'] <> 0)
  2519. {
  2520. 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> ';
  2521. }
  2522. else
  2523. {
  2524. echo Display::return_icon('prev.png',get_lang('PreviousQuestion'),array('align'=>'middle')).' '.get_lang('PreviousQuestion').' ';
  2525. }
  2526. echo ' | ';
  2527. if ($_GET['question'] < ($survey_data['number_of_questions']-1))
  2528. {
  2529. 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>';
  2530. }
  2531. else
  2532. {
  2533. echo get_lang('NextQuestion'). ' '.Display::return_icon('next.png',get_lang('NextQuestion'),array('align'=>'middle'));
  2534. }
  2535. }
  2536. echo '</div>';
  2537. echo $question['survey_question'];
  2538. echo '<br />';
  2539. if ($question['type'] == 'score')
  2540. {
  2541. /** @todo this function should return the options as this is needed further in the code */
  2542. $options = SurveyUtil::display_question_report_score($survey_data, $question, $offset);
  2543. }
  2544. elseif ($question['type'] == 'open')
  2545. {
  2546. /** @todo also get the user who has answered this */
  2547. $sql = "SELECT * FROM $table_survey_answer WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'
  2548. AND question_id = '".Database::escape_string($question['question_id'])."'";
  2549. $result = Database::query($sql, __FILE__, __LINE__);
  2550. while ($row = Database::fetch_array($result))
  2551. {
  2552. echo $row['option_id'].'<hr noshade="noshade" size="1" />';
  2553. }
  2554. }
  2555. else
  2556. {
  2557. // getting the options
  2558. $sql = "SELECT * FROM $table_survey_question_option
  2559. WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'
  2560. AND question_id = '".Database::escape_string($question['question_id'])."'
  2561. ORDER BY sort ASC";
  2562. $result = Database::query($sql, __FILE__, __LINE__);
  2563. while ($row = Database::fetch_array($result))
  2564. {
  2565. $options[$row['question_option_id']] = $row;
  2566. }
  2567. // getting the answers
  2568. $sql = "SELECT *, count(answer_id) as total FROM $table_survey_answer
  2569. WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'
  2570. AND question_id = '".Database::escape_string($question['question_id'])."'
  2571. GROUP BY option_id, value";
  2572. $result = Database::query($sql, __FILE__, __LINE__);
  2573. while ($row = Database::fetch_array($result))
  2574. {
  2575. $number_of_answers += $row['total'];
  2576. $data[$row['option_id']] = $row;
  2577. }
  2578. //echo '<pre>';
  2579. //print_r($data);
  2580. //echo '<pre>';
  2581. // displaying the table: headers
  2582. echo '<table>';
  2583. echo ' <tr>';
  2584. echo ' <th>&nbsp;</th>';
  2585. echo ' <th>'.get_lang('AbsoluteTotal').'</th>';
  2586. echo ' <th>'.get_lang('Percentage').'</th>';
  2587. echo ' <th>'.get_lang('VisualRepresentation').'</th>';
  2588. echo ' <tr>';
  2589. // displaying the table: the content
  2590. if(is_array($options)){
  2591. foreach ($options as $key=>$value)
  2592. {
  2593. $absolute_number = $data[$value['question_option_id']]['total'];
  2594. if($number_of_answers == 0)
  2595. {
  2596. $answers_number = 0;
  2597. }
  2598. else
  2599. {
  2600. $answers_number = $absolute_number/$number_of_answers*100;
  2601. }
  2602. echo ' <tr>';
  2603. echo ' <td>'.$value['option_text'].'</td>';
  2604. 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>';
  2605. echo ' <td align="right">'.round($answers_number, 2).' %</td>';
  2606. echo ' <td align="right">';
  2607. $size = $answers_number*2;
  2608. if ($size > 0)
  2609. {
  2610. echo '<div style="border:1px solid #264269; background-color:#aecaf4; height:10px; width:'.$size.'px">&nbsp;</div>';
  2611. }
  2612. echo ' </td>';
  2613. echo ' </tr>';
  2614. }
  2615. }
  2616. // displaying the table: footer (totals)
  2617. echo ' <tr>';
  2618. echo ' <td style="border-top:1px solid black;"><b>'.get_lang('Total').'</b></td>';
  2619. echo ' <td style="border-top:1px solid black;" align="right"><b>'.($number_of_answers==0?'0':$number_of_answers).'</b></td>';
  2620. echo ' <td style="border-top:1px solid black;">&nbsp;</td>';
  2621. echo ' <td style="border-top:1px solid black;">&nbsp;</td>';
  2622. echo ' </tr>';
  2623. echo '</table>';
  2624. }
  2625. if (isset($_GET['viewoption']))
  2626. {
  2627. echo get_lang('PeopleWhoAnswered').': '.$options[Security::remove_XSS($_GET['viewoption'])]['option_text'].'<br />';
  2628. if (is_numeric($_GET['value']))
  2629. {
  2630. $sql_restriction = "AND value='".Database::escape_string($_GET['value'])."'";
  2631. }
  2632. $sql = "SELECT user FROM $table_survey_answer WHERE option_id = '".Database::escape_string($_GET['viewoption'])."' $sql_restriction";
  2633. $result = Database::query($sql, __FILE__, __LINE__);
  2634. while ($row = Database::fetch_array($result))
  2635. {
  2636. echo '<a href="reporting.php?action=userreport&survey_id='.Security::remove_XSS($_GET['survey_id']).'&user='.$row['user'].'">'.$row['user'].'</a><br />';
  2637. }
  2638. }
  2639. }
  2640. /**
  2641. * Display score data about a survey question
  2642. * @param array Question info
  2643. * @param integer The offset of results shown
  2644. * @return void (direct output)
  2645. */
  2646. function display_question_report_score($survey_data, $question, $offset)
  2647. {
  2648. // Database table definitions
  2649. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  2650. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  2651. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  2652. // getting the options
  2653. $sql = "SELECT * FROM $table_survey_question_option
  2654. WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'
  2655. AND question_id = '".Database::escape_string($question['question_id'])."'
  2656. ORDER BY sort ASC";
  2657. $result = Database::query($sql, __FILE__, __LINE__);
  2658. while ($row = Database::fetch_array($result))
  2659. {
  2660. $options[$row['question_option_id']] = $row;
  2661. }
  2662. // getting the answers
  2663. $sql = "SELECT *, count(answer_id) as total FROM $table_survey_answer
  2664. WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'
  2665. AND question_id = '".Database::escape_string($question['question_id'])."'
  2666. GROUP BY option_id, value";
  2667. $result = Database::query($sql, __FILE__, __LINE__);
  2668. while ($row = Database::fetch_array($result))
  2669. {
  2670. $number_of_answers += $row['total'];
  2671. $data[$row['option_id']][$row['value']] = $row;
  2672. }
  2673. // displaying the table: headers
  2674. echo '<table>';
  2675. echo ' <tr>';
  2676. echo ' <th>&nbsp;</th>';
  2677. echo ' <th>'.get_lang('Score').'</th>';
  2678. echo ' <th>'.get_lang('AbsoluteTotal').'</th>';
  2679. echo ' <th>'.get_lang('Percentage').'</th>';
  2680. echo ' <th>'.get_lang('VisualRepresentation').'</th>';
  2681. echo ' <tr>';
  2682. // displaying the table: the content
  2683. foreach ($options as $key=>$value)
  2684. {
  2685. for ($i=1; $i<=$question['max_value']; $i++)
  2686. {
  2687. $absolute_number = $data[$value['question_option_id']][$i]['total'];
  2688. echo ' <tr>';
  2689. echo ' <td>'.$value['option_text'].'</td>';
  2690. echo ' <td>'.$i.'</td>';
  2691. 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>';
  2692. echo ' <td>'.round($absolute_number/$number_of_answers*100, 2).' %</td>';
  2693. echo ' <td>';
  2694. $size = ($absolute_number/$number_of_answers*100*2);
  2695. if ($size > 0)
  2696. {
  2697. echo ' <div style="border:1px solid #264269; background-color:#aecaf4; height:10px; width:'.$size.'px">&nbsp;</div>';
  2698. }
  2699. echo ' </td>';
  2700. echo ' </tr>';
  2701. }
  2702. }
  2703. // displaying the table: footer (totals)
  2704. echo ' <tr>';
  2705. echo ' <td style="border-top:1px solid black"><b>'.get_lang('Total').'</b></td>';
  2706. echo ' <td style="border-top:1px solid black">&nbsp;</td>';
  2707. echo ' <td style="border-top:1px solid black"><b>'.$number_of_answers.'</b></td>';
  2708. echo ' <td style="border-top:1px solid black">&nbsp;</td>';
  2709. echo ' <td style="border-top:1px solid black">&nbsp;</td>';
  2710. echo ' </tr>';
  2711. echo '</table>';
  2712. }
  2713. /**
  2714. * This functions displays the complete reporting
  2715. * @return string HTML code
  2716. * @todo open questions are not in the complete report yet.
  2717. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2718. * @version February 2007
  2719. */
  2720. function display_complete_report()
  2721. {
  2722. // Database table definitions
  2723. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  2724. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  2725. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  2726. // actions bar
  2727. echo '<div class="actions">';
  2728. 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>';
  2729. 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>';
  2730. 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>';
  2731. echo '</div>';
  2732. // the form
  2733. 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']).'">';
  2734. echo '<input type="hidden" name="export_report" value="export_report">';
  2735. echo '<input type="hidden" name="export_format" value="csv">';
  2736. echo '</form>';
  2737. 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']).'">';
  2738. echo '<input type="hidden" name="export_report" value="export_report">';
  2739. echo '<input type="hidden" name="export_format" value="xls">';
  2740. echo '</form>';
  2741. 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']).'">';
  2742. // the table
  2743. echo '<br /><table class="data_table" border="1">';
  2744. // getting the number of options per question
  2745. echo ' <tr>';
  2746. echo ' <th>';
  2747. if ($_POST['submit_question_filter'] OR $_POST['export_report'])
  2748. {
  2749. echo ' <button class="cancel" type="submit" name="reset_question_filter" value="'.get_lang('ResetQuestionFilter').'">'.get_lang('ResetQuestionFilter').'</button>';
  2750. }
  2751. echo ' <button class="save" type="submit" name="submit_question_filter" value="'.get_lang('SubmitQuestionFilter').'">'.get_lang('SubmitQuestionFilter').'</button>';
  2752. echo '</th>';
  2753. $display_extra_user_fields = false;
  2754. if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR !empty($_POST['fields_filter']))
  2755. {
  2756. //show user fields section with a big th colspan that spans over all fields
  2757. $extra_user_fields = UserManager::get_extra_fields(0,0,5,'ASC',false);
  2758. $num = count($extra_user_fields);
  2759. if($num > 0)
  2760. {
  2761. echo '<th '.($num>0?' colspan="'.$num.'"':'').'>';
  2762. echo '<label><input type="checkbox" name="fields_filter" value="1" checked="checked"/> ';
  2763. echo get_lang('UserFields');
  2764. echo '</label>';
  2765. echo '</th>';
  2766. $display_extra_user_fields = true;
  2767. }
  2768. }
  2769. // Get all the questions ordered by the "sort" column
  2770. $sql = "SELECT q.question_id, q.type, q.survey_question, count(o.question_option_id) as number_of_options
  2771. FROM $table_survey_question q LEFT JOIN $table_survey_question_option o
  2772. ON q.question_id = o.question_id
  2773. WHERE q.question_id = o.question_id
  2774. AND q.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  2775. GROUP BY q.question_id
  2776. ORDER BY q.sort ASC";
  2777. $result = Database::query($sql, __FILE__, __LINE__);
  2778. while ($row = Database::fetch_array($result))
  2779. {
  2780. // we show the questions if
  2781. // 1. there is no question filter and the export button has not been clicked
  2782. // 2. there is a quesiton filter but the question is selected for display
  2783. //if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR in_array($row['question_id'], $_POST['questions_filter']))
  2784. if (!($_POST['submit_question_filter']) OR (is_array($_POST['questions_filter']) && in_array($row['question_id'], $_POST['questions_filter']))) {
  2785. // we do not show comment and pagebreak question types
  2786. if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
  2787. {
  2788. echo ' <th';
  2789. if ($row['number_of_options'] >0)
  2790. {
  2791. echo ' colspan="'.$row['number_of_options'].'"';
  2792. }
  2793. echo '>';
  2794. echo '<label><input type="checkbox" name="questions_filter[]" value="'.$row['question_id'].'" checked="checked"/> ';
  2795. echo $row['survey_question'];
  2796. echo '</label>';
  2797. echo '</th>';
  2798. }
  2799. //no column at all if it's not a question
  2800. }
  2801. $questions[$row['question_id']] = $row;
  2802. }
  2803. echo ' </tr>';
  2804. // getting all the questions and options
  2805. echo ' <tr>';
  2806. echo ' <th>&nbsp;</th>'; // the user column
  2807. if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR !empty($_POST['fields_filter']))
  2808. {
  2809. //show the fields names for user fields
  2810. foreach($extra_user_fields as $field)
  2811. {
  2812. echo '<th>'.$field[3].'</th>';
  2813. }
  2814. }
  2815. $sql = "SELECT sq.question_id, sq.survey_id,
  2816. sq.survey_question, sq.display,
  2817. sq.sort, sq.type, sqo.question_option_id,
  2818. sqo.option_text, sqo.sort as option_sort
  2819. FROM $table_survey_question sq
  2820. LEFT JOIN $table_survey_question_option sqo
  2821. ON sq.question_id = sqo.question_id
  2822. WHERE sq.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  2823. ORDER BY sq.sort ASC, sqo.sort ASC";
  2824. $result = Database::query($sql, __FILE__, __LINE__);
  2825. while ($row = Database::fetch_array($result)) {
  2826. // we show the options if
  2827. // 1. there is no question filter and the export button has not been clicked
  2828. // 2. there is a question filter but the question is selected for display
  2829. //if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR in_array($row['question_id'], $_POST['questions_filter']))
  2830. if (!($_POST['submit_question_filter']) OR (is_array($_POST['questions_filter']) && in_array($row['question_id'], $_POST['questions_filter']))) {
  2831. // we do not show comment and pagebreak question types
  2832. if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak' AND $row['type'] <> 'open')
  2833. {
  2834. echo ' <th>';
  2835. echo $row['option_text'];
  2836. echo '</th>';
  2837. $possible_answers[$row['question_id']][$row['question_option_id']] =$row['question_option_id'];
  2838. }
  2839. //no column at all if the question was not a question
  2840. }
  2841. }
  2842. echo ' </tr>';
  2843. // getting all the answers of the users
  2844. $old_user='';
  2845. $answers_of_user = array();
  2846. $sql = "SELECT * FROM $table_survey_answer WHERE survey_id='".Database::escape_string($_GET['survey_id'])."' ORDER BY user ASC";
  2847. $result = Database::query($sql, __FILE__, __LINE__);
  2848. while ($row = Database::fetch_array($result))
  2849. {
  2850. if ($old_user <> $row['user'] AND $old_user<>'')
  2851. {
  2852. SurveyUtil::display_complete_report_row($possible_answers, $answers_of_user, $old_user, $questions, $display_extra_user_fields);
  2853. $answers_of_user=array();
  2854. }
  2855. if ($questions[$row['question_id']]['type']<> 'open')
  2856. {
  2857. $answers_of_user[$row['question_id']][$row['option_id']] = $row;
  2858. }
  2859. else
  2860. {
  2861. $answers_of_user[$row['question_id']][0] = $row;
  2862. }
  2863. $old_user = $row['user'];
  2864. }
  2865. SurveyUtil::display_complete_report_row($possible_answers, $answers_of_user, $old_user, $questions, $display_extra_user_fields); // this is to display the last user
  2866. echo '</table>';
  2867. echo '</form>';
  2868. }
  2869. /**
  2870. * This function displays a row (= a user and his/her answers) in the table of the complete report.
  2871. *
  2872. * @param array Possible options
  2873. * @param array User answers
  2874. * @param mixed User ID or user details string
  2875. * @param boolean Whether to show extra user fields or not
  2876. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2877. * @version February 2007 - Updated March 2008
  2878. */
  2879. function display_complete_report_row($possible_options, $answers_of_user, $user, $questions, $display_extra_user_fields=false)
  2880. {
  2881. global $survey_data;
  2882. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  2883. echo '<tr>';
  2884. if ($survey_data['anonymous'] == 0)
  2885. {
  2886. if(intval($user)!==0)
  2887. {
  2888. $sql = 'SELECT firstname, lastname FROM '.Database::get_main_table(TABLE_MAIN_USER).' WHERE user_id='.intval($user);
  2889. $rs = Database::query($sql, __FILE__, __LINE__);
  2890. if($row = Database::fetch_array($rs))
  2891. {
  2892. $user_displayed = api_get_person_name($row['firstname'], $row['lastname']);
  2893. }
  2894. else
  2895. {
  2896. $user_displayed = '-';
  2897. }
  2898. 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
  2899. }
  2900. else
  2901. {
  2902. echo ' <th>'.$user.'</th>'; // the user column
  2903. }
  2904. }
  2905. else
  2906. {
  2907. echo '<th>-</th>';
  2908. }
  2909. if ($display_extra_user_fields)
  2910. {
  2911. //show user fields data, if any, for this user
  2912. $user_fields_values = UserManager::get_extra_user_data(intval($user),false,false);
  2913. foreach($user_fields_values as $value)
  2914. {
  2915. echo '<td align="center">'.$value.'</td>';
  2916. }
  2917. }
  2918. if(is_array($possible_options)) {
  2919. foreach ($possible_options as $question_id=>$possible_option)
  2920. {
  2921. if ($questions[$question_id]['type'] == 'open')
  2922. {
  2923. echo '<td align="center">';
  2924. echo $answers_of_user[$question_id]['0']['option_id'];
  2925. echo '</td>';
  2926. }
  2927. else
  2928. {
  2929. foreach ($possible_option as $option_id=>$value)
  2930. {
  2931. echo '<td align="center">';
  2932. if (!empty($answers_of_user[$question_id][$option_id]))
  2933. {
  2934. if ($answers_of_user[$question_id][$option_id]['value']<>0)
  2935. {
  2936. echo $answers_of_user[$question_id][$option_id]['value'];
  2937. }
  2938. else
  2939. {
  2940. echo 'v';
  2941. }
  2942. }
  2943. }
  2944. }
  2945. }
  2946. }
  2947. echo '</tr>';
  2948. }
  2949. /**
  2950. * Quite similar to display_complete_report(), returns an HTML string
  2951. * that can be used in a csv file
  2952. * @todo consider merging this function with display_complete_report
  2953. * @return string The contents of a csv file
  2954. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  2955. * @version February 2007
  2956. */
  2957. function export_complete_report($user_id=0)
  2958. {
  2959. global $charset;
  2960. // Database table definitions
  2961. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  2962. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  2963. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  2964. // the first column
  2965. $return = ';';
  2966. //show extra fields blank space (enough for extra fields on next line)
  2967. $display_extra_user_fields = false;
  2968. if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR !empty($_POST['fields_filter']))
  2969. {
  2970. //show user fields section with a big th colspan that spans over all fields
  2971. $extra_user_fields = UserManager::get_extra_fields(0,0,5,'ASC',false);
  2972. $num = count($extra_user_fields);
  2973. $return .= str_repeat(';',$num);
  2974. $display_extra_user_fields = true;
  2975. }
  2976. $sql = "SELECT questions.question_id, questions.type, questions.survey_question, count(options.question_option_id) as number_of_options
  2977. FROM $table_survey_question questions LEFT JOIN $table_survey_question_option options
  2978. ON questions.question_id = options.question_id "
  2979. ." WHERE questions.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  2980. GROUP BY questions.question_id "
  2981. ." ORDER BY questions.sort ASC";
  2982. $result = Database::query($sql, __FILE__, __LINE__);
  2983. while ($row = Database::fetch_array($result))
  2984. {
  2985. // we show the questions if
  2986. // 1. there is no question filter and the export button has not been clicked
  2987. // 2. there is a quesiton filter but the question is selected for display
  2988. if (!($_POST['submit_question_filter']) OR (is_array($_POST['questions_filter']) && in_array($row['question_id'], $_POST['questions_filter'])))
  2989. {
  2990. // we do not show comment and pagebreak question types
  2991. if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
  2992. {
  2993. if($row['number_of_options'] == 0 && $row['type']=='open')
  2994. {
  2995. $return .= str_replace("\r\n",' ', api_html_entity_decode(strip_tags($row['survey_question']), ENT_QUOTES, $charset)).';';
  2996. }
  2997. else
  2998. {
  2999. for ($ii = 0; $ii < $row['number_of_options']; $ii ++)
  3000. {
  3001. $return .= str_replace("\r\n",' ', api_html_entity_decode(strip_tags($row['survey_question']), ENT_QUOTES, $charset)).';';
  3002. }
  3003. }
  3004. }
  3005. }
  3006. }
  3007. $return .= "\n";
  3008. // getting all the questions and options
  3009. $return .= ';';
  3010. //show extra field values
  3011. if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR !empty($_POST['fields_filter']))
  3012. {
  3013. //show the fields names for user fields
  3014. foreach($extra_user_fields as $field)
  3015. {
  3016. $return .= '"'.str_replace("\r\n",' ',api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES, $charset)).'";';
  3017. }
  3018. }
  3019. $sql = "SELECT survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type,
  3020. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  3021. FROM $table_survey_question survey_question
  3022. LEFT JOIN $table_survey_question_option survey_question_option
  3023. ON survey_question.question_id = survey_question_option.question_id
  3024. WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  3025. ORDER BY survey_question.sort ASC, survey_question_option.sort ASC";
  3026. $result = Database::query($sql, __FILE__, __LINE__);
  3027. $possible_answers = array();
  3028. $possible_answers_type = array();
  3029. while ($row = Database::fetch_array($result))
  3030. {
  3031. // we show the options if
  3032. // 1. there is no question filter and the export button has not been clicked
  3033. // 2. there is a quesiton filter but the question is selected for display
  3034. if (!($_POST['submit_question_filter']) OR (is_array($_POST['questions_filter']) && in_array($row['question_id'], $_POST['questions_filter'])))
  3035. {
  3036. // we do not show comment and pagebreak question types
  3037. if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
  3038. {
  3039. $row['option_text'] = str_replace(array("\r","\n"),array('',''),$row['option_text']);
  3040. $return .= api_html_entity_decode(strip_tags($row['option_text']), ENT_QUOTES, $charset).';';
  3041. $possible_answers[$row['question_id']][$row['question_option_id']] =$row['question_option_id'];
  3042. $possible_answers_type[$row['question_id']] = $row['type'];
  3043. }
  3044. }
  3045. }
  3046. $return .= "\n";
  3047. // getting all the answers of the users
  3048. $old_user='';
  3049. $answers_of_user = array();
  3050. $sql = "SELECT * FROM $table_survey_answer WHERE survey_id='".Database::escape_string($_GET['survey_id'])."'";
  3051. if ($user_id<>0)
  3052. {
  3053. $sql .= "AND user='".Database::escape_string($user_id)."' ";
  3054. }
  3055. $sql .= "ORDER BY user ASC";
  3056. $open_question_iterator = 1;
  3057. $result = Database::query($sql, __FILE__, __LINE__);
  3058. while ($row = Database::fetch_array($result))
  3059. {
  3060. if ($old_user <> $row['user'] AND $old_user <> '')
  3061. {
  3062. $return .= SurveyUtil::export_complete_report_row($possible_answers, $answers_of_user, $old_user, $display_extra_user_fields);
  3063. $answers_of_user=array();
  3064. }
  3065. if($possible_answers_type[$row['question_id']] == 'open')
  3066. {
  3067. $temp_id = 'open'.$open_question_iterator;
  3068. $answers_of_user[$row['question_id']][$temp_id] = $row;
  3069. $open_question_iterator++;
  3070. }
  3071. else
  3072. {
  3073. $answers_of_user[$row['question_id']][$row['option_id']] = $row;
  3074. }
  3075. $old_user = $row['user'];
  3076. }
  3077. $return .= SurveyUtil::export_complete_report_row($possible_answers, $answers_of_user, $old_user, $display_extra_user_fields); // this is to display the last user
  3078. return $return;
  3079. }
  3080. /**
  3081. * Add a line to the csv file
  3082. *
  3083. * @param array Possible answers
  3084. * @param array User's answers
  3085. * @param mixed User ID or user details as string - Used as a string in the result string
  3086. * @param boolean Whether to display user fields or not
  3087. * @return string One line of the csv file
  3088. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3089. * @version February 2007
  3090. */
  3091. function export_complete_report_row($possible_options, $answers_of_user, $user, $display_extra_user_fields=false)
  3092. {
  3093. global $charset;
  3094. global $survey_data;
  3095. $return = '';
  3096. if ($survey_data['anonymous'] == 0)
  3097. {
  3098. if(intval($user)!==0)
  3099. {
  3100. $sql = 'SELECT firstname, lastname FROM '.Database::get_main_table(TABLE_MAIN_USER).' WHERE user_id='.intval($user);
  3101. $rs = Database::query($sql, __FILE__, __LINE__);
  3102. if($row = Database::fetch_array($rs))
  3103. {
  3104. $user_displayed = api_get_person_name($row['firstname'], $row['lastname']);
  3105. }
  3106. else
  3107. {
  3108. $user_displayed = '-';
  3109. }
  3110. //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
  3111. $return .= $user_displayed.';';
  3112. }
  3113. else
  3114. {
  3115. //echo ' <th>'.$user.'</th>'; // the user column
  3116. $return .= $user.';';
  3117. }
  3118. }
  3119. else
  3120. {
  3121. $return .= '-;'; // the user column
  3122. }
  3123. if($display_extra_user_fields)
  3124. {
  3125. //show user fields data, if any, for this user
  3126. $user_fields_values = UserManager::get_extra_user_data(intval($user),false,false);
  3127. foreach($user_fields_values as $value)
  3128. {
  3129. $return .= '"'.str_replace('"','""', api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset)).'";';
  3130. }
  3131. }
  3132. if(is_array($possible_options))
  3133. {
  3134. foreach ($possible_options as $question_id=>$possible_option)
  3135. {
  3136. if(is_array($possible_option) && count($possible_option)>0)
  3137. {
  3138. foreach ($possible_option as $option_id=>$value)
  3139. {
  3140. $my_answer_of_user=($answers_of_user[$question_id]==null) ? array() : $answers_of_user[$question_id];
  3141. $key = array_keys($my_answer_of_user);
  3142. if(substr($key[0],0,4)=='open')
  3143. {
  3144. $return .= '"'.str_replace('"','""',api_html_entity_decode(strip_tags($answers_of_user[$question_id][$key[0]]['option_id']), ENT_QUOTES, $charset)).'"';
  3145. }
  3146. elseif (!empty($answers_of_user[$question_id][$option_id]))
  3147. {
  3148. //$return .= 'v';
  3149. if ($answers_of_user[$question_id][$option_id]['value']<>0)
  3150. {
  3151. $return .= $answers_of_user[$question_id][$option_id]['value'];
  3152. }
  3153. else
  3154. {
  3155. $return .= 'v';
  3156. }
  3157. }
  3158. $return .= ';';
  3159. }
  3160. }
  3161. }
  3162. }
  3163. $return .= "\n";
  3164. return $return;
  3165. }
  3166. /**
  3167. * Quite similar to display_complete_report(), returns an HTML string
  3168. * that can be used in a csv file
  3169. * @todo consider merging this function with display_complete_report
  3170. * @return string The contents of a csv file
  3171. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3172. * @version February 2007
  3173. */
  3174. function export_complete_report_xls($filename, $user_id=0)
  3175. {
  3176. global $charset;
  3177. require_once(api_get_path(LIBRARY_PATH).'pear/Spreadsheet_Excel_Writer/Writer.php');
  3178. $workbook = new Spreadsheet_Excel_Writer();
  3179. $workbook->send($filename);
  3180. $worksheet =& $workbook->addWorksheet('Report 1');
  3181. $line = 0;
  3182. $column = 1; //skip the first column (row titles)
  3183. //show extra fields blank space (enough for extra fields on next line)
  3184. $display_extra_user_fields = false;
  3185. if (!($_POST['submit_question_filter'] OR $_POST['export_report']) OR !empty($_POST['fields_filter']))
  3186. {
  3187. //show user fields section with a big th colspan that spans over all fields
  3188. $extra_user_fields = UserManager::get_extra_fields(0,0,5,'ASC',false);
  3189. $num = count($extra_user_fields);
  3190. for($i=0;$i<$num;$i++)
  3191. {
  3192. $worksheet->write($line,$column,'');
  3193. $column ++;
  3194. }
  3195. $display_extra_user_fields = true;
  3196. }
  3197. // Database table definitions
  3198. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  3199. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  3200. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  3201. // First line (questions)
  3202. $sql = "SELECT questions.question_id, questions.type, questions.survey_question, count(options.question_option_id) as number_of_options
  3203. FROM $table_survey_question questions LEFT JOIN $table_survey_question_option options
  3204. ON questions.question_id = options.question_id "
  3205. ." WHERE questions.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  3206. GROUP BY questions.question_id "
  3207. ." ORDER BY questions.sort ASC";
  3208. $result = Database::query($sql, __FILE__, __LINE__);
  3209. while ($row = Database::fetch_array($result))
  3210. {
  3211. // we show the questions if
  3212. // 1. there is no question filter and the export button has not been clicked
  3213. // 2. there is a quesiton filter but the question is selected for display
  3214. if (!($_POST['submit_question_filter']) OR (is_array($_POST['questions_filter']) && in_array($row['question_id'], $_POST['questions_filter'])))
  3215. {
  3216. // we do not show comment and pagebreak question types
  3217. if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
  3218. {
  3219. if($row['number_of_options'] == 0 && $row['type']=='open')
  3220. {
  3221. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['survey_question']), ENT_QUOTES, $charset));
  3222. $column ++;
  3223. }
  3224. else
  3225. {
  3226. for ($ii = 0; $ii < $row['number_of_options']; $ii ++)
  3227. {
  3228. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['survey_question']), ENT_QUOTES, $charset));
  3229. $column ++;
  3230. }
  3231. }
  3232. }
  3233. }
  3234. }
  3235. $line++;
  3236. $column = 1;
  3237. //show extra field values
  3238. if ($display_extra_user_fields)
  3239. {
  3240. //show the fields names for user fields
  3241. foreach($extra_user_fields as $field)
  3242. {
  3243. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($field[3]), ENT_QUOTES, $charset));
  3244. $column++;
  3245. }
  3246. }
  3247. // getting all the questions and options (second line)
  3248. $sql = "SELECT survey_question.question_id, survey_question.survey_id, survey_question.survey_question, survey_question.display, survey_question.sort, survey_question.type,
  3249. survey_question_option.question_option_id, survey_question_option.option_text, survey_question_option.sort as option_sort
  3250. FROM $table_survey_question survey_question
  3251. LEFT JOIN $table_survey_question_option survey_question_option
  3252. ON survey_question.question_id = survey_question_option.question_id
  3253. WHERE survey_question.survey_id = '".Database::escape_string($_GET['survey_id'])."'
  3254. ORDER BY survey_question.sort ASC, survey_question_option.sort ASC";
  3255. $result = Database::query($sql, __FILE__, __LINE__);
  3256. $possible_answers = array();
  3257. $possible_answers_type = array();
  3258. while ($row = Database::fetch_array($result))
  3259. {
  3260. // we show the options if
  3261. // 1. there is no question filter and the export button has not been clicked
  3262. // 2. there is a quesiton filter but the question is selected for display
  3263. if (!($_POST['submit_question_filter']) OR (is_array($_POST['questions_filter']) && in_array($row['question_id'], $_POST['questions_filter'])))
  3264. {
  3265. // we do not show comment and pagebreak question types
  3266. if ($row['type'] <> 'comment' AND $row['type'] <> 'pagebreak')
  3267. {
  3268. $worksheet->write($line,$column,api_html_entity_decode(strip_tags($row['option_text']), ENT_QUOTES, $charset));
  3269. $possible_answers[$row['question_id']][$row['question_option_id']] =$row['question_option_id'];
  3270. $possible_answers_type[$row['question_id']] = $row['type'];
  3271. $column++;
  3272. }
  3273. }
  3274. }
  3275. // getting all the answers of the users
  3276. $line ++;
  3277. $column = 0;
  3278. $old_user='';
  3279. $answers_of_user = array();
  3280. $sql = "SELECT * FROM $table_survey_answer " .
  3281. "WHERE survey_id='".Database::escape_string($_GET['survey_id'])."' ";
  3282. if ($user_id<>0)
  3283. {
  3284. $sql .= "AND user='".Database::escape_string($user_id)."' ";
  3285. }
  3286. $sql .= "ORDER BY user ASC";
  3287. $open_question_iterator = 1;
  3288. $result = Database::query($sql, __FILE__, __LINE__);
  3289. while ($row = Database::fetch_array($result))
  3290. {
  3291. if ($old_user <> $row['user'] AND $old_user <> '')
  3292. {
  3293. $return = SurveyUtil::export_complete_report_row_xls($possible_answers, $answers_of_user, $old_user, $display_extra_user_fields);
  3294. foreach($return as $elem)
  3295. {
  3296. $worksheet->write($line,$column,$elem);
  3297. $column++;
  3298. }
  3299. $answers_of_user=array();
  3300. $line++;
  3301. $column=0;
  3302. }
  3303. if($possible_answers_type[$row['question_id']] == 'open')
  3304. {
  3305. $temp_id = 'open'.$open_question_iterator;
  3306. $answers_of_user[$row['question_id']][$temp_id] = $row;
  3307. $open_question_iterator++;
  3308. }
  3309. else
  3310. {
  3311. $answers_of_user[$row['question_id']][$row['option_id']] = $row;
  3312. }
  3313. $old_user = $row['user'];
  3314. }
  3315. $return = SurveyUtil::export_complete_report_row_xls($possible_answers, $answers_of_user, $old_user); // this is to display the last user
  3316. foreach($return as $elem)
  3317. {
  3318. $worksheet->write($line,$column,$elem);
  3319. $column++;
  3320. }
  3321. $workbook->close();
  3322. return null;
  3323. }
  3324. /**
  3325. * Add a line to the csv file
  3326. *
  3327. * @param array Possible answers
  3328. * @param array User's answers
  3329. * @param mixed User ID or user details as string - Used as a string in the result string
  3330. * @param boolean Whether to display user fields or not
  3331. * @return string One line of the csv file
  3332. */
  3333. function export_complete_report_row_xls($possible_options, $answers_of_user, $user, $display_extra_user_fields=false)
  3334. {
  3335. global $charset;
  3336. $return = array();
  3337. global $survey_data;
  3338. if ($survey_data['anonymous'] == 0)
  3339. {
  3340. if(intval($user)!==0)
  3341. {
  3342. $sql = 'SELECT firstname, lastname FROM '.Database::get_main_table(TABLE_MAIN_USER).' WHERE user_id='.intval($user);
  3343. $rs = Database::query($sql, __FILE__, __LINE__);
  3344. if($row = Database::fetch_array($rs))
  3345. {
  3346. $user_displayed = api_get_person_name($row['firstname'], $row['lastname']);
  3347. }
  3348. else
  3349. {
  3350. $user_displayed = '-';
  3351. }
  3352. //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
  3353. $return[] = $user_displayed;
  3354. }
  3355. else
  3356. {
  3357. //echo ' <th>'.$user.'</th>'; // the user column
  3358. $return[] = $user;
  3359. }
  3360. }
  3361. else
  3362. {
  3363. $return[] = '-'; // the user column
  3364. }
  3365. if($display_extra_user_fields)
  3366. {
  3367. //show user fields data, if any, for this user
  3368. $user_fields_values = UserManager::get_extra_user_data(intval($user),false,false);
  3369. foreach($user_fields_values as $value)
  3370. {
  3371. $return[] = api_html_entity_decode(strip_tags($value), ENT_QUOTES, $charset);
  3372. }
  3373. }
  3374. if(is_array($possible_options))
  3375. {
  3376. foreach ($possible_options as $question_id=>$possible_option)
  3377. {
  3378. if(is_array($possible_option) && count($possible_option)>0)
  3379. {
  3380. foreach ($possible_option as $option_id=>$value)
  3381. {
  3382. $my_answers_of_user=($answers_of_user[$question_id]==null) ? array() : $answers_of_user[$question_id];
  3383. $key = array_keys($my_answers_of_user);
  3384. if(substr($key[0],0,4)=='open')
  3385. {
  3386. $return[] = api_html_entity_decode(strip_tags($answers_of_user[$question_id][$key[0]]['option_id']), ENT_QUOTES, $charset);
  3387. }
  3388. elseif (!empty($answers_of_user[$question_id][$option_id]))
  3389. {
  3390. //$return .= 'v';
  3391. if ($answers_of_user[$question_id][$option_id]['value']<>0)
  3392. {
  3393. $return[] = $answers_of_user[$question_id][$option_id]['value'];
  3394. }
  3395. else
  3396. {
  3397. $return[] = 'v';
  3398. }
  3399. }
  3400. else
  3401. {
  3402. $return[] = '';
  3403. }
  3404. }
  3405. }
  3406. }
  3407. }
  3408. return $return;
  3409. }
  3410. /**
  3411. * This function displays the comparative report which allows you to compare two questions
  3412. * A comparative report creates a table where one question is on the x axis and a second question is on the y axis.
  3413. * In the intersection is the number of people who have answerd positive on both options.
  3414. *
  3415. * @return string HTML code
  3416. *
  3417. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3418. * @version February 2007
  3419. */
  3420. function display_comparative_report()
  3421. {
  3422. // allowed question types for comparative report
  3423. $allowed_question_types = array('yesno', 'multiplechoice', 'multipleresponse', 'dropdown', 'percentage', 'score');
  3424. // getting all the questions
  3425. $questions = survey_manager::get_questions($_GET['survey_id']);
  3426. // actions bar
  3427. echo '<div class="actions">';
  3428. 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>';
  3429. echo '</div>';
  3430. // displaying an information message that only the questions with predefined answers can be used in a comparative report
  3431. Display::display_normal_message(get_lang('OnlyQuestionsWithPredefinedAnswers'), false);
  3432. // The form for selecting the axis of the table
  3433. 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']).'">';
  3434. // survey_id
  3435. echo '<input type="hidden" name="action" value="'.Security::remove_XSS($_GET['action']).'"/>';
  3436. echo '<input type="hidden" name="survey_id" value="'.Security::remove_XSS($_GET['survey_id']).'"/>';
  3437. // X axis
  3438. echo get_lang('SelectXAxis').': ';
  3439. echo '<select name="xaxis">';
  3440. echo '<option value="">---</option>';
  3441. foreach ($questions as $key=>$question)
  3442. {
  3443. if (is_array($allowed_question_types))
  3444. {
  3445. if (in_array($question['type'], $allowed_question_types))
  3446. {
  3447. echo '<option value="'.$question['question_id'].'"';
  3448. if ($_GET['xaxis'] == $question['question_id'])
  3449. {
  3450. echo ' selected="selected"';
  3451. }
  3452. echo '">'.api_substr(strip_tags($question['question']), 0, 50).'</option>';
  3453. }
  3454. }
  3455. }
  3456. echo '</select><br /><br />';
  3457. // Y axis
  3458. echo get_lang('SelectYAxis').': ';
  3459. echo '<select name="yaxis">';
  3460. echo '<option value="">---</option>';
  3461. foreach ($questions as $key=>$question)
  3462. {
  3463. if (in_array($question['type'], $allowed_question_types))
  3464. {
  3465. echo '<option value="'.$question['question_id'].'"';
  3466. if ($_GET['yaxis'] == $question['question_id'])
  3467. {
  3468. echo ' selected="selected"';
  3469. }
  3470. echo '">'.api_substr(strip_tags($question['question']), 0, 50).'</option>';
  3471. }
  3472. }
  3473. echo '</select><br /><br />';
  3474. echo '<button class="save" type="submit" name="Submit" value="Submit">'.get_lang('CompareQuestions').'</button>';
  3475. echo '</form>';
  3476. // getting all the information of the x axis
  3477. if (isset($_GET['xaxis']) AND is_numeric($_GET['xaxis']))
  3478. {
  3479. $question_x = survey_manager::get_question($_GET['xaxis']);
  3480. }
  3481. // getting all the information of the y axis
  3482. if (isset($_GET['yaxis']) AND is_numeric($_GET['yaxis']))
  3483. {
  3484. $question_y = survey_manager::get_question($_GET['yaxis']);
  3485. }
  3486. if (isset($_GET['xaxis']) AND is_numeric($_GET['xaxis']) AND isset($_GET['yaxis']) AND is_numeric($_GET['yaxis']))
  3487. {
  3488. // getting the answers of the two questions
  3489. $answers_x = SurveyUtil::get_answers_of_question_by_user($_GET['survey_id'], $_GET['xaxis']);
  3490. $answers_y = SurveyUtil::get_answers_of_question_by_user($_GET['survey_id'], $_GET['yaxis']);
  3491. // displaying the table
  3492. echo '<table border="1" class="data_table">';
  3493. // the header
  3494. echo ' <tr>';
  3495. for ($ii=0; $ii<=count($question_x['answers']); $ii++)
  3496. {
  3497. if ($ii == 0)
  3498. {
  3499. echo ' <th>&nbsp;</th>';
  3500. }
  3501. else
  3502. {
  3503. if ($question_x['type']=='score')
  3504. {
  3505. for($x=1; $x<=$question_x['maximum_score']; $x++)
  3506. {
  3507. echo ' <th>'.$question_x['answers'][($ii-1)].'<br />'.$x.'</th>';
  3508. }
  3509. $x='';
  3510. }
  3511. else
  3512. {
  3513. echo ' <th>'.$question_x['answers'][($ii-1)].'</th>';
  3514. }
  3515. }
  3516. }
  3517. echo ' </tr>';
  3518. // the main part
  3519. for ($ij=0; $ij<count($question_y['answers']); $ij++)
  3520. {
  3521. // The Y axis is a scoring question type so we have more rows than the options (actually options * maximum score)
  3522. if ($question_y['type'] == 'score')
  3523. {
  3524. for($y=1; $y<=$question_y['maximum_score']; $y++)
  3525. {
  3526. echo ' <tr>';
  3527. for ($ii=0; $ii<=count($question_x['answers']); $ii++)
  3528. {
  3529. if ($question_x['type']=='score')
  3530. {
  3531. for($x=1; $x<=$question_x['maximum_score']; $x++)
  3532. {
  3533. if ($ii == 0)
  3534. {
  3535. echo ' <th>'.$question_y['answers'][($ij)].' '.$y.'</th>';
  3536. break;
  3537. }
  3538. else
  3539. {
  3540. echo ' <td align="center">';
  3541. echo SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], $x, $y);
  3542. echo '</td>';
  3543. }
  3544. }
  3545. }
  3546. else
  3547. {
  3548. if ($ii == 0)
  3549. {
  3550. echo ' <th>'.$question_y['answers'][($ij)].' '.$y.'</th>';
  3551. }
  3552. else
  3553. {
  3554. echo ' <td align="center">';
  3555. echo SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], 0, $y);
  3556. echo '</td>';
  3557. }
  3558. }
  3559. }
  3560. echo ' </tr>';
  3561. }
  3562. }
  3563. // The Y axis is NOT a score question type so the number of rows = the number of options
  3564. else
  3565. {
  3566. echo ' <tr>';
  3567. for ($ii=0; $ii<=count($question_x['answers']); $ii++)
  3568. {
  3569. if ($question_x['type']=='score')
  3570. {
  3571. for($x=1; $x<=$question_x['maximum_score']; $x++)
  3572. {
  3573. if ($ii == 0)
  3574. {
  3575. echo ' <th>'.$question_y['answers'][($ij)].'</th>';
  3576. break;
  3577. }
  3578. else
  3579. {
  3580. echo ' <td align="center">';
  3581. echo SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], $x, 0);
  3582. echo '</td>';
  3583. }
  3584. }
  3585. }
  3586. else
  3587. {
  3588. if ($ii == 0)
  3589. {
  3590. echo ' <th>'.$question_y['answers'][($ij)].'</th>';
  3591. }
  3592. else
  3593. {
  3594. echo ' <td align="center">';
  3595. echo SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)]);
  3596. echo '</td>';
  3597. }
  3598. }
  3599. }
  3600. echo ' </tr>';
  3601. }
  3602. }
  3603. echo '</table>';
  3604. }
  3605. }
  3606. /**
  3607. * Get all the answers of a question grouped by user
  3608. *
  3609. * @param integer Survey ID
  3610. * @param integer Question ID
  3611. * @return Array Array containing all answers of all users, grouped by user
  3612. *
  3613. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3614. * @version February 2007 - Updated March 2008
  3615. */
  3616. function get_answers_of_question_by_user($survey_id, $question_id)
  3617. {
  3618. // Database table definitions
  3619. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  3620. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  3621. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  3622. $sql = "SELECT * FROM $table_survey_answer
  3623. WHERE survey_id='".Database::escape_string($survey_id)."'
  3624. AND question_id='".Database::escape_string($question_id)."'
  3625. ORDER BY USER ASC";
  3626. $result = Database::query($sql, __FILE__, __LINE__);
  3627. while ($row = Database::fetch_array($result))
  3628. {
  3629. if ($row['value'] == 0)
  3630. {
  3631. $return[$row['user']][] = $row['option_id'];
  3632. }
  3633. else
  3634. {
  3635. $return[$row['user']][] = $row['option_id'].'*'.$row['value'];
  3636. }
  3637. }
  3638. return $return;
  3639. }
  3640. /**
  3641. * Count the number of users who answer positively on both options
  3642. *
  3643. * @param array All answers of the x axis
  3644. * @param array All answers of the y axis
  3645. * @param integer x axis value (= the option_id of the first question)
  3646. * @param integer y axis value (= the option_id of the second question)
  3647. * @return integer Number of users who have answered positively to both options
  3648. *
  3649. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3650. * @version February 2007
  3651. */
  3652. function comparative_check($answers_x, $answers_y, $option_x, $option_y, $value_x=0, $value_y=0)
  3653. {
  3654. if ($value_x==0)
  3655. {
  3656. $check_x = $option_x;
  3657. }
  3658. else
  3659. {
  3660. $check_x = $option_x.'*'.$value_x;
  3661. }
  3662. if ($value_y==0)
  3663. {
  3664. $check_y = $option_y;
  3665. }
  3666. else
  3667. {
  3668. $check_y = $option_y.'*'.$value_y;
  3669. }
  3670. $counter = 0;
  3671. if(is_array($answers_x)){
  3672. foreach ($answers_x as $user => $answers)
  3673. {
  3674. // check if the user has given $option_x as answer
  3675. if (in_array($check_x, $answers))
  3676. {
  3677. // check if the user has given $option_y as an answer
  3678. if (in_array($check_y, $answers_y[$user]))
  3679. {
  3680. $counter++;
  3681. }
  3682. }
  3683. }
  3684. }
  3685. return $counter;
  3686. }
  3687. /**
  3688. * Get all the information about the invitations of a certain survey
  3689. *
  3690. * @return array Lines of invitation [user, code, date, empty element]
  3691. *
  3692. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3693. * @version January 2007
  3694. *
  3695. * @todo use survey_id parameter instead of $_GET
  3696. */
  3697. function get_survey_invitations_data()
  3698. {
  3699. // Database table definition
  3700. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  3701. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  3702. $sql = "SELECT
  3703. survey_invitation.user as col1,
  3704. survey_invitation.invitation_code as col2,
  3705. survey_invitation.invitation_date as col3,
  3706. '' as col4
  3707. FROM $table_survey_invitation survey_invitation
  3708. LEFT JOIN $table_user user ON survey_invitation.user = user.user_id
  3709. WHERE survey_invitation.survey_id = '".Database::escape_string($_GET['survey_id'])."' AND session_id='".api_get_session_id()."' ";
  3710. $res = Database::query($sql, __FILE__, __LINE__);
  3711. while ($row = Database::fetch_array($res))
  3712. {
  3713. $survey_invitation_data[] = $row;
  3714. }
  3715. return $survey_invitation_data;
  3716. }
  3717. /**
  3718. * Get the total number of survey invitations for a given survey (through $_GET['survey_id'])
  3719. *
  3720. * @return integer Total number of survey invitations
  3721. *
  3722. * @todo use survey_id parameter instead of $_GET
  3723. *
  3724. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3725. * @version January 2007
  3726. */
  3727. function get_number_of_survey_invitations()
  3728. {
  3729. // Database table definition
  3730. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  3731. $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()."' ";
  3732. $res = Database::query($sql, __FILE__, __LINE__);
  3733. $row = Database::fetch_array($res,'ASSOC');
  3734. return $row['total'];
  3735. }
  3736. /**
  3737. * Save the invitation mail
  3738. *
  3739. * @param string Text of the e-mail
  3740. * @param integer Whether the mail contents are for invite mail (0, default) or reminder mail (1)
  3741. *
  3742. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3743. * @version January 2007
  3744. */
  3745. function save_invite_mail($mailtext, $mail_subject, $reminder=0) {
  3746. // Database table definition
  3747. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  3748. // reminder or not
  3749. if ($reminder == 0) {
  3750. $mail_field = 'invite_mail';
  3751. }
  3752. else {
  3753. $mail_field = 'reminder_mail';
  3754. }
  3755. $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'])."'";
  3756. $result = Database::query($sql, __FILE__, __LINE__);
  3757. }
  3758. /**
  3759. * This function saves all the invitations of course users and additional users in the database
  3760. * and sends the invitations by email
  3761. *
  3762. * @param array Users array can be both a list of course uids AND a list of additional emailaddresses
  3763. * @param string Title of the invitation, used as the title of the mail
  3764. * @param string Text of the invitation, used as the text of the mail.
  3765. * The text has to contain a **link** string or this will automatically be added to the end
  3766. *
  3767. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3768. * @version January 2007
  3769. *
  3770. */
  3771. function save_invitations($users_array, $invitation_title, $invitation_text,
  3772. $reminder=0, $sendmail=0, $remindUnAnswered=0) {
  3773. global $charset;
  3774. if (!is_array($users_array)) return 0; // should not happen
  3775. // getting the survey information
  3776. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  3777. // Database table to store the invitations data
  3778. $table_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION);
  3779. $survey_invitations = array();
  3780. $survey_invitations = SurveyUtil::get_invitations($survey_data['survey_code']);
  3781. $already_invited = array();
  3782. $already_invited = SurveyUtil::get_invited_users($survey_data['code']);
  3783. // remind unanswered is a special version of remind all reminder
  3784. $exclude_users = array();
  3785. if ($remindUnAnswered == 1) { // remind only unanswered users
  3786. $reminder = 1;
  3787. $exclude_users = survey_manager::get_people_who_filled_survey($_GET['survey_id']);
  3788. }
  3789. $counter = 0; //nr of invitations "sent" (if sendmail option)
  3790. foreach ($users_array as $key=>$value) {
  3791. if(!isset($value) || $value=="") continue;
  3792. // skip user if reminding only unanswered people
  3793. if (in_array($value, $exclude_users)) continue;
  3794. // get the unique invitation code if we already have it
  3795. if ($reminder == 1 AND array_key_exists($value,$survey_invitations)) {
  3796. $invitation_code = $survey_invitations[$value]['invitation_code'];
  3797. }
  3798. else {
  3799. $invitation_code = md5($value.microtime());
  3800. }
  3801. $newUser = false; // user not already invited
  3802. // store the invitation if user_id not in $already_invited['course_users'] OR email is not in $already_invited['additional_users']
  3803. $addit_users_array = explode(';',$already_invited['additional_users']);
  3804. $my_alredy_invited=($already_invited['course_users']==null) ? array() : $already_invited['course_users'];
  3805. if ((is_numeric($value) AND !in_array($value,$my_alredy_invited)) OR
  3806. (!is_numeric($value) AND !in_array($value,$addit_users_array))) {
  3807. $newUser = true;
  3808. if (!array_key_exists($value,$survey_invitations)) {
  3809. $sql = "INSERT INTO $table_survey_invitation (user, survey_code, invitation_code, invitation_date) VALUES
  3810. ('".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'))."')";
  3811. $result = Database::query($sql, __FILE__, __LINE__);
  3812. }
  3813. }
  3814. // send the email if checkboxed
  3815. if (($newUser==true || $reminder==1) && $sendmail<>0) {
  3816. // make a change for absolute url
  3817. if (isset($invitation_text)) {
  3818. $invitation_text = api_html_entity_decode(api_convert_encoding($invitation_text, $charset, $_SESSION['oLP']->encoding), ENT_QUOTES, $charset);
  3819. $invitation_text = str_replace('src="../../','src="'.api_get_path(WEB_PATH).'', $invitation_text);
  3820. $invitation_text = trim(stripslashes($invitation_text));
  3821. }
  3822. SurveyUtil::send_invitation_mail($value, $invitation_code, $invitation_title, $invitation_text);
  3823. $counter++;
  3824. }
  3825. }
  3826. return $counter; // number of invitations sent
  3827. }
  3828. /**
  3829. *
  3830. * Send the invitation by mail.
  3831. *
  3832. * @param invitedUser - the userId (course user) or emailaddress of additional user
  3833. * $param $invitation_code - the unique invitation code for the URL
  3834. * @return void
  3835. *
  3836. */
  3837. function send_invitation_mail($invitedUser, $invitation_code, $invitation_title, $invitation_text) {
  3838. global $_user;
  3839. global $_course;
  3840. global $_configuration;
  3841. $portal_url = $_configuration['root_web'];
  3842. if ($_configuration['multiple_access_urls']==true) {
  3843. $access_url_id = api_get_current_access_url_id();
  3844. if ($access_url_id != -1 ) {
  3845. $url = api_get_access_url($access_url_id);
  3846. $portal_url = $url['url'];
  3847. }
  3848. }
  3849. // replacing the **link** part with a valid link for the user
  3850. $survey_link = $portal_url.$_configuration['code_append'].'survey/'.'fillsurvey.php?course='.$_course['sysCode'].'&invitationcode='.$invitation_code;
  3851. $text_link = '<a href="'.$survey_link.'">'.get_lang('ClickHereToAnswerTheSurvey')."</a><br />\r\n<br />\r\n".get_lang('OrCopyPasteTheFollowingUrl')." <br />\r\n ".$survey_link;
  3852. $replace_count = 0;
  3853. $full_invitation_text = api_str_ireplace('**link**', $text_link ,$invitation_text, $replace_count);
  3854. if ($replace_count < 1) {
  3855. $full_invitation_text = $full_invitation_text . "<br />\r\n<br />\r\n".$text_link;
  3856. }
  3857. // optionally: finding the e-mail of the course user
  3858. if (is_numeric($invitedUser)) {
  3859. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  3860. $sql = "SELECT firstname, lastname, email FROM $table_user WHERE user_id='".Database::escape_string($invitedUser)."'";
  3861. $result = Database::query($sql, __FILE__, __LINE__);
  3862. $row = Database::fetch_array($result);
  3863. $recipient_email = $row['email'];
  3864. $recipient_name = api_get_person_name($row['firstname'], $row['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  3865. }
  3866. else {
  3867. /** @todo check if the address is a valid email */
  3868. $recipient_email = $invitedUser;
  3869. }
  3870. // sending the mail
  3871. $sender_name = api_get_person_name($_user['firstName'], $_user['lastName'], null, PERSON_NAME_EMAIL_ADDRESS);
  3872. $sender_email = $_user['mail'];
  3873. $replyto = array();
  3874. if(api_get_setting('survey_email_sender_noreply')=='noreply') {
  3875. $noreply = api_get_setting('noreply_email_address');
  3876. if(!empty($noreply)) {
  3877. $replyto['Reply-to'] = $noreply;
  3878. $sender_name = $noreply;
  3879. $sender_email = $noreply;
  3880. }
  3881. }
  3882. api_mail_html($recipient_name, $recipient_email, $invitation_title, $full_invitation_text, $sender_name, $sender_email, $replyto);
  3883. }
  3884. /**
  3885. * This function recalculates the number of users who have been invited and updates the survey table with this value.
  3886. *
  3887. * @param string Survey code
  3888. * @return void
  3889. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3890. * @version January 2007
  3891. */
  3892. function update_count_invited($survey_code)
  3893. {
  3894. // Database table definition
  3895. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  3896. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  3897. // counting the number of people that are invited
  3898. $sql = "SELECT count(user) as total FROM $table_survey_invitation WHERE survey_code = '".Database::escape_string($survey_code)."'";
  3899. $result = Database::query($sql, __FILE__, __LINE__);
  3900. $row = Database::fetch_array($result);
  3901. $total_invited = $row['total'];
  3902. // updating the field in the survey table
  3903. $sql = "UPDATE $table_survey SET invited = '".Database::escape_string($total_invited)."' WHERE code = '".Database::escape_string($survey_code)."'";
  3904. $result = Database::query($sql, __FILE__, __LINE__);
  3905. }
  3906. /**
  3907. * This function gets all the invited users for a given survey code.
  3908. *
  3909. * @param string Survey code
  3910. * @param string optional - course database
  3911. * @return array Array containing the course users and additional users (non course users)
  3912. *
  3913. * @todo consider making $defaults['additional_users'] also an array
  3914. *
  3915. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3916. * @version January 2007
  3917. */
  3918. function get_invited_users($survey_code,$course_db='')
  3919. {
  3920. // Database table definition
  3921. if (!empty($course_db)) {
  3922. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION,$course_db);
  3923. } else {
  3924. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  3925. }
  3926. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  3927. // Selecting all the invitations of this survey AND the additional emailaddresses (the left join)
  3928. $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname' : ' ORDER BY lastname, firstname';
  3929. $sql = "SELECT user
  3930. FROM $table_survey_invitation as table_invitation
  3931. LEFT JOIN $table_user as table_user
  3932. ON table_invitation.user = table_user.user_id
  3933. WHERE survey_code='".Database::escape_string($survey_code)."'".$order_clause;
  3934. $defaults = array();
  3935. $defaults['course_users'] = array();
  3936. $defaults['additional_users'] = '';
  3937. $result = Database::query($sql, __FILE__, __LINE__);
  3938. while ($row = Database::fetch_array($result))
  3939. {
  3940. if (is_numeric($row['user']))
  3941. {
  3942. $defaults['course_users'][] = $row['user'];
  3943. }
  3944. else
  3945. {
  3946. if (empty($defaults['additional_users']))
  3947. {
  3948. $defaults['additional_users'] = $row['user'];
  3949. }
  3950. else
  3951. {
  3952. $defaults['additional_users'] .= ';'.$row['user'];
  3953. }
  3954. }
  3955. }
  3956. return $defaults;
  3957. }
  3958. /**
  3959. * Get all the invitations
  3960. *
  3961. * @param string Survey code
  3962. * @return array Database rows matching the survey code
  3963. *
  3964. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3965. * @version September 2007
  3966. */
  3967. function get_invitations($survey_code)
  3968. {
  3969. // Database table definition
  3970. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  3971. $sql = "SELECT * FROM $table_survey_invitation WHERE survey_code = '".Database::escape_string($survey_code)."'";
  3972. $result = Database::query($sql, __FILE__, __LINE__);
  3973. $return = array();
  3974. while ($row = Database::fetch_array($result))
  3975. {
  3976. $return[$row['user']] = $row;
  3977. }
  3978. return $return;
  3979. }
  3980. /**
  3981. * This function displays the form for searching a survey
  3982. *
  3983. * @return void (direct output)
  3984. *
  3985. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3986. * @version January 2007
  3987. *
  3988. * @todo use quickforms
  3989. * @todo consider moving this to surveymanager.inc.lib.php
  3990. */
  3991. function display_survey_search_form()
  3992. {
  3993. echo '<form method="get" action="survey_list.php?search=advanced">';
  3994. echo '<div class="row"><div class="form_header">'.get_lang('SearchASurvey').'</div></div>';
  3995. echo ' <div class="row">
  3996. <div class="label">
  3997. '.get_lang('Title').'
  3998. </div>
  3999. <div class="formw">
  4000. <input type="text" id="search_title" name="keyword_title"/>
  4001. </div>
  4002. </div>';
  4003. echo ' <div class="row">
  4004. <div class="label">
  4005. '.get_lang('Code').'
  4006. </div>
  4007. <div class="formw">
  4008. <input type="text" name="keyword_code"/>
  4009. </div>
  4010. </div>';
  4011. echo ' <div class="row">
  4012. <div class="label">
  4013. '.get_lang('Language').'
  4014. </div>
  4015. <div class="formw">';
  4016. echo ' <select name="keyword_language"><option value="%">'.get_lang('All').'</option>';
  4017. $languages = api_get_languages();
  4018. foreach ($languages['name'] as $index => $name)
  4019. {
  4020. echo '<option value="'.$languages['folder'][$index].'">'.$name.'</option>';
  4021. }
  4022. echo ' </select>';
  4023. echo ' </div>
  4024. </div>';
  4025. echo '<input type="hidden" name="cidReq" value="'.api_get_course_id().'"/>';
  4026. echo ' <div class="row">
  4027. <div class="label">
  4028. </div>
  4029. <div class="formw">
  4030. <button class="search" type="submit" name="do_search">'.get_lang('Search').'</button>
  4031. </div>
  4032. </div>';
  4033. echo '</form>';
  4034. echo '<div style="clear: both;margin-bottom: 10px;"></div>';
  4035. }
  4036. /**
  4037. * This function displays the sortable table with all the surveys
  4038. *
  4039. * @return void (direct output)
  4040. *
  4041. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4042. * @version January 2007
  4043. */
  4044. function display_survey_list()
  4045. {
  4046. $parameters = array();
  4047. $parameters['cidReq']=api_get_course_id();
  4048. if ($_GET['do_search'])
  4049. {
  4050. $message = get_lang('DisplaySearchResults').'<br />';
  4051. $message .= '<a href="'.api_get_self().'?'.api_get_cidreq().'">'.get_lang('DisplayAll').'</a>';
  4052. Display::display_normal_message($message, false);
  4053. }
  4054. // Create a sortable table with survey-data
  4055. $table = new SortableTable('surveys', 'get_number_of_surveys', 'get_survey_data',2);
  4056. $table->set_additional_parameters($parameters);
  4057. $table->set_header(0, '', false);
  4058. $table->set_header(1, get_lang('SurveyName'));
  4059. $table->set_header(2, get_lang('SurveyCode'));
  4060. $table->set_header(3, get_lang('NumberOfQuestions'));
  4061. $table->set_header(4, get_lang('Author'));
  4062. $table->set_header(5, get_lang('Language'));
  4063. //$table->set_header(6, get_lang('Shared'));
  4064. $table->set_header(6, get_lang('AvailableFrom'));
  4065. $table->set_header(7, get_lang('AvailableUntil'));
  4066. $table->set_header(8, get_lang('Invite'));
  4067. $table->set_header(9, get_lang('Anonymous'));
  4068. $table->set_header(10, get_lang('Modify'), false,'width="150"');
  4069. $table->set_column_filter(9, 'anonymous_filter');
  4070. $table->set_column_filter(10, 'modify_filter');
  4071. $table->set_form_actions(array ('delete' => get_lang('DeleteSurvey')));
  4072. $table->display();
  4073. }
  4074. function display_survey_list_for_coach()
  4075. {
  4076. $parameters = array();
  4077. $parameters['cidReq']=api_get_course_id();
  4078. if(isset($_GET['do_search']))
  4079. {
  4080. $message = get_lang('DisplaySearchResults').'<br />';
  4081. $message .= '<a href="'.api_get_self().'?'.api_get_cidreq().'">'.get_lang('DisplayAll').'</a>';
  4082. Display::display_normal_message($message, false);
  4083. }
  4084. // Create a sortable table with survey-data
  4085. $table = new SortableTable('surveys', 'get_number_of_surveys_for_coach', 'get_survey_data_for_coach',2);
  4086. $table->set_additional_parameters($parameters);
  4087. $table->set_header(0, '', false);
  4088. $table->set_header(1, get_lang('SurveyName'));
  4089. $table->set_header(2, get_lang('SurveyCode'));
  4090. $table->set_header(3, get_lang('NumberOfQuestions'));
  4091. $table->set_header(4, get_lang('Author'));
  4092. $table->set_header(5, get_lang('Language'));
  4093. //$table->set_header(6, get_lang('Shared'));
  4094. $table->set_header(6, get_lang('AvailableFrom'));
  4095. $table->set_header(7, get_lang('AvailableUntil'));
  4096. $table->set_header(8, get_lang('Invite'));
  4097. $table->set_header(9, get_lang('Anonymous'));
  4098. $table->set_header(10, get_lang('Modify'), false,'width="130"');
  4099. $table->set_column_filter(9, 'anonymous_filter');
  4100. $table->set_column_filter(10, 'modify_filter_for_coach');
  4101. $table->display();
  4102. }
  4103. /**
  4104. * This function changes the modify column of the sortable table
  4105. *
  4106. * @param integer $survey_id the id of the survey
  4107. * @return html code that are the actions that can be performed on any survey
  4108. *
  4109. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4110. * @version January 2007
  4111. */
  4112. function modify_filter($survey_id)
  4113. {
  4114. global $charset;
  4115. $survey_id = Security::remove_XSS($survey_id);
  4116. $return = '';
  4117. // coach can see that only if the survey is in his session
  4118. if(api_is_allowed_to_edit() || api_is_element_in_the_session(TOOL_SURVEY, $survey_id))
  4119. {
  4120. $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;';
  4121. $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;';
  4122. $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;';
  4123. }
  4124. //$return .= '<a href="create_survey_in_another_language.php?id_survey='.$survey_id.'">'.Display::return_icon('copy.gif', get_lang('Copy')).'</a>';
  4125. //$return .= '<a href="survey.php?survey_id='.$survey_id.'">'.Display::return_icon('add.gif', get_lang('Add')).'</a>';
  4126. $return .= '<a href="preview.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.Display::return_icon('preview.gif', get_lang('Preview')).'</a>&nbsp;';
  4127. $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;';
  4128. $return .= '<a href="reporting.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.Display::return_icon('statistics.gif', get_lang('Reporting')).'</a>';
  4129. return $return;
  4130. }
  4131. function modify_filter_for_coach($survey_id)
  4132. {
  4133. global $charset;
  4134. $survey_id = Security::remove_XSS($survey_id);
  4135. // $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>';
  4136. //$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>';
  4137. //$return .= '<a href="create_survey_in_another_language.php?id_survey='.$survey_id.'">'.Display::return_icon('copy.gif', get_lang('Copy')).'</a>';
  4138. //$return .= '<a href="survey.php?survey_id='.$survey_id.'">'.Display::return_icon('add.gif', get_lang('Add')).'</a>';
  4139. $return .= '<a href="preview.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.Display::return_icon('preview.gif', get_lang('Preview')).'</a>&nbsp;';
  4140. $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;';
  4141. $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;';
  4142. //$return .= '<a href="reporting.php?'.api_get_cidreq().'&amp;survey_id='.$survey_id.'">'.Display::return_icon('statistics.gif', get_lang('Reporting')).'</a>';
  4143. return $return;
  4144. }
  4145. /**
  4146. * Returns "yes" when given parameter is one, "no" for any other value
  4147. * @param integer Whether anonymous or not
  4148. * @return string "Yes" or "No" in the current language
  4149. */
  4150. function anonymous_filter($anonymous)
  4151. {
  4152. if ($anonymous == 1)
  4153. {
  4154. return get_lang('Yes');
  4155. }
  4156. else
  4157. {
  4158. return get_lang('No');
  4159. }
  4160. }
  4161. /**
  4162. * This function handles the search restriction for the SQL statements
  4163. *
  4164. * @return string Part of a SQL statement or false on error
  4165. *
  4166. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4167. * @version January 2007
  4168. */
  4169. function survey_search_restriction()
  4170. {
  4171. if(isset($_GET['do_search']))
  4172. {
  4173. if ($_GET['keyword_title']<>'')
  4174. {
  4175. $search_term[] = 'title like "%" \''.Database::escape_string($_GET['keyword_title']).'\' "%"';
  4176. }
  4177. if ($_GET['keyword_code']<>'')
  4178. {
  4179. $search_term[] = 'code =\''.Database::escape_string($_GET['keyword_code']).'\'';
  4180. }
  4181. if ($_GET['keyword_language']<>'%')
  4182. {
  4183. $search_term[] = 'lang =\''.Database::escape_string($_GET['keyword_language']).'\'';
  4184. }
  4185. $my_search_term=($search_term==null) ? array() : $search_term;
  4186. $search_restriction = implode(' AND ', $my_search_term);
  4187. return $search_restriction;
  4188. }
  4189. else
  4190. {
  4191. return false;
  4192. }
  4193. }
  4194. /**
  4195. * This function calculates the total number of surveys
  4196. *
  4197. * @return integer Total number of surveys
  4198. *
  4199. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4200. * @version January 2007
  4201. */
  4202. function get_number_of_surveys()
  4203. {
  4204. global $table_survey;
  4205. $search_restriction = SurveyUtil::survey_search_restriction();
  4206. if ($search_restriction)
  4207. {
  4208. $search_restriction = 'WHERE '.$search_restriction;
  4209. }
  4210. $sql = "SELECT count(survey_id) AS total_number_of_items FROM ".$table_survey.' '.$search_restriction;
  4211. $res = Database::query($sql, __FILE__, __LINE__);
  4212. $obj = Database::fetch_object($res);
  4213. return $obj->total_number_of_items;
  4214. }
  4215. function get_number_of_surveys_for_coach()
  4216. {
  4217. /*global $table_survey;
  4218. $search_restriction = SurveyUtil::survey_search_restriction();
  4219. if ($search_restriction)
  4220. {
  4221. $search_restriction = 'WHERE '.$search_restriction;
  4222. }
  4223. $sql = "SELECT count(survey_id) AS total_number_of_items FROM ".$table_survey.' '.$search_restriction;
  4224. $res = Database::query($sql, __FILE__, __LINE__);
  4225. $obj = Database::fetch_object($res);
  4226. return $obj->total_number_of_items;
  4227. */
  4228. //ugly fix
  4229. require_once(api_get_path(LIBRARY_PATH).'surveymanager.lib.php');
  4230. $survey_tree = new SurveyTree();
  4231. return count($survey_tree->get_last_children_from_branch($survey_tree->surveylist));
  4232. }
  4233. /**
  4234. * This function gets all the survey data that is to be displayed in the sortable table
  4235. *
  4236. * @param unknown_type $from
  4237. * @param unknown_type $number_of_items
  4238. * @param unknown_type $column
  4239. * @param unknown_type $direction
  4240. * @return unknown
  4241. *
  4242. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4243. * @version January 2007
  4244. */
  4245. function get_survey_data($from, $number_of_items, $column, $direction)
  4246. {
  4247. global $table_survey;
  4248. global $table_user;
  4249. global $table_survey_question;
  4250. // searching
  4251. $search_restriction = SurveyUtil::survey_search_restriction();
  4252. if ($search_restriction)
  4253. {
  4254. $search_restriction = ' AND '.$search_restriction;
  4255. }
  4256. //condition for the session
  4257. $session_id = api_get_session_id();
  4258. $condition_session = api_get_session_condition($session_id);
  4259. //IF(is_shared<>0,'V','-') AS col6,
  4260. $sql = "SELECT
  4261. survey.survey_id AS col0,
  4262. CONCAT('<a href=\"survey.php?survey_id=',survey.survey_id,'\">',survey.title,'</a>') AS col1,
  4263. survey.code AS col2,
  4264. count(survey_question.question_id) AS col3,
  4265. ".(api_is_western_name_order() ? "CONCAT(user.firstname, ' ', user.lastname)" : "CONCAT(user.lastname, ' ', user.firstname)")." AS col4,
  4266. survey.lang AS col5,
  4267. survey.avail_from AS col6,
  4268. survey.avail_till AS col7,
  4269. 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,
  4270. survey.anonymous AS col9,
  4271. survey.survey_id AS col10,
  4272. survey.session_id AS session_id
  4273. FROM $table_survey survey
  4274. LEFT JOIN $table_survey_question survey_question ON survey.survey_id = survey_question.survey_id
  4275. , $table_user user
  4276. WHERE survey.author = user.user_id
  4277. $search_restriction
  4278. $condition_session
  4279. ";
  4280. $sql .= " GROUP BY survey.survey_id";
  4281. $sql .= " ORDER BY col$column $direction ";
  4282. $sql .= " LIMIT $from,$number_of_items";
  4283. $res = Database::query($sql, __FILE__, __LINE__);
  4284. $surveys = array();
  4285. $array = array();
  4286. while ($survey = Database::fetch_array($res)) {
  4287. $array[0] = $survey[0];
  4288. $array[1] = $survey[1];
  4289. //validacion when belongs to a session
  4290. $session_img = api_get_session_image($survey['session_id'], $_user['status']);
  4291. $array[2] = $survey[2] . $session_img;
  4292. $array[3] = $survey[3];
  4293. $array[4] = $survey[4];
  4294. $array[5] = $survey[5];
  4295. $array[6] = $survey[6];
  4296. $array[7] = $survey[7];
  4297. $array[8] = $survey[8];
  4298. $array[9] = $survey[9];
  4299. $array[10] = $survey[10];
  4300. $surveys[] = $array;
  4301. }
  4302. return $surveys;
  4303. }
  4304. function get_survey_data_for_coach($from, $number_of_items, $column, $direction)
  4305. {
  4306. //echo '<pre>';
  4307. //echo '----------------';
  4308. require_once(api_get_path(LIBRARY_PATH).'surveymanager.lib.php');
  4309. $survey_tree = new SurveyTree();
  4310. $last_version_surveys = $survey_tree->get_last_children_from_branch($survey_tree->surveylist);
  4311. //echo '----------------';
  4312. //print_r($last_version_surveys);
  4313. $list=array();
  4314. foreach($last_version_surveys as $survey)
  4315. {
  4316. $list[]=$survey['id'];
  4317. }
  4318. if (count($list)>0)
  4319. {
  4320. $list_condition = " AND survey.survey_id IN (".implode(',',$list).") ";
  4321. }
  4322. else
  4323. {
  4324. $list_condition ='';
  4325. }
  4326. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  4327. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  4328. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  4329. //print_r($survey_tree->surveylist);
  4330. //IF(is_shared<>0,'V','-') AS col6,
  4331. $sql = "SELECT
  4332. survey.survey_id AS col0,
  4333. survey.title AS col1,
  4334. survey.code AS col2,
  4335. count(survey_question.question_id) AS col3,
  4336. ".(api_is_western_name_order() ? "CONCAT(user.firstname, ' ', user.lastname)" : "CONCAT(user.lastname, ' ', user.firstname)")." AS col4,
  4337. survey.lang AS col5,
  4338. survey.avail_from AS col6,
  4339. survey.avail_till AS col7,
  4340. 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,
  4341. survey.anonymous AS col9,
  4342. survey.survey_id AS col10
  4343. FROM $table_survey survey
  4344. LEFT JOIN $table_survey_question survey_question ON survey.survey_id = survey_question.survey_id
  4345. , $table_user user
  4346. WHERE survey.author = user.user_id $list_condition
  4347. $search_restriction
  4348. ";
  4349. $sql .= " GROUP BY survey.survey_id";
  4350. $sql .= " ORDER BY col$column $direction ";
  4351. $sql .= " LIMIT $from,$number_of_items";
  4352. $res = Database::query($sql, __FILE__, __LINE__);
  4353. $surveys = array ();
  4354. while ($survey = Database::fetch_array($res))
  4355. {
  4356. $surveys[] = $survey;
  4357. }
  4358. return $surveys;
  4359. }
  4360. /**
  4361. * Display all the active surveys for the given course user
  4362. *
  4363. * @param unknown_type $user_id
  4364. *
  4365. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4366. * @version April 2007
  4367. */
  4368. function survey_list_user($user_id)
  4369. {
  4370. global $_course;
  4371. // Database table definitions
  4372. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  4373. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  4374. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  4375. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  4376. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  4377. $all_question_id=array();
  4378. $sql='SELECT question_id from '.$table_survey_question;
  4379. $result=Database::query($sql,__FILE__,__LINE__);
  4380. while($row=Database::fetch_array($result,'ASSOC')) {
  4381. $all_question_id[]=$row;
  4382. }
  4383. $count=0;
  4384. for ($i=0;$i<count($all_question_id);$i++) {
  4385. $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();
  4386. $result=Database::query($sql,__FILE__,__LINE__);
  4387. while($row=Database::fetch_array($result,'ASSOC')) {
  4388. if ($row['count'] == 0) {
  4389. $count++;
  4390. break;
  4391. }
  4392. }
  4393. if ($count>0) {
  4394. $link_add=true;
  4395. break;
  4396. }
  4397. }
  4398. echo '<table class="data_table">';
  4399. echo '<tr>';
  4400. echo ' <th>'.get_lang('SurveyName').'</th>';
  4401. echo ' <th>'.get_lang('Anonymous').'</th>';
  4402. echo '</tr>';
  4403. $sql = "SELECT * FROM $table_survey survey, $table_survey_invitation survey_invitation
  4404. WHERE survey_invitation.user = '".Database::escape_string($user_id)."'
  4405. AND survey.code = survey_invitation.survey_code
  4406. AND survey.avail_from <= '".date('Y-m-d H:i:s')."'
  4407. AND survey.avail_till >= '".date('Y-m-d H:i:s')."'
  4408. ";
  4409. $result = Database::query($sql, __FILE__, __LINE__);
  4410. $counter = 0;
  4411. while ($row = Database::fetch_array($result,'ASSOC')) {
  4412. // get the user into survey answer table (user or anonymus)
  4413. $sql = "SELECT user FROM $table_survey_answer
  4414. WHERE survey_id = (SELECT survey_id from $table_survey WHERE code ='".Database::escape_string($row['code'])."')";
  4415. $result_answer = Database::query($sql, __FILE__, __LINE__);
  4416. $row_answer = Database::fetch_array($result_answer,'ASSOC');
  4417. echo '<tr>';
  4418. if ($row['answered'] == 0)
  4419. {
  4420. echo '<td><a href="fillsurvey.php?course='.$_course['sysCode'].'&amp;invitationcode='.$row['invitation_code'].'&amp;cidReq='.$_course['sysCode'].'">'.$row['title'].'</a></td>';
  4421. }
  4422. else
  4423. {
  4424. echo '<td>'.$row['title'].'</td>';
  4425. }
  4426. echo '<td>';
  4427. echo ($row['anonymous'] == 1)?get_lang('Yes'):get_lang('No');
  4428. echo '</td>';
  4429. echo '</tr>';
  4430. if ($row['anonymous'] == 1) {
  4431. $current_user_id=$_SESSION['surveyuser'];
  4432. } else {
  4433. $current_user_id=api_get_user_id();
  4434. }
  4435. $link_available=self::show_link_available(api_get_user_id(),$row['code'],$current_user_id);
  4436. if ($link_add===true && $link_available===true) {
  4437. 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>';
  4438. }
  4439. }
  4440. echo '</table>';
  4441. }
  4442. /**
  4443. * Creates a multi array with the user fields that we can show. We look the visibility with the api_get_setting function
  4444. * The username is always NOT able to change it.
  4445. * @author Julio Montoya Armas <gugli100@gmail.com>, Dokeos: Personality Test modification
  4446. * @return array[value_name][name]
  4447. * array[value_name][visibilty]
  4448. *
  4449. */
  4450. function make_field_list()
  4451. {
  4452. // LAST NAME and FIRST NAME
  4453. $field_list_array=array();
  4454. $field_list_array['lastname']['name']=get_lang('Lastname');
  4455. $field_list_array['firstname']['name']=get_lang('Firstname');
  4456. if (api_get_setting('profile', 'name') !== 'true')
  4457. {
  4458. $field_list_array['firstname']['visibility']=0;
  4459. $field_list_array['lastname']['visibility']=0;
  4460. }
  4461. else
  4462. {
  4463. $field_list_array['firstname']['visibility']=1;
  4464. $field_list_array['lastname']['visibility']=1;
  4465. }
  4466. $field_list_array['username']['name']=get_lang('Username');
  4467. $field_list_array['username']['visibility']=0;
  4468. // OFFICIAL CODE
  4469. $field_list_array['official_code']['name']=get_lang('OfficialCode');
  4470. if (api_get_setting('profile', 'officialcode') !== 'true')
  4471. {
  4472. $field_list_array['official_code']['visibility']=1;
  4473. }
  4474. else
  4475. {
  4476. $field_list_array['official_code']['visibility']=0;
  4477. }
  4478. // EMAIL
  4479. $field_list_array['email']['name']=get_lang('Email');
  4480. if (api_get_setting('profile', 'email') !== 'true')
  4481. {
  4482. $field_list_array['email']['visibility']=1;
  4483. }
  4484. else
  4485. {
  4486. $field_list_array['email']['visibility']=0;
  4487. }
  4488. // OPENID URL
  4489. //$field_list_array[]='openid_authentication';
  4490. /*
  4491. if(is_profile_editable() && api_get_setting('openid_authentication')=='true')
  4492. {
  4493. $form->addElement('text', 'openid', get_lang('OpenIDURL'), array('size' => 40));
  4494. if (api_get_setting('profile', 'openid') !== 'true')
  4495. $form->freeze('openid');
  4496. $form->applyFilter('openid', 'trim');
  4497. //if (api_get_setting('registration', 'openid') == 'true')
  4498. // $form->addRule('openid', get_lang('ThisFieldIsRequired'), 'required');
  4499. }*/
  4500. // PHONE
  4501. $field_list_array['phone']['name']=get_lang('Phone');
  4502. if (api_get_setting('profile', 'phone') !== 'true')
  4503. {
  4504. $field_list_array['phone']['visibility']=0;
  4505. }
  4506. else
  4507. {
  4508. $field_list_array['phone']['visibility']=1;
  4509. }
  4510. // LANGUAGE
  4511. $field_list_array['language']['name']=get_lang('Language');
  4512. if (api_get_setting('profile', 'language') !== 'true')
  4513. {
  4514. $field_list_array['language']['visibility']=0;
  4515. }
  4516. else
  4517. {
  4518. $field_list_array['language']['visibility']=1;
  4519. }
  4520. // EXTRA FIELDS
  4521. $extra = UserManager::get_extra_fields(0,50,5,'ASC');
  4522. $extra_data = UserManager::get_extra_user_data(api_get_user_id(),true);
  4523. foreach($extra as $id => $field_details)
  4524. {
  4525. if($field_details[6] == 0)
  4526. {
  4527. continue;
  4528. }
  4529. switch($field_details[2])
  4530. {
  4531. case USER_FIELD_TYPE_TEXT:
  4532. $field_list_array['extra_'.$field_details[1]]['name']=$field_details[3];
  4533. if ($field_details[7] == 0)
  4534. {
  4535. $field_list_array['extra_'.$field_details[1]]['visibility']=0;
  4536. }
  4537. else
  4538. {
  4539. $field_list_array['extra_'.$field_details[1]]['visibility']=1;
  4540. }
  4541. break;
  4542. case USER_FIELD_TYPE_TEXTAREA:
  4543. $field_list_array['extra_'.$field_details[1]]['name']=$field_details[3];
  4544. if ($field_details[7] == 0)
  4545. {
  4546. $field_list_array['extra_'.$field_details[1]]['visibility']=0;
  4547. }
  4548. else
  4549. {
  4550. $field_list_array['extra_'.$field_details[1]]['visibility']=1;
  4551. }
  4552. break;
  4553. case USER_FIELD_TYPE_RADIO:
  4554. $field_list_array['extra_'.$field_details[1]]['name']=$field_details[3];
  4555. if ($field_details[7] == 0)
  4556. {
  4557. $field_list_array['extra_'.$field_details[1]]['visibility']=0;
  4558. }
  4559. else
  4560. {
  4561. $field_list_array['extra_'.$field_details[1]]['visibility']=1;
  4562. }
  4563. break;
  4564. case USER_FIELD_TYPE_SELECT:
  4565. $field_list_array['extra_'.$field_details[1]]['name']=$field_details[3];
  4566. if ($field_details[7] == 0)
  4567. {
  4568. $field_list_array['extra_'.$field_details[1]]['visibility']=0;
  4569. }
  4570. else
  4571. {
  4572. $field_list_array['extra_'.$field_details[1]]['visibility']=1;
  4573. }
  4574. break;
  4575. case USER_FIELD_TYPE_SELECT_MULTIPLE:
  4576. $field_list_array['extra_'.$field_details[1]]['name']=$field_details[3];
  4577. if ($field_details[7] == 0)
  4578. {
  4579. $field_list_array['extra_'.$field_details[1]]['visibility']=0;
  4580. }
  4581. else
  4582. {
  4583. $field_list_array['extra_'.$field_details[1]]['visibility']=1;
  4584. }
  4585. break;
  4586. case USER_FIELD_TYPE_DATE:
  4587. $field_list_array['extra_'.$field_details[1]]['name']=$field_details[3];
  4588. if ($field_details[7] == 0)
  4589. {
  4590. $field_list_array['extra_'.$field_details[1]]['visibility']=0;
  4591. }
  4592. else
  4593. {
  4594. $field_list_array['extra_'.$field_details[1]]['visibility']=1;
  4595. }
  4596. break;
  4597. case USER_FIELD_TYPE_DATETIME:
  4598. $field_list_array['extra_'.$field_details[1]]['name']=$field_details[3];
  4599. if ($field_details[7] == 0)
  4600. {
  4601. $field_list_array['extra_'.$field_details[1]]['visibility']=0;
  4602. }
  4603. else
  4604. {
  4605. $field_list_array['extra_'.$field_details[1]]['visibility']=1;
  4606. }
  4607. break;
  4608. case USER_FIELD_TYPE_DOUBLE_SELECT:
  4609. $field_list_array['extra_'.$field_details[1]]['name']=$field_details[3];
  4610. if ($field_details[7] == 0)
  4611. {
  4612. $field_list_array['extra_'.$field_details[1]]['visibility']=0;
  4613. }
  4614. else
  4615. {
  4616. $field_list_array['extra_'.$field_details[1]]['visibility']=1;
  4617. }
  4618. /*
  4619. foreach ($field_details[8] as $key=>$element)
  4620. {
  4621. if ($element[2][0] == '*')
  4622. {
  4623. $values['*'][$element[0]] = str_replace('*','',$element[2]);
  4624. }
  4625. else
  4626. {
  4627. $values[0][$element[0]] = $element[2];
  4628. }
  4629. }
  4630. $group='';
  4631. $group[] =& HTML_QuickForm::createElement('select', 'extra_'.$field_details[1],'',$values[0],'');
  4632. $group[] =& HTML_QuickForm::createElement('select', 'extra_'.$field_details[1].'*','',$values['*'],'');
  4633. $form->addGroup($group, 'extra_'.$field_details[1], $field_details[3], '&nbsp;');
  4634. if ($field_details[7] == 0) $form->freeze('extra_'.$field_details[1]);
  4635. // recoding the selected values for double : if the user has selected certain values, we have to assign them to the correct select form
  4636. if (key_exists('extra_'.$field_details[1], $extra_data))
  4637. {
  4638. // exploding all the selected values (of both select forms)
  4639. $selected_values = explode(';',$extra_data['extra_'.$field_details[1]]);
  4640. $extra_data['extra_'.$field_details[1]] =array();
  4641. // looping through the selected values and assigning the selected values to either the first or second select form
  4642. foreach ($selected_values as $key=>$selected_value)
  4643. {
  4644. if (key_exists($selected_value,$values[0]))
  4645. {
  4646. $extra_data['extra_'.$field_details[1]]['extra_'.$field_details[1]] = $selected_value;
  4647. }
  4648. else
  4649. {
  4650. $extra_data['extra_'.$field_details[1]]['extra_'.$field_details[1].'*'] = $selected_value;
  4651. }
  4652. }
  4653. }*/
  4654. break;
  4655. case USER_FIELD_TYPE_DIVIDER:
  4656. //$form->addElement('static',$field_details[1], '<br /><strong>'.$field_details[3].'</strong>');
  4657. break;
  4658. }
  4659. }
  4660. return $field_list_array;
  4661. }
  4662. /**
  4663. * @author Isaac Flores Paz <florespaz@bidsoftperu.com>
  4664. * @param int $user_id - User ID
  4665. * @param int $user_id_answer - User in survey answer table (user id or anonymus)
  4666. * @return boolean
  4667. *
  4668. */
  4669. function show_link_available($user_id,$survey_code,$user_answer)
  4670. {
  4671. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  4672. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  4673. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  4674. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  4675. $survey_code = Database::escape_string($survey_code);
  4676. $user_id = Database::escape_string($user_id);
  4677. $user_answer = Database::escape_string($user_answer);
  4678. $sql='SELECT COUNT(*) as count FROM '.$table_survey_invitation.' WHERE user='.$user_id.' AND survey_code="'.$survey_code.'" AND answered="1"';
  4679. $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")';
  4680. $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.'" ';
  4681. $result=Database::query($sql,__FILE__,__LINE__);
  4682. $result2=Database::query($sql2,__FILE__,__LINE__);
  4683. $result3=Database::query($sql3,__FILE__,__LINE__);
  4684. $row=Database::fetch_array($result,'ASSOC');
  4685. $row2=Database::fetch_array($result2,'ASSOC');
  4686. $row3=Database::fetch_array($result3,'ASSOC');
  4687. if ($row['count']==1 && $row3['count']!=$row2['count']) {
  4688. return true;
  4689. } else {
  4690. return false;
  4691. }
  4692. }
  4693. }
  4694. ?>