survey.lib.php 209 KB

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