diff --git a/amd/src/page-edit-plan.js b/amd/src/page-edit-plan.js index 4ca4a16..2fea2bc 100644 --- a/amd/src/page-edit-plan.js +++ b/amd/src/page-edit-plan.js @@ -230,6 +230,7 @@ export function init(contextid,categoryid) { args: { content: content, format: "application/json", + context_id: contextid, }, }])[0].done(function(response){ if(response.success){ diff --git a/classes/local/gradegenerator.php b/classes/local/gradegenerator.php index eea746b..278d57c 100644 --- a/classes/local/gradegenerator.php +++ b/classes/local/gradegenerator.php @@ -262,10 +262,12 @@ class gradegenerator { } public function toFile(string $filename){ + $filename = self::expand_tilde($filename); file_put_contents($filename,$this->serialize()); } public function fromFile(string $filename){ + $filename = self::expand_tilde($filename); if(file_exists($filename)){ try{ $json = file_get_contents($filename); @@ -277,4 +279,15 @@ class gradegenerator { } } + private static function expand_tilde($path) + { + if (function_exists('posix_getuid') && strpos($path, '~') !== false) { + $info = posix_getpwuid(posix_getuid()); + $path = str_replace('~', $info['dir'], $path); + } + + return $path; + } + + } diff --git a/classes/studyplan.php b/classes/studyplan.php index 1b833ad..ba397ed 100644 --- a/classes/studyplan.php +++ b/classes/studyplan.php @@ -242,18 +242,23 @@ class studyplan { } } - public static function find_all($contextid=1){ + public static function find_all($contextid=-1){ global $DB, $USER; $list = []; - - if($contextid <= 1){ - $contextid = 1; - $where = "context_id <= :contextid OR context_id IS NULL"; - } else { - $where = "context_id = :contextid"; + + if($contextid <= 0){ + $ids = $DB->get_fieldset_select(self::TABLE,"id",""); } - - $ids = $DB->get_fieldset_select(self::TABLE,"id",$where,["contextid" => $contextid]); + else{ + if($contextid == 1){ + $contextid = 1; + $where = "context_id <= :contextid OR context_id IS NULL"; + } else { + $where = "context_id = :contextid"; + } + $ids = $DB->get_fieldset_select(self::TABLE,"id",$where,["contextid" => $contextid]); + } + foreach($ids as $id) { $list[] = studyplan::findById($id); @@ -638,7 +643,7 @@ class studyplan { return $lines; } - public static function import_studyplan($content,$format="application/json") + public static function import_studyplan($content,$format="application/json",$context_id=1) { if($format != "application/json") { return false;} @@ -648,6 +653,8 @@ class studyplan { // Make sure the aggregation_config is re-encoded as json text $content["studyplan"]["aggregation_config"] = json_encode($content["studyplan"]["aggregation_config"]); + // And make sure the context_id is set to the + $content["studyplan"]["context_id"] = $context_id; $plan = self::add($content["studyplan"]); return $plan->import_studylines_model($content["studyplan"]["studylines"]); diff --git a/classes/studyplanservice.php b/classes/studyplanservice.php index 2115037..d129652 100644 --- a/classes/studyplanservice.php +++ b/classes/studyplanservice.php @@ -1005,7 +1005,7 @@ class studyplanservice extends \external_api return success::structure(); } - public static function import_plan($content,$format="application/json",$context_id=0) + public static function import_plan($content,$format="application/json",$context_id=1) { try{ @@ -1013,7 +1013,7 @@ class studyplanservice extends \external_api $context = webservicehelper::find_context($context_id); webservicehelper::require_capabilities(self::CAP_EDIT,$context); - $result = studyplan::import_studyplan($content,$format); + $result = studyplan::import_studyplan($content,$format,$context_id); return (new success($result, "During study plan import"))->model(); } catch(\webservice_access_exception $x) { diff --git a/cli/prime_students.php b/cli/prime_students.php index c06e7c5..e5648d3 100644 --- a/cli/prime_students.php +++ b/cli/prime_students.php @@ -46,7 +46,7 @@ if (empty($options['studyplan']) && empty($options["all"])) { } if(!empty($options["all"])){ - $plans = studyplan::find_all(); + $plans = studyplan::find_all(); } else { $plans = studyplan::find_by_shortname($options["studyplan"]); }