survey.lib.php 231 KB

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