From 64452eb4386ba2185947399799597023b1baa202 Mon Sep 17 00:00:00 2001 From: moodler Date: Wed, 5 Apr 2006 01:38:06 +0000 Subject: [PATCH] Various fixes that have accumulated --- mod/data/{add.php => edit.php} | 24 +++-- mod/data/field/date/field.class.php | 4 + mod/data/index.php | 23 ++++- mod/data/lib.php | 26 +++-- mod/data/rsslib.php | 7 +- mod/data/tabs.php | 33 +++--- mod/data/templates.php | 1 + mod/data/view.php | 151 ++++++++++++++++------------ 8 files changed, 172 insertions(+), 97 deletions(-) rename mod/data/{add.php => edit.php} (93%) diff --git a/mod/data/add.php b/mod/data/edit.php similarity index 93% rename from mod/data/add.php rename to mod/data/edit.php index b1548d2642..ff156854cc 100755 --- a/mod/data/add.php +++ b/mod/data/edit.php @@ -56,6 +56,10 @@ } require_course_login($course, false, $cm); + + if (!isloggedin() or isguest()) { + redirect('view.php?d='.$data->id); + } /// If it's hidden then it's don't show anything. :) if (empty($cm->visible) and !isteacher($course->id)) { @@ -101,7 +105,7 @@ /// Check to see if groups are being used here if ($groupmode = groupmode($course, $cm)) { // Groups are being used - $currentgroup = setup_and_print_groups($course, $groupmode, 'add.php?d='.$data->id.'&sesskey='.sesskey().'&'); + $currentgroup = setup_and_print_groups($course, $groupmode, 'edit.php?d='.$data->id.'&sesskey='.sesskey().'&'); } else { $currentgroup = 0; } @@ -125,7 +129,7 @@ /// Process incoming data for adding/updating records - if ($datarecord = data_submitted($CFG->wwwroot.'/mod/data/add.php') and confirm_sesskey()) { + if ($datarecord = data_submitted($CFG->wwwroot.'/mod/data/edit.php') and confirm_sesskey()) { $ignorenames = array('MAX_FILE_SIZE','sesskey','d','rid'); // strings to be ignored in input data @@ -211,6 +215,10 @@ add_to_log($course->id, 'data', 'add', "view.php?d=$data->id&rid=$recordid", $data->id, $cm->id); notify(get_string('entrysaved','data')); + + if (!empty($datarecord->saveandview)) { + redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$rid); + } } } } // End of form processing @@ -231,7 +239,7 @@ $replacement = array(); //html to replace those yucky tags //form goes here first in case add template is empty - echo '
'; + echo ''; echo ''; echo ''; echo ''; @@ -261,9 +269,11 @@ } echo $newtext; - echo '
'; - if ($rid){ - echo ' '; + echo '
'; + if ($rid) { + echo ' '; + } else { + echo ''; } echo '
'; print_simple_box_end(); @@ -304,7 +314,7 @@ print_simple_box_end(); } else { echo '
'; - echo ''.get_string('uploadrecords', 'data').''; + echo ''.get_string('uploadrecords', 'data').''; echo '
'; } } diff --git a/mod/data/field/date/field.class.php b/mod/data/field/date/field.class.php index 1039f19022..9b2824ea75 100755 --- a/mod/data/field/date/field.class.php +++ b/mod/data/field/date/field.class.php @@ -82,6 +82,10 @@ class data_field_date extends data_field_base { } } + function get_sort_sql($fieldname) { + return 'CAST('.$fieldname.' AS unsigned)'; + } + } diff --git a/mod/data/index.php b/mod/data/index.php index 88bb958ccf..a418cc79c4 100755 --- a/mod/data/index.php +++ b/mod/data/index.php @@ -65,6 +65,14 @@ $table->align = array ('center', 'center', 'center', 'center'); } + $rss = (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds)); + + if ($rss) { + require_once($CFG->libdir."/rsslib.php"); + array_push($table->head, 'RSS'); + array_push($table->align, 'center'); + } + $currentgroup = get_current_group($course->id); if ($currentgroup and isteacheredit($course->id)) { $group = get_record("groups", "id", $currentgroup); @@ -98,6 +106,11 @@ } else { $numunapprovedrecords = get_string('noapprovalrequired', 'data'); } + + $rsslink = ''; + if ($rss && $data->rssarticles > 0) { + $rsslink = rss_get_link($course->id, $USER->id, 'data', $data->id, 'RSS'); + } if ($course->format == 'weeks' or $course->format == 'topics') { if ($data->section !== $currentsection) { @@ -109,11 +122,17 @@ } $currentsection = $data->section; } + $row = array ($printsection, $link, $data->intro, $numrecords, $numunapprovedrecords); - $table->data[] = array ($printsection, $link, $data->intro, $numrecords, $numunapprovedrecords); } else { - $table->data[] = array ($link, $data->intro, $numrecords, $numunapprovedrecords); + $row = array ($link, $data->intro, $numrecords, $numunapprovedrecords); } + + if ($rss) { + array_push($row, $rsslink); + } + + $table->data[] = $row; } echo "
"; diff --git a/mod/data/lib.php b/mod/data/lib.php index 62f5462578..a6278627a8 100755 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -289,6 +289,11 @@ class data_field_base { /// Base class for Database Field Types (see field/* return 'content'; } +/// Returns the SQL needed to refer to the column. Some fields may need to CAST() etc. + function get_sort_sql($fieldname) { + return $fieldname; + } + /// Returns the name/type of the field function name(){ return get_string('name'.$this->type, 'data'); @@ -810,7 +815,7 @@ function data_get_coursemodule_info($coursemodule) { * @param string $template * * output null * ************************************************************************/ -function data_print_template($template, $records, $data, $search='',$page=0, $return=false){ +function data_print_template($template, $records, $data, $search='',$page=0, $return=false) { global $CFG; static $fields = NULL; @@ -824,7 +829,11 @@ function data_print_template($template, $records, $data, $search='',$page=0, $re $isteacher = isteacher($data->course); } - foreach ($records as $record) { //only 1 record for single mode + if (empty($records)) { + return; + } + + foreach ($records as $record) { /// Might be just one for the single template /// Replacing tags $patterns = array(); @@ -840,7 +849,7 @@ function data_print_template($template, $records, $data, $search='',$page=0, $re $patterns[]='/\#\#Edit\#\#/i'; $patterns[]='/\#\#Delete\#\#/i'; if ($isteacher or data_isowner($record->id)) { - $replacement[] = 'wwwroot.'/mod/data/edit.php?d=' .$data->id.'&rid='.$record->id.'&sesskey='.sesskey().'">'.get_string('edit').''; $replacement[] = ''.get_string('delete').''; @@ -854,18 +863,21 @@ function data_print_template($template, $records, $data, $search='',$page=0, $re $patterns[]='/\#\#MoreURL\#\#/i'; $replacement[] = $CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$record->id; + $patterns[]='/\#\#User\#\#/i'; + $replacement[] = ''.fullname($record).''; $patterns[]='/\#\#Approve\#\#/i'; if ($isteacher && ($data->approval) && (!$record->approved)){ - $replacement[] = ''.get_string('approve').''; + $replacement[] = ''.get_string('approve').''; } else { $replacement[] = ''; } - $patterns[]='/\#\#Comment\#\#/i'; + $patterns[]='/\#\#Comments\#\#/i'; if (($template == 'listtemplate') && ($data->comments)) { $comments = count_records('data_comments','recordid',$record->id); - $replacement[] = ''.$comments.' '.get_string('comment','data').''; + $replacement[] = ''.get_string('comments','data', $comments).''; } else { $replacement[] = ''; } @@ -1089,6 +1101,8 @@ function data_get_ratings($recordid, $sort="u.firstname ASC") { //prints all comments + a text box for adding additional comment function data_print_comments($data, $record, $page=0) { + echo ''; + if ($comments = get_records('data_comments','recordid',$record->id)) { foreach ($comments as $comment) { data_print_comment($data, $comment, $page); diff --git a/mod/data/rsslib.php b/mod/data/rsslib.php index 8451ee7de3..cb82a1b42f 100644 --- a/mod/data/rsslib.php +++ b/mod/data/rsslib.php @@ -38,11 +38,14 @@ if (!$firstfield = get_record_sql('SELECT id,name from '.$CFG->prefix.'data_fields WHERE dataid = '.$data->id.' ORDER by id', true)) { continue; } - + + // Get the data_records out. + $approved = ($data->approval) ? ' AND dr.approved = 1 ' : ' '; + $sql = 'SELECT dr.* ' . "FROM {$CFG->prefix}data_records AS dr " . - "WHERE dr.dataid = {$data->id} " . + "WHERE dr.dataid = {$data->id} " .$approved. 'ORDER BY dr.timecreated DESC ' . "LIMIT {$data->rssarticles}"; diff --git a/mod/data/tabs.php b/mod/data/tabs.php index 370939b20b..6a868c6385 100755 --- a/mod/data/tabs.php +++ b/mod/data/tabs.php @@ -40,21 +40,26 @@ $row[] = new tabobject('single', $CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&mode=single', get_string('single','data'), '', true); } - if (isteacher($course->id) or ($data->participants == DATA_STUDENTS_ONLY) or ($data->participants == DATA_TEACHERS_AND_STUDENTS)){ - $addstring = empty($editentry) ? get_string('add', 'data') : get_string('editentry', 'data'); - $row[] = new tabobject('add', $CFG->wwwroot.'/mod/data/add.php?d='.$data->id, $addstring, '', true); - } - if (isteacher($course->id)) { - if ($currenttab == 'list') { - $defaultemplate = 'listtemplate'; - } else if ($currenttab == 'add') { - $defaultemplate = 'addtemplate'; - } else { - $defaultemplate = 'singletemplate'; + if (isloggedin() and !isguest()) { + if (isteacher($course->id) or ($data->participants == DATA_STUDENTS_ONLY) or + ($data->participants == DATA_TEACHERS_AND_STUDENTS)){ + $addstring = empty($editentry) ? get_string('add', 'data') : get_string('editentry', 'data'); + $row[] = new tabobject('add', $CFG->wwwroot.'/mod/data/edit.php?d='.$data->id, $addstring, '', true); + } + if (isteacher($course->id)) { + if ($currenttab == 'list') { + $defaultemplate = 'listtemplate'; + } else if ($currenttab == 'add') { + $defaultemplate = 'addtemplate'; + } else { + $defaultemplate = 'singletemplate'; + } + + $row[] = new tabobject('templates', $CFG->wwwroot.'/mod/data/templates.php?d='.$data->id. + '&mode='.$defaultemplate, get_string('templates','data')); + $row[] = new tabobject('fields', $CFG->wwwroot.'/mod/data/field.php?d='.$data->id, + get_string('fields','data'), '', true); } - - $row[] = new tabobject('templates', $CFG->wwwroot.'/mod/data/templates.php?d='.$data->id.'&mode='.$defaultemplate, get_string('templates','data')); - $row[] = new tabobject('fields', $CFG->wwwroot.'/mod/data/field.php?d='.$data->id, get_string('fields','data'), '', true); } $tabs[] = $row; diff --git a/mod/data/templates.php b/mod/data/templates.php index 5b1231e808..24aacdabb1 100755 --- a/mod/data/templates.php +++ b/mod/data/templates.php @@ -186,6 +186,7 @@ echo ''; echo ''; echo ''; + echo ''; echo ''; echo '



