From d6a3679fe8a25a7de7b5e84a3a69ddcdc179c1fe Mon Sep 17 00:00:00 2001 From: Arnaud Ligot Date: Sun, 27 Feb 2011 12:48:45 +0100 Subject: [PATCH] working first implementation of reports_ tables --- main/reports/modules/quiz.php | 34 +++++++++ main/reports/reports.cli.php | 10 ++- main/reports/reports.lib.php | 137 +++++++++++++++++++++++++++++++++- 3 files changed, 176 insertions(+), 5 deletions(-) create mode 100644 main/reports/modules/quiz.php diff --git a/main/reports/modules/quiz.php b/main/reports/modules/quiz.php new file mode 100644 index 0000000000..979e48f20c --- /dev/null +++ b/main/reports/modules/quiz.php @@ -0,0 +1,34 @@ + + 'select '.$course_id.' as course_id, '. + $reports_modules_quiz_toolid.' as tool_id, '. + 'q.id as child_id, q.title as child_name, '. + "'".$course_code."'".' as course_code from '. + Database::get_course_table(TABLE_QUIZ_TEST, $course_db). + ' q', + 'values_query_function' => 'reports_modules_quiz_quizVal')); +} + +function reports_modules_quiz_quizVal($quiz, $key_id) { + return array('type'=> 'sql', 'sql' => + 'select '.$key_id.', exe_user_id as uid, '. + 'session_id, -1 as attempt, exe_result as score, '. + REPORTS_PROGRESS_COMPLETED.' as progress, '. + 'exe_duration as time from '. + Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES). + ' where exe_cours_id = '."'".$quiz['course_code']."'". + ' and exe_exo_id='.$quiz['child_id']); +} diff --git a/main/reports/reports.cli.php b/main/reports/reports.cli.php index 5b1ab933a6..8cbff96fd6 100644 --- a/main/reports/reports.cli.php +++ b/main/reports/reports.cli.php @@ -22,7 +22,9 @@ $longopts = array( 'session:', 'attempt:', 'uid:', - 'key:'); + 'key:', + 'addDBKeys', + 'build'); $options = getopt("", $longopts); @@ -33,6 +35,12 @@ if (array_key_exists('help', $options)) else if (array_key_exists('clearAll', $options)) { reports_clearAll(); echo Database::error(); +} else if (array_key_exists('build', $options)) { + reports_build(); + echo Database::error(); +} else if (array_key_exists('addDBKeys', $options)) { + reports_addDBKeys(); + echo Database::error(); } else if (array_key_exists('addValue', $options)) { reports_addValue($options['key'], $options['session'], $options['uid'], $options['attempt'], $options['score'], diff --git a/main/reports/reports.lib.php b/main/reports/reports.lib.php index f8d58f0de2..6c703edde4 100644 --- a/main/reports/reports.lib.php +++ b/main/reports/reports.lib.php @@ -2,17 +2,89 @@ require_once '../inc/global.inc.php'; -// FIXME chamilo upgrade table creation + +define ('REPORTS_PROGRESS_COMPLETED', 1); + +$reports_modules = array(); + +$reports_enabled_modules = array('quiz'); + // clear all reporting data function reports_clearAll() { +/* Database::query('DELETE FROM '.Database::get_main_table(TABLE_MAIN_REPORTS_KEYS)); Database::query('DELETE FROM '.Database::get_main_table(TABLE_MAIN_REPORTS_VALUES)); +*/ + Database::query('DROP TABLE '.Database::get_main_table(TABLE_MAIN_REPORTS_KEYS)); + Database::query('DROP TABLE '.Database::get_main_table(TABLE_MAIN_REPORTS_VALUES)); + Database::query(' +CREATE TABLE '.Database::get_main_table(TABLE_MAIN_REPORTS_KEYS).' ( + `id` int(11) NOT NULL AUTO_INCREMENT primary key, + `course_id` int(11) DEFAULT NULL, + `tool_id` int(11) DEFAULT NULL, + `child_id` int(11) DEFAULT NULL, + `child_name` varchar(64) DEFAULT NULL, + `subchild_id` int(11) DEFAULT NULL, + `subchild_name` varchar(64) DEFAULT NULL, + `subsubchild_id` int(11) DEFAULT NULL, + `subsubchild_name` varchar(64) DEFAULT NULL, + `link` varchar(256) DEFAULT NULL)'); + + Database::query(' +CREATE TABLE '.Database::get_main_table(TABLE_MAIN_REPORTS_VALUES).' ( + `key_id` int(11) NOT NULL, + `uid` int(11) NOT NULL, + `session_id` int(11) NOT NULL, + `attempt` int(11) NOT NULL, + `score` decimal(5,3) DEFAULT NULL, + `progress` int(11) DEFAULT NULL, + `time` int(11) DEFAULT NULL)'); +} + +function reports_addDBKeys() { + // cannot had this primary key here due to mysql restrction on auto_increment +// Database::query('alter table '.Database::get_main_table(TABLE_MAIN_REPORTS_KEYS). +// ' primary key(id)'); + Database::query('alter ignore table '. + Database::get_main_table(TABLE_MAIN_REPORTS_KEYS). + ' add index(course_id)'); + Database::query('alter table '. + Database::get_main_table(TABLE_MAIN_REPORTS_KEYS). + ' add index(course_id,tool_id,child_id,subchild_id,subsubchild_id)'); + Database::query('alter ignore table '. + Database::get_main_table(TABLE_MAIN_REPORTS_VALUES). + ' add index(uid)'); + Database::query('alter ignore table '. + Database::get_main_table(TABLE_MAIN_REPORTS_VALUES). + ' add primary key(key_id,uid,session_id,attempt)'); } // build all reporting data function reports_build() { - // FIXME + global $reports_enabled_modules, $reports_modules; + + // include + init + foreach($reports_enabled_modules as $module) { + require_once('modules/'.$module.'.php'); + $initFunc = 'reports_modules_'.$module.'_init'; + + $initFunc(); + } + + // init For Each Courses + foreach($reports_enabled_modules as $module) { + $initFuncFEC = 'reports_modules_'.$module.'_init_forEachCourses'; + foreach(CourseManager::get_courses_list() as $course) + $initFuncFEC($course['code'], $course['id'], + $course['db_name']); + } + + // fetch data + foreach($reports_enabled_modules as $module) + foreach ($reports_modules[$module] as $keys) + reports_automaticAdd($keys['keys_query'], + $keys['values_query_function']); } // add a key and returns his id @@ -36,7 +108,7 @@ function reports_addKey($course_id, $tool_id, ($subchild_name == '' ? 'NULL' : "'$subchild_name'").', '. ($subsubchild_id == '' ? 'NULL' : $subsubchild_id).', '. ($subsubchild_name == '' ? 'NULL' : "'$subsubchild_name'").', '. - ($link == '' ? 'NULL' : "'$lin'").')'); + ($link == '' ? 'NULL' : "'$link'").')'); return Database::insert_id(); } @@ -56,10 +128,18 @@ function reports_addValue($key, $session, $uid, $attempt, $score, ($time == '' ? 'NULL' : $time).')'); } +// add a value using a sub query warning take care about the order of the fields +function reports_addValueQuery($query) { + Database::query('INSERT into '. + Database::get_main_table(TABLE_MAIN_REPORTS_VALUES). + ' (key_id, uid, session_id, attempt, score, '. + 'progress, time) ('.$query.')'); +} + // return tools ID (parametre is a constant from main_api function reports_getToolId($tool) { $tools = array_flip(api_get_tools_lists()); - if (array_key_exists($tool, $tools)) + if (array_key_exists($tool, $tools)) return $tools[$tool]; else return null; @@ -72,3 +152,52 @@ function reports_getVisibilitySQL () { // fixme sessions } +// this function execute keys_query (SQL statement) +// each rows may returns following fields course_id, tool_id, child_id, +// child_name, subchild_id, subchild_name, subsubchild_id, subsubchild_name, +// link +// row may contains other fields. +// rows are parsed using fetch_assoc +// assoc array are then given to values_query_function +// this function must return a assoc array +// return["type"] should be either 'static' or 'sql' +// return["static"] should contains an array of assoc array which may +// includes following headers: session, uid, attempt, score progress and +// time. +// return["sql"] (when type==sql) an sql query returning the same fields. +// this sql stateuement MUST include a field key_id with the value given +// to the function as parametre. This statement will be passed to +// reports_addValueQuery +function reports_automaticAdd($keys_query, $values_query_function) { + $keys_result = Database::query($keys_query); + if (!$keys_query) { + echo 'folowwing keys_query failed: '.$keys_query."\n"; + return; + } + $num = Database::num_rows($keys_result); + for ($i = 0; $i < $num; $i++) { + $keys = Database::fetch_assoc($keys_result); + $key_id = reports_addKey( + array_key_exists('course_id', $keys) ? $keys['course_id'] : '', + array_key_exists('tool_id', $keys) ? $keys['tool_id'] : '', + array_key_exists('child_id', $keys) ? $keys['child_id'] : '', + array_key_exists('child_name', $keys) ? $keys['child_name'] : '', + array_key_exists('subchild_id', $keys) ? $keys['subchild_id'] : '', + array_key_exists('subchild_name', $keys) ? $keys['subchild_name'] : '', + array_key_exists('subsubchild_id', $keys) ? $keys['subsubchild_id'] : '', + array_key_exists('subsubchild_name', $keys) ? $keys['subsubchild_name'] : '', + array_key_exists('link', $keys) ? $keys['link'] : ''); + $values = $values_query_function($keys, $key_id); + if ($values['type'] == 'static') + for ($j = 0; $j