Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / grade / import / xml / import.php
blob960c40e24c9489cf016d3e854a77a18f36ac5dfe
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
32 $feedback = optional_param('feedback', 0, PARAM_BOOL);
34 if (!$course = get_record('course', 'id', $id)) {
35 print_error('nocourseid');
38 require_login($course);
39 $context = get_context_instance(CONTEXT_COURSE, $id);
41 require_capability('moodle/grade:import', $context);
42 require_capability('gradeimport/xml:view', $context);
45 // Large files are likely to take their time and memory. Let PHP know
46 // that we'll take longer, and that the process should be recycled soon
47 // to free up memory.
48 @set_time_limit(0);
49 @raise_memory_limit("256M");
50 if (function_exists('apache_child_terminate')) {
51 @apache_child_terminate();
54 $text = download_file_content($url);
55 if ($text === false) {
56 error('Can not read file');
59 $error = '';
60 $importcode = import_xml_grades($text, $course, $error);
62 if ($importcode !== false) {
63 /// comit the code if we are up this far
65 if (defined('USER_KEY_LOGIN')) {
66 if (grade_import_commit($id, $importcode, $feedback, false)) {
67 echo 'ok';
68 die;
69 } else {
70 error('Grade import error'); //TODO: localize
73 } else {
74 $strgrades = get_string('grades', 'grades');
75 $actionstr = get_string('xml', 'grades');
76 $navigation = grade_build_nav(__FILE__, $actionstr, array('courseid' => $course->id));
78 print_header($course->shortname.': '.get_string('grades'), $course->fullname, $navigation);
79 print_grade_plugin_selector($id, 'import', 'xml');
81 grade_import_commit($id, $importcode, $feedback, true);
83 print_footer();
84 die;
87 } else {
88 error($error);