NEW MODULE FROM MOODLE.COM - DATABASE !
[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');
28
29 define('PAGE_DATA_VIEW', 'mod-data-view');
30 define('PAGE_DATA', PAGE_DATA_VIEW);
31
32 require_once('pagelib.php');
33 require_login();
34
35 page_map_class(PAGE_DATA_VIEW, 'page_data');
36 $DEFINEDPAGES = array(PAGE_DATA_VIEW,);
37
38 $id = optional_param('id', 0, PARAM_INT); // course module id
39 $d = optional_param('d', 0, PARAM_INT); // database id
40 $search = optional_param('search','',PARAM_NOTAGS); //search string
41 $page = optional_param('page', 0, PARAM_INT); //offset of the current record
42 $rid = optional_param('rid', 0, PARAM_INT); //record id
43 $perpagemenu = optional_param('perpage1', 0, PARAM_INT); //value from drop down
44
45 if ($id) {
46 if (! $cm = get_record('course_modules', 'id', $id)) {
47 error('Course Module ID was incorrect');
48 }
49 if (! $course = get_record('course', 'id', $cm->course)) {
50 error('Course is misconfigured');
51 }
52 if (! $data = get_record('data', 'id', $cm->instance)) {
53 error('Course module is incorrect');
54 }
55
56 } else {
57 if (! $data = get_record('data', 'id', $d)) {
58 error('Data ID is incorrect');
59 }
60 if (! $course = get_record('course', 'id', $data->course)) {
61 error('Course is misconfigured');
62 }
63 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
64 error('Course Module ID was incorrect');
65 }
66 }
67
68 if (isteacher($course->id)) {
69 if (!count_records('data_fields','dataid',$data->id)) { // Brand new database!
70 redirect($CFG->wwwroot.'/mod/data/fields.php?d='.$data->id); // Redirect to field entry
71 }
72 }
73
74 //set user preference if available
75 if (isset($_POST['updatepref'])){
76
77 if (!$perpage = $perpagemenu){ //if menu not in use, use the text field
78 $perpage = (int)optional_param('perpage',10);
79 }
80 $perpage = ($perpage <= 0) ? 10 : $perpage ;
81 set_user_preference('data_perpage', $perpage);
82 }
83
84 $d = $data->id;//set this so tabs can work properly
85 add_to_log($course->id, 'data', 'view', "view.php?id=$cm->id", $data->id, $cm->id);
86
87
88// Initialize $PAGE, compute blocks
89 $PAGE = page_create_instance($data->id);
90 $pageblocks = blocks_setup($PAGE);
91 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]), 210);
92
93 if (!empty($edit) && $PAGE->user_allowed_editing()) {
94 if ($edit == 'on') {
95 $USER->editing = true;
96 } else if ($edit == 'off') {
97 $USER->editing = false;
98 }
99 }
100/// Print the page header
101 $PAGE->print_header($course->shortname.': %fullname%');
102
103 echo '<table id="layout-table"><tr>';
104
105 if(!empty($CFG->showblocksonmodpages) && (blocks_have_content($pageblocks, BLOCK_POS_LEFT) || $PAGE->user_is_editing())) {
106 echo '<td style="width: '.$blocks_preferred_width.'px;" id="left-column">';
107 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
108 echo '</td>';
109 }
110
111 echo '<td id="middle-column">';
112
113 print_heading(format_string($data->name));
114
115/// Check to see if groups are being used here
116 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
117 $currentgroup = setup_and_print_groups($course, $groupmode, "view.php?id=$cm->id");
118 } else {
119 $currentgroup = 0;
120 }
121
122 if ($currentgroup) {
123 $groupselect = " AND groupid = '$currentgroup'";
124 $groupparam = "&amp;groupid=$currentgroup";
125 } else {
126 $groupselect = "";
127 $groupparam = "";
128 }
129
130/// Print the tabs
131
132 $currenttab = 'browse';
133 include('tabs.php');
134
135/// Print the browsing interface
136
137 /***************************
138 * code to delete a record *
139 ***************************/
140 if (($delete = optional_param('delete',0,PARAM_INT)) && confirm_sesskey()){
141 if (isteacheredit($course) or data_isowner($delete)){
142 if ($confirm = optional_param('confirm',0,PARAM_INT)){
143 //find all contents in this record?
144 if ($contents = get_records('data_content','recordid',$delete)){
145
146 //for each content, delete the file associated
147 foreach ($contents as $content){
148 $field = get_record('data_fields','id',$content->fieldid);
149
150 if ($g = data_get_field($field)){ //it is possible that the field is deleted by teacher
151 $g->delete_data_content_files($data->id, $delete, $content->content);
152 }
153 }
154 delete_records('data_records','id',$delete);
155 delete_records('data_content','recordid',$delete);
156 notify (get_string('recorddeleted','data'));
157 }
158 }
159 else { //prints annoying confirmation dialogue
160 $field = get_record('data_records','id',$delete);
161 print_simple_box_start('center', '60%');
162 echo '<div align="center">';
163 echo '<form action = "view.php?d='.$data->id.'&amp;delete='.$delete.'" method="post">';
164 //add sesskey
165 echo get_string('confirmdeleterecord','data');
166 echo '<p />';
167 echo '<input type="hidden" value="'.sesskey().'" name="sesskey">';
168 echo '<input type="submit" value="'.get_string('ok').'"> ';
169 echo '<input type="hidden" name="confirm" value="1">';
170 echo '<input type="button" value="'.get_string('cancel').'" onclick="javascript:history.go(-1);" />';
171 echo '</form>';
172 echo '</div>';
173 print_simple_box_end();
174 echo '</td></tr></table>';
175 print_footer($course);
176 exit;
177 }
178 }
179 }
180
181 //if not editting teacher, check whether user has sufficient records to view
182 if (!isteacheredit($course->id) and data_numentries($data) < $data->requiredentriestoview){
183 notify (($data->requiredentriestoview - data_numentris(data)).'&nbsp;'.get_string('insufficiententries','data'));
184 print_footer($course);
185 exit;
186 }
187
188 if ($rid){ //set per page to 1, if looking for 1 specific record
189 set_user_preference('data_perpage', PERPAGE_SINGLE);
190 }
191
192 /*****************************
193 * Setting up page variables *
194 *****************************/
195
196 $perpage = get_user_preferences('data_perpage', 10); //get default per page
197
198 $baseurl = 'view.php?d='.$data->id.'&amp;search='.$search.'&amp;';
199
200 if ($rid){ //only used for single mode, but rid should not appear in multi view anyway
201 $sqlo = 'SELECT COUNT(*) FROM '.$CFG->prefix
202 .'data_records WHERE id < '.$rid.' AND dataid='.$data->id;
203 $page = count_records_sql($sqlo);
204 }
205
206 if ($search){ //if in search mode, only search text fields
207
208 $sql = 'SELECT DISTINCT c.* FROM '.$CFG->prefix.'data_content c LEFT JOIN '
209 .$CFG->prefix.'data_fields f on c.fieldid = f.id WHERE f.dataid = '
210 .$data->id.' AND c.content LIKE "%'.$search.'%" ';/*" AND f.type="text" ';*/
211
212 $sqlcount = 'SELECT COUNT(DISTINCT c.recordid) FROM '.$CFG->prefix
213 .'data_content c LEFT JOIN '.$CFG->prefix
214 .'data_fields f on c.fieldid = f.id WHERE f.dataid = '
215 .$data->id.' AND c.content LIKE "%'.$search.'%" ';/* AND f.type="text"';*/
216 }
217
218 else { //else get everything
219
220 $sql = 'SELECT * FROM '.$CFG->prefix.'data_records WHERE dataid ='.$data->id.' ORDER BY id ASC ';
221 $sqlcount = 'SELECT COUNT(*) FROM '.$CFG->prefix
222 .'data_records WHERE dataid ='.$data->id.' ';
223 }
224
225 $limit = $perpage > 1 ? sql_paging_limit($page * $perpage, $perpage)
226 : $limit = sql_paging_limit($page, PERPAGE_SINGLE);
227
228 $sql = $sql . $limit;
229 $totalcount = count_records_sql($sqlcount);
230
231 if (!$records = get_records_sql($sql)){
232 if ($search){
233 notify(get_string('nomatch','data'));
234 }
235 else {
236 notify(get_string('norecords','data'));
237 }
238
239 data_print_preference_form($data, $perpage, $search);
240 print_footer($course);
241 exit;
242 }
243
244 //print header for multi view
245 if ($perpage > 1){
246
247 echo $data->listtemplateheader;
248 $listmode = 'listtemplate';
249 if (empty($data->listtemplate)){
250 notify(get_string('nolisttemplate','data'));
251 }
252 }
253 else {
254 $listmode = 'singletemplate';
255 if (empty($data->singletemplate)){
256 notify(get_string('nosingletemplate','data'));
257 }
258 }
259
260 print_paging_bar($totalcount, $page, $perpage, $baseurl, $pagevar='page');
261
262 //for each record we find, we do a string replacement for tags.
263 data_print_template($records, $data, $search, $listmode);
264
265 print_paging_bar($totalcount, $page, $perpage, $baseurl, $pagevar='page');
266
267 if ($perpage > 1){
268 echo $data->listtemplatefooter; //print footer
269 }
270
271 data_print_preference_form($data, $perpage, $search);
272
273/// Finish the page
274
275 echo '</td></tr></table>';
276
277 print_footer($course);
278
279?>