when no gradebook roles is set, there are lots of sql breakages so we should check...
[moodle-pu.git] / grade / import / xml / import.php
bloba52370842fef05d2d07d45d2ab73bc7d5d9b49d2
1 <?php //$Id$
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 ///////////////////////////////////////////////////////////////////////////
26 require_once '../../../config.php';
27 require_once 'lib.php';
28 require_once $CFG->libdir.'/filelib.php';
30 $url = required_param('url', PARAM_URL); // only real urls here
31 $id = required_param('id', PARAM_INT); // course id
33 if (!$course = get_record('course', 'id', $id)) {
34 print_error('nocourseid');
37 require_login($course);
38 $context = get_context_instance(CONTEXT_COURSE, $id);
40 require_capability('moodle/grade:import', $context);
41 require_capability('gradeimport/xml:view', $context);
44 // Large files are likely to take their time and memory. Let PHP know
45 // that we'll take longer, and that the process should be recycled soon
46 // to free up memory.
47 @set_time_limit(0);
48 @raise_memory_limit("256M");
49 if (function_exists('apache_child_terminate')) {
50 @apache_child_terminate();
53 $text = download_file_content($url);
54 if ($text === false) {
55 error('Can not read file');
58 $error = '';
59 $importcode = import_xml_grades($text, $course, $error);
61 if ($importcode !== false) {
62 /// comit the code if we are up this far
64 if (defined('USER_KEY_LOGIN')) {
65 if (grade_import_commit($id, $importcode, false)) {
66 echo 'ok';
67 die;
68 } else {
69 error('Grade import error'); //TODO: localize
72 } else {
73 $strgrades = get_string('grades', 'grades');
74 $actionstr = get_string('xml', 'grades');
75 $navigation = grade_build_nav(__FILE__, $actionstr, array('courseid' => $course->id));
77 print_header($course->shortname.': '.get_string('grades'), $course->fullname, $navigation);
78 print_grade_plugin_selector($id, 'import', 'xml');
80 grade_import_commit($id, $importcode);
82 print_footer();
83 die;
86 } else {
87 error($error);