5 * This file is include from view.php and print.php
10 /// Creating the SQL statements
12 /// Initialise some variables
15 $textlib = textlib_get_instance();
17 /// Calculate the SQL sortkey to be used by the SQL statements later
20 $sqlsortkey = "timecreated";
23 $sqlsortkey = "timemodified";
26 $sqlsortkey = "firstname";
29 $sqlsortkey = "lastname";
32 $sqlsortorder = $sortorder;
34 /// Pivot is the field that set the break by groups (category, initial, author name, etc)
36 /// fullpivot indicate if the whole pivot should be compared agasint the db or just the first letter
37 /// printpivot indicate if the pivot should be printed or not
42 if ( !empty($USER->id
) ) {
43 $userid = "OR ge.userid = $USER->id";
46 case GLOSSARY_CATEGORY_VIEW
:
47 if ($hook == GLOSSARY_SHOW_ALL_CATEGORIES
) {
49 $sqlselect = "SELECT gec.id AS cid, ge.*, gec.entryid, gc.name AS glossarypivot";
50 $sqlfrom = "FROM {$CFG->prefix}glossary_entries ge,
51 {$CFG->prefix}glossary_entries_categories gec,
52 {$CFG->prefix}glossary_categories gc";
53 $sqlwhere = "WHERE (ge.glossaryid = '$glossary->id' OR ge.sourceglossaryid = '$glossary->id') AND
54 ge.id = gec.entryid AND gc.id = gec.categoryid AND
55 (ge.approved != 0 $userid)";
57 $sqlorderby = ' ORDER BY gc.name, ge.concept';
59 } elseif ($hook == GLOSSARY_SHOW_NOT_CATEGORISED
) {
62 $sqlselect = "SELECT ge.*, concept AS glossarypivot";
63 $sqlfrom = "FROM {$CFG->prefix}glossary_entries ge LEFT JOIN {$CFG->prefix}glossary_entries_categories gec
64 ON ge.id = gec.entryid";
65 $sqlwhere = "WHERE (glossaryid = '$glossary->id' OR sourceglossaryid = '$glossary->id') AND
66 (ge.approved != 0 $userid) AND gec.entryid IS NULL";
69 $sqlorderby = ' ORDER BY concept';
74 $sqlselect = "SELECT ge.*, ce.entryid, c.name AS glossarypivot";
75 $sqlfrom = "FROM {$CFG->prefix}glossary_entries ge, {$CFG->prefix}glossary_entries_categories ce, {$CFG->prefix}glossary_categories c";
76 $sqlwhere = "WHERE ge.id = ce.entryid AND ce.categoryid = '$hook' AND
77 ce.categoryid = c.id AND ge.approved != 0 AND
78 (ge.glossaryid = '$glossary->id' OR ge.sourceglossaryid = '$glossary->id') AND
79 (ge.approved != 0 $userid)";
81 $sqlorderby = ' ORDER BY c.name, ge.concept';
85 case GLOSSARY_AUTHOR_VIEW
:
89 if ( $sqlsortkey == 'firstname' ) {
90 $usernamefield = sql_fullname('u.firstname' , 'u.lastname');
92 $usernamefield = sql_fullname('u.lastname' , 'u.firstname');
94 $where = "AND " . sql_substr() . "(upper($usernamefield),1," . $textlib->strlen($hook) . ") = '" . $textlib->strtoupper($hook) . "'";
96 if ( $hook == 'ALL' ) {
100 $sqlselect = "SELECT ge.*, $usernamefield AS glossarypivot, 1 AS userispivot ";
101 $sqlfrom = "FROM {$CFG->prefix}glossary_entries ge, {$CFG->prefix}user u";
102 $sqlwhere = "WHERE ge.userid = u.id AND
103 (ge.approved != 0 $userid)
105 (ge.glossaryid = '$glossary->id' OR ge.sourceglossaryid = '$glossary->id')";
106 $sqlorderby = "ORDER BY $usernamefield $sqlsortorder, ge.concept";
108 case GLOSSARY_APPROVAL_VIEW
:
113 if ($hook != 'ALL' and $hook != 'SPECIAL') {
114 $where = 'AND ' . sql_substr() . '(upper(concept),1,' . $textlib->strlen($hook) . ') = \'' . $textlib->strtoupper($hook) . '\'';
117 $sqlselect = "SELECT ge.*, ge.concept AS glossarypivot";
118 $sqlfrom = "FROM {$CFG->prefix}glossary_entries ge";
119 $sqlwhere = "WHERE (ge.glossaryid = '$glossary->id' OR ge.sourceglossaryid = '$glossary->id') AND
120 ge.approved = 0 $where";
123 $sqlorderby = "ORDER BY $sqlsortkey $sqlsortorder";
125 $sqlorderby = "ORDER BY ge.concept";
128 case GLOSSARY_DATE_VIEW
:
130 case GLOSSARY_STANDARD_VIEW
:
132 $sqlselect = "SELECT ge.*, ge.concept AS glossarypivot";
133 $sqlfrom = "FROM {$CFG->prefix}glossary_entries ge";
138 $NOTLIKE = 'NOT ' . $LIKE;
143 /// Some differences in syntax for PostgreSQL
144 if ($CFG->dbfamily
== "postgres") {
149 $NOTREGEXP = "NOT REGEXP";
154 $definitionsearch = "";
156 $searchterms = explode(" ",$hook);
158 foreach ($searchterms as $searchterm) {
160 if ($conceptsearch) {
161 $conceptsearch .= " AND ";
164 $aliassearch .= " AND ";
166 if ($definitionsearch) {
167 $definitionsearch .= " AND ";
170 /// Under Oracle and MSSQL, trim the + and - operators and perform
171 /// simpler LIKE search
172 if ($CFG->dbfamily
== 'oracle' ||
$CFG->dbfamily
== 'mssql') {
173 $searchterm = trim($searchterm, '+-');
176 if (substr($searchterm,0,1) == "+") {
177 $searchterm = substr($searchterm,1);
178 $conceptsearch .= " ge.concept $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
179 $aliassearch .= " al.alias $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
180 $definitionsearch .= " ge.definition $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
181 } else if (substr($searchterm,0,1) == "-") {
182 $searchterm = substr($searchterm,1);
183 $conceptsearch .= " ge.concept $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
184 $aliassearch .= " al.alias $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
185 $definitionsearch .= " ge.definition $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
187 $conceptsearch .= " ge.concept $LIKE '%$searchterm%' ";
188 $aliassearch .= " al.alias $LIKE '%$searchterm%' ";
189 $definitionsearch .= " ge.definition $LIKE '%$searchterm%' ";
193 //Search in aliases first
195 $listaliases = array();
196 $recaliases = get_records_sql ("SELECT al.id, al.entryid
197 FROM {$CFG->prefix}glossary_alias al,
198 {$CFG->prefix}glossary_entries ge
199 WHERE (ge.glossaryid = '$glossary->id' OR
200 ge.sourceglossaryid = '$glossary->id') AND
201 (ge.approved != 0 $userid) AND
202 ge.id = al.entryid AND
206 foreach ($recaliases as $recalias) {
207 $listaliases[] = $recalias->entryid
;
209 $idaliases = implode (',',$listaliases);
212 //Add seach conditions in concepts and, if needed, in definitions
214 $where = "AND (( $conceptsearch) ";
216 //Include aliases id if found
217 if (!empty($idaliases)) {
218 $where .= " OR ge.id IN ($idaliases) ";
221 //Include search in definitions if requested
223 $where .= " OR ($definitionsearch) )";
232 $sqlfrom .= " left join {$CFG->prefix}glossary_alias ga on ge.id = ga.entryid ";
233 $where = "AND (ge.concept = '$hook' OR ga.alias = '$hook' )
239 $where = "AND ge.id = '$hook'";
243 if ($hook != 'ALL' and $hook != 'SPECIAL') {
244 $where = 'AND ' . sql_substr() . '(upper(concept),1,' . $textlib->strlen($hook) . ') = \'' . $textlib->strtoupper($hook) . '\'';
246 if ($hook == 'SPECIAL') {
247 //Create appropiate IN contents
248 $alphabet = explode(",", get_string("alphabet"));
250 for ($i = 0; $i < count($alphabet); $i++
) {
254 $sqlalphabet .= '\''.$alphabet[$i].'\'';
256 $where = 'AND ' . sql_substr() . '(upper(concept),1,1) NOT IN (' . $textlib->strtoupper($sqlalphabet) . ')';
261 $sqlwhere = "WHERE (ge.glossaryid = '$glossary->id' or ge.sourceglossaryid = '$glossary->id') AND
262 (ge.approved != 0 $userid)
265 case GLOSSARY_DATE_VIEW
:
266 $sqlorderby = "ORDER BY $sqlsortkey $sqlsortorder";
269 case GLOSSARY_STANDARD_VIEW
:
270 $sqlorderby = "ORDER BY ge.concept";
276 $count = count_records_sql("select count(*) $sqlfrom $sqlwhere");
278 $limitfrom = $offset;
281 if ( $offset >= 0 ) {
282 $limitnum = $entriesbypage;
285 $allentries = get_records_sql("$sqlselect $sqlfrom $sqlwhere $sqlorderby", $limitfrom, $limitnum);