- Initial changes to categorize the entries..
[moodle.git] / mod / glossary / lib.php
CommitLineData
07842023 1<?PHP // $Id$
2
3/// Library of functions and constants for module glossary
4/// (replace glossary with the name of your module and delete this line)
5
e179048e 6require_once("$CFG->dirroot/files/mimetypes.php");
07842023 7
8function glossary_add_instance($glossary) {
9/// Given an object containing all the necessary data,
10/// (defined by the form in mod.html) this function
11/// will create a new instance and return the id number
12/// of the new instance.
13
14 $glossary->timecreated = time();
15 $glossary->timemodified = $glossary->timecreated;
16
17 # May have to add extra stuff in here #
18
19 return insert_record("glossary", $glossary);
20}
21
22
23function glossary_update_instance($glossary) {
24/// Given an object containing all the necessary data,
25/// (defined by the form in mod.html) this function
26/// will update an existing instance with new data.
27
28 $glossary->timemodified = time();
29 $glossary->id = $glossary->instance;
30
31 # May have to add extra stuff in here #
32
33 return update_record("glossary", $glossary);
34}
35
36
37function glossary_delete_instance($id) {
38/// Given an ID of an instance of this module,
39/// this function will permanently delete the instance
40/// and any data that depends on it.
41
42 if (! $glossary = get_record("glossary", "id", "$id")) {
43 return false;
44 }
45
46 $result = true;
47
48 # Delete any dependent records here #
49
50 if (! delete_records("glossary", "id", "$glossary->id")) {
51 $result = false;
52 }
53 delete_records("glossary_entries", "glossaryid", "$glossary->id");
54
55 return $result;
56}
57
58function glossary_user_outline($course, $user, $mod, $glossary) {
59/// Return a small object with summary information about what a
60/// user has done with a given particular instance of this module
61/// Used for user activity reports.
62/// $return->time = the time they did it
63/// $return->info = a short text description
64
65 return $return;
66}
67
68function glossary_user_complete($course, $user, $mod, $glossary) {
69/// Print a detailed representation of what a user has done with
70/// a given particular instance of this module, for user activity reports.
71
72 return true;
73}
74
75function glossary_print_recent_activity($course, $isteacher, $timestart) {
76/// Given a course and a time, this module should find recent activity
77/// that has occurred in glossary activities and print it out.
78/// Return true if there was output, or false is there was none.
79
80 global $CFG, $THEME;
81
82 if (!$logs = get_records_select("log", "time > '$timestart' AND ".
83 "course = '$course->id' AND ".
84 "module = 'glossary' AND ".
85 "action = 'add %' ", "time ASC")) {
86 return false;
87 }
88
07842023 89 foreach ($logs as $log) {
90 //Create a temp valid module structure (course,id)
91 $tempmod->course = $log->course;
92 $tempmod->id = $log->info;
93 //Obtain the visible property from the instance
94 $modvisible = instance_is_visible($log->module,$tempmod);
07842023 95
96 //Only if the mod is visible
97 if ($modvisible) {
98 $entries[$log->info] = glossary_log_info($log);
99 $entries[$log->info]->time = $log->time;
100 $entries[$log->info]->url = $log->url;
101 }
07842023 102 }
103
07842023 104 $content = false;
105 if ($entries) {
106 $strftimerecent = get_string("strftimerecent");
107 $content = true;
108 print_headline(get_string("newentries", "glossary").":");
109 foreach ($entries as $entry) {
110 $date = userdate($entry->timemodified, $strftimerecent);
111 echo "<p><font size=1>$date - $entry->firstname $entry->lastname<br>";
112 echo "\"<a href=\"$CFG->wwwroot/mod/glossary/$entry->url\">";
113 echo "$entry->concept";
114 echo "</a>\"</font></p>";
115 }
116 }
117
118 return $content;
119}
120
121function glossary_cron () {
122/// Function to be run periodically according to the moodle cron
123/// This function searches for things that need to be done, such
124/// as sending out mail, toggling flags etc ...
125
126 global $CFG;
127
128 return true;
129}
130
131function glossary_grades($glossaryid) {
132/// Must return an array of grades for a given instance of this module,
133/// indexed by user. It also returns a maximum allowed grade.
134
135 $return->grades = NULL;
136 $return->maxgrade = NULL;
137
138 return $return;
139}
140
141
142//////////////////////////////////////////////////////////////////////////////////////
143/// Any other glossary functions go here. Each of them must have a name that
144/// starts with glossary_
145
146function glossary_log_info($log) {
147 global $CFG;
148 return get_record_sql("SELECT g.*, u.firstname, u.lastname
149 FROM {$CFG->prefix}glossary_entries g,
150 {$CFG->prefix}user u
151 WHERE g.glossaryid = '$log->info'
152 AND u.id = '$log->userid'");
153}
154
155function glossary_get_entries($glossaryid, $entrylist) {
156 global $CFG;
157
158 return get_records_sql("SELECT id,userid,concept,definition,format
159 FROM {$CFG->prefix}glossary_entries
160 WHERE glossaryid = '$glossaryid'
161 AND id IN ($entrylist)");
162}
163
164function glossary_print_entry($course, $cm, $glossary, $entry) {
e179048e 165 global $THEME, $USET, $CFG;
166
167 $PermissionGranted = 0;
168 $formatfile = "$CFG->dirroot/mod/glossary/formats/$glossary->displayformat.php";
169 $functionname = "glossary_print_entry_by_format";
170
171 if ( $glossary->displayformat > 0 ) {
172 if ( file_exists($formatfile) ) {
173 include_once($formatfile);
174 if (function_exists($functionname) ) {
175 $PermissionGranted = 1;
176 }
177 }
07842023 178 } else {
e179048e 179 $PermissionGranted = 1;
07842023 180 }
e179048e 181 if ( $glossary->displayformat > 0 and $PermissionGranted ) {
182 glossary_print_entry_by_format($course, $cm, $glossary, $entry);
07842023 183 } else {
e179048e 184 glossary_print_entry_by_default($course, $cm, $glossary, $entry);
07842023 185 }
07842023 186
07842023 187}
188
e179048e 189function glossary_print_entry_by_default($course, $cm, $glossary, $entry) {
07842023 190 global $THEME, $USER;
191
192 $colour = $THEME->cellheading2;
193
194 echo "\n<TR>";
e179048e 195 echo "<TD WIDTH=100% BGCOLOR=\"#FFFFFF\">";
196 if ($entry->attachment) {
197 $entry->course = $course->id;
198 echo "<table border=0 align=right><tr><td>";
199 echo glossary_print_attachments($entry,"html");
200 echo "</td></tr></table>";
201 }
202 echo "<b>$entry->concept</b>: ";
07842023 203 echo format_text($entry->definition, $entry->format);
204 glossary_print_entry_icons($course, $cm, $glossary, $entry);
205 echo "</td>";
206 echo "</TR>";
207}
208
209function glossary_print_entry_icons($course, $cm, $glossary, $entry) {
210 global $THEME, $USER;
211
212 if (isteacher($course->id) or $glossary->studentcanpost and $entry->userid == $USER->id) {
213 echo "<p align=right>";
214 if (isteacher($course->id) and !$glossary->mainglossary) {
215 $mainglossary = get_record("glossary","mainglossary",1,"course",$course->id);
216 if ( $mainglossary ) {
07842023 217
218 echo "<a href=\"exportentry.php?id=$cm->id&entry=$entry->id\"><img alt=\"" . get_string("exporttomainglossary","glossary") . "\"src=\"export.gif\" height=11 width=11 border=0></a> ";
219
220 }
221 }
222 echo "<a href=\"deleteentry.php?id=$cm->id&mode=delete&entry=$entry->id\"><img alt=\"" . get_string("delete") . "\"src=\"../../pix/t/delete.gif\" height=11 width=11 border=0></a> ";
223 echo "<a href=\"edit.php?id=$cm->id&e=$entry->id\"><img alt=\"" . get_string("edit") . "\" src=\"../../pix/t/edit.gif\" height=11 width=11 border=0></a>";
224 }
225}
226
227function glossary_search_entries($searchterms, $glossary, $includedefinition) {
228/// Returns a list of entries found using an array of search terms
229/// eg word +word -word
230///
231
232 global $CFG;
233
234 if (!isteacher($glossary->course)) {
235 $glossarymodule = get_record("modules", "name", "glossary");
6a22879b 236 $onlyvisible = " AND g.id = cm.instance AND cm.visible = 1 AND cm.module = $glossarymodule->id";
07842023 237 $onlyvisibletable = ", {$CFG->prefix}course_modules cm";
238 } else {
239
240 $onlyvisible = "";
241 $onlyvisibletable = "";
242 }
243
244 /// Some differences in syntax for PostgreSQL
245 if ($CFG->dbtype == "postgres7") {
246 $LIKE = "ILIKE"; // case-insensitive
247 $NOTLIKE = "NOT ILIKE"; // case-insensitive
248 $REGEXP = "~*";
249 $NOTREGEXP = "!~*";
250 } else {
251 $LIKE = "LIKE";
252 $NOTLIKE = "NOT LIKE";
253 $REGEXP = "REGEXP";
254 $NOTREGEXP = "NOT REGEXP";
255 }
256
257 $conceptsearch = "";
258 $definitionsearch = "";
259
260
261 foreach ($searchterms as $searchterm) {
262 if (strlen($searchterm) < 2) {
263 continue;
264 }
265 if ($conceptsearch) {
266 $conceptsearch.= " OR ";
267 }
268 if ($definitionsearch) {
269 $definitionsearch.= " OR ";
270 }
271
272 if (substr($searchterm,0,1) == "+") {
273 $searchterm = substr($searchterm,1);
274 $conceptsearch.= " e.concept $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
275 $definitionsearch .= " e.definition $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
276 } else if (substr($searchterm,0,1) == "-") {
277 $searchterm = substr($searchterm,1);
278 $conceptsearch .= " e.concept $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
279 $definitionsearch .= " e.definition $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
280 } else {
281 $conceptsearch .= " e.concept $LIKE '%$searchterm%' ";
282 $definitionsearch .= " e.definition $LIKE '%$searchterm%' ";
283 }
284 }
285
286 if ( !$includedefinition ) {
287 $definitionsearch = "0";
288 }
289
290 $selectsql = "{$CFG->prefix}glossary_entries e,
291 {$CFG->prefix}glossary g $onlyvisibletable
292 WHERE ($conceptsearch OR $definitionsearch)
293 AND e.glossaryid = g.id $onlyvisible
294 AND g.id = $glossary->id";
295
296 $totalcount = count_records_sql("SELECT COUNT(*) FROM $selectsql");
297
298 return get_records_sql("SELECT e.concept, e.definition, e.userid, e.timemodified, e.id, e.format FROM
299 $selectsql ORDER BY e.concept ASC $limit");
300}
301
302function glossary_get_participants($glossaryid) {
303//Returns the users with data in one glossary
304//(users with records in glossary_entries, students)
305
306 global $CFG;
307
308 //Get students
309 $students = get_records_sql("SELECT DISTINCT u.*
310 FROM {$CFG->prefix}user u,
311 {$CFG->prefix}glossary_entries g
312 WHERE g.glossaryid = '$glossaryid' and
313 u.id = g.userid");
314
315 //Return students array (it contains an array of unique users)
316 return ($students);
317}
318
e179048e 319
320function glossary_file_area_name($entry) {
321// Creates a directory file name, suitable for make_upload_directory()
322 global $CFG;
323
324 return "$entry->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id";
325}
326
327function glossary_file_area($entry) {
328 return make_upload_directory( glossary_file_area_name($entry) );
329}
330
331function glossary_delete_old_attachments($entry, $exception="") {
332// Deletes all the user files in the attachments area for a entry
333// EXCEPT for any file named $exception
334
335 if ($basedir = glossary_file_area($entry)) {
336 if ($files = get_directory_list($basedir)) {
337 foreach ($files as $file) {
338 if ($file != $exception) {
339 unlink("$basedir/$file");
340// notify("Existing file '$file' has been deleted!");
341 }
342 }
343 }
344 if (!$exception) { // Delete directory as well, if empty
345 rmdir("$basedir");
346 }
347 }
348}
349
350function glossary_copy_attachments($entry, $newentry) {
351/// Given a entry object that is being copied to glossaryid,
352/// this function checks that entry
353/// for attachments, and if any are found, these are
354/// copied to the new glossary directory.
355
356 global $CFG;
357
358 $return = true;
359
360 if ($entries = get_records_select("glossary_entries", "id = '$entry->id' AND attachment <> ''")) {
361 foreach ($entries as $curentry) {
362 $oldentry->id = $entry->id;
363 $oldentry->course = $entry->course;
364 $oldentry->glossaryid = $curentry->glossaryid;
365 $oldentrydir = "$CFG->dataroot/".glossary_file_area_name($oldentry);
366 if (is_dir($oldentrydir)) {
367
368 $newentrydir = glossary_file_area($newentry);
369 if (! copy("$oldentrydir/$newentry->attachment", "$newentrydir/$newentry->attachment")) {
370 $return = false;
371 }
372 }
373 }
374 }
375 return $return;
376}
377
378function glossary_move_attachments($entry, $glossaryid) {
379/// Given a entry object that is being moved to glossaryid,
380/// this function checks that entry
381/// for attachments, and if any are found, these are
382/// moved to the new glossary directory.
383
384 global $CFG;
385
386 $return = true;
387
388 if ($entries = get_records_select("glossary_entries", "glossaryid = '$entry->id' AND attachment <> ''")) {
389 foreach ($entries as $entry) {
390 $oldentry->course = $entry->course;
391 $oldentry->glossaryid = $entry->glossaryid;
392 $oldentrydir = "$CFG->dataroot/".glossary_file_area_name($oldentry);
393 if (is_dir($oldentrydir)) {
394 $newentry = $oldentry;
395 $newentry->glossaryid = $glossaryid;
396 $newentrydir = "$CFG->dataroot/".glossary_file_area_name($newentry);
397 if (! @rename($oldentrydir, $newentrydir)) {
398 $return = false;
399 }
400 }
401 }
402 }
403 return $return;
404}
405
406function glossary_add_attachment($entry, $newfile) {
407// $entry is a full entry record, including course and glossary
408// $newfile is a full upload array from $_FILES
409// If successful, this function returns the name of the file
410
411 global $CFG;
412
413 if (empty($newfile['name'])) {
414 return "";
415 }
416
417 $newfile_name = clean_filename($newfile['name']);
418
419 if (valid_uploaded_file($newfile)) {
420 if (! $newfile_name) {
421 notify("This file had a wierd filename and couldn't be uploaded");
422
423 } else if (! $dir = glossary_file_area($entry)) {
424 notify("Attachment could not be stored");
425 $newfile_name = "";
426
427 } else {
428 if (move_uploaded_file($newfile['tmp_name'], "$dir/$newfile_name")) {
429 chmod("$dir/$newfile_name", $CFG->directorypermissions);
430 glossary_delete_old_attachments($entry, $newfile_name);
431 } else {
432 notify("An error happened while saving the file on the server");
433 $newfile_name = "";
434 }
435 }
436 } else {
437 $newfile_name = "";
438 }
439
440 return $newfile_name;
441}
442
443function glossary_print_attachments($entry, $return=NULL) {
444// if return=html, then return a html string.
445// if return=text, then return a text-only string.
446// otherwise, print HTML for non-images, and return image HTML
447
448 global $CFG;
449
450 $filearea = glossary_file_area_name($entry);
451
452 $imagereturn = "";
453 $output = "";
454
455 if ($basedir = glossary_file_area($entry)) {
456 if ($files = get_directory_list($basedir)) {
457 $strattachment = get_string("attachment", "glossary");
458 $strpopupwindow = get_string("popupwindow");
459 foreach ($files as $file) {
460 $icon = mimeinfo("icon", $file);
461 if ($CFG->slasharguments) {
462 $ffurl = "file.php/$filearea/$file";
463 } else {
464 $ffurl = "file.php?file=/$filearea/$file";
465 }
466 $image = "<img border=0 src=\"$CFG->wwwroot/files/pix/$icon\" height=16 width=16 alt=\"$strpopupwindow\">";
467
468 if ($return == "html") {
469 $output .= "<a target=_image href=\"$CFG->wwwroot/$ffurl\">$image</a> ";
470 $output .= "<a target=_image href=\"$CFG->wwwroot/$ffurl\">$file</a><br />";
471 } else if ($return == "text") {
472 $output .= "$strattachment $file:\n$CFG->wwwroot/$ffurl\n";
473
474 } else {
475 if ($icon == "image.gif") { // Image attachments don't get printed as links
476 $imagereturn .= "<br /><img src=\"$CFG->wwwroot/$ffurl\">";
477 } else {
478 link_to_popup_window("/$ffurl", "attachment", $image, 500, 500, $strattachment);
479 echo "<a target=_image href=\"$CFG->wwwroot/$ffurl\">$file</a>";
480 echo "<br />";
481 }
482 }
483 }
484 }
485 }
486
487 if ($return) {
488 return $output;
489 }
490
491 return $imagereturn;
492}
493
494?>