survey.lib.php 187 KB

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