moodle_local_treestudyplan/classes/form/studyplan_fromtemplateform.php

191 lines
6.9 KiB
PHP
Raw Permalink Normal View History

2024-04-19 16:46:30 +02:00
<?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
*/
2024-04-19 16:46:30 +02:00
namespace local_treestudyplan\form;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/repository/lib.php');
use local_treestudyplan\studyplan;
use local_treestudyplan\contextinfo;
use local_treestudyplan\studyplanservice;
use local_treestudyplan\courseservice;
use local_treestudyplan\form\text_integer;
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
2024-04-19 16:46:30 +02:00
*/
class studyplan_fromtemplateform 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.
*
2024-04-19 16:46:30 +02:00
* @param object $params The parameters for form initialization
* @return array Form data based on parameters
2024-06-02 19:23:40 +02:00
*/
2024-04-19 16:46:30 +02:00
public static function init_customdata(object $params) {
$customdata = new stdClass;
$customdata->context = \context::instance_by_id($params->contextid);
2024-06-02 19:23:40 +02:00
return $customdata;
2024-04-19 16:46:30 +02:00
}
/**
* Generate form data from parameters
* Also validate parameters and access permissions here
2024-06-02 19:23:40 +02:00
*
2024-04-19 16:46:30 +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
*/
2024-04-19 16:46:30 +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.
2024-04-19 16:46:30 +02:00
The form API does seem needlessly convoluted in it's use, but we need the editor...
*/
$entry = new stdClass;
$entry->context_id = $customdata->context->id;
// 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()) {
2024-04-19 16:46:30 +02:00
$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.
2024-04-19 16:46:30 +02:00
$entry->periods = 4;
$entry->template_id = 0;
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-04-19 16:46:30 +02:00
2024-06-02 23:23:32 +02:00
// Define the form.
2024-04-19 16:46:30 +02:00
$templatelist = [];
2024-06-02 23:23:32 +02:00
foreach (studyplan::find_template() as $s) {
2024-04-19 16:46:30 +02:00
$c = (new contextinfo($s->context()))->model();
2024-06-02 19:23:40 +02:00
$templatelist[$s->id()] = implode(" / ", $c['path']) . " / " . $s->name();
2024-04-19 16:46:30 +02:00
}
if (count($templatelist) > 0) {
2024-06-02 19:23:40 +02:00
$mform->addElement('hidden', 'hastemplates', 'yes');
2024-04-19 16:46:30 +02:00
$field = 'template_id';
2024-06-02 19:23:40 +02:00
$mform->addElement('autocomplete', $field,
get_string('studyplan_fromtemplate', 'local_treestudyplan'),
$templatelist);
$mform->addRule($field, null, 'required', null, 'client');
2024-04-19 16:46:30 +02:00
$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');
2024-04-19 16:46:30 +02:00
$field = 'shortname';
2024-06-02 19:23:40 +02:00
$mform->addElement('text', $field,
get_string('studyplan_shortname', 'local_treestudyplan'),
2024-04-19 16:46:30 +02:00
[]);
$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 = [];
2024-06-02 23:23:32 +02:00
foreach (courseservice::list_available_categories('edit') as $c) {
2024-06-02 19:23:40 +02:00
$contextlist[$c['context_id']] = implode(" / ", $c['category']['path']);
}
2024-04-19 16:46:30 +02:00
$field = 'context_id';
2024-06-02 19:23:40 +02:00
$mform->addElement('autocomplete', $field,
get_string('studyplan_context', 'local_treestudyplan'),
$contextlist);
$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) {
// Only add these fields if the studyplans are timed.
$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');
}
} else {
2024-06-02 19:23:40 +02:00
$mform->addElement('hidden', 'hastemplates', 'no');
$field = 'warning';
2024-06-02 19:23:40 +02:00
$mform->addElement('static', $field,
get_string('warning', 'core'),
get_string('no_templates', 'local_treestudyplan')
);
}
2024-04-19 16:46:30 +02:00
}
2024-06-02 19:23:40 +02:00
/**
2024-04-19 16:46:30 +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 ($entry->hastemplates == "yes") {
// Find template study plan.
$template = studyplan::find_by_id($entry->template_id);
// Copy template plan.
2024-06-02 19:23:40 +02:00
$plan = $template->duplicate($entry->name, $entry->shortname, $entry->context_id, $entry->idnumber, $entry->startdate);
/* 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
return studyplanservice::clean_returnvalue(studyplan::simple_structure(), $plan->simple_model());
} else {
return null;
}
2024-04-19 16:46:30 +02:00
}
2024-06-02 23:23:32 +02:00
}