Added mform through ajax features to support rich editor for studyplan description
This commit is contained in:
parent
2a642f33c8
commit
49fa4b2ed3
45
classes/local/forms/formbase.php
Normal file
45
classes/local/forms/formbase.php
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace local_treestudyplan\local\forms;
|
||||||
|
|
||||||
|
use moodleform;
|
||||||
|
|
||||||
|
abstract class formbase extends moodleform {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the form while providing the correct defaults for our type of forms
|
||||||
|
* @param array $ajaxformdata Provide submitted form data through Ajax here
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function __construct($ajaxformdata=null) {
|
||||||
|
parent::__construct(null, null, 'post', '', null, true, $ajaxformdata);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate form data from parameters
|
||||||
|
* Also validate parameters and access permissions here
|
||||||
|
*
|
||||||
|
* @param array $params The parameters for form initialization
|
||||||
|
* @return array Form data based on parameters
|
||||||
|
*/
|
||||||
|
abstract public static function init_data(array $params);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate security access for this form based on the provided parameters
|
||||||
|
* Return true if validation passes, false or throw an exception if it does not.
|
||||||
|
*
|
||||||
|
* @param array $params The parameters for form initialization
|
||||||
|
* @return bool True if security validation passes.
|
||||||
|
* @throws \moodle_exception if access denied for a specific reason.
|
||||||
|
*/
|
||||||
|
abstract public static function check_security(array $params);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process the submission and perform necessary actions
|
||||||
|
* @return bool True if submission successful
|
||||||
|
* @throws \moodle_exception if an error must be given for a specific reason.
|
||||||
|
*/
|
||||||
|
abstract public function process_submission();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -2,19 +2,58 @@
|
||||||
|
|
||||||
namespace local_treestudyplan\local\forms;
|
namespace local_treestudyplan\local\forms;
|
||||||
|
|
||||||
use moodleform;
|
/**
|
||||||
|
* Moodleform class for the studyplan editor. A Moodleform is used here to facilitate a rich editor
|
||||||
|
* in the studyplan description
|
||||||
|
*/
|
||||||
|
class studyplan_editform extends formbase {
|
||||||
|
|
||||||
class studyplan_editform extends moodleform {
|
/**
|
||||||
|
* Validate security access for this form based on the provided parameters
|
||||||
|
* Return true if validation passes, false or throw an exception if it does not.
|
||||||
|
*
|
||||||
|
* @param array $params The parameters for form initialization
|
||||||
|
* @return bool True if security validation passes.
|
||||||
|
* @throws \moodle_exception if access denied for a specific reason.
|
||||||
|
*/
|
||||||
|
public static function check_security(array $params) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate form data from parameters
|
||||||
|
* Also validate parameters and access permissions here
|
||||||
|
*
|
||||||
|
* @param array $params The parameters for form initialization
|
||||||
|
* @return array Form data based on parameters
|
||||||
|
*/
|
||||||
|
public static function init_data(array $params) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up the form definition
|
||||||
|
*/
|
||||||
public function definition() {
|
public function definition() {
|
||||||
global $CFG, $COURSE, $DB, $PAGE;
|
|
||||||
$mform = $this->_form;
|
$mform = $this->_form;
|
||||||
|
|
||||||
// Activity.
|
// Define the form
|
||||||
$mform->addElement('editor', 'activityeditor',
|
$mform->addElement('editor', 'activityeditor',
|
||||||
get_string('activityeditor', 'assign'), array('rows' => 10), array('maxfiles' => EDITOR_UNLIMITED_FILES,
|
get_string('activityeditor', 'assign'), array('rows' => 10), array('maxfiles' => EDITOR_UNLIMITED_FILES,
|
||||||
'noclean' => true, 'context' => $this->context, 'subdirs' => true));
|
'noclean' => true, 'context' => $this->context, 'subdirs' => true));
|
||||||
$mform->addHelpButton('activityeditor', 'activityeditor', 'assign');
|
$mform->addHelpButton('activityeditor', 'activityeditor', 'assign');
|
||||||
$mform->setType('activityeditor', PARAM_RAW);
|
$mform->setType('activityeditor', PARAM_RAW);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process the submission and perform necessary actions
|
||||||
|
* @throws \moodle_exception if an error must be given for a specific reason.
|
||||||
|
*/
|
||||||
|
public function process_submission()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
161
classes/utilityservice.php
Normal file
161
classes/utilityservice.php
Normal file
|
@ -0,0 +1,161 @@
|
||||||
|
<?php
|
||||||
|
// This file is part of the Studyplan plugin for Moodle
|
||||||
|
//
|
||||||
|
// Moodle is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// Moodle is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
/**
|
||||||
|
* Webservice class for managing studyplans
|
||||||
|
* @package local_treestudyplan
|
||||||
|
* @copyright 2023 P.M. Kuipers
|
||||||
|
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace local_treestudyplan;
|
||||||
|
defined('MOODLE_INTERNAL') || die();
|
||||||
|
|
||||||
|
use local_treestudyplan\local\helpers\webservicehelper;
|
||||||
|
|
||||||
|
require_once($CFG->libdir.'/externallib.php');
|
||||||
|
require_once($CFG->libdir.'/badgeslib.php');
|
||||||
|
require_once($CFG->libdir.'/gradelib.php');
|
||||||
|
require_once($CFG->dirroot.'/course/modlib.php');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Webservice class for managing studyplans
|
||||||
|
*/
|
||||||
|
class utilityservice extends \external_api {
|
||||||
|
/**
|
||||||
|
* Capability required to edit study plans
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
const CAP_EDIT = "local/treestudyplan:editstudyplan";
|
||||||
|
/**
|
||||||
|
* Capability required to view studyplans (for other users)
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
const CAP_VIEW = "local/treestudyplan:viewuserreports";
|
||||||
|
|
||||||
|
|
||||||
|
protected function load_mform($formname, $params, $ajaxformdata = null) {
|
||||||
|
global $CFG;
|
||||||
|
$modmoodleform = "$CFG->dirroot/local/treestudyplan/local/forms/{$formname}.php";
|
||||||
|
if (!file_exists($modmoodleform)) {
|
||||||
|
// We don't need to load the form (autoloading will handle that) but do need to check it's existence
|
||||||
|
throw new \moodle_exception('noformdesc', 'local_treestudyplan');;
|
||||||
|
}
|
||||||
|
|
||||||
|
$mformclassname = "\\local_treestudyplan\\local\\forms\\{$formname}";
|
||||||
|
$jsonparams = json_decode($params);
|
||||||
|
|
||||||
|
if($mformclassname::security_validate($params)){
|
||||||
|
$mform = new $mformclassname($ajaxformdata);
|
||||||
|
$mform->set_data($mformclassname::init_data($jsonparams));
|
||||||
|
return $mform;
|
||||||
|
} else {
|
||||||
|
throw new \moodle_exception('accessexception', 'core');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns description of get_mform parameters
|
||||||
|
*
|
||||||
|
* @return \external_function_parameters
|
||||||
|
* @since Moodle 3.1
|
||||||
|
*/
|
||||||
|
public static function get_mform_parameters() {
|
||||||
|
return new \external_function_parameters([
|
||||||
|
'formname' => new \external_value(PARAM_COMPONENT, 'name of the treestudyplan form to parse'),
|
||||||
|
'params' => new \external_value(PARAM_RAW, 'JSON encoded parameters for form initialization'),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a HTML of a studyplan mform(s) via AJAX to insert into html
|
||||||
|
*
|
||||||
|
* @param string $formname Name of the form.
|
||||||
|
* @param array $params Params needed to properly initialize the form
|
||||||
|
* @return array HTML and JavaScript fragments for insertion into stuff.
|
||||||
|
*/
|
||||||
|
public static function get_mform($formname, $params = '') {
|
||||||
|
global $OUTPUT, $PAGE, $CFG;
|
||||||
|
|
||||||
|
// Hack alert: Set a default URL to stop the annoying debug.
|
||||||
|
$PAGE->set_url('/');
|
||||||
|
// Hack alert: Forcing bootstrap_renderer to initiate moodle page.
|
||||||
|
$OUTPUT->header();
|
||||||
|
|
||||||
|
// Overwriting page_requirements_manager with the fragment one so only JS included from.
|
||||||
|
// this point is returned to the user.
|
||||||
|
$PAGE->start_collecting_javascript_requirements();
|
||||||
|
|
||||||
|
$mform = self::load_mform($formname, $params);
|
||||||
|
$html = $mform->render();
|
||||||
|
|
||||||
|
$jsfooter = $PAGE->requires->get_end_code();
|
||||||
|
$output = ['html' => $html, 'javascript' => $jsfooter];
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns description of get_mform() result value
|
||||||
|
*
|
||||||
|
* @return \core_external\external_description
|
||||||
|
* @since Moodle 3.1
|
||||||
|
*/
|
||||||
|
public static function get_mform_returns() {
|
||||||
|
return new \external_single_structure(
|
||||||
|
[
|
||||||
|
'html' => new \external_value(PARAM_RAW, 'HTML fragment.'),
|
||||||
|
'javascript' => new \external_value(PARAM_RAW, 'JavaScript fragment')
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Parameter description for webservice function submit_cm_editform
|
||||||
|
*/
|
||||||
|
public static function submit_mform_parameters() : \external_function_parameters {
|
||||||
|
return new \external_function_parameters( [
|
||||||
|
'formname' => new \external_value(PARAM_COMPONENT, 'name of the treestudyplan form to parse'),
|
||||||
|
'params' => new \external_value(PARAM_RAW, 'JSON encoded parameters for form initialization'),
|
||||||
|
"formdata" => new \external_value(PARAM_RAW, 'url encoded form data'),
|
||||||
|
] );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return value description for webservice function submit_cm_editform
|
||||||
|
*/
|
||||||
|
public static function submit_mform_returns() : \external_description {
|
||||||
|
return success::structure();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Submit specified form data into form
|
||||||
|
* @param string $formname Name of the form.
|
||||||
|
* @param array $params Params needed to properly initialize the form
|
||||||
|
* @param mixed $formdata
|
||||||
|
* @return array Success/fail structure
|
||||||
|
*/
|
||||||
|
public static function submit_mform($formname, $params, $formdata) {
|
||||||
|
// Load the form, provide submitted form data and perform security checks
|
||||||
|
$mform = self::load_mform($formname, $params, $formdata);
|
||||||
|
|
||||||
|
if ($mform->process_submission()) {
|
||||||
|
return success::success()->model();
|
||||||
|
} else {
|
||||||
|
return success::fail("Error in submission data")->model();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -591,4 +591,22 @@ $functions = [
|
||||||
'ajax' => true,
|
'ajax' => true,
|
||||||
'loginrequired' => true,
|
'loginrequired' => true,
|
||||||
],
|
],
|
||||||
|
'local_treestudyplan_get_mform' => [ // Web service function name.
|
||||||
|
'classname' => '\local_treestudyplan\utilityservice', // Class containing the external function.
|
||||||
|
'methodname' => 'get_mform', // External function name.
|
||||||
|
'description' => 'Get a studyplan specific form',
|
||||||
|
'type' => 'read', // Database rights of the web service function (read, write).
|
||||||
|
'capabilities' => 'local/treestudyplan:editstudyplan',
|
||||||
|
'ajax' => true,
|
||||||
|
'loginrequired' => true,
|
||||||
|
],
|
||||||
|
'local_treestudyplan_submit_mform' => [ // Web service function name.
|
||||||
|
'classname' => '\local_treestudyplan\utilityservice', // Class containing the external function.
|
||||||
|
'methodname' => 'submit_mform', // External function name.
|
||||||
|
'description' => 'Submit a studyplan specific form through ajax',
|
||||||
|
'type' => 'read', // Database rights of the web service function (read, write).
|
||||||
|
'capabilities' => 'local/treestudyplan:editstudyplan',
|
||||||
|
'ajax' => true,
|
||||||
|
'loginrequired' => true,
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|
|
@ -22,11 +22,11 @@
|
||||||
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 = 2023090701; // YYYYMMDDHH (year, month, day, iteration).
|
$plugin->version = 2023101900; // 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-RC1";
|
$plugin->release = "1.1.0-RC1";
|
||||||
$plugin->maturity = MATURITY_STABLE;
|
$plugin->maturity = MATURITY_BETA; /*MATURITY_STABLE;*/
|
||||||
|
|
||||||
// Supported from Moodle 3.11 to 4.1 (4.2 not yet tested).
|
// Supported from Moodle 3.11 to 4.1 (4.2 not yet tested).
|
||||||
$plugin->supported = [ 311, 401 ];
|
$plugin->supported = [ 311, 401 ];
|
||||||
|
|
Loading…
Reference in New Issue
Block a user