$mform->addElement($element);
}
}
- $this->add_action_buttons(false, get_string('submit'));
+ $this->add_action_buttons(false, get_string('submit'));
}
}
?>
\ No newline at end of file
* Base export class
*/
class grade_export {
-
+
var $format = ''; // export format
var $id; // course id
var $itemids; // comma separated grade_item ids;
var $comments = array(); // Collect all comments for each grade
var $totals = array(); // Collect all totals in this array
var $columns = array(); // Accumulate column names in this array.
- var $columnhtml = array(); // Accumulate column html in this array.
+ var $columnhtml = array(); // Accumulate column html in this array.
var $columnidnumbers = array(); // Collect all gradeitem id numbers
var $students = array();
var $course; // course
-
+
// common strings
- var $strgrades;
- var $strgrade;
-
+ var $strgrades;
+ var $strgrade;
+
/**
* Constructor should set up all the private variables ready to be pulled
* @input int id - course id
* @input string itemids - comma separated value of itemids to process for this export
*/
function grade_export($id, $itemids = '') {
-
+
$this->strgrades = get_string("grades");
$this->strgrade = get_string("grade");
$this->itemids = $itemids;
-
- $strmax = get_string("maximumshort");
-
+
+ $strmax = get_string("maximumshort");
+
if (! $course = get_record("course", "id", $id)) {
error("Course ID was incorrect");
}
$context = get_context_instance(CONTEXT_COURSE, $id);
require_capability('moodle/course:viewcoursegrades', $context);
-
+
$this->id = $id;
$this->course = $course;
/// Check to see if groups are being used in this course
if ($groupmode = groupmode($course)) { // Groups are being used
-
+
if (isset($_GET['group'])) {
$changegroup = $_GET['group']; /// 0 or higher
} else {
}
$currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
-
+
} else {
$currentgroup = false;
}
// if grade_item ids are specified
if ($itemids) {
foreach ($itemids as $iid) {
-
+
if ($iid) {
$params->id = clean_param($iid, PARAM_INT);
$gradeitems[] = new grade_item($params);
- }
- }
+ }
+ }
} else {
// else we get all items for this course
$gradeitems = grade_grades::fetch_all(array('courseid'=>$this->id));
}
-
+
if ($gradeitems) {
foreach ($gradeitems as $gradeitem) {
-
+
// load as an array of grade_final objects
- if ($itemgrades = $gradeitem -> get_final()) {
-
+ if ($itemgrades = $gradeitem -> get_final()) {
+
$this->columns[$gradeitem->id] = "$gradeitem->itemmodule: ".format_string($gradeitem->itemname,true)." - $gradeitem->grademax";
-
- $this->columnidnumbers[$gradeitem->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";
} else {
$maxgrade = "";
- }
-
- if (!empty($this->students)) {
+ }
+
+ if (!empty($this->students)) {
foreach ($this->students as $student) {
unset($studentgrade);
// add support for comment here MDL-9634
-
+
if (!empty($itemgrades[$student->id])) {
$studentgrade = $itemgrades[$student->id];
}
-
+
if (!empty($studentgrade->finalgrade)) {
- $this->grades[$student->id][$gradeitem->id] = $currentstudentgrade = $studentgrade->finalgrade;
+ $this->grades[$student->id][$gradeitem->id] = $currentstudentgrade = $studentgrade->finalgrade;
} else {
$this->grades[$student->id][$gradeitem->id] = $currentstudentgrade = "";
$this->gradeshtml[$student->id][$gradeitem->id] = "";
} else {
$this->totals[$student->id] = (float)($this->totals[$student->id]) + 0;
}
-
- if (!empty($comment)) {
+
+ if (!empty($comment)) {
// load comments here
if ($studentgrade) {
$studentgrade->load_text();
$this->comments[$student->id][$gradeitem->id] = $comment;
}
} else {
- $this->comments[$student->id][$gradeitem->id] = '';
+ $this->comments[$student->id][$gradeitem->id] = '';
}
}
}
}
}
- }
+ }
}
-
+
/**
* To be implemented by child classes
*/
function print_grades() { }
-
+
/**
* Displays all the grades on screen as a feedback mechanism
*/
foreach ($this->columns as $column) {
$column = strip_tags($column);
echo "<th>$column</th>";
-
- /// add a column_feedback column
+
+ /// add a column_feedback column
if ($feedback) {
echo "<th>{$column}_feedback</th>";
- }
+ }
}
echo '<th>'.get_string("total")."</th>";
echo '</tr>';
/// Print all the lines of data.
-
-
- foreach ($this->grades as $studentid => $studentgrades) {
-
+
+
+ foreach ($this->grades as $studentid => $studentgrades) {
+
echo '<tr>';
$student = $this->students[$studentid];
if (empty($this->totals[$student->id])) {
$this->totals[$student->id] = '';
}
-
-
+
+
echo "<td>$student->firstname</td><td>$student->lastname</td><td>$student->idnumber</td><td>$student->institution</td><td>$student->department</td><td>$student->email</td>";
foreach ($studentgrades as $grade) {
$grade = strip_tags($grade);
- echo "<td>$grade</td>";
-
+ echo "<td>$grade</td>";
+
if ($feedback) {
echo '<td>'.array_shift($this->comments[$student->id]).'</td>';
- }
+ }
}
echo '<td>'.$this->totals[$student->id].'</td>';
echo "</tr>";
require_once("../../../config.php");
require_once($CFG->dirroot.'/grade/export/lib.php');
require_once('grade_export_ods.php');
-
+
$id = required_param('id', PARAM_INT); // course id
$itemids = explode(",", required_param('itemids', PARAM_RAW));
-$feedback = optional_param('feedback', '', PARAM_ALPHA);
-
+$feedback = optional_param('feedback', '', PARAM_ALPHA);
+
// print all the exported data here
$export = new grade_export_ods($id, $itemids);
$export->print_grades($feedback);
-
+
?>
\ No newline at end of file
require_once($CFG->dirroot.'/grade/export/lib.php');
class grade_export_ods extends grade_export {
-
+
var $format = 'ods'; // export format
-
+
/**
* To be implemented by child classes
*/
- function print_grades($feedback = false) {
-
- global $CFG;
+ function print_grades($feedback = false) {
+
+ global $CFG;
require_once($CFG->dirroot.'/lib/odslib.class.php');
-
+
/// Whether this plugin is entitled to update export time
if ($expplugins = explode(",", $CFG->gradeexport)) {
if (in_array($this->format, $expplugins)) {
$export = true;
} else {
- $export = false;
+ $export = false;
}
} else {
- $export = false;
+ $export = false;
}
-
+
/// Calculate file name
$downloadfilename = clean_filename("{$this->course->shortname} $this->strgrades.ods");
/// Creating a workbook
$workbook->send($downloadfilename);
/// Adding the worksheet
$myxls =& $workbook->add_worksheet($this->strgrades);
-
+
/// Print names of all the fields
$myxls->write_string(0,0,get_string("firstname"));
$myxls->write_string(0,1,get_string("lastname"));
$pos=6;
foreach ($this->columns as $column) {
$myxls->write_string(0,$pos++,strip_tags($column));
-
- /// add a column_feedback column
+
+ /// add a column_feedback column
if ($feedback) {
$myxls->write_string(0,$pos++,strip_tags($column."_feedback"));
}
}
$myxls->write_string(0,$pos,get_string("total"));
-
+
/// Print all the lines of data.
$i = 0;
if (!empty($this->grades)) {
if (empty($this->totals[$student->id])) {
$this->totals[$student->id] = '';
}
-
+
$myxls->write_string($i,0,$student->firstname);
$myxls->write_string($i,1,$student->lastname);
$myxls->write_string($i,2,$student->idnumber);
else {
$myxls->write_string($i,$j++,strip_tags($grade));
}
-
+
// writing comment if requested
if ($feedback) {
$myxls->write_string($i,$j++,array_shift($this->comments[$student->id]));
- }
-
+ }
+
/// if export flag needs to be set
/// construct the grade_grades object and update timestamp if CFG flag is set
-
+
if ($export) {
$params= new object();
$params->itemid = $gradeitemid;
$params->userid = $studentid;
-
+
$grade_grades = new grade_grades($params);
$grade_grades->exported = time();
// update the time stamp;
/// Close the workbook
$workbook->close();
-
+
exit;
}
}
require_once($CFG->dirroot.'/grade/export/lib.php');
require_once('grade_export_ods.php');
-$id = required_param('id', PARAM_INT); // course id
+$id = required_param('id', PARAM_INT); // course id
require_login($id);
require_capability('moodle/course:downloadallgrades', get_context_instance(CONTEXT_COURSE, $id));
$course = get_record('course', 'id', $id);
-$feedback = optional_param('feedback', '', PARAM_ALPHA);
+$feedback = optional_param('feedback', '', PARAM_ALPHA);
$strgrades = get_string('grades', 'grades');
$actionstr = get_string('exportods', 'grades');
$gradenav = "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>";
} else {
$itemidsurl = implode(",",$data->itemids);
}
-
+
// print the grades on screen for feedbacks
-
+
$export = new grade_export($id, $data->itemids);
$export->display_grades($feedback);
// this redirect should trigger a download prompt
redirect('export.php?id='.$id.'&itemids='.$itemidsurl);
- exit;
+ exit;
}
print_gradeitem_selections($id);
require_once("../../../config.php");
require_once($CFG->dirroot.'/grade/export/lib.php');
require_once('grade_export_txt.php');
-
+
$id = required_param('id', PARAM_INT); // course id
$itemids = explode(",", required_param('itemids', PARAM_RAW));
-$feedback = optional_param('feedback', '', PARAM_ALPHA);
-
+$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
require_once($CFG->dirroot.'/grade/export/lib.php');
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";
- }
+ $this->separator = "\t";
+ }
}
-
+
/**
* To be implemented by child classes
*/
- function print_grades($feedback = false) {
+ function print_grades($feedback = false) {
global $CFG;
if (in_array($this->format, $expplugins)) {
$export = true;
} else {
- $export = false;
+ $export = false;
}
} else {
- $export = false;
+ $export = false;
}
-
- /// Print header to force download
- header("Content-Type: application/download\n");
+
+ /// Print header to force download
+ header("Content-Type: application/download\n");
$downloadfilename = clean_filename("{$this->course->shortname} $this->strgrades");
header("Content-Disposition: attachment; filename=\"$downloadfilename.txt\"");
foreach ($this->columns as $column) {
$column = strip_tags($column);
echo "{$this->separator}$column";
-
- /// add a column_feedback column
+
+ /// add a column_feedback column
if ($feedback) {
echo "{$this->separator}{$column}_feedback";
- }
+ }
}
echo "{$this->separator}".get_string("total")."\n";
-
+
/// Print all the lines of data.
foreach ($this->grades as $studentid => $studentgrades) {
-
+
$student = $this->students[$studentid];
if (empty($this->totals[$student->id])) {
$this->totals[$student->id] = '';
foreach ($studentgrades as $gradeitemid => $grade) {
$grade = strip_tags($grade);
- echo "{$this->separator}$grade";
-
+ echo "{$this->separator}$grade";
+
if ($feedback) {
echo "{$this->separator}".array_shift($this->comments[$student->id]);
- }
-
+ }
+
/// if export flag needs to be set
/// construct the grade_grades object and update timestamp if CFG flag is set
$params = new object();
$params->itemid = $gradeitemid;
$params->userid = $studentid;
-
+
$grade_grades = new grade_grades($params);
$grade_grades->exported = time();
// update the time stamp;
echo "{$this->separator}".$this->totals[$student->id];
echo "\n";
}
-
+
exit;
}
}
$mform->addElement($element);
}
}
-
- include_once($CFG->libdir.'/pear/HTML/QuickForm/radio.php');
+
+ include_once($CFG->libdir.'/pear/HTML/QuickForm/radio.php');
$radio = array();
$radio[] = &MoodleQuickForm::createElement('radio', 'separator', null, get_string('septab', 'grades'), 'tab');
$radio[] = &MoodleQuickForm::createElement('radio', 'separator', null, get_string('sepcomma', 'grades'), 'comma');
$mform->addGroup($radio, 'separator', get_string('separator', 'grades'), ' ', false);
- $mform->setDefault('separator', 'comma');
+ $mform->setDefault('separator', 'comma');
- $this->add_action_buttons(false, get_string('submit'));
+ $this->add_action_buttons(false, get_string('submit'));
}
}
?>
\ No newline at end of file
} else {
$itemidsurl = implode(",",$data->itemids);
}
-
+
$export = new grade_export($id, $data->itemids);
$export->display_grades($feedback);
-
+
// this redirect should trigger a download prompt
redirect('export.php?id='.$id.'&itemids='.$itemidsurl.'&separator='.$data->separator);
- exit;
+ exit;
}
// print the form to choose what grade_items to export
require_once("../../../config.php");
require_once($CFG->dirroot.'/grade/export/lib.php');
require_once('grade_export_xls.php');
-
+
$id = required_param('id', PARAM_INT); // course id
$itemids = explode(",", required_param('itemids', PARAM_RAW));
-$feedback = optional_param('feedback', '', PARAM_ALPHA);
-
+$feedback = optional_param('feedback', '', PARAM_ALPHA);
+
// print all the exported data here
$export = new grade_export_xls($id, $itemids);
$export->print_grades($feedback);
-
+
?>
\ No newline at end of file
require_once($CFG->dirroot.'/grade/export/lib.php');
class grade_export_xls extends grade_export {
-
+
var $format = 'xls'; // export format
-
+
/**
* To be implemented by child classes
*/
- function print_grades($feedback = false) {
-
- global $CFG;
-
+ function print_grades($feedback = false) {
+
+ global $CFG;
+
/// Whether this plugin is entitled to update export time
if ($expplugins = explode(",", $CFG->gradeexport)) {
if (in_array($this->format, $expplugins)) {
$export = true;
} else {
- $export = false;
+ $export = false;
}
} else {
- $export = false;
+ $export = false;
}
-
+
require_once($CFG->dirroot.'/lib/excellib.class.php');
/// Calculate file name
$workbook->send($downloadfilename);
/// Adding the worksheet
$myxls =& $workbook->add_worksheet($this->strgrades);
-
+
/// Print names of all the fields
$myxls->write_string(0,0,get_string("firstname"));
$myxls->write_string(0,1,get_string("lastname"));
$pos=6;
foreach ($this->columns as $column) {
$myxls->write_string(0,$pos++,strip_tags($column));
- /// add a column_feedback column
+ /// add a column_feedback column
if ($feedback) {
$myxls->write_string(0,$pos++,strip_tags($column."_feedback"));
}
}
$myxls->write_string(0,$pos,get_string("total"));
-
+
/// Print all the lines of data.
$i = 0;
if (!empty($this->grades)) {
if (empty($this->totals[$student->id])) {
$this->totals[$student->id] = '';
}
-
+
$myxls->write_string($i,0,$student->firstname);
$myxls->write_string($i,1,$student->lastname);
$myxls->write_string($i,2,$student->idnumber);
}
else {
$myxls->write_string($i,$j++,strip_tags($grade));
- }
-
+ }
+
// writing comment if requested
if ($feedback) {
$myxls->write_string($i,$j++,array_shift($this->comments[$student->id]));
/// if export flag needs to be set
/// construct the grade_grades object and update timestamp if CFG flag is set
-
+
if ($export) {
$params = new object();
$params->itemid = $gradeitemid;
$params->userid = $studentid;
-
+
$grade_grades = new grade_grades($params);
$grade_grades->exported = time();
// update the time stamp;
/// Close the workbook
$workbook->close();
- exit;
+ exit;
}
}
} else {
$itemidsurl = implode(",",$data->itemids);
}
-
+
// print the grades on screen for feedbacks
-
+
$export = new grade_export($id, $data->itemids);
$export->display_grades($feedback);
-
+
// this redirect should trigger a download prompt
redirect('export.php?id='.$id.'&itemids='.$itemidsurl);
- exit;
+ exit;
}
print_gradeitem_selections($id);
require_once("../../../config.php");
require_once($CFG->dirroot.'/grade/export/lib.php');
require_once('grade_export_xml.php');
-
+
$id = required_param('id', PARAM_INT); // course id
$itemids = explode(",", required_param('itemids', PARAM_RAW));
-$feedback = optional_param('feedback', '', PARAM_ALPHA);
-
+$feedback = optional_param('feedback', '', PARAM_ALPHA);
+
// print all the exported data here
$export = new grade_export_xml($id, $itemids);
$export->print_grades($feedback);
-
+
?>
\ No newline at end of file
require_once($CFG->dirroot.'/grade/export/lib.php');
class grade_export_xml extends grade_export {
-
+
var $format = 'xml'; // export format
-
+
/**
* To be implemented by child classes
*/
- function print_grades($feedback = false) {
-
+ function print_grades($feedback = false) {
+
global $CFG;
/// Whether this plugin is entitled to update export time
if (in_array($this->format, $expplugins)) {
$export = true;
} else {
- $export = false;
+ $export = false;
}
} else {
- $export = false;
+ $export = false;
}
-
+
require_once($CFG->dirroot.'/lib/excellib.class.php');
/// Calculate file name
- $downloadfilename = clean_filename("{$this->course->shortname} $this->strgrades.xml");
+ $downloadfilename = clean_filename("{$this->course->shortname} $this->strgrades.xml");
- header("Content-type: text/xml; charset=UTF-8");
+ header("Content-type: text/xml; charset=UTF-8");
header("Content-Disposition: attachment; filename=\"$downloadfilename\"");
-
+
/// time stamp to ensure uniqueness of batch export
echo '<results batch="xml_export_'.time().'">';
-
+
foreach ($this->columnidnumbers as $index => $idnumber) {
-
- // studentgrades[] index should match with corresponding $index
+
+ // studentgrades[] index should match with corresponding $index
foreach ($this->grades as $studentid => $studentgrades) {
echo '<result>';
-
+
// state can be new, or regrade
// require comparing of timestamps in db
-
+
$params = new object();
$params->idnumber = $idnumber;
// get the grade item
$gradeitem = new grade_item($params);
-
+
// we are trying to figure out if this is a new grade, or a regraded grade
// only relevant if this grade for this user is already exported
-
+
// get the grade_grades for this user
$params = new object();
$params->itemid = $gradeitem->id;
$params->userid = $studentid;
-
+
$grade_grades = new grade_grades($params);
-
+
// if exported, check grade_history, if modified after export, set state to regrade
if (!empty($grade_grades->exported)) {
//TODO: use timemodified or something else instead
/* if (record_exists_select('grade_history', 'itemid = '.$gradeitem->id.' AND userid = '.$studentid.' AND timemodified > '.$grade_grades->exported)) {
- $status = 'regrade';
+ $status = 'regrade';
} else {
- $status = 'new';
+ $status = 'new';
}*/
- } else {
+ } else {
// never exported
- $status = 'new';
+ $status = 'new';
}
-
+
echo '<state>'.$status.'</state>';
// only need id number
echo '<assignment>'.$idnumber.'</assignment>';
echo '<student>'.$studentid.'</student>';
echo '<score>'.$studentgrades[$index].'</score>';
if ($feedback) {
- echo '<feedback>'.$this->comments[$studentid][$index].'</feedback>';
+ echo '<feedback>'.$this->comments[$studentid][$index].'</feedback>';
}
echo '</result>';
}
}
echo '</results>';
- exit;
+ exit;
}
}
$course = get_record('course', 'id', $id);
$feedback = optional_param('feedback', '', PARAM_ALPHA);
-
+
$strgrades = get_string('grades', 'grades');
$actionstr = get_string('exportxml', 'grades');
$gradenav = "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>";
} else {
$itemidsurl = implode(",",$data->itemids);
}
-
+
// print the grades on screen for feedbacks
-
+
$export = new grade_export($id, $data->itemids);
$export->display_grades($feedback);
-
+
// this redirect should trigger a download prompt
redirect('export.php?id='.$id.'&itemids='.$itemidsurl);
exit;
// 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 = data_submitted()) && !empty($formdata->map)) {
-
+
// temporary file name supplied by form
- $filename = $CFG->dataroot.'/temp/'.clean_param($formdata->filename, PARAM_FILE);
+ $filename = $CFG->dataroot.'/temp/'.clean_param($formdata->filename, PARAM_FILE);
if ($fp = fopen($filename, "r")) {
// --- get header (field names) ---
$header = split($csv_delimiter, clean_param(fgets($fp,1024), PARAM_RAW));
-
+
foreach ($header as $i => $h) {
$h = trim($h); $header[$i] = $h; // remove whitespace
- }
+ }
} else {
- error ('could not open file '.$filename);
+ error ('could not open file '.$filename);
}
-
+
$map = array();
// loops mapping_0, mapping_1 .. mapping_n and construct $map array
foreach ($header as $i => $head) {
- $map[$i] = $formdata->{'mapping_'.$i};
+ $map[$i] = $formdata->{'mapping_'.$i};
}
// if mapping informatioin is supplied
foreach ($map as $i=>$j) {
if ($j == 0) {
// you can have multiple ignores
- continue;
+ continue;
} else {
if (!isset($maperrors[$j])) {
- $maperrors[$j] = true;
+ $maperrors[$j] = true;
} else {
- // collision
+ // collision
unlink($filename); // needs to be uploaded again, sorry
error('mapping collision detected, 2 fields maps to the same grdae item '.$j);
}
if (function_exists('apache_child_terminate')) {
@apache_child_terminate();
}
-
+
// we only operate if file is readable
if ($fp = fopen($filename, "r")) {
-
+
// read the first line makes sure this doesn't get read again
$header = split($csv_delimiter, clean_param(fgets($fp,1024), PARAM_RAW));
-
- // use current (non-conflicting) time stamp
- $importcode = time();
+
+ // use current (non-conflicting) time stamp
+ $importcode = time();
while (get_record('grade_import_values', 'import_code', $importcode)) {
- $importcode = time();
+ $importcode = time();
}
-
+
$newgradeitems = array(); // temporary array to keep track of what new headers are processed
$status = true;
-
+
while (!feof ($fp)) {
// add something
- $line = split($csv_delimiter, fgets($fp,1024));
-
+ $line = split($csv_delimiter, fgets($fp,1024));
+
// array to hold all grades to be inserted
$newgrades = array();
// array to hold all feedback
- $newfeedbacks = array();
+ $newfeedbacks = array();
// each line is a student record
- foreach ($line as $key => $value) {
+ foreach ($line as $key => $value) {
//decode encoded commas
$value = clean_param($value, PARAM_RAW);
$value = preg_replace($csv_encode,$csv_delimiter2,trim($value));
if (isset($t[1])) {
$t1 = $t[1];
} else {
- $t1 = '';
+ $t1 = '';
}
-
+
switch ($t0) {
case 'userid': //
if (!$user = get_record('user','id', $value)) {
import_cleanup($importcode);
notify("user mapping error, could not find user with id \"$value\"");
$status = false;
- break 3;
+ break 3;
}
$studentid = $value;
break;
import_cleanup($importcode);
notify("user mapping error, could not find user with idnumber \"$value\"");
$status = false;
- break 3;
+ break 3;
}
$studentid = $user->id;
break;
import_cleanup($importcode);
notify("user mapping error, could not find user with email address \"$value\"");
$status = false;
- break 3;
+ break 3;
}
- $studentid = $user->id;
+ $studentid = $user->id;
break;
case 'username':
if (!$user = get_record('user', 'username', $value)) {
import_cleanup($importcode);
notify("user mapping error, could not find user with username \"$value\"");
$status = false;
- break 3;
+ break 3;
}
$studentid = $user->id;
break;
case 'new':
// first check if header is already in temp database
-
- if (empty($newgradeitems[$key])) {
-
+
+ if (empty($newgradeitems[$key])) {
+
$newgradeitem->itemname = $header[$key];
- $newgradeitem->import_code = $importcode;
-
+ $newgradeitem->import_code = $importcode;
+
// failed to insert into new grade item buffer
if (!$newgradeitems[$key] = insert_record('grade_import_newitem', $newgradeitem)) {
$status = false;
import_cleanup($importcode);
notify(get_string('importfailed', 'grades'));
- break 3;
+ break 3;
}
// add this to grade_import_newitem table
- // add the new id to $newgradeitem[$key]
- }
+ // add the new id to $newgradeitem[$key]
+ }
unset($newgrade);
$newgrade -> newgradeitem = $newgradeitems[$key];
- $newgrade -> finalgrade = $value;
+ $newgrade -> finalgrade = $value;
$newgrades[] = $newgrade;
-
- // if not, put it in
+
+ // if not, put it in
// else, insert grade into the table
break;
case 'feedback':
$feedback -> feedback = $value;
$newfeedbacks[] = $feedback;
}
- break;
+ break;
default:
// existing grade items
if (!empty($map[$key]) && $value!=="") {
-
+
// non numeric grade value supplied, possibly mapped wrong column
if (!is_numeric($value)) {
echo "<br/>t0 is $t0";
echo "<br/>grade is $value";
- $status = false;
+ $status = false;
import_cleanup($importcode);
notify(get_string('badgrade', 'grades'));
break 3;
}
-
- // case of an id, only maps id of a grade_item
+
+ // case of an id, only maps id of a grade_item
// this was idnumber
include_once($CFG->libdir.'/grade/grade_item.php');
if (!$gradeitem = new grade_item(array('id'=>$map[$key]))) {
notify(get_string('importfailed', 'grades'));
break 3;
}
-
+
// check if grade item is locked if so, abort
if ($gradeitem->locked) {
$status = false;
import_cleanup($importcode);
notify(get_string('gradeitemlocked', 'grades'));
- break 3;
+ break 3;
}
unset($newgrade);
$newgrade -> itemid = $gradeitem->id;
- $newgrade -> finalgrade = $value;
+ $newgrade -> finalgrade = $value;
$newgrades[] = $newgrade;
- } // otherwise, we ignore this column altogether
+ } // otherwise, we ignore this column altogether
// because user has chosen to ignore them (e.g. institution, address etc)
break;
}
// insert results of this students into buffer
if (!empty($newgrades)) {
-
+
foreach ($newgrades as $newgrade) {
-
+
// check if grade_grades is locked and if so, abort
if ($grade_grades = new grade_grades(array('itemid'=>$newgrade->itemid, 'userid'=>$studentid))) {
if ($grade_grades->locked) {
}
}
- /// at this stage if things are all ok, we commit the changes from temp table
+ /// at this stage if things are all ok, we commit the changes from temp table
if ($status) {
grade_import_commit($course->id, $importcode);
}
// temporary file can go now
unlink($filename);
} else {
- error ('import file '.$filename.' not readable');
+ error ('import file '.$filename.' not readable');
}
} else if ($formdata = $mform->get_data()) {
// else if file is just uploaded
-
+
$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.
$text = my_file_get_contents($filename);
// trim utf-8 bom
- $textlib = new textlib();
- /// normalize line endings and do the encoding conversion
- $text = $textlib->convert($text, $formdata->encoding);
+ $textlib = new textlib();
+ /// normalize line endings and do the encoding conversion
+ $text = $textlib->convert($text, $formdata->encoding);
$text = $textlib->trim_utf8_bom($text);
// Fix mac/dos newlines
$text = preg_replace('!\r\n?!',"\n",$text);
fwrite($fp,$text);
fclose($fp);
- $fp = fopen($filename, "r");
-
+ $fp = fopen($filename, "r");
+
// --- get header (field names) ---
$header = split($csv_delimiter, clean_param(fgets($fp,1024), PARAM_RAW));
-
+
// print some preview
$numlines = 0; // 0 preview lines displayed
echo '<tr>';
foreach ($header as $h) {
$h = clean_param($h, PARAM_RAW);
- echo '<th>'.$h.'</th>';
+ echo '<th>'.$h.'</th>';
}
echo '</tr>';
while (!feof ($fp) && $numlines <= $formdata->previewrows) {
- $lines = split($csv_delimiter, fgets($fp,1024));
+ $lines = split($csv_delimiter, fgets($fp,1024));
echo '<tr>';
foreach ($lines as $line) {
echo '<td>'.$line.'</td>';;
echo '</tr>';
}
echo '</table>';
-
+
/// feeding gradeitems into the grade_import_mapping_form
include_once($CFG->libdir.'/grade/grade_item.php');
$gradeitems = array();
foreach ($grade_items as $grade_item) {
// skip course type and category type
if ($grade_item->itemtype == 'course' || $grade_item->itemtype == 'category') {
- continue;
- }
-
+ continue;
+ }
+
// this was idnumber
- $gradeitems[$grade_item->id] = $grade_item->itemname;
+ $gradeitems[$grade_item->id] = $grade_item->itemname;
}
}
}
$encodings = $textlib->get_encodings();
$mform->addElement('select', 'encoding', get_string('encoding', 'grades'), $encodings);
- $options = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
+ $options = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
$mform->addElement('select', 'previewrows', 'Preview rows', $options); // TODO: localize
$mform->setType('previewrows', PARAM_INT);
$this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
}
class grade_import_mapping_form extends moodleform {
-
+
function definition () {
global $CFG;
$mform =& $this->_form;
}
}
$mform->addElement('select', 'mapfrom', get_string('mapfrom', 'grades'), $mapfromoptions);
- //choose_from_menu($mapfromoptions, 'mapfrom');
-
+ //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', 'grades'), $maptooptions);
-
+
$mform->addElement('header', 'general', get_string('mappings', 'grades'));
-
+
// add a comment option
if ($gradeitems = $this->_customdata['gradeitems']) {
- $comments = array();
+ $comments = array();
foreach ($gradeitems as $itemid => $itemname) {
- $comments['feedback_'.$itemid] = 'comments for '.$itemname;
- }
+ $comments['feedback_'.$itemid] = 'comments for '.$itemname;
+ }
}
include_once($CFG->libdir.'/gradelib.php');
- if ($header) {
+ if ($header) {
$i = 0; // index
foreach ($header as $h) {
$h = trim($h);
- // this is what each header maps to
- $mform->addElement('selectgroups',
- 'mapping_'.$i, s($h),
- array('others'=>array('0'=>'ignore', 'new'=>'new gradeitem'),
- 'gradeitems'=>$gradeitems,
+ // this is what each header maps to
+ $mform->addElement('selectgroups',
+ 'mapping_'.$i, s($h),
+ array('others'=>array('0'=>'ignore', 'new'=>'new gradeitem'),
+ 'gradeitems'=>$gradeitems,
'comments'=>$comments));
$i++;
}
//echo '<input name="filename" value='.$newfilename.' type="hidden" />';
$mform->addElement('hidden', 'filename', $newfilename);
$mform->setType('filename', PARAM_FILE);
- $this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
+ $this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
}
}
<?php // $Id$
/**
- * given an import code, commits all entries in buffer tables
+ * given an import code, commits all entries in buffer tables
* (grade_import_value and grade_import_newitem)
- * If this function is called, we assume that all data collected
+ * If this function is called, we assume that all data collected
* up to this point is fine and we can go ahead and commit
* @param int courseid - id of the course
* @param string importcode - import batch identifier
*/
function grade_import_commit($courseid, $importcode) {
global $CFG;
-
+
include_once($CFG->libdir.'/gradelib.php');
include_once($CFG->libdir.'/grade/grade_item.php');
$commitstart = time(); // start time in case we need to roll back
$newitemids = array(); // array to hold new grade_item ids from grade_import_newitem table, mapping array
-
+
/// first select distinct new grade_items with this batch
-
- if ($newitems = get_records_sql("SELECT *
+
+ if ($newitems = get_records_sql("SELECT *
FROM {$CFG->prefix}grade_import_newitem
WHERE import_code = $importcode")) {
-
- // instances of the new grade_items created, cached
+
+ // instances of the new grade_items created, cached
// in case grade_update fails, so that we can remove them
$instances = array();
foreach ($newitems as $newitem) {
// get all grades with this item
-
- if ($grades = get_records('grade_import_values', 'newgradeitem', $newitem->id)) {
+
+ if ($grades = get_records('grade_import_values', 'newgradeitem', $newitem->id)) {
// make the grardes array for update_grade
-
+
// find the max instance number of 'manual' grade item
// and increment that number by 1 by hand
// I can not find other ways to make 'manual' type work,
// unless we have a 'new' flag for grade_update to let it
// know that this is a new grade_item, and let grade_item
// handle the instance id in the case of a 'manual' import?
- if ($lastimport = get_record_sql("SELECT *
+ if ($lastimport = get_record_sql("SELECT *
FROM {$CFG->prefix}grade_items
WHERE courseid = $courseid
AND itemtype = 'manual'
ORDER BY iteminstance DESC", true)) {
$instance = $lastimport->iteminstance + 1;
} else {
- $instance = 1;
+ $instance = 1;
}
-
+
$instances[] = $instance;
// if fails, deletes all the created grade_items and grades
// insert each individual grade to this new grade item
$failed = 0;
- foreach ($grades as $grade) {
+ foreach ($grades as $grade) {
if (!$gradeitem->update_final_grade($grade->userid, $grade->finalgrade, NULL, NULL, $grade->feedback)) {
$failed = 1;
break;
}
if ($failed) {
foreach ($instances as $instance) {
- $gradeitem = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual', 'iteminstance'=>$instance));
+ $gradeitem = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual', 'iteminstance'=>$instance));
// this method does not seem to delete all the raw grades and the item itself
// which I think should be deleted in this case, can I use sql directly here?
$gradeitem->delete();
/// then find all existing items
- if ($gradeitems = get_records_sql("SELECT DISTINCT (itemid)
+ if ($gradeitems = get_records_sql("SELECT DISTINCT (itemid)
FROM {$CFG->prefix}grade_import_values
WHERE import_code = $importcode
AND itemid > 0")) {
$modifieditems = array();
-
+
foreach ($gradeitems as $itemid=>$iteminfo) {
-
+
if (!$gradeitem = new grade_item(array('id'=>$itemid))) {
// not supposed to happen, but just in case
import_cleanup($importcode);
$modifieditems[] = $itemid;
}
-
+
if (!empty($failed)) {
import_cleanup($importcode);
return false;
// trim utf-8 bom
$textlib = new textlib();
// converts to propert unicode
- $text = $textlib->convert($text, $formdata->encoding);
+ $text = $textlib->convert($text, $formdata->encoding);
$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']) {
-
+
// import batch identifier timestamp
$importcode = time();
$status = true;
-
+
$numlines = 0;
-
+
// print some previews
print_heading(get_string('importpreview', 'grades'));
-
- echo '<table cellpadding="5">';
+
+ echo '<table cellpadding="5">';
foreach ($results as $i => $result) {
if ($numlines < $formdata->previewrows && isset($results[$i+1])) {
echo '<tr>';
$status = false;
break;
}
-
+
// grade item locked, abort
if ($gradeitem->locked) {
$status = false;
notify(get_string('gradeitemlocked', 'grades'));
- break 3;
- }
-
+ break 3;
+ }
+
// check if grade_grades is locked and if so, abort
if ($grade_grades = new grade_grades(array('itemid'=>$gradeitem->id, 'userid'=>$result['#']['student'][0]['#']))) {
if ($grade_grades->locked) {
$newgrades[] = $newgrade;
}
}
-
+
// loop through info collected so far
if ($status && !empty($newgrades)) {
foreach ($newgrades as $newgrade) {
-
+
// check if user exist
if (!$user = get_record('user', 'id', $newgrade->userid)) {
// no user found, abort
notify(get_string('baduserid', 'grades'));
notify(get_string('importfailed', 'grades'));
break;
- }
-
+ }
+
// check grade value is a numeric grade
if (!is_numeric($newgrade->rawgrade)) {
$status = false;
if ($status) {
/// comit the code if we are up this far
grade_import_commit($id, $importcode);
- }
+ }
} else {
// no results section found in xml,
// assuming bad format, abort import
- notify('badxmlformat', 'grade');
+ notify('badxmlformat', 'grade');
}
} else {
// display the standard upload file form
$weightsum = 0;
$sum = 0;
foreach($items as $key=>$value) {
- $grade_value = isset($grade_values[$key]) ? $grade_values[$key] : 0;
+ $grade_value = isset($grade_values[$key]) ? $grade_values[$key] : 0;
if ($items[$key]->aggregationcoef <= 0) {
continue;
}
}
break;
- case GRADE_AGGREGATE_EXTRACREDIT_MEAN_ALL: // special average
+ case GRADE_AGGREGATE_EXTRACREDIT_MEAN_ALL: // special average
$num = 0;
$sum = 0;
foreach($items as $key=>$value) {
- $grade_value = isset($grade_values[$key]) ? $grade_values[$key] : 0;
+ $grade_value = isset($grade_values[$key]) ? $grade_values[$key] : 0;
if ($items[$key]->aggregationcoef == 0) {
$num += 1;
$sum += $grade_value;
}
break;
- case GRADE_AGGREGATE_EXTRACREDIT_MEAN_GRADED: // special average
+ case GRADE_AGGREGATE_EXTRACREDIT_MEAN_GRADED: // special average
$num = 0;
$sum = 0;
foreach($grade_values as $key=>$grade_value) {
or $this->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN_GRADED
or $this->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN_ALL
or $this->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN_GRADED);
-
+
}
/**
*/
function standardise_score($rawgrade, $source_min, $source_max, $target_min, $target_max) {
if (is_null($rawgrade)) {
- return null;
+ return null;
}
$factor = ($rawgrade - $source_min) / ($source_max - $source_min);