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