survey_invite.php 10 KB

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