moodle/mod/glossary/sql.php

271 lines
9.7 KiB
PHP
Raw Normal View History

<?php
/**
* SQL.PHP
* This file is include from view.php and print.php
2008-06-08 10:43:39 +00:00
* @copyright 2003
**/
/// Creating the SQL statements
/// Initialise some variables
$sqlorderby = '';
$sqlsortkey = NULL;
$textlib = textlib_get_instance();
/// Calculate the SQL sortkey to be used by the SQL statements later
2008-06-08 10:43:39 +00:00
switch ( $sortkey ) {
case "CREATION":
$sqlsortkey = "timecreated";
break;
2008-06-08 10:43:39 +00:00
case "UPDATE":
$sqlsortkey = "timemodified";
break;
2008-06-08 10:43:39 +00:00
case "FIRSTNAME":
$sqlsortkey = "firstname";
break;
2008-06-08 10:43:39 +00:00
case "LASTNAME":
$sqlsortkey = "lastname";
break;
}
$sqlsortorder = $sortorder;
/// 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
$fullpivot = 1;
2008-06-08 10:43:39 +00:00
$params = array('gid1'=>$glossary->id, 'gid2'=>$glossary->id, 'myid'=>$USER->id, 'hook'=>$hook);
$userid = '';
if ( isloggedin() ) {
2008-06-08 10:43:39 +00:00
$userid = "OR ge.userid = :myid";
}
switch ($tab) {
case GLOSSARY_CATEGORY_VIEW:
2008-06-08 10:43:39 +00:00
if ($hook == GLOSSARY_SHOW_ALL_CATEGORIES ) {
$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
ge.id = gec.entryid AND gc.id = gec.categoryid AND
2008-06-08 10:43:39 +00:00
(ge.approved <> 0 $userid)";
$sqlorderby = ' ORDER BY gc.name, ge.concept';
2008-06-08 10:43:39 +00:00
} elseif ($hook == GLOSSARY_SHOW_NOT_CATEGORISED ) {
$printpivot = 0;
$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";
$sqlorderby = ' ORDER BY concept';
} else {
$printpivot = 0;
$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
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)";
$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);
if ( $sqlsortkey == 'firstname' ) {
2008-06-08 10:43:39 +00:00
$usernamefield = $DB->sql_fullname('u.firstname' , 'u.lastname');
} else {
2008-06-08 10:43:39 +00:00
$usernamefield = $DB->sql_fullname('u.lastname' , 'u.firstname');
}
$where = "AND " . $DB->sql_substr("upper($usernamefield)", 1, $textlib->strlen($hook)) . " = :hookup";
if ( $hook == 'ALL' ) {
$where = '';
}
$sqlselect = "SELECT ge.*, $usernamefield AS glossarypivot, 1 AS userispivot ";
2008-06-08 10:43:39 +00:00
$sqlfrom = "FROM {glossary_entries} ge, {user} u";
$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)";
$sqlorderby = "ORDER BY $usernamefield $sqlsortorder, ge.concept";
break;
case GLOSSARY_APPROVAL_VIEW:
$fullpivot = 0;
$printpivot = 0;
$where = '';
2008-06-08 10:43:39 +00:00
$params['hookup'] = $textlib->strtoupper($hook);
if ($hook != 'ALL' and $hook != 'SPECIAL') {
$where = "AND " . $DB->sql_substr("upper(concept)", 1, $textlib->strlen($hook)) . " = :hookup";
}
$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
ge.approved = 0 $where";
2008-06-08 10:43:39 +00:00
if ( $sqlsortkey ) {
$sqlorderby = "ORDER BY $sqlsortkey $sqlsortorder";
} else {
$sqlorderby = "ORDER BY ge.concept";
}
break;
case GLOSSARY_DATE_VIEW:
$printpivot = 0;
case GLOSSARY_STANDARD_VIEW:
default:
$sqlselect = "SELECT ge.*, ge.concept AS glossarypivot";
2008-06-08 10:43:39 +00:00
$sqlfrom = "FROM {glossary_entries} ge";
$where = '';
$fullpivot = 0;
switch ( $mode ) {
2008-06-08 10:43:39 +00:00
case 'search':
2008-06-08 10:43:39 +00:00
if ($DB->sql_regex_supported()) {
$REGEXP = $DB->sql_regex(true);
$NOTREGEXP = $DB->sql_regex(false);
}
2008-06-08 10:43:39 +00:00
$searchcond = array();
$alcond = array();
//$params = array();
2008-06-08 10:43:39 +00:00
$i = 0;
$concat = $DB->sql_concat('ge.concept', "' '", 'ge.definition',"' '", "COALESCE(al.alias, '')");
$searchterms = explode(" ",$hook);
foreach ($searchterms as $searchterm) {
2008-06-08 10:43:39 +00:00
$i++;
$NOT = false; /// Initially we aren't going to perform NOT LIKE searches, only MSSQL and Oracle
2008-06-08 10:43:39 +00:00
/// will use it to simulate the "-" operator with LIKE clause
/// 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 = true;
2008-06-08 10:43:39 +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]|$)";
} 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]|$)";
} else {
2008-06-08 10:43:39 +00:00
if ($textlib->strlen($searchterm) < 2) {
continue;
}
if ($NOT) {
$searchcond[] = "$concat NOT LIKE :ss$i"; //TODO: MDL-24080
} else {
$searchcond[] = $DB->sql_like($concat, ":ss$i", false);
}
2008-06-08 10:43:39 +00:00
$params['ss'.$i] = "%$searchterm%";
}
}
2008-06-08 10:43:39 +00:00
if (empty($searchcond)) {
$where = "AND 1=2 "; // no search result
} 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)";
}
break;
2008-06-08 10:43:39 +00:00
case 'term':
$params['hook2'] = $hook;
$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) ";
break;
2008-06-08 10:43:39 +00:00
case 'entry':
$printpivot = 0;
2008-06-08 10:43:39 +00:00
$where = "AND ge.id = :hook";
break;
2008-06-08 10:43:39 +00:00
case 'letter':
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";
}
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";
}
break;
}
2008-06-08 10:43:39 +00:00
$sqlwhere = "WHERE (ge.glossaryid = :gid1 or ge.sourceglossaryid = :gid2) AND
(ge.approved <> 0 $userid)
$where";
switch ( $tab ) {
2008-06-08 10:43:39 +00:00
case GLOSSARY_DATE_VIEW:
$sqlorderby = "ORDER BY $sqlsortkey $sqlsortorder";
break;
2008-06-08 10:43:39 +00:00
case GLOSSARY_STANDARD_VIEW:
$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);
$limitfrom = $offset;
$limitnum = 0;
2008-06-08 10:43:39 +00:00
if ( $offset >= 0 ) {
$limitnum = $entriesbypage;
}
2008-06-08 10:43:39 +00:00
$allentries = $DB->get_records_sql("$sqlselect $sqlfrom $sqlwhere $sqlorderby", $params, $limitfrom, $limitnum);