Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * This file is part of the Database module for Moodle
19
 *
20
 * @copyright 2005 Martin Dougiamas  http://dougiamas.com
21
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 * @package mod_data
23
 */
24
 
25
use mod_data\manager;
26
 
27
require_once(__DIR__ . '/../../config.php');
28
require_once($CFG->dirroot . '/mod/data/locallib.php');
29
require_once($CFG->libdir . '/rsslib.php');
30
 
31
/// One of these is necessary!
32
$id = optional_param('id', 0, PARAM_INT);  // course module id
33
$d = optional_param('d', 0, PARAM_INT);   // database id
34
$rid = optional_param('rid', 0, PARAM_INT);    //record id
35
$mode = optional_param('mode', '', PARAM_ALPHA);    // Force the browse mode  ('single')
36
$filter = optional_param('filter', 0, PARAM_BOOL);
37
// search filter will only be applied when $filter is true
38
 
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
$disapprove = optional_param('disapprove', 0, PARAM_INT);    // disapproval recordid
44
$delete = optional_param('delete', 0, PARAM_INT);    //delete recordid
45
$multidelete = optional_param_array('delcheck', null, PARAM_INT);
46
$serialdelete = optional_param('serialdelete', null, PARAM_RAW);
47
$confirm = optional_param('confirm', 0, PARAM_INT);
48
 
49
$record = null;
50
 
51
if ($id) {
52
    list($course, $cm) = get_course_and_cm_from_cmid($id, manager::MODULE);
53
    $manager = manager::create_from_coursemodule($cm);
54
} else if ($rid) {
55
    $record = $DB->get_record('data_records', ['id' => $rid], '*', MUST_EXIST);
56
    $manager = manager::create_from_data_record($record);
57
    $cm = $manager->get_coursemodule();
58
    $course = get_course($cm->course);
59
} else {   // We must have $d.
60
    $data = $DB->get_record('data', ['id' => $d], '*', MUST_EXIST);
61
    $manager = manager::create_from_instance($data);
62
    $cm = $manager->get_coursemodule();
63
    $course = get_course($cm->course);
64
}
65
 
66
$data = $manager->get_instance();
67
$context = $manager->get_context();
68
 
69
require_login($course, true, $cm);
70
 
71
require_once($CFG->dirroot . '/comment/lib.php');
72
comment::init();
73
 
74
require_capability('mod/data:viewentry', $context);
75
 
76
/// Check further parameters that set browsing preferences
77
if (!isset($SESSION->dataprefs)) {
78
    $SESSION->dataprefs = array();
79
}
80
if (!isset($SESSION->dataprefs[$data->id])) {
81
    $SESSION->dataprefs[$data->id] = array();
82
    $SESSION->dataprefs[$data->id]['search'] = '';
83
    $SESSION->dataprefs[$data->id]['search_array'] = array();
84
    $SESSION->dataprefs[$data->id]['sort'] = $data->defaultsort;
85
    $SESSION->dataprefs[$data->id]['advanced'] = 0;
86
    $SESSION->dataprefs[$data->id]['order'] = ($data->defaultsortdir == 0) ? 'ASC' : 'DESC';
87
}
88
 
89
// reset advanced form
90
if (!is_null(optional_param('resetadv', null, PARAM_RAW))) {
91
    $SESSION->dataprefs[$data->id]['search_array'] = array();
92
    // we need the redirect to cleanup the form state properly
93
    redirect("view.php?id=$cm->id&amp;mode=$mode&amp;search=&amp;advanced=1");
94
}
95
 
96
$advanced = optional_param('advanced', -1, PARAM_INT);
97
if ($advanced == -1) {
98
    $advanced = $SESSION->dataprefs[$data->id]['advanced'];
99
} else {
100
    if (!$advanced) {
101
        // explicitly switched to normal mode - discard all advanced search settings
102
        $SESSION->dataprefs[$data->id]['search_array'] = array();
103
    }
104
    $SESSION->dataprefs[$data->id]['advanced'] = $advanced;
105
}
106
 
