Ejemplo n.º 1
0
 /**
  * Update an extra field value for a given user
  * @param    integer   $userId User ID
  * @param    string    $variable Field variable name
  * @param    string    $value Field value
  *
  * @return    boolean    true if field updated, false otherwise
  */
 public static function update_extra_field_value($userId, $variable, $value = '')
 {
     $extraFieldValue = new ExtraFieldValue('user');
     $params = ['item_id' => $userId, 'variable' => $variable, 'value' => $value];
     $extraFieldValue->save($params);
 }
Ejemplo n.º 2
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);
     }
 }
Ejemplo n.º 3
0
 /**
  * Update an extra field value for a given course
  * @param    integer    Course ID
  * @param    string    Field variable name
  * @param    string    Field value
  * @return    boolean    true if field updated, false otherwise
  */
 public static function update_course_extra_field_value($course_code, $variable, $value = '')
 {
     $courseInfo = api_get_course_info($course_code);
     $courseId = $courseInfo['real_id'];
     $extraFieldValues = new ExtraFieldValue('course');
     $params = ['item_id' => $courseId, 'variable' => $variable, 'value' => $value];
     $extraFieldValues->save($params);
 }
 static function transaction_cambiar_generic($extra_field_variable, $data)
 {
     global $data_list;
     $uidIdPrograma = $data['item_id'];
     //$orig_id = $data['orig_id'];
     $destination_id = $data['dest_id'];
     $common_message = " - item_id:  {$uidIdPrograma}, dest_id: {$destination_id} -  looking for extra_field_variable: {$extra_field_variable} - with data " . print_r($data, 1);
     $session_id = self::get_session_id_by_programa_id($uidIdPrograma, $data_list);
     if (!empty($session_id)) {
         //??
         $extra_field = new ExtraField('session');
         $extra_field_info = $extra_field->get_handler_field_info_by_field_variable($extra_field_variable);
         //horario, aula, etc
         if (empty($extra_field_info)) {
             return array('message' => "Extra field {$extra_field_variable} doest not exists in the DB {$common_message}", 'status_id' => self::TRANSACTION_STATUS_FAILED);
         }
         //check if option exists
         $extra_field_option = new ExtraFieldOption('session');
         $extra_field_option_info = $extra_field_option->get_field_option_by_field_and_option($extra_field_info['id'], $destination_id);
         //horario, aula, etc
         if ($extra_field_option_info) {
             $extra_field_value = new ExtraFieldValue('session');
             //Getting info before
             $info_before = $extra_field_value->get_values_by_handler_and_field_id($session_id, $extra_field_info['id']);
             //Delete previous extra field value
             $extra_field_value->delete_values_by_handler_and_field_id($session_id, $extra_field_info['id']);
             $params = array('session_id' => $session_id, 'field_id' => $extra_field_info['id'], 'field_value' => $destination_id);
             $extra_field_value->save($params);
             //Getting info after
             $info_after = $extra_field_value->get_values_by_handler_and_field_id($session_id, $extra_field_info['id']);
             return array('entity' => $extra_field_variable, 'before' => $info_before, 'after' => $info_after, 'message' => "Extra field  {$extra_field_variable} saved with params: " . print_r($params, 1), 'status_id' => self::TRANSACTION_STATUS_SUCCESSFUL);
         } else {
             return array('message' => "Option does not exists dest_id: {$destination_id}  {$common_message}", 'status_id' => self::TRANSACTION_STATUS_FAILED);
         }
     } else {
         return array('message' => "Session does not exists: {$uidIdPrograma}   {$common_message}", 'status_id' => self::TRANSACTION_STATUS_FAILED);
     }
 }
