moodle/mod/data/view.php

389 lines
16 KiB
PHP
Raw Normal View History

<?php // $Id$
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.org //
// //
// Copyright (C) 2005 Martin Dougiamas http://dougiamas.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
require_once('../../config.php');
require_once('lib.php');
require_once($CFG->libdir.'/blocklib.php');
2006-02-13 07:57:54 +00:00
require_once("$CFG->libdir/rsslib.php");
require_once('pagelib.php');
require_login();
$id = optional_param('id', 0, PARAM_INT); // course module id
$d = optional_param('d', 0, PARAM_INT); // database id
$search = optional_param('search','',PARAM_NOTAGS); //search string
$page = optional_param('page', 0, PARAM_INT); //offset of the current record
$rid = optional_param('rid', 0, PARAM_INT); //record id
2006-03-26 05:03:10 +00:00
$approve = optional_param('approve', 0, PARAM_INT); //approval recordid
$delete = optional_param('delete', 0, PARAM_INT); //delete recordid
$perpagemenu = optional_param('perpage1', 0, PARAM_INT); //value from drop down
2006-02-03 08:11:36 +00:00
$sort = optional_param('sort',0,PARAM_INT); //sort by field
$order = optional_param('order','ASC',PARAM_ALPHA); //sort order
2006-02-06 06:13:34 +00:00
$group = optional_param('group','0',PARAM_INT); //groupid
2006-02-03 08:11:36 +00:00
if ($id) {
if (! $cm = get_record('course_modules', 'id', $id)) {
error('Course Module ID was incorrect');
}
if (! $course = get_record('course', 'id', $cm->course)) {
error('Course is misconfigured');
}
if (! $data = get_record('data', 'id', $cm->instance)) {
error('Course module is incorrect');
}
} else {
if (! $data = get_record('data', 'id', $d)) {
error('Data ID is incorrect');
}
if (! $course = get_record('course', 'id', $data->course)) {
error('Course is misconfigured');
}
if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
error('Course Module ID was incorrect');
}
}
if (isteacher($course->id)) {
2006-03-22 08:32:54 +00:00
if (!record_exists('data_fields','dataid',$data->id)) { // Brand new database!
2006-03-24 02:44:05 +00:00
redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
}
}
//set user preference if available
2006-03-26 05:03:10 +00:00
if (isset($_GET['updatepref'])){
if (!$perpage = $perpagemenu){ //if menu not in use, use the text field
$perpage = (int)optional_param('perpage',10);
}
$perpage = ($perpage <= 0) ? 10 : $perpage ;
set_user_preference('data_perpage', $perpage);
}
$d = $data->id;//set this so tabs can work properly
2006-03-22 08:32:54 +00:00
add_to_log($course->id, 'data', 'view', "view.php?id=$cm->id", $data->id, $cm->id);
// Initialize $PAGE, compute blocks
$PAGE = page_create_instance($data->id);
$pageblocks = blocks_setup($PAGE);
$blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]), 210);
if (!empty($edit) && $PAGE->user_allowed_editing()) {
if ($edit == 'on') {
$USER->editing = true;
} else if ($edit == 'off') {
$USER->editing = false;
}
}
/// RSS meta
$rssmeta = '';
if (isset($CFG->enablerssfeeds) && isset($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
$rsspath = rss_get_url($course->id, $USER->id, 'data', $data->id);
$rssmeta = '<link rel="alternate" type="application/rss+xml" ';
$rssmeta .= 'title ="'.$course->shortname.': %fullname%" href="'.$rsspath.'" />';
}
/// Print the page header
$PAGE->print_header($course->shortname.': %fullname%', '', $rssmeta);
echo '<table id="layout-table"><tr>';
if(!empty($CFG->showblocksonmodpages) && (blocks_have_content($pageblocks, BLOCK_POS_LEFT) || $PAGE->user_is_editing())) {
echo '<td style="width: '.$blocks_preferred_width.'px;" id="left-column">';
blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
echo '</td>';
}
echo '<td id="middle-column">';
print_heading(format_string($data->name));
2006-02-13 07:57:54 +00:00
// Do we need to show a link to the RSS feed for the records?
if (isset($CFG->enablerssfeeds) && isset($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
echo '<div style="float:right;">';
2006-03-22 08:32:54 +00:00
rss_print_link($course->id, $USER->id, 'data', $data->id, get_string('rsstype'));
2006-02-13 07:57:54 +00:00
echo '</div>';
echo '<div style="clear:both;"></div>';
}
2006-02-06 04:17:30 +00:00
if ($data->intro) {
print_simple_box(format_text($data->intro), 'center', '70%', '', 5, 'generalbox', 'intro');
echo '<br />';
}
/// Check to see if groups are being used here
if ($groupmode = groupmode($course, $cm)) { // Groups are being used
2006-03-22 08:32:54 +00:00
$currentgroup = setup_and_print_groups($course, $groupmode,
'view.php?d='.$data->id.'&amp;search='.s($search).'&amp;sort='.s($sort).
'&amp;order='.s($order).'&amp;');
} else {
$currentgroup = 0;
}
if ($currentgroup) {
2006-02-06 06:30:18 +00:00
$groupselect = " AND (r.groupid = '$currentgroup' OR r.groupid = 0)";
$groupparam = "&amp;groupid=$currentgroup";
} else {
$groupselect = "";
$groupparam = "";
}
/// Print the tabs
$currenttab = 'browse';
include('tabs.php');
2006-03-26 05:03:10 +00:00
/// Approve any requested records
2006-03-26 05:03:10 +00:00
if ($approve && confirm_sesskey() && isteacher($course->id)) {
if ($record = get_record('data_records', 'id', $approve)) { // Need to check this is valid
if ($record->dataid == $data->id) { // Must be from this database
$newrecord->id = $record->id;
$newrecord->approved = 1;
if (update_record('data_records', $newrecord)) {
notify(get_string('recordapproved','data'), 'notifysuccess');
}
}
}
2006-02-03 08:11:36 +00:00
}
2006-03-22 08:32:54 +00:00
2006-03-26 05:03:10 +00:00
/// Delete any requested records
if ($delete && confirm_sesskey()) {
if (isteacheredit($course) or data_isowner($delete)){
2006-03-22 14:31:32 +00:00
if ($confirm = optional_param('confirm',0,PARAM_INT)) {
if ($contents = get_records('data_content','recordid', $delete)) {
foreach ($contents as $content) { // Delete files or whatever else this field allows
if ($field = data_get_field_from_id($content->fieldid, $data)) { // Might not be there
$field->delete_content($content->recordid);
}
}
2006-03-22 14:31:32 +00:00
}
delete_records('data_content','recordid',$delete);
delete_records('data_records','id',$delete);
2006-03-22 14:31:32 +00:00
add_to_log($course->id, 'data', 'record delete', "view.php?id=$cm->id", $data->id, $cm->id);
2006-03-22 14:31:32 +00:00
notify(get_string('recorddeleted','data'));
} else { // Print a confirmation page
notice_yesno(get_string('confirmdeleterecord','data'),
'view.php?d='.$data->id.'&amp;delete='.$delete.'&amp;confirm=1&amp;sesskey='.sesskey(),
'view.php?d='.$data->id);
print_footer($course);
exit;
}
}
}
2006-03-26 05:03:10 +00:00
// If not editting teacher, check whether user has sufficient records to view
if (!isteacheredit($course->id) and data_numentries($data) < $data->requiredentriestoview){
2006-03-23 05:38:53 +00:00
notify (($data->requiredentriestoview - data_numentries($data)).'&nbsp;'.get_string('insufficiententries','data'));
echo '</td></tr></table>';
print_footer($course);
exit;
}
if ($rid){ //set per page to 1, if looking for 1 specific record
set_user_preference('data_perpage', DATA_PERPAGE_SINGLE);
}
2006-03-10 09:08:59 +00:00
/*****************************
* Setting up page variables *
*****************************/
$perpage = get_user_preferences('data_perpage', 10); //get default per page
2006-02-06 06:13:34 +00:00
$baseurl = 'view.php?d='.$data->id.'&amp;search='.s($search).'&amp;sort='.s($sort).'&amp;order='.s($order).'&amp;group='.$currentgroup.'&amp;';
2006-02-03 08:11:36 +00:00
//if database requires approval, then we need to do some work
//and get those approved entries, or entries belongs to owner
if ((!isteacher($course->id)) && ($data->approval)){
$approvesql = ' AND (r.approved=1 OR r.userid='.$USER->id.') ';
} else {
$approvesql = '';
}
if ($rid){ //only used for single mode, but rid should not appear in multi view anyway
$ridsql = 'AND r.id < '.$rid.' ';
2006-02-03 08:11:36 +00:00
} else {
$ridsql = '';
}
2006-02-03 08:11:36 +00:00
if ($sort) { //supports (sort and search)
//first find the field that we are sorting
$sortfield = data_get_field_from_id($sort, $data);
2006-02-03 08:11:36 +00:00
$sortcontent = $sortfield->get_sort_field();
///SEARCH AND SORT SQL
$sql = 'SELECT DISTINCT c.recordid, c.recordid
FROM '.$CFG->prefix.'data_content c, '
.$CFG->prefix.'data_records r, '
.$CFG->prefix.'data_content c1
WHERE c.recordid = r.id
AND c1.recordid = r.id
AND r.dataid = '.$data->id.'
2006-02-06 06:13:34 +00:00
AND c.fieldid = '.$sort.' '.$groupselect.'
2006-02-03 08:11:36 +00:00
AND ((c1.content LIKE "%'.$search.'%") OR
(c1.content1 LIKE "%'.$search.'%") OR
(c1.content2 LIKE "%'.$search.'%") OR
(c1.content3 LIKE "%'.$search.'%") OR
(c1.content4 LIKE "%'.$search.'%")) '.$approvesql.'
ORDER BY c.'.$sortcontent.' '.$order.' ';
$sqlcount = 'SELECT COUNT(DISTINCT c.recordid)
FROM '.$CFG->prefix.'data_content c, '
.$CFG->prefix.'data_records r, '
.$CFG->prefix.'data_content c1
WHERE c.recordid = r.id
AND c1.recordid = r.id
AND r.dataid = '.$data->id.'
2006-02-06 06:13:34 +00:00
AND c.fieldid = '.$sort.' '.$groupselect.'
2006-02-03 08:11:36 +00:00
AND ((c1.content LIKE "%'.$search.'%") OR
(c1.content1 LIKE "%'.$search.'%") OR
(c1.content2 LIKE "%'.$search.'%") OR
(c1.content3 LIKE "%'.$search.'%") OR
(c1.content4 LIKE "%'.$search.'%")) '.$approvesql.'
ORDER BY c.'.$sortcontent.' '.$order.' ';
//sqlindex is used to find the number of entries smaller than the current rid
//useful for zooming into single view from multi view (so we can keep track
//of exact and relative position of records
$sqlindex = 'SELECT COUNT(DISTINCT c.recordid)
FROM '.$CFG->prefix.'data_content c, '
.$CFG->prefix.'data_records r, '
.$CFG->prefix.'data_content c1
WHERE c.recordid = r.id
AND c1.recordid = r.id
AND r.dataid = '.$data->id.'
2006-02-06 06:13:34 +00:00
AND c.fieldid = '.$sort.' '.$ridsql.' '.$groupselect.'
2006-02-03 08:11:36 +00:00
AND ((c1.content LIKE "%'.$search.'%") OR
(c1.content1 LIKE "%'.$search.'%") OR
(c1.content2 LIKE "%'.$search.'%") OR
(c1.content3 LIKE "%'.$search.'%") OR
(c1.content4 LIKE "%'.$search.'%")) '.$approvesql.'
ORDER BY c.'.$sortcontent.' '.$order.' ';
} else if ($search){ //search only, no sort. if in search mode, only search text fields
$sql = 'SELECT DISTINCT c.recordid, c.recordid
FROM '.$CFG->prefix.'data_content c, '
.$CFG->prefix.'data_fields f, '
.$CFG->prefix.'data_records r
2006-02-06 06:13:34 +00:00
WHERE c.recordid = r.id '.$groupselect.' '.$approvesql.' AND
2006-02-03 08:11:36 +00:00
c.fieldid = f.id AND f.dataid = '
.$data->id.' AND c.content LIKE "%'.$search.'%" ORDER BY r.id '.$order.' ';
2006-02-03 08:11:36 +00:00
$sqlcount = 'SELECT COUNT(DISTINCT c.recordid)
FROM '.$CFG->prefix.'data_content c, '
.$CFG->prefix.'data_fields f, '
.$CFG->prefix.'data_records r
2006-02-06 06:13:34 +00:00
WHERE c.recordid = r.id '.$groupselect.' '.$approvesql.' AND
2006-02-03 08:11:36 +00:00
c.fieldid = f.id AND f.dataid = '
.$data->id.' AND c.content LIKE "%'.$search.'%" ORDER BY r.id '.$order.' ';
2006-02-03 08:11:36 +00:00
$sqlindex = 'SELECT COUNT(DISTINCT c.recordid)
FROM '.$CFG->prefix.'data_content c, '
.$CFG->prefix.'data_fields f, '
.$CFG->prefix.'data_records r
2006-02-06 06:13:34 +00:00
WHERE c.recordid = r.id '.$groupselect.' '.$approvesql.' AND
2006-02-03 08:11:36 +00:00
c.fieldid = f.id AND f.dataid = '
.$data->id.' '.$ridsql.' AND c.content LIKE "%'.$search.'%" ORDER BY r.id '.$order.' ';
2006-02-03 08:11:36 +00:00
} else { //else get everything, no search, no sort
2006-02-06 06:13:34 +00:00
$sql = 'SELECT * FROM '.$CFG->prefix.'data_records r WHERE r.dataid ='.$data->id.' '.$groupselect.' '.$approvesql.' ORDER BY r.id '.$order.' ';
$sqlcount = 'SELECT COUNT(*) FROM '.$CFG->prefix
2006-02-06 06:13:34 +00:00
.'data_records r WHERE r.dataid ='.$data->id.' '.$groupselect.' '.$approvesql.'ORDER BY r.id '.$order.' ';
2006-02-03 08:11:36 +00:00
$sqlindex = 'SELECT COUNT(*) FROM '.$CFG->prefix
2006-02-06 06:13:34 +00:00
.'data_records r WHERE r.dataid ='.$data->id.' '.$groupselect.' '.$ridsql.' '.$approvesql .'ORDER BY r.id '.$order.' ';
2006-02-03 08:11:36 +00:00
}
if ($rid) { //this is used in zooming
$page = count_records_sql($sqlindex);
}
$limit = $perpage > 1 ? sql_paging_limit($page * $perpage, $perpage)
: $limit = sql_paging_limit($page, DATA_PERPAGE_SINGLE);
$sql = $sql . $limit;
2006-02-03 08:11:36 +00:00
$totalcount = count_records_sql($sqlcount);
if (!$records = get_records_sql($sql)){
if ($search){
notify(get_string('nomatch','data'));
}
else {
notify(get_string('norecords','data'));
}
data_print_preference_form($data, $perpage, $search);
echo '</td></tr></table>';
print_footer($course);
exit;
}
2006-02-03 08:11:36 +00:00
//print header for multi view
if ($perpage > 1){
echo $data->listtemplateheader;
$listmode = 'listtemplate';
if (empty($data->listtemplate)){
notify(get_string('nolisttemplate','data'));
}
}
else {
$listmode = 'singletemplate';
if (empty($data->singletemplate)){
notify(get_string('nosingletemplate','data'));
}
}
2005-12-05 08:56:48 +00:00
print_paging_bar($totalcount, $page, $perpage, $baseurl, $pagevar='page');
//for each record we find, we do a string replacement for tags.
2006-02-06 06:13:34 +00:00
data_print_template($records, $data, $search, $listmode, $sort, $page, $rid, $order, $currentgroup);
print_paging_bar($totalcount, $page, $perpage, $baseurl, $pagevar='page');
if ($perpage > 1){
echo $data->listtemplatefooter; //print footer
}
2006-02-03 08:11:36 +00:00
data_print_preference_form($data, $perpage, $search, $sort, $order);
// Finish the page
echo '</td></tr></table>';
print_footer($course);
?>