MDL-75234 mod_data: load default templates when empty
The mod_data is forcing teachers to understand how to write templates even if they want to use basic forms. With this patch the default templates will be auto updated unless the user manually define the templates.
This commit is contained in:
+92
-187
@@ -23,103 +23,85 @@
|
||||
* @package mod_data
|
||||
*/
|
||||
|
||||
use mod_data\manager;
|
||||
|
||||
require_once('../../config.php');
|
||||
require_once('locallib.php');
|
||||
require_once("$CFG->libdir/rsslib.php");
|
||||
require_once("$CFG->libdir/form/filemanager.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
|
||||
$mode ='addtemplate'; //define the mode for this page, only 1 mode available
|
||||
$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.
|
||||
$mode = 'addtemplate'; // Define the mode for this page, only 1 mode available.
|
||||
$tags = optional_param_array('tags', [], PARAM_TAGLIST);
|
||||
$redirectbackto = optional_param('backto', '', PARAM_LOCALURL); // The location to redirect back.
|
||||
|
||||
$url = new moodle_url('/mod/data/edit.php');
|
||||
|
||||
$record = null;
|
||||
|
||||
if ($id) {
|
||||
list($course, $cm) = get_course_and_cm_from_cmid($id, manager::MODULE);
|
||||
$manager = manager::create_from_coursemodule($cm);
|
||||
} else { // We must have $d.
|
||||
$data = $DB->get_record('data', ['id' => $d], '*', MUST_EXIST);
|
||||
$manager = manager::create_from_instance($data);
|
||||
$cm = $manager->get_coursemodule();
|
||||
$course = get_course($cm->course);
|
||||
}
|
||||
$data = $manager->get_instance();
|
||||
$context = $manager->get_context();
|
||||
$url->param('id', $cm->id);
|
||||
|
||||
if ($rid !== 0) {
|
||||
$record = $DB->get_record('data_records', array(
|
||||
'id' => $rid,
|
||||
'dataid' => $d,
|
||||
), '*', MUST_EXIST);
|
||||
$record = $DB->get_record(
|
||||
'data_records',
|
||||
['id' => $rid, 'dataid' => $data->id],
|
||||
'*',
|
||||
MUST_EXIST
|
||||
);
|
||||
$url->param('rid', $rid);
|
||||
}
|
||||
|
||||
if ($id) {
|
||||
$url->param('id', $id);
|
||||
$PAGE->set_url($url);
|
||||
if (! $cm = get_coursemodule_from_id('data', $id)) {
|
||||
throw new \moodle_exception('invalidcoursemodule');
|
||||
}
|
||||
if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
|
||||
throw new \moodle_exception('coursemisconf');
|
||||
}
|
||||
if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
|
||||
throw new \moodle_exception('invalidcoursemodule');
|
||||
}
|
||||
|
||||
} else {
|
||||
$url->param('d', $d);
|
||||
$PAGE->set_url($url);
|
||||
if (! $data = $DB->get_record('data', array('id'=>$d))) {
|
||||
throw new \moodle_exception('invalidid', 'data');
|
||||
}
|
||||
if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
|
||||
throw new \moodle_exception('coursemisconf');
|
||||
}
|
||||
if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
|
||||
throw new \moodle_exception('invalidcoursemodule');
|
||||
}
|
||||
}
|
||||
$PAGE->set_url($url);
|
||||
require_login($course, false, $cm);
|
||||
|
||||
$url->param('backto', $redirectbackto);
|
||||
|
||||
require_login($course, false, $cm);
|
||||
|
||||
if (isguestuser()) {
|
||||
redirect('view.php?d='.$data->id);
|
||||
}
|
||||
|
||||
$context = context_module::instance($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");
|
||||
|
||||
$PAGE->set_title($data->name);
|
||||
$PAGE->set_heading($course->fullname);
|
||||
echo $OUTPUT->header();
|
||||
notice(get_string("activityiscurrentlyhidden"));
|
||||
}
|
||||
|
||||
/// Can't use this if there are no fields
|
||||
if (has_capability('mod/data:managetemplates', $context)) {
|
||||
if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) { // Brand new database!
|
||||
redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
|
||||
if (!$manager->has_fields()) {
|
||||
redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry.
|
||||
}
|
||||
}
|
||||
|
||||
if ($rid) {
|
||||
// When editing an existing record, we require the session key
|
||||
// When editing an existing record, we require the session key.
|
||||
require_sesskey();
|
||||
}
|
||||
|
||||
// Get Group information for permission testing and record creation
|
||||
// Get Group information for permission testing and record creation.
|
||||
$currentgroup = groups_get_activity_group($cm);
|
||||
$groupmode = groups_get_activity_groupmode($cm);
|
||||
|
||||
if (!has_capability('mod/data:manageentries', $context)) {
|
||||
if ($rid) {
|
||||
// User is editing an existing record
|
||||
// User is editing an existing record.
|
||||
if (!data_user_can_manage_entry($record, $data, $context)) {
|
||||
throw new \moodle_exception('noaccess', 'data');
|
||||
}
|
||||
} else if (!data_user_can_add_entry($data, $currentgroup, $groupmode, $context)) {
|
||||
// User is trying to create a new record
|
||||
// User is trying to create a new record.
|
||||
throw new \moodle_exception('noaccess', 'data');
|
||||
}
|
||||
}
|
||||
|
||||
/// RSS and CSS and JS meta
|
||||
// RSS and CSS and JS meta.
|
||||
if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
|
||||
$courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
|
||||
$rsstitle = $courseshortname . ': ' . format_string($data->name);
|
||||
@@ -132,16 +114,7 @@ if ($data->jstemplate) {
|
||||
$PAGE->requires->js('/mod/data/js.php?d='.$data->id, true);
|
||||
}
|
||||
|
||||
$possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
|
||||
|
||||
foreach ($possiblefields as $field) {
|
||||
if ($field->type == 'file' || $field->type == 'picture') {
|
||||
require_once($CFG->dirroot.'/repository/lib.php');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// Define page variables
|
||||
// Define page variables.
|
||||
$strdata = get_string('modulenameplural','data');
|
||||
|
||||
if ($rid) {
|
||||
@@ -156,86 +129,53 @@ $PAGE->activityheader->disable();
|
||||
|
||||
// Process incoming data for adding/updating records.
|
||||
|
||||
// Keep track of any notifications.
|
||||
$generalnotifications = array();
|
||||
$fieldnotifications = array();
|
||||
// Keep track of any notifications ad submitted data.
|
||||
$processeddata = null;
|
||||
$datarecord = data_submitted() ?: null;
|
||||
|
||||
// Process the submitted form.
|
||||
if ($datarecord = data_submitted() and confirm_sesskey()) {
|
||||
if ($rid) {
|
||||
// Updating an existing record.
|
||||
if ($datarecord && confirm_sesskey()) {
|
||||
// Validate the form to ensure that enough data was submitted.
|
||||
$fields = $manager->get_field_records();
|
||||
$processeddata = data_process_submission($data, $fields, $datarecord);
|
||||
|
||||
// Retrieve the format for the fields.
|
||||
$fields = $DB->get_records('data_fields', array('dataid' => $datarecord->d));
|
||||
|
||||
// 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);
|
||||
|
||||
if ($processeddata->validated) {
|
||||
// Enough data to update the record.
|
||||
if ($processeddata->validated) {
|
||||
if ($rid) {
|
||||
$recordid = $rid;
|
||||
// Updating an existing record.
|
||||
data_update_record_fields_contents($data, $record, $context, $datarecord, $processeddata);
|
||||
core_tag_tag::set_item_tags('mod_data', 'data_records', $rid, $context, $tags);
|
||||
|
||||
$viewurl = new moodle_url('/mod/data/view.php', array(
|
||||
'd' => $data->id,
|
||||
'rid' => $rid,
|
||||
));
|
||||
redirect($viewurl);
|
||||
} else {
|
||||
// Add instance to data_record.
|
||||
$recordid = data_add_record($data, $currentgroup);
|
||||
if ($recordid) {
|
||||
// Now populate the fields contents of the new record.
|
||||
data_add_fields_contents_to_new_record($data, $context, $recordid, $fields, $datarecord, $processeddata);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
// No recordid was specified - creating a new entry.
|
||||
|
||||
// Retrieve the format for the fields.
|
||||
$fields = $DB->get_records('data_fields', array('dataid' => $datarecord->d));
|
||||
|
||||
// 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)) {
|
||||
|
||||
// Now populate the fields contents of the new record.
|
||||
data_add_fields_contents_to_new_record($data, $context, $recordid, $fields, $datarecord, $processeddata);
|
||||
|
||||
if ($recordid) {
|
||||
core_tag_tag::set_item_tags('mod_data', 'data_records', $recordid, $context, $tags);
|
||||
|
||||
if (!empty($datarecord->saveandview)) {
|
||||
$viewurl = new moodle_url('/mod/data/view.php', array(
|
||||
'd' => $data->id,
|
||||
'rid' => $recordid,
|
||||
));
|
||||
redirect($viewurl);
|
||||
} else if (!empty($datarecord->saveandadd)) {
|
||||
if (!empty($datarecord->saveandadd)) {
|
||||
// User has clicked "Save and add another". Reset all of the fields.
|
||||
$datarecord = null;
|
||||
} else {
|
||||
$viewurl = new moodle_url('/mod/data/view.php', [
|
||||
'd' => $data->id,
|
||||
'rid' => $recordid,
|
||||
]);
|
||||
redirect($viewurl);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// End of form processing.
|
||||
|
||||
|
||||
/// Print the page header
|
||||
|
||||
echo $OUTPUT->header();
|
||||
|
||||
groups_print_activity_menu($cm, $CFG->wwwroot.'/mod/data/edit.php?d='.$data->id);
|
||||
|
||||
/// 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
|
||||
// Form goes here first in case add template is empty.
|
||||
echo '<form enctype="multipart/form-data" action="edit.php" method="post">';
|
||||
echo '<div>';
|
||||
echo '<input name="d" value="'.$data->id.'" type="hidden" />';
|
||||
@@ -249,80 +189,45 @@ if (!$rid){
|
||||
echo $OUTPUT->heading(get_string('editentry','data'));
|
||||
}
|
||||
|
||||
/******************************************
|
||||
* Regular expression replacement section *
|
||||
******************************************/
|
||||
if ($data->addtemplate){
|
||||
$possiblefields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
|
||||
$patterns = array();
|
||||
$replacements = array();
|
||||
$template = $manager->get_template($mode);
|
||||
echo $template->parse_add_entry($processeddata, $rid, $datarecord);
|
||||
|
||||
///then we generate strings to replace
|
||||
foreach ($possiblefields as $eachfield){
|
||||
$field = data_get_field($eachfield, $data);
|
||||
|
||||
// To skip unnecessary calls to display_add_field().
|
||||
if (strpos($data->addtemplate, "[[".$field->field->name."]]") !== false) {
|
||||
// Replace the field tag.
|
||||
$patterns[] = "[[".$field->field->name."]]";
|
||||
$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;
|
||||
}
|
||||
|
||||
if (core_tag_tag::is_enabled('mod_data', 'data_records')) {
|
||||
$patterns[] = "##tags##";
|
||||
$replacements[] = data_generate_tag_form($rid);
|
||||
}
|
||||
|
||||
$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 = '';
|
||||
if (empty($redirectbackto)) {
|
||||
$redirectbackto = new \moodle_url('/mod/data/view.php', ['id' => $cm->id]);
|
||||
}
|
||||
|
||||
foreach ($generalnotifications as $notification) {
|
||||
echo $OUTPUT->notification($notification);
|
||||
}
|
||||
echo $newtext;
|
||||
|
||||
$redirectbackto = !empty($redirectbackto) ? $redirectbackto :
|
||||
new \moodle_url('/mod/data/view.php', ['d' => $data->id]);
|
||||
$actionbuttons = html_writer::link($redirectbackto, get_string('cancel'), ['class' => 'btn btn-secondary']);
|
||||
$actionbuttons .= html_writer::empty_tag('input', ['type' => 'submit', 'name' => 'saveandview',
|
||||
'value' => get_string('save'), 'class' => 'btn btn-primary ml-2']);
|
||||
$actionbuttons = html_writer::link(
|
||||
$redirectbackto,
|
||||
get_string('cancel'),
|
||||
['class' => 'btn btn-secondary', 'role' => 'button']
|
||||
);
|
||||
$actionbuttons .= html_writer::empty_tag('input', [
|
||||
'type' => 'submit',
|
||||
'name' => 'saveandview',
|
||||
'value' => get_string('save'),
|
||||
'class' => 'btn btn-primary ml-2'
|
||||
]);
|
||||
|
||||
if (!$rid && ((!$data->maxentries) ||
|
||||
has_capability('mod/data:manageentries', $context) ||
|
||||
(data_numentries($data) < ($data->maxentries - 1)))) {
|
||||
$actionbuttons .= html_writer::empty_tag('input', ['type' => 'submit', 'name' => 'saveandadd',
|
||||
'value' => get_string('saveandadd', 'data'), 'class' => 'btn btn-primary ml-2']);
|
||||
has_capability('mod/data:manageentries', $context) ||
|
||||
(data_numentries($data) < ($data->maxentries - 1)))) {
|
||||
$actionbuttons .= html_writer::empty_tag('input', [
|
||||
'type' => 'submit', 'name' => 'saveandadd',
|
||||
'value' => get_string('saveandadd', 'data'), 'class' => 'btn btn-primary ml-2'
|
||||
]);
|
||||
}
|
||||
|
||||
echo html_writer::div($actionbuttons, 'mdl-align mt-2');
|
||||
echo $OUTPUT->box_end();
|
||||
echo '</div></form>';
|
||||
|
||||
|
||||
/// Finish the page
|
||||
|
||||
// Print the stuff that need to come after the form fields.
|
||||
if (!$fields = $DB->get_records('data_fields', array('dataid'=>$data->id))) {
|
||||
throw new \moodle_exception('nofieldindatabase', 'data');
|
||||
}
|
||||
foreach ($fields as $eachfield) {
|
||||
$field = data_get_field($eachfield, $data);
|
||||
$possiblefields = $manager->get_fields();
|
||||
foreach ($possiblefields as $field) {
|
||||
$field->print_after_form();
|
||||
}
|
||||
|
||||
// Finish the page.
|
||||
if (empty($possiblefields)) {
|
||||
throw new \moodle_exception('nofieldindatabase', 'data');
|
||||
}
|
||||
echo $OUTPUT->footer();
|
||||
|
||||
Reference in New Issue
Block a user