announcements.inc.php 26 KB

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