Esempio n. 1
0
/**
 * 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 mod_glossary_comment_form();
    trusttext_prepare_edit($comment->entrycomment, $comment->format, can_use_html_editor(), $context);
    $mform->set_data(array('cid' => $cid, 'action' => 'edit', 'entrycomment' => $comment->entrycomment, 'format' => $comment->format));
    if ($data = $mform->get_data()) {
        trusttext_after_edit($data->entrycomment, $context);
        $updatedcomment = new object();
        $updatedcomment->id = $cid;
        $updatedcomment->entrycomment = $data->entrycomment;
        $updatedcomment->format = $data->format;
        $updatedcomment->timemodified = time();
        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;
    }
}
Esempio n. 2
0
/**
 * Edit existing comments
 */
function glossary_comment_edit()
{
    global $CFG, $USER, $DB;
    $id = optional_param('id', 0, PARAM_INT);
    // Comment ID
    if (!($comment = $DB->get_record('glossary_comments', array('id' => $id)))) {
        print_error('invalidcomment');
    }
    if (!($entry = $DB->get_record('glossary_entries', array('id' => $comment->entryid)))) {
        print_error('invalidentry');
    }
    if (!($glossary = $DB->get_record('glossary', array('id' => $entry->glossaryid)))) {
        print_error('invalidid', 'glossary');
    }
    if (!($cm = get_coursemodule_from_instance('glossary', $glossary->id))) {
        print_error('invalidcoursemodule');
    }
    if (!($course = $DB->get_record('course', array('id' => $cm->course)))) {
        print_error('coursemisconf');
    }
    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');
    }
    if ($comment->userid != $USER->id and !has_capability('mod/glossary:managecomments', $context)) {
        print_error('nopermissiontoeditcomment');
    }
    $ineditperiod = time() - $comment->timemodified < $CFG->maxeditingtime || $glossary->editalways;
    if ((!has_capability('mod/glossary:comment', $context) or !$ineditperiod) and !has_capability('mod/glossary:managecomments', $context)) {
        print_error('cannoteditcommentexpired');
    }
    $commentoptions = array('trusttext' => true, 'maxfiles' => 0);
    $comment->action = 'edit';
    $comment = file_prepare_standard_editor($comment, 'entrycomment', $commentoptions, $context);
    $mform = new mod_glossary_comment_form(array('current' => $comment, 'commentoptions' => $commentoptions));
    if ($updatedcomment = $mform->get_data()) {
        $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}&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;
    }
}