Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / grade / edit / outcome / import.php
blobd4a163a1d4c320f7e811411f8adb9324dcc12f4f
1 <?php // $Id$
2 // Allows a user to import outcomes (and associated scales)
4 ///////////////////////////////////////////////////////////////////////////
5 // //
6 // NOTICE OF COPYRIGHT //
7 // //
8 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
9 // http://moodle.com //
10 // //
11 // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
12 // //
13 // This program is free software; you can redistribute it and/or modify //
14 // it under the terms of the GNU General Public License as published by //
15 // the Free Software Foundation; either version 2 of the License, or //
16 // (at your option) any later version. //
17 // //
18 // This program is distributed in the hope that it will be useful, //
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21 // GNU General Public License for more details: //
22 // //
23 // http://www.gnu.org/copyleft/gpl.html //
24 // //
25 ///////////////////////////////////////////////////////////////////////////
27 /// THIS SCRIPT IS CALLED WITH "require_once()" FROM index.php
28 if (!defined('MOODLE_INTERNAL')) {
29 die('Direct access to this script is forbidden.');
32 $courseid = optional_param('id', 0, PARAM_INT);
33 $action = optional_param('action', '', PARAM_ALPHA);
34 $scope = optional_param('scope', 'global', PARAM_ALPHA);
36 /// Make sure they can even access this course
37 if ($courseid) {
38 if (!$course = get_record('course', 'id', $courseid)) {
39 print_error('nocourseid');
41 require_login($course);
42 $context = get_context_instance(CONTEXT_COURSE, $course->id);
44 if (empty($CFG->enableoutcomes)) {
45 redirect('../../index.php?id='.$courseid);
48 } else {
49 require_once $CFG->libdir.'/adminlib.php';
50 admin_externalpage_setup('outcomes');
51 $context = get_context_instance(CONTEXT_SYSTEM);
54 require_capability('moodle/grade:manageoutcomes', $context);
56 $strgrades = get_string('grades');
57 $pagename = get_string('outcomes', 'grades');
59 $navigation = grade_build_nav(__FILE__, $pagename, $courseid);
61 $strshortname = get_string('shortname');
62 $strfullname = get_string('fullname');
63 $strscale = get_string('scale');
64 $strstandardoutcome = get_string('outcomesstandard', 'grades');
65 $strcustomoutcomes = get_string('outcomescustom', 'grades');
66 $strdelete = get_string('delete');
67 $stredit = get_string('edit');
68 $srtcreatenewoutcome = get_string('outcomecreate', 'grades');
69 $stritems = get_string('items', 'grades');
70 $strcourses = get_string('courses');
71 $stredit = get_string('edit');
72 $strexport = get_string('export', 'grades');
74 if (!confirm_sesskey()) {
75 break;
78 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
79 $caneditsystemscales = has_capability('moodle/course:managescales', $systemcontext);
81 if ($courseid) {
82 /// Print header
83 print_header_simple($strgrades.': '.$pagename, ': '.$strgrades, $navigation, '', '', true, '', navmenu($course));
84 /// Print the plugin selector at the top
85 print_grade_plugin_selector($courseid, 'edit', 'outcome');
87 $caneditcoursescales = has_capability('moodle/course:managescales', $context);
89 $currenttab = 'outcomes';
90 require('tabs.php');
92 } else {
93 admin_externalpage_print_header();
94 $caneditcoursescales = $caneditsystemscales;
97 $imported_file = $upload_form->_upload_manager->files;
99 if ($imported_file['userfile']['size'] == 0) {
100 redirect('index.php'. ($courseid ? "?id=$courseid" : ''), get_string('importfilemissing', 'grades'));
103 /// which scope are we importing the outcomes in?
104 if (isset($courseid) && ($scope == 'local')) {
105 // custom scale
106 $local_scope = true;
107 } elseif (($scope == 'global') && has_capability('moodle/grade:manage', get_context_instance(CONTEXT_SYSTEM))) {
108 // global scale
109 $local_scope = false;
110 } else {
111 // shouldn't happen .. user might be trying to access this script without the right permissions.
112 redirect('index.php', get_string('importerror', 'grades'));
115 // open the file, start importing data
116 if ($handle = fopen($imported_file['userfile']['tmp_name'], 'r')) {
117 $line = 0; // will keep track of current line, to give better error messages.
118 $file_headers = '';
120 // $csv_data needs to have at least these columns, the value is the default position in the data file.
121 $headers = array('outcome_name' => 0, 'outcome_shortname' => 1, 'scale_name' => 3, 'scale_items' => 4);
122 $optional_headers = array('outcome_description'=>2, 'scale_description' => 5);
123 $imported_headers = array(); // will later be initialized with the values found in the file
125 // data should be separated by a ';'. *NOT* by a comma! TODO: version 2.0
126 // or whenever we can depend on PHP5, set the second parameter (8192) to 0 (unlimited line length) : the database can store over 128k per line.
127 while ( $csv_data = fgetcsv($handle, 8192, ';', '"')) { // if the line is over 8k, it won't work...
128 $line++;
130 // be tolerant on input, as fgetcsv returns "an array comprising a single null field" on blank lines
131 if ($csv_data == array(null)) {
132 continue;
135 // on first run, grab and analyse the header
136 if ($file_headers == '') {
138 $file_headers = array_flip($csv_data); // save the header line ... TODO: use the header line to let import work with columns in arbitrary order
140 $error = false;
141 foreach($headers as $key => $value) {
142 // sanity check #1: make sure the file contains all the mandatory headers
143 if (!array_key_exists($key, $file_headers)) {
144 $error = true;
145 break;
148 if ($error) {
149 print_box(get_string('importoutcomenofile', 'grades', $line));
150 break;
153 foreach(array_merge($headers, $optional_headers) as $header => $position) {
154 // match given columns to expected columns *into* $headers
155 $imported_headers[$header] = $file_headers[$header];
158 continue; // we don't import headers
161 // sanity check #2: every line must have the same number of columns as there are
162 // headers. If not, processing stops.
163 if ( count($csv_data) != count($file_headers) ) {
164 print_box(get_string('importoutcomenofile', 'grades', $line));
165 //print_box(var_export($csv_data, true) ."<br />". var_export($header, true));
166 break;
169 // sanity check #3: all required fields must be present on the current line.
170 foreach ($headers as $header => $position) {
171 if ($csv_data[$imported_headers[$header]] == '') {
172 print_box(get_string('importoutcomenofile', 'grades', $line));
173 break;
177 //var_dump($csv_data);
178 //$db->debug = 3498723498237; // .. very large randomly-typed random value
180 if ($local_scope) {
181 $outcome = get_records_select('grade_outcomes', 'shortname = "'. $csv_data[$imported_headers['outcome_shortname']] .'" and courseid = '. $courseid );
182 } else {
183 $outcome = get_records_select('grade_outcomes', 'shortname = "'. $csv_data[$imported_headers['outcome_shortname']] .'" and courseid is null');
185 //var_export($outcome);
187 if ($outcome) {
188 // already exists, print a message and skip.
189 print_box(get_string('importskippedoutcome', 'grades', $csv_data[$imported_headers['outcome_shortname']]));
190 continue;
193 // new outcome will be added, search for compatible existing scale...
194 $scale = get_records_select('scale', 'name ="'. $csv_data[$imported_headers['scale_name']] .'" and scale ="'. $csv_data[$imported_headers['scale_items']] .'" and (courseid = '. $courseid .' or courseid = 0)');
196 if ($scale) {
197 // already exists in the right scope: use it.
198 $scale_id = key($scale);
199 } else {
200 if (!has_capability('moodle/course:managescales', $context)) {
201 print_box(get_string('importskippednomanagescale', 'grades', $csv_data[$imported_headers['outcome_shortname']]));
202 continue;
203 } else {
204 // scale doesn't exists : create it.
205 $scale_data = array('name' => $csv_data[$imported_headers['scale_name']],
206 'scale' => $csv_data[$imported_headers['scale_items']],
207 'description' => $csv_data[$imported_headers['scale_description']],
208 'userid' => $USER->id);
210 if ($local_scope) {
211 $scale_data['courseid'] = $courseid;
212 } else {
213 $scale_data['courseid'] = 0; // 'global' : scale use '0', outcomes use null
215 $scale = new grade_scale($scale_data);
216 $scale_id = $scale->insert();
220 // add outcome
221 $outcome_data = array('shortname' => $csv_data[$imported_headers['outcome_shortname']],
222 'fullname' => $csv_data[$imported_headers['outcome_name']],
223 'scaleid' => $scale_id,
224 'description' => $csv_data[$imported_headers['outcome_description']],
225 'usermodified' => $USER->id);
227 if ($local_scope) {
228 $outcome_data['courseid'] = $courseid;
229 } else {
230 $outcome_data['courseid'] = null; // 'global' : scale use '0', outcomes use null
232 $outcome = new grade_outcome($outcome_data);
233 $outcome_id = $outcome->insert();
235 $outcome_success_strings = new StdClass();
236 $outcome_success_strings->name = $outcome_data['fullname'];
237 $outcome_success_strings->id = $outcome_id;
238 print_box(get_string('importoutcomesuccess', 'grades', $outcome_success_strings));
240 } else {
241 print_box(get_string('importoutcomenofile', 'grades', 0));
244 // finish
245 fclose($handle);
247 if ($courseid) {
248 print_footer($course);
249 } else {
250 admin_externalpage_print_footer();