From 989c16eeb9acb49e98be8a6164ca39b88594df32 Mon Sep 17 00:00:00 2001 From: skodak Date: Tue, 5 May 2009 08:57:12 +0000 Subject: [PATCH] MDL-16692 converted glossary comments to new editor + file API fixes --- lang/en_utf8/error.php | 1 - lib/filelib.php | 21 +++++++--- mod/glossary/comment.php | 78 +++++++++++++++++------------------ mod/glossary/comment_form.php | 24 ++++++----- mod/glossary/comments.php | 2 +- mod/glossary/lib.php | 6 +-- 6 files changed, 72 insertions(+), 60 deletions(-) diff --git a/lang/en_utf8/error.php b/lang/en_utf8/error.php index 2afdc6d16c..676b006614 100644 --- a/lang/en_utf8/error.php +++ b/lang/en_utf8/error.php @@ -84,7 +84,6 @@ $string['cannotimport'] = 'Import error'; $string['cannotimportgrade'] = 'Grade import error'; $string['cannotimportformat'] = 'Sorry, importing this format is not yet implemented!'; $string['cannotinsertcategory'] = 'Weird error. The category was not inserted.'; -$string['cannotinsertcomment'] = 'Could not insert this new comment'; $string['cannotinsertgrade'] = 'Cannot insert grade item without course id!'; $string['cannotinsertrate'] = 'Could not insert a new rating ($a->id = $a->rating)'; $string['cannotinsertrecord'] = 'Could not insert new record ID $a'; diff --git a/lib/filelib.php b/lib/filelib.php index f4acc0f13f..a59935bd72 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -128,6 +128,9 @@ function file_prepare_standard_editor($data, $field, array $options, $context=nu if (!isset($options['subdirs'])) { $options['subdirs'] = false; } + if (!isset($options['maxfiles'])) { + $options['maxfiles'] = 0; // no files by default + } if (empty($data->id) or empty($context)) { $contextid = null; @@ -152,9 +155,13 @@ function file_prepare_standard_editor($data, $field, array $options, $context=nu $contextid = $context->id; } - $draftid_editor = file_get_submitted_draft_itemid($field); - $currenttext = file_prepare_draft_area($draftid_editor, $contextid, $filearea, $data->id, $options['subdirs'], $data->{$field}, $options['forcehttps']); - $data->{$field.'_editor'} = array('text'=>$currenttext, 'format'=>$data->{$field.'format'}, 'itemid'=>$draftid_editor); + if ($options['maxfiles'] != 0) { + $draftid_editor = file_get_submitted_draft_itemid($field); + $currenttext = file_prepare_draft_area($draftid_editor, $contextid, $filearea, $data->id, $options['subdirs'], $data->{$field}, $options['forcehttps']); + $data->{$field.'_editor'} = array('text'=>$currenttext, 'format'=>$data->{$field.'format'}, 'itemid'=>$draftid_editor); + } else { + $data->{$field.'_editor'} = array('text'=>$data->{$field}, 'format'=>$data->{$field.'format'}, 0); + } return $data; } @@ -169,7 +176,7 @@ function file_prepare_standard_editor($data, $field, array $options, $context=nu * @param int $itemid item id, required if item exists * @return object modified data object */ -function file_postupdate_standard_editor($data, $field, array $options, $context, $filearea, $itemid) { +function file_postupdate_standard_editor($data, $field, array $options, $context, $filearea=null, $itemid=null) { $options = (array)$options; if (!isset($options['trusttext'])) { $options['trusttext'] = false; @@ -195,7 +202,11 @@ function file_postupdate_standard_editor($data, $field, array $options, $context $editor = $data->{$field.'_editor'}; - $data->{$field} = file_save_draft_area_files($editor['itemid'], $context->id, $filearea, $itemid, $options, $editor['text'], $options['forcehttps']); + if ($options['maxfiles'] != 0 or is_null($filearea) or is_null($itemid)) { + $data->{$field} = file_save_draft_area_files($editor['itemid'], $context->id, $filearea, $itemid, $options, $editor['text'], $options['forcehttps']); + } else { + $data->{$field} = $editor['text']; + } $data->{$field.'format'} = $editor['format']; return $data; diff --git a/mod/glossary/comment.php b/mod/glossary/comment.php index 43f6e427c5..befe17f585 100644 --- a/mod/glossary/comment.php +++ b/mod/glossary/comment.php @@ -30,9 +30,9 @@ switch ($action) { function glossary_comment_add() { global $USER, $DB; - $eid = optional_param('eid', 0, PARAM_INT); // Entry ID + $entryid = optional_param('entryid', 0, PARAM_INT); // Entry ID - if (!$entry = $DB->get_record('glossary_entries', array('id'=>$eid))) { + if (!$entry = $DB->get_record('glossary_entries', array('id'=>$entryid))) { print_error('invalidentry'); } if (!$glossary = $DB->get_record('glossary', array('id'=>$entry->glossaryid))) { @@ -45,34 +45,37 @@ function glossary_comment_add() { print_error('coursemisconf'); } - require_login($course->id, false, $cm); + require_login($course, false, $cm); $context = get_context_instance(CONTEXT_MODULE, $cm->id); /// Both the configuration and capability must allow comments if (!$glossary->allowcomments or !has_capability('mod/glossary:comment', $context)) { print_error('nopermissiontocomment'); } - $mform = new mod_glossary_comment_form(); - $mform->set_data(array('eid'=>$eid, 'action'=>'add')); + $commentoptions = array('trusttext'=>true, 'maxfiles'=>0); + + $comment = new object(); + $comment->id = null; + $comment->action = 'edit'; + $comment->entryid = $entry->id; + + $comment = file_prepare_standard_editor($comment, 'entrycomment', $commentoptions, $context); + + $mform = new mod_glossary_comment_form(array('current'=>$comment, 'commentoptions'=>$commentoptions)); if ($mform->is_cancelled()) { redirect("comments.php?id=$cm->id&eid=$entry->id"); } - if ($data = $mform->get_data()) { - $newcomment = new object(); - $newcomment->entryid = $entry->id; - $newcomment->entrycomment = $data->entrycomment; - $newcomment->entrycommentformat = $data->entrycommentformat; - $newcomment->entrycommenttrust = trusttext_trusted($context); - $newcomment->timemodified = time(); - $newcomment->userid = $USER->id; - - if (!$newcomment->id = $DB->insert_record('glossary_comments', $newcomment)) { - print_error('cannotinsertcomment'); - } else { - add_to_log($course->id, 'glossary', 'add comment', "comments.php?id=$cm->id&eid=$entry->id", "$newcomment->id", $cm->id); - } + if ($newcomment = $mform->get_data()) { + + $newcomment = file_postupdate_standard_editor($newcomment, 'entrycomment', $newcommentoptions, $context);//no files - can be used before insert + $newcomment->timemodified = time(); + $newcomment->userid = $USER->id; + + $newcomment->id = $DB->insert_record('glossary_comments', $newcomment); + + 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 { @@ -89,10 +92,10 @@ function glossary_comment_add() { function glossary_comment_delete() { global $USER, $DB; - $cid = optional_param('cid', 0, PARAM_INT); // Comment ID + $id = optional_param('id', 0, PARAM_INT); // Comment ID $confirm = optional_param('confirm', 0, PARAM_BOOL); // delete confirmation - if (!$comment = $DB->get_record('glossary_comments', array('id'=>$cid))) { + if (!$comment = $DB->get_record('glossary_comments', array('id'=>$id))) { print_error('invalidcomment'); } if (!$entry = $DB->get_record('glossary_entries', array('id'=>$comment->entryid))) { @@ -108,7 +111,7 @@ function glossary_comment_delete() { print_error('coursemisconf'); } - require_login($course->id, false, $cm); + require_login($course, false, $cm); $context = get_context_instance(CONTEXT_MODULE, $cm->id); if (($comment->userid <> $USER->id) and !has_capability('mod/glossary:managecomments', $context)) { print_error('nopermissiontodelcomment', 'glossary'); @@ -118,15 +121,15 @@ function glossary_comment_delete() { } if (data_submitted() and $confirm) { - $DB->delete_records('glossary_comments', array('id'=>$cid)); + $DB->delete_records('glossary_comments', array('id'=>$id)); 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); + $optionsyes = array('action'=>'delete', 'id'=>$id, 'confirm'=>1); $linkno = 'comments.php'; - $optionsno = array('id'=>$cm->id, 'eid'=>$entry->id); + $optionsno = array('id'=>$cm->id, 'entryid'=>$entry->id); $strdeletewarning = get_string('areyousuredeletecomment','glossary'); glossary_comment_print_header($course, $cm, $glossary, $entry, 'delete'); @@ -143,9 +146,9 @@ function glossary_comment_delete() { function glossary_comment_edit() { global $CFG, $USER, $DB; - $cid = optional_param('cid', 0, PARAM_INT); // Comment ID + $id = optional_param('id', 0, PARAM_INT); // Comment ID - if (!$comment = $DB->get_record('glossary_comments', array('id'=>$cid))) { + if (!$comment = $DB->get_record('glossary_comments', array('id'=>$id))) { print_error('invalidcomment'); } if (!$entry = $DB->get_record('glossary_entries', array('id'=>$comment->entryid))) { @@ -161,7 +164,7 @@ function glossary_comment_edit() { print_error('coursemisconf'); } - require_login($course->id, false, $cm); + require_login($course, false, $cm); $context = get_context_instance(CONTEXT_MODULE, $cm->id); if (!$glossary->allowcomments and !has_capability('mod/glossary:managecomments', $context)) { print_error('nopermissiontodelinglossary', 'glossary'); @@ -174,20 +177,17 @@ function glossary_comment_edit() { print_error('cannoteditcommentexpired'); } - // clean up existing text if needed - $comment = trusttext_pre_edit($comment, 'entrycomment', $context); + $commentoptions = array('trusttext'=>true, 'maxfiles'=>0); + + $comment->action = 'edit'; + $comment = file_prepare_standard_editor($comment, 'entrycomment', $commentoptions, $context); - $mform = new mod_glossary_comment_form(); - $mform->set_data(array('cid'=>$cid, 'action'=>'edit', 'entrycomment'=>$comment->entrycomment, 'entrycommentformat'=>$comment->entrycommentformat)); + $mform = new mod_glossary_comment_form(array('current'=>$comment, 'commentoptions'=>$commentoptions)); - if ($data = $mform->get_data()) { + if ($updatedcomment = $mform->get_data()) { - $updatedcomment = new object(); - $updatedcomment->id = $cid; - $updatedcomment->entrycomment = $data->entrycomment; - $updatedcomment->entrycommentformat = $data->entrycommentformat; - $updatedcomment->entrycommenttrust = trusttext_trusted($context); - $updatedcomment->timemodified = time(); + $updatedcomment = file_postupdate_standard_editor($updatedcomment, 'entrycomment', $commentoptions, $context); + $updatedcomment->timemodified = time(); $DB->update_record('glossary_comments', $updatedcomment); add_to_log($course->id, 'glossary', 'update comment', "comments.php?id=$cm->id&eid=$entry->id", "$updatedcomment->id",$cm->id); diff --git a/mod/glossary/comment_form.php b/mod/glossary/comment_form.php index c9ed5b6019..d576f0efd1 100644 --- a/mod/glossary/comment_form.php +++ b/mod/glossary/comment_form.php @@ -6,21 +6,20 @@ class mod_glossary_comment_form extends moodleform { function definition() { $mform =& $this->_form; + $current = $this->_customdata['current']; + $commentoptions = $this->_customdata['commentoptions']; + // 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 trust text or cleaned before the display - $mform->setHelpButton('entrycomment', array('writing', 'richtext2'), false, 'editorhelpbutton'); - - $mform->addElement('format', 'entrycommentformat', get_string('format')); - $mform->setHelpButton('entrycommentformat', array('textformat', get_string('helpformatting'))); + $mform->addElement('editor', 'entrycomment_editor', get_string('comment', 'glossary'), $commentoptions); + $mform->addRule('entrycomment_editor', get_string('required'), 'required', null, 'client'); + $mform->setType('entrycomment_editor', PARAM_RAW); // processed by trust text or cleaned before the display // hidden optional params - $mform->addElement('hidden', 'cid', 0); - $mform->setType('cid', PARAM_INT); + $mform->addElement('hidden', 'id', 0); + $mform->setType('id', PARAM_INT); - $mform->addElement('hidden', 'eid', 0); - $mform->setType('eid', PARAM_INT); + $mform->addElement('hidden', 'entryid', 0); + $mform->setType('entryid', PARAM_INT); $mform->addElement('hidden', 'action', ''); $mform->setType('action', PARAM_ACTION); @@ -28,6 +27,9 @@ class mod_glossary_comment_form extends moodleform { //------------------------------------------------------------------------------- // buttons $this->add_action_buttons(false); + +//------------------------------------------------------------------------------- + $this->set_data($current); } } ?> \ No newline at end of file diff --git a/mod/glossary/comments.php b/mod/glossary/comments.php index 09c769b844..1019a7f604 100644 --- a/mod/glossary/comments.php +++ b/mod/glossary/comments.php @@ -56,7 +56,7 @@ print_heading(format_string(get_string('commentson','glossary')." \"$entry->concept\"")); if (has_capability('mod/glossary:comment', $context) and $glossary->allowcomments) { - print_heading("id\">$straddcomment \"$straddcomment\""); + print_heading("id\">$straddcomment \"$straddcomment\""); } if ($comments = $DB->get_records("glossary_comments", array("entryid"=>$entry->id), "timemodified ASC")) { diff --git a/mod/glossary/lib.php b/mod/glossary/lib.php index 10a7c8047d..cf4b31ebe4 100644 --- a/mod/glossary/lib.php +++ b/mod/glossary/lib.php @@ -751,7 +751,7 @@ function glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode='',$h if (has_capability('mod/glossary:comment', $context) and $glossary->allowcomments) { $output = true; - $return .= ' '.get_string('addcomment','glossary').$altsuffix.''; + $return .= ' '.get_string('addcomment','glossary').$altsuffix.''; } @@ -1431,11 +1431,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&action=edit\">id&action=edit\">\""pixpath/t/edit.gif\" class=\"iconsmall\" /> "; } if ( ($glossary->allowcomments && $USER->id == $comment->userid) || has_capability('mod/glossary:managecomments', $context) ) { - echo "id&action=delete\">id&action=delete\">\""pixpath/t/delete.gif\" class=\"iconsmall\" />"; } -- 2.39.5