announcements.inc.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  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. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  25. /*
  26. ==============================================================================
  27. DISPLAY FUNCTIONS
  28. ==============================================================================
  29. */
  30. /**
  31. * displays one specific announcement
  32. * @param $announcement_id, the id of the announcement you want to display
  33. * @todo remove globals
  34. * @todo more security checking
  35. */
  36. function display_announcement($announcement_id)
  37. {
  38. global $_user, $dateFormatLong;
  39. if ($announcement_id != strval(intval($announcement_id))) { return false; } // potencial sql injection
  40. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  41. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  42. if ($_user['user_id'])
  43. {
  44. $sql_query = " SELECT announcement.*, toolitemproperties.*
  45. FROM $tbl_announcement announcement, $tbl_item_property toolitemproperties
  46. WHERE announcement.id = toolitemproperties.ref
  47. AND announcement.id = '$announcement_id'
  48. AND toolitemproperties.tool='announcement'
  49. AND (toolitemproperties.to_user_id='".$_user['user_id']."' OR toolitemproperties.to_group_id='0')
  50. AND toolitemproperties.visibility='1'
  51. ORDER BY display_order DESC";
  52. }
  53. else
  54. {
  55. $sql_query = " SELECT announcement.*, toolitemproperties.*
  56. FROM $tbl_announcement announcement, $tbl_item_property toolitemproperties
  57. WHERE announcement.id = toolitemproperties.ref
  58. AND announcement.id = '$announcement_id'
  59. AND toolitemproperties.tool='announcement'
  60. AND toolitemproperties.to_group_id='0'
  61. AND toolitemproperties.visibility='1'";
  62. }
  63. $sql_result = Database::query($sql_query,__FILE__,__LINE__);
  64. $result = Database::fetch_array($sql_result);
  65. if ($result !== false) // A sanity check.
  66. {
  67. $title = $result['title'];
  68. $content = $result['content'];
  69. $content = make_clickable($content);
  70. $content = text_filter($content);
  71. $last_post_datetime = $result['insert_date'];// post time format datetime de mysql
  72. list($last_post_date, $last_post_time) = split(" ", $last_post_datetime);
  73. }
  74. echo "<table height=\"100\" width=\"100%\" border=\"1\" cellpadding=\"5\" cellspacing=\"0\" id=\"agenda_list\">\n";
  75. echo "<tr class=\"data\"><td>" . $title . "</td></tr>\n";
  76. echo "<tr><td class=\"announcements_datum\">" . get_lang('AnnouncementPublishedOn') . " : " . api_ucfirst(format_locale_date($dateFormatLong,strtotime($last_post_date) ) ) . "</td></tr>\n";
  77. echo "<tr class=\"text\"><td>$content</td></tr>\n";
  78. echo "</table>";
  79. }
  80. /*======================================
  81. SHOW_TO_FORM
  82. ======================================*/
  83. /**
  84. * this function shows the form for sending a message to a specific group or user.
  85. */
  86. function show_to_form($to_already_selected)
  87. {
  88. $user_list=get_course_users();
  89. $group_list=get_course_groups();
  90. if ($to_already_selected == "")
  91. $to_already_selected = array();
  92. echo "\n<table id=\"recipient_list\" style=\"display: none;\">\n";
  93. echo "\t<tr>\n";
  94. // the form containing all the groups and all the users of the course
  95. echo "\t\t<td>\n";
  96. echo "<strong>".get_lang('Users')."</strong><br />";
  97. construct_not_selected_select_form($group_list,$user_list,$to_already_selected);
  98. echo "\t\t</td>\n";
  99. // the buttons for adding or removing groups/users
  100. echo "\n\t\t<td valign=\"middle\">\n";
  101. /*echo "\t\t<input type=\"button\" ",
  102. "onClick=\"javascript: move(this.form.elements[0],this.form.elements[3])\" ",// 7 & 4 : fonts
  103. "value=\" >> \">",
  104. "\n\t\t<p>&nbsp;</p>",
  105. "\n\t\t<input type=\"button\"",
  106. "onClick=\"javascript: move(this.form.elements[3],this.form.elements[0])\" ",
  107. "value=\" << \">";*/
  108. ?>
  109. <button class="arrowr" type="button" onClick="javascript: move(this.form.elements[0], this.form.elements[3])" onClick="javascript: move(this.form.elements[0], this.form.elements[3])"></button>
  110. <br /> <br />
  111. <button class="arrowl" type="button" onClick="javascript: move(this.form.elements[3], this.form.elements[0])" onClick="javascript: move(this.form.elements[3], this.form.elements[0])"></button>
  112. <?php
  113. echo "\t\t</td>\n";
  114. echo "\n\t\t<td>\n";
  115. // the form containing the selected groups and users
  116. echo "<strong>".get_lang('DestinationUsers')."</strong><br />";
  117. construct_selected_select_form($group_list,$user_list,$to_already_selected);
  118. echo "\t\t</td>\n";
  119. echo "\t</tr>\n";
  120. echo "</table>";
  121. }
  122. /*===========================================
  123. CONSTRUCT_NOT_SELECT_SELECT_FORM
  124. ===========================================*/
  125. /**
  126. * this function shows the form for sending a message to a specific group or user.
  127. */
  128. function construct_not_selected_select_form($group_list=null, $user_list=null,$to_already_selected)
  129. {
  130. echo "\t\t<select name=\"not_selected_form[]\" size=5 style=\"width:200px\" multiple>\n";
  131. // adding the groups to the select form
  132. if ($group_list)
  133. {
  134. foreach($group_list as $this_group)
  135. {
  136. if (is_array($to_already_selected))
  137. {
  138. 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
  139. {
  140. echo "\t\t<option value=\"GROUP:".$this_group['id']."\">",
  141. "G: ",$this_group['name']," - " . $this_group['userNb'] . " " . get_lang('Users') .
  142. "</option>\n";
  143. }
  144. }
  145. }
  146. // a divider
  147. echo "\t\t<option value=\"\">---------------------------------------------------------</option>\n";
  148. }
  149. // adding the individual users to the select form
  150. foreach($user_list as $this_user)
  151. {
  152. if (is_array($to_already_selected)) {
  153. 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
  154. {
  155. echo "\t\t<option value=\"USER:",$this_user["user_id"],"\">",
  156. "", api_get_person_name($this_user['firstName'], $this_user['lastName']),
  157. "</option>\n";
  158. }
  159. }
  160. }
  161. echo "\t\t</select>\n";
  162. }
  163. /*==========================================
  164. CONSTRUCT_SELECTED_SELECT_FORM
  165. ==========================================*/
  166. /**
  167. * this function shows the form for sending a message to a specific group or user.
  168. */
  169. function construct_selected_select_form($group_list=null, $user_list=null,$to_already_selected)
  170. {
  171. // we separate the $to_already_selected array (containing groups AND users into
  172. // two separate arrays
  173. $groupuser = array();
  174. if (is_array($to_already_selected))
  175. {
  176. $groupuser=separate_users_groups($to_already_selected);
  177. }
  178. $groups_to_already_selected=$groupuser['groups'];
  179. $users_to_already_selected=$groupuser['users'];
  180. // we load all the groups and all the users into a reference array that we use to search the name of the group / user
  181. $ref_array_groups=get_course_groups();
  182. $ref_array_users=get_course_users();
  183. // we construct the form of the already selected groups / users
  184. echo "\t\t<select name=\"selectedform[]\" size=\"5\" multiple style=\"width:200px\" width=\"200px\">";
  185. if (is_array($to_already_selected)) {
  186. foreach($to_already_selected as $groupuser)
  187. {
  188. list($type,$id)=explode(":",$groupuser);
  189. if ($type=="GROUP")
  190. {
  191. echo "\t\t<option value=\"".$groupuser."\">G: ".$ref_array_groups[$id]['name']."</option>";
  192. }
  193. else
  194. {
  195. foreach($ref_array_users as $key=>$value){
  196. if($value['user_id']==$id){
  197. echo "\t\t<option value=\"".$groupuser."\">".api_get_person_name($value['firstName'], $value['lastName'])."</option>";
  198. break;
  199. }
  200. }
  201. }
  202. }
  203. } else {
  204. if ($to_already_selected=='everyone') {
  205. // adding the groups to the select form
  206. if (is_array($ref_array_groups))
  207. {
  208. foreach($ref_array_groups as $this_group)
  209. {
  210. //api_display_normal_message("group " . $thisGroup[id] . $thisGroup[name]);
  211. 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
  212. {
  213. echo "\t\t<option value=\"GROUP:".$this_group['id']."\">",
  214. "G: ",$this_group['name']," &ndash; " . $this_group['userNb'] . " " . get_lang('Users') .
  215. "</option>\n";
  216. }
  217. }
  218. }
  219. // adding the individual users to the select form
  220. foreach($ref_array_users as $this_user)
  221. {
  222. 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
  223. {
  224. echo "\t\t<option value=\"USER:",$this_user['user_id'],"\">",
  225. "", api_get_person_name($this_user['lastName'], $this_user['firstName']),
  226. "</option>\n";
  227. }
  228. }
  229. }
  230. }
  231. echo "</select>\n";
  232. }
  233. /**
  234. * this function shows the form for sending a message to a specific group or user.
  235. */
  236. function show_to_form_group($group_id)
  237. {
  238. echo "\n<table id=\"recipient_list\" style=\"display: none;\">\n";
  239. echo "\t<tr>\n";
  240. echo "\t\t<td>\n";
  241. echo "\t\t<select name=\"not_selected_form[]\" size=5 style=\"width:200px\" multiple>\n";
  242. $group_users = GroupManager::get_subscribed_users($group_id);
  243. foreach ($group_users as $user){
  244. echo '<option value="'.$user['user_id'].'">'.api_get_person_name($user['firstname'], $user['lastname']).'</option>';
  245. }
  246. echo '</select>';
  247. echo "\t\t</td>\n";
  248. // the buttons for adding or removing groups/users
  249. echo "\n\t\t<td valign=\"middle\">\n";
  250. /*echo "\t\t<input type=\"button\" ",
  251. "onClick=\"move(this.form.elements[1],this.form.elements[4])\" ",// 7 & 4 : fonts
  252. "value=\" >> \">",
  253. "\n\t\t<p>&nbsp;</p>",
  254. "\n\t\t<input type=\"button\"",
  255. "onClick=\"move(this.form.elements[4],this.form.elements[1])\" ",
  256. "value=\" << \">";*/
  257. ?>
  258. <button class="arrowr" type="button" onClick="javascript: move(this.form.elements[1], this.form.elements[4])" onClick="javascript: move(this.form.elements[1], this.form.elements[4])"></button>
  259. <br /> <br />
  260. <button class="arrowl" type="button" onClick="javascript: move(this.form.elements[4], this.form.elements[1])" onClick="javascript: move(this.form.elements[4], this.form.elements[1])"></button>
  261. <?php
  262. echo "\t\t</td>\n";
  263. echo "\n\t\t<td>\n";
  264. echo "\t\t<select name=\"selectedform[]\" size=5 style=\"width:200px\" multiple>\n";
  265. echo '</select>';
  266. echo "\t\t</td>\n";
  267. echo "\t</tr>\n";
  268. echo "</table>";
  269. }
  270. /*
  271. ==============================================================================
  272. DATA FUNCTIONS
  273. ==============================================================================
  274. */
  275. /**
  276. * this function gets all the users of the course,
  277. * including users from linked courses
  278. */
  279. function get_course_users()
  280. {
  281. //this would return only the users from real courses:
  282. //$user_list = CourseManager::get_user_list_from_course_code(api_get_course_id());
  283. $user_list = CourseManager::get_real_and_linked_user_list(api_get_course_id(), true, $_SESSION['id_session']);
  284. return $user_list;
  285. }
  286. /**
  287. * this function gets all the groups of the course,
  288. * not including linked courses
  289. */
  290. function get_course_groups()
  291. {
  292. $new_group_list = CourseManager::get_group_list_of_course(api_get_course_id(), intval($_SESSION['id_session']));
  293. return $new_group_list;
  294. }
  295. /*======================================
  296. LOAD_EDIT_USERS
  297. ======================================*/
  298. /**
  299. * This tools loads all the users and all the groups who have received
  300. * a specific item (in this case an announcement item)
  301. */
  302. function load_edit_users($tool, $id)
  303. {
  304. global $_course;
  305. global $tbl_item_property;
  306. $tool = Database::escape_string($tool);
  307. $id = Database::escape_string($id);
  308. $sql="SELECT * FROM $tbl_item_property WHERE tool='$tool' AND ref='$id'";
  309. $result=Database::query($sql,__FILE__,__LINE__) or die (mysql_error());
  310. while ($row=Database::fetch_array($result))
  311. {
  312. $to_group=$row['to_group_id'];
  313. switch ($to_group)
  314. {
  315. // it was send to one specific user
  316. case null:
  317. $to[]="USER:".$row['to_user_id'];
  318. break;
  319. // it was sent to everyone
  320. case 0:
  321. return "everyone";
  322. exit;
  323. break;
  324. default:
  325. $to[]="GROUP:".$row['to_group_id'];
  326. }
  327. }
  328. return $to;
  329. }
  330. /*======================================
  331. USER_GROUP_FILTER_JAVASCRIPT
  332. ======================================*/
  333. /**
  334. * returns the javascript for setting a filter
  335. * this goes into the $htmlHeadXtra[] array
  336. */
  337. function user_group_filter_javascript()
  338. {
  339. return "<script language=\"JavaScript\" type=\"text/JavaScript\">
  340. <!--
  341. function jumpMenu(targ,selObj,restore)
  342. {
  343. eval(targ+\".location='\"+selObj.options[selObj.selectedIndex].value+\"'\");
  344. if (restore) selObj.selectedIndex=0;
  345. }
  346. //-->
  347. </script>
  348. ";
  349. }
  350. /*======================================
  351. TO_JAVASCRIPT
  352. ========================================*/
  353. /**
  354. * returns all the javascript that is required for easily
  355. * setting the target people/groups
  356. * this goes into the $htmlHeadXtra[] array
  357. */
  358. function to_javascript()
  359. {
  360. return "<script type=\"text/javascript\" language=\"JavaScript\">
  361. <!-- Begin javascript menu swapper
  362. function move(fbox, tbox)
  363. {
  364. var arrFbox = new Array();
  365. var arrTbox = new Array();
  366. var arrLookup = new Array();
  367. var i;
  368. for (i = 0; i < tbox.options.length; i++)
  369. {
  370. arrLookup[tbox.options[i].text] = tbox.options[i].value;
  371. arrTbox[i] = tbox.options[i].text;
  372. }
  373. var fLength = 0;
  374. var tLength = arrTbox.length;
  375. for(i = 0; i < fbox.options.length; i++)
  376. {
  377. arrLookup[fbox.options[i].text] = fbox.options[i].value;
  378. if (fbox.options[i].selected && fbox.options[i].value != \"\")
  379. {
  380. arrTbox[tLength] = fbox.options[i].text;
  381. tLength++;
  382. }
  383. else
  384. {
  385. arrFbox[fLength] = fbox.options[i].text;
  386. fLength++;
  387. }
  388. }
  389. arrFbox.sort();
  390. arrTbox.sort();
  391. var arrFboxGroup = new Array();
  392. var arrFboxUser = new Array();
  393. var prefix_x;
  394. for (x = 0; x < arrFbox.length; x++) {
  395. prefix_x = arrFbox[x].substring(0,2);
  396. if (prefix_x == 'G:') {
  397. arrFboxGroup.push(arrFbox[x]);
  398. } else {
  399. arrFboxUser.push(arrFbox[x]);
  400. }
  401. }
  402. arrFboxGroup.sort();
  403. arrFboxUser.sort();
  404. arrFbox = arrFboxGroup.concat(arrFboxUser);
  405. var arrTboxGroup = new Array();
  406. var arrTboxUser = new Array();
  407. var prefix_y;
  408. for (y = 0; y < arrTbox.length; y++) {
  409. prefix_y = arrTbox[y].substring(0,2);
  410. if (prefix_y == 'G:') {
  411. arrTboxGroup.push(arrTbox[y]);
  412. } else {
  413. arrTboxUser.push(arrTbox[y]);
  414. }
  415. }
  416. arrTboxGroup.sort();
  417. arrTboxUser.sort();
  418. arrTbox = arrTboxGroup.concat(arrTboxUser);
  419. fbox.length = 0;
  420. tbox.length = 0;
  421. var c;
  422. for(c = 0; c < arrFbox.length; c++)
  423. {
  424. var no = new Option();
  425. no.value = arrLookup[arrFbox[c]];
  426. no.text = arrFbox[c];
  427. fbox[c] = no;
  428. }
  429. for(c = 0; c < arrTbox.length; c++)
  430. {
  431. var no = new Option();
  432. no.value = arrLookup[arrTbox[c]];
  433. no.text = arrTbox[c];
  434. tbox[c] = no;
  435. }
  436. }
  437. function validate()
  438. {
  439. var f = document.new_calendar_item;
  440. f.submit();
  441. return true;
  442. }
  443. function selectAll(cbList,bSelect,showwarning)
  444. {
  445. if (document.getElementById('emailTitle').value==''){
  446. document.getElementById('msg_error').innerHTML='".get_lang('FieldRequired')."';
  447. document.getElementById('msg_error').style.display='block';
  448. document.getElementById('emailTitle').focus();
  449. }else {
  450. if (cbList.length < 1) {
  451. if (!confirm(\"".get_lang('Send2All')."\")) {
  452. return false;
  453. }
  454. }
  455. for (var i=0; i<cbList.length; i++)
  456. cbList[i].selected = cbList[i].checked = bSelect;
  457. document.f1.submit();
  458. }
  459. }
  460. function reverseAll(cbList)
  461. {
  462. for (var i=0; i<cbList.length; i++)
  463. {
  464. cbList[i].checked = !(cbList[i].checked)
  465. cbList[i].selected = !(cbList[i].selected)
  466. }
  467. }
  468. function plus_attachment() {
  469. if (document.getElementById('options').style.display == 'none') {
  470. document.getElementById('options').style.display = 'block';
  471. document.getElementById('plus').innerHTML='&nbsp;<img style=\"vertical-align:middle;\" src=\"../img/div_hide.gif\" alt=\"\" />&nbsp;".get_lang('AddAnAttachment')."';
  472. } else {
  473. document.getElementById('options').style.display = 'none';
  474. document.getElementById('plus').innerHTML='&nbsp;<img style=\"vertical-align:middle;\" src=\"../img/div_show.gif\" alt=\"\" />&nbsp;".get_lang('AddAnAttachment')."';
  475. }
  476. }
  477. // End -->
  478. </script>";
  479. }
  480. /*======================================
  481. SENT_TO_FORM
  482. ======================================*/
  483. /**
  484. * constructs the form to display all the groups and users the message has been sent to
  485. * input: $sent_to_array is a 2 dimensional array containing the groups and the users
  486. * the first level is a distinction between groups and users:
  487. * $sent_to_array['groups'] * and $sent_to_array['users']
  488. * $sent_to_array['groups'] (resp. $sent_to_array['users']) is also an array
  489. * containing all the id's of the groups (resp. users) who have received this message.
  490. * @author Patrick Cool <patrick.cool@>
  491. */
  492. function sent_to_form($sent_to_array)
  493. {
  494. // we find all the names of the groups
  495. $group_names=get_course_groups();
  496. count($sent_to_array);
  497. // we count the number of users and the number of groups
  498. if (isset($sent_to_array['users']))
  499. {
  500. $number_users=count($sent_to_array['users']);
  501. }
  502. else
  503. {
  504. $number_users=0;
  505. }
  506. if (isset($sent_to_array['groups']))
  507. {
  508. $number_groups=count($sent_to_array['groups']);
  509. }
  510. else
  511. {
  512. $number_groups=0;
  513. }
  514. $total_numbers=$number_users+$number_groups;
  515. // starting the form if there is more than one user/group
  516. if ($total_numbers >1)
  517. {
  518. $output="<select name=\"sent to\">\n";
  519. $output.="<option>".get_lang("SentTo")."</option>";
  520. // outputting the name of the groups
  521. if (is_array($sent_to_array['groups']))
  522. {
  523. foreach ($sent_to_array['groups'] as $group_id)
  524. {
  525. $output.="\t<option value=\"\">G: ".$group_names[$group_id]['name']."</option>\n";
  526. }
  527. }
  528. if (isset($sent_to_array['users']))
  529. {
  530. if (is_array($sent_to_array['users']))
  531. {
  532. foreach ($sent_to_array['users'] as $user_id)
  533. {
  534. $user_info = api_get_user_info($user_id);
  535. $output.="\t<option value=\"\">".api_get_person_name($user_info['firstName'], $user_info['lastName'])."</option>\n";
  536. }
  537. }
  538. }
  539. // ending the form
  540. $output.="</select>\n";
  541. }
  542. else // there is only one user/group
  543. {
  544. if (isset($sent_to_array['users']) and is_array($sent_to_array['users']))
  545. {
  546. $user_info = api_get_user_info($sent_to_array['users'][0]);
  547. echo api_get_person_name($user_info['firstName'], $user_info['lastName']);
  548. }
  549. if (isset($sent_to_array['groups']) and is_array($sent_to_array['groups']) and $sent_to_array['groups'][0]!==0)
  550. {
  551. $group_id=$sent_to_array['groups'][0];
  552. echo $group_names[$group_id]['name'];
  553. }
  554. if (isset($sent_to_array['groups']) and is_array($sent_to_array['groups']) and $sent_to_array['groups'][0]==0)
  555. {
  556. echo get_lang("Everybody");
  557. }
  558. }
  559. if(!empty($output))
  560. {
  561. echo $output;
  562. }
  563. }
  564. /*======================================
  565. SEPARATE_USERS_GROUPS
  566. ======================================*/
  567. /**
  568. * This function separates the users from the groups
  569. * users have a value USER:XXX (with XXX the dokeos id
  570. * groups have a value GROUP:YYY (with YYY the group id)
  571. * @param array Array of strings that define the type and id of each destination
  572. * @return array Array of groups and users (each an array of IDs)
  573. */
  574. function separate_users_groups($to)
  575. {
  576. foreach($to as $to_item) {
  577. list($type, $id) = explode(':', $to_item);
  578. switch($type) {
  579. case 'GROUP':
  580. $grouplist[] = intval($id);
  581. break;
  582. case 'USER':
  583. $userlist[] = intval($id);
  584. break;
  585. }
  586. }
  587. $send_to['groups']=$grouplist;
  588. $send_to['users']=$userlist;
  589. return $send_to;
  590. }
  591. /*======================================
  592. SENT_TO()
  593. ======================================*/
  594. /**
  595. * Returns all the users and all the groups a specific announcement item
  596. * has been sent to
  597. * @param string The tool (announcement, agenda, ...)
  598. * @param int ID of the element of the corresponding type
  599. * @return array Array of users and groups to whom the element has been sent
  600. */
  601. function sent_to($tool, $id)
  602. {
  603. global $_course;
  604. global $tbl_item_property;
  605. $tool = Database::escape_string($tool);
  606. $id = Database::escape_string($id);
  607. $sent_to_group = array();
  608. $sent_to = array();
  609. $sql="SELECT * FROM $tbl_item_property WHERE tool='$tool' AND ref='".$id."'";
  610. $result = Database::query($sql,__FILE__,__LINE__);
  611. while ($row=Database::fetch_array($result)) {
  612. // if to_group_id is null then it is sent to a specific user
  613. // if to_group_id = 0 then it is sent to everybody
  614. if ($row['to_group_id'] != 0)
  615. {
  616. $sent_to_group[]=$row['to_group_id'];
  617. }
  618. // if to_user_id <> 0 then it is sent to a specific user
  619. if ($row['to_user_id'] <> 0)
  620. {
  621. $sent_to_user[]=$row['to_user_id'];
  622. }
  623. }
  624. if (isset($sent_to_group))
  625. {
  626. $sent_to['groups']=$sent_to_group;
  627. }
  628. if (isset($sent_to_user))
  629. {
  630. $sent_to['users']=$sent_to_user;
  631. }
  632. return $sent_to;
  633. }
  634. /*===================================================
  635. CHANGE_VISIBILITY($tool,$id)
  636. =================================================*/
  637. /**
  638. * This functions swithes the visibility a course resource
  639. * using the visibility field in 'item_property'
  640. * values: 0 = invisibility for
  641. * @param string The tool (announcement, agenda, ...)
  642. * @param int ID of the element of the corresponding type
  643. * @return bool False on failure, True on success
  644. */
  645. function change_visibility_announcement($tool,$id)
  646. {
  647. global $_course;
  648. global $tbl_item_property;
  649. $tool = Database::escape_string($tool);
  650. $id = Database::escape_string($id);
  651. $sql="SELECT * FROM $tbl_item_property WHERE tool='$tool' AND ref='$id'";
  652. $result=Database::query($sql,__FILE__,__LINE__) or die (mysql_error());
  653. $row=Database::fetch_array($result);
  654. if ($row['visibility']=='1')
  655. {
  656. $sql_visibility="UPDATE $tbl_item_property SET visibility='0' WHERE tool='$tool' AND ref='$id'";
  657. }
  658. else
  659. {
  660. $sql_visibility="UPDATE $tbl_item_property SET visibility='1' WHERE tool='$tool' AND ref='$id'";
  661. }
  662. $result=Database::query($sql_visibility,__FILE__,__LINE__);
  663. if ($result === false) {
  664. return false;
  665. }
  666. return true;
  667. }
  668. /**
  669. * Store an announcement in the database (including its attached file if any)
  670. * @param string Announcement title (pure text)
  671. * @param string Content of the announcement (can be HTML)
  672. * @param int Display order in the list of announcements
  673. * @param array Array of users and groups to send the announcement to
  674. * @param array uploaded file $_FILES
  675. * @param string Comment describing the attachment
  676. * @return int false on failure, ID of the announcement on success
  677. */
  678. function store_advalvas_item($emailTitle, $newContent, $order, $to, $file = array(), $file_comment='') {
  679. global $_course;
  680. global $nameTools;
  681. global $_user;
  682. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  683. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  684. // filter data
  685. $emailTitle = Database::escape_string($emailTitle);
  686. $newContent = Database::escape_string($newContent);
  687. $order = intval($order);
  688. // store in the table announcement
  689. $sql = "INSERT INTO $tbl_announcement SET content = '$newContent', title = '$emailTitle', end_date = NOW(), display_order ='$order', session_id=".intval($_SESSION['id_session']);
  690. $result = Database::query($sql,__FILE__,__LINE__);
  691. if ($result === false) {
  692. return false;
  693. }
  694. //store the attach file
  695. $last_id = Database::insert_id();
  696. if (!empty($file)) {
  697. $save_attachment = add_announcement_attachment_file($last_id, $file_comment, $_FILES['user_upload']);
  698. }
  699. // store in item_property (first the groups, then the users
  700. if (!is_null($to)) // !is_null($to): when no user is selected we send it to everyone
  701. {
  702. $send_to=separate_users_groups($to);
  703. // storing the selected groups
  704. if (is_array($send_to['groups']))
  705. {
  706. foreach ($send_to['groups'] as $group)
  707. {
  708. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", $_user['user_id'], $group);
  709. }
  710. }
  711. // storing the selected users
  712. if (is_array($send_to['users']))
  713. {
  714. foreach ($send_to['users'] as $user)
  715. {
  716. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", $_user['user_id'], '', $user);
  717. }
  718. }
  719. }
  720. else // the message is sent to everyone, so we set the group to 0
  721. {
  722. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", $_user['user_id'], '0');
  723. }
  724. return $last_id;
  725. }
  726. function store_advalvas_group_item($emailTitle,$newContent, $order, $to, $to_users, $file = array(), $file_comment='')
  727. {
  728. global $_course;
  729. global $nameTools;
  730. global $_user;
  731. // database definitions
  732. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  733. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  734. $newContent=stripslashes($newContent);
  735. $emailTitle = Database::escape_string($emailTitle);
  736. $newContent = Database::escape_string($newContent);
  737. $order = intval($order);
  738. // store in the table announcement
  739. $sql = "INSERT INTO $tbl_announcement SET content = '$newContent', title = '$emailTitle', end_date = NOW(), display_order ='$order', session_id=".intval($_SESSION['id_session']);
  740. $result = Database::query($sql,__FILE__,__LINE__) or die (mysql_error());
  741. if ($result === false) {
  742. return false;
  743. }
  744. //store the attach file
  745. $last_id = Database::insert_id();
  746. if (empty($file)) {
  747. $save_attachment = add_announcement_attachment_file($last_id, $file_comment, $file);
  748. }
  749. // store in item_property (first the groups, then the users
  750. if (!isset($to_users)) // !isset($to): when no user is selected we send it to everyone
  751. {
  752. $send_to=separate_users_groups($to);
  753. // storing the selected groups
  754. if (is_array($send_to['groups']))
  755. {
  756. foreach ($send_to['groups'] as $group)
  757. {
  758. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", $_user['user_id'], $group);
  759. }
  760. }
  761. }
  762. else // the message is sent to everyone, so we set the group to 0
  763. {
  764. // storing the selected users
  765. if (is_array($to_users))
  766. {
  767. foreach ($to_users as $user)
  768. {
  769. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", $_user['user_id'], '', $user);
  770. }
  771. }
  772. }
  773. return $last_id;
  774. }
  775. /*==================================================
  776. EDIT_VALVAS_ITEM
  777. ==================================================*/
  778. /**
  779. * This function stores the announcement Item in the table announcement
  780. * and updates the item_property also
  781. */
  782. function edit_advalvas_item($id,$emailTitle,$newContent,$to,$file = array(), $file_comment='')
  783. {
  784. global $_course;
  785. global $nameTools;
  786. global $_user;
  787. global $tbl_announcement;
  788. global $tbl_item_property;
  789. $newContent=stripslashes($newContent);
  790. $emailTitle = Database::escape_string(Security::remove_XSS($emailTitle));
  791. $newContent = Database::escape_string(Security::remove_XSS($newContent,COURSEMANAGERLOWSECURITY));
  792. $order = intval($order);
  793. // store the modifications in the table announcement
  794. $sql = "UPDATE $tbl_announcement SET content='$newContent', title = '$emailTitle' WHERE id='$id'";
  795. $result = Database::query($sql,__FILE__,__LINE__) or die (mysql_error());
  796. // save attachment file
  797. $row_attach = get_attachment($id);
  798. $id_attach = intval($row_attach['id']);
  799. if (!empty($file)) {
  800. if (empty($id_attach)) {
  801. add_announcement_attachment_file($id,$file_comment,$file);
  802. } else {
  803. edit_announcement_attachment_file($id_attach,$file,$file_comment);
  804. }
  805. }
  806. // we remove everything from item_property for this
  807. $sql_delete="DELETE FROM $tbl_item_property WHERE ref='$id' AND tool='announcement'";
  808. $result = Database::query($sql_delete,__FILE__,__LINE__) or die (mysql_error());
  809. // store in item_property (first the groups, then the users
  810. if (!is_null($to)) // !is_null($to): when no user is selected we send it to everyone
  811. {
  812. $send_to=separate_users_groups($to);
  813. // storing the selected groups
  814. if (is_array($send_to['groups']))
  815. {
  816. foreach ($send_to['groups'] as $group)
  817. {
  818. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, "AnnouncementUpdated", $_user['user_id'], $group);
  819. }
  820. }
  821. // storing the selected users
  822. if (is_array($send_to['users']))
  823. {
  824. foreach ($send_to['users'] as $user)
  825. {
  826. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, "AnnouncementUpdated", $_user['user_id'], 0, $user);
  827. }
  828. }
  829. }
  830. else // the message is sent to everyone, so we set the group to 0
  831. {
  832. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, "AnnouncementUpdated", $_user['user_id'], '0');
  833. }
  834. }
  835. /*
  836. ==============================================================================
  837. MAIL FUNCTIONS
  838. ==============================================================================
  839. */
  840. /**
  841. * Sends an announcement by email to a list of users.
  842. * Emails are sent one by one to try to avoid antispam.
  843. */
  844. function send_announcement_email($user_list, $course_code, $_course, $mail_title, $mail_content)
  845. {
  846. global $_user;
  847. foreach ($user_list as $this_user) {
  848. $mail_subject = get_lang('professorMessage').' - '.$_course['official_code'].' - '.$mail_title;
  849. $mail_body = '['.$_course['official_code'].'] - ['.$_course['name']."]\n";
  850. $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, api_get_system_encoding())))." \n\n-- \n";
  851. $mail_body .= api_get_person_name($_user['firstName'], $_user['lastName'], null, PERSON_NAME_EMAIL_ADDRESS).' ';
  852. $mail_body .= '<'.$_user['mail'].">\n";
  853. $mail_body .= $_course['official_code'].' '.$_course['name'];
  854. @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']);
  855. }
  856. }
  857. function update_mail_sent($insert_id)
  858. {
  859. global $_course;
  860. global $tbl_announcement;
  861. if ($insert_id != strval(intval($insert_id))) { return false; }
  862. $insert_id = Database::escape_string($insert_id);
  863. // store the modifications in the table tbl_annoucement
  864. $sql = "UPDATE $tbl_announcement SET email_sent='1' WHERE id='$insert_id'";
  865. Database::query($sql,__FILE__,__LINE__);
  866. }
  867. /**
  868. * Gets all announcements from a user by course
  869. * @param string course db
  870. * @param int user id
  871. * @return array html with the content and count of announcements or false otherwise
  872. */
  873. function get_all_annoucement_by_user_course($course_db, $user_id)
  874. {
  875. if (empty($course_db) || empty($user_id)) {
  876. return false;
  877. }
  878. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT, $course_db);
  879. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY, $course_db);
  880. if (!empty($user_id) && is_numeric($user_id)) {
  881. $user_id = intval($user_id);
  882. $sql="SELECT DISTINCT announcement.title, announcement.content
  883. FROM $tbl_announcement announcement, $tbl_item_property toolitemproperties
  884. WHERE announcement.id = toolitemproperties.ref
  885. AND toolitemproperties.tool='announcement'
  886. AND (toolitemproperties.insert_user_id='$user_id' AND (toolitemproperties.to_group_id='0' OR toolitemproperties.to_group_id is null))
  887. AND toolitemproperties.visibility='1'
  888. AND announcement.session_id = 0
  889. ORDER BY display_order DESC";
  890. $rs = Database::query($sql,__FILE__,__LINE__);
  891. $num_rows = Database::num_rows($rs);
  892. $content = '';
  893. $i=0;
  894. $result = array();
  895. if ($num_rows>0) {
  896. while ($myrow = Database::fetch_array($rs)) {
  897. //if ($i<=4) {
  898. $content.= '<strong>'.$myrow['title'].'</strong><br /><br />';
  899. $content.= $myrow['content'];
  900. /*} else {
  901. break;
  902. }*/
  903. $i++;
  904. }
  905. $result['content'] = $content;
  906. $result['count'] = $i;
  907. return $result;
  908. }
  909. return false;
  910. }
  911. return false;
  912. }
  913. /*
  914. ==============================================================================
  915. ATTACHMENT FUNCTIONS
  916. ==============================================================================
  917. */
  918. /**
  919. * Show a list with all the attachments according to the post's id
  920. * @param int announcement id
  921. * @return array with the post info
  922. * @author Arthur Portugal
  923. * @version November 2009, dokeos 1.8.6.2
  924. */
  925. function get_attachment($announcement_id) {
  926. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  927. $announcement_id=Database::escape_string($announcement_id);
  928. $row=array();
  929. $sql = 'SELECT id,path, filename,comment FROM '. $tbl_announcement_attachment.' WHERE announcement_id = '.(int)$announcement_id.'';
  930. $result=Database::query($sql, __FILE__, __LINE__);
  931. if (Database::num_rows($result)!=0) {
  932. $row=Database::fetch_array($result,ASSOC);
  933. }
  934. return $row;
  935. }
  936. /**
  937. * This function add a attachment file into announcement
  938. * @param int announcement id
  939. * @param string file comment
  940. * @param array uploaded file $_FILES
  941. * @return int -1 if failed, 0 if unknown (should not happen), 1 if success
  942. */
  943. function add_announcement_attachment_file($announcement_id, $file_comment, $file) {
  944. global $_course;
  945. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  946. $return = 0;
  947. $announcement_id = intval($announcement_id);
  948. if (is_array($file) && $file['error'] == 0 ) {
  949. $courseDir = $_course['path'].'/upload/announcements'; // TODO: This path is obsolete. The new document repository scheme should be kept in mind here.
  950. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  951. $updir = $sys_course_path.$courseDir;
  952. // Try to add an extension to the file if it hasn't one
  953. $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']);
  954. // user's file name
  955. $file_name = $file['name'];
  956. if (!filter_extension($new_file_name)) {
  957. $return = -1;
  958. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  959. } else {
  960. $new_file_name = uniqid('');
  961. $new_path = $updir.'/'.$new_file_name;
  962. $result = @move_uploaded_file($file['tmp_name'], $new_path);
  963. $safe_file_comment = Database::escape_string($file_comment);
  964. $safe_file_name = Database::escape_string($file_name);
  965. $safe_new_file_name = Database::escape_string($new_file_name);
  966. // Storing the attachments if any
  967. $sql = 'INSERT INTO '.$tbl_announcement_attachment.'(filename, comment, path, announcement_id, size) '.
  968. "VALUES ( '$safe_file_name', '$file_comment', '$safe_new_file_name' , '$announcement_id', '".intval($file['size'])."' )";
  969. $result = Database::query($sql, __LINE__, __FILE__);
  970. $return = 1;
  971. }
  972. }
  973. return $return;
  974. }
  975. /**
  976. * This function edit a attachment file into announcement
  977. * @param int attach id
  978. * @param array uploaded file $_FILES
  979. * @param string file comment
  980. * @return int
  981. */
  982. function edit_announcement_attachment_file($id_attach, $file, $file_comment) {
  983. global $_course;
  984. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  985. $return = 0;
  986. if (is_array($file) && $file['error'] == 0 ) {
  987. $courseDir = $_course['path'].'/upload/announcements'; // TODO: This path is obsolete. The new document repository scheme should be kept in mind here.
  988. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  989. $updir = $sys_course_path.$courseDir;
  990. // Try to add an extension to the file if it hasn't one
  991. $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']);
  992. // user's file name
  993. $file_name =$file ['name'];
  994. if (!filter_extension($new_file_name)) {
  995. $return -1;
  996. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  997. } else {
  998. $new_file_name = uniqid('');
  999. $new_path = $updir.'/'.$new_file_name;
  1000. $result = @move_uploaded_file($file['tmp_name'], $new_path);
  1001. $safe_file_comment = Database::escape_string($file_comment);
  1002. $safe_file_name = Database::escape_string($file_name);
  1003. $safe_new_file_name = Database::escape_string($new_file_name);
  1004. $id_attach = intval($id_attach);
  1005. $sql = "UPDATE $tbl_announcement_attachment SET filename = '$safe_file_name', comment = '$safe_file_comment', path = '$safe_new_file_name', size ='".intval($file['size'])."'
  1006. WHERE id = '$id_attach'";
  1007. $result = Database::query($sql, __FILE__,__LINE__);
  1008. if ($result === false) {
  1009. $return = -1;
  1010. Display :: display_error_message(get_lang('UplUnableToSaveFile'));
  1011. } else {
  1012. $return = 1;
  1013. }
  1014. }
  1015. }
  1016. return $return;
  1017. }
  1018. /**
  1019. * This function delete a attachment file by id
  1020. * @param integer attachment file Id
  1021. *
  1022. */
  1023. function delete_announcement_attachment_file($id) {
  1024. global $_course;
  1025. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1026. $id=Database::escape_string($id);
  1027. $sql="DELETE FROM $tbl_announcement_attachment WHERE id = $id";
  1028. $result=Database::query($sql, __FILE__,__LINE__);
  1029. // update item_property
  1030. //api_item_property_update($_course, 'announcement_attachment', $id,'AnnouncementAttachmentDeleted', api_get_user_id());
  1031. }