107
$search_array = $SESSION->dataprefs[$data->id]['search_array'];
108
 
109
if (!empty($advanced)) {
110
    $search = '';
111
 
112
    //Added to ammend paging error. This error would occur when attempting to go from one page of advanced
113
    //search results to another.  All fields were reset in the page transfer, and there was no way of determining
114
    //whether or not the user reset them.  This would cause a blank search to execute whenever the user attempted
115
    //to see any page of results past the first.
116
    //This fix works as follows:
117
    //$paging flag is set to false when page 0 of the advanced search results is viewed for the first time.
118
    //Viewing any page of results after page 0 passes the false $paging flag though the URL (see line 523) and the
119
    //execution falls through to the second condition below, allowing paging to be set to true.
120
    //Paging remains true and keeps getting passed though the URL until a new search is performed
121
    //(even if page 0 is revisited).
122
    //A false $paging flag generates advanced search results based on the fields input by the user.
123
    //A true $paging flag generates davanced search results from the $SESSION global.
124
 
125
    $paging = optional_param('paging', NULL, PARAM_BOOL);
126
    if($page == 0 && !isset($paging)) {
127
        $paging = false;
128
    }
129
    else {
130
        $paging = true;
131
    }
132
 
133
    // Now build the advanced search array.
134
    list($search_array, $search) = data_build_search_array($data, $paging, $search_array);
135
    $SESSION->dataprefs[$data->id]['search_array'] = $search_array;     // Make it sticky.
136
 
137
} else {
138
    $search = optional_param('search', $SESSION->dataprefs[$data->id]['search'], PARAM_NOTAGS);
139
    //Paging variable not used for standard search. Set it to null.
140
    $paging = NULL;
141
}
142
 
143
// Disable search filters if $filter is not true:
144
if (! $filter) {
145
    $search = '';
146
}
147
 
148
$SESSION->dataprefs[$data->id]['search'] = $search;   // Make it sticky
149
 
150
$sort = optional_param('sort', $SESSION->dataprefs[$data->id]['sort'], PARAM_INT);
151
$SESSION->dataprefs[$data->id]['sort'] = $sort;       // Make it sticky
152
 
153
$order = (optional_param('order', $SESSION->dataprefs[$data->id]['order'], PARAM_ALPHA) == 'ASC') ? 'ASC': 'DESC';
154
$SESSION->dataprefs[$data->id]['order'] = $order;     // Make it sticky
155
 
156
 
157
$oldperpage = get_user_preferences('data_perpage_'.$data->id, 10);
158
$perpage = optional_param('perpage', $oldperpage, PARAM_INT);
159
 
160
if ($perpage < 2) {
161
    $perpage = 2;
162
}
163
if ($perpage != $oldperpage) {
164
    set_user_preference('data_perpage_'.$data->id, $perpage);
165
}
166
 
167
// Trigger module viewed event and completion.
168
$manager->set_module_viewed($course);
169
 
170
$urlparams = array('d' => $data->id);
171
if ($record) {
172
    $urlparams['rid'] = $record->id;
173
}
174
if ($mode) {
175
    $urlparams['mode'] = $mode;
176
}
177
if ($page) {
178
    $urlparams['page'] = $page;
179
}
180
if ($filter) {
181
    $urlparams['filter'] = $filter;
182
}
183
$pageurl = new moodle_url('/mod/data/view.php', $urlparams);
184
 
185
// Initialize $PAGE, compute blocks.
186
$PAGE->set_url($pageurl);
187
 
188
if (($edit != -1) and $PAGE->user_allowed_editing()) {
189
    $USER->editing = $edit;
190
}
191
 
192
$courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
193
 
194
/// RSS and CSS and JS meta
195
$meta = '';
196
if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
197
    $rsstitle = $courseshortname . ': ' . format_string($data->name);
198
    rss_add_http_header($context, 'mod_data', $data, $rsstitle);
