surveyUtil.class.php 159 KB

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