Example #1
0
/**
 * Adds a repetitive item to the database
 * @param   array   Course info
 * @param   int     The original event's id
 * @param   string  Type of repetition
 * @param   int     Timestamp of end of repetition (repeating until that date)
 * @param   array   Original event's destination (users list)
 * @param 	string  a comment about a attachment file into agenda
 * @return  boolean False if error, True otherwise
 */
function agenda_add_repeat_item($course_info, $orig_id, $type, $end, $orig_dest, $file_comment = '')
{
    $t_agenda = Database::get_course_table(TABLE_AGENDA);
    $t_agenda_r = Database::get_course_table(TABLE_AGENDA_REPEAT);
    $course_id = $course_info['real_id'];
    $sql = 'SELECT title, content, start_date as sd, end_date as ed FROM ' . $t_agenda . ' WHERE c_id = ' . $course_id . ' AND id ="' . intval($orig_id) . '" ';
    $res = Database::query($sql);
    if (Database::num_rows($res) !== 1) {
        return false;
    }
    $row = Database::fetch_array($res);
    $orig_start = api_strtotime(api_get_local_time($row['sd']));
    $orig_end = api_strtotime(api_get_local_time($row['ed']));
    $diff = $orig_end - $orig_start;
    $orig_title = $row['title'];
    $orig_content = $row['content'];
    $now = time();
    $type = Database::escape_string($type);
    $end = intval($end);
    if (1 <= $end && $end <= 500) {
        //we assume that, with this type of value, the user actually gives a count of repetitions
        //and that he wants us to calculate the end date with that (particularly in case of imports from ical)
        switch ($type) {
            case 'daily':
                $end = $orig_start + 86400 * $end;
                break;
            case 'weekly':
                $end = add_week($orig_start, $end);
                break;
            case 'monthlyByDate':
                $end = add_month($orig_start, $end);
                break;
            case 'monthlyByDay':
                //TODO
                break;
            case 'monthlyByDayR':
                //TODO
                break;
            case 'yearly':
                $end = add_year($orig_start, $end);
                break;
        }
    }
    if ($end > $now && in_array($type, array('daily', 'weekly', 'monthlyByDate', 'monthlyByDay', 'monthlyByDayR', 'yearly'))) {
        $sql = "INSERT INTO {$t_agenda_r} (c_id, cal_id, cal_type, cal_end) VALUES ({$course_id}, '{$orig_id}','{$type}',{$end})";
        $res = Database::query($sql);
        switch ($type) {
            case 'daily':
                for ($i = $orig_start + 86400; $i <= $end; $i += 86400) {
                    $res = agenda_add_item($course_info, $orig_title, $orig_content, date('Y-m-d H:i:s', $i), date('Y-m-d H:i:s', $i + $diff), $orig_dest, $orig_id, $file_comment);
                }
                break;
            case 'weekly':
                for ($i = $orig_start + 604800; $i <= $end; $i += 604800) {
                    $res = agenda_add_item($course_info, $orig_title, $orig_content, date('Y-m-d H:i:s', $i), date('Y-m-d H:i:s', $i + $diff), $orig_dest, $orig_id, $file_comment);
                }
                break;
            case 'monthlyByDate':
                $next_start = add_month($orig_start);
                while ($next_start <= $end) {
                    $res = agenda_add_item($course_info, $orig_title, $orig_content, date('Y-m-d H:i:s', $next_start), date('Y-m-d H:i:s', $next_start + $diff), $orig_dest, $orig_id, $file_comment);
                    $next_start = add_month($next_start);
                }
                break;
            case 'monthlyByDay':
                //not yet implemented
                break;
            case 'monthlyByDayR':
                //not yet implemented
                break;
            case 'yearly':
                $next_start = add_year($orig_start);
                while ($next_start <= $end) {
                    $res = agenda_add_item($course_info, $orig_title, $orig_content, date('Y-m-d H:i:s', $next_start), date('Y-m-d H:i:s', $next_start + $diff), $orig_dest, $orig_id, $file_comment);
                    $next_start = add_year($next_start);
                }
                break;
        }
    }
    return true;
}
        $statement->bindValue(':user', $user);
        $statement->execute();
        $name = $statement->fetch();
        $statement->closeCursor();
    } catch (Exception $e) {
    }
    return $name;
}
function add_month($EId, $description, $Money)
{
    global $db;
    try {
        $time = date('Y:m:d', time());
        $query = 'INSERT INTO month(EId, Mid, Descript, Money ) VALUES ("' . $EId['EId'] . '","' . $time . '","' . $description . '","' . $Money . '")';
        $db->exec($query);
    } catch (Exception $e) {
        return false;
    }
    return true;
}
if (isset($_POST['submit'])) {
    $EId = get_full($_POST['EId']);
    $description = array();
    $description[0] = $_POST['luongcoban'];
    $description[1] = $_POST['phucap'];
    $luongduan = implode('', $_POST['luongduan']);
    $description[2] = $luongduan;
    $import_description = implode('<br>', $description);
    $sum = $_POST['sum'];
    $add_month = add_month($EId, $import_description, $sum);
}