3 require_once('../../config.php');
4 require_once('lib.php');
5 require_once('comment_form.php');
7 $action = optional_param('action','add', PARAM_ACTION
);
10 error('Guests are not allowed to post comments!');
15 glossary_comment_add();
18 glossary_comment_delete();
21 glossary_comment_edit();
24 error('Incorrect action specified');
30 function glossary_comment_add() {
33 $eid = optional_param('eid', 0, PARAM_INT
); // Entry ID
35 if (!$entry = get_record('glossary_entries', 'id', $eid)) {
36 error('Entry is incorrect');
38 if (!$glossary = get_record('glossary', 'id', $entry->glossaryid
)) {
39 error('Incorrect glossary');
41 if (!$cm = get_coursemodule_from_instance('glossary', $glossary->id
)) {
42 error('Course Module ID was incorrect');
44 if (!$course = get_record('course', 'id', $cm->course
)) {
45 error('Course is misconfigured');
48 require_login($course->id
, false, $cm);
49 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
50 /// Both the configuration and capability must allow comments
51 if (!$glossary->allowcomments
or !has_capability('mod/glossary:comment', $context)) {
52 error('You can\'t add comments to this glossary!');
55 $mform = new mod_glossary_comment_form();
56 $mform->set_data(array('eid'=>$eid, 'action'=>'add'));
58 if ($mform->is_cancelled()) {
59 redirect("comments.php?id=$cm->id&eid=$entry->id");
62 if ($data = $mform->get_data()) {
63 trusttext_after_edit($data->entrycomment
, $context);
65 $newcomment = new object();
66 $newcomment->entryid
= $entry->id
;
67 $newcomment->entrycomment
= $data->entrycomment
;
68 $newcomment->format
= $data->format
;
69 $newcomment->timemodified
= time();
70 $newcomment->userid
= $USER->id
;
72 if (!$newcomment->id
= insert_record('glossary_comments', $newcomment)) {
73 error('Could not insert this new comment');
75 add_to_log($course->id
, 'glossary', 'add comment', "comments.php?id=$cm->id&eid=$entry->id", "$newcomment->id", $cm->id
);
77 redirect("comments.php?id=$cm->id&eid=$entry->id");
80 glossary_comment_print_header($course, $cm, $glossary, $entry, 'add');
82 print_footer($course);
88 * Deleting existing comments
90 function glossary_comment_delete() {
93 $cid = optional_param('cid', 0, PARAM_INT
); // Comment ID
94 $confirm = optional_param('confirm', 0, PARAM_BOOL
); // delete confirmation
96 if (!$comment = get_record('glossary_comments', 'id', $cid)) {
97 error('Comment is incorrect');
99 if (!$entry = get_record('glossary_entries', 'id', $comment->entryid
)) {
100 error('Entry is incorrect');
102 if (!$glossary = get_record('glossary', 'id', $entry->glossaryid
)) {
103 error('Incorrect glossary');
105 if (!$cm = get_coursemodule_from_instance('glossary', $glossary->id
)) {
106 error('Course Module ID was incorrect');
108 if (!$course = get_record('course', 'id', $cm->course
)) {
109 error('Course is misconfigured');
112 require_login($course->id
, false, $cm);
113 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
114 if (($comment->userid
<> $USER->id
) and !has_capability('mod/glossary:managecomments', $context)) {
115 error('You can\'t delete other people\'s comments!');
117 if (!$glossary->allowcomments
and !has_capability('mod/glossary:managecomments', $context)) {
118 error('You can\'t delete comments in this glossary!');
121 if (data_submitted() and $confirm) {
122 delete_records('glossary_comments','id', $cid);
123 add_to_log($course->id
, 'glossary', 'delete comment', "comments.php?id=$cm->id&eid=$entry->id", "$comment->id",$cm->id
);
124 redirect("comments.php?id=$cm->id&eid=$entry->id");
127 $linkyes = 'comment.php';
128 $optionsyes = array('action'=>'delete', 'cid'=>$cid, 'confirm'=>1);
129 $linkno = 'comments.php';
130 $optionsno = array('id'=>$cm->id
, 'eid'=>$entry->id
);
131 $strdeletewarning = get_string('areyousuredeletecomment','glossary');
133 glossary_comment_print_header($course, $cm, $glossary, $entry, 'delete');
134 glossary_print_comment($course, $cm, $glossary, $entry, $comment);
135 notice_yesno($strdeletewarning, $linkyes, $linkno, $optionsyes, $optionsno, 'post', 'get');
136 print_footer($course);
142 * Edit existing comments
144 function glossary_comment_edit() {
147 $cid = optional_param('cid', 0, PARAM_INT
); // Comment ID
149 if (!$comment = get_record('glossary_comments', 'id', $cid)) {
150 error('Comment is incorrect');
152 if (!$entry = get_record('glossary_entries', 'id', $comment->entryid
)) {
153 error('Entry is incorrect');
155 if (!$glossary = get_record('glossary', 'id', $entry->glossaryid
)) {
156 error('Incorrect glossary');
158 if (!$cm = get_coursemodule_from_instance('glossary', $glossary->id
)) {
159 error('Course Module ID was incorrect');
161 if (!$course = get_record('course', 'id', $cm->course
)) {
162 error('Course is misconfigured');
165 require_login($course->id
, false, $cm);
166 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
167 if (!$glossary->allowcomments
and !has_capability('mod/glossary:managecomments', $context)) {
168 error('You can\'t edit comments in this glossary!');
170 if (($comment->userid
<> $USER->id
) and !has_capability('mod/glossary:managecomments', $context)) {
171 error('You can\'t edit other people\'s comments!');
173 $ineditperiod = ((time() - $comment->timemodified
< $CFG->maxeditingtime
) ||
$glossary->editalways
);
174 if ((!has_capability('mod/glossary:comment', $context) or !$ineditperiod) and !has_capability('mod/glossary:managecomments', $context)) {
175 error('You can\'t edit this. Time expired!');
178 $mform = new mod_glossary_comment_form();
179 trusttext_prepare_edit($comment->entrycomment
, $comment->format
, can_use_html_editor(), $context);
180 $mform->set_data(array('cid'=>$cid, 'action'=>'edit', 'entrycomment'=>$comment->entrycomment
, 'format'=>$comment->format
));
182 if ($data = $mform->get_data()) {
183 trusttext_after_edit($data->entrycomment
, $context);
185 $updatedcomment = new object();
186 $updatedcomment->id
= $cid;
187 $updatedcomment->entrycomment
= $data->entrycomment
;
188 $updatedcomment->format
= $data->format
;
189 $updatedcomment->timemodified
= time();
191 if (!update_record('glossary_comments', $updatedcomment)) {
192 error('Could not update this comment');
194 add_to_log($course->id
, 'glossary', 'update comment', "comments.php?id=$cm->id&eid=$entry->id", "$updatedcomment->id",$cm->id
);
196 redirect("comments.php?id=$cm->id&eid=$entry->id");
199 glossary_comment_print_header($course, $cm, $glossary, $entry, 'edit');
201 print_footer($course);
206 //////////////////////////////////
207 /// utility functions
208 //////////////////////////////////
210 function glossary_comment_print_header($course, $cm, $glossary, $entry, $action) {
213 $straction = get_string('addingcomment','glossary');
216 $straction = get_string('editingcomment','glossary');
219 $straction = get_string('deletingcomment','glossary');
223 $strglossaries = get_string('modulenameplural', 'glossary');
224 $strglossary = get_string('modulename', 'glossary');
225 $strcomments = get_string('comments', 'glossary');
228 $navlinks[] = array('name' => $strglossaries, 'link' => "index.php?id=$course->id", 'type' => 'activity');
229 $navlinks[] = array('name' => format_string($glossary->name
,true), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
230 $navlinks[] = array('name' => $strcomments, 'link' => "comments.php?id=$cm->id&eid=$entry->id", 'type' => 'title');
231 $navlinks[] = array('name' => $straction, 'link' => '', 'type' => 'action');
233 $navigation = build_navigation($navlinks);
235 print_header_simple(format_string($glossary->name
), '', $navigation,
236 '', '', true, update_module_button($cm->id
, $course->id
, $strglossary),
237 navmenu($course, $cm));
238 /// print original glossary entry for any comment action (add, update, delete)
239 glossary_print_entry($course, $cm, $glossary, $entry, 'approval', '', false);