New Glossary module, version 0.1 - by Williams Castillo!
[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
6
7$glossary_CONSTANT = 7; /// for example
8
9
10function glossary_add_instance($glossary) {
11/// Given an object containing all the necessary data,
12/// (defined by the form in mod.html) this function
13/// will create a new instance and return the id number
14/// of the new instance.
15
16 $glossary->timecreated = time();
17 $glossary->timemodified = $glossary->timecreated;
18
19 # May have to add extra stuff in here #
20
21 return insert_record("glossary", $glossary);
22}
23
24
25function glossary_update_instance($glossary) {
26/// Given an object containing all the necessary data,
27/// (defined by the form in mod.html) this function
28/// will update an existing instance with new data.
29
30 $glossary->timemodified = time();
31 $glossary->id = $glossary->instance;
32
33 # May have to add extra stuff in here #
34
35 return update_record("glossary", $glossary);
36}
37
38
39function glossary_delete_instance($id) {
40/// Given an ID of an instance of this module,
41/// this function will permanently delete the instance
42/// and any data that depends on it.
43
44 if (! $glossary = get_record("glossary", "id", "$id")) {
45 return false;
46 }
47
48 $result = true;
49
50 # Delete any dependent records here #
51
52 if (! delete_records("glossary", "id", "$glossary->id")) {
53 $result = false;
54 }
55 delete_records("glossary_entries", "glossaryid", "$glossary->id");
56
57 return $result;
58}
59
60function glossary_user_outline($course, $user, $mod, $glossary) {
61/// Return a small object with summary information about what a
62/// user has done with a given particular instance of this module
63/// Used for user activity reports.
64/// $return->time = the time they did it
65/// $return->info = a short text description
66
67 return $return;
68}
69
70function glossary_user_complete($course, $user, $mod, $glossary) {
71/// Print a detailed representation of what a user has done with
72/// a given particular instance of this module, for user activity reports.
73
74 return true;
75}
76
77function glossary_print_recent_activity($course, $isteacher, $timestart) {
78/// Given a course and a time, this module should find recent activity
79/// that has occurred in glossary activities and print it out.
80/// Return true if there was output, or false is there was none.
81
82 global $CFG, $THEME;
83
84 if (!$logs = get_records_select("log", "time > '$timestart' AND ".
85 "course = '$course->id' AND ".
86 "module = 'glossary' AND ".
87 "action = 'add %' ", "time ASC")) {
88 return false;
89 }
90
91 echo "<h1>ANTES</h1>";
92 foreach ($logs as $log) {
93 //Create a temp valid module structure (course,id)
94 $tempmod->course = $log->course;
95 $tempmod->id = $log->info;
96 //Obtain the visible property from the instance
97 $modvisible = instance_is_visible($log->module,$tempmod);
98 echo "<h1>ADENTRO => ANTES</h1>";
99
100 //Only if the mod is visible
101 if ($modvisible) {
102 $entries[$log->info] = glossary_log_info($log);
103 $entries[$log->info]->time = $log->time;
104 $entries[$log->info]->url = $log->url;
105 }
106 echo "<h1>ADENTRO => DESPUES</h1>";
107 }
108
109 echo "<h1>DESPUES</h1>";
110
111 $content = false;
112 if ($entries) {
113 $strftimerecent = get_string("strftimerecent");
114 $content = true;
115 print_headline(get_string("newentries", "glossary").":");
116 foreach ($entries as $entry) {
117 $date = userdate($entry->timemodified, $strftimerecent);
118 echo "<p><font size=1>$date - $entry->firstname $entry->lastname<br>";
119 echo "\"<a href=\"$CFG->wwwroot/mod/glossary/$entry->url\">";
120 echo "$entry->concept";
121 echo "</a>\"</font></p>";
122 }
123 }
124
125 return $content;
126}
127
128function glossary_cron () {
129/// Function to be run periodically according to the moodle cron
130/// This function searches for things that need to be done, such
131/// as sending out mail, toggling flags etc ...
132
133 global $CFG;
134
135 return true;
136}
137
138function glossary_grades($glossaryid) {
139/// Must return an array of grades for a given instance of this module,
140/// indexed by user. It also returns a maximum allowed grade.
141
142 $return->grades = NULL;
143 $return->maxgrade = NULL;
144
145 return $return;
146}
147
148
149//////////////////////////////////////////////////////////////////////////////////////
150/// Any other glossary functions go here. Each of them must have a name that
151/// starts with glossary_
152
153function glossary_log_info($log) {
154 global $CFG;
155 return get_record_sql("SELECT g.*, u.firstname, u.lastname
156 FROM {$CFG->prefix}glossary_entries g,
157 {$CFG->prefix}user u
158 WHERE g.glossaryid = '$log->info'
159 AND u.id = '$log->userid'");
160}
161
162function glossary_get_entries($glossaryid, $entrylist) {
163 global $CFG;
164
165 return get_records_sql("SELECT id,userid,concept,definition,format
166 FROM {$CFG->prefix}glossary_entries
167 WHERE glossaryid = '$glossaryid'
168 AND id IN ($entrylist)");
169}
170
171function glossary_print_entry($course, $cm, $glossary, $entry) {
172 switch ( $glossary->displayformat ) {
173 case 0:
174 echo "<table width=70% border=0><tr><td>";
175 glossary_print_entry_with_user($course, $cm, $glossary, $entry);
176 echo "</td></tr></table>";
177 break;
178 case 1:
179 echo "<table width=70% border=0><tr><td>";
180 glossary_print_entry_without_user($course, $cm, $glossary, $entry);
181 echo "</td></tr></table>";
182 break;
183 case 2:
184// echo "<table width=70% border=0><tr><td>";
185 glossary_print_short_entries($course, $cm, $glossary, $entry);
186// echo "</td></tr></table>";
187 break;
188 }
189}
190function glossary_print_entry_with_user($course, $cm, $glossary, $entry) {
191 global $THEME, $USER;
192
193// if ($entry->timemarked < $entry->modified) {
194 $colour = $THEME->cellheading2;
195// } else {
196// $colour = $THEME->cellheading;
197// }
198
199 $user = get_record("user", "id", $entry->userid);
200 $strby = get_string("writtenby","glossary");
201
202 echo "\n<TABLE BORDER=1 CELLSPACING=0 valign=top cellpadding=10>";
203
204 echo "\n<TR>";
205 echo "\n<TD ROWSPAN=2 BGCOLOR=\"$THEME->cellheading\" WIDTH=35 VALIGN=TOP>";
206 if ($entry) {
207 print_user_picture($user->id, $course->id, $user->picture);
208 }
209 echo "</TD>";
210 echo "<TD NOWRAP WIDTH=100% BGCOLOR=\"$colour\">";
211 if ($entry) {
212 echo "<b>$entry->concept</b><br><FONT SIZE=2>$strby $user->firstname $user->lastname</font>";
213 echo "&nbsp;&nbsp;<FONT SIZE=1>(".get_string("lastedited").": ".userdate($entry->timemodified).")</FONT></small>";
214 }
215 echo "</TR>";
216
217 echo "\n<TR><TD WIDTH=100% BGCOLOR=\"$THEME->cellcontent\">";
218 if ($entry) {
219 echo format_text($entry->definition, $entry->format);
220
221 glossary_print_entry_icons($course, $cm, $glossary, $entry);
222
223 } else {
224 echo "<center>";
225 print_string("noentry", "glossary");
226 echo "</center>";
227 }
228 echo "</TD></TR>";
229
230 echo "</TABLE>\n";
231}
232
233function glossary_print_entry_without_user($course, $cm, $glossary, $entry) {
234 global $THEME, $USER;
235
236// if ($entry->timemarked < $entry->modified) {
237 $colour = $THEME->cellheading2;
238// } else {
239// $colour = $THEME->cellheading;
240// }
241
242 echo "\n<TABLE BORDER=1 CELLSPACING=0 width=100% valign=top cellpadding=10>";
243
244 echo "\n<TR>";
245 echo "<TD WIDTH=100% BGCOLOR=\"$colour\"><b>$entry->concept</b><br>";
246 if ($entry) {
247 echo "&nbsp;&nbsp;<FONT SIZE=1>".get_string("lastedited").": ".userdate($entry->timemodified)."</FONT>";
248 }
249 echo "</TR>";
250
251 echo "\n<TR><TD WIDTH=100% BGCOLOR=\"$THEME->cellcontent\">";
252 if ($entry) {
253 echo format_text($entry->definition, $entry->format);
254
255 glossary_print_entry_icons($course, $cm, $glossary, $entry);
256
257 } else {
258 echo "<center>";
259 print_string("noentry", "glossary");
260 echo "</center>";
261 }
262 echo "</TD></TR>";
263
264 echo "</TABLE>\n";
265}
266
267function glossary_print_short_entries($course, $cm, $glossary, $entry) {
268 global $THEME, $USER;
269
270 $colour = $THEME->cellheading2;
271
272 echo "\n<TR>";
273 echo "<TD WIDTH=100% BGCOLOR=\"#FFFFFF\"><b>$entry->concept</b>: ";
274 echo format_text($entry->definition, $entry->format);
275 glossary_print_entry_icons($course, $cm, $glossary, $entry);
276 echo "</td>";
277 echo "</TR>";
278}
279
280function glossary_print_entry_icons($course, $cm, $glossary, $entry) {
281 global $THEME, $USER;
282
283 if (isteacher($course->id) or $glossary->studentcanpost and $entry->userid == $USER->id) {
284 echo "<p align=right>";
285 if (isteacher($course->id) and !$glossary->mainglossary) {
286 $mainglossary = get_record("glossary","mainglossary",1,"course",$course->id);
287 if ( $mainglossary ) {
288/* link_to_popup_window ("$CFG->wwwroot/mod/glossary/exportentry.php?id=$cm->id&entry=$entry->id",
289 "popup",
290 "<img alt=\"" . get_string("exporttomainglossary","glossary") . "\"src=\"export.gif\" height=11 width=11 border=0>",
291 400, 500, get_string("exporttomainglossary","glossary"), "none");
292*/
293
294 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> ";
295
296 }
297 }
298 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> ";
299 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>";
300 }
301}
302
303function glossary_search_entries($searchterms, $glossary, $includedefinition) {
304/// Returns a list of entries found using an array of search terms
305/// eg word +word -word
306///
307
308 global $CFG;
309
310 if (!isteacher($glossary->course)) {
311 $glossarymodule = get_record("modules", "name", "glossary");
312 $onlyvisible = " AND f.id = cm.instance AND cm.visible = 1 AND cm.module = $glossarymodule->id";
313 $onlyvisibletable = ", {$CFG->prefix}course_modules cm";
314 } else {
315
316 $onlyvisible = "";
317 $onlyvisibletable = "";
318 }
319
320 /// Some differences in syntax for PostgreSQL
321 if ($CFG->dbtype == "postgres7") {
322 $LIKE = "ILIKE"; // case-insensitive
323 $NOTLIKE = "NOT ILIKE"; // case-insensitive
324 $REGEXP = "~*";
325 $NOTREGEXP = "!~*";
326 } else {
327 $LIKE = "LIKE";
328 $NOTLIKE = "NOT LIKE";
329 $REGEXP = "REGEXP";
330 $NOTREGEXP = "NOT REGEXP";
331 }
332
333 $conceptsearch = "";
334 $definitionsearch = "";
335
336
337 foreach ($searchterms as $searchterm) {
338 if (strlen($searchterm) < 2) {
339 continue;
340 }
341 if ($conceptsearch) {
342 $conceptsearch.= " OR ";
343 }
344 if ($definitionsearch) {
345 $definitionsearch.= " OR ";
346 }
347
348 if (substr($searchterm,0,1) == "+") {
349 $searchterm = substr($searchterm,1);
350 $conceptsearch.= " e.concept $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
351 $definitionsearch .= " e.definition $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
352 } else if (substr($searchterm,0,1) == "-") {
353 $searchterm = substr($searchterm,1);
354 $conceptsearch .= " e.concept $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
355 $definitionsearch .= " e.definition $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
356 } else {
357 $conceptsearch .= " e.concept $LIKE '%$searchterm%' ";
358 $definitionsearch .= " e.definition $LIKE '%$searchterm%' ";
359 }
360 }
361
362 if ( !$includedefinition ) {
363 $definitionsearch = "0";
364 }
365
366 $selectsql = "{$CFG->prefix}glossary_entries e,
367 {$CFG->prefix}glossary g $onlyvisibletable
368 WHERE ($conceptsearch OR $definitionsearch)
369 AND e.glossaryid = g.id $onlyvisible
370 AND g.id = $glossary->id";
371
372 $totalcount = count_records_sql("SELECT COUNT(*) FROM $selectsql");
373
374 return get_records_sql("SELECT e.concept, e.definition, e.userid, e.timemodified, e.id, e.format FROM
375 $selectsql ORDER BY e.concept ASC $limit");
376}
377
378function glossary_get_participants($glossaryid) {
379//Returns the users with data in one glossary
380//(users with records in glossary_entries, students)
381
382 global $CFG;
383
384 //Get students
385 $students = get_records_sql("SELECT DISTINCT u.*
386 FROM {$CFG->prefix}user u,
387 {$CFG->prefix}glossary_entries g
388 WHERE g.glossaryid = '$glossaryid' and
389 u.id = g.userid");
390
391 //Return students array (it contains an array of unique users)
392 return ($students);
393}
394
395?>