got editor working

This commit is contained in:
PMKuipers 2023-10-21 23:24:25 +02:00
parent 602bb6e0b3
commit 0dda0c6a45
9 changed files with 59 additions and 29 deletions

View File

@ -1,3 +1,3 @@
define("local_treestudyplan/util/mform-helper",["exports","core/ajax","core/fragment","core/notification","./string-helper","./debugger"],(function(_exports,_ajax,_fragment,_notification,_stringHelper,_debugger){function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.default=void 0,_notification=_interopRequireDefault(_notification),_debugger=_interopRequireDefault(_debugger);var _default={install:function(Vue){var debug=new _debugger.default("treestudyplan-mform-helper"),strings=(0,_stringHelper.load_strings)({editmod:{save$core:"save$core",cancel$core:"cancel$core"}});Vue.component("mform",{props:{name:{type:String},params:{type:Object}},data:function(){return{content:"",loading:!0,uuid:void 0!==crypto.randomUUID?crypto.randomUUID():"10000000-1000-4000-8000-100000000000".replace(/[018]/g,(function(c){return(c^crypto.getRandomValues(new Uint8Array(1))[0]&15>>c/4).toString(16)})),text:strings}},computed:{},methods:{openForm:function(){this.$refs.editormodal.show()},onShown:function(){var self=this;debug.info('Loading form "'.concat(self.name,'" with params'),self.params),self.loading=!1,(0,_ajax.call)([{methodname:"local_treestudyplan_get_mform",args:{formname:self.name,params:JSON.stringify(self.params)}}])[0].then((function(data){self.content=data.html,self.loading=!1,(0,_fragment.processCollectedJavascript)(data.js)})).catch(_notification.default.exception)},onSave:function(){var self=this,form=this.$refs.content.getElementsByTagName("form")[0];form.dispatchEvent(new Event("save-form-state"));var formdata=new FormData(form),data=new URLSearchParams(formdata).toString();(0,_ajax.call)([{methodname:"local_treestudyplan_submit_mform",args:{formname:self.name,params:JSON.stringify(self.params),data:data}}])[0].then((function(){self.$emit("saved",formdata)})).catch(_notification.default.exception)}},template:'\n <span class=\'s-edit-mod\'><a href=\'#\' @click.prevent="openForm"><slot><i class="fa fa-cog"></i></slot></a>\n <b-modal\n ref="editormodal"\n scrollable\n centered\n size="xl"\n id="\'modal-\'+uuid"\n @shown="onShown"\n @ok="onSave"\n :title="title"\n :ok-title="text.save$core"\n ><div :class="\'s-edit-mod-form \'" ref="content"\n ><div v-if="loading" class="d-flex justify-content-center mb-3"\n ><b-spinner variant="primary"></b-spinner\n ></div\n ><span v-else v-html="content"\n ></div\n ></b-modal>\n </span>\n '})}};return _exports.default=_default,_exports.default})); define("local_treestudyplan/util/mform-helper",["exports","core/ajax","core/fragment","core/templates","core/notification","./string-helper","./debugger"],(function(_exports,_ajax,_fragment,_templates,_notification,_stringHelper,_debugger){function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.default=void 0,_notification=_interopRequireDefault(_notification),_debugger=_interopRequireDefault(_debugger);var _default={install:function(Vue){var debug=new _debugger.default("treestudyplan-mform-helper"),strings=(0,_stringHelper.load_strings)({editmod:{save$core:"save$core",cancel$core:"cancel$core"}});Vue.component("mform",{props:{name:{type:String},params:{type:Object},title:{type:String,default:""}},data:function(){return{content:"",loading:!0,uuid:void 0!==crypto.randomUUID?crypto.randomUUID():"10000000-1000-4000-8000-100000000000".replace(/[018]/g,(function(c){return(c^crypto.getRandomValues(new Uint8Array(1))[0]&15>>c/4).toString(16)})),text:strings}},computed:{},methods:{openForm:function(){this.$refs.editormodal.show()},onShown:function(){var self=this;debug.info('Loading form "'.concat(self.name,'" with params'),self.params),self.loading=!1,(0,_ajax.call)([{methodname:"local_treestudyplan_get_mform",args:{formname:self.name,params:JSON.stringify(self.params)}}])[0].then((function(data){var html=data.html;self.loading=!1;var js=(0,_fragment.processCollectedJavascript)(data.javascript);debug.info("Replacing content / el",self.$refs.content),debug.info("Replacing content / html",html),debug.info("Replacing content / js",js);var r=(0,_templates.replaceNodeContents)(self.$refs.content,html,js);debug.info("R:",r)})).catch(_notification.default.exception)},onSave:function(){var self=this,form=this.$refs.content.getElementsByTagName("form")[0];form.dispatchEvent(new Event("save-form-state"));var formdata=new FormData(form),data=new URLSearchParams(formdata).toString();(0,_ajax.call)([{methodname:"local_treestudyplan_submit_mform",args:{formname:self.name,params:JSON.stringify(self.params),formdata:data}}])[0].then((function(){self.$emit("saved",formdata)})).catch(_notification.default.exception)}},template:'\n <span class=\'mform-container\'><a href=\'#\' @click.prevent="openForm"><slot><i class="fa fa-cog"></i></slot></a>\n <b-modal\n ref="editormodal"\n scrollable\n centered\n size="xl"\n id="\'modal-\'+uuid"\n @shown="onShown"\n @ok="onSave"\n :title="title"\n :ok-title="text.save$core"\n ><div :class="\'s-mform-content\'" ref="content"\n ><div class="d-flex justify-content-center mb-3"\n ><b-spinner variant="primary"></b-spinner\n ></div\n ></div\n ></b-modal>\n </span>\n '})}};return _exports.default=_default,_exports.default}));
//# sourceMappingURL=mform-helper.min.js.map //# sourceMappingURL=mform-helper.min.js.map

