survey.lib.php 192 KB

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