surveyUtil.class.php 143 KB

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