fixes (MDL-8449) Quiz start and close times should allow the teacher to select a...
[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');
aab98aaf 31
3d4b223a 32
3d45b8e5 33/// One of these is necessary!
3d4b223a 34 $id = optional_param('id', 0, PARAM_INT); // course module id
35 $d = optional_param('d', 0, PARAM_INT); // database id
3d45b8e5 36 $rid = optional_param('rid', 0, PARAM_INT); //record id
37
38 $mode = optional_param('mode', '', PARAM_ALPHA); // Force the browse mode ('single')
39
d2b23346 40 $edit = optional_param('edit', -1, PARAM_BOOL);
3d45b8e5 41
42/// These can be added to perform an action on a record
473dd288 43 $approve = optional_param('approve', 0, PARAM_INT); //approval recordid
44 $delete = optional_param('delete', 0, PARAM_INT); //delete recordid
aab98aaf 45
3d4b223a 46 if ($id) {
f9d5371b 47 if (! $cm = get_coursemodule_from_id('data', $id)) {
3d4b223a 48 error('Course Module ID was incorrect');
49 }
50 if (! $course = get_record('course', 'id', $cm->course)) {
51 error('Course is misconfigured');
52 }
53 if (! $data = get_record('data', 'id', $cm->instance)) {
54 error('Course module is incorrect');
55 }
3d45b8e5 56 $record = NULL;
3d4b223a 57
3d45b8e5 58 } else if ($rid) {
59 if (! $record = get_record('data_records', 'id', $rid)) {
60 error('Record ID is incorrect');
61 }
62 if (! $data = get_record('data', 'id', $record->dataid)) {
63 error('Data ID is incorrect');
64 }
65 if (! $course = get_record('course', 'id', $data->course)) {
66 error('Course is misconfigured');
67 }
68 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
69 error('Course Module ID was incorrect');
70 }
71 } else { // We must have $d
3d4b223a 72 if (! $data = get_record('data', 'id', $d)) {
73 error('Data ID is incorrect');
74 }
75 if (! $course = get_record('course', 'id', $data->course)) {
76 error('Course is misconfigured');
77 }
78 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
79 error('Course Module ID was incorrect');
80 }
3d45b8e5 81 $record = NULL;
3d4b223a 82 }
83
7ddda9db 84 require_course_login($course, true, $cm);
aab98aaf 85
dabfd0ed 86 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
20821a12 87 require_capability('mod/data:viewentry', $context);
3d1c33ef 88
89/// If it's hidden then it's don't show anything. :)
0468976c 90 if (empty($cm->visible) and !has_capability('mod/data:managetemplates', $context)) {
1d8a829a 91 $strdatabases = get_string("modulenameplural", "data");
92 $navigation = "<a href=\"index.php?id=$course->id\">$strdatabases</a> ->";
93 print_header_simple(format_string($data->name), "",
94 "$navigation ".format_string($data->name), "", "", true, '', navmenu($course, $cm));
3d1c33ef 95 notice(get_string("activityiscurrentlyhidden"));
96 }
97
3d45b8e5 98/// If we have an empty Database then redirect because this page is useless without data
0468976c 99 if (has_capability('mod/data:managetemplates', $context)) {
ed69c723 100 if (!record_exists('data_fields','dataid',$data->id)) { // Brand new database!
5bac6d10 101 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
3d4b223a 102 }
103 }
e0279f63 104
3d45b8e5 105
106/// Check further parameters that set browsing preferences
107 if (!isset($SESSION->dataprefs)) {
108 $SESSION->dataprefs = array();
e0279f63 109 }
3d45b8e5 110 if (!isset($SESSION->dataprefs[$data->id])) {
111 $SESSION->dataprefs[$data->id] = array();
112 $SESSION->dataprefs[$data->id]['search'] = '';
113 $SESSION->dataprefs[$data->id]['sort'] = $data->defaultsort;
114 $SESSION->dataprefs[$data->id]['order'] = ($data->defaultsortdir == 0) ? 'ASC' : 'DESC';
3d4b223a 115 }
3d45b8e5 116 $search = optional_param('search', $SESSION->dataprefs[$data->id]['search'], PARAM_NOTAGS);
76a2fd82 117 $textlib = new textlib();
118 if ($textlib->strlen($search) < 2) {
119 $search = '';
120 }
3d45b8e5 121 $SESSION->dataprefs[$data->id]['search'] = $search; // Make it sticky
122
123 $sort = optional_param('sort', $SESSION->dataprefs[$data->id]['sort'], PARAM_INT);
124 $SESSION->dataprefs[$data->id]['sort'] = $sort; // Make it sticky
125
126 $order = (optional_param('order', $SESSION->dataprefs[$data->id]['order'], PARAM_ALPHA) == 'ASC') ? 'ASC': 'DESC';
127 $SESSION->dataprefs[$data->id]['order'] = $order; // Make it sticky
128
129
130 $oldperpage = get_user_preferences('data_perpage_'.$data->id, 10);
131 $perpage = optional_param('perpage', $oldperpage, PARAM_INT);
132
133 if ($perpage < 2) {
134 $perpage = 2;
135 }
136 if ($perpage != $oldperpage) {
137 set_user_preference('data_perpage_'.$data->id, $perpage);
138 }
139
140 $page = optional_param('page', 0, PARAM_INT);
ed69c723 141
3d4b223a 142 add_to_log($course->id, 'data', 'view', "view.php?id=$cm->id", $data->id, $cm->id);
143
144
145// Initialize $PAGE, compute blocks
146 $PAGE = page_create_instance($data->id);
147 $pageblocks = blocks_setup($PAGE);
148 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]), 210);
149
d2b23346 150 if (($edit != -1) and $PAGE->user_allowed_editing()) {
151 $USER->editing = $edit;
3d4b223a 152 }
3d4b223a 153
b0100852 154/// RSS and CSS and JS meta
68635e6f 155 $meta = '';
64452eb4 156 if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
c853304e 157 $rsspath = rss_get_url($course->id, $USER->id, 'data', $data->id);
68635e6f 158 $meta .= '<link rel="alternate" type="application/rss+xml" ';
159 $meta .= 'title ="'.$course->shortname.': %fullname%" href="'.$rsspath.'" />';
160 }
161 if ($data->csstemplate) {
162 $meta .= '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/mod/data/css.php?d='.$data->id.'" /> ';
c853304e 163 }
b0100852 164 if ($data->jstemplate) {
165 $meta .= '<script type="text/javascript" src="'.$CFG->wwwroot.'/mod/data/js.php?d='.$data->id.'"></script>';
166 }
167
aab98aaf 168
c853304e 169/// Print the page header
68635e6f 170 $PAGE->print_header($course->shortname.': %fullname%', '', $meta);
aab98aaf 171
3d4b223a 172
236d839f 173/// If we have blocks, then print the left side here
174 if (!empty($CFG->showblocksonmodpages)) {
175 echo '<table id="layout-table"><tr>';
176 if ((blocks_have_content($pageblocks, BLOCK_POS_LEFT) || $PAGE->user_is_editing())) {
177 echo '<td style="width: '.$blocks_preferred_width.'px;" id="left-column">';
178 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
179 echo '</td>';
180 }
181 echo '<td id="middle-column">';
3d4b223a 182 }
183
3d4b223a 184 print_heading(format_string($data->name));
aab98aaf 185
a593aeee 186 // Do we need to show a link to the RSS feed for the records?
64452eb4 187 if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
a593aeee 188 echo '<div style="float:right;">';
ed69c723 189 rss_print_link($course->id, $USER->id, 'data', $data->id, get_string('rsstype'));
a593aeee 190 echo '</div>';
191 echo '<div style="clear:both;"></div>';
192 }
aab98aaf 193
9e08cf6e 194 if ($data->intro and empty($page) and empty($record) and $mode != 'single') {
c1afef80 195 print_box(format_text($data->intro), 'generalbox', 'intro');
56135f6b 196 }
197
3d4b223a 198/// Check to see if groups are being used here
199 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
aab98aaf 200 $currentgroup = setup_and_print_groups($course, $groupmode,
ed69c723 201 'view.php?d='.$data->id.'&amp;search='.s($search).'&amp;sort='.s($sort).
202 '&amp;order='.s($order).'&amp;');
3d4b223a 203 } else {
204 $currentgroup = 0;
205 }
206
473dd288 207/// Delete any requested records
208
0468976c 209 if ($delete && confirm_sesskey() && (has_capability('mod/data:manageentries', $context) or data_isowner($delete))) {
3d45b8e5 210 if ($confirm = optional_param('confirm',0,PARAM_INT)) {
211 if ($deleterecord = get_record('data_records', 'id', $delete)) { // Need to check this is valid
212 if ($deleterecord->dataid == $data->id) { // Must be from this database
213 if ($contents = get_records('data_content','recordid', $deleterecord->id)) {
214 foreach ($contents as $content) { // Delete files or whatever else this field allows
215 if ($field = data_get_field_from_id($content->fieldid, $data)) { // Might not be there
216 $field->delete_content($content->recordid);
217 }
3d4b223a 218 }
219 }
3d45b8e5 220 delete_records('data_content','recordid', $deleterecord->id);
221 delete_records('data_records','id', $deleterecord->id);
f0497d6f 222
3d45b8e5 223 add_to_log($course->id, 'data', 'record delete', "view.php?id=$cm->id", $data->id, $cm->id);
f0497d6f 224
3d45b8e5 225 notify(get_string('recorddeleted','data'), 'notifysuccess');
226 }
3d4b223a 227 }
3d45b8e5 228
229 } else { // Print a confirmation page
64452eb4 230 if ($deleterecord = get_record('data_records', 'id', $delete)) { // Need to check this is valid
231 if ($deleterecord->dataid == $data->id) { // Must be from this database
aab98aaf 232 notice_yesno(get_string('confirmdeleterecord','data'),
64452eb4 233 'view.php?d='.$data->id.'&amp;delete='.$delete.'&amp;confirm=1&amp;sesskey='.sesskey(),
234 'view.php?d='.$data->id);
3d45b8e5 235
64452eb4 236 $records[] = $deleterecord;
3f9672d3 237 echo data_print_template('singletemplate', $records, $data, '', 0, true);
64452eb4 238
239 print_footer($course);
240 exit;
241 }
242 }
243 }
244 }
245
246
247
248/// Print the tabs
249
250 if ($record or $mode == 'single') {
251 $currenttab = 'single';
252 } else {
253 $currenttab = 'list';
254 }
aab98aaf 255 include('tabs.php');
64452eb4 256
257
258/// Approve any requested records
259
0468976c 260 if ($approve && confirm_sesskey() && has_capability('mod/data:approve', $context)) {
64452eb4 261 if ($approverecord = get_record('data_records', 'id', $approve)) { // Need to check this is valid
262 if ($approverecord->dataid == $data->id) { // Must be from this database
263 $newrecord->id = $approverecord->id;
264 $newrecord->approved = 1;
265 if (update_record('data_records', $newrecord)) {
266 notify(get_string('recordapproved','data'), 'notifysuccess');
267 }
268 }
3d4b223a 269 }
270 }
271
75c42c87 272// If not teacher, check whether user has sufficient records to view
0468976c 273 if (!has_capability('mod/data:managetemplates', $context) and data_numentries($data) < $data->requiredentriestoview){
2b1d7c8e 274 notify (($data->requiredentriestoview - data_numentries($data)).'&nbsp;'.get_string('insufficiententries','data'));
af25f45e 275 echo '</td></tr></table>';
3d4b223a 276 print_footer($course);
277 exit;
278 }
279
3d4b223a 280
3d45b8e5 281/// We need to examine the whole dataset to produce the correct paging
33819735 282
0468976c 283 if ((!has_capability('mod/data:managetemplates', $context)) && ($data->approval)) {
4431d2e0 284 if (isloggedin()) {
3d45b8e5 285 $approveselect = ' AND (r.approved=1 OR r.userid='.$USER->id.') ';
4431d2e0 286 } else {
3d45b8e5 287 $approveselect = ' AND r.approved=1 ';
4431d2e0 288 }
cf3e199b 289 } else {
3d45b8e5 290 $approveselect = ' ';
cf3e199b 291 }
292
3d45b8e5 293 if ($currentgroup) {
294 $groupselect = " AND (r.groupid = '$currentgroup' OR r.groupid = 0)";
cf3e199b 295 } else {
3d45b8e5 296 $groupselect = ' ';
3d4b223a 297 }
3d4b223a 298
3d45b8e5 299/// Find the field we are sorting on
a3318c36 300 if ($sort and $sortfield = data_get_field_from_id($sort, $data)) {
301
cf3e199b 302 $sortcontent = $sortfield->get_sort_field();
64452eb4 303 $sortcontentfull = $sortfield->get_sort_sql('c.'.$sortcontent);
3d45b8e5 304
56253c2d 305 $what = ' DISTINCT r.id, r.approved, r.userid, u.firstname, u.lastname, c.'.$sortcontent.' ';
3d45b8e5 306 $count = ' COUNT(DISTINCT c.recordid) ';
64452eb4 307 $tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r,'.$CFG->prefix.'data_content c1, '.$CFG->prefix.'user u ';
aab98aaf 308 $where = 'WHERE c.recordid = r.id
309 AND c.fieldid = '.$sort.'
310 AND r.dataid = '.$data->id.'
311 AND r.userid = u.id
3d45b8e5 312 AND c1.recordid = r.id ';
499ed215 313 $sortorder = ' ORDER BY '.$sortcontentfull.' '.$order.' , r.id ASC ';
64452eb4 314 if ($search) {
411f589f 315 $searchselect = " AND (c1.content LIKE '%$search%') ";
64452eb4 316 } else {
317 $searchselect = ' ';
318 }
3d45b8e5 319
aab98aaf 320 } else if ($search) {
64452eb4 321 $what = ' DISTINCT r.id, r.approved, r.userid, u.firstname, u.lastname ';
3d45b8e5 322 $count = ' COUNT(DISTINCT c.recordid) ';
64452eb4 323 $tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r, '.$CFG->prefix.'user u ';
aab98aaf 324 $where = 'WHERE c.recordid = r.id
325 AND r.userid = u.id
3d45b8e5 326 AND r.dataid = '.$data->id;
499ed215 327 $sortorder = ' ORDER BY r.id ASC ';
411f589f 328 $searchselect = " AND (c.content LIKE '%$search%') ";
3d45b8e5 329
330 } else {
56253c2d 331 $what = ' DISTINCT r.id, r.approved, r.timecreated, r.userid, u.firstname, u.lastname ';
3d45b8e5 332 $count = ' COUNT(r.id) ';
64452eb4 333 $tables = $CFG->prefix.'data_records r, '.$CFG->prefix.'user u ';
334 $where = 'WHERE r.dataid = '.$data->id. ' AND r.userid = u.id ';
335 $sortorder = ' ORDER BY r.timecreated '.$order. ' ';
3d45b8e5 336 $searchselect = ' ';
3d4b223a 337 }
3d4b223a 338
64452eb4 339
3d45b8e5 340/// To actually fetch the records
341
342 $fromsql = ' FROM '.$tables.$where.$groupselect.$approveselect.$searchselect;
aab98aaf 343
3d45b8e5 344 $sqlselect = 'SELECT '.$what.$fromsql.$sortorder;
345
346 $sqlcount = 'SELECT '.$count.$fromsql; // Total number of records
347
348/// Work out the paging numbers
349
3d4b223a 350 $totalcount = count_records_sql($sqlcount);
351
3d45b8e5 352 if ($record) { // We need to just show one, so where is it in context?
353 $nowperpage = 1;
354 $mode = 'single';
de89899b 355
356# Following code needs testing to make it work
64452eb4 357# if ($sort) { // We need to search by that field
358# if ($content = get_field('data_content', 'content', 'recordid', $record->id, 'fieldid', $sort)) {
359# $content = addslashes($content);
360# if ($order == 'ASC') {
aab98aaf 361# $lessthan = " AND $sortcontentfull < '$content'
de89899b 362# OR ($sortcontentfull = '$content' AND r.id < '$record->id') ";
64452eb4 363# } else {
aab98aaf 364# $lessthan = " AND $sortcontentfull > '$content'
de89899b 365# OR ($sortcontentfull = '$content' AND r.id < '$record->id') ";
64452eb4 366# }
367# } else { // Failed to find data (shouldn't happen), so fall back to something easy
368# $lessthan = " r.id < '$record->id' ";
369# }
370# } else {
371# $lessthan = " r.id < '$record->id' ";
372# }
373# $sqlindex = 'SELECT COUNT(DISTINCT c.recordid) '.$fromsql.$lessthan.$sortorder;
374# $page = count_records_sql($sqlindex);
375
499ed215 376
64452eb4 377 $allrecords = get_records_sql($sqlselect); // Kludgey but accurate at least!
378 $page = 0;
379 foreach ($allrecords as $key => $allrecord) {
380 if ($key == $record->id) {
381 break;
3d45b8e5 382 }
64452eb4 383 $page++;
3d45b8e5 384 }
3d45b8e5 385
386 } else if ($mode == 'single') { // We rely on ambient $page settings
387 $nowperpage = 1;
388
389 } else {
390 $nowperpage = $perpage;
391 }
392
393/// Get the actual records
394
ea9a729f 395 $records = get_records_sql($sqlselect, $page * $nowperpage, $nowperpage);
3d45b8e5 396
397 if (empty($records)) { // Nothing to show!
3dec563c 398 if ($record) { // Something was requested so try to show that at least (bug 5132)
aab98aaf 399 if (has_capability('mod/data:manageentries', $context) || empty($data->approval) ||
3dec563c 400 $record->approved || (isloggedin() && $record->userid == $USER->id)) {
401 if (!$currentgroup || $record->groupid == $currentgroup || $record->groupid == 0) {
402 $records[] = $record;
403 }
404 }
405 }
406 if ($records) { // OK, we can show this one
407 data_print_template('singletemplate', $records, $data, $search, $page);
408 } else if ($search){
3d4b223a 409 notify(get_string('nomatch','data'));
2535bff9 410 data_print_show_all_form($data, $perpage, $sort, $order, $mode);
dbdfc3db 411 } else {
3d4b223a 412 notify(get_string('norecords','data'));
413 }
cf3e199b 414
3d45b8e5 415 } else { // We have some records to print
68c88622 416
3d45b8e5 417 if ($mode == 'single') { // Single template
418 $baseurl = 'view.php?d='.$data->id.'&amp;mode=single&amp;';
3d4b223a 419
3d45b8e5 420 print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
421
422 if (empty($data->singletemplate)){
423 notify(get_string('nosingletemplate','data'));
3f9672d3 424 data_generate_default_template($data, 'singletemplate', 0, false, false);
3d45b8e5 425 }
3d4b223a 426
52373fe7 427 data_print_template('singletemplate', $records, $data, $search, $page);
3d45b8e5 428
429 print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
430
431 } else { // List template
432 $baseurl = 'view.php?d='.$data->id.'&amp;';
433
434 print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
435
436 if (empty($data->listtemplate)){
437 notify(get_string('nolisttemplate','data'));
3f9672d3 438 data_generate_default_template($data, 'listtemplate', 0, false, false);
3d45b8e5 439 }
440 echo $data->listtemplateheader;
52373fe7 441 data_print_template('listtemplate', $records, $data, $search, $page);
3d45b8e5 442 echo $data->listtemplatefooter;
443
444 print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page');
445 }
33819735 446
f852d652 447 }
448
449 if ($records || $search || $page) {
2535bff9 450 data_print_preference_form($data, $perpage, $search, $sort, $order, $mode);
64452eb4 451 }
3d4b223a 452
236d839f 453/// If we have blocks, then print the left side here
454 if (!empty($CFG->showblocksonmodpages)) {
455 echo '</td>'; // Middle column
456 if ((blocks_have_content($pageblocks, BLOCK_POS_RIGHT) || $PAGE->user_is_editing())) {
457 echo '<td style="width: '.$blocks_preferred_width.'px;" id="right-column">';
458 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT);
459 echo '</td>';
460 }
461 echo '</table>';
462 }
463
3d45b8e5 464 print_footer($course);
0997e51a 465?>