Browse Source

Clean all results for this test before the selected date -ref #6970

baelmyhu 11 years ago
parent
commit
a2b3c3c09d

+ 14 - 5
main/exercice/exercise.class.php

@@ -1482,11 +1482,13 @@ class Exercise {
 
     /**
      * Cleans the student's results only for the Exercise tool (Not from the LP)
-     * The LP results are NOT deleted
+     * The LP results are NOT deleted by default, otherwise put $clean_lp_tests = true
      * Works with exercises in sessions
+     * @param bool $clean_lp_tests
+     * @param int $clean_result_before_date
      * @return int quantity of user's exercises deleted
      */
-    function clean_results($clean_lp_tests = false) {
+    function clean_results($clean_lp_tests = false, $clean_result_before_date = '0') {
         $table_track_e_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
         $table_track_e_attempt   = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
 
@@ -1494,10 +1496,17 @@ class Exercise {
                         orig_lp_id = 0 AND
                         orig_lp_item_id = 0';
 
+        // if we want to delete results from LP too
         if ($clean_lp_tests) {
             $sql_where = "";
         }
 
+        // if we want to delete attempts before date $clean_result_before_date
+        // $clean_result_before_date must be a valid UTC-0 date yyyy-mm-dd
+        if ($clean_result_before_date != '0') {
+            $sql_where .= "  AND exe_date <= '$clean_result_before_date' ";
+        }
+
         $sql_select = "SELECT exe_id FROM $table_track_e_exercises
 					   WHERE 	exe_cours_id = '".api_get_course_id()."' AND
 								exe_exo_id = ".$this->id." AND
@@ -1507,7 +1516,8 @@ class Exercise {
         $result   = Database::query($sql_select);
         $exe_list = Database::store_result($result);
 
-        //deleting TRACK_E_ATTEMPT table
+        // deleting TRACK_E_ATTEMPT table
+        // check if exe in learning path or not
         $i = 0;
         if (is_array($exe_list) && count($exe_list) > 0) {
             foreach($exe_list as $item) {
@@ -1517,13 +1527,12 @@ class Exercise {
             }
         }
 
-        //delete TRACK_E_EXERCICES table
+        // delete TRACK_E_EXERCICES table
         $sql = "DELETE FROM $table_track_e_exercises
                 WHERE exe_cours_id = '".api_get_course_id()."'
                 AND exe_exo_id = ".$this->id."
                 $sql_where
                 AND session_id = ".api_get_session_id()."";
-
         Database::query($sql);
 
         return $i;

+ 100 - 6
main/exercice/exercise_report.php

@@ -23,6 +23,7 @@ require_once '../gradebook/lib/be.inc.php';
 $this_section = SECTION_COURSES;
 
 $htmlHeadXtra[] = api_get_jqgrid_js();
+$htmlHeadXtra[] = api_get_datepicker_js();
 
 // Access control
 api_protect_course_script(true, false, true);
@@ -221,7 +222,19 @@ if ($is_allowedToEdit && $origin != 'learnpath') {
         $actions .='<a href="stats.php?'.api_get_cidreq().'&exerciseId='.$exercise_id.'">'.Display :: return_icon('statistics.png', get_lang('ReportByQuestion'), '', ICON_SIZE_MEDIUM).'</a>';
         $actions .= '<a id="export_opener" href="'.api_get_self().'?export_report=1&hotpotato_name='.Security::remove_XSS($_GET['path']).'&exerciseId='.intval($_GET['exerciseId']).'" >'.
         Display::return_icon('save.png', get_lang('Export'), '', ICON_SIZE_MEDIUM).'</a>';
-        $actions .= Display::url(Display::return_icon('clean_before_date.png', get_lang('CleanStudentsResultsBeforeDate'), '', ICON_SIZE_MEDIUM), '', array('onclick' => "javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('AreYouSureToEmptyTestResults'), ENT_QUOTES, $charset))." ".addslashes($row['title'])."?"."')) return false;", 'href' => 'exercice_report.php?'.api_get_cidreq().'&choice=clean_all_test&sec_token='.$token));
+        // clean result before a selected date icon
+        $actions .= Display::url(
+            Display::return_icon('clean_before_date.png', get_lang('CleanStudentsResultsBeforeDate'), '', ICON_SIZE_MEDIUM), '', array('onclick' => "javascript:display_date_picker()", 'href' => '#')
+        );
+        // clean result before a selected date datepicker popup
+        $actions .= Display::span(
+            Display::input('input', 'datepicker_start', get_lang('SelectADateOnTheCalendar'),
+                array('onmouseover'=>'datepicker_input_mouseover()', 'id'=>'datepicker_start', 'onchange'=>'datepicker_input_changed()', 'readonly'=>'readonly')
+            ).
+            Display::button('delete', get_lang('Delete'),
+                array('onclick'=>'submit_datepicker()')),
+            array('style'=>'display:none', 'id'=>'datepicker_span')
+        );
     }
 } else {
     $actions .= '<a href="exercice.php">'.Display :: return_icon('back.png', get_lang('GoBackToQuestionList'), '', ICON_SIZE_MEDIUM).'</a>';
@@ -240,6 +253,7 @@ if (($is_allowedToEdit || $is_tutor || api_is_coach()) && $_GET['delete'] == 'de
     }
 }
 
+
 if ($is_allowedToEdit || $is_tutor) {
     $nameTools = get_lang('StudentScore');
     $interbreadcrumb[] = array("url" => "exercice.php?gradebook=$gradebook", "name" => get_lang('Exercices'));
@@ -257,6 +271,27 @@ if ($is_allowedToEdit || $is_tutor) {
 
 Display :: display_header($nameTools);
 
+// Clean all results for this test before the selected date
+$datepicker_language = get_datepicker_langage_code();
+if (($is_allowedToEdit || $is_tutor || api_is_coach()) && isset($_GET['delete_before_date']) && $locked == false) {
+    // ask for the date
+    $check = Security::check_token('get');
+    if ($check) {
+        // delete attempts before date $_GET['delete_before_date']
+        $dateUTC = api_get_utc_datetime($_GET['delete_before_date']);
+        if (api_is_valid_date($_GET['delete_before_date'])) {
+            $objExerciseTmp = new Exercise();
+            if ($objExerciseTmp->read($exercise_id)) {
+                $nb_del = $objExerciseTmp->clean_results(true, $_GET['delete_before_date']);
+                Display :: display_confirmation_message(sprintf(get_lang('XResultsCleaned'), $nb_del));
+            }
+        }
+    }
+}
+
+// Security token to protect deletion
+$token = Security::get_token();
+
 $actions = Display::div($actions, array('class' => 'actions'));
 
 $extra = '<script>
@@ -384,6 +419,7 @@ $extra_params['autowidth'] = 'true';
 //height auto
 $extra_params['height'] = 'auto';
 ?>
+
 <script>
 
     function setSearchSelect(columnName) {
@@ -426,11 +462,11 @@ $extra_params['height'] = 'auto';
     }
 
     $(function() {
-<?php
-echo Display::grid_js('results', $url, $columns, $column_model, $extra_params, array(), $action_links, true);
+        <?php
+        echo Display::grid_js('results', $url, $columns, $column_model, $extra_params, array(), $action_links, true);
 
-if ($is_allowedToEdit || $is_tutor) {
-    ?>
+        if ($is_allowedToEdit || $is_tutor) {
+            ?>
 
                 //setSearchSelect("status");
                 //
@@ -472,14 +508,72 @@ if ($is_allowedToEdit || $is_tutor) {
                 var sgrid = $("#results")[0];
                 sgrid.triggerToolbar();
 
-<?php } ?>
+        <?php } ?>
+        });
+
+        // datepicker functions
+        var datapickerInputModified = false;
+
+        /**
+         * return true if the datepicker input has been modified
+         */
+        function datepicker_input_changed() {
+            datapickerInputModified = true;
+        }
+
+        /**
+        * disply the datepicker calendar on mouse over the input
+        */
+        function datepicker_input_mouseover() {
+            $('#datepicker_start').datepicker( "show" );
+        }
+
+        /**
+        * display or hide the datepicker input, calendar and button
+        */
+        function display_date_picker() {
+            $('#datepicker_span').toggle();
+            $('#datepicker_start').datepicker( "show" );
+        }
+
+        /**
+        * confirm deletion
+        */
+        function submit_datepicker() {
+            if (datapickerInputModified) {
+                var dateTypeVar = $('#datepicker_start').datepicker('getDate');
+                var dateForBDD = $.datepicker.formatDate('yy-mm-dd', dateTypeVar);
+                // format the date for confirm box
+                var selectedDate = $.datepicker.formatDate( "DD, MM d, yy", dateTypeVar, {
+                    dayNamesShort: $.datepicker.regional[ "<?php echo $datepicker_language; ?>" ].dayNamesShort,
+                    dayNames: $.datepicker.regional[ "<?php echo $datepicker_language; ?>" ].dayNames,
+                    monthNamesShort: $.datepicker.regional[ "<?php echo $datepicker_language; ?>" ].monthNamesShort,
+                    monthNames: $.datepicker.regional[ "<?php echo $datepicker_language; ?>" ].monthNames
+                });
+                if (confirm("<?php echo convert_double_quote_to_single(get_lang('AreYouSureDeleteTestResultBeforeDateD')) ?>"+" "+selectedDate)) {
+                    self.location.href = "exercise_report.php?<?php echo api_get_cidreq(); ?>&exerciseId=<?php echo $exercise_id; ?>&delete_before_date="+dateForBDD+"&sec_token=<?php echo $token; ?>";
+                }
+            }
+        }
+
+        /**
+        * initiate datepicker
+        */
+        $(function(){
+            $( "#datepicker_start" ).datepicker({
+                defaultDate: "",
+                changeMonth: false,
+                numberOfMonths: 1
+            });
         });
+
 </script>
 <form id="export_report_form" method="post" action="exercise_report.php">
     <input type="hidden" name="csvBuffer" id="csvBuffer" value="" />
     <input type="hidden" name="export_report" id="export_report" value="1" />
     <input type="hidden" name="exerciseId" id="exerciseId" value="<?php echo $exercise_id ?>" />
 </form>
+
 <?php
 echo Display::grid_html('results');
 Display :: display_footer();

BIN
main/img/icons/32/clean_before_date.png


+ 33 - 0
main/inc/lib/internationalization.lib.php

@@ -3812,6 +3812,39 @@ function setting_gettext() {
     textdomain($domain);
 }
 
+/**
+ * Return true is $in_date is a valid date
+ * date format yyyy-mm-dd hh:mm:ss
+ * @param $in_date
+ */
+function api_is_valid_date($in_date) {
+    if (preg_match('/^([0-9]{4})-([0-1][0-9])-([0-3][0-9])( ([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/', $in_date, $tab_matches)) {
+        if (checkdate($tab_matches[2], $tab_matches[3], $tab_matches[1])
+            && (($tab_matches[5] <= 23 && $tab_matches[6] <= 59 && $tab_matches[7] <= 59)
+                || ($tab_matches[5] == 24 && $tab_matches[6] == '00' && $tab_matches[7] == '00'))) {
+            return true;
+        }
+    }
+    return false;
+}
+
+/**
+ * Return the encoding country code for jquery datepicker
+ * used for exemple in main/exercice/exercise_report.php
+ */
+function get_datepicker_langage_code() {
+    $languaje   = 'en-GB';
+    $platform_isocode = strtolower(api_get_language_isocode());
+
+    // languages supported by jqgrid see files in main/inc/lib/javascript/jqgrid/js/i18n
+    $datapicker_langs = array('af', 'ar', 'ar-DZ', 'az', 'bg', 'bs', 'ca', 'cs', 'cy-GB', 'da', 'de', 'el', 'en-AU', 'en-GB', 'en-NZ', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fo', 'fr', 'fr-CH', 'gl', 'he', 'hi', 'hr', 'hu', 'hy', 'id', 'is', 'it', 'ja', 'ka', 'kk', 'km', 'ko', 'lb', 'lt', 'lv', 'mk', 'ml', 'ms', 'nl', 'nl-BE', 'no', 'pl', 'pt', 'pt-BR', 'rm', 'ro', 'ru', 'sk', 'sl', 'sq', 'sr', 'sr-SR', 'sv', 'ta', 'th', 'tj', 'tr', 'uk', 'vi', 'zh-CN', 'zh-HK', 'zh-TW');
+    if (in_array($platform_isocode, $datapicker_langs)) {
+        $languaje = $platform_isocode;
+    }
+    return $languaje;
+}
+
+
 /**
  * Functions for internal use behind this API
  */

+ 34 - 0
main/inc/lib/main_api.lib.php

@@ -5894,6 +5894,10 @@ function api_get_jqgrid_js() {
     return api_get_jquery_libraries_js(array('jqgrid'));
 }
 
+function api_get_datepicker_js() {
+    return api_get_jquery_libraries_js(array('datepicker'));
+}
+
 /**
  * Returns the jquery library js and css headers
  *
@@ -5956,9 +5960,28 @@ function api_get_jquery_libraries_js($libraries) {
     	$js .= api_get_js('bxslider/jquery.bxSlider.min.js');
     	$js .= api_get_css($js_path.'bxslider/bx_styles/bx_styles.css');
     }
+
+    // jquery datepicker
+    if (in_array('datepicker',$libraries)) {
+        $languaje   = 'en-GB';
+        $platform_isocode = strtolower(api_get_language_isocode());
+
+        // languages supported by jqgrid see files in main/inc/lib/javascript/jqgrid/js/i18n
+        $datapicker_langs = array('af', 'ar', 'ar-DZ', 'az', 'bg', 'bs', 'ca', 'cs', 'cy-GB', 'da', 'de', 'el', 'en-AU', 'en-GB', 'en-NZ', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fo', 'fr', 'fr-CH', 'gl', 'he', 'hi', 'hr', 'hu', 'hy', 'id', 'is', 'it', 'ja', 'ka', 'kk', 'km', 'ko', 'lb', 'lt', 'lv', 'mk', 'ml', 'ms', 'nl', 'nl-BE', 'no', 'pl', 'pt', 'pt-BR', 'rm', 'ro', 'ru', 'sk', 'sl', 'sq', 'sr', 'sr-SR', 'sv', 'ta', 'th', 'tj', 'tr', 'uk', 'vi', 'zh-CN', 'zh-HK', 'zh-TW');
+        if (in_array($platform_isocode, $datapicker_langs)) {
+            $languaje = $platform_isocode;
+        }
+//        $js .= api_get_css($js_path.'jqgrid/css/ui.jqgrid.css');
+        $js .= api_get_js('jquery-ui/ui/i18n/jquery.ui.datepicker-'.$languaje.'.js');
+    }
+
     return $js;
 }
 
+
+
+
+
 /**
  * Returns the course's URL
  *
@@ -6941,3 +6964,14 @@ function api_get_protocol()
 {
     return api_is_https() ? 'https' : 'http';
 }
+
+/**
+ * Return a string where " are replaced with 2 '
+ * It is usefull when you pass a PHP variable in a Javascript browser dialog
+ * e.g. : alert("<?php get_lang('message') ?>");
+ * and message contains character "
+ * @param $in_text
+ */
+function convert_double_quote_to_single($in_text) {
+    return api_preg_replace('/"/', "''", $in_text);
+}