surveyUtil.class.php 156 KB

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