libdir/rsslib.php"); $id = optional_param('id', 0, PARAM_INT); // course module id $d = optional_param('d', 0, PARAM_INT); // database id $rid = optional_param('rid', 0, PARAM_INT); //record id $import = optional_param('import', 0, PARAM_INT); // show import form $cancel = optional_param('cancel', ''); // cancel an add $mode ='addtemplate'; //define the mode for this page, only 1 mode available if ($id) { if (! $cm = get_coursemodule_from_id('data', $id)) { error('Course Module ID was incorrect'); } if (! $course = get_record('course', 'id', $cm->course)) { error('Course is misconfigured'); } if (! $data = get_record('data', 'id', $cm->instance)) { error('Course module is incorrect'); } } else { if (! $data = get_record('data', 'id', $d)) { error('Data ID is incorrect'); } if (! $course = get_record('course', 'id', $data->course)) { error('Course is misconfigured'); } if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) { error('Course Module ID was incorrect'); } } require_login($course->id, false, $cm); if (!isloggedin() or isguest()) { redirect('view.php?d='.$data->id); } $context = get_context_instance(CONTEXT_MODULE, $cm->id); /// If it's hidden then it doesn't show anything. :) if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) { $strdatabases = get_string("modulenameplural", "data"); $navigation = build_navigation('', $cm); print_header_simple(format_string($data->name), "", $navigation, "", "", true, '', navmenu($course, $cm)); notice(get_string("activityiscurrentlyhidden")); } /// Can't use this if there are no fields if (has_capability('mod/data:managetemplates', $context)) { if (!record_exists('data_fields','dataid',$data->id)) { // Brand new database! redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry } } if ($rid) { // So do you have access? if (!(has_capability('mod/data:manageentries', $context) or data_isowner($rid)) or !confirm_sesskey() ) { print_error('noaccess','data'); } } if ($cancel) { redirect('view.php?d='.$data->id); } /// RSS and CSS and JS meta $meta = ''; if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) { $rsspath = rss_get_url($course->id, $USER->id, 'data', $data->id); $meta .= 'shortname) .': %fullname%" href="'.$rsspath.'" />'; } if ($data->csstemplate) { $meta .= ' '; } if ($data->jstemplate) { $meta .= ''; } /// Print the page header $strdata = get_string('modulenameplural','data'); $navigation = build_navigation('', $cm); print_header_simple($data->name, '', $navigation, '', $meta, true, update_module_button($cm->id, $course->id, get_string('modulename', 'data')), navmenu($course, $cm), '', ''); /// Check to see if groups are being used here groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/data/edit.php?d='.$data->id); $currentgroup = groups_get_activity_group($cm); $groupmode = groups_get_activity_groupmode($cm); print_heading(format_string($data->name)); if ($currentgroup) { $groupselect = " AND groupid = '$currentgroup'"; $groupparam = "&groupid=$currentgroup"; } else { $groupselect = ""; $groupparam = ""; $currentgroup = 0; } /// Print the tabs $currenttab = 'add'; if ($rid) { $editentry = true; //used in tabs } include('tabs.php'); /// Process incoming data for adding/updating records if ($datarecord = data_submitted($CFG->wwwroot.'/mod/data/edit.php') and confirm_sesskey()) { $ignorenames = array('MAX_FILE_SIZE','sesskey','d','rid','saveandview','cancel'); // strings to be ignored in input data if ($rid) { /// Update some records /// All student edits are marked unapproved by default $record = get_record('data_records','id',$rid); /// reset approved flag after student edit if (!has_capability('mod/data:approve', $context)) { $record->approved = 0; } $record->groupid = $currentgroup; $record->timemodified = time(); update_record('data_records',$record); /// Update all content $field = NULL; foreach ($datarecord as $name => $value) { if (!in_array($name, $ignorenames)) { $namearr = explode('_',$name); // Second one is the field id if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes $field = data_get_field_from_id($namearr[1], $data); } if ($field) { $field->update_content($rid, $value, $name); } } } add_to_log($course->id, 'data', 'update', "view.php?d=$data->id&rid=$rid", $data->id, $cm->id); redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$rid); } else { /// Add some new records if (!data_user_can_add_entry($data, $currentgroup, $groupmode)) { error('Can not add entries!'); } /// Check if maximum number of entry as specified by this database is reached /// Of course, you can't be stopped if you are an editting teacher! =) if (data_atmaxentries($data) and !has_capability('mod/data:manageentries',$context)){ notify (get_string('atmaxentry','data')); print_footer($course); exit; } ///Empty form checking - you can't submit an empty form! $emptyform = true; // assume the worst foreach ($datarecord as $name => $value) { if (!in_array($name, $ignorenames)) { $namearr = explode('_', $name); // Second one is the field id if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes $field = data_get_field_from_id($namearr[1], $data); } if ($field->notemptyfield($value, $name)) { $emptyform = false; break; // if anything has content, this form is not empty, so stop now! } } } if ($emptyform){ //nothing gets written to database notify(get_string('emptyaddform','data')); } if (!$emptyform && $recordid = data_add_record($data, $currentgroup)) { //add instance to data_record /// Insert a whole lot of empty records to make sure we have them $fields = get_records('data_fields','dataid',$data->id); foreach ($fields as $field) { $content->recordid = $recordid; $content->fieldid = $field->id; insert_record('data_content',$content); } //for each field in the add form, add it to the data_content. foreach ($datarecord as $name => $value){ if (!in_array($name, $ignorenames)) { $namearr = explode('_', $name); // Second one is the field id if (empty($field->field) || ($namearr[1] != $field->field->id)) { // Try to reuse classes $field = data_get_field_from_id($namearr[1], $data); } if ($field) { $field->update_content($recordid, $value, $name); } } } add_to_log($course->id, 'data', 'add', "view.php?d=$data->id&rid=$recordid", $data->id, $cm->id); notify(get_string('entrysaved','data')); if (!empty($datarecord->saveandview)) { redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$recordid); } } } } // End of form processing /// Print the browsing interface $patterns = array(); //tags to replace $replacement = array(); //html to replace those yucky tags //form goes here first in case add template is empty echo '
'; echo '
'; echo ''; echo ''; echo ''; print_simple_box_start('center','80%'); if (!$rid){ print_heading(get_string('newentry','data'), '', 2); } /****************************************** * Regular expression replacement section * ******************************************/ if ($data->addtemplate){ $possiblefields = get_records('data_fields','dataid',$data->id,'id'); ///then we generate strings to replace foreach ($possiblefields as $eachfield){ $field = data_get_field($eachfield, $data); $patterns[]="[[".$field->field->name."]]"; $replacements[] = $field->display_add_field($rid); $patterns[]="[[".$field->field->name."#id]]"; $replacements[] = 'field_'.$field->field->id; } $newtext = str_ireplace($patterns, $replacements, $data->{$mode}); } else { //if the add template is not yet defined, print the default form! echo data_generate_default_template($data, 'addtemplate', $rid, true, false); $newtext = ''; } echo $newtext; echo '
'; if ($rid) { echo ' '; } else { echo ''; } echo '
'; print_simple_box_end(); echo '
'; /// Upload records section. Only for teachers and the admin. if (has_capability('mod/data:manageentries',$context)) { if ($import) { print_simple_box_start('center','80%'); print_heading(get_string('uploadrecords', 'data'), '', 3); $maxuploadsize = get_max_upload_file_size(); echo '
'; echo '
'; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
'.get_string('csvfile', 'data').':'; helpbutton('importcsv', get_string('csvimport', 'data'), 'data', true, false); echo '
'.get_string('fielddelimiter', 'data').':'; echo get_string('defaultfielddelimiter', 'data').'
'.get_string('fieldenclosure', 'data').':'; echo get_string('defaultfieldenclosure', 'data').'
'; echo ''; echo '
'; echo '
'; print_simple_box_end(); } else { echo '
'; echo ''.get_string('uploadrecords', 'data').''; echo '
'; } } /// Finish the page // Print the stuff that need to come after the form fields. if (!$fields = get_records('data_fields', 'dataid', $data->id)) { print_error('nofieldindatabase', 'data'); } foreach ($fields as $eachfield) { $field = data_get_field($eachfield, $data); $field->print_after_form(); } print_footer($course); ?>