Skip to content

Commit

Permalink
grade import/export fixes to use forms lib
Browse files Browse the repository at this point in the history
  • Loading branch information
toyomoyo committed May 24, 2007
1 parent 2186f72 commit 3f8bcf7
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 118 deletions.
21 changes: 21 additions & 0 deletions grade/export/grade_export_form.php
@@ -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'));
}
}
?>
30 changes: 4 additions & 26 deletions grade/export/lib.php
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions grade/export/txt/grade_export_txt_form.php
@@ -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'));
}
}
?>
6 changes: 5 additions & 1 deletion grade/export/txt/index.php
Expand Up @@ -53,7 +53,11 @@
$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
Expand Down Expand Up @@ -81,6 +85,6 @@
echo '</div>';
echo '</form>';
}

*/
print_footer();
?>
6 changes: 6 additions & 0 deletions grade/export/txt/version.php
@@ -0,0 +1,6 @@
<?php

$plugin->version = 2007052201;
$plugin->requires = 2007051801;

?>
88 changes: 13 additions & 75 deletions grade/import/csv/index.php
@@ -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
Expand All @@ -28,25 +20,20 @@
}

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];
Expand All @@ -57,9 +44,9 @@
// 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')) {
Expand All @@ -76,7 +63,6 @@

foreach ($header as $i => $h) {
$h = trim($h); $header[$i] = $h; // remove whitespace
// flag events to add columns if needed (?)
}

while (!feof ($fp)) {
Expand All @@ -86,7 +72,7 @@
// each line is a student record
unset ($studentid);
unset ($studentgrades);
print_object($map);

foreach ($line as $key => $value) {

//decode encoded commas
Expand Down Expand Up @@ -125,8 +111,7 @@
$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");
}
}
}
Expand All @@ -137,7 +122,7 @@
error ('import file '.$filename.' not readable');
}

} else if ( $formdata = $mform->get_data() ) {
} else if ($formdata = $mform->get_data() ) {

$filename = $mform->get_userfile_name();

Expand Down Expand Up @@ -165,56 +150,9 @@
// --- 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();
}
Expand Down
75 changes: 59 additions & 16 deletions grade/import/grade_import_form.php
Expand Up @@ -25,32 +25,75 @@ function get_userfile_name(){
}
}


class grade_import_mapping_form extends moodleform {

function definition () {
global $CFG;

$mform =& $this->_form;

// course id needs to be passed for auth purposes
$mform->addElement('hidden', 'id', optional_param('id'));
// this is an array of headers
$header = $this->_customdata['header'];
// temporary filename
$filename = $this->_customdata['filename'];
// course id
$id = $this->_customdata['id'];

$this->add_action_buttons(false, get_string('uploadgrades'));
}
$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();

function setup ($headers = '', $filename = '') {
$mform =& $this->_form;
if (is_array($headers)) {
foreach ($headers as $header) {
$mform->addElement('hidden', $header, $header);
$mform->addRule($header, null, 'required');
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);

}
}
?>

0 comments on commit 3f8bcf7

Please sign in to comment.