announcements.inc.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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 $charset;
  847. global $_user;
  848. foreach ($user_list as $this_user) {
  849. /* Header : Bericht van uw lesgever - GES ($course_code) - Morgen geen les! ($mail_title)
  850. Body : John Doe (prenom + nom) <john_doe@hotmail.com> (email)
  851. Morgen geen les! ($mail_title)
  852. Morgen is er geen les, de les wordt geschrapt wegens vergadering (newContent)
  853. */
  854. $mail_subject = get_lang('professorMessage').' - '.$_course['official_code'].' - '.$mail_title;
  855. $mail_body = '['.$_course['official_code'].'] - ['.$_course['name']."]\n";
  856. $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";
  857. $mail_body .= api_get_person_name($_user['firstName'], $_user['lastName'], null, PERSON_NAME_EMAIL_ADDRESS).' ';
  858. $mail_body .= '<'.$_user['mail'].">\n";
  859. $mail_body .= $_course['official_code'].' '.$_course['name'];
  860. //set the charset and use it for the encoding of the email - small fix, not really clean (should check the content encoding origin first)
  861. //here we use the encoding used for the webpage where the text is encoded (ISO-8859-1 in this case)
  862. if(empty($charset)){$charset='ISO-8859-1';}
  863. $encoding = 'Content-Type: text/plain; charset='. $charset;
  864. $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);
  865. }
  866. }
  867. function update_mail_sent($insert_id)
  868. {
  869. global $_course;
  870. global $tbl_announcement;
  871. if ($insert_id != strval(intval($insert_id))) { return false; }
  872. $insert_id = Database::escape_string($insert_id);
  873. // store the modifications in the table tbl_annoucement
  874. $sql = "UPDATE $tbl_announcement SET email_sent='1' WHERE id='$insert_id'";
  875. Database::query($sql,__FILE__,__LINE__);
  876. }
  877. /**
  878. * Gets all announcements from a user by course
  879. * @param string course db
  880. * @param int user id
  881. * @return array html with the content and count of announcements or false otherwise
  882. */
  883. function get_all_annoucement_by_user_course($course_db, $user_id)
  884. {
  885. if (empty($course_db) || empty($user_id)) {
  886. return false;
  887. }
  888. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT, $course_db);
  889. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY, $course_db);
  890. if (!empty($user_id) && is_numeric($user_id)) {
  891. $user_id = intval($user_id);
  892. $sql="SELECT DISTINCT announcement.title, announcement.content
  893. FROM $tbl_announcement announcement, $tbl_item_property toolitemproperties
  894. WHERE announcement.id = toolitemproperties.ref
  895. AND toolitemproperties.tool='announcement'
  896. AND (toolitemproperties.insert_user_id='$user_id' AND (toolitemproperties.to_group_id='0' OR toolitemproperties.to_group_id is null))
  897. AND toolitemproperties.visibility='1'
  898. AND announcement.session_id = 0
  899. ORDER BY display_order DESC";
  900. $rs = Database::query($sql,__FILE__,__LINE__);
  901. $num_rows = Database::num_rows($rs);
  902. $content = '';
  903. $i=0;
  904. $result = array();
  905. if ($num_rows>0) {
  906. while ($myrow = Database::fetch_array($rs)) {
  907. //if ($i<=4) {
  908. $content.= '<strong>'.$myrow['title'].'</strong><br /><br />';
  909. $content.= $myrow['content'];
  910. /*} else {
  911. break;
  912. }*/
  913. $i++;
  914. }
  915. $result['content'] = $content;
  916. $result['count'] = $i;
  917. return $result;
  918. }
  919. return false;
  920. }
  921. return false;
  922. }
  923. /*
  924. ==============================================================================
  925. ATTACHMENT FUNCTIONS
  926. ==============================================================================
  927. */
  928. /**
  929. * Show a list with all the attachments according to the post's id
  930. * @param int announcement id
  931. * @return array with the post info
  932. * @author Arthur Portugal
  933. * @version November 2009, dokeos 1.8.6.2
  934. */
  935. function get_attachment($announcement_id) {
  936. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  937. $announcement_id=Database::escape_string($announcement_id);
  938. $row=array();
  939. $sql = 'SELECT id,path, filename,comment FROM '. $tbl_announcement_attachment.' WHERE announcement_id = '.(int)$announcement_id.'';
  940. $result=Database::query($sql, __FILE__, __LINE__);
  941. if (Database::num_rows($result)!=0) {
  942. $row=Database::fetch_array($result,ASSOC);
  943. }
  944. return $row;
  945. }
  946. /**
  947. * This function add a attachment file into announcement
  948. * @param int announcement id
  949. * @param string file comment
  950. * @param array uploaded file $_FILES
  951. * @return int -1 if failed, 0 if unknown (should not happen), 1 if success
  952. */
  953. function add_announcement_attachment_file($announcement_id, $file_comment, $file) {
  954. global $_course;
  955. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  956. $return = 0;
  957. $announcement_id = intval($announcement_id);
  958. if (is_array($file) && $file['error'] == 0 ) {
  959. $courseDir = $_course['path'].'/upload/announcements';
  960. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  961. $updir = $sys_course_path.$courseDir;
  962. // Try to add an extension to the file if it hasn't one
  963. $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']);
  964. // user's file name
  965. $file_name = $file['name'];
  966. if (!filter_extension($new_file_name)) {
  967. $return = -1;
  968. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  969. } else {
  970. $new_file_name = uniqid('');
  971. $new_path = $updir.'/'.$new_file_name;
  972. $result = @move_uploaded_file($file['tmp_name'], $new_path);
  973. $safe_file_comment = Database::escape_string($file_comment);
  974. $safe_file_name = Database::escape_string($file_name);
  975. $safe_new_file_name = Database::escape_string($new_file_name);
  976. // Storing the attachments if any
  977. $sql = 'INSERT INTO '.$tbl_announcement_attachment.'(filename, comment, path, announcement_id, size) '.
  978. "VALUES ( '$safe_file_name', '$file_comment', '$safe_new_file_name' , '$announcement_id', '".intval($file['size'])."' )";
  979. $result = Database::query($sql, __LINE__, __FILE__);
  980. $return = 1;
  981. }
  982. }
  983. return $return;
  984. }
  985. /**
  986. * This function edit a attachment file into announcement
  987. * @param int attach id
  988. * @param array uploaded file $_FILES
  989. * @param string file comment
  990. * @return int
  991. */
  992. function edit_announcement_attachment_file($id_attach, $file, $file_comment) {
  993. global $_course;
  994. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  995. $return = 0;
  996. if (is_array($file) && $file['error'] == 0 ) {
  997. $courseDir = $_course['path'].'/upload/announcements';
  998. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  999. $updir = $sys_course_path.$courseDir;
  1000. // Try to add an extension to the file if it hasn't one
  1001. $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']);
  1002. // user's file name
  1003. $file_name =$file ['name'];
  1004. if (!filter_extension($new_file_name)) {
  1005. $return -1;
  1006. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  1007. } else {
  1008. $new_file_name = uniqid('');
  1009. $new_path = $updir.'/'.$new_file_name;
  1010. $result = @move_uploaded_file($file['tmp_name'], $new_path);
  1011. $safe_file_comment = Database::escape_string($file_comment);
  1012. $safe_file_name = Database::escape_string($file_name);
  1013. $safe_new_file_name = Database::escape_string($new_file_name);
  1014. $id_attach = intval($id_attach);
  1015. $sql = "UPDATE $tbl_announcement_attachment SET filename = '$safe_file_name', comment = '$safe_file_comment', path = '$safe_new_file_name', size ='".intval($file['size'])."'
  1016. WHERE id = '$id_attach'";
  1017. $result = Database::query($sql, __FILE__,__LINE__);
  1018. if ($result === false) {
  1019. $return = -1;
  1020. Display :: display_error_message(get_lang('UplUnableToSaveFile'));
  1021. } else {
  1022. $return = 1;
  1023. }
  1024. }
  1025. }
  1026. return $return;
  1027. }
  1028. /**
  1029. * This function delete a attachment file by id
  1030. * @param integer attachment file Id
  1031. *
  1032. */
  1033. function delete_announcement_attachment_file($id) {
  1034. global $_course;
  1035. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1036. $id=Database::escape_string($id);
  1037. $sql="DELETE FROM $tbl_announcement_attachment WHERE id = $id";
  1038. error_log($sql);
  1039. $result=Database::query($sql, __FILE__,__LINE__);
  1040. // update item_property
  1041. //api_item_property_update($_course, 'announcement_attachment', $id,'AnnouncementAttachmentDeleted', api_get_user_id());
  1042. }