From ce34ed3a764ba88559fa57fb6b2db013b167dedc Mon Sep 17 00:00:00 2001 From: toyomoyo Date: Thu, 24 May 2007 02:16:31 +0000 Subject: [PATCH] fix for MDL-9137, adding support for gradebook plugins for db, cron, events, permission structures etc --- admin/cron.php | 40 ++++++++++++++++++ admin/index.php | 4 ++ grade/export/lib.php | 40 +++++++++++------- grade/export/xml/index.php | 4 +- grade/import/xml/index.php | 86 ++++++++++++++++++++++++++++++++++++++ lib/accesslib.php | 11 ++++- lib/eventslib.php | 9 ++++ 7 files changed, 176 insertions(+), 18 deletions(-) create mode 100755 grade/import/xml/index.php diff --git a/admin/cron.php b/admin/cron.php index 213456a1e8..c3c17fb9a7 100644 --- a/admin/cron.php +++ b/admin/cron.php @@ -386,6 +386,46 @@ } } } + + // 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 (function_exists($cron_function)) { + mtrace("Processing gradebook import function $cron_function ...", ''); + $cron_function; + } + } + } + } + + 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 (function_exists($cron_function)) { + mtrace("Processing gradebook export function $cron_function ...", ''); + $cron_function; + } + } + } + } + + 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 (function_exists($cron_function)) { + mtrace("Processing gradebook report function $cron_function ...", ''); + $cron_function; + } + } + } + } //Unset session variables and destroy it @session_unset(); diff --git a/admin/index.php b/admin/index.php index 3bbd7a1188..9eda697019 100644 --- a/admin/index.php +++ b/admin/index.php @@ -424,6 +424,10 @@ require_once($CFG->dirroot.'/admin/mnet/adminlib.php'); upgrade_RPC_functions("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards +/// Upgrade all plugins for gradebook + upgrade_plugins('gradeexport', 'grade/export', "$CFG->wwwroot/$CFG->admin/index.php"); + upgrade_plugins('gradeimport', 'grade/import', "$CFG->wwwroot/$CFG->admin/index.php"); + upgrade_plugins('gradereport', 'grade/report', "$CFG->wwwroot/$CFG->admin/index.php"); /// just make sure upgrade logging is properly terminated upgrade_log_finish(); diff --git a/grade/export/lib.php b/grade/export/lib.php index bdf916d826..651389e0f1 100755 --- a/grade/export/lib.php +++ b/grade/export/lib.php @@ -22,7 +22,7 @@ // http://www.gnu.org/copyleft/gpl.html // // // /////////////////////////////////////////////////////////////////////////// -include_once('../../../config.php'); + include_once($CFG->dirroot.'/lib/gradelib.php'); include_once($CFG->dirroot.'/grade/lib.php'); /** @@ -30,10 +30,15 @@ include_once($CFG->dirroot.'/grade/lib.php'); * @input int id - course id */ function print_gradeitem_selections($id, $params = NULL) { - + global $CFG; // print all items for selections // make this a standard function in lib maybe - if ($grade_items = grade_get_items($id)) { + //if ($grade_items = grade_get_items($id)) { + include_once('grade_export_form.php'); + $mform = new grade_export_form(qualified_me(), array('id'=>$id)); + $mform->display(); + /* + echo '
'; echo '
'; foreach ($grade_items as $grade_item) { @@ -52,7 +57,8 @@ function print_gradeitem_selections($id, $params = NULL) { echo ''; echo '
'; echo '
'; - } + */ + //} } /** * Base export class @@ -155,16 +161,19 @@ class grade_export { $maxgrade = "$strmax: $gradeitem->grademax"; } else { $maxgrade = ""; - } + } if (!empty($this->students)) { foreach ($this->students as $student) { - + unset($studentgrade); // add support for comment here MDL-9634 - $studentgrade = $itemgrades[$student->id]; + + if (!empty($itemgrades[$student->id])) { + $studentgrade = $itemgrades[$student->id]; + } if (!empty($studentgrade->gradevalue)) { - $this->grades[$student->id][$gradeitem->id] = $currentstudentgrade = $studentgrade->gradevalue; + $this->grades[$student->id][$gradeitem->id] = $currentstudentgrade = $studentgrade->gradevalue; } else { $this->grades[$student->id][$gradeitem->id] = $currentstudentgrade = ""; $this->gradeshtml[$student->id][$gradeitem->id] = ""; @@ -175,13 +184,14 @@ class grade_export { $this->totals[$student->id] = (float)($this->totals[$student->id]) + 0; } - // load comments here - $studentgrade->load_text(); - // get the actual comment - $comment = $studentgrade->grade_grades_text->feedback; - - if (!empty($comment)) { - $this->comments[$student->id][$gradeitem->id] = $comment; + if (!empty($comment)) { + // load comments here + if ($studentgrade) { + $studentgrade->load_text(); + // get the actual comment + $comment = $studentgrade->grade_grades_text->feedback; + $this->comments[$student->id][$gradeitem->id] = $comment; + } } else { $this->comments[$student->id][$gradeitem->id] = ''; } diff --git a/grade/export/xml/index.php b/grade/export/xml/index.php index 6c05ef1841..280bf9a7a3 100755 --- a/grade/export/xml/index.php +++ b/grade/export/xml/index.php @@ -39,7 +39,7 @@ if (($data = data_submitted()) && confirm_sesskey()) { // print the grades on screen for feedbacks $course = get_record('course', 'id', $id); - $action = 'exporttxt'; + $action = 'exportxml'; print_header($course->shortname.': '.get_string('grades'), $course->fullname, grade_nav($course, $action)); $export = new grade_export($id, $data->itemids); @@ -47,7 +47,7 @@ if (($data = data_submitted()) && confirm_sesskey()) { // this redirect should trigger a download prompt redirect('export.php?id='.$id.'&itemids='.$itemidsurl); - exit; + exit; } $course = get_record('course', 'id', $id); $action = 'exportxml'; diff --git a/grade/import/xml/index.php b/grade/import/xml/index.php new file mode 100755 index 0000000000..4f6e3b8b75 --- /dev/null +++ b/grade/import/xml/index.php @@ -0,0 +1,86 @@ + 'somevalue', 'someotherfield' => 'someothervalue') ); +and then in your subclass, in definition, you can access +$this->_customdata['somefield'] +*/ + +require_once('../../../config.php'); + +// capability check +$id = required_param('id', PARAM_INT); // course id +// require_capability('moodle/site:uploadusers', get_context_instance(CONTEXT_SYSTEM)); + +require_once('../grade_import_form.php'); + + +require_once($CFG->dirroot.'/grade/lib.php'); +$course = get_record('course', 'id', $id); +$action = 'importxml'; +print_header($course->shortname.': '.get_string('grades'), $course->fullname, grade_nav($course, $action)); + +$mform = new grade_import_form(); + +//$mform2 = new grade_import_mapping_form(); + +//if ($formdata = $mform2->get_data() ) { + +if ( $formdata = $mform->get_data() ) { + + $filename = $mform->get_userfile_name(); + + // Large files are likely to take their time and memory. Let PHP know + // that we'll take longer, and that the process should be recycled soon + // to free up memory. + @set_time_limit(0); + @raise_memory_limit("192M"); + if (function_exists('apache_child_terminate')) { + @apache_child_terminate(); + } + + $text = my_file_get_contents($filename); + // trim utf-8 bom + $textlib = new textlib(); + $text = $textlib->trim_utf8_bom($text); + // Fix mac/dos newlines + $text = preg_replace('!\r\n?!',"\n",$text); + + // text is the text, we should xmlize it + include_once($CFG->dirroot.'/lib/xmlize.php'); + $content = xmlize($text); + + if ($results = $content['results']['#']['result']) { + foreach ($results as $result) { + unset ($eventdata); + + $eventdata->idnumber = $result['#']['assignment'][0]['#']; + $eventdata->userid = $result['#']['student'][0]['#']; + $eventdata->gradevalue = $result['#']['score'][0]['#']; + + trigger_event('grade_added', $eventdata); + echo "
triggering event for $eventdata->idnumber... student id is $eventdata->userid and grade is $eventdata->gradevalue"; + } + } + +} else { + $mform->display(); +} +print_footer(); + +function my_file_get_contents($filename, $use_include_path = 0) { + /// Returns the file as one big long string + + $data = ""; + $file = @fopen($filename, "rb", $use_include_path); + if ($file) { + while (!feof($file)) { + $data .= fread($file, 1024); + } + fclose($file); + } + return $data; +} +?> \ No newline at end of file diff --git a/lib/accesslib.php b/lib/accesslib.php index ec9fe5dcc3..0ae8659c21 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -2406,10 +2406,19 @@ function load_capability_def($component) { 's/'.$compparts[1].'/db/access.php'; $varprefix = $compparts[0].'_'.$compparts[1]; } else if ($compparts[0] == 'format') { - // Similar to the above, course formats are 'format' while they + // Similar to the above, course formats are 'format' while they // are stored in 'course/format'. $defpath = $CFG->dirroot.'/course/'.$component.'/db/access.php'; $varprefix = $compparts[0].'_'.$compparts[1]; + } else if ($compparts[0] == 'gradeimport') { + $defpath = $CFG->dirroot.'/grade/import/'.$component.'/db/access.php'; + $varprefix = $component; + } else if ($compparts[0] == 'gradeexport') { + $defpath = $CFG->dirroot.'/grade/export/'.$component.'/db/access.php'; + $varprefix = $component; + } else if ($compparts[0] == 'gradereport') { + $defpath = $CFG->dirroot.'/grade/report/'.$component.'/db/access.php'; + $varprefix = $component; } else { $defpath = $CFG->dirroot.'/'.$component.'/db/access.php'; $varprefix = str_replace('/', '_', $component); diff --git a/lib/eventslib.php b/lib/eventslib.php index 1620b6c184..3550962ba4 100755 --- a/lib/eventslib.php +++ b/lib/eventslib.php @@ -39,6 +39,15 @@ function events_load_def($component) { // are stored in 'course/format'. $defpath = $CFG->dirroot.'/course/format/'.$compparts[1].'/db/events.php'; + } else if ($compparts[0] == 'gradeimport') { + $defpath = $CFG->dirroot.'/grade/import/'.$compparts[1].'/db/events.php'; + + } else if ($compparts[0] == 'gradeexport') { + $defpath = $CFG->dirroot.'/grade/export/'.$compparts[1].'/db/events.php'; + + } else if ($compparts[0] == 'gradereport') { + $defpath = $CFG->dirroot.'/grade/report/'.$compparts[1].'/db/events.php'; + } else { $defpath = $CFG->dirroot.'/'.$component.'/db/events.php'; } -- 2.39.5