myagenda.inc.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. <?php //$Id: agenda.php 16490 2008-10-10 14:29:52Z elixir_inter $
  2. /* For licensing terms, see /dokeos_license.txt
  3. ==============================================================================
  4. @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  5. @author: Toon Van Hoecke <toon.vanhoecke@ugent.be>, Ghent University
  6. @author: Eric Remy (initial version)
  7. @version: 2.2 alpha
  8. @description: this file generates a general agenda of all items of the
  9. courses the user is registered for
  10. ==============================================================================
  11. version info:
  12. -------------
  13. -> version 2.2 : Patrick Cool, patrick.cool@ugent.be, november 2004
  14. Personal Agenda added. The user can add personal agenda items. The items
  15. are stored in a dokeos_user database because it is not course or platform
  16. based. A personal agenda view was also added. This lists all the personal
  17. agenda items of that user.
  18. -> version 2.1 : Patrick Cool, patrick.cool@ugent.be, , oktober 2004
  19. This is the version that works with the Group based Agenda tool.
  20. -> version 2.0 (alpha): Patrick Cool, patrick.cool@ugent.be, , oktober 2004
  21. The 2.0 version introduces besides the month view also a week- and day view.
  22. In the 2.5 (final) version it will be possible for the student to add his/her
  23. own agenda items. The platform administrator can however decide if the students
  24. are allowed to do this or not.
  25. The alpha version only contains the three views. The personal agenda feature is
  26. not yet completely finished. There are however already some parts of the code
  27. for adding a personal agenda item present.
  28. this code was not released in an official dokeos but was only used in the offical
  29. server of the Ghent University where it underwent serious testing
  30. -> version 1.5: Toon Van Hoecke, toon.vanhoecke@ugent.be, december 2003
  31. -> version 1.0: Eric Remy, eremy@rmwc.edu, 6 Oct 2003
  32. The tool was initially called master-calendar as it collects all the calendar
  33. items of all the courses one is subscribed to. It was very soon integrated in
  34. Dokeos as this was a really basic and very usefull tool.
  35. */
  36. /**
  37. * Settings (you may alter this at will
  38. */
  39. $setting_agenda_link = 'coursecode'; // valid values are coursecode and icon
  40. /**
  41. * This function retrieves all the agenda items of all the courses the user is subscribed to
  42. */
  43. function get_myagendaitems($courses_dbs, $month, $year)
  44. {
  45. global $_user;
  46. global $_configuration;
  47. global $setting_agenda_link;
  48. $items = array ();
  49. // get agenda-items for every course
  50. foreach ($courses_dbs as $key => $array_course_info)
  51. {
  52. //databases of the courses
  53. $TABLEAGENDA = Database :: get_course_table(TABLE_AGENDA, $array_course_info["db"]);
  54. $TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY, $array_course_info["db"]);
  55. $group_memberships = GroupManager :: get_group_ids($array_course_info["db"], $_user['user_id']);
  56. // if the user is administrator of that course we show all the agenda items
  57. if ($array_course_info['status'] == '1')
  58. {
  59. //echo "course admin";
  60. $sqlquery = "SELECT
  61. DISTINCT agenda.*, item_property.*
  62. FROM ".$TABLEAGENDA." agenda,
  63. ".$TABLE_ITEMPROPERTY." item_property
  64. WHERE agenda.id = item_property.ref
  65. AND MONTH(agenda.start_date)='".$month."'
  66. AND YEAR(agenda.start_date)='".$year."'
  67. AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  68. AND item_property.visibility='1'
  69. GROUP BY agenda.id
  70. ORDER BY start_date ";
  71. }
  72. // if the user is not an administrator of that course
  73. else
  74. {
  75. //echo "GEEN course admin";
  76. if (is_array($group_memberships) && count($group_memberships)>0)
  77. {
  78. $sqlquery = "SELECT
  79. agenda.*, item_property.*
  80. FROM ".$TABLEAGENDA." agenda,
  81. ".$TABLE_ITEMPROPERTY." item_property
  82. WHERE agenda.id = item_property.ref
  83. AND MONTH(agenda.start_date)='".$month."'
  84. AND YEAR(agenda.start_date)='".$year."'
  85. AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  86. AND ( item_property.to_user_id='".$_user['user_id']."' OR item_property.to_group_id IN (0, ".implode(", ", $group_memberships).") )
  87. AND item_property.visibility='1'
  88. ORDER BY start_date ";
  89. }
  90. else
  91. {
  92. $sqlquery = "SELECT
  93. agenda.*, item_property.*
  94. FROM ".$TABLEAGENDA." agenda,
  95. ".$TABLE_ITEMPROPERTY." item_property
  96. WHERE agenda.id = item_property.ref
  97. AND MONTH(agenda.start_date)='".$month."'
  98. AND YEAR(agenda.start_date)='".$year."'
  99. AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  100. AND ( item_property.to_user_id='".$_user['user_id']."' OR item_property.to_group_id='0')
  101. AND item_property.visibility='1'
  102. ORDER BY start_date ";
  103. }
  104. }
  105. $result = Database::query($sqlquery, __FILE__, __LINE__);
  106. while ($item = Database::fetch_array($result)) {
  107. $agendaday = date("j",strtotime($item['start_date']));
  108. if(!isset($items[$agendaday])){$items[$agendaday]=array();}
  109. $time= date("H:i",strtotime($item['start_date']));
  110. $end_time= date("H:i",strtotime($item['end_date']));
  111. $URL = api_get_path(WEB_PATH)."main/calendar/agenda.php?cidReq=".urlencode($array_course_info["code"])."&amp;day=$agendaday&amp;month=$month&amp;year=$year#$agendaday"; // RH //Patrick Cool: to highlight the relevant agenda item
  112. if ($setting_agenda_link == 'coursecode') {
  113. $title=$array_course_info['title'];
  114. $agenda_link = api_substr($title, 0, 14);
  115. }
  116. else {
  117. $agenda_link = Display::return_icon('course_home.gif');
  118. }
  119. if(!isset($items[$agendaday][$item['start_date']])) {
  120. $items[$agendaday][$item['start_date']] = '';
  121. }
  122. $items[$agendaday][$item['start_date']] .= "".get_lang('StartTimeWindow')."&nbsp;<i>".$time."</i>"."&nbsp;-&nbsp;".get_lang("EndTimeWindow")."&nbsp;<i>".$end_time."</i>&nbsp;";
  123. $items[$agendaday][$item['start_date']] .= '<br />'."<a href=\"$URL\" title=\"".Security::remove_XSS($array_course_info['title'])."\">".$agenda_link."</a> ".Security::remove_XSS($item['title'])."<br /> ";
  124. $items[$agendaday][$item['start_date']] .= '<br/>';
  125. }
  126. }
  127. // sorting by hour for every day
  128. $agendaitems = array ();
  129. while (list ($agendaday, $tmpitems) = each($items)) {
  130. if(!isset($agendaitems[$agendaday])) {
  131. $agendaitems[$agendaday] = '';
  132. }
  133. sort($tmpitems);
  134. while (list ($key, $val) = each($tmpitems)) {
  135. $agendaitems[$agendaday] .= $val;
  136. }
  137. }
  138. //print_r($agendaitems);
  139. return $agendaitems;
  140. }
  141. /**
  142. * Show the monthcalender of the given month
  143. * @param array Agendaitems
  144. * @param int Month number
  145. * @param int Year number
  146. * @param array Array of strings containing long week day names (deprecated, you can send an empty array instead)
  147. * @param string The month name
  148. * @return void Direct output
  149. */
  150. function display_mymonthcalendar($agendaitems, $month, $year, $weekdaynames=array(), $monthName)
  151. {
  152. global $DaysShort,$course_path;
  153. //Handle leap year
  154. $numberofdays = array (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  155. if (($year % 400 == 0) or ($year % 4 == 0 and $year % 100 <> 0))
  156. $numberofdays[2] = 29;
  157. //Get the first day of the month
  158. $dayone = getdate(mktime(0, 0, 0, $month, 1, $year));
  159. //Start the week on monday
  160. $startdayofweek = $dayone['wday'] <> 0 ? ($dayone['wday'] - 1) : 6;
  161. $g_cc = (isset($_GET['courseCode'])?$_GET['courseCode']:'');
  162. $backwardsURL = api_get_self()."?coursePath=".urlencode($course_path)."&amp;courseCode=".Security::remove_XSS($g_cc)."&amp;action=view&amp;view=month&amp;month=". ($month == 1 ? 12 : $month -1)."&amp;year=". ($month == 1 ? $year -1 : $year);
  163. $forewardsURL = api_get_self()."?coursePath=".urlencode($course_path)."&amp;courseCode=".Security::remove_XSS($g_cc)."&amp;action=view&amp;view=month&amp;month=". ($month == 12 ? 1 : $month +1)."&amp;year=". ($month == 12 ? $year +1 : $year);
  164. echo "<table class=\"data_table\">\n", "<tr>\n", "<th width=\"10%\"><a href=\"", $backwardsURL, "\">&#171;</a></th>\n", "<th width=\"80%\" colspan=\"5\">", $monthName, " ", $year, "</th>\n", "<th width=\"10%\"><a href=\"", $forewardsURL, "\">&#187;</a></th>\n", "</tr>\n";
  165. echo "<tr>\n";
  166. for ($ii = 1; $ii < 8; $ii ++)
  167. {
  168. echo "<td class=\"weekdays\">", $DaysShort[$ii % 7], "</td>\n";
  169. }
  170. echo "</tr>\n";
  171. $curday = -1;
  172. $today = getdate();
  173. while ($curday <= $numberofdays[$month]) {
  174. echo "<tr>\n";
  175. for ($ii = 0; $ii < 7; $ii ++) {
  176. if (($curday == -1) && ($ii == $startdayofweek)) {
  177. $curday = 1;
  178. }
  179. if (($curday > 0) && ($curday <= $numberofdays[$month])) {
  180. $bgcolor = $ii < 5 ? $class = "class=\"days_week\" style=\"width:10%;\"" : $class = "class=\"days_weekend\" style=\"width:10%;\"";
  181. $dayheader = "<b>$curday</b><br />";
  182. if (($curday == $today['mday']) && ($year == $today['year']) && ($month == $today['mon'])) {
  183. $dayheader = "<b>$curday - ".get_lang("Today")."</b><br />";
  184. $class = "class=\"days_today\" style=\"width:10%;\"";
  185. }
  186. echo "<td ".$class.">", "".$dayheader;
  187. if (!empty($agendaitems[$curday])) {
  188. echo "<span class=\"agendaitem\">".$agendaitems[$curday]."</span>";
  189. }
  190. echo "</td>\n";
  191. $curday ++;
  192. } else {
  193. echo "<td>&nbsp;</td>\n";
  194. }
  195. }
  196. echo "</tr>\n";
  197. }
  198. echo "</table>\n";
  199. }
  200. /**
  201. * Show the mini calender of the given month
  202. */
  203. function display_myminimonthcalendar($agendaitems, $month, $year, $monthName)
  204. {
  205. global $DaysShort,$course_path;
  206. //Handle leap year
  207. $numberofdays = array (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  208. if (($year % 400 == 0) or ($year % 4 == 0 and $year % 100 <> 0))
  209. $numberofdays[2] = 29;
  210. //Get the first day of the month
  211. $dayone = getdate(mktime(0, 0, 0, $month, 1, $year));
  212. //Start the week on monday
  213. $startdayofweek = $dayone['wday'] <> 0 ? ($dayone['wday'] - 1) : 6;
  214. $g_cc = (isset($_GET['courseCode'])?$_GET['courseCode']:'');
  215. $backwardsURL = api_get_self()."?coursePath=".urlencode($course_path)."&amp;courseCode=".Security::remove_XSS($g_cc)."&amp;month=". ($month == 1 ? 12 : $month -1)."&amp;year=". ($month == 1 ? $year -1 : $year);
  216. $forewardsURL = api_get_self()."?coursePath=".urlencode($course_path)."&amp;courseCode=".Security::remove_XSS($g_cc)."&amp;month=". ($month == 12 ? 1 : $month +1)."&amp;year=". ($month == 12 ? $year +1 : $year);
  217. echo "<table class=\"data_table\">\n", "<tr>\n", "<th width=\"10%\"><a href=\"", $backwardsURL, "\">&#171;</a></th>\n", "<th width=\"80%\" colspan=\"5\">", $monthName, " ", $year, "</th>\n", "<th width=\"10%\"><a href=\"", $forewardsURL, "\">&#187;</a></th>\n", "</tr>\n";
  218. echo "<tr>\n";
  219. for ($ii = 1; $ii < 8; $ii ++)
  220. {
  221. echo "<td class=\"weekdays\">", $DaysShort[$ii % 7], "</td>\n";
  222. }
  223. echo "</tr>\n";
  224. $curday = -1;
  225. $today = getdate();
  226. while ($curday <= $numberofdays[$month])
  227. {
  228. echo "<tr>\n";
  229. for ($ii = 0; $ii < 7; $ii ++)
  230. {
  231. if (($curday == -1) && ($ii == $startdayofweek))
  232. {
  233. $curday = 1;
  234. }
  235. if (($curday > 0) && ($curday <= $numberofdays[$month]))
  236. {
  237. $bgcolor = $ii < 5 ? $class = "class=\"days_week\"" : $class = "class=\"days_weekend\"";
  238. $dayheader = "$curday";
  239. if (($curday == $today['mday']) && ($year == $today['year']) && ($month == $today['mon']))
  240. {
  241. $dayheader = "$curday";
  242. $class = "class=\"days_today\"";
  243. }
  244. echo "\t<td ".$class.">";
  245. if (!empty($agendaitems[$curday]))
  246. {
  247. echo "<a href=\"".api_get_self()."?action=view&amp;view=day&amp;day=".$curday."&amp;month=".$month."&amp;year=".$year."\">".$dayheader."</a>";
  248. }
  249. else
  250. {
  251. echo $dayheader;
  252. }
  253. // "a".$dayheader." <span class=\"agendaitem\">".$agendaitems[$curday]."</span>\n";
  254. echo "</td>\n";
  255. $curday ++;
  256. }
  257. else
  258. {
  259. echo "<td>&nbsp;</td>\n";
  260. }
  261. }
  262. echo "</tr>\n";
  263. }
  264. echo "</table>\n";
  265. }
  266. /**
  267. * This function shows all the forms that are needed form adding /editing a new personal agenda item
  268. * when there is no $id passed in the function we are adding a new agenda item, if there is a $id
  269. * we are editing
  270. * attention: we have to check that the student is editing an item that belongs to him/her
  271. */
  272. function show_new_personal_item_form($id = "")
  273. {
  274. global $year, $MonthsLong;
  275. global $tbl_personal_agenda;
  276. global $_user;
  277. // we construct the default time and date data (used if we are not editing a personal agenda item)
  278. $today = getdate();
  279. $day = $today['mday'];
  280. $month = $today['mon'];
  281. $year = $today['year'];
  282. $hours = $today['hours'];
  283. $minutes = $today['minutes'];
  284. // if an $id is passed to this function this means we are editing an item
  285. // we are loading the information here (we do this after everything else
  286. // to overwrite the default information)
  287. if (strlen($id) > 0 && $id != strval(intval($id))) {
  288. return false; //potential SQL injection
  289. }
  290. if ($id <> "")
  291. {
  292. $sql = "SELECT * FROM ".$tbl_personal_agenda." WHERE user='".$_user['user_id']."' AND id='".$id."'";
  293. $result = Database::query($sql, __FILE__, __LINE__);
  294. $aantal = Database::num_rows($result);
  295. if ($aantal <> 0)
  296. {
  297. $row = Database::fetch_array($result);
  298. $year = substr($row['date'], 0, 4);
  299. $month = substr($row['date'], 5, 2);
  300. $day = substr($row['date'], 8, 2);
  301. $hours = substr($row['date'], 11, 2);
  302. $minutes = substr($row['date'], 14, 2);
  303. $title = $row['title'];
  304. $text = $row['text'];
  305. }
  306. else
  307. {
  308. return false;
  309. }
  310. }
  311. echo "<form method=\"post\" action=\"myagenda.php?action=add_personal_agenda_item&amp;id=$id\" name=\"newedit_form\">\n";
  312. echo "<table width=\"100%\" id=\"newedit_form\">\n";
  313. echo "\t<tr class=\"title\">\n\t\t<td colspan=\"3\"><h4>";
  314. echo ($_GET['action'] == 'edit_personal_agenda_item') ? get_lang("ModifyPersonalCalendarItem") : get_lang("AddPersonalCalendarItem");
  315. echo "</h4></td>\n\t</tr>\n";
  316. echo "\t<tr class=\"subtitle\">\n\t\t<td>\n";
  317. echo "<!-- date: 1 -> 31 -->\n";
  318. echo "\t\t".get_lang("Date").": \n";
  319. // ********** The form containing the days (0->31) ********** \\
  320. echo "<select name=\"frm_day\">\n";
  321. // small loop for filling all the dates
  322. // 2do: the available dates should be those of the selected month => february is from 1 to 28 (or 29) and not to 31
  323. for ($i = 1; $i <= 31; $i ++)
  324. {
  325. // values have to have double digits
  326. if ($i <= 9)
  327. {
  328. $value = "0".$i;
  329. }
  330. else
  331. {
  332. $value = $i;
  333. }
  334. // the current day is indicated with [] around the date
  335. if ($value == $day)
  336. {
  337. echo "\t\t\t\t <option value=\"".$value."\" selected>".$i."</option>\n";
  338. }
  339. else
  340. {
  341. echo "\t\t\t\t <option value=\"".$value."\">".$i."</option>\n";
  342. }
  343. }
  344. echo "</select>\n\n";
  345. // ********** The form containing the months (jan->dec) ********** \\
  346. echo "<!-- month: january -> december -->\n";
  347. echo "<select name=\"frm_month\">\n";
  348. for ($i = 1; $i <= 12; $i ++)
  349. {
  350. // values have to have double digits
  351. if ($i <= 9)
  352. {
  353. $value = "0".$i;
  354. }
  355. else
  356. {
  357. $value = $i;
  358. }
  359. // the current month is indicated with [] around the month name
  360. if ($value == $month)
  361. {
  362. echo "\t<option value=\"".$value."\" selected>".$MonthsLong[$i -1]."</option>\n";
  363. }
  364. else
  365. {
  366. echo "\t<option value=\"".$value."\">".$MonthsLong[$i -1]."</option>\n";
  367. }
  368. }
  369. echo "</select>\n\n";
  370. // ********** The form containing the years ********** \\
  371. echo "<!-- year -->\n";
  372. echo "<select name=\"frm_year\">";
  373. echo "<option value=\"". ($year -1)."\">". ($year -1)."</option>\n";
  374. echo "<option value=\"".$year."\" selected>".$year."</option>\n";
  375. for ($i = 1; $i <= 5; $i ++)
  376. {
  377. $value = $year + $i;
  378. echo "\t<option value=\"".$value."\">".$value."</option>\n";
  379. }
  380. echo "</select>";
  381. echo "<a title=\"Kalender\" href=\"javascript:openCalendar('newedit_form', 'frm_')\">".Display::return_icon('calendar_select.gif', get_lang('Select'), array ('style' => 'vertical-align: middle;'))."</a>";
  382. echo "&nbsp;&nbsp;";
  383. // ********** The form containing the hours (00->23) ********** \\
  384. echo "<!-- time: hour -->\n";
  385. echo get_lang("Time").": \n";
  386. echo "<select name=\"frm_hour\">\n";
  387. for ($i = 1; $i <= 24; $i ++)
  388. {
  389. // values have to have double digits
  390. if ($i <= 9)
  391. {
  392. $value = "0".$i;
  393. }
  394. else
  395. {
  396. $value = $i;
  397. }
  398. // the current hour is indicated with [] around the hour
  399. if ($hours == $value)
  400. {
  401. echo "\t\t\t\t<option value=\"".$value."\" selected> ".$value." </option>\n";
  402. }
  403. else
  404. {
  405. echo "\t\t\t\t<option value=\"".$value."\"> ".$value." </option>\n";
  406. }
  407. }
  408. echo "</select>";
  409. // ********** The form containing the minutes ********** \\
  410. echo "<select name=\"frm_minute\">";
  411. echo "<option value=\"".$minutes."\">".$minutes."</option>";
  412. echo "<option value=\"00\">00</option>";
  413. echo "<option value=\"05\">05</option>";
  414. echo "<option value=\"10\">10</option>";
  415. echo "<option value=\"15\">15</option>";
  416. echo "<option value=\"20\">20</option>";
  417. echo "<option value=\"25\">25</option>";
  418. echo "<option value=\"30\">30</option>";
  419. echo "<option value=\"35\">35</option>";
  420. echo "<option value=\"40\">40</option>";
  421. echo "<option value=\"45\">45</option>";
  422. echo "<option value=\"50\">50</option>";
  423. echo "<option value=\"55\">55</option>";
  424. echo "</select>";
  425. echo "</td></tr>";
  426. // ********** The title field ********** \\
  427. echo "<tr class=\"subtitle\"><td colspan=\"2\">";
  428. echo get_lang('Title').': <input type="text" name="frm_title" size="50" value="'.$title.'" />';
  429. echo "</td></tr>";
  430. // ********** The text field ********** \\
  431. echo "<tr><td colspan=\"2\">";
  432. //api_disp_html_area('frm_content', $text, '300px');
  433. echo'<textarea name="frm_content" style="width: 450px; height: 100px;">'.$text.'</textarea>';
  434. echo "</td></tr>";
  435. // ********** The Submit button********** \\
  436. echo "<tr><td colspan=\"2\">";
  437. echo '<button type="submit" class="add" name="Submit" value="'.get_lang('AddEvent').'" >'.get_lang('AddEvent').'</button>';
  438. echo "</td></tr>";
  439. echo "</table>\n</form>\n";
  440. }
  441. /**
  442. * This function shows all the forms that are needed form adding a new personal agenda item
  443. */
  444. function store_personal_item($day, $month, $year, $hour, $minute, $title, $content, $id = "")
  445. {
  446. global $tbl_personal_agenda;
  447. global $_user;
  448. //constructing the date
  449. $date = $year."-".$month."-".$day." ".$hour.":".$minute.":00";
  450. $date = Database::escape_string($date);
  451. $title = Database::escape_string($title);
  452. $content = Database::escape_string($content);
  453. if ($id != strval(intval($id))) {
  454. return false; //potential SQL injection
  455. }
  456. if ($id <> "")
  457. { // we are updating
  458. $sql = "UPDATE ".$tbl_personal_agenda." SET user='".$_user['user_id']."', title='".$title."', text='".$content."', date='".$date."' WHERE id='".$id."'";
  459. }
  460. else
  461. { // we are adding a new item
  462. $sql = "INSERT INTO $tbl_personal_agenda (user, title, text, date) VALUES ('".$_user['user_id']."','$title', '$content', '$date')";
  463. }
  464. $result = Database::query($sql, __FILE__, __LINE__);
  465. }
  466. /**
  467. * This function finds all the courses (also those of sessions) of the user and returns an array containing the
  468. * database name of the courses.
  469. * Xritten by Noel Dieschburg <noel.dieschburg@dokeos.com>
  470. */
  471. function get_all_courses_of_user()
  472. {
  473. global $TABLECOURS;
  474. global $TABLECOURSUSER;
  475. global $_user;
  476. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  477. $tbl_session_course_user= Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  478. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  479. $sql_select_courses = "SELECT c.code k, c.visual_code vc, c.title i, c.tutor_name t,
  480. c.db_name db, c.directory dir, '5' as status
  481. FROM $TABLECOURS c, $tbl_session_course_user srcu
  482. WHERE srcu.id_user='".$_user['user_id']."'
  483. AND c.code=srcu.course_code
  484. UNION
  485. SELECT c.code k, c.visual_code vc, c.title i, c.tutor_name t,
  486. c.db_name db, c.directory dir, cru.status status
  487. FROM $TABLECOURS c, $TABLECOURSUSER cru
  488. WHERE cru.user_id='".$_user['user_id']."'
  489. AND c.code=cru.course_code";
  490. $result = Database::query($sql_select_courses);
  491. while ($row = Database::fetch_array($result))
  492. {
  493. // we only need the database name of the course
  494. $courses[] = array ("db" => $row['db'], "code" => $row['k'], "visual_code" => $row['vc'], "title" => $row['i'], "directory" => $row['dir'], "status" => $row['status']);
  495. }
  496. return $courses;
  497. }
  498. /**
  499. * This function finds all the courses of the user and returns an array containing the
  500. * database name of the courses.
  501. */
  502. function get_courses_of_user()
  503. {
  504. global $TABLECOURS;
  505. global $TABLECOURSUSER;
  506. global $_user;
  507. $sql_select_courses = "SELECT course.code k, course.visual_code vc,
  508. course.title i, course.tutor_name t, course.db_name db, course.directory dir, course_rel_user.status status
  509. FROM $TABLECOURS course,
  510. $TABLECOURSUSER course_rel_user
  511. WHERE course.code = course_rel_user.course_code
  512. AND course_rel_user.user_id = '".$_user['user_id']."'";
  513. $result = Database::query($sql_select_courses);
  514. while ($row = Database::fetch_array($result))
  515. {
  516. // we only need the database name of the course
  517. $courses[] = array ("db" => $row['db'], "code" => $row['k'], "visual_code" => $row['vc'], "title" => $row['i'], "directory" => $row['dir'], "status" => $row['status']);
  518. }
  519. return $courses;
  520. }
  521. /**
  522. * This function retrieves all the personal agenda items and add them to the agenda items found by the other functions.
  523. */
  524. function get_personal_agenda_items($agendaitems, $day = "", $month = "", $year = "", $week = "", $type)
  525. {
  526. global $tbl_personal_agenda;
  527. global $_user;
  528. global $_configuration;
  529. // 1. creating the SQL statement for getting the personal agenda items in MONTH view
  530. if ($type == "month_view" or $type == "") // we are in month view
  531. {
  532. $sql = "SELECT * FROM ".$tbl_personal_agenda." WHERE user='".$_user['user_id']."' and MONTH(date)='".$month."' AND YEAR(date) = '".$year."' ORDER BY date ASC";
  533. }
  534. // 2. creating the SQL statement for getting the personal agenda items in WEEK view
  535. if ($type == "week_view") // we are in week view
  536. {
  537. $start_end_day_of_week = calculate_start_end_of_week($week, $year);
  538. $start_day = $start_end_day_of_week['start']['day'];
  539. $start_month = $start_end_day_of_week['start']['month'];
  540. $start_year = $start_end_day_of_week['start']['year'];
  541. $end_day = $start_end_day_of_week['end']['day'];
  542. $end_month = $start_end_day_of_week['end']['month'];
  543. $end_year = $start_end_day_of_week['end']['year'];
  544. // in sql statements you have to use year-month-day for date calculations
  545. $start_filter = $start_year."-".$start_month."-".$start_day." 00:00:00";
  546. $end_filter = $end_year."-".$end_month."-".$end_day." 23:59:59";
  547. $sql = " SELECT * FROM ".$tbl_personal_agenda." WHERE user='".$_user['user_id']."'
  548. AND date>='".$start_filter."' AND date<='".$end_filter."'";
  549. }
  550. // 3. creating the SQL statement for getting the personal agenda items in DAY view
  551. if ($type == "day_view") // we are in day view
  552. {
  553. // we could use mysql date() function but this is only available from 4.1 and higher
  554. $start_filter = $year."-".$month."-".$day." 00:00:00";
  555. $end_filter = $year."-".$month."-".$day." 23:59:59";
  556. $sql = " SELECT * FROM ".$tbl_personal_agenda." WHERE user='".$_user['user_id']."' AND date>='".$start_filter."' AND date<='".$end_filter."'";
  557. }
  558. //echo "day:".$day."/";
  559. //echo "month:".$month."/";
  560. //echo "year:".$year."/";
  561. //echo "week:".$week."/";
  562. //echo $type."<p>";
  563. //echo "<pre>".$sql."</pre>";
  564. global $_configuration;
  565. $root_url = $_configuration['root_web'];
  566. if ($_configuration['multiple_access_urls']==true) {
  567. $access_url_id = api_get_current_access_url_id();
  568. if ($access_url_id != -1 ){
  569. $url = api_get_access_url($access_url_id);
  570. $root_url = $url['url'];
  571. }
  572. }
  573. $result = Database::query($sql, __FILE__, __LINE__);
  574. while ($item = Database::fetch_array($result))
  575. {
  576. // we break the date field in the database into a date and a time part
  577. $agenda_db_date = explode(" ", $item[date]);
  578. $date = $agenda_db_date[0];
  579. $time = $agenda_db_date[1];
  580. // we divide the date part into a day, a month and a year
  581. $agendadate = explode("-", $date);
  582. $year = intval($agendadate[0]);
  583. $month = intval($agendadate[1]);
  584. $day = intval($agendadate[2]);
  585. // we divide the time part into hour, minutes, seconds
  586. $agendatime = explode(":", $time);
  587. $hour = $agendatime[0];
  588. $minute = $agendatime[1];
  589. $second = $agendatime[2];
  590. // if the student has specified a course we a add a link to that course
  591. if ($item['course'] <> "")
  592. {
  593. $url = $root_url."main/calendar/agenda.php?cidReq=".urlencode($item['course'])."&amp;day=$day&amp;month=$month&amp;year=$year#$day"; // RH //Patrick Cool: to highlight the relevant agenda item
  594. $course_link = "<a href=\"$url\" title=\"".$item['course']."\">".$item['course']."</a>";
  595. }
  596. else
  597. {
  598. $course_link = "";
  599. }
  600. // Creating the array that will be returned. If we have week or month view we have an array with the date as the key
  601. // if we have a day_view we use a half hour as index => key 33 = 16h30
  602. if ($type !== "day_view") // This is the array construction for the WEEK or MONTH view
  603. {
  604. //Display events in agenda
  605. $agendaitems[$day] .= "<div><i>$hour:$minute</i> $course_link <a href=\"myagenda.php?action=view&amp;view=personal&amp;day=$day&amp;month=$month&amp;year=$year&amp;id=".$item['id']."#".$item['id']."\" class=\"personal_agenda\">".$item['title']."</a></div><br />";
  606. }
  607. else // this is the array construction for the DAY view
  608. {
  609. $halfhour = 2 * $agendatime['0'];
  610. if ($agendatime['1'] >= '30')
  611. {
  612. $halfhour = $halfhour +1;
  613. }
  614. //Display events by list
  615. $agendaitems[$halfhour] .= "<div><i>$hour:$minute</i> $course_link <a href=\"myagenda.php?action=view&amp;view=personal&amp;day=$day&amp;month=$month&amp;year=$year&amp;id=".$item['id']."#".$item['id']."\" class=\"personal_agenda\">".$item['title']."</a></div>";
  616. }
  617. }
  618. //print_r($agendaitems);
  619. return $agendaitems;
  620. }
  621. /**
  622. * This function retrieves one personal agenda item returns it.
  623. * @param int The agenda item ID
  624. * @return array The results of the database query, or null if not found
  625. */
  626. function get_personal_agenda_item($id)
  627. {
  628. $tbl_personal_agenda = Database :: get_user_personal_table(TABLE_PERSONAL_AGENDA);
  629. $id = Database::escape_string($id);
  630. // make sure events of the personal agenda can only be seen by the user himself
  631. $user = api_get_user_id();
  632. $sql = " SELECT * FROM ".$tbl_personal_agenda." WHERE id=".$id." AND user = ".$user;
  633. $result = Database::query($sql, __FILE__, __LINE__);
  634. if(Database::num_rows($result)==1)
  635. {
  636. $item = Database::fetch_array($result);
  637. }
  638. else
  639. {
  640. $item = null;
  641. }
  642. return $item;
  643. }
  644. /**
  645. * This function retrieves all the personal agenda items of the user and shows
  646. * these items in one list (ordered by date and grouped by month (the month_bar)
  647. */
  648. function show_personal_agenda()
  649. {
  650. global $tbl_personal_agenda;
  651. global $MonthsLong, $charset;
  652. global $_user;
  653. // The SQL statement that retrieves all the personal agenda items of this user
  654. $sql = "SELECT * FROM ".$tbl_personal_agenda." WHERE user='".$_user['user_id']."' ORDER BY date DESC";
  655. $result = Database::query($sql, __FILE__, __LINE__);
  656. // variable initialisation
  657. $month_bar = "";
  658. // setting the default day, month and year
  659. if (!$_GET['day'] AND !$_GET['month'] AND !$_GET['year'])
  660. {
  661. $today = getdate();
  662. $year = $today['year'];
  663. $month = $today['mon'];
  664. $day = $today['mday'];
  665. }
  666. $export_icon = 'export.png';
  667. $export_icon_low = 'export_low_fade.png';
  668. $export_icon_high = 'export_high_fade.png';
  669. // starting the table output
  670. echo "<table class=\"data_table\">\n";
  671. if (Database::num_rows($result) > 0)
  672. {
  673. while ($myrow = Database::fetch_array($result))
  674. {
  675. /*--------------------------------------------------
  676. display: the month bar
  677. --------------------------------------------------*/
  678. if ($month_bar != date("m", strtotime($myrow["date"])).date("Y", strtotime($myrow["date"])))
  679. {
  680. $month_bar = date("m", strtotime($myrow["date"])).date("Y", strtotime($myrow["date"]));
  681. echo "<tr><th class=\"title\" colspan=\"2\" class=\"month\" valign=\"top\">".$MonthsLong[date("n", strtotime($myrow["date"])) - 1]." ".date("Y", strtotime($myrow["date"]))."</th></tr>\n";
  682. }
  683. // highlight: if a date in the small calendar is clicked we highlight the relevant items
  684. $db_date = (int) date("d", strtotime($myrow["date"])).date("n", strtotime($myrow["date"])).date("Y", strtotime($myrow["date"]));
  685. if ($_GET["day"].$_GET["month"].$_GET["year"] <> $db_date)
  686. {
  687. $style = "data";
  688. $text_style = "text";
  689. }
  690. else
  691. {
  692. $style = "datanow";
  693. $text_style = "text";
  694. }
  695. /*--------------------------------------------------
  696. display: date and time
  697. --------------------------------------------------*/
  698. echo "\t<tr>\n\t\t";
  699. echo '<td class="'.$style.'">';
  700. // adding an internal anchor
  701. echo "<a name=\"".$myrow["id"]."\"></a>";
  702. echo date("d", strtotime($myrow["date"]))." ".$MonthsLong[date("n", strtotime($myrow["date"])) - 1]." ".date("Y", strtotime($myrow["date"]))."&nbsp;";
  703. echo api_ucfirst(strftime(get_lang("timeNoSecFormat"), strtotime($myrow["date"])));
  704. echo "</td>";
  705. echo '<td></td>'; //remove when enabling ical
  706. //echo '<td class="'.$style.'">';
  707. //echo '<a class="ical_export" href="ical_export.php?type=personal&id='.$myrow['id'].'&class=confidential" title="'.get_lang('ExportiCalConfidential').'">'.Display::return_icon($export_icon_high, get_lang('ExportiCalConfidential')).'</a>';
  708. //echo '<a class="ical_export" href="ical_export.php?type=personal&id='.$myrow['id'].'&class=private" title="'.get_lang('ExportiCalPrivate').'">'.Display::return_icon($export_icon_low, get_lang('ExportiCalPrivate')).'</a>';
  709. //echo '<a class="ical_export" href="ical_export.php?type=personal&id='.$myrow['id'].'&class=public" title="'.get_lang('ExportiCalPublic').'">'.Display::return_icon($export_icon, get_lang('ExportiCalPublic')).'</a>';
  710. //echo "\n\t\t</td>\n\t";
  711. echo "</tr>";
  712. /*--------------------------------------------------
  713. display: the title
  714. --------------------------------------------------*/
  715. echo "<tr>";
  716. echo '<td class="'.$style.'" colspan="2">';
  717. echo $myrow['title'];
  718. echo "\n\t\t</td>\n\t";
  719. echo "</tr>\n";
  720. /*--------------------------------------------------
  721. display: the content
  722. --------------------------------------------------*/
  723. $content = $myrow['text'];
  724. $content = make_clickable($content);
  725. $content = text_filter($content);
  726. echo "\t<tr>\n\t\t<td class=\"".$text_style."\" colspan='2'>";
  727. echo $content;
  728. echo "</td></tr>";
  729. /*--------------------------------------------------
  730. display: the edit / delete icons
  731. --------------------------------------------------*/
  732. echo "\t<tr>\n\t\t<td class=\"".$text_style."\" colspan='2'>";
  733. echo "<a href=\"myagenda.php?action=edit_personal_agenda_item&amp;id=".$myrow['id']."\">".Display::return_icon('edit.gif', get_lang('Edit'))."</a>";
  734. echo "<a href=\"".api_get_self()."?action=delete&amp;id=".$myrow['id']."\" onclick=\"javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset))."')) return false;\">".Display::return_icon('delete.gif', get_lang('Delete'))."</a>";
  735. echo "</td></tr>";
  736. }
  737. }
  738. else
  739. {
  740. echo '<tr><td colspan="2">'.get_lang('NoAgendaItems').'</td></tr>';
  741. }
  742. echo "</table>\n";
  743. }
  744. /**
  745. * This function retrieves all the personal agenda items of the given user_id and shows
  746. * these items in one list (ordered by date and grouped by month (the month_bar)
  747. * @param int user id
  748. */
  749. function show_simple_personal_agenda($user_id)
  750. {
  751. $tbl_personal_agenda = Database :: get_user_personal_table(TABLE_PERSONAL_AGENDA);
  752. global $MonthsLong, $charset;
  753. // The SQL statement that retrieves all the personal agenda items of this user
  754. $sql = "SELECT * FROM ".$tbl_personal_agenda." WHERE user='".$user_id."' ORDER BY date DESC";
  755. $result = Database::query($sql, __FILE__, __LINE__);
  756. // variable initialisation
  757. $month_bar = "";
  758. // setting the default day, month and year
  759. if (!$_GET['day'] AND !$_GET['month'] AND !$_GET['year']) {
  760. $today = getdate();
  761. $year = $today['year'];
  762. $month = $today['mon'];
  763. $day = $today['mday'];
  764. }
  765. $export_icon = 'export.png';
  766. $export_icon_low = 'export_low_fade.png';
  767. $export_icon_high = 'export_high_fade.png';
  768. $content = '';
  769. // starting the table output
  770. if (Database::num_rows($result) > 0) {
  771. while ($myrow = Database::fetch_array($result)) {
  772. /*--------------------------------------------------
  773. display: the month bar
  774. --------------------------------------------------*/
  775. if ($month_bar != date("m", strtotime($myrow["date"])).date("Y", strtotime($myrow["date"]))) {
  776. $month_bar = date("m", strtotime($myrow["date"])).date("Y", strtotime($myrow["date"]));
  777. $content.= $MonthsLong[date("n", strtotime($myrow["date"])) - 1]." ".date("Y", strtotime($myrow["date"]));
  778. }
  779. // highlight: if a date in the small calendar is clicked we highlight the relevant items
  780. $db_date = (int) date("d", strtotime($myrow["date"])).date("n", strtotime($myrow["date"])).date("Y", strtotime($myrow["date"]));
  781. if ($_GET["day"].$_GET["month"].$_GET["year"] <> $db_date) {
  782. $style = "data";
  783. $text_style = "text";
  784. } else {
  785. $style = "datanow";
  786. $text_style = "text";
  787. }
  788. /*--------------------------------------------------
  789. display: date and time
  790. --------------------------------------------------*/
  791. // adding an internal anchor
  792. $content.= date("d", strtotime($myrow["date"]))." ".$MonthsLong[date("n", strtotime($myrow["date"])) - 1]." ".date("Y", strtotime($myrow["date"]))."&nbsp;";
  793. $content.= api_ucfirst(strftime(get_lang("timeNoSecFormat"), strtotime($myrow["date"])));
  794. /*--------------------------------------------------
  795. display: the title
  796. --------------------------------------------------*/
  797. $content.= '<br />';
  798. $content.= $myrow['title'];
  799. $content.= '<br />';
  800. /*--------------------------------------------------
  801. display: the content
  802. --------------------------------------------------*/
  803. /*
  804. $content = $myrow['title'];
  805. $content = make_clickable($content);
  806. $content = text_filter($content);*/
  807. return $content;
  808. }
  809. } else {
  810. return $content;
  811. }
  812. }
  813. /**
  814. * This function deletes a personal agenda item
  815. * There is an additional check to make sure that one cannot delete an item that
  816. * does not belong to him/her
  817. */
  818. function delete_personal_agenda($id)
  819. {
  820. global $tbl_personal_agenda;
  821. global $_user;
  822. if ($id != strval(intval($id))) {
  823. return false; //potential SQL injection
  824. }
  825. if ($id <> '')
  826. {
  827. $sql = "SELECT * FROM ".$tbl_personal_agenda." WHERE user='".$_user['user_id']."' AND id='".$id."'";
  828. $result = Database::query($sql, __FILE__, __LINE__);
  829. $aantal = Database::num_rows($result);
  830. if ($aantal <> 0)
  831. {
  832. $sql = "DELETE FROM ".$tbl_personal_agenda." WHERE user='".$_user['user_id']."' AND id='".$id."'";
  833. $result = Database::query($sql, __FILE__, __LINE__);
  834. }
  835. }
  836. }
  837. /**
  838. * Get personal agenda items between two dates (=all events from all registered courses)
  839. * @param int user ID of the user
  840. * @param string Optional start date in datetime format (if no start date is given, uses today)
  841. * @param string Optional end date in datetime format (if no date is given, uses one year from now)
  842. * @return array Array of events ordered by start date, in [0]('datestart','dateend','title'),[1]('datestart','dateend','title','link','coursetitle') format, where datestart and dateend are in yyyyMMddhhmmss format.
  843. * @TODO Implement really personal events (from user DB) and global events (from main DB)
  844. */
  845. function get_personal_agenda_items_between_dates($user_id, $date_start='', $date_end='') {
  846. $items = array ();
  847. if ($user_id != strval(intval($user_id))) { return $items; }
  848. if (empty($date_start)) { $date_start = date('Y-m-d H:i:s');}
  849. if (empty($date_end)) { $date_end = date('Y-m-d H:i:s',mktime(0, 0, 0, date("m"), date("d"), date("Y")+1));}
  850. $expr = '/\d{4}-\d{2}-\d{2}\ \d{2}:\d{2}:\d{2}/';
  851. if(!preg_match($expr,$date_start)) { return $items; }
  852. if(!preg_match($expr,$date_end)) { return $items; }
  853. // get agenda-items for every course
  854. $courses = api_get_user_courses($user_id,false);
  855. require_once(api_get_path(LIBRARY_PATH).'groupmanager.lib.php');
  856. foreach ($courses as $id => $course)
  857. {
  858. $c = api_get_course_info($course['code']);
  859. //databases of the courses
  860. $t_a = Database :: get_course_table(TABLE_AGENDA, $course['db']);
  861. $t_ip = Database :: get_course_table(TABLE_ITEM_PROPERTY, $course['db']);
  862. // get the groups to which the user belong
  863. $group_memberships = GroupManager :: get_group_ids($course['db'], $user_id);
  864. // if the user is administrator of that course we show all the agenda items
  865. if ($course['status'] == '1') {
  866. //echo "course admin";
  867. $sqlquery = "SELECT ".
  868. " DISTINCT agenda.*, item_property.* ".
  869. " FROM ".$t_a." agenda, ".
  870. $t_ip." item_property ".
  871. " WHERE agenda.id = item_property.ref ".
  872. " AND agenda.start_date>='$date_start' ".
  873. " AND agenda.end_date<='$date_end' ".
  874. " AND item_property.tool='".TOOL_CALENDAR_EVENT."' ".
  875. " AND item_property.visibility='1' ".
  876. " GROUP BY agenda.id ".
  877. " ORDER BY start_date ";
  878. } else {
  879. // if the user is not an administrator of that course, then...
  880. if (is_array($group_memberships) && count($group_memberships)>0)
  881. {
  882. $sqlquery = "SELECT " .
  883. " agenda.*, item_property.* ".
  884. " FROM ".$t_a." agenda, ".
  885. $t_ip." item_property ".
  886. " WHERE agenda.id = item_property.ref ".
  887. " AND agenda.start_date>='$date_start' ".
  888. " AND agenda.end_date<='$date_end' ".
  889. " AND item_property.tool='".TOOL_CALENDAR_EVENT."' ".
  890. " AND ( item_property.to_user_id='".$user_id."' OR item_property.to_group_id IN (0, ".implode(", ", $group_memberships).") ) ".
  891. " AND item_property.visibility='1' ".
  892. " ORDER BY start_date ";
  893. } else {
  894. $sqlquery = "SELECT ".
  895. " agenda.*, item_property.* ".
  896. " FROM ".$t_a." agenda, ".
  897. $t_ip." item_property ".
  898. " WHERE agenda.id = item_property.ref ".
  899. " AND agenda.start_date>='$date_start' ".
  900. " AND agenda.end_date<='$date_end' ".
  901. " AND item_property.tool='".TOOL_CALENDAR_EVENT."' ".
  902. " AND ( item_property.to_user_id='".$user_id."' OR item_property.to_group_id='0') ".
  903. " AND item_property.visibility='1' ".
  904. " ORDER BY start_date ";
  905. }
  906. }
  907. $result = Database::query($sqlquery, __FILE__, __LINE__);
  908. while ($item = Database::fetch_array($result)) {
  909. $agendaday = date("j",strtotime($item['start_date']));
  910. $URL = api_get_path(WEB_PATH)."main/calendar/agenda.php?cidReq=".urlencode($course["code"])."&amp;day=$agendaday&amp;month=$month&amp;year=$year#$agendaday";
  911. list($year,$month,$day,$hour,$min,$sec) = split('[-: ]',$item['start_date']);
  912. $start_date = $year.$month.$day.$hour.$min;
  913. list($year,$month,$day,$hour,$min,$sec) = split('[-: ]',$item['end_date']);
  914. $end_date = $year.$month.$day.$hour.$min;
  915. $items[] = array(
  916. 'datestart'=>$start_date,
  917. 'dateend'=>$end_date,
  918. 'title'=>$item['title'],
  919. 'link'=>$URL,
  920. 'coursetitle'=>$c['name'],
  921. );
  922. }
  923. }
  924. return $items;
  925. }
  926. ?>