survey_invite.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.survey
  5. * @author unknown, the initial survey that did not make it in 1.8 because of bad code
  6. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts of the code
  7. * @author Julio Montoya Chamilo: cleanup, refactoring, security improvements
  8. * @version $Id: survey_invite.php 10680 2007-01-11 21:26:23Z pcool $
  9. *
  10. * @todo checking if the additional emails are valid (or add a rule for this)
  11. * @todo check if the mailtext contains the **link** part, if not, add the link to the end
  12. * @todo add rules: title and text cannot be empty
  13. */
  14. //require_once '../inc/global.inc.php';
  15. $this_section = SECTION_COURSES;
  16. if (!api_is_allowed_to_edit(false, true)) {
  17. api_not_allowed(true);
  18. }
  19. // Database table definitions
  20. $table_survey = Database:: get_course_table(TABLE_SURVEY);
  21. $table_survey_question = Database:: get_course_table(TABLE_SURVEY_QUESTION);
  22. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  23. $table_course = Database:: get_main_table(TABLE_MAIN_COURSE);
  24. $table_user = Database:: get_main_table(TABLE_MAIN_USER);
  25. $course_id = api_get_course_int_id();
  26. // Getting the survey information
  27. $survey_id = Security::remove_XSS($_GET['survey_id']);
  28. $survey_data = SurveyManager::get_survey($survey_id);
  29. if (empty($survey_data)) {
  30. api_not_allowed(true, get_lang('InvallidSurvey'));
  31. }
  32. $urlname = strip_tags(api_substr(api_html_entity_decode($survey_data['title'], ENT_QUOTES), 0, 40));
  33. if (api_strlen(strip_tags($survey_data['title'])) > 40) {
  34. $urlname .= '...';
  35. }
  36. // Breadcrumbs
  37. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq(), 'name' => get_lang('SurveyList'));
  38. if (api_is_course_admin()) {
  39. $interbreadcrumb[] = array(
  40. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id.'&'.api_get_cidreq(),
  41. 'name' => $urlname,
  42. );
  43. } else {
  44. $interbreadcrumb[] = array(
  45. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_invite.php?survey_id='.$survey_id.'&'.api_get_cidreq(),
  46. 'name' => $urlname,
  47. );
  48. }
  49. $tool_name = get_lang('SurveyPublication');
  50. // Displaying the header
  51. Display::display_header($tool_name,'Survey');
  52. echo '<script>
  53. $(function() {
  54. $("#check_mail").change(function() {
  55. $("#mail_text_wrapper").toggle();
  56. });
  57. });
  58. </script>';
  59. // Checking if there is another survey with this code.
  60. // If this is the case there will be a language choice
  61. $sql = "SELECT * FROM $table_survey
  62. WHERE
  63. c_id = $course_id AND
  64. code='".Database::escape_string($survey_data['code'])."'";
  65. $result = Database::query($sql);
  66. if (Database::num_rows($result) > 1) {
  67. Display::display_warning_message(get_lang('IdenticalSurveycodeWarning'));
  68. }
  69. // Invited / answered message
  70. if ($survey_data['invited'] > 0 && !isset($_POST['submit'])) {
  71. $message = '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=answered&survey_id='.$survey_data['survey_id'].'">'.$survey_data['answered'].'</a> ';
  72. $message .= get_lang('HaveAnswered').' ';
  73. $message .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=invited&survey_id='.$survey_data['survey_id'].'">'.$survey_data['invited'].'</a> ';
  74. $message .= get_lang('WereInvited');
  75. Display::display_normal_message($message, false);
  76. }
  77. // Building the form for publishing the survey
  78. $form = new FormValidator(
  79. 'publish_form',
  80. 'post',
  81. api_get_self().'?survey_id='.$survey_id.'&'.api_get_cidReq()
  82. );
  83. $form->addElement('header', $tool_name);
  84. // Course users
  85. $complete_user_list = CourseManager::get_user_list_from_course_code(
  86. api_get_course_id(),
  87. api_get_session_id(),
  88. '',
  89. api_sort_by_first_name() ? 'ORDER BY firstname' : 'ORDER BY lastname'
  90. );
  91. $possible_users = array();
  92. foreach ($complete_user_list as & $user) {
  93. $possible_users[$user['user_id']] = api_get_person_name($user['firstname'], $user['lastname']);
  94. }
  95. CourseManager::addUserGroupMultiSelect($form, array());
  96. /*$form->addElement(
  97. 'advmultiselect',
  98. 'course_users',
  99. get_lang('CourseUsers'),
  100. $possible_users,
  101. 'style="width: 250px; height: 200px;"'
  102. );*/
  103. // Additional users
  104. $form->addElement(
  105. 'textarea',
  106. 'additional_users',
  107. array(get_lang('AdditonalUsers'), get_lang('AdditonalUsersComment')),
  108. array('rows' => 5)
  109. );
  110. $form->addElement('html', '<div id="check_mail">');
  111. $form->addElement('checkbox', 'send_mail','', get_lang('SendMail'));
  112. $form->addElement('html', '</div>');
  113. $form->addElement('html', '<div id="mail_text_wrapper">');
  114. // The title of the mail
  115. $form->addText('mail_title', get_lang('MailTitle'), false);
  116. // The text of the mail
  117. $form->addHtmlEditor(
  118. 'mail_text',
  119. array(get_lang('MailText'), get_lang('UseLinkSyntax')),
  120. false,
  121. [],
  122. array('ToolbarSet' => 'Survey', 'Height' => '150')
  123. );
  124. $form->addElement('html', '</div>');
  125. // You cab send a reminder to unanswered people if the survey is not anonymous
  126. if ($survey_data['anonymous'] != 1) {
  127. $form->addElement('checkbox', 'remindUnAnswered', '', get_lang('RemindUnanswered'));
  128. }
  129. // Allow resending to all selected users
  130. $form->addElement('checkbox', 'resend_to_all', '', get_lang('ReminderResendToAllUsers'));
  131. // Submit button
  132. $form->addButtonSave(get_lang('PublishSurvey'));
  133. // The rules (required fields)
  134. /*if ($survey_data['send_mail'] == 0) {
  135. $form->addRule('mail_title', get_lang('ThisFieldIsRequired'), 'required');
  136. $form->addRule('mail_text', get_lang('ThisFieldIsRequired'), 'required');
  137. }*/
  138. $portal_url = api_get_path(WEB_PATH);
  139. if (api_is_multiple_url_enabled()) {
  140. $access_url_id = api_get_current_access_url_id();
  141. if ($access_url_id != -1) {
  142. $url = api_get_access_url($access_url_id);
  143. $portal_url = $url['url'];
  144. }
  145. }
  146. // Show the URL that can be used by users to fill a survey without invitation
  147. $auto_survey_link = $portal_url.'main/survey/fillsurvey.php?course='.$_course['sysCode'].'&invitationcode=auto&scode='.$survey_data['survey_code'];
  148. $form->addElement('label', null, get_lang('AutoInviteLink'));
  149. $form->addElement('label', null, $auto_survey_link);
  150. if ($form->validate()) {
  151. $values = $form->exportValues();
  152. if (isset($values['send_mail']) && $values['send_mail'] == 1) {
  153. if (empty($values['mail_title']) || empty($values['mail_text'])) {
  154. Display :: display_error_message(get_lang('FormHasErrorsPleaseComplete'));
  155. // Getting the invited users
  156. $defaults = SurveyUtil::get_invited_users($survey_data['code']);
  157. // Getting the survey mail text
  158. if (!empty($survey_data['reminder_mail'])) {
  159. $defaults['mail_text'] = $survey_data['reminder_mail'];
  160. } else {
  161. $defaults['mail_text'] = $survey_data['invite_mail'];
  162. }
  163. $defaults['mail_title'] = $survey_data['mail_subject'];
  164. $defaults['send_mail'] = 1;
  165. $form->setDefaults($defaults);
  166. $form->display();
  167. return;
  168. }
  169. }
  170. // Save the invitation mail
  171. SurveyUtil::save_invite_mail(
  172. $values['mail_text'],
  173. $values['mail_title'],
  174. !empty($survey_data['invite_mail'])
  175. );
  176. $resendAll = isset($values['resend_to_all']) ? $values['resend_to_all'] : '';
  177. $sendMail = isset($values['send_mail']) ? $values['send_mail'] : '';
  178. $remindUnAnswered = isset($values['remindUnAnswered']) ? $values['remindUnAnswered'] : '';
  179. // Saving the invitations for the course users
  180. $count_course_users = SurveyUtil::saveInvitations(
  181. $values['users'],
  182. $values['mail_title'],
  183. $values['mail_text'],
  184. $resendAll,
  185. $sendMail,
  186. $remindUnAnswered
  187. );
  188. // Saving the invitations for the additional users
  189. $values['additional_users'] = $values['additional_users'].';'; // This is for the case when you enter only one email
  190. $temp = str_replace(',', ';', $values['additional_users']); // This is to allow , and ; as email separators
  191. $additional_users = explode(';', $temp);
  192. for ($i = 0; $i < count($additional_users); $i++) {
  193. $additional_users[$i] = trim($additional_users[$i]);
  194. }
  195. $counter_additional_users = SurveyUtil::saveInvitations(
  196. $additional_users,
  197. $values['mail_title'],
  198. $values['mail_text'],
  199. $resendAll,
  200. $sendMail,
  201. $remindUnAnswered
  202. );
  203. // Updating the invited field in the survey table
  204. SurveyUtil::update_count_invited($survey_data['code']);
  205. $total_count = $count_course_users + $counter_additional_users;
  206. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  207. // Counting the number of people that are invited
  208. $sql = "SELECT * FROM $table_survey
  209. WHERE
  210. c_id = $course_id AND
  211. code = '".Database::escape_string($survey_data['code'])."'
  212. ";
  213. $result = Database::query($sql);
  214. $row = Database::fetch_array($result);
  215. $total_invited = $row['invited'];
  216. if ($total_invited > 0) {
  217. $message = '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=answered&survey_id='.$survey_data['survey_id'].'">'.
  218. $survey_data['answered'].'</a> ';
  219. $message .= get_lang('HaveAnswered').' ';
  220. $message .= '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=invited&survey_id='.$survey_data['survey_id'].'">'.
  221. $total_invited.'</a> ';
  222. $message .= get_lang('WereInvited');
  223. Display::display_normal_message($message, false);
  224. Display::display_confirmation_message($total_count.' '.get_lang('InvitationsSend'));
  225. }
  226. } else {
  227. // Getting the invited users
  228. $defaults = SurveyUtil::get_invited_users($survey_data['code']);
  229. // Getting the survey mail text
  230. if (!empty($survey_data['reminder_mail'])) {
  231. $defaults['mail_text'] = $survey_data['reminder_mail'];
  232. } else {
  233. $defaults['mail_text'] = $survey_data['invite_mail'];
  234. }
  235. $defaults['mail_title'] = $survey_data['mail_subject'];
  236. $defaults['send_mail'] = 1;
  237. $form->setDefaults($defaults);
  238. $form->display();
  239. }
  240. Display :: display_footer();