From c46aeeab9c281fe49680d21638e1f83d2b9cc02d Mon Sep 17 00:00:00 2001 From: Dongsheng Cai Date: Tue, 8 Jun 2010 05:23:44 +0000 Subject: [PATCH] MDL-22574, fixed outcomes import form --- grade/edit/outcome/import.php | 55 ++++++++++----------- grade/edit/outcome/import_outcomes_form.php | 12 ++--- grade/edit/outcome/index.php | 12 ----- grade/lib.php | 15 +++--- 4 files changed, 40 insertions(+), 54 deletions(-) diff --git a/grade/edit/outcome/import.php b/grade/edit/outcome/import.php index d34307ee700..f0bc6461d76 100644 --- a/grade/edit/outcome/import.php +++ b/grade/edit/outcome/import.php @@ -15,15 +15,18 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/// THIS SCRIPT IS CALLED WITH "require_once()" FROM index.php -if (!defined('MOODLE_INTERNAL')) { - die('Direct access to this script is forbidden.'); -} +require_once(dirname(__FILE__).'/../../../config.php'); +require_once($CFG->dirroot.'/lib/formslib.php'); +require_once($CFG->dirroot.'/grade/lib.php'); +require_once($CFG->libdir.'/gradelib.php'); +require_once('import_outcomes_form.php'); -$courseid = optional_param('id', 0, PARAM_INT); +$courseid = optional_param('courseid', 0, PARAM_INT); $action = optional_param('action', '', PARAM_ALPHA); $scope = optional_param('scope', 'global', PARAM_ALPHA); +$PAGE->set_url('/grade/edit/outcome/import.php', array('courseid' => $courseid)); + /// Make sure they can even access this course if ($courseid) { if (!$course = $DB->get_record('course', array('id' => $courseid))) { @@ -46,31 +49,27 @@ require_capability('moodle/grade:manageoutcomes', $context); $navigation = grade_build_nav(__FILE__, get_string('outcomes', 'grades'), $courseid); -if (!confirm_sesskey()) { - break; -} +$upload_form = new import_outcomes_form(); -$systemcontext = get_context_instance(CONTEXT_SYSTEM); -$caneditsystemscales = has_capability('moodle/course:managescales', $systemcontext); - -if ($courseid) { +// display import form +if (!$upload_form->get_data()) { print_grade_page_head($courseid, 'outcome', 'import', get_string('importoutcomes', 'grades')); - - $caneditcoursescales = has_capability('moodle/course:managescales', $context); - -} else { - echo $OUTPUT->header(); - $caneditcoursescales = $caneditsystemscales; + $upload_form->display(); + echo $OUTPUT->footer(); + die; } +print_grade_page_head($courseid, 'outcome', 'import', get_string('importoutcomes', 'grades')); -$imported_file = $upload_form->_upload_manager->files; +$imported_file = $CFG->dataroot . '/temp/outcomeimport/importedfile_'.time().'.csv'; +make_upload_directory('temp/outcomeimport'); -if ($imported_file['userfile']['size'] == 0) { - redirect('index.php'. ($courseid ? "?id=$courseid" : ''), get_string('importfilemissing', 'grades')); +// copying imported file +if (!$upload_form->save_file('userfile', $imported_file, true)) { + redirect('import.php'. ($courseid ? "?courseid=$courseid" : ''), get_string('importfilemissing', 'grades')); } /// which scope are we importing the outcomes in? -if (isset($courseid) && ($scope == 'local')) { +if (isset($courseid) && ($scope == 'custom')) { // custom scale $local_scope = true; } elseif (($scope == 'global') && has_capability('moodle/grade:manage', get_context_instance(CONTEXT_SYSTEM))) { @@ -82,7 +81,7 @@ if (isset($courseid) && ($scope == 'local')) { } // open the file, start importing data -if ($handle = fopen($imported_file['userfile']['tmp_name'], 'r')) { +if ($handle = fopen($imported_file, 'r')) { $line = 0; // will keep track of current line, to give better error messages. $file_headers = ''; @@ -119,7 +118,7 @@ if ($handle = fopen($imported_file['userfile']['tmp_name'], 'r')) { if ($error) { echo $OUTPUT->box_start('generalbox importoutcomenofile buttons'); echo get_string('importoutcomenofile', 'grades', $line); - echo $OUTPUT->single_button(new moodle_url('/grade/edit/outcome/index.php', array('id'=> $courseid)), get_string('back'), 'get'); + echo $OUTPUT->single_button(new moodle_url('/grade/edit/outcome/import.php', array('courseid'=> $courseid)), get_string('back'), 'get'); echo $OUTPUT->box_end(); $fatal_error = true; break; @@ -138,7 +137,7 @@ if ($handle = fopen($imported_file['userfile']['tmp_name'], 'r')) { if ( count($csv_data) != count($file_headers) ) { echo $OUTPUT->box_start('generalbox importoutcomenofile'); echo get_string('importoutcomenofile', 'grades', $line); - echo $OUTPUT->single_button(new moodle_url('/grade/edit/outcome/index.php', array('id'=> $courseid)), get_string('back'), 'get'); + echo $OUTPUT->single_button(new moodle_url('/grade/edit/outcome/import.php', array('courseid'=> $courseid)), get_string('back'), 'get'); echo $OUTPUT->box_end(); $fatal_error = true; //echo $OUTPUT->box(var_export($csv_data, true) ."
". var_export($header, true)); @@ -150,15 +149,13 @@ if ($handle = fopen($imported_file['userfile']['tmp_name'], 'r')) { if ($csv_data[$imported_headers[$header]] == '') { echo $OUTPUT->box_start('generalbox importoutcomenofile'); echo get_string('importoutcomenofile', 'grades', $line); - echo $OUTPUT->single_button(new moodle_url('/grade/edit/outcome/index.php', array('id'=> $courseid)), get_string('back'), 'get'); + echo $OUTPUT->single_button(new moodle_url('/grade/edit/outcome/import.php', array('courseid'=> $courseid)), get_string('back'), 'get'); echo $OUTPUT->box_end(); $fatal_error = true; break; } } - //var_dump($csv_data); - // MDL-17273 errors in csv are not preventing import from happening. We break from the while loop here if ($fatal_error) { break; @@ -236,5 +233,7 @@ if ($handle = fopen($imported_file['userfile']['tmp_name'], 'r')) { // finish fclose($handle); +// delete temp file +unlink($imported_file); echo $OUTPUT->footer(); diff --git a/grade/edit/outcome/import_outcomes_form.php b/grade/edit/outcome/import_outcomes_form.php index 18ceceef4d8..0737f034ed1 100644 --- a/grade/edit/outcome/import_outcomes_form.php +++ b/grade/edit/outcome/import_outcomes_form.php @@ -24,25 +24,25 @@ require_once($CFG->dirroot.'/lib/formslib.php'); class import_outcomes_form extends moodleform { public function definition() { - global $COURSE, $USER; + global $PAGE, $USER; $mform =& $this->_form; - //$this->set_upload_manager(new upload_manager('importfile', false, false, null, false, 0, true, true, false)); $mform->addElement('hidden', 'action', 'upload'); $mform->setType('action', PARAM_ACTION); - $mform->addElement('hidden', 'id', $COURSE->id); + $mform->addElement('hidden', 'courseid', $PAGE->course->id); $mform->setType('id', PARAM_INT); $scope = array(); - if (($COURSE->id > 1) && has_capability('moodle/grade:manage', get_context_instance(CONTEXT_SYSTEM))) { + if (($PAGE->course->id > 1) && has_capability('moodle/grade:manage', get_context_instance(CONTEXT_SYSTEM))) { $mform->addElement('radio', 'scope', get_string('importcustom', 'grades'), null, 'custom'); $mform->addElement('radio', 'scope', get_string('importstandard', 'grades'), null, 'global'); $mform->setDefault('scope', 'custom'); } - $mform->addElement('file', 'userfile', get_string('importoutcomes', 'grades')); - $mform->addHelpButton('userfile', 'importoutcomes', 'grades'); + $mform->addElement('filepicker', 'userfile', get_string('importoutcomes', 'grades')); + $mform->addRule('userfile', get_string('required'), 'required', null, 'server'); + $mform->setHelpButton('userfile', array('importoutcomes', get_string('importoutcomes', 'grades'), 'grade')); $mform->addElement('submit', 'save', get_string('uploadthisfile')); diff --git a/grade/edit/outcome/index.php b/grade/edit/outcome/index.php index 9caa2d5a27f..acf65d3912c 100644 --- a/grade/edit/outcome/index.php +++ b/grade/edit/outcome/index.php @@ -24,7 +24,6 @@ */ require_once(dirname(__FILE__).'/../../../config.php'); -require_once($CFG->dirroot.'/lib/formslib.php'); require_once($CFG->dirroot.'/grade/lib.php'); require_once($CFG->libdir.'/gradelib.php'); @@ -57,14 +56,6 @@ if ($courseid) { /// return tracking object $gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'outcome', 'courseid'=>$courseid)); -require_once('import_outcomes_form.php'); -$upload_form = new import_outcomes_form(); - -if ($upload_form_data = $upload_form->get_data()) { - require_once('import.php'); - exit(); -} - $strgrades = get_string('grades'); $pagename = get_string('outcomes', 'grades'); @@ -253,8 +244,6 @@ if ( !empty($outcomes_tables) ) { } echo $OUTPUT->container_end(); -$upload_form->display(); - echo $OUTPUT->footer(); /** @@ -270,4 +259,3 @@ function grade_print_scale_link($courseid, $scale, $gpr) { $url = $gpr->add_url_params($url); return html_writer::link($url, $scale->get_name()); } - diff --git a/grade/lib.php b/grade/lib.php index 90ad3650a2f..33fac625bff 100644 --- a/grade/lib.php +++ b/grade/lib.php @@ -472,7 +472,7 @@ function grade_get_plugin_info($courseid, $active_type, $active_plugin) { if ($reports = grade_helper::get_plugins_reports($courseid)) { $plugin_info['report'] = $reports; } - + if ($edittree = grade_helper::get_info_edit_structure($courseid)) { $plugin_info['edittree'] = $edittree; } @@ -483,9 +483,6 @@ function grade_get_plugin_info($courseid, $active_type, $active_plugin) { if ($outcomes = grade_helper::get_info_outcomes($courseid)) { $plugin_info['outcome'] = $outcomes; - if ($active_type == 'outcome' && $active_plugin == 'import') { - $plugin_info['outcome']['import'] = new grade_plugin_info('import', null, get_string('importoutcomes', 'grades')); - } } if ($letters = grade_helper::get_info_letters($courseid)) { @@ -495,7 +492,7 @@ function grade_get_plugin_info($courseid, $active_type, $active_plugin) { if ($imports = grade_helper::get_plugins_import($courseid)) { $plugin_info['import'] = $imports; } - + if ($exports = grade_helper::get_plugins_export($courseid)) { $plugin_info['export'] = $exports; } @@ -513,7 +510,7 @@ function grade_get_plugin_info($courseid, $active_type, $active_plugin) { } } } - + // Put settings last if ($setting = grade_helper::get_info_manage_settings($courseid)) { $plugin_info['settings'] = array('course'=>$setting); @@ -615,9 +612,9 @@ function print_grade_page_head($courseid, $active_type, $active_plugin=null, $heading = false, $return=false, $buttons=false) { global $CFG, $OUTPUT, $PAGE; - + $plugin_info = grade_get_plugin_info($courseid, $active_type, $active_plugin); - + // Determine the string of the active plugin $stractive_plugin = ($active_plugin) ? $plugin_info['strings']['active_plugin_str'] : $heading; $stractive_type = $plugin_info['strings'][$active_type]; @@ -2297,6 +2294,8 @@ abstract class grade_helper { $outcomes['course'] = new grade_plugin_info('course', $url, get_string('outcomescourse', 'grades')); $url = new moodle_url('/grade/edit/outcome/index.php', array('id'=>$courseid)); $outcomes['edit'] = new grade_plugin_info('edit', $url, get_string('editoutcomes', 'grades')); + $url = new moodle_url('/grade/edit/outcome/import.php', array('courseid'=>$courseid)); + $outcomes['import'] = new grade_plugin_info('import', $url, get_string('importoutcomes', 'grades')); } else { $url = new moodle_url('/grade/edit/outcome/course.php', array('id'=>$courseid)); $outcomes['edit'] = new grade_plugin_info('edit', $url, get_string('outcomescourse', 'grades'));