dokeos
[ class tree: dokeos ] [ index: dokeos ] [ all elements ]

Source for file agenda.inc.php

Documentation is available at agenda.inc.php

  1. <?php //$Id: agenda.inc.php 15546 2008-06-11 04:50:51Z yannoo $
  2. /*
  3. ==============================================================================
  4.     Dokeos - elearning and course management software
  5.  
  6.     Copyright (c) 2004-2008 Dokeos SPRL
  7.     Copyright (c) 2003 Ghent University (UGent)
  8.     Copyright (c) 2001 Universite catholique de Louvain (UCL)
  9.  
  10.     For a full list of contributors, see "credits.txt".
  11.     The full license can be read in "license.txt".
  12.  
  13.     This program is free software; you can redistribute it and/or
  14.     modify it under the terms of the GNU General Public License
  15.     as published by the Free Software Foundation; either version 2
  16.     of the License, or (at your option) any later version.
  17.  
  18.     See the GNU General Public License for more details.
  19.  
  20.     Contact: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium, info@dokeos.com
  21. ==============================================================================
  22.     @author: Patrick Cool, patrick.cool@UGent.be
  23.     @version: 1.1
  24.     @todo: synchronisation with the function in myagenda.php (for instance: using one function for the mini_calendar
  25. ==============================================================================
  26.     Large parts of the code are recycled from the old agenda tool, but I
  27.     reworked it and cleaned the code to make it more readable. The code for
  28.     the small calender on the left is taken from the My Agenda tool.
  29.  
  30.     Reabability is also the reason why I use the if ($is_allowed_to_edit)
  31.     check for each part of the code. I'm aware that is duplication, but
  32.     it makes the code much easier to read.
  33. ==============================================================================
  34. */
  35.  
  36. /*
  37. -----------------------------------------------------------
  38.     Constants and variables
  39. -----------------------------------------------------------
  40. */
  41. // the variables for the days and the months
  42. // Defining the shorts for the days
  43. $DaysShort array (get_lang("SundayShort")get_lang("MondayShort")get_lang("TuesdayShort")get_lang("WednesdayShort")get_lang("ThursdayShort")get_lang("FridayShort")get_lang("SaturdayShort"));
  44. // Defining the days of the week to allow translation of the days
  45. $DaysLong array (get_lang("SundayLong")get_lang("MondayLong")get_lang("TuesdayLong")get_lang("WednesdayLong")get_lang("ThursdayLong")get_lang("FridayLong")get_lang("SaturdayLong"));
  46. // Defining the months of the year to allow translation of the months
  47. $MonthsLong array (get_lang("JanuaryLong")get_lang("FebruaryLong")get_lang("MarchLong")get_lang("AprilLong")get_lang("MayLong")get_lang("JuneLong")get_lang("JulyLong")get_lang("AugustLong")get_lang("SeptemberLong")get_lang("OctoberLong")get_lang("NovemberLong")get_lang("DecemberLong"));
  48.  
  49. /*
  50. ==============================================================================
  51.         FUNCTIONS
  52. ==============================================================================
  53. */
  54.  
  55. /**
  56. * Retrieves all the agenda items from the table
  57. @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  58. @author Yannick Warnier <yannick.warnier@dokeos.com> - cleanup
  59. @param integer $month: the integer value of the month we are viewing
  60. @param integer $year: the 4-digit year indication e.g. 2005
  61. @return array 
  62. */
  63. function get_calendar_items($month$year)
  64. {
  65.     global $_user$_course;
  66.     global $is_allowed_to_edit;
  67.  
  68.     // database variables
  69.     $TABLEAGENDA=Database::get_course_table(TABLE_AGENDA);
  70.     $TABLE_ITEM_PROPERTY=Database::get_course_table(TABLE_ITEM_PROPERTY);
  71.  
  72.     $month_first_day mktime(0,0,0,$month,1,$year);
  73.     $month_last_day  mktime(0,0,0,$month+1,1,$year)-1;
  74.     if($month==12)
  75.     {
  76.         $month_last_day mktime(0,0,0,1,1,$year+1)-1;
  77.     }
  78.  
  79.     $group_memberships=GroupManager::get_group_ids($_course['dbName']$_user['user_id']);
  80.     $repeats array();
  81.     if (is_allowed_to_edit(OR (api_get_course_setting('allow_user_edit_agenda'&& !api_is_anonymous()))
  82.     {
  83.         $sql="SELECT
  84.             DISTINCT agenda.*, toolitemproperties.*
  85.             FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." toolitemproperties
  86.             WHERE agenda.id = toolitemproperties.ref   ".
  87.             " AND MONTH(agenda.start_date)='".$month."' AND YEAR(agenda.start_date)='".$year."'
  88.             AND toolitemproperties.tool='".TOOL_CALENDAR_EVENT."'
  89.             AND toolitemproperties.visibility='1'
  90.             GROUP BY agenda.id ".
  91.             "ORDER BY  start_date ";
  92.     }
  93.     // if the user is not an administrator of that course
  94.     else
  95.     {
  96.         if (is_array($group_membershipsand count($group_memberships)>0)
  97.         {
  98.             $sql="SELECT
  99.                 agenda.*, toolitemproperties.*
  100.                 FROM ".$TABLEAGENDA." agenda,    ".$TABLE_ITEM_PROPERTY." toolitemproperties
  101.                 WHERE agenda.id = toolitemproperties.ref   ".
  102.                 //$show_all_current.
  103.                 " AND MONTH(agenda.start_date)='".$month."'
  104.                 AND toolitemproperties.tool='".TOOL_CALENDAR_EVENT."'
  105.                 AND    ( toolitemproperties.to_user_id='".$_user['user_id']."' OR toolitemproperties.to_group_id IN (0, ".implode(", "$group_memberships).") )
  106.                 AND toolitemproperties.visibility='1'"
  107.                 ."ORDER BY  start_date ";
  108.         }
  109.         else
  110.         {
  111.             $sql="SELECT
  112.                 agenda.*, toolitemproperties.*
  113.                 FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." toolitemproperties
  114.                 WHERE agenda.id = toolitemproperties.ref   ".
  115.                 " AND MONTH(agenda.start_date)='".$month."'
  116.                 AND toolitemproperties.tool='".TOOL_CALENDAR_EVENT."'
  117.                 AND ( toolitemproperties.to_user_id='".$_user['user_id']."' OR toolitemproperties.to_group_id='0')
  118.                 AND toolitemproperties.visibility='1' ".
  119.                 "ORDER BY  start_date ";
  120.         }
  121.     }
  122.     $result=api_sql_query($sql,__FILE__,__LINE__);
  123.     $data=array();
  124.     while ($row=Database::fetch_array($result))
  125.     {
  126.         $datum_item=(int)substr($row["start_date"],8,2);
  127.         $data[$datum_item][intval($datum_item)][$row;
  128.     }
  129.     return $data;
  130. }
  131.  
  132.  
  133. /**
  134. * show the mini calender of the given month
  135. @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  136. @param array an array containing all the agenda items for the given month
  137. @param integer $month: the integer value of the month we are viewing
  138. @param integer $year: the 4-digit year indication e.g. 2005
  139. @param string $monthName: the language variable for the mont name
  140. @return html code
  141. @todo refactor this so that $monthName is no longer needed as a parameter
  142. */
  143. function display_minimonthcalendar($agendaitems$month$year$monthName)
  144. {
  145.     global $DaysShort;
  146.     //Handle leap year
  147.     $numberofdays array (0312831303130313130313031);
  148.     if (($year 400 == 0or ($year == and $year 100 <> 0))
  149.         $numberofdays[229;
  150.     //Get the first day of the month
  151.     $dayone getdate(mktime(000$month1$year));
  152.     //Start the week on monday
  153.     $startdayofweek $dayone['wday'<> ($dayone['wday'16;
  154.     $backwardsURL api_get_self()."?".api_get_cidreq()."&coursePath=".(empty($_GET['coursePath'])?'':$_GET['coursePath'])."&amp;courseCode=".(empty($_GET['courseCode'])?'':$_GET['courseCode'])."&amp;month="($month == 12 $month -1)."&amp;year="($month == $year -$year);
  155.     $forewardsURL api_get_self()."?".api_get_cidreq()."&coursePath=".(empty($_GET['coursePath'])?'':$_GET['coursePath'])."&amp;courseCode=".(empty($_GET['courseCode'])?'':$_GET['courseCode'])."&amp;month="($month == 12 $month +1)."&amp;year="($month == 12 $year +$year);
  156.  
  157.     echo     "<table id=\"smallcalendar\">\n",
  158.             "<tr class=\"title\">\n",
  159.             "<td width=\"10%\"><a href=\""$backwardsURL"\"> &laquo; </a></td>\n",
  160.             "<td width=\"80%\" colspan=\"5\">"$monthName" "$year"</td>\n",
  161.             "<td width=\"10%\"><a href=\""$forewardsURL"\"> &raquo; </a></td>\n""</tr>\n";
  162.  
  163.     echo "<tr>\n";
  164.     for ($ii 1$ii 8$ii ++)
  165.     {
  166.         echo "<td class=\"weekdays\">"$DaysShort[$ii 7]"</td>\n";
  167.     }
  168.     echo "</tr>\n";
  169.     $curday = -1;
  170.     $today getdate();
  171.     while ($curday <= $numberofdays[$month])
  172.     {
  173.         echo "<tr>\n";
  174.         for ($ii 0$ii 7$ii ++)
  175.         {
  176.             if (($curday == -1&& ($ii == $startdayofweek))
  177.             {
  178.                 $curday 1;
  179.             }
  180.             if (($curday 0&& ($curday <= $numberofdays[$month]))
  181.             {
  182.                 $bgcolor $ii $class="class=\"days_week\"" $class="class=\"days_weekend\"";
  183.                 $dayheader "$curday";
  184.                 if (($curday == $today['mday']&& ($year == $today['year']&& ($month == $today['mon']))
  185.                 {
  186.                     $dayheader "$curday";
  187.                     $class "class=\"days_today\"";
  188.                 }
  189.                 echo "\t<td ".$class.">";
  190.                 if (!empty($agendaitems[$curday]))
  191.                 {
  192.                     echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=view&amp;view=day&amp;day=".$curday."&amp;month=".$month."&amp;year=".$year."\">".$dayheader."</a>";
  193.                 }
  194.                 else
  195.                 {
  196.                     echo $dayheader;
  197.                 }
  198.                 // "a".$dayheader." <span class=\"agendaitem\">".$agendaitems[$curday]."</span>\n";
  199.                 echo "</td>\n";
  200.                 $curday ++;
  201.             }
  202.             else
  203.             {
  204.                 echo "<td>&nbsp;</td>\n";
  205.             }
  206.         }
  207.         echo "</tr>\n";
  208.     }
  209.     echo "</table>\n";
  210. }
  211.  
  212.  
  213. /**
  214. * show the calender of the given month
  215. @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  216. @param integer $month: the integer value of the month we are viewing
  217. @param integer $year: the 4-digit year indication e.g. 2005
  218. @return html code
  219. */
  220. function display_monthcalendar($month$year)
  221. {
  222.     global $MonthsLong;
  223.     global $DaysShort;
  224.     global $origin;
  225.  
  226.     // grabbing all the calendar items for this year and storing it in a array
  227.     $data=get_calendar_items($month,$year);
  228.  
  229.  
  230.     //Handle leap year
  231.     $numberofdays array(0,31,28,31,30,31,30,31,31,30,31,30,31);
  232.     if (($year%400 == 0or ($year%4==and $year%100<>0)) $numberofdays[229;
  233.  
  234.     //Get the first day of the month
  235.     $dayone getdate(mktime(0,0,0,$month,1,$year));
  236.       //Start the week on monday
  237.     $startdayofweek $dayone['wday']<>($dayone['wday']-16;
  238.  
  239.     $backwardsURL api_get_self()."?".api_get_cidreq()."&origin=$origin&amp;month=".($month==12 $month-1)."&amp;year=".($month==$year-$year);
  240.     $forewardsURL api_get_self()."?".api_get_cidreq()."&origin=$origin&amp;month=".($month==12 $month+1)."&amp;year=".($month==12 $year+$year);
  241.  
  242.        $maand_array_maandnummer=$month-1;
  243.  
  244.     echo "<table id=\"smallcalendar\">\n",
  245.         "<tr class=\"title\">\n",
  246.         "<td width=\"10%\"><a href=\"",$backwardsURL,"\"> &laquo; </a></td>\n",
  247.         "<td width=\"80%\" colspan=\"5\">",$MonthsLong[$maand_array_maandnummer]," ",$year,"</td>\n",
  248.         "<td width=\"10%\"><a href=\"",$forewardsURL,"\"> &raquo; </a></td>\n",
  249.         "</tr>\n";
  250.  
  251.     echo "<tr>\n";
  252.  
  253.     for ($ii=1;$ii<8$ii++)
  254.     {
  255.     echo "<td class=\"weekdays\" width=\"14%\">",$DaysShort[$ii%7],"</td>\n";
  256.   }
  257.  
  258.     echo "</tr>\n";
  259.     $curday = -1;
  260.     $today getdate();
  261.     while ($curday <=$numberofdays[$month])
  262.       {
  263.     echo "<tr>\n";
  264.         for ($ii=0$ii<7$ii++)
  265.           {
  266.               if (($curday == -1)&&($ii==$startdayofweek))
  267.             {
  268.                 $curday 1;
  269.             }
  270.             if (($curday>0)&&($curday<=$numberofdays[$month]))
  271.             {
  272.                 $bgcolor $ii<"class=\"alternativeBgLight\"" "class=\"alternativeBgDark\"";
  273.  
  274.                 $dayheader "$curday";
  275.                 if (key_exists($curday,$data))
  276.                 {
  277.                     $dayheader="<a href='".api_get_self()."?".api_get_cidreq()."&amp;view=list&amp;origin=$origin&amp;month=$month&amp;year=$year&amp;day=$curday#$curday'>".$curday."</a>"
  278.                     foreach ($data[$curdayas $key=>$agenda_item)
  279.                     {
  280.                         foreach ($agenda_item as $key=>$value)
  281.                         {
  282.                             $dayheader .= '<br /><b>'.substr($value['start_date'],11,8).'</b>';    
  283.                             $dayheader .= ' - ';
  284.                             $dayheader .= $value['title'];
  285.                         }
  286.                     }
  287.                 }
  288.  
  289.                 if (($curday==$today['mday'])&&($year ==$today['year'])&&($month == $today['mon']))
  290.                 {
  291.             echo "<td id=\"today\" ",$bgcolor,"\">".$dayheader." \n";
  292.       }
  293.                 else
  294.                 {
  295.             echo "<td id=\"days\" ",$bgcolor,"\">".$dayheader." \n";
  296.                 }
  297.             echo "</td>\n";
  298.  
  299.                   $curday++;
  300.         }
  301.               else
  302.         {
  303.     echo "<td>&nbsp;</td>";
  304.  
  305.         }
  306.         }
  307.     echo "</tr>";
  308.     }
  309. echo "</table>";
  310. }
  311.  
  312.  
  313. /**
  314. * returns all the javascript that is required for easily selecting the target people/groups this goes into the $htmlHeadXtra[] array
  315. @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  316. @return javascript code
  317. */
  318. function to_javascript()
  319. {
  320. $Send2All=get_lang("Send2All");
  321.  
  322.  
  323. return "<script type=\"text/javascript\" language=\"JavaScript\">
  324.  
  325. <!-- Begin javascript menu swapper
  326.  
  327. function move(fbox,    tbox)
  328. {
  329.     var    arrFbox    = new Array();
  330.     var    arrTbox    = new Array();
  331.     var    arrLookup =    new    Array();
  332.  
  333.     var    i;
  334.     for    (i = 0;    i <    tbox.options.length; i++)
  335.     {
  336.         arrLookup[tbox.options[i].text]    = tbox.options[i].value;
  337.         arrTbox[i] = tbox.options[i].text;
  338.     }
  339.  
  340.     var    fLength    = 0;
  341.     var    tLength    = arrTbox.length;
  342.  
  343.     for(i =    0; i < fbox.options.length;    i++)
  344.     {
  345.         arrLookup[fbox.options[i].text]    = fbox.options[i].value;
  346.  
  347.         if (fbox.options[i].selected &&    fbox.options[i].value != \"\")
  348.         {
  349.             arrTbox[tLength] = fbox.options[i].text;
  350.             tLength++;
  351.         }
  352.         else
  353.         {
  354.             arrFbox[fLength] = fbox.options[i].text;
  355.             fLength++;
  356.         }
  357.     }
  358.  
  359.     arrFbox.sort();
  360.     arrTbox.sort();
  361.     fbox.length    = 0;
  362.     tbox.length    = 0;
  363.  
  364.     var    c;
  365.     for(c =    0; c < arrFbox.length; c++)
  366.     {
  367.         var    no = new Option();
  368.         no.value = arrLookup[arrFbox[c]];
  369.         no.text    = arrFbox[c];
  370.         fbox[c]    = no;
  371.     }
  372.     for(c =    0; c < arrTbox.length; c++)
  373.     {
  374.         var    no = new Option();
  375.         no.value = arrLookup[arrTbox[c]];
  376.         no.text    = arrTbox[c];
  377.         tbox[c]    = no;
  378.     }
  379. }
  380.  
  381. function validate()
  382. {
  383.     var    f =    document.new_calendar_item;
  384.     f.submit();
  385.     return true;
  386. }
  387.  
  388. function selectAll(cbList,bSelect,showwarning)
  389. {
  390.     //if (cbList.length <    1) {
  391.     //    alert(\"$Send2All\");
  392.     //    return;
  393.     //}
  394.     for    (var i=0; i<cbList.length; i++)
  395.         cbList[i].selected = cbList[i].checked = bSelect
  396. }
  397.  
  398. function reverseAll(cbList)
  399. {
  400.     for    (var i=0; i<cbList.length; i++)
  401.     {
  402.         cbList[i].checked  = !(cbList[i].checked)
  403.         cbList[i].selected = !(cbList[i].selected)
  404.     }
  405. }
  406. //    End    -->
  407. </script>";
  408. }
  409.  
  410.  
  411. /**
  412. * returns the javascript for setting a filter. This is a jump menu
  413. @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  414. @return javascript code
  415. */
  416. {
  417. return "<script language=\"JavaScript\" type=\"text/JavaScript\">
  418. <!--
  419. function MM_jumpMenu(targ,selObj,restore){
  420.   eval(targ+\".location='\"+selObj.options[selObj.selectedIndex].value+\"'\");
  421.   if (restore) selObj.selectedIndex=0;
  422. }
  423. //-->
  424. </script>
  425. ";
  426. }
  427.  
  428.  
  429. /**
  430. * this function gets all the users of the current course
  431. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  432. @return array: associative array where the key is the id of the user and the value is an array containing
  433.             the first name, the last name, the user id
  434. */
  435. function get_course_users()
  436. {
  437. global $tbl_user;
  438. global $tbl_courseUser$tbl_session_course_user;
  439. global $_cid;
  440.  
  441. // not 100% if this is necessary, this however prevents a notice
  442. if (!isset($courseadmin_filter))
  443.     {$courseadmin_filter='';}
  444.  
  445. $sql "SELECT u.user_id uid, u.lastname lastName, u.firstname firstName
  446.         FROM $tbl_user as u, $tbl_courseUser as cu
  447.         WHERE cu.course_code = '".$_cid."'
  448.             AND cu.user_id = u.user_id $courseadmin_filter
  449.         ORDER BY u.lastname, u.firstname";
  450. $result api_sql_query($sql,__FILE__,__LINE__);
  451. while($user=Database::fetch_array($result)){
  452.     $users[$user[0]] $user;
  453. }
  454.  
  455. if(!empty($_SESSION['id_session'])){
  456.     $sql "SELECT u.user_id uid, u.lastname lastName, u.firstName firstName
  457.             FROM $tbl_session_course_user AS session_course_user
  458.             INNER JOIN $tbl_user u
  459.                 ON u.user_id = session_course_user.id_user
  460.             WHERE id_session='".$_SESSION['id_session']."'
  461.             AND course_code='$_cid'";
  462.  
  463.     $result api_sql_query($sql,__FILE__,__LINE__);
  464.     while($user=Database::fetch_array($result)){
  465.         $users[$user[0]] $user;
  466.     }
  467.  
  468. }
  469.  
  470. return $users;
  471.  
  472. }
  473.  
  474.  
  475. /**
  476. * this function gets all the groups of the course
  477. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  478. @return array 
  479. */
  480. function get_course_groups()
  481. {
  482.     global $tbl_group;
  483.     global $tbl_groupUser;
  484.     $group_list array();
  485.     
  486.     $sql "SELECT g.id, g.name, COUNT(gu.id) userNb
  487.                     FROM ".$tbl_group." AS g LEFT JOIN ".$tbl_groupUser." gu
  488.                     ON g.id = gu.group_id
  489.                     GROUP BY g.id";
  490.     
  491.     $result api_sql_query($sql,__FILE__,__LINE__or die(Database::error());
  492.     while ($group_data Database::fetch_array($result))
  493.     {
  494.         $group_list [$group_data['id']] $group_data;
  495.     }
  496.     return $group_list;
  497. }
  498.  
  499.  
  500. /**
  501. * this function shows the form for sending a message to a specific group or user.
  502. @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  503. @return html code
  504. */
  505. function show_to_form($to_already_selected)
  506. {
  507. $user_list=get_course_users();
  508. $group_list=get_course_groups();
  509.  
  510. echo "\n<table id=\"recipient_list\" style=\"display: none;\">\n";
  511.     echo "\t<tr>\n";
  512.     // the form containing all the groups and all the users of the course
  513.     echo "\t\t<td>\n";
  514.         construct_not_selected_select_form($group_list,$user_list,$to_already_selected);
  515.     echo "\t\t</td>\n";
  516.     // the buttons for adding or removing groups/users
  517.     echo "\n\t\t<td valign=\"middle\">\n";
  518.     echo "\t\t<input type=\"button\" ",
  519.                 "onclick=\"move(this.form.elements[2],this.form.elements[5])\" ",
  520.                 "value=\"   &gt;&gt;   \" />",
  521.  
  522.                 "\n\t\t<p>&nbsp;</p>",
  523.  
  524.                 "\n\t\t<input type=\"button\" ",
  525.                 "onclick=\"move(this.form.elements[5],this.form.elements[2])\" ",
  526.                 "value=\"   &lt;&lt;   \" />";
  527.     echo "\t\t</td>\n";
  528.     echo "\n\t\t<td>\n";
  529.         construct_selected_select_form($group_list,$user_list,$to_already_selected);
  530.     echo "\t\t</td>\n";
  531.     echo "\t</tr>\n";
  532. echo "</table>";
  533. }
  534.  
  535.  
  536. /**
  537. * this function shows the form with the user that were not selected
  538. @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  539. @return html code
  540. */
  541. function construct_not_selected_select_form($group_list=null$user_list=null,$to_already_selected=array())
  542. {
  543.     echo "\t\t<select name=\"not_selected_form[]\" size=\"5\" multiple=\"multiple\" style=\"width:200px\">\n";
  544.  
  545.     // adding the groups to the select form
  546.     if (is_array($group_list))
  547.     {
  548.         foreach($group_list as $this_group)
  549.         {
  550.             //api_display_normal_message("group " . $thisGroup[id] . $thisGroup[name]);
  551.             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
  552.                 {
  553.                 echo    "\t\t<option value=\"GROUP:".$this_group['id']."\">",
  554.                     "G: ",$this_group['name']," &ndash; " $this_group['userNb'" " get_lang('Users'.
  555.                     "</option>\n";
  556.             }
  557.         }
  558.         // a divider
  559.         echo    "\t\t<option value=\"\">----------------------------------</option>\n";
  560.     }
  561.  
  562.     // adding the individual users to the select form
  563.     foreach($user_list as $this_user)
  564.     {
  565.         if (!is_array($to_already_selected|| !in_array("USER:".$this_user['uid'],$to_already_selected)) // $to_already_selected is the array containing the users (and groups) that are already selected
  566.         {
  567.             echo    "\t\t<option value=\"USER:",$this_user['uid'],"\">",
  568.                 "",$this_user['lastName']," ",$this_user['firstName'],
  569.                 "</option>\n";
  570.         }
  571.     }
  572.     echo "\t\t</select>\n";
  573. }
  574.  
  575.  
  576.  
  577. /**
  578. * This function shows the form with the user that were selected
  579. @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  580. @return html code
  581. */
  582. function construct_selected_select_form($group_list=null$user_list=null,$to_already_selected)
  583. {
  584.     // we separate the $to_already_selected array (containing groups AND users into
  585.     // two separate arrays
  586.     if (is_array($to_already_selected))
  587.     {
  588.          $groupuser=separate_users_groups($to_already_selected);
  589.     }
  590.     $groups_to_already_selected=$groupuser['groups'];
  591.     $users_to_already_selected=$groupuser['users'];
  592.  
  593.     // we load all the groups and all the users into a reference array that we use to search the name of the group / user
  594.     $ref_array_groups=get_course_groups();
  595.     $ref_array_users=get_course_users();
  596.  
  597.     // we construct the form of the already selected groups / users
  598.     echo "\t\t<select name=\"selectedform[]\" size=\"5\" multiple=\"multiple\" style=\"width:200px\">";
  599.     if(is_array($to_already_selected))
  600.     {
  601.         foreach($to_already_selected as $groupuser)
  602.         {
  603.             list($type,$id)=explode(":",$groupuser);
  604.             if ($type=="GROUP")
  605.             {
  606.                 echo "\t\t<option value=\"".$groupuser."\">G: ".$ref_array_groups[$id]['name']."</option>";
  607.             }
  608.             else
  609.             {
  610.                 echo "\t\t<option value=\"".$groupuser."\">".$ref_array_users[$id]['lastName']." ".$ref_array_users[$id]['firstName']."</option>";
  611.             }
  612.         }
  613.     }
  614.     echo "</select>\n";
  615. }
  616.  
  617.  
  618.  
  619. /**
  620. * This function stores the Agenda Item in the table calendar_event and updates the item_property table also
  621. @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  622. @return integer the id of the last added agenda item
  623. */
  624. {
  625.     global $_user$_course;
  626.     $TABLEAGENDA Database::get_course_table(TABLE_AGENDA);
  627.     $t_agenda_repeat Database::get_course_Table(TABLE_AGENDA_REPEAT);
  628.     
  629.     // some filtering of the input data
  630.     $title=strip_tags(trim($_POST['title']))// no html allowed in the title
  631.     $content=trim($_POST['content']);
  632.     $start_date=(int)$_POST['fyear']."-".(int)$_POST['fmonth']."-".(int)$_POST['fday']." ".(int)$_POST['fhour'].":".(int)$_POST['fminute'].":00";
  633.     $end_date=(int)$_POST['end_fyear']."-".(int)$_POST['end_fmonth']."-".(int)$_POST['end_fday']." ".(int)$_POST['end_fhour'].":".(int)$_POST['end_fminute'].":00";
  634.     
  635.     // store in the table calendar_event
  636.     $sql "INSERT INTO ".$TABLEAGENDA."
  637.                             (title,content, start_date, end_date)
  638.                             VALUES
  639.                             ('".$title."','".$content."', '".$start_date."','".$end_date."')";
  640.     
  641.     $result api_sql_query($sql,__FILE__,__LINE__or die (Database::error());
  642.     $last_id=Database::insert_id();
  643.     
  644.     // store in last_tooledit (first the groups, then the users
  645.     $to=$_POST['selectedform'];
  646.     
  647.     if ((!is_null($to))or (!empty($_SESSION['toolgroup']))) // !is_null($to): when no user is selected we send it to everyone
  648.     {
  649.         $send_to=separate_users_groups($to);
  650.         // storing the selected groups
  651.         if (is_array($send_to['groups']))
  652.         {
  653.             foreach ($send_to['groups'as $group)
  654.             {
  655.                 api_item_property_update($_courseTOOL_CALENDAR_EVENT$last_id,"AgendaAdded"$_user['user_id']$group,'',$start_date$end_date);
  656.             }
  657.         }
  658.         // storing the selected users
  659.         if (is_array($send_to['users']))
  660.         {
  661.             foreach ($send_to['users'as $user)
  662.             {
  663.                 api_item_property_update($_courseTOOL_CALENDAR_EVENT$last_id,"AgendaAdded"$_user['user_id'],'',$user$start_date,$end_date);
  664.             }
  665.         }
  666.     }
  667.     else // the message is sent to everyone, so we set the group to 0
  668.     {
  669.         api_item_property_update($_courseTOOL_CALENDAR_EVENT$last_id,"AgendaAdded"$_user['user_id']'','',$start_date,$end_date);
  670.     }
  671.     // storing the resources
  672.     store_resources($_SESSION['source_type'],$last_id);
  673.     
  674.     //if repetitive, insert element into agenda_repeat table
  675.     if(!empty($_POST['repeat']&& !empty($_POST['repeat_type']))
  676.     {
  677.         if(!empty($_POST['repeat_end_year']&& !empty($_POST['repeat_end_month']&& !empty($_POST['repeat_end_day']))
  678.         {
  679.             $end_y intval($_POST['repeat_end_year']);
  680.             $end_m intval($_POST['repeat_end_month']);
  681.             $end_d intval($_POST['repeat_end_day']);
  682.             $end mktime((int)$_POST['fhour'],(int)$_POST['fminute'],0,$end_m,$end_d,$end_y);
  683.             $now time();
  684.             $type Database::escape_string($_POST['repeat_type']);
  685.         
  686.             if($end $now 
  687.                 && in_array($type,array('daily','weekly','monthlyByDate','monthlyByDay','monthlyByDayR','yearly')))
  688.             {
  689.                $sql "INSERT INTO $t_agenda_repeat (cal_id, cal_type, cal_end).
  690.                     " VALUES ($last_id,'$type',$end)";
  691.                $res Database::query($sql,__FILE__,__LINE__);
  692.             }
  693.         }
  694.     }
  695.     return $last_id;
  696. }
  697.  
  698. /**
  699.  * Stores the given agenda item as an announcement (unlinked copy)
  700.  * @param    integer        Agenda item's ID
  701.  * @return    integer        New announcement item's ID
  702.  */
  703.     $table_agenda Database::get_course_table(TABLE_AGENDA);
  704.     $table_ann Database::get_course_table(TABLE_ANNOUNCEMENT);
  705.     //check params
  706.     if(empty($item_idor $item_id != strval(intval($item_id))){return -1;}
  707.     //get the agenda item
  708.     $sql "SELECT * FROM $table_agenda WHERE id = '".$item_id."'";
  709.     $res api_sql_query($sql,__FILE__,__LINE__);
  710.     if(Database::num_rows($res)>0){
  711.         $row Database::fetch_array($res);
  712.         //we have the agenda event, copy it
  713.         //get the maximum value for display order in announcement table
  714.         $sql_max "SELECT MAX(display_order) FROM $table_ann";
  715.         $res_max api_sql_query($sql_max,__FILE__,__LINE__);
  716.         $row_max Database::fetch_array($res_max);
  717.         $max $row_max[0]+1;
  718.         //build the announcement text
  719.         $content $row['start_date']." - ".$row['end_date']."\n\n".$row['content'];
  720.         //insert announcement
  721.  
  722.         $sql_ins "INSERT INTO $table_ann (title,content,end_date,display_order) .
  723.                 "VALUES ('".$row['title']."','$content','".$row['end_date']."','$max')";
  724.         $res_ins api_sql_query($sql_ins,__FILE__,__LINE__);
  725.         if($res 0)
  726.         {
  727.             $ann_id Database::get_last_insert_id();
  728.             //Now also get the list of item_properties rows for this agenda_item (calendar_event)
  729.             //and copy them into announcement item_properties
  730.             $table_props Database::get_course_table(TABLE_ITEM_PROPERTY);
  731.             $sql_props "SELECT * FROM $table_props WHERE tool = 'calendar_event' AND ref='$item_id'";
  732.             $res_props api_sql_query($sql_props,__FILE__,__LINE__);
  733.             if(Database::num_rows($res_props)>0)
  734.             {
  735.                 while($row_props Database::fetch_array($res_props))
  736.                 {
  737.                     //insert into announcement item_property
  738.                     $time date("Y-m-d H:i:s"time());
  739.                     $sql_ins_props "INSERT INTO $table_props .
  740.                             "(tool, insert_user_id, insert_date, " .
  741.                             "lastedit_date, ref, lastedit_type," .
  742.                             "lastedit_user_id, to_group_id, to_user_id, " .
  743.                             "visibility, start_visible, end_visible)" .
  744.                             " VALUES " .
  745.                             "('announcement','".$row_props['insert_user_id']."','".$time."'," .
  746.                             "'$time','$ann_id','AnnouncementAdded',.
  747.                             "'".$row_props['last_edit_user_id']."','".$row_props['to_group_id']."','".$row_props['to_user_id']."'," .
  748.                             "'".$row_props['visibility']."','".$row_props['start_visible']."','".$row_props['end_visible']."')";
  749.                     $res_ins_props api_sql_query($sql_ins_props,__FILE__,__LINE__);
  750.                     if($res_ins_props <= 0){
  751.                         error_log('SQL Error in '.__FILE__.' at line '.__LINE__.': '.$sql_ins_props);
  752.                     }else{
  753.                         //copy was a success
  754.                         return $ann_id;
  755.                     }
  756.                 }
  757.             }
  758.         }else{
  759.             return -1;
  760.         }
  761.     }
  762.     return -1;
  763. }
  764.  
  765. /**
  766. * This function separates the users from the groups
  767. * users have a value USER:XXX (with XXX the dokeos id
  768. * groups have a value GROUP:YYY (with YYY the group id)
  769. @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  770. @return array 
  771. */
  772. function separate_users_groups($to)
  773. {
  774.     $grouplist array();
  775.     $userlist  array();
  776.     if(is_array($to&& count($to)>0)
  777.     {
  778.         foreach($to as $to_item)
  779.         {
  780.         list($type$idexplode(':'$to_item);
  781.     
  782.         switch($type)
  783.             {
  784.             case 'GROUP':
  785.                 $grouplist[=$id;
  786.                 break;
  787.             case 'USER':
  788.                 $userlist[=$id;
  789.                 break;
  790.             }
  791.         }
  792.         $send_to['groups']=$grouplist;
  793.         $send_to['users']=$userlist;
  794.     }
  795.     return $send_to;
  796. }
  797.  
  798.  
  799.  
  800. /**
  801. * returns all the users and all the groups a specific Agenda item has been sent to
  802. @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  803. @return array 
  804. */
  805. function sent_to($tool$id)
  806. {
  807. global $_course;
  808. $TABLE_ITEM_PROPERTY Database::get_course_table(TABLE_ITEM_PROPERTY);
  809.  
  810. $sql="SELECT * FROM $TABLE_ITEM_PROPERTY WHERE tool='".$tool."' AND ref='".$id."'";
  811. $result=api_sql_query($sql,__FILE__,__LINE__);
  812. while ($row=Database::fetch_array($result))
  813.     {
  814.     // if to_group_id is null then it is sent to a specific user
  815.     // if to_group_id = 0 then it is sent to everybody
  816.     if (!is_null($row['to_group_id']) )
  817.         {
  818.         $sent_to_group[]=$row['to_group_id'];
  819.         //echo $row['to_group_id'];
  820.         }
  821.     // if to_user_id <> 0 then it is sent to a specific user
  822.     if ($row['to_user_id']<>0)
  823.         {
  824.         $sent_to_user[]=$row['to_user_id'];
  825.         }
  826.     }
  827. if (isset($sent_to_group))
  828.     {
  829.     $sent_to['groups']=$sent_to_group;
  830.     }
  831. if (isset($sent_to_user))
  832.     {
  833.     $sent_to['users']=$sent_to_user;
  834.     }
  835. return $sent_to;
  836. }
  837.  
  838.  
  839.  
  840. /**
  841. * constructs the form to display all the groups and users the message has been sent to
  842. @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  843. @param  array $sent_to_array: a 2 dimensional array containing the groups and the users
  844. *                 the first level is a distinction between groups and users: $sent_to_array['groups'] and $sent_to_array['users']
  845. *                 $sent_to_array['groups'] (resp. $sent_to_array['users']) is also an array containing all the id's of the
  846. *                 groups (resp. users) who have received this message.
  847. @return html 
  848. */
  849. function sent_to_form($sent_to_array)
  850. {
  851. // we find all the names of the groups
  852. $group_names=get_course_groups();
  853.  
  854. count($sent_to_array);
  855.  
  856. // we count the number of users and the number of groups
  857. if (isset($sent_to_array['users']))
  858.     {
  859.     $number_users=count($sent_to_array['users']);
  860.     }
  861. else
  862.     {
  863.     $number_users=0;
  864.     }
  865. if (isset($sent_to_array['groups']))
  866.     {
  867.     $number_groups=count($sent_to_array['groups']);
  868.     }
  869. else
  870.     {
  871.     $number_groups=0;
  872.     }
  873. $total_numbers=$number_users+$number_groups;
  874.  
  875. // starting the form if there is more than one user/group
  876. if ($total_numbers >1)
  877.     {
  878.     $output="<select name=\"sent to\">\n";
  879.     $output.="<option>".get_lang("SentTo")."</option>";
  880.     // outputting the name of the groups
  881.     if (is_array($sent_to_array['groups']))
  882.         {
  883.         foreach ($sent_to_array['groups'as $group_id)
  884.             {
  885.             $output.="\t<option value=\"\">G: ".$group_names[$group_id]['name']."</option>\n";
  886.             }
  887.         }
  888.     if (isset($sent_to_array['users']))
  889.     {
  890.         if (is_array($sent_to_array['users']))
  891.             {
  892.             foreach ($sent_to_array['users'as $user_id)
  893.                 {
  894.                 $user_info=api_get_user_info($user_id);
  895.                 $output.="\t<option value=\"\">".$user_info['firstName']." ".$user_info['lastName']."</option>\n";
  896.                 }
  897.             }
  898.     }
  899.  
  900.     // ending the form
  901.     $output.="</select>\n";
  902.     }
  903. else // there is only one user/group
  904.     {
  905.     if (is_array($sent_to_array['users']))
  906.         {
  907.         $user_info=api_get_user_info($sent_to_array['users'][0]);
  908.         echo $user_info['firstName']." ".$user_info['lastName'];
  909.         }
  910.     if (is_array($sent_to_array['groups']and $sent_to_array['groups'][0]!==0)
  911.         {
  912.         $group_id=$sent_to_array['groups'][0];
  913.         echo $group_names[$group_id]['name'];
  914.         }
  915.     if (is_array($sent_to_array['groups']and $sent_to_array['groups'][0]==0)
  916.         {
  917.         echo get_lang("Everybody");
  918.         }
  919.     //.$sent_to_array['groups'][0];
  920.     }
  921.  
  922. echo $output;
  923. }
  924.  
  925.  
  926. /**
  927. * This function displays a dropdown list that allows the course administrator do view the calendar items of one specific group
  928. @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  929. */
  930. {
  931. $group_list=get_course_groups();
  932.  
  933. echo "<select name=\"select\" onchange=\"MM_jumpMenu('parent',this,0)\">";
  934. echo "<option value=\"agenda.php?group=none\">show all groups</option>";
  935. foreach($group_list as $this_group)
  936.     {
  937.     // echo "<option value=\"agenda.php?isStudentView=true&amp;group=".$this_group['id']."\">".$this_group['name']."</option>";
  938.     echo "<option value=\"agenda.php?group=".$this_group['id']."\" ";
  939.     echo ($this_group['id']==$_SESSION['group'])" selected":"" ;
  940.     echo ">".$this_group['name']."</option>";
  941.     }
  942. echo "</select>";
  943. }
  944.  
  945.  
  946.  
  947. /**
  948. * This function displays a dropdown list that allows the course administrator do view the calendar items of one specific group
  949. @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  950. */
  951. {
  952. $user_list=get_course_users();
  953.  
  954. echo "<select name=\"select\" onchange=\"MM_jumpMenu('parent',this,0)\">";
  955. echo "<option value=\"agenda.php?user=none\">show all users</option>";
  956. foreach($user_list as $this_user)
  957.     {
  958.     // echo "<option value=\"agenda.php?isStudentView=true&amp;user=".$this_user['uid']."\">".$this_user['lastName']." ".$this_user['firstName']."</option>";
  959.     echo "<option value=\"agenda.php?user=".$this_user['uid']."\" ";
  960.     echo ($this_user['uid']==$_SESSION['user'])" selected":"" ;
  961.     echo ">".$this_user['lastName']." ".$this_user['firstName']."</option>";
  962.     }
  963. echo "</select>";
  964. }
  965.  
  966.  
  967.  
  968. /**
  969. * This function displays a dropdown list that allows the course administrator do view the calendar items of one specific group
  970. @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  971. */
  972. {
  973.     echo "\n<select name=\"select\" onchange=\"MM_jumpMenu('parent',this,0)\">";
  974.     echo "\n\t<option value=\"agenda.php?user=none\">".get_lang("ShowAll")."</option>";
  975.  
  976.     // Groups
  977.     echo "\n\t<optgroup label=\"".get_lang("Groups")."\">";
  978.     $group_list=get_course_groups();
  979.     foreach($group_list as $this_group)
  980.     {
  981.         // echo "<option value=\"agenda.php?isStudentView=true&amp;group=".$this_group['id']."\">".$this_group['name']."</option>";
  982.         echo "\n\t\t<option value=\"agenda.php?group=".$this_group['id']."\" ";
  983.         echo ($this_group['id']==$_SESSION['group'])" selected":"" ;
  984.         echo ">".$this_group['name']."</option>";
  985.     }
  986.     echo "\n\t</optgroup>";
  987.  
  988.     // Users
  989.     echo "\n\t<optgroup label=\"".get_lang("Users")."\">";
  990.     $user_list=get_course_users();
  991.     foreach($user_list as $this_user)
  992.         {
  993.         // echo "<option value=\"agenda.php?isStudentView=true&amp;user=".$this_user['uid']."\">".$this_user['lastName']." ".$this_user['firstName']."</option>";
  994.         echo "\n\t\t<option value=\"agenda.php?user=".$this_user['uid']."\" ";
  995.         echo ($this_user['uid']==$_SESSION['user'])" selected":"" ;
  996.         echo ">".$this_user['lastName']." ".$this_user['firstName']."</option>";
  997.         }
  998.     echo "\n\t</optgroup>";
  999.     echo "</select>";
  1000. }
  1001.  
  1002.  
  1003.  
  1004. /**
  1005. * This tools loads all the users and all the groups who have received a specific item (in this case an agenda item)
  1006. @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1007. */
  1008. function load_edit_users($tool$id)
  1009. {
  1010. global $_course;
  1011. $TABLE_ITEM_PROPERTY Database::get_course_table(TABLE_ITEM_PROPERTY);
  1012.  
  1013. $sql="SELECT * FROM $TABLE_ITEM_PROPERTY WHERE tool='$tool' AND ref='$id'";
  1014. $result=api_sql_query($sql,__FILE__,__LINE__or die (Database::error());
  1015. while ($row=Database::fetch_array($result))
  1016.     {
  1017.     $to_group=$row['to_group_id'];
  1018.     switch ($to_group)
  1019.         {
  1020.         // it was send to one specific user
  1021.         case null:
  1022.             $to[]="USER:".$row['to_user_id'];
  1023.             break;
  1024.         // it was sent to everyone
  1025.         case 0:
  1026.              return "everyone";
  1027.              exit;
  1028.             break;
  1029.         default:
  1030.             $to[]="GROUP:".$row['to_group_id'];
  1031.         }
  1032.     }
  1033. return $to;
  1034. }
  1035.  
  1036.  
  1037.  
  1038. /**
  1039. * This functions swithes the visibility a course resource using the visible field in 'last_tooledit' values: 0 = invisible
  1040. @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1041. */
  1042. function change_visibility($tool,$id)
  1043. {
  1044.     global $_course;
  1045.     $TABLE_ITEM_PROPERTY Database::get_course_table(TABLE_ITEM_PROPERTY);
  1046.  
  1047.     $sql="SELECT * FROM $TABLE_ITEM_PROPERTY WHERE tool='".TOOL_CALENDAR_EVENT."' AND ref='$id'";
  1048.     $result=api_sql_query($sql,__FILE__,__LINE__or die (Database::error());
  1049.     $row=Database::fetch_array($result);
  1050.  
  1051.     if ($row['visibility']=='1')
  1052.     {
  1053.         $sql_visibility="UPDATE $TABLE_ITEM_PROPERTY SET visibility='0' WHERE tool='$tool' AND ref='$id'";
  1054.         api_item_property_update($_course,TOOL_CALENDAR_EVENT,$id,"invisible",api_get_user_id());
  1055.     }
  1056.     else
  1057.     {
  1058.         $sql_visibility="UPDATE $TABLE_ITEM_PROPERTY SET visibility='1' WHERE tool='$tool' AND ref='$id'";
  1059.         api_item_property_update($_course,TOOL_CALENDAR_EVENT,$id,"visible",api_get_user_id());
  1060.     }
  1061. }
  1062.  
  1063.  
  1064.  
  1065. /**
  1066. * The links that allows the course administrator to add a new agenda item, to filter on groups or users
  1067. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1068. */
  1069. {
  1070.     echo "<li><a href='".api_get_self()."?".api_get_cidreq()."&action=add&amp;origin=".$_GET['origin']."'><img src=\"../img/view_more_stats.gif\" alt=\"".get_lang('MoreStats')."\" border=\"0\" /> ".get_lang("AgendaAdd")."</a><br /></li>";
  1071.     if (empty ($_SESSION['toolgroup']))
  1072.     {
  1073.         echo "<li>".get_lang('UserGroupFilter')."<br/>";
  1074.         echo "<form name=\"filter\">";
  1075.         show_user_group_filter_form();
  1076.         echo "</form>";
  1077.     }
  1078.     echo "</li>";
  1079. }
  1080.  
  1081.  
  1082.  
  1083. /**
  1084. * The links that allows the student AND course administrator to show all agenda items and sort up/down
  1085. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1086. */
  1087. {
  1088.     global $show;
  1089.     if ($_SESSION['sort'== 'DESC')
  1090.     {
  1091.         echo "<li><a href='".api_get_self()."?".api_get_cidreq()."&sort=asc&amp;origin=".$_GET['origin']."'>".Display::return_icon('calendar_up.gif',get_lang('AgendaSortChronologicallyUp')).' '.get_lang("AgendaSortChronologicallyUp")."</a></li>";
  1092.     }
  1093.     else
  1094.     {
  1095.         echo "<li><a href='".api_get_self()."?".api_get_cidreq()."&sort=desc&amp;origin=".$_GET['origin']."'>".Display::return_icon('calendar_down.gif',get_lang('AgendaSortChronologicallyDown')).' '.get_lang("AgendaSortChronologicallyDown")."</a></li>";
  1096.     }
  1097.  
  1098.     // showing the link to show all items or only those of the current month
  1099.     if ($_SESSION['show']=="showcurrent")
  1100.     {
  1101.         echo "<li><a href='".api_get_self()."?".api_get_cidreq()."&action=showall&amp;origin=".$_GET['origin']."'>".Display::return_icon('calendar_select.gif').' '.get_lang("ShowAll")."</a></li>";
  1102.     }
  1103.     else
  1104.     {
  1105.         echo "<li><a href='".api_get_self()."?".api_get_cidreq()."&action=showcurrent&amp;origin=".$_GET['origin']."'>".Display::return_icon('calendar_month.gif').' '.get_lang("ShowCurrent")."</a></li>";
  1106.     }
  1107.     
  1108.     if ($_SESSION['view'<> 'month')
  1109.     {
  1110.         echo "\t<li><a href=\"".api_get_self()."?action=view&amp;view=month\"><img src=\"../img/calendar_month.gif\" border=\"0\" alt=\"".get_lang('MonthView')."\" /> ".get_lang('MonthView')."</a></li>\n";
  1111.     }
  1112.     else 
  1113.     {
  1114.         echo "\t<li><a href=\"".api_get_self()."?action=view&amp;view=list\"><img src=\"../img/calendar_select.gif\" border=\"0\" alt=\"".get_lang('ListView')."\" /> ".get_lang('ListView')."</a></li>\n";
  1115.     }
  1116. }
  1117.  
  1118.  
  1119.  
  1120. /**
  1121. * get all the information of the agenda_item from the database
  1122. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1123. @param integer the id of the agenda item we are getting all the information of
  1124. @return an associative array that contains all the information of the agenda item. The keys are the database fields
  1125. */
  1126. function get_agenda_item($id)
  1127. {
  1128.     global $TABLEAGENDA;
  1129.     $t_agenda_repeat Database::get_course_table(TABLE_AGENDA_REPEAT);
  1130.     $item array();
  1131.     if(empty($id))
  1132.     {
  1133.         $id=(int)addslashes($_GET['id']);
  1134.     }
  1135.     else
  1136.     {
  1137.         $id = (int) $id;
  1138.     }
  1139.     if(empty($id)){return $item;}
  1140.     $sql                     "SELECT * FROM ".$TABLEAGENDA." WHERE id='".$id."'";
  1141.     $result                    api_sql_query($sql,__FILE__,__LINE__);
  1142.     $entry_to_edit             Database::fetch_array($result);
  1143.     $item['title']            $entry_to_edit["title"];
  1144.     $item['content']        $entry_to_edit["content"];
  1145.     $item['start_date']        $entry_to_edit["start_date"];
  1146.     $item['end_date']        $entry_to_edit["end_date"];
  1147.     $item['to']                load_edit_users(TOOL_CALENDAR_EVENT$id);
  1148.     // if the item has been sent to everybody then we show the compact to form
  1149.     if ($item['to']=="everyone")
  1150.     {
  1151.         $_SESSION['allow_individual_calendar']="hide";
  1152.     }
  1153.     else
  1154.     {
  1155.         $_SESSION['allow_individual_calendar']="show";
  1156.     }
  1157.     $item['repeat'false;
  1158.     $sql "SELECT * FROM $t_agenda_repeat WHERE cal_id = $id";
  1159.     $res Database::query($sql,__FILE__,__LINE__);
  1160.     if(Database::num_rows($res)>0)
  1161.     {
  1162.         //this event is repetitive
  1163.         $row Database::fetch_array($res);
  1164.         $item['repeat'true;
  1165.         $item['repeat_type'$row['cal_type'];
  1166.         $item['repeat_end'$row['cal_end'];
  1167.         $item['repeat_frequency'$row['cal_frequency']//unused in 1.8.5 RC1 - will be used later to say if once every 2 or 3 weeks, for example
  1168.         $item['repeat_days'$row['cal_days']//unused in 1.8.5 RC1 - will be used later
  1169.     }
  1170.     //TODO - add management of repeat exceptions
  1171.     return $item;
  1172. }
  1173. /**
  1174. * This is the function that updates an agenda item. It does 3 things
  1175. * 1. storethe start_date, end_date, title and message in the calendar_event table
  1176. * 2. store the groups/users who this message is meant for in the item_property table
  1177. * 3. modify the attachments (if needed)
  1178. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1179. */
  1180. {
  1181.     global $_user$_course;
  1182.  
  1183.     // database definitions
  1184.     $TABLE_ITEM_PROPERTY Database::get_course_table(TABLE_ITEM_PROPERTY);
  1185.  
  1186.     // STEP 1: editing the calendar_event table
  1187.     // 1.a.  some filtering of the input data
  1188.     $id=(int)$_POST['id'];
  1189.     $title=strip_tags(trim($_POST['title']))// no html allowed in the title
  1190.     $content=trim($_POST['content']);
  1191.     $start_date=(int)$_POST['fyear']."-".(int)$_POST['fmonth']."-".(int)$_POST['fday']." ".(int)$_POST['fhour'].":".(int)$_POST['fminute'].":00";
  1192.     $end_date=(int)$_POST['end_fyear']."-".(int)$_POST['end_fmonth']."-".(int)$_POST['end_fday']." ".(int)$_POST['end_fhour'].":".(int)$_POST['end_fminute'].":00";
  1193.     $to=$_POST['selectedform'];
  1194.     // 1.b. the actual saving in calendar_event table
  1195.     $edit_result=save_edit_agenda_item($id,$title,$content,$start_date,$end_date);
  1196.  
  1197.     // step 2: editing the item_propery table (=delete all and add the new destination users/groups)
  1198.     if ($edit_result=true)
  1199.     {
  1200.         // 2.a. delete everything for the users
  1201.         $sql_delete="DELETE FROM ".$TABLE_ITEM_PROPERTY." WHERE ref='$id' AND tool='".TOOL_CALENDAR_EVENT."'";
  1202.  
  1203.         $result api_sql_query($sql_delete,__FILE__,__LINE__or die (Database::error());
  1204.         // 2.b. storing the new users/groups
  1205.         if (!is_null($to)) // !is_null($to): when no user is selected we send it to everyone
  1206.         {
  1207.             $send_to=separate_users_groups($to);
  1208.             // storing the selected groups
  1209.             if (is_array($send_to['groups']))
  1210.             {
  1211.                 foreach ($send_to['groups'as $group)
  1212.                 {
  1213.                     api_item_property_update($_courseTOOL_CALENDAR_EVENT$id,"AgendaModified"$_user['user_id']$group,'',$start_date$end_date);
  1214.                 }
  1215.             }
  1216.             // storing the selected users
  1217.             if (is_array($send_to['users']))
  1218.             {
  1219.                 foreach ($send_to['users'as $user)
  1220.                 {
  1221.                     api_item_property_update($_courseTOOL_CALENDAR_EVENT$id,"AgendaModified"$_user['user_id'],'',$user$start_date,$end_date);
  1222.                 }
  1223.             }
  1224.         }
  1225.         else // the message is sent to everyone, so we set the group to 0
  1226.         {
  1227.             api_item_property_update($_courseTOOL_CALENDAR_EVENT$id,"AgendaModified"$_user['user_id']'','',$start_date,$end_date);
  1228.         }
  1229.  
  1230.     //if ($edit_result=true)
  1231.  
  1232.     // step 3: update the attachments (=delete all and add those in the session
  1233.     update_added_resources("Agenda"$id);
  1234.  
  1235.     // return the message;
  1236.     Display::display_normal_message(get_lang("EditSuccess"));
  1237.  
  1238. }
  1239.  
  1240. /**
  1241. * This function stores the Agenda Item in the table calendar_event and updates the item_property table also (after an edit)
  1242. @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1243. */
  1244. function save_edit_agenda_item($id,$title,$content,$start_date,$end_date)
  1245. {
  1246.     $TABLEAGENDA         Database::get_course_table(TABLE_AGENDA);
  1247.  
  1248.     // store the modifications in the table calendar_event
  1249.     $sql "UPDATE ".$TABLEAGENDA."
  1250.                                 SET title='".$title."',
  1251.                                     content='".$content."',
  1252.                                     start_date='".$start_date."',
  1253.                                     end_date='".$end_date."'
  1254.                                 WHERE id='".$id."'";
  1255.     $result api_sql_query($sql,__FILE__,__LINE__or die (Database::error());
  1256.     return true;
  1257. }
  1258.  
  1259. /**
  1260. * This is the function that deletes an agenda item.
  1261. * The agenda item is no longer fycically deleted but the visibility in the item_property table is set to 2
  1262. * which means that it is invisible for the student AND course admin. Only the platform administrator can see it.
  1263. * This will in a later stage allow the platform administrator to recover resources that were mistakenly deleted
  1264. * by the course administrator
  1265. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1266. @param integer the id of the agenda item wa are deleting
  1267. */
  1268. function delete_agenda_item($id)
  1269. {
  1270.     global $_course;
  1271.     if (is_allowed_to_edit()  OR (api_get_course_setting('allow_user_edit_agenda'&& !api_is_anonymous()))
  1272.     {
  1273.         if (!empty($_GET['id']&& isset($_GET['action']&& $_GET['action']=="delete")
  1274.         {
  1275.             $t_agenda     Database::get_course_table(TABLE_AGENDA);
  1276.             $t_agenda_r   Database::get_course_table(TABLE_AGENDA_REPEAT);
  1277.             $id=(int)addslashes($_GET['id']);
  1278.             $sql "SELECT * FROM $t_agenda_r WHERE cal_id = $id";
  1279.             $res Database::query($sql,__FILE__,__LINE__);
  1280.             if(Database::num_rows($res)>0)
  1281.             {
  1282.                 $sql_children "SELECT * FROM $t_agenda WHERE parent_event_id = $id";
  1283.                 $res_children Database::query($sql_children,__FILE__,__LINE__);
  1284.                 if(Database::num_rows($res_children)>0)
  1285.                 {
  1286.                     while ($row_child Database::fetch_array($res_children))
  1287.                     {
  1288.                         api_item_property_update($_course,TOOL_CALENDAR_EVENT,$row_child['id'],'delete',api_get_user_id());            
  1289.                     }
  1290.                 }
  1291.                 $sql_del "DELETE FROM $t_agenda_r WHERE cal_id = $id";
  1292.                 $res_del Database::query($sql_del,__FILE__,__LINE__);
  1293.             }
  1294.             //$sql = "DELETE FROM ".$TABLEAGENDA." WHERE id='$id'";
  1295.             //$sql= "UPDATE ".$TABLE_ITEM_PROPERTY." SET visibility='2' WHERE tool='Agenda' and ref='$id'";
  1296.             //$result = api_sql_query($sql,__FILE__,__LINE__) or die (Database::error());
  1297.             api_item_property_update($_course,TOOL_CALENDAR_EVENT,$id,'delete',api_get_user_id());
  1298.  
  1299.             // delete the resources that were added to this agenda item
  1300.             // 2DO: as we no longer fysically delete the agenda item (to make it possible to 'restore'
  1301.             //        deleted items, we should not delete the added resources either.
  1302.             // delete_added_resource("Agenda", $id); // -> this is no longer needed as the message is not really deleted but only visibility=2 (only platform admin can see it)
  1303.  
  1304.             //resetting the $id;
  1305.             $id=null;
  1306.  
  1307.             // displaying the result message in the yellow box
  1308.             Display::display_normal_message(get_lang("AgendaDeleteSuccess"));
  1309.         }      // if (isset($id)&&$id&&isset($action)&&$action=="delete")
  1310.     // if ($is_allowed_to_edit)
  1311.  
  1312. }
  1313. /**
  1314. * Makes an agenda item visible or invisible for a student
  1315. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1316. @param integer id the id of the agenda item we are changing the visibility of
  1317. */
  1318. function showhide_agenda_item($id)
  1319. {
  1320.     global $nameTools;
  1321.     /*==================================================
  1322.                 SHOW / HIDE A CALENDAR ITEM
  1323.       ==================================================*/
  1324.     //  and $_GET['isStudentView']<>"false" is added to prevent that the visibility is changed after you do the following:
  1325.     // change visibility -> studentview -> course manager view
  1326.     if ((is_allowed_to_edit(OR (api_get_course_setting('allow_user_edit_agenda'&& !api_is_anonymous())) and $_GET['isStudentView']<>"false")
  1327.     {
  1328.         if (isset($_GET['id'])&&$_GET['id']&&isset($_GET['action'])&&$_GET['action']=="showhide")
  1329.         {
  1330.             $id=(int)addslashes($_GET['id']);
  1331.             change_visibility($nameTools,$id);
  1332.             Display::display_normal_message(get_lang("VisibilityChanged"));
  1333.         }
  1334.     }
  1335. }
  1336. /**
  1337. * Displays all the agenda items
  1338. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1339. @author Yannick Warnier <yannick.warnier@dokeos.com> - cleanup
  1340. */
  1341. {
  1342.     global $TABLEAGENDA;
  1343.     global $TABLE_ITEM_PROPERTY;
  1344.     global $select_month$select_year;
  1345.     global $DaysShort$DaysLong$MonthsLong;
  1346.     global $is_courseAdmin;
  1347.     global $dateFormatLong$timeNoSecFormat,$charset$_user$_course;
  1348.  
  1349.     // getting the group memberships
  1350.     $group_memberships=GroupManager::get_group_ids($_course['dbName'],$_user['user_id']);
  1351.  
  1352.     // getting the name of the groups
  1353.     $group_names=get_course_groups();
  1354.  
  1355.     /*--------------------------------------------------
  1356.             CONSTRUCT THE SQL STATEMENT
  1357.       --------------------------------------------------*/
  1358.  
  1359.     $start 0;
  1360.     $stop 0;
  1361.     // this is to make a difference between showing everything (all months) or only the current month)
  1362.     // $show_all_current is a part of the sql statement
  1363.     if ($_SESSION['show']!=="showall")
  1364.     {
  1365.         $show_all_current=" AND MONTH(start_date)=$select_month AND year(start_date)=$select_year";
  1366.         $start mktime(0,0,0,$select_month,1,$select_year);
  1367.         $stop 0;
  1368.         if(empty($select_year)){$select_year date('Y');}
  1369.         if(empty($select_month)){$select_month date('m');}
  1370.         if($select_month==12)
  1371.         {
  1372.             $stop mktime(0,0,0,1,1,$select_year+1)-1;
  1373.         }
  1374.         else
  1375.         {
  1376.             $stop mktime(0,0,0,$select_month+1,1,$select_year)-1;
  1377.         }
  1378.     }
  1379.     else
  1380.     {
  1381.         $show_all_current="";
  1382.         $start time();
  1383.         $stop mktime(0,0,0,1,1,2038);//by default, set year to maximum for mktime()
  1384.     }
  1385.  
  1386.     // by default we use the id of the current user. The course administrator can see the agenda of other users by using the user / group filter
  1387.     $user_id=$_user['user_id'];
  1388.     if ($_SESSION['user']!==null)
  1389.     {
  1390.         $user_id=$_SESSION['user'];
  1391.     }
  1392.     if ($_SESSION['group']!==null)
  1393.     {
  1394.         $group_id=$_SESSION['group'];
  1395.     }
  1396.     if ($_SESSION['toolgroup']!==null)
  1397.     {
  1398.         $group_id=$_SESSION['toolgroup'];
  1399.     }
  1400.     
  1401.     $repeats array()//placeholder for repeated events
  1402.     //echo "user:".$_SESSION['user']."group: ".$_SESSION['group'];
  1403.     // A. you are a course admin
  1404.     //if ($is_courseAdmin)
  1405.     if (is_allowed_to_edit(OR (api_get_course_setting('allow_user_edit_agenda'&& !api_is_anonymous()))
  1406.     {
  1407.         // A.1. you are a course admin with a USER filter
  1408.         // => see only the messages of this specific user + the messages of the group (s)he is member of.
  1409.         if (!empty($_SESSION['user']))
  1410.         {
  1411.             $group_memberships=GroupManager::get_group_ids($_course['dbName'],$_SESSION['user']);
  1412.             if (is_array($group_memberships&& count($group_memberships)>0)
  1413.             {
  1414.                 $sql="SELECT
  1415.                     agenda.*, toolitemproperties.*
  1416.                     FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." toolitemproperties
  1417.                     WHERE agenda.id = toolitemproperties.ref   ".$show_all_current."
  1418.                     AND toolitemproperties.tool='".TOOL_CALENDAR_EVENT."'
  1419.                     AND    ( toolitemproperties.to_user_id=$user_id OR toolitemproperties.to_group_id IN (0, ".implode(", "$group_memberships).") )
  1420.                     AND toolitemproperties.visibility='1'
  1421.                     ORDER BY start_date ".$_SESSION['sort'];
  1422.             }
  1423.             else
  1424.             {
  1425.                 $sql="SELECT
  1426.                     agenda.*, toolitemproperties.*
  1427.                     FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." toolitemproperties
  1428.                     WHERE agenda.id = toolitemproperties.ref   ".$show_all_current."
  1429.                     AND toolitemproperties.tool='".TOOL_CALENDAR_EVENT."'
  1430.                     AND ( toolitemproperties.to_user_id=$user_id OR toolitemproperties.to_group_id='0')
  1431.                     AND toolitemproperties.visibility='1'
  1432.                     ORDER BY start_date ".$_SESSION['sort'];
  1433.             }
  1434.         }
  1435.         // A.2. you are a course admin with a GROUP filter
  1436.         // => see only the messages of this specific group
  1437.         elseif (!empty($_SESSION['group']))
  1438.         {
  1439.             $sql="SELECT
  1440.                 agenda.*, toolitemproperties.*
  1441.                 FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." toolitemproperties
  1442.                 WHERE agenda.id = toolitemproperties.ref  ".$show_all_current."
  1443.                 AND toolitemproperties.tool='".TOOL_CALENDAR_EVENT."'
  1444.                 AND ( toolitemproperties.to_group_id=$group_id OR toolitemproperties.to_group_id='0')
  1445.                 AND toolitemproperties.visibility='1'
  1446.                 GROUP BY toolitemproperties.ref
  1447.                 ORDER BY start_date ".$_SESSION['sort'];
  1448.         }
  1449.         // A.3 you are a course admin without any group or user filter
  1450.         else
  1451.         {
  1452.             // A.3.a you are a course admin without user or group filter but WITH studentview
  1453.             // => see all the messages of all the users and groups without editing possibilities
  1454.             if ($_GET['isStudentView']=='true')
  1455.             {
  1456.                 $sql="SELECT
  1457.                     agenda.*, toolitemproperties.*
  1458.                     FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." toolitemproperties
  1459.                     WHERE agenda.id = toolitemproperties.ref  ".$show_all_current."
  1460.                     AND toolitemproperties.tool='".TOOL_CALENDAR_EVENT."'
  1461.                     AND toolitemproperties.visibility='1'
  1462.                     GROUP BY toolitemproperties.ref
  1463.                     ORDER BY start_date ".$_SESSION['sort'];
  1464.  
  1465.             }
  1466.             // A.3.b you are a course admin without user or group filter and WITHOUT studentview (= the normal course admin view)
  1467.             // => see all the messages of all the users and groups with editing possibilities
  1468.             else
  1469.             {
  1470.                 $sql="SELECT
  1471.                     agenda.*, toolitemproperties.*
  1472.                     FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." toolitemproperties
  1473.                     WHERE agenda.id = toolitemproperties.ref  ".$show_all_current."
  1474.                     AND toolitemproperties.tool='".TOOL_CALENDAR_EVENT."'
  1475.                     AND ( toolitemproperties.visibility='0' or toolitemproperties.visibility='1')
  1476.                     GROUP BY toolitemproperties.ref
  1477.                     ORDER BY start_date ".$_SESSION['sort'];
  1478.             }
  1479.         }
  1480.  
  1481.     //if (is_allowed_to_edit() OR( api_get_course_setting('allow_user_edit_agenda') && !api_is_anonymous()))
  1482.  
  1483.     // B. you are a student
  1484.     else
  1485.     {
  1486.         if (is_array($group_membershipsand count($group_memberships)>0)
  1487.         {
  1488.             $sql="SELECT
  1489.                 agenda.*, toolitemproperties.*
  1490.                 FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." toolitemproperties
  1491.                 WHERE agenda.id = toolitemproperties.ref   ".$show_all_current."
  1492.                 AND toolitemproperties.tool='".TOOL_CALENDAR_EVENT."'
  1493.                 AND    ( toolitemproperties.to_user_id=$user_id OR toolitemproperties.to_group_id IN (0, ".implode(", "$group_memberships).") )
  1494.                 AND toolitemproperties.visibility='1'
  1495.                 ORDER BY start_date ".$_SESSION['sort'];
  1496.         }
  1497.         else
  1498.         {
  1499.             if ($_user['user_id'])
  1500.             {
  1501.                 $sql="SELECT
  1502.                     agenda.*, toolitemproperties.*
  1503.                     FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." toolitemproperties
  1504.                     WHERE agenda.id = toolitemproperties.ref   ".$show_all_current."
  1505.                     AND toolitemproperties.tool='".TOOL_CALENDAR_EVENT."'
  1506.                     AND ( toolitemproperties.to_user_id=$user_id OR toolitemproperties.to_group_id='0')
  1507.                     AND toolitemproperties.visibility='1'
  1508.                     ORDER BY start_date ".$_SESSION['sort'];
  1509.             }
  1510.             else
  1511.             {
  1512.                 $sql="SELECT
  1513.                     agenda.*, toolitemproperties.*
  1514.                     FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." toolitemproperties
  1515.                     WHERE agenda.id = toolitemproperties.ref   ".$show_all_current."
  1516.                     AND toolitemproperties.tool='".TOOL_CALENDAR_EVENT."'
  1517.                     AND toolitemproperties.to_group_id='0'
  1518.                     AND toolitemproperties.visibility='1'
  1519.                     ORDER BY start_date ".$_SESSION['sort'];
  1520.             }
  1521.         }
  1522.     // you are a student
  1523.  
  1524.     //echo "<pre>".$sql."</pre>";
  1525.     $result=api_sql_query($sql,__FILE__,__LINE__or die(Database::error());
  1526.     $number_items=Database::num_rows($result);
  1527.  
  1528.     /*--------------------------------------------------
  1529.             DISPLAY: NO ITEMS
  1530.       --------------------------------------------------*/
  1531.     if ($number_items==0)
  1532.     {
  1533.         echo "<table id=\"agenda_list\" ><tr><td>".get_lang("NoAgendaItems")."</td></tr></table>";
  1534.     }
  1535.  
  1536.     /*--------------------------------------------------
  1537.             DISPLAY: THE ITEMS
  1538.       --------------------------------------------------*/
  1539.  
  1540.     $month_bar="";
  1541.     $event_list="";
  1542.     $counter=0;
  1543.     $export_icon api_get_path('WEB_IMG_PATH').'export.png';
  1544.     $export_icon_low api_get_path('WEB_IMG_PATH').'export_low_fade.png';
  1545.     $export_icon_high api_get_path('WEB_IMG_PATH').'export_high_fade.png';
  1546.     
  1547.     while($myrow=Database::fetch_array($result))
  1548.     {
  1549.         $is_repeated !empty($myrow['parent_event_id']);
  1550.         echo '<table class="data_table">',"\n";
  1551.         /*--------------------------------------------------
  1552.                 display: the month bar
  1553.          --------------------------------------------------*/
  1554.         // Make the month bar appear only once.
  1555.         if ($month_bar != date("m",strtotime($myrow["start_date"])).date("Y",strtotime($myrow["start_date"])))
  1556.         {
  1557.             $month_bar date("m",strtotime($myrow["start_date"])).date("Y",strtotime($myrow["start_date"]));
  1558.             echo "\t<tr>\n\t\t<td class=\"agenda_month_divider\" colspan=\"3\" valign=\"top\">".
  1559.             ucfirst(format_locale_date("%B %Y",strtotime($myrow["start_date"]))).
  1560.             "</td>\n\t</tr>\n";
  1561.         }
  1562.  
  1563.         /*--------------------------------------------------
  1564.          display: the icon, title, destinees of the item
  1565.          -------------------------------------------------*/
  1566.         echo '<tr>';
  1567.     
  1568.         // highlight: if a date in the small calendar is clicked we highlight the relevant items
  1569.         $db_date=(int)date("d",strtotime($myrow["start_date"])).date("n",strtotime($myrow["start_date"])).date("Y",strtotime($myrow["start_date"]));
  1570.         if ($_GET["day"].$_GET["month"].$_GET["year"<>$db_date)
  1571.         {
  1572.             if ($myrow['visibility']=='0')
  1573.             {
  1574.                 $style="data_hidden";
  1575.                 $stylenotbold="datanotbold_hidden";
  1576.                 $text_style="text_hidden";
  1577.             }
  1578.             else
  1579.             {
  1580.                 $style="data";
  1581.                 $stylenotbold="datanotbold";
  1582.                 $text_style="text";
  1583.             }
  1584.     
  1585.         }
  1586.         else
  1587.         {
  1588.             $style="datanow";
  1589.             $stylenotbold="datanotboldnow";
  1590.             $text_style="textnow";
  1591.         }
  1592.     
  1593.         echo "\t\t<th>\n";
  1594.         // adding an internal anchor
  1595.         echo "\t\t\t<a name=\"".(int)date("d",strtotime($myrow["start_date"]))."\"></a>";
  1596.         // the icons. If the message is sent to one or more specific users/groups
  1597.         // we add the groups icon
  1598.         // 2do: if it is sent to groups we display the group icon, if it is sent to a user we show the user icon
  1599.         Display::display_icon('agenda.gif'get_lang('Agenda'));
  1600.         if ($myrow['to_group_id']!=='0')
  1601.         {
  1602.             echo "<img src=\"../img/group.gif\" border=\"0\" alt=\"".get_lang('Group')."\"/>";
  1603.         }
  1604.         echo " ".$myrow['title']."\n";
  1605.         echo "\t\t</th>\n";
  1606.     
  1607.         // the message has been sent to
  1608.         echo "\t\t<th>".get_lang("SentTo").": ";
  1609.         $sent_to=sent_to(TOOL_CALENDAR_EVENT$myrow["ref"]);
  1610.         $sent_to_form=sent_to_form($sent_to);
  1611.         echo $sent_to_form;
  1612.         echo "</th>";
  1613.     
  1614.         if (!$is_repeated && (is_allowed_to_edit(OR (api_get_course_setting('allow_user_edit_agenda'&& !api_is_anonymous())))
  1615.         {
  1616.             echo '<th>'.get_lang('Modify');    
  1617.             echo '</th></tr>';
  1618.         }
  1619.         
  1620.         /*--------------------------------------------------
  1621.                  display: the title
  1622.          --------------------------------------------------*/
  1623.         echo "<tr class='row_odd'>";
  1624.         echo "\t\t<td>".get_lang("StartTimeWindow").": ";
  1625.         echo ucfirst(format_locale_date($dateFormatLong,strtotime($myrow["start_date"])))."&nbsp;&nbsp;&nbsp;";
  1626.         echo ucfirst(strftime($timeNoSecFormat,strtotime($myrow["start_date"])))."";
  1627.         echo "</td>\n";
  1628.         echo "\t\t<td>";
  1629.         if ($myrow["end_date"]<>"0000-00-00 00:00:00")
  1630.         {
  1631.             echo get_lang("EndTimeWindow").": ";
  1632.             echo ucfirst(format_locale_date($dateFormatLong,strtotime($myrow["end_date"])))."&nbsp;&nbsp;&nbsp;";
  1633.             echo ucfirst(strftime($timeNoSecFormat,strtotime($myrow["end_date"])))."";
  1634.         }
  1635.         echo "</td>\n";
  1636.         
  1637.         /*--------------------------------------------------
  1638.          display: edit delete button (course admin only)
  1639.          --------------------------------------------------*/
  1640.     
  1641.         
  1642.         if (!$is_repeated && (is_allowed_to_edit(OR (api_get_course_setting('allow_user_edit_agenda'&& !api_is_anonymous())))
  1643.         {
  1644.             echo '<td align="center">';
  1645.             // edit
  1646.             echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&origin='.$_GET['origin'].'&amp;action=edit&amp;id='.$myrow['id'].'" title="'.get_lang("ModifyCalendarItem").'">';
  1647.             echo "<img src=\"../img/edit.gif\" border=\"0\" alt=\"".get_lang("ModifyCalendarItem")."\" /></a>";
  1648.             
  1649.             echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&origin=".$_GET['origin']."&amp;action=delete&amp;id=".$myrow['id']."\" onclick=\"javascript:if(!confirm('".addslashes(htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES,$charset))."')) return false;\"  title=\"".get_lang("Delete")."\">";
  1650.             echo "<img src=\"../img/delete.gif\" border=\"0\" alt=\"".get_lang("Delete")."\"/></a>";
  1651.                   
  1652.             echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&origin='.$_GET['origin'].'&amp;action=announce&amp;id='.$myrow['id'].'" title="'.get_lang("AddAnnouncement").'">';                
  1653.             echo "<img src=\"../img/announce_add.gif\" border=\"0\" alt=\"".get_lang("AddAnnouncement")."\"/></a>";
  1654.             if ($myrow['visibility']==1)
  1655.             {
  1656.                 $image_visibility="visible";
  1657.             }
  1658.             else
  1659.             {
  1660.                 $image_visibility="invisible";
  1661.             }
  1662.             echo     '<a href="'.api_get_self().'?'.api_get_cidreq().'&origin='.$_GET['origin'].'&amp;action=showhide&amp;id='.$myrow['id'].'" title="'.get_lang("langVisible").'">',
  1663.                     '<img src="../img/'.$image_visibility.'.gif" border="0" alt="'.get_lang("Visible").'" /></a>';
  1664.             echo '</td>';    
  1665.         }
  1666.         echo '</tr>';
  1667.     
  1668.         echo '<tr class="row_even">';
  1669.         
  1670.         if (!$is_repeated && (is_allowed_to_edit(OR (api_get_course_setting('allow_user_edit_agenda'&& !api_is_anonymous())))
  1671.         {
  1672.             $td_colspan'<td colspan="3">';
  1673.         }
  1674.         else
  1675.         {
  1676.             $td_colspan'<td colspan="2">';
  1677.         }
  1678.         
  1679.         
  1680.         /*--------------------------------------------------
  1681.                  display: the content
  1682.          --------------------------------------------------*/
  1683.         $content $myrow['content'];
  1684.         $content make_clickable($content);
  1685.         $content text_filter($content);
  1686.         echo "<tr class='row_even'>";
  1687.         echo $td_colspan;    
  1688.         echo '<a class="ical_export" href="ical_export.php?'.api_get_cidreq().'&type=course&id='.$myrow['id'].'&class=confidential" title="'.get_lang('ExportiCalConfidential').'"><img src="'.$export_icon_high.'" alt="'.get_lang('ExportiCalConfidential').'"/></a>';
  1689.         echo '<a class="ical_export" href="ical_export.php?'.api_get_cidreq().'&type=course&id='.$myrow['id'].'&class=private" title="'.get_lang('ExportiCalPrivate').'"><img src="'.$export_icon_low.'" alt="'.get_lang('ExportiCalPrivate').'"/></a>';
  1690.         echo '<a class="ical_export" href="ical_export.php?'.api_get_cidreq().'&type=course&id='.$myrow['id'].'&class=public" title="'.get_lang('ExportiCalPublic').'"><img src="'.$export_icon.'" alt="'.get_lang('ExportiCalPublic').'"/></a>';
  1691.         echo '<a href="#" onclick="javascript:win_print=window.open(\'print.php?id='.$myrow['id'].'\',\'popup\',\'left=100,top=100,width=700,height=500,scrollbars=1,resizable=0\'); win_print.focus(); return false;">'.Display::return_icon('print.gif'get_lang('Print')).'</a>&nbsp;';
  1692.         echo $content;
  1693.         echo '</td></tr>';
  1694.     
  1695.         /*--------------------------------------------------
  1696.                  display: the added resources
  1697.          --------------------------------------------------*/
  1698.         if (check_added_resources("Agenda"$myrow["id"]))
  1699.         {
  1700.             
  1701.             echo '<tr>';
  1702.             echo $td_colspan;        
  1703.             echo "<i>".get_lang("AddedResources")."</i><br/>";
  1704.             if ($myrow['visibility']==0)
  1705.             {
  1706.                 $addedresource_style="invisible";
  1707.             }
  1708.             display_added_resources("Agenda"$myrow["id"]$addedresource_style);
  1709.             echo "</td></tr>";
  1710.         }
  1711.     
  1712.     
  1713.         $event_list.=$myrow['id'].',';
  1714.     
  1715.         $counter++;
  1716.     
  1717.         /*--------------------------------------------------
  1718.          display: jump-to-top icon
  1719.          --------------------------------------------------*/
  1720.         echo '<tr>';
  1721.         echo $td_colspan;
  1722.         if($is_repeated){echo get_lang('RepeatedEvent'),'<a href="',api_get_self(),'?',api_get_cidreq,'&agenda_id=',$myrow['parent_event_id'],'" alt="',get_lang('RepeatedEventViewOriginalEvent'),'">',get_lang('RepeatedEventViewOriginalEvent'),'</a>';}
  1723.         echo "<a href=\"#top\"><img src=\"../img/top.gif\" border=\"0\" alt=\"to top\" align=\"right\" /></a></td></tr>";
  1724.         echo "</table><br /><br />";
  1725.     // end while ($myrow=Database::fetch_array($result))
  1726.  
  1727.     if(!empty($event_list))
  1728.     {
  1729.         $event_list=substr($event_list,0,-1);
  1730.     }
  1731.     else
  1732.     {
  1733.         $event_list='0';
  1734.     }
  1735.     
  1736.     echo "<form name=\"event_list_form\"><input type=\"hidden\" name=\"event_list\" value=\"$event_list\" /></form>";
  1737.     
  1738.     // closing the layout table
  1739.     echo "</td>",
  1740.         "</tr>",
  1741.         "</table>";
  1742. }
  1743.  
  1744. /**
  1745. * Displays only 1 agenda item. This is used when an agenda item is added to the learning path.
  1746. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1747. */
  1748. function display_one_agenda_item($agenda_id)
  1749. {
  1750.     global $TABLEAGENDA;
  1751.     global $TABLE_ITEM_PROPERTY;
  1752.     global $select_month$select_year;
  1753.     global $DaysShort$DaysLong$MonthsLong;
  1754.     global $is_courseAdmin;
  1755.     global $dateFormatLong$timeNoSecFormat$charset;
  1756.     global $_user;
  1757.     //echo "displaying agenda items";
  1758.  
  1759.  
  1760.     // getting the name of the groups
  1761.     $group_names=get_course_groups();
  1762.  
  1763.     /*--------------------------------------------------
  1764.             CONSTRUCT THE SQL STATEMENT
  1765.       --------------------------------------------------*/
  1766.  
  1767.     $sql="SELECT agenda.*, toolitemproperties.*
  1768.                     FROM ".$TABLEAGENDA." agenda, ".$TABLE_ITEM_PROPERTY." toolitemproperties
  1769.                     WHERE agenda.id = toolitemproperties.ref
  1770.                     AND toolitemproperties.tool='".TOOL_CALENDAR_EVENT."'
  1771.                     AND toolitemproperties.visibility='1'
  1772.                     AND agenda.id='$agenda_id'";
  1773.     $result=api_sql_query($sql,__FILE__,__LINE__or die(Database::error());
  1774.     $number_items=Database::num_rows($result);
  1775.     $myrow=Database::fetch_array($result)// there should be only one item so no need for a while loop
  1776.     
  1777.     $sql_rep "SELECT * FROM $TABLEAGENDA WHERE id = $agenda_id AND parent_event_id IS NOT NULL AND parent_event_id !=0";
  1778.     $res_rep Database::query($sql_rep,__FILE__,__LINE__);
  1779.     $repeat false;
  1780.     $repeat_id 0;
  1781.     if(Database::num_rows($res_rep)>0)
  1782.     {
  1783.         $repeat=true;
  1784.         $row_rep Database::fetch_array($res_rep);
  1785.         $repeat_id $row_rep['parent_event_id']
  1786.     }
  1787.     
  1788.     /*--------------------------------------------------
  1789.             DISPLAY: NO ITEMS
  1790.       --------------------------------------------------*/
  1791.     if ($number_items==0)
  1792.     {
  1793.         echo "<table id=\"agenda_list\" ><tr><td>".get_lang("NoAgendaItems")."</td></tr></table>";
  1794.     }
  1795.  
  1796.     /*--------------------------------------------------
  1797.             DISPLAY: THE ITEMS
  1798.       --------------------------------------------------*/
  1799.     echo "<table id=\"agenda_list\">\n";
  1800.  
  1801.     /*--------------------------------------------------
  1802.      DISPLAY : the icon, title, destinees of the item
  1803.       --------------------------------------------------*/
  1804.     echo "\t<tr>\n";
  1805.  
  1806.     // highlight: if a date in the small calendar is clicked we highlight the relevant items
  1807.     $db_date=(int)date("d",strtotime($myrow["start_date"])).date("n",strtotime($myrow["start_date"])).date("Y",strtotime($myrow["start_date"]));
  1808.     if ($_GET["day"].$_GET["month"].$_GET["year"<>$db_date)
  1809.     {
  1810.         if ($myrow['visibility']=='0')
  1811.         {
  1812.             $style="data_hidden";
  1813.             $stylenotbold="datanotbold_hidden";
  1814.             $text_style="text_hidden";
  1815.         }
  1816.         else
  1817.         {
  1818.             $style="data";
  1819.             $stylenotbold="datanotbold";
  1820.             $text_style="text";
  1821.         }
  1822.     }
  1823.     else
  1824.     {
  1825.         $style="datanow";
  1826.         $stylenotbold="datanotboldnow";
  1827.         $text_style="textnow";
  1828.     }
  1829.  
  1830.  
  1831.     echo "\t\t<td class=\"".$style."\">\n";
  1832.  
  1833.     // adding an internal anchor
  1834.     echo "\t\t\t<a name=\"".(int)date("d",strtotime($myrow["start_date"]))."\"></a>";
  1835.  
  1836.     // the icons. If the message is sent to one or more specific users/groups
  1837.     // we add the groups icon
  1838.     // 2do: if it is sent to groups we display the group icon, if it is sent to a user we show the user icon
  1839.     echo Display::return_icon('agenda.gif');
  1840.     if ($myrow['to_group_id']!=='0')
  1841.         {
  1842.         echo Display::return_icon('group.gif')//"<img src=\"../img/group.gif\" border=\"0\" />";
  1843.         }
  1844.     echo " ".$myrow['title']."\n";
  1845.     echo "\t\t</td>\n";
  1846.  
  1847.     // the message has been sent to
  1848.     echo "\t\t<td class=\"".$stylenotbold."\">".get_lang("SentTo").": ";
  1849.     $sent_to=sent_to(TOOL_CALENDAR_EVENT$myrow["ref"]);
  1850.     $sent_to_form=sent_to_form($sent_to);
  1851.     echo $sent_to_form;
  1852.     echo "</td>\n\t</tr>\n";
  1853.  
  1854.     /*--------------------------------------------------
  1855.                  DISPLAY: the title
  1856.       --------------------------------------------------*/
  1857.     echo "\t<tr class=\"".$stylenotbold."\">\n";
  1858.     echo "\t\t<td>".get_lang("StartTime").": ";
  1859.     echo ucfirst(format_locale_date($dateFormatLong,strtotime($myrow["start_date"])))."&nbsp;&nbsp;&nbsp;";
  1860.     echo ucfirst(strftime($timeNoSecFormat,strtotime($myrow["start_date"])))."";
  1861.     echo "</td>\n";
  1862.     echo "\t\t<td>".get_lang("EndTime").": ";
  1863.     echo ucfirst(format_locale_date($dateFormatLong,strtotime($myrow["end_date"])))."&nbsp;&nbsp;&nbsp;";
  1864.     echo ucfirst(strftime($timeNoSecFormat,strtotime($myrow["end_date"])))."";
  1865.     echo "</td>\n";
  1866.     echo "\n\t</tr>\n";
  1867.  
  1868.     /*--------------------------------------------------
  1869.                  DISPLAY: the content
  1870.       --------------------------------------------------*/
  1871.     $export_icon api_get_path('WEB_IMG_PATH').'export.png';
  1872.     $export_icon_low api_get_path('WEB_IMG_PATH').'export_low_fade.png';
  1873.     $export_icon_high api_get_path('WEB_IMG_PATH').'export_high_fade.png';
  1874.  
  1875.     $content $myrow['content'];
  1876.     $content make_clickable($content);
  1877.     $content text_filter($content);
  1878.     //echo "\t<tr>\n\t\t<td class=\"".$text_style."\" colspan='2'>";
  1879.     //echo $content;
  1880.     //echo "</td></tr>";
  1881.     echo "<tr class='row_even'>";
  1882.     echo '<td colspan="2">';   
  1883.     echo '<a class="ical_export" href="ical_export.php?'.api_get_cidreq().'&type=course&id='.$myrow['id'].'&class=confidential" title="'.get_lang('ExportiCalConfidential').'"><img src="'.$export_icon_high.'" alt="'.get_lang('ExportiCalConfidential').'"/></a>';
  1884.     echo '<a class="ical_export" href="ical_export.php?'.api_get_cidreq().'&type=course&id='.$myrow['id'].'&class=private" title="'.get_lang('ExportiCalPrivate').'"><img src="'.$export_icon_low.'" alt="'.get_lang('ExportiCalPrivate').'"/></a>';
  1885.     echo '<a class="ical_export" href="ical_export.php?'.api_get_cidreq().'&type=course&id='.$myrow['id'].'&class=public" title="'.get_lang('ExportiCalPublic').'"><img src="'.$export_icon.'" alt="'.get_lang('ExportiCalPublic').'"/></a>';
  1886.     echo '<a href="#" onclick="javascript:win_print=window.open(\'print.php?id='.$myrow['id'].'\',\'popup\',\'left=100,top=100,width=700,height=500,scrollbars=1,resizable=0\'); win_print.focus(); return false;">'.Display::return_icon('print.gif'get_lang('Print')).'</a>&nbsp;';
  1887.     echo $content;
  1888.     echo '</td></tr>';
  1889.  
  1890.     /*--------------------------------------------------
  1891.                  DISPLAY: the added resources
  1892.       --------------------------------------------------*/
  1893.     if (check_added_resources("Agenda"$myrow["id"]))
  1894.         {
  1895.         echo "<tr><td colspan='2'>";
  1896.         echo "<i>".get_lang("AddedResources")."</i><br/>";
  1897.         if ($myrow['visibility']==0)
  1898.         {
  1899.             $addedresource_style="invisible";
  1900.         }
  1901.         display_added_resources("Agenda"$myrow["id"]$addedresource_style);
  1902.         echo "</td></tr>";
  1903.         }
  1904.  
  1905.     /*--------------------------------------------------
  1906.         DISPLAY: edit delete button (course admin only)
  1907.       --------------------------------------------------*/
  1908.     echo '<tr><td colspan="2">';
  1909.     if (!$repeat && is_allowed_to_edit())
  1910.         {
  1911.         // edit
  1912.         echo     "<a href=\"".api_get_self()."?".api_get_cidreq()."&origin=".$_GET['origin']."&amp;action=edit&amp;id=".$myrow['id']."\">",
  1913.                 "<img src=\"../img/edit.gif\" border=\"0\" alt=\"".get_lang("ModifyCalendarItem")."\" /></a>",
  1914.                 "<a href=\"".api_get_self()."?".api_get_cidreq()."&origin=".$_GET['origin']."&amp;action=delete&amp;id=".$myrow['id']."\" onclick=\"javascript:if(!confirm('".addslashes(htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES,$charset))."')) return false;\">",
  1915.                 "<img src=\"../img/delete.gif\" border=\"0\" alt=\"".get_lang("Delete")."\" /></a>";
  1916.         if ($myrow['visibility']==1)
  1917.             {
  1918.             $image_visibility="visible";
  1919.             }
  1920.         else
  1921.             {
  1922.             $image_visibility="invisible";
  1923.             }
  1924.         echo     "<a href=\"".api_get_self()."?".api_get_cidreq()."&origin=".$_GET['origin']."&amp;action=showhide&amp;id=".$myrow['id']."\">",
  1925.                 "<img src=\"../img/".$image_visibility.".gif\" border=\"0\" alt=\"".get_lang("Visible")."\" /></a><br /><br />";
  1926.         }
  1927.     echo "</td>";
  1928.     if($repeat)
  1929.     {
  1930.         echo '<tr>';
  1931.         echo '<td colspan="2">',get_lang('RepeatedEvent'),'<a href="',api_get_self(),'?',api_get_cidreq(),'&agenda_id=',$repeat_id,'" alt="',get_lang('RepeatedEventViewOriginalEvent'),'">',get_lang('RepeatedEventViewOriginalEvent'),'</a></td>';
  1932.         echo '</tr>';
  1933.     }
  1934.     echo "</table>";
  1935.  
  1936.     // closing the layout table
  1937.     echo "</td>",
  1938.         "</tr>",
  1939.         "</table>";
  1940. }
  1941.  
  1942.  
  1943.  
  1944.  
  1945. /**
  1946. * Show the form for adding a new agenda item. This is the same function that is used whenever we are editing an
  1947. * agenda item. When the id parameter is empty (default behaviour), then we show an empty form, else we are editing and
  1948. * we have to retrieve the information that is in the database and use this information in the forms.
  1949. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1950. @param integer id, the id of the agenda item we are editing. By default this is empty which means that we are adding an
  1951. *          agenda item.
  1952. */
  1953. function show_add_form($id '')
  1954. {
  1955.  
  1956.     global $MonthsLong;
  1957.  
  1958.     // the default values for the forms
  1959.     if ($_GET['originalresource'!== 'no')
  1960.     {
  1961.         $day    date('d');
  1962.         $month    date('m');
  1963.         $year    date('Y');
  1964.         $hours    9;
  1965.         $minutes'00';
  1966.  
  1967.         $end_day    date('d');
  1968.         $end_month    date('m');
  1969.         $end_year    date('Y');
  1970.         $end_hours    17;
  1971.         $end_minutes'00';
  1972.         $repeat false;
  1973.     }
  1974.     else
  1975.     {
  1976.  
  1977.         // we are coming from the resource linker so there might already have been some information in the form.
  1978.         // When we clicked on the button to add resources we stored every form information into a session and now we
  1979.         // are doing the opposite thing: getting the information out of the session and putting it into variables to
  1980.         // display it in the forms.
  1981.         $form_elements=$_SESSION['formelements'];
  1982.         $day=$form_elements['day'];
  1983.         $month=$form_elements['month'];
  1984.         $year=$form_elements['year'];
  1985.         $hours=$form_elements['hour'];
  1986.         $minutes=$form_elements['minutes'];
  1987.         $end_day=$form_elements['end_day'];
  1988.         $end_month=$form_elements['end_month'];
  1989.         $end_year=$form_elements['end_year'];
  1990.         $end_hours=$form_elements['end_hours'];
  1991.         $end_minutes=$form_elements['end_minutes'];
  1992.         $title=$form_elements['title'];
  1993.         $content=$form_elements['content'];
  1994.         $id=$form_elements['id'];
  1995.         $to=$form_elements['to'];
  1996.         $repeat $form_elements['repeat'];
  1997.     }
  1998.  
  1999.  
  2000.     //    switching the send to all/send to groups/send to users
  2001.     if ($_POST['To'])
  2002.     {
  2003.             $day            $_POST['fday'];
  2004.             $month            $_POST['fmonth'];
  2005.             $year            $_POST['fyear'];
  2006.             $hours            $_POST['fhour'];
  2007.             $minutes        $_POST['fminute'];
  2008.             $end_day        $_POST['end_fday'];
  2009.             $end_month        $_POST['end_fmonth'];
  2010.             $end_year        $_POST['end_fyear'];
  2011.             $end_hours        $_POST['end_fhour'];
  2012.             $end_minutes    $_POST['end_fminute'];
  2013.             $title             $_POST['title'];
  2014.             $content        $_POST['content'];
  2015.             // the invisible fields
  2016.             $action            $_POST['action'];
  2017.             $id                $_POST['id'];
  2018.             $repeat         !empty($_POST['repeat'])?true:false;
  2019.         }
  2020.  
  2021.  
  2022.     // if the id is set then we are editing an agenda item
  2023.     if (is_int($id))
  2024.     {
  2025.         //echo "before get_agenda_item".$_SESSION['allow_individual_calendar'];
  2026.         $item_2_edit=get_agenda_item($id);
  2027.         $title    $item_2_edit['title'];
  2028.         $content$item_2_edit['content'];
  2029.         // start date
  2030.         list($datepart$timepartsplit(" "$item_2_edit['start_date']);
  2031.         list($year$month$dayexplode("-"$datepart);
  2032.         list($hours$minutes$secondsexplode(":"$timepart);
  2033.         // end date
  2034.         list($datepart$timepartsplit(" "$item_2_edit['end_date']);
  2035.         list($end_year$end_month$end_dayexplode("-"$datepart);
  2036.         list($end_hours$end_minutes$end_secondsexplode(":"$timepart);
  2037.         // attachments
  2038.         edit_added_resources("Agenda"$id);
  2039.         $to=$item_2_edit['to'];
  2040.         //echo "<br />after get_agenda_item".$_SESSION['allow_individual_calendar'];
  2041.     }
  2042.     $content=stripslashes($content);
  2043.     $title=stripslashes($title);
  2044.     // we start a completely new item, we do not come from the resource linker
  2045.     if ($_GET['originalresource']!=="no" and $_GET['action']=="add")
  2046.     {
  2047.  
  2048.         $_SESSION["formelements"]=null;
  2049.         unset_session_resources();
  2050.     }
  2051. ?>
  2052.  
  2053. <!-- START OF THE FORM  -->
  2054. <form enctype="multipart/form-data" action="<?php echo api_get_self().'?origin='.$_GET['origin'].'&amp;action='.$_GET['action']?>" method="post" name="new_calendar_item">
  2055. <input type="hidden" name="id" value="<?php if (isset($id)) echo $id?>" />
  2056. <input type="hidden" name="action" value="<?php if (isset($_GET['action'])) echo $_GET['action']?>" />
  2057.  
  2058. <table border="0" cellpadding="5" cellspacing="0" width="100%" id="newedit_form">
  2059.     <!-- the title -->
  2060.     <tr class="title">
  2061.         <td>
  2062.         <span style="font-weight: bold;"><?php echo (isset($idAND $id<>'')?get_lang('ModifyCalendarItem'):get_lang("AddCalendarItem")?></span>
  2063.         </td>
  2064.         <td colspan="2" align="right">
  2065.         <label for="ical_import"><?php echo get_lang('ICalFileImport');?></label>
  2066.         <input type="file" name="ical_import"/><input type="submit" name="ical_submit" value="<?php echo get_lang('Ok');?>"/>
  2067.         </td>
  2068.     </tr>
  2069.  
  2070.     <!--  the select specific users / send to all form -->
  2071.     <?php
  2072.     if (isset ($_SESSION['toolgroup']))
  2073.     {
  2074.         echo '<tr id="subtitle">';
  2075.         echo '<td colspan="3">';
  2076.         echo '<input type="hidden" name="selectedform[0]" value="GROUP:'.$_SESSION['toolgroup'].'"/>' ;
  2077.         echo '<input type="hidden" name="To" value="true"/>' ;
  2078.         echo '</td>';
  2079.         echo '</tr>';
  2080.  
  2081.     }
  2082.     else
  2083.     {
  2084.  
  2085.         ?>
  2086.         <tr class="subtitle">
  2087.             <td valign="top" colspan="3">
  2088.                 <?php
  2089.                 // this variable defines if the course administrator can send a message to a specific user / group
  2090.                 // or not
  2091.                 //echo "<input type=\"submit\" name=\"To\" value=\"".get_lang("SelectGroupsUsers")."\" style=\"float:left\">" ;
  2092.  
  2093.                 //echo "sessiewaarde: ".$_SESSION['allow_individual_calendar'];
  2094.                 echo get_lang("SentTo").": ";
  2095.                 if ((isset($_GET['id'])  && $to=='everyone'|| !isset($_GET['id'])){
  2096.                     echo get_lang("Everybody").'&nbsp;';
  2097.                 }
  2098.                 echo '<a href="#" onclick="if(document.getElementById(\'recipient_list\').style.display==\'none\') document.getElementById(\'recipient_list\').style.display=\'block\'; else document.getElementById(\'recipient_list\').style.display=\'none\';">'.get_lang('ModifyRecipientList').'</a>';
  2099.                 show_to_form($to);
  2100.                 if (isset($_GET['id']&& $to!='everyone'){
  2101.                     echo '<script>document.getElementById(\'recipient_list\').style.display=\'block\';</script>';
  2102.                 }
  2103.             ?>
  2104.             <hr noshade="noshade" color="#cccccc" />
  2105.         </td>
  2106.     </tr>
  2107.  
  2108.     <?php
  2109.     }
  2110.     ?>
  2111.  
  2112.     <!-- START date and time -->
  2113.     <tr class="subtitle">
  2114.         <td>
  2115.  
  2116.             <table cellpadding="0" cellspacing="0" border="0" width="100%">
  2117.  
  2118.             <tr><td width="110">
  2119.                 <!-- date: 1 -> 31 -->
  2120.                 <?php echo get_lang('StartDate').": \n"?>
  2121.             </td>
  2122.  
  2123.             <td>
  2124.  
  2125.             <select name="fday" onchange="javascript:document.new_calendar_item.end_fday.value=this.value;">
  2126.                 <?php
  2127.                     // small loop for filling all the dates
  2128.                     // 2do: the available dates should be those of the selected month => february is from 1 to 28 (or 29) and not to 31
  2129.                     echo "\n";
  2130.                     foreach (range(131as $i)
  2131.                     {
  2132.                         // values have to have double digits
  2133.                         $value ($i <= '0'.$i $i );
  2134.                         // the current day is indicated with [] around the date
  2135.                         if ($value==$day)
  2136.                         {
  2137.                             echo "\t\t\t\t <option value=\"".$value."\" selected> ".$i." </option>\n";
  2138.                         }
  2139.                         else
  2140.                         {
  2141.                             echo "\t\t\t\t<option value=\"$value\">$i</option>\n";
  2142.                         }
  2143.                     }
  2144.                      ?>
  2145.             </select>
  2146.  
  2147.             <!-- month: january -> december -->
  2148.             <select name="fmonth" onchange="javascript:document.new_calendar_item.end_fmonth.value=this.value;">
  2149.                 <?php
  2150.                     echo "\n";
  2151.                     for ($i=1$i<=12$i++)
  2152.                     {
  2153.                         // values have to have double digits
  2154.                         if ($i<=9)
  2155.                         {
  2156.                             $value="0".$i;
  2157.                         }
  2158.                         else
  2159.                         {
  2160.                             $value=$i;
  2161.                         }
  2162.                         if ($value==$month)
  2163.                         {
  2164.                             echo "\t\t\t\t <option value=\"".$value."\" selected>".$MonthsLong[$i-1]."</option>\n";
  2165.                         }
  2166.                         else
  2167.                         {
  2168.                             echo "\t\t\t\t <option value=\"".$value."\">".$MonthsLong[$i-1]."</option>\n";
  2169.                         }
  2170.                     ?>
  2171.             </select>
  2172.  
  2173.             <select name="fyear" onchange="javascript:document.new_calendar_item.end_fyear.value=this.value;">
  2174.                 <option value="<?php echo ($year-1)?>"><?php echo ($year-1)?></option>
  2175.                 <option value="<?php echo $year?>" selected="selected"><?php echo $year?></option>
  2176.                 <?php
  2177.                     echo "\n";
  2178.                     for ($i=1$i<=5$i++)
  2179.                     {
  2180.                         $value=$year+$i;
  2181.                         echo "\t\t\t\t<option value=\"$value\">$value</option>\n";
  2182.                     ?>
  2183.             </select>
  2184.             <a href="javascript:openCalendar('new_calendar_item', 'f')"><img src="../img/calendar_select.gif" border="0" alt="Select"/></a>
  2185.             </td></tr></table>
  2186.         </td>
  2187.         <td>
  2188.  
  2189.             <table cellpadding="0" cellspacing="0" border="0" width="100%">
  2190.  
  2191.             <tr><td width="110">
  2192.                 <!-- date: 1 -> 31 -->
  2193.                 <?php echo get_lang('StartTime').": \n"?>
  2194.             </td>
  2195.  
  2196.             <td>
  2197.  
  2198.             <select name="fhour" onchange="javascript:document.new_calendar_item.end_fhour.value=this.value;">
  2199.                 <option value="--">--</option>
  2200.                 <?php
  2201.                     echo "\n";
  2202.                     foreach (range(124as $i)
  2203.                     {
  2204.                         // values have to have double digits
  2205.                         $value ($i <= '0'.$i $i );
  2206.                         // the current hour is indicated with [] around the hour
  2207.                         if ($hours==$value)
  2208.                         {
  2209.                             echo "\t\t\t\t<option value=\"".$value."\" selected> ".$value." </option>\n";
  2210.                         }
  2211.                         else
  2212.                         {
  2213.                             echo "\t\t\t\t<option value=\"$value\">$value</option>\n";
  2214.                         }
  2215.                     ?>
  2216.             </select>
  2217.             <?php echo get_lang('HourMinuteDivider')?>
  2218.             <select name="fminute" onchange="javascript:document.new_calendar_item.end_fminute.value=this.value;">
  2219.                 <option value="<?php echo $minutes ?>"><?php echo $minutes?></option>
  2220.                 <option value="--">--</option>
  2221.                 <?php
  2222.                     foreach (range(059as $i)
  2223.                     {
  2224.                         // values have to have double digits
  2225.                         $value ($i <= '0'.$i $i );
  2226.                         echo "\t\t\t\t<option value=\"$value\">$value</option>\n";
  2227.                     ?>
  2228.             </select>
  2229.             </td></tr></table>
  2230.         </td>
  2231.     </tr>
  2232.     <!-- END date and time -->
  2233.  
  2234.     <tr class="subtitle">
  2235.         <td>
  2236.  
  2237.             <table cellpadding="0" cellspacing="0" border="0" width="100%">
  2238.  
  2239.             <tr><td width="110">
  2240.                 <!-- date: 1 -> 31 -->
  2241.                 <?php echo get_lang('EndDate').": \n"?>
  2242.             </td>
  2243.  
  2244.             <td>
  2245.  
  2246.             <select name="end_fday">
  2247.                 <?php
  2248.                     // small loop for filling all the dates
  2249.                     // 2do: the available dates should be those of the selected month => february is from 1 to 28 (or 29) and not to 31
  2250.                     echo "\n";
  2251.                     foreach (range(131as $i)
  2252.                     {
  2253.                         // values have to have double digits
  2254.                         $value ($i <= '0'.$i $i );
  2255.                         // the current day is indicated with [] around the date
  2256.                         if ($value==$end_day)
  2257.                             echo "\t\t\t\t <option value=\"".$value."\" selected> ".$i." </option>\n";}
  2258.                         else
  2259.                             echo "\t\t\t\t <option value=\"".$value."\">".$i."</option>\n"}
  2260.                         }?>
  2261.                 </select>
  2262.  
  2263.                 <!-- month: january -> december -->
  2264.                 <select name="end_fmonth">
  2265.                     <?php
  2266.                     echo "\n";
  2267.                     foreach (range(112as $i)
  2268.                     {
  2269.                         // values have to have double digits
  2270.                         $value ($i <= '0'.$i $i );
  2271.                         if ($value==$end_month)
  2272.                             echo "\t\t\t\t <option value=\"".$value."\" selected>".$MonthsLong[$i-1]."</option>\n"}
  2273.                         else
  2274.                             echo "\t\t\t\t <option value=\"".$value."\">".$MonthsLong[$i-1]."</option>\n"}
  2275.                         }?>
  2276.                 </select>
  2277.  
  2278.                 <select name="end_fyear">
  2279.                     <option value="<?php echo ($end_year-1?>"><?php echo ($end_year-1?></option>
  2280.                     <option value="<?php echo $end_year ?>" selected> <?php echo $end_year ?> </option>
  2281.                     <?php
  2282.                     echo "\n";
  2283.                     for ($i=1$i<=5$i++)
  2284.                     {
  2285.                         $value=$end_year+$i;
  2286.                         echo "\t\t\t\t<option value=\"$value\">$value</option>\n";
  2287.                     ?>
  2288.             </select>
  2289.             <a href="javascript:openCalendar('new_calendar_item', 'end_f')"><img src="../img/calendar_select.gif" border="0" /></a>
  2290.             </td></tr></table>
  2291.         </td>
  2292.  
  2293.         <td>
  2294.  
  2295.             <table cellpadding="0" cellspacing="0" border="0" width="100%">
  2296.  
  2297.             <tr><td width="110">
  2298.                 <!-- date: 1 -> 31 -->
  2299.                 <?php echo get_lang('EndTime').": \n"?>
  2300.             </td>
  2301.  
  2302.             <td>
  2303.  
  2304.             <select name="end_fhour">
  2305.                 <option value="--">--</option>
  2306.                 <?php
  2307.                     echo "\n";
  2308.                     foreach (range(124as $i)
  2309.                     {
  2310.                         // values have to have double digits
  2311.                         $value ($i <= '0'.$i $i );
  2312.                         // the current hour is indicated with [] around the hour
  2313.                         if ($end_hours==$value)
  2314.                             echo "\t\t\t\t<option value=\"".$value."\" selected> ".$value." </option>\n"}
  2315.                         else
  2316.                             echo "\t\t\t\t<option value=\"".$value."\"> ".$value." </option>\n"}
  2317.                     ?>
  2318.             </select>
  2319.             <?php echo get_lang('HourMinuteDivider')?>
  2320.             <select name="end_fminute">
  2321.                 <option value="<?php echo $end_minutes?>"><?php echo $end_minutes?></option>
  2322.                 <option value="--">--</option>
  2323.                 <?php
  2324.                     foreach (range(059as $i)
  2325.                     {
  2326.                         // values have to have double digits
  2327.                         $value ($i <= '0'.$i $i );
  2328.                         echo "\t\t\t\t<option value=\"$value\">$value</option>\n";
  2329.                     ?>
  2330.             </select>
  2331.             </td></tr></table>
  2332.         </td>
  2333.     </tr>
  2334.  
  2335.     <tr class="subtitle">
  2336.         <td colspan="3" valign="top"><hr noshade="noshade" color="#cccccc" /><?php echo get_lang('ItemTitle')?> :
  2337.             <!--<div style='margin-left: 80px'><textarea name="title" cols="50" rows="2" wrap="virtual" style="vertical-align:top; width:75%; height:50px;"><?php  if (isset($title)) echo $title?></textarea></div>-->
  2338.             <input type="text" size="60" name="title" value="<?php  if (isset($title)) echo $title?>" />
  2339.         </td>
  2340.     </tr>
  2341.  
  2342.     <tr>
  2343.         <td colspan="3">
  2344.  
  2345.             <?php
  2346.             require_once(api_get_path(LIBRARY_PATH"/fckeditor/fckeditor.php");
  2347.  
  2348.             $oFCKeditor new FCKeditor('content';
  2349.             $oFCKeditor->BasePath    api_get_path(WEB_PATH'main/inc/lib/fckeditor/' ;
  2350.             $oFCKeditor->Height        '175';
  2351.             $oFCKeditor->Width        '100%';
  2352.             $oFCKeditor->Value        $content;
  2353.             $oFCKeditor->Config['CustomConfigurationsPath'api_get_path(REL_PATH)."main/inc/lib/fckeditor/myconfig.js";
  2354.             $oFCKeditor->ToolbarSet "Middle";
  2355.  
  2356.             $TBL_LANGUAGES Database::get_main_table(TABLE_MAIN_LANGUAGE);
  2357.             $sql="SELECT isocode FROM ".$TBL_LANGUAGES." WHERE english_name='".$_SESSION["_course"]["language"]."'";
  2358.             $result_sql=api_sql_query($sql);
  2359.             $isocode_language=Database::result($result_sql,0,0);
  2360.             $oFCKeditor->Config['DefaultLanguage'$isocode_language;
  2361.  
  2362.             $return =    $oFCKeditor->CreateHtml();
  2363.  
  2364.             echo $return;
  2365.  
  2366.  ?>
  2367.         </td>
  2368.     </tr>
  2369.     <!--<?php /* ADDED BY UGENT, Patrick Cool, march 2004 */ ?>
  2370.     <tr>
  2371.         <td colspan="7">
  2372.         <?php
  2373.             //onclick="selectAll(this.form.elements[6],true)"
  2374.             if ($_SESSION['allow_individual_calendar']=='show')
  2375.                 show_addresource_button('onclick="selectAll(this.form.elements[6],true)"');
  2376.             else
  2377.                 show_addresource_button();
  2378.             $form_elements=$_SESSION['formelements'];
  2379.         ?>
  2380.         </td>
  2381.     </tr>-->
  2382.     <?php
  2383.        //if ($_SESSION['addedresource'])
  2384.        echo "\t<tr>\n";
  2385.        echo "\t\t<td colspan=\"3\">\n";
  2386.        echo display_resources(0);
  2387.        $test=$_SESSION['addedresource'];
  2388.        echo "\t\t</td>\n\t</tr>\n";
  2389.        /* END ADDED BY UGENT, Patrick Cool, march 2004 */
  2390.     if(empty($id)) //only show repeat fields when adding the first time
  2391.     {
  2392.     ?>
  2393.     <tr>
  2394.       <td><label for="repeat"><?php echo get_lang('RepeatedEvent');?></label><input type="checkbox" name="repeat" <?php echo ($repeat?'checked="checked"':'');?>/></td>
  2395.       <td colspan="2" />
  2396.     </tr>
  2397.     <tr>
  2398.       <td><label for="repeat_type"><?php echo get_lang('RepeatType');?></label>
  2399.         <select name="repeat_type">
  2400.           <option value="daily"><?php echo get_lang('RepeatDaily');?></option>
  2401.           <option value="weekly"><?php echo get_lang('RepeatWeekly');?></option>
  2402.           <option value="monthlyByDate"><?php echo get_lang('RepeatMonthlyByDate');?></option>
  2403.           <!--option value="monthlyByDay"><?php echo get_lang('RepeatMonthlyByDay');?></option>
  2404.           <option value="monthlyByDayR"><?php echo get_lang('RepeatMonthlyByDayR');?></option-->
  2405.           <option value="yearly"><?php echo get_lang('RepeatYearly');?></option>
  2406.         </select>
  2407.       </td>
  2408.       <td colspan="2" />
  2409.     </tr>
  2410.     <tr>
  2411.       <td><label for="repeat_end_day"><?php echo get_lang('RepeatEnd');?></label>
  2412.             <select name="repeat_end_day">
  2413.                 <?php
  2414.                     // small loop for filling all the dates
  2415.                     // 2do: the available dates should be those of the selected month => february is from 1 to 28 (or 29) and not to 31
  2416.                     echo "\n";
  2417.                     foreach (range(131as $i)
  2418.                     {
  2419.                         // values have to have double digits
  2420.                         $value ($i <= '0'.$i $i );
  2421.                         // the current day is indicated with [] around the date
  2422.                         if ($value==$end_day)
  2423.                             echo "\t\t\t\t <option value=\"".$value."\" selected> ".$i." </option>\n";}
  2424.                         else
  2425.                             echo "\t\t\t\t <option value=\"".$value."\">".$i."</option>\n"}
  2426.                         }?>
  2427.                 </select>
  2428.  
  2429.                 <!-- month: january -> december -->
  2430.                 <select name="repeat_end_month">
  2431.                     <?php
  2432.                     echo "\n";
  2433.                     foreach (range(112as $i)
  2434.                     {
  2435.                         // values have to have double digits
  2436.                         $value ($i <= '0'.$i $i );
  2437.                         if ($value==$end_month+1)
  2438.                             echo "\t\t\t\t ",'<option value="',$value,'" selected="selected">',$MonthsLong[$i-1],"</option>\n"}
  2439.                         else
  2440.                             echo "\t\t\t\t ",'<option value="',$value,'">',$MonthsLong[$i-1],"</option>\n"}
  2441.                         }?>
  2442.                 </select>
  2443.  
  2444.                 <select name="repeat_end_year">
  2445.                     <option value="<?php echo ($end_year-1?>"><?php echo ($end_year-1?></option>
  2446.                     <option value="<?php echo $end_year ?>" selected> <?php echo $end_year ?> </option>
  2447.                     <?php
  2448.                     echo "\n";
  2449.                     for ($i=1$i<=5$i++)
  2450.                     {
  2451.                         $value=$end_year+$i;
  2452.                         echo "\t\t\t\t<option value=\"$value\">$value</option>\n";
  2453.                     ?>
  2454.             </select>
  2455.             <a href="javascript:openCalendar('new_calendar_item', 'repeat_end_')"><img src="../img/calendar_select.gif" border="0" /></a>
  2456.       </td>
  2457.       <td colspan="2" />      
  2458.     </tr>
  2459.     <?php
  2460.     }//only show repeat fields if adding, not if editing
  2461.     ?>
  2462.     <tr>
  2463.         <td colspan="3">
  2464.             <input type="submit" name="submit_event" value="<?php echo get_lang('Ok')?>" onclick="selectAll(this.form.elements[5],true)" />
  2465.         </td>
  2466.     </tr>
  2467. </table>
  2468. </form>
  2469. <p>&nbsp;</p>
  2470. <?php
  2471. }
  2472.  
  2473. function get_agendaitems($month$year)
  2474. {
  2475.     global $_user;
  2476.     global $_configuration;
  2477.  
  2478.     $items array ();
  2479.  
  2480.     //databases of the courses
  2481.     $TABLEAGENDA         Database :: get_course_table(TABLE_AGENDA);
  2482.     $TABLE_ITEMPROPERTY Database :: get_course_table(TABLE_ITEM_PROPERTY);
  2483.  
  2484.     $group_memberships GroupManager :: get_group_ids(Database::get_current_course_database()$_user['user_id']);
  2485.     // if the user is administrator of that course we show all the agenda items
  2486.     if (api_is_allowed_to_edit())
  2487.     {
  2488.         //echo "course admin";
  2489.         $sqlquery "SELECT
  2490.                         DISTINCT agenda.*, item_property.*
  2491.                         FROM ".$TABLEAGENDA." agenda,
  2492.                              ".$TABLE_ITEMPROPERTY." item_property
  2493.                         WHERE agenda.id = item_property.ref
  2494.                         AND MONTH(agenda.start_date)='".$month."'
  2495.                         AND YEAR(agenda.start_date)='".$year."'
  2496.                         AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  2497.                         AND item_property.visibility='1'
  2498.                         GROUP BY agenda.id
  2499.                         ORDER BY start_date ";
  2500.     }
  2501.     // if the user is not an administrator of that course
  2502.     else
  2503.     {
  2504.         //echo "GEEN course admin";
  2505.         if (is_array($group_memberships&& count($group_memberships)>0)
  2506.         {
  2507.             $sqlquery "SELECT
  2508.                             agenda.*, item_property.*
  2509.                             FROM ".$TABLEAGENDA." agenda,
  2510.                                 ".$TABLE_ITEMPROPERTY." item_property
  2511.                             WHERE agenda.id = item_property.ref 
  2512.                             AND MONTH(agenda.start_date)='".$month."'
  2513.                             AND YEAR(agenda.start_date)='".$year."'
  2514.                             AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  2515.                             AND    ( item_property.to_user_id='".$_user['user_id']."' OR item_property.to_group_id IN (0, ".implode(", "$group_memberships).") )
  2516.                             AND item_property.visibility='1'
  2517.                             ORDER BY start_date ";
  2518.         }
  2519.         else
  2520.         {
  2521.             $sqlquery "SELECT
  2522.                             agenda.*, item_property.*
  2523.                             FROM ".$TABLEAGENDA." agenda,
  2524.                             ".$TABLE_ITEMPROPERTY." item_property
  2525.                             WHERE agenda.id = item_property.ref
  2526.                             AND MONTH(agenda.start_date)='".$month."'
  2527.                             AND YEAR(agenda.start_date)='".$year."'
  2528.                             AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  2529.                             AND ( item_property.to_user_id='".$_user['user_id']."' OR item_property.to_group_id='0')
  2530.                             AND item_property.visibility='1'
  2531.                             ORDER BY start_date ";
  2532.         }
  2533.     }
  2534.  
  2535.     $mycourse api_get_course_info();
  2536.     $result api_sql_query($sqlquery__FILE____LINE__);
  2537.     while ($item Database::fetch_array($result))
  2538.     {
  2539.         $agendaday date('j',strtotime($item['start_date']));
  2540.         $timedate('H:i',strtotime($item['start_date']));
  2541.         $URL $_configuration['root_web'].'main/calendar/agenda.php?cidReq='.$mycourse['id']."&amp;day=$agendaday&amp;month=$month&amp;year=$year#$agendaday"// RH  //Patrick Cool: to highlight the relevant agenda item
  2542.         $items[$agendaday][$item['start_time']] .= '<i>'.$time.'</i> <a href="'.$URL.'" title="'.$mycourse['name'].'">'.$mycourse['official_code'].'</a> '.$item['title'].'<br />';
  2543.     }
  2544.         
  2545.     // sorting by hour for every day
  2546.     $agendaitems array ();
  2547.     while (list ($agendaday$tmpitemseach($items))
  2548.     {
  2549.         sort($tmpitems);
  2550.         while (list ($key$valeach($tmpitems))
  2551.         {
  2552.             $agendaitems[$agendaday.= $val;
  2553.         }
  2554.     }
  2555.     return $agendaitems;
  2556. }
  2557.  
  2558. {
  2559.     echo '<b>'.get_lang('UpcomingEvent').'</b><br />';
  2560.     $number_of_items_to_show = (int)api_get_setting('number_of_upcoming_events');
  2561.     
  2562.     //databases of the courses
  2563.     $TABLEAGENDA         Database :: get_course_table(TABLE_AGENDA);
  2564.     $TABLE_ITEMPROPERTY Database :: get_course_table(TABLE_ITEM_PROPERTY);
  2565.     $mycourse api_get_course_info();
  2566.     $myuser api_get_user_info();
  2567.  
  2568.     $group_memberships GroupManager :: get_group_ids($mycourse['dbName']$myuser['user_id']);
  2569.     // if the user is administrator of that course we show all the agenda items
  2570.     if (api_is_allowed_to_edit())
  2571.     {
  2572.         //echo "course admin";
  2573.         $sqlquery "SELECT
  2574.                         DISTINCT agenda.*, item_property.*
  2575.                         FROM ".$TABLEAGENDA." agenda,
  2576.                              ".$TABLE_ITEMPROPERTY." item_property
  2577.                         WHERE agenda.id = item_property.ref
  2578.                         AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  2579.                         AND item_property.visibility='1'
  2580.                         AND agenda.start_date > NOW()
  2581.                         GROUP BY agenda.id
  2582.                         ORDER BY start_date ";
  2583.     }
  2584.     // if the user is not an administrator of that course
  2585.     else
  2586.     {
  2587.         //echo "GEEN course admin";
  2588.         if (is_array($group_membershipsand count($group_memberships)>0)
  2589.         {
  2590.             $sqlquery "SELECT
  2591.                             agenda.*, item_property.*
  2592.                             FROM ".$TABLEAGENDA." agenda,
  2593.                                 ".$TABLE_ITEMPROPERTY." item_property
  2594.                             WHERE agenda.id = item_property.ref 
  2595.                             AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  2596.                             AND    ( item_property.to_user_id='".$myuser['user_id']."' OR item_property.to_group_id IN (0, ".implode(", "$group_memberships).") )
  2597.                             AND item_property.visibility='1'
  2598.                             AND agenda.start_date > NOW()
  2599.                             ORDER BY start_date ";
  2600.         }
  2601.         else
  2602.         {
  2603.             $sqlquery "SELECT
  2604.                             agenda.*, item_property.*
  2605.                             FROM ".$TABLEAGENDA." agenda,
  2606.                             ".$TABLE_ITEMPROPERTY." item_property
  2607.                             WHERE agenda.id = item_property.ref
  2608.                             AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  2609.                             AND ( item_property.to_user_id='".$myuser['user_id']."' OR item_property.to_group_id='0')
  2610.                             AND item_property.visibility='1'
  2611.                             AND agenda.start_date > NOW()
  2612.                             ORDER BY start_date ";
  2613.         }
  2614.     }
  2615.     $result api_sql_query($sqlquery__FILE____LINE__);
  2616.     $counter 0;
  2617.     while ($item Database::fetch_array($result,'ASSOC'))    
  2618.     {
  2619.         if ($counter $number_of_items_to_show)
  2620.         {
  2621.             echo $item['start_date'],' - ',$item['title'],'<br />';
  2622.             $counter++;
  2623.         }
  2624.     }
  2625. }
  2626. /**
  2627.  * This function calculates the startdate of the week (monday)
  2628.  * and the enddate of the week (sunday)
  2629.  * and returns it as an array
  2630.  */
  2631. function calculate_start_end_of_week($week_number$year)
  2632. {
  2633.     // determine the start and end date
  2634.     // step 1: we calculate a timestamp for a day in this week
  2635.     $random_day_in_week mktime(00011$year($week_number-1(24 60 60)// we calculate a random day in this week
  2636.     // step 2: we which day this is (0=sunday, 1=monday, ...)
  2637.     $number_day_in_week date('w'$random_day_in_week);
  2638.     // step 3: we calculate the timestamp of the monday of the week we are in
  2639.     $start_timestamp $random_day_in_week (($number_day_in_week -124 60 60);
  2640.     // step 4: we calculate the timestamp of the sunday of the week we are in
  2641.     $end_timestamp $random_day_in_week (($number_day_in_week +124 60 603600;
  2642.     // step 5: calculating the start_day, end_day, start_month, end_month, start_year, end_year
  2643.     $start_day date('j'$start_timestamp);
  2644.     $start_month date('n'$start_timestamp);
  2645.     $start_year date('Y'$start_timestamp);
  2646.     $end_day date('j'$end_timestamp);
  2647.     $end_month date('n'$end_timestamp);
  2648.     $end_year date('Y'$end_timestamp);
  2649.     $start_end_array['start']['day'$start_day;
  2650.     $start_end_array['start']['month'$start_month;
  2651.     $start_end_array['start']['year'$start_year;
  2652.     $start_end_array['end']['day'$end_day;
  2653.     $start_end_array['end']['month'$end_month;
  2654.     $start_end_array['end']['year'$end_year;
  2655.     return $start_end_array;
  2656. }
  2657. /**
  2658.  * Show the mini calendar of the given month
  2659.  */
  2660. function display_daycalendar($agendaitems$day$month$year$weekdaynames$monthName)
  2661. {
  2662.     global $DaysShort$DaysLong$course_path;
  2663.     global $MonthsLong;
  2664.     global $query;
  2665.  
  2666.     // timestamp of today
  2667.     $today mktime();
  2668.     $nextday $today (24 60 60);
  2669.     $previousday $today (24 60 60);
  2670.     // the week number of the year
  2671.     $week_number date("W"$today);
  2672.     // if we moved to the next / previous day we have to recalculate the $today variable
  2673.     if ($_GET['day'])
  2674.     {
  2675.         $today mktime(000$month$day$year);
  2676.         $nextday $today (24 60 60);
  2677.         $previousday $today (24 60 60);
  2678.         $week_number date("W"$today);
  2679.     }
  2680.     // calculating the start date of the week
  2681.     // the date of the monday of this week is the timestamp of today minus
  2682.     // number of days that have already passed this week * 24 hours * 60 minutes * 60 seconds
  2683.     $current_day date("j"$today)// Day of the month without leading zeros (1 to 31) of today
  2684.     $day_of_the_week date("w"$today)// Numeric representation of the day of the week    0 (for Sunday) through 6 (for Saturday) of today
  2685.     //$timestamp_first_date_of_week=$today-(($day_of_the_week-1)*24*60*60); // timestamp of the monday of this week
  2686.     //$timestamp_last_date_of_week=$today+((7-$day_of_the_week)*24*60*60); // timestamp of the sunday of this week
  2687.     // we are loading all the calendar items of all the courses for today
  2688.     echo "<table id=\"agenda_list\">\n";
  2689.     // the forward and backwards url
  2690.     $backwardsURL api_get_self()."?coursePath=".urlencode($course_path)."&amp;courseCode=".Security::remove_XSS($_GET['courseCode'])."&amp;action=view&amp;view=day&amp;day=".date("j"$previousday)."&amp;month=".date("n"$previousday)."&amp;year=".date("Y"$previousday);
  2691.     $forewardsURL api_get_self()."?coursePath=".urlencode($course_path)."&amp;courseCode=".Security::remove_XSS($_GET['courseCode'])."&amp;action=view&amp;view=day&amp;day=".date("j"$nextday)."&amp;month=".date("n"$nextday)."&amp;year=".date("Y"$nextday);
  2692.     // The title row containing the day
  2693.     echo "<tr class=\"title\">\n""<td width=\"10%\"><a href=\""$backwardsURL"\">&#171;</a></td>\n""<td>";
  2694.     echo $DaysLong[$day_of_the_week]." ".date("j"$today)." ".$MonthsLong[date("n"$today1]." ".date("Y"$today);
  2695.     echo "</td>";
  2696.     echo "<td width=\"10%\"><a href=\""$forewardsURL"\">&#187;</a></td>\n";
  2697.     echo "</tr>\n";
  2698.     // the rows for each half an hour
  2699.     for ($i 10$i 48$i ++)
  2700.     {
  2701.         echo "<tr>\n";
  2702.         echo "\t";
  2703.         if ($i == 0)
  2704.         {
  2705.             $class "class=\"alternativeBgLight\"";
  2706.             echo ("<td $class valign=\"top\" width=\"75\">"(($i2)." ".get_lang("HourShort")." 00</td>\n");
  2707.         }
  2708.         else
  2709.         {
  2710.             $class "";
  2711.             echo ("<td valign=\"top\" width=\"75\">"((($i2(2))." ".get_lang("HourShort")." 30</td>\n");
  2712.         }
  2713.         echo "\t<td $class valign=\"top\" colspan=\"2\">\n";
  2714.         if (is_array($agendaitems[$i]))
  2715.         {
  2716.             foreach ($agendaitems[$ias $key => $value)
  2717.             {
  2718.                 echo $value;
  2719.             }
  2720.         }
  2721.         else
  2722.         {
  2723.             echo $agendaitems[$i];
  2724.         }
  2725.         echo "\t</td>\n";
  2726.         echo "</tr>\n";
  2727.     }
  2728.     echo "</table>\n";
  2729. }
  2730. /**
  2731.  *    Display the weekly view of the calendar
  2732.  */ 
  2733. function display_weekcalendar($agendaitems$month$year$weekdaynames$monthName)
  2734. {
  2735.     global $DaysShort,$course_path;
  2736.     global $MonthsLong;
  2737.     // timestamp of today
  2738.     $today time();
  2739.     $day_of_the_week date("w"$today);
  2740.     $thisday_of_the_week date("w"$today);
  2741.     // the week number of the year
  2742.     $week_number date("W"$today);
  2743.     $thisweek_number $week_number;
  2744.     // if we moved to the next / previous week we have to recalculate the $today variable
  2745.     if ($_GET['week'])
  2746.     {
  2747.         $today mktime(00011$year);
  2748.         $today $today (((int)$_GET['week']-1(24 60 60));
  2749.         $week_number date("W"$today);
  2750.     }
  2751.     // calculating the start date of the week
  2752.     // the date of the monday of this week is the timestamp of today minus
  2753.     // number of days that have already passed this week * 24 hours * 60 minutes * 60 seconds
  2754.     $current_day date("j"$today)// Day of the month without leading zeros (1 to 31) of today
  2755.     $day_of_the_week date("w"$today)// Numeric representation of the day of the week    0 (for Sunday) through 6 (for Saturday) of today
  2756.     $timestamp_first_date_of_week $today (($day_of_the_week -124 60 60)// timestamp of the monday of this week
  2757.     $timestamp_last_date_of_week $today (($day_of_the_week24 60 60)// timestamp of the sunday of this week
  2758.     $backwardsURL api_get_self()."?coursePath=".urlencode($course_path)."&amp;courseCode=".Security::remove_XSS($_GET['courseCode'])."&amp;action=view&amp;view=week&amp;week="($week_number -1);
  2759.     $forewardsURL api_get_self()."?coursePath=".urlencode($course_path)."&amp;courseCode=".Security::remove_XSS($_GET['courseCode'])."&amp;action=view&amp;view=week&amp;week="($week_number +1);
  2760.     echo "<table id=\"agenda_list\">\n";
  2761.     // The title row containing the the week information (week of the year (startdate of week - enddate of week)
  2762.     echo "<tr class=\"title\">\n";
  2763.     echo "<td width=\"10%\"><a href=\""$backwardsURL"\">&#171;</a></td>\n";
  2764.     echo "<td colspan=\"5\">".get_lang("Week")." ".$week_number;
  2765.     echo " (".$DaysShort['1']." ".date("j"$timestamp_first_date_of_week)." ".$MonthsLong[date("n"$timestamp_first_date_of_week1]." ".date("Y"$timestamp_first_date_of_week)." - ".$DaysShort['0']." ".date("j"$timestamp_last_date_of_week)." ".$MonthsLong[date("n"$timestamp_last_date_of_week1]." ".date("Y"$timestamp_last_date_of_week).')';
  2766.     echo "</td>";
  2767.     echo "<td width=\"10%\"><a href=\""$forewardsURL"\">&#187;</a></td>\n""</tr>\n";
  2768.     // The second row containing the short names of the days of the week
  2769.     echo "<tr>\n";
  2770.     // this is the Day of the month without leading zeros (1 to 31) of the monday of this week
  2771.     $tmp_timestamp $timestamp_first_date_of_week;
  2772.     for ($ii 1$ii 8$ii ++)
  2773.     {
  2774.         $is_today ($ii == $thisday_of_the_week AND (!isset($_GET['week']OR $_GET['week']==$thisweek_number));
  2775.         echo "\t<td class=\"weekdays\">";
  2776.         if ($is_today)
  2777.         {
  2778.             echo "<font color=#CC3300>";
  2779.         }
  2780.         echo $DaysShort[$ii 7]." ".date("j"$tmp_timestamp)." ".$MonthsLong[date("n"$tmp_timestamp1];
  2781.         if ($is_today)
  2782.         {
  2783.             echo "</font>";
  2784.         }
  2785.         echo "</td>\n";
  2786.         // we 24 hours * 60 minutes * 60 seconds to the $tmp_timestamp
  2787.         $array_tmp_timestamp[$tmp_timestamp;
  2788.         $tmp_timestamp $tmp_timestamp (24 60 60);
  2789.     }
  2790.     echo "</tr>\n";
  2791.     // the table cells containing all the entries for that day
  2792.     echo "<tr>\n";
  2793.     $counter 0;
  2794.     foreach ($array_tmp_timestamp as $key => $value)
  2795.     {
  2796.         if ($counter 5)
  2797.         {
  2798.             $class "class=\"days_week\"";
  2799.         }
  2800.         else
  2801.         {
  2802.             $class "class=\"days_weekend\"";
  2803.         }
  2804.         if ($counter == $thisday_of_the_week -AND (!isset($_GET['week']OR $_GET['week']==$thisweek_number))
  2805.         {
  2806.             $class "class=\"days_today\"";
  2807.         }
  2808.  
  2809.         echo "\t<td ".$class.">";
  2810.         echo "<span class=\"agendaitem\">".$agendaitems[date('j'$value)]."&nbsp;</span> ";
  2811.         echo "</td>\n";
  2812.         $counter ++;
  2813.     }
  2814.     echo "</tr>\n";
  2815.     echo "</table>\n";
  2816. }
  2817. /**
  2818.  * Show the monthcalender of the given month
  2819.  */
  2820. function get_day_agendaitems($courses_dbs$month$year$day)
  2821. {
  2822.     global $_user;
  2823.     global $_configuration;
  2824.     global $setting_agenda_link;
  2825.  
  2826.     $items array ();
  2827.  
  2828.     // get agenda-items for every course
  2829.     //$query=api_sql_query($sql_select_courses);
  2830.     foreach ($courses_dbs as $key => $array_course_info)
  2831.     {
  2832.         //databases of the courses
  2833.         $TABLEAGENDA Database :: get_course_table(TABLE_AGENDA$array_course_info['db']);
  2834.         $TABLE_ITEMPROPERTY Database :: get_course_table(TABLE_ITEM_PROPERTY$array_course_info['db']);
  2835.  
  2836.         // getting all the groups of the user for the current course
  2837.         $group_memberships GroupManager :: get_group_ids($array_course_info['db']$_user['user_id']);
  2838.         // if the user is administrator of that course we show all the agenda items
  2839.         if ($array_course_info['status'== '1')
  2840.         {
  2841.             //echo "course admin";
  2842.             $sqlquery "SELECT
  2843.                                         DISTINCT agenda.*, item_property.*
  2844.                                         FROM ".$TABLEAGENDA." agenda,
  2845.                                             ".$TABLE_ITEMPROPERTY." item_property
  2846.                                         WHERE agenda.id = item_property.ref
  2847.                                         AND DAYOFMONTH(start_date)='".$day."' AND MONTH(start_date)='".$month."' AND YEAR(start_date)='".$year."'
  2848.                                         AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  2849.                                         AND item_property.visibility='1'
  2850.                                         GROUP BY agenda.id
  2851.                                         ORDER BY start_date ";
  2852.         }
  2853.         // if the user is not an administrator of that course
  2854.         else
  2855.         {
  2856.             //echo "GEEN course admin";
  2857.             if (is_array($group_memberships&& count($group_memberships)>0)
  2858.             {
  2859.                 $sqlquery "SELECT
  2860.                                                     agenda.*, item_property.*
  2861.                                                     FROM ".$TABLEAGENDA." agenda,
  2862.                                                         ".$TABLE_ITEMPROPERTY." item_property
  2863.                                                     WHERE agenda.id = item_property.ref
  2864.                                                     AND DAYOFMONTH(start_date)='".$day."' AND MONTH(start_date)='".$month."' AND YEAR(start_date)='".$year."'
  2865.                                                     AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  2866.                                                     AND    ( item_property.to_user_id='".$_user['user_id']."' OR `item_property`.`to_group_id` IN (0, ".implode(", "$group_memberships).") )
  2867.                                                     AND item_property.visibility='1'
  2868.                                                     ORDER BY start_date ";
  2869.             }
  2870.             else
  2871.             {
  2872.                 $sqlquery "SELECT
  2873.                                                     agenda.*, item_property.*
  2874.                                                     FROM ".$TABLEAGENDA." agenda,
  2875.                                                         ".$TABLE_ITEMPROPERTY." item_property
  2876.                                                     WHERE agenda.id = item_property.ref 
  2877.                                                     AND DAYOFMONTH(start_date)='".$day."' AND MONTH(start_date)='".$month."' AND YEAR(start_date)='".$year."'
  2878.                                                     AND item_property.tool='".TOOL_CALENDAR_EVENT."'
  2879.                                                     AND ( item_property.to_user_id='".$_user['user_id']."' OR item_property.to_group_id='0')
  2880.                                                     AND item_property.visibility='1'
  2881.                                                     ORDER BY start_date ";
  2882.             }
  2883.         }
  2884.         //$sqlquery = "SELECT * FROM $agendadb WHERE DAYOFMONTH(day)='$day' AND month(day)='$month' AND year(day)='$year'";
  2885.         //echo "abc";
  2886.         //echo $sqlquery;
  2887.         $result api_sql_query($sqlquery__FILE____LINE__);
  2888.         //echo Database::num_rows($result);
  2889.         while ($item Database::fetch_array($result))
  2890.         {
  2891.             // in the display_daycalendar function we use $i (ranging from 0 to 47) for each halfhour
  2892.             // we want to know for each agenda item for this day to wich halfhour it must be assigned
  2893.             list ($datepart$timepartsplit(" "$item['start_date']);
  2894.             list ($year$month$dayexplode("-"$datepart);
  2895.             list ($hours$minutes$secondsexplode(":"$timepart);
  2896.  
  2897.             $halfhour $hours;
  2898.             if ($minutes >= '30')
  2899.             {
  2900.                 $halfhour $halfhour +1;
  2901.             }
  2902.  
  2903.             if ($setting_agenda_link == 'coursecode')
  2904.             {
  2905.                 $title=$array_course_info['title'];
  2906.                 $agenda_link substr($title014);
  2907.             }
  2908.             else 
  2909.             {
  2910.                 $agenda_link Display::return_icon('course_home.gif');
  2911.             }            
  2912.             
  2913.             //$URL = $_configuration['root_web'].$mycours["dir"]."/";
  2914.             $URL $_configuration['root_web'].'main/calendar/agenda.php?cidReq='.urlencode($array_course_info["code"])."&amp;day=$day&amp;month=$month&amp;year=$year#$day"// RH  //Patrick Cool: to highlight the relevant agenda item
  2915.             $items[$halfhour][.= "<i>".$hours.":".$minutes."</i> <a href=\"$URL\" title=\"".$array_course_info['name']."\">".$agenda_link."</a>  ".$item['title']."<br />";
  2916.         }
  2917.     }
  2918.     // sorting by hour for every day
  2919.     /*$agendaitems = array();
  2920.     while (list($agendaday, $tmpitems) = each($items))
  2921.     {
  2922.         sort($tmpitems);
  2923.         while (list($key,$val) = each($tmpitems))
  2924.         {
  2925.             $agendaitems[$agendaday].=$val;
  2926.         }
  2927.     }*/
  2928.     $agendaitems $items;
  2929.     //print_r($agendaitems);
  2930.     return $agendaitems;
  2931. }
  2932. /**
  2933.  * Return agenda items of the week
  2934.  */
  2935. function get_week_agendaitems($courses_dbs$month$year$week '')
  2936. {
  2937.     global $TABLEAGENDA$TABLE_ITEMPROPERTY;
  2938.     global $_user;
  2939.     global $_configuration;
  2940.     global $setting_agenda_link;
  2941.  
  2942.     $items array ();
  2943.     // The default value of the week
  2944.     if ($week == '')
  2945.     {
  2946.         $week_number date("W"time());
  2947.     }
  2948.     else
  2949.     {
  2950.         $week_number $week;
  2951.     }
  2952.     $start_end calculate_start_end_of_week($week_number$year);
  2953.     $start_filter $start_end['start']['year']."-".$start_end['start']['month']."-".$start_end['start']['day'];
  2954.     $end_filter $start_end['end']['year']."-".$start_end['end']['month']."-".$start_end['end']['day'];
  2955.     // get agenda-items for every course
  2956.     foreach ($courses_dbs as $key => $array_course_info)
  2957.     {
  2958.         //databases of the courses
  2959.         $TABLEAGENDA Database :: get_course_table(TABLE_AGENDA$array_course_info["db"]);
  2960.         $TABLE_ITEMPROPERTY Database :: get_course_table(TABLE_ITEM_PROPERTY$array_course_info["db"]);
  2961.  
  2962.         // getting all the groups of the user for the current course
  2963.         $group_memberships GroupManager :: get_group_ids($array_course_info["db"]$_user['user_id']);
  2964.  
  2965.         // if the user is administrator of that course we show all the agenda items
  2966.         if ($array_course_info['status'== '1')
  2967.         {
  2968.             //echo "course admin";
  2969.             $sqlquery "SELECT
  2970.                                         DISTINCT a.*, i.*
  2971.                                         FROM ".$TABLEAGENDA." a,
  2972.                                             ".$TABLE_ITEMPROPERTY." i
  2973.                                         WHERE a.id = i.ref  
  2974.                                         AND a.start_date>='".$start_filter."' AND a.start_date<='".$end_filter."'
  2975.                                         AND i.tool='".TOOL_CALENDAR_EVENT."'
  2976.                                         AND i.visibility='1'
  2977.                                         GROUP BY a.id
  2978.                                         ORDER BY a.start_date";
  2979.         }
  2980.         // if the user is not an administrator of that course
  2981.         else
  2982.         {
  2983.             //echo "GEEN course admin";
  2984.             if (is_array($group_memberships&& count($group_memberships)>0)
  2985.             {
  2986.                 $sqlquery "SELECT
  2987.                                                     a.*, i.*
  2988.                                                     FROM ".$TABLEAGENDA." a,
  2989.                                                          ".$TABLE_ITEMPROPERTY." i
  2990.                                                     WHERE a.id = i.`ref` 
  2991.                                                     AND a.start_date>='".$start_filter."' AND a.start_date<='".$end_filter."'
  2992.                                                     AND i.tool='".TOOL_CALENDAR_EVENT."'
  2993.                                                     AND    ( i.to_user_id='".$_user['user_id']."' OR i.to_group_id IN (0, ".implode(", "$group_memberships).") )
  2994.                                                     AND i.visibility='1'
  2995.                                                     ORDER BY a.start_date";
  2996.             }
  2997.             else
  2998.             {
  2999.                 $sqlquery "SELECT
  3000.                                                     a.*, i.*
  3001.                                                     FROM ".$TABLEAGENDA." a,
  3002.                                                          ".$TABLE_ITEMPROPERTY." i
  3003.                                                     WHERE a.id = i.ref 
  3004.                                                     AND a.start_date>='".$start_filter."' AND a.start_date<='".$end_filter."'
  3005.                                                     AND i.tool='".TOOL_CALENDAR_EVENT."'
  3006.                                                     AND ( i.to_user_id='".$_user['user_id']."' OR i.to_group_id='0')
  3007.                                                     AND i.visibility='1'
  3008.                                                     ORDER BY a.start_date";
  3009.             }
  3010.         }
  3011.         //echo "<pre>".$sqlquery."</pre>";
  3012.         // $sqlquery = "SELECT * FROM $agendadb WHERE (DAYOFMONTH(day)>='$start_day' AND DAYOFMONTH(day)<='$end_day')
  3013.         //                AND (MONTH(day)>='$start_month' AND MONTH(day)<='$end_month')
  3014.         //                AND (YEAR(day)>='$start_year' AND YEAR(day)<='$end_year')";
  3015.         $result api_sql_query($sqlquery__FILE____LINE__);
  3016.         while ($item Database::fetch_array($result))
  3017.         {
  3018.             $agendaday date("j",strtotime($item['start_date']));
  3019.             $timedate("H:i",strtotime($item['start_date']));
  3020.  
  3021.             if ($setting_agenda_link == 'coursecode')
  3022.             {                
  3023.                 $title=$array_course_info['title'];
  3024.                 $agenda_link substr($title014);
  3025.             }
  3026.             else 
  3027.             {
  3028.                 $agenda_link Display::return_icon('course_home.gif');
  3029.             }            
  3030.             
  3031.             $URL $_configuration['root_web']."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
  3032.             $items[$agendaday][$item['start_time']] .= "<i>$time</i> <a href=\"$URL\" title=\"".$array_course_info["name"]."\">".$agenda_link."</a>  ".$item['title']."<br />";
  3033.         }
  3034.     }
  3035.     // sorting by hour for every day
  3036.     $agendaitems array ();
  3037.     while (list ($agendaday$tmpitemseach($items))
  3038.     {
  3039.         sort($tmpitems);
  3040.         while (list ($key$valeach($tmpitems))
  3041.         {
  3042.             $agendaitems[$agendaday.= $val;
  3043.         }
  3044.     }
  3045.     //print_r($agendaitems);
  3046.     return $agendaitems;
  3047. }
  3048. /**
  3049.  * Get repeated events of a course between two dates (timespan of a day).
  3050.  * Returns an array containing the events
  3051.  * @param   string  Course info array (as returned by api_get_course_info())
  3052.  * @param    int        UNIX timestamp of span start. Defaults 0, later transformed into today's start
  3053.  * @param    int        UNIX timestamp. Defaults to 0, later transformed into today's end
  3054.  * @param   array   A set of parameters to alter the SQL query
  3055.  * @return    array    [int] => [course_id,parent_event_id,start_date,end_date,title,description]
  3056.  */
  3057. function get_repeated_events_day_view($course_info,$start=0,$end=0,$params)
  3058. {
  3059.     $events array();
  3060.     //initialise all values
  3061.     $y=0;
  3062.     $m=0;
  3063.     $d=0;
  3064.     //block $end if higher than 2038 -- PHP doesn't go past that
  3065.     if($end>2145934800){$end 2145934800;}
  3066.     if($start == or $end == 0)
  3067.     {    
  3068.         $y=date('Y');
  3069.         $m=date('m');
  3070.         $d=date('j');
  3071.     }
  3072.     if($start==0)
  3073.     {
  3074.         $start mktime(0,0,0,$m,$d,$y);
  3075.     }
  3076.     $db_start date('Y-m-d H:i:s',$start);
  3077.     if($end==0)
  3078.     {
  3079.         $end mktime(23,59,59,$m,$d,$y);
  3080.     }
  3081.     //$db_end = date('Y-m-d H:i:s',$end);
  3082.     
  3083.     $t_cal Database::get_course_table(TABLE_AGENDA,$course_info['dbName']);
  3084.     $t_cal_repeat Database::get_course_table(TABLE_AGENDA_REPEAT,$course_info['dbName']);
  3085.     $t_ip Database::get_course_table(TABLE_ITEM_PROPERTY,$course_info['dbName']);
  3086.     $sql "SELECT c.id, c.title, c.content, " .
  3087.             " UNIX_TIMESTAMP(c.start_date) as orig_start, UNIX_TIMESTAMP(c.end_date) as orig_end, " .
  3088.             " cr.cal_type, cr.cal_end " .
  3089.             " FROM $t_cal c, $t_cal_repeat cr, $t_ip as item_property .
  3090.             " WHERE cr.cal_end >= $start .
  3091.             " AND cr.cal_id = c.id " .
  3092.             " AND item_property.ref = c.id ".
  3093.             " AND item_property.tool = '".TOOL_CALENDAR_EVENT."' ".
  3094.             " AND c.start_date <= '$db_start"
  3095.             .(!empty($params['conditions'])?$params['conditions']:'')
  3096.             .(!empty($params['groupby'])?' GROUP BY '.$params['groupby']:'')
  3097.             .(!empty($params['orderby'])?' ORDER BY '.$params['orderby']:'');
  3098.     $res api_sql_query($sql,__FILE__,__LINE__);
  3099.     if(Database::num_rows($res)>0)
  3100.     {
  3101.         while($row Database::fetch_array($res))
  3102.         {
  3103.             $orig_start $row['orig_start'];
  3104.             $orig_end $row['orig_end'];
  3105.             $repeat_type $row['cal_type'];
  3106.             switch($repeat_type)
  3107.             {
  3108.                 case 'daily':
  3109.                     //we are in the daily view, so if this element is repeated daily and
  3110.                     //the repetition is still active today (which is a condition of the SQL query)
  3111.                     //then the event happens today. Just build today's timestamp for start and end
  3112.                     $time_orig_h date('H',$orig_start);
  3113.                     $time_orig_m date('i',$orig_start);
  3114.                     $time_orig_s date('s',$orig_start);
  3115.                     $int_time (($time_orig_h*60)+$time_orig_m)*60+$time_orig_s//time in seconds since 00:00:00
  3116.                     $span $orig_end $orig_start//total seconds between start and stop of original event
  3117.                     $current_start =$start $int_time//unixtimestamp start of today's event 
  3118.                     $current_stop $start+$int_time+$span//unixtimestamp stop of today's event
  3119.                     $events[array($course_info['id'],$row['id'],$current_start,$current_stop,$row['title'],$row['content']);
  3120.                     break;
  3121.                 case 'weekly':
  3122.                     $time_orig date('Y/n/W/j/N/G/i/s',$orig_start);
  3123.                     list($y_orig,$m_orig,$w_orig,$d_orig,$dw_orig,$h_orig,$n_orig,$s_origsplit('/',$time_orig);
  3124.                     $time_now date('Y/n/W/j/N/G/i/s',$end);
  3125.                     list($y_now,$m_now,$w_now,$d_now,$dw_now,$h_now,$n_now,$s_nowsplit('/',$time_now);
  3126.                     if((($y_now>$y_origOR (($y_now == $y_orig&& ($w_now>$w_orig))) && ($dw_orig == $dw_now))
  3127.                     //if the event is after the original (at least one week) and the day of the week is the same
  3128.                       $time_orig_end date('Y/n/W/j/N/G/i/s',$orig_end);
  3129.                       list($y_orig_e,$m_orig_e,$w_orig_e,$d_orig_e,$dw_orig_e,$h_orig_e,$n_orig_e,$s_orig_esplit('/',$time_orig_end);
  3130.                       $events[array($course_info['id'],$row['id'],mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now),mktime($h_orig_e,$n_orig_e,$s_orig_e,$m_now,$d_orig_e,$y_now),$row['title'],$row['content']);
  3131.                     }
  3132.                     break;
  3133.                 case 'monthlyByDate':
  3134.                     $time_orig date('Y/n/j/G/i/s',$orig_start);
  3135.                     list($y_orig,$m_orig,$d_orig,$h_orig,$n_orig,$s_origsplit('/',$time_orig);
  3136.                     $time_now date('Y/n/j/G/i/s',$end);
  3137.                     list($y_now,$m_now,$d_now,$h_now,$n_now,$s_nowsplit('/',$time_now);
  3138.                     if((($y_now>$y_origOR (($y_now == $y_orig&& ($m_now>$m_orig))) && ($d_orig == $d_now))
  3139.                     {
  3140.                       $time_orig_end date('Y/n/j/G/i/s',$orig_end);
  3141.                       list($y_orig_e,$m_orig_e,$d_orig_e,$h_orig_e,$n_orig_e,$s_orig_esplit('/',$time_orig_end);
  3142.                       $events[array($course_info['id'],$row['id'],mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now),mktime($h_orig_e,$n_orig_e,$s_orig_e,$m_now,$d_orig_e,$y_now),$row['title'],$row['content']);
  3143.                     }
  3144.                     break;
  3145.                 case 'monthlyByDayR':
  3146.                     //not implemented yet
  3147.                     break;
  3148.                 case 'monthlyByDay':
  3149.                     //not implemented yet
  3150.                     break;
  3151.                 case 'yearly':
  3152.                     $time_orig date('Y/n/j/z/G/i/s',$orig_start);
  3153.                     list($y_orig,$m_orig,$d_orig,$dy_orig,$h_orig,$n_orig,$s_origsplit('/',$time_orig);
  3154.                     $time_now date('Y/n/j/z/G/i/s',$end);
  3155.                     list($y_now,$m_now,$d_now,$dy_now,$h_now,$n_now,$s_nowsplit('/',$time_now);
  3156.                     if(($y_now>$y_orig&& ($dy_orig == $dy_now))
  3157.                     {
  3158.                       $time_orig_end date('Y/n/j/G/i/s',$orig_end);
  3159.                       list($y_orig_e,$m_orig_e,$d_orig_e,$dy_orig_e,$h_orig_e,$n_orig_e,$s_orig_esplit('/',$time_orig_end);
  3160.                       $events[array($course_info['id'],$row['id'],mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now),mktime($h_orig_e,$n_orig_e,$s_orig_e,$m_now,$d_orig_e,$y_now),$row['title'],$row['content']);
  3161.                     }
  3162.                     break;
  3163.                 default:
  3164.                     break;
  3165.             }
  3166.         }
  3167.     }
  3168.     return $events;
  3169. }
  3170. /**
  3171.  * Get repeated events of a course between two dates (timespan of a week).
  3172.  * Returns an array containing the events
  3173.  * @param    string    Course info array (as returned by api_get_course_info())
  3174.  * @param    int        UNIX timestamp of span start. Defaults 0, later transformed into today's start
  3175.  * @param    int        UNIX timestamp. Defaults to 0, later transformed into today's end
  3176.  * @param   array   A set of parameters to alter the SQL query
  3177.  * @return    array    [int] => [course_id,parent_event_id,start_date,end_date,title,description]
  3178.  */
  3179. function get_repeated_events_week_view($course_info,$start=0,$end=0,$params)
  3180. {
  3181.     $events array();
  3182.     //block $end if higher than 2038 -- PHP doesn't go past that
  3183.     if($end>2145934800){$end 2145934800;}
  3184.     //initialise all values
  3185.     $y=0;
  3186.     $m=0;
  3187.     $d=0;
  3188.     if($start == or $end == 0)
  3189.     {
  3190.         $time time();
  3191.         $dw date('w',$time);
  3192.         $week_start $time (($dw-1)*86400);
  3193.         $y date('Y',$week_start);
  3194.         $m date('m',$week_start);
  3195.         $d date('j',$week_start);
  3196.         $w date('W',$week_start);
  3197.     }
  3198.     if($start==0)
  3199.     {
  3200.         $start mktime(0,0,0,$m,$d,$y);
  3201.     }
  3202.     $db_start date('Y-m-d H:i:s',$start);
  3203.     if($end==0)
  3204.     {
  3205.         $end $start+(86400*7)-1//start of week, more 7 days, minus 1 second to get back to the previoyus day
  3206.     }
  3207.     //$db_end = date('Y-m-d H:i:s',$end);
  3208.     
  3209.     $t_cal Database::get_course_table(TABLE_AGENDA,$course_info['dbName']);
  3210.     $t_cal_repeat Database::get_course_table(TABLE_AGENDA_REPEAT,$course_info['dbName']);
  3211.     $t_ip Database::get_course_table(TABLE_ITEM_PROPERTY,$course_info['dbName']);
  3212.     $sql "SELECT c.id, c.title, c.content, " .
  3213.             " UNIX_TIMESTAMP(c.start_date) as orig_start, UNIX_TIMESTAMP(c.end_date) as orig_end, " .
  3214.             " cr.cal_type, cr.cal_end " .
  3215.             " FROM $t_cal c, $t_cal_repeat cr, $t_ip as item_property .
  3216.             " WHERE cr.cal_end >= $start .
  3217.             " AND cr.cal_id = c.id " .
  3218.             " AND item_property.ref = c.id ".
  3219.             " AND item_property.tool = '".TOOL_CALENDAR_EVENT."' ".
  3220.             " AND c.start_date <= '$db_start"
  3221.             .(!empty($params['conditions'])?$params['conditions']:'')
  3222.             .(!empty($params['groupby'])?' GROUP BY '.$params['groupby']:'')
  3223.             .(!empty($params['orderby'])?' ORDER BY '.$params['orderby']:'');
  3224.     $res api_sql_query($sql,__FILE__,__LINE__);
  3225.     if(Database::num_rows($res)>0)
  3226.     {
  3227.         while($row Database::fetch_array($res))
  3228.         {
  3229.             $orig_start $row['orig_start'];
  3230.             $orig_end $row['orig_end'];
  3231.             $repeat_type $row['cal_type'];
  3232.             switch($repeat_type)
  3233.             {
  3234.                 case 'daily':
  3235.                     $time_orig_h date('H',$orig_start);
  3236.                     $time_orig_m date('i',$orig_start);
  3237.                     $time_orig_s date('s',$orig_start);
  3238.                     $int_time (($time_orig_h*60)+$time_orig_m)*60+$time_orig_s//time in seconds since 00:00:00
  3239.                     $span $orig_end $orig_start//total seconds between start and stop of original event
  3240.                     for($i=0;$i<7;$i++)
  3241.                     {
  3242.                         $current_start $start ($i*86400$int_time//unixtimestamp start of today's event 
  3243.                         $current_stop $start ($i*86400$int_time $span//unixtimestamp stop of today's event
  3244.                         $events[array($course_info['id'],$row['id'],$current_start,$current_stop,$row['title'],$row['content']);
  3245.                     }
  3246.                     break;
  3247.                 case 'weekly':
  3248.                     $time_orig date('Y/n/W/j/N/G/i/s',$orig_start);
  3249.                     list($y_orig,$m_orig,$w_orig,$d_orig,$dw_orig,$h_orig,$n_orig,$s_origsplit('/',$time_orig);
  3250.                     $time_now date('Y/n/W/j/N/G/i/s',$end);
  3251.                     list($y_now,$m_now,$w_now,$d_now,$dw_now,$h_now,$n_now,$s_nowsplit('/',$time_now);
  3252.                     if((($y_now>$y_origOR (($y_now == $y_orig&& ($w_now>$w_orig))))
  3253.                     //if the event is after the original (at least one week) and the day of the week is the same
  3254.                       $time_orig_end date('Y/n/W/j/N/G/i/s',$orig_end);
  3255.                       list($y_orig_e,$m_orig_e,$w_orig_e,$d_orig_e,$dw_orig_e,$h_orig_e,$n_orig_e,$s_orig_esplit('/',$time_orig_end);
  3256.                       $events[array($course_info['id'],$row['id'],mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now),mktime($h_orig_e,$n_orig_e,$s_orig_e,$m_now,$d_orig_e,$y_now),$row['title'],$row['content']);
  3257.                     }
  3258.                     break;
  3259.                 case 'monthlyByDate':
  3260.                     $time_orig date('Y/n/W/j/G/i/s',$orig_start);
  3261.                     list($y_orig,$m_orig,$w_orig,$d_orig,$h_orig,$n_orig,$s_origsplit('/',$time_orig);
  3262.                     $time_now date('Y/n/W/j/G/i/s',$end);
  3263.                     list($y_now,$m_now,$w_now,$d_now,$h_now,$n_now,$s_nowsplit('/',$time_now);
  3264.                     $event_repetition_time mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now)
  3265.                     if((($y_now>$y_origOR (($y_now == $y_orig&& ($m_now>$m_orig))) && ($start<$event_repetition_time && $event_repetition_time<$end))
  3266.                     //if the event is after the original (at least one month) and the original event's day is between the first day of the week and the last day of the week
  3267.                       $time_orig_end date('Y/n/j/G/i/s',$orig_end);
  3268.                       list($y_orig_e,$m_orig_e,$d_orig_e,$h_orig_e,$n_orig_e,$s_orig_esplit('/',$time_orig_end);
  3269.                       $events[array($course_info['id'],$row['id'],mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now),mktime($h_orig_e,$n_orig_e,$s_orig_e,$m_now,$d_orig_e,$y_now),$row['title'],$row['content']);
  3270.                     }
  3271.                     break;
  3272.                 case 'monthlyByDayR':
  3273.                     //not implemented yet
  3274.                     break;
  3275.                 case 'monthlyByDay':
  3276.                     //not implemented yet
  3277.                     break;
  3278.                 case 'yearly':
  3279.                     $time_orig date('Y/n/j/z/G/i/s',$orig_start);
  3280.                     list($y_orig,$m_orig,$d_orig,$dy_orig,$h_orig,$n_orig,$s_origsplit('/',$time_orig);
  3281.                     $time_now date('Y/n/j/z/G/i/s',$end);
  3282.                     list($y_now,$m_now,$d_now,$dy_now,$h_now,$n_now,$s_nowsplit('/',$time_now);
  3283.                     $event_repetition_time mktime($h_orig,$n_orig,$s_orig,$m_orig,$d_orig,$y_now)
  3284.                     if((($y_now>$y_orig&& ($start<$event_repetition_time && $event_repetition_time<$end)))
  3285.                     {
  3286.                       $time_orig_end date('Y/n/j/G/i/s',$orig_end);
  3287.                       list($y_orig_e,$m_orig_e,$d_orig_e,$dy_orig_e,$h_orig_e,$n_orig_e,$s_orig_esplit('/',$time_orig_end);
  3288.                       $events[array($course_info['id'],$row['id'],mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now),mktime($h_orig_e,$n_orig_e,$s_orig_e,$m_now,$d_orig_e,$y_now),$row['title'],$row['content']);
  3289.                     }
  3290.                     break;
  3291.                 default:
  3292.                     break;
  3293.             }
  3294.         }
  3295.     }
  3296.     return $events;
  3297. }
  3298. /**
  3299.  * Get repeated events of a course between two dates (timespan of a month).
  3300.  * Returns an array containing the events
  3301.  * @param   string  Course info array (as returned by api_get_course_info())
  3302.  * @param    int        UNIX timestamp of span start. Defaults 0, later transformed into today's start
  3303.  * @param    int        UNIX timestamp. Defaults to 0, later transformed into today's end
  3304.  * @param   array   A set of parameters to alter the SQL query
  3305.  * @return    array    [int] => [course_id,parent_event_id,start_date,end_date,title,description]
  3306.  */
  3307. function get_repeated_events_month_view($course_info,$start=0,$end=0,$params)
  3308. {
  3309.     $events array();
  3310.     //block $end if higher than 2038 -- PHP doesn't go past that
  3311.     if($end>2145934800){$end 2145934800;}
  3312.     //initialise all values
  3313.     $y=0;
  3314.     $m=0;
  3315.     $d=0;
  3316.     if($start == or $end == 0)
  3317.     {
  3318.         $time time();
  3319.         $y date('Y');
  3320.         $m date('m');
  3321.     }
  3322.     if($start==0)
  3323.     {
  3324.         $start mktime(0,0,0,$m,1,$y);
  3325.     }
  3326.     $db_start date('Y-m-d H:i:s',$start);
  3327.     if($end==0)
  3328.     {
  3329.         if($m==12)
  3330.         {
  3331.             $end mktime(0,0,0,1,1,$y+1)-1//start of next month, minus 1 second to get back to the previoyus day
  3332.         }
  3333.         else
  3334.         {
  3335.             $end mktime(0,0,0,$m+1,1,$y)-1;
  3336.         }
  3337.     }
  3338.     //$db_end = date('Y-m-d H:i:s',$end);
  3339.     
  3340.     $t_cal Database::get_course_table(TABLE_AGENDA,$course_info['dbName']);
  3341.     $t_cal_repeat Database::get_course_table(TABLE_AGENDA_REPEAT,$course_info['dbName']);
  3342.     $t_ip Database::get_course_table(TABLE_ITEM_PROPERTY,$course_info['dbName']);
  3343.     $sql "SELECT c.id, c.title, c.content, " .
  3344.             " UNIX_TIMESTAMP(c.start_date) as orig_start, UNIX_TIMESTAMP(c.end_date) as orig_end, " .
  3345.             " cr.cal_type, cr.cal_end " .
  3346.             " FROM $t_cal c, $t_cal_repeat cr, $t_ip as item_property .
  3347.             " WHERE cr.cal_end >= $start .
  3348.             " AND cr.cal_id = c.id " .
  3349.             " AND item_property.ref = c.id ".
  3350.             " AND item_property.tool = '".TOOL_CALENDAR_EVENT."' ".
  3351.             " AND c.start_date <= '$db_start"
  3352.             .(!empty($params['conditions'])?$params['conditions']:'')
  3353.             .(!empty($params['groupby'])?' GROUP BY '.$params['groupby']:'')
  3354.             .(!empty($params['orderby'])?' ORDER BY '.$params['orderby']:'');
  3355.     $res api_sql_query($sql,__FILE__,__LINE__);
  3356.     if(Database::num_rows($res)>0)
  3357.     {
  3358.         while($row Database::fetch_array($res))
  3359.         {
  3360.             $orig_start $row['orig_start'];
  3361.             $orig_end $row['orig_end'];
  3362.             $repeat_type $row['cal_type'];
  3363.             switch($repeat_type)
  3364.             {
  3365.                 case 'daily':
  3366.                     $time_orig_h date('H',$orig_start);
  3367.                     $time_orig_m date('i',$orig_start);
  3368.                     $time_orig_s date('s',$orig_start);
  3369.                     $month_last_day date('d',$end);
  3370.                     $int_time (($time_orig_h*60)+$time_orig_m)*60+$time_orig_s//time in seconds since 00:00:00
  3371.                     $span $orig_end $orig_start//total seconds between start and stop of original event
  3372.                     for($i=0;$i<$month_last_day;$i++)
  3373.                     {
  3374.                         $current_start $start ($i*86400$int_time//unixtimestamp start of today's event 
  3375.                         $current_stop $start ($i*86400$int_time $span//unixtimestamp stop of today's event
  3376.                         $events[array($course_info['id'],$row['id'],$current_start,$current_stop,$row['title'],$row['content']);
  3377.                     }
  3378.                     break;
  3379.                 case 'weekly':
  3380.                     //A weekly repeated event is very difficult to catch in a month view,
  3381.                     //because weeks start before or at the same time as the first day of the month
  3382.                     //The same can be said for the end of the month.
  3383.                     // The idea is thus to get all possible events by enlarging the scope of
  3384.                     // the month to get complete weeks covering the complete month, and then take out
  3385.                     // the events that start before the 1st ($start) or after the last day of the month ($end) 
  3386.                     $time_orig date('Y/n/W/j/N/G/i/s',$orig_start);
  3387.                     list($y_orig,$m_orig,$w_orig,$d_orig,$dw_orig,$h_orig,$n_orig,$s_origsplit('/',$time_orig);
  3388.                     $time_orig_end date('Y/n/W/j/N/G/i/s',$orig_end);
  3389.                     list($y_orig_e,$m_orig_e,$w_orig_e,$d_orig_e,$dw_orig_e,$h_orig_e,$n_orig_e,$s_orig_esplit('/',$time_orig_end);
  3390.  
  3391.                     $time_now date('Y/n/W/j/N/G/i/s',$end);
  3392.                     list($y_now,$m_now,$w_now,$d_now,$dw_now,$h_now,$n_now,$s_nowsplit('/',$time_now);
  3393.  
  3394.                     $month_first_week date('W',$start);
  3395.                     $month_last_week date('W',$end);
  3396.  
  3397.                     if(($y_now>$y_origOR (($y_now == $y_orig&& ($w_now>$w_orig)))
  3398.                     //if the event is after the original (at least one week) and the day of the week is the same
  3399.                         for($i=$month_first_week;$i<=$month_last_week;$i++)
  3400.                         {
  3401.                           //the "day of the week" of repetition is the same as the $dw_orig,
  3402.                           //so to get the "day of the month" from the "day of the week", we have
  3403.                           //to get the first "day of the week" for this week and add the number
  3404.                           //of days (in seconds) to reach the $dw_orig
  3405.                           //example: the first week spans between the 28th of April (Monday) to the
  3406.                           // 4th of May (Sunday). The event occurs on the 2nd day of each week.
  3407.                           // This means the event occurs on 29/4, 6/5, 13/5, 20/5 and 27/5.
  3408.                           // We want to get all of these, and then reject 29/4 because it is out
  3409.                           // of the month itself.
  3410.                           
  3411.                           //First, to get the start time of the first day of the month view (even if
  3412.                           // the day is from the past month), we get the month start date (1/5) and
  3413.                           // see which day of the week it is, and subtract the number of days necessary
  3414.                           // to get back to the first day of the week.
  3415.                           $month_first_day_weekday date('N',$start);
  3416.                           $first_week_start $start (($month_first_day_weekday-1)*86400)
  3417.                           
  3418.                           //Second, we add the week day of the original event, so that we have an
  3419.                           // absolute time that represents the first repetition of the event in
  3420.                           // our 4- or 5-weeks timespan
  3421.                           $first_event_repeat_start $first_week_start (($dw_orig-1)*86400($h_orig*3600($n_orig*60$s_orig;
  3422.  
  3423.                           //Third, we start looping through the repetitions and see if they are between
  3424.                           // $start and $end
  3425.                           for($i $first_event_repeat_start$i<=$end$i+=604800)
  3426.                           {
  3427.                               if($start<$i && $i<$end)
  3428.                             {
  3429.                                list($y_repeat,$m_repeat,$d_repeat,$h_repeat,$n_repeat,$s_repeatsplit('/',date('Y/m/j/H/i/s',$i));
  3430.                                $events[array($course_info['id'],$row['id'],mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now),mktime($h_orig_e,$n_orig_e,$s_orig_e,$m_now,$d_orig_e,$y_now),$row['title'],$row['content']);
  3431.                             }
  3432.                           }
  3433.                         }
  3434.                     }
  3435.                     break;
  3436.                 case 'monthlyByDate':
  3437.                     $time_orig date('Y/n/W/j/G/i/s',$orig_start);
  3438.                     list($y_orig,$m_orig,$w_orig,$d_orig,$h_orig,$n_orig,$s_origsplit('/',$time_orig);
  3439.                     $time_now date('Y/n/W/j/G/i/s',$end);
  3440.                     list($y_now,$m_now,$w_now,$d_now,$h_now,$n_now,$s_nowsplit('/',$time_now);
  3441.                     $event_repetition_time mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now)
  3442.                     if(($y_now>$y_origOR (($y_now == $y_orig&& ($m_now>$m_orig)))
  3443.                     //if the event is after the original (at least one month) and the original event's day is between the first day of the week and the last day of the week
  3444.                       $time_orig_end date('Y/n/j/G/i/s',$orig_end);
  3445.                       list($y_orig_e,$m_orig_e,$d_orig_e,$h_orig_e,$n_orig_e,$s_orig_esplit('/',$time_orig_end);
  3446.                       $events[array($course_info['id'],$row['id'],mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now),mktime($h_orig_e,$n_orig_e,$s_orig_e,$m_now,$d_orig_e,$y_now),$row['title'],$row['content']);
  3447.                     }
  3448.                     break;
  3449.                 case 'monthlyByDayR':
  3450.                     //not implemented yet
  3451.                     break;
  3452.                 case 'monthlyByDay':
  3453.                     //not implemented yet
  3454.                     break;
  3455.                 case 'yearly':
  3456.                     $time_orig date('Y/n/j/z/G/i/s',$orig_start);
  3457.                     list($y_orig,$m_orig,$d_orig,$dy_orig,$h_orig,$n_orig,$s_origsplit('/',$time_orig);
  3458.                     $time_now date('Y/n/j/z/G/i/s',$end);
  3459.                     list($y_now,$m_now,$d_now,$dy_now,$h_now,$n_now,$s_nowsplit('/',$time_now);
  3460.                     $event_repetition_time mktime($h_orig,$n_orig,$s_orig,$m_orig,$d_orig,$y_now)
  3461.                     if((($y_now>$y_orig&& ($start<$event_repetition_time && $event_repetition_time<$end)))
  3462.                     {
  3463.                       $time_orig_end date('Y/n/j/G/i/s',$orig_end);
  3464.                       list($y_orig_e,$m_orig_e,$d_orig_e,$dy_orig_e,$h_orig_e,$n_orig_e,$s_orig_esplit('/',$time_orig_end);
  3465.                       $events[array($course_info['id'],$row['id'],mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now),mktime($h_orig_e,$n_orig_e,$s_orig_e,$m_now,$d_orig_e,$y_now),$row['title'],$row['content']);
  3466.                     }
  3467.                     break;
  3468.                 default:
  3469.                     break;
  3470.             }
  3471.         }
  3472.     }
  3473.     return $events;
  3474. }
  3475. /**
  3476.  * Get repeated events of a course between two dates (1 year timespan). Used for the list display.
  3477.  * This is virtually unlimited but by default it shortens to 100 years from now (even a birthday shouldn't be useful more than this time - except for turtles)
  3478.  * Returns an array containing the events
  3479.  * @param   string  Course info array (as returned by api_get_course_info())
  3480.  * @param   int     UNIX timestamp of span start. Defaults 0, later transformed into today's start
  3481.  * @param   int     UNIX timestamp. Defaults to 0, later transformed into today's end
  3482.  * @param   array   A set of parameters to alter the SQL query
  3483.  * @return  array   [int] => [course_id,parent_event_id,start_date,end_date,title,description]
  3484.  */
  3485. function get_repeated_events_list_view($course_info,$start=0,$end=0,$params)
  3486. {
  3487.     $events array();
  3488.     //block $end if higher than 2038 -- PHP doesn't go past that
  3489.     if($end>2145934800){$end 2145934800;}
  3490.     //initialise all values
  3491.     $y=0;
  3492.     $m=0;
  3493.     $d=0;
  3494.     if(empty($startor empty($end))
  3495.     {
  3496.         $time time();
  3497.         $y date('Y');
  3498.         $m date('m');
  3499.     }
  3500.     if(empty($start))
  3501.     {
  3502.         $start mktime(000$m1$y);
  3503.     }
  3504.     $db_start date('Y-m-d H:i:s'$start);
  3505.     if(empty($end))
  3506.     {
  3507.         $end mktime(000112037);
  3508.     }
  3509.     //$db_end = date('Y-m-d H:i:s',$end);
  3510.     
  3511.     $t_cal Database::get_course_table(TABLE_AGENDA,$course_info['dbName']);
  3512.     $t_cal_repeat Database::get_course_table(TABLE_AGENDA_REPEAT,$course_info['dbName']);
  3513.     $t_ip Database::get_course_table(TABLE_ITEM_PROPERTY,$course_info['dbName']);
  3514.     $sql "SELECT c.id, c.title, c.content, " .
  3515.             " UNIX_TIMESTAMP(c.start_date) as orig_start, UNIX_TIMESTAMP(c.end_date) as orig_end, " .
  3516.             " cr.cal_type, cr.cal_end " .
  3517.             " FROM $t_cal c, $t_cal_repeat cr, $t_ip as item_property .
  3518.             " WHERE cr.cal_end >= $start .
  3519.             " AND cr.cal_id = c.id " .
  3520.             " AND item_property.ref = c.id ".
  3521.             " AND item_property.tool = '".TOOL_CALENDAR_EVENT."' ".
  3522.             " AND c.start_date <= '$db_start"
  3523.             .(!empty($params['conditions'])?$params['conditions']:'')
  3524.             .(!empty($params['groupby'])?' GROUP BY '.$params['groupby']:'')
  3525.             .(!empty($params['orderby'])?' ORDER BY '.$params['orderby']:'');
  3526.     $res api_sql_query($sql,__FILE__,__LINE__);
  3527.     if(Database::num_rows($res)>0)
  3528.     {
  3529.         while($row Database::fetch_array($res))
  3530.         {
  3531.             $orig_start $row['orig_start'];
  3532.             $orig_end $row['orig_end'];
  3533.             $repeat_type $row['cal_type'];
  3534.             $repeat_end $row['cal_end'];
  3535.             switch($repeat_type)
  3536.             {
  3537.                 case 'daily':
  3538.                     $time_orig_h date('H',$orig_start);
  3539.                     $time_orig_m date('i',$orig_start);
  3540.                     $time_orig_s date('s',$orig_start);
  3541.                     $span $orig_end $orig_start//total seconds between start and stop of original event
  3542.                     for($i=$orig_start+86400;($i<$end && $i<=$repeat_end);$i+=86400)
  3543.                     {
  3544.                         $current_start $i//unixtimestamp start of today's event 
  3545.                         $current_stop $i $span//unixtimestamp stop of today's event
  3546.                         $events[array($course_info['id'],$row['id'],$current_start,$current_stop,$row['title'],$row['content']);
  3547.                     }
  3548.                     break;
  3549.                 case 'weekly':
  3550.                     //A weekly repeated event is very difficult to catch in a month view,
  3551.                     // because weeks start before or at the same time as the first day of the month
  3552.                     //The same can be said for the end of the month.
  3553.                     // The idea is thus to get all possible events by enlarging the scope of
  3554.                     // the month to get complete weeks covering the complete month, and then take out
  3555.                     // the events that start before the 1st ($start) or after the last day of the month ($end) 
  3556.                     $time_orig date('Y/n/W/j/N/G/i/s',$orig_start);
  3557.                     list($y_orig,$m_orig,$w_orig,$d_orig,$dw_orig,$h_orig,$n_orig,$s_origsplit('/',$time_orig);
  3558.                     $time_orig_end date('Y/n/W/j/N/G/i/s',$orig_end);
  3559.                     list($y_orig_e,$m_orig_e,$w_orig_e,$d_orig_e,$dw_orig_e,$h_orig_e,$n_orig_e,$s_orig_esplit('/',$time_orig_end);
  3560.  
  3561.                     $time_now date('Y/n/W/j/N/G/i/s',$end);
  3562.                     list($y_now,$m_now,$w_now,$d_now,$dw_now,$h_now,$n_now,$s_nowsplit('/',$time_now);
  3563.                     if($w_now==52)
  3564.                     {
  3565.                         ++$y_now;
  3566.                         $w_now=1;    
  3567.                     }
  3568.                     else
  3569.                     {
  3570.                         ++$w_now;
  3571.                     }
  3572.                     $month_first_week date('W',$start);
  3573.                     $total_weeks ((date('Y',$end)-$y_orig)-1)*52;
  3574.                     $month_last_week $month_first_week $total_weeks;
  3575.  
  3576.                     if(($y_now>$y_origOR (($y_now == $y_orig&& ($w_now>$w_orig)))
  3577.                     //if the event is after the original (at least one week) and the day of the week is the same
  3578.                         //for($i=$month_first_week;($i<=$month_last_week && $i<1000);$i++)
  3579.                         //{
  3580.                             
  3581.                         
  3582.                           /*
  3583.                            The "day of the week" of repetition is the same as the $dw_orig,
  3584.                            so to get the "day of the month" from the "day of the week", we have
  3585.                            to get the first "day of the week" for this week and add the number
  3586.                            of days (in seconds) to reach the $dw_orig
  3587.                           example: the first week spans between the 28th of April (Monday) to the
  3588.                            4th of May (Sunday). The event occurs on the 2nd day of each week.
  3589.                            This means the event occurs on 29/4, 6/5, 13/5, 20/5 and 27/5.
  3590.                            We want to get all of these, and then reject 29/4 because it is out
  3591.                            of the month itself.
  3592.                           First, to get the start time of the first day of the month view (even if
  3593.                            the day is from the past month), we get the month start date (1/5) and
  3594.                            see which day of the week it is, and subtract the number of days necessary
  3595.                            to get back to the first day of the week.
  3596.                           */
  3597.                           $month_first_day_weekday date('N',$start);
  3598.                           $first_week_start $start (($month_first_day_weekday-1)*86400)
  3599.                           
  3600.                           //Second, we add the week day of the original event, so that we have an
  3601.                           // absolute time that represents the first repetition of the event in
  3602.                           // our 4- or 5-weeks timespan
  3603.                           $first_event_repeat_start $first_week_start (($dw_orig-1)*86400($h_orig*3600($n_orig*60$s_orig;
  3604.  
  3605.                           //Third, we start looping through the repetitions and see if they are between
  3606.                           // $start and $end
  3607.                           for($i $first_event_repeat_start($i<=$end && $i<=$repeat_end)$i+=604800)
  3608.                           {
  3609.                             if($start<$i && $i<=$end && $i<=$repeat_end)
  3610.                             {
  3611.                                list($y_repeat,$m_repeat,$d_repeat,$h_repeat,$n_repeat,$s_repeatsplit('/',date('Y/m/j/H/i/s',$i));
  3612.                                $new_start_time mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now);
  3613.                                $new_stop_time mktime($h_orig_e,$n_orig_e,$s_orig_e,$m_now,$d_orig_e,$y_now);
  3614.                                $events[array($course_info['id']$row['id']$new_start_time$new_stop_time$row['title']$row['content']);
  3615.                             }
  3616.                             $time_now date('Y/n/W/j/N/G/i/s',$i+604800);
  3617.                             list($y_now,$m_now,$w_now,$d_now,$dw_now,$h_now,$n_now,$s_nowsplit('/',$time_now);
  3618.                           }
  3619.                         //}
  3620.                     }
  3621.                     break;
  3622.                 case 'monthlyByDate':
  3623.                     $time_orig date('Y/n/W/j/G/i/s',$orig_start);
  3624.                     list($y_orig,$m_orig,$w_orig,$d_orig,$h_orig,$n_orig,$s_origsplit('/',$time_orig);
  3625.                     
  3626.                     $time_now date('Y/n/W/j/G/i/s',$start);
  3627.                     list($y_now,$m_now,$w_now,$d_now,$h_now,$n_now,$s_nowsplit('/',$time_now);
  3628.                     //make sure we are one month ahead (to avoid being the same month as the original event)
  3629.                     if($m_now==12)
  3630.                     {
  3631.                         ++$y_now;
  3632.                         $m_now 1;
  3633.                     }
  3634.                     else
  3635.                     {
  3636.                         ++$m_now;
  3637.                     }
  3638.                     
  3639.                     $time_orig_end date('Y/n/j/G/i/s',$orig_end);
  3640.                     list($y_orig_e,$m_orig_e,$d_orig_e,$h_orig_e,$n_orig_e,$s_orig_esplit('/',$time_orig_end);
  3641.  
  3642.                     $event_repetition_time mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now)
  3643.                     $diff $orig_end $orig_start;
  3644.                     while((($y_now>$y_origOR (($y_now == $y_orig&& ($m_now>$m_orig))) && ($event_repetition_time $end&& ($event_repetition_time $repeat_end))
  3645.                     //if the event is after the original (at least one month) and the original event's day is between the first day of the week and the last day of the week
  3646.                       $new_start_time mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now);
  3647.                       $new_stop_time mktime($h_orig_e,$n_orig_e,$s_orig_e,$m_now,$d_orig_e,$y_now);
  3648.                       $events[array($course_info['id'],$row['id'],$new_start_time,$new_stop_time,$row['title'],$row['content']);
  3649.                       if($m_now==12)
  3650.                       {
  3651.                           ++$y_now;
  3652.                         $m_now 1;
  3653.                       }
  3654.                       else
  3655.                       {
  3656.                         ++$m_now;
  3657.                       }
  3658.                       $event_repetition_time mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now)
  3659.                     }
  3660.                     break;
  3661.                 case 'monthlyByDayR':
  3662.                     //not implemented yet
  3663.                     break;
  3664.                 case 'monthlyByDay':
  3665.                     //not implemented yet
  3666.                     break;
  3667.                 case 'yearly':
  3668.                     $time_orig date('Y/n/j/z/G/i/s',$orig_start);
  3669.                     list($y_orig,$m_orig,$d_orig,$dy_orig,$h_orig,$n_orig,$s_origsplit('/',$time_orig);
  3670.                     $time_now date('Y/n/j/z/G/i/s',$end);
  3671.                     list($y_now,$m_now,$d_now,$dy_now,$h_now,$n_now,$s_nowsplit('/',$time_now);
  3672.                     $event_repetition_time mktime($h_orig,$n_orig,$s_orig,$m_orig,$d_orig,$y_now)
  3673.                     while((($y_now>$y_orig&& ($start<$event_repetition_time && $event_repetition_time<$end && $event_repetition_time<$repeat_end)))
  3674.                     {
  3675.                       $time_orig_end date('Y/n/j/G/i/s',$orig_end);
  3676.                       list($y_orig_e,$m_orig_e,$d_orig_e,$dy_orig_e,$h_orig_e,$n_orig_e,$s_orig_esplit('/',$time_orig_end);
  3677.                       $events[array($course_info['id'],$row['id'],mktime($h_orig,$n_orig,$s_orig,$m_now,$d_orig,$y_now),mktime($h_orig_e,$n_orig_e,$s_orig_e,$m_now,$d_orig_e,$y_now),$row['title'],$row['content']);
  3678.                       ++$y_now;
  3679.                       $event_repetition_time mktime($h_orig,$n_orig,$s_orig,$m_orig,$d_orig,$y_now)
  3680.                     }
  3681.                     break;
  3682.                 default:
  3683.                     break;
  3684.             }
  3685.         }
  3686.     }
  3687.     return $events;
  3688. }
  3689. /**
  3690.  * Tells if an agenda item is repeated
  3691.  * @param   string  Course database
  3692.  * @param   int     The agenda item
  3693.  * @return  boolean True if repeated, false otherwise
  3694.  */
  3695. function is_repeated_event($id,$course=null)
  3696. {
  3697.     if(empty($course))
  3698.     {
  3699.         $course_info api_get_course_info();
  3700.         $course $course_info['dbName']
  3701.     }
  3702.     $id = (int) $id;
  3703.     $t_agenda_repeat Database::get_course_table(TABLE_AGENDA_REPEAT,$course);
  3704.     $sql "SELECT * FROM $t_agenda_repeat WHERE cal_id = $id";
  3705.     $res Database::query($sql,__FILE__,__LINE__);
  3706.     if(Database::num_rows($res)>0)
  3707.     {
  3708.         return true;
  3709.     }
  3710.     return false;
  3711. }
  3712. /**
  3713.  * Adds x weeks to a UNIX timestamp
  3714.  * @param   int     The timestamp
  3715.  * @param   int     The number of weeks to add
  3716.  * @return  int     The new timestamp
  3717.  */
  3718. function add_week($timestamp,$num=1)
  3719. {
  3720.     return $timestamp $num*604800;
  3721. }
  3722. /**
  3723.  * Adds x months to a UNIX timestamp
  3724.  * @param   int     The timestamp
  3725.  * @param   int     The number of years to add
  3726.  * @return  int     The new timestamp
  3727.  */
  3728. function add_month($timestamp,$num=1)
  3729. {
  3730.     list($y$m$d$h$n$ssplit('/',date('Y/m/d/h/i/s',$timestamp));
  3731.     if($m+$num>12)
  3732.     {
  3733.         $y += floor($num/12);
  3734.         $m += $num%12;
  3735.     }
  3736.     else
  3737.     {
  3738.         $m += $num;
  3739.     }
  3740.     return mktime($h$n$s$m$d$y);
  3741. }
  3742. /**
  3743.  * Adds x years to a UNIX timestamp
  3744.  * @param   int     The timestamp
  3745.  * @param   int     The number of years to add
  3746.  * @return  int     The new timestamp
  3747.  */
  3748. function add_year($timestamp,$num=1)
  3749. {
  3750.     list($y$m$d$h$n$ssplit('/',date('Y/m/d/h/i/s',$timestamp));
  3751.     return mktime($h$n$s$m$d$y+$num);
  3752. }
  3753. /**
  3754.  * Adds an agenda item in the database. Similar to store_new_agenda_item() except it takes parameters
  3755.  * @param   array   Course info
  3756.  * @param   string  Event title
  3757.  * @param   string  Event content/description
  3758.  * @param   string  Start date
  3759.  * @param   string  End date
  3760.  * @param   array   List of groups to which this event is added
  3761.  * @param   int     Parent id (optional)
  3762.  * @return  int     The new item's DB ID
  3763.  */
  3764. function agenda_add_item($course_info$title$content$db_start_date$db_end_date$to=array()$parent_id=null)
  3765. {
  3766.     $user_id    api_get_user_id();
  3767.     $t_agenda   Database::get_course_table(TABLE_AGENDA,$course_info['dbName']);
  3768.     
  3769.     // some filtering of the input data
  3770.     $title      Database::escape_string($title)// no html allowed in the title
  3771.     $content    Database::escape_string($content);
  3772.     $start_date Database::escape_string($db_start_date);
  3773.     $end_date   Database::escape_string($db_end_date);
  3774.     
  3775.     // store in the table calendar_event
  3776.     $sql "INSERT INTO ".$t_agenda."
  3777.                             (title,content, start_date, end_date".(!empty($parent_id)?',parent_event_id':'').")
  3778.                             VALUES
  3779.                             ('".$title."','".$content."', '".$start_date."','".$end_date."'".(!empty($parent_id)?','.((int)$parent_id):'').")";
  3780.     
  3781.     $result api_sql_query($sql,__FILE__,__LINE__or die (Database::error());
  3782.     $last_id=Database::insert_id();
  3783.     
  3784.     // store in last_tooledit (first the groups, then the users
  3785.     $done false;
  3786.     if ((!is_null($to))or (!empty($_SESSION['toolgroup']))) // !is_null($to): when no user is selected we send it to everyone
  3787.     {
  3788.         $send_to=separate_users_groups($to);
  3789.         // storing the selected groups
  3790.         if (is_array($send_to['groups']))
  3791.         {
  3792.             foreach ($send_to['groups'as $group)
  3793.             {
  3794.                 api_item_property_update($course_infoTOOL_CALENDAR_EVENT$last_id"AgendaAdded"$user_id$group,'',$start_date$end_date);
  3795.                 $done true;
  3796.             }
  3797.         }
  3798.         // storing the selected users
  3799.         if (is_array($send_to['users']))
  3800.         {
  3801.             foreach ($send_to['users'as $user)
  3802.             {
  3803.                 api_item_property_update($course_infoTOOL_CALENDAR_EVENT$last_id"AgendaAdded"$user_id,'',$user$start_date,$end_date);
  3804.                 $done true;
  3805.             }
  3806.         }
  3807.     }
  3808.     
  3809.     if(!$done// the message is sent to everyone, so we set the group to 0
  3810.     {
  3811.         api_item_property_update($course_infoTOOL_CALENDAR_EVENT$last_id"AgendaAdded"$user_id'','',$start_date,$end_date);
  3812.     }
  3813.     // storing the resources
  3814.     store_resources($_SESSION['source_type'],$last_id);
  3815.     return $last_id;    
  3816. }
  3817. /**
  3818.  * Adds a repetitive item to the database
  3819.  * @param   array   Course info
  3820.  * @param   int     The original event's id
  3821.  * @param   string  Type of repetition
  3822.  * @param   int     Timestamp of end of repetition (repeating until that date)
  3823.  * @param   array   Original event's destination
  3824.  * @return  boolean False if error, True otherwise
  3825.  */
  3826. function agenda_add_repeat_item($course_info,$orig_id,$type,$end,$orig_dest)
  3827. {
  3828.     $t_agenda   Database::get_course_table(TABLE_AGENDA,$course_info['dbName']);
  3829.     $t_agenda_r Database::get_course_table(TABLE_AGENDA_REPEAT,$course_info['dbName']);
  3830.     //$sql = "SELECT title, content, UNIX_TIMESTAMP(start_date) as sd, UNIX_TIMESTAMP(end_date) as ed FROM $t_agenda WHERE id = $orig_id";
  3831.     $sql "SELECT title, content, start_date as sd, end_date as ed FROM $t_agenda WHERE id = $orig_id";
  3832.     $res Database::query($sql,__FILE__,__LINE__);
  3833.     if(Database::num_rows($res)!==1){return false;}
  3834.     $row Database::fetch_array($res);
  3835.     //$orig_start = $row['sd'];
  3836.     $orig_start mktime(substr($row['sd'],11,2),substr($row['sd'],14,2),substr($row['sd'],17,2),substr($row['sd'],5,2),substr($row['sd'],8,2),substr($row['sd'],0,4));
  3837.     //$orig_end   = $row['ed'];
  3838.     $orig_end   mktime(substr($row['ed'],11,2),substr($row['ed'],14,2),substr($row['ed'],17,2),substr($row['ed'],5,2),substr($row['ed'],8,2),substr($row['ed'],0,4));
  3839.     $diff $orig_end $orig_start;
  3840.     $orig_title $row['title'];
  3841.     $orig_content $row['content'];
  3842.     $now time();
  3843.     $type Database::escape_string($type);
  3844.     $end = (int) $end;
  3845.     if(1<=$end && $end<=500)
  3846.     {
  3847.         //we assume that, with this type of value, the user actually gives a count of repetitions
  3848.         //and that he wants us to calculate the end date with that (particularly in case of imports from ical)
  3849.         switch($type)
  3850.         {
  3851.             case 'daily':
  3852.                 $end $orig_start (86400*$end);
  3853.                 break;
  3854.             case 'weekly':
  3855.                 $end add_week($orig_start,$end);
  3856.                 break;
  3857.             case 'monthlyByDate':
  3858.                 $end add_month($orig_start,$end);
  3859.                 break;
  3860.             case 'monthlyByDay':
  3861.                 //TODO
  3862.                 break;
  3863.             case 'monthlyByDayR':
  3864.                 //TODO
  3865.                 break;
  3866.             case 'yearly':
  3867.                 $end add_year($orig_start,$end);
  3868.                 break;
  3869.         }
  3870.     }
  3871.     if($end $now 
  3872.         && in_array($type,array('daily','weekly','monthlyByDate','monthlyByDay','monthlyByDayR','yearly')))
  3873.     {
  3874.        $sql "INSERT INTO $t_agenda_r (cal_id, cal_type, cal_end).
  3875.             " VALUES ($orig_id,'$type',$end)";
  3876.        $res Database::query($sql,__FILE__,__LINE__);
  3877.         switch($type)
  3878.         {
  3879.             case 'daily':
  3880.                 for($i $orig_start 86400($i <= $end)$i += 86400)
  3881.                 {
  3882.                     $res agenda_add_item($course_info$orig_title$orig_contentdate('Y-m-d H:i:s'$i)date('Y-m-d H:i:s'$i+$diff)$orig_dest$orig_id);
  3883.                 }
  3884.                 break;
  3885.             case 'weekly':
  3886.                 for($i $orig_start 604800($i <= $end)$i += 604800)
  3887.                 {
  3888.                     $res agenda_add_item($course_info$orig_title$orig_contentdate('Y-m-d H:i:s'$i)date('Y-m-d H:i:s'$i+$diff)$orig_dest$orig_id);
  3889.                 }
  3890.                 break;
  3891.             case 'monthlyByDate':
  3892.                 $next_start add_month($orig_start);
  3893.                 while($next_start <= $end)
  3894.                 {
  3895.                     $res agenda_add_item($course_info$orig_title$orig_contentdate('Y-m-d H:i:s'$next_start)date('Y-m-d H:i:s'$next_start+$diff)$orig_dest$orig_id);
  3896.                     $next_start add_month($next_start);
  3897.                 }
  3898.                 break;
  3899.             case 'monthlyByDay':
  3900.                 //not yet implemented
  3901.                 break;
  3902.             case 'monthlyByDayR':
  3903.                 //not yet implemented
  3904.                 break;
  3905.             case 'yearly':
  3906.                 $next_start add_year($orig_start);
  3907.                 while($next_start <= $end)
  3908.                 {
  3909.                     $res agenda_add_item($course_info$orig_title$orig_contentdate('Y-m-d H:i:s'$next_start)date('Y-m-d H:i:s'$next_start+$diff)$orig_dest$orig_id);
  3910.                     $next_start add_year($next_start);
  3911.                 }
  3912.                 break;
  3913.         }
  3914.     }
  3915.     return true;
  3916. }
  3917. /**
  3918.  * Import an iCal file into the database
  3919.  * @param   array   Course info
  3920.  * @return  boolean True on success, false otherwise
  3921.  */
  3922. function agenda_import_ical($course_info,$file)
  3923. {
  3924.     require_once(api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  3925.     $charset api_get_setting('platform_charset');
  3926.     $filepath api_get_path(GARBAGE_PATH).$file['name'];
  3927.     if(!@move_uploaded_file($file['tmp_name'],$filepath))
  3928.     {
  3929.         error_log('Problem moving uploaded file: '.$file['error'].' in '.__FILE__.' line '.__LINE__);
  3930.         return false;
  3931.     }
  3932.     require_once (api_get_path(LIBRARY_PATH).'icalcreator/iCalcreator.class.php');
  3933.     $ical new vcalendar();
  3934.     $ical->setConfig'directory'dirname($filepath) );
  3935.     $ical->setConfig'filename'basename($filepath) );
  3936.     $ical->parse();
  3937.     //we need to recover: summary, description, dtstart, dtend, organizer, attendee, location (=course name), 
  3938.     // rrule
  3939.     $ve $ical->getComponent(0);
  3940.     //print_r($ve);
  3941.     $ttitle $ve->getProperty('summary');
  3942.     //print_r($ttitle);
  3943.     $title mb_convert_encoding($ttitle,$charset,'UTF-8');
  3944.     $tdesc $ve->getProperty('description');
  3945.     $desc mb_convert_encoding($tdesc,$charset,'UTF-8');
  3946.     $ts $ve->getProperty('dtstart');
  3947.     $start_date $ts['year'].'-'.$ts['month'].'-'.$ts['day'].' '.$ts['hour'].':'.$ts['min'].':'.$ts['sec'];
  3948.     $ts $ve->getProperty('dtend');
  3949.     $end_date $ts['year'].'-'.$ts['month'].'-'.$ts['day'].' '.$ts['hour'].':'.$ts['min'].':'.$ts['sec'];
  3950.     //echo $start_date.' - '.$end_date;
  3951.     $organizer $ve->getProperty('organizer');
  3952.     $attendee $ve->getProperty('attendee');
  3953.     $course_name $ve->getProperty('location');
  3954.     //insert the event in our database
  3955.     $id agenda_add_item($course_info,$title,$desc,$start_date,$end_date,$_POST['selectedform']);
  3956.     
  3957.     $repeat $ve->getProperty('rrule');
  3958.     if(is_array($repeat&& !empty($repeat['FREQ']))
  3959.     {
  3960.         $trans array('DAILY'=>'daily','WEEKLY'=>'weekly','MONTHLY'=>'monthlyByDate','YEARLY'=>'yearly');
  3961.         $freq $trans[$repeat['FREQ']];
  3962.         $interval $repeat['INTERVAL'];
  3963.         if(isset($repeat['UNTIL']&& is_array($repeat['UNTIL']))
  3964.         {
  3965.             $until mktime(23,59,59,$repeat['UNTIL']['month'],$repeat['UNTIL']['day'],$repeat['UNTIL']['year']);
  3966.             $res agenda_add_repeat_item($course_info,$id,$freq,$until,$_POST['selectedform']);
  3967.         }
  3968.         //TODO: deal with count
  3969.         if(!empty($repeat['COUNT']))
  3970.         {
  3971.             $count $repeat['COUNT'];
  3972.             $res agenda_add_repeat_item($course_info,$id,$freq,$count,$_POST['selectedform']);            
  3973.         }
  3974.     }
  3975.     return true;
  3976. }
  3977. ?>

Documentation generated on Thu, 12 Jun 2008 12:56:28 -0500 by phpDocumentor 1.4.1