PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/glossary/sql.php

https://github.com/kpike/moodle
PHP | 277 lines | 206 code | 53 blank | 18 comment | 41 complexity | d73dbcef757f743accf2f2478a6bc0ef MD5 | raw file
  1. <?php
  2. /**
  3. * SQL.PHP
  4. * This file is include from view.php and print.php
  5. * @copyright 2003
  6. **/
  7. /// Creating the SQL statements
  8. /// Initialise some variables
  9. $sqlorderby = '';
  10. $sqlsortkey = NULL;
  11. $textlib = textlib_get_instance();
  12. // For cases needing inner view
  13. $sqlwrapheader = '';
  14. $sqlwrapfooter = '';
  15. /// Calculate the SQL sortkey to be used by the SQL statements later
  16. switch ( $sortkey ) {
  17. case "CREATION":
  18. $sqlsortkey = "timecreated";
  19. break;
  20. case "UPDATE":
  21. $sqlsortkey = "timemodified";
  22. break;
  23. case "FIRSTNAME":
  24. $sqlsortkey = "firstname";
  25. break;
  26. case "LASTNAME":
  27. $sqlsortkey = "lastname";
  28. break;
  29. }
  30. $sqlsortorder = $sortorder;
  31. /// Pivot is the field that set the break by groups (category, initial, author name, etc)
  32. /// fullpivot indicate if the whole pivot should be compared agasint the db or just the first letter
  33. /// printpivot indicate if the pivot should be printed or not
  34. $fullpivot = 1;
  35. $params = array('gid1'=>$glossary->id, 'gid2'=>$glossary->id, 'myid'=>$USER->id, 'hook'=>$hook);
  36. $userid = '';
  37. if ( isloggedin() ) {
  38. $userid = "OR ge.userid = :myid";
  39. }
  40. switch ($tab) {
  41. case GLOSSARY_CATEGORY_VIEW:
  42. if ($hook == GLOSSARY_SHOW_ALL_CATEGORIES ) {
  43. $sqlselect = "SELECT gec.id AS cid, ge.*, gec.entryid, gc.name AS glossarypivot";
  44. $sqlfrom = "FROM {glossary_entries} ge,
  45. {glossary_entries_categories} gec,
  46. {glossary_categories} gc";
  47. $sqlwhere = "WHERE (ge.glossaryid = :gid1 OR ge.sourceglossaryid = :gid2) AND
  48. ge.id = gec.entryid AND gc.id = gec.categoryid AND
  49. (ge.approved <> 0 $userid)";
  50. $sqlorderby = ' ORDER BY gc.name, ge.concept';
  51. } elseif ($hook == GLOSSARY_SHOW_NOT_CATEGORISED ) {
  52. $printpivot = 0;
  53. $sqlselect = "SELECT ge.*, concept AS glossarypivot";
  54. $sqlfrom = "FROM {glossary_entries} ge LEFT JOIN {glossary_entries_categories} gec
  55. ON ge.id = gec.entryid";
  56. $sqlwhere = "WHERE (glossaryid = :gid1 OR sourceglossaryid = :gid2) AND
  57. (ge.approved <> 0 $userid) AND gec.entryid IS NULL";
  58. $sqlorderby = ' ORDER BY concept';
  59. } else {
  60. $printpivot = 0;
  61. $sqlselect = "SELECT ge.*, ce.entryid, c.name AS glossarypivot";
  62. $sqlfrom = "FROM {glossary_entries} ge, {glossary_entries_categories} ce, {glossary_categories} c";
  63. $sqlwhere = "WHERE ge.id = ce.entryid AND ce.categoryid = :hook AND
  64. ce.categoryid = c.id AND ge.approved != 0 AND
  65. (ge.glossaryid = :gid1 OR ge.sourceglossaryid = :gid2) AND
  66. (ge.approved <> 0 $userid)";
  67. $sqlorderby = ' ORDER BY c.name, ge.concept';
  68. }
  69. break;
  70. case GLOSSARY_AUTHOR_VIEW:
  71. $where = '';
  72. $params['hookup'] = $textlib->strtoupper($hook);
  73. if ( $sqlsortkey == 'firstname' ) {
  74. $usernamefield = $DB->sql_fullname('u.firstname' , 'u.lastname');
  75. } else {
  76. $usernamefield = $DB->sql_fullname('u.lastname' , 'u.firstname');
  77. }
  78. $where = "AND " . $DB->sql_substr("upper($usernamefield)", 1, $textlib->strlen($hook)) . " = :hookup";
  79. if ( $hook == 'ALL' ) {
  80. $where = '';
  81. }
  82. $sqlselect = "SELECT ge.*, $usernamefield AS glossarypivot, 1 AS userispivot ";
  83. $sqlfrom = "FROM {glossary_entries} ge, {user} u";
  84. $sqlwhere = "WHERE ge.userid = u.id AND
  85. (ge.approved <> 0 $userid)
  86. $where AND
  87. (ge.glossaryid = :gid1 OR ge.sourceglossaryid = :gid2)";
  88. $sqlorderby = "ORDER BY $usernamefield $sqlsortorder, ge.concept";
  89. break;
  90. case GLOSSARY_APPROVAL_VIEW:
  91. $fullpivot = 0;
  92. $printpivot = 0;
  93. $where = '';
  94. $params['hookup'] = $textlib->strtoupper($hook);
  95. if ($hook != 'ALL' and $hook != 'SPECIAL') {
  96. $where = "AND " . $DB->sql_substr("upper(concept)", 1, $textlib->strlen($hook)) . " = :hookup";
  97. }
  98. $sqlselect = "SELECT ge.*, ge.concept AS glossarypivot";
  99. $sqlfrom = "FROM {glossary_entries} ge";
  100. $sqlwhere = "WHERE (ge.glossaryid = :gid1 OR ge.sourceglossaryid = :gid2) AND
  101. ge.approved = 0 $where";
  102. if ( $sqlsortkey ) {
  103. $sqlorderby = "ORDER BY $sqlsortkey $sqlsortorder";
  104. } else {
  105. $sqlorderby = "ORDER BY ge.concept";
  106. }
  107. break;
  108. case GLOSSARY_DATE_VIEW:
  109. $printpivot = 0;
  110. case GLOSSARY_STANDARD_VIEW:
  111. default:
  112. $sqlselect = "SELECT ge.*, ge.concept AS glossarypivot";
  113. $sqlfrom = "FROM {glossary_entries} ge";
  114. $where = '';
  115. $fullpivot = 0;
  116. switch ( $mode ) {
  117. case 'search':
  118. if ($DB->sql_regex_supported()) {
  119. $REGEXP = $DB->sql_regex(true);
  120. $NOTREGEXP = $DB->sql_regex(false);
  121. }
  122. $searchcond = array();
  123. $alcond = array();
  124. //$params = array();
  125. $i = 0;
  126. $concat = $DB->sql_concat('ge.concept', "' '", 'ge.definition',"' '", "COALESCE(al.alias, '')");
  127. $searchterms = explode(" ",$hook);
  128. foreach ($searchterms as $searchterm) {
  129. $i++;
  130. $NOT = false; /// Initially we aren't going to perform NOT LIKE searches, only MSSQL and Oracle
  131. /// will use it to simulate the "-" operator with LIKE clause
  132. /// Under Oracle and MSSQL, trim the + and - operators and perform
  133. /// simpler LIKE (or NOT LIKE) queries
  134. if (!$DB->sql_regex_supported()) {
  135. if (substr($searchterm, 0, 1) == '-') {
  136. $NOT = true;
  137. }
  138. $searchterm = trim($searchterm, '+-');
  139. }
  140. if (substr($searchterm,0,1) == '+') {
  141. $searchterm = trim($searchterm, '+-');
  142. if ($textlib->strlen($searchterm) < 2) {
  143. continue;
  144. }
  145. $searchterm = preg_quote($searchterm, '|');
  146. $searchcond[] = "$concat $REGEXP :ss$i";
  147. $params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)";
  148. } else if (substr($searchterm,0,1) == "-") {
  149. $searchterm = trim($searchterm, '+-');
  150. if ($textlib->strlen($searchterm) < 2) {
  151. continue;
  152. }
  153. $searchterm = preg_quote($searchterm, '|');
  154. $searchcond[] = "$concat $NOTREGEXP :ss$i";
  155. $params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)";
  156. } else {
  157. if ($textlib->strlen($searchterm) < 2) {
  158. continue;
  159. }
  160. $searchcond[] = $DB->sql_like($concat, ":ss$i", false, true, $NOT);
  161. $params['ss'.$i] = "%$searchterm%";
  162. }
  163. }
  164. if (empty($searchcond)) {
  165. $where = "AND 1=2 "; // no search result
  166. } else {
  167. $searchcond = implode(" AND ", $searchcond);
  168. // Need one inner view here to avoid distinct + text
  169. $sqlwrapheader = 'SELECT ge.*, ge.concept AS glossarypivot
  170. FROM {glossary_entries} ge
  171. JOIN ( ';
  172. $sqlwrapfooter = ' ) gei ON (ge.id = gei.id)';
  173. $sqlselect = "SELECT DISTINCT ge.id";
  174. $sqlfrom = "FROM {glossary_entries} ge
  175. LEFT JOIN {glossary_alias} al ON al.entryid = ge.id";
  176. $where = "AND ($searchcond)";
  177. }
  178. break;
  179. case 'term':
  180. $params['hook2'] = $hook;
  181. $printpivot = 0;
  182. $sqlfrom .= " LEFT JOIN {glossary_alias} ga on ge.id = ga.entryid";
  183. $where = "AND (ge.concept = :hook OR ga.alias = :hook2) ";
  184. break;
  185. case 'entry':
  186. $printpivot = 0;
  187. $where = "AND ge.id = :hook";
  188. break;
  189. case 'letter':
  190. if ($hook != 'ALL' and $hook != 'SPECIAL') {
  191. $params['hookup'] = $textlib->strtoupper($hook);
  192. $where = "AND " . $DB->sql_substr("upper(concept)", 1, $textlib->strlen($hook)) . " = :hookup";
  193. }
  194. if ($hook == 'SPECIAL') {
  195. //Create appropiate IN contents
  196. $alphabet = explode(",", get_string('alphabet', 'langconfig'));
  197. list($nia, $aparams) = $DB->get_in_or_equal($alphabet, SQL_PARAMS_NAMED, $start='a', false);
  198. $params = array_merge($params, $aparams);
  199. $where = "AND " . $DB->sql_substr("upper(concept)", 1, 1) . " $nia";
  200. }
  201. break;
  202. }
  203. $sqlwhere = "WHERE (ge.glossaryid = :gid1 or ge.sourceglossaryid = :gid2) AND
  204. (ge.approved <> 0 $userid)
  205. $where";
  206. switch ( $tab ) {
  207. case GLOSSARY_DATE_VIEW:
  208. $sqlorderby = "ORDER BY $sqlsortkey $sqlsortorder";
  209. break;
  210. case GLOSSARY_STANDARD_VIEW:
  211. $sqlorderby = "ORDER BY ge.concept";
  212. default:
  213. break;
  214. }
  215. break;
  216. }
  217. $count = $DB->count_records_sql("SELECT COUNT(DISTINCT(ge.id)) $sqlfrom $sqlwhere", $params);
  218. $limitfrom = $offset;
  219. $limitnum = 0;
  220. if ( $offset >= 0 ) {
  221. $limitnum = $entriesbypage;
  222. }
  223. $query = "$sqlwrapheader $sqlselect $sqlfrom $sqlwhere $sqlwrapfooter $sqlorderby";
  224. $allentries = $DB->get_records_sql($query, $params, $limitfrom, $limitnum);