survey.lib.php 198 KB

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