]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-6940 - I decided to rewrite the glossary comments UI, the original code (pre...
authorskodak <skodak>
Sat, 28 Oct 2006 21:55:19 +0000 (21:55 +0000)
committerskodak <skodak>
Sat, 28 Oct 2006 21:55:19 +0000 (21:55 +0000)
mod/glossary/comment.php
mod/glossary/comment_form.php
mod/glossary/comments.php
mod/glossary/lib.php

index 0b20eefd4edf94737bdedc6c56646a8577b99611..e4441de06a54a0e09aa0f975edfc520416fdc3af 100644 (file)
 <?php // $Id$
 
-/// This page prints a particular instance of glossary
-    require_once('../../config.php');
-    require_once('lib.php');
-    include('comment_form.php');
-    $id   = required_param('id', PARAM_INT);             // Course Module ID
-    $eid  = required_param('eid', PARAM_INT);            // Entry ID
-    $cid  = optional_param('cid', 0, PARAM_INT);         // Comment ID
-    $confirm = optional_param('confirm',0, PARAM_INT);  // Confirm the action
-    $action = optional_param('action','add', PARAM_ACTION);
+require_once('../../config.php');
+require_once('lib.php');
+require_once('comment_form.php');
+
+$action = optional_param('action','add', PARAM_ACTION);
+
+if (isguest()) {
+    error('Guests are not allowed to post comments!');
+}
+
+switch ($action) {
+    case 'add':
+        glossary_comment_add();
+        die;
+    case 'delete':
+        glossary_comment_delete();
+        die;
+    case 'edit':
+        glossary_comment_edit();
+        die;
+    default:
+        error('Incorrect action specified');
+}
+
+/**
+ * Add new comment
+ */
+function glossary_comment_add() {
+    global $USER;
+
+    $eid = optional_param('eid', 0, PARAM_INT); // Entry ID
 
-    $action = strtolower($action);
+    if (!$entry = get_record('glossary_entries', 'id', $eid)) {
+        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: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&amp;eid=$entry->id", "$newcomment->id", $cm->id);
+        }
+        redirect("comments.php?id=$cm->id&amp;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&amp;eid=$entry->id", "$comment->id",$cm->id);
+        redirect("comments.php?id=$cm->id&amp;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&amp;eid=$entry->id", "$updatedcomment->id",$cm->id);
+        }
+        redirect("comments.php?id=$cm->id&amp;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;
         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&amp;eid=$entry->id", "$comment->id",$cm->id);
-            redirect("comments.php?id=$cm->id&amp;eid=$entry->id");
 
-        } else {
-            print_header_simple(format_string($glossary->name), '',
-    "<a href=\"index.php?id=$course->id\">$strglossaries</a> -> <a href=\"view.php?id=$cm->id\">".format_string($glossary->name,true)."</a> -> <a href=\"comments.php?id=$cm->id&amp;eid=$entry->id\">$strcomments</a> -> " . $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 '<center><br />'.get_string('areyousuredeletecomment','glossary');
-                ?>
-                    <form name="form" method="post" action="comment.php">
-                    <input type="hidden" name="id"          value="<?php p($id) ?>" />
-                    <input type="hidden" name="eid"         value="<?php p($eid) ?>" />
-                    <input type="hidden" name="cid"         value="<?php p($cid) ?>" />
-                    <input type="hidden" name="action"      value="delete" />
-                    <input type="hidden" name="confirm"     value="1" />
-                    <input type="submit" value="<?php print_string('yes')?>" />
-                    <input type="button" value="<?php print_string('no')?>" onclick="javascript:history.go(-1);" />
-
-                    </form>
-                    </center>
-                <?php
-            print_simple_box_end();
-        }
-    } else {
-
-        if (!$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&amp;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&amp;eid=$entry->id", "$newentry->id",$cm->id);
-                }
-            }
-            redirect("comments.php?id=$cm->id&amp;eid=$entry->id");
-
-        } else {
-            print_header_simple(format_string($glossary->name), '',
-                "<a href=\"index.php?id=$course->id\">$strglossaries</a> -> <a href=\"view.php?id=$cm->id\">".
-                format_string($glossary->name,true)."</a> -> <a href=\"comments.php?id=$cm->id&amp;eid=$entry->id\">$strcomments</a> -> " . $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), '',
+        "<a href=\"index.php?id=$course->id\">$strglossaries</a> -> <a href=\"view.php?id=$cm->id\">".format_string($glossary->name,true)."</a> -> <a href=\"comments.php?id=$cm->id&amp;eid=$entry->id\">$strcomments</a> -> " . $straction,
+        '', '', true, update_module_button($cm->id, $course->id, $strglossary),
+        navmenu($course, $cm));
+}
 ?>
