From: skodak Date: Sat, 28 Oct 2006 21:55:19 +0000 (+0000) Subject: MDL-6940 - I decided to rewrite the glossary comments UI, the original code (pre... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=eebc919280f5c3360b6f3ad03ef4a8f9b4affb71;p=moodle.git MDL-6940 - I decided to rewrite the glossary comments UI, the original code (pre 1.8) was not nice at all, it is IMHO more readable now and the forms implementation could be greatly simplified; this can also serve as sample trusttext implementation outside of forms code --- diff --git a/mod/glossary/comment.php b/mod/glossary/comment.php index 0b20eefd4e..e4441de06a 100644 --- a/mod/glossary/comment.php +++ b/mod/glossary/comment.php @@ -1,52 +1,209 @@ glossaryid)) { + error('Incorrect glossary'); + } + if (!$cm = get_coursemodule_from_instance('glossary', $glossary->id)) { + error('Course Module ID was incorrect'); + } + if (!$course = get_record('course', 'id', $cm->course)) { + error('Course is misconfigured'); + } + + require_login($course->id, false, $cm); + $context = get_context_instance(CONTEXT_MODULE, $cm->id); + if (!$glossary->allowcomments and !has_capability('mod/glossary:comment', $context)) { + error('You can\'t add comments to this glossary!'); + } - global $USER, $CFG; + $mform = new glossary_comment_form('comment.php'); + $mform->set_defaults(array('eid'=>$eid, 'action'=>'add')); - if (!$cm = get_coursemodule_from_id('glossary', $id)) { - error('Course Module ID was incorrect'); + if ($data = $mform->data_submitted()) { + trusttext_after_edit($data->entrycomment, $context); + + $newcomment = new object(); + $newcomment->entryid = $entry->id; + $newcomment->entrycomment = $data->entrycomment; + $newcomment->format = $data->format; + $newcomment->timemodified = time(); + $newcomment->userid = $USER->id; + + if (!$newcomment->id = insert_record('glossary_comments', $newcomment)) { + error('Could not insert this new comment'); + } else { + add_to_log($course->id, 'glossary', 'add comment', "comments.php?id=$cm->id&eid=$entry->id", "$newcomment->id", $cm->id); + } + redirect("comments.php?id=$cm->id&eid=$entry->id"); + + } else { + glossary_comment_print_header($course, $cm, $glossary, $entry, 'add'); + $mform->display(); + print_footer($course); + die; } +} - if (!$course = get_record('course', 'id', $cm->course)) { +/** + * Deleting existing comments + */ +function glossary_comment_delete() { + global $USER; + + $cid = optional_param('cid', 0, PARAM_INT); // Comment ID + $confirm = optional_param('confirm', 0, PARAM_BOOL); // delete confirmation + if (!$comment = get_record('glossary_comments', 'id', $cid)) { + error('Comment is incorrect'); + } + if (!$entry = get_record('glossary_entries', 'id', $comment->entryid)) { + error('Entry is incorrect'); + } + if (!$glossary = get_record('glossary', 'id', $entry->glossaryid)) { + error('Incorrect glossary'); + } + if (!$cm = get_coursemodule_from_instance('glossary', $glossary->id)) { + error('Course Module ID was incorrect'); + } + if (!$course = get_record('course', 'id', $cm->course)) { error('Course is misconfigured'); } - if (!$glossary = get_record('glossary', 'id', $cm->instance)) { - error('Course module is incorrect'); + require_login($course->id, false, $cm); + $context = get_context_instance(CONTEXT_MODULE, $cm->id); + if (($comment->userid <> $USER->id) and !has_capability('mod/glossary:managecomments', $context)) { + error('You can\'t delete other people\'s comments!'); + } + if (!$glossary->allowcomments and !has_capability('mod/glossary:managecomments', $context)) { + error('You can\'t delete comments in this glossary!'); } - if (!$entry = get_record('glossary_entries', 'id', $eid)) { - error('Entry is incorrect'); + if (data_submitted() and $confirm) { + delete_records('glossary_comments','id', $cid); + add_to_log($course->id, 'glossary', 'delete comment', "comments.php?id=$cm->id&eid=$entry->id", "$comment->id",$cm->id); + redirect("comments.php?id=$cm->id&eid=$entry->id"); + + } else { + $linkyes = 'comment.php'; + $optionsyes = array('action'=>'delete', 'cid'=>$cid, 'confirm'=>1); + $linkno = 'comments.php'; + $optionsno = array('id'=>$cm->id, 'eid'=>$entry->id); + $strdeletewarning = get_string('areyousuredeletecomment','glossary'); + + glossary_comment_print_header($course, $cm, $glossary, $entry, 'delete'); + glossary_print_comment($course, $cm, $glossary, $entry, $comment); + notice_yesno($strdeletewarning, $linkyes, $linkno, $optionsyes, $optionsno, 'post', 'get'); + print_footer($course); + die; } +} - if ($cid ) { - if (!$comment = get_record('glossary_comments', 'id', $cid)) { - error('Comment is incorrect'); - } +/** + * Edit existing comments + */ +function glossary_comment_edit() { + global $CFG, $USER; + + $cid = optional_param('cid', 0, PARAM_INT); // Comment ID + + if (!$comment = get_record('glossary_comments', 'id', $cid)) { + error('Comment is incorrect'); + } + if (!$entry = get_record('glossary_entries', 'id', $comment->entryid)) { + error('Entry is incorrect'); + } + if (!$glossary = get_record('glossary', 'id', $entry->glossaryid)) { + error('Incorrect glossary'); + } + if (!$cm = get_coursemodule_from_instance('glossary', $glossary->id)) { + error('Course Module ID was incorrect'); + } + if (!$course = get_record('course', 'id', $cm->course)) { + error('Course is misconfigured'); } require_login($course->id, false, $cm); - $context = get_context_instance(CONTEXT_MODULE, $cm->id); + if (!$glossary->allowcomments and !has_capability('mod/glossary:managecomments', $context)) { + error('You can\'t edit comments in this glossary!'); + } + if (($comment->userid <> $USER->id) and !has_capability('mod/glossary:managecomments', $context)) { + error('You can\'t edit other people\'s comments!'); + } + $ineditperiod = ((time() - $comment->timemodified < $CFG->maxeditingtime) || $glossary->editalways); + if ((!has_capability('mod/glossary:comment', $context) or !$ineditperiod) and !has_capability('mod/glossary:managecomments', $context)) { + error('You can\'t edit this. Time expired!'); + } + + $mform = new glossary_comment_form('comment.php'); + trusttext_prepare_edit($comment->entrycomment, $comment->format, can_use_html_editor(), $context); + $mform->set_defaults(array('cid'=>$cid, 'action'=>'edit', 'entrycomment'=>$comment->entrycomment, 'format'=>$comment->format)); + + if ($data = $mform->data_submitted()) { + trusttext_after_edit($data->entrycomment, $context); + + $updatedcomment = new object(); + $updatedcomment->id = $cid; + $updatedcomment->entrycomment = $data->entrycomment; + $updatedcomment->format = $data->format; + $updatedcomment->timemodified = time(); - if (isguest()) { - error('Guests are not allowed to post comments', $_SERVER['HTTP_REFERER']); + if (!update_record('glossary_comments', $updatedcomment)) { + error('Could not update this comment'); + } else { + add_to_log($course->id, 'glossary', 'update comment', "comments.php?id=$cm->id&eid=$entry->id", "$updatedcomment->id",$cm->id); + } + redirect("comments.php?id=$cm->id&eid=$entry->id"); + + } else { + glossary_comment_print_header($course, $cm, $glossary, $entry, 'edit'); + $mform->display(); + print_footer($course); + die; } - add_to_log($course->id, 'glossary', 'view', "view.php?id=$cm->id", "$glossary->id",$cm->id); +} + +////////////////////////////////// +/// utility functions +////////////////////////////////// - switch ( $action ){ +function glossary_comment_print_header($course, $cm, $glossary, $entry, $action) { + switch ($action){ case 'add': $straction = get_string('addingcomment','glossary'); break; @@ -56,118 +213,15 @@ case 'delete': $straction = get_string('deletingcomment','glossary'); break; - default: - $action = 'add'; - $straction = get_string('addingcomment','glossary'); - break; } - $strglossaries = get_string('modulenameplural', 'glossary'); - $strglossary = get_string('modulename', 'glossary'); - $strcomments = get_string('comments', 'glossary'); - - /// Input section - - if ( $action == 'delete' ) { - if (($comment->userid <> $USER->id) and !has_capability('mod/glossary:managecomments', $context)) { - error('You can\'t delete other people\'s comments!'); - } - if (!$glossary->allowcomments && !has_capability('mod/glossary:managecomments', $context)) { - error('You can\'t delete comments in this glossary!'); - } - if ( data_submitted() and $confirm ) { - delete_records('glossary_comments','id', $cid); - add_to_log($course->id, 'glossary', 'delete comment', "comments.php?id=$cm->id&eid=$entry->id", "$comment->id",$cm->id); - redirect("comments.php?id=$cm->id&eid=$entry->id"); - } else { - print_header_simple(format_string($glossary->name), '', - "id\">$strglossaries -> id\">".format_string($glossary->name,true)." -> id&eid=$entry->id\">$strcomments -> " . $straction, - '', '', true, update_module_button($cm->id, $course->id, $strglossary), - navmenu($course, $cm)); - glossary_print_comment($course, $cm, $glossary, $entry, $comment); - print_simple_box_start('center','40%', '#FFBBBB'); - echo '

'.get_string('areyousuredeletecomment','glossary'); - ?> -
- - - - - - - - -
-
- allowcomments && !has_capability('mod/glossary:comment', $context)) { - error('You can\'t add/edit comments to this glossary!'); - } - if ( $action == 'edit' ) { - - if (!isset($comment->timemodified)) { - $timetocheck = 0; - } else { - $timetocheck = $comment->timemodified; - } - $ineditperiod = ((time() - $timetocheck < $CFG->maxeditingtime) || $glossary->editalways); - if ( (!$ineditperiod || $USER->id != $comment->userid) and !has_capability('mod/glossary:comment', $context) and $cid) { - if ( $USER->id != $comment->userid ) { - error('You can\'t edit other people\'s comments!'); - } elseif (!$ineditperiod) { - error('You can\'t edit this. Time expired!'); - } - die; - } - } - - $mform = new glossary_comment_form('comment.php', - compact('comment', 'cm', 'entry', 'action', 'context')); - if ($fromform = $mform->data_submitted()) { - trusttext_after_edit($fromform->comment, $context); - $newentry->entryid = $entry->id; - $newentry->entrycomment = $fromform->comment; - $newentry->format = $fromform->format; - $newentry->timemodified = time(); - - if ($action == 'add') { - - $newentry->userid = $USER->id; - - if (! $newentry->id = insert_record('glossary_comments', $newentry)) { - error('Could not insert this new comment'); - } else { - add_to_log($course->id, 'glossary', 'add comment', "comments.php?id=$cm->id&eid=$entry->id", "$newentry->id", $cm->id); - } - } else { - $newentry->id = $fromform->cid; - $newentry->userid = $comment->userid; - - if (! update_record('glossary_comments', $newentry)) { - error('Could not update this comment'); - } else { - add_to_log($course->id, 'glossary', 'update comment', "comments.php?id=$cm->id&eid=$entry->id", "$newentry->id",$cm->id); - } - } - redirect("comments.php?id=$cm->id&eid=$entry->id"); - - } else { - print_header_simple(format_string($glossary->name), '', - "id\">$strglossaries -> id\">". - format_string($glossary->name,true)." -> id&eid=$entry->id\">$strcomments -> " . $straction, - '', '', true, update_module_button($cm->id, $course->id, $strglossary), navmenu($course, $cm)); - /// original glossary entry - glossary_print_entry($course, $cm, $glossary, $entry, 'approval', '', false); - // TODO add buttons - //helpbutton("writing", get_string("helpwriting"), "moodle", true, true); - - $mform->display(); - } - } - /// Finish the page - print_footer($course); + $strglossaries = get_string('modulenameplural', 'glossary'); + $strglossary = get_string('modulename', 'glossary'); + $strcomments = get_string('comments', 'glossary'); + + print_header_simple(format_string($glossary->name), '', + "id\">$strglossaries -> id\">".format_string($glossary->name,true)." -> id&eid=$entry->id\">$strcomments -> " . $straction, + '', '', true, update_module_button($cm->id, $course->id, $strglossary), + navmenu($course, $cm)); +} ?> \ No newline at end of file diff --git a/mod/glossary/comment_form.php b/mod/glossary/comment_form.php index 9b8bca89bc..5a6e6c6420 100644 --- a/mod/glossary/comment_form.php +++ b/mod/glossary/comment_form.php @@ -1,70 +1,33 @@ libdir.'/formslib.php'; +require_once $CFG->libdir.'/formslib.php'; + class glossary_comment_form extends moodleform { function definition() { - $mform =& $this->_form; - if(isset($this->_customdata['comment'])) { - $comment = $this->_customdata['comment']; - $commentid=$comment->id; - $commenttext = $comment->entrycomment; - $defaultformat=$comment->format; - - }else{ - $commentid=0; - $commenttext =''; - $defaultformat=null; - } - $entry = $this->_customdata['entry']; - $cm = $this->_customdata['cm']; - $action = $this->_customdata['action']; - - $mform =& $this->_form; - - //two pronged attack for trusttext - //submitted value - if (!empty($_POST)){ - trusttext_prepare_edit($_POST['comment'], $_POST['format'], can_use_html_editor(), - $this->_customdata['context']); - } - - $mform->addElement('htmleditor','comment',get_string("comment", "glossary")); - $mform->setType('comment', PARAM_RAW); - - $mform->addElement('format', 'format', get_string("format")); - $mform->setDefault('format',$defaultformat); + $mform =& $this->_form; - //second prong : defaults - // format element works it's default out for itself - $format=$mform->exportValue('format'); - trusttext_prepare_edit($commenttext, $format, can_use_html_editor(), - $this->_customdata['context']); - $mform->setDefault('format',$format); - $mform->setDefault('comment',$commenttext); + // visible elements + $mform->addElement('htmleditor', 'entrycomment',get_string('comment', 'glossary')); + $mform->addRule('entrycomment', get_string('required'), 'required', null, 'client'); + $mform->setType('entrycomment', PARAM_RAW); // processed by trusttext or cleaned before the display + $mform->addElement('format', 'format', get_string('format')); - //hidden elements, in this case setType may not be needed as these - //are all processed by optional_param in comment.php but just in case - //someone later gets data from form->data_submitted() we'll add them. - $mform->addElement('hidden','cid',$comment->id); + // hidden optional params + $mform->addElement('hidden', 'cid', 0); $mform->setType('cid', PARAM_INT); - $mform->addElement('hidden','id',$cm->id); - $mform->setType('cid', PARAM_INT); - - $mform->addElement('hidden','eid',$entry->id); + $mform->addElement('hidden', 'eid', 0); $mform->setType('eid', PARAM_INT); - $mform->addElement('hidden','action',$action); + $mform->addElement('hidden', 'action', ''); $mform->setType('action', PARAM_ACTION); - $buttonarray[] = &MoodleQuickForm::createElement('submit','submit',get_string("savechanges")); - $buttonarray[] = &MoodleQuickForm::createElement('reset','reset',get_string("revert")); - $mform->addGroup($buttonarray,'buttonar','', array(" "), false); - - + // buttons + $buttonarray[] = &MoodleQuickForm::createElement('submit', 'submit', get_string('savechanges')); + $buttonarray[] = &MoodleQuickForm::createElement('reset', 'reset', get_string('revert')); + $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); } - } ?> \ No newline at end of file diff --git a/mod/glossary/comments.php b/mod/glossary/comments.php index cdb31450fb..4deba4a19f 100644 --- a/mod/glossary/comments.php +++ b/mod/glossary/comments.php @@ -58,7 +58,7 @@ print_heading(format_string(get_string('commentson','glossary')." \"$entry->concept\"")); if ($glossary->allowcomments || has_capability('mod/glossary:managecomments', $context)) { - print_heading("id&eid=$entry->id\">$straddcomment \"\""); + print_heading("id\">$straddcomment \"\""); } if ($comments = get_records("glossary_comments","entryid",$entry->id,"timemodified ASC")) { diff --git a/mod/glossary/lib.php b/mod/glossary/lib.php index 41b3b0f0d3..fd9fc0d14f 100644 --- a/mod/glossary/lib.php +++ b/mod/glossary/lib.php @@ -799,7 +799,7 @@ function glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode='',$h if (has_capability('mod/glossary:comment', $context)) { $output = true; - $return .= ' '.get_string('addcomment','glossary').''; + $return .= ' '.get_string('addcomment','glossary').''; } @@ -1589,11 +1589,11 @@ function glossary_print_comment($course, $cm, $glossary, $entry, $comment) { $ineditperiod = ((time() - $comment->timemodified < $CFG->maxeditingtime) || $glossary->editalways); if ( ($glossary->allowcomments && $ineditperiod && $USER->id == $comment->userid) || has_capability('mod/glossary:managecomments', $context)) { - echo "id&eid=$entry->id&cid=$comment->id&action=edit\">id&action=edit\">\""pixpath/t/edit.gif\" height=\"11\" width=\"11\" border=\"0\" /> "; } if ( ($glossary->allowcomments && $USER->id == $comment->userid) || has_capability('mod/glossary:managecomments', $context) ) { - echo "id&eid=$entry->id&cid=$comment->id&action=delete\">id&action=delete\">\""pixpath/t/delete.gif\" height=\"11\" width=\"11\" border=\"0\" />"; }