SCORM MDL-18202 MDL-21379 fix spaces
[moodle.git] / mod / glossary / import.php
1 <?php
3 require_once("../../config.php");
4 require_once("lib.php");
5 require_once("$CFG->dirroot/course/lib.php");
7 $id = required_param('id', PARAM_INT);    // Course Module ID
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?
14 $mode     = optional_param('mode', 'letter', PARAM_ALPHA );
15 $hook     = optional_param('hook', 'ALL', PARAM_ALPHANUM);
17 $url = new moodle_url('/mod/glossary/import.php', array('id'=>$id));
18 if ($step !== 0) {
19     $url->param('step', $step);
20 }
21 if ($dest !== 'current') {
22     $url->param('dest', $dest);
23 }
24 if ($file !== '') {
25     $url->param('file', $file);
26 }
27 if ($catsincl !== 0) {
28     $url->param('catsincl', $catsincl);
29 }
30 if ($mode !== 'letter') {
31     $url->param('mode', $mode);
32 }
33 if ($hook !== 'ALL') {
34     $url->param('hook', $hook);
35 }
36 $PAGE->set_url($url);
38 if (! $cm = get_coursemodule_from_id('glossary', $id)) {
39     print_error('invalidcoursemodule');
40 }
42 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
43     print_error('coursemisconf');
44 }
46 if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
47     print_error('invalidid', 'glossary');
48 }
50 require_login($course->id, false, $cm);
52 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
53 require_capability('mod/glossary:import', $context);
55 if ($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');
68 $PAGE->set_url('/mod/glossary/import.php', array('id'=>$cm->id, 'mode'=>$mode, 'hook'=>$hook));
69 $PAGE->navbar->add($strimportentries);
70 $PAGE->set_title(format_string($glossary->name));
72 echo $OUTPUT->header();
73 echo $OUTPUT->heading($strimportentries);
75 if ( !$step ) {
76     echo $OUTPUT->box_start('glossarydisplay generalbox');
77     include("import.html");
78     echo $OUTPUT->box_end();
80     echo $OUTPUT->footer();
81     exit;
82 }
84 require_sesskey();
85 $form = data_submitted();
86 $file = $_FILES["file"];
88 require_once($CFG->dirroot.'/lib/uploadlib.php');
89 $um = new upload_manager('file',false,false,$course,false,0);
91 if (!$um->preprocess_files()) {
92     echo $OUTPUT->box_start('glossarydisplay generalbox');
93     echo $OUTPUT->continue_button('import.php?id='.$id);
94     echo $OUTPUT->box_end();
96     echo $OUTPUT->footer();
97     die();
98 }
100 if ($xml = glossary_read_imported_file($file['tmp_name']) ) {
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]['#'];
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();
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             }
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
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.
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;
178                 if (! $mod->coursemodule = add_course_module($mod) ) {
179                     print_error('cannotaddcoursemodule');
180                 }
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));
188                 $DB->set_field("course_modules", "visible", $visible, array("id"=>$mod->coursemodule));
189                 $DB->set_field("course_modules", "section", $sectionid, array("id"=>$mod->coursemodule));
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);
198                 rebuild_course_cache($course->id);
200                 echo $OUTPUT->box(get_string("newglossarycreated","glossary"),'generalbox boxaligncenter boxwidthnormal');
201                 echo '<p>';
202             }
203         } else {
204             echo $OUTPUT->notification("Error while trying to create the new glossary.");
205             echo $OUTPUT->footer();
206             exit;
207         }
208     }
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         }
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));
229                 } else {
230                     $dupentry = $DB->get_record("glossary_entries", array("lower(concept)"=>moodle_strtolower($newentry->concept)), array("glossaryid"=>$glossary->id));
231                 }
232                 if ($dupentry) {
233                     $permissiongranted = 0;
234                 }
235             }
236         } else {
237             $permissiongranted = 0;
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();
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             }
261             if ( $newentry->id = $DB->insert_record("glossary_entries",$newentry) )  {
262                 $importedentries++;
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]['#'];
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);
275                     }
276                 }
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];
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
297                             $entrycat = new opbject();
298                             $entrycat->entryid    = $newentry->id;
299                             $entrycat->categoryid = $category->id;
300                             $DB->insert_record("glossary_entries_categories",$entrycat);
301                         }
302                     }
303                 }
304             } else {
305                 $entriesrejected++;
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>";
320             }
321         }
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 ) {
346         echo '<tr>';
347         echo '<td width="50%" align="right">';
348         echo get_string("importedcategories","glossary");
349         echo ':</td>';
350         echo '<td width="50%">';
351         echo $importedcats;
352         echo '</td>';
353         echo '</tr>';
354     }
355     echo '</table><hr />';
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);
367     } else {
368         echo $OUTPUT->continue_button('import.php?id='.$id);
369     }
370     echo $OUTPUT->box_end();
371 } else {
372     notice(get_string('errorparsingxml', 'glossary'));
375 /// Finish the page
376 echo $OUTPUT->footer();