MDL-5583 mod_data: Add required fields

This is implemented for most fields but not all. The reason is that there are
some fields for which this does not make sense because no entry is a valid entry.

The supported fields are:
    checkbox
    file
    latlong
    menu
    number
    picture
    radio
    text
    textarea
    url

The unsupported fields are:
    date
    multimenu
This commit is contained in:
Damyon Wiese
2015-03-06 09:55:49 +08:00
committed by John Okely
parent 9776f3dcdb
commit b89cca197e
30 changed files with 665 additions and 153 deletions
+106 -72
View File
@@ -34,8 +34,14 @@ $rid = optional_param('rid', 0, PARAM_INT); //record id
$cancel = optional_param('cancel', '', PARAM_RAW); // cancel an add
$mode ='addtemplate'; //define the mode for this page, only 1 mode available
$url = new moodle_url('/mod/data/edit.php');
if ($rid !== 0) {
$record = $DB->get_record('data_records', array(
'id' => $rid,
'dataid' => $d,
), '*', MUST_EXIST);
$url->param('rid', $rid);
}
if ($cancel !== '') {
@@ -152,105 +158,121 @@ if ($rid) {
$PAGE->set_title($data->name);
$PAGE->set_heading($course->fullname);
/// Process incoming data for adding/updating records
// Process incoming data for adding/updating records.
// Keep track of any notifications.
$generalnotifications = array();
$fieldnotifications = array();
// Process the submitted form.
if ($datarecord = data_submitted() and confirm_sesskey()) {
if ($rid) {
// Updating an existing record.
$ignorenames = array('MAX_FILE_SIZE','sesskey','d','rid','saveandview','cancel'); // strings to be ignored in input data
// Retrieve the format for the fields.
$fields = $DB->get_records('data_fields', array('dataid' => $datarecord->d));
if ($rid) { /// Update some records
// Validate the form to ensure that enough data was submitted.
$processeddata = data_process_submission($data, $fields, $datarecord);
/// All student edits are marked unapproved by default
$record = $DB->get_record('data_records', array('id'=>$rid));
// Add the new notification data.
$generalnotifications = array_merge($generalnotifications, $processeddata->generalnotifications);
$fieldnotifications = array_merge($fieldnotifications, $processeddata->fieldnotifications);
/// reset approved flag after student edit
if (!has_capability('mod/data:approve', $context)) {
$record->approved = 0;
}
if ($processeddata->validated) {
// Enough data to update the record.
$record->timemodified = time();
$DB->update_record('data_records', $record);
// Obtain the record to be updated.
/// 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);
}
// Reset the approved flag after edit if the user does not have permission to approve their own entries.
if (!has_capability('mod/data:approve', $context)) {
$record->approved = 0;
}
}
// Trigger an event for updating this record.
$event = \mod_data\event\record_updated::create(array(
'objectid' => $rid,
'context' => $context,
'courseid' => $course->id,
'other' => array(
'dataid' => $data->id
)
));
$event->add_record_snapshot('data', $data);
$event->trigger();
// Update the parent record.
$record->timemodified = time();
$DB->update_record('data_records', $record);
redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$rid);
} else { /// Add some new records
///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!
}
// Update all content.
foreach ($processeddata->fields as $fieldname => $field) {
$field->update_content($rid, $datarecord->$fieldname, $fieldname);
}
// Trigger an event for updating this record.
$event = \mod_data\event\record_updated::create(array(
'objectid' => $rid,
'context' => $context,
'courseid' => $course->id,
'other' => array(
'dataid' => $data->id
)
));
$event->add_record_snapshot('data', $data);
$event->trigger();
$viewurl = new moodle_url('/mod/data/view.php', array(
'd' => $data->id,
'rid' => $rid,
));
redirect($viewurl);
}
if ($emptyform){ //nothing gets written to database
echo $OUTPUT->notification(get_string('emptyaddform','data'));
}
} else {
// No recordid was specified - creating a new entry.
if (!$emptyform && $recordid = data_add_record($data, $currentgroup)) { //add instance to data_record
// Retrieve the format for the fields.
$fields = $DB->get_records('data_fields', array('dataid' => $datarecord->d));
/// Insert a whole lot of empty records to make sure we have them
$fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
// Validate the form to ensure that enough data was submitted.
$processeddata = data_process_submission($data, $fields, $datarecord);
// Add the new notification data.
$generalnotifications = array_merge($generalnotifications, $processeddata->generalnotifications);
$fieldnotifications = array_merge($fieldnotifications, $processeddata->fieldnotifications);
// Add instance to data_record.
if ($processeddata->validated && $recordid = data_add_record($data, $currentgroup)) {
// Insert a whole lot of empty records to make sure we have them.
$records = array();
foreach ($fields as $field) {
$content = new stdClass();
$content->recordid = $recordid;
$content->fieldid = $field->id;
$DB->insert_record('data_content',$content);
$records[] = $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);
}
}
// Bulk insert the records now. Some records may have no data but all must exist.
$DB->insert_records('data_content', $records);
// Add all provided content.
foreach ($processeddata->fields as $fieldname => $field) {
$field->update_content($recordid, $datarecord->$fieldname, $fieldname);
}
// Trigger an event for updating this record.
$event = \mod_data\event\record_created::create(array(
'objectid' => $rid,
'context' => $context,
'courseid' => $course->id,
'other' => array(
'dataid' => $data->id
)
));
$event->add_record_snapshot('data', $data);
$event->trigger();
if (!empty($datarecord->saveandview)) {
redirect($CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$recordid);
$viewurl = new moodle_url('/mod/data/view.php', array(
'd' => $data->id,
'rid' => $recordid,
));
redirect($viewurl);
}
}
}
} // End of form processing
}
// End of form processing.
/// Print the page header
@@ -300,9 +322,18 @@ if ($data->addtemplate){
// To skip unnecessary calls to display_add_field().
if (strpos($data->addtemplate, "[[".$field->field->name."]]") !== false) {
// Replace the field tag.
$patterns[] = "[[".$field->field->name."]]";
$replacements[] = $field->display_add_field($rid);
$errors = '';
if (!empty($fieldnotifications[$field->field->name])) {
foreach ($fieldnotifications[$field->field->name] as $notification) {
$errors .= $OUTPUT->notification($notification);
}
}
$replacements[] = $errors . $field->display_add_field($rid);
}
// Replace the field id tag.
$patterns[] = "[[".$field->field->name."#id]]";
$replacements[] = 'field_'.$field->field->id;
}
@@ -313,6 +344,9 @@ if ($data->addtemplate){
$newtext = '';
}
foreach ($generalnotifications as $notification) {
echo $OUTPUT->notification($notification);
}
echo $newtext;
echo '<div class="mdl-align"><input type="submit" name="saveandview" value="'.get_string('saveandview','data').'" />';