moodle_local_treestudyplan/classes/form/studyplanpage_editform.php

261 lines
11 KiB
PHP
Raw Permalink 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 23:42:28 +01:00
defined('MOODLE_INTERNAL') || die();
2024-01-26 23:42:28 +01:00
require_once($CFG->dirroot.'/repository/lib.php');
use local_treestudyplan\studyplan;
use local_treestudyplan\studyplanpage;
use local_treestudyplan\form\text_integer;
use local_treestudyplan\studyplanservice;
use moodle_exception;
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 studyplanpage_editform extends formbase {
/**
* Capability required to edit study plans
* @var string
*/
const CAP_EDIT = "local/treestudyplan:editstudyplan";
/**
2024-06-02 19:23:40 +02:00
* Translate parameters into customdata.
*
* @param object $params The parameters for form initialization
* @return array Form data based on parameters
2024-06-02 19:23:40 +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->plan = studyplan::find_by_id($params->studyplan_id);
} else {
$customdata->page = studyplanpage::find_by_id($params->page_id);
$customdata->plan = $customdata->page->studyplan();
$customdata->simplemodel = $customdata->page->simple_model();
}
$customdata->context = $customdata->plan->context();
$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.
'accepted_types' => ['.jpg', '.png'],
2024-01-26 23:42:28 +01:00
'return_types' => \FILE_INTERNAL | \FILE_EXTERNAL,
];
2024-06-02 19:23:40 +02:00
return $customdata;
}
/**
* Generate form data from parameters
* Also validate parameters and access permissions here
2024-06-02 19:23:40 +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
*/
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.
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) {
$plan = $customdata->plan;
$entry = new stdClass;
$entry->studyplan_id = $plan->id();
2024-06-02 23:23:32 +02:00
// By default, make the start date of a new page, continue 1 day after the last page's start date.
$otherpages = $plan->pages();
2024-06-02 19:23:40 +02:00
if (count($otherpages) > 0) {
2024-06-02 23:23:32 +02:00
$lastpage = $otherpages[count($otherpages) - 1];
// Propose 1 year after the last page's start date, if no end date is set.
if ($lastpage->enddate(false) == null) {
$entry->startdate = $lastpage->startdate()->add(new \DateInterval("P1Y"))->format("U");
} else {
2024-06-02 23:23:32 +02:00
// Otherwise, propose 1 day after the last page's end date.
$entry->startdate = $lastpage->enddate()->add(new \DateInterval("P1D"))->format("U");
}
} else {
2024-06-02 23:23:32 +02:00
// Determine the next august 1st for default value purposes. Only if no other page is available.
$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");
}
2024-06-02 19:23:40 +02:00
$entry->startdate = $august;
}
2024-06-02 23:23:32 +02:00
$entry->enddate = $entry->startdate + (364 * 24 * 60 * 60); // Not bothering with leap years here.
$entry->periods = 4;
} else {
$entry = $DB->get_record(studyplanpage::TABLE, ['id' => $customdata->page->id()]);
$entry->startdate = strtotime($entry->startdate);
$entry->enddate = strtotime($entry->enddate);
}
2024-06-02 23:23:32 +02:00
// Prepare the editor.
$entry = file_prepare_standard_editor( $entry,
'description',
$customdata->editoroptions,
\context_system::instance(),
'local_treestudyplan',
'studyplanpage',
2024-06-02 19:23:40 +02:00
($customdata->create) ? null : $customdata->page->id()
);
return $entry;
}
/**
* Set up the form definition
*/
public function definition() {
$mform = $this->_form;
$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 = 'fullname';
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');
2024-06-02 19:23:40 +02:00
$timeless = \get_config("local_treestudyplan", "timelessperiods");
2024-06-02 23:23:32 +02:00
if (!$timeless) {
$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_startdate', 'local_treestudyplan'),
[]);
$mform->addRule($field, null, 'required', null, 'client');
}
$field = 'periods';
2024-06-02 19:23:40 +02:00
$mform->addElement('text_integer', $field,
get_string( ($timeless) ? 'studyplan_slots' : 'studyplan_periods', 'local_treestudyplan'),
["unsigned" => true, "nonzero" => true]);
$mform->setType($field, PARAM_INT);
$mform->addRule($field, null, 'required', null, 'client');
$field = 'description_editor';
$mform->addElement('editor', $field,
2024-06-02 19:23:40 +02:00
get_string('studyplan_description', 'local_treestudyplan'),
null,
$customdata->editoroptions);
$mform->setType($field, PARAM_RAW);
}
2024-06-02 19:23:40 +02:00
/**
* Process the submitted data and perform necessary actions
* @param object $entry The processed form data;
* @return bool false if submission not successful
* @throws \moodle_exception if an error must be given for a specific reason.
*/
protected function process_submitted_data($entry) {
$customdata = (object)$this->_customdata;
2024-06-02 19:23:40 +02:00
if ($customdata->create) {
2024-06-02 23:23:32 +02:00
// Use our own abstraction to update the record, so caches are maintained.
$page = studyplanpage::add(['fullname' => $entry->fullname,
'shortname' => $entry->shortname,
2024-06-02 19:23:40 +02:00
'startdate' => date("Y-m-d", $entry->startdate),
'enddate' => date("Y-m-d", $entry->enddate),
'periods' => $entry->periods,
'studyplan_id' => $customdata->plan->id(),
]);
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',
2024-03-09 22:51:34 +01:00
'studyplanpage',
2024-06-02 19:23:40 +02:00
$page->id());
2024-06-02 23:23:32 +02:00
// Update the description.
$page->edit([
'description' => $entry->description,
'descriptionformat' => $entry->descriptionformat,
]);
} else {
$page = $customdata->page;
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',
'studyplanpage',
$page->id());
2024-06-02 23:23:32 +02:00
// Use our own abstraction to update the record, so caches are maintained.
$page->edit(['fullname' => $entry->fullname,
'shortname' => $entry->shortname,
'description' => $entry->description,
'descriptionformat' => $entry->descriptionformat,
2024-06-02 19:23:40 +02:00
'startdate' => date("Y-m-d", $entry->startdate),
'enddate' => date("Y-m-d", $entry->enddate),
'periods' => $entry->periods,
]);
}
/* Return the editor structure of the new / edited page
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
return studyplanservice::clean_returnvalue(studyplanpage::editor_structure(), $page->editor_model());
}
2024-06-02 23:23:32 +02:00
}