grade import/export fixes to use forms lib

This commit is contained in:
toyomoyo 2007-05-24 08:57:36 +00:00
parent 2186f72cb1
commit 3f8bcf7c72
7 changed files with 141 additions and 122 deletions

View File

@ -0,0 +1,21 @@
<?php // $Id$
require_once $CFG->libdir.'/formslib.php';
class grade_export_form extends moodleform {
function definition (){
global $CFG;
include_once($CFG->libdir.'/pear/HTML/QuickForm/advcheckbox.php');
$mform =& $this->_form;
$id = $this->_customdata['id']; // course id
$mform->addElement('hidden', 'id', $id);
if ($grade_items = grade_get_items($id)) {
foreach ($grade_items as $grade_item) {
$element = new HTML_QuickForm_advcheckbox('itemids[]', null, $grade_item->itemname, array('selected'=>'selected'), array(0, $grade_item->id));
$element->setChecked(1);
$mform->addElement($element);
}
}
$this->add_action_buttons(false, get_string('submit'));
}
}
?>

View File

@ -33,32 +33,10 @@ 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)) {
include_once('grade_export_form.php');
$mform = new grade_export_form(qualified_me(), array('id'=>$id));
$mform->display();
/*
echo '<form action="index.php" method="post">';
echo '<div>';
foreach ($grade_items as $grade_item) {
echo '<br/><input type="checkbox" name="itemids[]" value="'.$grade_item->id.'" checked="checked"/>';
if ($grade_item->itemtype == 'category') {
// grade categories should be displayed bold
echo '<b>'.$grade_item->itemname.'</b>';
} else {
echo $grade_item->itemname;
}
}
echo '<input type="hidden" name="id" value="'.$id.'"/>';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'"/>';
echo '<input type="submit" value="'.get_string('submit').'" />';
echo '</div>';
echo '</form>';
*/
//}
include_once('grade_export_form.php');
$mform = new grade_export_form(qualified_me(), array('id'=>$id));
$mform->display();
}
/**
* Base export class

View File

@ -0,0 +1,29 @@
<?php // $Id$
require_once $CFG->libdir.'/formslib.php';
class grade_export_txt_form extends moodleform {
function definition (){
global $CFG;
include_once($CFG->libdir.'/pear/HTML/QuickForm/advcheckbox.php');
$mform =& $this->_form;
$id = $this->_customdata['id']; // course id
$mform->addElement('hidden', 'id', $id);
if ($grade_items = grade_get_items($id)) {
foreach ($grade_items as $grade_item) {
$element = new HTML_QuickForm_advcheckbox('itemids[]', null, $grade_item->itemname, array('selected'=>'selected'), array(0, $grade_item->id));
$element->setChecked(1);
$mform->addElement($element);
}
}
include_once($CFG->libdir.'/pear/HTML/QuickForm/radio.php');
$radio = array();
$radio[] = &MoodleQuickForm::createElement('radio', 'separator', null, get_string('tab'), 'tab');
$radio[] = &MoodleQuickForm::createElement('radio', 'separator', null, get_string('comma'), 'comma');
$mform->addGroup($radio, 'separator', get_string('separator'), ' ', false);
$mform->setDefault('separator', 'comma');
$this->add_action_buttons(false, get_string('submit'));
}
}
?>

View File

@ -53,7 +53,11 @@ if (($data = data_submitted()) && confirm_sesskey()) {
$course = get_record('course', 'id', $id);
$action = 'exporttxt';
print_header($course->shortname.': '.get_string('grades'), $course->fullname, grade_nav($course, $action));
include_once('grade_export_txt_form.php');
$mform = new grade_export_txt_form(qualified_me(), array('id'=>$id));
$mform->display();
/*
// print_gradeitem_selections($id);
// print all items for selections
// make this a standard function in lib maybe
@ -81,6 +85,6 @@ if ($grade_items = grade_get_items($id)) {
echo '</div>';
echo '</form>';
}
*/
print_footer();
?>

6
grade/export/txt/version.php Executable file
View File

@ -0,0 +1,6 @@
<?php
$plugin->version = 2007052201;
$plugin->requires = 2007051801;
?>

View File

