survey.lib.php 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class SurveyManager
  5. * @package chamilo.survey
  6. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University:
  7. * cleanup, refactoring and rewriting large parts (if not all) of the code
  8. * @author Julio Montoya <gugli100@gmail.com>, Personality Test modification
  9. * and rewriting large parts of the code
  10. * @author cfasanando
  11. * @todo move this file to inc/lib
  12. * @todo use consistent naming for the functions (save vs store for instance)
  13. */
  14. class SurveyManager
  15. {
  16. /**
  17. * @param $code
  18. *
  19. * @return string
  20. */
  21. public static function generate_unique_code($code)
  22. {
  23. if (empty($code)) {
  24. return false;
  25. }
  26. $course_id = api_get_course_int_id();
  27. $table_survey = Database::get_course_table(TABLE_SURVEY);
  28. $code = Database::escape_string($code);
  29. $num = 0;
  30. $new_code = $code;
  31. while (true) {
  32. $sql = "SELECT * FROM $table_survey
  33. WHERE code = '$new_code' AND c_id = $course_id";
  34. $result = Database::query($sql);
  35. if (Database::num_rows($result)) {
  36. $num++;
  37. $new_code = $code.$num;
  38. } else {
  39. break;
  40. }
  41. }
  42. return $code.$num;
  43. }
  44. /**
  45. * Deletes all survey invitations of a user
  46. * @param int $user_id
  47. *
  48. * @return boolean
  49. * @assert ('') === false
  50. */
  51. public static function delete_all_survey_invitations_by_user($user_id)
  52. {
  53. $user_id = intval($user_id);
  54. if (empty($user_id)) {
  55. return false;
  56. }
  57. $table_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION);
  58. $table_survey = Database::get_course_table(TABLE_SURVEY);
  59. $sql = "SELECT survey_invitation_id, survey_code
  60. FROM $table_survey_invitation WHERE user = '$user_id' AND c_id <> 0 ";
  61. $result = Database::query($sql);
  62. while ($row = Database::fetch_array($result, 'ASSOC')) {
  63. $survey_invitation_id = $row['survey_invitation_id'];
  64. $survey_code = $row['survey_code'];
  65. $sql2 = "DELETE FROM $table_survey_invitation
  66. WHERE survey_invitation_id = '$survey_invitation_id' AND c_id <> 0";
  67. if (Database::query($sql2)) {
  68. $sql3 = "UPDATE $table_survey SET
  69. invited = invited-1
  70. WHERE c_id <> 0 AND code ='$survey_code'";
  71. Database::query($sql3);
  72. }
  73. }
  74. }
  75. /**
  76. *
  77. * @param string $course_code
  78. * @param int $session_id
  79. *
  80. * @return array
  81. * @assert ('') === false
  82. */
  83. public static function get_surveys($course_code, $session_id = 0)
  84. {
  85. $table_survey = Database::get_course_table(TABLE_SURVEY);
  86. if (empty($course_code)) {
  87. return false;
  88. }
  89. $course_info = api_get_course_info($course_code);
  90. $session_condition = api_get_session_condition($session_id, true, true);
  91. $sql = "SELECT * FROM $table_survey
  92. WHERE c_id = {$course_info['real_id']} $session_condition ";
  93. $result = Database::query($sql);
  94. $result = Database::store_result($result, 'ASSOC');
  95. return $result;
  96. }
  97. /**
  98. * Retrieves all the survey information
  99. *
  100. * @param integer $survey_id the id of the survey
  101. * @param boolean $shared this parameter determines if
  102. * we have to get the information of a survey from the central (shared) database or from the
  103. * course database
  104. * @param string course code optional
  105. *
  106. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  107. * @version February 2007
  108. * @assert ('') === false
  109. *
  110. * @todo this is the same function as in create_new_survey.php
  111. */
  112. public static function get_survey($survey_id, $shared = 0, $course_code = '', $simple_return = false)
  113. {
  114. // Table definition
  115. if (!empty($course_code)) {
  116. $my_course_id = $course_code;
  117. } else if (isset($_GET['course'])) {
  118. $my_course_id = Security::remove_XSS($_GET['course']);
  119. } else {
  120. $my_course_id = api_get_course_id();
  121. }
  122. $my_course_info = api_get_course_info($my_course_id);
  123. $table_survey = Database::get_course_table(TABLE_SURVEY);
  124. if ($shared != 0) {
  125. $table_survey = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  126. $sql = "SELECT * FROM $table_survey
  127. WHERE survey_id='".intval($survey_id)."' ";
  128. } else {
  129. $sql = "SELECT * FROM $table_survey
  130. WHERE
  131. survey_id='".intval($survey_id)."' AND
  132. c_id = ".$my_course_info['real_id'];
  133. }
  134. $result = Database::query($sql);
  135. $return = array();
  136. if (Database::num_rows($result) > 0) {
  137. $return = Database::fetch_array($result, 'ASSOC');
  138. if ($simple_return) {
  139. return $return;
  140. }
  141. // We do this (temporarily) to have the array match the quickform elements immediately
  142. // idealiter the fields in the db match the quickform fields
  143. $return['survey_code'] = $return['code'];
  144. $return['survey_title'] = $return['title'];
  145. $return['survey_subtitle'] = $return['subtitle'];
  146. $return['survey_language'] = $return['lang'];
  147. $return['start_date'] = $return['avail_from'];
  148. $return['end_date'] = $return['avail_till'];
  149. $return['survey_share'] = $return['is_shared'];
  150. $return['survey_introduction'] = $return['intro'];
  151. $return['survey_thanks'] = $return['surveythanks'];
  152. $return['survey_type'] = $return['survey_type'];
  153. $return['one_question_per_page'] = $return['one_question_per_page'];
  154. $return['show_form_profile'] = $return['show_form_profile'];
  155. $return['input_name_list'] = isset($return['input_name_list']) ? $return['input_name_list'] : null;
  156. $return['shuffle'] = $return['shuffle'];
  157. $return['parent_id'] = $return['parent_id'];
  158. $return['survey_version'] = $return['survey_version'];
  159. $return['anonymous'] = $return['anonymous'];
  160. }
  161. return $return;
  162. }
  163. /**
  164. * This function stores a survey in the database.
  165. *
  166. * @param array $values
  167. *
  168. * @return array $return the type of return message that has to be displayed and the message in it
  169. *
  170. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  171. * @version February 2007
  172. */
  173. public static function store_survey($values)
  174. {
  175. $_user = api_get_user_info();
  176. $course_id = api_get_course_int_id();
  177. $session_id = api_get_session_id();
  178. $courseCode = api_get_course_id();
  179. $table_survey = Database::get_course_table(TABLE_SURVEY);
  180. $shared_survey_id = 0;
  181. if (!isset($values['survey_id'])) {
  182. // Check if the code doesn't soon exists in this language
  183. $sql = 'SELECT 1 FROM '.$table_survey.'
  184. WHERE
  185. c_id = '.$course_id.' AND
  186. code="'.Database::escape_string($values['survey_code']).'" AND
  187. lang="'.Database::escape_string($values['survey_language']).'"';
  188. $rs = Database::query($sql);
  189. if (Database::num_rows($rs) > 0) {
  190. Display::addFlash(
  191. Display::return_message(
  192. get_lang('ThisSurveyCodeSoonExistsInThisLanguage'),
  193. 'error'
  194. )
  195. );
  196. $return['type'] = 'error';
  197. $return['id'] = isset($values['survey_id']) ? $values['survey_id'] : 0;
  198. return $return;
  199. }
  200. if (!isset($values['anonymous'])) {
  201. $values['anonymous'] = 0;
  202. }
  203. $values['anonymous'] = intval($values['anonymous']);
  204. $additional['columns'] = '';
  205. $extraParams = [];
  206. if ($values['anonymous'] == 0) {
  207. // Input_name_list
  208. $values['show_form_profile'] = isset($values['show_form_profile']) ? $values['show_form_profile'] : 0;
  209. $extraParams['show_form_profile'] = $values['show_form_profile'];
  210. if ($values['show_form_profile'] == 1) {
  211. // Input_name_list
  212. $fields = explode(',', $values['input_name_list']);
  213. $field_values = '';
  214. foreach ($fields as & $field) {
  215. if ($field != '') {
  216. if ($values[$field] == '') {
  217. $values[$field] = 0;
  218. }
  219. $field_values .= $field.':'.$values[$field].'@';
  220. }
  221. }
  222. $extraParams['form_fields'] = $field_values;
  223. } else {
  224. $extraParams['form_fields'] = '';
  225. }
  226. } else {
  227. // Input_name_list
  228. $extraParams['show_form_profile'] = 0;
  229. $extraParams['form_fields'] = '';
  230. }
  231. if ($values['survey_type'] == 1) {
  232. $extraParams['survey_type'] = 1;
  233. $extraParams['shuffle'] = $values['shuffle'];
  234. $extraParams['one_question_per_page'] = $values['one_question_per_page'];
  235. $extraParams['parent_id'] = $values['parent_id'];
  236. // Logic for versioning surveys
  237. if (!empty($values['parent_id'])) {
  238. $versionValue = '';
  239. $sql = 'SELECT survey_version
  240. FROM '.$table_survey.'
  241. WHERE
  242. c_id = '.$course_id.' AND
  243. parent_id = '.intval($values['parent_id']).'
  244. ORDER BY survey_version DESC
  245. LIMIT 1';
  246. $rs = Database::query($sql);
  247. if (Database::num_rows($rs) === 0) {
  248. $sql = 'SELECT survey_version FROM '.$table_survey.'
  249. WHERE
  250. c_id = '.$course_id.' AND
  251. survey_id = '.intval($values['parent_id']);
  252. $rs = Database::query($sql);
  253. $getversion = Database::fetch_array($rs, 'ASSOC');
  254. if (empty($getversion['survey_version'])) {
  255. $versionValue = ++$getversion['survey_version'];
  256. } else {
  257. $versionValue = $getversion['survey_version'];
  258. }
  259. } else {
  260. $row = Database::fetch_array($rs, 'ASSOC');
  261. $pos = api_strpos($row['survey_version']);
  262. if ($pos === false) {
  263. $row['survey_version'] = $row['survey_version'] + 1;
  264. $versionValue = $row['survey_version'];
  265. } else {
  266. $getlast = explode('\.', $row['survey_version']);
  267. $lastversion = array_pop($getlast);
  268. $lastversion = $lastversion + 1;
  269. $add = implode('.', $getlast);
  270. if ($add != '') {
  271. $insertnewversion = $add.'.'.$lastversion;
  272. } else {
  273. $insertnewversion = $lastversion;
  274. }
  275. $versionValue = $insertnewversion;
  276. }
  277. }
  278. $extraParams['survey_version'] = $versionValue;
  279. }
  280. }
  281. $params = [
  282. 'c_id' => $course_id,
  283. 'code' => strtolower(CourseManager::generate_course_code($values['survey_code'])),
  284. 'title' => $values['survey_title'],
  285. 'subtitle' => $values['survey_subtitle'],
  286. 'author' => $_user['user_id'],
  287. 'lang' => $values['survey_language'],
  288. 'avail_from' => $values['start_date'],
  289. 'avail_till' => $values['end_date'],
  290. 'is_shared' => $shared_survey_id,
  291. 'template' => 'template',
  292. 'intro' => $values['survey_introduction'],
  293. 'surveythanks' => $values['survey_thanks'],
  294. 'creation_date' => api_get_utc_datetime(),
  295. 'anonymous' => $values['anonymous'],
  296. 'session_id' => api_get_session_id(),
  297. 'visible_results' => $values['visible_results']
  298. ];
  299. $params = array_merge($params, $extraParams);
  300. $survey_id = Database::insert($table_survey, $params);
  301. if ($survey_id > 0) {
  302. $sql = "UPDATE $table_survey SET survey_id = $survey_id
  303. WHERE iid = $survey_id";
  304. Database::query($sql);
  305. // Insert into item_property
  306. api_item_property_update(
  307. api_get_course_info(),
  308. TOOL_SURVEY,
  309. $survey_id,
  310. 'SurveyAdded',
  311. api_get_user_id()
  312. );
  313. }
  314. if ($values['survey_type'] == 1 && !empty($values['parent_id'])) {
  315. self::copy_survey($values['parent_id'], $survey_id);
  316. }
  317. Display::addFlash(
  318. Display::return_message(
  319. get_lang('SurveyCreatedSuccesfully'),
  320. 'success'
  321. )
  322. );
  323. $return['id'] = $survey_id;
  324. } else {
  325. // Check whether the code doesn't soon exists in this language
  326. $sql = 'SELECT 1 FROM '.$table_survey.'
  327. WHERE
  328. c_id = '.$course_id.' AND
  329. code = "'.Database::escape_string($values['survey_code']).'" AND
  330. lang = "'.Database::escape_string($values['survey_language']).'" AND
  331. survey_id !='.intval($values['survey_id']);
  332. $rs = Database::query($sql);
  333. if (Database::num_rows($rs) > 0) {
  334. Display::addFlash(
  335. Display::return_message(
  336. get_lang('ThisSurveyCodeSoonExistsInThisLanguage'),
  337. 'error'
  338. )
  339. );
  340. $return['type'] = 'error';
  341. $return['id'] = isset($values['survey_id']) ? $values['survey_id'] : 0;
  342. return $return;
  343. }
  344. if (!isset($values['anonymous']) ||
  345. (isset($values['anonymous']) && $values['anonymous'] == '')
  346. ) {
  347. $values['anonymous'] = 0;
  348. }
  349. $values['shuffle'] = isset($values['shuffle']) ? $values['shuffle'] : null;
  350. $values['one_question_per_page'] = isset($values['one_question_per_page']) ? $values['one_question_per_page'] : null;
  351. $values['show_form_profile'] = isset($values['show_form_profile']) ? $values['show_form_profile'] : null;
  352. $extraParams = [];
  353. $extraParams['shuffle'] = $values['shuffle'];
  354. $extraParams['one_question_per_page'] = $values['one_question_per_page'];
  355. $extraParams['shuffle'] = $values['shuffle'];
  356. if ($values['anonymous'] == 0) {
  357. $extraParams['show_form_profile'] = $values['show_form_profile'];
  358. if ($values['show_form_profile'] == 1) {
  359. $fields = explode(',', $values['input_name_list']);
  360. $field_values = '';
  361. foreach ($fields as &$field) {
  362. if ($field != '') {
  363. if (!isset($values[$field]) ||
  364. (isset($values[$field]) && $values[$field] == '')
  365. ) {
  366. $values[$field] = 0;
  367. }
  368. $field_values .= $field.':'.$values[$field].'@';
  369. }
  370. }
  371. $extraParams['form_fields'] = $field_values;
  372. } else {
  373. $extraParams['form_fields'] = '';
  374. }
  375. } else {
  376. $extraParams['show_form_profile'] = 0;
  377. $extraParams['form_fields'] = '';
  378. }
  379. $params = [
  380. 'title' => $values['survey_title'],
  381. 'subtitle' => $values['survey_subtitle'],
  382. 'author' => $_user['user_id'],
  383. 'lang' => $values['survey_language'],
  384. 'avail_from' => $values['start_date'],
  385. 'avail_till' => $values['end_date'],
  386. 'is_shared' => $shared_survey_id,
  387. 'template' => 'template',
  388. 'intro' => $values['survey_introduction'],
  389. 'surveythanks' => $values['survey_thanks'],
  390. 'anonymous' => $values['anonymous'],
  391. 'session_id' => api_get_session_id(),
  392. 'visible_results' => $values['visible_results'],
  393. ];
  394. $params = array_merge($params, $extraParams);
  395. Database::update(
  396. $table_survey,
  397. $params,
  398. [
  399. 'c_id = ? AND survey_id = ?' => [
  400. $course_id,
  401. $values['survey_id'],
  402. ],
  403. ]
  404. );
  405. // Update into item_property (update)
  406. api_item_property_update(
  407. api_get_course_info(),
  408. TOOL_SURVEY,
  409. $values['survey_id'],
  410. 'SurveyUpdated',
  411. api_get_user_id()
  412. );
  413. Display::addFlash(
  414. Display::return_message(
  415. get_lang('SurveyUpdatedSuccesfully'),
  416. 'confirmation'
  417. )
  418. );
  419. $return['id'] = $values['survey_id'];
  420. }
  421. $survey_id = intval($return['id']);
  422. // Gradebook
  423. $gradebook_option = false;
  424. if (isset($values['survey_qualify_gradebook'])) {
  425. $gradebook_option = $values['survey_qualify_gradebook'] > 0;
  426. }
  427. $gradebook_link_type = 8;
  428. $link_info = GradebookUtils::isResourceInCourseGradebook(
  429. $courseCode,
  430. $gradebook_link_type,
  431. $survey_id,
  432. $session_id
  433. );
  434. $gradebook_link_id = isset($link_info['id']) ? $link_info['id'] : false;
  435. if ($gradebook_option) {
  436. if ($survey_id > 0) {
  437. $title_gradebook = ''; // Not needed here.
  438. $description_gradebook = ''; // Not needed here.
  439. $survey_weight = floatval($_POST['survey_weight']);
  440. $max_score = 1;
  441. if (!$gradebook_link_id) {
  442. GradebookUtils::add_resource_to_course_gradebook(
  443. $values['category_id'],
  444. $courseCode,
  445. $gradebook_link_type,
  446. $survey_id,
  447. $title_gradebook,
  448. $survey_weight,
  449. $max_score,
  450. $description_gradebook,
  451. 1,
  452. $session_id
  453. );
  454. } else {
  455. GradebookUtils::update_resource_from_course_gradebook(
  456. $gradebook_link_id,
  457. $courseCode,
  458. $survey_weight
  459. );
  460. }
  461. }
  462. } else {
  463. // Delete everything of the gradebook for this $linkId
  464. GradebookUtils::remove_resource_from_course_gradebook($gradebook_link_id);
  465. //comenting this line to correctly return the function msg
  466. //exit;
  467. }
  468. return $return;
  469. }
  470. /**
  471. * This function stores a shared survey in the central database.
  472. *
  473. * @param array $values
  474. * @return array $return the type of return message that has to be displayed and the message in it
  475. *
  476. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  477. * @version February 2007
  478. */
  479. public function store_shared_survey($values)
  480. {
  481. $_user = api_get_user_info();
  482. $_course = api_get_course_info();
  483. // Table definitions
  484. $table_survey = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY);
  485. if (!$values['survey_id'] ||
  486. !is_numeric($values['survey_id']) ||
  487. $values['survey_share']['survey_share'] == 'true'
  488. ) {
  489. $sql = "INSERT INTO $table_survey (code, title, subtitle, author, lang, template, intro, surveythanks, creation_date, course_code) VALUES (
  490. '".Database::escape_string($values['survey_code'])."',
  491. '".Database::escape_string($values['survey_title'])."',
  492. '".Database::escape_string($values['survey_subtitle'])."',
  493. '".intval($_user['user_id'])."',
  494. '".Database::escape_string($values['survey_language'])."',
  495. '".Database::escape_string('template')."',
  496. '".Database::escape_string($values['survey_introduction'])."',
  497. '".Database::escape_string($values['survey_thanks'])."',
  498. '".api_get_utc_datetime()."',
  499. '".$_course['id']."')";
  500. Database::query($sql);
  501. $return = Database::insert_id();
  502. $sql = "UPDATE $table_survey SET survey_id = $return WHERE iid = $return";
  503. Database::query($sql);
  504. } else {
  505. $sql = "UPDATE $table_survey SET
  506. code = '".Database::escape_string($values['survey_code'])."',
  507. title = '".Database::escape_string($values['survey_title'])."',
  508. subtitle = '".Database::escape_string($values['survey_subtitle'])."',
  509. author = '".intval($_user['user_id'])."',
  510. lang = '".Database::escape_string($values['survey_language'])."',
  511. template = '".Database::escape_string('template')."',
  512. intro = '".Database::escape_string($values['survey_introduction'])."',
  513. surveythanks = '".Database::escape_string($values['survey_thanks'])."'
  514. WHERE survey_id = '".Database::escape_string($values['survey_share']['survey_share'])."'";
  515. Database::query($sql);
  516. $return = $values['survey_share']['survey_share'];
  517. }
  518. return $return;
  519. }
  520. /**
  521. * This function deletes a survey (and also all the question in that survey
  522. *
  523. * @param int $survey_id id of the survey that has to be deleted
  524. * @return true
  525. *
  526. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  527. * @version January 2007
  528. */
  529. public static function delete_survey($survey_id, $shared = false, $course_id = '')
  530. {
  531. // Database table definitions
  532. if (empty($course_id)) {
  533. $course_id = api_get_course_int_id();
  534. }
  535. $survey_id = intval($survey_id);
  536. if (empty($survey_id)) {
  537. return false;
  538. }
  539. $course_info = api_get_course_info_by_id($course_id);
  540. $course_id = $course_info['real_id'];
  541. $table_survey = Database::get_course_table(TABLE_SURVEY);
  542. $table_survey_question_group = Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP);
  543. if ($shared) {
  544. $table_survey = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY);
  545. // Deleting the survey
  546. $sql = "DELETE FROM $table_survey
  547. WHERE survey_id='".$survey_id."'";
  548. Database::query($sql);
  549. } else {
  550. $sql = "DELETE FROM $table_survey
  551. WHERE c_id = $course_id AND survey_id='".$survey_id."'";
  552. Database::query($sql);
  553. }
  554. // Deleting groups of this survey
  555. $sql = "DELETE FROM $table_survey_question_group
  556. WHERE c_id = $course_id AND survey_id='".$survey_id."'";
  557. Database::query($sql);
  558. // Deleting the questions of the survey
  559. self::delete_all_survey_questions($survey_id, $shared);
  560. // Update into item_property (delete)
  561. api_item_property_update(
  562. $course_info,
  563. TOOL_SURVEY,
  564. $survey_id,
  565. 'SurveyDeleted',
  566. api_get_user_id()
  567. );
  568. return true;
  569. }
  570. /**
  571. * @param int $survey_id
  572. * @param int $new_survey_id
  573. * @param int $targetCourseId
  574. *
  575. * @return bool
  576. */
  577. public static function copy_survey(
  578. $survey_id,
  579. $new_survey_id = null,
  580. $targetCourseId = null
  581. ) {
  582. $course_id = api_get_course_int_id();
  583. if (!$targetCourseId) {
  584. $targetCourseId = $course_id;
  585. }
  586. // Database table definitions
  587. $table_survey = Database::get_course_table(TABLE_SURVEY);
  588. $table_survey_question_group = Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP);
  589. $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
  590. $table_survey_options = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  591. $survey_id = intval($survey_id);
  592. // Get groups
  593. $survey_data = self::get_survey($survey_id, 0, null, true);
  594. if (empty($survey_data)) {
  595. return true;
  596. }
  597. if (empty($new_survey_id)) {
  598. $params = $survey_data;
  599. $params['code'] = self::generate_unique_code($params['code']);
  600. $params['c_id'] = $targetCourseId;
  601. unset($params['survey_id']);
  602. $params['session_id'] = api_get_session_id();
  603. $params['title'] = $params['title'].' '.get_lang('Copy');
  604. unset($params['iid']);
  605. Database::insert($table_survey, $params);
  606. $new_survey_id = Database::insert_id();
  607. if ($new_survey_id) {
  608. $sql = "UPDATE $table_survey SET survey_id = $new_survey_id
  609. WHERE iid = $new_survey_id";
  610. Database::query($sql);
  611. // Insert into item_property
  612. api_item_property_update(
  613. api_get_course_info(),
  614. TOOL_SURVEY,
  615. $new_survey_id,
  616. 'SurveyAdded',
  617. api_get_user_id()
  618. );
  619. }
  620. } else {
  621. $new_survey_id = intval($new_survey_id);
  622. }
  623. $sql = "SELECT * FROM $table_survey_question_group
  624. WHERE c_id = $course_id AND survey_id='".$survey_id."'";
  625. $res = Database::query($sql);
  626. while ($row = Database::fetch_array($res, 'ASSOC')) {
  627. $params = array(
  628. 'c_id' => $targetCourseId,
  629. 'name' => $row['name'],
  630. 'description' => $row['description'],
  631. 'survey_id' => $new_survey_id
  632. );
  633. $insertId = Database::insert($table_survey_question_group, $params);
  634. $sql = "UPDATE $table_survey_question_group SET id = iid
  635. WHERE iid = $insertId";
  636. Database::query($sql);
  637. $group_id[$row['id']] = $insertId;
  638. }
  639. // Get questions
  640. $sql = "SELECT * FROM $table_survey_question
  641. WHERE c_id = $course_id AND survey_id='".$survey_id."'";
  642. $res = Database::query($sql);
  643. while ($row = Database::fetch_array($res, 'ASSOC')) {
  644. $params = array(
  645. 'c_id' => $targetCourseId,
  646. 'survey_id' => $new_survey_id,
  647. 'survey_question' => $row['survey_question'],
  648. 'survey_question_comment' => $row['survey_question_comment'],
  649. 'type' => $row['type'],
  650. 'display' => $row['display'],
  651. 'sort' => $row['sort'],
  652. 'shared_question_id' => $row['shared_question_id'],
  653. 'max_value' => $row['max_value'],
  654. 'survey_group_pri' => $row['survey_group_pri'],
  655. 'survey_group_sec1' => $row['survey_group_sec1'],
  656. 'survey_group_sec2' => $row['survey_group_sec2']
  657. );
  658. $insertId = Database::insert($table_survey_question, $params);
  659. $sql = "UPDATE $table_survey_question SET question_id = iid WHERE iid = $insertId";
  660. Database::query($sql);
  661. $question_id[$row['question_id']] = $insertId;
  662. }
  663. // Get questions options
  664. $sql = "SELECT * FROM $table_survey_options
  665. WHERE c_id = $course_id AND survey_id='".$survey_id."'";
  666. $res = Database::query($sql);
  667. while ($row = Database::fetch_array($res, 'ASSOC')) {
  668. $params = array(
  669. 'c_id' => $targetCourseId,
  670. 'question_id' => $question_id[$row['question_id']],
  671. 'survey_id' => $new_survey_id,
  672. 'option_text' => $row['option_text'],
  673. 'sort' => $row['sort'],
  674. 'value' => $row['value']
  675. );
  676. $insertId = Database::insert($table_survey_options, $params);
  677. $sql = "UPDATE $table_survey_options SET question_option_id = $insertId
  678. WHERE iid = $insertId";
  679. Database::query($sql);
  680. }
  681. return $new_survey_id;
  682. }
  683. /**
  684. * This function duplicates a survey (and also all the question in that survey
  685. *
  686. * @param int $survey_id id of the survey that has to be duplicated
  687. * @param int $courseId id of the course which survey has to be duplicated
  688. * @return true
  689. *
  690. * @author Eric Marguin <e.marguin@elixir-interactive.com>, Elixir Interactive
  691. * @version October 2007
  692. */
  693. public static function empty_survey($survey_id, $courseId = null)
  694. {
  695. // Database table definitions
  696. $table_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION);
  697. $table_survey_answer = Database::get_course_table(TABLE_SURVEY_ANSWER);
  698. $table_survey = Database::get_course_table(TABLE_SURVEY);
  699. $course_id = $courseId ? $courseId : api_get_course_int_id();
  700. $datas = self::get_survey($survey_id);
  701. $session_where = '';
  702. if (api_get_session_id() != 0) {
  703. $session_where = ' AND session_id = "'.api_get_session_id().'" ';
  704. }
  705. $sql = 'DELETE FROM '.$table_survey_invitation.'
  706. WHERE
  707. c_id = '.$course_id.' AND
  708. survey_code = "'.Database::escape_string($datas['code']).'" '.$session_where.' ';
  709. Database::query($sql);
  710. $sql = 'DELETE FROM '.$table_survey_answer.'
  711. WHERE c_id = '.$course_id.' AND survey_id='.intval($survey_id);
  712. Database::query($sql);
  713. $sql = 'UPDATE '.$table_survey.' SET invited=0, answered=0
  714. WHERE c_id = '.$course_id.' AND survey_id='.intval($survey_id);
  715. Database::query($sql);
  716. return true;
  717. }
  718. /**
  719. * This function recalculates the number of people who have taken the survey (=filled at least one question)
  720. *
  721. * @param int $survey_id the id of the survey somebody
  722. * @return true
  723. *
  724. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  725. * @version February 2007
  726. */
  727. public static function update_survey_answered($survey_data, $user, $survey_code)
  728. {
  729. // Database table definitions
  730. $table_survey = Database::get_course_table(TABLE_SURVEY);
  731. $table_survey_invitation = Database::get_course_table(TABLE_SURVEY_INVITATION);
  732. $survey_id = $survey_data['survey_id'];
  733. $course_id = $survey_data['c_id'];
  734. $session_id = $survey_data['session_id'];
  735. // Getting a list with all the people who have filled the survey
  736. $people_filled = self::get_people_who_filled_survey($survey_id, false, $course_id);
  737. $number = intval(count($people_filled));
  738. // Storing this value in the survey table
  739. $sql = "UPDATE $table_survey
  740. SET answered = $number
  741. WHERE
  742. c_id = $course_id AND
  743. survey_id = ".intval($survey_id);
  744. Database::query($sql);
  745. // Storing that the user has finished the survey.
  746. $sql = "UPDATE $table_survey_invitation SET answered='1'
  747. WHERE
  748. c_id = $course_id AND
  749. session_id='".$session_id."' AND
  750. user='".Database::escape_string($user)."' AND
  751. survey_code='".Database::escape_string($survey_code)."'";
  752. Database::query($sql);
  753. }
  754. /***
  755. * SURVEY QUESTION FUNCTIONS
  756. */
  757. /**
  758. * This function return the "icon" of the question type
  759. *
  760. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  761. * @version February 2007
  762. */
  763. public static function icon_question($type)
  764. {
  765. // the possible question types
  766. $possible_types = array(
  767. 'personality',
  768. 'yesno',
  769. 'multiplechoice',
  770. 'multipleresponse',
  771. 'open',
  772. 'dropdown',
  773. 'comment',
  774. 'pagebreak',
  775. 'percentage',
  776. 'score',
  777. );
  778. // the images array
  779. $icon_question = array(
  780. 'yesno' => 'yesno.png',
  781. 'personality' => 'yesno.png',
  782. 'multiplechoice' => 'mcua.png',
  783. 'multipleresponse' => 'mcma.png',
  784. 'open' => 'open_answer.png',
  785. 'dropdown' => 'dropdown.png',
  786. 'percentage' => 'percentagequestion.png',
  787. 'score' => 'scorequestion.png',
  788. 'comment' => 'commentquestion.png',
  789. 'pagebreak' => 'page_end.png',
  790. );
  791. if (in_array($type, $possible_types)) {
  792. return $icon_question[$type];
  793. } else {
  794. return false;
  795. }
  796. }
  797. /**
  798. * This function retrieves all the information of a question
  799. *
  800. * @param integer $question_id the id of the question
  801. * @return array
  802. *
  803. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  804. * @version January 2007
  805. *
  806. * @todo one sql call should do the trick
  807. */
  808. public static function get_question($question_id, $shared = false)
  809. {
  810. // Table definitions
  811. $tbl_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
  812. $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  813. $course_id = api_get_course_int_id();
  814. $sql = "SELECT * FROM $tbl_survey_question
  815. WHERE c_id = $course_id AND question_id='".intval($question_id)."'
  816. ORDER BY `sort` ";
  817. $sqlOption = " SELECT * FROM $table_survey_question_option
  818. WHERE c_id = $course_id AND question_id='".intval($question_id)."'
  819. ORDER BY `sort` ";
  820. if ($shared) {
  821. $tbl_survey_question = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  822. $table_survey_question_option = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  823. $sql = "SELECT * FROM $tbl_survey_question
  824. WHERE question_id='".intval($question_id)."'
  825. ORDER BY `sort` ";
  826. $sqlOption = "SELECT * FROM $table_survey_question_option
  827. WHERE question_id='".intval($question_id)."'
  828. ORDER BY `sort` ";
  829. }
  830. // Getting the information of the question
  831. $result = Database::query($sql);
  832. $row = Database::fetch_array($result, 'ASSOC');
  833. $return['survey_id'] = $row['survey_id'];
  834. $return['question_id'] = $row['question_id'];
  835. $return['type'] = $row['type'];
  836. $return['question'] = $row['survey_question'];
  837. $return['horizontalvertical'] = $row['display'];
  838. $return['shared_question_id'] = $row['shared_question_id'];
  839. $return['maximum_score'] = $row['max_value'];
  840. if ($row['survey_group_pri'] != 0) {
  841. $return['assigned'] = $row['survey_group_pri'];
  842. $return['choose'] = 1;
  843. } else {
  844. $return['assigned1'] = $row['survey_group_sec1'];
  845. $return['assigned2'] = $row['survey_group_sec2'];
  846. $return['choose'] = 2;
  847. }
  848. // Getting the information of the question options
  849. $result = Database::query($sqlOption);
  850. while ($row = Database::fetch_array($result, 'ASSOC')) {
  851. /** @todo this should be renamed to options instead of answers */
  852. $return['answers'][] = $row['option_text'];
  853. $return['values'][] = $row['value'];
  854. /** @todo this can be done more elegantly (used in reporting) */
  855. $return['answersid'][] = $row['question_option_id'];
  856. }
  857. return $return;
  858. }
  859. /**
  860. * This function gets all the question of any given survey
  861. *
  862. * @param integer $survey_id the id of the survey
  863. * @return array containing all the questions of the survey
  864. *
  865. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  866. * @version February 2007
  867. *
  868. * @todo one sql call should do the trick
  869. */
  870. public static function get_questions($survey_id, $course_id = '')
  871. {
  872. // Table definitions
  873. $tbl_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
  874. $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  875. if (empty($course_id)) {
  876. $course_id = api_get_course_int_id();
  877. }
  878. // Getting the information of the question
  879. $sql = "SELECT * FROM $tbl_survey_question
  880. WHERE c_id = $course_id AND survey_id='".intval($survey_id)."'";
  881. $result = Database::query($sql);
  882. $return = array();
  883. while ($row = Database::fetch_array($result, 'ASSOC')) {
  884. $return[$row['question_id']]['survey_id'] = $row['survey_id'];
  885. $return[$row['question_id']]['question_id'] = $row['question_id'];
  886. $return[$row['question_id']]['type'] = $row['type'];
  887. $return[$row['question_id']]['question'] = $row['survey_question'];
  888. $return[$row['question_id']]['horizontalvertical'] = $row['display'];
  889. $return[$row['question_id']]['maximum_score'] = $row['max_value'];
  890. $return[$row['question_id']]['sort'] = $row['sort'];
  891. }
  892. // Getting the information of the question options
  893. $sql = "SELECT * FROM $table_survey_question_option
  894. WHERE c_id = $course_id AND survey_id='".intval($survey_id)."'";
  895. $result = Database::query($sql);
  896. while ($row = Database::fetch_array($result, 'ASSOC')) {
  897. $return[$row['question_id']]['answers'][] = $row['option_text'];
  898. }
  899. return $return;
  900. }
  901. /**
  902. * This function saves a question in the database.
  903. * This can be either an update of an existing survey or storing a new survey
  904. * @param array $survey_data
  905. * @param array $form_content all the information of the form
  906. *
  907. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  908. * @version January 2007
  909. */
  910. public static function save_question($survey_data, $form_content)
  911. {
  912. $return_message = '';
  913. if (strlen($form_content['question']) > 1) {
  914. // Checks length of the question
  915. $empty_answer = false;
  916. if ($survey_data['survey_type'] == 1) {
  917. if (empty($form_content['choose'])) {
  918. $return_message = 'PleaseChooseACondition';
  919. return $return_message;
  920. }
  921. if (($form_content['choose'] == 2) &&
  922. ($form_content['assigned1'] == $form_content['assigned2'])
  923. ) {
  924. $return_message = 'ChooseDifferentCategories';
  925. return $return_message;
  926. }
  927. }
  928. if ($form_content['type'] != 'percentage') {
  929. if (isset($form_content['answers'])) {
  930. for ($i = 0; $i < count($form_content['answers']); $i++) {
  931. if (strlen($form_content['answers'][$i]) < 1) {
  932. $empty_answer = true;
  933. break;
  934. }
  935. }
  936. }
  937. }
  938. if ($form_content['type'] == 'score') {
  939. if (strlen($form_content['maximum_score']) < 1) {
  940. $empty_answer = true;
  941. }
  942. }
  943. $additional = array();
  944. $course_id = api_get_course_int_id();
  945. if (!$empty_answer) {
  946. // Table definitions
  947. $tbl_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
  948. // Getting all the information of the survey
  949. $survey_data = self::get_survey($form_content['survey_id']);
  950. // Storing the question in the shared database
  951. if (is_numeric($survey_data['survey_share']) && $survey_data['survey_share'] != 0) {
  952. $shared_question_id = self::save_shared_question($form_content, $survey_data);
  953. $form_content['shared_question_id'] = $shared_question_id;
  954. }
  955. // Storing a new question
  956. if ($form_content['question_id'] == '' || !is_numeric($form_content['question_id'])) {
  957. // Finding the max sort order of the questions in the given survey
  958. $sql = "SELECT max(sort) AS max_sort
  959. FROM $tbl_survey_question
  960. WHERE c_id = $course_id AND survey_id='".intval($form_content['survey_id'])."'";
  961. $result = Database::query($sql);
  962. $row = Database::fetch_array($result, 'ASSOC');
  963. $max_sort = $row['max_sort'];
  964. // Some variables defined for survey-test type
  965. $extraParams = [];
  966. if (isset($_POST['choose'])) {
  967. if ($_POST['choose'] == 1) {
  968. $extraParams['survey_group_pri'] = $_POST['assigned'];
  969. } elseif ($_POST['choose'] == 2) {
  970. $extraParams['survey_group_sec1'] = $_POST['assigned1'];
  971. $extraParams['survey_group_sec2'] = $_POST['assigned2'];
  972. }
  973. }
  974. $questionComment = isset($form_content['question_comment']) ? $form_content['question_comment'] : '';
  975. $maxScore = isset($form_content['maximum_score']) ? $form_content['maximum_score'] : '';
  976. $display = isset($form_content['horizontalvertical']) ? $form_content['horizontalvertical'] : '';
  977. $params = [
  978. 'c_id' => $course_id,
  979. 'survey_id' => $form_content['survey_id'],
  980. 'survey_question' => $form_content['question'],
  981. 'survey_question_comment' => $questionComment,
  982. 'type' => $form_content['type'],
  983. 'display' => $display,
  984. 'sort' => $max_sort + 1,
  985. 'shared_question_id' => $form_content['shared_question_id'],
  986. 'max_value' => $maxScore,
  987. ];
  988. $params = array_merge($params, $extraParams);
  989. $question_id = Database::insert($tbl_survey_question, $params);
  990. if ($question_id) {
  991. $sql = "UPDATE $tbl_survey_question SET question_id = $question_id
  992. WHERE iid = $question_id";
  993. Database::query($sql);
  994. $form_content['question_id'] = $question_id;
  995. $return_message = 'QuestionAdded';
  996. }
  997. } else {
  998. // Updating an existing question
  999. $extraParams = [];
  1000. if (isset($_POST['choose'])) {
  1001. if ($_POST['choose'] == 1) {
  1002. $extraParams['survey_group_pri'] = $_POST['assigned'];
  1003. $extraParams['survey_group_sec1'] = 0;
  1004. $extraParams['survey_group_sec2'] = 0;
  1005. } elseif ($_POST['choose'] == 2) {
  1006. $extraParams['survey_group_pri'] = 0;
  1007. $extraParams['survey_group_sec1'] = $_POST['assigned1'];
  1008. $extraParams['survey_group_sec2'] = $_POST['assigned2'];
  1009. }
  1010. }
  1011. $maxScore = isset($form_content['maximum_score']) ? $form_content['maximum_score'] : null;
  1012. $questionComment = isset($form_content['question_comment']) ? $form_content['question_comment'] : null;
  1013. // Adding the question to the survey_question table
  1014. $params = [
  1015. 'survey_question' => $form_content['question'],
  1016. 'survey_question_comment' => $questionComment,
  1017. 'display' => $form_content['horizontalvertical'],
  1018. ];
  1019. $params = array_merge($params, $extraParams);
  1020. Database::update(
  1021. $tbl_survey_question,
  1022. $params,
  1023. [
  1024. 'c_id = ? AND question_id = ?' => [
  1025. $course_id,
  1026. $form_content['question_id'],
  1027. ],
  1028. ]
  1029. );
  1030. $return_message = 'QuestionUpdated';
  1031. }
  1032. if (!empty($form_content['survey_id'])) {
  1033. //Updating survey
  1034. api_item_property_update(
  1035. api_get_course_info(),
  1036. TOOL_SURVEY,
  1037. $form_content['survey_id'],
  1038. 'SurveyUpdated',
  1039. api_get_user_id()
  1040. );
  1041. }
  1042. // Storing the options of the question
  1043. self::save_question_options($form_content, $survey_data);
  1044. } else {
  1045. $return_message = 'PleasFillAllAnswer';
  1046. }
  1047. } else {
  1048. $return_message = 'PleaseEnterAQuestion';
  1049. }
  1050. if (!empty($return_message)) {
  1051. Display::addFlash(Display::return_message(get_lang($return_message)));
  1052. }
  1053. return $return_message;
  1054. }
  1055. /**
  1056. * This function saves the question in the shared database
  1057. *
  1058. * @param array $form_content all the information of the form
  1059. * @param array $survey_data all the information of the survey
  1060. *
  1061. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1062. * @version February 2007
  1063. *
  1064. * @todo editing of a shared question
  1065. */
  1066. public function save_shared_question($form_content, $survey_data)
  1067. {
  1068. $_course = api_get_course_info();
  1069. // Table definitions
  1070. $tbl_survey_question = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  1071. // Storing a new question
  1072. if ($form_content['shared_question_id'] == '' ||
  1073. !is_numeric($form_content['shared_question_id'])
  1074. ) {
  1075. // Finding the max sort order of the questions in the given survey
  1076. $sql = "SELECT max(sort) AS max_sort FROM $tbl_survey_question
  1077. WHERE survey_id='".intval($survey_data['survey_share'])."'
  1078. AND code='".Database::escape_string($_course['id'])."'";
  1079. $result = Database::query($sql);
  1080. $row = Database::fetch_array($result, 'ASSOC');
  1081. $max_sort = $row['max_sort'];
  1082. // Adding the question to the survey_question table
  1083. $sql = "INSERT INTO $tbl_survey_question (survey_id, survey_question, survey_question_comment, type, display, sort, code) VALUES (
  1084. '".Database::escape_string($survey_data['survey_share'])."',
  1085. '".Database::escape_string($form_content['question'])."',
  1086. '".Database::escape_string($form_content['question_comment'])."',
  1087. '".Database::escape_string($form_content['type'])."',
  1088. '".Database::escape_string($form_content['horizontalvertical'])."',
  1089. '".Database::escape_string($max_sort + 1)."',
  1090. '".Database::escape_string($_course['id'])."')";
  1091. Database::query($sql);
  1092. $shared_question_id = Database::insert_id();
  1093. } else {
  1094. // Updating an existing question
  1095. // adding the question to the survey_question table
  1096. $sql = "UPDATE $tbl_survey_question SET
  1097. survey_question = '".Database::escape_string($form_content['question'])."',
  1098. survey_question_comment = '".Database::escape_string($form_content['question_comment'])."',
  1099. display = '".Database::escape_string($form_content['horizontalvertical'])."'
  1100. WHERE
  1101. question_id = '".intval($form_content['shared_question_id'])."' AND
  1102. code = '".Database::escape_string($_course['id'])."'";
  1103. Database::query($sql);
  1104. $shared_question_id = $form_content['shared_question_id'];
  1105. }
  1106. return $shared_question_id;
  1107. }
  1108. /**
  1109. * This functions moves a question of a survey up or down
  1110. *
  1111. * @param string $direction
  1112. * @param integer $survey_question_id
  1113. * @param integer $survey_id
  1114. *
  1115. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1116. * @version January 2007
  1117. */
  1118. public static function move_survey_question($direction, $survey_question_id, $survey_id)
  1119. {
  1120. // Table definition
  1121. $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
  1122. $course_id = api_get_course_int_id();
  1123. if ($direction == 'moveup') {
  1124. $sort = 'DESC';
  1125. }
  1126. if ($direction == 'movedown') {
  1127. $sort = 'ASC';
  1128. }
  1129. // Finding the two questions that needs to be swapped
  1130. $sql = "SELECT * FROM $table_survey_question
  1131. WHERE c_id = $course_id AND survey_id='".Database::escape_string($survey_id)."'
  1132. ORDER BY sort $sort";
  1133. $result = Database::query($sql);
  1134. $found = false;
  1135. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1136. if ($found) {
  1137. $question_id_two = $row['question_id'];
  1138. $question_sort_two = $row['sort'];
  1139. $found = false;
  1140. }
  1141. if ($row['question_id'] == $survey_question_id) {
  1142. $found = true;
  1143. $question_id_one = $row['question_id'];
  1144. $question_sort_one = $row['sort'];
  1145. }
  1146. }
  1147. $sql1 = "UPDATE $table_survey_question SET sort = '".Database::escape_string($question_sort_two)."'
  1148. WHERE c_id = $course_id AND question_id='".intval($question_id_one)."'";
  1149. Database::query($sql1);
  1150. $sql2 = "UPDATE $table_survey_question SET sort = '".Database::escape_string($question_sort_one)."'
  1151. WHERE c_id = $course_id AND question_id='".intval($question_id_two)."'";
  1152. Database::query($sql2);
  1153. }
  1154. /**
  1155. * This function deletes all the questions of a given survey
  1156. * This function is normally only called when a survey is deleted
  1157. *
  1158. * @param int $survey_id the id of the survey that has to be deleted
  1159. * @return true
  1160. *
  1161. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1162. * @version January 2007
  1163. */
  1164. public static function delete_all_survey_questions($survey_id, $shared = false)
  1165. {
  1166. $course_id = api_get_course_int_id();
  1167. // Table definitions
  1168. $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
  1169. $course_condition = " c_id = $course_id AND ";
  1170. if ($shared) {
  1171. $course_condition = '';
  1172. $table_survey_question = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  1173. }
  1174. $sql = "DELETE FROM $table_survey_question
  1175. WHERE $course_condition survey_id='".intval($survey_id)."'";
  1176. // Deleting the survey questions
  1177. Database::query($sql);
  1178. // Deleting all the options of the questions of the survey
  1179. self::delete_all_survey_questions_options($survey_id, $shared);
  1180. // Deleting all the answers on this survey
  1181. self::delete_all_survey_answers($survey_id);
  1182. }
  1183. /**
  1184. * This function deletes a survey question and all its options
  1185. *
  1186. * @param int $survey_id the id of the survey
  1187. * @param int $question_id the id of the question
  1188. * @param bool $shared
  1189. *
  1190. * @todo also delete the answers to this question
  1191. *
  1192. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1193. * @version March 2007
  1194. */
  1195. public static function delete_survey_question($survey_id, $question_id, $shared = false)
  1196. {
  1197. $course_id = api_get_course_int_id();
  1198. // Table definitions
  1199. $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
  1200. if ($shared) {
  1201. self::delete_shared_survey_question($survey_id, $question_id);
  1202. }
  1203. // Deleting the survey questions
  1204. $sql = "DELETE FROM $table_survey_question
  1205. WHERE
  1206. c_id = $course_id AND
  1207. survey_id='".intval($survey_id)."' AND
  1208. question_id='".intval($question_id)."'";
  1209. Database::query($sql);
  1210. // Deleting the options of the question of the survey
  1211. self::delete_survey_question_option($survey_id, $question_id, $shared);
  1212. }
  1213. /**
  1214. * This function deletes a shared survey question from the main database and all its options
  1215. *
  1216. * @param int $question_id the id of the question
  1217. * @param int $shared
  1218. *
  1219. * @todo delete all the options of this question
  1220. *
  1221. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1222. * @version March 2007
  1223. */
  1224. public static function delete_shared_survey_question($survey_id, $question_id)
  1225. {
  1226. // Table definitions
  1227. $table_survey_question = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  1228. $table_survey_question_option = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  1229. // First we have to get the shared_question_id
  1230. $question_data = self::get_question($question_id);
  1231. // Deleting the survey questions
  1232. $sql = "DELETE FROM $table_survey_question
  1233. WHERE question_id='".intval($question_data['shared_question_id'])."'";
  1234. Database::query($sql);
  1235. // Deleting the options of the question of the survey question
  1236. $sql = "DELETE FROM $table_survey_question_option
  1237. WHERE question_id='".intval($question_data['shared_question_id'])."'";
  1238. Database::query($sql);
  1239. }
  1240. /**
  1241. * This function stores the options of the questions in the table
  1242. *
  1243. * @param array $form_content
  1244. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1245. * @version January 2007
  1246. *
  1247. * @todo writing the update statement when editing a question
  1248. */
  1249. public static function save_question_options($form_content, $survey_data)
  1250. {
  1251. $course_id = api_get_course_int_id();
  1252. // A percentage question type has options 1 -> 100
  1253. if ($form_content['type'] == 'percentage') {
  1254. for ($i = 1; $i < 101; $i++) {
  1255. $form_content['answers'][] = $i;
  1256. }
  1257. }
  1258. if (is_numeric($survey_data['survey_share']) && $survey_data['survey_share'] != 0) {
  1259. self::save_shared_question_options($form_content, $survey_data);
  1260. }
  1261. // Table definition
  1262. $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  1263. // We are editing a question so we first have to remove all the existing options from the database
  1264. if (is_numeric($form_content['question_id'])) {
  1265. $sql = "DELETE FROM $table_survey_question_option
  1266. WHERE c_id = $course_id AND question_id = '".intval($form_content['question_id'])."'";
  1267. Database::query($sql);
  1268. }
  1269. $counter = 1;
  1270. if (isset($form_content['answers']) && is_array($form_content['answers'])) {
  1271. for ($i = 0; $i < count($form_content['answers']); $i++) {
  1272. $values = isset($form_content['values']) ? $form_content['values'][$i] : '';
  1273. $params = [
  1274. 'c_id' => $course_id,
  1275. 'question_id' => $form_content['question_id'],
  1276. 'survey_id' => $form_content['survey_id'],
  1277. 'option_text' => $form_content['answers'][$i],
  1278. 'value' => $values,
  1279. 'sort' => $counter,
  1280. ];
  1281. $insertId = Database::insert($table_survey_question_option, $params);
  1282. if ($insertId) {
  1283. $sql = "UPDATE $table_survey_question_option
  1284. SET question_option_id = $insertId
  1285. WHERE iid = $insertId";
  1286. Database::query($sql);
  1287. $counter++;
  1288. }
  1289. }
  1290. }
  1291. }
  1292. /**
  1293. * This function stores the options of the questions in the shared table
  1294. *
  1295. * @param array $form_content
  1296. *
  1297. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1298. * @version February 2007
  1299. *
  1300. * @todo writing the update statement when editing a question
  1301. */
  1302. public function save_shared_question_options($form_content, $survey_data)
  1303. {
  1304. if (is_array($form_content) && is_array($form_content['answers'])) {
  1305. // Table definition
  1306. $table = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  1307. // We are editing a question so we first have to remove all the existing options from the database
  1308. $sql = "DELETE FROM $table
  1309. WHERE question_id = '".Database::escape_string($form_content['shared_question_id'])."'";
  1310. Database::query($sql);
  1311. $counter = 1;
  1312. foreach ($form_content['answers'] as & $answer) {
  1313. $params = [
  1314. 'question_id' => $form_content['shared_question_id'],
  1315. 'survey_id' => $survey_data['is_shared'],
  1316. 'option_text' => $answer,
  1317. 'sort' => $counter,
  1318. ];
  1319. Database::insert($table, $params);
  1320. $counter++;
  1321. }
  1322. }
  1323. }
  1324. /**
  1325. * This function deletes all the options of the questions of a given survey
  1326. * This function is normally only called when a survey is deleted
  1327. *
  1328. * @param $survey_id the id of the survey that has to be deleted
  1329. * @return true
  1330. *
  1331. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1332. * @version January 2007
  1333. */
  1334. public static function delete_all_survey_questions_options($survey_id, $shared = false)
  1335. {
  1336. // Table definitions
  1337. $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  1338. $course_id = api_get_course_int_id();
  1339. $course_condition = " c_id = $course_id AND ";
  1340. if ($shared) {
  1341. $course_condition = "";
  1342. $table_survey_question_option = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  1343. }
  1344. $sql = "DELETE FROM $table_survey_question_option
  1345. WHERE $course_condition survey_id='".intval($survey_id)."'";
  1346. // Deleting the options of the survey questions
  1347. Database::query($sql);
  1348. return true;
  1349. }
  1350. /**
  1351. * This function deletes the options of a given question
  1352. *
  1353. * @param int $survey_id
  1354. * @param int $question_id
  1355. * @param bool $shared
  1356. *
  1357. * @return bool
  1358. *
  1359. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1360. * @author Julio Montoya
  1361. * @version March 2007
  1362. */
  1363. public static function delete_survey_question_option($survey_id, $question_id, $shared = false)
  1364. {
  1365. $course_id = api_get_course_int_id();
  1366. $course_condition = " c_id = $course_id AND ";
  1367. // Table definitions
  1368. $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  1369. if ($shared) {
  1370. $course_condition = "";
  1371. $table_survey_question_option = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  1372. }
  1373. // Deleting the options of the survey questions
  1374. $sql = "DELETE from $table_survey_question_option
  1375. WHERE
  1376. $course_condition survey_id='".intval($survey_id)."' AND
  1377. question_id='".intval($question_id)."'";
  1378. Database::query($sql);
  1379. return true;
  1380. }
  1381. /**
  1382. * SURVEY ANSWERS FUNCTIONS
  1383. */
  1384. /**
  1385. * This function deletes all the answers anyone has given on this survey
  1386. * This function is normally only called when a survey is deleted
  1387. *
  1388. * @param $survey_id the id of the survey that has to be deleted
  1389. * @return true
  1390. *
  1391. * @todo write the function
  1392. *
  1393. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1394. * @version January 2007,december 2008
  1395. */
  1396. public static function delete_all_survey_answers($survey_id)
  1397. {
  1398. $course_id = api_get_course_int_id();
  1399. $table_survey_answer = Database::get_course_table(TABLE_SURVEY_ANSWER);
  1400. $survey_id = intval($survey_id);
  1401. $sql = "DELETE FROM $table_survey_answer
  1402. WHERE c_id = $course_id AND survey_id=$survey_id";
  1403. Database::query($sql);
  1404. return true;
  1405. }
  1406. /**
  1407. * @param int $user_id
  1408. * @param int $survey_id
  1409. * @param int $course_id
  1410. * @return bool
  1411. */
  1412. public static function is_user_filled_survey($user_id, $survey_id, $course_id)
  1413. {
  1414. $table_survey_answer = Database::get_course_table(TABLE_SURVEY_ANSWER);
  1415. $user_id = intval($user_id);
  1416. $course_id = intval($course_id);
  1417. $survey_id = intval($survey_id);
  1418. $sql = "SELECT DISTINCT user
  1419. FROM $table_survey_answer
  1420. WHERE
  1421. c_id = $course_id AND
  1422. user = $user_id AND
  1423. survey_id = $survey_id";
  1424. $result = Database::query($sql);
  1425. if (Database::num_rows($result)) {
  1426. return true;
  1427. }
  1428. return false;
  1429. }
  1430. /**
  1431. * This function gets all the persons who have filled the survey
  1432. *
  1433. * @param integer $survey_id
  1434. * @return array
  1435. *
  1436. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1437. * @version February 2007
  1438. */
  1439. public static function get_people_who_filled_survey($survey_id, $all_user_info = false, $course_id = null)
  1440. {
  1441. // Database table definition
  1442. $table_survey_answer = Database::get_course_table(TABLE_SURVEY_ANSWER);
  1443. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  1444. // Variable initialisation
  1445. $return = array();
  1446. if (empty($course_id)) {
  1447. $course_id = api_get_course_int_id();
  1448. } else {
  1449. $course_id = intval($course_id);
  1450. }
  1451. if ($all_user_info) {
  1452. $order_clause = api_sort_by_first_name() ? ' ORDER BY user.firstname, user.lastname' : ' ORDER BY user.lastname, user.firstname';
  1453. $sql = "SELECT DISTINCT
  1454. answered_user.user as invited_user,
  1455. user.firstname,
  1456. user.lastname,
  1457. user.user_id
  1458. FROM $table_survey_answer answered_user
  1459. LEFT JOIN $table_user as user ON answered_user.user = user.user_id
  1460. WHERE
  1461. answered_user.c_id = $course_id AND
  1462. survey_id= '".Database::escape_string($survey_id)."' ".
  1463. $order_clause;
  1464. } else {
  1465. $sql = "SELECT DISTINCT user FROM $table_survey_answer
  1466. WHERE c_id = $course_id AND survey_id= '".Database::escape_string($survey_id)."' ";
  1467. }
  1468. $res = Database::query($sql);
  1469. while ($row = Database::fetch_array($res, 'ASSOC')) {
  1470. if ($all_user_info) {
  1471. $userInfo = api_get_user_info($row['user_id']);
  1472. $row['user_info'] = $userInfo;
  1473. $return[] = $row;
  1474. } else {
  1475. $return[] = $row['user'];
  1476. }
  1477. }
  1478. return $return;
  1479. }
  1480. /**
  1481. * @return bool
  1482. */
  1483. public static function survey_generation_hash_available()
  1484. {
  1485. if (extension_loaded('mcrypt')) {
  1486. return true;
  1487. }
  1488. return false;
  1489. }
  1490. /**
  1491. * @param int $survey_id
  1492. * @param int $course_id
  1493. * @param int $session_id
  1494. * @param int $group_id
  1495. * @return string
  1496. */
  1497. public static function generate_survey_hash($survey_id, $course_id, $session_id, $group_id)
  1498. {
  1499. $hash = hash('sha512', api_get_security_key().'_'.$course_id.'_'.$session_id.'_'.$group_id.'_'.$survey_id);
  1500. return $hash;
  1501. }
  1502. /**
  1503. * @param int $survey_id
  1504. * @param int $course_id
  1505. * @param int $session_id
  1506. * @param int $group_id
  1507. * @param string $hash
  1508. *
  1509. * @return bool
  1510. */
  1511. public static function validate_survey_hash($survey_id, $course_id, $session_id, $group_id, $hash)
  1512. {
  1513. $survey_generated_hash = self::generate_survey_hash($survey_id, $course_id, $session_id, $group_id);
  1514. if ($survey_generated_hash == $hash) {
  1515. return true;
  1516. }
  1517. return false;
  1518. }
  1519. /**
  1520. * @param int $survey_id
  1521. * @param int $course_id
  1522. * @param int $session_id
  1523. * @param int $group_id
  1524. *
  1525. * @return string
  1526. */
  1527. public static function generate_survey_link($survey_id, $course_id, $session_id, $group_id)
  1528. {
  1529. $code = self::generate_survey_hash($survey_id, $course_id, $session_id, $group_id);
  1530. return api_get_path(WEB_CODE_PATH).'survey/link.php?h='.$code.'&i='.$survey_id.'&c='.intval($course_id).'&s='.intval($session_id).'&g='.$group_id;
  1531. }
  1532. }