survey.lib.php 183 KB

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