survey.lib.php 183 KB

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