diff --git a/classes/local/forms/formbase.php b/classes/local/forms/formbase.php new file mode 100644 index 0000000..26c9022 --- /dev/null +++ b/classes/local/forms/formbase.php @@ -0,0 +1,45 @@ +_form; - // Activity. + // Define the form $mform->addElement('editor', 'activityeditor', get_string('activityeditor', 'assign'), array('rows' => 10), array('maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean' => true, 'context' => $this->context, 'subdirs' => true)); $mform->addHelpButton('activityeditor', 'activityeditor', 'assign'); $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() + { + + } + + } \ No newline at end of file diff --git a/classes/utilityservice.php b/classes/utilityservice.php new file mode 100644 index 0000000..a58447c --- /dev/null +++ b/classes/utilityservice.php @@ -0,0 +1,161 @@ +. +/** + * 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(); + } + } + + + +} diff --git a/db/services.php b/db/services.php index 3ec3465..38d93fd 100644 --- a/db/services.php +++ b/db/services.php @@ -591,4 +591,22 @@ $functions = [ 'ajax' => 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, + ], ]; diff --git a/version.php b/version.php index 062c370..518a42d 100644 --- a/version.php +++ b/version.php @@ -22,11 +22,11 @@ defined('MOODLE_INTERNAL') || die(); $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->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). $plugin->supported = [ 311, 401 ];