2009-11-01 14:55:15 +00:00
|
|
|
<?php
|
2003-09-16 18:51:40 +00:00
|
|
|
|
2005-02-17 08:38:50 +00:00
|
|
|
require_once('../../config.php');
|
|
|
|
require_once('lib.php');
|
2006-12-19 07:03:08 +00:00
|
|
|
require_once('edit_form.php');
|
2003-09-16 18:51:40 +00:00
|
|
|
|
2008-08-17 21:27:43 +00:00
|
|
|
$cmid = required_param('cmid', PARAM_INT); // Course Module ID
|
|
|
|
$id = optional_param('id', 0, PARAM_INT); // EntryID
|
2003-09-16 18:51:40 +00:00
|
|
|
|
2008-08-17 21:27:43 +00:00
|
|
|
if (!$cm = get_coursemodule_from_id('glossary', $cmid)) {
|
2008-06-06 03:15:53 +00:00
|
|
|
print_error('invalidcoursemodule');
|
2003-09-16 18:51:40 +00:00
|
|
|
}
|
|
|
|
|
2008-08-17 21:27:43 +00:00
|
|
|
if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
|
2008-06-06 03:15:53 +00:00
|
|
|
print_error('coursemisconf');
|
2003-09-16 18:51:40 +00:00
|
|
|
}
|
|
|
|
|
2009-04-16 06:36:11 +00:00
|
|
|
require_login($course, false, $cm);
|
2003-09-16 18:51:40 +00:00
|
|
|
|
2008-08-17 21:27:43 +00:00
|
|
|
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
|
|
|
|
|
|
|
|
if (!$glossary = $DB->get_record('glossary', array('id'=>$cm->instance))) {
|
2008-06-06 03:15:53 +00:00
|
|
|
print_error('invalidid', 'glossary');
|
2003-09-16 18:51:40 +00:00
|
|
|
}
|
2004-08-28 19:15:52 +00:00
|
|
|
|
2010-01-16 15:39:56 +00:00
|
|
|
$url = new moodle_url('/mod/glossary/edit.php', array('cmid'=>$cm->id));
|
2009-09-11 05:38:44 +00:00
|
|
|
if (!empty($id)) {
|
|
|
|
$url->param('id', $id);
|
|
|
|
}
|
|
|
|
$PAGE->set_url($url);
|
|
|
|
|
2008-08-17 21:27:43 +00:00
|
|
|
if ($id) { // if entry is specified
|
2010-03-31 07:41:31 +00:00
|
|
|
if (isguestuser()) {
|
|
|
|
print_error('guestnoedit', 'glossary', "$CFG->wwwroot/mod/glossary/view.php?id=$cmid");
|
|
|
|
}
|
|
|
|
|
2008-08-17 21:27:43 +00:00
|
|
|
if (!$entry = $DB->get_record('glossary_entries', array('id'=>$id, 'glossaryid'=>$glossary->id))) {
|
2008-06-06 03:15:53 +00:00
|
|
|
print_error('invalidentry');
|
2007-02-13 19:25:04 +00:00
|
|
|
}
|
2008-08-17 21:27:43 +00:00
|
|
|
|
2007-02-13 19:25:04 +00:00
|
|
|
$ineditperiod = ((time() - $entry->timecreated < $CFG->maxeditingtime) || $glossary->editalways);
|
|
|
|
if (!has_capability('mod/glossary:manageentries', $context) and !($entry->userid == $USER->id and ($ineditperiod and has_capability('mod/glossary:write', $context)))) {
|
2008-08-17 21:27:43 +00:00
|
|
|
if ($USER->id != $fromdb->userid) {
|
|
|
|
print_error('errcannoteditothers', 'glossary', "view.php?id=$cm->id&mode=entry&hook=$id");
|
|
|
|
} elseif (!$ineditperiod) {
|
|
|
|
print_error('erredittimeexpired', 'glossary', "view.php?id=$cm->id&mode=entry&hook=$id");
|
|
|
|
}
|
2007-02-13 19:25:04 +00:00
|
|
|
}
|
2008-08-17 21:27:43 +00:00
|
|
|
|
|
|
|
//prepare extra data
|
|
|
|
if ($aliases = $DB->get_records_menu("glossary_alias", array("entryid"=>$id), '', 'id, alias')) {
|
|
|
|
$entry->aliases = implode("\n", $aliases) . "\n";
|
|
|
|
}
|
|
|
|
if ($categoriesarr = $DB->get_records_menu("glossary_entries_categories", array('entryid'=>$id), '', 'id, categoryid')) {
|
|
|
|
// TODO: this fetches cats from both main and secondary glossary :-(
|
|
|
|
$entry->categories = array_values($categoriesarr);
|
|
|
|
}
|
|
|
|
|
2006-10-20 07:01:51 +00:00
|
|
|
} else { // new entry
|
|
|
|
require_capability('mod/glossary:write', $context);
|
2010-03-31 07:41:31 +00:00
|
|
|
// note: guest user does not have any write capability
|
2010-09-21 08:37:36 +00:00
|
|
|
$entry = new stdClass();
|
2009-04-28 21:20:43 +00:00
|
|
|
$entry->id = null;
|
2004-08-28 19:15:52 +00:00
|
|
|
}
|
2006-10-20 07:01:51 +00:00
|
|
|
|
2009-04-28 21:20:43 +00:00
|
|
|
$maxfiles = 99; // TODO: add some setting
|
|
|
|
$maxbytes = $course->maxbytes; // TODO: add some setting
|
|
|
|
|
2010-06-10 17:56:14 +00:00
|
|
|
$definitionoptions = array('trusttext'=>true, 'subdirs'=>false, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes, 'context'=>$context);
|
2009-04-28 21:20:43 +00:00
|
|
|
$attachmentoptions = array('subdirs'=>false, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes);
|
2009-04-16 06:36:11 +00:00
|
|
|
|
2010-07-03 13:37:13 +00:00
|
|
|
$entry = file_prepare_standard_editor($entry, 'definition', $definitionoptions, $context, 'mod_glossary', 'entry', $entry->id);
|
|
|
|
$entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $context, 'mod_glossary', 'attachment', $entry->id);
|
2009-04-16 06:36:11 +00:00
|
|
|
|
2009-04-28 21:20:43 +00:00
|
|
|
$entry->cmid = $cm->id;
|
2009-04-16 06:36:11 +00:00
|
|
|
|
2009-04-29 07:52:35 +00:00
|
|
|
// create form and set initial data
|
|
|
|
$mform = new mod_glossary_entry_form(null, array('current'=>$entry, 'cm'=>$cm, 'glossary'=>$glossary,
|
|
|
|
'definitionoptions'=>$definitionoptions, 'attachmentoptions'=>$attachmentoptions));
|
2008-08-17 21:27:43 +00:00
|
|
|
|
2006-12-19 07:03:08 +00:00
|
|
|
if ($mform->is_cancelled()){
|
2008-08-17 21:27:43 +00:00
|
|
|
if ($id){
|
2010-07-18 08:39:58 +00:00
|
|
|
redirect("view.php?id=$cm->id&mode=entry&hook=$id");
|
2006-12-19 07:03:08 +00:00
|
|
|
} else {
|
|
|
|
redirect("view.php?id=$cm->id");
|
|
|
|
}
|
|
|
|
|
2009-04-28 21:20:43 +00:00
|
|
|
} else if ($entry = $mform->get_data()) {
|
2003-10-21 04:55:22 +00:00
|
|
|
$timenow = time();
|
2005-04-18 18:54:56 +00:00
|
|
|
|
2009-04-28 21:20:43 +00:00
|
|
|
$categories = empty($entry->categories) ? array() : $entry->categories;
|
|
|
|
unset($entry->categories);
|
|
|
|
$aliases = trim($entry->aliases);
|
|
|
|
unset($entry->aliases);
|
|
|
|
|
2008-08-17 21:27:43 +00:00
|
|
|
if (empty($entry->id)) {
|
|
|
|
$entry->glossaryid = $glossary->id;
|
|
|
|
$entry->timecreated = $timenow;
|
|
|
|
$entry->userid = $USER->id;
|
|
|
|
$entry->timecreated = $timenow;
|
|
|
|
$entry->sourceglossaryid = 0;
|
|
|
|
$entry->teacherentry = has_capability('mod/glossary:manageentries', $context);
|
2003-10-29 23:33:54 +00:00
|
|
|
}
|
2005-01-19 12:02:52 +00:00
|
|
|
|
2009-04-28 21:20:43 +00:00
|
|
|
$entry->concept = trim($entry->concept);
|
2009-04-20 10:05:50 +00:00
|
|
|
$entry->definition = ''; // updated later
|
|
|
|
$entry->definitionformat = FORMAT_HTML; // updated later
|
2009-04-28 21:20:43 +00:00
|
|
|
$entry->definitiontrust = 0; // updated later
|
2009-04-20 10:05:50 +00:00
|
|
|
$entry->timemodified = $timenow;
|
|
|
|
$entry->approved = 0;
|
2009-04-28 21:20:43 +00:00
|
|
|
$entry->usedynalink = isset($entry->usedynalink) ? $entry->usedynalink : 0;
|
|
|
|
$entry->casesensitive = isset($entry->casesensitive) ? $entry->casesensitive : 0;
|
|
|
|
$entry->fullmatch = isset($entry->fullmatch) ? $entry->fullmatch : 0;
|
2008-08-17 21:27:43 +00:00
|
|
|
|
|
|
|
if ($glossary->defaultapproval or has_capability('mod/glossary:approve', $context)) {
|
|
|
|
$entry->approved = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($entry->id)) {
|
|
|
|
//new entry
|
2009-04-16 06:36:11 +00:00
|
|
|
$entry->id = $DB->insert_record('glossary_entries', $entry);
|
2010-11-15 20:16:45 +01:00
|
|
|
|
|
|
|
// Update completion state
|
|
|
|
$completion = new completion_info($course);
|
|
|
|
if ($completion->is_enabled($cm) == COMPLETION_TRACKING_AUTOMATIC && $glossary->completionentries && $entry->approved) {
|
|
|
|
$completion->update_state($cm, COMPLETION_COMPLETE);
|
|
|
|
}
|
|
|
|
|
2009-04-16 06:36:11 +00:00
|
|
|
add_to_log($course->id, "glossary", "add entry",
|
2009-04-20 10:49:31 +00:00
|
|
|
"view.php?id=$cm->id&mode=entry&hook=$entry->id", $entry->id, $cm->id);
|
2006-12-28 21:21:44 +00:00
|
|
|
|
2008-08-17 21:27:43 +00:00
|
|
|
} else {
|
|
|
|
//existing entry
|
2009-04-16 06:36:11 +00:00
|
|
|
$DB->update_record('glossary_entries', $entry);
|
|
|
|
add_to_log($course->id, "glossary", "update entry",
|
|
|
|
"view.php?id=$cm->id&mode=entry&hook=$entry->id",
|
|
|
|
$entry->id, $cm->id);
|
2008-08-17 21:27:43 +00:00
|
|
|
}
|
2004-08-28 23:22:34 +00:00
|
|
|
|
2009-04-28 21:20:43 +00:00
|
|
|
// save and relink embedded images and save attachments
|
2010-07-03 13:37:13 +00:00
|
|
|
$entry = file_postupdate_standard_editor($entry, 'definition', $definitionoptions, $context, 'mod_glossary', 'entry', $entry->id);
|
|
|
|
$entry = file_postupdate_standard_filemanager($entry, 'attachment', $attachmentoptions, $context, 'mod_glossary', 'attachment', $entry->id);
|
2003-11-25 17:44:51 +00:00
|
|
|
|
2009-04-28 21:20:43 +00:00
|
|
|
// store the updated value values
|
2009-04-16 06:36:11 +00:00
|
|
|
$DB->update_record('glossary_entries', $entry);
|
2008-08-17 21:27:43 +00:00
|
|
|
|
2009-04-16 06:36:11 +00:00
|
|
|
//refetch complete entry
|
|
|
|
$entry = $DB->get_record('glossary_entries', array('id'=>$entry->id));
|
2003-10-21 04:55:22 +00:00
|
|
|
|
2008-08-17 21:27:43 +00:00
|
|
|
// update entry categories
|
|
|
|
$DB->delete_records('glossary_entries_categories', array('entryid'=>$entry->id));
|
|
|
|
// TODO: this deletes cats from both both main and secondary glossary :-(
|
2009-04-28 21:20:43 +00:00
|
|
|
if (!empty($categories) and array_search(0, $categories) === false) {
|
|
|
|
foreach ($categories as $catid) {
|
2010-09-21 08:37:36 +00:00
|
|
|
$newcategory = new stdClass();
|
2008-08-17 21:27:43 +00:00
|
|
|
$newcategory->entryid = $entry->id;
|
|
|
|
$newcategory->categoryid = $catid;
|
|
|
|
$DB->insert_record('glossary_entries_categories', $newcategory, false);
|
2003-11-05 00:06:31 +00:00
|
|
|
}
|
|
|
|
}
|
2006-12-19 07:03:08 +00:00
|
|
|
|
2008-08-17 21:27:43 +00:00
|
|
|
// update aliases
|
|
|
|
$DB->delete_records('glossary_alias', array('entryid'=>$entry->id));
|
|
|
|
if ($aliases !== '') {
|
|
|
|
$aliases = explode("\n", $aliases);
|
|
|
|
foreach ($aliases as $alias) {
|
|
|
|
$alias = trim($alias);
|
|
|
|
if ($alias !== '') {
|
2010-09-21 08:37:36 +00:00
|
|
|
$newalias = new stdClass();
|
2008-08-17 21:27:43 +00:00
|
|
|
$newalias->entryid = $entry->id;
|
|
|
|
$newalias->alias = $alias;
|
|
|
|
$DB->insert_record('glossary_alias', $newalias, false);
|
2003-11-05 00:06:31 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-22 10:42:39 +00:00
|
|
|
}
|
2008-08-17 21:27:43 +00:00
|
|
|
|
2010-07-18 08:39:58 +00:00
|
|
|
redirect("view.php?id=$cm->id&mode=entry&hook=$entry->id");
|
2004-05-10 18:10:17 +00:00
|
|
|
}
|
2006-12-19 07:03:08 +00:00
|
|
|
|
2010-05-28 06:38:17 +00:00
|
|
|
if (!empty($id)) {
|
|
|
|
$PAGE->navbar->add(get_string('edit'));
|
|
|
|
}
|
2008-08-17 21:27:43 +00:00
|
|
|
|
2009-09-07 03:20:35 +00:00
|
|
|
$PAGE->set_title(format_string($glossary->name));
|
2010-05-28 06:38:17 +00:00
|
|
|
$PAGE->set_heading($course->fullname);
|
2009-09-07 03:20:35 +00:00
|
|
|
echo $OUTPUT->header();
|
2009-08-06 08:23:39 +00:00
|
|
|
echo $OUTPUT->heading(format_string($glossary->name));
|
2003-09-16 03:07:21 +00:00
|
|
|
|
2006-12-19 07:03:08 +00:00
|
|
|
$mform->display();
|
2005-04-18 18:54:56 +00:00
|
|
|
|
2009-08-06 14:13:29 +00:00
|
|
|
echo $OUTPUT->footer();
|
2003-09-16 03:07:21 +00:00
|
|
|
|