myagenda.inc.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. <?php
  2. /* For licensing terms, see /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. @deprecated use the Agenda class
  8. */
  9. /**
  10. * Settings (you may alter this at will
  11. */
  12. $setting_agenda_link = 'coursecode'; // valid values are coursecode and icon
  13. /**
  14. * This function retrieves all the agenda items of all the courses the user is subscribed to
  15. */
  16. function get_myagendaitems($user_id, $courses_dbs, $month, $year) {
  17. global $setting_agenda_link;
  18. $user_id = intval($user_id);
  19. $items = array();
  20. $my_list = array();
  21. // get agenda-items for every course
  22. foreach ($courses_dbs as $key => $array_course_info) {
  23. //databases of the courses
  24. $TABLEAGENDA = Database :: get_course_table(TABLE_AGENDA);
  25. $TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  26. $group_memberships = GroupManager :: get_group_ids($array_course_info["real_id"], $user_id);
  27. $course_user_status = CourseManager::get_user_in_course_status($user_id, $array_course_info["code"]);
  28. // if the user is administrator of that course we show all the agenda items
  29. if ($course_user_status == '1') {
  30. //echo "course admin";
  31. $sqlquery = "SELECT DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  32. FROM ".$TABLEAGENDA." agenda,
  33. ".$TABLE_ITEMPROPERTY." ip
  34. WHERE agenda.id = ip.ref
  35. AND MONTH(agenda.start_date)='".$month."'
  36. AND YEAR(agenda.start_date)='".$year."'
  37. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  38. AND ip.visibility='1'
  39. GROUP BY agenda.id
  40. ORDER BY start_date ";
  41. } else {
  42. // if the user is not an administrator of that course
  43. if (is_array($group_memberships) && count($group_memberships)>0) {
  44. $sqlquery = "SELECT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  45. FROM ".$TABLEAGENDA." agenda,
  46. ".$TABLE_ITEMPROPERTY." ip
  47. WHERE agenda.id = ip.ref
  48. AND MONTH(agenda.start_date)='".$month."'
  49. AND YEAR(agenda.start_date)='".$year."'
  50. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  51. AND ( ip.to_user_id='".$user_id."' OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).") )
  52. AND ip.visibility='1'
  53. ORDER BY start_date ";
  54. } else {
  55. $sqlquery = "SELECT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  56. FROM ".$TABLEAGENDA." agenda,
  57. ".$TABLE_ITEMPROPERTY." ip
  58. WHERE agenda.id = ip.ref
  59. AND MONTH(agenda.start_date)='".$month."'
  60. AND YEAR(agenda.start_date)='".$year."'
  61. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  62. AND ( ip.to_user_id='".$user_id."' OR ip.to_group_id='0')
  63. AND ip.visibility='1'
  64. ORDER BY start_date ";
  65. }
  66. }
  67. $result = Database::query($sqlquery);
  68. while ($item = Database::fetch_array($result, 'ASSOC')) {
  69. $agendaday = -1;
  70. if ($item['start_date'] != '0000-00-00 00:00:00') {
  71. $item['start_date'] = api_get_local_time($item['start_date']);
  72. $item['start_date_tms'] = api_strtotime($item['start_date']);
  73. $agendaday = date("j", $item['start_date_tms']);
  74. }
  75. if ($item['end_date'] != '0000-00-00 00:00:00') {
  76. $item['end_date'] = api_get_local_time($item['end_date']);
  77. }
  78. $url = api_get_path(WEB_CODE_PATH)."calendar/agenda.php?cidReq=".urlencode($array_course_info["code"])."&day=$agendaday&month=$month&year=$year#$agendaday";
  79. $item['url'] = $url;
  80. $item['course_name'] = $array_course_info['title'];
  81. $item['calendar_type'] = 'course';
  82. $item['course_id'] = $array_course_info['course_id'];
  83. $my_list[$agendaday][] = $item;
  84. }
  85. }
  86. // sorting by hour for every day
  87. $agendaitems = array ();
  88. while (list ($agendaday, $tmpitems) = each($items)) {
  89. if(!isset($agendaitems[$agendaday])) {
  90. $agendaitems[$agendaday] = '';
  91. }
  92. sort($tmpitems);
  93. while (list ($key, $val) = each($tmpitems)) {
  94. $agendaitems[$agendaday] .= $val;
  95. }
  96. }
  97. return $my_list;
  98. }
  99. /**
  100. * Show the monthcalender of the given month
  101. * @param array Agendaitems
  102. * @param int Month number
  103. * @param int Year number
  104. * @param array Array of strings containing long week day names (deprecated, you can send an empty array instead)
  105. * @param string The month name
  106. * @return void Direct output
  107. */
  108. function display_mymonthcalendar($user_id, $agendaitems, $month, $year, $weekdaynames = array(), $monthName, $show_content = true) {
  109. global $DaysShort, $course_path;
  110. //Handle leap year
  111. $numberofdays = array (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  112. if (($year % 400 == 0) or ($year % 4 == 0 and $year % 100 <> 0))
  113. $numberofdays[2] = 29;
  114. //Get the first day of the month
  115. $dayone = getdate(mktime(0, 0, 0, $month, 1, $year));
  116. //Start the week on monday
  117. $startdayofweek = $dayone['wday'] <> 0 ? ($dayone['wday'] - 1) : 6;
  118. $g_cc = (isset($_GET['courseCode'])?$_GET['courseCode']:'');
  119. $prev_icon = Display::return_icon('action_prev.png',get_lang('Previous'));
  120. $next_icon = Display::return_icon('action_next.png',get_lang('Next'));
  121. $next_month = ($month == 1 ? 12 : $month -1);
  122. $prev_month = ($month == 12 ? 1 : $month +1);
  123. $next_year = ($month == 1 ? $year -1 : $year);
  124. $prev_year = ($month == 12 ? $year +1 : $year);
  125. if ($show_content) {
  126. $back_url = Display::url($prev_icon, api_get_self()."?coursePath=".urlencode($course_path)."&amp;courseCode=".Security::remove_XSS($g_cc)."&amp;action=view&amp;view=month&amp;month=".$next_month."&amp;year=".$next_year);
  127. $next_url = Display::url($next_icon, api_get_self()."?coursePath=".urlencode($course_path)."&amp;courseCode=".Security::remove_XSS($g_cc)."&amp;action=view&amp;view=month&amp;month=".$prev_month."&amp;year=".$prev_year);
  128. } else {
  129. $back_url = Display::url($prev_icon, '', array('onclick'=>"load_calendar('".$user_id."','".$next_month."', '".$next_year."'); "));
  130. $next_url = Display::url($next_icon, '', array('onclick'=>"load_calendar('".$user_id."','".$prev_month."', '".$prev_year."'); "));
  131. }
  132. echo '<table id="agenda_list"><tr>';
  133. echo '<th width="10%">'.$back_url.'</th>';
  134. echo '<th width="80%" colspan="5"><br /><h3>'.$monthName." ".$year.'</h3></th>';
  135. echo '<th width="10%">'.$next_url.'</th>';
  136. echo '</tr>';
  137. echo '<tr>';
  138. for ($ii = 1; $ii < 8; $ii ++) {
  139. echo '<td class="weekdays">'.$DaysShort[$ii % 7].'</td>';
  140. }
  141. echo '</tr>';
  142. $curday = -1;
  143. $today = getdate();
  144. while ($curday <= $numberofdays[$month]) {
  145. echo "<tr>";
  146. for ($ii = 0; $ii < 7; $ii ++) {
  147. if (($curday == -1) && ($ii == $startdayofweek)) {
  148. $curday = 1;
  149. }
  150. if (($curday > 0) && ($curday <= $numberofdays[$month])) {
  151. $bgcolor = $class = 'class="days_week"';
  152. $dayheader = Display::div($curday, array('class'=>'agenda_day'));
  153. if (($curday == $today['mday']) && ($year == $today['year']) && ($month == $today['mon'])) {
  154. $class = "class=\"days_today\" style=\"width:10%;\"";
  155. }
  156. echo "<td ".$class.">".$dayheader;
  157. if (!empty($agendaitems[$curday])) {
  158. $items = $agendaitems[$curday];
  159. $items = ArrayClass::msort($items, 'start_date_tms');
  160. foreach($items as $value) {
  161. $value['title'] = Security::remove_XSS($value['title']);
  162. $start_time = api_format_date($value['start_date'], TIME_NO_SEC_FORMAT);
  163. $end_time = '';
  164. if (!empty($value['end_date']) && $value['end_date'] != '0000-00-00 00:00:00') {
  165. $end_time = '-&nbsp;<i>'.api_format_date($value['end_date'], DATE_TIME_FORMAT_LONG).'</i>';
  166. }
  167. $complete_time = '<i>'.api_format_date($value['start_date'], DATE_TIME_FORMAT_LONG).'</i>&nbsp;'.$end_time;
  168. $time = '<i>'.$start_time.'</i>';
  169. switch($value['calendar_type']) {
  170. case 'personal':
  171. $bg_color = '#D0E7F4';
  172. $icon = Display::return_icon('user.png', get_lang('MyAgenda'), array(), ICON_SIZE_SMALL);
  173. break;
  174. case 'global':
  175. $bg_color = '#FFBC89';
  176. $icon = Display::return_icon('view_remove.png', get_lang('GlobalEvent'), array(), ICON_SIZE_SMALL);
  177. break;
  178. case 'course':
  179. $bg_color = '#CAFFAA';
  180. $icon_name = 'course.png';
  181. if (!empty($value['session_id'])) {
  182. $icon_name = 'session.png';
  183. }
  184. if ($show_content) {
  185. $icon = Display::url(Display::return_icon($icon_name, $value['course_name'].' '.get_lang('Course'), array(), ICON_SIZE_SMALL), $value['url']);
  186. } else {
  187. $icon = Display::return_icon($icon_name, $value['course_name'].' '.get_lang('Course'), array(), ICON_SIZE_SMALL);
  188. }
  189. break;
  190. default:
  191. break;
  192. }
  193. $result = '<div class="rounded_div_agenda" style="background-color:'.$bg_color.';">';
  194. if ($show_content) {
  195. //Setting a personal event to green
  196. $icon = Display::div($icon, array('style'=>'float:right'));
  197. $link = $value['calendar_type'].'_'.$value['id'].'_'.$value['course_id'].'_'.$value['session_id'];
  198. //Link to bubble
  199. $url = Display::url(Text::cut($value['title'], 40), '#', array('id'=>$link, 'class'=>'opener'));
  200. $result .= $time.' '.$icon.' '.Display::div($url);
  201. //Hidden content
  202. $content = Display::div($icon.Display::tag('h2', $value['course_name']).'<hr />'.Display::tag('h3', $value['title']).$complete_time.'<hr />'.Security::remove_XSS($value['content']));
  203. //Main div
  204. $result .= Display::div($content, array('id'=>'main_'.$link, 'class' => 'dialog', 'style' => 'display:none'));
  205. $result .= '</div>';
  206. echo $result;
  207. //echo Display::div($content, array('id'=>'main_'.$value['calendar_type'].'_'.$value['id'], 'class' => 'dialog'));
  208. } else {
  209. echo $result .= $icon.'</div>';
  210. }
  211. }
  212. }
  213. echo "</td>";
  214. $curday ++;
  215. } else {
  216. echo "<td></td>";
  217. }
  218. }
  219. echo "</tr>";
  220. }
  221. echo "</table>";
  222. }
  223. /**
  224. * Show the mini calender of the given month
  225. */
  226. function display_myminimonthcalendar($agendaitems, $month, $year, $monthName) {
  227. global $DaysShort,$course_path;
  228. //Handle leap year
  229. $numberofdays = array (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  230. if (($year % 400 == 0) or ($year % 4 == 0 and $year % 100 <> 0))
  231. $numberofdays[2] = 29;
  232. //Get the first day of the month
  233. $dayone = getdate(mktime(0, 0, 0, $month, 1, $year));
  234. //Start the week on monday
  235. $startdayofweek = $dayone['wday'] <> 0 ? ($dayone['wday'] - 1) : 6;
  236. $g_cc = (isset($_GET['courseCode'])?$_GET['courseCode']:'');
  237. $backwardsURL = api_get_self()."?coursePath=".Security::remove_XSS($course_path)."&amp;courseCode=".Security::remove_XSS($g_cc)."&amp;month=". ($month == 1 ? 12 : $month -1)."&amp;year=". ($month == 1 ? $year -1 : $year);
  238. $forewardsURL = api_get_self()."?coursePath=".Security::remove_XSS($course_path)."&amp;courseCode=".Security::remove_XSS($g_cc)."&amp;month=". ($month == 12 ? 1 : $month +1)."&amp;year=". ($month == 12 ? $year +1 : $year);
  239. echo "<table class=\"data_table\">", "<tr>", "<th width=\"10%\"><a href=\"", $backwardsURL, "\">".Display::return_icon('action_prev.png',get_lang('Previous'))."</a></th>";
  240. echo "<th width=\"80%\" colspan=\"5\">", $monthName, " ", $year, "</th>", "<th width=\"10%\"><a href=\"", $forewardsURL, "\">".Display::return_icon('action_next.png',get_lang('Next'))."</a></th>", "</tr>";
  241. echo "<tr>";
  242. for ($ii = 1; $ii < 8; $ii ++)
  243. {
  244. echo "<td class=\"weekdays\">", $DaysShort[$ii % 7], "</td>";
  245. }
  246. echo "</tr>";
  247. $curday = -1;
  248. $today = getdate();
  249. while ($curday <= $numberofdays[$month])
  250. {
  251. echo "<tr>";
  252. for ($ii = 0; $ii < 7; $ii ++) {
  253. if (($curday == -1) && ($ii == $startdayofweek))
  254. {
  255. $curday = 1;
  256. }
  257. if (($curday > 0) && ($curday <= $numberofdays[$month])) {
  258. $bgcolor = $ii < 5 ? $class = 'class="days_week"' : $class = 'class="days_weekend"';
  259. $dayheader = "$curday";
  260. if (($curday == $today['mday']) && ($year == $today['year']) && ($month == $today['mon'])) {
  261. $dayheader = "$curday";
  262. $class = "class=\"days_today\"";
  263. }
  264. echo "<td ".$class.">";
  265. if (!empty($agendaitems[$curday])) {
  266. echo "<a href=\"".api_get_self()."?action=view&amp;view=day&amp;day=".$curday."&amp;month=".$month."&amp;year=".$year."\">".$dayheader."</a>";
  267. } else {
  268. echo $dayheader;
  269. }
  270. // "a".$dayheader." <span class=\"agendaitem\">".$agendaitems[$curday]."</span>";
  271. echo "</td>";
  272. $curday ++;
  273. }
  274. else
  275. {
  276. echo "<td>&nbsp;</td>";
  277. }
  278. }
  279. echo "</tr>";
  280. }
  281. echo "</table>";
  282. }
  283. /**
  284. * This function shows all the forms that are needed form adding/editing a new personal agenda item
  285. * @param date is the time in day
  286. * @param date is the time in month
  287. * @param date is the time in year
  288. * @param date is the time in hour
  289. * @param date is the time in minute
  290. * @param string is the agenda title
  291. * @param string is the content
  292. * @param int is the id this param is optional, but is necessary if the item require be edited
  293. */
  294. function store_personal_item($day, $month, $year, $hour, $minute, $title, $content, $id = "") {
  295. $tbl_personal_agenda = Database :: get_main_table(TABLE_PERSONAL_AGENDA);
  296. //constructing the date
  297. $date = $year."-".$month."-".$day." ".$hour.":".$minute.":00";
  298. if (!empty($date)) {
  299. $date = api_get_utc_datetime($date);
  300. }
  301. $date = Database::escape_string($date);
  302. $title = Database::escape_string($title);
  303. $content = Database::escape_string($content);
  304. $id = intval($id);
  305. if (!empty($id)) {
  306. // we are updating
  307. $sql = "UPDATE ".$tbl_personal_agenda." SET user='".api_get_user_id()."', title='".$title."', text='".$content."', date='".$date."' WHERE id= ".$id;
  308. } else {
  309. // we are adding a new item
  310. $sql = "INSERT INTO $tbl_personal_agenda (user, title, text, date) VALUES ('".api_get_user_id()."','$title', '$content', '$date')";
  311. }
  312. $result = Database::query($sql);
  313. }
  314. /**
  315. * This function finds all the courses (also those of sessions) of the user and returns an array containing the
  316. * database name of the courses.
  317. * Xritten by Noel Dieschburg <noel.dieschburg@dokeos.com>
  318. * @todo remove this function and use the CourseManager get_courses_list_by_user_id
  319. */
  320. function get_all_courses_of_user() {
  321. $TABLECOURS = Database :: get_main_table(TABLE_MAIN_COURSE);
  322. $TABLECOURSUSER = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  323. $tbl_session_course_user= Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  324. $sql_select_courses = "SELECT c.code k, c.visual_code vc, c.title i, c.tutor_name t,
  325. c.db_name db, c.directory dir, '5' as status
  326. FROM $TABLECOURS c, $tbl_session_course_user srcu
  327. WHERE srcu.id_user='".api_get_user_id()."'
  328. AND c.id = srcu.c_id
  329. UNION
  330. SELECT c.code k, c.visual_code vc, c.title i, c.tutor_name t,
  331. c.db_name db, c.directory dir, cru.status status
  332. FROM $TABLECOURS c, $TABLECOURSUSER cru
  333. WHERE cru.user_id='".api_get_user_id()."'
  334. AND c.id = cru.c_id";
  335. $result = Database::query($sql_select_courses);
  336. while ($row = Database::fetch_array($result)) {
  337. // we only need the database name of the course
  338. $courses[] = array ("db" => $row['db'], "code" => $row['k'], "visual_code" => $row['vc'], "title" => $row['i'], "directory" => $row['dir'], "status" => $row['status']);
  339. }
  340. return $courses;
  341. }
  342. /**
  343. * This function finds all the courses of the user and returns an array containing the
  344. * database name of the courses.
  345. */
  346. function get_courses_of_user() {
  347. $TABLECOURS = Database :: get_main_table(TABLE_MAIN_COURSE);
  348. $TABLECOURSUSER = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  349. $sql_select_courses = "SELECT course.code k, course.visual_code vc,
  350. course.title i, course.tutor_name t, course.db_name db, course.directory dir, course_rel_user.status status
  351. FROM $TABLECOURS course,
  352. $TABLECOURSUSER course_rel_user
  353. WHERE course.id = course_rel_user.c_id
  354. AND course_rel_user.user_id = '".api_get_user_id()."'";
  355. $result = Database::query($sql_select_courses);
  356. while ($row = Database::fetch_array($result)) {
  357. // we only need the database name of the course
  358. $courses[] = array ("db" => $row['db'], "code" => $row['k'], "visual_code" => $row['vc'], "title" => $row['i'], "directory" => $row['dir'], "status" => $row['status']);
  359. }
  360. return $courses;
  361. }
  362. /**
  363. * This function retrieves all the personal agenda items and add them to the agenda items found by the other functions.
  364. */
  365. function get_personal_agenda_items($user_id, $agendaitems, $day = "", $month = "", $year = "", $week = "", $type) {
  366. $tbl_personal_agenda = Database :: get_main_table(TABLE_PERSONAL_AGENDA);
  367. $user_id = intval($user_id);
  368. // 1. creating the SQL statement for getting the personal agenda items in MONTH view
  369. if ($type == "month_view" or $type == "") {
  370. // we are in month view
  371. $sql = "SELECT * FROM ".$tbl_personal_agenda." WHERE user='".$user_id."' and MONTH(date)='".$month."' AND YEAR(date) = '".$year."' ORDER BY date ASC";
  372. }
  373. // 2. creating the SQL statement for getting the personal agenda items in WEEK view
  374. // we are in week view
  375. if ($type == "week_view") {
  376. $start_end_day_of_week = calculate_start_end_of_week($week, $year);
  377. $start_day = $start_end_day_of_week['start']['day'];
  378. $start_month = $start_end_day_of_week['start']['month'];
  379. $start_year = $start_end_day_of_week['start']['year'];
  380. $end_day = $start_end_day_of_week['end']['day'];
  381. $end_month = $start_end_day_of_week['end']['month'];
  382. $end_year = $start_end_day_of_week['end']['year'];
  383. // in sql statements you have to use year-month-day for date calculations
  384. $start_filter = $start_year."-".$start_month."-".$start_day." 00:00:00";
  385. $start_filter = api_get_utc_datetime($start_filter);
  386. $end_filter = $end_year."-".$end_month."-".$end_day." 23:59:59";
  387. $end_filter = api_get_utc_datetime($end_filter);
  388. $sql = " SELECT * FROM ".$tbl_personal_agenda." WHERE user='".$user_id."' AND date>='".$start_filter."' AND date<='".$end_filter."'";
  389. }
  390. // 3. creating the SQL statement for getting the personal agenda items in DAY view
  391. if ($type == "day_view") {
  392. // we are in day view
  393. // we could use mysql date() function but this is only available from 4.1 and higher
  394. $start_filter = $year."-".$month."-".$day." 00:00:00";
  395. $start_filter = api_get_utc_datetime($start_filter);
  396. $end_filter = $year."-".$month."-".$day." 23:59:59";
  397. $end_filter = api_get_utc_datetime($end_filter);
  398. $sql = " SELECT * FROM ".$tbl_personal_agenda." WHERE user='".$user_id."' AND date>='".$start_filter."' AND date<='".$end_filter."'";
  399. }
  400. $result = Database::query($sql);
  401. while ($item = Database::fetch_array($result, 'ASSOC')) {
  402. $time_minute = api_convert_and_format_date($item['date'], TIME_NO_SEC_FORMAT);
  403. $item['date'] = api_get_local_time($item['date']);
  404. $item['start_date_tms'] = api_strtotime($item['date']);
  405. $item['content'] = $item['text'];
  406. // we break the date field in the database into a date and a time part
  407. $agenda_db_date = explode(" ", $item['date']);
  408. $date = $agenda_db_date[0];
  409. $time = $agenda_db_date[1];
  410. // we divide the date part into a day, a month and a year
  411. $agendadate = explode("-", $item['date']);
  412. $year = intval($agendadate[0]);
  413. $month = intval($agendadate[1]);
  414. $day = intval($agendadate[2]);
  415. // we divide the time part into hour, minutes, seconds
  416. $agendatime = explode(":", $time);
  417. $hour = $agendatime[0];
  418. $minute = $agendatime[1];
  419. $second = $agendatime[2];
  420. if ($type == 'month_view') {
  421. $item['calendar_type'] = 'personal';
  422. $item['start_date'] = $item['date'];
  423. $agendaitems[$day][] = $item;
  424. continue;
  425. }
  426. // if the student has specified a course we a add a link to that course
  427. if ($item['course'] <> "") {
  428. $url = api_get_path(WEB_CODE_PATH)."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
  429. $course_link = "<a href=\"$url\" title=\"".$item['course']."\">".$item['course']."</a>";
  430. } else {
  431. $course_link = "";
  432. }
  433. // Creating the array that will be returned. If we have week or month view we have an array with the date as the key
  434. // if we have a day_view we use a half hour as index => key 33 = 16h30
  435. if ($type !== "day_view") {
  436. // This is the array construction for the WEEK or MONTH view
  437. //Display events in agenda
  438. $agendaitems[$day] .= "<div><i>$time_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 />";
  439. } else {
  440. // this is the array construction for the DAY view
  441. $halfhour = 2 * $agendatime['0'];
  442. if ($agendatime['1'] >= '30') {
  443. $halfhour = $halfhour +1;
  444. }
  445. //Display events by list
  446. $agendaitems[$halfhour] .= "<div><i>$time_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>";
  447. }
  448. }
  449. return $agendaitems;
  450. }
  451. /**
  452. * This function retrieves one personal agenda item returns it.
  453. * @param int The agenda item ID
  454. * @return array The results of the database query, or null if not found
  455. */
  456. function get_personal_agenda_item($id) {
  457. $tbl_personal_agenda = Database :: get_main_table(TABLE_PERSONAL_AGENDA);
  458. $id = Database::escape_string($id);
  459. // make sure events of the personal agenda can only be seen by the user himself
  460. $user = api_get_user_id();
  461. $sql = " SELECT * FROM ".$tbl_personal_agenda." WHERE id=".$id." AND user = ".$user;
  462. $result = Database::query($sql);
  463. if(Database::num_rows($result)==1) {
  464. $item = Database::fetch_array($result);
  465. } else {
  466. $item = null;
  467. }
  468. return $item;
  469. }
  470. /**
  471. * This function retrieves all the personal agenda items of the user and shows
  472. * these items in one list (ordered by date and grouped by month (the month_bar)
  473. */
  474. function show_personal_agenda() {
  475. global $MonthsLong, $charset;
  476. $tbl_personal_agenda = Database :: get_main_table(TABLE_PERSONAL_AGENDA);
  477. // The SQL statement that retrieves all the personal agenda items of this user
  478. $sql = "SELECT * FROM ".$tbl_personal_agenda." WHERE user='".api_get_user_id()."' ORDER BY date DESC";
  479. $result = Database::query($sql);
  480. // variable initialisation
  481. $month_bar = "";
  482. // setting the default day, month and year
  483. if (!isset($_GET['day']) AND !isset($_GET['month']) AND !isset($_GET['year'])) {
  484. $today = getdate();
  485. $year = $today['year'];
  486. $month = $today['mon'];
  487. $day = $today['mday'];
  488. }
  489. $export_icon = 'export.png';
  490. $export_icon_low = 'export_low_fade.png';
  491. $export_icon_high = 'export_high_fade.png';
  492. // starting the table output
  493. echo '<table class="data_table">';
  494. $th = Display::tag('th', get_lang('Title'));
  495. $th .= Display::tag('th', get_lang('Content'));
  496. $th .= Display::tag('th', get_lang('StartTimeWindow'));
  497. $th .= Display::tag('th', get_lang('Modify'));
  498. echo Display::tag('tr', $th);
  499. if (Database::num_rows($result) > 0) {
  500. $counter = 0;
  501. while ($myrow = Database::fetch_array($result)) {
  502. /* display: the month bar */
  503. if ($month_bar != date("m", strtotime($myrow["date"])).date("Y", strtotime($myrow["date"]))) {
  504. $month_bar = date("m", strtotime($myrow["date"])).date("Y", strtotime($myrow["date"]));
  505. //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>";
  506. }
  507. // highlight: if a date in the small calendar is clicked we highlight the relevant items
  508. $db_date = (int) date("d", strtotime($myrow["date"])).date("n", strtotime($myrow["date"])).date("Y", strtotime($myrow["date"]));
  509. /*
  510. if ($_GET["day"].$_GET["month"].$_GET["year"] <> $db_date) {
  511. $style = "data";
  512. $text_style = "text";
  513. } else {
  514. $style = "datanow";
  515. $text_style = "text";
  516. }*/
  517. $class = 'row_even';
  518. if ($counter % 2) {
  519. $class = 'row_odd';
  520. }
  521. echo '<tr class="'.$class.'">';
  522. echo '<td>';
  523. /* display: the title */
  524. echo $myrow['title'];
  525. echo "</td>";
  526. // display: the content
  527. $content = $myrow['text'];
  528. echo "<td>";
  529. echo $content;
  530. echo "</td>";
  531. //display: date and time
  532. echo '<td>';
  533. // adding an internal anchor
  534. /*echo "<a name=\"".$myrow["id"]."\"></a>";
  535. echo date("d", strtotime($myrow["date"]))." ".$MonthsLong[date("n", strtotime($myrow["date"])) - 1]." ".date("Y", strtotime($myrow["date"]))."&nbsp;";*/
  536. $myrow["date"] = api_get_local_time($myrow["date"]);
  537. echo api_format_date($myrow["date"], DATE_TIME_FORMAT_LONG);
  538. echo "</td>";
  539. //echo '<td></td>'; //remove when enabling ical
  540. //echo '<td class="'.$style.'">';
  541. //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>';
  542. //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>';
  543. //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>';
  544. //echo "</td>";
  545. //echo "</tr>";
  546. /* display: the edit / delete icons */
  547. echo "<td>";
  548. echo "<a href=\"myagenda.php?action=edit_personal_agenda_item&amp;id=".$myrow['id']."\">".Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL)."</a> ";
  549. 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.png', get_lang('Delete'), array(), ICON_SIZE_SMALL)."</a>";
  550. echo "</td></tr>";
  551. $counter++;
  552. }
  553. } else {
  554. echo '<tr><td colspan="2">'.get_lang('NoAgendaItems').'</td></tr>';
  555. }
  556. echo "</table>";
  557. }
  558. /**
  559. * This function retrieves all the personal agenda items of the given user_id and shows
  560. * these items in one list (ordered by date and grouped by month (the month_bar)
  561. * @param int user id
  562. */
  563. function show_simple_personal_agenda($user_id) {
  564. global $MonthsLong, $charset;
  565. $tbl_personal_agenda = Database :: get_main_table(TABLE_PERSONAL_AGENDA);
  566. // The SQL statement that retrieves all the personal agenda items of this user
  567. $sql = "SELECT * FROM ".$tbl_personal_agenda." WHERE user='".$user_id."' ORDER BY date DESC";
  568. $result = Database::query($sql);
  569. // variable initialisation
  570. $month_bar = "";
  571. // setting the default day, month and year
  572. if (!$_GET['day'] AND !$_GET['month'] AND !$_GET['year']) {
  573. $today = getdate();
  574. $year = $today['year'];
  575. $month = $today['mon'];
  576. $day = $today['mday'];
  577. }
  578. $export_icon = 'export.png';
  579. $export_icon_low = 'export_low_fade.png';
  580. $export_icon_high = 'export_high_fade.png';
  581. $content = '';
  582. // starting the table output
  583. if (Database::num_rows($result) > 0) {
  584. while ($myrow = Database::fetch_array($result)) {
  585. /*--------------------------------------------------
  586. display: the month bar
  587. --------------------------------------------------*/
  588. if ($month_bar != date("m", strtotime($myrow["date"])).date("Y", strtotime($myrow["date"]))) {
  589. $month_bar = date("m", strtotime($myrow["date"])).date("Y", strtotime($myrow["date"]));
  590. $content.= $MonthsLong[date("n", strtotime($myrow["date"])) - 1]." ".date("Y", strtotime($myrow["date"]));
  591. }
  592. // highlight: if a date in the small calendar is clicked we highlight the relevant items
  593. $db_date = (int) date("d", strtotime($myrow["date"])).date("n", strtotime($myrow["date"])).date("Y", strtotime($myrow["date"]));
  594. if ($_GET["day"].$_GET["month"].$_GET["year"] <> $db_date) {
  595. $style = "data";
  596. $text_style = "text";
  597. } else {
  598. $style = "datanow";
  599. $text_style = "text";
  600. }
  601. /*--------------------------------------------------
  602. display: date and time
  603. --------------------------------------------------*/
  604. // adding an internal anchor
  605. $content.= date("d", strtotime($myrow["date"]))." ".$MonthsLong[date("n", strtotime($myrow["date"])) - 1]." ".date("Y", strtotime($myrow["date"]))."&nbsp;";
  606. $content.= strftime(get_lang("timeNoSecFormat"), strtotime($myrow["date"]));
  607. $content.= '<br />';
  608. $content.= $myrow['title'];
  609. $content.= '<br />';
  610. return $content;
  611. }
  612. } else {
  613. return $content;
  614. }
  615. }
  616. /**
  617. * This function deletes a personal agenda item
  618. * There is an additional check to make sure that one cannot delete an item that
  619. * does not belong to him/her
  620. */
  621. function delete_personal_agenda($id) {
  622. $tbl_personal_agenda = Database :: get_main_table(TABLE_PERSONAL_AGENDA);
  623. if ($id != strval(intval($id))) {
  624. return false; //potential SQL injection
  625. }
  626. if ($id <> '')
  627. {
  628. $sql = "SELECT * FROM ".$tbl_personal_agenda." WHERE user='".api_get_user_id()."' AND id='".$id."'";
  629. $result = Database::query($sql);
  630. $aantal = Database::num_rows($result);
  631. if ($aantal <> 0)
  632. {
  633. $sql = "DELETE FROM ".$tbl_personal_agenda." WHERE user='".api_get_user_id()."' AND id='".$id."'";
  634. $result = Database::query($sql);
  635. }
  636. }
  637. }
  638. /**
  639. * Get personal agenda items between two dates (=all events from all registered courses)
  640. * @param int user ID of the user
  641. * @param string Optional start date in datetime format (if no start date is given, uses today)
  642. * @param string Optional end date in datetime format (if no date is given, uses one year from now)
  643. * @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.
  644. * @TODO Implement really personal events (from user DB) and global events (from main DB)
  645. */
  646. function get_personal_agenda_items_between_dates($user_id, $date_start='', $date_end='') {
  647. $items = array ();
  648. if ($user_id != strval(intval($user_id))) { return $items; }
  649. if (empty($date_start)) { $date_start = date('Y-m-d H:i:s');}
  650. if (empty($date_end)) { $date_end = date('Y-m-d H:i:s',mktime(0, 0, 0, date("m"), date("d"), date("Y")+1));}
  651. $expr = '/\d{4}-\d{2}-\d{2}\ \d{2}:\d{2}:\d{2}/';
  652. if(!preg_match($expr,$date_start)) { return $items; }
  653. if(!preg_match($expr,$date_end)) { return $items; }
  654. // get agenda-items for every course
  655. $courses = api_get_user_courses($user_id,false);
  656. foreach ($courses as $id => $course)
  657. {
  658. $c = api_get_course_info($course['code']);
  659. //databases of the courses
  660. $t_a = Database :: get_course_table(TABLE_AGENDA, $course['db']);
  661. $t_ip = Database :: get_course_table(TABLE_ITEM_PROPERTY, $course['db']);
  662. // get the groups to which the user belong
  663. $group_memberships = GroupManager :: get_group_ids($course['db'], $user_id);
  664. // if the user is administrator of that course we show all the agenda items
  665. if ($course['status'] == '1') {
  666. //echo "course admin";
  667. $sqlquery = "SELECT ".
  668. " DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref ".
  669. " FROM ".$t_a." agenda, ".
  670. $t_ip." ip ".
  671. " WHERE agenda.id = ip.ref ".
  672. " AND agenda.start_date>='$date_start' ".
  673. " AND agenda.end_date<='$date_end' ".
  674. " AND ip.tool='".TOOL_CALENDAR_EVENT."' ".
  675. " AND ip.visibility='1' ".
  676. " GROUP BY agenda.id ".
  677. " ORDER BY start_date ";
  678. } else {
  679. // if the user is not an administrator of that course, then...
  680. if (is_array($group_memberships) && count($group_memberships)>0)
  681. {
  682. $sqlquery = "SELECT " .
  683. "DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref ".
  684. " FROM ".$t_a." agenda, ".
  685. $t_ip." ip ".
  686. " WHERE agenda.id = ip.ref ".
  687. " AND agenda.start_date>='$date_start' ".
  688. " AND agenda.end_date<='$date_end' ".
  689. " AND ip.tool='".TOOL_CALENDAR_EVENT."' ".
  690. " AND ( ip.to_user_id='".$user_id."' OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).") ) ".
  691. " AND ip.visibility='1' ".
  692. " ORDER BY start_date ";
  693. } else {
  694. $sqlquery = "SELECT ".
  695. "DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref ".
  696. " FROM ".$t_a." agenda, ".
  697. $t_ip." ip ".
  698. " WHERE agenda.id = ip.ref ".
  699. " AND agenda.start_date>='$date_start' ".
  700. " AND agenda.end_date<='$date_end' ".
  701. " AND ip.tool='".TOOL_CALENDAR_EVENT."' ".
  702. " AND ( ip.to_user_id='".$user_id."' OR ip.to_group_id='0') ".
  703. " AND ip.visibility='1' ".
  704. " ORDER BY start_date ";
  705. }
  706. }
  707. $result = Database::query($sqlquery);
  708. while ($item = Database::fetch_array($result)) {
  709. $agendaday = date("j",strtotime($item['start_date']));
  710. $URL = api_get_path(WEB_PATH)."main/calendar/agenda.php?cidReq=".urlencode($course["code"])."&amp;day=$agendaday&amp;month=$month&amp;year=$year#$agendaday";
  711. list($year,$month,$day,$hour,$min,$sec) = split('[-: ]',$item['start_date']);
  712. $start_date = $year.$month.$day.$hour.$min;
  713. list($year,$month,$day,$hour,$min,$sec) = split('[-: ]',$item['end_date']);
  714. $end_date = $year.$month.$day.$hour.$min;
  715. $items[] = array(
  716. 'datestart'=>$start_date,
  717. 'dateend'=>$end_date,
  718. 'title'=>$item['title'],
  719. 'link'=>$URL,
  720. 'coursetitle'=>$c['name'],
  721. );
  722. }
  723. }
  724. return $items;
  725. }