survey_invite.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 '../inc/global.inc.php';
  18. // Including additional libraries
  19. require_once 'survey.lib.php';
  20. require_once api_get_path(LIBRARY_PATH).'course.lib.php';
  21. require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
  22. require_once api_get_path(LIBRARY_PATH).'mail.lib.inc.php';
  23. $this_section = SECTION_COURSES;
  24. if (!api_is_allowed_to_edit(false, true)) {
  25. Display :: display_header(get_lang('ToolSurvey'));
  26. Display :: display_error_message(get_lang('NotAllowed'), false);
  27. Display :: display_footer();
  28. exit;
  29. }
  30. // Database table definitions
  31. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  32. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  33. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  34. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  35. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  36. $user_info = Database::get_main_table(TABLE_MAIN_SURVEY_REMINDER); // TODO: To be checked. TABLE_MAIN_SURVEY_REMINDER has not been defined.
  37. // Getting the survey information
  38. $survey_id = Security::remove_XSS($_GET['survey_id']);
  39. $survey_data = survey_manager::get_survey($survey_id);
  40. if (empty($survey_data)) {
  41. Display :: display_header(get_lang('ToolSurvey'));
  42. Display :: display_error_message(get_lang('InvallidSurvey'), false);
  43. Display :: display_footer();
  44. exit;
  45. }
  46. $urlname = strip_tags(api_substr(api_html_entity_decode($survey_data['title'], ENT_QUOTES), 0, 40));
  47. if (api_strlen(strip_tags($survey_data['title'])) > 40) {
  48. $urlname .= '...';
  49. }
  50. // Breadcrumbs
  51. $interbreadcrumb[] = array('url' => 'survey_list.php', 'name' => get_lang('SurveyList'));
  52. if (api_is_course_admin()) {
  53. $interbreadcrumb[] = array('url' => 'survey.php?survey_id='.$survey_id, 'name' => $urlname);
  54. } else {
  55. $interbreadcrumb[] = array('url' => 'survey_invite.php?survey_id='.$survey_id, 'name' => $urlname);
  56. }
  57. $tool_name = get_lang('SurveyPublication');
  58. // Displaying the header
  59. Display::display_header($tool_name,'Survey');
  60. // Checking if there is another survey with this code.
  61. // If this is the case there will be a language choice
  62. $sql = "SELECT * FROM $table_survey WHERE code='".Database::escape_string($survey_data['code'])."'";
  63. $result = Database::query($sql);
  64. if (Database::num_rows($result) > 1) {
  65. Display::display_warning_message(get_lang('IdenticalSurveycodeWarning'));
  66. }
  67. // Invited / answered message
  68. if ($survey_data['invited'] > 0) {
  69. $message = '<a href="survey_invitation.php?view=answered&amp;survey_id='.$survey_data['survey_id'].'">'.$survey_data['answered'].'</a> ';
  70. $message .= get_lang('HaveAnswered').' ';
  71. $message .= '<a href="survey_invitation.php?view=invited&amp;survey_id='.$survey_data['survey_id'].'">'.$survey_data['invited'].'</a> ';
  72. $message .= get_lang('WereInvited');
  73. Display::display_normal_message($message, false);
  74. }
  75. // Building the form for publishing the survey
  76. $form = new FormValidator('publish_form', 'post', api_get_self().'?survey_id='.$survey_id);
  77. $form->addElement('header', '', $tool_name);
  78. // Course users
  79. $complete_user_list = CourseManager :: get_user_list_from_course_code($_course['id'], true, $_SESSION['id_session'], '', api_sort_by_first_name() ? 'ORDER BY firstname' : 'ORDER BY lastname');
  80. $possible_users = array();
  81. foreach ($complete_user_list as $index => & $user) {
  82. $possible_users[$user['user_id']] = api_get_person_name($user['firstname'], $user['lastname']);
  83. }
  84. $users = $form->addElement('advmultiselect', 'course_users', get_lang('CourseUsers'), $possible_users, 'style="width: 250px; height: 200px;"');
  85. $users->setElementTemplate('
  86. {javascript}
  87. <table{class}>
  88. <!-- BEGIN label_2 --><tr><th>{label_2}</th><!-- END label_2 -->
  89. <!-- BEGIN label_3 --><th>&nbsp;</th><th>{label_3}</th></tr><!-- END label_3 -->
  90. <tr>
  91. <td valign="top">{unselected}</td>
  92. <td align="center">{add}<br /><br />{remove}</td>
  93. <td valign="top">{selected}</td>
  94. </tr>
  95. </table>
  96. ');
  97. $users->setButtonAttributes('add', array('class' => 'arrowr'));
  98. $users->setButtonAttributes('remove', array('class' => 'arrowl'));
  99. // Additional users
  100. $form->addElement('textarea', 'additional_users', get_lang('AdditonalUsers'), array('cols' => 50, 'rows' => 2));
  101. // Additional users comment
  102. $form->addElement('static', null, null, get_lang('AdditonalUsersComment'));
  103. // The title of the mail
  104. $form->addElement('text', 'mail_title', get_lang('MailTitle'), array('size' => '80'));
  105. // The text of the mail
  106. $form->addElement('html_editor', 'mail_text', get_lang('MailText'), null, array('ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '150'));
  107. // Some explanation of the mail
  108. $form->addElement('static', null, null, get_lang('UseLinkSyntax'));
  109. $form->addElement('checkbox', 'send_mail', '', get_lang('SendMail'));
  110. // You cab send a reminder to unanswered people if the survey is not anonymous
  111. if ($survey_data['anonymous'] != 1) {
  112. $form->addElement('checkbox', 'remindUnAnswered', '', get_lang('RemindUnanswered'));
  113. }
  114. // Allow resending to all selected users
  115. $form->addElement('checkbox', 'resend_to_all', '', get_lang('ReminderResendToAllUsers'));
  116. // Submit button
  117. $form->addElement('style_submit_button', 'submit', get_lang('PublishSurvey'), 'class="save"');
  118. // The rules (required fields)
  119. $form->addRule('mail_title', get_lang('ThisFieldIsRequired'), 'required');
  120. $form->addRule('mail_text', get_lang('ThisFieldIsRequired'), 'required');
  121. $portal_url = $_configuration['root_web'];
  122. if ($_configuration['multiple_access_urls']) {
  123. $access_url_id = api_get_current_access_url_id();
  124. if ($access_url_id != -1) {
  125. $url = api_get_access_url($access_url_id);
  126. $portal_url = $url['url'];
  127. }
  128. }
  129. // Show the URL that can be used by users to fill a survey without invitation
  130. $auto_survey_link = $portal_url.$_configuration['code_append'].
  131. 'survey/'.'fillsurvey.php?course='.$_course['sysCode'].
  132. '&invitationcode=auto&scode='.$survey_data['survey_code'];
  133. $form->addElement('static',null, null, '<br \><br \>' . get_lang('AutoInviteLink'));
  134. $form->addElement('static',null, null, $auto_survey_link);
  135. if ($form->validate()) {
  136. $values = $form->exportValues();
  137. // Save the invitation mail
  138. SurveyUtil::save_invite_mail($values['mail_text'], $values['mail_title'], !empty($survey_data['invite_mail']));
  139. // Saving the invitations for the course users
  140. $count_course_users = SurveyUtil::save_invitations($values['course_users'], $values['mail_title'],
  141. $values['mail_text'], $values['resend_to_all'], $values['send_mail'], $values['remindUnAnswered']);
  142. // Saving the invitations for the additional users
  143. $values['additional_users'] = $values['additional_users'].';'; // This is for the case when you enter only one email
  144. $temp = str_replace(',', ';', $values['additional_users']); // This is to allow , and ; as email separators
  145. $additional_users = explode(';', $temp);
  146. for ($i = 0; $i < count($additional_users); $i++) {
  147. $additional_users[$i] = trim($additional_users[$i]);
  148. }
  149. $counter_additional_users = SurveyUtil::save_invitations($additional_users, $values['mail_title'],
  150. $values['mail_text'], $values['resend_to_all'], $values['send_mail'], $values['remindUnAnswered']);
  151. // Updating the invited field in the survey table
  152. SurveyUtil::update_count_invited($survey_data['code']);
  153. $total_count = $count_course_users + $counter_additional_users;
  154. Display :: display_confirmation_message($total_count.' '.get_lang('InvitationsSend'));
  155. } else {
  156. // Getting the invited users
  157. $defaults = SurveyUtil::get_invited_users($survey_data['code']);
  158. // Getting the survey mail text
  159. if (!empty($survey_data['reminder_mail'])) {
  160. $defaults['mail_text'] = $survey_data['reminder_mail'];
  161. } else {
  162. $defaults['mail_text'] = $survey_data['invite_mail'];
  163. }
  164. $defaults['mail_title'] = $survey_data['mail_subject'];
  165. $defaults['send_mail'] = 1;
  166. $form->setDefaults($defaults);
  167. $form->display();
  168. }
  169. // Footer
  170. Display :: display_footer();