From 5f580a9bde8fe4372eec67bfc6e495a2923d8311 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 25 Aug 2016 12:38:41 +0800 Subject: [PATCH] Used inplace editable API for editing element names --- classes/edit_form.php | 5 ++++- lib.php | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/classes/edit_form.php b/classes/edit_form.php index 8a00eef..4cdc322 100644 --- a/classes/edit_form.php +++ b/classes/edit_form.php @@ -243,8 +243,11 @@ class edit_form extends \moodleform { $table->align = array('left', 'left', 'left'); // Loop through and add the elements to the table. foreach ($elements as $element) { + $elementname = new \core\output\inplace_editable('mod_customcert', 'elementname', $element->id, + true, format_string($element->name), $element->name); + $row = new \html_table_row(); - $row->cells[] = $element->name; + $row->cells[] = $OUTPUT->render($elementname); $row->cells[] = $element->element; // Link to edit this element. $link = new \moodle_url($editelementlink, $editelementlinkparams + array('id' => $element->id, diff --git a/lib.php b/lib.php index 33c01ee..e430d9a 100644 --- a/lib.php +++ b/lib.php @@ -354,3 +354,41 @@ function customcert_extend_settings_navigation(settings_navigation $settings, na return $customcertnode->trim_if_empty(); } + +/** + * Handles editing the 'name' of the element in a list. + * + * @param $itemtype + * @param $itemid + * @param $newvalue + * @return \core\output\inplace_editable + */ +function mod_customcert_inplace_editable($itemtype, $itemid, $newvalue) { + global $DB; + + if ($itemtype === 'elementname') { + $element = $DB->get_record('customcert_elements', array('id' => $itemid), '*', MUST_EXIST); + $page = $DB->get_record('customcert_pages', array('id' => $element->pageid), '*', MUST_EXIST); + $template = $DB->get_record('customcert_templates', array('id' => $page->templateid), '*', MUST_EXIST); + + // Set the template object. + $template = new \mod_customcert\template($template); + // Perform checks. + if ($cm = $template->get_cm()) { + require_login($cm->course, false, $cm); + } else { + require_login(); + } + // Make sure the user has the required capabilities. + $template->require_manage(); + + // Clean input and update the record. + $updateelement = new stdClass(); + $updateelement->id = $element->id; + $updateelement->name = clean_param($newvalue, PARAM_TEXT); + $DB->update_record('customcert_elements', $updateelement); + + return new \core\output\inplace_editable('mod_customcert', 'elementname', $element->id, true, + $updateelement->name, $updateelement->name); + } +}