"MDL-13766, ->pixpath doesn't work in web service, use ->httpswwwroot instead"
[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
3681f9a9 25 require_once(dirname(__FILE__) . '/../../config.php');
26 require_once($CFG->dirroot . '/mod/data/lib.php');
27 require_once($CFG->libdir . '/rsslib.php');
4e1132a8 28 require_once($CFG->libdir . '/completionlib.php');
9f7f1a74 29
3d45b8e5 30/// One of these is necessary!
d53e5129 31 $id = optional_param('id', 0, PARAM_INT); // course module id
32 $d = optional_param('d', 0, PARAM_INT); // database id
33 $rid = optional_param('rid', 0, PARAM_INT); //record id
34 $mode = optional_param('mode', '', PARAM_ALPHA); // Force the browse mode ('single')
35 $filter = optional_param('filter', 0, PARAM_BOOL);
36 // search filter will only be applied when $filter is true
3d45b8e5 37
d2b23346 38 $edit = optional_param('edit', -1, PARAM_BOOL);
7900ecb0 39 $page = optional_param('page', 0, PARAM_INT);
3d45b8e5 40/// These can be added to perform an action on a record
473dd288 41 $approve = optional_param('approve', 0, PARAM_INT); //approval recordid
42 $delete = optional_param('delete', 0, PARAM_INT); //delete recordid
aab98aaf 43
3d4b223a 44 if ($id) {
f9d5371b 45 if (! $cm = get_coursemodule_from_id('data', $id)) {
29c1bb15 46 print_error('invalidcoursemodule');
3d4b223a 47 }
3223bc9e 48 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
29c1bb15 49 print_error('coursemisconf');
3d4b223a 50 }
3223bc9e 51 if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
29c1bb15 52 print_error('invalidcoursemodule');
3d4b223a 53 }
3d45b8e5 54 $record = NULL;
3d4b223a 55
3d45b8e5 56 } else if ($rid) {
3223bc9e 57 if (! $record = $DB->get_record('data_records', array('id'=>$rid))) {
29c1bb15 58 print_error('invalidrecord', 'data');
3d45b8e5 59 }
3223bc9e 60 if (! $data = $DB->get_record('data', array('id'=>$record->dataid))) {
29c1bb15 61 print_error('invalidid', 'data');
3d45b8e5 62 }
3223bc9e 63 if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
29c1bb15 64 print_error('coursemisconf');
3d45b8e5 65 }
66 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
29c1bb15 67 print_error('invalidcoursemodule');
3d45b8e5 68 }
69 } else { // We must have $d
3223bc9e 70 if (! $data = $DB->get_record('data', array('id'=>$d))) {
29c1bb15 71 print_error('invalidid', 'data');
3d4b223a 72 }
3223bc9e 73 if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
29c1bb15 74 print_error('coursemisconf');
3d4b223a 75 }
76 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
29c1bb15 77 print_error('invalidcoursemodule');
3d4b223a 78 }
3d45b8e5 79 $record = NULL;
3d4b223a 80 }
81
7ddda9db 82 require_course_login($course, true, $cm);
aab98aaf 83
dabfd0ed 84 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
20821a12 85 require_capability('mod/data:viewentry', $context);
3d1c33ef 86
3d45b8e5 87/// If we have an empty Database then redirect because this page is useless without data
0468976c 88 if (has_capability('mod/data:managetemplates', $context)) {
3223bc9e 89 if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) { // Brand new database!
5bac6d10 90 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
3d4b223a 91 }
92 }
e0279f63 93
3d45b8e5 94
95/// Check further parameters that set browsing preferences
96 if (!isset($SESSION->dataprefs)) {
97 $SESSION->dataprefs = array();
e0279f63 98 }
3d45b8e5 99 if (!isset($SESSION->dataprefs[$data->id])) {
100 $SESSION->dataprefs[$data->id] = array();
101 $SESSION->dataprefs[$data->id]['search'] = '';
7900ecb0 102 $SESSION->dataprefs[$data->id]['search_array'] = array();
3d45b8e5 103 $SESSION->dataprefs[$data->id]['sort'] = $data->defaultsort;
7900ecb0 104 $SESSION->dataprefs[$data->id]['advanced'] = 0;
3d45b8e5 105 $SESSION->dataprefs[$data->id]['order'] = ($data->defaultsortdir == 0) ? 'ASC' : 'DESC';
3d4b223a 106 }
de8ff581 107
eeeb4f2a 108 // reset advanced form
109 if (!is_null(optional_param('resetadv', null, PARAM_RAW))) {
110 $SESSION->dataprefs[$data->id]['search_array'] = array();
111 // we need the redirect to cleanup the form state properly
112 redirect("view.php?id=$cm->id&amp;mode=$mode&amp;search=&amp;advanced=1");
113 }
114
de8ff581 115 $advanced = optional_param('advanced', -1, PARAM_INT);
116 if ($advanced == -1) {
117 $advanced = $SESSION->dataprefs[$data->id]['advanced'];
118 } else {
119 if (!$advanced) {
120 // explicitly switched to normal mode - discard all advanced search settings
121 $SESSION->dataprefs[$data->id]['search_array'] = array();
122 }
832123e1 123 $SESSION->dataprefs[$data->id]['advanced'] = $advanced;
de8ff581 124 }
125
7900ecb0 126 $search_array = $SESSION->dataprefs[$data->id]['search_array'];
eeeb4f2a 127
7900ecb0 128 if (!empty($advanced)) {
129 $search = '';
69c0a609 130 $vals = array();
3223bc9e 131 $fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
8429163d 132
7900ecb0 133 //Added to ammend paging error. This error would occur when attempting to go from one page of advanced
134 //search results to another. All fields were reset in the page transfer, and there was no way of determining
135 //whether or not the user reset them. This would cause a blank search to execute whenever the user attempted
136 //to see any page of results past the first.
137 //This fix works as follows:
138 //$paging flag is set to false when page 0 of the advanced search results is viewed for the first time.
139 //Viewing any page of results after page 0 passes the false $paging flag though the URL (see line 523) and the
140 //execution falls through to the second condition below, allowing paging to be set to true.
141 //Paging remains true and keeps getting passed though the URL until a new search is performed
142 //(even if page 0 is revisited).
8429163d 143 //A false $paging flag generates advanced search results based on the fields input by the user.
7900ecb0 144 //A true $paging flag generates davanced search results from the $SESSION global.
8429163d 145
7900ecb0 146 $paging = optional_param('paging', NULL, PARAM_BOOL);
147 if($page == 0 && !isset($paging)) {
148 $paging = false;
149 }
150 else {
151 $paging = true;
152 }
714bec74 153 if (!empty($fields)) {
7900ecb0 154 foreach($fields as $field) {
2d9b3ef2 155 $searchfield = data_get_field_from_id($field->id, $data);
7900ecb0 156 //Get field data to build search sql with. If paging is false, get from user.
157 //If paging is true, get data from $search_array which is obtained from the $SESSION (see line 116).
158 if(!$paging) {
159 $val = $searchfield->parse_search_field();
714bec74 160 } else {
7900ecb0 161 //Set value from session if there is a value @ the required index.
714bec74 162 if (isset($search_array[$field->id])) {
7900ecb0 163 $val = $search_array[$field->id]->data;
714bec74 164 } else { //If there is not an entry @ the required index, set value to blank.
7900ecb0 165 $val = '';
166 }
7900ecb0 167 }
714bec74 168 if (!empty($val)) {
169 $search_array[$field->id] = new object();
e3487936 170 list($search_array[$field->id]->sql, $search_array[$field->id]->params) = $searchfield->generate_sql('c'.$field->id, $val);
714bec74 171 $search_array[$field->id]->data = $val;
69c0a609 172 $vals[] = $val;
714bec74 173 } else {
174 // clear it out
175 unset($search_array[$field->id]);
7900ecb0 176 }
177 }
178 }
714bec74 179
180 if (!$paging) {
181 // name searching
182 $fn = optional_param('u_fn', '', PARAM_NOTAGS);
183 $ln = optional_param('u_ln', '', PARAM_NOTAGS);
184 } else {
de8ff581 185 $fn = isset($search_array[DATA_FIRSTNAME]) ? $search_array[DATA_FIRSTNAME]->data : '';
186 $ln = isset($search_array[DATA_LASTNAME]) ? $search_array[DATA_LASTNAME]->data : '';
714bec74 187 }
188 if (!empty($fn)) {
189 $search_array[DATA_FIRSTNAME] = new object();
e3487936 190 $search_array[DATA_FIRSTNAME]->sql = '';
191 $search_array[DATA_FIRSTNAME]->params = array();
192 $search_array[DATA_FIRSTNAME]->field = 'u.firstname';
193 $search_array[DATA_FIRSTNAME]->data = $fn;
69c0a609 194 $vals[] = $fn;
714bec74 195 } else {
196 unset($search_array[DATA_FIRSTNAME]);
197 }
198 if (!empty($ln)) {
199 $search_array[DATA_LASTNAME] = new object();
e3487936 200 $search_array[DATA_LASTNAME]->sql = '';
201 $search_array[DATA_FIRSTNAME]->params = array();
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
d53e5129 225 // Disable search filters if $filter is not true:
32d799c6 226 if (! $filter) {
227 $search = '';
228 }
d53e5129 229
8e1ec6be 230 $textlib = textlib_get_instance();
76a2fd82 231 if ($textlib->strlen($search) < 2) {
232 $search = '';
233 }
3d45b8e5 234 $SESSION->dataprefs[$data->id]['search'] = $search; // Make it sticky
235
236 $sort = optional_param('sort', $SESSION->dataprefs[$data->id]['sort'], PARAM_INT);
237 $SESSION->dataprefs[$data->id]['sort'] = $sort; // Make it sticky
238
239 $order = (optional_param('order', $SESSION->dataprefs[$data->id]['order'], PARAM_ALPHA) == 'ASC') ? 'ASC': 'DESC';
240 $SESSION->dataprefs[$data->id]['order'] = $order; // Make it sticky
241
242
243 $oldperpage = get_user_preferences('data_perpage_'.$data->id, 10);
244 $perpage = optional_param('perpage', $oldperpage, PARAM_INT);
245
246 if ($perpage < 2) {
247 $perpage = 2;
248 }
249 if ($perpage != $oldperpage) {
250 set_user_preference('data_perpage_'.$data->id, $perpage);
251 }
252
3d4b223a 253 add_to_log($course->id, 'data', 'view', "view.php?id=$cm->id", $data->id, $cm->id);
254
255
256// Initialize $PAGE, compute blocks
ad52c04f 257 $PAGE->set_url('mod/data/view.php', array('id' => $cm->id));
3d4b223a 258 $pageblocks = blocks_setup($PAGE);
259 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]), 210);
260
d2b23346 261 if (($edit != -1) and $PAGE->user_allowed_editing()) {
262 $USER->editing = $edit;
3d4b223a 263 }
3d4b223a 264
b0100852 265/// RSS and CSS and JS meta
68635e6f 266 $meta = '';
64452eb4 267 if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
c853304e 268 $rsspath = rss_get_url($course->id, $USER->id, 'data', $data->id);
68635e6f 269 $meta .= '<link rel="alternate" type="application/rss+xml" ';
6ba65fa0 270 $meta .= 'title ="'. format_string($course->shortname) .': %fullname%" href="'.$rsspath.'" />';
68635e6f 271 }
272 if ($data->csstemplate) {
273 $meta .= '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/mod/data/css.php?d='.$data->id.'" /> ';
c853304e 274 }
b0100852 275 if ($data->jstemplate) {
276 $meta .= '<script type="text/javascript" src="'.$CFG->wwwroot.'/mod/data/js.php?d='.$data->id.'"></script>';
277 }
278
c853304e 279/// Print the page header
b7b2d0f3 280 // Note: MDL-19010 there will be further changes to printing header and blocks.
281 // The code will be much nicer than this eventually.
3681f9a9 282 $title = $course->shortname.': ' . format_string($data->name);
283
284 $buttons = '<table><tr><td>'.update_module_button($cm->id, $course->id, get_string('modulename', 'data')).'</td>';
285 if ($PAGE->user_allowed_editing() && !empty($CFG->showblocksonmodpages)) {
286 $buttons .= '<td><form '.$CFG->frametarget.' method="get" action="view.php"><div>'.
287 '<input type="hidden" name="id" value="'.$cm->id.'" />'.
288 '<input type="hidden" name="edit" value="'.($PAGE->user_is_editing()?'off':'on').'" />'.
289 '<input type="submit" value="'.get_string($PAGE->user_is_editing()?'blockseditoff':'blocksediton').'" /></div></form></td>';
290 }
291 $buttons .= '</tr></table>';
aab98aaf 292
3681f9a9 293 $navigation = build_navigation(array(), $cm);
294 print_header($title, $course->fullname, $navigation, '', '', true, $buttons, navmenu($course, $cm));
3d4b223a 295
236d839f 296/// If we have blocks, then print the left side here
297 if (!empty($CFG->showblocksonmodpages)) {
298 echo '<table id="layout-table"><tr>';
299 if ((blocks_have_content($pageblocks, BLOCK_POS_LEFT) || $PAGE->user_is_editing())) {
300 echo '<td style="width: '.$blocks_preferred_width.'px;" id="left-column">';
9f7f1a74 301 print_container_start();
236d839f 302 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
9f7f1a74 303 print_container_end();
236d839f 304 echo '</td>';
305 }
306 echo '<td id="middle-column">';
9f7f1a74 307 print_container_start();
3d4b223a 308 }
309
3b27b0fe 310/// Check to see if groups are being used here
13534ef7
ML
311 $returnurl = 'view.php?d='.$data->id.'&amp;search='.s($search).'&amp;sort='.s($sort).'&amp;order='.s($order).'&amp;';
312 groups_print_activity_menu($cm, $returnurl);
313 $currentgroup = groups_get_activity_group($cm);
314 $groupmode = groups_get_activity_groupmode($cm);
3b27b0fe 315
3d4b223a 316 print_heading(format_string($data->name));
aab98aaf 317
a593aeee 318 // Do we need to show a link to the RSS feed for the records?
64452eb4 319 if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
a593aeee 320 echo '<div style="float:right;">';
ed69c723 321 rss_print_link($course->id, $USER->id, 'data', $data->id, get_string('rsstype'));
a593aeee 322 echo '</div>';
323 echo '<div style="clear:both;"></div>';
324 }
aab98aaf 325
9e08cf6e 326 if ($data->intro and empty($page) and empty($record) and $mode != 'single') {
ea6073bb 327 $options = new object();
328 $options->noclean = true;
b4950d96 329 print_box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro');
56135f6b 330 }
331
473dd288 332/// Delete any requested records
333
046dd7dc 334 if ($delete && confirm_sesskey() && (has_capability('mod/data:manageentries', $context) or data_isowner($delete))) {
3d45b8e5 335 if ($confirm = optional_param('confirm',0,PARAM_INT)) {
3223bc9e 336 if ($deleterecord = $DB->get_record('data_records', array('id'=>$delete))) { // Need to check this is valid
3d45b8e5 337 if ($deleterecord->dataid == $data->id) { // Must be from this database
3223bc9e 338 if ($contents = $DB->get_records('data_content', array('recordid'=>$deleterecord->id))) {
3d45b8e5 339 foreach ($contents as $content) { // Delete files or whatever else this field allows
340 if ($field = data_get_field_from_id($content->fieldid, $data)) { // Might not be there
341 $field->delete_content($content->recordid);
342 }
3d4b223a 343 }
344 }
3223bc9e 345 $DB->delete_records('data_content', array('recordid'=>$deleterecord->id));
346 $DB->delete_records('data_records', array('id'=>$deleterecord->id));
f0497d6f 347
3d45b8e5 348 add_to_log($course->id, 'data', 'record delete', "view.php?id=$cm->id", $data->id, $cm->id);
f0497d6f 349
3d45b8e5 350 notify(get_string('recorddeleted','data'), 'notifysuccess');
351 }
3d4b223a 352 }
3d45b8e5 353
354 } else { // Print a confirmation page
3223bc9e 355 if ($deleterecord = $DB->get_record('data_records', array('id'=>$delete))) { // Need to check this is valid
64452eb4 356 if ($deleterecord->dataid == $data->id) { // Must be from this database
aab98aaf 357 notice_yesno(get_string('confirmdeleterecord','data'),
64452eb4 358 'view.php?d='.$data->id.'&amp;delete='.$delete.'&amp;confirm=1&amp;sesskey='.sesskey(),
359 'view.php?d='.$data->id);
3d45b8e5 360
64452eb4 361 $records[] = $deleterecord;
3f9672d3 362 echo data_print_template('singletemplate', $records, $data, '', 0, true);
64452eb4 363
364 print_footer($course);
365 exit;
366 }
367 }
368 }
369 }
370
371
372
373/// Print the tabs
374
375 if ($record or $mode == 'single') {
376 $currenttab = 'single';
7900ecb0 377 } elseif($mode == 'asearch') {
378 $currenttab = 'asearch';
379 }
380 else {
64452eb4 381 $currenttab = 'list';
382 }
aab98aaf 383 include('tabs.php');
64452eb4 384
eeeb4f2a 385 if ($mode == 'asearch') {
386 $maxcount = 0;
8429163d 387
eeeb4f2a 388 } else {
7900ecb0 389 /// Approve any requested records
e3487936 390 $params = array(); // named params array
64452eb4 391
8429163d 392 $approvecap = has_capability('mod/data:approve', $context);
bb5740f4 393
394 if ($approve && confirm_sesskey() && $approvecap) {
3223bc9e 395 if ($approverecord = $DB->get_record('data_records', array('id'=>$approve))) { // Need to check this is valid
7900ecb0 396 if ($approverecord->dataid == $data->id) { // Must be from this database
397 $newrecord->id = $approverecord->id;
398 $newrecord->approved = 1;
3223bc9e 399 if ($DB->update_record('data_records', $newrecord)) {
7900ecb0 400 notify(get_string('recordapproved','data'), 'notifysuccess');
401 }
64452eb4 402 }
403 }
3d4b223a 404 }
8429163d 405
c861f079 406 $numentries = data_numentries($data);
407 /// Check the number of entries required against the number of entries already made (doesn't apply to teachers)
eeeb4f2a 408 if ($data->requiredentries > 0 && $numentries < $data->requiredentries && !has_capability('mod/data:manageentries', $context)) {
409 $data->entriesleft = $data->requiredentries - $numentries;
410 $strentrieslefttoadd = get_string('entrieslefttoadd', 'data', $data);
411 notify($strentrieslefttoadd);
c861f079 412 }
413
414 /// Check the number of entries required before to view other participant's entries against the number of entries already made (doesn't apply to teachers)
415 $requiredentries_allowed = true;
416 if ($data->requiredentriestoview > 0 && $numentries < $data->requiredentriestoview && !has_capability('mod/data:manageentries', $context)) {
417 $data->entrieslefttoview = $data->requiredentriestoview - $numentries;
418 $strentrieslefttoaddtoview = get_string('entrieslefttoaddtoview', 'data', $data);
419 notify($strentrieslefttoaddtoview);
eeeb4f2a 420 $requiredentries_allowed = false;
421 }
3d4b223a 422
bb5740f4 423 /// setup group and approve restrictions
424 if (!$approvecap && $data->approval) {
7900ecb0 425 if (isloggedin()) {
e3487936 426 $approveselect = ' AND (r.approved=1 OR r.userid=:myid1) ';
427 $params['myid1'] = $USER->id;
7900ecb0 428 } else {
429 $approveselect = ' AND r.approved=1 ';
430 }
4431d2e0 431 } else {
7900ecb0 432 $approveselect = ' ';
4431d2e0 433 }
3d4b223a 434
7900ecb0 435 if ($currentgroup) {
e3487936 436 $groupselect = " AND (r.groupid = :currentgroup OR r.groupid = 0)";
437 $params['currentgroup'] = $currentgroup;
64452eb4 438 } else {
7900ecb0 439 $groupselect = ' ';
64452eb4 440 }
3d45b8e5 441
245ac557 442 $ilike = $DB->sql_ilike(); //Be case-insensitive
026d562e 443
7900ecb0 444 /// Find the field we are sorting on
3239b010 445 if ($sort <= 0 or !$sortfield = data_get_field_from_id($sort, $data)) {
bb5740f4 446
447 switch ($sort) {
448 case DATA_LASTNAME:
449 $ordering = "u.lastname $order, u.firstname $order";
450 break;
451 case DATA_FIRSTNAME:
452 $ordering = "u.firstname $order, u.lastname $order";
453 break;
454 case DATA_APPROVED:
455 $ordering = "r.approved $order, r.timecreated $order";
456 break;
3239b010 457 case DATA_TIMEMODIFIED:
458 $ordering = "r.timemodified $order";
459 break;
460 case DATA_TIMEADDED:
bb5740f4 461 default:
3239b010 462 $sort = 0;
bb5740f4 463 $ordering = "r.timecreated $order";
714bec74 464 }
465
466 $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname';
467 $count = ' COUNT(DISTINCT c.recordid) ';
e3487936 468 $tables = '{data_content} c,{data_records} r, {data_content} cs, {user} u ';
714bec74 469 $where = 'WHERE c.recordid = r.id
e3487936 470 AND r.dataid = :dataid
714bec74 471 AND r.userid = u.id
472 AND cs.recordid = r.id ';
e3487936 473 $params['dataid'] = $data->id;
714bec74 474 $sortorder = ' ORDER BY '.$ordering.', r.id ASC ';
475 $searchselect = '';
476
477 // If requiredentries is not reached, only show current user's entries
478 if (!$requiredentries_allowed) {
e3487936 479 $where .= ' AND u.id = :myid2 ';
480 $params['myid2'] = $USER->id;
714bec74 481 }
482
483 if (!empty($advanced)) { //If advanced box is checked.
e3487936 484 $i = 0;
714bec74 485 foreach($search_array as $key => $val) { //what does $search_array hold?
486 if ($key == DATA_FIRSTNAME or $key == DATA_LASTNAME) {
e3487936 487 $i++;
488 $searchselect .= " AND $val->field $ilike :search_flname_$i";
489 $params['search_flname_'.$i] = "%$val->data%";
714bec74 490 continue;
491 }
e3487936 492 $tables .= ', {data_content} c'.$key.' ';
714bec74 493 $where .= ' AND c'.$key.'.recordid = r.id';
494 $searchselect .= ' AND ('.$val->sql.') ';
e3487936 495 $params = array_merge($params, $val->params);
714bec74 496 }
497 } else if ($search) {
e3487936 498 $searchselect = " AND (cs.content $ilike :search1 OR u.firstname $ilike :search2 OR u.lastname $ilike :search3 ) ";
499 $params['search1'] = "%$search%";
500 $params['search2'] = "%$search%";
501 $params['search3'] = "%$search%";
714bec74 502 } else {
503 $searchselect = ' ';
504 }
505
3239b010 506 } else {
7900ecb0 507
508 $sortcontent = $sortfield->get_sort_field();
509 $sortcontentfull = $sortfield->get_sort_sql('c.'.$sortcontent);
714bec74 510
aa4c6b68 511 $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname, '.$DB->sql_compare_text($sortcontentfull).' AS _order ';
7900ecb0 512 $count = ' COUNT(DISTINCT c.recordid) ';
e3487936 513 $tables = '{data_content} c, {data_records} r, {data_content} cs, {user} u ';
7900ecb0 514 $where = 'WHERE c.recordid = r.id
e3487936 515 AND c.fieldid = :sort
516 AND r.dataid = :dataid
7900ecb0 517 AND r.userid = u.id
714bec74 518 AND cs.recordid = r.id ';
e3487936 519 $params['dataid'] = $data->id;
520 $params['sort'] = $sort;
b99b25ea 521 $sortorder = ' ORDER BY _order '.$order.' , r.id ASC ';
7900ecb0 522 $searchselect = '';
714bec74 523
87518137 524 // If requiredentries is not reached, only show current user's entries
525 if (!$requiredentries_allowed) {
526 $where .= ' AND u.id = ' . $USER->id;
e3487936 527 $params['myid2'] = $USER->id;
87518137 528 }
714bec74 529
530 if (!empty($advanced)) { //If advanced box is checked.
531 foreach($search_array as $key => $val) { //what does $search_array hold?
532 if ($key == DATA_FIRSTNAME or $key == DATA_LASTNAME) {
e3487936 533 $i++;
534 $searchselect .= " AND $val->field $ilike :search_flname_$i";
535 $params['search_flname_'.$i] = "%$val->data%";
714bec74 536 continue;
537 }
e3487936 538 $tables .= ', {data_content} c'.$key.' ';
4ea69f88 539 $where .= ' AND c'.$key.'.recordid = r.id AND c'.$key.'.fieldid = '.$key;
7900ecb0 540 $searchselect .= ' AND ('.$val->sql.') ';
e3487936 541 $params = array_merge($params, $val->params);
7900ecb0 542 }
714bec74 543 } else if ($search) {
e3487936 544 $searchselect = " AND (cs.content $ilike :search1 OR u.firstname $ilike :search2 OR u.lastname $ilike :search3 ) ";
545 $params['search1'] = "%$search%";
546 $params['search2'] = "%$search%";
547 $params['search3'] = "%$search%";
7900ecb0 548 } else {
549 $searchselect = ' ';
714bec74 550 }
7900ecb0 551 }
3d4b223a 552
7900ecb0 553 /// To actually fetch the records
3d45b8e5 554
eeeb4f2a 555 $fromsql = "FROM $tables $where $groupselect $approveselect $searchselect";
556 $sqlselect = "SELECT $what $fromsql $sortorder";
557 $sqlcount = "SELECT $count $fromsql"; // Total number of records when searching
029cf846 558 $sqlrids = "SELECT tmp.id FROM ($sqlselect) tmp";
eeeb4f2a 559 $sqlmax = "SELECT $count FROM $tables $where $groupselect $approveselect"; // number of all recoirds user may see
3d45b8e5 560
eeeb4f2a 561 /// Work out the paging numbers and counts
3d45b8e5 562
e3487936 563 $totalcount = $DB->count_records_sql($sqlcount, $params);
eeeb4f2a 564 if (empty($searchselect)) {
565 $maxcount = $totalcount;
566 } else {
e3487936 567 $maxcount = $DB->count_records_sql($sqlmax, $params);
eeeb4f2a 568 }
3d4b223a 569
7900ecb0 570 if ($record) { // We need to just show one, so where is it in context?
571 $nowperpage = 1;
572 $mode = 'single';
de89899b 573
7900ecb0 574 $page = 0;
e3487936 575 if ($allrecordids = $DB->get_records_sql($sqlrids, $params)) {
029cf846 576 $allrecordids = array_keys($allrecordids);
577 $page = (int)array_search($record->id, $allrecordids);
578 unset($allrecordids);
3d45b8e5 579 }
3d45b8e5 580
7900ecb0 581 } else if ($mode == 'single') { // We rely on ambient $page settings
582 $nowperpage = 1;
3d45b8e5 583
7900ecb0 584 } else {
585 $nowperpage = $perpage;
586 }
3d45b8e5 587
7900ecb0 588 /// Get the actual records
8429163d 589
e3487936 590 if (!$records = $DB->get_records_sql($sqlselect, $params, $page * $nowperpage, $nowperpage)) {
eeeb4f2a 591 // Nothing to show!
7900ecb0 592 if ($record) { // Something was requested so try to show that at least (bug 5132)
593 if (has_capability('mod/data:manageentries', $context) || empty($data->approval) ||
594 $record->approved || (isloggedin() && $record->userid == $USER->id)) {
595 if (!$currentgroup || $record->groupid == $currentgroup || $record->groupid == 0) {
eeeb4f2a 596 // OK, we can show this one
597 $records = array($record->id => $record);
598 $totalcount = 1;
7900ecb0 599 }
3dec563c 600 }
601 }
eeeb4f2a 602 }
603
604 if (empty($records)) {
605 if ($maxcount){
606 $a = new object();
607 $a->max = $maxcount;
608 $a->reseturl = "view.php?id=$cm->id&amp;mode=$mode&amp;search=&amp;advanced=0";
609 notify(get_string('foundnorecords','data', $a));
7900ecb0 610 } else {
611 notify(get_string('norecords','data'));
612 }
cf3e199b 613
eeeb4f2a 614 } else { // We have some records to print
615
616 if ($maxcount != $totalcount) {
617 $a = new object();
618 $a->num = $totalcount;
619 $a->max = $maxcount;
620 $a->reseturl = "view.php?id=$cm->id&amp;mode=$mode&amp;search=&amp;advanced=0";
8429163d 621 notify(get_string('foundrecords', 'data', $a), 'notifysuccess');
eeeb4f2a 622 }
68c88622 623
7900ecb0 624 if ($mode == 'single') { // Single template
d53e5129 625 $baseurl = 'view.php?d=' . $data->id . '&amp;mode=single&amp;';
2d9b3ef2 626 if (!empty($search)) {
627 $baseurl .= 'filter=1&amp;';
628 }
7900ecb0 629 print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
3d45b8e5 630
7900ecb0 631 if (empty($data->singletemplate)){
632 notify(get_string('nosingletemplate','data'));
633 data_generate_default_template($data, 'singletemplate', 0, false, false);
634 }
3d4b223a 635
7900ecb0 636 data_print_template('singletemplate', $records, $data, $search, $page);
3d45b8e5 637
7900ecb0 638 print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
3d45b8e5 639
7900ecb0 640 } else { // List template
641 $baseurl = 'view.php?d='.$data->id.'&amp;';
642 //send the advanced flag through the URL so it is remembered while paging.
643 $baseurl .= 'advanced='.$advanced.'&amp;';
2d9b3ef2 644 if (!empty($search)) {
645 $baseurl .= 'filter=1&amp;';
646 }
7900ecb0 647 //pass variable to allow determining whether or not we are paging through results.
648 $baseurl .= 'paging='.$paging.'&amp;';
3d45b8e5 649
7900ecb0 650 print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
3d45b8e5 651
7900ecb0 652 if (empty($data->listtemplate)){
653 notify(get_string('nolisttemplate','data'));
654 data_generate_default_template($data, 'listtemplate', 0, false, false);
655 }
656 echo $data->listtemplateheader;
657 data_print_template('listtemplate', $records, $data, $search, $page);
658 echo $data->listtemplatefooter;
659
660 print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
3d45b8e5 661 }
3d45b8e5 662
3d45b8e5 663 }
7900ecb0 664 }
8429163d 665
7900ecb0 666 $search = trim($search);
667 if (empty($records)) {
668 $records = array();
f852d652 669 }
670
7900ecb0 671 //Advanced search form doesn't make sense for single (redirects list view)
eeeb4f2a 672 if (($maxcount || $mode == 'asearch') && $mode != 'single') {
7900ecb0 673 data_print_preference_form($data, $perpage, $search, $sort, $order, $search_array, $advanced, $mode);
64452eb4 674 }
3d4b223a 675
236d839f 676/// If we have blocks, then print the left side here
677 if (!empty($CFG->showblocksonmodpages)) {
9f7f1a74 678 print_container_end();
236d839f 679 echo '</td>'; // Middle column
680 if ((blocks_have_content($pageblocks, BLOCK_POS_RIGHT) || $PAGE->user_is_editing())) {
681 echo '<td style="width: '.$blocks_preferred_width.'px;" id="right-column">';
9f7f1a74 682 print_container_start();
236d839f 683 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT);
9f7f1a74 684 print_container_end();
236d839f 685 echo '</td>';
686 }
78e02af9 687 echo '</tr></table>';
236d839f 688 }
689
3d45b8e5 690 print_footer($course);
18a2a0cb 691
692/// Mark as viewed
693 $completion=new completion_info($course);
694 $completion->set_module_viewed($cm);
0997e51a 695?>