Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
// This file is part of Moodle - http://moodle.org/
4
//
5
// Moodle is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// Moodle is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17
 
18
require_once $CFG->libdir.'/gradelib.php';
19
require_once($CFG->libdir.'/xmlize.php');
20
require_once $CFG->dirroot.'/grade/lib.php';
21
require_once $CFG->dirroot.'/grade/import/lib.php';
22
 
23
function import_xml_grades($text, $course, &$error) {
24
    global $USER, $DB;
25
 
26
    $importcode = get_new_importcode();
27
 
28
    $status = true;
29
 
30
    $content = xmlize($text);
31
 
32
    if (!empty($content['results']['#']['result'])) {
33
        $results = $content['results']['#']['result'];
34
 
35
        foreach ($results as $i => $result) {
36
            $gradeidnumber = $result['#']['assignment'][0]['#'];
37
            if (!$grade_items = grade_item::fetch_all(array('idnumber'=>$gradeidnumber, 'courseid'=>$course->id))) {
38
                // gradeitem does not exist
39
                // no data in temp table so far, abort
40
                $status = false;
41
                $error  = get_string('errincorrectgradeidnumber', 'gradeimport_xml', $gradeidnumber);
42
                break;
43
            } else if (count($grade_items) != 1) {
44
                $status = false;
45
                $error  = get_string('errduplicategradeidnumber', 'gradeimport_xml', $gradeidnumber);
46
                break;
47
            } else {
48
                $grade_item = reset($grade_items);
49
            }
50
 
51
            // grade item locked, abort
52
            if ($grade_item->is_locked()) {
53
                $status = false;
54
                $error  = get_string('gradeitemlocked', 'grades');
55
                break;
56
            }
57
 
58
            // check if user exist and convert idnumber to user id
59
            $useridnumber = $result['#']['student'][0]['#'];
60
            if (!$user = $DB->get_record('user', array('idnumber' =>$useridnumber))) {
61
                // no user found, abort
62
                $status = false;
63
                $error = get_string('errincorrectuseridnumber', 'gradeimport_xml', $useridnumber);
64
                break;
65
            }
66
 
67
            // check if grade_grade is locked and if so, abort
68
            if ($grade_grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$user->id))) {
69
                $grade_grade->grade_item =& $grade_item;
70
                if ($grade_grade->is_locked()) {
71
                    // individual grade locked, abort
72
                    $status = false;
73
                    $error  = get_string('gradelocked', 'grades');
74
                    break;
75
                }
76
            }
77
 
78
            $newgrade = new stdClass();
79
            $newgrade->itemid     = $grade_item->id;
80
            $newgrade->userid     = $user->id;
81
            $newgrade->importcode = $importcode;
82
            $newgrade->importer   = $USER->id;
83
 
84
            // check grade value exists and is a numeric grade
85
            if (isset($result['#']['score'][0]['#']) && $result['#']['score'][0]['#'] !== '-') {
86
                if (is_numeric($result['#']['score'][0]['#'])) {
87
                    $newgrade->finalgrade = $result['#']['score'][0]['#'];
88
                } else {
89
                    $status = false;
90
                    $error = get_string('badgrade', 'grades');
91
                    break;
92
                }
93
            } else {
94
                $newgrade->finalgrade = NULL;
95
            }
96
 
97
            // check grade feedback exists
98
            if (isset($result['#']['feedback'][0]['#'])) {
99
                $newgrade->feedback = $result['#']['feedback'][0]['#'];
100
            } else {
101
                $newgrade->feedback = NULL;
102
            }
103
 
104
            // insert this grade into a temp table
105
            $DB->insert_record('grade_import_values', $newgrade);
106
        }
107
 
108
    } else {
109
        // no results section found in xml,
110
        // assuming bad format, abort import
111
        $status = false;
112
        $error = get_string('errbadxmlformat', 'gradeimport_xml');
113
    }
114
 
115
    if ($status) {
116
        return $importcode;
117
 
118
    } else {
119
        import_cleanup($importcode);
120
        return false;
121
    }
122
}
123