Merge branch 'MDL-57868-32' of git://github.com/jleyva/moodle into MOODLE_32_STABLE
[moodle.git] / mod / glossary / import.php
blobf377d0765e7448085428b864913bd154996370eb
1 <?php
3 require_once("../../config.php");
4 require_once("lib.php");
5 require_once("$CFG->dirroot/course/lib.php");
6 require_once("$CFG->dirroot/course/modlib.php");
7 require_once('import_form.php');
9 $id = required_param('id', PARAM_INT); // Course Module ID
11 $mode = optional_param('mode', 'letter', PARAM_ALPHA );
12 $hook = optional_param('hook', 'ALL', PARAM_ALPHANUM);
14 $url = new moodle_url('/mod/glossary/import.php', array('id'=>$id));
15 if ($mode !== 'letter') {
16 $url->param('mode', $mode);
18 if ($hook !== 'ALL') {
19 $url->param('hook', $hook);
21 $PAGE->set_url($url);
23 if (! $cm = get_coursemodule_from_id('glossary', $id)) {
24 print_error('invalidcoursemodule');
27 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
28 print_error('coursemisconf');
31 if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
32 print_error('invalidid', 'glossary');
35 require_login($course, false, $cm);
37 $context = context_module::instance($cm->id);
38 require_capability('mod/glossary:import', $context);
40 $strglossaries = get_string("modulenameplural", "glossary");
41 $strglossary = get_string("modulename", "glossary");
42 $strallcategories = get_string("allcategories", "glossary");
43 $straddentry = get_string("addentry", "glossary");
44 $strnoentries = get_string("noentries", "glossary");
45 $strsearchindefinition = get_string("searchindefinition", "glossary");
46 $strsearch = get_string("search");
47 $strimportentries = get_string('importentriesfromxml', 'glossary');
49 $PAGE->navbar->add($strimportentries);
50 $PAGE->set_title($glossary->name);
51 $PAGE->set_heading($course->fullname);
53 echo $OUTPUT->header();
54 echo $OUTPUT->heading($strimportentries);
56 $form = new mod_glossary_import_form();
58 if ( !$data = $form->get_data() ) {
59 echo $OUTPUT->box_start('glossarydisplay generalbox');
60 // display upload form
61 $data = new stdClass();
62 $data->id = $id;
63 $form->set_data($data);
64 $form->display();
65 echo $OUTPUT->box_end();
66 echo $OUTPUT->footer();
67 exit;
70 $result = $form->get_file_content('file');
72 if (empty($result)) {
73 echo $OUTPUT->box_start('glossarydisplay generalbox');
74 echo $OUTPUT->continue_button('import.php?id='.$id);
75 echo $OUTPUT->box_end();
76 echo $OUTPUT->footer();
77 die();
80 // Large exports are likely to take their time and memory.
81 core_php_time_limit::raise();
82 raise_memory_limit(MEMORY_EXTRA);
84 if ($xml = glossary_read_imported_file($result)) {
85 $importedentries = 0;
86 $importedcats = 0;
87 $entriesrejected = 0;
88 $rejections = '';
89 $glossarycontext = $context;
91 if ($data->dest == 'newglossary') {
92 // If the user chose to create a new glossary
93 $xmlglossary = $xml['GLOSSARY']['#']['INFO'][0]['#'];
95 if ( $xmlglossary['NAME'][0]['#'] ) {
96 $glossary = new stdClass();
97 $glossary->modulename = 'glossary';
98 $glossary->module = $cm->module;
99 $glossary->name = ($xmlglossary['NAME'][0]['#']);
100 $glossary->globalglossary = ($xmlglossary['GLOBALGLOSSARY'][0]['#']);
101 $glossary->intro = ($xmlglossary['INTRO'][0]['#']);
102 $glossary->introformat = isset($xmlglossary['INTROFORMAT'][0]['#']) ? $xmlglossary['INTROFORMAT'][0]['#'] : FORMAT_MOODLE;
103 $glossary->showspecial = ($xmlglossary['SHOWSPECIAL'][0]['#']);
104 $glossary->showalphabet = ($xmlglossary['SHOWALPHABET'][0]['#']);
105 $glossary->showall = ($xmlglossary['SHOWALL'][0]['#']);
106 $glossary->cmidnumber = null;
108 // Setting the default values if no values were passed
109 if ( isset($xmlglossary['ENTBYPAGE'][0]['#']) ) {
110 $glossary->entbypage = ($xmlglossary['ENTBYPAGE'][0]['#']);
111 } else {
112 $glossary->entbypage = $CFG->glossary_entbypage;
114 if ( isset($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']) ) {
115 $glossary->allowduplicatedentries = ($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']);
116 } else {
117 $glossary->allowduplicatedentries = $CFG->glossary_dupentries;
119 if ( isset($xmlglossary['DISPLAYFORMAT'][0]['#']) ) {
120 $glossary->displayformat = ($xmlglossary['DISPLAYFORMAT'][0]['#']);
121 } else {
122 $glossary->displayformat = 2;
124 if ( isset($xmlglossary['ALLOWCOMMENTS'][0]['#']) ) {
125 $glossary->allowcomments = ($xmlglossary['ALLOWCOMMENTS'][0]['#']);
126 } else {
127 $glossary->allowcomments = $CFG->glossary_allowcomments;
129 if ( isset($xmlglossary['USEDYNALINK'][0]['#']) ) {
130 $glossary->usedynalink = ($xmlglossary['USEDYNALINK'][0]['#']);
131 } else {
132 $glossary->usedynalink = $CFG->glossary_linkentries;
134 if ( isset($xmlglossary['DEFAULTAPPROVAL'][0]['#']) ) {
135 $glossary->defaultapproval = ($xmlglossary['DEFAULTAPPROVAL'][0]['#']);
136 } else {
137 $glossary->defaultapproval = $CFG->glossary_defaultapproval;
140 // These fields were not included in export, assume zero.
141 $glossary->assessed = 0;
142 $glossary->availability = null;
144 // New glossary is to be inserted in section 0, it is always visible.
145 $glossary->section = 0;
146 $glossary->visible = 1;
148 // Include new glossary and return the new ID
149 if ( !($glossary = add_moduleinfo($glossary, $course)) ) {
150 echo $OUTPUT->notification("Error while trying to create the new glossary.");
151 glossary_print_tabbed_table_end();
152 echo $OUTPUT->footer();
153 exit;
154 } else {
155 $glossarycontext = context_module::instance($glossary->coursemodule);
156 glossary_xml_import_files($xmlglossary, 'INTROFILES', $glossarycontext->id, 'intro', 0);
157 echo $OUTPUT->box(get_string("newglossarycreated","glossary"),'generalbox boxaligncenter boxwidthnormal');
159 } else {
160 echo $OUTPUT->notification("Error while trying to create the new glossary.");
161 echo $OUTPUT->footer();
162 exit;
166 $xmlentries = $xml['GLOSSARY']['#']['INFO'][0]['#']['ENTRIES'][0]['#']['ENTRY'];
167 $sizeofxmlentries = sizeof($xmlentries);
168 for($i = 0; $i < $sizeofxmlentries; $i++) {
169 // Inserting the entries
170 $xmlentry = $xmlentries[$i];
171 $newentry = new stdClass();
172 $newentry->concept = trim($xmlentry['#']['CONCEPT'][0]['#']);
173 $definition = $xmlentry['#']['DEFINITION'][0]['#'];
174 if (!is_string($definition)) {
175 print_error('errorparsingxml', 'glossary');
177 $newentry->definition = trusttext_strip($definition);
178 if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) {
179 $newentry->casesensitive = $xmlentry['#']['CASESENSITIVE'][0]['#'];
180 } else {
181 $newentry->casesensitive = $CFG->glossary_casesensitive;
184 $permissiongranted = 1;
185 if ( $newentry->concept and $newentry->definition ) {
186 if ( !$glossary->allowduplicatedentries ) {
187 // checking if the entry is valid (checking if it is duplicated when should not be)
188 if ( $newentry->casesensitive ) {
189 $dupentry = $DB->record_exists_select('glossary_entries',
190 'glossaryid = :glossaryid AND concept = :concept', array(
191 'glossaryid' => $glossary->id,
192 'concept' => $newentry->concept));
193 } else {
194 $dupentry = $DB->record_exists_select('glossary_entries',
195 'glossaryid = :glossaryid AND LOWER(concept) = :concept', array(
196 'glossaryid' => $glossary->id,
197 'concept' => core_text::strtolower($newentry->concept)));
199 if ($dupentry) {
200 $permissiongranted = 0;
203 } else {
204 $permissiongranted = 0;
206 if ($permissiongranted) {
207 $newentry->glossaryid = $glossary->id;
208 $newentry->sourceglossaryid = 0;
209 $newentry->approved = 1;
210 $newentry->userid = $USER->id;
211 $newentry->teacherentry = 1;
212 $newentry->definitionformat = $xmlentry['#']['FORMAT'][0]['#'];
213 $newentry->timecreated = time();
214 $newentry->timemodified = time();
216 // Setting the default values if no values were passed
217 if ( isset($xmlentry['#']['USEDYNALINK'][0]['#']) ) {
218 $newentry->usedynalink = $xmlentry['#']['USEDYNALINK'][0]['#'];
219 } else {
220 $newentry->usedynalink = $CFG->glossary_linkentries;
222 if ( isset($xmlentry['#']['FULLMATCH'][0]['#']) ) {
223 $newentry->fullmatch = $xmlentry['#']['FULLMATCH'][0]['#'];
224 } else {
225 $newentry->fullmatch = $CFG->glossary_fullmatch;
228 $newentry->id = $DB->insert_record("glossary_entries",$newentry);
229 $importedentries++;
231 $xmlaliases = @$xmlentry['#']['ALIASES'][0]['#']['ALIAS']; // ignore missing ALIASES
232 $sizeofxmlaliases = sizeof($xmlaliases);
233 for($k = 0; $k < $sizeofxmlaliases; $k++) {
234 /// Importing aliases
235 $xmlalias = $xmlaliases[$k];
236 $aliasname = $xmlalias['#']['NAME'][0]['#'];
238 if (!empty($aliasname)) {
239 $newalias = new stdClass();
240 $newalias->entryid = $newentry->id;
241 $newalias->alias = trim($aliasname);
242 $newalias->id = $DB->insert_record("glossary_alias",$newalias);
246 if (!empty($data->catsincl)) {
247 // If the categories must be imported...
248 $xmlcats = @$xmlentry['#']['CATEGORIES'][0]['#']['CATEGORY']; // ignore missing CATEGORIES
249 $sizeofxmlcats = sizeof($xmlcats);
250 for($k = 0; $k < $sizeofxmlcats; $k++) {
251 $xmlcat = $xmlcats[$k];
253 $newcat = new stdClass();
254 $newcat->name = $xmlcat['#']['NAME'][0]['#'];
255 $newcat->usedynalink = $xmlcat['#']['USEDYNALINK'][0]['#'];
256 if ( !$category = $DB->get_record("glossary_categories", array("glossaryid"=>$glossary->id,"name"=>$newcat->name))) {
257 // Create the category if it does not exist
258 $category = new stdClass();
259 $category->name = $newcat->name;
260 $category->glossaryid = $glossary->id;
261 $category->id = $DB->insert_record("glossary_categories",$category);
262 $importedcats++;
264 if ( $category ) {
265 // inserting the new relation
266 $entrycat = new stdClass();
267 $entrycat->entryid = $newentry->id;
268 $entrycat->categoryid = $category->id;
269 $DB->insert_record("glossary_entries_categories",$entrycat);
274 // Import files embedded in the entry text.
275 glossary_xml_import_files($xmlentry['#'], 'ENTRYFILES', $glossarycontext->id, 'entry', $newentry->id);
277 // Import files attached to the entry.
278 if (glossary_xml_import_files($xmlentry['#'], 'ATTACHMENTFILES', $glossarycontext->id, 'attachment', $newentry->id)) {
279 $DB->update_record("glossary_entries", array('id' => $newentry->id, 'attachment' => '1'));
282 } else {
283 $entriesrejected++;
284 if ( $newentry->concept and $newentry->definition ) {
285 // add to exception report (duplicated entry))
286 $rejections .= "<tr><td>$newentry->concept</td>" .
287 "<td>" . get_string("duplicateentry","glossary"). "</td></tr>";
288 } else {
289 // add to exception report (no concept or definition found))
290 $rejections .= "<tr><td>---</td>" .
291 "<td>" . get_string("noconceptfound","glossary"). "</td></tr>";
296 // Reset caches.
297 \mod_glossary\local\concept_cache::reset_glossary($glossary);
299 // processed entries
300 echo $OUTPUT->box_start('glossarydisplay generalbox');
301 echo '<table class="glossaryimportexport">';
302 echo '<tr>';
303 echo '<td width="50%" align="right">';
304 echo get_string("totalentries","glossary");
305 echo ':</td>';
306 echo '<td width="50%" align="left">';
307 echo $importedentries + $entriesrejected;
308 echo '</td>';
309 echo '</tr>';
310 echo '<tr>';
311 echo '<td width="50%" align="right">';
312 echo get_string("importedentries","glossary");
313 echo ':</td>';
314 echo '<td width="50%" align="left">';
315 echo $importedentries;
316 if ( $entriesrejected ) {
317 echo ' <small>(' . get_string("rejectedentries","glossary") . ": $entriesrejected)</small>";
319 echo '</td>';
320 echo '</tr>';
321 if (!empty($data->catsincl)) {
322 echo '<tr>';
323 echo '<td width="50%" align="right">';
324 echo get_string("importedcategories","glossary");
325 echo ':</td>';
326 echo '<td width="50%">';
327 echo $importedcats;
328 echo '</td>';
329 echo '</tr>';
331 echo '</table><hr />';
333 // rejected entries
334 if ($rejections) {
335 echo $OUTPUT->heading(get_string("rejectionrpt","glossary"), 4);
336 echo '<table class="glossaryimportexport">';
337 echo $rejections;
338 echo '</table><hr />';
340 /// Print continue button, based on results
341 if ($importedentries) {
342 echo $OUTPUT->continue_button('view.php?id='.$id);
343 } else {
344 echo $OUTPUT->continue_button('import.php?id='.$id);
346 echo $OUTPUT->box_end();
347 } else {
348 echo $OUTPUT->box_start('glossarydisplay generalbox');
349 echo get_string('errorparsingxml', 'glossary');
350 echo $OUTPUT->continue_button('import.php?id='.$id);
351 echo $OUTPUT->box_end();
354 /// Finish the page
355 echo $OUTPUT->footer();