From 5bdacd72efe782069fcfdfa648fcdc23807afbe4 Mon Sep 17 00:00:00 2001 From: skodak Date: Thu, 8 Nov 2007 09:58:55 +0000 Subject: [PATCH] MFC: MDL-10365 grade history can be turned off + lifetime --- admin/cron.php | 26 ++++++------- admin/settings/server.php | 15 +++++++ lang/en_utf8/grades.php | 5 +++ lib/grade/grade_object.php | 80 ++++++++++++++++++++------------------ lib/gradelib.php | 14 +++++++ 5 files changed, 90 insertions(+), 50 deletions(-) diff --git a/admin/cron.php b/admin/cron.php index dbb0be0ce3e..ba149b4cfd6 100644 --- a/admin/cron.php +++ b/admin/cron.php @@ -189,9 +189,9 @@ } - mtrace('Starting grade job ...', ''); + mtrace('Starting main gradebook job ...'); grade_cron(); - mtrace('Done ...'); + mtrace('done.'); /// Run all core cron jobs, but not every time since they aren't too important. @@ -471,13 +471,13 @@ } } } - + // run gradebook import/export/report cron if ($gradeimports = get_list_of_plugins('grade/import')) { foreach ($gradeimports as $gradeimport) { - if (file_exists($CFG->dirroot.'/grade/import/lib.php')) { - require_once($CFG->dirroot.'/grade/import/lib.php'); - $cron_function = 'gradeimport_'.$gradeimport.'_cron'; + if (file_exists($CFG->dirroot.'/grade/import/'.$gradeimport.'/lib.php')) { + require_once($CFG->dirroot.'/grade/import/'.$gradeimport.'/lib.php'); + $cron_function = 'grade_import_'.$gradeimport.'_cron'; if (function_exists($cron_function)) { mtrace("Processing gradebook import function $cron_function ...", ''); $cron_function; @@ -488,9 +488,9 @@ if ($gradeexports = get_list_of_plugins('grade/export')) { foreach ($gradeexports as $gradeexport) { - if (file_exists($CFG->dirroot.'/grade/export/lib.php')) { - require_once($CFG->dirroot.'/grade/export/lib.php'); - $cron_function = 'gradeexport_'.$gradeexport.'_cron'; + if (file_exists($CFG->dirroot.'/grade/export/'.$gradeexport.'/lib.php')) { + require_once($CFG->dirroot.'/grade/export/'.$gradeexport.'/lib.php'); + $cron_function = 'grade_export_'.$gradeexport.'_cron'; if (function_exists($cron_function)) { mtrace("Processing gradebook export function $cron_function ...", ''); $cron_function; @@ -498,12 +498,12 @@ } } } - + if ($gradereports = get_list_of_plugins('grade/report')) { foreach ($gradereports as $gradereport) { - if (file_exists($CFG->dirroot.'/grade/report/lib.php')) { - require_once($CFG->dirroot.'/grade/report/lib.php'); - $cron_function = 'gradereport_'.$gradereport.'_cron'; + if (file_exists($CFG->dirroot.'/grade/report/'.$gradereport.'/lib.php')) { + require_once($CFG->dirroot.'/grade/report/'.$gradereport.'/lib.php'); + $cron_function = 'grade_report_'.$gradereport.'_cron'; if (function_exists($cron_function)) { mtrace("Processing gradebook report function $cron_function ...", ''); $cron_function; diff --git a/admin/settings/server.php b/admin/settings/server.php index b6cdd8b1bd5..8c2d9ebf7bc 100644 --- a/admin/settings/server.php +++ b/admin/settings/server.php @@ -182,6 +182,21 @@ $temp->add(new admin_setting_configselect('loglifetime', get_string('loglifetime 60 => get_string('numdays', '', 60), 30 => get_string('numdays', '', 30)))); + +$temp->add(new admin_setting_configcheckbox('disablegradehistory', get_string('disablegradehistory', 'grades'), + get_string('configdisablegradehistory', 'grades'), 0, PARAM_INT)); + +$temp->add(new admin_setting_configselect('gradehistorylifetime', get_string('gradehistorylifetime', 'grades'), + get_string('configgradehistorylifetime', 'grades'), 0, array(0 => get_string('neverdeletehistory', 'grades'), + 1000 => get_string('numdays', '', 1000), + 365 => get_string('numdays', '', 365), + 180 => get_string('numdays', '', 180), + 150 => get_string('numdays', '', 150), + 120 => get_string('numdays', '', 120), + 90 => get_string('numdays', '', 90), + 60 => get_string('numdays', '', 60), + 30 => get_string('numdays', '', 30)))); + $ADMIN->add('server', $temp); diff --git a/lang/en_utf8/grades.php b/lang/en_utf8/grades.php index 1d4b66f2d21..ede599f3eed 100644 --- a/lang/en_utf8/grades.php +++ b/lang/en_utf8/grades.php @@ -61,6 +61,7 @@ $string['categorytotal'] = 'Category total'; $string['changereportdefaults'] = 'Change report defaults'; $string['choosecategory'] = 'Select Category'; $string['compact'] = 'Compact'; +$string['configdisablegradehistory'] = 'Disable traking of changes in grades related tables. This may speed up the server operation and conserve space in database.'; $string['configaggregationposition'] = 'The position of the aggregation column in the report, in reference to the real grades.'; $string['configaggregationview'] = 'Each category can be displayed in three ways: Full mode (aggregated column and grade item columns), the aggregated column only, or the grade items alone.'; $string['configaveragesdecimalpoints'] = 'The number of decimal points to display for each average (group or whole), below a column of grades.'; @@ -74,6 +75,7 @@ $string['configgradeboundary'] = 'A percentage boundary over which grades will b $string['configgradedisplaytype'] = 'Grades can be shown as real grades, as percentages (in reference to the minimum and maximum grades) or as letters (A, B, C etc..)'; $string['configgradeexportdisplaytype'] = 'Grades can be shown as real grades, as percentages (in reference to the minimum and maximum grades) or as letters (A, B, C etc..) during export. This can be overriden during export.'; $string['configexportdecimalpoints'] = 'The number of decimal points to display for export. This can be overriden during export.'; +$string['configgradehistorylifetime'] = 'This specifies the length of time you want to keep history of changes in grade related tables. It is recommended to keep it as long as possible. If you experience performance problems or have limited database space, try to set lower value.'; $string['configgradeletter'] = 'A letter or other symbol used to represent a range of grades.'; $string['configgradeletterdefault'] = 'A letter or other symbol used to represent a range of grades. Leave this field empty to use the site default (currently $a).'; $string['configgradepublishing'] = 'Enable publishing in exports and imports: Exported grades can be accessed by accessing a URL, without having to log on to a Moodle site. Grades can be imported by accessing such a URL (which means that a moodle site can import grades published by another site). By default only administrators may use this feature, please educate users before adding required capabilities to other roles (dangers of bookmark sharing and download accelerators, IP restrictions, etc.).'; @@ -115,6 +117,7 @@ $string['decimalpoints'] = 'Overall decimal points'; $string['default'] = 'Default'; $string['defaultprev'] = 'Default ($a)'; $string['deletecategory'] = 'Delete Category'; +$string['disablegradehistory'] = 'Disable grade history'; $string['displaylettergrade'] = 'Display Letter Grades'; $string['displaypercent'] = 'Display Percents'; $string['displaypoints'] = 'Display Points'; @@ -180,6 +183,7 @@ $string['gradeexceptionshelp'] = 'Grade Exceptions Help'; $string['gradeexportdisplaytype'] = 'Grade export display type'; $string['gradeexportdecimalpoints'] = 'Grade export decimal points'; $string['gradehelp'] = 'Grade Help'; +$string['gradehistorylifetime'] = 'Grade history lifetime'; $string['gradeitem'] = 'Grade item'; $string['gradeitemislocked'] = 'This activity is locked in the gradebook. Changes that are made to grades in this activity will not be copied to the gradebook until it is unlocked.'; $string['gradeitemlocked'] = 'Grading locked'; @@ -282,6 +286,7 @@ $string['movingelement'] = 'Moving $a'; $string['multfactor'] = 'Multiplicator'; $string['multfactorhelp'] = 'Factor by which all grades for this grade item will be multiplied.'; $string['myreportpreferences'] = 'My report preferences'; +$string['neverdeletehistory'] = 'Never delete history'; $string['newcategory'] = 'New category'; $string['newuserkey'] = 'New user key'; $string['no'] = 'No'; diff --git a/lib/grade/grade_object.php b/lib/grade/grade_object.php index d31bf6f16ae..b730ee81afe 100644 --- a/lib/grade/grade_object.php +++ b/lib/grade/grade_object.php @@ -7,7 +7,7 @@ // Moodle - Modular Object-Oriented Dynamic Learning Environment // // http://moodle.com // // // -// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com // +// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com // // // // This program is free software; you can redistribute it and/or modify // // it under the terms of the GNU General Public License as published by // @@ -127,11 +127,9 @@ class grade_object { /** * Factory method - uses the parameters to retrieve matching instance from the DB. * @static final protected - * @return mixed object insatnce or false if not found + * @return mixed object instance or false if not found */ function fetch_helper($table, $classname, $params) { - // we have to do use this hack because of the incomplete OOP implementation in PHP4 :-( - // in PHP5 we could do it much better if ($instances = grade_object::fetch_all_helper($table, $classname, $params)) { if (count($instances) > 1) { // we should not tolerate any errors here - problems might appear later @@ -149,8 +147,6 @@ class grade_object { * @return mixed array of object instances or false if not found */ function fetch_all_helper($table, $classname, $params) { - // we have to do use this hack because of the incomplete OOP implementation in PHP4 :-( - // in PHP5 we could do it much better $instance = new $classname(); $classvars = (array)$instance; @@ -197,7 +193,12 @@ class grade_object { * @return boolean success */ function update($source=null) { - global $USER; + global $USER, $CFG; + + if (empty($this->id)) { + debugging('Can not update grade object, no id!'); + return false; + } $data = $this->get_record_data(); @@ -205,14 +206,15 @@ class grade_object { return false; } - // track history - // TODO: add history disable switch - unset($data->timecreated); - $data->action = GRADE_HISTORY_UPDATE; - $data->oldid = $this->id; - $data->source = $source; - $data->userlogged = $USER->id; - insert_record($this->table.'_history', addslashes_recursive($data)); + if (empty($CFG->disablegradehistory)) { + unset($data->timecreated); + $data->action = GRADE_HISTORY_UPDATE; + $data->oldid = $this->id; + $data->source = $source; + $data->timemodified = time(); + $data->userlogged = $USER->id; + insert_record($this->table.'_history', addslashes_recursive($data)); + } return true; } @@ -223,22 +225,24 @@ class grade_object { * @return boolean success */ function delete($source=null) { - global $USER; + global $USER, $CFG; - // track history - // TODO: add history disable switch - if ($data = get_record($this->table, 'id', $this->id)) { - unset($data->id); - unset($data->timecreated); - $data->action = GRADE_HISTORY_DELETE; - $data->oldid = $this->id; - $data->source = $source; - $data->timemodified = time(); - $data->userlogged = $USER->id; + if (empty($this->id)) { + debugging('Can not delete grade object, no id!'); + return false; } + $data = $this->get_record_data(); + if (delete_records($this->table, 'id', $this->id)) { - if ($data) { + if (empty($CFG->disablegradehistory)) { + unset($data->id); + unset($data->timecreated); + $data->action = GRADE_HISTORY_DELETE; + $data->oldid = $this->id; + $data->source = $source; + $data->timemodified = time(); + $data->userlogged = $USER->id; insert_record($this->table.'_history', addslashes_recursive($data)); } return true; @@ -274,7 +278,7 @@ class grade_object { * @return int PK ID if successful, false otherwise */ function insert($source=null) { - global $USER; + global $USER, $CFG; if (!empty($this->id)) { debugging("Grade object already exists!"); @@ -291,14 +295,17 @@ class grade_object { // set all object properties from real db data $this->update_from_db(); - // track history - // TODO: add history disable switch - unset($data->timecreated); - $data->action = GRADE_HISTORY_INSERT; - $data->oldid = $this->id; - $data->source = $source; - $data->userlogged = $USER->id; - insert_record($this->table.'_history', addslashes_recursive($data)); + $data = $this->get_record_data(); + + if (empty($CFG->disablegradehistory)) { + unset($data->timecreated); + $data->action = GRADE_HISTORY_INSERT; + $data->oldid = $this->id; + $data->source = $source; + $data->timemodified = time(); + $data->userlogged = $USER->id; + insert_record($this->table.'_history', addslashes_recursive($data)); + } return $this->id; } @@ -338,6 +345,5 @@ class grade_object { } } } - } ?> diff --git a/lib/gradelib.php b/lib/gradelib.php index 17469caf358..45788d644b0 100644 --- a/lib/gradelib.php +++ b/lib/gradelib.php @@ -1143,6 +1143,20 @@ function grade_cron() { rs_close($rs); } + //TODO: do not run this cleanup every cron invocation + // cleanup history tables + if (!empty($CFG->gradehistorylifetime)) { // value in days + $histlifetime = $now - ($CFG->gradehistorylifetime * 3600 * 24); + $tables = array('grade_outcomes_history', 'grade_categories_history', 'grade_items_history', 'grade_grades_history', 'scale_history'); + foreach ($tables as $table) { + if (delete_records_select($table, "timemodified < '$histlifetime'")) { + mtrace(" Deleted old grade history records from '$table'"); + } + + } + } + + } ?>