next up previous contents
Next: grade_import_form.php Up: 成績 Previous: 匯入程式修改   Contents

csv/index.php

  1. 由於系統匯入成績來源來自不同的 moodle 或 excel 資料,欄位比對主要還是以學號姓名為主,所以刪除 userid, username,useridnumber 等欄位,新增 firstname(姓名), lastname(學號) 兩個欄位做比對。而姓名因 excel 檔使用 big5 編碼與系統的 utf8 不同,經常造成比對錯誤,所以主要還是以學號比對為主。然而系統一開始並未對姓名學號做不可重複的設定,有部分學生以不同的 email 重複申請,所以在匯入成績時,有時因找到多筆資料中的其中一筆學生並未在課程中,而無法匯入。為解決此問題,增加先檢查學生是否在課程中,並以在課程中的帳號為主,先取得課程選課方式。
    [root@dywang grade]# diff -urNwbBE /var/www/html/moodle/grade/import/csv/index.php  import/csv/index.php 
    --- /var/www/html/moodle/grade/import/csv/index.php	2012-06-26 01:14:18.000000000 +0800
    +++ import/csv/index.php	2016-06-24 06:45:06.296821882 +0800
    @@ -226,6 +226,7 @@
             $newgradeitems = array(); // temporary array to keep track of what new headers are processed
             $status = true;
     
    +        $enrols = $DB->get_records('enrol', array('courseid' => $course->id, 'roleid' => "5"));
             while (!feof ($fp)) {
                 // add something
                 $line = explode($csv_delimiter, fgets($fp, GRADE_CSV_LINE_LENGTH));
    
  2. 刪除 userid, username,useridnumber 等欄位,新增 firstname(姓名), lastname(學號) 兩個欄位。
    @@ -265,7 +266,7 @@
                     }
     
                     switch ($t0) {
    -                    case 'userid': //
    +            /*        case 'userid': //
                             if (!$user = $DB->get_record('user', array('id' => $value))) {
                                 // user not found, abort whole import
                                 import_cleanup($importcode);
    @@ -285,7 +286,16 @@
                             }
                             $studentid = $user->id;
                         break;
    -                    case 'useremail':
    +                    case 'username':
    +                        if (!$user = $DB->get_record('user', array('username' => $value))) {
    +                            import_cleanup($importcode);
    +                            echo $OUTPUT->notification("user mapping error, could not find user with username \"$value\"");
    +                            $status = false;
    +                            break 3;
    +                        }
    +                        $studentid = $user->id;
    +                    break;
    +            */        case 'useremail':
                             if (!$user = $DB->get_record('user', array('email' => $value))) {
                                 import_cleanup($importcode);
                                 echo $OUTPUT->notification("user mapping error, could not find user with email address \"$value\"");
    @@ -294,15 +304,31 @@
                             }
                             $studentid = $user->id;
                         break;
    -                    case 'username':
    -                        if (!$user = $DB->get_record('user', array('username' => $value))) {
    +                    case 'firstname':
    +                        if (!$user = $DB->get_record('user', array('firstname' => $value))) {
                                 import_cleanup($importcode);
    -                            echo $OUTPUT->notification("user mapping error, could not find user with username \"$value\"");
    +                            echo $OUTPUT->notification("user mapping error, could not find user with firstname \"$value\"");
                                 $status = false;
                                 break 3;
                             }
                             $studentid = $user->id;
                         break;
    
  3. 學號比對若有多筆符合,再檢查是否在課程名單中,若有則匯入以此帳號為主。
    +                    case 'lastname':
    +                        if (!$users = $DB->get_records('user', array('lastname' => $value))) {
    +                            import_cleanup($importcode);
    +                            echo $OUTPUT->notification("user mapping error, could not find user with lastname \"$value\"");
    +                            $status = false;
    +                            break 3;
    +                        }
    +                        foreach($users as $user){
    +                            foreach($enrols as $enrol){
    +                                if ($DB->get_record('user_enrolments', array('enrolid' => $enrol->id ,'userid' => $user->id))) {
    +                                   $studentid = $user->id;
    +                                   break;
    +                                }
    +                           }
    +                        }      
    +                    break;
                         case 'new':
                             // first check if header is already in temp database
    



2018-08-14