* Prints all grade items for selection
* @input int id - course id
*/
-function print_gradeitem_selections($id) {
+function print_gradeitem_selections($id, $params = NULL) {
// print all items for selections
// make this a standard function in lib maybe
$this->comments[$student->id] = array(); // Collect all comments in tihs array
}
}
-
+
// if grade_item ids are specified
- if ($iids = explode(",", $itemids)) {
- foreach ($iids as $iid) {
+ if ($itemids) {
+ foreach ($itemids as $iid) {
$params->id = $iid;
$gradeitems[] = new grade_item($params);
}
} else {
// else we get all items for this course
$gradeitems = grade_get_items($this->id);
- }
+ }
if ($gradeitems) {
foreach ($gradeitems as $gradeitem) {
-
+
+ $gradeitem -> generate_final();
// load as an array of grade_final objects
- if ($itemgrades = $gradeitem -> load_final()) {
+ if ($itemgrades = $gradeitem -> load_final()) {
- $this->columns[$itemgrades->id] = "$gradeitem->itemmodule: ".format_string($gradeitem->itemname,true)." - $gradeitem->grademax";
+ $this->columns[$gradeitem->id] = "$gradeitem->itemmodule: ".format_string($gradeitem->itemname,true)." - $gradeitem->grademax";
- $this->columnidnumbers[$itemgrades->id] = $gradeitem->idnumber; // this might be needed for some export plugins
+ $this->columnidnumbers[$gradeitem->id] = $gradeitem->idnumber; // this might be needed for some export plugins
if (!empty($gradeitem->grademax)) {
$maxgrade = "$strmax: $gradeitem->grademax";
$studentgrade = $itemgrades[$student->id];
if (!empty($studentgrade->gradevalue)) {
- $this->grades[$student->id][$itemgrades->id] = $currentstudentgrade = $studentgrade->gradevalue;
+ $this->grades[$student->id][$gradeitem->id] = $currentstudentgrade = $studentgrade->gradevalue;
} else {
- $this->grades[$student->id][$itemgrades->id] = $currentstudentgrade = "";
- $this->gradeshtml[$student->id][$itemgrades->id] = "";
+ $this->grades[$student->id][$gradeitem->id] = $currentstudentgrade = "";
+ $this->gradeshtml[$student->id][$gradeitem->id] = "";
}
if (!empty($maxgrade)) {
$this->totals[$student->id] = (float)($this->totals[$student->id]) + (float)($currentstudentgrade);
$comment = $studentgrade->grade_grades_text->feedback;
if (!empty($comment)) {
- $this->comments[$student->id][$itemgrades->id] = $comment;
+ $this->comments[$student->id][$gradeitem->id] = $comment;
} else {
- $this->comments[$student->id][$itemgrades->id] = '';
+ $this->comments[$student->id][$gradeitem->id] = '';
}
}
}
require_once('grade_export_ods.php');
$id = required_param('id', PARAM_INT); // course id
-$itemids = required_param('itemids', PARAM_NOTAGS);
+$itemids = explode(",", required_param('itemids', PARAM_NOTAGS));
$feedback = optional_param('feedback', '', PARAM_ALPHA);
// print all the exported data here
$feedback = optional_param('feedback', '', PARAM_ALPHA);
// process post information
-if ($data = data_submitted() && confirm_sesskey()) {
+if (($data = data_submitted()) && confirm_sesskey()) {
if (!is_array($data->itemids)) {
$itemidsurl = $data->itemids;
require_once('grade_export_txt.php');
$id = required_param('id', PARAM_INT); // course id
-$itemids = required_param('itemids', PARAM_NOTAGS);
+$itemids = explode(",", required_param('itemids', PARAM_NOTAGS));
$feedback = optional_param('feedback', '', PARAM_ALPHA);
// print all the exported data here
$export = new grade_export_txt($id, $itemids);
+$export->set_separator(optional_param('separator'));
$export->print_grades($feedback);
?>
\ No newline at end of file
class grade_export_txt extends grade_export {
var $format = 'txt'; // export format
+ var $separator = "\t"; // default separator
+
+ function set_separator($separator) {
+ if ($separator == 'comma') {
+ $this->separator = ",";
+ } else if ($separator == 'tab') {
+ $this->separator = "\t";
+ }
+ }
/**
* To be implemented by child classes
/// Print names of all the fields
- echo get_string("firstname")."\t".
- get_string("lastname")."\t".
- get_string("idnumber")."\t".
- get_string("institution")."\t".
- get_string("department")."\t".
+ echo get_string("firstname")."$this->separator".
+ get_string("lastname")."{$this->separator}".
+ get_string("idnumber")."{$this->separator}".
+ get_string("institution")."{$this->separator}".
+ get_string("department")."{$this->separator}".
get_string("email");
foreach ($this->columns as $column) {
$column = strip_tags($column);
- echo "\t$column";
+ echo "{$this->separator}$column";
/// add a column_feedback column
if ($feedback) {
- echo "\t{$column}_feedback";
+ echo "{$this->separator}{$column}_feedback";
}
}
- echo "\t".get_string("total")."\n";
+ echo "{$this->separator}".get_string("total")."\n";
/// Print all the lines of data.
foreach ($this->grades as $studentid => $studentgrades) {
if (empty($this->totals[$student->id])) {
$this->totals[$student->id] = '';
}
- echo "$student->firstname\t$student->lastname\t$student->idnumber\t$student->institution\t$student->department\t$student->email";
+ echo "$student->firstname{$this->separator}$student->lastname{$this->separator}$student->idnumber{$this->separator}$student->institution{$this->separator}$student->department{$this->separator}$student->email";
foreach ($studentgrades as $gradeitemid => $grade) {
$grade = strip_tags($grade);
- echo "\t$grade";
+ echo "{$this->separator}$grade";
if ($feedback) {
- echo "\t".array_shift($this->comments[$student->id]);
+ echo "{$this->separator}".array_shift($this->comments[$student->id]);
}
/// if export flag needs to be set
$grade_grades_final->update();
}
}
- echo "\t".$this->totals[$student->id];
+ echo "{$this->separator}".$this->totals[$student->id];
echo "\n";
}
$feedback = optional_param('feedback', '', PARAM_ALPHA);
// process post information
-if ($data = data_submitted() && confirm_sesskey()) {
+if (($data = data_submitted()) && confirm_sesskey()) {
if (!is_array($data->itemids)) {
$itemidsurl = $data->itemids;
$export->display_grades($feedback);
// this redirect should trigger a download prompt
- redirect('export.php?id='.$id.'&itemids='.$itemidsurl);
+ redirect('export.php?id='.$id.'&itemids='.$itemidsurl.'&separator='.$data->separator);
exit;
}
-print_gradeitem_selections($id);
+
+$course = get_record('course', 'id', $id);
+$action = 'exporttxt';
+print_header($course->shortname.': '.get_string('grades'), $course->fullname, grade_nav($course, $action));
+
+// print_gradeitem_selections($id);
+// print all items for selections
+// make this a standard function in lib maybe
+if ($grade_items = grade_get_items($id)) {
+ 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 '<br/>';
+ echo 'tab<input type="radio" name="separator" value="tab"/>';
+ echo 'comma<input type="radio" name="separator" value="comma"/>';
+ echo '<input type="hidden" name="id" value="'.$id.'"/>';
+ echo '<input type="hidden" name="sesskey" value="'.sesskey().'"/>';
+ echo '<br/>';
+ echo '<input type="submit" value="'.get_string('submit').'" />';
+ echo '</div>';
+ echo '</form>';
+}
+
+print_footer();
?>
\ No newline at end of file
require_once('grade_export_xls.php');
$id = required_param('id', PARAM_INT); // course id
-$itemids = required_param('itemids', PARAM_NOTAGS);
+$itemids = explode(",", required_param('itemids', PARAM_NOTAGS));
$feedback = optional_param('feedback', '', PARAM_ALPHA);
// print all the exported data here
$feedback = optional_param('feedback', '', PARAM_ALPHA);
// process post information
-if ($data = data_submitted() && confirm_sesskey()) {
+if (($data = data_submitted()) && confirm_sesskey()) {
if (!is_array($data->itemids)) {
$itemidsurl = $data->itemids;
require_once('grade_export_xml.php');
$id = required_param('id', PARAM_INT); // course id
-$itemids = required_param('itemids', PARAM_NOTAGS);
+$itemids = explode(",", required_param('itemids', PARAM_NOTAGS));
$feedback = optional_param('feedback', '', PARAM_ALPHA);
// print all the exported data here
$feedback = optional_param('feedback', '', PARAM_ALPHA);
// process post information
-if ($data = data_submitted() && confirm_sesskey()) {
+if (($data = data_submitted()) && confirm_sesskey()) {
if (!is_array($data->itemids)) {
$itemidsurl = $data->itemids;
$map[$formdata->mapfrom] = $formdata->mapto;
- $filename = $CFG->dataroot.'/temp/cvstemp';
-
- // 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.
+ // 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.
@set_time_limit(0);
@raise_memory_limit("192M");
if (function_exists('apache_child_terminate')) {
}
$text = my_file_get_contents($filename);
- $fp = fopen($filename, "r");
- // --- get header (field names) ---
- $header = split($csv_delimiter, fgets($fp,1024));
+ // we only operate if file is readable
+ if ($fp = fopen($filename, "r")) {
- foreach ($header as $i => $h) {
- $h = trim($h); $header[$i] = $h; // remove whitespace
- // flag events to add columns if needed (?)
- }
+ // --- get header (field names) ---
+ $header = split($csv_delimiter, fgets($fp,1024));
+
+ foreach ($header as $i => $h) {
+ $h = trim($h); $header[$i] = $h; // remove whitespace
+ // flag events to add columns if needed (?)
+ }
- while (!feof ($fp)) {
- // add something
- $line = split($csv_delimiter, fgets($fp,1024));
+ while (!feof ($fp)) {
+ // add something
+ $line = split($csv_delimiter, fgets($fp,1024));
- // each line is a student record
- unset ($studentid);
- unset ($studentgrades);
- print_object($map);
- foreach ($line as $key => $value) {
+ // each line is a student record
+ unset ($studentid);
+ unset ($studentgrades);
+ print_object($map);
+ foreach ($line as $key => $value) {
- //decode encoded commas
- $value = preg_replace($csv_encode,$csv_delimiter2,trim($value));
- switch ($map[$header[$key]]) {
- case 'userid': //
- $studentid = $value;
- break;
- case 'useridnumber':
- $user = get_record('user', 'idnumber', $value);
- $studentid = $user->id;
- break;
- case 'useremail':
- $user = get_record('user', 'email', $value);
- $studentid = $user->id;
- break;
- case 'username':
- $user = get_record('user', 'username', $value);
- $studentid = $user->id;
- break;
- // might need to add columns for comments
- default:
- if (!empty($map[$header[$key]])) {
- // case of an idnumber, only maps idnumber of a grade_item
- $studentgrades[$map[$header[$key]]] = $value;
- } // otherwise, we ignore this column altogether (e.g. institution, address etc)
- break;
+ //decode encoded commas
+ $value = preg_replace($csv_encode,$csv_delimiter2,trim($value));
+ switch ($map[$header[$key]]) {
+ case 'userid': //
+ $studentid = $value;
+ break;
+ case 'useridnumber':
+ $user = get_record('user', 'idnumber', $value);
+ $studentid = $user->id;
+ break;
+ case 'useremail':
+ $user = get_record('user', 'email', $value);
+ $studentid = $user->id;
+ break;
+ case 'username':
+ $user = get_record('user', 'username', $value);
+ $studentid = $user->id;
+ break;
+ // might need to add columns for comments
+ default:
+ if (!empty($map[$header[$key]])) {
+ // case of an idnumber, only maps idnumber of a grade_item
+ $studentgrades[$map[$header[$key]]] = $value;
+ } // otherwise, we ignore this column altogether (e.g. institution, address etc)
+ break;
+ }
}
- }
- if (!empty($studentgrades)) {
- foreach ($studentgrades as $idnumber => $studentgrade) {
+ if (!empty($studentgrades)) {
+ foreach ($studentgrades as $idnumber => $studentgrade) {
- unset($eventdata);
- $eventdata->idnumber = $idnumber;
- $eventdata->userid = $studentid;
- $eventdata->gradevalue = $studentgrade;
- trigger_event('grade_added', $eventdata);
+ unset($eventdata);
+ $eventdata->idnumber = $idnumber;
+ $eventdata->userid = $studentid;
+ $eventdata->gradevalue = $studentgrade;
+ trigger_event('grade_added', $eventdata);
- echo "<br/>triggering event for $idnumber... student id is $studentid and grade is $studentgrade";
+ echo "<br/>triggering event for $idnumber... student id is $studentid and grade is $studentgrade";
+ }
}
}
+
+ // temporary file can go now
+ unlink($filename);
+ } else {
+ error ('import file '.$filename.' not readable');
}
+
} else if ( $formdata = $mform->get_data() ) {
$filename = $mform->get_userfile_name();
$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;
}
echo '<input type="hidden" name="maps[]" value="'.$h.'"/>';
// this is what they map to
- /**
- * options are userid,
- * useridnumber,
- * useremail,
- * ignore (redundant column or
- * idnumber of grade_item (add individually);
- */
-
$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='.$filename.' type="hidden" />';
+ echo '<input name="filename" value='.$newfilename.' type="hidden" />';
echo '<input type="submit" value="upload" />';
echo '</form>';
//$mform2->display();
// move file to $CFG->dataroot/temp
- move_uploaded_file($filename, $CFG->dataroot.'/temp/cvstemp');
} else {
$mform->display();
}
print_footer();
-
function my_file_get_contents($filename, $use_include_path = 0) {
/// Returns the file as one big long string