Exemplo n.º 1
0
     $smarty->assign("T_USER_COURSE", $user);
     $form->setDefaults(array("completed" => $user->user['completed'], "score" => $user->user['completed'] ? $user->user['score'] : round($totalScore), "comments" => $user->user['comments']));
     if ($user->user['to_timestamp']) {
         $smarty->assign("T_TO_TIMESTAMP", $user->user['to_timestamp']);
     } else {
         $smarty->assign("T_TO_TIMESTAMP", mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y")));
     }
     if ($form->isSubmitted() && $form->validate()) {
         $toTimestamp = mktime($_POST['completion_Hour'], $_POST['completion_Minute'], 0, $_POST['completion_Month'], $_POST['completion_Day'], $_POST['completion_Year']);
         if ($form->exportValue('completed')) {
             $courseUser = EfrontUserFactory::factory($_GET['edit_user'], false, 'student');
             $courseUser->completeCourse($currentCourse->course['id'], $form->exportValue('score'), $form->exportValue('comments'), $toTimestamp);
         } else {
             $fields = array("completed" => 0, "score" => 0, "issued_certificate" => '', "to_timestamp" => null, "comments" => '');
             $where = "users_LOGIN = '******'edit_user'] . "' and courses_ID=" . $currentCourse->course['id'];
             EfrontCourse::persistCourseUsers($fields, $where, $currentCourse->course['id'], $_GET['edit_user']);
             if ($user->user['issued_certificate'] != "") {
                 EfrontEvent::triggerEvent(array("type" => EfrontEvent::COURSE_CERTIFICATE_REVOKE, "users_LOGIN" => $_GET['edit_user'], "lessons_ID" => $currentCourse->course['id'], "lessons_name" => $currentCourse->course['name']));
             }
         }
         $message = _STUDENTSTATUSCHANGED;
         $message_type = 'success';
     }
     $renderer = prepareFormRenderer($form);
     $smarty->assign('T_COMPLETE_COURSE_FORM', $renderer->toArray());
 } else {
     if (isset($_GET['issue_certificate']) && !empty($result)) {
         try {
             $certificate = $currentCourse->prepareCertificate($_GET['issue_certificate']);
             $currentCourse->issueCertificate($_GET['issue_certificate'], $certificate);
             eF_redirect('' . basename($_SERVER['PHP_SELF']) . '?' . $baseUrl . '&op=course_certificates&reset_popup=1&&message=' . urlencode(_CERTIFICATEISSUEDSUCCESFULLY) . '&message_type=success');
 /**
  * Complete course
  *
  * This function is used to set the course status to completed for
  * the current user. If the course is set to automatically issue a
  * certificate, the certificate is issued.
  * <br/>Example:
  * <code>
  * $user -> completeCourse(5, 87, 'Very good progress');									  //Complete course with id 5
  * </code>
  *
  * @param Efrontmixed $course Either an EfrontCourse object or a course id
  * @param int $score The course score
  * @param string $comments Comments for the course completion
  * @return boolean True if everything is ok
  */
 public function completeCourse($course, $score, $comments, $time = '')
 {
     $time ? $timestamp = $time : ($timestamp = time());
     if (!$course instanceof EfrontCourse) {
         $course = new EfrontCourse($course);
     }
     $constraints = array('archive' => false, 'active' => true, 'return_objects' => false);
     $userCourses = $this->getUserCourses($constraints);
     if (in_array($course->course['id'], array_keys($userCourses))) {
         //keep completed date when it is set (when only score changed for example)
         $checkCompleted = $userCourses[$course->course['id']]['to_timestamp'];
         $fields = array('completed' => 1, 'to_timestamp' => $timestamp, 'score' => str_replace(',', '.', $score), 'comments' => $comments);
         $where = "users_LOGIN = '******'login'] . "' and courses_ID=" . $course->course['id'];
         EfrontCourse::persistCourseUsers($fields, $where, $course->course['id'], $this->user['login']);
         if (!self::$cached_modules) {
             self::$cached_modules = eF_loadAllModules();
         }
         // Trigger all necessary events. If the function has not been re-defined in the derived module class, nothing will happen
         foreach (self::$cached_modules as $module) {
             $module->onCompleteCourse($course->course['id'], $this->user['login']);
         }
         if ($course->options['auto_certificate']) {
             $certificate = $course->prepareCertificate($this->user['login'], $time);
             $course->issueCertificate($this->user['login'], $certificate);
         }
         $event = array("type" => EfrontEvent::COURSE_COMPLETION, "users_LOGIN" => $this->user['login'], "lessons_ID" => $course->course['id'], "lessons_name" => $course->course['name'], "replace" => true);
         EfrontEvent::triggerEvent($event);
         // Assign the related course skills to the employee
         if (G_VERSIONTYPE == 'enterprise') {
             #cpp#ifdef ENTERPRISE
             if (!$this->aspects['hcd']) {
                 $this->aspects['hcd'] = EfrontEmployeeFactory::factory($this->user['login']);
             }
             $employee = $this->aspects['hcd'];
             $newSkills = eF_getTableDataFlat("module_hcd_course_offers_skill", "skill_ID, specification", "courses_ID = '" . $course->course['id'] . "'");
             // The course associated skills will *complement* the existing ones - last argument = true
             $employee->addSkills($newSkills['skill_ID'], $newSkills['specification'], array_fill(0, sizeof($newSkills['skill_ID']), $score), true);
         }
         #cpp#endif
         return true;
     } else {
         return false;
     }
 }
Exemplo n.º 3
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());
         }
     }
 }