diff --git a/mod/data/comment.php b/mod/data/comment.php index 9aa019ca1d3..a04d97720fc 100755 --- a/mod/data/comment.php +++ b/mod/data/comment.php @@ -4,61 +4,60 @@ require_once('lib.php'); //param needed to go back to view.php - $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 - $sort = optional_param('sort',0,PARAM_INT); //sort by field - $order = optional_param('order','ASC',PARAM_ALPHA); //sort order - $group = optional_param('group','0',PARAM_INT); //groupid + $rid = required_param('rid', PARAM_INT); // Record ID + $page = optional_param('page', 0, PARAM_INT); // Page ID //param needed for comment operations $mode = optional_param('mode','',PARAM_ALPHA); - $rid = optional_param('rid','',PARAM_INT); $commentid = optional_param('commentid','',PARAM_INT); $confirm = optional_param('confirm','',PARAM_INT); $commentcontent = optional_param('commentcontent','',PARAM_NOTAGS); $template = optional_param('template','',PARAM_ALPHA); - if ((!$record = get_record('data_records','id',$rid))) { - if (!$comment = get_record('data_comments','id',$commentid)) { - error ('this record does not exist'); - } else { - $record = get_record('data_records','id',$comment->recordid); + if (! $record = get_record('data_records', 'id', $rid)) { + error('Record ID is incorrect'); + } + if (! $data = get_record('data', 'id', $record->dataid)) { + error('Data ID is incorrect'); + } + if (! $course = get_record('course', 'id', $data->course)) { + error('Course is misconfigured'); + } + + require_login($course->id); + + if ($commentid) { + if (! $comment = get_record('data_comments', 'id', $commentid)) { + error('Comment ID is misconfigured'); + } + if ($comment->recordid != $record->id) { + error('Comment ID is misconfigured'); + } + if (!isteacher($course->id) && $comment->userid != $USER->id) { + error('Comment is not yours to edit!'); } } - - if (!$data = get_record('data','id',$record->dataid)) { - error ('this database does not exist'); - } - + switch ($mode) { case 'add': $newcomment = new object; $newcomment->userid = $USER->id; $newcomment->created = time(); $newcomment->modified = time(); - if (($newcomment->content = $commentcontent) && ($newcomment->recordid = $rid)) { + if (($newcomment->content = $commentcontent) && ($newcomment->recordid = $record->id)) { insert_record('data_comments',$newcomment); } - redirect('view.php?d='.s($d).'&search='.s($search).'&sort='.s($sort).'&order='.s($order).'&group='.s($group).'&page='.s($page).'&rid='.s($rid), get_string("commentsaved", "data")); + redirect('view.php?rid='.$record->id.'&page='.$page, get_string('commentsaved', 'data')); break; case 'edit': //print edit form print_header(); - $comment = get_record('data_comments','id',$commentid); - print_heading('Edit'); + print_heading(get_string('edit')); echo '