Exemplo n.º 1
0
 protected function importData($line, $type, $data)
 {
     //pr($line);exit;
     try {
         switch ($type) {
             case "users":
                 $newUser = EfrontUser::createUser($data);
                 $this->log["success"][] = _LINE . " {$line}: " . _IMPORTEDUSER . " " . $newUser->login;
                 break;
             case "users_to_courses":
                 //Check if a user exists and whether it has the same case
                 $userFound = false;
                 if (!in_array($data['users_login'], $this->allUserLogins)) {
                     //For case-insensitive matches
                     foreach ($this->allUserLogins as $login) {
                         if (mb_strtolower($data['users_login']) == mb_strtolower($login)) {
                             $data['users_login'] = $login;
                             $userFound = true;
                         }
                     }
                 } else {
                     $userFound = true;
                 }
                 if ($userFound) {
                     $courses_name = trim($data['course_name']);
                     $courses_ID = $this->getCourseByName($courses_name);
                     unset($data['course_name']);
                     if ($courses_ID) {
                         foreach ($courses_ID as $course_ID) {
                             $data['courses_ID'] = $course_ID;
                             $course = new EfrontCourse($course_ID);
                             if (is_null($this->course_users[$course_ID])) {
                                 $result = eF_getTableDataFlat("users_to_courses", "users_LOGIN", "courses_ID={$course_ID} AND archive=0");
                                 $this->course_users[$course_ID] = array_combine($result['users_LOGIN'], $result['users_LOGIN']);
                             }
                             if (isset($this->course_users[$course_ID][$data['users_login']])) {
                                 throw new Exception("User is already assigned to the course", self::USER_TO_COURSE_EXISTS);
                             }
                             //$course -> addUsers($data['users_login'], (isset($data['user_type']) && $data['user_type']?$data['user_type']:"student"));
                             $course->addUsers($data['users_login'], isset($data['user_type']) ? $data['user_type'] : "student");
                             $where = "users_login = '******'users_login'] . "' AND courses_ID = " . $data['courses_ID'];
                             $data['completed'] ? $data['completed'] = 1 : ($data['completed'] = 0);
                             EfrontCourse::persistCourseUsers($data, $where, $data['courses_ID'], $data['users_login']);
                             if ($data['active']) {
                                 $course->confirm($data['users_login']);
                             } else {
                                 $course->unconfirm($data['users_login']);
                             }
                             $this->course_users[$course_ID][$data['users_login']] = $data['users_login'];
                             $this->log["success"][] = _LINE . " {$line}: " . _NEWCOURSEASSIGNMENT . " " . $courses_name . " - " . $data['users_login'];
                         }
                     } else {
                         if ($courses_name != "") {
                             $course = EfrontCourse::createCourse(array("name" => $courses_name));
                             $this->log["success"][] = _LINE . " {$line}: " . _NEWCOURSE . " " . $courses_name;
                             $course->addUsers($data['users_login'], isset($data['user_type']) ? $data['user_type'] : "student");
                             $courses_ID = $course->course['id'];
                             $this->courseNamesToIds[$courses_name] = array($courses_ID);
                             $where = "users_login = '******'users_login'] . "' AND courses_ID = " . $courses_ID;
                             EfrontCourse::persistCourseUsers($data, $where, $courses_ID, $data['users_login']);
                             if ($data['active']) {
                                 $course->confirm($data['users_login']);
                             } else {
                                 $course->unconfirm($data['users_login']);
                             }
                             $this->log["success"][] = _LINE . " {$line}: " . _NEWCOURSEASSIGNMENT . " " . $courses_name . " - " . $data['users_login'];
                         } else {
                             $this->log["failure"][] = _LINE . " {$line}: " . _COULDNOTFINDCOURSE . " " . $courses_name;
                         }
                     }
                 } else {
                     $this->log["failure"][] = _LINE . " {$line}: " . _USERDOESNOTEXIST . ": " . $data['users_login'];
                 }
                 break;
             case "users_to_lessons":
                 //Check if a user exists and whether it has the same case
                 $userFound = false;
                 if (!in_array($data['users_login'], $this->allUserLogins)) {
                     //For case-insensitive matches
                     foreach ($this->allUserLogins as $login) {
                         if (mb_strtolower($data['users_login']) == mb_strtolower($login)) {
                             $data['users_login'] = $login;
                             $userFound = true;
                         }
                     }
                 } else {
                     $userFound = true;
                 }
                 if ($userFound) {
                     $lessons_name = trim($data['lesson_name']);
                     $lessons_ID = $this->getLessonByName($lessons_name);
                     unset($data['lesson_name']);
                     if ($lessons_ID) {
                         foreach ($lessons_ID as $lesson_ID) {
                             $data['lessons_ID'] = $lesson_ID;
                             $lesson = new EfrontLesson($lesson_ID);
                             if (is_null($this->lesson_users[$lesson_ID])) {
                                 $result = eF_getTableDataFlat("users_to_lessons", "users_LOGIN", "lessons_ID={$lesson_ID} AND archive=0");
                                 $this->lesson_users[$lesson_ID] = array_combine($result['users_LOGIN'], $result['users_LOGIN']);
                             }
                             if (isset($this->lesson_users[$lesson_ID][$data['users_login']])) {
                                 throw new Exception("User is already assigned to the lesson", self::USER_TO_LESSON_EXISTS);
                             }
                             if (!$lesson->lesson['course_only']) {
                                 $lesson->addUsers($data['users_login'], isset($data['user_type']) ? $data['user_type'] : "student");
                             }
                             $data['completed'] ? $data['completed'] = 1 : ($data['completed'] = 0);
                             eF_updateTableData("users_to_lessons", $data, "users_login = '******'users_login'] . "' AND lessons_ID = " . $data['lessons_ID']);
                             if (!$lesson->lesson['course_only']) {
                                 if ($data['active']) {
                                     $lesson->confirm($data['users_login']);
                                 } else {
                                     $lesson->unconfirm($data['users_login']);
                                 }
                             }
                             $this->log["success"][] = _LINE . " {$line}: " . _NEWLESSONASSIGNMENT . " " . $lessons_name . " - " . $data['users_login'];
                         }
                     } else {
                         if ($lessons_name != "") {
                             $lesson = EfrontLesson::createLesson(array("name" => $lessons_name, 'course_only' => false));
                             $this->log["success"][] = _LINE . " {$line}: " . _NEWLESSON . " " . $lessons_name;
                             $lesson->addUsers($data['users_login'], isset($data['user_type']) ? $data['user_type'] : "student");
                             $lessons_ID = $lesson->lesson['id'];
                             $this->lessonNamesToIds[$lessons_name] = array($lessons_ID);
                             eF_updateTableData("users_to_lessons", $data, "users_login = '******'users_login'] . "' AND lessons_ID = " . $lessons_ID);
                             if ($data['active']) {
                                 $lesson->confirm($data['users_login']);
                             } else {
                                 $lesson->unconfirm($data['users_login']);
                             }
                             $this->log["success"][] = _LINE . " {$line}: " . _NEWLESSONASSIGNMENT . " " . $lessons_name . " - " . $data['users_login'];
                         } else {
                             $this->log["failure"][] = _LINE . " {$line}: " . _COULDNOTFINDLESSON . " " . $lessons_name;
                         }
                     }
                 } else {
                     $this->log["failure"][] = _LINE . " {$line}: " . _USERDOESNOTEXIST . ": " . $data['users_login'];
                 }
                 break;
             case "users_to_groups":
                 //debug();
                 $groups_ID = $this->getGroupByName($data['groups.name']);
                 $group_name = $data['groups.name'];
                 unset($data['groups.name']);
                 foreach ($groups_ID as $group_ID) {
                     $data['groups_ID'] = $group_ID;
                     $group = new EfrontGroup($group_ID);
                     $group->addUsers(array($data['users_login']));
                     $this->log["success"][] = _LINE . " {$line}: " . _NEWGROUPASSIGNMENT . " " . $group_name . " - " . $data['users_login'];
                 }
                 break;
                 //debug(false);
                 #cpp#ifdef ENTERPRISE
             //debug(false);
             #cpp#ifdef ENTERPRISE
             case "employees":
                 $this->cleanUpEmptyValues($data);
                 // a bit customized here, based on the fact that employees are always created together AFTER users (so the object should exist)
                 eF_updateTableData("module_hcd_employees", $data, "users_login='******'users_login'] . "'");
                 break;
             case "branches":
                 // If no father defined - root, else we may need to create the father first
                 $data['father_branch_ID'] = $this->getFatherBranchId($data['father_branch_name']);
                 $father_name = $data['father_branch_name'];
                 $branch_ID = $this->getBranchByName($data['name']);
                 $branch_name = $data['name'];
                 if (sizeof($branch_ID) > 0 && $data['name'] != $data['father_branch_name']) {
                     //TODO
                     unset($data['father_branch_name']);
                     $data['branch_ID'] = $branch_ID[0];
                     throw new EfrontBranchException(_BRANCHALREADYEXISTS, EfrontBranchException::BRANCH_EXISTS);
                 } else {
                     unset($data['father_branch_name']);
                     $data['url'] = EfrontBranch::getBranchUrl($data['name']);
                     $branch = EfrontBranch::createBranch($data);
                     $this->setBranchByName($branch_name, $branch->branch['branch_ID']);
                 }
                 $this->log["success"][] = _LINE . " {$line}: " . _NEWBRANCH . " " . $branch_name;
                 break;
             case "job_descriptions":
                 if ($data['branch_name'] == "") {
                     $data['branch_ID'] = "all";
                     // this denotes to the createJob class to put the job in all branches
                 } else {
                     $data['branch_ID'] = $this->getBranchByName($data['branch_name']);
                     if (sizeof($data['branch_ID']) > 0) {
                         //TODO: maybe different handling when multiple branches are found
                         $data['branch_ID'] = $data['branch_ID'][0];
                     } else {
                         throw new EfrontJobException(_BRANCHDESIGNATEDFORTHISJOBDESCRIPTIONDOESNOTEXIST, EfrontJobException::BRANCH_NOT_EXISTS);
                     }
                 }
                 unset($data['branch_name']);
                 if ($data['description'] != "") {
                     $job_ID = $this->getJobByName($data['description']);
                     if (sizeof($job_ID) > 0) {
                         $data['job_description_ID'] = $job_ID[0];
                         throw new EfrontJobException(_JOBDESCRIPTIONEXISTSALREADY, EfrontJobException::JOB_ALREADY_EXISTS);
                     } else {
                         EfrontJob::createJob($data);
                         $this->log["success"][] = _LINE . " {$line}: " . _NEWJOB . " " . $data['description'];
                     }
                 } else {
                     $this->log["failure"][] = _LINE . " {$line}: " . _NOTITLEPROVIDEDFORNEWJOB;
                 }
                 break;
             case "skills":
                 if ($data['skill_category'] == "") {
                     throw new EfrontSkillException(_MISSINGSKILLCATEGORY, EfrontSkillException::INVALID_SKILL_CATEGORY);
                 } else {
                     $data['categories_ID'] = $this->getSkillCategoryByName($data['skill_category']);
                     if ($data['categories_ID'][0] != "") {
                         $data['categories_ID'] = $data['categories_ID'][0];
                     } else {
                         // create skill category
                         $data['categories_ID'] = eF_insertTableData("module_hcd_skill_categories", array('description' => $data['skill_category']));
                         $this->setSkillCategoryByName($data['skill_category'], $data['categories_ID']);
                     }
                 }
                 unset($data['skill_category']);
                 $skill_ID = $this->getSkillByName($data['description']);
                 if ($skill_ID) {
                     //TODO: another double issue
                     $data['skill_ID'] = $skill_ID[0];
                     throw new EfrontSkillException(_SKILLALREADYEXISTS, EfrontSkillException::SKILL_EXISTS);
                 } else {
                     EfrontSkill::createSkill($data);
                     $this->log["success"][] = _LINE . " {$line}: " . _NEWSKILL . " " . $data['description'];
                 }
                 break;
             case "users_to_jobs":
                 // Get user
                 $user = EfrontUserFactory::factory($data["users_login"]);
                 // Get branch id
                 $branch_ID = $this->getBranchByName($data['branch_name']);
                 $branch_name = $data['branch_name'];
                 if ($branch_ID[0] != "") {
                     if (sizeof($branch_ID) == 1) {
                         $branch_ID = $branch_ID[0];
                     } else {
                         throw new EfrontBranchException(_BRANCHNAMEAMBIGUOUS, EfrontBranchException::BRANCH_AMBIGUOUS);
                     }
                 } else {
                     throw new EfrontBranchException(_BRANCHDOESNOTEXIST, EfrontBranchException::BRANCH_NOT_EXISTS);
                 }
                 // Get job id
                 $job_name = $data['description'];
                 if ($job_name != "") {
                     $new_job_description_ID = eF_getJobDescriptionId($job_name, $branch_ID);
                 } else {
                     throw new EfrontJobException(_MISSING_JOB_DESCRIPTION, EfrontJobException::MISSING_JOB_DESCRIPTION);
                 }
                 // Get hcd employee object
                 if ($data['supervisor']) {
                     $employee = new EfrontSupervisor(array("users_login" => $data["users_login"]));
                     $position = 1;
                 } else {
                     $employee = new EfrontEmployee(array("users_login" => $data["users_login"]));
                     $position = 0;
                 }
                 // Assign job
                 try {
                     $employee->addJob($user, $new_job_description_ID, $branch_ID, $position);
                     $this->log["success"][] = _LINE . " {$line}: " . _NEWJOBASSIGNMENT . " " . $data["users_login"] . " - (" . $branch_name . " - " . $job_name . ") ";
                 } catch (Exception $e) {
                     if ($this->options['replace_existing']) {
                         $employee->removeJob($new_job_description_ID);
                         $employee->addJob($user, $new_job_description_ID, $branch_ID, $position);
                         $this->log["success"][] = _LINE . " {$line}: " . _NEWJOBASSIGNMENT . " " . $data["users_login"] . " - (" . $branch_name . " - " . $job_name . ") ";
                     }
                 }
                 break;
             case "users_to_skills":
                 $skill_ID = $this->getSkillByName($data['description']);
                 $skill_name = $data['description'];
                 if ($skill_ID[0] != "") {
                     if (sizeof($skill_ID) == 1) {
                         $skill_ID = $skill_ID[0];
                     } else {
                         throw new EfrontSkillException(_SKILLNAMEAMBIGUOUS, EfrontSkillException::SKILL_AMBIGUOUS);
                     }
                 } else {
                     throw new EfrontSkillException(_SKILLDOESNOTEXIST, EfrontSkillException::SKILL_NOT_EXISTS);
                 }
                 $skill = new EfrontSkill($skill_ID);
                 $skill->assignToEmployee($data['users_login'], $data['specification']);
                 $this->log["success"][] = _LINE . " {$line}: " . _NEWSKILLASSIGNMENT . " " . $data["users_login"] . " - " . $skill_name;
                 break;
             case "courses_to_branches":
                 $branchId = $this->getBranchByName($data['branch_name']);
                 if (!$branchId[0]) {
                     $fields = array('name' => $data['branch_name'], 'url' => EfrontBranch::getBranchUrl($data['branch_name']));
                     $branch = EfrontBranch::createBranch($fields);
                 } else {
                     $branch = new EfrontBranch($branchId[0]);
                 }
                 $courseId = $this->getCourseByName($data['course_name']);
                 if ($courseId[0]) {
                     $branch->addCoursesToBranch($courseId[0]);
                 }
                 $this->log["success"][] = _LINE . " {$line}: " . _NEWCOURSETOBRANCHASSIGNMENT . " " . $data['course_name'] . " - " . $data['branch_name'];
                 break;
                 #cpp#endif
         }
     } catch (Exception $e) {
         if ($this->options['replace_existing']) {
             if ($this->isAlreadyExistsException($e->getCode(), $type)) {
                 $this->updateExistingData($line, $type, $data);
             } else {
                 $this->log["failure"][] = _LINE . " {$line}: " . $e->getMessage();
                 // ." ". str_replace("\n", "<BR>", $e->getTraceAsString());
             }
         } else {
             $this->log["failure"][] = _LINE . " {$line}: " . $e->getMessage();
             // ." ". str_replace("\n", "<BR>", $e->getTraceAsString());
         }
     }
 }