199
}
200
if ($data->csstemplate) {
201
    $PAGE->requires->css('/mod/data/css.php?d='.$data->id);
202
}
203
if ($data->jstemplate) {
204
    $PAGE->requires->js('/mod/data/js.php?d='.$data->id, true);
205
}
206
 
207
/// Print the page header
208
// Note: MDL-19010 there will be further changes to printing header and blocks.
209
// The code will be much nicer than this eventually.
210
 
211
if ($PAGE->user_allowed_editing() && !$PAGE->theme->haseditswitch) {
212
    // Change URL parameter and block display string value depending on whether editing is enabled or not
213
    if ($PAGE->user_is_editing()) {
214
        $urlediting = 'off';
215
        $strediting = get_string('blockseditoff');
216
    } else {
217
        $urlediting = 'on';
218
        $strediting = get_string('blocksediton');
219
    }
220
    $editurl = new moodle_url($CFG->wwwroot.'/mod/data/view.php', ['id' => $cm->id, 'edit' => $urlediting]);
221
    $PAGE->set_button($OUTPUT->single_button($editurl, $strediting));
222
}
223
 
224
if ($mode == 'asearch') {
225
    $PAGE->navbar->add(get_string('search'));
226
}
227
 
228
$PAGE->add_body_class('mediumwidth');
229
$titleparts = [
230
    format_string($data->name),
231
    format_string($course->fullname),
232
];
233
if (!empty(trim($search))) {
234
    // Indicate search results on page title when searching.
235
    array_unshift($titleparts, get_string('searchresults', 'data', s($search)));
236
} else if (!empty($delete) && empty($confirm)) {
237
    // Displaying the delete confirmation page.
238
    array_unshift($titleparts, get_string('deleteentry', 'data'));
239
} else if ($record !== null || $mode == 'single') {
240
    // Indicate on the page tile if the user is viewing this page on single view mode.
241
    array_unshift($titleparts, get_string('single', 'data'));
242
}
243
$PAGE->set_title(implode(moodle_page::TITLE_SEPARATOR, $titleparts));
244
$PAGE->set_heading($course->fullname);
245
$PAGE->force_settings_menu(true);
246
if ($delete && confirm_sesskey() && (data_user_can_manage_entry($delete, $data, $context))) {
247
    $PAGE->activityheader->disable();
248
}
249
 
250
// Check to see if groups are being used here.
251
// We need the most up to date current group value. Make sure it is updated at this point.
252
$currentgroup = groups_get_activity_group($cm, true);
253
$groupmode = groups_get_activity_groupmode($cm);
254
$canmanageentries = has_capability('mod/data:manageentries', $context);
255
echo $OUTPUT->header();
256
 
257
if (!$manager->has_fields()) {
258
    // It's a brand-new database. There are no fields.
259
    $renderer = $manager->get_renderer();
260
    echo $renderer->render_database_zero_state($manager);
261
    echo $OUTPUT->footer();
262
    // Don't check the rest of the options. There is no field, there is nothing else to work with.
263
    exit;
264
}
265
 
266
// Detect entries not approved yet and show hint instead of not found error.
267
if ($record and !data_can_view_record($data, $record, $currentgroup, $canmanageentries)) {
268
    throw new \moodle_exception('notapprovederror', 'data');
269
}
270
 
271
// Do we need to show a link to the RSS feed for the records?
272
//this links has been Settings (database activity administration) block
273
/*if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
274
    echo '<div style="float:right;">';
275
    rss_print_link($context->id, $USER->id, 'mod_data', $data->id, get_string('rsstype'));
276
    echo '</div>';
277
    echo '<div style="clear:both;"></div>';
278
}*/
279
 
280
if ($data->intro and empty($page) and empty($record) and $mode != 'single') {
281
    $options = new stdClass();
282
    $options->noclean = true;
283
}
284
 
285
/// Delete any requested records
286
 
