-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocallib.php
257 lines (234 loc) · 7.74 KB
/
locallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Bulk user registration functions
*
* @package tool
* @subpackage uploadcourse
* @copyright 2004 onwards Martin Dougiamas (http://dougiamas.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Validate input during csv processing and fix as appropriate
*
* @param mixed $value
* @param string $key
* @param moodle_url $returnurl
* @param integer $linenum
*/
function uc_validate_item($value, $key, $returnurl, $linenum) {
switch($key) {
case 'category':
if (is_int($value)) {
if (uc_category_exists($value)) {
return $value;
} else {
print_error('erroronline', 'error', $returnurl, $linenum);
}
} else if (is_string($value)) {
// String path
$value = clean_param($value, PARAM_PATH); // clean
$categories = explode('/', $value);
array_walk($categories, 'trim');
return $categories;
} else {
// Must be a string or integer
print_error('erroronline', 'error', $returnurl, $linenum);
}
break;
case 'startdate':
if (is_int($value)) {
return $value;
} else {
return strtotime($value);
}
break;
case 'teacher_ids':
$teachers = explode('/', $value);
array_walk($teachers, 'trim');
return clean_param_array($teachers, PARAM_TEXT);
break;
default:
switch (gettype($value)) {
case 'boolean':
return clean_param($value, PARAM_BOOLEAN);
break;
case 'integer':
return clean_param($value, PARAM_INT);
break;
case 'double':
return clean_param($value, PARAM_FLOAT);
break;
case 'string':
default:
return clean_param($value, PARAM_TEXT);
break;
}
break;
}
}
/**
* Validation callback function - verified the column line of the csv field.
* Derived from uu_validate_user_upload_columns() in tool/uploaduser
* This will fail without a valid header row and at least one row of data
**/
function uc_validate_course_upload_columns(csv_import_reader $cir, $stdfields, moodle_url $returnurl) {
$columns = $cir->get_columns();
// Verify that we have a header row AND at least one row of data
if (empty($columns)) {
$cir->close();
$cir->cleanup();
print_error('cannotreadtmpfile', 'error', $returnurl);
} else if(count($columns) < 2) {
$cir->close();
$cir->cleanup();
print_error('csvfewcolumns', 'error', $returnurl);
}
// Textlib for handling unicode data
$textlib = textlib_get_instance();
// Cycle through the columns. Note that $value is unused.
$processed = array();
foreach ($columns as $key => $value) {
$field = $columns[$key];
$field_lc = $textlib->strtolower($field);
// Fields should be lower-cased. Check for presence in $stdfields and normalize
if (in_array($field, $stdfields) || in_array($field_lc, $stdfields)) {
$field_new = $field_lc;
} else {
$cir->close();
$cir->cleanup();
print_error('invalidfieldname', 'error', $returnurl, $field);
}
// Check for duplicates
if (in_array($field_new, $processed)) {
$cir->close();
$cir->cleanup();
print_error('duplicatefieldname', 'error', $returnurl, $field_new);
}
$processed[$key] = $field_new;
}
return $processed;
}
/**
* Checks if the given category id exists
*
* @param integer $category
* @return boolean
*/
function uc_category_exists($category) {
global $DB;
return $DB->record_exists('course_categories', array('id' => $category));
}
/**
* Finds the category with the given name and parent, or create it if it doesn't exist. Returns
* the category ID. Also sets the $hstatus variable:
* -1 : Failed to create category
* 1 : Category found
* 2 : Created new category
*
* @param string $hname
* @param integer $hstatus
* @param integer $hparent
* @param array $categories_cached
* @return integer
*/
function uc_category_create($hname, &$hstatus, $hparent=0, &$categories_cached) {
global $DB;
// Does this category exist?
$hash = md5($hname . $hparent);
if (array_key_exists($hash, $categories_cached)) {
$hstatus = 1;
return $categories_cached[$hash];
} else if ($result = $DB->get_record('course_categories', array('name' => $hname, 'parent' => $hparent))) {
$hstatus = 1;
$categories_cached[$hash] = $result->id;
return $result->id;
} else {
$data = new stdClass();
$data->name = $hname;
$data->parent = $hparent;
if ($id = $DB->insert_record('course_categories', $data, true)) {
$hstatus = 2;
$categories_cached[$hash] = $id;
return $id;
} else {
$hstatus = -1;
return -1;
}
}
}
/**
* Checks the existence of a course shortname
* @param string $shortname
* @return boolean
*/
function uc_course_shortname_exists($shortname) {
global $DB;
return $DB->record_exists('course', array('shortname' => $shortname)); // Check shortname is unique before inserting
}
/**
* Attempts to create a course. $coursedata contains the default course object
*
* @param integer $category
* @param array $course
* @param array $headers
* @param object $coursedata
*
* @return mixed
*/
function uc_create_course($category, $course, $headers, $coursedata) {
global $DB;
if(!is_array($course) || !is_array($headers)) {
return false;
}
// Add category
$course['category'] = $category;
// Split out teachers
$teachers = $course['teachers'];
unset($course['teachers']);
// Add items
foreach ($headers as $key => $value) {
if (!empty($course[$value])) {
$coursedata->{$value} = $course[$value];
}
}
$newcourse = create_course($coursedata);
// Enrol teachers
$context = get_context_instance(CONTEXT_COURSE, $newcourse->id);
foreach ($teachers as $teacher) {
role_assign(3, $teacher, $context->id);
}
return true;
}
/**
* Creates a status object to hold all the counters.
* @param array $courses
* @return object
*/
function uc_create_status_object($courses, $mteachers) {
$status = new stdClass();
$status->bulk = count($courses);
$status->skipped = 0;
$status->created = 0;
$status->broken = 0;
$status->read = 0;
$status->catcreated = 0;
$status->catbroken = 0;
$status->mteachers = array_unique($mteachers);
return $status;
}
?>