MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / glossary / import.php
bloba93d9bd57becf5ad80a6d5fe1179f8aee733a3ea
1 <?php // $Id$
3 require_once("../../config.php");
4 require_once("lib.php");
5 require_once("$CFG->dirroot/course/lib.php");
6 global $CFG, $USER;
8 $id = required_param('id', PARAM_INT); // Course Module ID
10 $step = optional_param('step', 0, PARAM_INT);
11 $dest = optional_param('dest', 'current', PARAM_ALPHA); // current | new
12 $file = optional_param('file', '', PARAM_FILE); // file to import
13 $catsincl = optional_param('catsincl', 0, PARAM_INT); // Import Categories too?
15 $mode = optional_param('mode', 'letter', PARAM_ALPHA );
16 $hook = optional_param('hook', 'ALL', PARAM_ALPHANUM);
18 if (! $cm = get_coursemodule_from_id('glossary', $id)) {
19 error("Course Module ID was incorrect");
22 if (! $course = get_record("course", "id", $cm->course)) {
23 error("Course is misconfigured");
26 if (! $glossary = get_record("glossary", "id", $cm->instance)) {
27 error("Course module is incorrect");
30 require_login($course->id, false, $cm);
32 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
33 require_capability('mod/glossary:import', $context);
35 if ($dest != 'new' and $dest != 'current') {
36 $dest = 'current';
38 $strglossaries = get_string("modulenameplural", "glossary");
39 $strglossary = get_string("modulename", "glossary");
40 $strallcategories = get_string("allcategories", "glossary");
41 $straddentry = get_string("addentry", "glossary");
42 $strnoentries = get_string("noentries", "glossary");
43 $strsearchconcept = get_string("searchconcept", "glossary");
44 $strsearchindefinition = get_string("searchindefinition", "glossary");
45 $strsearch = get_string("search");
46 $strimportentries = get_string('importentriesfromxml', 'glossary');
48 $navlinks = array();
49 $navlinks[] = array('name' => $strglossaries, 'link' => "index.php?id=$course->id", 'type' => 'activity');
50 $navlinks[] = array('name' => format_string($glossary->name), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
51 $navlinks[] = array('name' => $strimportentries, 'link' => '', 'type' => 'title');
53 $navigation = build_navigation($navlinks);
55 print_header_simple(format_string($glossary->name), "", $navigation,
56 "", "", true, update_module_button($cm->id, $course->id, $strglossary),
57 navmenu($course, $cm));
59 print_heading($strimportentries);
61 if ( !$step ) {
62 print_box_start('glossarydisplay generalbox');
63 include("import.html");
64 print_box_end();
66 print_footer($course);
67 exit;
70 $form = data_submitted();
71 $file = $_FILES["file"];
73 require_once($CFG->dirroot.'/lib/uploadlib.php');
74 $um = new upload_manager('file',false,false,$course,false,0);
76 if (!$um->preprocess_files()) {
77 print_box_start('glossarydisplay generalbox');
78 print_continue('import.php?id='.$id);
79 print_box_end();
81 print_footer();
82 die();
85 if ($xml = glossary_read_imported_file($file['tmp_name']) ) {
87 $importedentries = 0;
88 $importedcats = 0;
89 $entriesrejected = 0;
90 $rejections = '';
91 if ($dest == 'new') {
92 // If the user chose to create a new glossary
93 $xmlglossary = $xml['GLOSSARY']['#']['INFO'][0]['#'];
95 if ( $xmlglossary['NAME'][0]['#'] ) {
96 unset($glossary);
97 $glossary->name = addslashes($xmlglossary['NAME'][0]['#']);
98 $glossary->course = $course->id;
99 $glossary->globalglossary = addslashes($xmlglossary['GLOBALGLOSSARY'][0]['#']);
100 $glossary->intro = addslashes($xmlglossary['INTRO'][0]['#']);
101 $glossary->showspecial = addslashes($xmlglossary['SHOWSPECIAL'][0]['#']);
102 $glossary->showalphabet = addslashes($xmlglossary['SHOWALPHABET'][0]['#']);
103 $glossary->showall = addslashes($xmlglossary['SHOWALL'][0]['#']);
104 $glossary->timecreated = time();
105 $glossary->timemodified = time();
107 // Setting the default values if no values were passed
108 if ( isset($xmlglossary['ENTBYPAGE'][0]['#']) ) {
109 $glossary->entbypage = addslashes($xmlglossary['ENTBYPAGE'][0]['#']);
110 } else {
111 $glossary->entbypage = $CFG->glossary_entbypage;
113 if ( isset($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']) ) {
114 $glossary->allowduplicatedentries = addslashes($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']);
115 } else {
116 $glossary->allowduplicatedentries = $CFG->glossary_dupentries;
118 if ( isset($xmlglossary['DISPLAYFORMAT'][0]['#']) ) {
119 $glossary->displayformat = addslashes($xmlglossary['DISPLAYFORMAT'][0]['#']);
120 } else {
121 $glossary->displayformat = 2;
123 if ( isset($xmlglossary['ALLOWCOMMENTS'][0]['#']) ) {
124 $glossary->allowcomments = addslashes($xmlglossary['ALLOWCOMMENTS'][0]['#']);
125 } else {
126 $glossary->allowcomments = $CFG->glossary_allowcomments;
128 if ( isset($xmlglossary['USEDYNALINK'][0]['#']) ) {
129 $glossary->usedynalink = addslashes($xmlglossary['USEDYNALINK'][0]['#']);
130 } else {
131 $glossary->usedynalink = $CFG->glossary_linkentries;
133 if ( isset($xmlglossary['DEFAULTAPPROVAL'][0]['#']) ) {
134 $glossary->defaultapproval = addslashes($xmlglossary['DEFAULTAPPROVAL'][0]['#']);
135 } else {
136 $glossary->defaultapproval = $CFG->glossary_defaultapproval;
139 // Include new glossary and return the new ID
140 if ( !$glossary->id = glossary_add_instance($glossary) ) {
141 notify("Error while trying to create the new glossary.");
142 echo '</center>';
143 glossary_print_tabbed_table_end();
144 print_footer($course);
145 exit;
146 } else {
147 //The instance has been created, so lets do course_modules
148 //and course_sections
149 $mod->groupmode = $course->groupmode; /// Default groupmode the same as course
151 $mod->instance = $glossary->id;
152 // course_modules and course_sections each contain a reference
153 // to each other, so we have to update one of them twice.
155 if (! $currmodule = get_record("modules", "name", 'glossary')) {
156 error("Glossary module doesn't exist");
158 $mod->module = $currmodule->id;
159 $mod->course = $course->id;
160 $mod->modulename = 'glossary';
161 $mod->section = 0;
163 if (! $mod->coursemodule = add_course_module($mod) ) {
164 error("Could not add a new course module");
167 if (! $sectionid = add_mod_to_section($mod) ) {
168 error("Could not add the new course module to that section");
170 //We get the section's visible field status
171 $visible = get_field("course_sections","visible","id",$sectionid);
173 if (! set_field("course_modules", "visible", $visible, "id", $mod->coursemodule)) {
174 error("Could not update the course module with the correct visibility");
177 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
178 error("Could not update the course module with the correct section");
180 add_to_log($course->id, "course", "add mod",
181 "../mod/$mod->modulename/view.php?id=$mod->coursemodule",
182 "$mod->modulename $mod->instance");
183 add_to_log($course->id, $mod->modulename, "add",
184 "view.php?id=$mod->coursemodule",
185 "$mod->instance", $mod->coursemodule);
187 rebuild_course_cache($course->id);
189 print_simple_box(get_string("newglossarycreated","glossary"),"center","70%");
190 echo '<p>';
192 } else {
193 notify("Error while trying to create the new glossary.");
194 print_footer($course);
195 exit;
199 $xmlentries = $xml['GLOSSARY']['#']['INFO'][0]['#']['ENTRIES'][0]['#']['ENTRY'];
200 for($i = 0; $i < sizeof($xmlentries); $i++) {
201 // Inserting the entries
202 $xmlentry = $xmlentries[$i];
203 unset($newentry);
204 $newentry->concept = trim(addslashes($xmlentry['#']['CONCEPT'][0]['#']));
205 $newentry->definition = trusttext_strip(addslashes($xmlentry['#']['DEFINITION'][0]['#']));
206 if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) {
207 $newentry->casesensitive = addslashes($xmlentry['#']['CASESENSITIVE'][0]['#']);
208 } else {
209 $newentry->casesensitive = $CFG->glossary_casesensitive;
212 $permissiongranted = 1;
213 if ( $newentry->concept and $newentry->definition ) {
214 if ( !$glossary->allowduplicatedentries ) {
215 // checking if the entry is valid (checking if it is duplicated when should not be)
216 if ( $newentry->casesensitive ) {
217 $dupentry = get_record("glossary_entries","concept",$newentry->concept,"glossaryid",$glossary->id);
218 } else {
219 $dupentry = get_record("glossary_entries","lower(concept)",moodle_strtolower($newentry->concept),"glossaryid",$glossary->id);
221 if ($dupentry) {
222 $permissiongranted = 0;
225 } else {
226 $permissiongranted = 0;
228 if ($permissiongranted) {
229 $newentry->glossaryid = $glossary->id;
230 $newentry->sourceglossaryid = 0;
231 $newentry->approved = 1;
232 $newentry->userid = $USER->id;
233 $newentry->teacherentry = 1;
234 $newentry->format = addslashes($xmlentry['#']['FORMAT'][0]['#']);
235 $newentry->timecreated = time();
236 $newentry->timemodified = time();
238 // Setting the default values if no values were passed
239 if ( isset($xmlentry['#']['USEDYNALINK'][0]['#']) ) {
240 $newentry->usedynalink = addslashes($xmlentry['#']['USEDYNALINK'][0]['#']);
241 } else {
242 $newentry->usedynalink = $CFG->glossary_linkentries;
244 if ( isset($xmlentry['#']['FULLMATCH'][0]['#']) ) {
245 $newentry->fullmatch = addslashes($xmlentry['#']['FULLMATCH'][0]['#']);
246 } else {
247 $newentry->fullmatch = $CFG->glossary_fullmatch;
250 if ( $newentry->id = insert_record("glossary_entries",$newentry) ) {
251 $importedentries++;
253 $xmlaliases = @$xmlentry['#']['ALIASES'][0]['#']['ALIAS']; // ignore missing ALIASES
254 for($k = 0; $k < sizeof($xmlaliases); $k++) {
255 /// Importing aliases
256 $xmlalias = $xmlaliases[$k];
257 $aliasname = $xmlalias['#']['NAME'][0]['#'];
259 if (!empty($aliasname)) {
260 unset($newalias);
261 $newalias->entryid = $newentry->id;
262 $newalias->alias = trim(addslashes($aliasname));
263 $newalias->id = insert_record("glossary_alias",$newalias);
267 if ( $catsincl ) {
268 // If the categories must be imported...
269 $xmlcats = @$xmlentry['#']['CATEGORIES'][0]['#']['CATEGORY']; // ignore missing CATEGORIES
270 for($k = 0; $k < sizeof($xmlcats); $k++) {
271 $xmlcat = $xmlcats[$k];
272 unset($newcat);
274 $newcat->name = addslashes($xmlcat['#']['NAME'][0]['#']);
275 $newcat->usedynalink = addslashes($xmlcat['#']['USEDYNALINK'][0]['#']);
276 if ( !$category = get_record("glossary_categories","glossaryid",$glossary->id,"name",$newcat->name) ) {
277 // Create the category if it does not exist
278 unset($category);
279 $category->name = $newcat->name;
280 $category->glossaryid = $glossary->id;
281 if ( !$category->id = insert_record("glossary_categories",$category)) {
282 // add to exception report (can't insert category)
283 $rejections .= "<tr><td>&nbsp;<strong>" . get_string("category","glossary") . ":</strong>$newcat->name</td>" .
284 "<td>" . get_string("cantinsertcat","glossary"). "</td></tr>";
285 } else {
286 $importedcats++;
289 if ( $category ) {
290 // inserting the new relation
291 unset($entrycat);
292 $entrycat->entryid = $newentry->id;
293 $entrycat->categoryid = $category->id;
294 if ( !insert_record("glossary_entries_categories",$entrycat) ) {
295 // add to exception report (can't insert relation)
296 $rejections .= "<tr><td>&nbsp;<strong>" . get_string("category","glossary") . ":</strong>$newcat->name</td>" .
297 "<td>" . get_string("cantinsertrel","glossary"). "</td></tr>";
302 } else {
303 $entriesrejected++;
304 // add to exception report (can't insert new record)
305 $rejections .= "<tr><td>$newentry->concept</td>" .
306 "<td>" . get_string("cantinsertrec","glossary"). "</td></tr>";
308 } else {
309 $entriesrejected++;
310 if ( $newentry->concept and $newentry->definition ) {
311 // add to exception report (duplicated entry))
312 $rejections .= "<tr><td>$newentry->concept</td>" .
313 "<td>" . get_string("duplicateentry","glossary"). "</td></tr>";
314 } else {
315 // add to exception report (no concept or definition found))
316 $rejections .= "<tr><td>---</td>" .
317 "<td>" . get_string("noconceptfound","glossary"). "</td></tr>";
321 // processed entries
322 print_box_start('glossarydisplay generalbox');
323 echo '<table class="glossaryimportexport">';
324 echo '<tr>';
325 echo '<td width="50%" align="right">';
326 echo get_string("totalentries","glossary");
327 echo ':</td>';
328 echo '<td width="50%" align="left">';
329 echo $importedentries + $entriesrejected;
330 echo '</td>';
331 echo '</tr>';
332 echo '<tr>';
333 echo '<td width="50%" align="right">';
334 echo get_string("importedentries","glossary");
335 echo ':</td>';
336 echo '<td width="50%" align="left">';
337 echo $importedentries;
338 if ( $entriesrejected ) {
339 echo ' <small>(' . get_string("rejectedentries","glossary") . ": $entriesrejected)</small>";
341 echo '</td>';
342 echo '</tr>';
343 if ( $catsincl ) {
344 echo '<tr>';
345 echo '<td width="50%" align="right">';
346 echo get_string("importedcategories","glossary");
347 echo ':</td>';
348 echo '<td width="50%">';
349 echo $importedcats;
350 echo '</td>';
351 echo '</tr>';
353 echo '</table><hr />';
355 // rejected entries
356 if ($rejections) {
357 echo '<table class="glossaryimportexport">';
358 echo '<tr><td align="center" colspan="2" width="100%"><strong>' . get_string("rejectionrpt","glossary") . '</strong></tr>';
359 echo $rejections;
360 echo '</table><hr />';
362 /// Print continue button, based on results
363 if ($importedentries) {
364 print_continue('view.php?id='.$id);
365 } else {
366 print_continue('import.php?id='.$id);
368 print_box_end();
369 } else {
370 notice(get_string('errorparsingxml', 'glossary'));
373 /// Finish the page
374 print_footer($course);