survey_invite.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. /*
  3. DOKEOS - elearning and course management software
  4. For a full list of contributors, see documentation/credits.html
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. See "documentation/licence.html" more details.
  10. Contact:
  11. Dokeos
  12. Rue des Palais 44 Paleizenstraat
  13. B-1030 Brussels - Belgium
  14. Tel. +32 (2) 211 34 56
  15. */
  16. /**
  17. * @package dokeos.survey
  18. * @author unknown, the initial survey that did not make it in 1.8 because of bad code
  19. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University: cleanup, refactoring and rewriting large parts of the code
  20. * @version $Id: survey_invite.php 10680 2007-01-11 21:26:23Z pcool $
  21. *
  22. * @todo checking if the additional emails are valid (or add a rule for this)
  23. * @todo check if the mailtext contains the **link** part, if not, add the link to the end
  24. * @todo add rules: title and text cannot be empty
  25. */
  26. // name of the language file that needs to be included
  27. $language_file = 'survey';
  28. // including the global dokeos file
  29. require ('../inc/global.inc.php');
  30. // including additional libraries
  31. //require_once (api_get_path(LIBRARY_PATH)."/survey.lib.php");
  32. require_once('survey.lib.php');
  33. require_once (api_get_path(LIBRARY_PATH)."course.lib.php");
  34. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  35. require_once (api_get_path(LIBRARY_PATH)."mail.lib.inc.php");
  36. /** @todo this has to be moved to a more appropriate place (after the display_header of the code)*/
  37. if (!api_is_allowed_to_edit())
  38. {
  39. Display :: display_header();
  40. Display :: display_error_message(get_lang('NotAllowed'), false);
  41. Display :: display_footer();
  42. exit;
  43. }
  44. // Database table definitions
  45. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  46. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  47. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  48. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  49. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  50. $user_info = Database :: get_main_table(TABLE_MAIN_SURVEY_REMINDER);
  51. // getting the survey information
  52. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  53. $urlname = substr(strip_tags($survey_data['title']), 0, 40);
  54. if (strlen(strip_tags($survey_data['title'])) > 40)
  55. {
  56. $urlname .= '...';
  57. }
  58. // breadcrumbs
  59. $interbreadcrumb[] = array ('url' => 'survey_list.php', 'name' => get_lang('SurveyList'));
  60. $interbreadcrumb[] = array ('url' => 'survey.php?survey_id='.$_GET['survey_id'], 'name' => $urlname);
  61. $tool_name = get_lang('SurveyPublication');
  62. // Displaying the header
  63. Display::display_header($tool_name);
  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 code='".Database::escape_string($survey_data['code'])."'";
  67. $result = api_sql_query($sql, __FILE__, __LINE__);
  68. if (mysql_num_rows($result) > 1)
  69. {
  70. Display::display_warning_message(get_lang('IdenticalSurveycodeWarning'));
  71. }
  72. // invited / answered message
  73. if ($survey_data['invited'] > 0)
  74. {
  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='.$_GET['survey_id']);
  83. // Course users
  84. $complete_user_list = CourseManager :: get_user_list_from_course_code($_course['id'], true, $_SESSION['id_session']);
  85. $possible_users = array ();
  86. foreach ($complete_user_list as $index => $user)
  87. {
  88. $possible_users[$user['user_id']] = $user['lastname'].' '.$user['firstname'];
  89. }
  90. $users = $form->addElement('advmultiselect', 'course_users', get_lang('CourseUsers'), $possible_users, 'style="width: 250px; height: 200px;"');
  91. $users->setElementTemplate('
  92. {javascript}
  93. <table{class}>
  94. <!-- BEGIN label_2 --><tr><th>{label_2}</th><!-- END label_2 -->
  95. <!-- BEGIN label_3 --><th>&nbsp;</th><th>{label_3}</th></tr><!-- END label_3 -->
  96. <tr>
  97. <td valign="top">{unselected}</td>
  98. <td align="center">{add}<br /><br />{remove}</td>
  99. <td valign="top">{selected}</td>
  100. </tr>
  101. </table>
  102. ');
  103. // additional users
  104. $form->addElement('textarea', 'additional_users', get_lang('AdditonalUsers'), array ('cols' => 50, 'rows' => 2));
  105. // additional users comment
  106. $form->addElement('static', null, null, get_lang('AdditonalUsersComment'));
  107. // the title of the mail
  108. $form->addElement('text', 'mail_title', get_lang('MailTitle'),array('size' => '80'));
  109. // the text of the mail
  110. $fck_attribute['Width'] = '100%';
  111. $fck_attribute['Height'] = '150';
  112. $fck_attribute['ToolbarSet'] = 'Survey';
  113. $form->addElement('html_editor', 'mail_text', get_lang('MailText'));
  114. // some explanation of the mail
  115. $form->addElement('static', null, null, get_lang('UseLinkSyntax'));
  116. // allow resending to all selected users
  117. $form->addElement('checkbox', 'resend_to_all', '', get_lang('ReminderResendToAllUsers'));
  118. // submit button
  119. $form->addElement('submit', 'submit', get_lang('Ok'));
  120. // The rules (required fields)
  121. $form->addRule('mail_title', get_lang('ThisFieldIsRequired'), 'required');
  122. $form->addRule('mail_text', get_lang('ThisFieldIsRequired'), 'required');
  123. if ($form->validate())
  124. {
  125. $values = $form->exportValues();
  126. // save the invitation mail
  127. save_invite_mail($values['mail_text'], $values['resend_to_all']);
  128. // saving the invitations for the course users
  129. $count_course_users = save_invitations($values['course_users'], $values['mail_title'], $values['mail_text'], $values['resend_to_all']);
  130. // saving the invitations for the additional users
  131. $values['additional_users'] = $values['additional_users'].';'; // this is for the case when you enter only one email
  132. $temp = str_replace(',',';',$values['additional_users']); // this is to allow , and ; as email separators
  133. $additional_users = explode(';',$temp);
  134. for($i=0; $i<count($additional_users); $i++)
  135. {
  136. $additional_users[$i] = trim($additional_users[$i]);
  137. }
  138. $counter_additional_users = save_invitations($additional_users, $values['mail_title'], $values['mail_text'], $values['resend_to_all']);
  139. // updating the invited field in the survey table
  140. update_count_invited($survey_data['code']);
  141. $total_count = $count_course_users + $counter_additional_users;
  142. Display :: display_confirmation_message($total_count.' '.get_lang('InvitationsSend'), false);
  143. }
  144. else
  145. {
  146. // getting the invited users
  147. $defaults = get_invited_users($survey_data['code']);
  148. // getting the survey mail text
  149. if (!empty($survey_data['reminder_mail']))
  150. {
  151. $defaults['mail_text'] = $survey_data['reminder_mail'];
  152. }
  153. else
  154. {
  155. $defaults['mail_text'] = $survey_data['invite_mail'];
  156. }
  157. $form->setDefaults($defaults);
  158. $form->display();
  159. }
  160. // Footer
  161. Display :: display_footer();
  162. /**
  163. * Save the invitation mail
  164. *
  165. * @param unknown_type $mailtext
  166. *
  167. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  168. * @version January 2007
  169. */
  170. function save_invite_mail($mailtext, $reminder=0)
  171. {
  172. // Database table definition
  173. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  174. // reminder or not
  175. if ($reminder == 0)
  176. {
  177. $mail_field = 'invite_mail';
  178. }
  179. else
  180. {
  181. $mail_field = 'reminder_mail';
  182. }
  183. $sql = "UPDATE $table_survey SET $mail_field = '".Database::escape_string($mailtext)."' WHERE survey_id = '".$_GET['survey_id']."'";
  184. $result = api_sql_query($sql, __FILE__, __LINE__);
  185. }
  186. /**
  187. * This function saves all the invitations of course users and additional users in the
  188. *
  189. * @param unknown_type $users_array
  190. * @param string $invitation_title the title of the invitation is used as the title of the mail
  191. * @param string $invitation_text the text of the invitation is used as the text of the mail.
  192. * The $invitation_text has to contain a **link** string or this will automatically be added to the end
  193. *
  194. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  195. * @version January 2007
  196. *
  197. * @todo create the survey link
  198. */
  199. function save_invitations($users_array, $invitation_title, $invitation_text, $reminder=0)
  200. {
  201. global $_user;
  202. global $_course;
  203. global $_configuration;
  204. // getting the survey information
  205. $survey_data = survey_manager::get_survey($_GET['survey_id']);
  206. // Database table definition
  207. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  208. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  209. // get the people who are already invited
  210. if ($reminder == 1)
  211. {
  212. $already_invited = array();
  213. $survey_invitations = get_invitations($survey_data['survey_code']);
  214. }
  215. else
  216. {
  217. $already_invited = get_invited_users($survey_data['code']);
  218. }
  219. $counter = 0;
  220. if (is_array($users_array))
  221. {
  222. foreach ($users_array as $key=>$value)
  223. {
  224. // generating the unique code
  225. if ($reminder == 1 AND key_exists($value,$survey_invitations))
  226. {
  227. $invitation_code = $survey_invitations[$value]['invitation_code'];
  228. }
  229. else
  230. {
  231. $invitation_code = md5($value.microtime());
  232. }
  233. $survey_link = '';
  234. $full_invitation_text= '';
  235. // storing the invitation (only if the user_id is not in $already_invited['course_users'] OR email is not in $already_invited['additional_users']
  236. if ((is_numeric($value) AND !in_array($value,$already_invited['course_users'])) OR (!is_numeric($value) AND !strstr($already_invited['additional_users'], $value)) AND !empty($value))
  237. {
  238. if (!key_exists($value,$survey_invitations))
  239. {
  240. $sql = "INSERT INTO $table_survey_invitation (user, survey_code, invitation_code, invitation_date) VALUES
  241. ('".Database::escape_string($value)."','".Database::escape_string($survey_data['code'])."','".Database::escape_string($invitation_code)."','".Database::escape_string(date('Y-m-d H:i:s'))."')";
  242. $result = api_sql_query($sql, __FILE__, __LINE__);
  243. }
  244. // replacing the **link** part with a valid link for the user
  245. $survey_link = $_configuration['root_web'].$_configuration['code_append'].'survey/'.'fillsurvey.php?course='.$_course['sysCode'].'&invitationcode='.$invitation_code;
  246. $text_link = '<a href="'.$survey_link.'">'.get_lang('ClickHereToAnswerTheSurvey')."</a><br />\r\n<br />\r\n".get_lang('OrCopyPasteTheFollowingUrl')." <br />\r\n ".$survey_link;
  247. $full_invitation_text = str_ireplace('**link**', $text_link ,$invitation_text, $replace_count);
  248. if ($replace_count < 1)
  249. {
  250. $full_invitation_text = $full_invitation_text . "<br />\r\n<br />\r\n".$text_link;
  251. }
  252. // optionally: finding the e-mail of the course user
  253. if (is_numeric($value))
  254. {
  255. $sql = "SELECT firstname, lastname, email FROM $table_user WHERE user_id='".Database::escape_string($value)."'";
  256. $result = api_sql_query($sql, __FILE__, __LINE__);
  257. $row = mysql_fetch_assoc($result);
  258. $recipient_email = $row['email'];
  259. $recipient_name = $row['firstname'].' '.$row['lastname'];
  260. }
  261. else
  262. {
  263. /** @todo check if the $value is a valid email */
  264. $recipient_email = $value;
  265. }
  266. // sending the mail
  267. $sender_name = $_user['firstName'].' '.$_user['lastName'];
  268. $sender_email = $_user['mail'];
  269. api_mail_html($recipient_name, $recipient_email, $invitation_title, $full_invitation_text, $sender_name, $sender_email, '');
  270. //mail($recipient_email, strip_tags($invitation_title), strip_tags($invitation_text));
  271. $counter++;
  272. }
  273. }
  274. }
  275. return $counter;
  276. }
  277. /**
  278. * This function recalculates the number of users who have been invited and updates the survey table with this value.
  279. *
  280. * @param unknown_type $survey_id
  281. *
  282. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  283. * @version January 2007
  284. */
  285. function update_count_invited($survey_code)
  286. {
  287. // Database table definition
  288. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  289. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  290. // counting the number of people that are invited
  291. $sql = "SELECT count(user) as total FROM $table_survey_invitation WHERE survey_code = '".Database::escape_string($survey_code)."'";
  292. $result = api_sql_query($sql, __FILE__, __LINE__);
  293. $row = mysql_fetch_assoc($result);
  294. $total_invited = $row['total'];
  295. // updating the field in the survey table
  296. $sql = "UPDATE $table_survey SET invited = '".Database::escape_string($total_invited)."' WHERE code = '".Database::escape_string($survey_code)."'";
  297. $result = api_sql_query($sql, __FILE__, __LINE__);
  298. }
  299. /**
  300. * This function gets all the invited users for a given survey code.
  301. *
  302. * @param unknown_type $survey_id
  303. * @return array containing the course users and additional users (non course users)
  304. *
  305. * @todo consider making $defaults['additional_users'] also an array
  306. *
  307. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  308. * @version January 2007
  309. */
  310. function get_invited_users($survey_code)
  311. {
  312. // Database table definition
  313. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  314. // Selecting all the invitations of this survey
  315. $sql = "SELECT user FROM $table_survey_invitation WHERE survey_code='".Database::escape_string($survey_code)."'";
  316. $result = api_sql_query($sql, __FILE__, __LINE__);
  317. while ($row = mysql_fetch_assoc($result))
  318. {
  319. if (is_numeric($row['user']))
  320. {
  321. $defaults['course_users'][] = $row['user'];
  322. }
  323. else
  324. {
  325. if (empty($defaults['additional_users']))
  326. {
  327. $defaults['additional_users'] = $row['user'];
  328. }
  329. else
  330. {
  331. $defaults['additional_users'] .= ';'.$row['user'];
  332. }
  333. }
  334. }
  335. return $defaults;
  336. }
  337. /**
  338. * get all the invitations
  339. *
  340. * @param unknown_type $survey_code
  341. * @return array
  342. *
  343. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  344. * @version September 2007
  345. */
  346. function get_invitations($survey_code)
  347. {
  348. // Database table definition
  349. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  350. $sql = "SELECT * FROM $table_survey_invitation WHERE survey_code = '".Database::escape_string($survey_code)."'";
  351. $result = api_sql_query($sql, __FILE__, __LINE__);
  352. while ($row = mysql_fetch_assoc($result))
  353. {
  354. $return[$row['user']] = $row;
  355. }
  356. return $return;
  357. }
  358. ?>