survey_invite.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. // Language file that needs to be included
  15. $language_file = 'survey';
  16. // Including the global initialization file
  17. require_once '../inc/global.inc.php';
  18. // Including additional libraries
  19. require_once 'survey.lib.php';
  20. $this_section = SECTION_COURSES;
  21. if (!api_is_allowed_to_edit(false, true)) {
  22. Display :: display_header(get_lang('ToolSurvey'));
  23. Display :: display_error_message(get_lang('NotAllowed'), false);
  24. Display :: display_footer();
  25. exit;
  26. }
  27. // Database table definitions
  28. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  29. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  30. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  31. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  32. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  33. $course_id = api_get_course_int_id();
  34. // Getting the survey information
  35. $survey_id = Security::remove_XSS($_GET['survey_id']);
  36. $survey_data = survey_manager::get_survey($survey_id);
  37. if (empty($survey_data)) {
  38. Display :: display_header(get_lang('ToolSurvey'));
  39. Display :: display_error_message(get_lang('InvallidSurvey'), false);
  40. Display :: display_footer();
  41. exit;
  42. }
  43. $urlname = strip_tags(api_substr(api_html_entity_decode($survey_data['title'], ENT_QUOTES), 0, 40));
  44. if (api_strlen(strip_tags($survey_data['title'])) > 40) {
  45. $urlname .= '...';
  46. }
  47. // Breadcrumbs
  48. $interbreadcrumb[] = array('url' => 'survey_list.php', 'name' => get_lang('SurveyList'));
  49. if (api_is_course_admin()) {
  50. $interbreadcrumb[] = array('url' => 'survey.php?survey_id='.$survey_id, 'name' => $urlname);
  51. } else {
  52. $interbreadcrumb[] = array('url' => 'survey_invite.php?survey_id='.$survey_id, 'name' => $urlname);
  53. }
  54. $tool_name = get_lang('SurveyPublication');
  55. // Displaying the header
  56. Display::display_header($tool_name, 'Survey');
  57. echo '<script>
  58. $(function() {
  59. $("#check_mail").change(function() {
  60. $("#mail_text").toggle();
  61. });
  62. });
  63. </script>';
  64. // Checking if there is another survey with this code.
  65. // If this is the case there will be a language choice
  66. $sql = "SELECT * FROM $table_survey WHERE c_id = $course_id AND code='".Database::escape_string(
  67. $survey_data['code']
  68. )."'";
  69. $result = Database::query($sql);
  70. if (Database::num_rows($result) > 1) {
  71. Display::display_warning_message(get_lang('IdenticalSurveycodeWarning'));
  72. }
  73. // Invited / answered message
  74. if ($survey_data['invited'] > 0 && !isset($_POST['submit'])) {
  75. $message = '<a href="survey_invitation.php?view=answered&amp;survey_id='.$survey_data['survey_id'].'">'.$survey_data['answered'].'</a> ';
  76. $message .= get_lang('HaveAnswered').' ';
  77. $message .= '<a href="survey_invitation.php?view=invited&amp;survey_id='.$survey_data['survey_id'].'">'.$survey_data['invited'].'</a> ';
  78. $message .= get_lang('WereInvited');
  79. Display::display_normal_message($message, false);
  80. }
  81. // Building the form for publishing the survey
  82. $form = new FormValidator('publish_form', 'post', api_get_self().'?survey_id='.$survey_id.'&'.api_get_cidReq());
  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. $list = UserManager::generate_user_group_array(api_get_course_id(), api_get_session_id());
  96. $users = $form->addElement('advmultiselect', 'course_users', get_lang('CourseUsers'), $list);
  97. $users->setButtonAttributes('add');
  98. $users->setButtonAttributes('remove');
  99. // Additional users
  100. $form->addElement(
  101. 'textarea',
  102. 'additional_users',
  103. array(get_lang('AdditonalUsers'), get_lang('AdditonalUsersComment')),
  104. array('class' => 'span6', 'rows' => 5)
  105. );
  106. $form->addElement('html', '<div id="check_mail">');
  107. $form->addElement('checkbox', 'send_mail', '', get_lang('SendMail'));
  108. $form->addElement('html', '</div>');
  109. $form->addElement('html', '<div id="mail_text">');
  110. // The title of the mail
  111. $form->addElement('text', 'mail_title', get_lang('MailTitle'), array('class' => 'span6'));
  112. // The text of the mail
  113. $form->addElement(
  114. 'html_editor',
  115. 'mail_text',
  116. array(get_lang('MailText'), get_lang('UseLinkSyntax')),
  117. null,
  118. array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '150')
  119. );
  120. $form->addElement('html', '</div>');
  121. // You cab send a reminder to unanswered people if the survey is not anonymous
  122. if ($survey_data['anonymous'] != 1) {
  123. $form->addElement('checkbox', 'remindUnAnswered', '', get_lang('RemindUnanswered'));
  124. }
  125. // Allow resending to all selected users
  126. $form->addElement('checkbox', 'resend_to_all', '', get_lang('ReminderResendToAllUsers'));
  127. // Submit button
  128. $form->addElement('style_submit_button', 'submit', get_lang('PublishSurvey'), 'class="save"');
  129. // The rules (required fields)
  130. $portal_url = api_get_path(WEB_PATH);
  131. if (api_is_multiple_url_enabled()) {
  132. $access_url_id = api_get_current_access_url_id();
  133. if ($access_url_id != -1) {
  134. $url = api_get_access_url($access_url_id);
  135. $portal_url = $url['url'];
  136. }
  137. }
  138. // Show the URL that can be used by users to fill a survey without invitation
  139. $auto_survey_link = $portal_url.'main/survey/fillsurvey.php?course='.$_course['sysCode'].'&invitationcode=auto&scode='.$survey_data['survey_code'];
  140. $form->addElement('label', null, get_lang('AutoInviteLink'));
  141. $form->addElement('label', null, "<pre>$auto_survey_link</pre>");
  142. if ($form->validate()) {
  143. $values = $form->exportValues();
  144. if (isset($values['send_mail']) && $values['send_mail'] == 1) {
  145. if (empty($values['mail_title']) || empty($values['mail_text'])) {
  146. Display :: display_error_message(get_lang('FormHasErrorsPleaseComplete'));
  147. // Getting the invited users
  148. $defaults = SurveyUtil::get_invited_users($survey_data['code']);
  149. // Getting the survey mail text
  150. if (!empty($survey_data['reminder_mail'])) {
  151. $defaults['mail_text'] = $survey_data['reminder_mail'];
  152. } else {
  153. $defaults['mail_text'] = $survey_data['invite_mail'];
  154. }
  155. $defaults['mail_title'] = $survey_data['mail_subject'];
  156. $defaults['send_mail'] = 1;
  157. $form->setDefaults($defaults);
  158. $form->display();
  159. }
  160. }
  161. // Save the invitation mail
  162. SurveyUtil::save_invite_mail($values['mail_text'], $values['mail_title'], !empty($survey_data['invite_mail']));
  163. // Saving the invitations for the course users
  164. $count_course_users = SurveyUtil::save_invitations(
  165. $values['course_users'],
  166. $values['mail_title'],
  167. $values['mail_text'],
  168. $values['resend_to_all'],
  169. $values['send_mail'],
  170. $values['remindUnAnswered']
  171. );
  172. // Saving the invitations for the additional users
  173. $values['additional_users'] = $values['additional_users'].';'; // This is for the case when you enter only one email
  174. $temp = str_replace(',', ';', $values['additional_users']); // This is to allow , and ; as email separators
  175. $additional_users = explode(';', $temp);
  176. for ($i = 0; $i < count($additional_users); $i++) {
  177. $additional_users[$i] = trim($additional_users[$i]);
  178. }
  179. $counter_additional_users = SurveyUtil::save_invitations(
  180. $additional_users,
  181. $values['mail_title'],
  182. $values['mail_text'],
  183. $values['resend_to_all'],
  184. $values['send_mail'],
  185. $values['remindUnAnswered']
  186. );
  187. // Updating the invited field in the survey table
  188. SurveyUtil::update_count_invited($survey_data['code']);
  189. $total_count = $count_course_users + $counter_additional_users;
  190. $total_invited = count(SurveyUtil::get_invitations($survey_data['code']));
  191. if ($total_invited > 0) {
  192. $message = '<a href="survey_invitation.php?view=answered&amp;survey_id='.$survey_data['survey_id'].'">'.$survey_data['answered'].'</a> ';
  193. $message .= get_lang('HaveAnswered').' ';
  194. $message .= '<a href="survey_invitation.php?view=invited&amp;survey_id='.$survey_data['survey_id'].'">'.$total_invited.'</a> ';
  195. $message .= get_lang('WereInvited');
  196. Display::display_normal_message($message, false);
  197. Display::display_confirmation_message($total_count.' '.get_lang('InvitationsSend'));
  198. }
  199. } else {
  200. // Getting the invited users
  201. $defaults = SurveyUtil::get_invited_users($survey_data['code']);
  202. // Getting the survey mail text
  203. if (!empty($survey_data['reminder_mail'])) {
  204. $defaults['mail_text'] = $survey_data['reminder_mail'];
  205. } else {
  206. $defaults['mail_text'] = $survey_data['invite_mail'];
  207. }
  208. $defaults['mail_title'] = $survey_data['mail_subject'];
  209. $defaults['send_mail'] = 1;
  210. $defaults['course_users'] = array_keys(
  211. UserManager::transform_user_group_array($defaults['course_users'], $defaults['course_groups'], true, true)
  212. );
  213. $form->setDefaults($defaults);
  214. $form->display();
  215. }
  216. Display :: display_footer();