survey.lib.php 241 KB

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