'; echo ''; diff --git a/mod/data/view.php b/mod/data/view.php index f627017214..fa90edd054 100755 --- a/mod/data/view.php +++ b/mod/data/view.php @@ -152,7 +152,7 @@ /// RSS and CSS meta $meta = ''; - if (isset($CFG->enablerssfeeds) && isset($CFG->data_enablerssfeeds) && $data->rssarticles > 0) { + if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) { $rsspath = rss_get_url($course->id, $USER->id, 'data', $data->id); $meta .= 'shortname.': %fullname%" href="'.$rsspath.'" />'; @@ -178,14 +178,14 @@ print_heading(format_string($data->name)); // 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) { + if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) { echo '
'; rss_print_link($course->id, $USER->id, 'data', $data->id, get_string('rsstype')); echo '
'; echo '
'; } - if ($data->intro and empty($sort) and empty($search) and empty($page) and empty($record)) { + if ($data->intro and empty($page) and empty($record)) { print_simple_box(format_text($data->intro), 'center', '70%', '', 5, 'generalbox', 'intro'); } @@ -198,31 +198,6 @@ $currentgroup = 0; } - -/// Print the tabs - - if ($record or $mode == 'single') { - $currenttab = 'single'; - } else { - $currenttab = 'list'; - } - include('tabs.php'); - - -/// Approve any requested records - - if ($approve && confirm_sesskey() && isteacher($course->id)) { - if ($approverecord = get_record('data_records', 'id', $approve)) { // Need to check this is valid - if ($approverecord->dataid == $data->id) { // Must be from this database - $newrecord->id = $approverecord->id; - $newrecord->approved = 1; - if (update_record('data_records', $newrecord)) { - notify(get_string('recordapproved','data'), 'notifysuccess'); - } - } - } - } - /// Delete any requested records if ($delete && confirm_sesskey() && (isteacher($course->id) or data_isowner($delete))) { @@ -246,12 +221,45 @@ } } else { // Print a confirmation page - notice_yesno(get_string('confirmdeleterecord','data'), - 'view.php?d='.$data->id.'&delete='.$delete.'&confirm=1&sesskey='.sesskey(), - 'view.php?d='.$data->id); + if ($deleterecord = get_record('data_records', 'id', $delete)) { // Need to check this is valid + if ($deleterecord->dataid == $data->id) { // Must be from this database + notice_yesno(get_string('confirmdeleterecord','data'), + 'view.php?d='.$data->id.'&delete='.$delete.'&confirm=1&sesskey='.sesskey(), + 'view.php?d='.$data->id); - print_footer($course); - exit; + $records[] = $deleterecord; + data_print_template('singletemplate', $records, $data); + + print_footer($course); + exit; + } + } + } + } + + + +/// Print the tabs + + if ($record or $mode == 'single') { + $currenttab = 'single'; + } else { + $currenttab = 'list'; + } + include('tabs.php'); + + +/// Approve any requested records + + if ($approve && confirm_sesskey() && isteacher($course->id)) { + if ($approverecord = get_record('data_records', 'id', $approve)) { // Need to check this is valid + if ($approverecord->dataid == $data->id) { // Must be from this database + $newrecord->id = $approverecord->id; + $newrecord->approved = 1; + if (update_record('data_records', $newrecord)) { + notify(get_string('recordapproved','data'), 'notifysuccess'); + } + } } } @@ -282,45 +290,47 @@ $groupselect = ' '; } - /// Find the field we are sorting on if ($sort) { $sortfield = data_get_field_from_id($sort, $data); $sortcontent = $sortfield->get_sort_field(); + $sortcontentfull = $sortfield->get_sort_sql('c.'.$sortcontent); - $what = ' DISTINCT r.id, r.approved '; + $what = ' DISTINCT r.id, r.approved, r.userid, u.firstname, u.lastname '; $count = ' COUNT(DISTINCT c.recordid) '; - $tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r,'.$CFG->prefix.'data_content c1 '; + $tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r,'.$CFG->prefix.'data_content c1, '.$CFG->prefix.'user u '; $where = 'WHERE c.recordid = r.id AND c.fieldid = '.$sort.' AND r.dataid = '.$data->id.' + AND r.userid = u.id AND c1.recordid = r.id '; - $sortorder = ' ORDER BY c.'.$sortcontent.' '.$order.' '; + $sortorder = ' ORDER BY '.$sortcontentfull.' '.$order.' '; + if ($search) { + $searchselect = ' AND (c1.content LIKE "%'.$search.'%") '; + } else { + $searchselect = ' '; + } } else if ($search) { - $what = ' DISTINCT r.id, r.approved '; + $what = ' DISTINCT r.id, r.approved, r.userid, u.firstname, u.lastname '; $count = ' COUNT(DISTINCT c.recordid) '; - $tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r '; + $tables = $CFG->prefix.'data_content c,'.$CFG->prefix.'data_records r, '.$CFG->prefix.'user u '; $where = 'WHERE c.recordid = r.id + AND r.userid = u.id AND r.dataid = '.$data->id; $sortorder = ' ORDER BY r.id '; + $searchselect = ' AND (c.content LIKE "%'.$search.'%") '; } else { - $what = ' DISTINCT r.id, r.approved '; + $what = ' DISTINCT r.id, r.approved, r.userid, u.firstname, u.lastname '; $count = ' COUNT(r.id) '; - $tables = $CFG->prefix.'data_records r '; - $where = 'WHERE r.dataid = '.$data->id; - $sortorder = ' ORDER BY r.id '; - } - -/// Restrict by a search if we have one - - if ($search) { - $searchselect = ' AND (c.content LIKE "%'.$search.'%") '; - } else { + $tables = $CFG->prefix.'data_records r, '.$CFG->prefix.'user u '; + $where = 'WHERE r.dataid = '.$data->id. ' AND r.userid = u.id '; + $sortorder = ' ORDER BY r.timecreated '.$order. ' '; $searchselect = ' '; } + /// To actually fetch the records $fromsql = ' FROM '.$tables.$where.$groupselect.$approveselect.$searchselect; @@ -336,22 +346,31 @@ if ($record) { // We need to just show one, so where is it in context? $nowperpage = 1; $mode = 'single'; - if ($sort) { // We need to search by that field - if ($content = get_field('data_content', 'content', 'recordid', $record->id, 'fieldid', $sort)) { - $content = addslashes($content); - if ($order == 'ASC') { - $lessthan = " AND c.$sortcontent < '$content' "; - } else { - $lessthan = " AND c.$sortcontent > '$content' "; - } - } else { // Failed to find data (shouldn't happen), so fall back to something easy - $lessthan = " r.id < '$record->id' "; +# if ($sort) { // We need to search by that field +# if ($content = get_field('data_content', 'content', 'recordid', $record->id, 'fieldid', $sort)) { +# $content = addslashes($content); +# if ($order == 'ASC') { +# $lessthan = " AND $sortcontentfull < '$content' "; +# } else { +# $lessthan = " AND $sortcontentfull > '$content' "; +# } +# } else { // Failed to find data (shouldn't happen), so fall back to something easy +# $lessthan = " r.id < '$record->id' "; +# } +# } else { +# $lessthan = " r.id < '$record->id' "; +# } +# $sqlindex = 'SELECT COUNT(DISTINCT c.recordid) '.$fromsql.$lessthan.$sortorder; +# $page = count_records_sql($sqlindex); + + $allrecords = get_records_sql($sqlselect); // Kludgey but accurate at least! + $page = 0; + foreach ($allrecords as $key => $allrecord) { + if ($key == $record->id) { + break; } - } else { - $lessthan = " r.id < '$record->id' "; + $page++; } - $sqlindex = 'SELECT COUNT(DISTINCT c.recordid) '.$fromsql.$lessthan.$sortorder; - $page = count_records_sql($sqlindex); } else if ($mode == 'single') { // We rely on ambient $page settings $nowperpage = 1; @@ -401,9 +420,9 @@ print_paging_bar($totalcount, $page, $nowperpage, $baseurl, $pagevar='page'); } - } - data_print_preference_form($data, $perpage, $search, $sort, $order); + data_print_preference_form($data, $perpage, $search, $sort, $order); + } print_footer($course); ?> -- 2.39.5