CopyRight (C) 2007 Information Technology Center, Nagoya Institute of Technology, All Rights Reserved. --- lib.php.org 2007-12-03 11:31:58.000000000 +0900 +++ lib.php 2007-12-03 12:17:13.000000000 +0900 @@ -586,6 +586,16 @@ $this->display_submissions(); break; + case 'download':// Main window, download everything + $this->download_submissions(); + // redirect('submissions.php?id='.$this->cm->id); + break; + + case 'upload':// Main window, upload feedback + $this->upload_submissions(); + // redirect('submissions.php?id='.$this->cm->id); + break; + case 'fastgrade': ///do the fast grading stuff - this process should work for all 3 subclasses $grading = false; @@ -1183,7 +1193,7 @@ $table->add_data($row); } } - + /* /// Print quickgrade form around the table if ($quickgrade){ echo '
'; @@ -1199,6 +1209,26 @@ echo '

'; echo '
'; } + */ + + /// Print quickgrade form around the table + if ($quickgrade){ + echo '

'; + echo '
'; + echo ''; + echo ''; + echo ''; + echo '

'; + } + + $table->print_html(); /// Print the whole table + + if ($quickgrade) echo ''; + /// End of fast grading form /// Mini form for setting user preference @@ -1234,6 +1264,216 @@ print_footer($this->course); } + /* + * Download all the submissions in csv format + */ + function download_submissions() { + global $CFG, $db, $USER; + $course = $this->course; + $assignment = $this->assignment; + add_to_log($course->id, 'assignment', 'download submission', + 'submissions.php?id='.$this->cm->id, $this->assignment->id, $this->cm->id); + $context = get_context_instance(CONTEXT_MODULE, $this->cm->id); + $users = get_users_by_capability($context, 'mod/assignment:submit'); + $select = 'SELECT u.username, u.firstname, u.lastname, + s.id AS submissionid, s.grade, s.submissioncomment, s.timemarked '; + $sql = 'FROM '.$CFG->prefix.'user u '. + 'LEFT JOIN '.$CFG->prefix.'assignment_submissions s + ON u.id = s.userid AND s.assignment = '.$this->assignment->id.' '. + 'WHERE '.$where.'u.id IN ('.implode(',', array_keys($users)).') '; + if ($ausers = get_records_sql($select.$sql)) { + header("Content-Type: application/download\n"); + $downloadfilename = clean_filename($course->shortname.'_assignment_'.date("Y-m-d_H-i-s")); + header("Content-Disposition: attachment; filename=\"$downloadfilename.csv\""); + $hdr = "username,firstname,lastname,grade,submissioncomment,date\r\n"; + $hdr = (ord($hdr{0}) == 0xef && ord($hdr{1}) == 0xbb && ord($hdr{2}) == 0xbf) ? substr($hdr, 3) : $hdr; + echo mb_convert_encoding($hdr, "sjis-win", "UTF-8"); + foreach ($ausers as $auser) { + $grade = (!empty($auser->submissionid)) ? $auser->grade : -1; + $username = $auser->username; + $firstname = $auser->firstname; + $lastname = $auser->lastname; + $date = date("Y/n/j G:i", $auser->timemarked); + $comment = $auser->submissioncomment; + $str = "$username,$firstname,$lastname,$grade,$comment,$date\r\n"; + $str = (ord($str{0}) == 0xef && ord($str{1}) == 0xbb && ord($str{2}) == 0xbf) ? substr($str, 3) : $str; + echo mb_convert_encoding($str, "sjis-win", "UTF-8"); + } + } + return; + } + function del_bom($str) { return (ord($str{0}) == 0xef && ord($str{1}) == 0xbb && ord($str{2}) == 0xbf) ? substr($str, 3) : $str;} + + /** + * Upload course submissions in csv format + */ + function upload_submissions() { + global $CFG, $db, $USER; + require_once($CFG->libdir.'/uploadlib.php'); + print_header_simple(format_string($this->assignment->name,true), "", + ''.$this->strassignments. + ' -> '. + format_string($this->assignment->name,true).' -> '. $this->strsubmissions, + '', '', true, update_module_button($this->cm->id, $course->id, $this->strassignment), + navmenu($course, $cm)); + $csv_encode = '/\&\#44/'; + if (isset($CFG->CSV_DELIMITER)) { + $csv_delimiter = '\\' . $CFG->CSV_DELIMITER; + $csv_delimiter2 = $CFG->CSV_DELIMITER; + if (isset($CFG->CSV_ENCODE)) $csv_encode = '/\&\#' . $CFG->CSV_ENCODE . '/'; + } else { + $csv_delimiter = "\,"; + $csv_delimiter2 = ","; + } + $um = new upload_manager('feedbackfile',false,false,null,false,0); + if ($um->preprocess_files() && confirm_sesskey()) { + $filename = $um->files['feedbackfile']['tmp_name']; + $text = ""; + if ($file = @fopen($filename, "rb", $use_include_path)) { + while (!feof($file)) $text .= preg_replace('!\r\n?!',"\n",fread($file, 1024)); + fclose($file); + } else { + error("$filename open error",'submissions.php?sesskey='.$USER->sesskey); + exit; + } + $fp = fopen($filename, "w"); + fwrite($fp,$text); + fclose($fp); + $fp = fopen($filename, "r"); + // $columns = get_records_sql('SHOW COLUMNS FROM '.$CFG->prefix.'assignment_submissions'); + // var_dump($columns); + $required = array("username"=>1,"grade"=>1,"submissioncomment"=>1,"date"=>1); + $optional = array("firstname"=>1,"lastname"=>1); + $str = fgets($fp,1024); + $str = mb_convert_encoding($str, "UTF-8", "sjis-win"); + $header = split($csv_delimiter, $str); + // check for valid field names + foreach ($header as $i => $h) { + $h = trim($h); $header[$i] = $h; // remove whitespace + if (!($required[$h] or $optional[$h])) { + error(get_string('invalidfieldname', 'error', $h), 'submissions.php?id='.$this->cm->id); + } + if ($required[$h]) $required[$h] = 0; + } + foreach ($required as $key => $value) if ($value) error(get_string('fieldrequired', 'error', $key),'submissions.php?sesskey='. + $USER->sesskey); + $linenum = 2; // since header is line 1 + $newadded= 0; + $updated = 0; + $errors = 0; + while (!feof ($fp)) { + $str = fgets($fp,1024); + $str = mb_convert_encoding($str, "UTF-8", "sjis-win"); + $line = split($csv_delimiter,$str); + if($line[0]=="") break; + foreach ($line as $key => $value) { + $record[$header[$key]] = preg_replace($csv_encode,$csv_delimiter2,trim($value)); + if($header[$key]=='date') { + $datetime = $record[$header[$key]]; + if(preg_match("/^(\d\d\d\d)\/(\d+)\/(\d+)\s+(\d+):(\d+).*$/", $datetime, $val)) { + $record[$header[$key]] = mktime($val[4], $val[5], 0, $val[2], $val[3], $val[1]); + } else { + $a = new Object; + $a->key = $header[$key]; + $a->date = $datetime; + echo("
".get_string('formaterror','assignment',$a)."
"); + $errors++; + continue; + } + } + } + if ($record[$header[0]]) { + foreach ($record as $name => $value) { + if ($required[$name] and !$value) { + error(get_string('missingfield', 'error', $name). " ".get_string('erroronline', 'error', $linenum) .". ".get_string('processingstops', 'error'),'uploadfeedback.php?sesskey='.$USER->sesskey); + } else {// normal entry + // $feedback->{$name} = addslashes($value); + $feedback->{$name} = $value; + } + } + $feedback->timemarked = $feedback->date; + unset($feedback->date); + $linenum++; + $username = $feedback->username; + unset($feedback->username); + if ($studentuser = get_record("user","username",$username)) { + if(isset($header['firstname'])) { + if(!$studentuser->firstname == $feedback->firstname) { + echo("
".get_string('firstnameunmatched','assignment',$username)."
"); + $errors++; + continue; + } + } + if(isset($header['lastname'])) { + if(!$studentuser->lastname == $feedback->lastname) { + echo("
".get_string('lastnameunmatched','assignment',$username)."
"); + $errors++; + continue; + } + } + $feedback->userid = $studentuser->id; + } else { + echo("
".get_string('notregistered','assignment',$username)."
"); + $errors++; + continue; + } + // var_dump($feedback); + if ($oldfeedback = get_record("assignment_submissions","userid", + $feedback->userid,"assignment",$this->assignment->id)) {// Record is being updated + $feedback->id = $oldfeedback->id; + // if(!$feedback->timemodifieded) $feedback->timemodified = time(); + if(update_record('assignment_submissions', $feedback)) { + echo("
".get_string('updated','assignment',$username)."
"); + $updated++; + } else { + echo("
".get_string('updateerror','assignment',$username)."
"); + $errors++; + continue; + } + } else { + $feedback->timecreated = time(); + // $feedback->timemodified = time(); + $feedback->assignment = $this->assignment->id; + $feedback->teacher = $USER->id; + if ($feedback->id = insert_record("assignment_submissions", $feedback)) { + echo("
".get_string('added','assignment',$username)."
"); + $newadded++; + } else { + echo("
".get_string('addeerror','assignment',$username)."
"); + $errors++; + continue; + } + } + } + unset ($feedback); + } + fclose($fp); + echo("
"); + echo("
".get_string('addedrecords','assignment',$username)." : $newadded
"); + echo("
".get_string('updatedrecords','assignment',$username)." : $updated
"); + echo("
".get_string('errorrecords','assignment',$username)." : $errors
"); + echo("
"); + } + //select upload file + print_heading_with_help(get_string('uploadfeedback','assignment'), 'uploadfeedback'); + echo '
'; + echo get_string('file','moodle').' : '; + echo ''; + echo ''; + echo ''; + echo '
'; + print_footer($this->course); + } + /* + function dateconv($datetime) { + if(preg_match("/^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]).([0-5][0-9]):([0-5][0-9])$/", $datetime, $val)) { + return mktime($val[4], $val[5], 0, $val[2], $val[3], $val[1]); + } else { + return FALSE; + } + } + */ + /** * Process teacher feedback submission *