287
if ($delete && confirm_sesskey() && (data_user_can_manage_entry($delete, $data, $context))) {
288
    if ($confirm) {
289
        if (data_delete_record($delete, $data, $course->id, $cm->id)) {
290
            echo $OUTPUT->notification(get_string('recorddeleted','data'), 'notifysuccess');
291
        }
292
    } else {   // Print a confirmation page
293
        $userfieldsapi = \core_user\fields::for_userpic()->excluding('id');
294
        $allnamefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
295
        $dbparams = array($delete);
296
        if ($deleterecord = $DB->get_record_sql("SELECT dr.*, $allnamefields
297
                                                   FROM {data_records} dr
298
                                                        JOIN {user} u ON dr.userid = u.id
299
                                                  WHERE dr.id = ?", $dbparams, MUST_EXIST)) { // Need to check this is valid.
300
            if ($deleterecord->dataid == $data->id) {                       // Must be from this database
301
                echo $OUTPUT->heading(get_string('deleteentry', 'mod_data'), 2, 'mb-4');
302
                $deletebutton = new single_button(
303
                    new moodle_url('/mod/data/view.php?d=' . $data->id . '&delete=' . $delete . '&confirm=1'),
304
                    get_string('delete'), 'post',
305
                    single_button::BUTTON_DANGER
306
                );
307
                echo $OUTPUT->confirm(get_string('confirmdeleterecord','data'),
308
                        $deletebutton, 'view.php?d='.$data->id);
309
 
310
                $records[] = $deleterecord;
311
                $parser = $manager->get_template('singletemplate');
312
                echo $parser->parse_entries($records);
313
 
314
                echo $OUTPUT->footer();
315
                exit;
316
            }
317
        }
318
    }
319
}
320
 
321
 
322
// Multi-delete.
323
if ($serialdelete) {
324
    $multidelete = json_decode($serialdelete);
325
}
326
 
327
if ($multidelete && confirm_sesskey() && $canmanageentries) {
328
    if ($confirm = optional_param('confirm', 0, PARAM_INT)) {
329
        foreach ($multidelete as $value) {
330
            data_delete_record($value, $data, $course->id, $cm->id);
331
        }
332
    } else {
333
        $validrecords = array();
334
        $recordids = array();
335
        foreach ($multidelete as $value) {
336
            $userfieldsapi = \core_user\fields::for_userpic()->excluding('id');
337
            $allnamefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
338
            $dbparams = array('id' => $value);
339
            if ($deleterecord = $DB->get_record_sql("SELECT dr.*, $allnamefields
340
                                                       FROM {data_records} dr
341
                                                       JOIN {user} u ON dr.userid = u.id
342
                                                      WHERE dr.id = ?", $dbparams)) { // Need to check this is valid.
343
                if ($deleterecord->dataid == $data->id) {  // Must be from this database.
344
                    $validrecords[] = $deleterecord;
345
                    $recordids[] = $deleterecord->id;
346
                }
347
            }
348
        }
349
        $serialiseddata = json_encode($recordids);
350
        $submitactions = array('d' => $data->id, 'sesskey' => sesskey(), 'confirm' => '1', 'serialdelete' => $serialiseddata);
351
        $action = new moodle_url('/mod/data/view.php', $submitactions);
352
        $cancelurl = new moodle_url('/mod/data/view.php', array('d' => $data->id));
353
        $deletebutton = new single_button($action, get_string('delete'), 'post', single_button::BUTTON_DANGER);
354
        echo $OUTPUT->confirm(get_string('confirmdeleterecords', 'data'), $deletebutton, $cancelurl);
355
        $parser = $manager->get_template('listtemplate');
356
        echo $parser->parse_entries($validrecords);
357
        echo $OUTPUT->footer();
358
        exit;
359
    }
360
}
361
 
362
// If data activity closed dont let students in.
363
// No need to display warnings because activity dates are displayed at the top of the page.
364
list($showactivity, $warnings) = data_get_time_availability_status($data, $canmanageentries);
365
 
366
if ($showactivity) {
367
 
368
    if ($mode == 'asearch') {
369
        $maxcount = 0;
370
        data_print_preference_form($data, $perpage, $search, $sort, $order, $search_array, $advanced, $mode);
371
 
372
    } else {
373
        // Approve or disapprove any requested records
374
        $approvecap = has_capability('mod/data:approve', $context);
375
 
376
        if (($approve || $disapprove) && confirm_sesskey() && $approvecap) {
377
            $newapproved = $approve ? true : false;
378
            $recordid = $newapproved ? $approve : $disapprove;
379
            if ($approverecord = $DB->get_record('data_records', array('id' => $recordid))) {   // Need to check this is valid
380
                if ($approverecord->dataid == $data->id) {                       // Must be from this database
381
                    data_approve_entry($approverecord->id, $newapproved);
382
                    $msgkey = $newapproved ? 'recordapproved' : 'recorddisapproved';
383
                    echo $OUTPUT->notification(get_string($msgkey, 'data'), 'notifysuccess');
384
                }
385
            }
386
        }
387
 
388
        $numentries = data_numentries($data);
389
    /// Check the number of entries required against the number of entries already made (doesn't apply to teachers)
390
        if ($data->entriesleft = data_get_entries_left_to_add($data, $numentries, $canmanageentries)) {
391
            $strentrieslefttoadd = get_string('entrieslefttoadd', 'data', $data);
392
            echo $OUTPUT->notification($strentrieslefttoadd);
393
        }
394
 
395
    /// 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)
396
        $requiredentries_allowed = true;
397
        if ($data->entrieslefttoview = data_get_entries_left_to_view($data, $numentries, $canmanageentries)) {
398
            $strentrieslefttoaddtoview = get_string('entrieslefttoaddtoview', 'data', $data);
399
            echo $OUTPUT->notification($strentrieslefttoaddtoview);
400
            $requiredentries_allowed = false;
401
        }
402
 
403
        if ($groupmode != NOGROUPS) {
404
            $returnurl = new moodle_url('/mod/data/view.php', ['d' => $data->id, 'mode' => $mode, 'search' => s($search),
405
                'sort' => s($sort), 'order' => s($order)]);
406
            echo html_writer::div(groups_print_activity_menu($cm, $returnurl, true), 'mb-3');
407
        }
408
 
409
        // Search for entries.
410
        list($records, $maxcount, $totalcount, $page, $nowperpage, $sort, $mode) =
411
            data_search_entries($data, $cm, $context, $mode, $currentgroup, $search, $sort, $order, $page, $perpage, $advanced, $search_array, $record);
412
        $hasrecords = !empty($records);
413
 
414
        if ($maxcount == 0) {
415
            $renderer = $manager->get_renderer();
416
            echo $renderer->render_empty_database($manager);
417
            echo $OUTPUT->footer();
418
            // There is no entry, so makes no sense to check different views, pagination, etc.
419
            exit;
420
        }
421
 
422
        $actionbar = new \mod_data\output\action_bar($data->id, $pageurl);
423
        echo $actionbar->get_view_action_bar($hasrecords, $mode);
424
 
425
        // Advanced search form doesn't make sense for single (redirects list view).
426
        if ($maxcount && $mode != 'single') {
427
            data_print_preference_form($data, $perpage, $search, $sort, $order, $search_array, $advanced, $mode);
428
        }
429
 
430
        if (empty($records)) {
431
            if ($maxcount){
432
                $a = new stdClass();
433
                $a->max = $maxcount;
434
                $a->reseturl = "view.php?id=$cm->id&amp;mode=$mode&amp;search=&amp;advanced=0";
435
                echo $OUTPUT->box_start();
436
                echo get_string('foundnorecords', 'data', $a);
437
                echo $OUTPUT->box_end();
438
            } else {
439
                echo $OUTPUT->box_start();
440
                echo get_string('norecords', 'data');
441
                echo $OUTPUT->box_end();
442
            }
443
 
444
        } else {
445
            //  We have some records to print.
446
            $formurl = new moodle_url('/mod/data/view.php', ['d' => $data->id, 'sesskey' => sesskey()]);
447
            echo html_writer::start_tag('form', ['action' => $formurl, 'method' => 'post']);
448
 
449
            if ($maxcount != $totalcount) {
450
                $a = new stdClass();
451
                $a->num = $totalcount;
452
                $a->max = $maxcount;
453
                $a->reseturl = "view.php?id=$cm->id&amp;mode=$mode&amp;search=&amp;advanced=0";
454
                echo $OUTPUT->box_start();
455
                echo get_string('foundrecords', 'data', $a);
456
                echo $OUTPUT->box_end();
457
            }
458
 
459
            if ($mode == 'single') { // Single template
460
                $baseurl = '/mod/data/view.php';
461
                $baseurlparams = ['d' => $data->id, 'mode' => 'single'];
462
                if (!empty($search)) {
463
                    $baseurlparams['filter'] = 1;
464
                }
465
                if (!empty($page)) {
466
                    $baseurlparams['page'] = $page;
467
                }
468
                $baseurl = new moodle_url($baseurl, $baseurlparams);
469
 
470
                echo $OUTPUT->box_start('', 'data-singleview-content');
471
                require_once($CFG->dirroot.'/rating/lib.php');
472
                if ($data->assessed != RATING_AGGREGATE_NONE) {
473
                    $ratingoptions = new stdClass;
474
                    $ratingoptions->context = $context;
475
                    $ratingoptions->component = 'mod_data';
476
                    $ratingoptions->ratingarea = 'entry';
477
                    $ratingoptions->items = $records;
478
                    $ratingoptions->aggregate = $data->assessed;//the aggregation method
479
                    $ratingoptions->scaleid = $data->scale;
480
                    $ratingoptions->userid = $USER->id;
481
                    $ratingoptions->returnurl = $baseurl->out();
482
                    $ratingoptions->assesstimestart = $data->assesstimestart;
483
                    $ratingoptions->assesstimefinish = $data->assesstimefinish;
484
 
485
                    $rm = new rating_manager();
486
                    $records = $rm->get_ratings($ratingoptions);
487
                }
488
 
489
                $options = [
490
                    'search' => $search,
491
                    'page' => $page,
492
                    'baseurl' => $baseurl,
493
                ];
494
                $parser = $manager->get_template('singletemplate', $options);
495
                echo $parser->parse_entries($records);
496
                echo $OUTPUT->box_end();
497
            } else {
498
                // List template.
499
                $baseurl = '/mod/data/view.php';
500
                $baseurlparams = ['d' => $data->id, 'advanced' => $advanced, 'paging' => $paging];
501
                if (!empty($search)) {
502
                    $baseurlparams['filter'] = 1;
503
                }
504
                $baseurl = new moodle_url($baseurl, $baseurlparams);
505
 
506
                echo $OUTPUT->box_start('', 'data-listview-content');
507
                echo $data->listtemplateheader;
508
                $options = [
509
                    'search' => $search,
510
                    'page' => $page,
511
                    'baseurl' => $baseurl,
512
                ];
513
                $parser = $manager->get_template('listtemplate', $options);
514
                echo $parser->parse_entries($records);
515
 
516
                echo $data->listtemplatefooter;
517
                echo $OUTPUT->box_end();
518
            }
519
 
520
            $stickyfooter = new mod_data\output\view_footer(
521
                $manager,
522
                $totalcount,
523
                $page,
524
                $nowperpage,
525
                $baseurl,
526
                $parser
527
            );
528
            echo $OUTPUT->render($stickyfooter);
529
 
530
            echo html_writer::end_tag('form');
531
        }
532
    }
533
 
534
    $search = trim($search);
535
    if (empty($records)) {
536
        $records = array();
537
    }
538
}
539
 
540
echo $OUTPUT->footer();