Beispiel #1
0
 /**
  * @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);
     }
 }
Beispiel #2
0
 /**
  * Get the course id based on the original id and field name in the extra fields.
  * Returns 0 if course was not found
  *
  * @param string $value Original course code
  * @param string $variable Original field name
  * @return int Course id
  */
 public static function getCourseInfoFromOriginalId($value, $variable)
 {
     $extraFieldValue = new ExtraFieldValue('course');
     $result = $extraFieldValue->get_item_id_from_field_variable_and_field_value($variable, $value);
     if (!empty($result)) {
         $courseInfo = api_get_course_info_by_id($result['item_id']);
         return $courseInfo;
     }
     return 0;
 }
Beispiel #3
0
 /**
  * Get the course id based on the original id and field name in the extra fields.
  * Returns 0 if course was not found
  *
  * @param string $original_course_id_value Original course id
  * @param string $original_course_id_name Original field name
  * @return int Course id
  */
 public static function get_course_id_from_original_id($original_course_id_value, $original_course_id_name)
 {
     $extraFieldValue = new ExtraFieldValue('course');
     $value = $extraFieldValue->get_item_id_from_field_variable_and_field_value($original_course_id_name, $original_course_id_value);
     if ($value) {
         return $value['item_id'];
     }
     return 0;
 }
 /**
  * Get the session id based on the original id and field name in the extra fields.
  * Returns 0 if session was not found
  *
  * @param string $value Original session id
  * @param string $variable Original field name
  * @return int Session id
  */
 public static function getSessionIdFromOriginalId($value, $variable)
 {
     $extraFieldValue = new ExtraFieldValue('session');
     $result = $extraFieldValue->get_item_id_from_field_variable_and_field_value($variable, $value);
     if (!empty($result)) {
         return $result['item_id'];
     }
     return 0;
 }
 /**
  * Assigns a user to a session based on rules in db_matches.php
  */
 static function add_user_to_session($data, &$omigrate = null)
 {
     $session_id = null;
     $user_id = null;
     if (is_array($omigrate) && $omigrate['boost_sessions']) {
         if (isset($omigrate['sessions'][$data['uidIdPrograma']])) {
             $session_id = $omigrate['sessions'][$data['uidIdPrograma']];
         }
     }
     if (empty($session_id)) {
         $extra_field_value = new ExtraFieldValue('session');
         $result = $extra_field_value->get_item_id_from_field_variable_and_field_value('uidIdPrograma', $data['uidIdPrograma']);
         //error_log('data[uidIdPrograma] '.$data['uidIdPrograma'].' returned $result[session_id]: '.$result['session_id']);
         if ($result && $result['session_id']) {
             $session_id = $result['session_id'];
         }
     }
     if (is_array($omigrate) && $omigrate['boost_users']) {
         if (isset($omigrate['users'][$data['uidIdPersona']])) {
             $user_id = $omigrate['users'][$data['uidIdPersona']];
         }
     }
     if (empty($user_id)) {
         $extra_field_value = new ExtraFieldValue('user');
         $result = $extra_field_value->get_item_id_from_field_variable_and_field_value('uidIdPersona', $data['uidIdPersona']);
         //error_log('data[uidIdPersona] '.$data['uidIdPersona'].' returned $result[user_id]: '.$result['user_id']);
         if ($result && $result['user_id']) {
             $user_id = $result['user_id'];
         }
     }
     if (!empty($session_id) && !empty($user_id)) {
         //error_log('Called: add_user_to_session - Subscribing: session_id: '.$session_id. '  user_id: '.$user_id);
         SessionManager::suscribe_users_to_session($session_id, array($user_id), SESSION_VISIBLE_READ_ONLY, false, false);
         //exit;
     } else {
         //error_log('Called: add_user_to_session - No idPrograma: '.$data['uidIdPrograma'].' - No uidIdPersona: '.$data['uidIdPersona']);
     }
 }
Beispiel #6
0
 /**
  * @param string $file
  * @param bool   $moveFile
  *
  * @return int
  */
 private function importCalendarStatic($file, $moveFile = true)
 {
     $data = Import::csvToArray($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::getSessionIdFromOriginalId($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['real_id']);
                 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']);
             $agenda->setIsAllowedToEdit(true);
             $eventComment = $event['comment'];
             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->addEvent($event['start'], $event['end'], false, $event['title'], $content, array('everyone'), false, null, array(), null, $eventComment);
             if (!empty($eventId)) {
                 $extraFieldValue->save(array('value' => $externalEventId, 'field_id' => $extraFieldInfo['id'], 'item_id' => $eventId));
                 $this->logger->addInfo("Event added: #{$eventId}");
             } else {
                 $this->logger->addInfo("Error while creating event.");
             }
         }
     }
     if ($moveFile) {
         $this->moveFile($file);
     }
 }