survey.lib.php 189 KB

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