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);
23 if (! $cm = get_coursemodule_from_id('glossary', $id)) {
24 throw new \
moodle_exception('invalidcoursemodule');
27 if (! $course = $DB->get_record("course", array("id"=>$cm->course
))) {
28 throw new \
moodle_exception('coursemisconf');
31 if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance
))) {
32 throw new \
moodle_exception('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
);
52 $PAGE->set_secondary_active_tab('modulepage');
53 $PAGE->activityheader
->disable();
55 $form = new mod_glossary_import_form('');
56 if ($form->is_cancelled()) {
57 redirect(new moodle_url('view.php', ['id' => $id]));
60 echo $OUTPUT->header();
61 echo $OUTPUT->heading($strimportentries);
63 if ( !$data = $form->get_data() ) {
64 echo $OUTPUT->box_start('glossarydisplay generalbox');
65 // display upload form
66 $data = new stdClass();
68 $form->set_data($data);
70 echo $OUTPUT->box_end();
71 echo $OUTPUT->footer();
75 $result = $form->get_file_content('file');
78 echo $OUTPUT->box_start('glossarydisplay generalbox');
79 echo $OUTPUT->continue_button('import.php?id='.$id);
80 echo $OUTPUT->box_end();
81 echo $OUTPUT->footer();
85 // Large exports are likely to take their time and memory.
86 core_php_time_limit
::raise();
87 raise_memory_limit(MEMORY_EXTRA
);
89 if ($xml = glossary_read_imported_file($result)) {
94 $glossarycontext = $context;
96 if ($data->dest
== 'newglossary') {
97 // If the user chose to create a new glossary
98 $xmlglossary = $xml['GLOSSARY']['#']['INFO'][0]['#'];
100 if ( $xmlglossary['NAME'][0]['#'] ) {
101 $glossary = new stdClass();
102 $glossary->modulename
= 'glossary';
103 $glossary->module
= $cm->module
;
104 $glossary->name
= ($xmlglossary['NAME'][0]['#']);
105 $glossary->globalglossary
= ($xmlglossary['GLOBALGLOSSARY'][0]['#']);
106 $glossary->intro
= ($xmlglossary['INTRO'][0]['#']);
107 $glossary->introformat
= isset($xmlglossary['INTROFORMAT'][0]['#']) ?
$xmlglossary['INTROFORMAT'][0]['#'] : FORMAT_MOODLE
;
108 $glossary->showspecial
= ($xmlglossary['SHOWSPECIAL'][0]['#']);
109 $glossary->showalphabet
= ($xmlglossary['SHOWALPHABET'][0]['#']);
110 $glossary->showall
= ($xmlglossary['SHOWALL'][0]['#']);
111 $glossary->cmidnumber
= null;
113 // Setting the default values if no values were passed
114 if ( isset($xmlglossary['ENTBYPAGE'][0]['#']) ) {
115 $glossary->entbypage
= ($xmlglossary['ENTBYPAGE'][0]['#']);
117 $glossary->entbypage
= $CFG->glossary_entbypage
;
119 if ( isset($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']) ) {
120 $glossary->allowduplicatedentries
= ($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']);
122 $glossary->allowduplicatedentries
= $CFG->glossary_dupentries
;
124 if ( isset($xmlglossary['DISPLAYFORMAT'][0]['#']) ) {
125 $glossary->displayformat
= ($xmlglossary['DISPLAYFORMAT'][0]['#']);
127 $glossary->displayformat
= 2;
129 if ( isset($xmlglossary['ALLOWCOMMENTS'][0]['#']) ) {
130 $glossary->allowcomments
= ($xmlglossary['ALLOWCOMMENTS'][0]['#']);
132 $glossary->allowcomments
= $CFG->glossary_allowcomments
;
134 if ( isset($xmlglossary['USEDYNALINK'][0]['#']) ) {
135 $glossary->usedynalink
= ($xmlglossary['USEDYNALINK'][0]['#']);
137 $glossary->usedynalink
= $CFG->glossary_linkentries
;
139 if ( isset($xmlglossary['DEFAULTAPPROVAL'][0]['#']) ) {
140 $glossary->defaultapproval
= ($xmlglossary['DEFAULTAPPROVAL'][0]['#']);
142 $glossary->defaultapproval
= $CFG->glossary_defaultapproval
;
145 // These fields were not included in export, assume zero.
146 $glossary->assessed
= 0;
147 $glossary->availability
= null;
149 // Check if we're creating the new glossary on the front page or inside a course.
150 if ($cm->course
== SITEID
) {
151 // On the front page, activities are in section 1.
152 $glossary->section
= 1;
154 // Inside a course, add to the general section (0).
155 $glossary->section
= 0;
157 // New glossary is always visible.
158 $glossary->visible
= 1;
159 $glossary->visibleoncoursepage
= 1;
161 // Include new glossary and return the new ID
162 if ( !($glossary = add_moduleinfo($glossary, $course)) ) {
163 echo $OUTPUT->notification("Error while trying to create the new glossary.");
164 glossary_print_tabbed_table_end();
165 echo $OUTPUT->footer();
168 $glossarycontext = context_module
::instance($glossary->coursemodule
);
169 glossary_xml_import_files($xmlglossary, 'INTROFILES', $glossarycontext->id
, 'intro', 0);
170 echo $OUTPUT->box(get_string("newglossarycreated","glossary"),'generalbox boxaligncenter boxwidthnormal');
173 echo $OUTPUT->notification("Error while trying to create the new glossary.");
174 echo $OUTPUT->footer();
179 $xmlentries = $xml['GLOSSARY']['#']['INFO'][0]['#']['ENTRIES'][0]['#']['ENTRY'];
180 $sizeofxmlentries = is_array($xmlentries) ?
count($xmlentries) : 0;
181 for($i = 0; $i < $sizeofxmlentries; $i++
) {
182 // Inserting the entries
183 $xmlentry = $xmlentries[$i];
184 $newentry = new stdClass();
185 $newentry->concept
= trim($xmlentry['#']['CONCEPT'][0]['#']);
186 $definition = $xmlentry['#']['DEFINITION'][0]['#'];
187 if (!is_string($definition)) {
188 throw new \
moodle_exception('errorparsingxml', 'glossary');
190 $newentry->definition
= trusttext_strip($definition);
191 if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) {
192 $newentry->casesensitive
= $xmlentry['#']['CASESENSITIVE'][0]['#'];
194 $newentry->casesensitive
= $CFG->glossary_casesensitive
;
197 $permissiongranted = 1;
198 if ( $newentry->concept
and $newentry->definition
) {
199 if ( !$glossary->allowduplicatedentries
) {
200 // checking if the entry is valid (checking if it is duplicated when should not be)
201 if ( $newentry->casesensitive
) {
202 $dupentry = $DB->record_exists_select('glossary_entries',
203 'glossaryid = :glossaryid AND concept = :concept', array(
204 'glossaryid' => $glossary->id
,
205 'concept' => $newentry->concept
));
207 $dupentry = $DB->record_exists_select('glossary_entries',
208 'glossaryid = :glossaryid AND LOWER(concept) = :concept', array(
209 'glossaryid' => $glossary->id
,
210 'concept' => core_text
::strtolower($newentry->concept
)));
213 $permissiongranted = 0;
217 $permissiongranted = 0;
219 if ($permissiongranted) {
220 $newentry->glossaryid
= $glossary->id
;
221 $newentry->sourceglossaryid
= 0;
222 $newentry->approved
= 1;
223 $newentry->userid
= $USER->id
;
224 $newentry->teacherentry
= 1;
225 $newentry->definitionformat
= $xmlentry['#']['FORMAT'][0]['#'];
226 $newentry->timecreated
= time();
227 $newentry->timemodified
= time();
229 // Setting the default values if no values were passed
230 if ( isset($xmlentry['#']['USEDYNALINK'][0]['#']) ) {
231 $newentry->usedynalink
= $xmlentry['#']['USEDYNALINK'][0]['#'];
233 $newentry->usedynalink
= $CFG->glossary_linkentries
;
235 if ( isset($xmlentry['#']['FULLMATCH'][0]['#']) ) {
236 $newentry->fullmatch
= $xmlentry['#']['FULLMATCH'][0]['#'];
238 $newentry->fullmatch
= $CFG->glossary_fullmatch
;
241 $newentry->id
= $DB->insert_record("glossary_entries",$newentry);
244 $xmlaliases = @$xmlentry['#']['ALIASES'][0]['#']['ALIAS']; // ignore missing ALIASES
245 $sizeofxmlaliases = is_array($xmlaliases) ?
count($xmlaliases) : 0;
246 for($k = 0; $k < $sizeofxmlaliases; $k++
) {
247 /// Importing aliases
248 $xmlalias = $xmlaliases[$k];
249 $aliasname = $xmlalias['#']['NAME'][0]['#'];
251 if (!empty($aliasname)) {
252 $newalias = new stdClass();
253 $newalias->entryid
= $newentry->id
;
254 $newalias->alias
= trim($aliasname);
255 $newalias->id
= $DB->insert_record("glossary_alias",$newalias);
259 if (!empty($data->catsincl
)) {
260 // If the categories must be imported...
261 $xmlcats = @$xmlentry['#']['CATEGORIES'][0]['#']['CATEGORY']; // ignore missing CATEGORIES
262 $sizeofxmlcats = is_array($xmlcats) ?
count($xmlcats) : 0;
263 for($k = 0; $k < $sizeofxmlcats; $k++
) {
264 $xmlcat = $xmlcats[$k];
266 $newcat = new stdClass();
267 $newcat->name
= $xmlcat['#']['NAME'][0]['#'];
268 $newcat->usedynalink
= $xmlcat['#']['USEDYNALINK'][0]['#'];
269 if ( !$category = $DB->get_record("glossary_categories", array("glossaryid"=>$glossary->id
,"name"=>$newcat->name
))) {
270 // Create the category if it does not exist
271 $category = new stdClass();
272 $category->name
= $newcat->name
;
273 $category->glossaryid
= $glossary->id
;
274 $category->id
= $DB->insert_record("glossary_categories",$category);
278 // inserting the new relation
279 $entrycat = new stdClass();
280 $entrycat->entryid
= $newentry->id
;
281 $entrycat->categoryid
= $category->id
;
282 $DB->insert_record("glossary_entries_categories",$entrycat);
287 // Import files embedded in the entry text.
288 glossary_xml_import_files($xmlentry['#'], 'ENTRYFILES', $glossarycontext->id
, 'entry', $newentry->id
);
290 // Import files attached to the entry.
291 if (glossary_xml_import_files($xmlentry['#'], 'ATTACHMENTFILES', $glossarycontext->id
, 'attachment', $newentry->id
)) {
292 $DB->update_record("glossary_entries", array('id' => $newentry->id
, 'attachment' => '1'));
295 // Import tags associated with the entry.
296 if (core_tag_tag
::is_enabled('mod_glossary', 'glossary_entries')) {
297 $xmltags = @$xmlentry['#']['TAGS'][0]['#']['TAG']; // Ignore missing TAGS.
298 $sizeofxmltags = is_array($xmltags) ?
count($xmltags) : 0;
299 for ($k = 0; $k < $sizeofxmltags; $k++
) {
301 $tag = $xmltags[$k]['#'];
303 core_tag_tag
::add_item_tag('mod_glossary', 'glossary_entries', $newentry->id
, $glossarycontext, $tag);
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>";
315 // add to exception report (no concept or definition found))
316 $rejections .= "<tr><td>---</td>" .
317 "<td>" . get_string("noconceptfound","glossary"). "</td></tr>";
323 \mod_glossary\local\concept_cache
::reset_glossary($glossary);
326 echo $OUTPUT->box_start('glossarydisplay generalbox');
327 echo '<table class="glossaryimportexport">';
329 echo '<td width="50%" align="right">';
330 echo get_string("totalentries","glossary");
332 echo '<td width="50%" align="left">';
333 echo $importedentries +
$entriesrejected;
337 echo '<td width="50%" align="right">';
338 echo get_string("importedentries","glossary");
340 echo '<td width="50%" align="left">';
341 echo $importedentries;
342 if ( $entriesrejected ) {
343 echo ' <small>(' . get_string("rejectedentries","glossary") . ": $entriesrejected)</small>";
347 if (!empty($data->catsincl
)) {
349 echo '<td width="50%" align="right">';
350 echo get_string("importedcategories","glossary");
352 echo '<td width="50%">';
357 echo '</table><hr />';
361 echo $OUTPUT->heading(get_string("rejectionrpt","glossary"), 4);
362 echo '<table class="glossaryimportexport">';
364 echo '</table><hr />';
366 /// Print continue button, based on results
367 if ($importedentries) {
368 echo $OUTPUT->continue_button('view.php?id='.$id);
370 echo $OUTPUT->continue_button('import.php?id='.$id);
372 echo $OUTPUT->box_end();
374 echo $OUTPUT->box_start('glossarydisplay generalbox');
375 echo get_string('errorparsingxml', 'glossary');
376 echo $OUTPUT->continue_button('import.php?id='.$id);
377 echo $OUTPUT->box_end();
381 echo $OUTPUT->footer();