announcements.inc.php 30 KB

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