/** * @param string $file * @param bool $moveFile * * @return int */ private function importCalendarStatic($file, $moveFile = true) { $data = Import::csv_to_array($file); if (!empty($data)) { $this->logger->addInfo(count($data) . " records found."); $eventsToCreate = array(); $errorFound = false; foreach ($data as $row) { $sessionId = null; $externalSessionId = null; if (isset($row['external_sessionID'])) { $externalSessionId = $row['external_sessionID']; $sessionId = SessionManager::get_session_id_from_original_id($externalSessionId, $this->extraFieldIdNameList['session']); } $courseCode = null; if (isset($row['coursecode'])) { $courseCode = $row['coursecode']; } $courseInfo = api_get_course_info($courseCode); if (empty($courseInfo)) { $this->logger->addInfo("Course '{$courseCode}' does not exists"); } if (empty($sessionId)) { $this->logger->addInfo("external_sessionID: " . $externalSessionId . " does not exists."); } $teacherId = null; if (!empty($sessionId) && !empty($courseInfo)) { $courseIncluded = SessionManager::relation_session_course_exist($sessionId, $courseInfo['code']); if ($courseIncluded == false) { $this->logger->addInfo("Course '{$courseCode}' is not included in session: {$sessionId}"); $errorFound = true; } else { $teachers = CourseManager::get_coach_list_from_course_code($courseInfo['code'], $sessionId); // Getting first teacher. if (!empty($teachers)) { $teacher = current($teachers); $teacherId = $teacher['user_id']; } else { $sessionInfo = api_get_session_info($sessionId); $teacherId = $sessionInfo['id_coach']; } } } else { $errorFound = true; } if (empty($teacherId)) { $errorFound = true; $this->logger->addInfo("No teacher found in course code : '{$courseCode}' and session: '{$sessionId}'"); } $date = $row['date']; $startTime = $row['time_start']; $endTime = $row['time_end']; $title = $row['title']; $comment = $row['comment']; $color = isset($row['color']) ? $row['color'] : ''; $startDateYear = substr($date, 0, 4); $startDateMonth = substr($date, 4, 2); $startDateDay = substr($date, 6, 8); $startDate = $startDateYear . '-' . $startDateMonth . '-' . $startDateDay . ' ' . $startTime . ":00"; $endDate = $startDateYear . '-' . $startDateMonth . '-' . $startDateDay . ' ' . $endTime . ":00"; if (!api_is_valid_date($startDate) || !api_is_valid_date($endDate)) { $this->logger->addInfo("Verify your dates: '{$startDate}' : '{$endDate}' "); $errorFound = true; } // If old events do nothing. /*if (api_strtotime($startDate) < time()) { continue; }*/ if ($errorFound == false) { $eventsToCreate[] = array('start' => $startDate, 'end' => $endDate, 'title' => $title, 'sender_id' => $teacherId, 'course_id' => $courseInfo['real_id'], 'session_id' => $sessionId, 'comment' => $comment, 'color' => $color, $this->extraFieldIdNameList['calendar_event'] => $row['external_calendar_itemID']); } } if (empty($eventsToCreate)) { $this->logger->addInfo("No events to add"); return 0; } $this->logger->addInfo("Ready to insert events"); $agenda = new Agenda(); $extraFieldValue = new ExtraFieldValue('calendar_event'); $extraFieldName = $this->extraFieldIdNameList['calendar_event']; $externalEventId = null; $extraField = new ExtraField('calendar_event'); $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable($extraFieldName); if (empty($extraFieldInfo)) { $this->logger->addInfo("No calendar event extra field created: {$extraFieldName}"); return 0; } foreach ($eventsToCreate as $event) { $update = false; $item = null; if (!isset($event[$extraFieldName])) { $this->logger->addInfo("No external_calendar_itemID found. Skipping ..."); continue; } else { $externalEventId = $event[$extraFieldName]; if (empty($externalEventId)) { $this->logger->addInfo("external_calendar_itemID was set but empty. Skipping ..."); continue; } $items = $extraFieldValue->get_item_id_from_field_variable_and_field_value($extraFieldName, $externalEventId, false, false, true); $item = null; foreach ($items as $tempItem) { if ($tempItem['c_id'] == $event['course_id']) { $item = $tempItem; } } if (!empty($item)) { $this->logger->addInfo("Event #{$externalEventId} was already added. Updating ..."); $update = true; //continue; } } $courseInfo = api_get_course_info_by_id($event['course_id']); $agenda->set_course($courseInfo); $agenda->setType('course'); $agenda->setSessionId($event['session_id']); $agenda->setSenderId($event['sender_id']); $agenda->setIsAllowedToEdit(true); $eventComment = $event['comment']; $color = $event['color']; // To use the event comment you need // ALTER TABLE c_calendar_event ADD COLUMN comment TEXT; // add in configuration.php allow_agenda_event_comment = true if (empty($courseInfo)) { $this->logger->addInfo("No course found for added: #" . $event['course_id'] . " Skipping ..."); continue; } if (empty($event['sender_id'])) { $this->logger->addInfo("No sender found: #" . $event['sender_id'] . " Skipping ..."); continue; } $content = ''; if ($update && isset($item['calendar_event_id'])) { //the event already exists, just update $eventId = $agenda->edit_event($item['calendar_event_id'], $event['start'], $event['end'], false, $event['title'], $content, array('everyone'), array(), null, $eventComment, $color); if ($eventId !== false) { $this->logger->addInfo("Event updated: #{$eventId}"); } else { $this->logger->addInfo("Error while updating event."); } } else { // New event. Create it. $eventId = $agenda->add_event($event['start'], $event['end'], false, $event['title'], $content, array('everyone'), false, null, array(), null, $eventComment, $color); if (!empty($eventId)) { $extraFieldValue->is_course_model = true; $extraFieldValue->save(array('field_value' => $externalEventId, 'field_id' => $extraFieldInfo['id'], 'calendar_event_id' => $eventId, 'c_id' => $event['course_id'])); $this->logger->addInfo("Event added: #{$eventId}"); } else { $this->logger->addInfo("Error while creating event."); } } } } if ($moveFile) { $this->moveFile($file); } }
/** * @param int $workId * @param array $params * @param array $courseInfo * @param int $groupId */ function updatePublicationAssignment($workId, $params, $courseInfo, $groupId) { $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT); $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION); $workId = intval($workId); $time = time(); $course_id = $courseInfo['real_id']; // Insert into agenda $agendaId = 0; if (isset($params['add_to_calendar']) && $params['add_to_calendar'] == 1) { require_once api_get_path(SYS_CODE_PATH).'calendar/agenda.inc.php'; require_once api_get_path(SYS_CODE_PATH).'resourcelinker/resourcelinker.inc.php'; // Setting today date $date = $end_date = $time; if (!empty($params['enableExpiryDate'])) { $end_date = $params['expires_on']; $date = $end_date; } $title = sprintf(get_lang('HandingOverOfTaskX'), $params['new_dir']); $description = isset($params['description']) ? $params['description'] : ''; $content = '<a href="'.api_get_path(WEB_CODE_PATH).'work/work_list.php?'.api_get_cidreq().'&id='.$workId.'">' .$params['new_dir'].'</a>'.$description; $agendaId = agendaExistsForWork($workId, $courseInfo); // Add/edit agenda $agenda = new Agenda(); $agenda->set_course($courseInfo); $agenda->type = 'course'; if (empty($agendaId)) { $agendaId = $agenda->add_event( $date, $end_date, 'false', $title, $content, array('GROUP:'.$groupId) ); } else { $agenda->edit_event( $agendaId, $end_date, $end_date, 'false', $title, $content ); } } $qualification = isset($params['qualification']) && !empty($params['qualification']) ? 1 : 0; $expiryDate = (isset($params['enableExpiryDate']) && $params['enableExpiryDate'] == 1) ? api_get_utc_datetime($params['expires_on']) : '0000-00-00 00:00:00'; $endDate = ((isset($params['enableEndDate']) && $params['enableEndDate']==1) ? api_get_utc_datetime($params['ends_on']) : '0000-00-00 00:00:00'); $data = get_work_assignment_by_id($workId, $course_id); if (empty($data)) { $sql = "INSERT INTO $table SET c_id = $course_id , expires_on = '".Database::escape_string($expiryDate)."', ends_on = '".Database::escape_string($endDate)."', add_to_calendar = $agendaId, enable_qualification = '$qualification', publication_id = '$workId'"; Database::query($sql); $my_last_id = Database::insert_id(); $sql = "UPDATE $workTable SET has_properties = $my_last_id , view_properties = 1 WHERE c_id = $course_id AND id = $workId"; Database::query($sql); } else { $sql = "UPDATE $table SET expires_on = '".$expiryDate."', ends_on = '".$endDate."', add_to_calendar = $agendaId, enable_qualification = '".$qualification."' WHERE publication_id = $workId AND c_id = $course_id AND id = ".$data['id']; Database::query($sql); } if (!empty($params['category_id'])) { require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/gradebookitem.class.php'; require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/evaluation.class.php'; require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/abstractlink.class.php'; require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php'; $link_info = is_resource_in_course_gradebook( $courseInfo['code'], LINK_STUDENTPUBLICATION, $workId, api_get_session_id() ); $linkId = null; if (!empty($link_info)) { $linkId = $link_info['id']; } if (isset($params['make_calification']) && $params['make_calification'] == 1) { if (empty($linkId)) { add_resource_to_course_gradebook( $params['category_id'], $courseInfo['code'], LINK_STUDENTPUBLICATION, $workId, $params['dir_name'], (float)$params['weight'], (float)$params['qualification'], $params['description'], 1, api_get_session_id() ); } else { update_resource_from_course_gradebook( $linkId, $courseInfo['code'], $params['weight'] ); } } else { // Delete everything of the gradebook for this $linkId remove_resource_from_course_gradebook($linkId); } } }
/** * Adds an announcement to the database * @param string Title of the announcement * @param string Content of the announcement * @param string Start date (YYYY-MM-DD HH:II: SS) * @param string End date (YYYY-MM-DD HH:II: SS) * @param int Whether the announcement should be visible to teachers (1) or not (0) * @param int Whether the announcement should be visible to students (1) or not (0) * @param int Whether the announcement should be visible to anonymous users (1) or not (0) * @param string The language for which the announvement should be shown. Leave null for all langages * @param int Whether to send an e-mail to all users (1) or not (0) * @return mixed insert_id on success, false on failure */ public static function add_announcement($title, $content, $date_start, $date_end, $visible_teacher = 0, $visible_student = 0, $visible_guest = 0, $lang = null, $send_mail = 0, $add_to_calendar = false) { $original_content = $content; $a_dateS = explode(' ', $date_start); $a_arraySD = explode('-', $a_dateS[0]); $a_arraySH = explode(':', $a_dateS[1]); $date_start_to_compare = array_merge($a_arraySD, $a_arraySH); $a_dateE = explode(' ', $date_end); $a_arrayED = explode('-', $a_dateE[0]); $a_arrayEH = explode(':', $a_dateE[1]); $date_end_to_compare = array_merge($a_arrayED, $a_arrayEH); $db_table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); if (!checkdate($date_start_to_compare[1], $date_start_to_compare[2], $date_start_to_compare[0])) { Display::display_normal_message(get_lang('InvalidStartDate')); return false; } if (($date_end_to_compare[1] || $date_end_to_compare[2] || $date_end_to_compare[0]) && !checkdate($date_end_to_compare[1], $date_end_to_compare[2], $date_end_to_compare[0])) { Display::display_normal_message(get_lang('InvalidEndDate')); return false; } if (strlen(trim($title)) == 0) { Display::display_normal_message(get_lang('InvalidTitle')); return false; } $start = api_get_utc_datetime($date_start); $end = api_get_utc_datetime($date_end); $title = Database::escape_string($title); $content = Database::escape_string($content); //Fixing urls that are sent by email $content = str_replace('src=\\"/home/', 'src=\\"' . api_get_path(WEB_PATH) . 'home/', $content); $content = str_replace('file=/home/', 'file=' . api_get_path(WEB_PATH) . 'home/', $content); $langsql = is_null($lang) ? 'NULL' : "'" . Database::escape_string($lang) . "'"; global $_configuration; $current_access_url_id = 1; if ($_configuration['multiple_access_urls']) { $current_access_url_id = api_get_current_access_url_id(); } $sql = "INSERT INTO " . $db_table . " (title,content,date_start,date_end,visible_teacher,visible_student,visible_guest, lang, access_url_id)\n\t\t\t\tVALUES ('" . $title . "','" . $content . "','" . $start . "','" . $end . "','" . $visible_teacher . "','" . $visible_student . "','" . $visible_guest . "'," . $langsql . ", " . $current_access_url_id . ")"; if ($send_mail == 1) { SystemAnnouncementManager::send_system_announcement_by_email($title, $content, $visible_teacher, $visible_student, $lang); } $res = Database::query($sql); if ($res === false) { Debug::log_s(mysql_error()); return false; } if ($add_to_calendar) { $agenda = new Agenda(); $agenda->setType('admin'); $agenda->add_event($date_start, $date_end, false, null, $title, $original_content); } return Database::insert_id(); }
$agenda = new Agenda(); $agenda->setType('course'); $repeatSettings = array(); if (!empty($_POST['repeat'])) { $repeatSettings = array('repeat_type' => $_POST['repeat_type'], 'repeat_end' => $repeatEndDay); /* $res = agenda_add_repeat_item( $course_info, $id, $_POST['repeat_type'], $repeatEndDay, $_POST['users'], $safe_file_comment );*/ } $id = $agenda->add_event($startDate, $endDate, null, null, $_POST['title'], $_POST['content'], $_POST['users'], false, null, array('comment' => $fileComment, 'file' => $fileAttachment), $repeatSettings); Display::display_confirmation_message(get_lang('AddSuccess')); } else { show_add_form(); } break; case "announce": //copying the agenda item into an announcement if (!(api_is_course_coach() && !api_is_element_in_the_session(TOOL_AGENDA, $event_id))) { // a coach can only delete an element belonging to his session $ann_id = store_agenda_item_as_announcement($event_id); $tool_group_link = isset($_SESSION['toolgroup']) ? '&toolgroup=' . $_SESSION['toolgroup'] : ''; Display::display_normal_message(get_lang('CopiedAsAnnouncement') . ' <a href="../announcements/announcements.php?id=' . $ann_id . $tool_group_link . '">' . get_lang('NewAnnouncement') . '</a>', false); } break; case 'importical':
if ($teacher) { switch ($action) { case 'add_to_calendar': $course_info = api_get_course_info(); $agenda = new Agenda(); $agenda->type = 'course'; $id = intval($_GET['id']); $title = sprintf(get_lang('VideoConferenceXCourseX'), $id, $course_info['name']); $content = Display::url(get_lang('GoToTheVideoConference'), $_GET['url']); $event_id = $agenda->add_event( $_REQUEST['start'], null, 'true', $title, $content, array('everyone') ); if (!empty($event_id)) { $message = Display::return_message(get_lang('VideoConferenceAddedToTheCalendar'), 'success'); } else { $message = Display::return_message(get_lang('Error'), 'error'); } break; case 'copy_record_to_link_tool': $result = $bbb->copyRecordToLinkTool($_GET['id']); if ($result) { $message = Display::return_message(get_lang('VideoConferenceAddedToTheLinkTool'), 'success'); } else { $message = Display::return_message(get_lang('Error'), 'error');
if (api_is_allowed_to_edit(false, true) or api_get_course_setting('allow_user_edit_agenda') && !api_is_anonymous() && api_is_allowed_to_session_edit(false, true) or GroupManager::user_has_access(api_get_user_id(), $group_id, GroupManager::GROUP_TOOL_CALENDAR) && GroupManager::is_tutor_of_group(api_get_user_id(), $group_id)) { switch ($action) { case 'add': $actionName = get_lang('Add'); $form = $agenda->getForm(array('action' => 'add')); if ($form->validate()) { $values = $form->getSubmitValues(); $sendEmail = isset($values['add_announcement']) ? true : false; $allDay = isset($values['all_day']) ? 'true' : 'false'; $sendAttachment = isset($_FILES['user_upload']) ? true : false; $attachment = $sendAttachment ? $_FILES['user_upload'] : null; $attachmentComment = isset($values['file_comment']) ? $values['file_comment'] : null; $comment = isset($values['comment']) ? $values['comment'] : null; $startDate = $values['date_range_start']; $endDate = $values['date_range_end']; $eventId = $agenda->add_event($startDate, $endDate, $allDay, $values['title'], $values['content'], $values['users_to_send'], $sendEmail, null, $attachment, $attachmentComment, $comment); if (!empty($values['repeat']) && !empty($eventId)) { // End date is always set as 23:59:59 $endDate = substr($values['repeat_end_day'], 0, 10) . ' 23:59:59'; $agenda->addRepeatedItem($eventId, $values['repeat_type'], $endDate, $values['users_to_send']); } $message = Display::return_message(get_lang('AddSuccess'), 'confirmation'); if ($sendEmail) { $message .= Display::return_message(get_lang('AdditionalMailWasSentToSelectedUsers'), 'confirmation'); } Session::write('message', $message); header("Location: {$agendaUrl}"); exit; } else { $content = $form->return_form(); }
api_protect_course_script(true); } $group_id = api_get_group_id(); $user_id = api_get_user_id(); $is_group_tutor = GroupManager::is_tutor_of_group($user_id, $group_id); $agenda = new Agenda(); $agenda->setType($type); //course,admin or personal switch ($action) { case 'add_event': if (!api_is_allowed_to_edit(null, true) && !$is_group_tutor && $type == 'course') { break; } $add_as_announcement = isset($_REQUEST['add_as_annonuncement']) ? $_REQUEST['add_as_annonuncement'] : null; $usersToSend = isset($_REQUEST['users_to_send']) ? $_REQUEST['users_to_send'] : null; echo $agenda->add_event($_REQUEST['start'], $_REQUEST['end'], $_REQUEST['all_day'], $_REQUEST['view'], $_REQUEST['title'], $_REQUEST['content'], $usersToSend, $add_as_announcement); break; case 'edit_event': if (!api_is_allowed_to_edit(null, true) && $type == 'course') { break; } $id_list = explode('_', $_REQUEST['id']); $id = $id_list[1]; $agenda->edit_event($id, $_REQUEST['start'], $_REQUEST['end'], $_REQUEST['all_day'], $_REQUEST['view'], $_REQUEST['title'], $_REQUEST['content']); break; case 'delete_event': if (!api_is_allowed_to_edit(null, true) && $type == 'course') { break; } $id_list = explode('_', $_REQUEST['id']); $id = $id_list[1];
/** * Import an iCal file into the database * @param array Course info * @return boolean True on success, false otherwise * @deprecated */ function agenda_import_ical($course_info, $file) { require_once api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php'; $charset = api_get_system_encoding(); $filepath = api_get_path(SYS_ARCHIVE_PATH) . $file['name']; if (!@move_uploaded_file($file['tmp_name'], $filepath)) { error_log('Problem moving uploaded file: ' . $file['error'] . ' in ' . __FILE__ . ' line ' . __LINE__); return false; } require_once api_get_path(LIBRARY_PATH) . 'icalcreator/iCalcreator.class.php'; $ical = new vcalendar(); $ical->setConfig('directory', dirname($filepath)); $ical->setConfig('filename', basename($filepath)); $return = $ical->parse(); //we need to recover: summary, description, dtstart, dtend, organizer, attendee, location (=course name), /* $ve = $ical->getComponent(VEVENT); $ttitle = $ve->getProperty('summary'); $title = api_convert_encoding($ttitle,$charset,'UTF-8'); $tdesc = $ve->getProperty('description'); $desc = api_convert_encoding($tdesc,$charset,'UTF-8'); $start_date = $ve->getProperty('dtstart'); $start_date_string = $start_date['year'].'-'.$start_date['month'].'-'.$start_date['day'].' '.$start_date['hour'].':'.$start_date['min'].':'.$start_date['sec']; $ts = $ve->getProperty('dtend'); if ($ts) { $end_date_string = $ts['year'].'-'.$ts['month'].'-'.$ts['day'].' '.$ts['hour'].':'.$ts['min'].':'.$ts['sec']; } else { //Check duration if dtend does not exist $duration = $ve->getProperty('duration'); if ($duration) { $duration = $ve->getProperty('duration'); $duration_string = $duration['year'].'-'.$duration['month'].'-'.$duration['day'].' '.$duration['hour'].':'.$duration['min'].':'.$duration['sec']; $start_date_tms = mktime(intval($start_date['hour']), intval($start_date['min']), intval($start_date['sec']), intval($start_date['month']), intval($start_date['day']), intval($start_date['year'])); //$start_date_tms = mktime(($start_date['hour']), ($start_date['min']), ($start_date['sec']), ($start_date['month']), ($start_date['day']), ($start_date['year'])); //echo date('d-m-Y - h:i:s', $start_date_tms); $end_date_string = mktime(intval($start_date['hour']) +$duration['hour'], intval($start_date['min']) + $duration['min'], intval($start_date['sec']) + $duration['sec'], intval($start_date['month']) + $duration['month'], intval($start_date['day'])+$duration['day'], intval($start_date['year']) + $duration['year']); $end_date_string = date('Y-m-d H:i:s', $end_date_string); //echo date('d-m-Y - h:i:s', $end_date_string); } } //echo $start_date.' - '.$end_date; $organizer = $ve->getProperty('organizer'); $attendee = $ve->getProperty('attendee'); $course_name = $ve->getProperty('location'); //insert the event in our database $id = agenda_add_item($course_info,$title,$desc,$start_date_string,$end_date_string,$_POST['selectedform']); $repeat = $ve->getProperty('rrule'); if(is_array($repeat) && !empty($repeat['FREQ'])) { $trans = array('DAILY'=>'daily','WEEKLY'=>'weekly','MONTHLY'=>'monthlyByDate','YEARLY'=>'yearly'); $freq = $trans[$repeat['FREQ']]; $interval = $repeat['INTERVAL']; if(isset($repeat['UNTIL']) && is_array($repeat['UNTIL'])) { $until = mktime(23,59,59,$repeat['UNTIL']['month'],$repeat['UNTIL']['day'],$repeat['UNTIL']['year']); $res = agenda_add_repeat_item($course_info,$id,$freq,$until,$_POST['selectedform']); } */ $eventcount = 0; $message = array(); $agenda_obj = new Agenda(); while (true) { //we need to recover: summary, description, dtstart, dtend, organizer, attendee, location (=course name) $ve = $ical->getComponent('VEVENT', $eventcount); if (!$ve) { break; } $ttitle = $ve->getProperty('summary'); $title = api_convert_encoding($ttitle, $charset, 'UTF-8'); $tdesc = $ve->getProperty('description'); $desc = api_convert_encoding($tdesc, $charset, 'UTF-8'); $start_date = $ve->getProperty('dtstart', false, true); if (isset($start_date['params']['VALUE'])) { $start_date_value = $start_date['value']; if ($start_date['params']['VALUE'] == 'DATE') { $start_date_string = $start_date_value['year'] . '-' . $start_date_value['month'] . '-' . $start_date_value['day'] . ''; } else { $start_date_string = $start_date_value['year'] . '-' . $start_date_value['month'] . '-' . $start_date_value['day'] . ' ' . $start_date_value['hour'] . ':' . $start_date_value['min'] . ':' . $start_date_value['sec']; } } else { continue; } $ts = $ve->getProperty('dtend'); if ($ts) { $end_date = $ve->getProperty('dtend', false, true); if (isset($end_date['params']['VALUE'])) { $end_date_value = $end_date['value']; if ($end_date['params']['VALUE'] == 'DATE') { $end_date_string = $end_date_value['year'] . '-' . $end_date_value['month'] . '-' . $end_date_value['day'] . ''; } else { $end_date_string = $end_date_value['year'] . '-' . $end_date_value['month'] . '-' . $end_date_value['day'] . ' ' . $end_date_value['hour'] . ':' . $end_date_value['min'] . ':' . $end_date_value['sec']; } } else { //Default behaviour $end_date_string = $ts['year'] . '-' . $ts['month'] . '-' . $ts['day'] . ' ' . $ts['hour'] . ':' . $ts['min'] . ':' . $ts['sec']; } } else { //Check duration if dtend does not exist $duration = $ve->getProperty('duration'); if ($duration) { $duration = $ve->getProperty('duration'); $duration_string = $duration['year'] . '-' . $duration['month'] . '-' . $duration['day'] . ' ' . $duration['hour'] . ':' . $duration['min'] . ':' . $duration['sec']; $start_date_tms = mktime(intval($start_date['hour']), intval($start_date['min']), intval($start_date['sec']), intval($start_date['month']), intval($start_date['day']), intval($start_date['year'])); //$start_date_tms = mktime(($start_date['hour']), ($start_date['min']), ($start_date['sec']), ($start_date['month']), ($start_date['day']), ($start_date['year'])); //echo date('d-m-Y - h:i:s', $start_date_tms); $end_date_string = mktime(intval($start_date['hour']) + $duration['hour'], intval($start_date['min']) + $duration['min'], intval($start_date['sec']) + $duration['sec'], intval($start_date['month']) + $duration['month'], intval($start_date['day']) + $duration['day'], intval($start_date['year']) + $duration['year']); $end_date_string = api_get_utc_datetime($end_date_string); //echo date('d-m-Y - h:i:s', $end_date_string); } } //echo $start_date.' - '.$end_date; $organizer = $ve->getProperty('organizer'); $attendee = $ve->getProperty('attendee'); $course_name = $ve->getProperty('location'); //insert the event in our database $agenda_obj->type = 'course'; $all_day = 'false'; if ($start_date_string == $end_date_string) { $all_day = 'true'; } $date = new DateTime($start_date_string); $date->add(new DateInterval('P1D')); if ($start_date_string == $date->format('Y-m-d h:i:s')) { $all_day = 'true'; } $id = $agenda_obj->add_event($start_date_string, $end_date_string, $all_day, $title, $desc, array('everyone')); $message[] = " {$title} - " . $start_date_string . " - " . $end_date_string; $repeat = $ve->getProperty('rrule'); if (is_array($repeat) && !empty($repeat['FREQ'])) { $trans = array('DAILY' => 'daily', 'WEEKLY' => 'weekly', 'MONTHLY' => 'monthlyByDate', 'YEARLY' => 'yearly'); $freq = $trans[$repeat['FREQ']]; $interval = $repeat['INTERVAL']; if (isset($repeat['UNTIL']) && is_array($repeat['UNTIL'])) { $until = mktime(23, 59, 59, $repeat['UNTIL']['month'], $repeat['UNTIL']['day'], $repeat['UNTIL']['year']); $res = agenda_add_repeat_item($course_info, $id, $freq, $until, $attendee); } //TODO: deal with count if (!empty($repeat['COUNT'])) { $count = $repeat['COUNT']; $res = agenda_add_repeat_item($course_info, $id, $freq, $count, $attendee); } } $eventcount++; } if (!empty($message)) { $message = implode('<br /> ', $message); } return $message; }
$group_id = api_get_group_id(); if ($type == 'course') { api_protect_course_script(true); } $group_id = api_get_group_id(); $is_group_tutor = GroupManager::is_tutor_of_group(api_get_user_id(), $group_id); $agenda = new Agenda(); $agenda->type = $type; switch ($action) { case 'add_event': if (!api_is_allowed_to_edit(null, true) && !$is_group_tutor && $type == 'course') { break; } $add_as_announcement = isset($_REQUEST['add_as_annonuncement']) ? $_REQUEST['add_as_annonuncement'] : null; $comment = isset($_REQUEST['comment']) ? $_REQUEST['comment'] : null; echo $agenda->add_event($_REQUEST['start'], $_REQUEST['end'], $_REQUEST['all_day'], $_REQUEST['title'], $_REQUEST['content'], $_REQUEST['users_to_send'], $add_as_announcement, null, array(), null, $comment); break; case 'edit_event': if (!api_is_allowed_to_edit(null, true) && $type == 'course') { break; } $id_list = explode('_', $_REQUEST['id']); $id = $id_list[1]; $agenda->edit_event($id, $_REQUEST['start'], $_REQUEST['end'], $_REQUEST['all_day'], $_REQUEST['title'], $_REQUEST['content']); break; case 'delete_event': if (!api_is_allowed_to_edit(null, true) && $type == 'course') { break; } $id_list = explode('_', $_REQUEST['id']); $id = $id_list[1];
/** * @param string $file * @param bool $moveFile * * @return int */ private function importCalendarStatic($file, $moveFile = true) { $data = Import::csv_to_array($file); if ($this->getDumpValues()) { // Remove all calendar items $truncateTables = array(Database::get_course_table(TABLE_AGENDA), Database::get_course_table(TABLE_AGENDA_ATTACHMENT), Database::get_course_table(TABLE_AGENDA_REPEAT), Database::get_course_table(TABLE_AGENDA_REPEAT_NOT), Database::get_main_table(TABLE_PERSONAL_AGENDA), Database::get_main_table(TABLE_PERSONAL_AGENDA_REPEAT_NOT), Database::get_main_table(TABLE_PERSONAL_AGENDA_REPEAT)); foreach ($truncateTables as $table) { $sql = "TRUNCATE {$table}"; Database::query($sql); } $table = Database::get_course_table(TABLE_ITEM_PROPERTY); $sql = "DELETE FROM {$table} WHERE tool = 'calendar_event'"; Database::query($sql); } if (!empty($data)) { $this->logger->addInfo(count($data) . " records found."); $eventsToCreate = array(); $errorFound = false; foreach ($data as $row) { $sessionId = null; $externalSessionId = null; if (isset($row['external_sessionID'])) { $externalSessionId = $row['external_sessionID']; $sessionId = SessionManager::get_session_id_from_original_id($externalSessionId, $this->extraFieldIdNameList['session']); } $courseCode = null; if (isset($row['coursecode'])) { $courseCode = $row['coursecode']; } $courseInfo = api_get_course_info($courseCode); if (empty($courseInfo)) { $this->logger->addInfo("Course '{$courseCode}' does not exists"); } if (empty($sessionId)) { $this->logger->addInfo("external_sessionID: " . $externalSessionId . " does not exists."); } $teacherId = null; if (!empty($sessionId) && !empty($courseInfo)) { $courseIncluded = SessionManager::relation_session_course_exist($sessionId, $courseInfo['code']); if ($courseIncluded == false) { $this->logger->addInfo("Course '{$courseCode}' is not included in session: {$sessionId}"); $errorFound = true; } else { $teachers = CourseManager::get_coach_list_from_course_code($courseInfo['code'], $sessionId); // Getting first teacher. if (!empty($teachers)) { $teacher = current($teachers); $teacherId = $teacher['user_id']; } else { $sessionInfo = api_get_session_info($sessionId); $teacherId = $sessionInfo['id_coach']; } } } else { $errorFound = true; } if (empty($teacherId)) { $errorFound = true; $this->logger->addInfo("No teacher found in course code : '{$courseCode}' and session: '{$sessionId}'"); } $date = $row['date']; $startTime = $row['time_start']; $endTime = $row['time_end']; $title = $row['title']; $startDateYear = substr($date, 0, 4); $startDateMonth = substr($date, 4, 2); $startDateDay = substr($date, 6, 8); $startDate = $startDateYear . '-' . $startDateMonth . '-' . $startDateDay . ' ' . $startTime . ":00"; $endDate = $startDateYear . '-' . $startDateMonth . '-' . $startDateDay . ' ' . $endTime . ":00"; if (!api_is_valid_date($startDate) or !api_is_valid_date($endDate)) { $this->logger->addInfo("Verify your dates: '{$startDate}' : '{$endDate}' "); $errorFound = true; } if ($errorFound == false) { $eventsToCreate[] = array('start' => $startDate, 'end' => $endDate, 'title' => $title, 'sender_id' => $teacherId, 'course_id' => $courseInfo['real_id'], 'session_id' => $sessionId, $this->extraFieldIdNameList['calendar_event'] => $row['external_calendar_itemID']); } } if (empty($eventsToCreate)) { $this->logger->addInfo("No events to add"); return 0; } $this->logger->addInfo("Ready to insert events"); $content = null; $agenda = new Agenda(); $extraFieldValue = new ExtraFieldValue('calendar_event'); $extraFieldName = $this->extraFieldIdNameList['calendar_event']; $externalEventId = null; $extraField = new ExtraField('calendar_event'); $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable($extraFieldName); if (empty($extraFieldInfo)) { $this->logger->addInfo("No calendar event extra field created: {$extraFieldName}"); return 0; } foreach ($eventsToCreate as $event) { if (!isset($event[$extraFieldName])) { $this->logger->addInfo("No external_calendar_itemID found. Skipping ..."); continue; } else { $externalEventId = $event[$extraFieldName]; $item = $extraFieldValue->get_item_id_from_field_variable_and_field_value($extraFieldName, $externalEventId); if (!empty($item) || empty($externalEventId)) { $this->logger->addInfo("Event #{$externalEventId} was already added . Skipping ..."); continue; } } $courseInfo = api_get_course_info_by_id($event['course_id']); $agenda->set_course($courseInfo); $agenda->setType('course'); $agenda->setSessionId($event['session_id']); $agenda->setSenderId($event['sender_id']); $eventComment = $event['comment']; // To use the event comment you need // ALTER TABLE c_calendar_event ADD COLUMN comment TEXT; // add in configuration.php allow_agenda_event_comment = true if (empty($courseInfo)) { $this->logger->addInfo("No course found for added: #" . $event['course_id'] . " Skipping ..."); continue; } if (empty($event['sender_id'])) { $this->logger->addInfo("No sender found: #" . $event['sender_id'] . " Skipping ..."); continue; } $eventId = $agenda->add_event($event['start'], $event['end'], false, $event['title'], $content, array('everyone'), false, null, array(), null, $eventComment); if (!empty($eventId)) { $extraFieldValue->save(array('field_value' => $externalEventId, 'field_id' => $extraFieldInfo['id'], 'calendar_event_id' => $eventId)); $this->logger->addInfo("Event added: #{$eventId}"); } else { $this->logger->addInfo("Error while creating event."); } } } if ($moveFile) { $this->moveFile($file); } }