announcements.inc.php 28 KB

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