survey.lib.php 231 KB

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