~ubuntu-branches/ubuntu/jaunty/moodle/jaunty

« back to all changes in this revision

Viewing changes to grade/import/xml/lib.php

  • Committer: Bazaar Package Importer
  • Author(s): Jordan Mantha, Matt Oquist
  • Date: 2009-02-25 15:16:22 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090225151622-0ekt1liwhv2obfza
Tags: 1.9.4.dfsg-0ubuntu1
* Merge with Debian git (Closes LP: #322961, #239481, #334611):
  - use Ubuntu's smarty lib directory for linking
  - use internal yui library 
  - add update-notifier support back in

[Matt Oquist]
  * renamed prerm script
  * significantly rewrote postinst and other maintainer scripts to improve
    user experience and package maintainability
    (Closes LP: #225662, #325450, #327843, #303078, #234609)

Show diffs side-by-side

added added

removed removed

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