Ejemplo n.º 5
0
 /**
  * Executes a fields match
  * @param array List of fields that must be matched ('fields_match' => array(0=>array('orig'=>'...','dest'=>'...',...)))
  * @param array Row of data
  * @param array Extra fields table definition
  */
 function execute_field_match($table, $row, $extra_fields = array())
 {
     //error_log('execute_field_match');
     $dest_row = array();
     $first_field = '';
     // If a dest table has been defined, fill $my_extra_fields with the
     //  extra_fields defined for that table
     $my_extra_fields = isset($table['dest_table']) && isset($extra_fields[$table['dest_table']]) ? $extra_fields[$table['dest_table']] : null;
     $extra_field_obj = null;
     $extra_field_value_obj = null;
     if (!empty($table['dest_table'])) {
         $extra_field_obj = new ExtraField($table['dest_table']);
         $extra_field_value_obj = new ExtraFieldValue($table['dest_table']);
     }
     $extra_fields_to_insert = array();
     global $data_list;
     // Fill the data list, if possible
     if (count($data_list['users']) < 1) {
         MigrationCustom::fill_data_list($data_list);
     }
     foreach ($table['fields_match'] as $id_field => $details) {
         //if ($table['dest_table'] == 'session') {error_log('Processing field '.$details['orig']);}
         $params = array();
         // Remove the table name prefix if any (in the orig field)
         if (isset($details['orig'])) {
             $field_exploded = explode('.', $details['orig']);
             if (isset($field_exploded[1])) {
                 $details['orig'] = $field_exploded[1];
             }
         }
         // process the fields one by one
         if ($details['func'] == 'none' || empty($details['func'])) {
             // if no function is defined to alter the field, take it as is
             $dest_data = $row[$details['orig']];
         } else {
             // if an alteration function is defined, run it on the field
             //error_log(__FILE__.' '.__LINE__.' Preparing to treat field with '.$details['func']);
             $dest_data = MigrationCustom::$details['func']($row[$details['orig']], $data_list, $row);
         }
         if (isset($dest_row[$details['dest']])) {
             $dest_row[$details['dest']] .= ' ' . $dest_data;
         } else {
             $dest_row[$details['dest']] = $dest_data;
         }
         //Extra field values
         $extra_field = isset($my_extra_fields) && isset($my_extra_fields[$details['dest']]) ? $my_extra_fields[$details['dest']] : null;
         // Check the array is there
         //if($table['dest_table'] == 'session') error_log('Extra field: '.print_r($extra_field,1));
         if (!empty($extra_field) && $extra_field_obj) {
             //if($table['dest_table'] == 'session') error_log('Extra_field no es vacío');
             // Check the "options" array is defined for this field (checking is_array is crucial here, see BT#5215)
             if (is_array($extra_field['options']) && count($extra_field['options']) > 0) {
                 //if($table['dest_table'] == 'session') error_log('...y sus opciones son: '.print_r($extra_field['options'],1));
                 //if($details['orig']=='uidIdPrograma') { error_log('Eso era lo inicial, del cual se tomó '.$details['dest'].': '.print_r($my_extra_fields,1));}
                 $options = $extra_field['options'];
                 $field_type = $extra_field['field_type'];
                 //if ($table['dest_table'] == 'session') {error_log('Field orig: '.$details['orig']);}
                 if (!empty($options)) {
                     //if ($table['dest_table'] == 'session') {error_log('Options not empty');}
                     if (!is_array($options)) {
                         $options = array($options);
                     }
                     foreach ($options as $option) {
                         if (is_array($option)) {
                             foreach ($option as $key => $value) {
                                 //error_log("$key $value --> {$dest_row[$details['dest']]} ");
                                 if ($key == 'option_value' && $value == $dest_row[$details['dest']]) {
                                     $value = $option['option_display_text'];
                                     if ($field_type == Extrafield::FIELD_TYPE_SELECT) {
                                         $value = $option['option_value'];
                                     }
                                     $params = array('field_id' => $option['field_id'], 'field_value' => $value);
                                     break 2;
                                 }
                             }
                         }
                     }
                 }
             } else {
                 $params = array('field_id' => $extra_field, 'field_value' => $dest_row[$details['dest']]);
             }
             if (!empty($params)) {
                 $extra_fields_to_insert[] = $params;
             }
             unset($dest_row[$details['dest']]);
         }
         unset($extra_field);
     }
     //if ($table['dest_table']=='session') { error_log('Params: '.print_r($params,1)); }
     // If a dest_func entry has been defind, use this entry as the main
     // operation to execute when inserting the item
     if (!empty($table['dest_func'])) {
         //error_log('Calling '.$table['dest_func'].' on data recovered: '.print_r($dest_row, 1));
         $dest_row['return_item_if_already_exists'] = true;
         $item_result = false;
         // Using call_user_func_array() has a serious impact on performance
         switch ($table['dest_func']) {
             case USER_FUNC_EXCEPTION_GRADEBOOK:
                 MigrationCustom::add_gradebook_result_with_evaluation($dest_row);
                 break;
             case USER_FUNC_EXCEPTION_ATTENDANCE:
                 MigrationCustom::create_attendance($dest_row);
                 break;
             default:
                 $item_result = call_user_func_array($table['dest_func'], array($dest_row, $data_list));
         }
         //After the function was executed fill the $data_list array
         switch ($table['dest_table']) {
             case 'course':
                 //Saving courses in array
                 if ($item_result) {
                     //$data_list['courses'][$dest_row['uidIdCurso']] = $item_result;
                 } else {
                     error_log('Course Not FOUND');
                     error_log(print_r($item_result, 1));
                     return false;
                 }
                 $handler_id = $item_result['code'];
                 break;
             case 'user':
                 if (!empty($item_result)) {
                     $handler_id = $item_result['user_id'];
                     //error_log($dest_row['email'].' '.$dest_row['uidIdPersona']);
                     if (isset($dest_row['uidIdAlumno'])) {
                         //$data_list['users_alumno'][$dest_row['uidIdAlumno']]['extra'] = $item_result;
                     }
                     if (isset($dest_row['uidIdEmpleado'])) {
                         //print_r($dest_row['uidIdEmpleado']);exit;
                         //$data_list['users_empleado'][$dest_row['uidIdEmpleado']]['extra'] = $item_result;
                     }
                 } else {
                     global $api_failureList;
                     error_log('Empty user details');
                     error_log(print_r($api_failureList, 1));
                 }
                 break;
             case 'session':
                 //$data_list['sessions'][$dest_row['uidIdPrograma']] = $item_result;
                 $handler_id = $item_result;
                 //session_id
                 break;
         }
         //Saving extra fields of the element
         //error_log('Checking extra fields for '.$extra_field_value_obj->handler_id.' '.$handler_id);
         if (!empty($extra_fields_to_insert)) {
             foreach ($extra_fields_to_insert as $params) {
                 //error_log('Trying to save '.print_r($params,1));
                 $params[$extra_field_value_obj->handler_id] = $handler_id;
                 $extra_field_value_obj->save($params);
             }
         }
     } else {
         // $this->errors_stack[] = "No destination data dest_func found. Abandoning data with first field $first_field = " . $dest_row[$first_field];
     }
     unset($extra_fields_to_insert);
     //remove to free up memory
     return $dest_row;
 }
 /**
  * Register the Registration ID (token) obtained from Google Cloud Messaging for a user
  * @param Chamilo\UserBundle\Entity\User $user The user
  * @param string $registrationId The token registration id from Google Cloud Messaging
  * @return int The id after insert or the number of affected rows after update. Otherwhise return false
  */
 public static function setGcmRegistrationId(Chamilo\UserBundle\Entity\User $user, $registrationId)
 {
     $registrationId = Security::remove_XSS($registrationId);
     $extraFieldValue = new ExtraFieldValue('user');
     return $extraFieldValue->save(['variable' => self::EXTRA_FIELD_GCM_REGISTRATION, 'value' => $registrationId, 'item_id' => $user->getId()]);
 }
Ejemplo n.º 7
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);
     }
 }