survey.lib.php 208 KB

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