MDL-22252 "couple of typos causing errors when importing glossary contents"
[moodle.git] / mod / glossary / import.php
CommitLineData
5fa0208e 1<?php
748b1932 2
5fa0208e 3require_once("../../config.php");
4require_once("lib.php");
5require_once("$CFG->dirroot/course/lib.php");
748b1932 6
5fa0208e 7$id = required_param('id', PARAM_INT); // Course Module ID
748b1932 8
5fa0208e 9$step = optional_param('step', 0, PARAM_INT);
10$dest = optional_param('dest', 'current', PARAM_ALPHA); // current | new
11$file = optional_param('file', '', PARAM_FILE); // file to import
12$catsincl = optional_param('catsincl', 0, PARAM_INT); // Import Categories too?
748b1932 13
5fa0208e 14$mode = optional_param('mode', 'letter', PARAM_ALPHA );
15$hook = optional_param('hook', 'ALL', PARAM_ALPHANUM);
748b1932 16
a6855934 17$url = new moodle_url('/mod/glossary/import.php', array('id'=>$id));
5fa0208e 18if ($step !== 0) {
19 $url->param('step', $step);
20}
21if ($dest !== 'current') {
22 $url->param('dest', $dest);
23}
24if ($file !== '') {
25 $url->param('file', $file);
26}
27if ($catsincl !== 0) {
28 $url->param('catsincl', $catsincl);
29}
30if ($mode !== 'letter') {
31 $url->param('mode', $mode);
32}
33if ($hook !== 'ALL') {
34 $url->param('hook', $hook);
35}
36$PAGE->set_url($url);
9f555b8f 37
5fa0208e 38if (! $cm = get_coursemodule_from_id('glossary', $id)) {
39 print_error('invalidcoursemodule');
40}
fe32b4f6 41
5fa0208e 42if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
43 print_error('coursemisconf');
44}
fe32b4f6 45
5fa0208e 46if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
47 print_error('invalidid', 'glossary');
48}
9f555b8f 49
5fa0208e 50require_login($course->id, false, $cm);
748b1932 51
5fa0208e 52$context = get_context_instance(CONTEXT_MODULE, $cm->id);
53require_capability('mod/glossary:import', $context);
748b1932 54
5fa0208e 55if ($dest != 'new' and $dest != 'current') {
56 $dest = 'current';
57}
58$strglossaries = get_string("modulenameplural", "glossary");
59$strglossary = get_string("modulename", "glossary");
60$strallcategories = get_string("allcategories", "glossary");
61$straddentry = get_string("addentry", "glossary");
62$strnoentries = get_string("noentries", "glossary");
63$strsearchconcept = get_string("searchconcept", "glossary");
64$strsearchindefinition = get_string("searchindefinition", "glossary");
65$strsearch = get_string("search");
66$strimportentries = get_string('importentriesfromxml', 'glossary');
67
a6855934 68$PAGE->set_url('/mod/glossary/import.php', array('id'=>$cm->id, 'mode'=>$mode, 'hook'=>$hook));
5fa0208e 69$PAGE->navbar->add($strimportentries);
70$PAGE->set_title(format_string($glossary->name));
5fa0208e 71
72echo $OUTPUT->header();
73echo $OUTPUT->heading($strimportentries);
74
75if ( !$step ) {
76 echo $OUTPUT->box_start('glossarydisplay generalbox');
77 include("import.html");
78 echo $OUTPUT->box_end();
73e6ac63 79
5fa0208e 80 echo $OUTPUT->footer();
81 exit;
82}
18b8fbfa 83
dba386b5 84require_sesskey();
5fa0208e 85$form = data_submitted();
86$file = $_FILES["file"];
6ca98074 87
5fa0208e 88require_once($CFG->dirroot.'/lib/uploadlib.php');
89$um = new upload_manager('file',false,false,$course,false,0);
18b8fbfa 90
5fa0208e 91if (!$um->preprocess_files()) {
92 echo $OUTPUT->box_start('glossarydisplay generalbox');
93 echo $OUTPUT->continue_button('import.php?id='.$id);
94 echo $OUTPUT->box_end();
748b1932 95
5fa0208e 96 echo $OUTPUT->footer();
97 die();
98}
73e6ac63 99
5fa0208e 100if ($xml = glossary_read_imported_file($file['tmp_name']) ) {
101
102 $importedentries = 0;
103 $importedcats = 0;
104 $entriesrejected = 0;
105 $rejections = '';
106 if ($dest == 'new') {
107 // If the user chose to create a new glossary
108 $xmlglossary = $xml['GLOSSARY']['#']['INFO'][0]['#'];
109
110 if ( $xmlglossary['NAME'][0]['#'] ) {
111 unset($glossary);
112 $glossary->name = ($xmlglossary['NAME'][0]['#']);
113 $glossary->course = $course->id;
114 $glossary->globalglossary = ($xmlglossary['GLOBALGLOSSARY'][0]['#']);
115 $glossary->intro = ($xmlglossary['INTRO'][0]['#']);
116 $glossary->showspecial = ($xmlglossary['SHOWSPECIAL'][0]['#']);
117 $glossary->showalphabet = ($xmlglossary['SHOWALPHABET'][0]['#']);
118 $glossary->showall = ($xmlglossary['SHOWALL'][0]['#']);
119 $glossary->timecreated = time();
120 $glossary->timemodified = time();
121
122 // Setting the default values if no values were passed
123 if ( isset($xmlglossary['ENTBYPAGE'][0]['#']) ) {
124 $glossary->entbypage = ($xmlglossary['ENTBYPAGE'][0]['#']);
125 } else {
126 $glossary->entbypage = $CFG->glossary_entbypage;
127 }
128 if ( isset($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']) ) {
129 $glossary->allowduplicatedentries = ($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']);
130 } else {
131 $glossary->allowduplicatedentries = $CFG->glossary_dupentries;
132 }
133 if ( isset($xmlglossary['DISPLAYFORMAT'][0]['#']) ) {
134 $glossary->displayformat = ($xmlglossary['DISPLAYFORMAT'][0]['#']);
135 } else {
136 $glossary->displayformat = 2;
137 }
138 if ( isset($xmlglossary['ALLOWCOMMENTS'][0]['#']) ) {
139 $glossary->allowcomments = ($xmlglossary['ALLOWCOMMENTS'][0]['#']);
140 } else {
141 $glossary->allowcomments = $CFG->glossary_allowcomments;
142 }
143 if ( isset($xmlglossary['USEDYNALINK'][0]['#']) ) {
144 $glossary->usedynalink = ($xmlglossary['USEDYNALINK'][0]['#']);
145 } else {
146 $glossary->usedynalink = $CFG->glossary_linkentries;
147 }
148 if ( isset($xmlglossary['DEFAULTAPPROVAL'][0]['#']) ) {
149 $glossary->defaultapproval = ($xmlglossary['DEFAULTAPPROVAL'][0]['#']);
150 } else {
151 $glossary->defaultapproval = $CFG->glossary_defaultapproval;
152 }
73e6ac63 153
5fa0208e 154 // Include new glossary and return the new ID
155 if ( !$glossary->id = glossary_add_instance($glossary) ) {
156 echo $OUTPUT->notification("Error while trying to create the new glossary.");
157 echo '</center>';
158 glossary_print_tabbed_table_end();
159 echo $OUTPUT->footer();
160 exit;
161 } else {
162 //The instance has been created, so lets do course_modules
163 //and course_sections
164 $mod->groupmode = $course->groupmode; /// Default groupmode the same as course
73e6ac63 165
5fa0208e 166 $mod->instance = $glossary->id;
167 // course_modules and course_sections each contain a reference
168 // to each other, so we have to update one of them twice.
73e6ac63 169
5fa0208e 170 if (! $currmodule = $DB->get_record("modules", array("name"=>'glossary'))) {
171 print_error('modulenotexist', 'debug', '', 'Glossary');
172 }
173 $mod->module = $currmodule->id;
174 $mod->course = $course->id;
175 $mod->modulename = 'glossary';
176 $mod->section = 0;
73e6ac63 177
5fa0208e 178 if (! $mod->coursemodule = add_course_module($mod) ) {
179 print_error('cannotaddcoursemodule');
180 }
73e6ac63 181
5fa0208e 182 if (! $sectionid = add_mod_to_section($mod) ) {
183 print_error('cannotaddcoursemoduletosection');
184 }
185 //We get the section's visible field status
186 $visible = $DB->get_field("course_sections", "visible", array("id"=>$sectionid));
73e6ac63 187
5fa0208e 188 $DB->set_field("course_modules", "visible", $visible, array("id"=>$mod->coursemodule));
189 $DB->set_field("course_modules", "section", $sectionid, array("id"=>$mod->coursemodule));
73e6ac63 190
5fa0208e 191 add_to_log($course->id, "course", "add mod",
192 "../mod/$mod->modulename/view.php?id=$mod->coursemodule",
193 "$mod->modulename $mod->instance");
194 add_to_log($course->id, $mod->modulename, "add",
195 "view.php?id=$mod->coursemodule",
196 "$mod->instance", $mod->coursemodule);
197
198 rebuild_course_cache($course->id);
199
200 echo $OUTPUT->box(get_string("newglossarycreated","glossary"),'generalbox boxaligncenter boxwidthnormal');
201 echo '<p>';
748b1932 202 }
5fa0208e 203 } else {
204 echo $OUTPUT->notification("Error while trying to create the new glossary.");
205 echo $OUTPUT->footer();
206 exit;
748b1932 207 }
5fa0208e 208 }
748b1932 209
5fa0208e 210 $xmlentries = $xml['GLOSSARY']['#']['INFO'][0]['#']['ENTRIES'][0]['#']['ENTRY'];
211 for($i = 0; $i < sizeof($xmlentries); $i++) {
212 // Inserting the entries
213 $xmlentry = $xmlentries[$i];
214 unset($newentry);
215 $newentry->concept = trim($xmlentry['#']['CONCEPT'][0]['#']);
216 $newentry->definition = trusttext_strip($xmlentry['#']['DEFINITION'][0]['#']);
217 if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) {
218 $newentry->casesensitive = $xmlentry['#']['CASESENSITIVE'][0]['#'];
219 } else {
220 $newentry->casesensitive = $CFG->glossary_casesensitive;
221 }
748b1932 222
5fa0208e 223 $permissiongranted = 1;
224 if ( $newentry->concept and $newentry->definition ) {
225 if ( !$glossary->allowduplicatedentries ) {
226 // checking if the entry is valid (checking if it is duplicated when should not be)
227 if ( $newentry->casesensitive ) {
228 $dupentry = $DB->get_record("glossary_entries", array("concept"=>$newentry->concept, "glossaryid"=>$glossary->id));
748b1932 229 } else {
c7a2b2c8 230 $dupentry = $DB->get_record("glossary_entries", array("lower(concept)"=>moodle_strtolower($newentry->concept), "glossaryid"=>$glossary->id));
748b1932 231 }
5fa0208e 232 if ($dupentry) {
233 $permissiongranted = 0;
748b1932 234 }
5fa0208e 235 }
236 } else {
237 $permissiongranted = 0;
238}
239 if ($permissiongranted) {
240 $newentry->glossaryid = $glossary->id;
241 $newentry->sourceglossaryid = 0;
242 $newentry->approved = 1;
243 $newentry->userid = $USER->id;
244 $newentry->teacherentry = 1;
245 $newentry->format = $xmlentry['#']['FORMAT'][0]['#'];
246 $newentry->timecreated = time();
247 $newentry->timemodified = time();
248
249 // Setting the default values if no values were passed
250 if ( isset($xmlentry['#']['USEDYNALINK'][0]['#']) ) {
251 $newentry->usedynalink = $xmlentry['#']['USEDYNALINK'][0]['#'];
252 } else {
253 $newentry->usedynalink = $CFG->glossary_linkentries;
254 }
255 if ( isset($xmlentry['#']['FULLMATCH'][0]['#']) ) {
256 $newentry->fullmatch = $xmlentry['#']['FULLMATCH'][0]['#'];
257 } else {
258 $newentry->fullmatch = $CFG->glossary_fullmatch;
259 }
748b1932 260
5fa0208e 261 if ( $newentry->id = $DB->insert_record("glossary_entries",$newentry) ) {
262 $importedentries++;
7965be79 263
5fa0208e 264 $xmlaliases = @$xmlentry['#']['ALIASES'][0]['#']['ALIAS']; // ignore missing ALIASES
265 for($k = 0; $k < sizeof($xmlaliases); $k++) {
266 /// Importing aliases
267 $xmlalias = $xmlaliases[$k];
268 $aliasname = $xmlalias['#']['NAME'][0]['#'];
e1013df8 269
5fa0208e 270 if (!empty($aliasname)) {
271 $newalias = new object();
272 $newalias->entryid = $newentry->id;
273 $newalias->alias = trim($aliasname);
274 $newalias->id = $DB->insert_record("glossary_alias",$newalias);
7965be79 275 }
5fa0208e 276 }
7965be79 277
5fa0208e 278 if ( $catsincl ) {
279 // If the categories must be imported...
280 $xmlcats = @$xmlentry['#']['CATEGORIES'][0]['#']['CATEGORY']; // ignore missing CATEGORIES
281 for($k = 0; $k < sizeof($xmlcats); $k++) {
282 $xmlcat = $xmlcats[$k];
283
284 $newcat = new object();
285 $newcat->name = $xmlcat['#']['NAME'][0]['#'];
286 $newcat->usedynalink = $xmlcat['#']['USEDYNALINK'][0]['#'];
287 if ( !$category = $DB->get_record("glossary_categories", array("glossaryid"=>$glossary->id,"name"=>$newcat->name))) {
288 // Create the category if it does not exist
289 $category = new object();
290 $category->name = $newcat->name;
291 $category->glossaryid = $glossary->id;
292 $category->id = $DB->insert_record("glossary_categories",$category);
293 $importedcats++;
294 }
295 if ( $category ) {
296 // inserting the new relation
c7a2b2c8 297 $entrycat = new object();
5fa0208e 298 $entrycat->entryid = $newentry->id;
299 $entrycat->categoryid = $category->id;
300 $DB->insert_record("glossary_entries_categories",$entrycat);
748b1932 301 }
302 }
748b1932 303 }
304 } else {
305 $entriesrejected++;
5fa0208e 306 // add to exception report (can't insert new record)
307 $rejections .= "<tr><td>$newentry->concept</td>" .
308 "<td>" . get_string("cantinsertrec","glossary"). "</td></tr>";
309 }
310 } else {
311 $entriesrejected++;
312 if ( $newentry->concept and $newentry->definition ) {
313 // add to exception report (duplicated entry))
314 $rejections .= "<tr><td>$newentry->concept</td>" .
315 "<td>" . get_string("duplicateentry","glossary"). "</td></tr>";
316 } else {
317 // add to exception report (no concept or definition found))
318 $rejections .= "<tr><td>---</td>" .
319 "<td>" . get_string("noconceptfound","glossary"). "</td></tr>";
748b1932 320 }
321 }
5fa0208e 322 }
323 // processed entries
324 echo $OUTPUT->box_start('glossarydisplay generalbox');
325 echo '<table class="glossaryimportexport">';
326 echo '<tr>';
327 echo '<td width="50%" align="right">';
328 echo get_string("totalentries","glossary");
329 echo ':</td>';
330 echo '<td width="50%" align="left">';
331 echo $importedentries + $entriesrejected;
332 echo '</td>';
333 echo '</tr>';
334 echo '<tr>';
335 echo '<td width="50%" align="right">';
336 echo get_string("importedentries","glossary");
337 echo ':</td>';
338 echo '<td width="50%" align="left">';
339 echo $importedentries;
340 if ( $entriesrejected ) {
341 echo ' <small>(' . get_string("rejectedentries","glossary") . ": $entriesrejected)</small>";
342 }
343 echo '</td>';
344 echo '</tr>';
345 if ( $catsincl ) {
73e6ac63 346 echo '<tr>';
41905731 347 echo '<td width="50%" align="right">';
5fa0208e 348 echo get_string("importedcategories","glossary");
73e6ac63 349 echo ':</td>';
5fa0208e 350 echo '<td width="50%">';
351 echo $importedcats;
73e6ac63 352 echo '</td>';
353 echo '</tr>';
5fa0208e 354 }
355 echo '</table><hr />';
748b1932 356
5fa0208e 357 // rejected entries
358 if ($rejections) {
359 echo '<table class="glossaryimportexport">';
360 echo '<tr><td align="center" colspan="2" width="100%"><strong>' . get_string("rejectionrpt","glossary") . '</strong></tr>';
361 echo $rejections;
362 echo '</table><hr />';
363 }
364/// Print continue button, based on results
365 if ($importedentries) {
366 echo $OUTPUT->continue_button('view.php?id='.$id);
748b1932 367 } else {
5fa0208e 368 echo $OUTPUT->continue_button('import.php?id='.$id);
748b1932 369 }
5fa0208e 370 echo $OUTPUT->box_end();
371} else {
372 notice(get_string('errorparsingxml', 'glossary'));
373}
748b1932 374
748b1932 375/// Finish the page
5fa0208e 376echo $OUTPUT->footer();
748b1932 377