survey_invite.php 10 KB

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