announcements.inc.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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 (!is_null($row['to_group_id']))
  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 string Comment describing the attachment
  675. * @return int false on failure, ID of the announcement on success
  676. */
  677. function store_advalvas_item($emailTitle, $newContent, $order, $to, $file_comment='') {
  678. global $_course;
  679. global $nameTools;
  680. global $_user;
  681. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  682. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  683. // filter data
  684. $emailTitle = Database::escape_string(Security::remove_XSS($emailTitle));
  685. $newContent = Database::escape_string(Security::remove_XSS($newContent,COURSEMANAGERLOWSECURITY));
  686. $order = intval($order);
  687. // store in the table announcement
  688. $sql = "INSERT INTO $tbl_announcement SET content = '$newContent', title = '$emailTitle', end_date = NOW(), display_order ='$order', session_id=".intval($_SESSION['id_session']);
  689. $result = Database::query($sql,__FILE__,__LINE__);
  690. if ($result === false) {
  691. return false;
  692. }
  693. //store the attach file
  694. $last_id = Database::insert_id();
  695. $save_attachment = add_announcement_attachment_file($last_id, $file_comment, $_FILES['user_upload']);
  696. // store in item_property (first the groups, then the users
  697. if (!is_null($to)) // !is_null($to): when no user is selected we send it to everyone
  698. {
  699. $send_to=separate_users_groups($to);
  700. // storing the selected groups
  701. if (is_array($send_to['groups']))
  702. {
  703. foreach ($send_to['groups'] as $group)
  704. {
  705. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", $_user['user_id'], $group);
  706. }
  707. }
  708. // storing the selected users
  709. if (is_array($send_to['users']))
  710. {
  711. foreach ($send_to['users'] as $user)
  712. {
  713. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", $_user['user_id'], '', $user);
  714. }
  715. }
  716. }
  717. else // the message is sent to everyone, so we set the group to 0
  718. {
  719. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", $_user['user_id'], '0');
  720. }
  721. return $last_id;
  722. }
  723. function store_advalvas_group_item($emailTitle,$newContent, $order, $to, $to_users, $file_comment='')
  724. {
  725. global $_course;
  726. global $nameTools;
  727. global $_user;
  728. // database definitions
  729. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  730. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  731. $newContent=stripslashes($newContent);
  732. $emailTitle = Database::escape_string(Security::remove_XSS($emailTitle));
  733. $newContent = Database::escape_string(Security::remove_XSS($newContent,COURSEMANAGERLOWSECURITY));
  734. $order = intval($order);
  735. // store in the table announcement
  736. $sql = "INSERT INTO $tbl_announcement SET content = '$newContent', title = '$emailTitle', end_date = NOW(), display_order ='$order', session_id=".intval($_SESSION['id_session']);
  737. $result = Database::query($sql,__FILE__,__LINE__) or die (mysql_error());
  738. if ($result === false) {
  739. return false;
  740. }
  741. //store the attach file
  742. $last_id = Database::insert_id();
  743. $save_attachment = add_announcement_attachment_file($last_id, $file_comment, $_FILES['user_upload']);
  744. // store in item_property (first the groups, then the users
  745. if (!isset($to_users)) // !isset($to): when no user is selected we send it to everyone
  746. {
  747. $send_to=separate_users_groups($to);
  748. // storing the selected groups
  749. if (is_array($send_to['groups']))
  750. {
  751. foreach ($send_to['groups'] as $group)
  752. {
  753. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", $_user['user_id'], $group);
  754. }
  755. }
  756. }
  757. else // the message is sent to everyone, so we set the group to 0
  758. {
  759. // storing the selected users
  760. if (is_array($to_users))
  761. {
  762. foreach ($to_users as $user)
  763. {
  764. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $last_id, "AnnouncementAdded", $_user['user_id'], '', $user);
  765. }
  766. }
  767. }
  768. return $last_id;
  769. }
  770. /*==================================================
  771. EDIT_VALVAS_ITEM
  772. ==================================================*/
  773. /**
  774. * This function stores the announcement Item in the table announcement
  775. * and updates the item_property also
  776. */
  777. function edit_advalvas_item($id,$emailTitle,$newContent,$to, $file_comment='')
  778. {
  779. global $_course;
  780. global $nameTools;
  781. global $_user;
  782. global $tbl_announcement;
  783. global $tbl_item_property;
  784. $newContent=stripslashes($newContent);
  785. $emailTitle = Database::escape_string(Security::remove_XSS($emailTitle));
  786. $newContent = Database::escape_string(Security::remove_XSS($newContent,COURSEMANAGERLOWSECURITY));
  787. $order = intval($order);
  788. // store the modifications in the table announcement
  789. $sql = "UPDATE $tbl_announcement SET content='$newContent', title = '$emailTitle' WHERE id='$id'";
  790. $result = Database::query($sql,__FILE__,__LINE__) or die (mysql_error());
  791. if(empty($last_id)){
  792. $last_id = $id;
  793. $save_attachment = add_announcement_attachment_file($last_id, $file_comment, $_FILES['user_upload']);
  794. }
  795. $last_id = $id;
  796. $edit_attachment = edit_announcement_attachment_file($last_id, $_FILES['user_upload'], $file_comment);
  797. // we remove everything from item_property for this
  798. $sql_delete="DELETE FROM $tbl_item_property WHERE ref='$id' AND tool='announcement'";
  799. $result = Database::query($sql_delete,__FILE__,__LINE__) or die (mysql_error());
  800. // store in item_property (first the groups, then the users
  801. if (!is_null($to)) // !is_null($to): when no user is selected we send it to everyone
  802. {
  803. $send_to=separate_users_groups($to);
  804. // storing the selected groups
  805. if (is_array($send_to['groups']))
  806. {
  807. foreach ($send_to['groups'] as $group)
  808. {
  809. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, "AnnouncementUpdated", $_user['user_id'], $group);
  810. }
  811. }
  812. // storing the selected users
  813. if (is_array($send_to['users']))
  814. {
  815. foreach ($send_to['users'] as $user)
  816. {
  817. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, "AnnouncementUpdated", $_user['user_id'], '', $user);
  818. }
  819. }
  820. }
  821. else // the message is sent to everyone, so we set the group to 0
  822. {
  823. api_item_property_update($_course, TOOL_ANNOUNCEMENT, $id, "AnnouncementUpdated", $_user['user_id'], '0');
  824. }
  825. }
  826. /*
  827. ==============================================================================
  828. MAIL FUNCTIONS
  829. ==============================================================================
  830. */
  831. /**
  832. * Sends an announcement by email to a list of users.
  833. * Emails are sent one by one to try to avoid antispam.
  834. */
  835. function send_announcement_email($user_list, $course_code, $_course, $mail_title, $mail_content)
  836. {
  837. global $charset;
  838. global $_user;
  839. foreach ($user_list as $this_user) {
  840. /* Header : Bericht van uw lesgever - GES ($course_code) - Morgen geen les! ($mail_title)
  841. Body : John Doe (prenom + nom) <john_doe@hotmail.com> (email)
  842. Morgen geen les! ($mail_title)
  843. Morgen is er geen les, de les wordt geschrapt wegens vergadering (newContent)
  844. */
  845. $mail_subject = get_lang('professorMessage').' - '.$_course['official_code'].' - '.$mail_title;
  846. $mail_body = '['.$_course['official_code'].'] - ['.$_course['name']."]\n";
  847. $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";
  848. $mail_body .= api_get_person_name($_user['firstName'], $_user['lastName'], null, PERSON_NAME_EMAIL_ADDRESS).' ';
  849. $mail_body .= '<'.$_user['mail'].">\n";
  850. $mail_body .= $_course['official_code'].' '.$_course['name'];
  851. //set the charset and use it for the encoding of the email - small fix, not really clean (should check the content encoding origin first)
  852. //here we use the encoding used for the webpage where the text is encoded (ISO-8859-1 in this case)
  853. if(empty($charset)){$charset='ISO-8859-1';}
  854. $encoding = 'Content-Type: text/plain; charset='. $charset;
  855. $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);
  856. }
  857. }
  858. function update_mail_sent($insert_id)
  859. {
  860. global $_course;
  861. global $tbl_announcement;
  862. if ($insert_id != strval(intval($insert_id))) { return false; }
  863. $insert_id = Database::escape_string($insert_id);
  864. // store the modifications in the table tbl_annoucement
  865. $sql = "UPDATE $tbl_announcement SET email_sent='1' WHERE id='$insert_id'";
  866. Database::query($sql,__FILE__,__LINE__);
  867. }
  868. /**
  869. * Gets all announcements from a user by course
  870. * @param string course db
  871. * @param int user id
  872. * @return string an html with the content
  873. */
  874. function get_all_annoucement_by_user_course($course_db, $user_id)
  875. {
  876. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT, $course_db);
  877. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY, $course_db);
  878. if (!empty($user_id) && is_numeric($user_id)) {
  879. $user_id = Database::escape_string($user_id);
  880. $sql="SELECT announcement.*, toolitemproperties.*
  881. FROM $tbl_announcement announcement, $tbl_item_property toolitemproperties
  882. WHERE announcement.id = toolitemproperties.ref
  883. AND toolitemproperties.tool='announcement'
  884. AND (toolitemproperties.insert_user_id='".$user_id."' AND toolitemproperties.to_group_id='0')
  885. AND toolitemproperties.visibility='1'
  886. AND announcement.session_id = 0
  887. ORDER BY display_order DESC";
  888. $result = Database::query($sql,__FILE__,__LINE__);
  889. $num_rows = Database::num_rows($result);
  890. $content = '';
  891. $i=0;
  892. if (Database::num_rows($result)>0) {
  893. while ($myrow = Database::fetch_array($result)) {
  894. if ($i<=4) {
  895. $content.= '<strong>'.$myrow['title'].'</strong><br /><br />';
  896. $content.= $myrow['content'];
  897. } else {
  898. break;
  899. }
  900. $i++;
  901. }
  902. return $content;
  903. } else {
  904. return $content;
  905. }
  906. } else {
  907. return '';
  908. }
  909. }
  910. /*
  911. ==============================================================================
  912. ATTACHMENT FUNCTIONS
  913. ==============================================================================
  914. */
  915. /**
  916. * Show a list with all the attachments according to the post's id
  917. * @param the post's id
  918. * @return array with the post info
  919. * @author Arthur Portugal
  920. * @version November 2009, dokeos 1.8.6.2
  921. */
  922. function get_attachment($announcement_id) {
  923. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  924. $announcement_id=Database::escape_string($announcement_id);
  925. $row=array();
  926. $sql = 'SELECT id,path, filename,comment FROM '. $tbl_announcement_attachment.' WHERE announcement_id = '.(int)$announcement_id.'';
  927. $result=Database::query($sql, __FILE__, __LINE__);
  928. if (Database::num_rows($result)!=0) {
  929. $row=Database::fetch_array($result,ASSOC);
  930. }
  931. return $row;
  932. }
  933. /**
  934. * This function add a attachment file into announcement
  935. * @param string a comment about file
  936. * @param int last id from announcement table
  937. * @return int -1 if failed, 0 if unknown (should not happen), 1 if success
  938. */
  939. function add_announcement_attachment_file($last_id, $file_comment, $file = array()) {
  940. global $_course;
  941. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  942. $return = 0;
  943. $last_id = intval($last_id);
  944. if (is_array($file) && $file['error'] == 0 ) {
  945. $courseDir = $_course['path'].'/upload/announcements';
  946. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  947. $updir = $sys_course_path.$courseDir;
  948. // Try to add an extension to the file if it hasn't one
  949. $new_file_name = add_ext_on_mime(stripslashes($_FILES['user_upload']['name']), $_FILES['user_upload']['type']);
  950. // user's file name
  951. $file_name = $_FILES['user_upload']['name'];
  952. if (!filter_extension($new_file_name)) {
  953. $return = -1;
  954. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  955. } else {
  956. $new_file_name = uniqid('');
  957. $new_path = $updir.'/'.$new_file_name;
  958. $result = @move_uploaded_file($_FILES['user_upload']['tmp_name'], $new_path);
  959. $safe_file_comment = Database::escape_string($file_comment);
  960. $safe_file_name = Database::escape_string($file_name);
  961. $safe_new_file_name = Database::escape_string($new_file_name);
  962. // Storing the attachments if any
  963. //if ($result) {
  964. $sql = 'INSERT INTO '.$tbl_announcement_attachment.'(filename, comment, path, announcement_id, size) '.
  965. "VALUES ( '$safe_file_name', '$file_comment', '$safe_new_file_name' , '$last_id', '".intval($_FILES['user_upload']['size'])."' )";
  966. $result = Database::query($sql, __LINE__, __FILE__);
  967. //$message .= ' / '.get_lang('FileUploadSucces').'<br />';
  968. $return = 1;
  969. //$last_id_file=Database::insert_id();
  970. //api_item_property_update($_course, 'announcement_attachment', $last_id_file ,'AnnouncementAttachmentAdded', api_get_user_id());
  971. //}
  972. }
  973. }
  974. return $return;
  975. }
  976. /**
  977. * This function edit a attachment file into announcement
  978. * @param string a comment about file
  979. * @param int Agenda Id
  980. * @param int attachment file Id
  981. */
  982. function edit_announcement_attachment_file($last_id, $file = array(), $file_comment) {
  983. global $_course;
  984. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  985. $return = 0;
  986. // Storing the attachments
  987. if(!empty($_FILES['user_upload'])) {
  988. $upload_ok = process_uploaded_file($_FILES['user_upload']);
  989. }
  990. if (!empty($upload_ok)) {
  991. $courseDir = $_course['path'].'/upload/announcements';
  992. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  993. $updir = $sys_course_path.$courseDir;
  994. // Try to add an extension to the file if it hasn't one
  995. $new_file_name = add_ext_on_mime(stripslashes($_FILES['user_upload']['name']), $_FILES['user_upload']['type']);
  996. // user's file name
  997. $file_name =$_FILES['user_upload'] ['name'];
  998. if (!filter_extension($new_file_name)) {
  999. $return -1;
  1000. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  1001. } else {
  1002. $new_file_name = uniqid('');
  1003. $new_path = $updir.'/'.$new_file_name;
  1004. $result = @move_uploaded_file($_FILES['user_upload']['tmp_name'], $new_path);
  1005. $safe_file_comment = Database::escape_string($file_comment);
  1006. $safe_file_name = Database::escape_string($file_name);
  1007. $safe_new_file_name = Database::escape_string($new_file_name);
  1008. $sql = "UPDATE $tbl_announcement_attachment SET filename = '$safe_file_name', comment = '$safe_file_comment', path = '$safe_new_file_name', announcement_id = '$last_id', size ='".intval($_FILES['user_upload']['size'])."'
  1009. WHERE announcement_id = '$last_id'";
  1010. $result = Database::query($sql, __FILE__,__LINE__);
  1011. if ($result === false) {
  1012. $return = -1;
  1013. Display :: display_error_message(get_lang('UplUnableToSaveFile'));
  1014. } else {
  1015. $return = 1;
  1016. }
  1017. //$message .= ' / '.get_lang('FileUploadSucces').'<br />';
  1018. }
  1019. }
  1020. return $return;
  1021. }
  1022. /**
  1023. * This function delete a attachment file by id
  1024. * @param integer attachment file Id
  1025. *
  1026. */
  1027. function delete_announcement_attachment_file($id) {
  1028. global $_course;
  1029. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1030. $id=Database::escape_string($id);
  1031. $sql="DELETE FROM $tbl_announcement_attachment WHERE id = $id";
  1032. error_log($sql);
  1033. $result=Database::query($sql, __FILE__,__LINE__);
  1034. // update item_property
  1035. //api_item_property_update($_course, 'announcement_attachment', $id,'AnnouncementAttachmentDeleted', api_get_user_id());
  1036. }