MDL-15106 towards data mod dml conversion
[moodle.git] / mod / data / view.php
CommitLineData
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 }
3223bc9e 49 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
714bec74 50 error('Course is misconfigured');
3d4b223a 51 }
3223bc9e 52 if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
714bec74 53 error('Course module is incorrect');
3d4b223a 54 }
3d45b8e5 55 $record = NULL;
3d4b223a 56
3d45b8e5 57 } else if ($rid) {
3223bc9e 58 if (! $record = $DB->get_record('data_records', array('id'=>$rid))) {
714bec74 59 error('Record ID is incorrect');
3d45b8e5 60 }
3223bc9e 61 if (! $data = $DB->get_record('data', array('id'=>$record->dataid))) {
714bec74 62 error('Data ID is incorrect');
3d45b8e5 63 }
3223bc9e 64 if (! $course = $DB->get_record('course', array('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
3223bc9e 71 if (! $data = $DB->get_record('data', array('id'=>$d))) {
714bec74 72 error('Data ID is incorrect');
3d4b223a 73 }
3223bc9e 74 if (! $course = $DB->get_record('course', array('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)) {
3223bc9e 90 if (!$DB->record_exists('data_fields', array('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
eeeb4f2a 109 // reset advanced form
110 if (!is_null(optional_param('resetadv', null, PARAM_RAW))) {
111 $SESSION->dataprefs[$data->id]['search_array'] = array();
112 // we need the redirect to cleanup the form state properly
113 redirect("view.php?id=$cm->id&amp;mode=$mode&amp;search=&amp;advanced=1");
114 }
115
de8ff581 116 $advanced = optional_param('advanced', -1, PARAM_INT);
117 if ($advanced == -1) {
118 $advanced = $SESSION->dataprefs[$data->id]['advanced'];
119 } else {
120 if (!$advanced) {
121 // explicitly switched to normal mode - discard all advanced search settings
122 $SESSION->dataprefs[$data->id]['search_array'] = array();
123 }
832123e1 124 $SESSION->dataprefs[$data->id]['advanced'] = $advanced;
de8ff581 125 }
126
7900ecb0 127 $search_array = $SESSION->dataprefs[$data->id]['search_array'];
eeeb4f2a 128
7900ecb0 129 if (!empty($advanced)) {
130 $search = '';
69c0a609 131 $vals = array();
3223bc9e 132 $fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
7900ecb0 133
134 //Added to ammend paging error. This error would occur when attempting to go from one page of advanced
135 //search results to another. All fields were reset in the page transfer, and there was no way of determining
136 //whether or not the user reset them. This would cause a blank search to execute whenever the user attempted
137 //to see any page of results past the first.
138 //This fix works as follows:
139 //$paging flag is set to false when page 0 of the advanced search results is viewed for the first time.
140 //Viewing any page of results after page 0 passes the false $paging flag though the URL (see line 523) and the
141 //execution falls through to the second condition below, allowing paging to be set to true.
142 //Paging remains true and keeps getting passed though the URL until a new search is performed
143 //(even if page 0 is revisited).
144 //A false $paging flag generates advanced search results based on the fields input by the user.
145 //A true $paging flag generates davanced search results from the $SESSION global.
146 //(See lines 147-158)
147
148 $paging = optional_param('paging', NULL, PARAM_BOOL);
149 if($page == 0 && !isset($paging)) {
150 $paging = false;
151 }
152 else {
153 $paging = true;
154 }
714bec74 155 if (!empty($fields)) {
7900ecb0 156 foreach($fields as $field) {
157 $searchfield = data_get_field_from_id($field->id, $data);
158 //Get field data to build search sql with. If paging is false, get from user.
159 //If paging is true, get data from $search_array which is obtained from the $SESSION (see line 116).
160 if(!$paging) {
161 $val = $searchfield->parse_search_field();
714bec74 162 } else {
7900ecb0 163 //Set value from session if there is a value @ the required index.
714bec74 164 if (isset($search_array[$field->id])) {
7900ecb0 165 $val = $search_array[$field->id]->data;
714bec74 166 } else { //If there is not an entry @ the required index, set value to blank.
7900ecb0 167 $val = '';
168 }
7900ecb0 169 }
714bec74 170 if (!empty($val)) {
171 $search_array[$field->id] = new object();
172 $search_array[$field->id]->sql = $searchfield->generate_sql('c'.$field->id, $val);
173 $search_array[$field->id]->data = $val;
69c0a609 174 $vals[] = $val;
714bec74 175 } else {
176 // clear it out
177 unset($search_array[$field->id]);
7900ecb0 178 }
179 }
180 }
714bec74 181
182 if (!$paging) {
183 // name searching
184 $fn = optional_param('u_fn', '', PARAM_NOTAGS);
185 $ln = optional_param('u_ln', '', PARAM_NOTAGS);
186 } else {
de8ff581 187 $fn = isset($search_array[DATA_FIRSTNAME]) ? $search_array[DATA_FIRSTNAME]->data : '';
188 $ln = isset($search_array[DATA_LASTNAME]) ? $search_array[DATA_LASTNAME]->data : '';
714bec74 189 }
190 if (!empty($fn)) {
191 $search_array[DATA_FIRSTNAME] = new object();
192 $search_array[DATA_FIRSTNAME]->sql = '';
193 $search_array[DATA_FIRSTNAME]->field = 'u.firstname';
194 $search_array[DATA_FIRSTNAME]->data = $fn;
69c0a609 195 $vals[] = $fn;
714bec74 196 } else {
197 unset($search_array[DATA_FIRSTNAME]);
198 }
199 if (!empty($ln)) {
200 $search_array[DATA_LASTNAME] = new object();
201 $search_array[DATA_LASTNAME]->sql = '';
202 $search_array[DATA_LASTNAME]->field = 'u.lastname';
203 $search_array[DATA_LASTNAME]->data = $ln;
69c0a609 204 $vals[] = $ln;
714bec74 205 } else {
206 unset($search_array[DATA_LASTNAME]);
207 }
208
7900ecb0 209 $SESSION->dataprefs[$data->id]['search_array'] = $search_array; // Make it sticky
714bec74 210
69c0a609 211 // in case we want to switch to simple search later - there might be multiple values there ;-)
212 if ($vals) {
213 $val = reset($vals);
214 if (is_string($val)) {
215 $search = $val;
216 }
217 }
218
714bec74 219 } else {
7900ecb0 220 $search = optional_param('search', $SESSION->dataprefs[$data->id]['search'], PARAM_NOTAGS);
221 //Paging variable not used for standard search. Set it to null.
222 $paging = NULL;
223 }
224
8e1ec6be 225 $textlib = textlib_get_instance();
76a2fd82 226 if ($textlib->strlen($search) < 2) {
227 $search = '';
228 }
3d45b8e5 229 $SESSION->dataprefs[$data->id]['search'] = $search; // Make it sticky
230
231 $sort = optional_param('sort', $SESSION->dataprefs[$data->id]['sort'], PARAM_INT);
232 $SESSION->dataprefs[$data->id]['sort'] = $sort; // Make it sticky
233
234 $order = (optional_param('order', $SESSION->dataprefs[$data->id]['order'], PARAM_ALPHA) == 'ASC') ? 'ASC': 'DESC';
235 $SESSION->dataprefs[$data->id]['order'] = $order; // Make it sticky
236
237
238 $oldperpage = get_user_preferences('data_perpage_'.$data->id, 10);
239 $perpage = optional_param('perpage', $oldperpage, PARAM_INT);
240
241 if ($perpage < 2) {
242 $perpage = 2;
243 }
244 if ($perpage != $oldperpage) {
245 set_user_preference('data_perpage_'.$data->id, $perpage);
246 }
247
3d4b223a 248 add_to_log($course->id, 'data', 'view', "view.php?id=$cm->id", $data->id, $cm->id);
249
250
251// Initialize $PAGE, compute blocks
252 $PAGE = page_create_instance($data->id);
253 $pageblocks = blocks_setup($PAGE);
254 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]), 210);
255
d2b23346 256 if (($edit != -1) and $PAGE->user_allowed_editing()) {
257 $USER->editing = $edit;
3d4b223a 258 }
3d4b223a 259
b0100852 260/// RSS and CSS and JS meta
68635e6f 261 $meta = '';
64452eb4 262 if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
c853304e 263 $rsspath = rss_get_url($course->id, $USER->id, 'data', $data->id);
68635e6f 264 $meta .= '<link rel="alternate" type="application/rss+xml" ';
6ba65fa0 265 $meta .= 'title ="'. format_string($course->shortname) .': %fullname%" href="'.$rsspath.'" />';
68635e6f 266 }
267 if ($data->csstemplate) {
268 $meta .= '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/mod/data/css.php?d='.$data->id.'" /> ';
c853304e 269 }
b0100852 270 if ($data->jstemplate) {
271 $meta .= '<script type="text/javascript" src="'.$CFG->wwwroot.'/mod/data/js.php?d='.$data->id.'"></script>';
272 }
273
aab98aaf 274
c853304e 275/// Print the page header
68635e6f 276 $PAGE->print_header($course->shortname.': %fullname%', '', $meta);
aab98aaf 277
3d4b223a 278
236d839f 279/// If we have blocks, then print the left side here
280 if (!empty($CFG->showblocksonmodpages)) {
281 echo '<table id="layout-table"><tr>';
282 if ((blocks_have_content($pageblocks, BLOCK_POS_LEFT) || $PAGE->user_is_editing())) {
283 echo '<td style="width: '.$blocks_preferred_width.'px;" id="left-column">';
9f7f1a74 284 print_container_start();
236d839f 285 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
9f7f1a74 286 print_container_end();
236d839f 287 echo '</td>';
288 }
289 echo '<td id="middle-column">';
9f7f1a74 290 print_container_start();
3d4b223a 291 }
292
3b27b0fe 293/// Check to see if groups are being used here
13534ef7
ML
294 $returnurl = 'view.php?d='.$data->id.'&amp;search='.s($search).'&amp;sort='.s($sort).'&amp;order='.s($order).'&amp;';
295 groups_print_activity_menu($cm, $returnurl);
296 $currentgroup = groups_get_activity_group($cm);
297 $groupmode = groups_get_activity_groupmode($cm);
3b27b0fe 298
3d4b223a 299 print_heading(format_string($data->name));
aab98aaf 300
a593aeee 301 // Do we need to show a link to the RSS feed for the records?
64452eb4 302 if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
a593aeee 303 echo '<div style="float:right;">';
ed69c723 304 rss_print_link($course->id, $USER->id, 'data', $data->id, get_string('rsstype'));
a593aeee 305 echo '</div>';
306 echo '<div style="clear:both;"></div>';
307 }
aab98aaf 308
9e08cf6e 309 if ($data->intro and empty($page) and empty($record) and $mode != 'single') {
ea6073bb 310 $options = new object();
311 $options->noclean = true;
312 print_box(format_text($data->intro, FORMAT_MOODLE, $options), 'generalbox', 'intro');
56135f6b 313 }
314
473dd288 315/// Delete any requested records
316
046dd7dc 317 if ($delete && confirm_sesskey() && (has_capability('mod/data:manageentries', $context) or data_isowner($delete))) {
3d45b8e5 318 if ($confirm = optional_param('confirm',0,PARAM_INT)) {
3223bc9e 319 if ($deleterecord = $DB->get_record('data_records', array('id'=>$delete))) { // Need to check this is valid
3d45b8e5 320 if ($deleterecord->dataid == $data->id) { // Must be from this database
3223bc9e 321 if ($contents = $DB->get_records('data_content', array('recordid'=>$deleterecord->id))) {
3d45b8e5 322 foreach ($contents as $content) { // Delete files or whatever else this field allows
323 if ($field = data_get_field_from_id($content->fieldid, $data)) { // Might not be there
324 $field->delete_content($content->recordid);
325 }
3d4b223a 326 }
327 }
3223bc9e 328 $DB->delete_records('data_content', array('recordid'=>$deleterecord->id));
329 $DB->delete_records('data_records', array('id'=>$deleterecord->id));
f0497d6f 330
3d45b8e5 331 add_to_log($course->id, 'data', 'record delete', "view.php?id=$cm->id", $data->id, $cm->id);
f0497d6f 332
3d45b8e5 333 notify(get_string('recorddeleted','data'), 'notifysuccess');
334 }
3d4b223a 335 }
3d45b8e5 336
337 } else { // Print a confirmation page
3223bc9e 338 if ($deleterecord = $DB->get_record('data_records', array('id'=>$delete))) { // Need to check this is valid
64452eb4 339 if ($deleterecord->dataid == $data->id) { // Must be from this database
aab98aaf 340 notice_yesno(get_string('confirmdeleterecord','data'),
64452eb4 341 'view.php?d='.$data->id.'&amp;delete='.$delete.'&amp;confirm=1&amp;sesskey='.sesskey(),
342 'view.php?d='.$data->id);
3d45b8e5 343
64452eb4 344 $records[] = $deleterecord;
3f9672d3 345 echo data_print_template('singletemplate', $records, $data, '', 0, true);
64452eb4 346
347 print_footer($course);
348 exit;
349 }
350 }
351 }
352 }
353
354
355
356/// Print the tabs
357
358 if ($record or $mode == 'single') {
359 $currenttab = 'single';
7900ecb0 360 } elseif($mode == 'asearch') {
361 $currenttab = 'asearch';
362 }
363 else {
64452eb4 364 $currenttab = 'list';
365 }
aab98aaf 366 include('tabs.php');
64452eb4 367
eeeb4f2a 368 if ($mode == 'asearch') {
369 $maxcount = 0;
370
371 } else {
7900ecb0 372 /// Approve any requested records
64452eb4 373
bb5740f4 374 $approvecap = has_capability('mod/data:approve', $context);
375
376 if ($approve && confirm_sesskey() && $approvecap) {
3223bc9e 377 if ($approverecord = $DB->get_record('data_records', array('id'=>$approve))) { // Need to check this is valid
7900ecb0 378 if ($approverecord->dataid == $data->id) { // Must be from this database
379 $newrecord->id = $approverecord->id;
380 $newrecord->approved = 1;
3223bc9e 381 if ($DB->update_record('data_records', $newrecord)) {
7900ecb0 382 notify(get_string('recordapproved','data'), 'notifysuccess');
383 }
64452eb4 384 }
385 }
3d4b223a 386 }
87518137 387
eeeb4f2a 388 // Check the number of entries required against the number of entries already made (doesn't apply to teachers)
389 $requiredentries_allowed = true;
390 $numentries = data_numentries($data);
391 if ($data->requiredentries > 0 && $numentries < $data->requiredentries && !has_capability('mod/data:manageentries', $context)) {
392 $data->entriesleft = $data->requiredentries - $numentries;
393 $strentrieslefttoadd = get_string('entrieslefttoadd', 'data', $data);
394 notify($strentrieslefttoadd);
395 $requiredentries_allowed = false;
396 }
3d4b223a 397
bb5740f4 398 /// setup group and approve restrictions
399 if (!$approvecap && $data->approval) {
7900ecb0 400 if (isloggedin()) {
401 $approveselect = ' AND (r.approved=1 OR r.userid='.$USER->id.') ';
402 } else {
403 $approveselect = ' AND r.approved=1 ';
404 }
4431d2e0 405 } else {
7900ecb0 406 $approveselect = ' ';
4431d2e0 407 }
3d4b223a 408
7900ecb0 409 if ($currentgroup) {
410 $groupselect = " AND (r.groupid = '$currentgroup' OR r.groupid = 0)";
64452eb4 411 } else {
7900ecb0 412 $groupselect = ' ';
64452eb4 413 }
3d45b8e5 414
026d562e 415 $ilike = sql_ilike(); //Be case-insensitive
416
7900ecb0 417 /// Find the field we are sorting on
3239b010 418 if ($sort <= 0 or !$sortfield = data_get_field_from_id($sort, $data)) {
bb5740f4 419
420 switch ($sort) {
421 case DATA_LASTNAME:
422 $ordering = "u.lastname $order, u.firstname $order";
423 break;
424 case DATA_FIRSTNAME:
425 $ordering = "u.firstname $order, u.lastname $order";
426 break;
427 case DATA_APPROVED:
428 $ordering = "r.approved $order, r.timecreated $order";
429 break;
3239b010 430 case DATA_TIMEMODIFIED:
431 $ordering = "r.timemodified $order";
432 break;
433 case DATA_TIMEADDED:
bb5740f4 434 default:
3239b010 435 $sort = 0;
bb5740f4 436 $ordering = "r.timecreated $order";
714bec74 437 }
438
439 $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname';
440 $count = ' COUNT(DISTINCT c.recordid) ';
441 $tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r,'.$CFG->prefix.'data_content cs, '.$CFG->prefix.'user u ';
442 $where = 'WHERE c.recordid = r.id
443 AND r.dataid = '.$data->id.'
444 AND r.userid = u.id
445 AND cs.recordid = r.id ';
446 $sortorder = ' ORDER BY '.$ordering.', r.id ASC ';
447 $searchselect = '';
448
449 // If requiredentries is not reached, only show current user's entries
450 if (!$requiredentries_allowed) {
451 $where .= ' AND u.id = ' . $USER->id;
452 }
453
454 if (!empty($advanced)) { //If advanced box is checked.
455 foreach($search_array as $key => $val) { //what does $search_array hold?
456 if ($key == DATA_FIRSTNAME or $key == DATA_LASTNAME) {
026d562e 457 $searchselect .= " AND $val->field $ilike '%{$val->data}%'";
714bec74 458 continue;
459 }
460 $tables .= ', '.$CFG->prefix.'data_content c'.$key.' ';
461 $where .= ' AND c'.$key.'.recordid = r.id';
462 $searchselect .= ' AND ('.$val->sql.') ';
463 }
464 } else if ($search) {
714bec74 465 $searchselect = " AND (cs.content $ilike '%$search%' OR u.firstname $ilike '%$search%' OR u.lastname $ilike '%$search%' ) ";
466 } else {
467 $searchselect = ' ';
468 }
469
3239b010 470 } else {
7900ecb0 471
472 $sortcontent = $sortfield->get_sort_field();
473 $sortcontentfull = $sortfield->get_sort_sql('c.'.$sortcontent);
714bec74 474
475 $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname, c.'.$sortcontent.', '.$sortcontentfull.' AS _order ';
7900ecb0 476 $count = ' COUNT(DISTINCT c.recordid) ';
714bec74 477 $tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r,'.$CFG->prefix.'data_content cs, '.$CFG->prefix.'user u ';
7900ecb0 478 $where = 'WHERE c.recordid = r.id
479 AND c.fieldid = '.$sort.'
480 AND r.dataid = '.$data->id.'
481 AND r.userid = u.id
714bec74 482 AND cs.recordid = r.id ';
b99b25ea 483 $sortorder = ' ORDER BY _order '.$order.' , r.id ASC ';
7900ecb0 484 $searchselect = '';
714bec74 485
87518137 486 // If requiredentries is not reached, only show current user's entries
487 if (!$requiredentries_allowed) {
488 $where .= ' AND u.id = ' . $USER->id;
489 }
714bec74 490
491 if (!empty($advanced)) { //If advanced box is checked.
492 foreach($search_array as $key => $val) { //what does $search_array hold?
493 if ($key == DATA_FIRSTNAME or $key == DATA_LASTNAME) {
026d562e 494 $searchselect .= " AND $val->field $ilike '%{$val->data}%'";
714bec74 495 continue;
496 }
7900ecb0 497 $tables .= ', '.$CFG->prefix.'data_content c'.$key.' ';
498 $where .= ' AND c'.$key.'.recordid = r.id';
499 $searchselect .= ' AND ('.$val->sql.') ';
500 }
714bec74 501 } else if ($search) {
714bec74 502 $searchselect = " AND (cs.content $ilike '%$search%' OR u.firstname $ilike '%$search%' OR u.lastname $ilike '%$search%' ) ";
7900ecb0 503 } else {
504 $searchselect = ' ';
714bec74 505 }
7900ecb0 506 }
3d4b223a 507
7900ecb0 508 /// To actually fetch the records
3d45b8e5 509
eeeb4f2a 510 $fromsql = "FROM $tables $where $groupselect $approveselect $searchselect";
511 $sqlselect = "SELECT $what $fromsql $sortorder";
512 $sqlcount = "SELECT $count $fromsql"; // Total number of records when searching
029cf846 513 $sqlrids = "SELECT tmp.id FROM ($sqlselect) tmp";
eeeb4f2a 514 $sqlmax = "SELECT $count FROM $tables $where $groupselect $approveselect"; // number of all recoirds user may see
3d45b8e5 515
eeeb4f2a 516 /// Work out the paging numbers and counts
3d45b8e5 517
7900ecb0 518 $totalcount = count_records_sql($sqlcount);
eeeb4f2a 519 if (empty($searchselect)) {
520 $maxcount = $totalcount;
521 } else {
522 $maxcount = count_records_sql($sqlmax);
523 }
3d4b223a 524
7900ecb0 525 if ($record) { // We need to just show one, so where is it in context?
526 $nowperpage = 1;
527 $mode = 'single';
de89899b 528
7900ecb0 529 $page = 0;
029cf846 530 if ($allrecordids = get_records_sql($sqlrids)) {
531 $allrecordids = array_keys($allrecordids);
532 $page = (int)array_search($record->id, $allrecordids);
533 unset($allrecordids);
3d45b8e5 534 }
3d45b8e5 535
7900ecb0 536 } else if ($mode == 'single') { // We rely on ambient $page settings
537 $nowperpage = 1;
3d45b8e5 538
7900ecb0 539 } else {
540 $nowperpage = $perpage;
541 }
3d45b8e5 542
7900ecb0 543 /// Get the actual records
544
eeeb4f2a 545 if (!$records = get_records_sql($sqlselect, $page * $nowperpage, $nowperpage)) {
546 // Nothing to show!
7900ecb0 547 if ($record) { // Something was requested so try to show that at least (bug 5132)
548 if (has_capability('mod/data:manageentries', $context) || empty($data->approval) ||
549 $record->approved || (isloggedin() && $record->userid == $USER->id)) {
550 if (!$currentgroup || $record->groupid == $currentgroup || $record->groupid == 0) {
eeeb4f2a 551 // OK, we can show this one
552 $records = array($record->id => $record);
553 $totalcount = 1;
7900ecb0 554 }
3dec563c 555 }
556 }
eeeb4f2a 557 }
558
559 if (empty($records)) {
560 if ($maxcount){
561 $a = new object();
562 $a->max = $maxcount;
563 $a->reseturl = "view.php?id=$cm->id&amp;mode=$mode&amp;search=&amp;advanced=0";
564 notify(get_string('foundnorecords','data', $a));
7900ecb0 565 } else {
566 notify(get_string('norecords','data'));
567 }
cf3e199b 568
eeeb4f2a 569 } else { // We have some records to print
570
571 if ($maxcount != $totalcount) {
572 $a = new object();
573 $a->num = $totalcount;
574 $a->max = $maxcount;
575 $a->reseturl = "view.php?id=$cm->id&amp;mode=$mode&amp;search=&amp;advanced=0";
576 notify(get_string('foundrecords', 'data', $a), 'notifysuccess');
577 }
68c88622 578
7900ecb0 579 if ($mode == 'single') { // Single template
580 $baseurl = 'view.php?d='.$data->id.'&amp;mode=single&amp;';
3d4b223a 581
7900ecb0 582 print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
3d45b8e5 583
7900ecb0 584 if (empty($data->singletemplate)){
585 notify(get_string('nosingletemplate','data'));
586 data_generate_default_template($data, 'singletemplate', 0, false, false);
587 }
3d4b223a 588
7900ecb0 589 data_print_template('singletemplate', $records, $data, $search, $page);
3d45b8e5 590
7900ecb0 591 print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
3d45b8e5 592
7900ecb0 593 } else { // List template
594 $baseurl = 'view.php?d='.$data->id.'&amp;';
595 //send the advanced flag through the URL so it is remembered while paging.
596 $baseurl .= 'advanced='.$advanced.'&amp;';
597 //pass variable to allow determining whether or not we are paging through results.
598 $baseurl .= 'paging='.$paging.'&amp;';
3d45b8e5 599
7900ecb0 600 print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
3d45b8e5 601
7900ecb0 602 if (empty($data->listtemplate)){
603 notify(get_string('nolisttemplate','data'));
604 data_generate_default_template($data, 'listtemplate', 0, false, false);
605 }
606 echo $data->listtemplateheader;
607 data_print_template('listtemplate', $records, $data, $search, $page);
608 echo $data->listtemplatefooter;
609
610 print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
3d45b8e5 611 }
3d45b8e5 612
3d45b8e5 613 }
7900ecb0 614 }
615
616 $search = trim($search);
617 if (empty($records)) {
618 $records = array();
f852d652 619 }
620
7900ecb0 621 //Advanced search form doesn't make sense for single (redirects list view)
eeeb4f2a 622 if (($maxcount || $mode == 'asearch') && $mode != 'single') {
7900ecb0 623 data_print_preference_form($data, $perpage, $search, $sort, $order, $search_array, $advanced, $mode);
64452eb4 624 }
3d4b223a 625
236d839f 626/// If we have blocks, then print the left side here
627 if (!empty($CFG->showblocksonmodpages)) {
9f7f1a74 628 print_container_end();
236d839f 629 echo '</td>'; // Middle column
630 if ((blocks_have_content($pageblocks, BLOCK_POS_RIGHT) || $PAGE->user_is_editing())) {
631 echo '<td style="width: '.$blocks_preferred_width.'px;" id="right-column">';
9f7f1a74 632 print_container_start();
236d839f 633 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT);
9f7f1a74 634 print_container_end();
236d839f 635 echo '</td>';
636 }
78e02af9 637 echo '</tr></table>';
236d839f 638 }
639
3d45b8e5 640 print_footer($course);
0997e51a 641?>