File diff suppressed because one or more lines are too long

View File

@ -7,7 +7,7 @@
import {call} from 'core/ajax'; import {call} from 'core/ajax';
import {processCollectedJavascript} from 'core/fragment'; import {processCollectedJavascript} from 'core/fragment';
//import {replaceNodeContents} from 'core/templates'; import {replaceNodeContents} from 'core/templates';
import notification from 'core/notification'; import notification from 'core/notification';
import {load_strings} from './string-helper'; import {load_strings} from './string-helper';
import Debugger from './debugger'; import Debugger from './debugger';
@ -45,7 +45,11 @@ export default {
}, },
params: { params: {
type: Object, type: Object,
} },
title: {
type: String,
default: "",
},
}, },
data() { data() {
return { return {
@ -70,10 +74,17 @@ export default {
methodname: 'local_treestudyplan_get_mform', methodname: 'local_treestudyplan_get_mform',
args: {formname: self.name, params: JSON.stringify(self.params)} args: {formname: self.name, params: JSON.stringify(self.params)}
}])[0].then((data)=>{ }])[0].then((data)=>{
self.content = data.html; const html = data.html;
self.loading = false; self.loading = false;
// Process the collected javascript; // Process the collected javascript;
processCollectedJavascript(data.js); const js = processCollectedJavascript(data.javascript);
debug.info("Replacing content / el",self.$refs["content"]);
debug.info("Replacing content / html", html);
debug.info("Replacing content / js",js);
const r = replaceNodeContents(self.$refs["content"], html, js);
debug.info("R:",r);
}).catch(notification.exception); }).catch(notification.exception);
}, },
@ -92,7 +103,7 @@ export default {
call([{ call([{
methodname: 'local_treestudyplan_submit_mform', methodname: 'local_treestudyplan_submit_mform',
args: {formname: self.name, params: JSON.stringify(self.params), data: data} args: {formname: self.name, params: JSON.stringify(self.params), formdata: data}
}])[0].then(()=>{ }])[0].then(()=>{
self.$emit("saved",formdata); self.$emit("saved",formdata);
}).catch(notification.exception); }).catch(notification.exception);
@ -100,7 +111,7 @@ export default {
} }
}, },
template: ` template: `
<span class='s-edit-mod'><a href='#' @click.prevent="openForm"><slot><i class="fa fa-cog"></i></slot></a> <span class='mform-container'><a href='#' @click.prevent="openForm"><slot><i class="fa fa-cog"></i></slot></a>
<b-modal <b-modal
ref="editormodal" ref="editormodal"
scrollable scrollable
@ -111,11 +122,10 @@ export default {
@ok="onSave" @ok="onSave"
:title="title" :title="title"
:ok-title="text.save$core" :ok-title="text.save$core"
><div :class="'s-edit-mod-form '" ref="content" ><div :class="'s-mform-content'" ref="content"
><div v-if="loading" class="d-flex justify-content-center mb-3" ><div class="d-flex justify-content-center mb-3"
><b-spinner variant="primary"></b-spinner ><b-spinner variant="primary"></b-spinner
></div ></div
><span v-else v-html="content"
></div ></div
></b-modal> ></b-modal>
</span> </span>

View File

@ -65,8 +65,11 @@ abstract class formbase extends \moodleform {
*/ */
public function process_submission() { public function process_submission() {
$data = $this->get_data(); $data = $this->get_data();
if($data) { if($data) {
$this->process_submitted_data($data); return $this->process_submitted_data($data);
} else {
return false;
} }
} }

View File

@ -3,6 +3,7 @@
namespace local_treestudyplan\local\forms; namespace local_treestudyplan\local\forms;
use local_treestudyplan\studyplan; use local_treestudyplan\studyplan;
use local_treestudyplan\local\helpers\webservicehelper; use local_treestudyplan\local\helpers\webservicehelper;
use moodle_exception;
use stdClass; use stdClass;
/** /**
@ -45,7 +46,7 @@ class studyplan_editform extends formbase {
* @throws \moodle_exception if access denied for a specific reason. * @throws \moodle_exception if access denied for a specific reason.
*/ */
public static function check_security(object $customdata) { public static function check_security(object $customdata) {
webservicehelper::require_capabilities(self::CAP_EDIT,$customdata->context); /*webservicehelper::require_capabilities(self::CAP_EDIT,$customdata->context); */
} }
/** /**
@ -68,7 +69,8 @@ class studyplan_editform extends formbase {
\context_system::instance(), \context_system::instance(),
'local_treestudyplan', 'local_treestudyplan',
'studyplan', 'studyplan',
$entry->id); $customdata->plan->id());
return $entry; return $entry;
} }
@ -89,7 +91,7 @@ class studyplan_editform extends formbase {
/** /**
* Process the submitted data and perform necessary actions * Process the submitted data and perform necessary actions
* @param object $entry The processed form data; * @param object $entry The processed form data;
* @return bool True if submission successful * @return bool false if submission not successful
* @throws \moodle_exception if an error must be given for a specific reason. * @throws \moodle_exception if an error must be given for a specific reason.
*/ */
protected function process_submitted_data($entry) { protected function process_submitted_data($entry) {
@ -101,12 +103,18 @@ class studyplan_editform extends formbase {
\context_system::instance(), \context_system::instance(),
'local_treestudyplan', 'local_treestudyplan',
'studyplan', 'studyplan',
$entry->id); $customdata->plan->id());
$f = fopen("/tmp/debug","a+");
fputs($f,print_r($entry,true));
fclose($f);
// Use our own abstraction to update the record, so caches are maintained // Use our own abstraction to update the record, so caches are maintained
$customdata->plan->edit(['description' => $entry->description, $customdata->plan->edit(['description' => $entry->description,
'descriptionformat' => $entry->descriptionformat]); 'descriptionformat' => $entry->descriptionformat]);
return true;
} }

View File

@ -158,7 +158,7 @@ class studyplan {
"shortname" => new \external_value(PARAM_TEXT, 'shortname of studyplan'), "shortname" => new \external_value(PARAM_TEXT, 'shortname of studyplan'),
"idnumber" => new \external_value(PARAM_TEXT, 'idnumber of curriculum'), "idnumber" => new \external_value(PARAM_TEXT, 'idnumber of curriculum'),
"context_id" => new \external_value(PARAM_INT, 'context_id of studyplan'), "context_id" => new \external_value(PARAM_INT, 'context_id of studyplan'),
"description" => new \external_value(PARAM_TEXT, 'description of studyplan'), "description" => new \external_value(PARAM_RAW, 'description of studyplan'),
"descriptionformat" => new \external_value(PARAM_INT, 'description format'), "descriptionformat" => new \external_value(PARAM_INT, 'description format'),
"aggregation" => new \external_value(PARAM_TEXT, 'selected aggregator'), "aggregation" => new \external_value(PARAM_TEXT, 'selected aggregator'),
"aggregation_config" => new \external_value(PARAM_TEXT, 'config string for aggregator'), "aggregation_config" => new \external_value(PARAM_TEXT, 'config string for aggregator'),
@ -202,7 +202,8 @@ class studyplan {
"name" => new \external_value(PARAM_TEXT, 'name of studyplan'), "name" => new \external_value(PARAM_TEXT, 'name of studyplan'),
"shortname" => new \external_value(PARAM_TEXT, 'shortname of studyplan'), "shortname" => new \external_value(PARAM_TEXT, 'shortname of studyplan'),
"idnumber" => new \external_value(PARAM_TEXT, 'idnumber of curriculum'), "idnumber" => new \external_value(PARAM_TEXT, 'idnumber of curriculum'),
"description" => new \external_value(PARAM_TEXT, 'description of studyplan'), "description" => new \external_value(PARAM_RAW, 'description of studyplan'),
"descriptionformat" => new \external_value(PARAM_INT, 'description format'),
"context_id" => new \external_value(PARAM_INT, 'context_id of studyplan'), "context_id" => new \external_value(PARAM_INT, 'context_id of studyplan'),
"aggregation" => new \external_value(PARAM_TEXT, 'selected aggregator'), "aggregation" => new \external_value(PARAM_TEXT, 'selected aggregator'),
"aggregation_config" => new \external_value(PARAM_TEXT, 'config string for aggregator'), "aggregation_config" => new \external_value(PARAM_TEXT, 'config string for aggregator'),
@ -232,6 +233,7 @@ class studyplan {
'shortname' => $this->r->shortname, 'shortname' => $this->r->shortname,
'idnumber' => $this->r->idnumber, 'idnumber' => $this->r->idnumber,
'description' => $this->r->description, 'description' => $this->r->description,
'descriptionformat' => $this->r->descriptionformat,
'context_id' => $this->context()->id, 'context_id' => $this->context()->id,
"aggregation" => $this->r->aggregation, "aggregation" => $this->r->aggregation,
"aggregation_config" => $this->aggregator->config_string(), "aggregation_config" => $this->aggregator->config_string(),
@ -558,7 +560,8 @@ class studyplan {
"id" => new \external_value(PARAM_INT, 'id of studyplan'), "id" => new \external_value(PARAM_INT, 'id of studyplan'),
"name" => new \external_value(PARAM_TEXT, 'name of studyplan'), "name" => new \external_value(PARAM_TEXT, 'name of studyplan'),
"shortname" => new \external_value(PARAM_TEXT, 'shortname of studyplan'), "shortname" => new \external_value(PARAM_TEXT, 'shortname of studyplan'),
"description" => new \external_value(PARAM_TEXT, 'description of studyplan'), "description" => new \external_value(PARAM_RAW, 'description of studyplan'),
"descriptionformat" => new \external_value(PARAM_INT, 'description format'),
"idnumber" => new \external_value(PARAM_TEXT, 'idnumber of curriculum'), "idnumber" => new \external_value(PARAM_TEXT, 'idnumber of curriculum'),
"pages" => new \external_multiple_structure(studyplanpage::user_structure()), "pages" => new \external_multiple_structure(studyplanpage::user_structure()),
"aggregation_info" => aggregator::basic_structure(), "aggregation_info" => aggregator::basic_structure(),
@ -578,6 +581,7 @@ class studyplan {
'name' => $this->r->name, 'name' => $this->r->name,
'shortname' => $this->r->shortname, 'shortname' => $this->r->shortname,
'description' => $this->r->description, 'description' => $this->r->description,
'descriptionformat' => $this->r->descriptionformat,
'idnumber' => $this->r->idnumber, 'idnumber' => $this->r->idnumber,
'pages' => [], 'pages' => [],
'aggregation_info' => $this->aggregator->basic_model(), 'aggregation_info' => $this->aggregator->basic_model(),

View File

@ -148,7 +148,7 @@ class studyplanpage {
"fullname" => new \external_value(PARAM_TEXT, 'name of studyplan page'), "fullname" => new \external_value(PARAM_TEXT, 'name of studyplan page'),
"shortname" => new \external_value(PARAM_TEXT, 'shortname of studyplan page'), "shortname" => new \external_value(PARAM_TEXT, 'shortname of studyplan page'),
"periods" => new \external_value(PARAM_INT, 'number of periods in studyplan page'), "periods" => new \external_value(PARAM_INT, 'number of periods in studyplan page'),
"description" => new \external_value(PARAM_TEXT, 'description of studyplan page'), "description" => new \external_value(PARAM_RAW, 'description of studyplan page'),
"startdate" => new \external_value(PARAM_TEXT, 'start date of studyplan'), "startdate" => new \external_value(PARAM_TEXT, 'start date of studyplan'),
"enddate" => new \external_value(PARAM_TEXT, 'end date of studyplan'), "enddate" => new \external_value(PARAM_TEXT, 'end date of studyplan'),
"perioddesc" => period::page_structure(), "perioddesc" => period::page_structure(),
@ -181,7 +181,7 @@ class studyplanpage {
"id" => new \external_value(PARAM_INT, 'id of studyplan'), "id" => new \external_value(PARAM_INT, 'id of studyplan'),
"fullname" => new \external_value(PARAM_TEXT, 'name of studyplan page'), "fullname" => new \external_value(PARAM_TEXT, 'name of studyplan page'),
"shortname" => new \external_value(PARAM_TEXT, 'shortname of studyplan page'), "shortname" => new \external_value(PARAM_TEXT, 'shortname of studyplan page'),
"description" => new \external_value(PARAM_TEXT, 'description of studyplan page'), "description" => new \external_value(PARAM_RAW, 'description of studyplan page'),
"periods" => new \external_value(PARAM_INT, 'number of periods in studyplan page'), "periods" => new \external_value(PARAM_INT, 'number of periods in studyplan page'),
"startdate" => new \external_value(PARAM_TEXT, 'start date of studyplan page'), "startdate" => new \external_value(PARAM_TEXT, 'start date of studyplan page'),
"enddate" => new \external_value(PARAM_TEXT, 'end date of studyplan page'), "enddate" => new \external_value(PARAM_TEXT, 'end date of studyplan page'),
@ -300,7 +300,7 @@ class studyplanpage {
"id" => new \external_value(PARAM_INT, 'id of studyplan page'), "id" => new \external_value(PARAM_INT, 'id of studyplan page'),
"fullname" => new \external_value(PARAM_TEXT, 'name of studyplan page'), "fullname" => new \external_value(PARAM_TEXT, 'name of studyplan page'),
"shortname" => new \external_value(PARAM_TEXT, 'shortname of studyplan page'), "shortname" => new \external_value(PARAM_TEXT, 'shortname of studyplan page'),
"description" => new \external_value(PARAM_TEXT, 'description of studyplan page'), "description" => new \external_value(PARAM_RAW, 'description of studyplan page'),
"periods" => new \external_value(PARAM_INT, 'number of slots in studyplan page'), "periods" => new \external_value(PARAM_INT, 'number of slots in studyplan page'),
"startdate" => new \external_value(PARAM_TEXT, 'start date of studyplan page'), "startdate" => new \external_value(PARAM_TEXT, 'start date of studyplan page'),
"enddate" => new \external_value(PARAM_TEXT, 'end date of studyplan page'), "enddate" => new \external_value(PARAM_TEXT, 'end date of studyplan page'),

View File

@ -89,18 +89,17 @@ class utilityservice extends \external_api {
\external_api::validate_context(\context_system::instance()); \external_api::validate_context(\context_system::instance());
require_login(null,false,null); require_login(null,false,null);
// Load the form before any output is started.
$mform = self::load_mform($formname, $params);
// Hack alert: Set a default URL to stop the annoying debug. // Hack alert: Set a default URL to stop the annoying debug.
$PAGE->set_url('/'); $PAGE->set_url('/');
// Hack alert: Forcing bootstrap_renderer to initiate moodle page. // Hack alert: Forcing bootstrap_renderer to initiate moodle page.
$OUTPUT->header(); $OUTPUT->header();
/* Overwriting page_requirements_manager with the fragment one so only JS included from /* Overwriting page_requirements_manager with the fragment one so only JS included from
this point is returned to the user. */ this point is returned to the user. */
$PAGE->start_collecting_javascript_requirements(); $PAGE->start_collecting_javascript_requirements();
// Load the form before any output is started.
$mform = self::load_mform($formname, $params);
// Perform actual render. // Perform actual render.
$html = $mform->render(); $html = $mform->render();
@ -149,10 +148,16 @@ class utilityservice extends \external_api {
* @return array Success/fail structure * @return array Success/fail structure
*/ */
public static function submit_mform($formname, $params, $formdata) { public static function submit_mform($formname, $params, $formdata) {
// Load the form, provide submitted form data and perform security checks \external_api::validate_context(\context_system::instance());
$mform = self::load_mform($formname, $params, $formdata); require_login(null,false,null);
if ($mform->process_submission()) { $ajaxformdata = [];
parse_str($formdata,$ajaxformdata);
// Load the form, provide submitted form data and perform security checks
$mform = self::load_mform($formname, $params, $ajaxformdata);
if ($mform->process_submission() !== false) {
return success::success()->model(); return success::success()->model();
} else { } else {
return success::fail("Error in submission data")->model(); return success::fail("Error in submission data")->model();

View File

@ -22,7 +22,7 @@
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
$plugin->component = 'local_treestudyplan'; // Recommended since 2.0.2 (MDL-26035). Required since 3.0 (MDL-48494). $plugin->component = 'local_treestudyplan'; // Recommended since 2.0.2 (MDL-26035). Required since 3.0 (MDL-48494).
$plugin->version = 2023102000; // YYYYMMDDHH (year, month, day, iteration). $plugin->version = 2023102100; // YYYYMMDDHH (year, month, day, iteration).
$plugin->requires = 2021051700; // YYYYMMDDHH (This is the release version for Moodle 3.11). $plugin->requires = 2021051700; // YYYYMMDDHH (This is the release version for Moodle 3.11).
$plugin->release = "1.1.0-b"; $plugin->release = "1.1.0-b";