Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/glossary/edit.php
Go to the documentation of this file.
00001 <?php
00002 
00003 require_once('../../config.php');
00004 require_once('lib.php');
00005 require_once('edit_form.php');
00006 
00007 $cmid = required_param('cmid', PARAM_INT);            // Course Module ID
00008 $id   = optional_param('id', 0, PARAM_INT);           // EntryID
00009 
00010 if (!$cm = get_coursemodule_from_id('glossary', $cmid)) {
00011     print_error('invalidcoursemodule');
00012 }
00013 
00014 if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
00015     print_error('coursemisconf');
00016 }
00017 
00018 require_login($course, false, $cm);
00019 
00020 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00021 
00022 if (!$glossary = $DB->get_record('glossary', array('id'=>$cm->instance))) {
00023     print_error('invalidid', 'glossary');
00024 }
00025 
00026 $url = new moodle_url('/mod/glossary/edit.php', array('cmid'=>$cm->id));
00027 if (!empty($id)) {
00028     $url->param('id', $id);
00029 }
00030 $PAGE->set_url($url);
00031 
00032 if ($id) { // if entry is specified
00033     if (isguestuser()) {
00034         print_error('guestnoedit', 'glossary', "$CFG->wwwroot/mod/glossary/view.php?id=$cmid");
00035     }
00036 
00037     if (!$entry = $DB->get_record('glossary_entries', array('id'=>$id, 'glossaryid'=>$glossary->id))) {
00038         print_error('invalidentry');
00039     }
00040 
00041     $ineditperiod = ((time() - $entry->timecreated <  $CFG->maxeditingtime) || $glossary->editalways);
00042     if (!has_capability('mod/glossary:manageentries', $context) and !($entry->userid == $USER->id and ($ineditperiod and has_capability('mod/glossary:write', $context)))) {
00043         if ($USER->id != $fromdb->userid) {
00044             print_error('errcannoteditothers', 'glossary', "view.php?id=$cm->id&amp;mode=entry&amp;hook=$id");
00045         } elseif (!$ineditperiod) {
00046             print_error('erredittimeexpired', 'glossary', "view.php?id=$cm->id&amp;mode=entry&amp;hook=$id");
00047         }
00048     }
00049 
00050     //prepare extra data
00051     if ($aliases = $DB->get_records_menu("glossary_alias", array("entryid"=>$id), '', 'id, alias')) {
00052         $entry->aliases = implode("\n", $aliases) . "\n";
00053     }
00054     if ($categoriesarr = $DB->get_records_menu("glossary_entries_categories", array('entryid'=>$id), '', 'id, categoryid')) {
00055         // TODO: this fetches cats from both main and secondary glossary :-(
00056         $entry->categories = array_values($categoriesarr);
00057     }
00058 
00059 } else { // new entry
00060     require_capability('mod/glossary:write', $context);
00061     // note: guest user does not have any write capability
00062     $entry = new stdClass();
00063     $entry->id = null;
00064 }
00065 
00066 $maxfiles = 99;                // TODO: add some setting
00067 $maxbytes = $course->maxbytes; // TODO: add some setting
00068 
00069 $definitionoptions = array('trusttext'=>true, 'subdirs'=>false, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes, 'context'=>$context);
00070 $attachmentoptions = array('subdirs'=>false, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes);
00071 
00072 $entry = file_prepare_standard_editor($entry, 'definition', $definitionoptions, $context, 'mod_glossary', 'entry', $entry->id);
00073 $entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $context, 'mod_glossary', 'attachment', $entry->id);
00074 
00075 $entry->cmid = $cm->id;
00076 
00077 // create form and set initial data
00078 $mform = new mod_glossary_entry_form(null, array('current'=>$entry, 'cm'=>$cm, 'glossary'=>$glossary,
00079                                                  'definitionoptions'=>$definitionoptions, 'attachmentoptions'=>$attachmentoptions));
00080 
00081 if ($mform->is_cancelled()){
00082     if ($id){
00083         redirect("view.php?id=$cm->id&mode=entry&hook=$id");
00084     } else {
00085         redirect("view.php?id=$cm->id");
00086     }
00087 
00088 } else if ($entry = $mform->get_data()) {
00089     $timenow = time();
00090 
00091     $categories = empty($entry->categories) ? array() : $entry->categories;
00092     unset($entry->categories);
00093     $aliases = trim($entry->aliases);
00094     unset($entry->aliases);
00095 
00096     if (empty($entry->id)) {
00097         $entry->glossaryid       = $glossary->id;
00098         $entry->timecreated      = $timenow;
00099         $entry->userid           = $USER->id;
00100         $entry->timecreated      = $timenow;
00101         $entry->sourceglossaryid = 0;
00102         $entry->teacherentry     = has_capability('mod/glossary:manageentries', $context);
00103     }
00104 
00105     $entry->concept          = trim($entry->concept);
00106     $entry->definition       = '';          // updated later
00107     $entry->definitionformat = FORMAT_HTML; // updated later
00108     $entry->definitiontrust  = 0;           // updated later
00109     $entry->timemodified     = $timenow;
00110     $entry->approved         = 0;
00111     $entry->usedynalink      = isset($entry->usedynalink) ?   $entry->usedynalink : 0;
00112     $entry->casesensitive    = isset($entry->casesensitive) ? $entry->casesensitive : 0;
00113     $entry->fullmatch        = isset($entry->fullmatch) ?     $entry->fullmatch : 0;
00114 
00115     if ($glossary->defaultapproval or has_capability('mod/glossary:approve', $context)) {
00116         $entry->approved = 1;
00117     }
00118 
00119     if (empty($entry->id)) {
00120         //new entry
00121         $entry->id = $DB->insert_record('glossary_entries', $entry);
00122 
00123         // Update completion state
00124         $completion = new completion_info($course);
00125         if ($completion->is_enabled($cm) == COMPLETION_TRACKING_AUTOMATIC && $glossary->completionentries && $entry->approved) {
00126             $completion->update_state($cm, COMPLETION_COMPLETE);
00127         }
00128 
00129         add_to_log($course->id, "glossary", "add entry",
00130                    "view.php?id=$cm->id&amp;mode=entry&amp;hook=$entry->id", $entry->id, $cm->id);
00131 
00132     } else {
00133         //existing entry
00134         $DB->update_record('glossary_entries', $entry);
00135         add_to_log($course->id, "glossary", "update entry",
00136                    "view.php?id=$cm->id&amp;mode=entry&amp;hook=$entry->id",
00137                    $entry->id, $cm->id);
00138     }
00139 
00140     // save and relink embedded images and save attachments
00141     $entry = file_postupdate_standard_editor($entry, 'definition', $definitionoptions, $context, 'mod_glossary', 'entry', $entry->id);
00142     $entry = file_postupdate_standard_filemanager($entry, 'attachment', $attachmentoptions, $context, 'mod_glossary', 'attachment', $entry->id);
00143 
00144     // store the updated value values
00145     $DB->update_record('glossary_entries', $entry);
00146 
00147     //refetch complete entry
00148     $entry = $DB->get_record('glossary_entries', array('id'=>$entry->id));
00149 
00150     // update entry categories
00151     $DB->delete_records('glossary_entries_categories', array('entryid'=>$entry->id));
00152     // TODO: this deletes cats from both both main and secondary glossary :-(
00153     if (!empty($categories) and array_search(0, $categories) === false) {
00154         foreach ($categories as $catid) {
00155             $newcategory = new stdClass();
00156             $newcategory->entryid    = $entry->id;
00157             $newcategory->categoryid = $catid;
00158             $DB->insert_record('glossary_entries_categories', $newcategory, false);
00159         }
00160     }
00161 
00162     // update aliases
00163     $DB->delete_records('glossary_alias', array('entryid'=>$entry->id));
00164     if ($aliases !== '') {
00165         $aliases = explode("\n", $aliases);
00166         foreach ($aliases as $alias) {
00167             $alias = trim($alias);
00168             if ($alias !== '') {
00169                 $newalias = new stdClass();
00170                 $newalias->entryid = $entry->id;
00171                 $newalias->alias   = $alias;
00172                 $DB->insert_record('glossary_alias', $newalias, false);
00173             }
00174         }
00175     }
00176 
00177     redirect("view.php?id=$cm->id&mode=entry&hook=$entry->id");
00178 }
00179 
00180 if (!empty($id)) {
00181     $PAGE->navbar->add(get_string('edit'));
00182 }
00183 
00184 $PAGE->set_title(format_string($glossary->name));
00185 $PAGE->set_heading($course->fullname);
00186 echo $OUTPUT->header();
00187 echo $OUTPUT->heading(format_string($glossary->name));
00188 
00189 $mform->display();
00190 
00191 echo $OUTPUT->footer();
00192 
 All Data Structures Namespaces Files Functions Variables Enumerations