announcements.inc.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. <?php //$Id: announcements.inc.php 14017 2007-12-18 19:17:58Z yannoo $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2005 Dokeos S.A.
  6. Copyright (c) various contributors
  7. For a full list of contributors, see "credits.txt".
  8. The full license can be read in "license.txt".
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. See the GNU General Public License for more details.
  14. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  15. Mail: info@dokeos.com
  16. ==============================================================================
  17. */
  18. /**
  19. ==============================================================================
  20. * Include file with functions for the announcements module.
  21. * @package dokeos.announcements
  22. ==============================================================================
  23. */
  24. /*
  25. ==============================================================================
  26. DISPLAY FUNCTIONS
  27. ==============================================================================
  28. */
  29. /**
  30. * displays one specific announcement
  31. * @param $announcement_id, the id of the announcement you want to display
  32. * @todo remove globals
  33. * @todo more security checking
  34. */
  35. function display_announcement($announcement_id)
  36. {
  37. global $_user, $dateFormatLong;
  38. $tbl_announcement = Database::get_course_table('announcement');
  39. $tbl_item_property = Database::get_course_table('item_property');
  40. if ($_user['user_id'])
  41. {
  42. $sql_query = " SELECT announcement.*, toolitemproperties.*
  43. FROM $tbl_announcement announcement, $tbl_item_property toolitemproperties
  44. WHERE announcement.id = toolitemproperties.ref
  45. AND announcement.id = '$announcement_id'
  46. AND toolitemproperties.tool='announcement'
  47. AND (toolitemproperties.to_user_id='".$_user['user_id']."' OR toolitemproperties.to_group_id='0')
  48. AND toolitemproperties.visibility='1'
  49. ORDER BY display_order DESC";
  50. }
  51. else
  52. {
  53. $sql_query = " SELECT announcement.*, toolitemproperties.*
  54. FROM $tbl_announcement announcement, $tbl_item_property toolitemproperties
  55. WHERE announcement.id = toolitemproperties.ref
  56. AND announcement.id = '$announcement_id'
  57. AND toolitemproperties.tool='announcement'
  58. AND toolitemproperties.to_group_id='0'
  59. AND toolitemproperties.visibility='1'";
  60. }
  61. $sql_result = api_sql_query($sql_query,__FILE__,__LINE__);
  62. $result = mysql_fetch_array($sql_result);
  63. $title = $result['title'];
  64. $content = $result['content'];
  65. $content = make_clickable($content);
  66. $content = text_filter($content);
  67. $last_post_datetime = $result['insert_date'];// post time format datetime de mysql
  68. list($last_post_date, $last_post_time) = split(" ", $last_post_datetime);
  69. echo "<table height=\"100\" width=\"100%\" border=\"1\" cellpadding=\"5\" cellspacing=\"0\" id=\"agenda_list\">\n";
  70. echo "<tr class=\"data\"><td>" . $title . "</td></tr>\n";
  71. echo "<tr><td class=\"announcements_datum\">" . get_lang('AnnouncementPublishedOn') . " : " . ucfirst(format_locale_date($dateFormatLong,strtotime($last_post_date) ) ) . "</td></tr>\n";
  72. echo "<tr class=\"text\"><td>$content</td></tr>\n";
  73. echo "</table>";
  74. }
  75. /*======================================
  76. SHOW_TO_FORM
  77. ======================================*/
  78. /**
  79. * this function shows the form for sending a message to a specific group or user.
  80. */
  81. function show_to_form($to_already_selected)
  82. {
  83. $user_list=get_course_users();
  84. $group_list=get_course_groups();
  85. echo "\n<table id=\"recipient_list\" style=\"display: none;\">\n";
  86. echo "\t<tr>\n";
  87. // the form containing all the groups and all the users of the course
  88. echo "\t\t<td>\n";
  89. construct_not_selected_select_form($group_list,$user_list,$to_already_selected);
  90. echo "\t\t</td>\n";
  91. // the buttons for adding or removing groups/users
  92. echo "\n\t\t<td valign=\"middle\">\n";
  93. echo "\t\t<input type=\"button\" ",
  94. "onClick=\"move(this.form.elements[0],this.form.elements[3])\" ",// 7 & 4 : fonts
  95. "value=\" >> \">",
  96. "\n\t\t<p>&nbsp;</p>",
  97. "\n\t\t<input type=\"button\"",
  98. "onClick=\"move(this.form.elements[3],this.form.elements[0])\" ",
  99. "value=\" << \">";
  100. echo "\t\t</td>\n";
  101. echo "\n\t\t<td>\n";
  102. // the form containing the selected groups and users
  103. construct_selected_select_form($group_list,$user_list,$to_already_selected);
  104. echo "\t\t</td>\n";
  105. echo "\t</tr>\n";
  106. echo "</table>";
  107. }
  108. /*===========================================
  109. CONSTRUCT_NOT_SELECT_SELECT_FORM
  110. ===========================================*/
  111. /**
  112. * this function shows the form for sending a message to a specific group or user.
  113. */
  114. function construct_not_selected_select_form($group_list=null, $user_list=null,$to_already_selected)
  115. {
  116. echo "\t\t<select name=\"not_selected_form[]\" size=5 style=\"width:200px\" multiple>\n";
  117. // adding the groups to the select form
  118. if ($group_list)
  119. {
  120. foreach($group_list as $this_group)
  121. {
  122. if (!in_array("GROUP:".$this_group['id'],$to_already_selected)) // $to_already_selected is the array containing the groups (and users) that are already selected
  123. {
  124. echo "\t\t<option value=\"GROUP:".$this_group['id']."\">",
  125. "G: ",$this_group['name']," - " . $this_group['userNb'] . " " . get_lang('Users') .
  126. "</option>\n";
  127. }
  128. }
  129. // a divider
  130. echo "\t\t<option value=\"\">---------------------------------------------------------</option>\n";
  131. }
  132. // adding the individual users to the select form
  133. foreach($user_list as $this_user)
  134. {
  135. if (!in_array("USER:".$this_user["user_id"],$to_already_selected)) // $to_already_selected is the array containing the users (and groups) that are already selected
  136. {
  137. echo "\t\t<option value=\"USER:",$this_user["user_id"],"\">",
  138. "",$this_user['lastName']," ",$this_user['firstName'],
  139. "</option>\n";
  140. }
  141. }
  142. echo "\t\t</select>\n";
  143. }
  144. /*==========================================
  145. CONSTRUCT_SELECTED_SELECT_FORM
  146. ==========================================*/
  147. /**
  148. * this function shows the form for sending a message to a specific group or user.
  149. */
  150. function construct_selected_select_form($group_list=null, $user_list=null,$to_already_selected)
  151. {
  152. // we separate the $to_already_selected array (containing groups AND users into
  153. // two separate arrays
  154. $groupuser = array();
  155. if (is_array($to_already_selected))
  156. {
  157. $groupuser=separate_users_groups($to_already_selected);
  158. }
  159. $groups_to_already_selected=$groupuser['groups'];
  160. $users_to_already_selected=$groupuser['users'];
  161. // we load all the groups and all the users into a reference array that we use to search the name of the group / user
  162. $ref_array_groups=get_course_groups();
  163. $ref_array_users=get_course_users();
  164. // we construct the form of the already selected groups / users
  165. echo "\t\t<select name=\"selectedform[]\" size=\"5\" multiple style=\"width:200px\" width=\"200px\">";
  166. foreach($to_already_selected as $groupuser)
  167. {
  168. list($type,$id)=explode(":",$groupuser);
  169. if ($type=="GROUP")
  170. {
  171. echo "\t\t<option value=\"".$groupuser."\">G: ".$ref_array_groups[$id]['name']."</option>";
  172. }
  173. else
  174. {
  175. foreach($ref_array_users as $key=>$value){
  176. if($value['user_id']==$id){
  177. echo "\t\t<option value=\"".$groupuser."\">".$value['lastName']." ".$value['firstName']."</option>";
  178. break;
  179. }
  180. }
  181. }
  182. }
  183. echo "</select>\n";
  184. }
  185. /**
  186. * this function shows the form for sending a message to a specific group or user.
  187. */
  188. function show_to_form_group($group_id)
  189. {
  190. echo "\n<table id=\"recipient_list\" style=\"display: none;\">\n";
  191. echo "\t<tr>\n";
  192. echo "\t\t<td>\n";
  193. echo "\t\t<select name=\"not_selected_form[]\" size=5 style=\"width:200px\" multiple>\n";
  194. $group_users = GroupManager::get_subscribed_users($group_id);
  195. foreach($group_users as $user){
  196. echo '<option value="'.$user['user_id'].'">'.$user['lastname'].' '.$user['firstname'].'</option>';
  197. }
  198. echo '</select>';
  199. echo "\t\t</td>\n";
  200. // the buttons for adding or removing groups/users
  201. echo "\n\t\t<td valign=\"middle\">\n";
  202. echo "\t\t<input type=\"button\" ",
  203. "onClick=\"move(this.form.elements[1],this.form.elements[4])\" ",// 7 & 4 : fonts
  204. "value=\" >> \">",
  205. "\n\t\t<p>&nbsp;</p>",
  206. "\n\t\t<input type=\"button\"",
  207. "onClick=\"move(this.form.elements[4],this.form.elements[1])\" ",
  208. "value=\" << \">";
  209. echo "\t\t</td>\n";
  210. echo "\n\t\t<td>\n";
  211. echo "\t\t<select name=\"selectedform[]\" size=5 style=\"width:200px\" multiple>\n";
  212. echo '</select>';
  213. echo "\t\t</td>\n";
  214. echo "\t</tr>\n";
  215. echo "</table>";
  216. }
  217. /*
  218. ==============================================================================
  219. DATA FUNCTIONS
  220. ==============================================================================
  221. */
  222. /**
  223. * this function gets all the users of the course,
  224. * including users from linked courses
  225. */
  226. function get_course_users()
  227. {
  228. //this would return only the users from real courses:
  229. //$user_list = CourseManager::get_user_list_from_course_code(api_get_course_id());
  230. $user_list = CourseManager::get_real_and_linked_user_list(api_get_course_id(), true, $_SESSION['id_session']);
  231. return $user_list;
  232. }
  233. /**
  234. * this function gets all the groups of the course,
  235. * not including linked courses
  236. */
  237. function get_course_groups()
  238. {
  239. $new_group_list = CourseManager::get_group_list_of_course(api_get_course_id());
  240. return $new_group_list;
  241. }
  242. /*======================================
  243. LOAD_EDIT_USERS
  244. ======================================*/
  245. /**
  246. * This tools loads all the users and all the groups who have received
  247. * a specific item (in this case an announcement item)
  248. */
  249. function load_edit_users($tool, $id)
  250. {
  251. global $_course;
  252. global $tbl_item_property;
  253. $sql="SELECT * FROM $tbl_item_property WHERE tool='$tool' AND ref='$id'";
  254. $result=api_sql_query($sql,__FILE__,__LINE__) or die (mysql_error());
  255. while ($row=mysql_fetch_array($result))
  256. {
  257. $to_group=$row['to_group_id'];
  258. switch ($to_group)
  259. {
  260. // it was send to one specific user
  261. case null:
  262. $to[]="USER:".$row['to_user_id'];
  263. break;
  264. // it was sent to everyone
  265. case 0:
  266. return "everyone";
  267. exit;
  268. break;
  269. default:
  270. $to[]="GROUP:".$row['to_group_id'];
  271. }
  272. }
  273. return $to;
  274. }
  275. /*======================================
  276. USER_GROUP_FILTER_JAVASCRIPT
  277. ======================================*/
  278. /**
  279. * returns the javascript for setting a filter
  280. * this goes into the $htmlHeadXtra[] array
  281. */
  282. function user_group_filter_javascript()
  283. {
  284. return "<script language=\"JavaScript\" type=\"text/JavaScript\">
  285. <!--
  286. function jumpMenu(targ,selObj,restore)
  287. {
  288. eval(targ+\".location='\"+selObj.options[selObj.selectedIndex].value+\"'\");
  289. if (restore) selObj.selectedIndex=0;
  290. }
  291. //-->
  292. </script>
  293. ";
  294. }
  295. /*======================================
  296. TO_JAVASCRIPT
  297. ========================================*/
  298. /**
  299. * returns all the javascript that is required for easily
  300. * setting the target people/groups
  301. * this goes into the $htmlHeadXtra[] array
  302. */
  303. function to_javascript()
  304. {
  305. return "<script type=\"text/javascript\" language=\"JavaScript\">
  306. <!-- Begin javascript menu swapper
  307. function move(fbox, tbox)
  308. {
  309. var arrFbox = new Array();
  310. var arrTbox = new Array();
  311. var arrLookup = new Array();
  312. var i;
  313. for (i = 0; i < tbox.options.length; i++)
  314. {
  315. arrLookup[tbox.options[i].text] = tbox.options[i].value;
  316. arrTbox[i] = tbox.options[i].text;
  317. }
  318. var fLength = 0;
  319. var tLength = arrTbox.length;
  320. for(i = 0; i < fbox.options.length; i++)
  321. {
  322. arrLookup[fbox.options[i].text] = fbox.options[i].value;
  323. if (fbox.options[i].selected && fbox.options[i].value != \"\")
  324. {
  325. arrTbox[tLength] = fbox.options[i].text;
  326. tLength++;
  327. }
  328. else
  329. {
  330. arrFbox[fLength] = fbox.options[i].text;
  331. fLength++;
  332. }
  333. }
  334. arrFbox.sort();
  335. arrTbox.sort();
  336. fbox.length = 0;
  337. tbox.length = 0;
  338. var c;
  339. for(c = 0; c < arrFbox.length; c++)
  340. {
  341. var no = new Option();
  342. no.value = arrLookup[arrFbox[c]];
  343. no.text = arrFbox[c];
  344. fbox[c] = no;
  345. }
  346. for(c = 0; c < arrTbox.length; c++)
  347. {
  348. var no = new Option();
  349. no.value = arrLookup[arrTbox[c]];
  350. no.text = arrTbox[c];
  351. tbox[c] = no;
  352. }
  353. }
  354. function validate()
  355. {
  356. var f = document.new_calendar_item;
  357. f.submit();
  358. return true;
  359. }
  360. function selectAll(cbList,bSelect,showwarning)
  361. {
  362. if (cbList.length < 1) {
  363. alert(\"".get_lang('Send2All')."\");
  364. return;
  365. }
  366. for (var i=0; i<cbList.length; i++)
  367. cbList[i].selected = cbList[i].checked = bSelect
  368. }
  369. function reverseAll(cbList)
  370. {
  371. for (var i=0; i<cbList.length; i++)
  372. {
  373. cbList[i].checked = !(cbList[i].checked)
  374. cbList[i].selected = !(cbList[i].selected)
  375. }
  376. }
  377. // End -->
  378. </script>";
  379. }
  380. /*======================================
  381. SENT_TO_FORM
  382. ======================================*/
  383. /**
  384. * constructs the form to display all the groups and users the message has been sent to
  385. * input: $sent_to_array is a 2 dimensional array containing the groups and the users
  386. * the first level is a distinction between groups and users:
  387. * $sent_to_array['groups'] * and $sent_to_array['users']
  388. * $sent_to_array['groups'] (resp. $sent_to_array['users']) is also an array
  389. * containing all the id's of the groups (resp. users) who have received this message.
  390. * @author Patrick Cool <patrick.cool@>
  391. */
  392. function sent_to_form($sent_to_array)
  393. {
  394. // we find all the names of the groups
  395. $group_names=get_course_groups();
  396. count($sent_to_array);
  397. // we count the number of users and the number of groups
  398. if (isset($sent_to_array['users']))
  399. {
  400. $number_users=count($sent_to_array['users']);
  401. }
  402. else
  403. {
  404. $number_users=0;
  405. }
  406. if (isset($sent_to_array['groups']))
  407. {
  408. $number_groups=count($sent_to_array['groups']);
  409. }
  410. else
  411. {
  412. $number_groups=0;
  413. }
  414. $total_numbers=$number_users+$number_groups;
  415. // starting the form if there is more than one user/group
  416. if ($total_numbers >1)
  417. {
  418. $output="<select name=\"sent to\">\n";
  419. $output.="<option>".get_lang("SentTo")."</option>";
  420. // outputting the name of the groups
  421. if (is_array($sent_to_array['groups']))
  422. {
  423. foreach ($sent_to_array['groups'] as $group_id)
  424. {
  425. $output.="\t<option value=\"\">G: ".$group_names[$group_id]['name']."</option>\n";
  426. }
  427. }
  428. if (isset($sent_to_array['users']))
  429. {
  430. if (is_array($sent_to_array['users']))
  431. {
  432. foreach ($sent_to_array['users'] as $user_id)
  433. {
  434. $user_info=api_get_user_info($user_id);
  435. $output.="\t<option value=\"\">".$user_info['lastName']." ".$user_info['firstName']."</option>\n";
  436. }
  437. }
  438. }
  439. // ending the form
  440. $output.="</select>\n";
  441. }
  442. else // there is only one user/group
  443. {
  444. if (is_array($sent_to_array['users']))
  445. {
  446. $user_info=api_get_user_info($sent_to_array['users'][0]);
  447. echo $user_info['lastName']." ".$user_info['firstName'];
  448. }
  449. if (is_array($sent_to_array['groups']) and $sent_to_array['groups'][0]!==0)
  450. {
  451. $group_id=$sent_to_array['groups'][0];
  452. echo $group_names[$group_id]['name'];
  453. }
  454. if (is_array($sent_to_array['groups']) and $sent_to_array['groups'][0]==0)
  455. {
  456. echo get_lang("Everybody");
  457. }
  458. }
  459. echo $output;
  460. }
  461. /*======================================
  462. SEPARATE_USERS_GROUPS
  463. ======================================*/
  464. /**
  465. * This function separates the users from the groups
  466. * users have a value USER:XXX (with XXX the dokeos id
  467. * groups have a value GROUP:YYY (with YYY the group id)
  468. */
  469. function separate_users_groups($to)
  470. {
  471. foreach($to as $to_item)
  472. {
  473. list($type, $id) = explode(':', $to_item);
  474. switch($type)
  475. {
  476. case 'GROUP':
  477. $grouplist[] =$id;
  478. break;
  479. case 'USER':
  480. $userlist[] =$id;
  481. break;
  482. }
  483. }
  484. $send_to['groups']=$grouplist;
  485. $send_to['users']=$userlist;
  486. return $send_to;
  487. }
  488. /*======================================
  489. SENT_TO()
  490. ======================================*/
  491. /**
  492. * returns all the users and all the groups a specific announcement item
  493. * has been sent to
  494. */
  495. function sent_to($tool, $id)
  496. {
  497. global $_course;
  498. global $tbl_item_property;
  499. $sql="SELECT * FROM $tbl_item_property WHERE tool='$tool' AND ref='".$id."'";
  500. $result = api_sql_query($sql,__FILE__,__LINE__);
  501. while ($row=mysql_fetch_array($result))
  502. {
  503. // if to_group_id is null then it is sent to a specific user
  504. // if to_group_id = 0 then it is sent to everybody
  505. if (!is_null($row['to_group_id']))
  506. {
  507. $sent_to_group[]=$row['to_group_id'];
  508. }
  509. // if to_user_id <> 0 then it is sent to a specific user
  510. if ($row['to_user_id'] <> 0)
  511. {
  512. $sent_to_user[]=$row['to_user_id'];
  513. }
  514. }
  515. if (isset($sent_to_group))
  516. {
  517. $sent_to['groups']=$sent_to_group;
  518. }
  519. if (isset($sent_to_user))
  520. {
  521. $sent_to['users']=$sent_to_user;
  522. }
  523. return $sent_to;
  524. }
  525. /*===================================================
  526. CHANGE_VISIBILITY($tool,$id)
  527. =================================================*/
  528. /**
  529. * This functions swithes the visibility a course resource
  530. * using the visibility field in 'item_property'
  531. * values: 0 = invisibility for
  532. */
  533. function change_visibility($tool,$id)
  534. {
  535. global $_course;
  536. global $tbl_item_property;
  537. $sql="SELECT * FROM $tbl_item_property WHERE tool='$tool' AND ref='$id'";
  538. $result=api_sql_query($sql,__FILE__,__LINE__) or die (mysql_error());
  539. $row=mysql_fetch_array($result);
  540. if ($row['visibility']=='1')
  541. {
  542. $sql_visibility="UPDATE $tbl_item_property SET visibility='0' WHERE tool='$tool' AND ref='$id'";
  543. }
  544. else
  545. {
  546. $sql_visibility="UPDATE $tbl_item_property SET visibility='1' WHERE tool='$tool' AND ref='$id'";
  547. }
  548. $result=api_sql_query($sql_visibility,__FILE__,__LINE__) or die (mysql_error());
  549. }
  550. /*====================================================
  551. STORE_ADVALVAS_ITEM
  552. ====================================================*/
  553. function store_advalvas_item($emailTitle,$newContent, $order, $to)
  554. {
  555. global $_course;
  556. global $nameTools;
  557. global $_user;
  558. global $tbl_announcement;
  559. global $tbl_item_property;
  560. // store in the table announcement
  561. $sql = "INSERT INTO $tbl_announcement SET content = '$newContent', title = '$emailTitle', end_date = NOW(), display_order ='$order'";
  562. $result = api_sql_query($sql,__FILE__,__LINE__) or die (mysql_error());
  563. $last_id= mysql_insert_id();
  564. // store in item_property (first the groups, then the users
  565. if (!is_null($to)) // !is_null($to): when no user is selected we send it to everyone
  566. {
  567. $send_to=separate_users_groups($to);
  568. // storing the selected groups
  569. if (is_array($send_to['groups']))
  570. {
  571. foreach ($send_to['groups'] as $group)
  572. {
  573. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", $_user['user_id'], $group);
  574. }
  575. }
  576. // storing the selected users
  577. if (is_array($send_to['users']))
  578. {
  579. foreach ($send_to['users'] as $user)
  580. {
  581. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", $_user['user_id'], '', $user);
  582. }
  583. }
  584. }
  585. else // the message is sent to everyone, so we set the group to 0
  586. {
  587. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", $_user['user_id'], '0');
  588. }
  589. return $last_id;
  590. }
  591. function store_advalvas_group_item($emailTitle,$newContent, $order, $to, $to_users)
  592. {
  593. global $_course;
  594. global $nameTools;
  595. global $_user;
  596. global $tbl_announcement;
  597. global $tbl_item_property;
  598. // store in the table announcement
  599. $sql = "INSERT INTO $tbl_announcement SET content = '$newContent', title = '$emailTitle', end_date = NOW(), display_order ='$order'";
  600. $result = api_sql_query($sql,__FILE__,__LINE__) or die (mysql_error());
  601. $last_id= mysql_insert_id();
  602. // store in item_property (first the groups, then the users
  603. if (!isset($to_users)) // !isset($to): when no user is selected we send it to everyone
  604. {
  605. $send_to=separate_users_groups($to);
  606. // storing the selected groups
  607. if (is_array($send_to['groups']))
  608. {
  609. foreach ($send_to['groups'] as $group)
  610. {
  611. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", $_user['user_id'], $group);
  612. }
  613. }
  614. }
  615. else // the message is sent to everyone, so we set the group to 0
  616. {
  617. // storing the selected users
  618. if (is_array($to_users))
  619. {
  620. foreach ($to_users as $user)
  621. {
  622. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", $_user['user_id'], '', $user);
  623. }
  624. }
  625. }
  626. return $last_id;
  627. }
  628. /*==================================================
  629. EDIT_VALVAS_ITEM
  630. ==================================================*/
  631. /**
  632. * This function stores the announcement Item in the table announcement
  633. * and updates the item_property also
  634. */
  635. function edit_advalvas_item($id,$emailTitle,$newContent,$to)
  636. {
  637. global $_course;
  638. global $nameTools;
  639. global $_user;
  640. global $tbl_announcement;
  641. global $tbl_item_property;
  642. // store the modifications in the table announcement
  643. $sql = "UPDATE $tbl_announcement SET content='$newContent', title = '$emailTitle' WHERE id='$id'";
  644. $result = api_sql_query($sql,__FILE__,__LINE__) or die (mysql_error());
  645. // we remove everything from item_property for this
  646. $sql_delete="DELETE FROM $tbl_item_property WHERE ref='$id' AND tool='announcement'";
  647. $result = api_sql_query($sql_delete,__FILE__,__LINE__) or die (mysql_error());
  648. // store in item_property (first the groups, then the users
  649. if (!is_null($to)) // !is_null($to): when no user is selected we send it to everyone
  650. {
  651. $send_to=separate_users_groups($to);
  652. // storing the selected groups
  653. if (is_array($send_to['groups']))
  654. {
  655. foreach ($send_to['groups'] as $group)
  656. {
  657. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, "AnnouncementUpdated", $_user['user_id'], $group);
  658. }
  659. }
  660. // storing the selected users
  661. if (is_array($send_to['users']))
  662. {
  663. foreach ($send_to['users'] as $user)
  664. {
  665. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, "AnnouncementUpdated", $_user['user_id'], '', $user);
  666. }
  667. }
  668. }
  669. else // the message is sent to everyone, so we set the group to 0
  670. {
  671. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, "AnnouncementUpdated", $_user['user_id'], '0');
  672. }
  673. }
  674. /*
  675. ==============================================================================
  676. MAIL FUNCTIONS
  677. ==============================================================================
  678. */
  679. /**
  680. * Sends an announcement by email to a list of users.
  681. * Emails are sent one by one to try to avoid antispam.
  682. */
  683. function send_announcement_email($user_list, $course_code, $_course, $mail_title, $mail_content)
  684. {
  685. global $_user;
  686. foreach ($user_list as $this_user)
  687. {
  688. /* Header : Bericht van uw lesgever - GES ($course_code) - Morgen geen les! ($mail_title)
  689. Body : John Doe (prenom + nom) <john_doe@hotmail.com> (email)
  690. Morgen geen les! ($mail_title)
  691. Morgen is er geen les, de les wordt geschrapt wegens vergadering (newContent)
  692. */
  693. $mail_subject = get_lang('professorMessage').' - '.$_course['official_code'].' - '.$mail_title;
  694. $mail_body = '['.$_course['official_code'].'] - ['.$_course['name']."]\n";
  695. $mail_body .= $this_user['lastname'].' '.$this_user['firstname'].' <'.$this_user["email"]."> \n\n".stripslashes($mail_title)."\n\n".trim(stripslashes(html_entity_decode(strip_tags(str_replace(array('<p>','</p>','<br />'),array('',"\n","\n"),$mail_content)))))." \n\n-- \n";
  696. $mail_body .= $_user['firstName'].' '.$_user['lastName'].' ';
  697. $mail_body .= '<'.$_user['mail'].">\n";
  698. $mail_body .= $_course['official_code'].' '.$_course['name'];
  699. //set the charset and use it for the encoding of the email - small fix, not really clean (should check the content encoding origin first)
  700. //here we use the encoding used for the webpage where the text is encoded (ISO-8859-1 in this case)
  701. if(empty($charset)){$charset='ISO-8859-1';}
  702. $encoding = 'Content-Type: text/plain; charset='. $charset;
  703. $newmail = api_mail($this_user['lastname'].' '.$this_user['firstname'], $this_user['email'], $mail_subject, $mail_body, $_SESSION['_user']['lastName'].' '.$_SESSION['_user']['firstName'], $_SESSION['_user']['mail'], $encoding);
  704. }
  705. }
  706. function update_mail_sent($insert_id)
  707. {
  708. global $_course;
  709. global $tbl_announcement;
  710. // store the modifications in the table tbl_annoucement
  711. $sql = "UPDATE $tbl_announcement SET email_sent='1' WHERE id='$insert_id'";
  712. api_sql_query($sql,__FILE__,__LINE__);
  713. }