survey_invite.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
  32. $table_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  33. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  34. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  35. $urlname = strip_tags(api_substr(api_html_entity_decode($survey_data['title'], ENT_QUOTES), 0, 40));
  36. if (api_strlen(strip_tags($survey_data['title'])) > 40) {
  37. $urlname .= '...';
  38. }
  39. // Breadcrumbs
  40. $interbreadcrumb[] = [
  41. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_list.php?'.api_get_cidreq(),
  42. 'name' => get_lang('SurveyList'),
  43. ];
  44. if (api_is_course_admin()) {
  45. if ($survey_data['survey_type'] == 3) {
  46. $interbreadcrumb[] = [
  47. 'url' => api_get_path(WEB_CODE_PATH).'survey/meeting.php?survey_id='.$survey_id.'&'.api_get_cidreq(),
  48. 'name' => $urlname,
  49. ];
  50. } else {
  51. $interbreadcrumb[] = [
  52. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$survey_id.'&'.api_get_cidreq(),
  53. 'name' => $urlname,
  54. ];
  55. }
  56. } else {
  57. $interbreadcrumb[] = [
  58. 'url' => api_get_path(WEB_CODE_PATH).'survey/survey_invite.php?survey_id='.$survey_id.'&'.api_get_cidreq(),
  59. 'name' => $urlname,
  60. ];
  61. }
  62. $tool_name = get_lang('SurveyPublication');
  63. // Displaying the header
  64. Display::display_header($tool_name, 'Survey');
  65. echo '<script>
  66. $(function() {
  67. $("#check_mail").change(function() {
  68. $("#mail_text_wrapper").toggle();
  69. });
  70. });
  71. </script>';
  72. // Checking if there is another survey with this code.
  73. // If this is the case there will be a language choice
  74. $sql = "SELECT * FROM $table_survey
  75. WHERE c_id = $course_id AND code='".Database::escape_string($survey_data['code'])."'";
  76. $result = Database::query($sql);
  77. if (Database::num_rows($result) > 1) {
  78. echo Display::return_message(get_lang('IdenticalSurveycodeWarning'), 'warning');
  79. }
  80. // Invited / answered message
  81. if ($survey_data['invited'] > 0 && !isset($_POST['submit'])) {
  82. $message = Display::url(
  83. $survey_data['answered'],
  84. api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=answered&survey_id='.$survey_data['survey_id'].'&'.api_get_cidreq()
  85. );
  86. $message .= ' '.get_lang('HaveAnswered').' ';
  87. $message .= Display::url(
  88. $survey_data['invited'],
  89. api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=invited&survey_id='.$survey_data['survey_id'].'&'.api_get_cidreq());
  90. $message .= ' '.get_lang('WereInvited');
  91. echo Display::return_message($message, 'normal', false);
  92. }
  93. // Building the form for publishing the survey
  94. $form = new FormValidator(
  95. 'publish_form',
  96. 'post',
  97. api_get_self().'?survey_id='.$survey_id.'&'.api_get_cidreq()
  98. );
  99. $form->addElement('header', '', $tool_name);
  100. // Course users
  101. $complete_user_list = CourseManager::get_user_list_from_course_code(
  102. api_get_course_id(),
  103. api_get_session_id(),
  104. '',
  105. api_sort_by_first_name() ? 'ORDER BY firstname' : 'ORDER BY lastname'
  106. );
  107. $possible_users = [];
  108. foreach ($complete_user_list as &$user) {
  109. $possible_users[$user['user_id']] = api_get_person_name(
  110. $user['firstname'],
  111. $user['lastname']
  112. );
  113. }
  114. CourseManager::addUserGroupMultiSelect($form, []);
  115. // Additional users
  116. $form->addElement(
  117. 'textarea',
  118. 'additional_users',
  119. [get_lang('AdditonalUsers'), get_lang('AdditonalUsersComment')],
  120. ['rows' => 5]
  121. );
  122. $form->addElement('html', '<div id="check_mail">');
  123. $form->addElement('checkbox', 'send_mail', '', get_lang('SendMail'));
  124. $form->addElement('html', '</div>');
  125. $form->addElement('html', '<div id="mail_text_wrapper">');
  126. // The title of the mail
  127. $form->addText('mail_title', get_lang('MailTitle'), false);
  128. // The text of the mail
  129. $form->addHtmlEditor(
  130. 'mail_text',
  131. [get_lang('MailText'), get_lang('UseLinkSyntax')],
  132. false,
  133. ['ToolbarSet' => 'Survey', 'Height' => '150']
  134. );
  135. $form->addElement('html', '</div>');
  136. // You cab send a reminder to unanswered people if the survey is not anonymous
  137. if ($survey_data['anonymous'] != 1 || api_get_configuration_value('survey_anonymous_show_answered')) {
  138. $form->addElement('checkbox', 'remindUnAnswered', '', get_lang('RemindUnanswered'));
  139. }
  140. // Allow resending to all selected users
  141. $form->addElement('checkbox', 'resend_to_all', '', get_lang('ReminderResendToAllUsers'));
  142. $form->addElement('checkbox', 'hide_link', '', get_lang('HideSurveyInvitationLink'));
  143. // Submit button
  144. $form->addButtonSave(get_lang('PublishSurvey'));
  145. $portal_url = api_get_path(WEB_PATH);
  146. if (api_is_multiple_url_enabled()) {
  147. $access_url_id = api_get_current_access_url_id();
  148. if ($access_url_id != -1) {
  149. $url = api_get_access_url($access_url_id);
  150. $portal_url = $url['url'];
  151. }
  152. }
  153. // Show the URL that can be used by users to fill a survey without invitation
  154. $auto_survey_link = SurveyUtil::generateFillSurveyLink(
  155. 'auto',
  156. $_course,
  157. $survey_data['session_id'],
  158. $survey_data['survey_code']
  159. );
  160. $form->addElement('label', null, get_lang('AutoInviteLink'));
  161. $form->addElement('label', null, $auto_survey_link);
  162. if ($form->validate()) {
  163. $values = $form->exportValues();
  164. $resendAll = isset($values['resend_to_all']) ? $values['resend_to_all'] : '';
  165. $sendMail = isset($values['send_mail']) ? $values['send_mail'] : '';
  166. $remindUnAnswered = isset($values['remindUnAnswered']) ? $values['remindUnAnswered'] : '';
  167. $users = isset($values['users']) ? $values['users'] : [];
  168. $hideLink = isset($values['hide_link']) && $values['hide_link'] ? true : false;
  169. if ($sendMail) {
  170. if (empty($values['mail_title']) || empty($values['mail_text'])) {
  171. echo Display::return_message(get_lang('FormHasErrorsPleaseComplete'), 'error');
  172. // Getting the invited users
  173. $defaults = SurveyUtil::get_invited_users($survey_data['code']);
  174. // Getting the survey mail text
  175. if (!empty($survey_data['reminder_mail'])) {
  176. $defaults['mail_text'] = $survey_data['reminder_mail'];
  177. } else {
  178. $defaults['mail_text'] = $survey_data['invite_mail'];
  179. }
  180. $defaults['mail_title'] = $survey_data['mail_subject'];
  181. $defaults['send_mail'] = 1;
  182. $form->setDefaults($defaults);
  183. $form->display();
  184. return;
  185. }
  186. }
  187. // Save the invitation mail
  188. SurveyUtil::save_invite_mail(
  189. $values['mail_text'],
  190. $values['mail_title'],
  191. !empty($survey_data['invite_mail'])
  192. );
  193. // Saving the invitations for the course users
  194. $count_course_users = SurveyUtil::saveInvitations(
  195. $users,
  196. $values['mail_title'],
  197. $values['mail_text'],
  198. $resendAll,
  199. $sendMail,
  200. $remindUnAnswered,
  201. false,
  202. $hideLink
  203. );
  204. // Saving the invitations for the additional users
  205. $values['additional_users'] = $values['additional_users'].';'; // This is for the case when you enter only one email
  206. $temp = str_replace(',', ';', $values['additional_users']); // This is to allow , and ; as email separators
  207. $additional_users = explode(';', $temp);
  208. for ($i = 0; $i < count($additional_users); $i++) {
  209. $additional_users[$i] = trim($additional_users[$i]);
  210. }
  211. $counter_additional_users = SurveyUtil::saveInvitations(
  212. $additional_users,
  213. $values['mail_title'],
  214. $values['mail_text'],
  215. $resendAll,
  216. $sendMail,
  217. $remindUnAnswered,
  218. true
  219. );
  220. // Updating the invited field in the survey table
  221. // Counting the number of people that are invited
  222. $total_invited = SurveyUtil::update_count_invited($survey_data['code']);
  223. $total_count = $count_course_users + $counter_additional_users;
  224. $invitationUrl = api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?survey_id='.$survey_data['survey_id'].'&'.api_get_cidreq();
  225. if ($total_invited > 0) {
  226. $message = '<a href="'.$invitationUrl.'&view=answered">'.
  227. $survey_data['answered'].'</a> ';
  228. $message .= get_lang('HaveAnswered').' ';
  229. $message .= '<a href="'.$invitationUrl.'&view=invited">'.
  230. $total_invited.'</a> ';
  231. $message .= get_lang('WereInvited');
  232. echo Display::return_message($message, 'normal', false);
  233. if ($sendMail) {
  234. echo Display::return_message($total_count.' '.get_lang('InvitationsSend'), 'success', false);
  235. }
  236. }
  237. } else {
  238. // Getting the invited users
  239. $defaults = SurveyUtil::get_invited_users($survey_data['code']);
  240. // Getting the survey mail text
  241. if (!empty($survey_data['reminder_mail'])) {
  242. $defaults['mail_text'] = $survey_data['reminder_mail'];
  243. } else {
  244. $defaults['mail_text'] = $survey_data['invite_mail'];
  245. }
  246. $defaults['mail_title'] = $survey_data['mail_subject'];
  247. $defaults['send_mail'] = 1;
  248. $form->setDefaults($defaults);
  249. $form->display();
  250. }
  251. Display::display_footer();