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 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();
63 $form->set_data($data);
65 echo $OUTPUT->box_end();
66 echo $OUTPUT->footer();
70 $result = $form->get_file_content('file');
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();
80 if ($xml = glossary_read_imported_file($result)) {
85 $glossarycontext = $context;
87 if ($data->dest
== 'newglossary') {
88 // If the user chose to create a new glossary
89 $xmlglossary = $xml['GLOSSARY']['#']['INFO'][0]['#'];
91 if ( $xmlglossary['NAME'][0]['#'] ) {
92 $glossary = new stdClass();
93 $glossary->modulename
= 'glossary';
94 $glossary->module
= $cm->module
;
95 $glossary->name
= ($xmlglossary['NAME'][0]['#']);
96 $glossary->globalglossary
= ($xmlglossary['GLOBALGLOSSARY'][0]['#']);
97 $glossary->intro
= ($xmlglossary['INTRO'][0]['#']);
98 $glossary->introformat
= isset($xmlglossary['INTROFORMAT'][0]['#']) ?
$xmlglossary['INTROFORMAT'][0]['#'] : FORMAT_MOODLE
;
99 $glossary->showspecial
= ($xmlglossary['SHOWSPECIAL'][0]['#']);
100 $glossary->showalphabet
= ($xmlglossary['SHOWALPHABET'][0]['#']);
101 $glossary->showall
= ($xmlglossary['SHOWALL'][0]['#']);
102 $glossary->cmidnumber
= null;
104 // Setting the default values if no values were passed
105 if ( isset($xmlglossary['ENTBYPAGE'][0]['#']) ) {
106 $glossary->entbypage
= ($xmlglossary['ENTBYPAGE'][0]['#']);
108 $glossary->entbypage
= $CFG->glossary_entbypage
;
110 if ( isset($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']) ) {
111 $glossary->allowduplicatedentries
= ($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']);
113 $glossary->allowduplicatedentries
= $CFG->glossary_dupentries
;
115 if ( isset($xmlglossary['DISPLAYFORMAT'][0]['#']) ) {
116 $glossary->displayformat
= ($xmlglossary['DISPLAYFORMAT'][0]['#']);
118 $glossary->displayformat
= 2;
120 if ( isset($xmlglossary['ALLOWCOMMENTS'][0]['#']) ) {
121 $glossary->allowcomments
= ($xmlglossary['ALLOWCOMMENTS'][0]['#']);
123 $glossary->allowcomments
= $CFG->glossary_allowcomments
;
125 if ( isset($xmlglossary['USEDYNALINK'][0]['#']) ) {
126 $glossary->usedynalink
= ($xmlglossary['USEDYNALINK'][0]['#']);
128 $glossary->usedynalink
= $CFG->glossary_linkentries
;
130 if ( isset($xmlglossary['DEFAULTAPPROVAL'][0]['#']) ) {
131 $glossary->defaultapproval
= ($xmlglossary['DEFAULTAPPROVAL'][0]['#']);
133 $glossary->defaultapproval
= $CFG->glossary_defaultapproval
;
136 // These fields were not included in export, assume zero.
137 $glossary->assessed
= 0;
138 $glossary->availability
= null;
140 // New glossary is to be inserted in section 0, it is always visible.
141 $glossary->section
= 0;
142 $glossary->visible
= 1;
144 // Include new glossary and return the new ID
145 if ( !($glossary = add_moduleinfo($glossary, $course)) ) {
146 echo $OUTPUT->notification("Error while trying to create the new glossary.");
147 glossary_print_tabbed_table_end();
148 echo $OUTPUT->footer();
151 $glossarycontext = context_module
::instance($glossary->coursemodule
);
152 glossary_xml_import_files($xmlglossary, 'INTROFILES', $glossarycontext->id
, 'intro', 0);
153 echo $OUTPUT->box(get_string("newglossarycreated","glossary"),'generalbox boxaligncenter boxwidthnormal');
156 echo $OUTPUT->notification("Error while trying to create the new glossary.");
157 echo $OUTPUT->footer();
162 $xmlentries = $xml['GLOSSARY']['#']['INFO'][0]['#']['ENTRIES'][0]['#']['ENTRY'];
163 $sizeofxmlentries = sizeof($xmlentries);
164 for($i = 0; $i < $sizeofxmlentries; $i++
) {
165 // Inserting the entries
166 $xmlentry = $xmlentries[$i];
167 $newentry = new stdClass();
168 $newentry->concept
= trim($xmlentry['#']['CONCEPT'][0]['#']);
169 $newentry->definition
= trusttext_strip($xmlentry['#']['DEFINITION'][0]['#']);
170 if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) {
171 $newentry->casesensitive
= $xmlentry['#']['CASESENSITIVE'][0]['#'];
173 $newentry->casesensitive
= $CFG->glossary_casesensitive
;
176 $permissiongranted = 1;
177 if ( $newentry->concept
and $newentry->definition
) {
178 if ( !$glossary->allowduplicatedentries
) {
179 // checking if the entry is valid (checking if it is duplicated when should not be)
180 if ( $newentry->casesensitive
) {
181 $dupentry = $DB->record_exists_select('glossary_entries',
182 'glossaryid = :glossaryid AND concept = :concept', array(
183 'glossaryid' => $glossary->id
,
184 'concept' => $newentry->concept
));
186 $dupentry = $DB->record_exists_select('glossary_entries',
187 'glossaryid = :glossaryid AND LOWER(concept) = :concept', array(
188 'glossaryid' => $glossary->id
,
189 'concept' => core_text
::strtolower($newentry->concept
)));
192 $permissiongranted = 0;
196 $permissiongranted = 0;
198 if ($permissiongranted) {
199 $newentry->glossaryid
= $glossary->id
;
200 $newentry->sourceglossaryid
= 0;
201 $newentry->approved
= 1;
202 $newentry->userid
= $USER->id
;
203 $newentry->teacherentry
= 1;
204 $newentry->definitionformat
= $xmlentry['#']['FORMAT'][0]['#'];
205 $newentry->timecreated
= time();
206 $newentry->timemodified
= time();
208 // Setting the default values if no values were passed
209 if ( isset($xmlentry['#']['USEDYNALINK'][0]['#']) ) {
210 $newentry->usedynalink
= $xmlentry['#']['USEDYNALINK'][0]['#'];
212 $newentry->usedynalink
= $CFG->glossary_linkentries
;
214 if ( isset($xmlentry['#']['FULLMATCH'][0]['#']) ) {
215 $newentry->fullmatch
= $xmlentry['#']['FULLMATCH'][0]['#'];
217 $newentry->fullmatch
= $CFG->glossary_fullmatch
;
220 $newentry->id
= $DB->insert_record("glossary_entries",$newentry);
223 $xmlaliases = @$xmlentry['#']['ALIASES'][0]['#']['ALIAS']; // ignore missing ALIASES
224 $sizeofxmlaliases = sizeof($xmlaliases);
225 for($k = 0; $k < $sizeofxmlaliases; $k++
) {
226 /// Importing aliases
227 $xmlalias = $xmlaliases[$k];
228 $aliasname = $xmlalias['#']['NAME'][0]['#'];
230 if (!empty($aliasname)) {
231 $newalias = new stdClass();
232 $newalias->entryid
= $newentry->id
;
233 $newalias->alias
= trim($aliasname);
234 $newalias->id
= $DB->insert_record("glossary_alias",$newalias);
238 if (!empty($data->catsincl
)) {
239 // If the categories must be imported...
240 $xmlcats = @$xmlentry['#']['CATEGORIES'][0]['#']['CATEGORY']; // ignore missing CATEGORIES
241 $sizeofxmlcats = sizeof($xmlcats);
242 for($k = 0; $k < $sizeofxmlcats; $k++
) {
243 $xmlcat = $xmlcats[$k];
245 $newcat = new stdClass();
246 $newcat->name
= $xmlcat['#']['NAME'][0]['#'];
247 $newcat->usedynalink
= $xmlcat['#']['USEDYNALINK'][0]['#'];
248 if ( !$category = $DB->get_record("glossary_categories", array("glossaryid"=>$glossary->id
,"name"=>$newcat->name
))) {
249 // Create the category if it does not exist
250 $category = new stdClass();
251 $category->name
= $newcat->name
;
252 $category->glossaryid
= $glossary->id
;
253 $category->id
= $DB->insert_record("glossary_categories",$category);
257 // inserting the new relation
258 $entrycat = new stdClass();
259 $entrycat->entryid
= $newentry->id
;
260 $entrycat->categoryid
= $category->id
;
261 $DB->insert_record("glossary_entries_categories",$entrycat);
266 // Import files embedded in the entry text.
267 glossary_xml_import_files($xmlentry['#'], 'ENTRYFILES', $glossarycontext->id
, 'entry', $newentry->id
);
269 // Import files attached to the entry.
270 if (glossary_xml_import_files($xmlentry['#'], 'ATTACHMENTFILES', $glossarycontext->id
, 'attachment', $newentry->id
)) {
271 $DB->update_record("glossary_entries", array('id' => $newentry->id
, 'attachment' => '1'));
276 if ( $newentry->concept
and $newentry->definition
) {
277 // add to exception report (duplicated entry))
278 $rejections .= "<tr><td>$newentry->concept</td>" .
279 "<td>" . get_string("duplicateentry","glossary"). "</td></tr>";
281 // add to exception report (no concept or definition found))
282 $rejections .= "<tr><td>---</td>" .
283 "<td>" . get_string("noconceptfound","glossary"). "</td></tr>";
289 \mod_glossary\local\concept_cache
::reset_glossary($glossary);
292 echo $OUTPUT->box_start('glossarydisplay generalbox');
293 echo '<table class="glossaryimportexport">';
295 echo '<td width="50%" align="right">';
296 echo get_string("totalentries","glossary");
298 echo '<td width="50%" align="left">';
299 echo $importedentries +
$entriesrejected;
303 echo '<td width="50%" align="right">';
304 echo get_string("importedentries","glossary");
306 echo '<td width="50%" align="left">';
307 echo $importedentries;
308 if ( $entriesrejected ) {
309 echo ' <small>(' . get_string("rejectedentries","glossary") . ": $entriesrejected)</small>";
313 if (!empty($data->catsincl
)) {
315 echo '<td width="50%" align="right">';
316 echo get_string("importedcategories","glossary");
318 echo '<td width="50%">';
323 echo '</table><hr />';
327 echo $OUTPUT->heading(get_string("rejectionrpt","glossary"), 4);
328 echo '<table class="glossaryimportexport">';
330 echo '</table><hr />';
332 /// Print continue button, based on results
333 if ($importedentries) {
334 echo $OUTPUT->continue_button('view.php?id='.$id);
336 echo $OUTPUT->continue_button('import.php?id='.$id);
338 echo $OUTPUT->box_end();
340 echo $OUTPUT->box_start('glossarydisplay generalbox');
341 echo get_string('errorparsingxml', 'glossary');
342 echo $OUTPUT->continue_button('import.php?id='.$id);
343 echo $OUTPUT->box_end();
347 echo $OUTPUT->footer();