diff --git a/lib/tests/behat/behat_forms.php b/lib/tests/behat/behat_forms.php index f7a5a529e65..fcde5986263 100644 --- a/lib/tests/behat/behat_forms.php +++ b/lib/tests/behat/behat_forms.php @@ -32,6 +32,7 @@ use Behat\Behat\Context\Step\Given as Given, Behat\Behat\Context\Step\When as When, Behat\Behat\Context\Step\Then as Then, Behat\Gherkin\Node\TableNode as TableNode, + Behat\Gherkin\Node\PyStringNode as PyStringNode, Behat\Mink\Element\NodeElement as NodeElement, Behat\Mink\Exception\ExpectationException as ExpectationException, Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException; @@ -159,6 +160,19 @@ class behat_forms extends behat_base { $this->set_field_value($field, $value); } + /** + * Sets the specified value to the field. + * + * @Given /^I set the field "(?P(?:[^"]|\\")*)" to multiline$/ + * @throws ElementNotFoundException Thrown by behat_base::find + * @param string $field + * @param PyStringNode $value + * @return void + */ + public function i_set_the_field_to_multiline($field, PyStringNode $value) { + $this->set_field_value($field, (string)$value); + } + /** * Sets the specified value to the field with xpath. * diff --git a/mod/data/backup/moodle2/backup_data_stepslib.php b/mod/data/backup/moodle2/backup_data_stepslib.php index 5fe9ee6f87a..385e66bb070 100644 --- a/mod/data/backup/moodle2/backup_data_stepslib.php +++ b/mod/data/backup/moodle2/backup_data_stepslib.php @@ -50,7 +50,7 @@ class backup_data_activity_structure_step extends backup_activity_structure_step $fields = new backup_nested_element('fields'); $field = new backup_nested_element('field', array('id'), array( - 'type', 'name', 'description', 'param1', 'param2', + 'type', 'name', 'description', 'required', 'param1', 'param2', 'param3', 'param4', 'param5', 'param6', 'param7', 'param8', 'param9', 'param10')); diff --git a/mod/data/db/install.xml b/mod/data/db/install.xml index 37b46f1d798..a37bbb68bfe 100644 --- a/mod/data/db/install.xml +++ b/mod/data/db/install.xml @@ -1,5 +1,5 @@ - @@ -54,6 +54,7 @@ + @@ -93,11 +94,11 @@ - - - - - + + + + + diff --git a/mod/data/db/upgrade.php b/mod/data/db/upgrade.php index 61c57d57640..02aa8f8810a 100644 --- a/mod/data/db/upgrade.php +++ b/mod/data/db/upgrade.php @@ -137,7 +137,18 @@ function xmldb_data_upgrade($oldversion) { // Moodle v2.8.0 release upgrade line. // Put any upgrade step following this. + if ($oldversion < 2015022600) { + // Define field required to be added to data_fields. + $table = new xmldb_table('data_fields'); + $field = new xmldb_field('required', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'description'); + + // Conditionally launch add field required. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + upgrade_mod_savepoint(true, 2015022600, 'data'); + } + return true; } - - diff --git a/mod/data/edit.php b/mod/data/edit.php index 6dd59f75e69..7350ccad302 100644 --- a/mod/data/edit.php +++ b/mod/data/edit.php @@ -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, $datarecord); } + + // 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 '
'; diff --git a/mod/data/field.php b/mod/data/field.php index e567fff4285..77e3a29eafa 100644 --- a/mod/data/field.php +++ b/mod/data/field.php @@ -146,6 +146,7 @@ switch ($mode) { $field->field->name = $fieldinput->name; $field->field->description = $fieldinput->description; + $field->field->required = !empty($fieldinput->required) ? 1 : 0; for ($i=1; $i<=10; $i++) { if (isset($fieldinput->{'param'.$i})) { @@ -264,7 +265,13 @@ if (($mode == 'new') && (!empty($newtype)) && confirm_sesskey()) { /// } else { //else print quiz style list of fields $table = new html_table(); - $table->head = array(get_string('fieldname','data'), get_string('type','data'), get_string('fielddescription', 'data'), get_string('action','data')); + $table->head = array( + get_string('fieldname', 'data'), + get_string('type', 'data'), + get_string('required', 'data'), + get_string('fielddescription', 'data'), + get_string('action', 'data'), + ); $table->align = array('left','left','left', 'center'); $table->wrap = array(false,false,false,false); @@ -273,21 +280,28 @@ if (($mode == 'new') && (!empty($newtype)) && confirm_sesskey()) { /// $field = data_get_field($ff, $data); + $baseurl = new moodle_url('/mod/data/field.php', array( + 'd' => $data->id, + 'fid' => $field->field->id, + 'sesskey' => sesskey(), + )); + + $displayurl = new moodle_url($baseurl, array( + 'mode' => 'display', + )); + + $deleteurl = new moodle_url($baseurl, array( + 'mode' => 'delete', + )); + $table->data[] = array( - - ''.$field->field->name.'', - - $field->image().' '.get_string($field->type, 'data'), - - shorten_text($field->field->description, 30), - - ''. - ''.get_string('edit').''. - ' '. - ''. - ''.get_string('delete').'' - + html_writer::link($displayurl, $field->field->name), + $field->image() . ' ' . get_string($field->type, 'data'), + $field->field->required ? get_string('yes') : get_string('no'), + shorten_text($field->field->description, 30), + html_writer::link($displayurl, $OUTPUT->pix_icon('t/edit', get_string('edit'))) . + ' ' . + html_writer::link($deleteurl, $OUTPUT->pix_icon('t/delete', get_string('delete'))), ); } } diff --git a/mod/data/field/checkbox/field.class.php b/mod/data/field/checkbox/field.class.php index 7571d077a5a..8b7c0a65330 100644 --- a/mod/data/field/checkbox/field.class.php +++ b/mod/data/field/checkbox/field.class.php @@ -26,20 +26,33 @@ class data_field_checkbox extends data_field_base { var $type = 'checkbox'; - function display_add_field($recordid=0) { - global $CFG, $DB; + function display_add_field($recordid = 0, $formdata = null) { + global $CFG, $DB, $OUTPUT; $content = array(); - if ($recordid) { + if ($formdata) { + $fieldname = 'field_' . $this->field->id; + $content = $formdata->$fieldname; + } else if ($recordid) { $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid)); $content = explode('##', $content); } else { $content = array(); } - $str = '
'; - $str .= '
'.$this->field->name.''; + $str = '
'; + $str .= '
'.$this->field->name; + if ($this->field->required) { + $str .= '$nbsp;' . get_string('requiredelement', 'form'); + $str .= ''; + $str .= '
'; + $str .= html_writer::img($OUTPUT->pix_url('req'), get_string('requiredelement', 'form'), + array('class' => 'req', 'title' => get_string('requiredelement', 'form'))); + $str .= '
'; + } else { + $str .= ''; + } $i = 0; foreach (explode("\n", $this->field->param1) as $checkbox) { @@ -211,5 +224,22 @@ class data_field_checkbox extends data_field_base { return implode('##', $vals); } -} + /** + * Check whether any boxes in the checkbox where checked. + * + * @param mixed $value The submitted values + * @param mixed $name + * @return bool + */ + function notemptyfield($value, $name) { + $found = false; + foreach ($value as $checkboxitem) { + if (!empty($checkboxitem)) { + $found = true; + break; + } + } + return $found; + } +} diff --git a/mod/data/field/checkbox/mod.html b/mod/data/field/checkbox/mod.html index a8812926b95..c6dfcaa503d 100644 --- a/mod/data/field/checkbox/mod.html +++ b/mod/data/field/checkbox/mod.html @@ -7,6 +7,10 @@ + + + field->required ? "checked=\"checked\"" : ""); ?>/> + diff --git a/mod/data/field/date/field.class.php b/mod/data/field/date/field.class.php index 2fa245d2984..fc5f4a94e45 100644 --- a/mod/data/field/date/field.class.php +++ b/mod/data/field/date/field.class.php @@ -34,10 +34,18 @@ class data_field_date extends data_field_base { var $month = 0; var $year = 0; - function display_add_field($recordid=0) { + function display_add_field($recordid = 0, $formdata = null) { global $DB, $OUTPUT; - if ($recordid) { + if ($formdata) { + $fieldname = 'field_' . $this->field->id . '_day'; + $day = $formdata->$fieldname; + $fieldname = 'field_' . $this->field->id . '_month'; + $month = $formdata->$fieldname; + $fieldname = 'field_' . $this->field->id . '_year'; + $year = $formdata->$fieldname; + $content = make_timestamp($year, $month, $day, 12, 0, 0, 0, false); + } else if ($recordid) { $content = (int)$DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid)); } else { $content = time(); @@ -128,5 +136,3 @@ class data_field_date extends data_field_base { } - - diff --git a/mod/data/field/file/field.class.php b/mod/data/field/file/field.class.php index 5fd2d215d2d..b1cee2445ba 100644 --- a/mod/data/field/file/field.class.php +++ b/mod/data/field/file/field.class.php @@ -25,7 +25,7 @@ class data_field_file extends data_field_base { var $type = 'file'; - function display_add_field($recordid=0) { + function display_add_field($recordid = 0, $formdata = null) { global $CFG, $DB, $OUTPUT, $PAGE, $USER; $file = false; @@ -36,7 +36,10 @@ class data_field_file extends data_field_base { $itemid = null; // editing an existing database entry - if ($recordid){ + if ($formdata) { + $fieldname = 'field_' . $this->field->id . '_file'; + $itemid = $formdata->$fieldname; + } else if ($recordid) { if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id); @@ -62,10 +65,18 @@ class data_field_file extends data_field_base { $itemid = file_get_unused_draft_itemid(); } - $html = ''; // database entry label - $html .= '
'; - $html .= '
'.$this->field->name.''; + $html = '
'; + $html .= '
'.$this->field->name; + + if ($this->field->required) { + $html .= ' ' . get_string('requiredelement', 'form') . ''; + $image = html_writer::img($OUTPUT->pix_url('req'), get_string('requiredelement', 'form'), + array('class' => 'req', 'title' => get_string('requiredelement', 'form'))); + $html .= html_writer::div($image); + } else { + $html .= ''; + } // itemid element $html .= ''; @@ -83,7 +94,6 @@ class data_field_file extends data_field_base { $output = $PAGE->get_renderer('core', 'files'); $html .= $output->render($fm); - $html .= '
'; $html .= '
'; @@ -204,6 +214,24 @@ class data_field_file extends data_field_base { return true; } + /** + * Custom notempty function + * + * @param string $value + * @param string $name + * @return bool + */ + function notemptyfield($value, $name) { + global $USER; + + $names = explode('_', $name); + if ($names[2] == 'file') { + $usercontext = context_user::instance($USER->id); + $fs = get_file_storage(); + $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $value); + return count($files) >= 2; + } + return false; + } + } - - diff --git a/mod/data/field/file/mod.html b/mod/data/field/file/mod.html index 8f8945c5bca..2f67d9adc61 100644 --- a/mod/data/field/file/mod.html +++ b/mod/data/field/file/mod.html @@ -7,6 +7,10 @@ + + + field->required ? "checked=\"checked\"" : ""); ?>/> + diff --git a/mod/data/field/latlong/field.class.php b/mod/data/field/latlong/field.class.php index e6108eec2bb..d89df71bdef 100644 --- a/mod/data/field/latlong/field.class.php +++ b/mod/data/field/latlong/field.class.php @@ -43,12 +43,17 @@ class data_field_latlong extends data_field_base { ); // Other map sources listed at http://kvaleberg.com/extensions/mapsources/index.php?params=51_30.4167_N_0_7.65_W_region:earth - function display_add_field($recordid=0) { - global $CFG, $DB; + function display_add_field($recordid = 0, $formdata = null) { + global $CFG, $DB, $OUTPUT; $lat = ''; $long = ''; - if ($recordid) { + if ($formdata) { + $fieldname = 'field_' . $this->field->id . '_0'; + $lat = $formdata->$fieldname; + $fieldname = 'field_' . $this->field->id . '_1'; + $long = $formdata->$fieldname; + } else if ($recordid) { if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { $lat = $content->content; $long = $content->content1; @@ -57,8 +62,21 @@ class data_field_latlong extends data_field_base { $str = '
'; $str .= '
'.$this->field->name.''; $str .= ''; - $str .= ''; + $str .= ''; + $str .= ''; + $str .= ''; $str .= '
'; - $str .= '°N
°E
°N
°E
'; $str .= '
'; $str .= '
'; @@ -223,6 +241,15 @@ class data_field_latlong extends data_field_base { return sprintf('%01.4f', $record->content) . ' ' . sprintf('%01.4f', $record->content1); } + /** + * Check if a field from an add form is empty + * + * @param mixed $value + * @param mixed $name + * @return bool + */ + function notemptyfield($value, $name) { + return isset($value) && !($value == ''); + } + } - - diff --git a/mod/data/field/latlong/mod.html b/mod/data/field/latlong/mod.html index fb34fb4ed75..1b0466d9a8a 100644 --- a/mod/data/field/latlong/mod.html +++ b/mod/data/field/latlong/mod.html @@ -7,6 +7,10 @@ + + + field->required?"checked=\"checked\"":""); ?>/> + diff --git a/mod/data/field/menu/field.class.php b/mod/data/field/menu/field.class.php index 4fb611bae42..c53e3cb320d 100644 --- a/mod/data/field/menu/field.class.php +++ b/mod/data/field/menu/field.class.php @@ -26,17 +26,19 @@ class data_field_menu extends data_field_base { var $type = 'menu'; - function display_add_field($recordid=0) { + function display_add_field($recordid = 0, $formdata = null) { global $DB, $OUTPUT; - if ($recordid){ + if ($formdata) { + $fieldname = 'field_' . $this->field->id; + $content = $formdata->$fieldname; + } else if ($recordid) { $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid)); $content = trim($content); } else { $content = ''; } - - $str = '
'; + $str = '
'; $options = array(); $rawoptions = explode("\n",$this->field->param1); @@ -47,7 +49,14 @@ class data_field_menu extends data_field_base { } } - $str .= html_writer::label(get_string('menuchoose', 'data'), 'field_'.$this->field->id, false, array('class' => 'accesshide')); + $str .= ''; $str .= html_writer::select($options, 'field_'.$this->field->id, $content, array(''=>get_string('menuchoose', 'data')), array('id'=>'field_'.$this->field->id)); $str .= '
'; @@ -108,5 +117,3 @@ class data_field_menu extends data_field_base { } } - - diff --git a/mod/data/field/menu/mod.html b/mod/data/field/menu/mod.html index f92fb136b23..e7c84e37cd4 100644 --- a/mod/data/field/menu/mod.html +++ b/mod/data/field/menu/mod.html @@ -7,6 +7,10 @@ + + + field->required?"checked=\"checked\"":""); ?>/> + diff --git a/mod/data/field/multimenu/field.class.php b/mod/data/field/multimenu/field.class.php index 1f3de9ff00a..75b6bc094d4 100644 --- a/mod/data/field/multimenu/field.class.php +++ b/mod/data/field/multimenu/field.class.php @@ -26,10 +26,17 @@ class data_field_multimenu extends data_field_base { var $type = 'multimenu'; - function display_add_field($recordid=0) { - global $DB; + function display_add_field($recordid = 0, $formdata = null) { + global $DB, $OUTPUT; - if ($recordid){ + if ($formdata) { + $fieldname = 'field_' . $this->field->id; + if (isset($formdata->$fieldname)) { + $content = $formdata->$fieldname; + } else { + $content = array(); + } + } else if ($recordid) { $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid)); $content = explode('##', $content); } else { @@ -38,10 +45,19 @@ class data_field_multimenu extends data_field_base { $str = '
'; $str .= ''; // hidden field - needed for empty selection - $str .= ''; + + $str .= ''; $str .= ' + + + field->required?"checked=\"checked\"":""); ?>/> + diff --git a/mod/data/field/number/mod.html b/mod/data/field/number/mod.html index 831560f1b57..7b1791b0a68 100644 --- a/mod/data/field/number/mod.html +++ b/mod/data/field/number/mod.html @@ -7,4 +7,8 @@ + + + field->required?"checked=\"checked\"":""); ?>/> + diff --git a/mod/data/field/picture/field.class.php b/mod/data/field/picture/field.class.php index a459791f790..d4ade199637 100644 --- a/mod/data/field/picture/field.class.php +++ b/mod/data/field/picture/field.class.php @@ -27,7 +27,7 @@ class data_field_picture extends data_field_base { var $previewwidth = 50; var $previewheight = 50; - function display_add_field($recordid=0) { + function display_add_field($recordid = 0, $formdata = null) { global $CFG, $DB, $OUTPUT, $USER, $PAGE; $file = false; @@ -37,7 +37,14 @@ class data_field_picture extends data_field_base { $itemid = null; $fs = get_file_storage(); - if ($recordid) { + if ($formdata) { + $fieldname = 'field_' . $this->field->id . '_file'; + $itemid = $formdata->$fieldname; + $fieldname = 'field_' . $this->field->id . '_alttext'; + if (isset($formdata->$fieldname)) { + $alttext = $formdata->$fieldname; + } + } else if ($recordid) { if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id); if (!empty($content->content)) { @@ -64,9 +71,17 @@ class data_field_picture extends data_field_base { } else { $itemid = file_get_unused_draft_itemid(); } + $str = '
'; + $str .= '
'.$this->field->name; - $str = '
'; - $str .= '
'.$this->field->name.''; + if ($this->field->required) { + $str .= ' ' . get_string('requiredelement', 'form') . ''; + $image = html_writer::img($OUTPUT->pix_url('req'), get_string('requiredelement', 'form'), + array('class' => 'req', 'title' => get_string('requiredelement', 'form'))); + $str .= html_writer::div($image); + } else { + $str .= ''; + } $str .= '