moodle_local_treestudyplan/classes/form/studyplan_editform.php

406 lines
16 KiB
PHP
Raw Normal View History

<?php
2024-06-02 23:23:32 +02:00
// 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/>.
2024-07-19 12:31:26 +02:00
2024-06-02 23:23:32 +02:00
/**
* No file description
* @package local_treestudyplan
* @copyright 2023 P.M. Kuipers
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_treestudyplan\form;
2024-01-26 12:26:01 +01:00
defined('MOODLE_INTERNAL') || die();
2024-01-26 12:26:01 +01:00
require_once($CFG->dirroot.'/repository/lib.php');
use local_treestudyplan\aggregator;
2023-10-19 17:48:43 +02:00
use local_treestudyplan\studyplan;
2024-05-10 15:22:52 +02:00
use local_treestudyplan\debug;
use local_treestudyplan\studyplanservice;
use local_treestudyplan\courseservice;
use local_treestudyplan\contextinfo;
use local_treestudyplan\form\text_integer;
2023-10-21 23:24:25 +02:00
use moodle_exception;
2023-10-20 14:03:13 +02:00
use stdClass;
/**
2024-06-02 19:23:40 +02:00
* 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 {
2023-10-19 17:48:43 +02:00
/**
* Capability required to edit study plans
* @var string
*/
const CAP_EDIT = "local/treestudyplan:editstudyplan";
2023-10-20 14:03:13 +02:00
/**
2024-06-02 19:23:40 +02:00
* Translate parameters into customdata.
*
2023-10-19 17:48:43 +02:00
* @param object $params The parameters for form initialization
2023-10-20 14:03:13 +02:00
* @return array Form data based on parameters
2024-06-02 19:23:40 +02:00
*/
2023-10-20 14:03:13 +02:00
public static function init_customdata(object $params) {
$customdata = new stdClass;
2024-06-02 23:23:32 +02:00
$customdata->create = $params->mode == 'create' ? true : false;
2024-06-02 19:23:40 +02:00
if ($customdata->create) {
$customdata->context = \context::instance_by_id($params->contextid);
} else {
2024-06-02 19:23:40 +02:00
$customdata->plan = studyplan::find_by_id($params->studyplan_id);
$customdata->context = $customdata->plan->context();
$customdata->simplemodel = $customdata->plan->simple_model();
}
2023-10-20 14:03:13 +02:00
$customdata->editoroptions = [
'trusttext' => true,
'subdirs' => true,
'maxfiles' => 20,
2024-06-02 23:23:32 +02:00
'maxbytes' => 20 * 1024 * 1024,
'context' => \context_system::instance(), // Keep the files in system context.
];
$customdata->fileoptions = [
'subdirs' => 0,
2024-06-02 23:23:32 +02:00
'maxbytes' => 10 * 1024 * 1024, // Max 10MiB should be sufficient for a picture.
'areamaxbytes' => 10485760,
2024-06-02 23:23:32 +02:00
'maxfiles' => 1, // Just one file.
2023-12-13 12:22:34 +01:00
'accepted_types' => ['.jpg', '.png', '.jpeg', '.svg', '.svgz'],
2024-01-26 12:26:01 +01:00
'return_types' => \FILE_INTERNAL | \FILE_EXTERNAL,
2023-10-20 14:03:13 +02:00
];
2024-06-02 19:23:40 +02:00
return $customdata;
2023-10-20 14:03:13 +02:00
}
/**
* Generate form data from parameters
* Also validate parameters and access permissions here
2024-06-02 19:23:40 +02:00
*
2023-10-20 14:03:13 +02:00
* @param object $customdata The parameters for form initialization
* @return array Form data based on parameters
2024-06-02 19:23:40 +02:00
*/
2023-10-20 14:03:13 +02:00
public function init_formdata(object $customdata) {
global $DB;
/* Use direct database retrieval to avoid our abstractions causing trouble
2024-06-02 19:23:40 +02:00
with existing moodle code assumptions.
2023-10-20 14:03:13 +02:00
The form API does seem needlessly convoluted in it's use, but we need the editor...
*/
2024-06-02 19:23:40 +02:00
if ($customdata->create) {
$entry = new stdClass;
$entry->context_id = $customdata->context->id;
2024-06-02 19:23:40 +02:00
$entry->aggregation = get_config("local_treestudyplan", "aggregation_mode");
$agcfg = json_decode(aggregator::create($entry->aggregation, "")->config_string(), true);
// Determine the next august 1st for default value purposes.
$august = strtotime("first day of august this year");
2024-06-02 19:23:40 +02:00
if ($august < time()) {
$august = strtotime("first day of august next year");
}
$entry->startdate = $august;
2024-06-02 23:23:32 +02:00
$entry->enddate = $august + (364 * 24 * 60 * 60); // Not bothering about leap years here.
$entry->periods = 4;
} else {
$entry = $DB->get_record(studyplan::TABLE, ['id' => $customdata->plan->id()]);
$entry->startdate = strtotime($customdata->simplemodel['pages'][0]['startdate']);
$entry->enddate = strtotime($customdata->simplemodel['pages'][0]['enddate']);
$entry->periods = $customdata->simplemodel['pages'][0]['periods'];
2024-06-02 19:23:40 +02:00
$agcfg = json_decode($customdata->plan->aggregator()->config_string(), true);
}
2024-06-02 23:23:32 +02:00
// Prepare the editor.
2023-10-20 14:03:13 +02:00
$entry = file_prepare_standard_editor( $entry,
'description',
$customdata->editoroptions,
\context_system::instance(),
'local_treestudyplan',
'studyplan',
2024-06-02 19:23:40 +02:00
($customdata->create) ? null : $customdata->plan->id()
);
2024-06-02 23:23:32 +02:00
// Prepare file area for the icon.
// Get an unused draft itemid which will be used for this form.
$draftitemid = file_get_submitted_draft_itemid('icon');
file_prepare_draft_area(
// The $draftitemid is the target location.
$draftitemid,
2024-06-02 23:23:32 +02:00
// The combination of contextid / component / filearea / itemid.
// Form the virtual bucket that files are currently stored in.
// And will be copied from.
\context_system::instance()->id,
'local_treestudyplan',
'icon',
2024-06-02 19:23:40 +02:00
($customdata->create) ? null : $customdata->plan->id(),
$customdata->fileoptions
);
$entry->icon = $draftitemid;
// Add aggregation configs to entry.
2024-06-02 19:23:40 +02:00
foreach ($agcfg as $key => $val) {
$entrykey = $entry->aggregation."_".$key;
$entry->$entrykey = $val;
}
2023-10-20 14:03:13 +02:00
return $entry;
}
/**
* Set up the form definition
*/
public function definition() {
$mform = $this->_form;
2023-10-20 14:03:13 +02:00
$customdata = (object)$this->_customdata;
2024-06-02 23:23:32 +02:00
// Register integer type.
text_integer::register();
2024-06-02 23:23:32 +02:00
// Define the form.
$field = 'name';
2024-06-02 19:23:40 +02:00
$mform->addElement('text', $field,
get_string('studyplan_name', 'local_treestudyplan'),
[]);
$mform->addRule($field, null, 'required', null, 'client');
$field = 'shortname';
2024-06-02 19:23:40 +02:00
$mform->addElement('text', $field,
get_string('studyplan_shortname', 'local_treestudyplan'),
[]);
$mform->addRule($field, null, 'required', null, 'client');
$field = 'idnumber';
2024-06-02 19:23:40 +02:00
$mform->addElement('text', $field,
get_string('studyplan_idnumber', 'local_treestudyplan'),
[]);
$contextlist = [];
// Add system if the user has permissions.
2024-11-01 12:43:44 +01:00
if (has_all_capabilities([courseservice::CAP_EDIT, 'moodle/category:viewcourselist'], \context_system::instance())) {
$contextlist[1] = get_string("coresystem");
}
// Add any other contexts the user has access to.
foreach (courseservice::categories_by_capability(courseservice::CAP_EDIT) as $c) {
$ctx = $c->get_context();
$ci = new contextinfo($ctx);
$contextlist[$ctx->id] = implode(" / ", $ci->path(false));
}
2023-12-12 23:44:02 +01:00
2024-06-02 19:23:40 +02:00
$mform->addElement('autocomplete', 'context_id',
get_string('studyplan_context', 'local_treestudyplan'),
$contextlist);
$mform->addRule('context_id', null, 'required', null, 'client');
$mform->addElement(
'filemanager',
'icon',
get_string('studyplan_icon', 'local_treestudyplan'),
null,
$customdata->fileoptions
);
if ($customdata->create) {
2024-06-02 19:23:40 +02:00
$timeless = \get_config("local_treestudyplan", "timelessperiods");
2024-06-02 23:23:32 +02:00
if (!$timeless) {
// Only add these fields if a new studyplan is created, to easily initialize the first page.
$field = 'startdate';
2024-06-02 19:23:40 +02:00
$mform->addElement('date_selector', $field,
get_string('studyplan_startdate', 'local_treestudyplan'),
[]);
$mform->addRule($field, null, 'required', null, 'client');
$field = 'enddate';
2024-06-02 19:23:40 +02:00
$mform->addElement('date_selector', $field,
get_string('studyplan_enddate', 'local_treestudyplan'),
[]);
$mform->addRule($field, null, 'required', null, 'client');
2024-06-02 19:23:40 +02:00
}
$field = 'periods';
2024-06-02 19:23:40 +02:00
$mform->addElement('text_integer', $field,
get_string('studyplan_slots', 'local_treestudyplan'),
["unsigned" => true]);
$mform->setType($field, PARAM_INT);
$mform->addRule($field, null, 'required', null, 'client');
2024-03-10 15:56:35 +01:00
} else {
2024-06-02 23:23:32 +02:00
// These fields are only relevant if the studyplan is edited.
2024-03-10 15:56:35 +01:00
$field = 'suspended';
2024-06-02 19:23:40 +02:00
$mform->addElement('advcheckbox', $field,
get_string('studyplan_suspend', 'local_treestudyplan'),
get_string('studyplan_suspend_details', 'local_treestudyplan'),
2024-03-10 15:56:35 +01:00
[],
);
2024-04-19 16:46:30 +02:00
$field = 'template';
$mform->addElement('advcheckbox', $field,
get_string('studyplan_template', 'local_treestudyplan'),
get_string('studyplan_template_details', 'local_treestudyplan'),
[],
);
}
$aggregators = [];
2024-06-02 23:23:32 +02:00
foreach (aggregator::list_model() as $a) {
// Add method only if not deprecated or currently used.
if ($customdata->simplemodel['aggregation'] == $a['id'] || !($a['deprecated'])) {
$aggregators[$a['id']] = $a['name'];
}
}
2024-04-19 16:46:30 +02:00
$field = 'aggregation';
2024-06-02 19:23:40 +02:00
$mform->addElement('select', $field,
get_string('choose_aggregation_style', 'local_treestudyplan'),
$aggregators);
/* Start Bistate aggregation specific items */
$field = 'bistate_thresh_excellent';
2024-06-02 19:23:40 +02:00
$mform->addElement('text_integer', $field,
get_string('setting_bistate_thresh_excellent', 'local_treestudyplan'),
["unsigned" => false],
);
$mform->setType($field, PARAM_INT);
2024-06-02 19:23:40 +02:00
$mform->hideif ($field, "aggregation", "neq", "bistate");
$field = 'bistate_thresh_good';
2024-06-02 19:23:40 +02:00
$mform->addElement('text_integer', $field,
get_string('setting_bistate_thresh_good', 'local_treestudyplan'),
["unsigned" => true],
);
$mform->setType($field, PARAM_INT);
2024-06-02 19:23:40 +02:00
$mform->hideif ($field, "aggregation", "neq", "bistate");
$field = 'bistate_thresh_completed';
2024-06-02 19:23:40 +02:00
$mform->addElement('text_integer', $field,
get_string('setting_bistate_thresh_completed', 'local_treestudyplan'),
["unsigned" => true],
);
$mform->setType($field, PARAM_INT);
2024-06-02 19:23:40 +02:00
$mform->hideif ($field, "aggregation", "neq", "bistate");
$field = 'bistate_use_failed';
2024-06-02 19:23:40 +02:00
$mform->addElement('checkbox', $field,
get_string('setting_bistate_support_failed', 'local_treestudyplan'),
[],
);
2024-06-02 19:23:40 +02:00
$mform->hideif ($field, "aggregation", "neq", "bistate");
$field = 'bistate_accept_pending_as_submitted';
2024-06-02 19:23:40 +02:00
$mform->addElement('checkbox', $field,
get_string('setting_bistate_accept_pending_submitted', 'local_treestudyplan'),
[],
);
2024-06-02 19:23:40 +02:00
$mform->hideif ($field, "aggregation", "neq", "bistate");
/* End Bistate aggregation specific items */
2023-10-20 14:03:13 +02:00
$mform->addElement('editor', 'description_editor',
2024-06-02 19:23:40 +02:00
get_string('studyplan_description', 'local_treestudyplan'),
null,
2023-10-20 14:03:13 +02:00
$customdata->editoroptions);
$mform->setType('description_editor', PARAM_RAW);
}
2024-06-02 19:23:40 +02:00
/**
2023-10-20 14:03:13 +02:00
* Process the submitted data and perform necessary actions
* @param object $entry The processed form data;
2023-10-21 23:24:25 +02:00
* @return bool false if submission not successful
* @throws \moodle_exception if an error must be given for a specific reason.
*/
2023-10-20 14:03:13 +02:00
protected function process_submitted_data($entry) {
$customdata = (object)$this->_customdata;
// Add aggregation configs to entry.
2024-06-02 23:23:32 +02:00
// Retrieve default config string from selected aggregation method.
$agcfg = json_decode(aggregator::create($entry->aggregation, "")->config_string(), true);
2024-06-02 19:23:40 +02:00
foreach ($agcfg as $key => $val) {
$entrykey = $entry->aggregation."_".$key;
2024-06-02 19:23:40 +02:00
$agcfg[$key] = $entry->$entrykey;
}
2024-06-02 19:23:40 +02:00
$aggregationconfig = json_encode($agcfg);
2024-04-19 16:46:30 +02:00
if ($customdata->create) {
2024-06-02 23:23:32 +02:00
// Use our own abstraction to create the record, so caches are maintained.
$plan = studyplan::add(['name' => $entry->name,
'shortname' => $entry->shortname,
'idnumber' => $entry->idnumber,
2023-12-12 23:44:02 +01:00
'context_id' => $entry->context_id,
'aggregation' => $entry->aggregation,
2024-06-02 19:23:40 +02:00
'aggregation_config' => $aggregationconfig,
'startdate' => date("Y-m-d", $entry->startdate),
'enddate' => date("Y-m-d", $entry->enddate),
'periods' => $entry->periods,
]);
2024-06-02 23:23:32 +02:00
// Process the provided files in the description editor.
$entry = file_postupdate_standard_editor($entry,
'description',
$customdata->editoroptions,
\context_system::instance(),
'local_treestudyplan',
'studyplan',
2024-06-02 19:23:40 +02:00
$plan->id());
2024-06-02 23:23:32 +02:00
// Update the description.
$plan->edit([
'description' => $entry->description,
'descriptionformat' => $entry->descriptionformat,
]);
} else {
$plan = $customdata->plan;
2024-06-02 19:23:40 +02:00
2024-06-02 23:23:32 +02:00
// Process the provided files in the description editor.
$entry = file_postupdate_standard_editor($entry,
'description',
$customdata->editoroptions,
\context_system::instance(),
'local_treestudyplan',
'studyplan',
$plan->id());
2024-06-02 23:23:32 +02:00
// Use our own abstraction to update the record, so caches are maintained.
$plan->edit(['name' => $entry->name,
'shortname' => $entry->shortname,
'idnumber' => $entry->idnumber,
2023-12-12 23:44:02 +01:00
'context_id' => $entry->context_id,
'description' => $entry->description,
'descriptionformat' => $entry->descriptionformat,
'aggregation' => $entry->aggregation,
2024-06-02 19:23:40 +02:00
'aggregation_config' => $aggregationconfig,
2024-03-10 15:56:35 +01:00
'suspended' => $entry->suspended,
2024-04-19 16:46:30 +02:00
'template' => $entry->template,
]);
}
2023-10-21 23:24:25 +02:00
// Now save the icon file in correct part of the File API.
file_save_draft_area_files(
// The $entry->icon property contains the itemid of the draft file area.
$entry->icon,
2024-06-02 23:23:32 +02:00
// The combination of contextid / component / filearea / itemid.
// Form the virtual bucket that file are stored in.
\context_system::instance()->id,
'local_treestudyplan',
'icon',
$plan->id(),
$customdata->fileoptions
);
/* Return the simple model of the plan to make sure we can update stuff.
Parse it through the clean_returnvalue function of exernal api (of which studyplanservice is a subclass)
2024-06-02 19:23:40 +02:00
so we return it in a consistent way
*/
2024-06-02 19:23:40 +02:00
$response = studyplanservice::clean_returnvalue(studyplan::simple_structure(), $plan->simple_model());
2024-05-10 15:22:52 +02:00
debug::dump($response);
return $response;
}
2024-06-02 23:23:32 +02:00
}