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)) {
86 if ($data->dest
== 'newglossary') {
87 // If the user chose to create a new glossary
88 $xmlglossary = $xml['GLOSSARY']['#']['INFO'][0]['#'];
90 if ( $xmlglossary['NAME'][0]['#'] ) {
91 $glossary = new stdClass();
92 $glossary->modulename
= 'glossary';
93 $glossary->module
= $cm->module
;
94 $glossary->name
= ($xmlglossary['NAME'][0]['#']);
95 $glossary->globalglossary
= ($xmlglossary['GLOBALGLOSSARY'][0]['#']);
96 $glossary->intro
= ($xmlglossary['INTRO'][0]['#']);
97 $glossary->introformat
= isset($xmlglossary['INTROFORMAT'][0]['#']) ?
$xmlglossary['INTROFORMAT'][0]['#'] : FORMAT_MOODLE
;
98 $glossary->showspecial
= ($xmlglossary['SHOWSPECIAL'][0]['#']);
99 $glossary->showalphabet
= ($xmlglossary['SHOWALPHABET'][0]['#']);
100 $glossary->showall
= ($xmlglossary['SHOWALL'][0]['#']);
101 $glossary->cmidnumber
= null;
103 // Setting the default values if no values were passed
104 if ( isset($xmlglossary['ENTBYPAGE'][0]['#']) ) {
105 $glossary->entbypage
= ($xmlglossary['ENTBYPAGE'][0]['#']);
107 $glossary->entbypage
= $CFG->glossary_entbypage
;
109 if ( isset($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']) ) {
110 $glossary->allowduplicatedentries
= ($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']);
112 $glossary->allowduplicatedentries
= $CFG->glossary_dupentries
;
114 if ( isset($xmlglossary['DISPLAYFORMAT'][0]['#']) ) {
115 $glossary->displayformat
= ($xmlglossary['DISPLAYFORMAT'][0]['#']);
117 $glossary->displayformat
= 2;
119 if ( isset($xmlglossary['ALLOWCOMMENTS'][0]['#']) ) {
120 $glossary->allowcomments
= ($xmlglossary['ALLOWCOMMENTS'][0]['#']);
122 $glossary->allowcomments
= $CFG->glossary_allowcomments
;
124 if ( isset($xmlglossary['USEDYNALINK'][0]['#']) ) {
125 $glossary->usedynalink
= ($xmlglossary['USEDYNALINK'][0]['#']);
127 $glossary->usedynalink
= $CFG->glossary_linkentries
;
129 if ( isset($xmlglossary['DEFAULTAPPROVAL'][0]['#']) ) {
130 $glossary->defaultapproval
= ($xmlglossary['DEFAULTAPPROVAL'][0]['#']);
132 $glossary->defaultapproval
= $CFG->glossary_defaultapproval
;
135 // These fields were not included in export, assume zero.
136 $glossary->assessed
= 0;
137 $glossary->availability
= null;
139 // New glossary is to be inserted in section 0, it is always visible.
140 $glossary->section
= 0;
141 $glossary->visible
= 1;
143 // Include new glossary and return the new ID
144 if ( !($glossary = add_moduleinfo($glossary, $course)) ) {
145 echo $OUTPUT->notification("Error while trying to create the new glossary.");
146 glossary_print_tabbed_table_end();
147 echo $OUTPUT->footer();
150 echo $OUTPUT->box(get_string("newglossarycreated","glossary"),'generalbox boxaligncenter boxwidthnormal');
153 echo $OUTPUT->notification("Error while trying to create the new glossary.");
154 echo $OUTPUT->footer();
159 $xmlentries = $xml['GLOSSARY']['#']['INFO'][0]['#']['ENTRIES'][0]['#']['ENTRY'];
160 $sizeofxmlentries = sizeof($xmlentries);
161 for($i = 0; $i < $sizeofxmlentries; $i++
) {
162 // Inserting the entries
163 $xmlentry = $xmlentries[$i];
164 $newentry = new stdClass();
165 $newentry->concept
= trim($xmlentry['#']['CONCEPT'][0]['#']);
166 $newentry->definition
= trusttext_strip($xmlentry['#']['DEFINITION'][0]['#']);
167 if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) {
168 $newentry->casesensitive
= $xmlentry['#']['CASESENSITIVE'][0]['#'];
170 $newentry->casesensitive
= $CFG->glossary_casesensitive
;
173 $permissiongranted = 1;
174 if ( $newentry->concept
and $newentry->definition
) {
175 if ( !$glossary->allowduplicatedentries
) {
176 // checking if the entry is valid (checking if it is duplicated when should not be)
177 if ( $newentry->casesensitive
) {
178 $dupentry = $DB->record_exists_select('glossary_entries',
179 'glossaryid = :glossaryid AND concept = :concept', array(
180 'glossaryid' => $glossary->id
,
181 'concept' => $newentry->concept
));
183 $dupentry = $DB->record_exists_select('glossary_entries',
184 'glossaryid = :glossaryid AND LOWER(concept) = :concept', array(
185 'glossaryid' => $glossary->id
,
186 'concept' => core_text
::strtolower($newentry->concept
)));
189 $permissiongranted = 0;
193 $permissiongranted = 0;
195 if ($permissiongranted) {
196 $newentry->glossaryid
= $glossary->id
;
197 $newentry->sourceglossaryid
= 0;
198 $newentry->approved
= 1;
199 $newentry->userid
= $USER->id
;
200 $newentry->teacherentry
= 1;
201 $newentry->definitionformat
= $xmlentry['#']['FORMAT'][0]['#'];
202 $newentry->timecreated
= time();
203 $newentry->timemodified
= time();
205 // Setting the default values if no values were passed
206 if ( isset($xmlentry['#']['USEDYNALINK'][0]['#']) ) {
207 $newentry->usedynalink
= $xmlentry['#']['USEDYNALINK'][0]['#'];
209 $newentry->usedynalink
= $CFG->glossary_linkentries
;
211 if ( isset($xmlentry['#']['FULLMATCH'][0]['#']) ) {
212 $newentry->fullmatch
= $xmlentry['#']['FULLMATCH'][0]['#'];
214 $newentry->fullmatch
= $CFG->glossary_fullmatch
;
217 $newentry->id
= $DB->insert_record("glossary_entries",$newentry);
220 $xmlaliases = @$xmlentry['#']['ALIASES'][0]['#']['ALIAS']; // ignore missing ALIASES
221 $sizeofxmlaliases = sizeof($xmlaliases);
222 for($k = 0; $k < $sizeofxmlaliases; $k++
) {
223 /// Importing aliases
224 $xmlalias = $xmlaliases[$k];
225 $aliasname = $xmlalias['#']['NAME'][0]['#'];
227 if (!empty($aliasname)) {
228 $newalias = new stdClass();
229 $newalias->entryid
= $newentry->id
;
230 $newalias->alias
= trim($aliasname);
231 $newalias->id
= $DB->insert_record("glossary_alias",$newalias);
235 if (!empty($data->catsincl
)) {
236 // If the categories must be imported...
237 $xmlcats = @$xmlentry['#']['CATEGORIES'][0]['#']['CATEGORY']; // ignore missing CATEGORIES
238 $sizeofxmlcats = sizeof($xmlcats);
239 for($k = 0; $k < $sizeofxmlcats; $k++
) {
240 $xmlcat = $xmlcats[$k];
242 $newcat = new stdClass();
243 $newcat->name
= $xmlcat['#']['NAME'][0]['#'];
244 $newcat->usedynalink
= $xmlcat['#']['USEDYNALINK'][0]['#'];
245 if ( !$category = $DB->get_record("glossary_categories", array("glossaryid"=>$glossary->id
,"name"=>$newcat->name
))) {
246 // Create the category if it does not exist
247 $category = new stdClass();
248 $category->name
= $newcat->name
;
249 $category->glossaryid
= $glossary->id
;
250 $category->id
= $DB->insert_record("glossary_categories",$category);
254 // inserting the new relation
255 $entrycat = new stdClass();
256 $entrycat->entryid
= $newentry->id
;
257 $entrycat->categoryid
= $category->id
;
258 $DB->insert_record("glossary_entries_categories",$entrycat);
264 if ( $newentry->concept
and $newentry->definition
) {
265 // add to exception report (duplicated entry))
266 $rejections .= "<tr><td>$newentry->concept</td>" .
267 "<td>" . get_string("duplicateentry","glossary"). "</td></tr>";
269 // add to exception report (no concept or definition found))
270 $rejections .= "<tr><td>---</td>" .
271 "<td>" . get_string("noconceptfound","glossary"). "</td></tr>";
277 \mod_glossary\local\concept_cache
::reset_glossary($glossary);
280 echo $OUTPUT->box_start('glossarydisplay generalbox');
281 echo '<table class="glossaryimportexport">';
283 echo '<td width="50%" align="right">';
284 echo get_string("totalentries","glossary");
286 echo '<td width="50%" align="left">';
287 echo $importedentries +
$entriesrejected;
291 echo '<td width="50%" align="right">';
292 echo get_string("importedentries","glossary");
294 echo '<td width="50%" align="left">';
295 echo $importedentries;
296 if ( $entriesrejected ) {
297 echo ' <small>(' . get_string("rejectedentries","glossary") . ": $entriesrejected)</small>";
301 if (!empty($data->catsincl
)) {
303 echo '<td width="50%" align="right">';
304 echo get_string("importedcategories","glossary");
306 echo '<td width="50%">';
311 echo '</table><hr />';
315 echo $OUTPUT->heading(get_string("rejectionrpt","glossary"), 4);
316 echo '<table class="glossaryimportexport">';
318 echo '</table><hr />';
320 /// Print continue button, based on results
321 if ($importedentries) {
322 echo $OUTPUT->continue_button('view.php?id='.$id);
324 echo $OUTPUT->continue_button('import.php?id='.$id);
326 echo $OUTPUT->box_end();
328 echo $OUTPUT->box_start('glossarydisplay generalbox');
329 echo get_string('errorparsingxml', 'glossary');
330 echo $OUTPUT->continue_button('import.php?id='.$id);
331 echo $OUTPUT->box_end();
335 echo $OUTPUT->footer();