Jelajahi Sumber

WIP: Add career user results see BT#16240

Julio 5 tahun lalu
induk
melakukan
6387d7b878

+ 102 - 0
main/cron/import_csv.php

@@ -2643,6 +2643,108 @@ class ImportCsv
         }
     }
 
+    /**
+     * @param $file
+     * @param bool  $moveFile
+     * @param array $teacherBackup
+     * @param array $groupBackup
+     */
+    private function importCareersResults(
+        $file,
+        $moveFile = false,
+        &$teacherBackup = [],
+        &$groupBackup = []
+    ) {
+        $data = Import::csv_reader($file);
+
+        if (!empty($data)) {
+            $this->logger->addInfo(count($data).' records found.');
+            $values = [];
+            foreach ($data as $row) {
+                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;
+                }
+                $values[$row['Column']][] = $row;
+            }
+
+            $careerList = [];
+            $careerNameList = [];
+            ksort($values);
+            $careerChamiloIdList = [];
+            $extraFieldValue = new ExtraFieldValue('career');
+            $extraFieldName = $this->extraFieldIdNameList['career'];
+
+            // 1. First create all items
+            foreach ($values as $column => $rowList) {
+                foreach ($rowList as $row) {
+                    $studentId = $row['StudentId'];
+                    $studentInfo = api_get_user_info($studentId);
+                    if (empty($studentInfo)) {
+                        $this->logger->addInfo("Student id not found: $studentId");
+                        continue;
+                    }
+
+                    $careerId = $row['CareerId'];
+                    $item = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
+                        $extraFieldName,
+                        $careerId
+                    );
+
+                    $careerChamiloId = null;
+                    if (empty($item)) {
+                        $this->logger->addInfo("Career not found: $careerId");
+                        continue;
+                    } else {
+                        if (isset($item['item_id'])) {
+                            $careerChamiloId = $item['item_id'];
+                            /*$career = new Career();
+                            $career = $career->find($careerChamiloId);
+                            $chamiloCareerName = $career['name'];
+                            $careerNameList[$careerId] = $chamiloCareerName;
+                            $careerChamiloIdList[$careerId] = $careerChamiloId;*/
+                        } else {
+                            continue;
+                        }
+                    }
+
+                    if (UserManager::userHasCareer($studentId, $careerChamiloId) === false) {
+                        $this->logger->addInfo("User #$studentId has no career # $careerChamiloId");
+                        continue;
+                    }
+
+                    $userCareerData = UserManager::getUserCareer($studentId, $careerChamiloId);
+
+                    $extraData = isset($userCareerData['extra_data']) && !empty($userCareerData['extra_data']) ? unserialize($userCareerData['extra_data']) : [];
+
+                    $extraData[$row['CourseId']][] = [
+                        'Description' => $row['Description'],
+                        'Period' => $row['Period'],
+                        'TeacherText' => $row['TeacherText'],
+                        'TeacherUsername' => $row['TeacherUsername'],
+                        'ScoreText' => $row['ScoreText'],
+                        'ScoreValue' => $row['ScoreValue'],
+                        'Info' => $row['Info'],
+                        'BgColor' => $row['BgColor'],
+                        'Color' => $row['Color'],
+                        'BorderColor' => $row['BorderColor'],
+                        'Icon' => $row['Icon'],
+                        'IconColor' => $row['IconColor'],
+                    ];
+
+                    $serializedValue = serialize($extraData);
+
+                    UserManager::updateUserCareer($userCareerData['id'], $serializedValue);
+                }
+            }
+        }
+    }
+
     /**
      * @param $file
      * @param bool  $moveFile

+ 0 - 2
main/inc/lib/career.lib.php

@@ -492,7 +492,6 @@ class Career extends Model
             $subGroupLabel = isset($subGroupData[1]) ? $subGroupData[1] : '';
 
             if (!empty($subGroupId) && !in_array($subGroupId, $subGroups)) {
-                //$subGroups[$subGroupId][] = $vertex->getId();
                 $subGroups[$subGroupId]['items'][] = $vertex->getId();
                 $subGroups[$subGroupId]['label'] = $subGroupLabel;
             }
@@ -517,7 +516,6 @@ class Career extends Model
                 /** @var Vertex $vertex */
                 foreach ($subGroupList['items'] as $vertex) {
                     if ($vertex instanceof Vertex) {
-                        $rowId = $vertex->getId();
                         $groupCourseList[$vertex->getAttribute('Column')][] = $vertex->getId();
                         $connectionList = $vertex->getAttribute('Connections');
                         if (empty($connectionList)) {

+ 38 - 1
main/inc/lib/usermanager.lib.php

@@ -6572,7 +6572,7 @@ SQL;
         }
 
         if (self::userHasCareer($userId, $careerId) === false) {
-            $params = ['user_id' => $userId, 'career_id' => $careerId, 'created_at' => api_get_utc_datetime()];
+            $params = ['user_id' => $userId, 'career_id' => $careerId, 'created_at' => api_get_utc_datetime(), 'updated_at' => api_get_utc_datetime()];
             $table = Database::get_main_table(TABLE_MAIN_USER_CAREER);
             Database::insert($table, $params);
         }
@@ -6580,6 +6580,43 @@ SQL;
         return true;
     }
 
+    /**
+     * @param int   $userCareerId
+     * @param array $data
+     *
+     * @return bool
+     */
+    public static function updateUserCareer($userCareerId, $data)
+    {
+        if (!api_get_configuration_value('allow_career_users')) {
+            return false;
+        }
+
+        $params = ['extra_data' => $data, 'updated_at' => api_get_utc_datetime()];
+        $table = Database::get_main_table(TABLE_MAIN_USER_CAREER);
+        Database::update($table, $params, [   'where' => ['id = ?' => (int) $userCareerId], ]);
+
+        return true;
+    }
+
+    /**
+     * @param int $userId
+     * @param int $careerId
+     *
+     * @return array
+     */
+    public static function getUserCareer($userId, $careerId)
+    {
+        $userId = (int) $userId;
+        $careerId = (int) $careerId;
+        $table = Database::get_main_table(TABLE_MAIN_USER_CAREER);
+
+        $sql = "SELECT * FROM $table WHERE user_id = $userId AND career_id = $careerId";
+        $result = Database::query($sql);
+
+        return Database::fetch_array($result, 'ASSOC');
+    }
+
     /**
      * @param int $userId
      * @param int $careerId

+ 2 - 0
main/install/configuration.dist.php

@@ -1332,6 +1332,8 @@ requires extension "php-soap"  sudo apt-get install php-soap
 // $_configuration['ck_editor_enter_mode_value'] = 'CKEDITOR.ENTER_BR';
 
 // CREATE TABLE user_career (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, career_id INT NOT NULL, created_at DATETIME NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
+// ALTER TABLE user_career ADD COLUMN extra_data LONGTEXT;
+// ALTER TABLE user_career ADD COLUMN updated_at DATETIME NOT NULL;
 // $_configuration['allow_career_users'] = false;
 
 // LP view menu location. Options: "left" or "right"