Skip to content

Commit

Permalink
ImportCSV: Add remove result id BT#18958
Browse files Browse the repository at this point in the history
  • Loading branch information
jmontoyaa committed Jun 30, 2021
1 parent f758185 commit 53b8efc
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions main/cron/import_csv.php
Expand Up @@ -161,6 +161,10 @@ public function run()
$method = 'importCareersResults';
}

if ($method == 'importCareersresultsremoveStatic') {
$method = 'importCareersResultsRemoveStatic';
}

if ($method == 'importOpensessions') {
$method = 'importOpenSessions';
}
Expand Down Expand Up @@ -262,6 +266,7 @@ public function run()
'unsubsessionsextid-static',
'subsessionsextid-static',
'calendar-static',
'careersresultsremove-static'
];

foreach ($sections as $section) {
Expand Down Expand Up @@ -289,6 +294,7 @@ public function run()
}
}
}

$this->logger->addInfo('teacher backup');
$this->logger->addInfo(print_r($teacherBackup, 1));

Expand Down Expand Up @@ -2813,6 +2819,107 @@ private function importCareers(
}
}

/**
* @param $file
* @param bool $moveFile
* @param array $teacherBackup
* @param array $groupBackup
*/
private function importCareersResultsRemoveStatic(
$file,
$moveFile = false,
&$teacherBackup = [],
&$groupBackup = []
) {
$data = Import::csv_reader($file);

$careerIdList = [];
$userIdList = [];

if (!empty($data)) {
$totalCount = count($data);
$this->logger->addInfo($totalCount.' records found.');

$extraFieldValue = new ExtraFieldValue('career');
$extraFieldName = $this->extraFieldIdNameList['career'];
$rowCounter = 0;
foreach ($data as $row) {
$this->logger->addInfo("---------- Row: # $rowCounter");
$rowCounter++;
if (empty($row)) {
continue;
}

foreach ($row as $key => $value) {
$key = (string) trim($key);
// Remove utf8 bom
$key = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $key);
$row[$key] = $value;
}

$rowStudentId = $row['StudentId'];

if (isset($userIdList[$rowStudentId])) {
$studentId = $userIdList[$rowStudentId];
} else {
$studentId = UserManager::get_user_id_from_original_id(
$rowStudentId,
$this->extraFieldIdNameList['user']
);
$userIdList[$rowStudentId] = $studentId;
}

$careerId = $row['CareerId'];
if (isset($careerIdList[$careerId])) {
$careerChamiloId = $careerIdList[$careerId];
} else {
$item = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
$extraFieldName,
$careerId
);

if (empty($item)) {
$careerIdList[$careerId] = 0;
continue;
} else {
if (isset($item['item_id'])) {
$careerChamiloId = $item['item_id'];
$careerIdList[$careerId] = $careerChamiloId;
} else {
$careerIdList[$careerId] = 0;
continue;
}
}
}

if (empty($careerChamiloId)) {
$this->logger->addInfo("Career not found: $careerId ");
continue;
}

$userCareerData = UserManager::getUserCareer($studentId, $careerChamiloId);

if (empty($userCareerData)) {
$this->logger->addInfo(
"User chamilo id # $studentId (".$row['StudentId'].") has no career #$careerChamiloId (ext #$careerId)"
);
continue;
}

$extraData = isset($userCareerData['extra_data']) && !empty($userCareerData['extra_data']) ? unserialize($userCareerData['extra_data']) : [];
$extraData[$row['CourseId']][$row['ResultId']] = [];
$serializedValue = serialize($extraData);

UserManager::updateUserCareer($userCareerData['id'], $serializedValue);

$this->logger->addInfo('Deleting: result id'.$row['ResultId']);
$this->logger->addInfo(
"Saving graph for user chamilo # $studentId (".$row['StudentId'].") with career #$careerChamiloId (ext #$careerId)"
);
}
}
}

/**
* @param $file
* @param bool $moveFile
Expand Down

0 comments on commit 53b8efc

Please sign in to comment.