2 ///////////////////////////////////////////////////////////////////////////
4 // NOTICE OF COPYRIGHT //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // http://moodle.org //
9 // Copyright (C) 2005 Martin Dougiamas http://dougiamas.com //
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. //
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: //
21 // http://www.gnu.org/copyleft/gpl.html //
23 ///////////////////////////////////////////////////////////////////////////
25 require_once('../../config.php');
26 require_once('lib.php');
27 require_once($CFG->libdir
.'/blocklib.php');
28 require_once("$CFG->libdir/rsslib.php");
30 require_once('pagelib.php');
32 /// One of these is necessary!
33 $id = optional_param('id', 0, PARAM_INT
); // course module id
34 $d = optional_param('d', 0, PARAM_INT
); // database id
35 $rid = optional_param('rid', 0, PARAM_INT
); //record id
37 $mode = optional_param('mode', '', PARAM_ALPHA
); // Force the browse mode ('single')
39 $edit = optional_param('edit', -1, PARAM_BOOL
);
40 $page = optional_param('page', 0, PARAM_INT
);
41 /// These can be added to perform an action on a record
42 $approve = optional_param('approve', 0, PARAM_INT
); //approval recordid
43 $delete = optional_param('delete', 0, PARAM_INT
); //delete recordid
46 if (! $cm = get_coursemodule_from_id('data', $id)) {
47 error('Course Module ID was incorrect');
49 if (! $course = get_record('course', 'id', $cm->course
)) {
50 error('Course is misconfigured');
52 if (! $data = get_record('data', 'id', $cm->instance
)) {
53 error('Course module is incorrect');
58 if (! $record = get_record('data_records', 'id', $rid)) {
59 error('Record ID is incorrect');
61 if (! $data = get_record('data', 'id', $record->dataid
)) {
62 error('Data ID is incorrect');
64 if (! $course = get_record('course', 'id', $data->course
)) {
65 error('Course is misconfigured');
67 if (! $cm = get_coursemodule_from_instance('data', $data->id
, $course->id
)) {
68 error('Course Module ID was incorrect');
70 } else { // We must have $d
71 if (! $data = get_record('data', 'id', $d)) {
72 error('Data ID is incorrect');
74 if (! $course = get_record('course', 'id', $data->course
)) {
75 error('Course is misconfigured');
77 if (! $cm = get_coursemodule_from_instance('data', $data->id
, $course->id
)) {
78 error('Course Module ID was incorrect');
83 require_course_login($course, true, $cm);
85 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
86 require_capability('mod/data:viewentry', $context);
88 /// If we have an empty Database then redirect because this page is useless without data
89 if (has_capability('mod/data:managetemplates', $context)) {
90 if (!record_exists('data_fields','dataid',$data->id
)) { // Brand new database!
91 redirect($CFG->wwwroot
.'/mod/data/field.php?d='.$data->id
); // Redirect to field entry
96 /// Check further parameters that set browsing preferences
97 if (!isset($SESSION->dataprefs
)) {
98 $SESSION->dataprefs
= array();
100 if (!isset($SESSION->dataprefs
[$data->id
])) {
101 $SESSION->dataprefs
[$data->id
] = array();
102 $SESSION->dataprefs
[$data->id
]['search'] = '';
103 $SESSION->dataprefs
[$data->id
]['search_array'] = array();
104 $SESSION->dataprefs
[$data->id
]['sort'] = $data->defaultsort
;
105 $SESSION->dataprefs
[$data->id
]['advanced'] = 0;
106 $SESSION->dataprefs
[$data->id
]['order'] = ($data->defaultsortdir
== 0) ?
'ASC' : 'DESC';
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&mode=$mode&search=&advanced=1");
116 $advanced = optional_param('advanced', -1, PARAM_INT
);
117 if ($advanced == -1) {
118 $advanced = $SESSION->dataprefs
[$data->id
]['advanced'];
121 // explicitly switched to normal mode - discard all advanced search settings
122 $SESSION->dataprefs
[$data->id
]['search_array'] = array();
124 $SESSION->dataprefs
[$data->id
]['advanced'] = $advanced;
127 $search_array = $SESSION->dataprefs
[$data->id
]['search_array'];
129 if (!empty($advanced)) {
132 $fields = get_records('data_fields', 'dataid', $data->id
);
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)
148 $paging = optional_param('paging', NULL, PARAM_BOOL
);
149 if($page == 0 && !isset($paging)) {
155 if (!empty($fields)) {
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).
161 $val = $searchfield->parse_search_field();
163 //Set value from session if there is a value @ the required index.
164 if (isset($search_array[$field->id
])) {
165 $val = $search_array[$field->id
]->data
;
166 } else { //If there is not an entry @ the required index, set value to blank.
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;
177 unset($search_array[$field->id
]);
184 $fn = optional_param('u_fn', '', PARAM_NOTAGS
);
185 $ln = optional_param('u_ln', '', PARAM_NOTAGS
);
187 $fn = isset($search_array[DATA_FIRSTNAME
]) ?
$search_array[DATA_FIRSTNAME
]->data
: '';
188 $ln = isset($search_array[DATA_LASTNAME
]) ?
$search_array[DATA_LASTNAME
]->data
: '';
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;
197 unset($search_array[DATA_FIRSTNAME
]);
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;
206 unset($search_array[DATA_LASTNAME
]);
209 $SESSION->dataprefs
[$data->id
]['search_array'] = $search_array; // Make it sticky
211 // in case we want to switch to simple search later - there might be multiple values there ;-)
214 if (is_string($val)) {
220 $search = optional_param('search', $SESSION->dataprefs
[$data->id
]['search'], PARAM_NOTAGS
);
221 //Paging variable not used for standard search. Set it to null.
225 $textlib = textlib_get_instance();
226 if ($textlib->strlen($search) < 2) {
229 $SESSION->dataprefs
[$data->id
]['search'] = $search; // Make it sticky
231 $sort = optional_param('sort', $SESSION->dataprefs
[$data->id
]['sort'], PARAM_INT
);
232 $SESSION->dataprefs
[$data->id
]['sort'] = $sort; // Make it sticky
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
238 $oldperpage = get_user_preferences('data_perpage_'.$data->id
, 10);
239 $perpage = optional_param('perpage', $oldperpage, PARAM_INT
);
244 if ($perpage != $oldperpage) {
245 set_user_preference('data_perpage_'.$data->id
, $perpage);
248 add_to_log($course->id
, 'data', 'view', "view.php?id=$cm->id", $data->id
, $cm->id
);
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);
256 if (($edit != -1) and $PAGE->user_allowed_editing()) {
257 $USER->editing
= $edit;
260 /// RSS and CSS and JS meta
262 if (!empty($CFG->enablerssfeeds
) && !empty($CFG->data_enablerssfeeds
) && $data->rssarticles
> 0) {
263 $rsspath = rss_get_url($course->id
, $USER->id
, 'data', $data->id
);
264 $meta .= '<link rel="alternate" type="application/rss+xml" ';
265 $meta .= 'title ="'. format_string($course->shortname
) .': %fullname%" href="'.$rsspath.'" />';
267 if ($data->csstemplate
) {
268 $meta .= '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot
.'/mod/data/css.php?d='.$data->id
.'" /> ';
270 if ($data->jstemplate
) {
271 $meta .= '<script type="text/javascript" src="'.$CFG->wwwroot
.'/mod/data/js.php?d='.$data->id
.'"></script>';
275 /// Print the page header
276 $PAGE->print_header($course->shortname
.': %fullname%', '', $meta);
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">';
284 print_container_start();
285 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT
);
286 print_container_end();
289 echo '<td id="middle-column">';
290 print_container_start();
293 /// Check to see if groups are being used here
294 $returnurl = 'view.php?d='.$data->id
.'&search='.s($search).'&sort='.s($sort).'&order='.s($order).'&';
295 groups_print_activity_menu($cm, $returnurl);
296 $currentgroup = groups_get_activity_group($cm);
297 $groupmode = groups_get_activity_groupmode($cm);
299 print_heading(format_string($data->name
));
301 // Do we need to show a link to the RSS feed for the records?
302 if (!empty($CFG->enablerssfeeds
) && !empty($CFG->data_enablerssfeeds
) && $data->rssarticles
> 0) {
303 echo '<div style="float:right;">';
304 rss_print_link($course->id
, $USER->id
, 'data', $data->id
, get_string('rsstype'));
306 echo '<div style="clear:both;"></div>';
309 if ($data->intro
and empty($page) and empty($record) and $mode != 'single') {
310 $options = new object();
311 $options->noclean
= true;
312 print_box(format_text($data->intro
, FORMAT_MOODLE
, $options), 'generalbox', 'intro');
315 /// Delete any requested records
317 if ($delete && confirm_sesskey() && (has_capability('mod/data:manageentries', $context) or data_isowner($delete))) {
318 if ($confirm = optional_param('confirm',0,PARAM_INT
)) {
319 if ($deleterecord = get_record('data_records', 'id', $delete)) { // Need to check this is valid
320 if ($deleterecord->dataid
== $data->id
) { // Must be from this database
321 if ($contents = get_records('data_content','recordid', $deleterecord->id
)) {
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
);
328 delete_records('data_content','recordid', $deleterecord->id
);
329 delete_records('data_records','id', $deleterecord->id
);
331 add_to_log($course->id
, 'data', 'record delete', "view.php?id=$cm->id", $data->id
, $cm->id
);
333 notify(get_string('recorddeleted','data'), 'notifysuccess');
337 } else { // Print a confirmation page
338 if ($deleterecord = get_record('data_records', 'id', $delete)) { // Need to check this is valid
339 if ($deleterecord->dataid
== $data->id
) { // Must be from this database
340 notice_yesno(get_string('confirmdeleterecord','data'),
341 'view.php?d='.$data->id
.'&delete='.$delete.'&confirm=1&sesskey='.sesskey(),
342 'view.php?d='.$data->id
);
344 $records[] = $deleterecord;
345 echo data_print_template('singletemplate', $records, $data, '', 0, true);
347 print_footer($course);
358 if ($record or $mode == 'single') {
359 $currenttab = 'single';
360 } elseif($mode == 'asearch') {
361 $currenttab = 'asearch';
364 $currenttab = 'list';
368 if ($mode == 'asearch') {
372 /// Approve any requested records
374 $approvecap = has_capability('mod/data:approve', $context);
376 if ($approve && confirm_sesskey() && $approvecap) {
377 if ($approverecord = get_record('data_records', 'id', $approve)) { // Need to check this is valid
378 if ($approverecord->dataid
== $data->id
) { // Must be from this database
379 $newrecord->id
= $approverecord->id
;
380 $newrecord->approved
= 1;
381 if (update_record('data_records', $newrecord)) {
382 notify(get_string('recordapproved','data'), 'notifysuccess');
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;
398 /// setup group and approve restrictions
399 if (!$approvecap && $data->approval
) {
401 $approveselect = ' AND (r.approved=1 OR r.userid='.$USER->id
.') ';
403 $approveselect = ' AND r.approved=1 ';
406 $approveselect = ' ';
410 $groupselect = " AND (r.groupid = '$currentgroup' OR r.groupid = 0)";
415 $ilike = sql_ilike(); //Be case-insensitive
417 /// Find the field we are sorting on
418 if ($sort <= 0 or !$sortfield = data_get_field_from_id($sort, $data)) {
422 $ordering = "u.lastname $order, u.firstname $order";
425 $ordering = "u.firstname $order, u.lastname $order";
428 $ordering = "r.approved $order, r.timecreated $order";
430 case DATA_TIMEMODIFIED
:
431 $ordering = "r.timemodified $order";
436 $ordering = "r.timecreated $order";
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
.'
445 AND cs.recordid = r.id ';
446 $sortorder = ' ORDER BY '.$ordering.', r.id ASC ';
449 // If requiredentries is not reached, only show current user's entries
450 if (!$requiredentries_allowed) {
451 $where .= ' AND u.id = ' . $USER->id
;
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
) {
457 $searchselect .= " AND $val->field $ilike '%{$val->data}%'";
460 $tables .= ', '.$CFG->prefix
.'data_content c'.$key.' ';
461 $where .= ' AND c'.$key.'.recordid = r.id';
462 $searchselect .= ' AND ('.$val->sql
.') ';
464 } else if ($search) {
465 $searchselect = " AND (cs.content $ilike '%$search%' OR u.firstname $ilike '%$search%' OR u.lastname $ilike '%$search%' ) ";
472 $sortcontent = $sortfield->get_sort_field();
473 $sortcontentfull = $sortfield->get_sort_sql('c.'.$sortcontent);
475 $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname, c.'.$sortcontent.', '.$sortcontentfull.' AS _order ';
476 $count = ' COUNT(DISTINCT c.recordid) ';
477 $tables = $CFG->prefix
.'data_content c,'.$CFG->prefix
.'data_records r,'.$CFG->prefix
.'data_content cs, '.$CFG->prefix
.'user u ';
478 $where = 'WHERE c.recordid = r.id
479 AND c.fieldid = '.$sort.'
480 AND r.dataid = '.$data->id
.'
482 AND cs.recordid = r.id ';
483 $sortorder = ' ORDER BY _order '.$order.' , r.id ASC ';
486 // If requiredentries is not reached, only show current user's entries
487 if (!$requiredentries_allowed) {
488 $where .= ' AND u.id = ' . $USER->id
;
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
) {
494 $searchselect .= " AND $val->field $ilike '%{$val->data}%'";
497 $tables .= ', '.$CFG->prefix
.'data_content c'.$key.' ';
498 $where .= ' AND c'.$key.'.recordid = r.id AND c'.$key.'.fieldid = '.$key;
499 $searchselect .= ' AND ('.$val->sql
.') ';
501 } else if ($search) {
502 $searchselect = " AND (cs.content $ilike '%$search%' OR u.firstname $ilike '%$search%' OR u.lastname $ilike '%$search%' ) ";
508 /// To actually fetch the records
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
513 $sqlrids = "SELECT tmp.id FROM ($sqlselect) tmp";
514 $sqlmax = "SELECT $count FROM $tables $where $groupselect $approveselect"; // number of all recoirds user may see
516 /// Work out the paging numbers and counts
518 $totalcount = count_records_sql($sqlcount);
519 if (empty($searchselect)) {
520 $maxcount = $totalcount;
522 $maxcount = count_records_sql($sqlmax);
525 if ($record) { // We need to just show one, so where is it in context?
530 if ($allrecordids = get_records_sql($sqlrids)) {
531 $allrecordids = array_keys($allrecordids);
532 $page = (int)array_search($record->id
, $allrecordids);
533 unset($allrecordids);
536 } else if ($mode == 'single') { // We rely on ambient $page settings
540 $nowperpage = $perpage;
543 /// Get the actual records
545 if (!$records = get_records_sql($sqlselect, $page * $nowperpage, $nowperpage)) {
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) {
551 // OK, we can show this one
552 $records = array($record->id
=> $record);
559 if (empty($records)) {
563 $a->reseturl
= "view.php?id=$cm->id&mode=$mode&search=&advanced=0";
564 notify(get_string('foundnorecords','data', $a));
566 notify(get_string('norecords','data'));
569 } else { // We have some records to print
571 if ($maxcount != $totalcount) {
573 $a->num
= $totalcount;
575 $a->reseturl
= "view.php?id=$cm->id&mode=$mode&search=&advanced=0";
576 notify(get_string('foundrecords', 'data', $a), 'notifysuccess');
579 if ($mode == 'single') { // Single template
580 $baseurl = 'view.php?d='.$data->id
.'&mode=single&';
582 print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
584 if (empty($data->singletemplate
)){
585 notify(get_string('nosingletemplate','data'));
586 data_generate_default_template($data, 'singletemplate', 0, false, false);
589 data_print_template('singletemplate', $records, $data, $search, $page);
591 print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
593 } else { // List template
594 $baseurl = 'view.php?d='.$data->id
.'&';
595 //send the advanced flag through the URL so it is remembered while paging.
596 $baseurl .= 'advanced='.$advanced.'&';
597 //pass variable to allow determining whether or not we are paging through results.
598 $baseurl .= 'paging='.$paging.'&';
600 print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
602 if (empty($data->listtemplate
)){
603 notify(get_string('nolisttemplate','data'));
604 data_generate_default_template($data, 'listtemplate', 0, false, false);
606 echo $data->listtemplateheader
;
607 data_print_template('listtemplate', $records, $data, $search, $page);
608 echo $data->listtemplatefooter
;
610 print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
616 $search = trim($search);
617 if (empty($records)) {
621 //Advanced search form doesn't make sense for single (redirects list view)
622 if (($maxcount ||
$mode == 'asearch') && $mode != 'single') {
623 data_print_preference_form($data, $perpage, $search, $sort, $order, $search_array, $advanced, $mode);
626 /// If we have blocks, then print the left side here
627 if (!empty($CFG->showblocksonmodpages
)) {
628 print_container_end();
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">';
632 print_container_start();
633 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT
);
634 print_container_end();
637 echo '</tr></table>';
640 print_footer($course);