\ No newline at end of file
index 9b8bca89bcb9f834f4a54cbf77f00b795c12167d..5a6e6c642086a37050f40e6b23bf3dd88dd80935 100644 (file)
@@ -1,70 +1,33 @@
 <?php //$Id$
 
 // TODO - THE HELP BUTTON FOR FORMATTING TO BE Included and formatting drop down box.
-include_once $CFG->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
index cdb31450fb3d5468ea916068d47305f1ec26a437..4deba4a19fdc2770cdf42e4122dde360579aedd1 100644 (file)
@@ -58,7 +58,7 @@
     print_heading(format_string(get_string('commentson','glossary')." <b>\"$entry->concept\"</b>"));
 
     if ($glossary->allowcomments || has_capability('mod/glossary:managecomments', $context)) {
-        print_heading("<a href=\"comment.php?id=$cm->id&amp;eid=$entry->id\">$straddcomment</a> <img title=\"$straddcomment\" src=\"comment.gif\" height=\"11\" width=\"11\" border=\"0\" alt=\"\" />");
+        print_heading("<a href=\"comment.php?action=add&amp;eid=$entry->id\">$straddcomment</a> <img title=\"$straddcomment\" src=\"comment.gif\" height=\"11\" width=\"11\" border=\"0\" alt=\"\" />");
     }
 
     if ($comments = get_records("glossary_comments","entryid",$entry->id,"timemodified ASC")) {
index 41b3b0f0d3cbaaee0371a8df58970c60c4fc482c..fd9fc0d14f1ce696f2c331999459c33e0f1ce114 100644 (file)
@@ -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 .= ' <a title="' . get_string('addcomment','glossary') . '" href="comment.php?id='.$cm->id.'&amp;eid='.$entry->id.'"><img src="comment.gif" height="11" width="11" border="0" alt="'.get_string('addcomment','glossary').'" /></a>';
+        $return .= ' <a title="' . get_string('addcomment','glossary') . '" href="comment.php?action=add&amp;eid='.$entry->id.'"><img src="comment.gif" height="11" width="11" border="0" alt="'.get_string('addcomment','glossary').'" /></a>';
     }
 
 
@@ -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 "<a href=\"comment.php?id=$cm->id&amp;eid=$entry->id&amp;cid=$comment->id&amp;action=edit\"><img  
+        echo "<a href=\"comment.php?cid=$comment->id&amp;action=edit\"><img  
                alt=\"" . get_string("edit") . "\" src=\"$CFG->pixpath/t/edit.gif\" height=\"11\" width=\"11\" border=\"0\" /></a> ";
     }
     if ( ($glossary->allowcomments && $USER->id == $comment->userid) || has_capability('mod/glossary:managecomments', $context) ) {
-        echo "<a href=\"comment.php?id=$cm->id&amp;eid=$entry->id&amp;cid=$comment->id&amp;action=delete\"><img  
+        echo "<a href=\"comment.php?cid=$comment->id&amp;action=delete\"><img  
                alt=\"" . get_string("delete") . "\" src=\"$CFG->pixpath/t/delete.gif\" height=\"11\" width=\"11\" border=\"0\" /></a>";
     }