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
36 $mode = optional_param('mode', '', PARAM_ALPHA
); // Force the browse mode ('single')
37 $filter = optional_param('filter', 0, PARAM_BOOL
);
38 // search filter will only be applied when $filter is true
40 $edit = optional_param('edit', -1, PARAM_BOOL
);
41 $page = optional_param('page', 0, PARAM_INT
);
42 /// These can be added to perform an action on a record
43 $approve = optional_param('approve', 0, PARAM_INT
); //approval recordid
44 $delete = optional_param('delete', 0, PARAM_INT
); //delete recordid
47 if (! $cm = get_coursemodule_from_id('data', $id)) {
48 error('Course Module ID was incorrect');
50 if (! $course = get_record('course', 'id', $cm->course
)) {
51 error('Course is misconfigured');
53 if (! $data = get_record('data', 'id', $cm->instance
)) {
54 error('Course module is incorrect');
59 if (! $record = get_record('data_records', 'id', $rid)) {
60 error('Record ID is incorrect');
62 if (! $data = get_record('data', 'id', $record->dataid
)) {
63 error('Data ID is incorrect');
65 if (! $course = get_record('course', 'id', $data->course
)) {
66 error('Course is misconfigured');
68 if (! $cm = get_coursemodule_from_instance('data', $data->id
, $course->id
)) {
69 error('Course Module ID was incorrect');
71 } else { // We must have $d
72 if (! $data = get_record('data', 'id', $d)) {
73 error('Data ID is incorrect');
75 if (! $course = get_record('course', 'id', $data->course
)) {
76 error('Course is misconfigured');
78 if (! $cm = get_coursemodule_from_instance('data', $data->id
, $course->id
)) {
79 error('Course Module ID was incorrect');
84 require_course_login($course, true, $cm);
86 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
87 require_capability('mod/data:viewentry', $context);
89 /// If we have an empty Database then redirect because this page is useless without data
90 if (has_capability('mod/data:managetemplates', $context)) {
91 if (!record_exists('data_fields','dataid',$data->id
)) { // Brand new database!
92 redirect($CFG->wwwroot
.'/mod/data/field.php?d='.$data->id
); // Redirect to field entry
97 /// Check further parameters that set browsing preferences
98 if (!isset($SESSION->dataprefs
)) {
99 $SESSION->dataprefs
= array();
101 if (!isset($SESSION->dataprefs
[$data->id
])) {
102 $SESSION->dataprefs
[$data->id
] = array();
103 $SESSION->dataprefs
[$data->id
]['search'] = '';
104 $SESSION->dataprefs
[$data->id
]['search_array'] = array();
105 $SESSION->dataprefs
[$data->id
]['sort'] = $data->defaultsort
;
106 $SESSION->dataprefs
[$data->id
]['advanced'] = 0;
107 $SESSION->dataprefs
[$data->id
]['order'] = ($data->defaultsortdir
== 0) ?
'ASC' : 'DESC';
110 // reset advanced form
111 if (!is_null(optional_param('resetadv', null, PARAM_RAW
))) {
112 $SESSION->dataprefs
[$data->id
]['search_array'] = array();
113 // we need the redirect to cleanup the form state properly
114 redirect("view.php?id=$cm->id&mode=$mode&search=&advanced=1");
117 $advanced = optional_param('advanced', -1, PARAM_INT
);
118 if ($advanced == -1) {
119 $advanced = $SESSION->dataprefs
[$data->id
]['advanced'];
122 // explicitly switched to normal mode - discard all advanced search settings
123 $SESSION->dataprefs
[$data->id
]['search_array'] = array();
125 $SESSION->dataprefs
[$data->id
]['advanced'] = $advanced;
128 $search_array = $SESSION->dataprefs
[$data->id
]['search_array'];
130 if (!empty($advanced)) {
133 $fields = get_records('data_fields', 'dataid', $data->id
);
135 //Added to ammend paging error. This error would occur when attempting to go from one page of advanced
136 //search results to another. All fields were reset in the page transfer, and there was no way of determining
137 //whether or not the user reset them. This would cause a blank search to execute whenever the user attempted
138 //to see any page of results past the first.
139 //This fix works as follows:
140 //$paging flag is set to false when page 0 of the advanced search results is viewed for the first time.
141 //Viewing any page of results after page 0 passes the false $paging flag though the URL (see line 523) and the
142 //execution falls through to the second condition below, allowing paging to be set to true.
143 //Paging remains true and keeps getting passed though the URL until a new search is performed
144 //(even if page 0 is revisited).
145 //A false $paging flag generates advanced search results based on the fields input by the user.
146 //A true $paging flag generates davanced search results from the $SESSION global.
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 // Disable search filters if $filter is not true:
230 $textlib = textlib_get_instance();
231 if ($textlib->strlen($search) < 2) {
234 $SESSION->dataprefs
[$data->id
]['search'] = $search; // Make it sticky
236 $sort = optional_param('sort', $SESSION->dataprefs
[$data->id
]['sort'], PARAM_INT
);
237 $SESSION->dataprefs
[$data->id
]['sort'] = $sort; // Make it sticky
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
243 $oldperpage = get_user_preferences('data_perpage_'.$data->id
, 10);
244 $perpage = optional_param('perpage', $oldperpage, PARAM_INT
);
249 if ($perpage != $oldperpage) {
250 set_user_preference('data_perpage_'.$data->id
, $perpage);
253 add_to_log($course->id
, 'data', 'view', "view.php?id=$cm->id", $data->id
, $cm->id
);
256 // Initialize $PAGE, compute blocks
257 $PAGE = page_create_instance($data->id
);
258 $pageblocks = blocks_setup($PAGE);
259 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT
]), 210);
261 if (($edit != -1) and $PAGE->user_allowed_editing()) {
262 $USER->editing
= $edit;
265 /// RSS and CSS and JS meta
267 if (!empty($CFG->enablerssfeeds
) && !empty($CFG->data_enablerssfeeds
) && $data->rssarticles
> 0) {
268 $rsspath = rss_get_url($course->id
, $USER->id
, 'data', $data->id
);
269 $meta .= '<link rel="alternate" type="application/rss+xml" ';
270 $meta .= 'title ="'. format_string($course->shortname
) .': %fullname%" href="'.$rsspath.'" />';
272 if ($data->csstemplate
) {
273 $meta .= '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot
.'/mod/data/css.php?d='.$data->id
.'" /> ';
275 if ($data->jstemplate
) {
276 $meta .= '<script type="text/javascript" src="'.$CFG->wwwroot
.'/mod/data/js.php?d='.$data->id
.'"></script>';
280 /// Print the page header
281 $PAGE->print_header($course->shortname
.': %fullname%', '', $meta);
284 /// If we have blocks, then print the left side here
285 if (!empty($CFG->showblocksonmodpages
)) {
286 echo '<table id="layout-table"><tr>';
287 if ((blocks_have_content($pageblocks, BLOCK_POS_LEFT
) ||
$PAGE->user_is_editing())) {
288 echo '<td style="width: '.$blocks_preferred_width.'px;" id="left-column">';
289 print_container_start();
290 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT
);
291 print_container_end();
294 echo '<td id="middle-column">';
295 print_container_start();
298 /// Check to see if groups are being used here
299 $returnurl = 'view.php?d='.$data->id
.'&search='.s($search).'&sort='.s($sort).'&order='.s($order).'&';
300 groups_print_activity_menu($cm, $returnurl);
301 $currentgroup = groups_get_activity_group($cm);
302 $groupmode = groups_get_activity_groupmode($cm);
304 // deletect entries not approved yet and show hint instead of not found error
305 if ($record and $data->approval
and !$record->approved
and $record->userid
!= $USER->id
and !has_capability('mod/data:manageentries', $context)) {
306 if (!$currentgroup or $record->groupid
== $currentgroup or $record->groupid
== 0) {
307 print_error('notapproved', 'data');
311 print_heading(format_string($data->name
));
313 // Do we need to show a link to the RSS feed for the records?
314 if (!empty($CFG->enablerssfeeds
) && !empty($CFG->data_enablerssfeeds
) && $data->rssarticles
> 0) {
315 echo '<div style="float:right;">';
316 rss_print_link($course->id
, $USER->id
, 'data', $data->id
, get_string('rsstype'));
318 echo '<div style="clear:both;"></div>';
321 if ($data->intro
and empty($page) and empty($record) and $mode != 'single') {
322 $options = new object();
323 $options->noclean
= true;
324 print_box(format_text($data->intro
, FORMAT_MOODLE
, $options), 'generalbox', 'intro');
327 /// Delete any requested records
329 if ($delete && confirm_sesskey() && (has_capability('mod/data:manageentries', $context) or data_isowner($delete))) {
330 if ($confirm = optional_param('confirm',0,PARAM_INT
)) {
331 if ($deleterecord = get_record('data_records', 'id', $delete)) { // Need to check this is valid
332 if ($deleterecord->dataid
== $data->id
) { // Must be from this database
333 if ($contents = get_records('data_content','recordid', $deleterecord->id
)) {
334 foreach ($contents as $content) { // Delete files or whatever else this field allows
335 if ($field = data_get_field_from_id($content->fieldid
, $data)) { // Might not be there
336 $field->delete_content($content->recordid
);
340 delete_records('data_content','recordid', $deleterecord->id
);
341 delete_records('data_records','id', $deleterecord->id
);
343 add_to_log($course->id
, 'data', 'record delete', "view.php?id=$cm->id", $data->id
, $cm->id
);
345 notify(get_string('recorddeleted','data'), 'notifysuccess');
349 } else { // Print a confirmation page
350 if ($deleterecord = get_record('data_records', 'id', $delete)) { // Need to check this is valid
351 if ($deleterecord->dataid
== $data->id
) { // Must be from this database
352 notice_yesno(get_string('confirmdeleterecord','data'),
353 'view.php?d='.$data->id
.'&delete='.$delete.'&confirm=1&sesskey='.sesskey(),
354 'view.php?d='.$data->id
);
356 $records[] = $deleterecord;
357 echo data_print_template('singletemplate', $records, $data, '', 0, true);
359 print_footer($course);
370 if ($record or $mode == 'single') {
371 $currenttab = 'single';
372 } elseif($mode == 'asearch') {
373 $currenttab = 'asearch';
376 $currenttab = 'list';
380 if ($mode == 'asearch') {
384 /// Approve any requested records
386 $approvecap = has_capability('mod/data:approve', $context);
388 if ($approve && confirm_sesskey() && $approvecap) {
389 if ($approverecord = get_record('data_records', 'id', $approve)) { // Need to check this is valid
390 if ($approverecord->dataid
== $data->id
) { // Must be from this database
391 $newrecord->id
= $approverecord->id
;
392 $newrecord->approved
= 1;
393 if (update_record('data_records', $newrecord)) {
394 notify(get_string('recordapproved','data'), 'notifysuccess');
400 $numentries = data_numentries($data);
401 /// Check the number of entries required against the number of entries already made (doesn't apply to teachers)
402 if ($data->requiredentries
> 0 && $numentries < $data->requiredentries
&& !has_capability('mod/data:manageentries', $context)) {
403 $data->entriesleft
= $data->requiredentries
- $numentries;
404 $strentrieslefttoadd = get_string('entrieslefttoadd', 'data', $data);
405 notify($strentrieslefttoadd);
408 /// 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)
409 $requiredentries_allowed = true;
410 if ($data->requiredentriestoview
> 0 && $numentries < $data->requiredentriestoview
&& !has_capability('mod/data:manageentries', $context)) {
411 $data->entrieslefttoview
= $data->requiredentriestoview
- $numentries;
412 $strentrieslefttoaddtoview = get_string('entrieslefttoaddtoview', 'data', $data);
413 notify($strentrieslefttoaddtoview);
414 $requiredentries_allowed = false;
417 /// setup group and approve restrictions
418 if (!$approvecap && $data->approval
) {
420 $approveselect = ' AND (r.approved=1 OR r.userid='.$USER->id
.') ';
422 $approveselect = ' AND r.approved=1 ';
425 $approveselect = ' ';
429 $groupselect = " AND (r.groupid = '$currentgroup' OR r.groupid = 0)";
434 $ilike = sql_ilike(); //Be case-insensitive
436 // Init some variables to be used by advanced search
437 $advsearchselect = '';
441 /// Find the field we are sorting on
442 if ($sort <= 0 or !$sortfield = data_get_field_from_id($sort, $data)) {
446 $ordering = "u.lastname $order, u.firstname $order";
449 $ordering = "u.firstname $order, u.lastname $order";
452 $ordering = "r.approved $order, r.timecreated $order";
454 case DATA_TIMEMODIFIED
:
455 $ordering = "r.timemodified $order";
460 $ordering = "r.timecreated $order";
463 $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname';
464 $count = ' COUNT(DISTINCT c.recordid) ';
465 $tables = $CFG->prefix
.'data_content c,'.$CFG->prefix
.'data_records r,'.$CFG->prefix
.'data_content cs, '.$CFG->prefix
.'user u ';
466 $where = 'WHERE c.recordid = r.id
467 AND r.dataid = '.$data->id
.'
469 AND cs.recordid = r.id ';
470 $sortorder = ' ORDER BY '.$ordering.', r.id ASC ';
473 // If requiredentries is not reached, only show current user's entries
474 if (!$requiredentries_allowed) {
475 $where .= ' AND u.id = ' . $USER->id
;
478 if (!empty($advanced)) { //If advanced box is checked.
479 foreach($search_array as $key => $val) { //what does $search_array hold?
480 if ($key == DATA_FIRSTNAME
or $key == DATA_LASTNAME
) {
481 $searchselect .= " AND $val->field $ilike '%{$val->data}%'";
484 $advtables .= ', '.$CFG->prefix
.'data_content c'.$key.' ';
485 $advwhere .= ' AND c'.$key.'.recordid = r.id';
486 $advsearchselect .= ' AND ('.$val->sql
.') ';
488 } else if ($search) {
489 $searchselect = " AND (cs.content $ilike '%$search%' OR u.firstname $ilike '%$search%' OR u.lastname $ilike '%$search%' ) ";
496 $sortcontent = $sortfield->get_sort_field();
497 $sortcontentfull = $sortfield->get_sort_sql('c.'.$sortcontent);
499 $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname, '.sql_compare_text($sortcontentfull).' AS _order ';
500 $count = ' COUNT(DISTINCT c.recordid) ';
501 $tables = $CFG->prefix
.'data_content c,'.$CFG->prefix
.'data_records r,'.$CFG->prefix
.'data_content cs, '.$CFG->prefix
.'user u ';
502 $where = 'WHERE c.recordid = r.id
503 AND c.fieldid = '.$sort.'
504 AND r.dataid = '.$data->id
.'
506 AND cs.recordid = r.id ';
507 $sortorder = ' ORDER BY _order '.$order.' , r.id ASC ';
510 // If requiredentries is not reached, only show current user's entries
511 if (!$requiredentries_allowed) {
512 $where .= ' AND u.id = ' . $USER->id
;
515 if (!empty($advanced)) { //If advanced box is checked.
516 foreach($search_array as $key => $val) { //what does $search_array hold?
517 if ($key == DATA_FIRSTNAME
or $key == DATA_LASTNAME
) {
518 $searchselect .= " AND $val->field $ilike '%{$val->data}%'";
521 $advtables .= ', '.$CFG->prefix
.'data_content c'.$key.' ';
522 $advwhere .= ' AND c'.$key.'.recordid = r.id AND c'.$key.'.fieldid = '.$key;
523 $advsearchselect .= ' AND ('.$val->sql
.') ';
525 } else if ($search) {
526 $searchselect = " AND (cs.content $ilike '%$search%' OR u.firstname $ilike '%$search%' OR u.lastname $ilike '%$search%' ) ";
532 /// To actually fetch the records
534 $fromsql = "FROM $tables $advtables $where $advwhere $groupselect $approveselect $searchselect $advsearchselect";
535 $sqlselect = "SELECT $what $fromsql $sortorder";
536 $sqlcount = "SELECT $count $fromsql"; // Total number of records when searching
537 $sqlrids = "SELECT tmp.id FROM ($sqlselect) tmp";
538 $sqlmax = "SELECT $count FROM $tables $where $groupselect $approveselect"; // number of all recoirds user may see
540 /// Work out the paging numbers and counts
542 $totalcount = count_records_sql($sqlcount);
543 if (empty($searchselect) && empty($advsearchselect)) {
544 $maxcount = $totalcount;
546 $maxcount = count_records_sql($sqlmax);
549 if ($record) { // We need to just show one, so where is it in context?
554 if ($allrecordids = get_records_sql($sqlrids)) {
555 $allrecordids = array_keys($allrecordids);
556 $page = (int)array_search($record->id
, $allrecordids);
557 unset($allrecordids);
560 } else if ($mode == 'single') { // We rely on ambient $page settings
564 $nowperpage = $perpage;
567 /// Get the actual records
569 if (!$records = get_records_sql($sqlselect, $page * $nowperpage, $nowperpage)) {
571 if ($record) { // Something was requested so try to show that at least (bug 5132)
572 if (has_capability('mod/data:manageentries', $context) ||
empty($data->approval
) ||
573 $record->approved ||
(isloggedin() && $record->userid
== $USER->id
)) {
574 if (!$currentgroup ||
$record->groupid
== $currentgroup ||
$record->groupid
== 0) {
575 // OK, we can show this one
576 $records = array($record->id
=> $record);
583 if (empty($records)) {
587 $a->reseturl
= "view.php?id=$cm->id&mode=$mode&search=&advanced=0";
588 notify(get_string('foundnorecords','data', $a));
590 notify(get_string('norecords','data'));
593 } else { // We have some records to print
595 if ($maxcount != $totalcount) {
597 $a->num
= $totalcount;
599 $a->reseturl
= "view.php?id=$cm->id&mode=$mode&search=&advanced=0";
600 notify(get_string('foundrecords', 'data', $a), 'notifysuccess');
603 if ($mode == 'single') { // Single template
604 $baseurl = 'view.php?d=' . $data->id
. '&mode=single&';
605 if (!empty($search)) {
606 $baseurl .= 'filter=1&';
608 print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
610 if (empty($data->singletemplate
)){
611 notify(get_string('nosingletemplate','data'));
612 data_generate_default_template($data, 'singletemplate', 0, false, false);
615 data_print_template('singletemplate', $records, $data, $search, $page);
617 print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
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 if (!empty($search)) {
624 $baseurl .= 'filter=1&';
626 //pass variable to allow determining whether or not we are paging through results.
627 $baseurl .= 'paging='.$paging.'&';
629 print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
631 if (empty($data->listtemplate
)){
632 notify(get_string('nolisttemplate','data'));
633 data_generate_default_template($data, 'listtemplate', 0, false, false);
635 echo $data->listtemplateheader
;
636 data_print_template('listtemplate', $records, $data, $search, $page);
637 echo $data->listtemplatefooter
;
639 print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
645 $search = trim($search);
646 if (empty($records)) {
650 //Advanced search form doesn't make sense for single (redirects list view)
651 if (($maxcount ||
$mode == 'asearch') && $mode != 'single') {
652 data_print_preference_form($data, $perpage, $search, $sort, $order, $search_array, $advanced, $mode);
655 /// If we have blocks, then print the left side here
656 if (!empty($CFG->showblocksonmodpages
)) {
657 print_container_end();
658 echo '</td>'; // Middle column
659 if ((blocks_have_content($pageblocks, BLOCK_POS_RIGHT
) ||
$PAGE->user_is_editing())) {
660 echo '<td style="width: '.$blocks_preferred_width.'px;" id="right-column">';
661 print_container_start();
662 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT
);
663 print_container_end();
666 echo '</tr></table>';
669 print_footer($course);