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 = []; $translation = [];
foreach($children as $c) foreach($children as $c)
{ {
$newchild = $c->duplicate($this); $newchild = $c->duplicate($new);
$translation[$c->id()] = $newchild->id(); $translation[$c->id()] = $newchild->id();
} }
return $new; return $new;

View file

@ -49,10 +49,6 @@ class studyplan {
return $this->r->name; return $this->r->name;
} }
public function startdate(){
return new \DateTime($this->r->startdate);
}
public function pages(){ public function pages(){
// cached version of find_studyplan_children. // cached version of find_studyplan_children.
// (may be premature optimization, since // (may be premature optimization, since
@ -174,10 +170,10 @@ class studyplan {
return $model; return $model;
} }
public static function add($fields){ public static function add($fields,$bare=false){
global $CFG, $DB; 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 ]; $info = ['enddate' => null ];
foreach($addable as $f){ foreach($addable as $f){
if(array_key_exists($f,$fields)){ if(array_key_exists($f,$fields)){
@ -191,20 +187,25 @@ class studyplan {
// Start temporary skräpp code // Start temporary skräpp code
// Add a single page and copy the names.This keeps the data sane until the upgrade to // Add a single page and copy the names.This keeps the data sane until the upgrade to
// real page management is done // 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 // TODO: Remove this when proper page management is implemented
$pageaddable = ['name','shortname','description','periods','startdate','enddate']; if(!$bare){
$pageinfo = ['studyplan_id' => $id];
foreach($pageaddable as $f){ $pageaddable = ['name','shortname','description',];
if(array_key_exists($f,$fields)){ $pageinfo = ['studyplan_id' => $id];
if($f == "name"){ foreach($pageaddable as $f){
$pageinfo["fullname"] = $fields[$f]; if(array_key_exists($f,$fields)){
} else { if($f == "name"){
$pageinfo[$f] = $fields[$f]; $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 // End temporary skräpp code
return $plan; return $plan;
@ -235,7 +236,7 @@ class studyplan {
if(count($this->pages()) == 1){ if(count($this->pages()) == 1){
// update the info to the page as well // update the info to the page as well
$page = $this->pages()[0]; $page = $this->pages()[0];
$pageeditable = ['name','shortname','description','periods','startdate','enddate']; $pageeditable = ['name','shortname','description'];
$pageinfo = []; $pageinfo = [];
foreach($pageeditable as $f){ foreach($pageeditable as $f){
if(array_key_exists($f,$fields)){ if(array_key_exists($f,$fields)){
@ -467,22 +468,19 @@ class studyplan {
public function duplicate($name,$shortname) public function duplicate($name,$shortname)
{ {
// First duplicate the studyplan structure // First duplicate the studyplan structure
$new =studyplan::add([ $newplan =studyplan::add([
'name' => $name, 'name' => $name,
'shortname' => $shortname, 'shortname' => $shortname,
'description' => $this->r->description, '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 // next, copy the studylines
foreach($this->pages() as $p){ foreach($this->pages() as $p){
$newchild = $p->duplicate($this); $newchild = $p->duplicate($newplan);
} }
return $new; return $newplan;
} }
public static function export_structure() public static function export_structure()
@ -510,9 +508,6 @@ class studyplan {
'name' => $this->r->name, 'name' => $this->r->name,
'shortname' => $this->r->shortname, 'shortname' => $this->r->shortname,
'description' => $this->r->description, 'description' => $this->r->description,
'slots' => $this->r->slots,
'startdate' => $this->r->startdate,
'enddate' => $this->r->enddate,
"aggregation" => $this->r->aggregation, "aggregation" => $this->r->aggregation,
"aggregation_config" => json_decode($this->aggregator->config_string()), "aggregation_config" => json_decode($this->aggregator->config_string()),
'aggregation_info' => $this->aggregator->basic_model(), 'aggregation_info' => $this->aggregator->basic_model(),
@ -545,7 +540,7 @@ class studyplan {
$content["studyplan"]["context_id"] = $context_id; $content["studyplan"]["context_id"] = $context_id;
// Create a new plan, based on the given parameters - this is the import studyplan part // 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 // Now import each page
return $plan->import_pages_model($content["studyplan"]["pages"]); 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){ if($DB->count_records('local_treestudyplan_line',['page_id' => $this->id]) > 0){
return success::fail('cannot delete studyplan that still has studylines'); return success::fail('cannot delete studyplan page that still has studylines');
} }
else else
{ {
@ -240,12 +240,13 @@ class studyplanpage {
return $new->simple_model(); return $new->simple_model();
} }
public function duplicate($name,$shortname) public function duplicate($new_studyplan)
{ {
// First duplicate the studyplan structure // First duplicate the studyplan structure
$new = studyplanpage::add([ $new = studyplanpage::add([
'fullname' => $name, 'studyplan_id' => $new_studyplan->id(),
'shortname' => $shortname, 'fullname' => $this->r->fullname,
'shortname' => $this->r->shortname,
'description' => $this->r->description, 'description' => $this->r->description,
'pages' => $this->r->pages, 'pages' => $this->r->pages,
'startdate' => $this->r->startdate, 'startdate' => $this->r->startdate,
@ -300,10 +301,10 @@ class studyplanpage {
$model = $this->editor_model(); $model = $this->editor_model();
$slots = intval($model["slots"]); $periods = intval($model["periods"]);
// First line // First line
$csv = "\"Studyline[{$slots}]\""; $csv = "\"\"";
for($i = 1; $i <= $slots; $i++){ for($i = 1; $i <= $periods; $i++){
$name = $plist[$i]->shortname(); $name = $plist[$i]->shortname();
$csv .= ",\"{$name}\""; $csv .= ",\"{$name}\"";
} }
@ -312,7 +313,7 @@ class studyplanpage {
foreach($model["studylines"] as $line){ foreach($model["studylines"] as $line){
// determine how many fields are simultaneous in the line at maximum // determine how many fields are simultaneous in the line at maximum
$maxlines = 1; $maxlines = 1;
for($i = 1; $i <= $slots; $i++){ for($i = 1; $i <= $periods; $i++){
if(count($line["slots"]) > $i){ if(count($line["slots"]) > $i){
$ct = 0; $ct = 0;
foreach($line["slots"][$i][studyline::SLOTSET_COMPETENCY] as $itm){ foreach($line["slots"][$i][studyline::SLOTSET_COMPETENCY] as $itm){
@ -329,7 +330,7 @@ class studyplanpage {
for($lct = 0; $lct < $maxlines; $lct++){ for($lct = 0; $lct < $maxlines; $lct++){
$csv .= "\"{$line["name"]}\""; $csv .= "\"{$line["name"]}\"";
for($i = 1; $i <= $slots; $i++){ for($i = 1; $i <= $periods; $i++){
$filled = false; $filled = false;
if(count($line["slots"]) > $i){ if(count($line["slots"]) > $i){
$ct = 0; $ct = 0;
@ -394,7 +395,7 @@ class studyplanpage {
public function export_model() public function export_model()
{ {
$model = [ $model = [
'fullname' => $this->r->name, 'fullname' => $this->r->fullname,
'shortname' => $this->r->shortname, 'shortname' => $this->r->shortname,
'description' => $this->r->description, 'description' => $this->r->description,
'periods' => $this->r->periods, 'periods' => $this->r->periods,
@ -443,6 +444,9 @@ class studyplanpage {
else if($content["type"] == "studyplanpage" && $content["version"] >= 2.0){ else if($content["type"] == "studyplanpage" && $content["version"] >= 2.0){
return $this->import_studylines_model($content["page"]["studylines"]); 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 { else {
return false; return false;
} }
@ -475,7 +479,7 @@ class studyplanpage {
foreach($model as $ix => $linemodel){ foreach($model as $ix => $linemodel){
$line = $this->find_studyline_by_shortname($linemodel["shortname"]); $line = $this->find_studyline_by_shortname($linemodel["shortname"]);
if(empty($line)){ if(empty($line)){
$linemodel["studyplan_id"] = $this->id; $linemodel["page_id"] = $this->id;
$line = studyline::add($linemodel); $line = studyline::add($linemodel);
} else { } else {
//$line->edit($linemodel); // Update the line with the settings from the imported file //$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); $plan = studyplan::findById($studyplan_id);
if($format == "csv"){ if($format == "csv"){
// FIXME: MAke sure this gets called for the page instead of the studyplan // FIXME: Make sure this webservice function gets called for the page instead of the studyplan
return $plan->pages()[0]->export_plan_csv(); return $plan->pages()[0]->export_page_csv();
} }
else{ else{
return $plan->export_plan(); 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){ public static function course_period_timing($period_id, $course_id, $span=1){
global $DB;
$period = period::findById($period_id); $period = period::findById($period_id);
$periodnr = $period->period(); $periodnr = $period->period();
$page = $period->page(); $page = $period->page();
@ -1226,6 +1227,14 @@ class studyplanservice extends \external_api
$pend = $periods[$periodnr + ($span - 1)]; $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 // Actually perform the timing changes, while also updating the module times
// Like what happens on a course "reset" // Like what happens on a course "reset"
reset_course_userdata((object)[ reset_course_userdata((object)[
@ -1237,7 +1246,7 @@ class studyplanservice extends \external_api
]); ]);
// purge course cache so the dates are properly reflected // purge course cache so the dates are properly reflected
\course_modinfo::purge_course_cache($course->id); \course_modinfo::purge_course_cache($course->id);
return (new courseinfo($course->id))->editor_model(); return (new courseinfo($course->id))->editor_model();
} else { } else {
// probably should return a nice message // probably should return a nice message