survey.lib.php 232 KB

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