From 58334f81813bf4f98fd29c1c0b39e8510fb7de2e Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Thu, 9 Oct 2014 09:27:08 +0200 Subject: [PATCH] Adding OnlyBestAttempts option see BT#8746 --- main/exercice/exercise_report.php | 44 ++-- main/exercice/exercise_result.class.php | 202 +++++++----------- .../hotpotatoes_exercise_result.class.php | 86 ++++---- 3 files changed, 149 insertions(+), 183 deletions(-) diff --git a/main/exercice/exercise_report.php b/main/exercice/exercise_report.php index 0cb8ecd0d4f..7bdddc9f312 100755 --- a/main/exercice/exercise_report.php +++ b/main/exercice/exercise_report.php @@ -69,18 +69,21 @@ api_not_allowed(true); } -if (!empty($exercise_id)) +if (!empty($exercise_id)) { $parameters['exerciseId'] = $exercise_id; +} + if (!empty($_GET['path'])) { $parameters['path'] = Security::remove_XSS($_GET['path']); } if (!empty($_REQUEST['export_report']) && $_REQUEST['export_report'] == '1') { - if (api_is_platform_admin() || api_is_course_admin() || api_is_course_tutor() || api_is_course_coach()) { - - $load_extra_data = false; + if (api_is_platform_admin() || api_is_course_admin() || + api_is_course_tutor() || api_is_course_coach() + ) { + $loadExtraData = false; if (isset($_REQUEST['extra_data']) && $_REQUEST['extra_data'] == 1) { - $load_extra_data = true; + $loadExtraData = true; } $includeAllUsers = false; @@ -90,19 +93,26 @@ $includeAllUsers = true; } + $onlyBestAttempts = false; + if (isset($_REQUEST['only_best_attempts']) && + $_REQUEST['only_best_attempts'] == 1 + ) { + $onlyBestAttempts = true; + } + require_once 'exercise_result.class.php'; $export = new ExerciseResult(); $export->setIncludeAllUsers($includeAllUsers); + $export->setOnlyBestAttempts($onlyBestAttempts); switch ($_GET['export_format']) { case 'xls' : $export->exportCompleteReportXLS( $documentPath, null, - $load_extra_data, + $loadExtraData, null, - $_GET['exerciseId'], - $_GET['hotpotato_name'] + $_GET['exerciseId'] ); exit; break; @@ -111,10 +121,9 @@ $export->exportCompleteReportCSV( $documentPath, null, - $load_extra_data, + $loadExtraData, null, - $_GET['exerciseId'], - $_GET['hotpotato_name'] + $_GET['exerciseId'] ); exit; break; @@ -251,11 +260,14 @@ $actions = null; if ($is_allowedToEdit && $origin != 'learnpath') { // the form - if (api_is_platform_admin() || api_is_course_admin() || api_is_course_tutor() || api_is_course_coach()) { + if (api_is_platform_admin() || api_is_course_admin() || + api_is_course_tutor() || api_is_course_coach() + ) { $actions .= ''.Display :: return_icon('back.png', get_lang('GoBackToQuestionList'), '', ICON_SIZE_MEDIUM).''; $actions .=''.Display :: return_icon('activity_monitor.png', get_lang('LiveResults'), '', ICON_SIZE_MEDIUM).''; $actions .=''.Display :: return_icon('statistics.png', get_lang('ReportByQuestion'), '', ICON_SIZE_MEDIUM).''; - $actions .= ''. + + $actions .= ''. Display::return_icon('save.png', get_lang('Export'), '', ICON_SIZE_MEDIUM).''; // clean result before a selected date icon $actions .= Display::url( @@ -349,7 +361,9 @@ var export_format = $("input[name=export_format]:checked").val(); var extra_data = $("input[name=load_extra_data]:checked").val(); var includeAllUsers = $("input[name=include_all_users]:checked").val(); - location.href = targetUrl+"&export_format="+export_format+"&extra_data="+extra_data+"&include_all_users="+includeAllUsers; + var attempts = $("input[name=only_best_attempts]:checked").val(); + + location.href = targetUrl+"&export_format="+export_format+"&extra_data="+extra_data+"&include_all_users="+includeAllUsers+"&only_best_attempts="+attempts; $( this ).dialog( "close" ); }, } @@ -366,6 +380,8 @@ $form->addElement('radio', 'export_format', null, get_lang('ExportAsXLS'), 'xls', array('id' => 'export_format_xls_label')); $form->addElement('checkbox', 'load_extra_data', null, get_lang('LoadExtraData'), '0', array('id' => 'export_format_xls_label')); $form->addElement('checkbox', 'include_all_users', null, get_lang('IncludeAllUsers'), '0'); +$form->addElement('checkbox', 'only_best_attempts', null, get_lang('OnlyBestAttempts'), '0'); + $form->setDefaults(array('export_format' => 'csv')); $extra .= $form->return_form(); $extra .= ''; diff --git a/main/exercice/exercise_result.class.php b/main/exercice/exercise_result.class.php index 0da82ef8890..60505628b0e 100755 --- a/main/exercice/exercise_result.class.php +++ b/main/exercice/exercise_result.class.php @@ -2,17 +2,16 @@ /* For licensing terms, see /license.txt */ /** - * ExerciseResult class: This class allows to instantiate an object - * of type ExerciseResult + * Class ExerciseResult * which allows you to export exercises results in multiple presentation forms * @package chamilo.exercise * @author Yannick Warnier */ class ExerciseResult { - private $exercises_list = array(); //stores the list of exercises - private $results = array(); //stores the results + private $results = array(); public $includeAllUsers = false; + public $onlyBestAttempts = false; /** * @param bool $includeAllUsers @@ -22,6 +21,14 @@ public function setIncludeAllUsers($includeAllUsers) $this->includeAllUsers = $includeAllUsers; } + /** + * @param bool $value + */ + public function setOnlyBestAttempts($value) + { + $this->onlyBestAttempts = $value; + } + /** * Gets the results of all students (or just one student if access is limited) * @@ -81,23 +88,11 @@ public function getExercisesReporting( INNER JOIN $TBL_TRACK_EXERCISES AS te ON (te.exe_exo_id = ce.id) INNER JOIN $TBL_USER AS user ON (user.user_id = exe_user_id) LEFT JOIN $TBL_TABLE_LP_MAIN AS tlm ON tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id - WHERE ce.c_id = $course_id AND - te.status != 'incomplete' AND - te.exe_cours_id='" . Database :: escape_string($cid) . "' $user_id_and $session_id_and AND - ce.active <>-1"; - $hpsql="SELECT ".(api_is_western_name_order() ? "firstname as userpart1, lastname userpart2" : "lastname as userpart1, firstname as userpart2").", - official_code, - email, - tth.exe_name, - tth.exe_result, - tth.exe_weighting, - tth.exe_date, - tth.exe_user_id as excruid - FROM $TBL_TRACK_HOTPOTATOES tth, $TBL_USER tu - WHERE tu.user_id=tth.exe_user_id AND - tth.exe_cours_id = '" . Database :: escape_string($cid) . "' AND - tth.exe_name = '$hotpotato_name' - ORDER BY tth.exe_cours_id ASC, tth.exe_date DESC"; + WHERE + ce.c_id = $course_id AND + te.status != 'incomplete' AND + te.exe_cours_id='" . Database :: escape_string($cid) . "' $user_id_and $session_id_and AND + ce.active <>-1"; } else { $user_id_and = ' AND te.exe_user_id = ' . api_get_user_id() . ' '; @@ -127,27 +122,28 @@ public function getExercisesReporting( te.exe_cours_id='" . Database :: escape_string($cid) . "' $user_id_and $session_id_and AND ce.active <>-1 AND ORDER BY userpart2, te.exe_cours_id ASC, ce.title ASC, te.exe_date DESC"; - - $hpsql = "SELECT '', exe_name, exe_result , exe_weighting, exe_date, exe_user_id as excruid - FROM $TBL_TRACK_HOTPOTATOES - WHERE - exe_user_id = '" . $user_id . "' AND - exe_cours_id = '" . Database :: escape_string($cid) . "' AND - tth.exe_name = '$hotpotato_name' - ORDER BY exe_cours_id ASC, exe_date DESC"; } $results = array(); $resx = Database::query($sql); + $bestAttemptPerUser = array(); while ($rowx = Database::fetch_array($resx,'ASSOC')) { - $results[] = $rowx; + if ($this->onlyBestAttempts) { + if (!isset($bestAttemptPerUser[$rowx['excruid']])) { + $bestAttemptPerUser[$rowx['excruid']] = $rowx; + } else { + if ($rowx['exresult'] > $bestAttemptPerUser[$rowx['excruid']]['exresult']) { + $bestAttemptPerUser[$rowx['excruid']] = $rowx; + } + } + } else { + $results[] = $rowx; + } } - $hpresults = array(); - $resx = Database::query($hpsql); - while ($rowx = Database::fetch_array($resx,'ASSOC')) { - $hpresults[] = $rowx; - } + if ($this->onlyBestAttempts) { + $results = $bestAttemptPerUser; + } $filter_by_not_revised = false; $filter_by_revised = false; @@ -165,19 +161,22 @@ public function getExercisesReporting( } } - //Print the results of tests + // Print the results of tests $userWithResults = array(); - if (is_array($results) && empty($hotpotato_name)) { - for ($i = 0; $i < sizeof($results); $i++) { + if (is_array($results)) { + $i = 0; + foreach ($results as $result) { $revised = false; //revised or not $sql_exe = "SELECT exe_id FROM $TBL_TRACK_ATTEMPT_RECORDING - WHERE author != '' AND exe_id = ".Database :: escape_string($results[$i]['exid'])." LIMIT 1"; + WHERE author != '' AND exe_id = ".Database :: escape_string($result['exid'])." + LIMIT 1"; $query = Database::query($sql_exe); - if (Database :: num_rows($query) > 0) + if (Database:: num_rows($query) > 0) { $revised = true; + } if ($filter_by_not_revised && $revised) { continue; @@ -188,54 +187,25 @@ public function getExercisesReporting( } $return[$i] = array(); - if (empty($user_id)) { - $return[$i]['official_code'] = $results[$i]['official_code']; - $return[$i]['first_name'] = $results[$i]['userpart1']; - $return[$i]['last_name'] = $results[$i]['userpart2']; - $return[$i]['user_id'] = $results[$i]['excruid']; - $return[$i]['email'] = $results[$i]['exemail']; + $return[$i]['official_code'] = $result['official_code']; + $return[$i]['first_name'] = $result['userpart1']; + $return[$i]['last_name'] = $result['userpart2']; + $return[$i]['user_id'] = $result['excruid']; + $return[$i]['email'] = $result['exemail']; } - $return[$i]['title'] = $results[$i]['extitle']; - $return[$i]['start_date'] = api_get_local_time($results[$i]['exstart']); - $return[$i]['end_date'] = api_get_local_time($results[$i]['exdate']); - $return[$i]['duration'] = $results[$i]['duration']; - $return[$i]['result'] = $results[$i]['exresult']; - $return[$i]['max'] = $results[$i]['exweight']; + $return[$i]['title'] = $result['extitle']; + $return[$i]['start_date'] = api_get_local_time($result['exstart']); + $return[$i]['end_date'] = api_get_local_time($result['exdate']); + $return[$i]['duration'] = $result['duration']; + $return[$i]['result'] = $result['exresult']; + $return[$i]['max'] = $result['exweight']; $return[$i]['status'] = $revised ? get_lang('Validated') : get_lang('NotValidated'); - $return[$i]['lp_id'] = $results[$i]['orig_lp_id']; - $return[$i]['lp_name'] = $results[$i]['lp_name']; - - $userWithResults[$results[$i]['excruid']] = 1; - } - } - - $isHotPotato = false; - - // Print the Result of Hotpotatoes Tests - if (is_array($hpresults) && !empty($hpresults)) { - $isHotPotato = true; - - for ($i = 0; $i < sizeof($hpresults); $i++) { - $return[$i] = array(); - $title = GetQuizName($hpresults[$i]['exe_name'], $document_path); - if ($title =='') { - $title = basename($hpresults[$i]['exe_name']); - } - if (empty($user_id)) { - $return[$i]['email'] = $hpresults[$i]['email']; - $return[$i]['first_name'] = $hpresults[$i]['userpart1']; - $return[$i]['last_name'] = $hpresults[$i]['userpart2']; - } - $return[$i]['title'] = $title; - $return[$i]['start_date'] = api_get_local_time($results[$i]['exstart']); - $return[$i]['end_date'] = api_get_local_time($results[$i]['exdate']); - $return[$i]['duration'] = $results[$i]['duration']; - - $return[$i]['result'] = $hpresults[$i]['exe_result']; - $return[$i]['max'] = $hpresults[$i]['exe_weighting']; + $return[$i]['lp_id'] = $result['orig_lp_id']; + $return[$i]['lp_name'] = $result['lp_name']; - $userWithResults[$results[$i]['excruid']] = 1; + $userWithResults[$result['excruid']] = 1; + $i++; } } @@ -253,50 +223,30 @@ public function getExercisesReporting( if (!in_array($student['user_id'], $userWithResults)) { $i = $latestId; $isWestern = api_is_western_name_order(); - if ($isHotPotato) { - $return[$i] = array(); - - if (empty($user_id)) { - $return[$i]['email'] = $student['email']; - if ($isWestern) { - $return[$i]['first_name'] = $student['firstname']; - $return[$i]['last_name'] = $student['lastname']; - } else { - $return[$i]['first_name'] = $student['lastname']; - $return[$i]['last_name'] = $student['firstname']; - } - } - $return[$i]['title'] = null; - $return[$i]['start_date'] = null; - $return[$i]['end_date'] = null; - $return[$i]['duration'] = null; - $return[$i]['result'] = null; - $return[$i]['max'] = null; - - } else { - if (empty($user_id)) { - $return[$i]['official_code'] = $student['official_code']; - if ($isWestern) { - $return[$i]['first_name'] = $student['firstname']; - $return[$i]['last_name'] = $student['lastname']; - } else { - $return[$i]['first_name'] = $student['lastname']; - $return[$i]['last_name'] = $student['firstname']; - } - - $return[$i]['user_id'] = $student['user_id']; - $return[$i]['email'] = $student['email']; + + if (empty($user_id)) { + $return[$i]['official_code'] = $student['official_code']; + if ($isWestern) { + $return[$i]['first_name'] = $student['firstname']; + $return[$i]['last_name'] = $student['lastname']; + } else { + $return[$i]['first_name'] = $student['lastname']; + $return[$i]['last_name'] = $student['firstname']; } - $return[$i]['title'] = null; - $return[$i]['start_date'] = null; - $return[$i]['end_date'] = null; - $return[$i]['duration'] = null; - $return[$i]['result'] = null; - $return[$i]['max'] = null; - $return[$i]['status'] = get_lang('NotAttempted'); - $return[$i]['lp_id'] = null; - $return[$i]['lp_name'] = null; + + $return[$i]['user_id'] = $student['user_id']; + $return[$i]['email'] = $student['email']; } + $return[$i]['title'] = null; + $return[$i]['start_date'] = null; + $return[$i]['end_date'] = null; + $return[$i]['duration'] = null; + $return[$i]['result'] = null; + $return[$i]['max'] = null; + $return[$i]['status'] = get_lang('NotAttempted'); + $return[$i]['lp_id'] = null; + $return[$i]['lp_name'] = null; + $latestId++; } } diff --git a/main/exercice/hotpotatoes_exercise_result.class.php b/main/exercice/hotpotatoes_exercise_result.class.php index 727b0e74e8b..dcf57be36dc 100755 --- a/main/exercice/hotpotatoes_exercise_result.class.php +++ b/main/exercice/hotpotatoes_exercise_result.class.php @@ -59,57 +59,57 @@ public function getExercisesReporting($document_path, $hotpotato_name) exe_cours_id = '" . Database :: escape_string($cid) . "' AND tth.exe_name = '$hotpotato_name' ORDER BY exe_cours_id ASC, exe_date ASC"; - } + } - $results = array(); + $results = array(); - $resx = Database::query($sql); - while ($rowx = Database::fetch_array($resx,'ASSOC')) { - $results[] = $rowx; - } + $resx = Database::query($sql); + while ($rowx = Database::fetch_array($resx,'ASSOC')) { + $results[] = $rowx; + } - $hpresults = array(); - $resx = Database::query($sql); - while ($rowx = Database::fetch_array($resx,'ASSOC')) { - $hpresults[] = $rowx; - } + $hpresults = array(); + $resx = Database::query($sql); + while ($rowx = Database::fetch_array($resx,'ASSOC')) { + $hpresults[] = $rowx; + } - /*if ($filter) { - switch ($filter) { - case 1 : - $filter_by_not_revised = true; - break; - case 2 : - $filter_by_revised = true; - break; - default : - null; + /*if ($filter) { + switch ($filter) { + case 1 : + $filter_by_not_revised = true; + break; + case 2 : + $filter_by_revised = true; + break; + default : + null; + } + }*/ + + // Print the Result of Hotpotatoes Tests + if (is_array($hpresults)) { + for($i = 0; $i < sizeof($hpresults); $i++) { + $return[$i] = array(); + $title = GetQuizName($hpresults[$i]['exe_name'], $document_path); + if ($title =='') { + $title = basename($hpresults[$i]['exe_name']); } - }*/ - - // Print the Result of Hotpotatoes Tests - if (is_array($hpresults)) { - for($i = 0; $i < sizeof($hpresults); $i++) { - $return[$i] = array(); - $title = GetQuizName($hpresults[$i]['exe_name'], $document_path); - if ($title =='') { - $title = basename($hpresults[$i]['exe_name']); - } - if(empty($user_id)) { - $return[$i]['email'] = $hpresults[$i]['email']; - $return[$i]['first_name'] = $hpresults[$i]['userpart1']; - $return[$i]['last_name'] = $hpresults[$i]['userpart2']; - } - $return[$i]['title'] = $title; - $return[$i]['exe_date'] = $hpresults[$i]['exe_date']; - - $return[$i]['result'] = $hpresults[$i]['exe_result']; - $return[$i]['max'] = $hpresults[$i]['exe_weighting']; + if(empty($user_id)) { + $return[$i]['email'] = $hpresults[$i]['email']; + $return[$i]['first_name'] = $hpresults[$i]['userpart1']; + $return[$i]['last_name'] = $hpresults[$i]['userpart2']; } + $return[$i]['title'] = $title; + $return[$i]['exe_date'] = $hpresults[$i]['exe_date']; + + $return[$i]['result'] = $hpresults[$i]['exe_result']; + $return[$i]['max'] = $hpresults[$i]['exe_weighting']; } - $this->results = $return; + } + $this->results = $return; - return true; + return true; }