MDL-10234
[moodle-linuxchix.git] / mod / data / comment.php
blobf9c696de26c638e470b70945b229f2240861745a
1 <?php // $Id$
3 require_once('../../config.php');
4 require_once('lib.php');
5 require_once('comment_form.php');
7 //param needed to go back to view.php
8 $rid = required_param('rid', PARAM_INT); // Record ID
9 $page = optional_param('page', 0, PARAM_INT); // Page ID
11 //param needed for comment operations
12 $mode = optional_param('mode','add',PARAM_ALPHA);
13 $commentid = optional_param('commentid','',PARAM_INT);
14 $confirm = optional_param('confirm','',PARAM_INT);
17 if (! $record = get_record('data_records', 'id', $rid)) {
18 error('Record ID is incorrect');
20 if (! $data = get_record('data', 'id', $record->dataid)) {
21 error('Data ID is incorrect');
23 if (! $course = get_record('course', 'id', $data->course)) {
24 error('Course is misconfigured');
26 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
27 error('Course Module ID was incorrect');
30 require_login($course->id, false, $cm);
32 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
34 if ($commentid) {
35 if (! $comment = get_record('data_comments', 'id', $commentid)) {
36 error('Comment ID is misconfigured');
38 if ($comment->recordid != $record->id) {
39 error('Comment ID is misconfigured');
41 if (!has_capability('mod/data:managecomments', $context) && $comment->userid != $USER->id) {
42 error('Comment is not yours to edit!');
44 } else {
45 $comment = false;
49 $mform = new mod_data_comment_form();
50 $mform->set_data(array('mode'=>$mode, 'page'=>$page, 'rid'=>$record->id, 'commentid'=>$commentid));
51 if ($comment) {
52 $format = $comment->format;
53 $content = $comment->content;
54 if (can_use_html_editor()) {
55 $options = new object();
56 $options->smiley = false;
57 $options->filter = false;
58 $content = format_text($content, $format, $options);
59 $format = FORMAT_HTML;
61 $mform->set_data(array('content'=>$content, 'format'=>$format));
65 if ($mform->is_cancelled()) {
66 redirect('view.php?rid='.$record->id.'&amp;page='.$page);
69 switch ($mode) {
70 case 'add':
71 if (!$formadata = $mform->get_data()) {
72 break; // something is wrong here, try again
75 $newcomment = new object();
76 $newcomment->userid = $USER->id;
77 $newcomment->created = time();
78 $newcomment->modified = time();
79 $newcomment->content = $formadata->content;
80 $newcomment->recordid = $formadata->rid;
81 if (insert_record('data_comments',$newcomment)) {
82 redirect('view.php?rid='.$record->id.'&amp;page='.$page);
83 } else {
84 error('Error while saving comment.');
87 break;
89 case 'edit': //print edit form
90 if (!$formadata = $mform->get_data()) {
91 break; // something is wrong here, try again
94 $updatedcomment = new object();
95 $updatedcomment->id = $formadata->commentid;
96 $updatedcomment->content = $formadata->content;
97 $updatedcomment->format = $formadata->format;
98 $updatedcomment->modified = time();
100 if (update_record('data_comments',$updatedcomment)) {
101 redirect('view.php?rid='.$record->id.'&amp;page='.$page);
102 } else {
103 error('Error while saving comment.');
105 break;
107 case 'delete': //deletes single comment from db
108 if ($confirm and confirm_sesskey() and $comment) {
109 delete_records('data_comments','id',$comment->id);
110 redirect('view.php?rid='.$record->id.'&amp;page='.$page, get_string('commentdeleted', 'data'));
112 } else { //print confirm delete form
113 print_header();
114 data_print_comment($data, $comment, $page);
116 notice_yesno(get_string('deletecomment','data'),
117 'comment.php?rid='.$record->id.'&amp;commentid='.$comment->id.'&amp;page='.$page.
118 '&amp;sesskey='.sesskey().'&amp;mode=delete&amp;confirm=1',
119 'view.php?rid='.$record->id.'&amp;page='.$page);
120 print_footer();
122 die;
123 break;
127 print_header();
128 data_print_comments($data, $record, $page, $mform);
129 print_footer();