@ -1,13 +1,5 @@
<?php
/*
This is development code, and it is not finished
$form = new custom_form_subclass(qualified_me(), array('somefield' => 'somevalue', 'someotherfield' => 'someothervalue') );
and then in your subclass, in definition, you can access
$this->_customdata['somefield']
*/
require_once('../../../config.php');
// capability check
@ -28,25 +20,20 @@ if (isset($CFG->CSV_DELIMITER)) {
}
require_once('../grade_import_form.php');
require_once($CFG->dirroot.'/grade/lib.php');
$course = get_record('course', 'id', $id);
$action = 'importcsv';
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 = data_submitted()) && !empty($formdata->map)) {
// mapping info here
// reconstruct the mapping
print_object($formdata);
// i am not able to get the mapping[] and map[] array using the following line
// they are somehow not returned with get_data()
// if ($formdata = $mform2->get_data()) {
foreach ($formdata->maps as $i=>$header) {
$map[$header] = $formdata->mapping[$i];
@ -57,9 +44,9 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
// temporary file name supplied by form
$filename = $CFG->dataroot.'/temp/'.$formdata->filename;
// 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.
// 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')) {
@ -76,7 +63,6 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
foreach ($header as $i => $h) {
$h = trim($h); $header[$i] = $h; // remove whitespace
// flag events to add columns if needed (?)
}
while (!feof ($fp)) {
@ -86,7 +72,7 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
// each line is a student record
unset ($studentid);
unset ($studentgrades);
print_object($map);
foreach ($line as $key => $value) {
//decode encoded commas
@ -125,8 +111,7 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
$eventdata->gradevalue = $studentgrade;
events_trigger('grade_added', $eventdata);
echo "<br/>triggering event for $idnumber... student id is $studentid and grade is $studentgrade";
debugging("triggering event for $idnumber... student id is $studentid and grade is $studentgrade");
}
}
}
@ -137,7 +122,7 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
error ('import file '.$filename.' not readable');
}
} else if ( $formdata = $mform->get_data() ) {
} else if ($formdata = $mform->get_data() ) {
$filename = $mform->get_userfile_name();
@ -165,56 +150,9 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
// --- get header (field names) ---
$header = split($csv_delimiter, fgets($fp,1024));
echo '<form action="index.php" method="post" />';
$mapfromoptions = array();
foreach ($header as $h) {
$mapfromoptions[$h] = $h;
}
choose_from_menu($mapfromoptions, 'mapfrom');
// one mapfrom (csv column) to mapto (one of 4 choices)
$maptooptions = array('userid'=>'userid', 'username'=>'username', 'useridnumber'=>'useridnumber', 'useremail'=>'useremail', '0'=>'ignore');
choose_from_menu($maptooptions, 'mapto');
$gradeitems = array();
include_once($CFG->libdir.'/gradelib.php');
if ($grade_items = grade_get_items($id)) {
foreach ($grade_items as $grade_item) {
$gradeitems[$grade_item->idnumber] = $grade_item->itemname;
}
}
foreach ($header as $h) {
$h = trim($h);
// this is the order of the headers
echo "<br/> this field is :".$h." => ";
echo '<input type="hidden" name="maps[]" value="'.$h.'"/>';
// this is what they map to
$mapfromoptions = array_merge(array('0'=>'ignore'), $gradeitems);
choose_from_menu($mapfromoptions, 'mapping[]', $h);
}
$newfilename = 'cvstemp_'.time();
move_uploaded_file($filename, $CFG->dataroot.'/temp/'.$newfilename);
echo '<input type="hidden" name="map" value="1"/>';
echo '<input type="hidden" name="id" value="'.$id.'"/>';
echo '<input name="filename" value='.$newfilename.' type="hidden" />';
echo '<input type="submit" value="upload" />';
echo '</form>';
// set the headers
//$mform2->setup($header, $filename);
//$mform2->display();
// move file to $CFG->dataroot/temp
// print mapping form
$mform2 = new grade_import_mapping_form(qualified_me(), array('id'=>$id, 'header'=>$header, 'filename'=>$filename));
$mform2->display();
} else {
$mform->display();
}

View File

@ -25,32 +25,75 @@ class grade_import_form extends moodleform {
}
}
class grade_import_mapping_form extends moodleform {
function definition () {
$mform =& $this->_form;
// course id needs to be passed for auth purposes
$mform->addElement('hidden', 'id', optional_param('id'));
$this->add_action_buttons(false, get_string('uploadgrades'));
}
function setup ($headers = '', $filename = '') {
function definition () {
global $CFG;
$mform =& $this->_form;
if (is_array($headers)) {
foreach ($headers as $header) {
$mform->addElement('hidden', $header, $header);
$mform->addRule($header, null, 'required');
// this is an array of headers
$header = $this->_customdata['header'];
// temporary filename
$filename = $this->_customdata['filename'];
// course id
$id = $this->_customdata['id'];
$mform->addElement('header', 'general', get_string('identifier'));
$mapfromoptions = array();
if ($header) {
foreach ($header as $h) {
$mapfromoptions[$h] = $h;
}
}
$mform->addElement('select', 'mapfrom', get_string('mapfrom'), $mapfromoptions);
//choose_from_menu($mapfromoptions, 'mapfrom');
$maptooptions = array('userid'=>'userid', 'username'=>'username', 'useridnumber'=>'useridnumber', 'useremail'=>'useremail', '0'=>'ignore');
//choose_from_menu($maptooptions, 'mapto');
$mform->addElement('select', 'mapto', get_string('mapto'), $maptooptions);
$mform->addElement('header', 'general', get_string('mappings'));
$gradeitems = array();
include_once($CFG->libdir.'/gradelib.php');
if ($id) {
if ($grade_items = grade_get_items($id)) {
foreach ($grade_items as $grade_item) {
$gradeitems[$grade_item->idnumber] = $grade_item->itemname;
}
}
}
if ($filename) {
$mform->addElement('hidden', 'filename', $filename);
$mform->addRule('filename', null, 'required');
}
if ($header) {
foreach ($header as $h) {
$h = trim($h);
// this is the order of the headers
$mform->addElement('hidden', 'maps[]', $h);
//echo '<input type="hidden" name="maps[]" value="'.$h.'"/>';
// this is what they map to
$mapfromoptions = array_merge(array('0'=>'ignore'), $gradeitems);
$mform->addElement('select', 'mapping[]', $h, $mapfromoptions);
//choose_from_menu($mapfromoptions, 'mapping[]', $h);
}
}
$newfilename = 'cvstemp_'.time();
move_uploaded_file($filename, $CFG->dataroot.'/temp/'.$newfilename);
// course id needs to be passed for auth purposes
$mform->addElement('hidden', 'map', 1);
$mform->addElement('hidden', 'id', optional_param('id'));
//echo '<input name="filename" value='.$newfilename.' type="hidden" />';
$mform->addElement('hidden', 'filename', $newfilename);
$this->add_action_buttons(false, get_string('uploadgrades'));
print_object($mform);
}
}
?>