Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/data/edit.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00026 require_once('../../config.php');
00027 require_once('lib.php');
00028 require_once("$CFG->libdir/rsslib.php");
00029 
00030 $id    = optional_param('id', 0, PARAM_INT);    // course module id
00031 $d     = optional_param('d', 0, PARAM_INT);    // database id
00032 $rid   = optional_param('rid', 0, PARAM_INT);    //record id
00033 $cancel   = optional_param('cancel', '', PARAM_RAW);    // cancel an add
00034 $mode ='addtemplate';    //define the mode for this page, only 1 mode available
00035 
00036 $url = new moodle_url('/mod/data/edit.php');
00037 if ($rid !== 0) {
00038     $url->param('rid', $rid);
00039 }
00040 if ($cancel !== '') {
00041     $url->param('cancel', $cancel);
00042 }
00043 
00044 if ($id) {
00045     $url->param('id', $id);
00046     $PAGE->set_url($url);
00047     if (! $cm = get_coursemodule_from_id('data', $id)) {
00048         print_error('invalidcoursemodule');
00049     }
00050     if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
00051         print_error('coursemisconf');
00052     }
00053     if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
00054         print_error('invalidcoursemodule');
00055     }
00056 
00057 } else {
00058     $url->param('d', $d);
00059     $PAGE->set_url($url);
00060     if (! $data = $DB->get_record('data', array('id'=>$d))) {
00061         print_error('invalidid', 'data');
00062     }
00063     if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
00064         print_error('coursemisconf');
00065     }
00066     if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
00067         print_error('invalidcoursemodule');
00068     }
00069 }
00070 
00071 require_login($course->id, false, $cm);
00072 
00073 if (isguestuser()) {
00074     redirect('view.php?d='.$data->id);
00075 }
00076 
00077 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00078 
00080 if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) {
00081     $strdatabases = get_string("modulenameplural", "data");
00082 
00083     $PAGE->set_title(format_string($data->name));
00084     $PAGE->set_heading(format_string($course->fullname));
00085     echo $OUTPUT->header();
00086     notice(get_string("activityiscurrentlyhidden"));
00087 }
00088 
00090 if (has_capability('mod/data:managetemplates', $context)) {
00091     if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) {      // Brand new database!
00092         redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id);  // Redirect to field entry
00093     }
00094 }
00095 
00096 if ($rid) {    // So do you have access?
00097     if (!(has_capability('mod/data:manageentries', $context) or data_isowner($rid)) or !confirm_sesskey() ) {
00098         print_error('noaccess','data');
00099     }
00100 }
00101 
00102 if ($cancel) {
00103     redirect('view.php?d='.$data->id);
00104 }
00105 
00106 
00108 if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
00109     $rsspath = rss_get_url($context->id, $USER->id, 'mod_data', $data->id);
00110     $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id)));
00111     $PAGE->add_alternate_version($courseshortname . ': %fullname%', $rsspath, 'application/rss+xml');
00112 }
00113 if ($data->csstemplate) {
00114     $PAGE->requires->css('/mod/data/css.php?d='.$data->id);
00115 }
00116 if ($data->jstemplate) {
00117     $PAGE->requires->js('/mod/data/js.php?d='.$data->id, true);
00118 }
00119 
00120 $possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
00121 
00122 foreach ($possiblefields as $field) {
00123     if ($field->type == 'file' || $field->type == 'picture') {
00124         require_once($CFG->dirroot.'/repository/lib.php');
00125         break;
00126     }
00127 }
00128 
00130 $strdata = get_string('modulenameplural','data');
00131 
00132 if ($rid) {
00133     $PAGE->navbar->add(get_string('editentry', 'data'));
00134 }
00135 
00136 $PAGE->set_title($data->name);
00137 $PAGE->set_heading($course->fullname);
00138 
00140 $currentgroup = groups_get_activity_group($cm);
00141 $groupmode = groups_get_activity_groupmode($cm);
00142 
00143 if ($currentgroup) {
00144     $groupselect = " AND groupid = '$currentgroup'";
00145     $groupparam = "&amp;groupid=$currentgroup";
00146 } else {
00147     $groupselect = "";
00148     $groupparam = "";
00149     $currentgroup = 0;
00150 }
00151 
00152 
00154 
00155 if ($datarecord = data_submitted() and confirm_sesskey()) {
00156 
00157     $ignorenames = array('MAX_FILE_SIZE','sesskey','d','rid','saveandview','cancel');  // strings to be ignored in input data
00158 
00159     if ($rid) {                                          
00160 
00162         $record = $DB->get_record('data_records', array('id'=>$rid));
00163 
00165         if (!has_capability('mod/data:approve', $context)) {
00166             $record->approved = 0;
00167         }
00168 
00169         $record->groupid = $currentgroup;
00170         $record->timemodified = time();
00171         $DB->update_record('data_records', $record);
00172 
00174         $field = NULL;
00175         foreach ($datarecord as $name => $value) {
00176             if (!in_array($name, $ignorenames)) {
00177                 $namearr = explode('_',$name);  // Second one is the field id
00178                 if (empty($field->field) || ($namearr[1] != $field->field->id)) {  // Try to reuse classes
00179                     $field = data_get_field_from_id($namearr[1], $data);
00180                 }
00181                 if ($field) {
00182                     $field->update_content($rid, $value, $name);
00183                 }
00184             }
00185         }
00186 
00187         add_to_log($course->id, 'data', 'update', "view.php?d=$data->id&amp;rid=$rid", $data->id, $cm->id);
00188 
00189         redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$rid);
00190 
00191     } else { 
00192 
00193         if (!data_user_can_add_entry($data, $currentgroup, $groupmode, $context)) {
00194             print_error('cannotadd', 'data');
00195         }
00196 
00199 
00200         if (data_atmaxentries($data) and !has_capability('mod/data:manageentries',$context)){
00201             echo $OUTPUT->header();
00202             echo $OUTPUT->notification(get_string('atmaxentry','data'));
00203             echo $OUTPUT->footer();
00204             exit;
00205         }
00206 
00208 
00209         $emptyform = true;      // assume the worst
00210 
00211         foreach ($datarecord as $name => $value) {
00212             if (!in_array($name, $ignorenames)) {
00213                 $namearr = explode('_', $name);  // Second one is the field id
00214                 if (empty($field->field) || ($namearr[1] != $field->field->id)) {  // Try to reuse classes
00215                     $field = data_get_field_from_id($namearr[1], $data);
00216                 }
00217                 if ($field->notemptyfield($value, $name)) {
00218                     $emptyform = false;
00219                     break;             // if anything has content, this form is not empty, so stop now!
00220                 }
00221             }
00222         }
00223 
00224         if ($emptyform){    //nothing gets written to database
00225             echo $OUTPUT->notification(get_string('emptyaddform','data'));
00226         }
00227 
00228         if (!$emptyform && $recordid = data_add_record($data, $currentgroup)) {    //add instance to data_record
00229 
00231             $fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
00232             foreach ($fields as $field) {
00233                 $content->recordid = $recordid;
00234                 $content->fieldid = $field->id;
00235                 $DB->insert_record('data_content',$content);
00236             }
00237 
00239             foreach ($datarecord as $name => $value){
00240                 if (!in_array($name, $ignorenames)) {
00241                     $namearr = explode('_', $name);  // Second one is the field id
00242                     if (empty($field->field) || ($namearr[1] != $field->field->id)) {  // Try to reuse classes
00243                         $field = data_get_field_from_id($namearr[1], $data);
00244                     }
00245                     if ($field) {
00246                         $field->update_content($recordid, $value, $name);
00247                     }
00248                 }
00249             }
00250 
00251             add_to_log($course->id, 'data', 'add', "view.php?d=$data->id&amp;rid=$recordid", $data->id, $cm->id);
00252 
00253             if (!empty($datarecord->saveandview)) {
00254                 redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$recordid);
00255             }
00256         }
00257     }
00258 }  // End of form processing
00259 
00260 
00262 
00263 echo $OUTPUT->header();
00264 groups_print_activity_menu($cm, $CFG->wwwroot.'/mod/data/edit.php?d='.$data->id);
00265 echo $OUTPUT->heading(format_string($data->name));
00266 
00268 
00269 $currenttab = 'add';
00270 if ($rid) {
00271     $editentry = true;  //used in tabs
00272 }
00273 include('tabs.php');
00274 
00275 
00277 
00278 $patterns = array();    //tags to replace
00279 $replacement = array();    //html to replace those yucky tags
00280 
00281 //form goes here first in case add template is empty
00282 echo '<form enctype="multipart/form-data" action="edit.php" method="post">';
00283 echo '<div>';
00284 echo '<input name="d" value="'.$data->id.'" type="hidden" />';
00285 echo '<input name="rid" value="'.$rid.'" type="hidden" />';
00286 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
00287 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
00288 
00289 if (!$rid){
00290     echo $OUTPUT->heading(get_string('newentry','data'), 2);
00291 }
00292 
00293 /******************************************
00294  * Regular expression replacement section *
00295  ******************************************/
00296 if ($data->addtemplate){
00297     $possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
00298 
00300     foreach ($possiblefields as $eachfield){
00301         $field = data_get_field($eachfield, $data);
00302         $patterns[]="[[".$field->field->name."]]";
00303         $replacements[] = $field->display_add_field($rid);
00304         $patterns[]="[[".$field->field->name."#id]]";
00305         $replacements[] = 'field_'.$field->field->id;
00306     }
00307     $newtext = str_ireplace($patterns, $replacements, $data->{$mode});
00308 
00309 } else {    //if the add template is not yet defined, print the default form!
00310     echo data_generate_default_template($data, 'addtemplate', $rid, true, false);
00311     $newtext = '';
00312 }
00313 
00314 echo $newtext;
00315 
00316 echo '<div class="mdl-align"><input type="submit" name="saveandview" value="'.get_string('saveandview','data').'" />';
00317 if ($rid) {
00318     echo '&nbsp;<input type="submit" name="cancel" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)" />';
00319 } else {
00320     if ((!$data->maxentries) || has_capability('mod/data:manageentries', $context) || (data_numentries($data) < ($data->maxentries - 1))) {
00321         echo '&nbsp;<input type="submit" value="'.get_string('saveandadd','data').'" />';
00322     }
00323 }
00324 echo '</div>';
00325 echo $OUTPUT->box_end();
00326 echo '</div></form>';
00327 
00328 
00330 
00331 // Print the stuff that need to come after the form fields.
00332 if (!$fields = $DB->get_records('data_fields', array('dataid'=>$data->id))) {
00333     print_error('nofieldindatabase', 'data');
00334 }
00335 foreach ($fields as $eachfield) {
00336     $field = data_get_field($eachfield, $data);
00337     $field->print_after_form();
00338 }
00339 
00340 echo $OUTPUT->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations