Commit | Line | Data |
---|---|---|
1adbd2c3 | 1 | <?php |
3d4b223a | 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 | |
36051c9e DC |
84 | require_once($CFG->dirroot . '/comment/lib.php'); |
85 | comment::init(); | |
e998effa | 86 | |
dabfd0ed | 87 | $context = get_context_instance(CONTEXT_MODULE, $cm->id); |
20821a12 | 88 | require_capability('mod/data:viewentry', $context); |
3d1c33ef | 89 | |
3d45b8e5 | 90 | /// If we have an empty Database then redirect because this page is useless without data |
0468976c | 91 | if (has_capability('mod/data:managetemplates', $context)) { |
3223bc9e | 92 | if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) { // Brand new database! |
5bac6d10 | 93 | redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry |
3d4b223a | 94 | } |
95 | } | |
e0279f63 | 96 | |
3d45b8e5 | 97 | |
98 | /// Check further parameters that set browsing preferences | |
99 | if (!isset($SESSION->dataprefs)) { | |
100 | $SESSION->dataprefs = array(); | |
e0279f63 | 101 | } |
3d45b8e5 | 102 | if (!isset($SESSION->dataprefs[$data->id])) { |
103 | $SESSION->dataprefs[$data->id] = array(); | |
104 | $SESSION->dataprefs[$data->id]['search'] = ''; | |
7900ecb0 | 105 | $SESSION->dataprefs[$data->id]['search_array'] = array(); |
3d45b8e5 | 106 | $SESSION->dataprefs[$data->id]['sort'] = $data->defaultsort; |
7900ecb0 | 107 | $SESSION->dataprefs[$data->id]['advanced'] = 0; |
3d45b8e5 | 108 | $SESSION->dataprefs[$data->id]['order'] = ($data->defaultsortdir == 0) ? 'ASC' : 'DESC'; |
3d4b223a | 109 | } |
de8ff581 | 110 | |
eeeb4f2a | 111 | // reset advanced form |
112 | if (!is_null(optional_param('resetadv', null, PARAM_RAW))) { | |
113 | $SESSION->dataprefs[$data->id]['search_array'] = array(); | |
114 | // we need the redirect to cleanup the form state properly | |
115 | redirect("view.php?id=$cm->id&mode=$mode&search=&advanced=1"); | |
116 | } | |
117 | ||
de8ff581 | 118 | $advanced = optional_param('advanced', -1, PARAM_INT); |
119 | if ($advanced == -1) { | |
120 | $advanced = $SESSION->dataprefs[$data->id]['advanced']; | |
121 | } else { | |
122 | if (!$advanced) { | |
123 | // explicitly switched to normal mode - discard all advanced search settings | |
124 | $SESSION->dataprefs[$data->id]['search_array'] = array(); | |
125 | } | |
832123e1 | 126 | $SESSION->dataprefs[$data->id]['advanced'] = $advanced; |
de8ff581 | 127 | } |
128 | ||
7900ecb0 | 129 | $search_array = $SESSION->dataprefs[$data->id]['search_array']; |
eeeb4f2a | 130 | |
7900ecb0 | 131 | if (!empty($advanced)) { |
132 | $search = ''; | |
69c0a609 | 133 | $vals = array(); |
3223bc9e | 134 | $fields = $DB->get_records('data_fields', array('dataid'=>$data->id)); |
8429163d | 135 | |
7900ecb0 | 136 | //Added to ammend paging error. This error would occur when attempting to go from one page of advanced |
137 | //search results to another. All fields were reset in the page transfer, and there was no way of determining | |
138 | //whether or not the user reset them. This would cause a blank search to execute whenever the user attempted | |
139 | //to see any page of results past the first. | |
140 | //This fix works as follows: | |
141 | //$paging flag is set to false when page 0 of the advanced search results is viewed for the first time. | |
142 | //Viewing any page of results after page 0 passes the false $paging flag though the URL (see line 523) and the | |
143 | //execution falls through to the second condition below, allowing paging to be set to true. | |
144 | //Paging remains true and keeps getting passed though the URL until a new search is performed | |
145 | //(even if page 0 is revisited). | |
8429163d | 146 | //A false $paging flag generates advanced search results based on the fields input by the user. |
7900ecb0 | 147 | //A true $paging flag generates davanced search results from the $SESSION global. |
8429163d | 148 | |
7900ecb0 | 149 | $paging = optional_param('paging', NULL, PARAM_BOOL); |
150 | if($page == 0 && !isset($paging)) { | |
151 | $paging = false; | |
152 | } | |
153 | else { | |
154 | $paging = true; | |
155 | } | |
714bec74 | 156 | if (!empty($fields)) { |
7900ecb0 | 157 | foreach($fields as $field) { |
2d9b3ef2 | 158 | $searchfield = data_get_field_from_id($field->id, $data); |
7900ecb0 | 159 | //Get field data to build search sql with. If paging is false, get from user. |
160 | //If paging is true, get data from $search_array which is obtained from the $SESSION (see line 116). | |
161 | if(!$paging) { | |
162 | $val = $searchfield->parse_search_field(); | |
714bec74 | 163 | } else { |
7900ecb0 | 164 | //Set value from session if there is a value @ the required index. |
714bec74 | 165 | if (isset($search_array[$field->id])) { |
7900ecb0 | 166 | $val = $search_array[$field->id]->data; |
714bec74 | 167 | } else { //If there is not an entry @ the required index, set value to blank. |
7900ecb0 | 168 | $val = ''; |
169 | } | |
7900ecb0 | 170 | } |
714bec74 | 171 | if (!empty($val)) { |
39790bd8 | 172 | $search_array[$field->id] = new stdClass(); |
e3487936 | 173 | list($search_array[$field->id]->sql, $search_array[$field->id]->params) = $searchfield->generate_sql('c'.$field->id, $val); |
714bec74 | 174 | $search_array[$field->id]->data = $val; |
69c0a609 | 175 | $vals[] = $val; |
714bec74 | 176 | } else { |
177 | // clear it out | |
178 | unset($search_array[$field->id]); | |
7900ecb0 | 179 | } |
180 | } | |
181 | } | |
714bec74 | 182 | |
183 | if (!$paging) { | |
184 | // name searching | |
185 | $fn = optional_param('u_fn', '', PARAM_NOTAGS); | |
186 | $ln = optional_param('u_ln', '', PARAM_NOTAGS); | |
187 | } else { | |
de8ff581 | 188 | $fn = isset($search_array[DATA_FIRSTNAME]) ? $search_array[DATA_FIRSTNAME]->data : ''; |
189 | $ln = isset($search_array[DATA_LASTNAME]) ? $search_array[DATA_LASTNAME]->data : ''; | |
714bec74 | 190 | } |
191 | if (!empty($fn)) { | |
39790bd8 | 192 | $search_array[DATA_FIRSTNAME] = new stdClass(); |
e3487936 | 193 | $search_array[DATA_FIRSTNAME]->sql = ''; |
194 | $search_array[DATA_FIRSTNAME]->params = array(); | |
195 | $search_array[DATA_FIRSTNAME]->field = 'u.firstname'; | |
196 | $search_array[DATA_FIRSTNAME]->data = $fn; | |
69c0a609 | 197 | $vals[] = $fn; |
714bec74 | 198 | } else { |
199 | unset($search_array[DATA_FIRSTNAME]); | |
200 | } | |
201 | if (!empty($ln)) { | |
39790bd8 | 202 | $search_array[DATA_LASTNAME] = new stdClass(); |
e3487936 | 203 | $search_array[DATA_LASTNAME]->sql = ''; |
1e72829d | 204 | $search_array[DATA_LASTNAME]->params = array(); |
e3487936 | 205 | $search_array[DATA_LASTNAME]->field = 'u.lastname'; |
206 | $search_array[DATA_LASTNAME]->data = $ln; | |
69c0a609 | 207 | $vals[] = $ln; |
714bec74 | 208 | } else { |
209 | unset($search_array[DATA_LASTNAME]); | |
210 | } | |
211 | ||
7900ecb0 | 212 | $SESSION->dataprefs[$data->id]['search_array'] = $search_array; // Make it sticky |
714bec74 | 213 | |
69c0a609 | 214 | // in case we want to switch to simple search later - there might be multiple values there ;-) |
215 | if ($vals) { | |
216 | $val = reset($vals); | |
217 | if (is_string($val)) { | |
218 | $search = $val; | |
219 | } | |
220 | } | |
221 | ||
714bec74 | 222 | } else { |
7900ecb0 | 223 | $search = optional_param('search', $SESSION->dataprefs[$data->id]['search'], PARAM_NOTAGS); |
224 | //Paging variable not used for standard search. Set it to null. | |
225 | $paging = NULL; | |
226 | } | |
227 | ||
d53e5129 | 228 | // Disable search filters if $filter is not true: |
32d799c6 | 229 | if (! $filter) { |
230 | $search = ''; | |
231 | } | |
d53e5129 | 232 | |
f8311def | 233 | if (textlib::strlen($search) < 2) { |
76a2fd82 | 234 | $search = ''; |
235 | } | |
3d45b8e5 | 236 | $SESSION->dataprefs[$data->id]['search'] = $search; // Make it sticky |
237 | ||
238 | $sort = optional_param('sort', $SESSION->dataprefs[$data->id]['sort'], PARAM_INT); | |
239 | $SESSION->dataprefs[$data->id]['sort'] = $sort; // Make it sticky | |
240 | ||
241 | $order = (optional_param('order', $SESSION->dataprefs[$data->id]['order'], PARAM_ALPHA) == 'ASC') ? 'ASC': 'DESC'; | |
242 | $SESSION->dataprefs[$data->id]['order'] = $order; // Make it sticky | |
243 | ||
244 | ||
245 | $oldperpage = get_user_preferences('data_perpage_'.$data->id, 10); | |
246 | $perpage = optional_param('perpage', $oldperpage, PARAM_INT); | |
247 | ||
248 | if ($perpage < 2) { | |
249 | $perpage = 2; | |
250 | } | |
251 | if ($perpage != $oldperpage) { | |
252 | set_user_preference('data_perpage_'.$data->id, $perpage); | |
253 | } | |
254 | ||
3d4b223a | 255 | add_to_log($course->id, 'data', 'view', "view.php?id=$cm->id", $data->id, $cm->id); |
256 | ||
257 | ||
a9f19d81 DC |
258 | $urlparams = array('d' => $data->id); |
259 | if ($record) { | |
260 | $urlparams['rid'] = $record->id; | |
261 | } | |
262 | if ($page) { | |
263 | $urlparams['page'] = $page; | |
264 | } | |
265 | if ($mode) { | |
266 | $urlparams['mode'] = $mode; | |
267 | } | |
268 | if ($filter) { | |
269 | $urlparams['filter'] = $filter; | |
270 | } | |
3d4b223a | 271 | // Initialize $PAGE, compute blocks |
a9f19d81 | 272 | $PAGE->set_url('/mod/data/view.php', $urlparams); |
3d4b223a | 273 | |
d2b23346 | 274 | if (($edit != -1) and $PAGE->user_allowed_editing()) { |
275 | $USER->editing = $edit; | |
3d4b223a | 276 | } |
3d4b223a | 277 | |
8ebbb06a SH |
278 | $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); |
279 | ||
b0100852 | 280 | /// RSS and CSS and JS meta |
68635e6f | 281 | $meta = ''; |
64452eb4 | 282 | if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) { |
8ebbb06a | 283 | $rsstitle = $courseshortname . ': %fullname%'; |
43b92251 | 284 | rss_add_http_header($context, 'mod_data', $data, $rsstitle); |
68635e6f | 285 | } |
286 | if ($data->csstemplate) { | |
c0467479 | 287 | $PAGE->requires->css('/mod/data/css.php?d='.$data->id); |
c853304e | 288 | } |
b0100852 | 289 | if ($data->jstemplate) { |
60409fe1 | 290 | $PAGE->requires->js('/mod/data/js.php?d='.$data->id, true); |
b0100852 | 291 | } |
292 | ||
6553cda7 | 293 | // Mark as viewed |
294 | $completion = new completion_info($course); | |
295 | $completion->set_module_viewed($cm); | |
296 | ||
c853304e | 297 | /// Print the page header |
b7b2d0f3 | 298 | // Note: MDL-19010 there will be further changes to printing header and blocks. |
299 | // The code will be much nicer than this eventually. | |
8ebbb06a | 300 | $title = $courseshortname.': ' . format_string($data->name); |
3681f9a9 | 301 | |
59e2121e | 302 | if ($PAGE->user_allowed_editing()) { |
4aea3cc7 | 303 | $buttons = '<table><tr><td><form method="get" action="view.php"><div>'. |
3681f9a9 | 304 | '<input type="hidden" name="id" value="'.$cm->id.'" />'. |
305 | '<input type="hidden" name="edit" value="'.($PAGE->user_is_editing()?'off':'on').'" />'. | |
92059c7e SH |
306 | '<input type="submit" value="'.get_string($PAGE->user_is_editing()?'blockseditoff':'blocksediton').'" /></div></form></td></tr></table>'; |
307 | $PAGE->set_button($buttons); | |
3681f9a9 | 308 | } |
aab98aaf | 309 | |
29b64a22 | 310 | if ($mode == 'asearch') { |
311 | $PAGE->navbar->add(get_string('search')); | |
312 | } | |
313 | ||
b0ff558c | 314 | $PAGE->set_title($title); |
315 | $PAGE->set_heading($course->fullname); | |
dd88de0e | 316 | |
b0ff558c | 317 | echo $OUTPUT->header(); |
1adbd2c3 | 318 | |
3b27b0fe | 319 | /// Check to see if groups are being used here |
f1035deb | 320 | $returnurl = $CFG->wwwroot . '/mod/data/view.php?d='.$data->id.'&search='.s($search).'&sort='.s($sort).'&order='.s($order).'&'; |
13534ef7 ML |
321 | groups_print_activity_menu($cm, $returnurl); |
322 | $currentgroup = groups_get_activity_group($cm); | |
323 | $groupmode = groups_get_activity_groupmode($cm); | |
768a2cea AG |
324 | // If a student is not part of a group and seperate groups is enabled, we don't |
325 | // want them seeing all records. | |
326 | if ($currentgroup == 0 && $groupmode == 1 && !has_capability('mod/data:manageentries', $context)) { | |
327 | $canviewallrecords = false; | |
328 | } else { | |
329 | $canviewallrecords = true; | |
330 | } | |
3b27b0fe | 331 | |
e822f07b | 332 | // detect entries not approved yet and show hint instead of not found error |
ebb621f6 | 333 | if ($record and $data->approval and !$record->approved and $record->userid != $USER->id and !has_capability('mod/data:manageentries', $context)) { |
334 | if (!$currentgroup or $record->groupid == $currentgroup or $record->groupid == 0) { | |
335 | print_error('notapproved', 'data'); | |
336 | } | |
337 | } | |
338 | ||
b2dc6880 | 339 | echo $OUTPUT->heading(format_string($data->name)); |
aab98aaf | 340 | |
a593aeee | 341 | // Do we need to show a link to the RSS feed for the records? |
9e86f2e7 AD |
342 | //this links has been Settings (database activity administration) block |
343 | /*if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) { | |
a593aeee | 344 | echo '<div style="float:right;">'; |
aa60291e | 345 | rss_print_link($context->id, $USER->id, 'mod_data', $data->id, get_string('rsstype')); |
a593aeee | 346 | echo '</div>'; |
347 | echo '<div style="clear:both;"></div>'; | |
9e86f2e7 | 348 | }*/ |
aab98aaf | 349 | |
9e08cf6e | 350 | if ($data->intro and empty($page) and empty($record) and $mode != 'single') { |
39790bd8 | 351 | $options = new stdClass(); |
ea6073bb | 352 | $options->noclean = true; |
883fa987 | 353 | echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro'); |
56135f6b | 354 | } |
355 | ||
473dd288 | 356 | /// Delete any requested records |
357 | ||
046dd7dc | 358 | if ($delete && confirm_sesskey() && (has_capability('mod/data:manageentries', $context) or data_isowner($delete))) { |
3d45b8e5 | 359 | if ($confirm = optional_param('confirm',0,PARAM_INT)) { |
3223bc9e | 360 | if ($deleterecord = $DB->get_record('data_records', array('id'=>$delete))) { // Need to check this is valid |
3d45b8e5 | 361 | if ($deleterecord->dataid == $data->id) { // Must be from this database |
3223bc9e | 362 | if ($contents = $DB->get_records('data_content', array('recordid'=>$deleterecord->id))) { |
3d45b8e5 | 363 | foreach ($contents as $content) { // Delete files or whatever else this field allows |
364 | if ($field = data_get_field_from_id($content->fieldid, $data)) { // Might not be there | |
365 | $field->delete_content($content->recordid); | |
366 | } | |
3d4b223a | 367 | } |
368 | } | |
3223bc9e | 369 | $DB->delete_records('data_content', array('recordid'=>$deleterecord->id)); |
370 | $DB->delete_records('data_records', array('id'=>$deleterecord->id)); | |
f0497d6f | 371 | |
3d45b8e5 | 372 | add_to_log($course->id, 'data', 'record delete', "view.php?id=$cm->id", $data->id, $cm->id); |
f0497d6f | 373 | |
4102b449 | 374 | echo $OUTPUT->notification(get_string('recorddeleted','data'), 'notifysuccess'); |
3d45b8e5 | 375 | } |
3d4b223a | 376 | } |
3d45b8e5 | 377 | |
378 | } else { // Print a confirmation page | |
3223bc9e | 379 | if ($deleterecord = $DB->get_record('data_records', array('id'=>$delete))) { // Need to check this is valid |
64452eb4 | 380 | if ($deleterecord->dataid == $data->id) { // Must be from this database |
e822f07b | 381 | $deletebutton = new single_button(new moodle_url('/mod/data/view.php?d='.$data->id.'&delete='.$delete.'&confirm=1'), get_string('delete'), 'post'); |
cd6137d7 | 382 | echo $OUTPUT->confirm(get_string('confirmdeleterecord','data'), |
e822f07b | 383 | $deletebutton, 'view.php?d='.$data->id); |
3d45b8e5 | 384 | |
64452eb4 | 385 | $records[] = $deleterecord; |
3f9672d3 | 386 | echo data_print_template('singletemplate', $records, $data, '', 0, true); |
64452eb4 | 387 | |
7b30ba8b | 388 | echo $OUTPUT->footer(); |
64452eb4 | 389 | exit; |
390 | } | |
391 | } | |
392 | } | |
393 | } | |
394 | ||
395 | ||
2742ffe7 | 396 | //if data activity closed dont let students in |
4a9eecc4 | 397 | $showactivity = true; |
2742ffe7 AD |
398 | if (!has_capability('mod/data:manageentries', $context)) { |
399 | $timenow = time(); | |
400 | if (!empty($data->timeavailablefrom) && $data->timeavailablefrom > $timenow) { | |
4a9eecc4 AD |
401 | echo $OUTPUT->notification(get_string('notopenyet', 'data', userdate($data->timeavailablefrom))); |
402 | $showactivity = false; | |
2742ffe7 | 403 | } else if (!empty($data->timeavailableto) && $timenow > $data->timeavailableto) { |
4a9eecc4 AD |
404 | echo $OUTPUT->notification(get_string('expired', 'data', userdate($data->timeavailableto))); |
405 | $showactivity = false; | |
2742ffe7 AD |
406 | } |
407 | } | |
64452eb4 | 408 | |
4a9eecc4 AD |
409 | if ($showactivity) { |
410 | // Print the tabs | |
64452eb4 | 411 | if ($record or $mode == 'single') { |
412 | $currenttab = 'single'; | |
7900ecb0 | 413 | } elseif($mode == 'asearch') { |
414 | $currenttab = 'asearch'; | |
415 | } | |
416 | else { | |
64452eb4 | 417 | $currenttab = 'list'; |
418 | } | |
aab98aaf | 419 | include('tabs.php'); |
64452eb4 | 420 | |
eeeb4f2a | 421 | if ($mode == 'asearch') { |
422 | $maxcount = 0; | |
8429163d | 423 | |
eeeb4f2a | 424 | } else { |
7900ecb0 | 425 | /// Approve any requested records |
e3487936 | 426 | $params = array(); // named params array |
64452eb4 | 427 | |
8429163d | 428 | $approvecap = has_capability('mod/data:approve', $context); |
bb5740f4 | 429 | |
430 | if ($approve && confirm_sesskey() && $approvecap) { | |
3223bc9e | 431 | if ($approverecord = $DB->get_record('data_records', array('id'=>$approve))) { // Need to check this is valid |
7900ecb0 | 432 | if ($approverecord->dataid == $data->id) { // Must be from this database |
39790bd8 | 433 | $newrecord = new stdClass(); |
7900ecb0 | 434 | $newrecord->id = $approverecord->id; |
435 | $newrecord->approved = 1; | |
dd88de0e PS |
436 | $DB->update_record('data_records', $newrecord); |
437 | echo $OUTPUT->notification(get_string('recordapproved','data'), 'notifysuccess'); | |
64452eb4 | 438 | } |
439 | } | |
3d4b223a | 440 | } |
8429163d | 441 | |
c861f079 | 442 | $numentries = data_numentries($data); |
443 | /// Check the number of entries required against the number of entries already made (doesn't apply to teachers) | |
eeeb4f2a | 444 | if ($data->requiredentries > 0 && $numentries < $data->requiredentries && !has_capability('mod/data:manageentries', $context)) { |
445 | $data->entriesleft = $data->requiredentries - $numentries; | |
446 | $strentrieslefttoadd = get_string('entrieslefttoadd', 'data', $data); | |
4102b449 | 447 | echo $OUTPUT->notification($strentrieslefttoadd); |
c861f079 | 448 | } |
449 | ||
450 | /// 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) | |
451 | $requiredentries_allowed = true; | |
452 | if ($data->requiredentriestoview > 0 && $numentries < $data->requiredentriestoview && !has_capability('mod/data:manageentries', $context)) { | |
453 | $data->entrieslefttoview = $data->requiredentriestoview - $numentries; | |
454 | $strentrieslefttoaddtoview = get_string('entrieslefttoaddtoview', 'data', $data); | |
4102b449 | 455 | echo $OUTPUT->notification($strentrieslefttoaddtoview); |
eeeb4f2a | 456 | $requiredentries_allowed = false; |
457 | } | |
3d4b223a | 458 | |
bb5740f4 | 459 | /// setup group and approve restrictions |
460 | if (!$approvecap && $data->approval) { | |
7900ecb0 | 461 | if (isloggedin()) { |
e3487936 | 462 | $approveselect = ' AND (r.approved=1 OR r.userid=:myid1) '; |
463 | $params['myid1'] = $USER->id; | |
7900ecb0 | 464 | } else { |
465 | $approveselect = ' AND r.approved=1 '; | |
466 | } | |
4431d2e0 | 467 | } else { |
7900ecb0 | 468 | $approveselect = ' '; |
4431d2e0 | 469 | } |
3d4b223a | 470 | |
7900ecb0 | 471 | if ($currentgroup) { |
e3487936 | 472 | $groupselect = " AND (r.groupid = :currentgroup OR r.groupid = 0)"; |
473 | $params['currentgroup'] = $currentgroup; | |
64452eb4 | 474 | } else { |
768a2cea AG |
475 | if ($canviewallrecords) { |
476 | $groupselect = ' '; | |
477 | } else { | |
478 | // If separate groups are enabled and the user isn't in a group or | |
479 | // a teacher, manager, admin etc, then just show them entries for 'All participants'. | |
480 | $groupselect = " AND r.groupid = 0"; | |
481 | } | |
64452eb4 | 482 | } |
3d45b8e5 | 483 | |
5e1f1a6e | 484 | // Init some variables to be used by advanced search |
485 | $advsearchselect = ''; | |
486 | $advwhere = ''; | |
487 | $advtables = ''; | |
488 | $advparams = array(); | |
489 | ||
7900ecb0 | 490 | /// Find the field we are sorting on |
3239b010 | 491 | if ($sort <= 0 or !$sortfield = data_get_field_from_id($sort, $data)) { |
bb5740f4 | 492 | |
493 | switch ($sort) { | |
494 | case DATA_LASTNAME: | |
495 | $ordering = "u.lastname $order, u.firstname $order"; | |
496 | break; | |
497 | case DATA_FIRSTNAME: | |
498 | $ordering = "u.firstname $order, u.lastname $order"; | |
499 | break; | |
500 | case DATA_APPROVED: | |
501 | $ordering = "r.approved $order, r.timecreated $order"; | |
502 | break; | |
3239b010 | 503 | case DATA_TIMEMODIFIED: |
504 | $ordering = "r.timemodified $order"; | |
505 | break; | |
506 | case DATA_TIMEADDED: | |
bb5740f4 | 507 | default: |
3239b010 | 508 | $sort = 0; |
bb5740f4 | 509 | $ordering = "r.timecreated $order"; |
714bec74 | 510 | } |
511 | ||
512 | $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname'; | |
513 | $count = ' COUNT(DISTINCT c.recordid) '; | |
e3487936 | 514 | $tables = '{data_content} c,{data_records} r, {data_content} cs, {user} u '; |
714bec74 | 515 | $where = 'WHERE c.recordid = r.id |
e3487936 | 516 | AND r.dataid = :dataid |
714bec74 | 517 | AND r.userid = u.id |
518 | AND cs.recordid = r.id '; | |
e3487936 | 519 | $params['dataid'] = $data->id; |
714bec74 | 520 | $sortorder = ' ORDER BY '.$ordering.', r.id ASC '; |
521 | $searchselect = ''; | |
522 | ||
523 | // If requiredentries is not reached, only show current user's entries | |
524 | if (!$requiredentries_allowed) { | |
e3487936 | 525 | $where .= ' AND u.id = :myid2 '; |
526 | $params['myid2'] = $USER->id; | |
714bec74 | 527 | } |
528 | ||
529 | if (!empty($advanced)) { //If advanced box is checked. | |
e3487936 | 530 | $i = 0; |
714bec74 | 531 | foreach($search_array as $key => $val) { //what does $search_array hold? |
532 | if ($key == DATA_FIRSTNAME or $key == DATA_LASTNAME) { | |
e3487936 | 533 | $i++; |
800bb0f7 | 534 | $searchselect .= " AND ".$DB->sql_like($val->field, ":search_flname_$i", false); |
e3487936 | 535 | $params['search_flname_'.$i] = "%$val->data%"; |
714bec74 | 536 | continue; |
537 | } | |
5e1f1a6e | 538 | $advtables .= ', {data_content} c'.$key.' '; |
539 | $advwhere .= ' AND c'.$key.'.recordid = r.id'; | |
540 | $advsearchselect .= ' AND ('.$val->sql.') '; | |
541 | $advparams = array_merge($advparams, $val->params); | |
714bec74 | 542 | } |
543 | } else if ($search) { | |
800bb0f7 | 544 | $searchselect = " AND (".$DB->sql_like('cs.content', ':search1', false)." OR ".$DB->sql_like('u.firstname', ':search2', false)." OR ".$DB->sql_like('u.lastname', ':search3', false)." ) "; |
e3487936 | 545 | $params['search1'] = "%$search%"; |
546 | $params['search2'] = "%$search%"; | |
547 | $params['search3'] = "%$search%"; | |
714bec74 | 548 | } else { |
549 | $searchselect = ' '; | |
550 | } | |
551 | ||
3239b010 | 552 | } else { |
7900ecb0 | 553 | |
6e6878b5 EL |
554 | $sortcontent = $DB->sql_compare_text('c.' . $sortfield->get_sort_field()); |
555 | $sortcontentfull = $sortfield->get_sort_sql($sortcontent); | |
714bec74 | 556 | |
0a498686 | 557 | $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname, ' . $sortcontentfull . ' AS sortorder '; |
7900ecb0 | 558 | $count = ' COUNT(DISTINCT c.recordid) '; |
e3487936 | 559 | $tables = '{data_content} c, {data_records} r, {data_content} cs, {user} u '; |
7900ecb0 | 560 | $where = 'WHERE c.recordid = r.id |
e3487936 | 561 | AND c.fieldid = :sort |
562 | AND r.dataid = :dataid | |
7900ecb0 | 563 | AND r.userid = u.id |
714bec74 | 564 | AND cs.recordid = r.id '; |
e3487936 | 565 | $params['dataid'] = $data->id; |
566 | $params['sort'] = $sort; | |
0a498686 | 567 | $sortorder = ' ORDER BY sortorder '.$order.' , r.id ASC '; |
7900ecb0 | 568 | $searchselect = ''; |
714bec74 | 569 | |
87518137 | 570 | // If requiredentries is not reached, only show current user's entries |
571 | if (!$requiredentries_allowed) { | |
572 | $where .= ' AND u.id = ' . $USER->id; | |
e3487936 | 573 | $params['myid2'] = $USER->id; |
87518137 | 574 | } |
db546510 | 575 | $i = 0; |
714bec74 | 576 | if (!empty($advanced)) { //If advanced box is checked. |
577 | foreach($search_array as $key => $val) { //what does $search_array hold? | |
578 | if ($key == DATA_FIRSTNAME or $key == DATA_LASTNAME) { | |
e3487936 | 579 | $i++; |
800bb0f7 | 580 | $searchselect .= " AND ".$DB->sql_like($val->field, ":search_flname_$i", false); |
e3487936 | 581 | $params['search_flname_'.$i] = "%$val->data%"; |
714bec74 | 582 | continue; |
583 | } | |
5e1f1a6e | 584 | $advtables .= ', {data_content} c'.$key.' '; |
585 | $advwhere .= ' AND c'.$key.'.recordid = r.id AND c'.$key.'.fieldid = '.$key; | |
586 | $advsearchselect .= ' AND ('.$val->sql.') '; | |
587 | $advparams = array_merge($advparams, $val->params); | |
7900ecb0 | 588 | } |
714bec74 | 589 | } else if ($search) { |
800bb0f7 | 590 | $searchselect = " AND (".$DB->sql_like('cs.content', ':search1', false)." OR ".$DB->sql_like('u.firstname', ':search2', false)." OR ".$DB->sql_like('u.lastname', ':search3', false)." ) "; |
e3487936 | 591 | $params['search1'] = "%$search%"; |
592 | $params['search2'] = "%$search%"; | |
593 | $params['search3'] = "%$search%"; | |
7900ecb0 | 594 | } else { |
595 | $searchselect = ' '; | |
714bec74 | 596 | } |
7900ecb0 | 597 | } |
3d4b223a | 598 | |
7900ecb0 | 599 | /// To actually fetch the records |
3d45b8e5 | 600 | |
5e1f1a6e | 601 | $fromsql = "FROM $tables $advtables $where $advwhere $groupselect $approveselect $searchselect $advsearchselect"; |
eeeb4f2a | 602 | $sqlcount = "SELECT $count $fromsql"; // Total number of records when searching |
eeeb4f2a | 603 | $sqlmax = "SELECT $count FROM $tables $where $groupselect $approveselect"; // number of all recoirds user may see |
5e1f1a6e | 604 | $allparams = array_merge($params, $advparams); |
3d45b8e5 | 605 | |
768a2cea AG |
606 | // Provide initial sql statements and parameters to reduce the number of total records. |
607 | $selectdata = $groupselect . $approveselect; | |
608 | $initialparams = array(); | |
609 | if ($currentgroup) { | |
610 | $initialparams['currentgroup'] = $params['currentgroup']; | |
611 | } | |
612 | if (!$approvecap && $data->approval && isloggedin()) { | |
613 | $initialparams['myid1'] = $params['myid1']; | |
614 | } | |
615 | ||
616 | $recordids = data_get_all_recordids($data->id, $selectdata, $initialparams); | |
db546510 | 617 | $newrecordids = data_get_advance_search_ids($recordids, $search_array, $data->id); |
0ba303e8 | 618 | $totalcount = count($newrecordids); |
db546510 | 619 | |
620 | if (!empty($advanced)) { | |
621 | $advancedsearchsql = data_get_advanced_search_sql($sort, $data, $newrecordids, $selectdata, $sortorder); | |
622 | $sqlselect = $advancedsearchsql['sql']; | |
623 | $allparams = array_merge($allparams, $advancedsearchsql['params']); | |
624 | } else { | |
625 | $sqlselect = "SELECT $what $fromsql $sortorder"; | |
626 | } | |
3d45b8e5 | 627 | |
db546510 | 628 | /// Work out the paging numbers and counts |
5e1f1a6e | 629 | if (empty($searchselect) && empty($advsearchselect)) { |
eeeb4f2a | 630 | $maxcount = $totalcount; |
631 | } else { | |
db546510 | 632 | $maxcount = count($recordids); |
eeeb4f2a | 633 | } |
3d4b223a | 634 | |
7900ecb0 | 635 | if ($record) { // We need to just show one, so where is it in context? |
636 | $nowperpage = 1; | |
637 | $mode = 'single'; | |
db79db63 AG |
638 | $page = 0; |
639 | // TODO MDL-33797 - Reduce this or consider redesigning the paging system. | |
640 | if ($allrecordids = $DB->get_fieldset_sql($sqlselect, $allparams)) { | |
641 | $page = (int)array_search($record->id, $allrecordids); | |
642 | unset($allrecordids); | |
643 | } | |
7900ecb0 | 644 | } else if ($mode == 'single') { // We rely on ambient $page settings |
645 | $nowperpage = 1; | |
3d45b8e5 | 646 | |
7900ecb0 | 647 | } else { |
648 | $nowperpage = $perpage; | |
649 | } | |
3d45b8e5 | 650 | |
7900ecb0 | 651 | /// Get the actual records |
8429163d | 652 | |
5e1f1a6e | 653 | if (!$records = $DB->get_records_sql($sqlselect, $allparams, $page * $nowperpage, $nowperpage)) { |
eeeb4f2a | 654 | // Nothing to show! |
7900ecb0 | 655 | if ($record) { // Something was requested so try to show that at least (bug 5132) |
656 | if (has_capability('mod/data:manageentries', $context) || empty($data->approval) || | |
657 | $record->approved || (isloggedin() && $record->userid == $USER->id)) { | |
658 | if (!$currentgroup || $record->groupid == $currentgroup || $record->groupid == 0) { | |
eeeb4f2a | 659 | // OK, we can show this one |
660 | $records = array($record->id => $record); | |
661 | $totalcount = 1; | |
7900ecb0 | 662 | } |
3dec563c | 663 | } |
664 | } | |
eeeb4f2a | 665 | } |
666 | ||
667 | if (empty($records)) { | |
668 | if ($maxcount){ | |
39790bd8 | 669 | $a = new stdClass(); |
eeeb4f2a | 670 | $a->max = $maxcount; |
671 | $a->reseturl = "view.php?id=$cm->id&mode=$mode&search=&advanced=0"; | |
4102b449 | 672 | echo $OUTPUT->notification(get_string('foundnorecords','data', $a)); |
7900ecb0 | 673 | } else { |
4102b449 | 674 | echo $OUTPUT->notification(get_string('norecords','data')); |
7900ecb0 | 675 | } |
cf3e199b | 676 | |
eeeb4f2a | 677 | } else { // We have some records to print |
678 | ||
679 | if ($maxcount != $totalcount) { | |
39790bd8 | 680 | $a = new stdClass(); |
eeeb4f2a | 681 | $a->num = $totalcount; |
682 | $a->max = $maxcount; | |
683 | $a->reseturl = "view.php?id=$cm->id&mode=$mode&search=&advanced=0"; | |
4102b449 | 684 | echo $OUTPUT->notification(get_string('foundrecords', 'data', $a), 'notifysuccess'); |
eeeb4f2a | 685 | } |
68c88622 | 686 | |
c04b01f4 AD |
687 | if ($mode == 'single') { // Single template |
688 | $baseurl = 'view.php?d=' . $data->id . '&mode=single&'; | |
2d9b3ef2 | 689 | if (!empty($search)) { |
c04b01f4 AD |
690 | $baseurl .= 'filter=1&'; |
691 | } | |
692 | if (!empty($page)) { | |
693 | $baseurl .= 'page=' . $page; | |
2d9b3ef2 | 694 | } |
929d7a83 | 695 | echo $OUTPUT->paging_bar($totalcount, $page, $nowperpage, $baseurl); |
3d45b8e5 | 696 | |
7900ecb0 | 697 | if (empty($data->singletemplate)){ |
4102b449 | 698 | echo $OUTPUT->notification(get_string('nosingletemplate','data')); |
7900ecb0 | 699 | data_generate_default_template($data, 'singletemplate', 0, false, false); |
700 | } | |
3d4b223a | 701 | |
d251b259 AD |
702 | //data_print_template() only adds ratings for singletemplate which is why we're attaching them here |
703 | //attach ratings to data records | |
704 | require_once($CFG->dirroot.'/rating/lib.php'); | |
2b04c41c SH |
705 | if ($data->assessed != RATING_AGGREGATE_NONE) { |
706 | $ratingoptions = new stdClass; | |
195a0848 | 707 | $ratingoptions->context = $context; |
2c2ff8d5 | 708 | $ratingoptions->component = 'mod_data'; |
2b04c41c | 709 | $ratingoptions->ratingarea = 'entry'; |
198ff498 AD |
710 | $ratingoptions->items = $records; |
711 | $ratingoptions->aggregate = $data->assessed;//the aggregation method | |
712 | $ratingoptions->scaleid = $data->scale; | |
713 | $ratingoptions->userid = $USER->id; | |
714 | $ratingoptions->returnurl = $CFG->wwwroot.'/mod/data/'.$baseurl; | |
715 | $ratingoptions->assesstimestart = $data->assesstimestart; | |
716 | $ratingoptions->assesstimefinish = $data->assesstimefinish; | |
198ff498 AD |
717 | |
718 | $rm = new rating_manager(); | |
719 | $records = $rm->get_ratings($ratingoptions); | |
720 | } | |
d251b259 | 721 | |
7900ecb0 | 722 | data_print_template('singletemplate', $records, $data, $search, $page); |
3d45b8e5 | 723 | |
929d7a83 | 724 | echo $OUTPUT->paging_bar($totalcount, $page, $nowperpage, $baseurl); |
3d45b8e5 | 725 | |
7900ecb0 | 726 | } else { // List template |
727 | $baseurl = 'view.php?d='.$data->id.'&'; | |
728 | //send the advanced flag through the URL so it is remembered while paging. | |
729 | $baseurl .= 'advanced='.$advanced.'&'; | |
2d9b3ef2 | 730 | if (!empty($search)) { |
731 | $baseurl .= 'filter=1&'; | |
732 | } | |
7900ecb0 | 733 | //pass variable to allow determining whether or not we are paging through results. |
734 | $baseurl .= 'paging='.$paging.'&'; | |
3d45b8e5 | 735 | |
929d7a83 | 736 | echo $OUTPUT->paging_bar($totalcount, $page, $nowperpage, $baseurl); |
3d45b8e5 | 737 | |
7900ecb0 | 738 | if (empty($data->listtemplate)){ |
4102b449 | 739 | echo $OUTPUT->notification(get_string('nolisttemplate','data')); |
7900ecb0 | 740 | data_generate_default_template($data, 'listtemplate', 0, false, false); |
741 | } | |
742 | echo $data->listtemplateheader; | |
743 | data_print_template('listtemplate', $records, $data, $search, $page); | |
744 | echo $data->listtemplatefooter; | |
745 | ||
929d7a83 | 746 | echo $OUTPUT->paging_bar($totalcount, $page, $nowperpage, $baseurl); |
3d45b8e5 | 747 | } |
3d45b8e5 | 748 | |
3d45b8e5 | 749 | } |
7900ecb0 | 750 | } |
8429163d | 751 | |
7900ecb0 | 752 | $search = trim($search); |
753 | if (empty($records)) { | |
754 | $records = array(); | |
f852d652 | 755 | } |
756 | ||
6708a1f5 | 757 | if ($mode == '' && !empty($CFG->enableportfolios)) { |
c3e1b5bd PL |
758 | require_once($CFG->libdir . '/portfoliolib.php'); |
759 | $button = new portfolio_add_button(); | |
760 | $button->set_callback_options('data_portfolio_caller', array('id' => $cm->id), '/mod/data/locallib.php'); | |
761 | if (data_portfolio_caller::has_files($data)) { | |
762 | $button->set_formats(array(PORTFOLIO_FORMAT_RICHHTML, PORTFOLIO_FORMAT_LEAP2A)); // no plain html for us | |
763 | } | |
764 | echo $button->to_html(PORTFOLIO_ADD_FULL_FORM); | |
765 | } | |
766 | ||
7900ecb0 | 767 | //Advanced search form doesn't make sense for single (redirects list view) |
eeeb4f2a | 768 | if (($maxcount || $mode == 'asearch') && $mode != 'single') { |
7900ecb0 | 769 | data_print_preference_form($data, $perpage, $search, $sort, $order, $search_array, $advanced, $mode); |
64452eb4 | 770 | } |
4a9eecc4 | 771 | } |
d4a03c00 | 772 | |
4a9eecc4 | 773 | echo $OUTPUT->footer(); |