2003-11-18 13:49:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SQL.PHP
|
|
|
|
* This file is include from view.php and print.php
|
|
|
|
* @version $Id$
|
2008-06-08 10:43:39 +00:00
|
|
|
* @copyright 2003
|
2003-11-18 13:49:31 +00:00
|
|
|
**/
|
|
|
|
|
|
|
|
/// Creating the SQL statements
|
|
|
|
|
2004-11-28 23:54:11 +00:00
|
|
|
/// Initialise some variables
|
|
|
|
$sqlorderby = '';
|
2006-05-21 07:42:58 +00:00
|
|
|
$sqlsortkey = NULL;
|
2006-07-12 20:29:18 +00:00
|
|
|
$textlib = textlib_get_instance();
|
2004-11-28 23:54:11 +00:00
|
|
|
|
2007-03-24 00:58:43 +00:00
|
|
|
/// Calculate the SQL sortkey to be used by the SQL statements later
|
2008-06-08 10:43:39 +00:00
|
|
|
switch ( $sortkey ) {
|
|
|
|
case "CREATION":
|
2007-03-24 00:58:43 +00:00
|
|
|
$sqlsortkey = "timecreated";
|
|
|
|
break;
|
2008-06-08 10:43:39 +00:00
|
|
|
case "UPDATE":
|
2007-03-24 00:58:43 +00:00
|
|
|
$sqlsortkey = "timemodified";
|
|
|
|
break;
|
2008-06-08 10:43:39 +00:00
|
|
|
case "FIRSTNAME":
|
2007-03-24 00:58:43 +00:00
|
|
|
$sqlsortkey = "firstname";
|
|
|
|
break;
|
2008-06-08 10:43:39 +00:00
|
|
|
case "LASTNAME":
|
2007-03-24 00:58:43 +00:00
|
|
|
$sqlsortkey = "lastname";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$sqlsortorder = $sortorder;
|
|
|
|
|
2003-11-18 13:49:31 +00:00
|
|
|
/// Pivot is the field that set the break by groups (category, initial, author name, etc)
|
|
|
|
|
|
|
|
/// fullpivot indicate if the whole pivot should be compared agasint the db or just the first letter
|
|
|
|
/// printpivot indicate if the pivot should be printed or not
|
2004-02-21 13:21:35 +00:00
|
|
|
|
2003-11-18 13:49:31 +00:00
|
|
|
$fullpivot = 1;
|
2008-06-08 10:43:39 +00:00
|
|
|
$params = array('gid1'=>$glossary->id, 'gid2'=>$glossary->id, 'myid'=>$USER->id, 'hook'=>$hook);
|
2003-11-18 13:49:31 +00:00
|
|
|
|
|
|
|
$userid = '';
|
2006-09-02 11:44:04 +00:00
|
|
|
if ( !empty($USER->id) ) {
|
2008-06-08 10:43:39 +00:00
|
|
|
$userid = "OR ge.userid = :myid";
|
2003-11-18 13:49:31 +00:00
|
|
|
}
|
|
|
|
switch ($tab) {
|
|
|
|
case GLOSSARY_CATEGORY_VIEW:
|
2008-06-08 10:43:39 +00:00
|
|
|
if ($hook == GLOSSARY_SHOW_ALL_CATEGORIES ) {
|
2003-11-18 13:49:31 +00:00
|
|
|
|
2006-10-30 19:02:58 +00:00
|
|
|
$sqlselect = "SELECT gec.id AS cid, ge.*, gec.entryid, gc.name AS glossarypivot";
|
2008-06-08 10:43:39 +00:00
|
|
|
$sqlfrom = "FROM {glossary_entries} ge,
|
|
|
|
{glossary_entries_categories} gec,
|
|
|
|
{glossary_categories} gc";
|
|
|
|
$sqlwhere = "WHERE (ge.glossaryid = :gid1 OR ge.sourceglossaryid = :gid2) AND
|
2003-11-18 13:49:31 +00:00
|
|
|
ge.id = gec.entryid AND gc.id = gec.categoryid AND
|
2008-06-08 10:43:39 +00:00
|
|
|
(ge.approved <> 0 $userid)";
|
2003-11-18 13:49:31 +00:00
|
|
|
|
2004-07-27 17:19:47 +00:00
|
|
|
$sqlorderby = ' ORDER BY gc.name, ge.concept';
|
2003-11-18 13:49:31 +00:00
|
|
|
|
2008-06-08 10:43:39 +00:00
|
|
|
} elseif ($hook == GLOSSARY_SHOW_NOT_CATEGORISED ) {
|
2003-11-18 13:49:31 +00:00
|
|
|
|
|
|
|
$printpivot = 0;
|
2006-10-30 17:46:37 +00:00
|
|
|
$sqlselect = "SELECT ge.*, concept AS glossarypivot";
|
2008-06-08 10:43:39 +00:00
|
|
|
$sqlfrom = "FROM {glossary_entries} ge LEFT JOIN {glossary_entries_categories} gec
|
|
|
|
ON ge.id = gec.entryid";
|
|
|
|
$sqlwhere = "WHERE (glossaryid = :gid1 OR sourceglossaryid = :gid2) AND
|
|
|
|
(ge.approved <> 0 $userid) AND gec.entryid IS NULL";
|
2003-11-18 13:49:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
$sqlorderby = ' ORDER BY concept';
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$printpivot = 0;
|
2006-10-30 17:46:37 +00:00
|
|
|
$sqlselect = "SELECT ge.*, ce.entryid, c.name AS glossarypivot";
|
2008-06-08 10:43:39 +00:00
|
|
|
$sqlfrom = "FROM {glossary_entries} ge, {glossary_entries_categories} ce, {glossary_categories} c";
|
|
|
|
$sqlwhere = "WHERE ge.id = ce.entryid AND ce.categoryid = :hook AND
|
2003-11-18 13:49:31 +00:00
|
|
|
ce.categoryid = c.id AND ge.approved != 0 AND
|
2008-06-08 10:43:39 +00:00
|
|
|
(ge.glossaryid = :gid1 OR ge.sourceglossaryid = :gid2) AND
|
|
|
|
(ge.approved <> 0 $userid)";
|
2003-11-18 13:49:31 +00:00
|
|
|
|
|
|
|
$sqlorderby = ' ORDER BY c.name, ge.concept';
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GLOSSARY_AUTHOR_VIEW:
|
|
|
|
|
|
|
|
$where = '';
|
2008-06-08 10:43:39 +00:00
|
|
|
$params['hookup'] = $textlib->strtoupper($hook);
|
2006-10-30 18:34:21 +00:00
|
|
|
|
2007-03-24 00:58:43 +00:00
|
|
|
if ( $sqlsortkey == 'firstname' ) {
|
2008-06-08 10:43:39 +00:00
|
|
|
$usernamefield = $DB->sql_fullname('u.firstname' , 'u.lastname');
|
2006-10-30 18:34:21 +00:00
|
|
|
} else {
|
2008-06-08 10:43:39 +00:00
|
|
|
$usernamefield = $DB->sql_fullname('u.lastname' , 'u.firstname');
|
2003-11-18 13:49:31 +00:00
|
|
|
}
|
2008-06-08 10:43:39 +00:00
|
|
|
$where = "AND " . $DB->sql_substr() . "(upper($usernamefield),1," . $textlib->strlen($hook) . ") = :hookup";
|
2006-10-30 18:34:21 +00:00
|
|
|
|
2003-11-18 13:49:31 +00:00
|
|
|
if ( $hook == 'ALL' ) {
|
|
|
|
$where = '';
|
|
|
|
}
|
|
|
|
|
2006-10-30 19:32:44 +00:00
|
|
|
$sqlselect = "SELECT ge.*, $usernamefield AS glossarypivot, 1 AS userispivot ";
|
2008-06-08 10:43:39 +00:00
|
|
|
$sqlfrom = "FROM {glossary_entries} ge, {user} u";
|
2003-11-18 13:49:31 +00:00
|
|
|
$sqlwhere = "WHERE ge.userid = u.id AND
|
2008-06-08 10:43:39 +00:00
|
|
|
(ge.approved <> 0 $userid)
|
|
|
|
$where AND
|
|
|
|
(ge.glossaryid = :gid1 OR ge.sourceglossaryid = :gid2)";
|
2003-11-21 02:50:08 +00:00
|
|
|
$sqlorderby = "ORDER BY $usernamefield $sqlsortorder, ge.concept";
|
2003-11-18 13:49:31 +00:00
|
|
|
break;
|
|
|
|
case GLOSSARY_APPROVAL_VIEW:
|
|
|
|
$fullpivot = 0;
|
|
|
|
$printpivot = 0;
|
|
|
|
|
|
|
|
$where = '';
|
2008-06-08 10:43:39 +00:00
|
|
|
$params['hookup'] = $textlib->strtoupper($hook);
|
|
|
|
|
2003-11-18 13:49:31 +00:00
|
|
|
if ($hook != 'ALL' and $hook != 'SPECIAL') {
|
2008-06-08 10:43:39 +00:00
|
|
|
$where = 'AND ' . $DB->sql_substr() . '(upper(concept),1,' . $textlib->strlen($hook) . ') = :hookup';
|
2003-11-18 13:49:31 +00:00
|
|
|
}
|
|
|
|
|
2006-10-30 17:46:37 +00:00
|
|
|
$sqlselect = "SELECT ge.*, ge.concept AS glossarypivot";
|
2008-06-08 10:43:39 +00:00
|
|
|
$sqlfrom = "FROM {glossary_entries} ge";
|
|
|
|
$sqlwhere = "WHERE (ge.glossaryid = :gid1 OR ge.sourceglossaryid = :gid2) AND
|
2003-11-18 13:49:31 +00:00
|
|
|
ge.approved = 0 $where";
|
2008-06-08 10:43:39 +00:00
|
|
|
|
2003-11-21 02:50:08 +00:00
|
|
|
if ( $sqlsortkey ) {
|
|
|
|
$sqlorderby = "ORDER BY $sqlsortkey $sqlsortorder";
|
2003-11-18 13:49:31 +00:00
|
|
|
} else {
|
|
|
|
$sqlorderby = "ORDER BY ge.concept";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GLOSSARY_DATE_VIEW:
|
2006-04-09 11:06:19 +00:00
|
|
|
$printpivot = 0;
|
2003-11-18 13:49:31 +00:00
|
|
|
case GLOSSARY_STANDARD_VIEW:
|
|
|
|
default:
|
2006-10-30 17:46:37 +00:00
|
|
|
$sqlselect = "SELECT ge.*, ge.concept AS glossarypivot";
|
2008-06-08 10:43:39 +00:00
|
|
|
$sqlfrom = "FROM {glossary_entries} ge";
|
2003-11-18 13:49:31 +00:00
|
|
|
|
|
|
|
$where = '';
|
|
|
|
$fullpivot = 0;
|
|
|
|
|
|
|
|
switch ( $mode ) {
|
2008-06-08 10:43:39 +00:00
|
|
|
case 'search':
|
2005-04-19 00:14:36 +00:00
|
|
|
|
2008-06-08 10:43:39 +00:00
|
|
|
if ($DB->sql_regex_supported()) {
|
|
|
|
$REGEXP = $DB->sql_regex(true);
|
|
|
|
$NOTREGEXP = $DB->sql_regex(false);
|
2005-04-19 00:14:36 +00:00
|
|
|
}
|
2008-06-08 10:43:39 +00:00
|
|
|
$LIKE = $DB->sql_ilike(); // case-insensitive
|
|
|
|
|
|
|
|
$searchcond = array();
|
|
|
|
$alcond = array();
|
|
|
|
$params = array();
|
|
|
|
$i = 0;
|
2005-04-19 00:14:36 +00:00
|
|
|
|
2008-06-08 10:43:39 +00:00
|
|
|
$concat = $DB->sql_concat('ge.concept', "' '", 'ge.definition',"' '", "COALESCE(al.alial, '')");
|
2005-04-19 00:14:36 +00:00
|
|
|
|
|
|
|
$searchterms = explode(" ",$hook);
|
|
|
|
|
|
|
|
foreach ($searchterms as $searchterm) {
|
2008-06-08 10:43:39 +00:00
|
|
|
$i++;
|
2005-11-06 20:06:05 +00:00
|
|
|
|
2008-06-08 10:43:39 +00:00
|
|
|
$NOT = ''; /// Initially we aren't going to perform NOT LIKE searches, only MSSQL and Oracle
|
|
|
|
/// will use it to simulate the "-" operator with LIKE clause
|
2006-10-31 20:34:12 +00:00
|
|
|
|
|
|
|
/// Under Oracle and MSSQL, trim the + and - operators and perform
|
2008-06-08 10:43:39 +00:00
|
|
|
/// simpler LIKE (or NOT LIKE) queries
|
|
|
|
if (!$DB->sql_regex_supported()) {
|
|
|
|
if (substr($searchterm, 0, 1) == '-') {
|
|
|
|
$NOT = ' NOT ';
|
|
|
|
}
|
2006-10-31 20:34:12 +00:00
|
|
|
$searchterm = trim($searchterm, '+-');
|
|
|
|
}
|
|
|
|
|
2008-06-08 10:43:39 +00:00
|
|
|
if (substr($searchterm,0,1) == '+') {
|
|
|
|
$searchterm = trim($searchterm, '+-');
|
|
|
|
if ($textlib->strlen($searchterm) < 2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$searchterm = preg_quote($searchterm, '|');
|
|
|
|
$searchcond[] = "$concat $REGEXP :ss$i";
|
|
|
|
$params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)";
|
|
|
|
|
2005-04-19 00:14:36 +00:00
|
|
|
} else if (substr($searchterm,0,1) == "-") {
|
2008-06-08 10:43:39 +00:00
|
|
|
$searchterm = trim($searchterm, '+-');
|
|
|
|
if ($textlib->strlen($searchterm) < 2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$searchterm = preg_quote($searchterm, '|');
|
|
|
|
$searchcond[] = "$concat $NOTREGEXP :ss$i";
|
|
|
|
$params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)";
|
|
|
|
|
2005-04-19 00:14:36 +00:00
|
|
|
} else {
|
2008-06-08 10:43:39 +00:00
|
|
|
if ($textlib->strlen($searchterm) < 2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$searchcond[] = "$concat $NOT $LIKE :ss$i";
|
|
|
|
$params['ss'.$i] = "%$searchterm%";
|
2004-12-11 00:54:37 +00:00
|
|
|
}
|
|
|
|
}
|
2005-04-19 00:14:36 +00:00
|
|
|
|
2008-06-08 10:43:39 +00:00
|
|
|
if (empty($searchcond)) {
|
|
|
|
$where = " 1=2 "; // no search result
|
2005-04-19 00:14:36 +00:00
|
|
|
|
2003-11-18 13:49:31 +00:00
|
|
|
} else {
|
2008-06-08 10:43:39 +00:00
|
|
|
$searchcond = implode(" AND ", $searchcond);
|
|
|
|
|
|
|
|
$sqlselect = "SELECT DISTINCT ge.*, ge.concept AS glossarypivot";
|
|
|
|
$sqlfrom = "FROM {glossary_entries} ge
|
|
|
|
LEFT JOIN {glossary_alias} al ON al.entryid = ge.id";
|
|
|
|
$where = "AND ($searchcond)";
|
2003-11-18 13:49:31 +00:00
|
|
|
}
|
2005-04-19 00:14:36 +00:00
|
|
|
|
2003-11-18 13:49:31 +00:00
|
|
|
break;
|
2008-06-08 10:43:39 +00:00
|
|
|
|
|
|
|
case 'term':
|
|
|
|
$params['hook2'] = $hook;
|
2003-11-18 13:49:31 +00:00
|
|
|
$printpivot = 0;
|
2008-06-08 10:43:39 +00:00
|
|
|
$sqlfrom .= " LEFT JOIN {glossary_alias} ga on ge.id = ga.entryid";
|
|
|
|
$where = "AND (ge.concept = :hook OR ga.alias = :hook2) ";
|
2003-11-18 13:49:31 +00:00
|
|
|
break;
|
|
|
|
|
2008-06-08 10:43:39 +00:00
|
|
|
case 'entry':
|
2003-11-18 13:49:31 +00:00
|
|
|
$printpivot = 0;
|
2008-06-08 10:43:39 +00:00
|
|
|
$where = "AND ge.id = :hook";
|
2003-11-18 13:49:31 +00:00
|
|
|
break;
|
|
|
|
|
2008-06-08 10:43:39 +00:00
|
|
|
case 'letter':
|
2003-11-18 13:49:31 +00:00
|
|
|
if ($hook != 'ALL' and $hook != 'SPECIAL') {
|
2008-06-08 10:43:39 +00:00
|
|
|
$params['hookup'] = $textlib->strtoupper($hook);
|
|
|
|
$where = 'AND ' . $DB->sql_substr() . '(upper(concept),1,' . $textlib->strlen($hook) . ') = :hookup';
|
2003-11-18 13:49:31 +00:00
|
|
|
}
|
2004-09-09 18:31:50 +00:00
|
|
|
if ($hook == 'SPECIAL') {
|
|
|
|
//Create appropiate IN contents
|
|
|
|
$alphabet = explode(",", get_string("alphabet"));
|
2008-06-08 10:43:39 +00:00
|
|
|
list($nia, $aparams) = $DB->get_in_or_equal($alphabet, SQL_PARAMS_NAMED, $start='a0', false);
|
|
|
|
$params = array_merge($params, $aparams);
|
|
|
|
$where = 'AND ' . $DB->sql_substr() . "(upper(concept),1,1) $nia";
|
2004-09-09 18:31:50 +00:00
|
|
|
}
|
2003-11-18 13:49:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-06-08 10:43:39 +00:00
|
|
|
|
|
|
|
$sqlwhere = "WHERE (ge.glossaryid = :gid1 or ge.sourceglossaryid = :gid2) AND
|
|
|
|
(ge.approved <> 0 $userid)
|
2003-11-18 13:49:31 +00:00
|
|
|
$where";
|
|
|
|
switch ( $tab ) {
|
2008-06-08 10:43:39 +00:00
|
|
|
case GLOSSARY_DATE_VIEW:
|
2003-11-21 02:50:08 +00:00
|
|
|
$sqlorderby = "ORDER BY $sqlsortkey $sqlsortorder";
|
2003-11-18 13:49:31 +00:00
|
|
|
break;
|
2008-06-08 10:43:39 +00:00
|
|
|
|
|
|
|
case GLOSSARY_STANDARD_VIEW:
|
2003-11-18 13:49:31 +00:00
|
|
|
$sqlorderby = "ORDER BY ge.concept";
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2008-06-08 10:43:39 +00:00
|
|
|
}
|
|
|
|
$count = $DB->count_records_sql("SELECT COUNT(DISTINCT(ge.id)) $sqlfrom $sqlwhere", $params);
|
2004-05-12 18:44:15 +00:00
|
|
|
|
2006-10-24 22:00:29 +00:00
|
|
|
$limitfrom = $offset;
|
|
|
|
$limitnum = 0;
|
2008-06-08 10:43:39 +00:00
|
|
|
|
2003-11-18 13:49:31 +00:00
|
|
|
if ( $offset >= 0 ) {
|
2006-10-24 22:00:29 +00:00
|
|
|
$limitnum = $entriesbypage;
|
2003-11-18 13:49:31 +00:00
|
|
|
}
|
2006-10-30 19:32:44 +00:00
|
|
|
|
2008-06-08 10:43:39 +00:00
|
|
|
$allentries = $DB->get_records_sql("$sqlselect $sqlfrom $sqlwhere $sqlorderby", $params, $limitfrom, $limitnum);
|
2006-10-24 22:00:29 +00:00
|
|
|
?>
|