removed undefined notices showing on recent activity on site front page when no user...
[moodle.git] / mod / glossary / import.php
CommitLineData
4528ab53 1<?php // $Id$
748b1932 2
3 require_once("../../config.php");
4 require_once("lib.php");
5 global $CFG, $USER;
6
7 require_variable($id); // Course Module ID
8
9 optional_variable($step,0);
10 optional_variable($dest,"current"); // current | new
11 optional_variable($file); // file to import
12 optional_variable($catsincl,0); // Import Categories too?
13
1ac87c73 14 optional_variable($mode,'letter');
15 optional_variable($hook,"ALL");
748b1932 16
17 if (! $cm = get_record("course_modules", "id", $id)) {
18 error("Course Module ID was incorrect");
19 }
20
21 if (! $course = get_record("course", "id", $cm->course)) {
22 error("Course is misconfigured");
23 }
24
25 if (! $glossary = get_record("glossary", "id", $cm->instance)) {
26 error("Course module is incorrect");
27 }
28
29 require_login($course->id);
30 if (!isteacher($course->id)) {
31 error("You must be a teacher to use this page.");
32 }
33
34 if ($course->category) {
35 $navigation = "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> ->";
36 }
37
38 if ($dest != 'new' and $dest != 'current') {
39 $dest = 'current';
40 }
41 $strglossaries = get_string("modulenameplural", "glossary");
42 $strglossary = get_string("modulename", "glossary");
43 $strallcategories = get_string("allcategories", "glossary");
44 $straddentry = get_string("addentry", "glossary");
45 $strnoentries = get_string("noentries", "glossary");
46 $strsearchconcept = get_string("searchconcept", "glossary");
47 $strsearchindefinition = get_string("searchindefinition", "glossary");
48 $strsearch = get_string("search");
49
50 print_header(strip_tags("$course->shortname: $glossary->name"), "$course->fullname",
51 "$navigation <A HREF=index.php?id=$course->id>$strglossaries</A> -> $glossary->name",
52 "", "", true, update_module_button($cm->id, $course->id, $strglossary),
53 navmenu($course, $cm));
54
55 echo '<p align="center"><font size="3"><b>' . stripslashes_safe($glossary->name);
56 echo '</b></font></p>';
57
58/// Info box
59
60 if ( $glossary->intro ) {
61 print_simple_box_start('center','70%');
62 echo format_text($glossary->intro);
63 print_simple_box_end();
64 }
65
66/// Tabbed browsing sections
67 $tab = GLOSSARY_IMPORT_VIEW;
68 include("tabs.html");
69
70 if ( !$step ) {
71 include("import.html");
72
73 glossary_print_tabbed_table_end();
74 print_footer($course);
75 exit;
76 }
77
78 $form = data_submitted();
79 $file = $_FILES["file"];
80// if ($xml = glossary_read_imported_file("C:\\download\\moodle\\moodle\\cursos\\10\\glossary\\Testing_Glossary\\glossary.xml") ) {
81 if ($xml = glossary_read_imported_file($file['tmp_name']) ) {
82
83 $importedentries = 0;
84 $importedcats = 0;
85 $entriesrejected = 0;
86 $rejections = '';
87 if ($dest == 'new') {
88 // If the user chose to create a new glossary
89 $xmlglossary = $xml['GLOSSARY']['#']['INFO'][0]['#'];
90
91 if ( $xmlglossary['NAME'][0]['#'] ) {
92 unset($glossary);
fd9f0ec8 93 $glossary->name = addslashes(utf8_decode($xmlglossary['NAME'][0]['#']));
748b1932 94 $glossary->course = $course->id;
95 $glossary->globalglossary = $xmlglossary['GLOBALGLOSSARY'][0]['#'];
1ac87c73 96 $glossary->entbypage = $xmlglossary['ENTBYPAGE'][0]['#'];
fd9f0ec8 97 $glossary->intro = addslashes(utf8_decode($xmlglossary['INTRO'][0]['#']));
748b1932 98 $glossary->showspecial = $xmlglossary['SHOWSPECIAL'][0]['#'];
99 $glossary->showalphabet = $xmlglossary['SHOWALPHABET'][0]['#'];
100 $glossary->showall = $xmlglossary['SHOWALL'][0]['#'];
101 $glossary->timecreated = time();
102 $glossary->timemodified = time();
103
104 // Setting the default values if no values were passed
105 if ( isset($xmlglossary['STUDENTCANPOST'][0]['#']) ) {
106 $glossary->studentcanpost = $xmlglossary['STUDENTCANPOST'][0]['#'];
107 } else {
108 $glossary->studentcanpost = $CFG->cnfstudentcanpost;
109 }
110 if ( isset($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']) ) {
111 $glossary->allowduplicatedentries = $xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#'];
112 } else {
113 $glossary->allowduplicatedentries = $CFG->cnfallowdupentries;
114 }
115 if ( isset($xmlglossary['ALLOWCOMMENTS'][0]['#']) ) {
116 $glossary->allowcomments = $xmlglossary['ALLOWCOMMENTS'][0]['#'];
117 } else {
118 $glossary->allowcomments = $CFG->cnfallowcomments;
119 }
120 if ( isset($xmlglossary['USEDYNALINK'][0]['#']) ) {
121 $glossary->usedynalink = $xmlglossary['USEDYNALINK'][0]['#'];
122 } else {
123 $glossary->usedynalink = $CFG->cnflinkglossaries;
124 }
125 if ( isset($xmlglossary['DEFAULTAPPROVAL'][0]['#']) ) {
126 $glossary->defaultapproval = $xmlglossary['DEFAULTAPPROVAL'][0]['#'];
127 } else {
128 $glossary->defaultapproval = $CFG->cnfapprovalstatus;
129 }
130
131 // Include new glossary and return the new ID
132 if ( !$glossary->id = glossary_add_instance($glossary) ) {
133 notify("Error while trying to create the new glossary.");
134 glossary_print_tabbed_table_end();
135 print_footer($course);
136 exit;
137 } else {
138 print_simple_box(get_string("newglossarycreated","glossary"),"center","70%");
139 echo '<p>';
140 }
141 } else {
142 notify("Error while trying to create the new glossary.");
143 glossary_print_tabbed_table_end();
144 print_footer($course);
145 exit;
146 }
147 }
148
149 $xmlentries = $xml['GLOSSARY']['#']['INFO'][0]['#']['ENTRIES'][0]['#']['ENTRY'];
150 for($i = 0; $i < sizeof($xmlentries); $i++) {
151 // Inserting the entries
152 $xmlentry = $xmlentries[$i];
153
154 unset($newentry);
b9656030 155 $newentry->concept = addslashes(trim(utf8_decode($xmlentry['#']['CONCEPT'][0]['#'])));
fd9f0ec8 156 $newentry->definition = addslashes(utf8_decode($xmlentry['#']['DEFINITION'][0]['#']));
748b1932 157
158 $permissiongranted = 1;
159 if ( $newentry->concept and $newentry->definition ) {
160 if ( !$glossary->allowduplicatedentries ) {
161 // checking if the entry is valid (checking if it is duplicated when should not be)
162 if ( $glossary->casesensitive ) {
163 $dupentry = get_record("glossary_entries","concept",$newentry->concept,"glossaryid",$glossary->id);
164 } else {
165 $dupentry = get_record("glossary_entries","ucase(concept)",strtoupper($newentry->concept),"glossaryid",$glossary->id);
166 }
167 if ($dupentry) {
168 $permissiongranted = 0;
169 }
170 }
171 } else {
172 $permissiongranted = 0;
173 }
174 if ($permissiongranted) {
175 $newentry->glossaryid = $glossary->id;
176 $newentry->sourceglossaryid = 0;
177 $newentry->approved = 1;
178 $newentry->userid = $USER->id;
179 $newentry->format = $xmlentry['#']['FORMAT'][0]['#'];
180 $newentry->timecreated = time();
181 $newentry->timemodified = time();
182 $newentry->teacherentry = $xmlentry['#']['TEACHERENTRY'][0]['#'];
183
184 // Setting the default values if no values were passed
185 if ( isset($xmlentry['#']['USEDYNALINK'][0]['#']) ) {
186 $newentry->usedynalink = $xmlentry['#']['USEDYNALINK'][0]['#'];
187 } else {
188 $newentry->usedynalink = $CFG->cnfusedynalink;
189 }
190 if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) {
191 $newentry->casesensitive = $xmlentry['#']['CASESENSITIVE'][0]['#'];
192 } else {
193 $newentry->casesensitive = $CFG->cnfcasesensitive;
194 }
195 if ( isset($xmlentry['#']['FULLMATCH'][0]['#']) ) {
196 $newentry->fullmatch = $xmlentry['#']['FULLMATCH'][0]['#'];
197 } else {
198 $newentry->fullmatch = $CFG->cnffullmatch;
199 }
200
201 if ( $newentry->id = insert_record("glossary_entries",$newentry) ) {
202 $importedentries++;
7965be79 203
204 $xmlaliases = $xmlentry['#']['ALIASES'][0]['#']['ALIAS'];
205 for($k = 0; $k < sizeof($xmlaliases); $k++) {
206 /// Importing aliases
207 $xmlalias = $xmlaliases[$k];
e1013df8 208 $aliasname = $xmlalias['#']['NAME'][0]['#'];
209
210 if (!empty($aliasname)) {
211 unset($newalias);
212 $newalias->entryid = $newentry->id;
213 $newalias->alias = addslashes(trim(utf8_decode($aliasname)));
214 $newalias->id = insert_record("glossary_alias",$newalias);
215 }
7965be79 216 }
217
748b1932 218 if ( $catsincl ) {
219 // If the categories must be imported...
220 $xmlcats = $xmlentry['#']['CATEGORIES'][0]['#']['CATEGORY'];
221 for($k = 0; $k < sizeof($xmlcats); $k++) {
222 $xmlcat = $xmlcats[$k];
223 unset($newcat);
224
9b690d2b 225 $newcat->name = addslashes(utf8_decode($xmlcat['#']['NAME'][0]['#']));
1ac87c73 226 $newcat->usedynalink = $xmlcat['#']['USEDYNALINK'][0]['#'];
748b1932 227 if ( !$category = get_record("glossary_categories","glossaryid",$glossary->id,"name",$newcat->name) ) {
228 // Create the category if it does not exist
229 unset($category);
230 $category->name = $newcat->name;
231 $category->glossaryid = $glossary->id;
232 if ( !$category->id = insert_record("glossary_categories",$category)) {
233 // add to exception report (can't insert category)
234 $rejections .= "<tr><td>&nbsp;<strong>" . get_string("category","glossary") . ":</strong>$newcat->name</td>" .
235 "<td>" . get_string("cantinsertcat","glossary"). "</td></tr>";
236 } else {
237 $importedcats++;
238 }
239 }
240 if ( $category ) {
241 // inserting the new relation
242 unset($entrycat);
243 $entrycat->entryid = $newentry->id;
244 $entrycat->categoryid = $category->id;
245 if ( !insert_record("glossary_entries_categories",$entrycat) ) {
246 // add to exception report (can't insert relation)
247 $rejections .= "<tr><td>&nbsp;<strong>" . get_string("category","glossary") . ":</strong>$newcat->name</td>" .
248 "<td>" . get_string("cantinsertrel","glossary"). "</td></tr>";
249 }
250 }
251 }
252 }
253 } else {
254 $entriesrejected++;
255 // add to exception report (can't insert new record)
256 $rejections .= "<tr><td>$newentry->concept</td>" .
257 "<td>" . get_string("cantinsertrec","glossary"). "</td></tr>";
258 }
259 } else {
260 $entriesrejected++;
261 if ( $newentry->concept and $newentry->definition ) {
262 // add to exception report (duplicated entry))
263 $rejections .= "<tr><td>$newentry->concept</td>" .
264 "<td>" . get_string("duplicateentry","glossary"). "</td></tr>";
265 } else {
266 // add to exception report (no concept or definition found))
267 $rejections .= "<tr><td>---</td>" .
268 "<td>" . get_string("noconceptfound","glossary"). "</td></tr>";
269 }
270 }
271 }
272 // processed entries
273 echo '<table border=0 width=100%>';
274 echo '<tr>';
275 echo '<td width=50% align=right>';
276 echo get_string("totalentries","glossary");
277 echo ':</td>';
278 echo '<td width=50%>';
279 echo $importedentries + $entriesrejected;
280 echo '</td>';
281 echo '</tr>';
282 echo '<tr>';
283 echo '<td width=50% align=right>';
284 echo get_string("importedentries","glossary");
285 echo ':</td>';
286 echo '<td width=50%>';
287 echo $importedentries;
288 if ( $entriesrejected ) {
289 echo ' <small>(' . get_string("rejectedentries","glossary") . ": $entriesrejected)</small>";
290 }
291 echo '</td>';
292 echo '</tr>';
293 if ( $catsincl ) {
294 echo '<tr>';
295 echo '<td width=50% align=right>';
296 echo get_string("importedcategories","glossary");
297 echo ':</td>';
298 echo '<td width=50%>';
299 echo $importedcats;
300 echo '</td>';
301 echo '</tr>';
302 }
303 echo '</table><hr width=75%>';
304
305 // rejected entries
306 if ($rejections) {
307 echo '<center><table border=0 width=70%>';
308 echo '<tr><td align=center colspan=2 width=100%><strong>' . get_string("rejectionrpt","glossary") . '</strong></tr>';
309 echo $rejections;
310 echo '</table></center><p><hr width=75%>';
311 }
312 } else {
313 notify("Error while trying to read the file.");
314 }
315
316 glossary_print_tabbed_table_end();
317
318/// Finish the page
319 print_footer($course);
320
9b690d2b 321?>