3 require_once("../../config.php");
4 require_once("lib.php");
5 require_once("$CFG->dirroot/course/lib.php");
8 $id = required_param('id', PARAM_INT
); // Course Module ID
10 $step = optional_param('step', 0, PARAM_INT
);
11 $dest = optional_param('dest', 'current', PARAM_ALPHA
); // current | new
12 $file = optional_param('file', '', PARAM_FILE
); // file to import
13 $catsincl = optional_param('catsincl', 0, PARAM_INT
); // Import Categories too?
15 $mode = optional_param('mode', 'letter', PARAM_ALPHA
);
16 $hook = optional_param('hook', 'ALL', PARAM_ALPHANUM
);
18 if (! $cm = get_coursemodule_from_id('glossary', $id)) {
19 error("Course Module ID was incorrect");
22 if (! $course = get_record("course", "id", $cm->course
)) {
23 error("Course is misconfigured");
26 if (! $glossary = get_record("glossary", "id", $cm->instance
)) {
27 error("Course module is incorrect");
30 require_login($course->id
, false, $cm);
32 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
33 require_capability('mod/glossary:import', $context);
35 if ($dest != 'new' and $dest != 'current') {
38 $strglossaries = get_string("modulenameplural", "glossary");
39 $strglossary = get_string("modulename", "glossary");
40 $strallcategories = get_string("allcategories", "glossary");
41 $straddentry = get_string("addentry", "glossary");
42 $strnoentries = get_string("noentries", "glossary");
43 $strsearchconcept = get_string("searchconcept", "glossary");
44 $strsearchindefinition = get_string("searchindefinition", "glossary");
45 $strsearch = get_string("search");
46 $strimportentries = get_string('importentriesfromxml', 'glossary');
48 $navigation = build_navigation($strimportentries, $cm);
49 print_header_simple(format_string($glossary->name
), "", $navigation,
50 "", "", true, update_module_button($cm->id
, $course->id
, $strglossary),
51 navmenu($course, $cm));
53 print_heading($strimportentries);
56 print_box_start('glossarydisplay generalbox');
57 include("import.html");
60 print_footer($course);
64 $form = data_submitted();
65 $file = $_FILES["file"];
67 require_once($CFG->dirroot
.'/lib/uploadlib.php');
68 $um = new upload_manager('file',false,false,$course,false,0);
70 if (!$um->preprocess_files()) {
71 print_box_start('glossarydisplay generalbox');
72 print_continue('import.php?id='.$id);
79 if ($xml = glossary_read_imported_file($file['tmp_name']) ) {
86 // If the user chose to create a new glossary
87 $xmlglossary = $xml['GLOSSARY']['#']['INFO'][0]['#'];
89 if ( $xmlglossary['NAME'][0]['#'] ) {
91 $glossary->name
= addslashes($xmlglossary['NAME'][0]['#']);
92 $glossary->course
= $course->id
;
93 $glossary->globalglossary
= addslashes($xmlglossary['GLOBALGLOSSARY'][0]['#']);
94 $glossary->intro
= addslashes($xmlglossary['INTRO'][0]['#']);
95 $glossary->showspecial
= addslashes($xmlglossary['SHOWSPECIAL'][0]['#']);
96 $glossary->showalphabet
= addslashes($xmlglossary['SHOWALPHABET'][0]['#']);
97 $glossary->showall
= addslashes($xmlglossary['SHOWALL'][0]['#']);
98 $glossary->timecreated
= time();
99 $glossary->timemodified
= time();
101 // Setting the default values if no values were passed
102 if ( isset($xmlglossary['ENTBYPAGE'][0]['#']) ) {
103 $glossary->entbypage
= addslashes($xmlglossary['ENTBYPAGE'][0]['#']);
105 $glossary->entbypage
= $CFG->glossary_entbypage
;
107 if ( isset($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']) ) {
108 $glossary->allowduplicatedentries
= addslashes($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']);
110 $glossary->allowduplicatedentries
= $CFG->glossary_dupentries
;
112 if ( isset($xmlglossary['DISPLAYFORMAT'][0]['#']) ) {
113 $glossary->displayformat
= addslashes($xmlglossary['DISPLAYFORMAT'][0]['#']);
115 $glossary->displayformat
= 2;
117 if ( isset($xmlglossary['ALLOWCOMMENTS'][0]['#']) ) {
118 $glossary->allowcomments
= addslashes($xmlglossary['ALLOWCOMMENTS'][0]['#']);
120 $glossary->allowcomments
= $CFG->glossary_allowcomments
;
122 if ( isset($xmlglossary['USEDYNALINK'][0]['#']) ) {
123 $glossary->usedynalink
= addslashes($xmlglossary['USEDYNALINK'][0]['#']);
125 $glossary->usedynalink
= $CFG->glossary_linkentries
;
127 if ( isset($xmlglossary['DEFAULTAPPROVAL'][0]['#']) ) {
128 $glossary->defaultapproval
= addslashes($xmlglossary['DEFAULTAPPROVAL'][0]['#']);
130 $glossary->defaultapproval
= $CFG->glossary_defaultapproval
;
133 // Include new glossary and return the new ID
134 if ( !$glossary->id
= glossary_add_instance($glossary) ) {
135 notify("Error while trying to create the new glossary.");
137 glossary_print_tabbed_table_end();
138 print_footer($course);
141 //The instance has been created, so lets do course_modules
142 //and course_sections
143 $mod->groupmode
= $course->groupmode
; /// Default groupmode the same as course
145 $mod->instance
= $glossary->id
;
146 // course_modules and course_sections each contain a reference
147 // to each other, so we have to update one of them twice.
149 if (! $currmodule = get_record("modules", "name", 'glossary')) {
150 error("Glossary module doesn't exist");
152 $mod->module
= $currmodule->id
;
153 $mod->course
= $course->id
;
154 $mod->modulename
= 'glossary';
157 if (! $mod->coursemodule
= add_course_module($mod) ) {
158 error("Could not add a new course module");
161 if (! $sectionid = add_mod_to_section($mod) ) {
162 error("Could not add the new course module to that section");
164 //We get the section's visible field status
165 $visible = get_field("course_sections","visible","id",$sectionid);
167 if (! set_field("course_modules", "visible", $visible, "id", $mod->coursemodule
)) {
168 error("Could not update the course module with the correct visibility");
171 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule
)) {
172 error("Could not update the course module with the correct section");
174 add_to_log($course->id
, "course", "add mod",
175 "../mod/$mod->modulename/view.php?id=$mod->coursemodule",
176 "$mod->modulename $mod->instance");
177 add_to_log($course->id
, $mod->modulename
, "add",
178 "view.php?id=$mod->coursemodule",
179 "$mod->instance", $mod->coursemodule
);
181 rebuild_course_cache($course->id
);
183 print_simple_box(get_string("newglossarycreated","glossary"),"center","70%");
187 notify("Error while trying to create the new glossary.");
188 print_footer($course);
193 $xmlentries = $xml['GLOSSARY']['#']['INFO'][0]['#']['ENTRIES'][0]['#']['ENTRY'];
194 for($i = 0; $i < sizeof($xmlentries); $i++
) {
195 // Inserting the entries
196 $xmlentry = $xmlentries[$i];
198 $newentry->concept
= trim(addslashes($xmlentry['#']['CONCEPT'][0]['#']));
199 $newentry->definition
= trusttext_strip(addslashes($xmlentry['#']['DEFINITION'][0]['#']));
200 if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) {
201 $newentry->casesensitive
= addslashes($xmlentry['#']['CASESENSITIVE'][0]['#']);
203 $newentry->casesensitive
= $CFG->glossary_casesensitive
;
206 $permissiongranted = 1;
207 if ( $newentry->concept
and $newentry->definition
) {
208 if ( !$glossary->allowduplicatedentries
) {
209 // checking if the entry is valid (checking if it is duplicated when should not be)
210 if ( $newentry->casesensitive
) {
211 $dupentry = get_record("glossary_entries","concept",$newentry->concept
,"glossaryid",$glossary->id
);
213 $dupentry = get_record("glossary_entries","lower(concept)",moodle_strtolower($newentry->concept
),"glossaryid",$glossary->id
);
216 $permissiongranted = 0;
220 $permissiongranted = 0;
222 if ($permissiongranted) {
223 $newentry->glossaryid
= $glossary->id
;
224 $newentry->sourceglossaryid
= 0;
225 $newentry->approved
= 1;
226 $newentry->userid
= $USER->id
;
227 $newentry->teacherentry
= 1;
228 $newentry->format
= addslashes($xmlentry['#']['FORMAT'][0]['#']);
229 $newentry->timecreated
= time();
230 $newentry->timemodified
= time();
232 // Setting the default values if no values were passed
233 if ( isset($xmlentry['#']['USEDYNALINK'][0]['#']) ) {
234 $newentry->usedynalink
= addslashes($xmlentry['#']['USEDYNALINK'][0]['#']);
236 $newentry->usedynalink
= $CFG->glossary_linkentries
;
238 if ( isset($xmlentry['#']['FULLMATCH'][0]['#']) ) {
239 $newentry->fullmatch
= addslashes($xmlentry['#']['FULLMATCH'][0]['#']);
241 $newentry->fullmatch
= $CFG->glossary_fullmatch
;
244 if ( $newentry->id
= insert_record("glossary_entries",$newentry) ) {
247 $xmlaliases = @$xmlentry['#']['ALIASES'][0]['#']['ALIAS']; // ignore missing ALIASES
248 for($k = 0; $k < sizeof($xmlaliases); $k++
) {
249 /// Importing aliases
250 $xmlalias = $xmlaliases[$k];
251 $aliasname = $xmlalias['#']['NAME'][0]['#'];
253 if (!empty($aliasname)) {
255 $newalias->entryid
= $newentry->id
;
256 $newalias->alias
= trim(addslashes($aliasname));
257 $newalias->id
= insert_record("glossary_alias",$newalias);
262 // If the categories must be imported...
263 $xmlcats = @$xmlentry['#']['CATEGORIES'][0]['#']['CATEGORY']; // ignore missing CATEGORIES
264 for($k = 0; $k < sizeof($xmlcats); $k++
) {
265 $xmlcat = $xmlcats[$k];
268 $newcat->name
= addslashes($xmlcat['#']['NAME'][0]['#']);
269 $newcat->usedynalink
= addslashes($xmlcat['#']['USEDYNALINK'][0]['#']);
270 if ( !$category = get_record("glossary_categories","glossaryid",$glossary->id
,"name",$newcat->name
) ) {
271 // Create the category if it does not exist
273 $category->name
= $newcat->name
;
274 $category->glossaryid
= $glossary->id
;
275 if ( !$category->id
= insert_record("glossary_categories",$category)) {
276 // add to exception report (can't insert category)
277 $rejections .= "<tr><td> <strong>" . get_string("category","glossary") . ":</strong>$newcat->name</td>" .
278 "<td>" . get_string("cantinsertcat","glossary"). "</td></tr>";
284 // inserting the new relation
286 $entrycat->entryid
= $newentry->id
;
287 $entrycat->categoryid
= $category->id
;
288 if ( !insert_record("glossary_entries_categories",$entrycat) ) {
289 // add to exception report (can't insert relation)
290 $rejections .= "<tr><td> <strong>" . get_string("category","glossary") . ":</strong>$newcat->name</td>" .
291 "<td>" . get_string("cantinsertrel","glossary"). "</td></tr>";
298 // add to exception report (can't insert new record)
299 $rejections .= "<tr><td>$newentry->concept</td>" .
300 "<td>" . get_string("cantinsertrec","glossary"). "</td></tr>";
304 if ( $newentry->concept
and $newentry->definition
) {
305 // add to exception report (duplicated entry))
306 $rejections .= "<tr><td>$newentry->concept</td>" .
307 "<td>" . get_string("duplicateentry","glossary"). "</td></tr>";
309 // add to exception report (no concept or definition found))
310 $rejections .= "<tr><td>---</td>" .
311 "<td>" . get_string("noconceptfound","glossary"). "</td></tr>";
316 print_box_start('glossarydisplay generalbox');
317 echo '<table class="glossaryimportexport">';
319 echo '<td width="50%" align="right">';
320 echo get_string("totalentries","glossary");
322 echo '<td width="50%" align="left">';
323 echo $importedentries +
$entriesrejected;
327 echo '<td width="50%" align="right">';
328 echo get_string("importedentries","glossary");
330 echo '<td width="50%" align="left">';
331 echo $importedentries;
332 if ( $entriesrejected ) {
333 echo ' <small>(' . get_string("rejectedentries","glossary") . ": $entriesrejected)</small>";
339 echo '<td width="50%" align="right">';
340 echo get_string("importedcategories","glossary");
342 echo '<td width="50%">';
347 echo '</table><hr />';
351 echo '<table class="glossaryimportexport">';
352 echo '<tr><td align="center" colspan="2" width="100%"><strong>' . get_string("rejectionrpt","glossary") . '</strong></tr>';
354 echo '</table><hr />';
356 /// Print continue button, based on results
357 if ($importedentries) {
358 print_continue('view.php?id='.$id);
360 print_continue('import.php?id='.$id);
364 notice(get_string('errorparsingxml', 'glossary'));
368 print_footer($course);