Commit | Line | Data |
---|---|---|
3d4b223a | 1 | <?php // $Id$ |
2 | /////////////////////////////////////////////////////////////////////////// | |
3 | // // | |
4 | // NOTICE OF COPYRIGHT // | |
5 | // // | |
6 | // Moodle - Modular Object-Oriented Dynamic Learning Environment // | |
7 | // http://moodle.org // | |
8 | // // | |
9 | // Copyright (C) 2005 Martin Dougiamas http://dougiamas.com // | |
10 | // // | |
11 | // This program is free software; you can redistribute it and/or modify // | |
12 | // it under the terms of the GNU General Public License as published by // | |
13 | // the Free Software Foundation; either version 2 of the License, or // | |
14 | // (at your option) any later version. // | |
15 | // // | |
16 | // This program is distributed in the hope that it will be useful, // | |
17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // | |
18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // | |
19 | // GNU General Public License for more details: // | |
20 | // // | |
21 | // http://www.gnu.org/copyleft/gpl.html // | |
22 | // // | |
23 | /////////////////////////////////////////////////////////////////////////// | |
24 | ||
25 | require_once('../../config.php'); | |
26 | require_once('lib.php'); | |
27 | require_once($CFG->libdir.'/blocklib.php'); | |
a593aeee | 28 | require_once("$CFG->libdir/rsslib.php"); |
3d4b223a | 29 | |
3d4b223a | 30 | require_once('pagelib.php'); |
9f7f1a74 | 31 | |
3d45b8e5 | 32 | /// One of these is necessary! |
3d4b223a | 33 | $id = optional_param('id', 0, PARAM_INT); // course module id |
34 | $d = optional_param('d', 0, PARAM_INT); // database id | |
3d45b8e5 | 35 | $rid = optional_param('rid', 0, PARAM_INT); //record id |
36 | ||
37 | $mode = optional_param('mode', '', PARAM_ALPHA); // Force the browse mode ('single') | |
38 | ||
d2b23346 | 39 | $edit = optional_param('edit', -1, PARAM_BOOL); |
7900ecb0 | 40 | $page = optional_param('page', 0, PARAM_INT); |
3d45b8e5 | 41 | /// These can be added to perform an action on a record |
473dd288 | 42 | $approve = optional_param('approve', 0, PARAM_INT); //approval recordid |
43 | $delete = optional_param('delete', 0, PARAM_INT); //delete recordid | |
aab98aaf | 44 | |
3d4b223a | 45 | if ($id) { |
f9d5371b | 46 | if (! $cm = get_coursemodule_from_id('data', $id)) { |
714bec74 | 47 | error('Course Module ID was incorrect'); |
3d4b223a | 48 | } |
49 | if (! $course = get_record('course', 'id', $cm->course)) { | |
714bec74 | 50 | error('Course is misconfigured'); |
3d4b223a | 51 | } |
52 | if (! $data = get_record('data', 'id', $cm->instance)) { | |
714bec74 | 53 | error('Course module is incorrect'); |
3d4b223a | 54 | } |
3d45b8e5 | 55 | $record = NULL; |
3d4b223a | 56 | |
3d45b8e5 | 57 | } else if ($rid) { |
58 | if (! $record = get_record('data_records', 'id', $rid)) { | |
714bec74 | 59 | error('Record ID is incorrect'); |
3d45b8e5 | 60 | } |
61 | if (! $data = get_record('data', 'id', $record->dataid)) { | |
714bec74 | 62 | error('Data ID is incorrect'); |
3d45b8e5 | 63 | } |
64 | if (! $course = get_record('course', 'id', $data->course)) { | |
714bec74 | 65 | error('Course is misconfigured'); |
3d45b8e5 | 66 | } |
67 | if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) { | |
714bec74 | 68 | error('Course Module ID was incorrect'); |
3d45b8e5 | 69 | } |
70 | } else { // We must have $d | |
3d4b223a | 71 | if (! $data = get_record('data', 'id', $d)) { |
714bec74 | 72 | error('Data ID is incorrect'); |
3d4b223a | 73 | } |
74 | if (! $course = get_record('course', 'id', $data->course)) { | |
714bec74 | 75 | error('Course is misconfigured'); |
3d4b223a | 76 | } |
77 | if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) { | |
714bec74 | 78 | error('Course Module ID was incorrect'); |
3d4b223a | 79 | } |
3d45b8e5 | 80 | $record = NULL; |
3d4b223a | 81 | } |
82 | ||
7ddda9db | 83 | require_course_login($course, true, $cm); |
aab98aaf | 84 | |
dabfd0ed | 85 | $context = get_context_instance(CONTEXT_MODULE, $cm->id); |
20821a12 | 86 | require_capability('mod/data:viewentry', $context); |
3d1c33ef | 87 | |
3d45b8e5 | 88 | /// If we have an empty Database then redirect because this page is useless without data |
0468976c | 89 | if (has_capability('mod/data:managetemplates', $context)) { |
ed69c723 | 90 | if (!record_exists('data_fields','dataid',$data->id)) { // Brand new database! |
5bac6d10 | 91 | redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry |
3d4b223a | 92 | } |
93 | } | |
e0279f63 | 94 | |
3d45b8e5 | 95 | |
96 | /// Check further parameters that set browsing preferences | |
97 | if (!isset($SESSION->dataprefs)) { | |
98 | $SESSION->dataprefs = array(); | |
e0279f63 | 99 | } |
3d45b8e5 | 100 | if (!isset($SESSION->dataprefs[$data->id])) { |
101 | $SESSION->dataprefs[$data->id] = array(); | |
102 | $SESSION->dataprefs[$data->id]['search'] = ''; | |
7900ecb0 | 103 | $SESSION->dataprefs[$data->id]['search_array'] = array(); |
3d45b8e5 | 104 | $SESSION->dataprefs[$data->id]['sort'] = $data->defaultsort; |
7900ecb0 | 105 | $SESSION->dataprefs[$data->id]['advanced'] = 0; |
3d45b8e5 | 106 | $SESSION->dataprefs[$data->id]['order'] = ($data->defaultsortdir == 0) ? 'ASC' : 'DESC'; |
3d4b223a | 107 | } |
de8ff581 | 108 | |
109 | $advanced = optional_param('advanced', -1, PARAM_INT); | |
110 | if ($advanced == -1) { | |
111 | $advanced = $SESSION->dataprefs[$data->id]['advanced']; | |
112 | } else { | |
113 | if (!$advanced) { | |
114 | // explicitly switched to normal mode - discard all advanced search settings | |
115 | $SESSION->dataprefs[$data->id]['search_array'] = array(); | |
116 | } | |
832123e1 | 117 | $SESSION->dataprefs[$data->id]['advanced'] = $advanced; |
de8ff581 | 118 | } |
119 | ||
7900ecb0 | 120 | $search_array = $SESSION->dataprefs[$data->id]['search_array']; |
121 | ||
122 | if (!empty($advanced)) { | |
123 | $search = ''; | |
69c0a609 | 124 | $vals = array(); |
7900ecb0 | 125 | $fields = get_records('data_fields', 'dataid', $data->id); |
126 | ||
127 | //Added to ammend paging error. This error would occur when attempting to go from one page of advanced | |
128 | //search results to another. All fields were reset in the page transfer, and there was no way of determining | |
129 | //whether or not the user reset them. This would cause a blank search to execute whenever the user attempted | |
130 | //to see any page of results past the first. | |
131 | //This fix works as follows: | |
132 | //$paging flag is set to false when page 0 of the advanced search results is viewed for the first time. | |
133 | //Viewing any page of results after page 0 passes the false $paging flag though the URL (see line 523) and the | |
134 | //execution falls through to the second condition below, allowing paging to be set to true. | |
135 | //Paging remains true and keeps getting passed though the URL until a new search is performed | |
136 | //(even if page 0 is revisited). | |
137 | //A false $paging flag generates advanced search results based on the fields input by the user. | |
138 | //A true $paging flag generates davanced search results from the $SESSION global. | |
139 | //(See lines 147-158) | |
140 | ||
141 | $paging = optional_param('paging', NULL, PARAM_BOOL); | |
142 | if($page == 0 && !isset($paging)) { | |
143 | $paging = false; | |
144 | } | |
145 | else { | |
146 | $paging = true; | |
147 | } | |
714bec74 | 148 | if (!empty($fields)) { |
7900ecb0 | 149 | foreach($fields as $field) { |
150 | $searchfield = data_get_field_from_id($field->id, $data); | |
151 | //Get field data to build search sql with. If paging is false, get from user. | |
152 | //If paging is true, get data from $search_array which is obtained from the $SESSION (see line 116). | |
153 | if(!$paging) { | |
154 | $val = $searchfield->parse_search_field(); | |
714bec74 | 155 | } else { |
7900ecb0 | 156 | //Set value from session if there is a value @ the required index. |
714bec74 | 157 | if (isset($search_array[$field->id])) { |
7900ecb0 | 158 | $val = $search_array[$field->id]->data; |
714bec74 | 159 | } else { //If there is not an entry @ the required index, set value to blank. |
7900ecb0 | 160 | $val = ''; |
161 | } | |
7900ecb0 | 162 | } |
714bec74 | 163 | if (!empty($val)) { |
164 | $search_array[$field->id] = new object(); | |
165 | $search_array[$field->id]->sql = $searchfield->generate_sql('c'.$field->id, $val); | |
166 | $search_array[$field->id]->data = $val; | |
69c0a609 | 167 | $vals[] = $val; |
714bec74 | 168 | } else { |
169 | // clear it out | |
170 | unset($search_array[$field->id]); | |
7900ecb0 | 171 | } |
172 | } | |
173 | } | |
714bec74 | 174 | |
175 | if (!$paging) { | |
176 | // name searching | |
177 | $fn = optional_param('u_fn', '', PARAM_NOTAGS); | |
178 | $ln = optional_param('u_ln', '', PARAM_NOTAGS); | |
179 | } else { | |
de8ff581 | 180 | $fn = isset($search_array[DATA_FIRSTNAME]) ? $search_array[DATA_FIRSTNAME]->data : ''; |
181 | $ln = isset($search_array[DATA_LASTNAME]) ? $search_array[DATA_LASTNAME]->data : ''; | |
714bec74 | 182 | } |
183 | if (!empty($fn)) { | |
184 | $search_array[DATA_FIRSTNAME] = new object(); | |
185 | $search_array[DATA_FIRSTNAME]->sql = ''; | |
186 | $search_array[DATA_FIRSTNAME]->field = 'u.firstname'; | |
187 | $search_array[DATA_FIRSTNAME]->data = $fn; | |
69c0a609 | 188 | $vals[] = $fn; |
714bec74 | 189 | } else { |
190 | unset($search_array[DATA_FIRSTNAME]); | |
191 | } | |
192 | if (!empty($ln)) { | |
193 | $search_array[DATA_LASTNAME] = new object(); | |
194 | $search_array[DATA_LASTNAME]->sql = ''; | |
195 | $search_array[DATA_LASTNAME]->field = 'u.lastname'; | |
196 | $search_array[DATA_LASTNAME]->data = $ln; | |
69c0a609 | 197 | $vals[] = $ln; |
714bec74 | 198 | } else { |
199 | unset($search_array[DATA_LASTNAME]); | |
200 | } | |
201 | ||
7900ecb0 | 202 | $SESSION->dataprefs[$data->id]['search_array'] = $search_array; // Make it sticky |
714bec74 | 203 | |
69c0a609 | 204 | // in case we want to switch to simple search later - there might be multiple values there ;-) |
205 | if ($vals) { | |
206 | $val = reset($vals); | |
207 | if (is_string($val)) { | |
208 | $search = $val; | |
209 | } | |
210 | } | |
211 | ||
714bec74 | 212 | } else { |
7900ecb0 | 213 | $search = optional_param('search', $SESSION->dataprefs[$data->id]['search'], PARAM_NOTAGS); |
214 | //Paging variable not used for standard search. Set it to null. | |
215 | $paging = NULL; | |
216 | } | |
217 | ||
8e1ec6be | 218 | $textlib = textlib_get_instance(); |
76a2fd82 | 219 | if ($textlib->strlen($search) < 2) { |
220 | $search = ''; | |
221 | } | |
3d45b8e5 | 222 | $SESSION->dataprefs[$data->id]['search'] = $search; // Make it sticky |
223 | ||
224 | $sort = optional_param('sort', $SESSION->dataprefs[$data->id]['sort'], PARAM_INT); | |
225 | $SESSION->dataprefs[$data->id]['sort'] = $sort; // Make it sticky | |
226 | ||
227 | $order = (optional_param('order', $SESSION->dataprefs[$data->id]['order'], PARAM_ALPHA) == 'ASC') ? 'ASC': 'DESC'; | |
228 | $SESSION->dataprefs[$data->id]['order'] = $order; // Make it sticky | |
229 | ||
230 | ||
231 | $oldperpage = get_user_preferences('data_perpage_'.$data->id, 10); | |
232 | $perpage = optional_param('perpage', $oldperpage, PARAM_INT); | |
233 | ||
234 | if ($perpage < 2) { | |
235 | $perpage = 2; | |
236 | } | |
237 | if ($perpage != $oldperpage) { | |
238 | set_user_preference('data_perpage_'.$data->id, $perpage); | |
239 | } | |
240 | ||
3d4b223a | 241 | add_to_log($course->id, 'data', 'view', "view.php?id=$cm->id", $data->id, $cm->id); |
242 | ||
243 | ||
244 | // Initialize $PAGE, compute blocks | |
245 | $PAGE = page_create_instance($data->id); | |
246 | $pageblocks = blocks_setup($PAGE); | |
247 | $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]), 210); | |
248 | ||
d2b23346 | 249 | if (($edit != -1) and $PAGE->user_allowed_editing()) { |
250 | $USER->editing = $edit; | |
3d4b223a | 251 | } |
3d4b223a | 252 | |
b0100852 | 253 | /// RSS and CSS and JS meta |
68635e6f | 254 | $meta = ''; |
64452eb4 | 255 | if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) { |
c853304e | 256 | $rsspath = rss_get_url($course->id, $USER->id, 'data', $data->id); |
68635e6f | 257 | $meta .= '<link rel="alternate" type="application/rss+xml" '; |
6ba65fa0 | 258 | $meta .= 'title ="'. format_string($course->shortname) .': %fullname%" href="'.$rsspath.'" />'; |
68635e6f | 259 | } |
260 | if ($data->csstemplate) { | |
261 | $meta .= '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/mod/data/css.php?d='.$data->id.'" /> '; | |
c853304e | 262 | } |
b0100852 | 263 | if ($data->jstemplate) { |
264 | $meta .= '<script type="text/javascript" src="'.$CFG->wwwroot.'/mod/data/js.php?d='.$data->id.'"></script>'; | |
265 | } | |
266 | ||
aab98aaf | 267 | |
c853304e | 268 | /// Print the page header |
68635e6f | 269 | $PAGE->print_header($course->shortname.': %fullname%', '', $meta); |
aab98aaf | 270 | |
3d4b223a | 271 | |
236d839f | 272 | /// If we have blocks, then print the left side here |
273 | if (!empty($CFG->showblocksonmodpages)) { | |
274 | echo '<table id="layout-table"><tr>'; | |
275 | if ((blocks_have_content($pageblocks, BLOCK_POS_LEFT) || $PAGE->user_is_editing())) { | |
276 | echo '<td style="width: '.$blocks_preferred_width.'px;" id="left-column">'; | |
9f7f1a74 | 277 | print_container_start(); |
236d839f | 278 | blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT); |
9f7f1a74 | 279 | print_container_end(); |
236d839f | 280 | echo '</td>'; |
281 | } | |
282 | echo '<td id="middle-column">'; | |
9f7f1a74 | 283 | print_container_start(); |
3d4b223a | 284 | } |
285 | ||
3b27b0fe | 286 | /// Check to see if groups are being used here |
13534ef7 ML |
287 | $returnurl = 'view.php?d='.$data->id.'&search='.s($search).'&sort='.s($sort).'&order='.s($order).'&'; |
288 | groups_print_activity_menu($cm, $returnurl); | |
289 | $currentgroup = groups_get_activity_group($cm); | |
290 | $groupmode = groups_get_activity_groupmode($cm); | |
3b27b0fe | 291 | |
3d4b223a | 292 | print_heading(format_string($data->name)); |
aab98aaf | 293 | |
a593aeee | 294 | // Do we need to show a link to the RSS feed for the records? |
64452eb4 | 295 | if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) { |
a593aeee | 296 | echo '<div style="float:right;">'; |
ed69c723 | 297 | rss_print_link($course->id, $USER->id, 'data', $data->id, get_string('rsstype')); |
a593aeee | 298 | echo '</div>'; |
299 | echo '<div style="clear:both;"></div>'; | |
300 | } | |
aab98aaf | 301 | |
9e08cf6e | 302 | if ($data->intro and empty($page) and empty($record) and $mode != 'single') { |
c1afef80 | 303 | print_box(format_text($data->intro), 'generalbox', 'intro'); |
56135f6b | 304 | } |
305 | ||
473dd288 | 306 | /// Delete any requested records |
307 | ||
046dd7dc | 308 | if ($delete && confirm_sesskey() && (has_capability('mod/data:manageentries', $context) or data_isowner($delete))) { |
3d45b8e5 | 309 | if ($confirm = optional_param('confirm',0,PARAM_INT)) { |
310 | if ($deleterecord = get_record('data_records', 'id', $delete)) { // Need to check this is valid | |
311 | if ($deleterecord->dataid == $data->id) { // Must be from this database | |
312 | if ($contents = get_records('data_content','recordid', $deleterecord->id)) { | |
313 | foreach ($contents as $content) { // Delete files or whatever else this field allows | |
314 | if ($field = data_get_field_from_id($content->fieldid, $data)) { // Might not be there | |
315 | $field->delete_content($content->recordid); | |
316 | } | |
3d4b223a | 317 | } |
318 | } | |
3d45b8e5 | 319 | delete_records('data_content','recordid', $deleterecord->id); |
320 | delete_records('data_records','id', $deleterecord->id); | |
f0497d6f | 321 | |
3d45b8e5 | 322 | add_to_log($course->id, 'data', 'record delete', "view.php?id=$cm->id", $data->id, $cm->id); |
f0497d6f | 323 | |
3d45b8e5 | 324 | notify(get_string('recorddeleted','data'), 'notifysuccess'); |
325 | } | |
3d4b223a | 326 | } |
3d45b8e5 | 327 | |
328 | } else { // Print a confirmation page | |
64452eb4 | 329 | if ($deleterecord = get_record('data_records', 'id', $delete)) { // Need to check this is valid |
330 | if ($deleterecord->dataid == $data->id) { // Must be from this database | |
aab98aaf | 331 | notice_yesno(get_string('confirmdeleterecord','data'), |
64452eb4 | 332 | 'view.php?d='.$data->id.'&delete='.$delete.'&confirm=1&sesskey='.sesskey(), |
333 | 'view.php?d='.$data->id); | |
3d45b8e5 | 334 | |
64452eb4 | 335 | $records[] = $deleterecord; |
3f9672d3 | 336 | echo data_print_template('singletemplate', $records, $data, '', 0, true); |
64452eb4 | 337 | |
338 | print_footer($course); | |
339 | exit; | |
340 | } | |
341 | } | |
342 | } | |
343 | } | |
344 | ||
345 | ||
346 | ||
347 | /// Print the tabs | |
348 | ||
349 | if ($record or $mode == 'single') { | |
350 | $currenttab = 'single'; | |
7900ecb0 | 351 | } elseif($mode == 'asearch') { |
352 | $currenttab = 'asearch'; | |
353 | } | |
354 | else { | |
64452eb4 | 355 | $currenttab = 'list'; |
356 | } | |
aab98aaf | 357 | include('tabs.php'); |
64452eb4 | 358 | |
7900ecb0 | 359 | if ($mode != 'asearch') { |
360 | /// Approve any requested records | |
64452eb4 | 361 | |
7900ecb0 | 362 | if ($approve && confirm_sesskey() && has_capability('mod/data:approve', $context)) { |
363 | if ($approverecord = get_record('data_records', 'id', $approve)) { // Need to check this is valid | |
364 | if ($approverecord->dataid == $data->id) { // Must be from this database | |
365 | $newrecord->id = $approverecord->id; | |
366 | $newrecord->approved = 1; | |
367 | if (update_record('data_records', $newrecord)) { | |
368 | notify(get_string('recordapproved','data'), 'notifysuccess'); | |
369 | } | |
64452eb4 | 370 | } |
371 | } | |
3d4b223a | 372 | } |
87518137 | 373 | |
374 | // Check the number of entries required against the number of entries already made (doesn't apply to teachers) | |
375 | $requiredentries_allowed = true; | |
376 | $numentries = data_numentries($data); | |
377 | if ($data->requiredentries > 0 && $numentries < $data->requiredentries && !has_capability('mod/data:manageentries', $context)) { | |
013a8774 | 378 | $data->entriesleft = $data->requiredentries - $numentries; |
379 | $strentrieslefttoadd = get_string('entrieslefttoadd', 'data', $data); | |
87518137 | 380 | notify($strentrieslefttoadd); |
381 | $requiredentries_allowed = false; | |
382 | } | |
3d4b223a | 383 | |
7900ecb0 | 384 | /// We need to examine the whole dataset to produce the correct paging |
33819735 | 385 | |
7900ecb0 | 386 | if ((!has_capability('mod/data:managetemplates', $context)) && ($data->approval)) { |
387 | if (isloggedin()) { | |
388 | $approveselect = ' AND (r.approved=1 OR r.userid='.$USER->id.') '; | |
389 | } else { | |
390 | $approveselect = ' AND r.approved=1 '; | |
391 | } | |
4431d2e0 | 392 | } else { |
7900ecb0 | 393 | $approveselect = ' '; |
4431d2e0 | 394 | } |
3d4b223a | 395 | |
7900ecb0 | 396 | if ($currentgroup) { |
397 | $groupselect = " AND (r.groupid = '$currentgroup' OR r.groupid = 0)"; | |
64452eb4 | 398 | } else { |
7900ecb0 | 399 | $groupselect = ' '; |
64452eb4 | 400 | } |
3d45b8e5 | 401 | |
026d562e | 402 | $ilike = sql_ilike(); //Be case-insensitive |
403 | ||
7900ecb0 | 404 | /// Find the field we are sorting on |
fb9d1e09 | 405 | if ($sort == DATA_FIRSTNAME or $sort == DATA_LASTNAME or empty($sort)) { |
714bec74 | 406 | |
fb9d1e09 | 407 | if ($sort == DATA_LASTNAME) { |
714bec74 | 408 | $ordering = "u.lastname $order, u.firstname $order"; |
6a5357f4 | 409 | } else if ($sort == DATA_FIRSTNAME) { |
714bec74 | 410 | $ordering = "u.firstname $order, u.lastname $order"; |
fb9d1e09 | 411 | } else { |
412 | $ordering = "r.timecreated $order"; | |
714bec74 | 413 | } |
414 | ||
415 | $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname'; | |
416 | $count = ' COUNT(DISTINCT c.recordid) '; | |
417 | $tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r,'.$CFG->prefix.'data_content cs, '.$CFG->prefix.'user u '; | |
418 | $where = 'WHERE c.recordid = r.id | |
419 | AND r.dataid = '.$data->id.' | |
420 | AND r.userid = u.id | |
421 | AND cs.recordid = r.id '; | |
422 | $sortorder = ' ORDER BY '.$ordering.', r.id ASC '; | |
423 | $searchselect = ''; | |
424 | ||
425 | // If requiredentries is not reached, only show current user's entries | |
426 | if (!$requiredentries_allowed) { | |
427 | $where .= ' AND u.id = ' . $USER->id; | |
428 | } | |
429 | ||
430 | if (!empty($advanced)) { //If advanced box is checked. | |
431 | foreach($search_array as $key => $val) { //what does $search_array hold? | |
432 | if ($key == DATA_FIRSTNAME or $key == DATA_LASTNAME) { | |
026d562e | 433 | $searchselect .= " AND $val->field $ilike '%{$val->data}%'"; |
714bec74 | 434 | continue; |
435 | } | |
436 | $tables .= ', '.$CFG->prefix.'data_content c'.$key.' '; | |
437 | $where .= ' AND c'.$key.'.recordid = r.id'; | |
438 | $searchselect .= ' AND ('.$val->sql.') '; | |
439 | } | |
440 | } else if ($search) { | |
714bec74 | 441 | $searchselect = " AND (cs.content $ilike '%$search%' OR u.firstname $ilike '%$search%' OR u.lastname $ilike '%$search%' ) "; |
442 | } else { | |
443 | $searchselect = ' '; | |
444 | } | |
445 | ||
fb9d1e09 | 446 | } else if ($sort > 0 and $sortfield = data_get_field_from_id($sort, $data)) { |
7900ecb0 | 447 | |
448 | $sortcontent = $sortfield->get_sort_field(); | |
449 | $sortcontentfull = $sortfield->get_sort_sql('c.'.$sortcontent); | |
714bec74 | 450 | |
451 | $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname, c.'.$sortcontent.', '.$sortcontentfull.' AS _order '; | |
7900ecb0 | 452 | $count = ' COUNT(DISTINCT c.recordid) '; |
714bec74 | 453 | $tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r,'.$CFG->prefix.'data_content cs, '.$CFG->prefix.'user u '; |
7900ecb0 | 454 | $where = 'WHERE c.recordid = r.id |
455 | AND c.fieldid = '.$sort.' | |
456 | AND r.dataid = '.$data->id.' | |
457 | AND r.userid = u.id | |
714bec74 | 458 | AND cs.recordid = r.id '; |
b99b25ea | 459 | $sortorder = ' ORDER BY _order '.$order.' , r.id ASC '; |
7900ecb0 | 460 | $searchselect = ''; |
714bec74 | 461 | |
87518137 | 462 | // If requiredentries is not reached, only show current user's entries |
463 | if (!$requiredentries_allowed) { | |
464 | $where .= ' AND u.id = ' . $USER->id; | |
465 | } | |
714bec74 | 466 | |
467 | if (!empty($advanced)) { //If advanced box is checked. | |
468 | foreach($search_array as $key => $val) { //what does $search_array hold? | |
469 | if ($key == DATA_FIRSTNAME or $key == DATA_LASTNAME) { | |
026d562e | 470 | $searchselect .= " AND $val->field $ilike '%{$val->data}%'"; |
714bec74 | 471 | continue; |
472 | } | |
7900ecb0 | 473 | $tables .= ', '.$CFG->prefix.'data_content c'.$key.' '; |
474 | $where .= ' AND c'.$key.'.recordid = r.id'; | |
475 | $searchselect .= ' AND ('.$val->sql.') '; | |
476 | } | |
714bec74 | 477 | } else if ($search) { |
714bec74 | 478 | $searchselect = " AND (cs.content $ilike '%$search%' OR u.firstname $ilike '%$search%' OR u.lastname $ilike '%$search%' ) "; |
7900ecb0 | 479 | } else { |
480 | $searchselect = ' '; | |
714bec74 | 481 | } |
482 | ||
7900ecb0 | 483 | } else if ($search) { |
714bec74 | 484 | $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname '; |
7900ecb0 | 485 | $count = ' COUNT(DISTINCT c.recordid) '; |
486 | $tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r, '.$CFG->prefix.'user u '; | |
487 | $where = 'WHERE c.recordid = r.id | |
488 | AND r.userid = u.id | |
489 | AND r.dataid = '.$data->id; | |
490 | $sortorder = ' ORDER BY r.id ASC '; | |
491 | $searchselect = ''; | |
714bec74 | 492 | |
87518137 | 493 | // If requiredentries is not reached, only show current user's entries |
494 | if (!$requiredentries_allowed) { | |
495 | $where .= ' AND u.id = ' . $USER->id; | |
496 | } | |
714bec74 | 497 | |
7900ecb0 | 498 | if (!empty($advanced)) { //Advanced search box again. |
499 | foreach($search_array as $key => $val) { | |
714bec74 | 500 | if ($key == DATA_FIRSTNAME or $key == DATA_LASTNAME) { |
026d562e | 501 | $searchselect .= " AND $val->field $ilike '%{$val->data}%'"; |
714bec74 | 502 | continue; |
503 | } | |
7900ecb0 | 504 | $tables .= ', '.$CFG->prefix.'data_content c'.$key.' '; |
505 | $where .= ' AND c'.$key.'.recordid = r.id '; | |
506 | $searchselect .= ' AND ('.$val->sql.') '; | |
507 | } | |
714bec74 | 508 | } else { |
714bec74 | 509 | $searchselect = " AND (c.content $ilike '%$search%' OR u.firstname $ilike '%$search%' OR u.lastname $ilike '%$search%' ) "; |
7900ecb0 | 510 | } |
3d45b8e5 | 511 | |
714bec74 | 512 | |
7900ecb0 | 513 | } else { |
c95034f2 | 514 | $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname '; |
7900ecb0 | 515 | $count = ' COUNT(r.id) '; |
516 | $tables = $CFG->prefix.'data_records r, '.$CFG->prefix.'user u '; | |
517 | $where = 'WHERE r.dataid = '.$data->id. ' AND r.userid = u.id '; | |
518 | $sortorder = ' ORDER BY r.timecreated '.$order. ' '; | |
519 | $searchselect = ' '; | |
714bec74 | 520 | |
87518137 | 521 | // If requiredentries is not reached, only show current user's entries |
522 | if (!$requiredentries_allowed) { | |
523 | $where .= ' AND u.id = ' . $USER->id; | |
524 | } | |
7900ecb0 | 525 | } |
3d4b223a | 526 | |
64452eb4 | 527 | |
7900ecb0 | 528 | /// To actually fetch the records |
3d45b8e5 | 529 | |
7900ecb0 | 530 | $fromsql = ' FROM '.$tables.$where.$groupselect.$approveselect.$searchselect; |
aab98aaf | 531 | |
7900ecb0 | 532 | $sqlselect = 'SELECT '.$what.$fromsql.$sortorder; |
3d45b8e5 | 533 | |
7900ecb0 | 534 | $sqlcount = 'SELECT '.$count.$fromsql; // Total number of records |
3d45b8e5 | 535 | |
7900ecb0 | 536 | /// Work out the paging numbers |
3d45b8e5 | 537 | |
7900ecb0 | 538 | $totalcount = count_records_sql($sqlcount); |
3d4b223a | 539 | |
7900ecb0 | 540 | if ($record) { // We need to just show one, so where is it in context? |
541 | $nowperpage = 1; | |
542 | $mode = 'single'; | |
de89899b | 543 | |
7900ecb0 | 544 | # Following code needs testing to make it work |
545 | # if ($sort) { // We need to search by that field | |
546 | # if ($content = get_field('data_content', 'content', 'recordid', $record->id, 'fieldid', $sort)) { | |
547 | # $content = addslashes($content); | |
548 | # if ($order == 'ASC') { | |
549 | # $lessthan = " AND $sortcontentfull < '$content' | |
550 | # OR ($sortcontentfull = '$content' AND r.id < '$record->id') "; | |
551 | # } else { | |
552 | # $lessthan = " AND $sortcontentfull > '$content' | |
553 | # OR ($sortcontentfull = '$content' AND r.id < '$record->id') "; | |
554 | # } | |
555 | # } else { // Failed to find data (shouldn't happen), so fall back to something easy | |
556 | # $lessthan = " r.id < '$record->id' "; | |
557 | # } | |
558 | # } else { | |
559 | # $lessthan = " r.id < '$record->id' "; | |
560 | # } | |
561 | # $sqlindex = 'SELECT COUNT(DISTINCT c.recordid) '.$fromsql.$lessthan.$sortorder; | |
562 | # $page = count_records_sql($sqlindex); | |
64452eb4 | 563 | |
499ed215 | 564 | |
7900ecb0 | 565 | $page = 0; |
714bec74 | 566 | if ($allrecords = get_records_sql($sqlselect)) { // Kludgey but accurate at least! |
567 | foreach ($allrecords as $key => $allrecord) { | |
568 | if ($key == $record->id) { | |
569 | break; | |
570 | } | |
571 | $page++; | |
7900ecb0 | 572 | } |
3d45b8e5 | 573 | } |
3d45b8e5 | 574 | |
7900ecb0 | 575 | } else if ($mode == 'single') { // We rely on ambient $page settings |
576 | $nowperpage = 1; | |
3d45b8e5 | 577 | |
7900ecb0 | 578 | } else { |
579 | $nowperpage = $perpage; | |
580 | } | |
3d45b8e5 | 581 | |
7900ecb0 | 582 | /// Get the actual records |
583 | ||
584 | $records = get_records_sql($sqlselect, $page * $nowperpage, $nowperpage); | |
3d45b8e5 | 585 | |
7900ecb0 | 586 | if (empty($records)) { // Nothing to show! |
587 | if ($record) { // Something was requested so try to show that at least (bug 5132) | |
588 | if (has_capability('mod/data:manageentries', $context) || empty($data->approval) || | |
589 | $record->approved || (isloggedin() && $record->userid == $USER->id)) { | |
590 | if (!$currentgroup || $record->groupid == $currentgroup || $record->groupid == 0) { | |
591 | $records[] = $record; | |
592 | } | |
3dec563c | 593 | } |
594 | } | |
7900ecb0 | 595 | if ($records) { // OK, we can show this one |
596 | data_print_template('singletemplate', $records, $data, $search, $page); | |
597 | } else if ($search){ | |
598 | notify(get_string('nomatch','data')); | |
599 | } else { | |
600 | notify(get_string('norecords','data')); | |
601 | } | |
cf3e199b | 602 | |
7900ecb0 | 603 | } else { // We have some records to print |
68c88622 | 604 | |
7900ecb0 | 605 | if ($mode == 'single') { // Single template |
606 | $baseurl = 'view.php?d='.$data->id.'&mode=single&'; | |
3d4b223a | 607 | |
7900ecb0 | 608 | print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page'); |
3d45b8e5 | 609 | |
7900ecb0 | 610 | if (empty($data->singletemplate)){ |
611 | notify(get_string('nosingletemplate','data')); | |
612 | data_generate_default_template($data, 'singletemplate', 0, false, false); | |
613 | } | |
3d4b223a | 614 | |
7900ecb0 | 615 | data_print_template('singletemplate', $records, $data, $search, $page); |
3d45b8e5 | 616 | |
7900ecb0 | 617 | print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page'); |
3d45b8e5 | 618 | |
7900ecb0 | 619 | } else { // List template |
620 | $baseurl = 'view.php?d='.$data->id.'&'; | |
621 | //send the advanced flag through the URL so it is remembered while paging. | |
622 | $baseurl .= 'advanced='.$advanced.'&'; | |
623 | //pass variable to allow determining whether or not we are paging through results. | |
624 | $baseurl .= 'paging='.$paging.'&'; | |
3d45b8e5 | 625 | |
7900ecb0 | 626 | print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page'); |
3d45b8e5 | 627 | |
7900ecb0 | 628 | if (empty($data->listtemplate)){ |
629 | notify(get_string('nolisttemplate','data')); | |
630 | data_generate_default_template($data, 'listtemplate', 0, false, false); | |
631 | } | |
632 | echo $data->listtemplateheader; | |
633 | data_print_template('listtemplate', $records, $data, $search, $page); | |
634 | echo $data->listtemplatefooter; | |
635 | ||
636 | print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page'); | |
3d45b8e5 | 637 | } |
3d45b8e5 | 638 | |
3d45b8e5 | 639 | } |
7900ecb0 | 640 | } |
641 | ||
642 | $search = trim($search); | |
643 | if (empty($records)) { | |
644 | $records = array(); | |
f852d652 | 645 | } |
646 | ||
7900ecb0 | 647 | //Advanced search form doesn't make sense for single (redirects list view) |
de8ff581 | 648 | if (($records || $search || $page || $mode == 'asearch') && $mode != 'single') { |
7900ecb0 | 649 | data_print_preference_form($data, $perpage, $search, $sort, $order, $search_array, $advanced, $mode); |
64452eb4 | 650 | } |
3d4b223a | 651 | |
236d839f | 652 | /// If we have blocks, then print the left side here |
653 | if (!empty($CFG->showblocksonmodpages)) { | |
9f7f1a74 | 654 | print_container_end(); |
236d839f | 655 | echo '</td>'; // Middle column |
656 | if ((blocks_have_content($pageblocks, BLOCK_POS_RIGHT) || $PAGE->user_is_editing())) { | |
657 | echo '<td style="width: '.$blocks_preferred_width.'px;" id="right-column">'; | |
9f7f1a74 | 658 | print_container_start(); |
236d839f | 659 | blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT); |
9f7f1a74 | 660 | print_container_end(); |
236d839f | 661 | echo '</td>'; |
662 | } | |
78e02af9 | 663 | echo '</tr></table>'; |
236d839f | 664 | } |
665 | ||
3d45b8e5 | 666 | print_footer($course); |
0997e51a | 667 | ?> |