Import/Export fixes

This commit is contained in:
PMKuipers 2023-08-07 23:07:59 +02:00
parent 833f0fb261
commit 9a8c6feb41
4 changed files with 51 additions and 43 deletions

View File

@ -323,7 +323,7 @@ class studyline {
$translation = [];
foreach($children as $c)
{
$newchild = $c->duplicate($this);
$newchild = $c->duplicate($new);
$translation[$c->id()] = $newchild->id();
}
return $new;

View File

@ -49,10 +49,6 @@ class studyplan {
return $this->r->name;
}
public function startdate(){
return new \DateTime($this->r->startdate);
}
public function pages(){
// cached version of find_studyplan_children.
// (may be premature optimization, since
@ -174,10 +170,10 @@ class studyplan {
return $model;
}
public static function add($fields){
public static function add($fields,$bare=false){
global $CFG, $DB;
$addable = ['name','shortname','description','context_id','periods','startdate','enddate','aggregation','aggregation_config'];
$addable = ['name','shortname','description','context_id','aggregation','aggregation_config'];
$info = ['enddate' => null ];
foreach($addable as $f){
if(array_key_exists($f,$fields)){
@ -191,20 +187,25 @@ class studyplan {
// Start temporary skräpp code
// Add a single page and copy the names.This keeps the data sane until the upgrade to
// real page management is done
// On import, adding an empty page messes things up for now, so we have an option to skip this....
// TODO: Remove this when proper page management is implemented
$pageaddable = ['name','shortname','description','periods','startdate','enddate'];
$pageinfo = ['studyplan_id' => $id];
foreach($pageaddable as $f){
if(array_key_exists($f,$fields)){
if($f == "name"){
$pageinfo["fullname"] = $fields[$f];
} else {
$pageinfo[$f] = $fields[$f];
if(!$bare){
$pageaddable = ['name','shortname','description',];
$pageinfo = ['studyplan_id' => $id];
foreach($pageaddable as $f){
if(array_key_exists($f,$fields)){
if($f == "name"){
$pageinfo["fullname"] = $fields[$f];
} else {
$pageinfo[$f] = $fields[$f];
}
}
}
$page = studyplanpage::add($pageinfo);
$plan->page_cache = [$page];
}
$page = studyplanpage::add($pageinfo);
$plan->page_cache = [$page];
// End temporary skräpp code
return $plan;
@ -235,7 +236,7 @@ class studyplan {
if(count($this->pages()) == 1){
// update the info to the page as well
$page = $this->pages()[0];
$pageeditable = ['name','shortname','description','periods','startdate','enddate'];
$pageeditable = ['name','shortname','description'];
$pageinfo = [];
foreach($pageeditable as $f){
if(array_key_exists($f,$fields)){
@ -467,22 +468,19 @@ class studyplan {
public function duplicate($name,$shortname)
{
// First duplicate the studyplan structure
$new =studyplan::add([
$newplan =studyplan::add([
'name' => $name,
'shortname' => $shortname,
'description' => $this->r->description,
'slots' => $this->r->slots,
'startdate' => $this->r->startdate,
'enddate' => empty($this->r->enddate)?null:$this->r->enddate,
]);
// next, copy the studylines
foreach($this->pages() as $p){
$newchild = $p->duplicate($this);
$newchild = $p->duplicate($newplan);
}
return $new;
return $newplan;
}
public static function export_structure()
@ -510,9 +508,6 @@ class studyplan {
'name' => $this->r->name,
'shortname' => $this->r->shortname,
'description' => $this->r->description,
'slots' => $this->r->slots,
'startdate' => $this->r->startdate,
'enddate' => $this->r->enddate,
"aggregation" => $this->r->aggregation,
"aggregation_config" => json_decode($this->aggregator->config_string()),
'aggregation_info' => $this->aggregator->basic_model(),
@ -545,7 +540,7 @@ class studyplan {
$content["studyplan"]["context_id"] = $context_id;
// Create a new plan, based on the given parameters - this is the import studyplan part
$plan = self::add($content["studyplan"]);
$plan = self::add($content["studyplan"],true);
// Now import each page
return $plan->import_pages_model($content["studyplan"]["pages"]);

View File

@ -175,8 +175,8 @@ class studyplanpage {
}
}
if($DB->count_records('local_treestudyplan_line',['studyplan_id' => $this->id]) > 0){
return success::fail('cannot delete studyplan that still has studylines');
if($DB->count_records('local_treestudyplan_line',['page_id' => $this->id]) > 0){
return success::fail('cannot delete studyplan page that still has studylines');
}
else
{
@ -240,12 +240,13 @@ class studyplanpage {
return $new->simple_model();
}
public function duplicate($name,$shortname)
public function duplicate($new_studyplan)
{
// First duplicate the studyplan structure
$new = studyplanpage::add([
'fullname' => $name,
'shortname' => $shortname,
'studyplan_id' => $new_studyplan->id(),
'fullname' => $this->r->fullname,
'shortname' => $this->r->shortname,
'description' => $this->r->description,
'pages' => $this->r->pages,
'startdate' => $this->r->startdate,
@ -300,10 +301,10 @@ class studyplanpage {
$model = $this->editor_model();
$slots = intval($model["slots"]);
$periods = intval($model["periods"]);
// First line
$csv = "\"Studyline[{$slots}]\"";
for($i = 1; $i <= $slots; $i++){
$csv = "\"\"";
for($i = 1; $i <= $periods; $i++){
$name = $plist[$i]->shortname();
$csv .= ",\"{$name}\"";
}
@ -312,7 +313,7 @@ class studyplanpage {
foreach($model["studylines"] as $line){
// determine how many fields are simultaneous in the line at maximum
$maxlines = 1;
for($i = 1; $i <= $slots; $i++){
for($i = 1; $i <= $periods; $i++){
if(count($line["slots"]) > $i){
$ct = 0;
foreach($line["slots"][$i][studyline::SLOTSET_COMPETENCY] as $itm){
@ -329,7 +330,7 @@ class studyplanpage {
for($lct = 0; $lct < $maxlines; $lct++){
$csv .= "\"{$line["name"]}\"";
for($i = 1; $i <= $slots; $i++){
for($i = 1; $i <= $periods; $i++){
$filled = false;
if(count($line["slots"]) > $i){
$ct = 0;
@ -394,7 +395,7 @@ class studyplanpage {
public function export_model()
{
$model = [
'fullname' => $this->r->name,
'fullname' => $this->r->fullname,
'shortname' => $this->r->shortname,
'description' => $this->r->description,
'periods' => $this->r->periods,
@ -443,6 +444,9 @@ class studyplanpage {
else if($content["type"] == "studyplanpage" && $content["version"] >= 2.0){
return $this->import_studylines_model($content["page"]["studylines"]);
}
else if($content["type"] == "studyplan" && $content["version"] >= 2.0){
return $this->import_studylines_model($content["studyplan"]["pages"][0]["studylines"]);
}
else {
return false;
}
@ -475,7 +479,7 @@ class studyplanpage {
foreach($model as $ix => $linemodel){
$line = $this->find_studyline_by_shortname($linemodel["shortname"]);
if(empty($line)){
$linemodel["studyplan_id"] = $this->id;
$linemodel["page_id"] = $this->id;
$line = studyline::add($linemodel);
} else {
//$line->edit($linemodel); // Update the line with the settings from the imported file

View File

@ -955,8 +955,8 @@ class studyplanservice extends \external_api
$plan = studyplan::findById($studyplan_id);
if($format == "csv"){
// FIXME: MAke sure this gets called for the page instead of the studyplan
return $plan->pages()[0]->export_plan_csv();
// FIXME: Make sure this webservice function gets called for the page instead of the studyplan
return $plan->pages()[0]->export_page_csv();
}
else{
return $plan->export_plan();
@ -1200,6 +1200,7 @@ class studyplanservice extends \external_api
}
public static function course_period_timing($period_id, $course_id, $span=1){
global $DB;
$period = period::findById($period_id);
$periodnr = $period->period();
$page = $period->page();
@ -1226,6 +1227,14 @@ class studyplanservice extends \external_api
$pend = $periods[$periodnr + ($span - 1)];
}
// First disable the automatic end date option, since it messes with the timing
// Unfortunately there is still no default option to turn this off
$record = $DB->get_record("course_format_options",["courseid" => $course->id, "name" => "automaticenddate"]);
if($record && $record->value){
$record->value = false;
$DB->update_record("course_format_options",$record);
}
// Actually perform the timing changes, while also updating the module times
// Like what happens on a course "reset"
reset_course_userdata((object)[
@ -1237,7 +1246,7 @@ class studyplanservice extends \external_api
]);
// purge course cache so the dates are properly reflected
\course_modinfo::purge_course_cache($course->id);
return (new courseinfo($course->id))->editor_model();
} else {
// probably should return a nice message