Esempio n. 1
0
 public function getUserLessonStatus($lesson, $user, $options)
 {
     $times = new EfrontTimes();
     $usersTimesInLessonContent = array();
     if ($lesson instanceof EfrontLesson) {
         $lessonId = $lesson->lesson['id'];
     } else {
         $lessonId = $lesson;
     }
     //$usersTimesInLessonContent[$user] = $times->getUserSessionTimeInLessonContent($user, $lessonId);
     $usersTimesInLessonContent[$user] = EfrontLesson::getUserActiveTimeInLesson($user, $lessonId);
     $usersDoneContent = EfrontStats::getStudentsSeenContent($lesson, $user, $options);
     //Calculate the done content for users in this lesson
     $usersAssignedProjects = array();
     if (!isset($options['noprojects']) || !$options['noprojects']) {
         $usersAssignedProjects = EfrontStats::getStudentsAssignedProjects($lesson, $user);
     }
     $usersDoneTests = array();
     if (!isset($options['notests']) || !$options['notests']) {
         $usersDoneTests = EfrontStats::getStudentsDoneTests($lesson, $user);
     }
     $roles = EfrontLessonUser::getLessonsRoles();
     //transpose projects array, from (login => array(project id => project)) to array(lesson id => array(login => array(project id => project)))
     $temp = array();
     foreach ($usersAssignedProjects as $login => $userProjects) {
         foreach ($userProjects as $projectId => $project) {
             $temp[$project['lessons_ID']][$login][$projectId] = $project;
         }
     }
     $usersAssignedProjects = $temp;
     //transpose tests array, from (login => array(test id => test)) to array(lesson id => array(login => array(test id => test)))
     $temp0 = eF_getTableData("content", "id,ctg_type");
     //filter feedbacks
     foreach ($temp0 as $value) {
         $checkctg[$value['id']] = $value['ctg_type'];
     }
     $temp = array();
     foreach ($usersDoneTests as $login => $userTests) {
         foreach ($userTests as $contentID => $test) {
             if ($checkctg[$contentID] != 'feedback') {
                 $temp[$test['lessons_ID']][$login][$contentID] = $test;
             }
         }
     }
     $usersDoneTests = $temp;
     if (!$user instanceof EfrontUser) {
         $user = EfrontUserFactory::factory($user);
         $user = $user->user;
     }
     if (!$lesson instanceof EfrontLesson) {
         $lesson = new EfrontLesson($lesson);
     }
     $result = eF_getTableData("users_to_lessons", "*", "users_LOGIN ='******'login'] . "' and lessons_ID = " . $lesson->lesson['id']);
     if (sizeof($result[0]['users_LOGIN']) > 0) {
         if ($lesson->lesson['duration'] && $result[0]['from_timestamp']) {
             $result[0]['remaining'] = $result[0]['from_timestamp'] + $lesson->lesson['duration'] * 3600 * 24 - time();
         } else {
             $result[0]['remaining'] = null;
         }
         //Check whether the lesson registration is expired. If so, set $result[0]['from_timestamp'] to false, so that the effect is to appear disabled
         if ($lesson->lesson['duration'] && $result[0]['from_timestamp'] && $lesson->lesson['duration'] * 3600 * 24 + $result[0]['from_timestamp'] < time()) {
             $lesson->removeUsers($result[0]['users_LOGIN']);
         } else {
             $usersLessons[$result[0]['lessons_ID']][$result[0]['users_LOGIN']] = $result[0];
             $usersLessonsTypes[$result[0]['lessons_ID']][$result[0]['users_LOGIN']] = $roles[$result[0]['user_type']];
             //Handy since we need to know whether a lesson has any students
         }
     }
     //Build a caching set for conditions, so that we avoid looping queries inside $lesson -> getConditions();
     $result = eF_getTableData("lesson_conditions", "*", "lessons_ID=" . $lesson->lesson['id']);
     $conditions = array();
     foreach ($result as $value) {
         $conditions[$value['lessons_ID']][] = $value;
     }
     $lessonStatus = array();
     if (in_array('student', $usersLessonsTypes[$lesson->lesson['id']])) {
         //Calculate these statistics only if the lesson has students
         !isset($conditions[$lesson->lesson['id']]) ? $conditions[$lesson->lesson['id']] = array() : null;
         $lessonConditions = $lesson->getConditions($conditions[$lesson->lesson['id']]);
         $lessonContent = new EfrontContentTree($lesson);
         $doneContent = isset($usersDoneContent[$lesson->lesson['id']]) ? $usersDoneContent[$lesson->lesson['id']] : array();
         $doneTests = isset($usersDoneTests[$lesson->lesson['id']]) ? $usersDoneTests[$lesson->lesson['id']] : array();
         $assignedProjects = isset($usersAssignedProjects[$lesson->lesson['id']]) ? $usersAssignedProjects[$lesson->lesson['id']] : array();
         $visitableContentIds = array();
         $visitableExampleIds = array();
         $visitableTestIds = array();
         $testIds = array();
         foreach ($iterator = new EfrontVisitableFilterIterator(new EfrontNodeFilterIterator(new RecursiveIteratorIterator(new RecursiveArrayIterator($lessonContent->tree), RecursiveIteratorIterator::SELF_FIRST))) as $key => $value) {
             if ($value['active']) {
                 switch ($value->offsetGet('ctg_type')) {
                     case 'theory':
                     case 'scorm':
                     case 'feedback':
                         $visitableContentIds[$key] = $key;
                         //Get the not-test unit ids for this content
                         break;
                     case 'examples':
                         $visitableExampleIds[$key] = $key;
                         //Get the not-test unit ids for this content
                         break;
                     case 'tests':
                     case 'scorm_test':
                         $visitableTestIds[$key] = $key;
                         //Get the scorm test unit ids for this content
                         $testIds[$key] = $key;
                         //Get the test unit ids for this content
                         break;
                 }
             }
         }
         $visitableUnits = $visitableContentIds + $visitableExampleIds + $visitableTestIds;
     }
     foreach ($usersLessons[$lesson->lesson['id']] as $login => $value) {
         $lessonStatus = array('login' => $login, 'name' => $user['name'], 'surname' => $user['surname'], 'basic_user_type' => $user['user_type'], 'user_type' => $value['user_type'], 'user_types_ID' => $user['user_types_ID'], 'different_role' => $value['user_type'] != $user['user_type'] && $value['user_type'] != $user['user_types_ID'], 'different_role' => !$users[$login]['user_types_ID'] && $value['user_type'] != $users[$login]['user_type'] || $users[$login]['user_types_ID'] && $value['user_type'] != $users[$login]['user_types_ID'], 'active' => $user['active'], 'lesson_name' => $lesson->lesson['name'], 'from_timestamp' => $value['from_timestamp'], 'remaining' => $value['remaining']);
         //Student - specific information
         if ($roles[$value['user_type']] == 'student') {
             !isset($doneContent[$login]) ? $doneContent[$login] = array() : null;
             !isset($assignedProjects[$login]) ? $assignedProjects[$login] = array() : null;
             list($conditionsMet, $lessonPassed) = self::checkConditions($doneContent[$login], $lessonConditions, $visitableUnits, $visitableTestIds, $usersTimesInLessonContent[$login]);
             //Content progress is theory and examples units seen
             $contentProgress = 0;
             if (isset($doneContent[$login]) && sizeof($doneContent[$login]) > 0 && (sizeof($visitableContentIds) > 0 || sizeof($visitableExampleIds) > 0)) {
                 $contentProgress = round(100 * sizeof(array_diff_key($doneContent[$login], $visitableTestIds)) / (sizeof($visitableContentIds) + sizeof($visitableExampleIds)), 2);
             }
             //Calculate tests average score and progress
             $testsProgress = 0;
             $numCompletedTests = 0;
             $testsAvgScore = array();
             if (sizeof($testIds) > 0 && isset($doneTests[$login]) && sizeof($doneTests[$login]) > 0) {
                 foreach ($doneTests[$login] as $doneTest) {
                     $testsAvgScore[] = $doneTest['active_score'];
                 }
                 $testsAvgScore = array_sum($testsAvgScore) / sizeof($testsAvgScore);
                 $numCompletedTests = 0;
                 if (!isset($doneTest[$login['lesson_status']]) || $doneTest[$login['lesson_status']] == 'passed' || $doneTest[$login['lesson_status']] == 'completed') {
                     $numCompletedTests++;
                 }
                 $testsProgress = round(100 * $numCompletedTests / sizeof($visitableTestIds), 2);
             } else {
                 $testsAvgScore = 0;
             }
             //Calculate projects average score and build done projects list, since we don't have this automatically
             $doneProjects = array();
             $projectsAvgScore = array();
             $projectsProgress = 0;
             if (sizeof($assignedProjects[$login]) > 0) {
                 foreach ($assignedProjects[$login] as $id => $project) {
                     if ($project['grade'] !== '' || $project['upload_timestamp']) {
                         $doneProjects[$id] = $project;
                         $projectsAvgScore[] = $project['grade'];
                     }
                 }
                 sizeof($doneProjects) > 0 ? $projectsAvgScore = array_sum($projectsAvgScore) / sizeof($projectsAvgScore) : ($projectsAvgScore = 0);
                 $projectsProgress = round(100 * sizeof($doneProjects) / sizeof($assignedProjects[$login]), 2);
             } else {
                 $projectsAvgScore = 0;
             }
             //Calculate overall progress, the number of done content + done (passed for SCORM) tests divided with the total units number
             $overallProgress = 0;
             if (sizeof($visitableUnits) > 0) {
                 $overallProgress = round(100 * sizeof(array_intersect(array_keys($visitableUnits), array_keys($doneContent[$login]))) / sizeof($visitableUnits), 2);
             }
             $lessonStatus['assigned_projects'] = $assignedProjects[$login];
             //the total assigned projects to the user, with information for each one.
             $lessonStatus['projects_progress'] = $projectsProgress;
             //the projects percentage done
             $lessonStatus['projects_avg_score'] = $projectsAvgScore;
             //the projects average score
             $lessonStatus['tests_progress'] = $testsProgress;
             //the tests percentage done
             $lessonStatus['tests_avg_score'] = $testsAvgScore;
             //the tests average score
             $lessonStatus['content_progress'] = $contentProgress;
             //the content (theory_examples) percentage done
             $lessonStatus['overall_progress'] = $overallProgress;
             //the total percentage done, including content and tests
             $lessonStatus['lesson_passed'] = $lessonPassed;
             $lessonStatus['total_conditions'] = sizeof($lessonConditions);
             $lessonStatus['conditions_passed'] = array_sum($conditionsMet);
             $lessonStatus['completed'] = $value['completed'];
             $lessonStatus['score'] = $value['score'];
             $lessonStatus['comments'] = $value['comments'] ? $value['comments'] : 0;
         }
     }
     return $lessonStatus;
 }