Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-32126 - data - backport of advanced search re-write

  • Loading branch information...
commit d4d88ffd73803a79241dd21608ea6cea1ac8dd41 1 parent efac2d2
@abgreeve abgreeve authored
Showing with 185 additions and 13 deletions.
  1. +169 −0 mod/data/lib.php
  2. +16 −13 mod/data/view.php
View
169 mod/data/lib.php
@@ -3467,3 +3467,172 @@ function data_user_can_delete_preset($context, $preset) {
return $candelete;
}
}
+
+/**
+ * Get all of the record ids from a database activity.
+ *
+ * @param int $dataid The dataid of the database module.
+ * @return array $idarray An array of record ids
+ */
+function data_get_all_recordids($dataid) {
+ global $DB;
+ $initsql = 'SELECT c.recordid
+ FROM {data_fields} f,
+ {data_content} c
+ WHERE f.dataid = :dataid
+ AND f.id = c.fieldid
+ GROUP BY c.recordid';
+ $initrecord = $DB->get_recordset_sql($initsql, array('dataid' => $dataid));
+ $idarray = array();
+ foreach ($initrecord as $data) {
+ $idarray[] = $data->recordid;
+ }
+ // Close the record set and free up resources.
+ $initrecord->close();
+ return $idarray;
+}
+
+/**
+ * Get the ids of all the records that match that advanced search criteria
+ * This goes and loops through each criterion one at a time until it either
+ * runs out of records or returns a subset of records.
+ *
+ * @param array $recordids An array of record ids.
+ * @param array $searcharray Contains information for the advanced search criteria
+ * @param int $dataid The data id of the database.
+ * @return array $recordids An array of record ids.
+ */
+function data_get_advance_search_ids($recordids, $searcharray, $dataid) {
+ $searchcriteria = array_keys($searcharray);
+ // Loop through and reduce the IDs one search criteria at a time.
+ foreach ($searchcriteria as $key) {
+ $recordids = data_get_recordids($key, $searcharray, $dataid, $recordids);
+ // If we don't have anymore IDs then stop.
+ if (!$recordids) {
+ break;
+ }
+ }
+ return $recordids;
+}
+
+/**
+ * Gets the record IDs given the search criteria
+ *
+ * @param string $alias Record alias.
+ * @param array $searcharray Criteria for the search.
+ * @param int $dataid Data ID for the database
+ * @param array $recordids An array of record IDs.
+ * @return array $nestarray An arry of record IDs
+ */
+function data_get_recordids($alias, $searcharray, $dataid, $recordids) {
+ global $DB;
+
+ $nestsearch = $searcharray[$alias];
+ // searching for content outside of mdl_data_content
+ if ($alias < 0) {
+ $alias = '';
+ }
+ list($insql, $params) = $DB->get_in_or_equal($recordids, SQL_PARAMS_NAMED);
+ $nestselect = 'SELECT c' . $alias . '.recordid
+ FROM {data_content} c' . $alias . ',
+ {data_fields} f,
+ {data_records} r,
+ {user} u ';
+ $nestwhere = 'WHERE u.id = r.userid
+ AND f.id = c' . $alias . '.fieldid
+ AND r.id = c' . $alias . '.recordid
+ AND r.dataid = :dataid
+ AND c' . $alias .'.recordid ' . $insql . '
+ AND ';
+
+ $params['dataid'] = $dataid;
+ if (count($nestsearch->params) != 0) {
+ $params = array_merge($params, $nestsearch->params);
+ $nestsql = $nestselect . $nestwhere . $nestsearch->sql;
+ } else {
+ $thing = $DB->sql_like($nestsearch->field, ':search1', false);
+ $nestsql = $nestselect . $nestwhere . $thing . ' GROUP BY c' . $alias . '.recordid';
+ $params['search1'] = "%$nestsearch->data%";
+ }
+ $nestrecords = $DB->get_recordset_sql($nestsql, $params);
+ $nestarray = array();
+ foreach ($nestrecords as $data) {
+ $nestarray[] = $data->recordid;
+ }
+ // Close the record set and free up resources.
+ $nestrecords->close();
+ return $nestarray;
+}
+
+/**
+ * Returns an array with an sql string for advanced searches and the parameters that go with them.
+ *
+ * @param int $sort DATA_*
+ * @param stdClass $data Data module object
+ * @param array $recordids An array of record IDs.
+ * @param string $selectdata Information for the select part of the sql statement.
+ * @param string $sortorder Additional sort parameters
+ * @return array sqlselect sqlselect['sql] has the sql string, sqlselect['params'] contains an array of parameters.
+ */
+function data_get_advanced_search_sql($sort, $data, $recordids, $selectdata, $sortorder) {
+ global $DB;
+ if ($sort == 0) {
+ $nestselectsql = 'SELECT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname
+ FROM {data_content} c,
+ {data_records} r,
+ {user} u ';
+ $groupsql = ' GROUP BY r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname ';
+ } else {
+ // Sorting through 'Other' criteria
+ if ($sort <= 0) {
+ switch ($sort) {
+ case DATA_LASTNAME:
+ $sortcontentfull = "u.lastname";
+ break;
+ case DATA_FIRSTNAME:
+ $sortcontentfull = "u.firstname";
+ break;
+ case DATA_APPROVED:
+ $sortcontentfull = "r.approved";
+ break;
+ case DATA_TIMEMODIFIED:
+ $sortcontentfull = "r.timemodified";
+ break;
+ case DATA_TIMEADDED:
+ default:
+ $sortcontentfull = "r.timecreated";
+ }
+ } else {
+ $sortfield = data_get_field_from_id($sort, $data);
+ $sortcontent = $DB->sql_compare_text('c.' . $sortfield->get_sort_field());
+ $sortcontentfull = $sortfield->get_sort_sql($sortcontent);
+ }
+
+ $nestselectsql = 'SELECT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname, ' . $sortcontentfull . '
+ AS _order
+ FROM {data_content} c,
+ {data_records} r,
+ {user} u ';
+ $groupsql = ' GROUP BY r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname, ' .$sortcontentfull;
+ }
+ $nestfromsql = 'WHERE c.recordid = r.id
+ AND r.dataid = :dataid
+ AND r.userid = u.id';
+
+ // Find the field we are sorting on
+ if ($sort > 0 or data_get_field_from_id($sort, $data)) {
+ $nestfromsql .= ' AND c.fieldid = :sort';
+ }
+
+ // If there are no record IDs then return an sql statment that will return no rows.
+ if (count($recordids) != 0) {
+ list($insql, $inparam) = $DB->get_in_or_equal($recordids, SQL_PARAMS_NAMED);
+ } else {
+ list($insql, $inparam) = $DB->get_in_or_equal(array('-1'), SQL_PARAMS_NAMED);
+ }
+ $nestfromsql .= ' AND c.recordid ' . $insql . $groupsql;
+ $nestfromsql = "$nestfromsql $selectdata";
+ $sqlselect['sql'] = "$nestselectsql $nestfromsql $sortorder";
+ $sqlselect['params'] = $inparam;
+ return $sqlselect;
+}
View
29 mod/data/view.php
@@ -561,6 +561,7 @@
$params['myid2'] = $USER->id;
}
+ $i = 0;
if (!empty($advanced)) { //If advanced box is checked.
foreach($search_array as $key => $val) { //what does $search_array hold?
if ($key == DATA_FIRSTNAME or $key == DATA_LASTNAME) {
@@ -587,30 +588,32 @@
/// To actually fetch the records
$fromsql = "FROM $tables $advtables $where $advwhere $groupselect $approveselect $searchselect $advsearchselect";
- $sqlselect = "SELECT $what $fromsql $sortorder";
- $sqlcount = "SELECT $count $fromsql"; // Total number of records when searching
- $sqlmax = "SELECT $count FROM $tables $where $groupselect $approveselect"; // number of all recoirds user may see
$allparams = array_merge($params, $advparams);
- /// Work out the paging numbers and counts
+ $recordids = data_get_all_recordids($data->id);
+ $newrecordids = data_get_advance_search_ids($recordids, $search_array, $data->id);
+ $totalcount = count($newrecordids);
+ $selectdata = $groupselect . $approveselect;
- $totalcount = $DB->count_records_sql($sqlcount, $allparams);
+ if (!empty($advanced)) {
+ $advancedsearchsql = data_get_advanced_search_sql($sort, $data, $newrecordids, $selectdata, $sortorder);
+ $sqlselect = $advancedsearchsql['sql'];
+ $allparams = array_merge($allparams, $advancedsearchsql['params']);
+ } else {
+ $sqlselect = "SELECT $what $fromsql $sortorder";
+ }
+
+ /// Work out the paging numbers and counts
if (empty($searchselect) && empty($advsearchselect)) {
$maxcount = $totalcount;
} else {
- $maxcount = $DB->count_records_sql($sqlmax, $params);
+ $maxcount = count($recordids);
}
if ($record) { // We need to just show one, so where is it in context?
$nowperpage = 1;
$mode = 'single';
-
- $page = 0;
- // TODO: Improve this because we are executing $sqlselect twice (here and some lines below)!
- if ($allrecordids = $DB->get_fieldset_sql($sqlselect, $allparams)) {
- $page = (int)array_search($record->id, $allrecordids);
- unset($allrecordids);
- }
+ $page = (int)array_search($record->id, $recordids);
} else if ($mode == 'single') { // We rely on ambient $page settings
$nowperpage = 1;
Please sign in to comment.
Something went wrong with that request. Please try again.