GitHub PullRequestReviewCommentEvent at 2014-05-08 17:27:38
Repository: chamilo-lms-utp
Actor: jmontoyaa
Payload: {"comment":{"url":"https://api.github.com/repos/beeznest/chamilo-lms-utp/pulls/comments/12440884","id":12440884,"diff_hunk":"@@ -3685,10 +3830,664 @@ public static function get_exercise_progress($sessionId = 0, $courseId = 0, $exe\n             question,\n             answer,\n             */\n+        }\n+        \n+        return $data;\n+    }\n+    \n+    /**\n+     * \n+     * @param int $userId\n+     * @param int $itemViewId\n+     * @param string $courseCode\n+     * @return int\n+     */\n+    public static function getAttemptNumber($userId, $itemViewId, $courseCode)\n+    {\n+        $userId = intval($userId);\n+        $itemViewId = intval($itemViewId);\n+        $courseCode = Database::escape_string($courseCode);\n+        $tblTrackExercices = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);\n+        \n+        $sqlQuery = \"SELECT count(*) FROM $tblTrackExercices\n+                     WHERE exe_user_id = $userId \n+                     AND orig_lp_item_view_id = $itemViewId\n+                     AND exe_cours_id = '$courseCode'\n+                     GROUP BY exe_user_id, orig_lp_item_view_id, exe_cours_id\n+                \";\n+        $rs = Database::query($sqlQuery);    \n+        if ($rs != false) {\n+            $row = Database::fetch_row($rs);\n+            return $row[0];\n+        } else {\n+            return 0;\n+        }\n+    }\n \n \n+    /**\n+    * Gets the exercise progress in a session\n+    * @param int $sessionId\n+    * @param int $courseId\n+    * @param int $exerciseId\n+    * @param string $dateFrom\n+    * @param string $dateTo\n+    * @param array $options\n+    * @param bool $type\n+    * @return array\n+    */\n+    public static function getExerciseProgressSession(\n+            $sessionId = 0, $courseId = 0, $exerciseId = 0, \n+            $dateFrom, $dateTo, $options = array(), $type = false)\n+    {\n+        $sessionId  = intval($sessionId);\n+        $courseId   = intval($courseId);\n+        $exerciseId = intval($exerciseId);\n+        $dateFrom  = Database::escape_string($dateFrom);\n+        $dateTo    = Database::escape_string($dateTo);\n+        /*\n+         * This method gets the data by blocks, as previous attempts at one single\n+         * query made it take ages. The logic of query division is described below\n+         */\n+        // Get tables names\n+        $tuser = Database::get_main_table(TABLE_MAIN_USER);\n+        $tquiz = Database::get_course_table(TABLE_QUIZ_TEST);\n+        $tquizAnswer = Database::get_course_table(TABLE_QUIZ_ANSWER);\n+        $tquizQuestion = Database::get_course_table(TABLE_QUIZ_QUESTION);\n+        $tquizRelQuestion = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);\n+        $ttrackExercises  = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);\n+        $ttrackAttempt    = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);\n+        $tblFieldValue = Database::get_main_table(TABLE_MAIN_LP_FIELD_VALUES);\n+        $tblFieldOption = Database::get_main_table(TABLE_MAIN_LP_FIELD_OPTIONS);\n+        $tblField = Database::get_main_table(TABLE_MAIN_LP_FIELD);\n+ \n+        require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';\n+        \n+        $sessions = array();\n+        $courses = array();\n+        // if session ID is defined but course ID is empty, get all the courses\n+        // from that session\n+        if (!empty($sessionId) && empty($courseId)) {\n+            // $courses is an array of course int id as index and course details hash as value\n+            $courses = SessionManager::get_course_list_by_session_id($sessionId);\n+            $sessions[$sessionId] = api_get_session_info($sessionId);\n+        } elseif (empty($sessionId) && !empty($courseId)) {\n+            // if, to the contrary, course is defined but not sessions, get the sessions that include this course\n+            // $sessions is an array like: [0] => ('id' => 3, 'name' => 'Session 35'), [1] => () etc;\n+            $course = api_get_course_info_by_id($courseId);\n+            $sessionsTemp = SessionManager::get_session_by_course($course['code']);\n+            $courses[$courseId] = $course;\n+            foreach ($sessionsTemp as $sessionItem) {\n+                $sessions[$sessionItem['id']] = $sessionItem;\n+            }\n+        } elseif (!empty($courseId) && !empty($sessionId)) {\n+            //none is empty\n+            $course = api_get_course_info_by_id($courseId);\n+            $courses[$courseId] = array($course['code']);\n+            $courses[$courseId]['code'] = $course['code'];\n+            $sessions[$sessionId] = api_get_session_info($sessionId);\n+        } else {\n+            //both are empty, not enough data, return an empty array\n+            return array();\n+        }\n+        // Now we have two arrays of courses and sessions with enough data to proceed\n+        // If no course could be found, we shouldn't return anything. Sessions can be empty (then we only return the pure-course-context results)\n+        if (count($courses) < 1) {\n+            return array();\n+        }\n+\n+        $data = array();\n+        // The following loop is less expensive than what it seems:\n+        // - if a course was defined, then we only loop through sessions\n+        // - if a session was defined, then we only loop through courses\n+        // - if a session and a course were defined, then we only loop once\n+        $fieldType = '';\n+        $extraField = '';\n+        if ($type) {\n+           $extraField = ', lo.option_display_text';\n+           $fieldType = \"LEFT JOIN $tblFieldValue lv ON lv.lp_id = te.orig_lp_id\n+                                                   AND lv.c_id = q.c_id\n+                         LEFT JOIN $tblFieldOption lo ON lv.field_value = lo.id\n+                         LEFT JOIN $tblField lf ON lf.id =  lo.field_id \n+                                                AND lf.field_variable = 'Tipo'\";            \n+        }\n+        \n+        foreach ($courses as $courseIdx => $courseData) {\n+            $where = '';\n+            $whereParams = array();\n+            $whereCourseCode = $courseData['code'];\n+            $whereSessionParams = '';\n+            if (count($sessions > 0)) {\n+                foreach ($sessions as $sessionIdx => $sessionData) {\n+                    if (!empty($sessionIdx)) {\n+                        $whereSessionParams .= $sessionIdx.',';\n+                    }\n+                }\n+                $whereSessionParams = substr($whereSessionParams,0,-1);\n+            }\n+\n+            if (!empty($exerciseId)) {\n+                $exerciseId = intval($exerciseId);\n+                $where .= ' AND q.id = %d ';\n+                $whereParams[] = $exerciseId;\n+            }\n+\n+            $limit = '';\n+            if (!empty($options['limit'])) {\n+                $limit = \" LIMIT \".$options['limit'];\n+            }\n+\n+            if (!empty($options['where'])) {\n+                $where .= ' AND '.Database::escape_string($options['where']);\n+            }\n+\n+            $order = '';\n+            if (!empty($options['order'])) {\n+                $order = \" ORDER BY \".$options['order'];\n+            }\n+\n+            if (!empty($dateTo) && !empty($dateFrom)) {\n+                $where .= sprintf(\" AND (te.start_date BETWEEN '%s 00:00:00' AND '%s 23:59:59')\", $dateFrom, $dateTo);\n+            }\n+\n+            $sql = \"SELECT\n+                te.session_id,\n+                ta.id as attempt_id,\n+                te.exe_user_id as user_id,\n+                te.exe_id as exercise_attempt_id,\n+                ta.question_id,\n+                ta.answer as answer_id,\n+                ta.tms as time,\n+                te.exe_exo_id as quiz_id,\n+                CONCAT ('c', q.c_id, '_e', q.id) as exercise_id,\n+                q.title as quiz_title,\n+                qq.description as description,\n+                ta.marks as grade,\n+                te.exe_cours_id\n+                $extraField\n+                FROM $ttrackExercises te\n+                INNER JOIN $ttrackAttempt ta ON ta.exe_id = te.exe_id\n+                INNER JOIN $tquiz q ON q.id = te.exe_exo_id\n+                INNER JOIN $tquizRelQuestion rq ON rq.exercice_id = q.id AND rq.c_id = q.c_id\n+                INNER JOIN $tquizQuestion qq ON qq.id = rq.question_id \n+                                                AND qq.c_id = rq.c_id \n+                                                AND qq.position = rq.question_order \n+                                                AND ta.question_id = rq.question_id\n+                $fieldType\n+                WHERE te.exe_cours_id = '$whereCourseCode' \".(empty($whereSessionParams)?'':\"AND te.session_id IN ($whereSessionParams)\").\"\n+                AND q.c_id = $courseIdx\n+                  $where $order $limit\";\n+            $sql_query = vsprintf($sql, $whereParams);\n+\n+            // Now browse through the results and get the data\n+            $rs = Database::query($sql_query);\n+            $userIds = array();\n+            $questionIds = array();\n+            $answerIds = array(); \n+            while ($row = Database::fetch_array($rs)) {\n+                //only show if exercise is visible\n+                if (api_get_item_visibility($courseData, 'quiz', $row['exercise_id'])) {\n+                    $userIds[$row['user_id']] = $row['user_id'];\n+                    $questionIds[$row['question_id']] = $row['question_id'];\n+                    $answerIds[$row['question_id']][$row['answer_id']] = $row['answer_id'];\n+                    $row['session'] = $sessions[$row['session_id']];\n+                    $data[] = $row;\n+                }\n+            }\n+            // Now fill questions data. Query all questions and answers for this test to avoid\n+            $sqlQuestions = \"SELECT tq.c_id, tq.id as question_id, tq.question, tqa.id_auto, \n+                                    tqa.answer, tqa.correct, tq.position, tqa.id_auto as answer_id\n+                               FROM $tquizQuestion tq, $tquizAnswer tqa\n+                               WHERE tqa.question_id =tq.id and tqa.c_id = tq.c_id\n+                                 AND tq.c_id = $courseIdx AND tq.id IN (\".implode(',',$questionIds).\")\";\n+            $resQuestions = Database::query($sqlQuestions);\n+            $answer = array();\n+            $question = array();\n+            while ($rowQuestion = Database::fetch_assoc($resQuestions)) {\n+                $questionId = $rowQuestion['question_id'];\n+                $answerId = $rowQuestion['answer_id'];\n+                $answer[$questionId][$answerId] = array(\n+                                                        'position' => $rowQuestion['position'],\n+                                                        'question' => $rowQuestion['question'],\n+                                                        'answer' => $rowQuestion['answer'],\n+                                                        'correct' => $rowQuestion['correct']\n+                                                       );\n+                $question[$questionId]['question'] = $rowQuestion['question'];\n+                \n+            }\n+            \n+            // Now fill users data\n+            $sqlUsers = \"SELECT user_id, username, lastname, firstname \n+                         FROM $tuser \n+                         WHERE user_id IN (\" . implode(',',$userIds) . \")\";\n+            $resUsers = Database::query($sqlUsers);\n+            while ($rowUser = Database::fetch_assoc($resUsers)) {\n+                $users[$rowUser['user_id']] = $rowUser;\n+            }\n+            $summary = array();\n+            foreach ($data as $id => $row) {\n+                $rowQuestId = $row['question_id'];\n+                $rowSession = $row['session_id'];\n+                if (!isset($summary[$rowSession][$rowQuestId]['grade'])) {\n+                    $summary[$rowSession][$rowQuestId]['session'] = $sessions[$row['session_id']]['name'];\n+                    $summary[$rowSession][$rowQuestId]['course'] = $row['exe_cours_id'];\n+                    $summary[$rowSession][$rowQuestId]['type'] = $row['option_display_text'];\n+                    $summary[$rowSession][$rowQuestId]['question'] = $question[$rowQuestId]['question'];\n+                    $summary[$rowSession][$rowQuestId]['question_id'] = $rowQuestId;\n+                    $summary[$rowSession][$rowQuestId]['description'] = $row['description'];\n+                    $summary[$rowSession][$rowQuestId]['quiz_title'] = $row['quiz_title'];\n+                    $summary[$rowSession][$rowQuestId]['grade'] = $row['grade'];\n+                    $summary[$rowSession][$rowQuestId]['count'] = 1;\n+                } else {\n+                    $summary[$rowSession][$rowQuestId]['grade'] += $row['grade'];\n+                    $summary[$rowSession][$rowQuestId]['count']++;\n+                }\n+            }\n+        }\n+        \n+        $result = array();\n+        foreach ($summary as $session) {\n+            foreach ($session as $questions) {\n+                $result[] = $questions;\n+            }\n+        }\n+        \n+        return $result;\n+    }\n+    \n+    /**\n+     * Gets the progress of the given course\n+     * And some values with tags\n+     * @param int course_id\n+     * @param array options order and limit keys\n+     * @return array\n+     */\n+    public static function getSessionProgressSummary($courseId, $sessionId, $options)\n+    {\n+        //tables\n+        $tblSessionCourseUser = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);\n+        $user = Database::get_main_table(TABLE_MAIN_USER);\n+        $tblCourseLp = Database::get_course_table(TABLE_LP_MAIN);\n+\n+        $course = api_get_course_info_by_id($courseId);\n+\n+        $where = \" WHERE course_code = '%s'\n+        AND s.status <> 2 \";\n+\n+        $limit = null;\n+        if (!empty($options['limit'])) {\n+            $limit = \" LIMIT \" . $options['limit'];\n+        }\n+\n+        if (!empty($options['where'])) {\n+            $where .= ' AND ' . $options['where'];\n+        }\n+\n+        $order = null;\n+        if (!empty($options['order'])) {\n+            $order = \" ORDER BY \" . $options['order'];\n+        }\n+\n+        \n+        $queryVariables = array($course['code']);\n+        if (!empty($sessionId)) {\n+            $where .= ' AND id_session = %s';\n+            $queryVariables[] = $sessionId;\n+            $sql = \"SELECT u.user_id, u.lastname, u.firstname, u.username, \n+                u.email, s.course_code, s.id_session\n+                FROM $tblSessionCourseUser s\n+                INNER JOIN $user u ON u.user_id = s.id_user\n+                $where $order $limit\";\n+        } else {\n+            $sql = \"SELECT u.user_id, u.lastname, u.firstname, u.username, \n+                u.email, s.course_code, s.id_session\n+                FROM $tblSessionCourseUser s\n+                INNER JOIN $user u ON u.user_id = s.id_user\n+                $where $order $limit\";\n+        }\n+        \n+        $sqlQuery = vsprintf($sql, $queryVariables);\n+\n+        $rs = Database::query($sqlQuery);\n+        while ($user = Database::fetch_array($rs)) {\n+            $users[$user['user_id']] = $user;\n+        }\n+\n+        /**\n+         *  Lessons\n+         */\n+        $sql = \"SELECT * FROM $tblCourseLp\n+        WHERE c_id = %s \"; \n+        $sqlQuery = sprintf($sql, $course['real_id']);\n+\n+        $result = Database::query($sqlQuery);\n+        $arrLesson = array(array());\n+        while ($row = Database::fetch_array($result)) {\n+            if (empty($arrLesson[$row['session_id']]['lessons_total'])) {\n+                $arrLesson[$row['session_id']]['lessons_total'] = 1;\n+            } else {\n+                $arrLesson[$row['session_id']]['lessons_total'] ++;\n+            }\n+        }\n+        $hasTypeTag = (!empty($options['type']) ? true : false);\n+        $tagData = array();\n+        $sessionSum = array();\n+        //process table info\n+        foreach ($users as $user) {\n+            $idSession = $user['id_session'];\n+            //Time Spent in Course\n+            $timeSpentCourse = Tracking::get_time_spent_on_the_course(","path":"main/inc/lib/tracking.lib.php","position":751,"original_position":751,"commit_id":"0173132e2558ed05e1ef34635f14db1f1e0a8337","original_commit_id":"0173132e2558ed05e1ef34635f14db1f1e0a8337","user":{"login":"jmontoyaa","id":158935,"avatar_url":"https://avatars.githubusercontent.com/u/158935?","gravatar_id":"35bac50f34dea0ea78259e7a0d23fa1e","url":"https://api.github.com/users/jmontoyaa","html_url":"https://github.com/jmontoyaa","followers_url":"https://api.github.com/users/jmontoyaa/followers","following_url":"https://api.github.com/users/jmontoyaa/following{/other_user}","gists_url":"https://api.github.com/users/jmontoyaa/gists{/gist_id}","starred_url":"https://api.github.com/users/jmontoyaa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jmontoyaa/subscriptions","organizations_url":"https://api.github.com/users/jmontoyaa/orgs","repos_url":"https://api.github.com/users/jmontoyaa/repos","events_url":"https://api.github.com/users/jmontoyaa/events{/privacy}","received_events_url":"https://api.github.com/users/jmontoyaa/received_events","type":"User","site_admin":false},"body":"Revisar el formato de código.","created_at":"2014-05-08T17:27:38Z","updated_at":"2014-05-08T17:27:38Z","html_url":"https://github.com/beeznest/chamilo-lms-utp/pull/29#discussion_r12440884","pull_request_url":"https://api.github.com/repos/beeznest/chamilo-lms-utp/pulls/29","_links":{"self":{"href":"https://api.github.com/repos/beeznest/chamilo-lms-utp/pulls/comments/12440884"},"html":{"href":"https://github.com/beeznest/chamilo-lms-utp/pull/29#discussion_r12440884"},"pull_request":{"href":"https://api.github.com/repos/beeznest/chamilo-lms-utp/pulls/29"}}}}