Fixed lost context bug

This commit is contained in:
PMKuipers 2023-12-13 21:19:45 +01:00
parent 4ca2c875ef
commit 32bab2de5e
4 changed files with 12 additions and 7 deletions

View File

@ -110,7 +110,7 @@ class contextinfo {
* @param int $contextid Context id
* @return context The context
*/
private static function context_by_id($contextid): \context {
public static function context_by_id($contextid): \context {
if ($contextid <= 1) {
$contextid = 1;
}

View File

@ -192,13 +192,18 @@ class studyplan {
* @return \context
*/
public function context(): \context {
global $DB;
if (!isset($this->context)) {
try {
$this->context = contextinfo::by_id($this->r->context_id)->context;
$this->context = contextinfo::context_by_id($this->r->context_id);
} catch (\dml_missing_record_exception $x) {
// Just throw it up again. catch is included here to make sure we know it throws this exception.
throw new \InvalidArgumentException(
"Context {$this->r->context_id} not available");
/* The associated context cannot be found.
Probably the category was removed, but the studyplan was not.
Revert the studyplan back to the system context to avoid lost studyplans.
*/
$this->context = \context_system::instance();
$this->r->context_id = $this->context->id;
$DB->update_record(self::TABLE, $this->r);
}
}
return $this->context;

View File

@ -557,7 +557,7 @@ $functions = [
'loginrequired' => true,
],
'local_treestudyplan_bulk_course_timing' => [ // Web service function name.
'classname' => '\local_treestudyplan\utilityservice', // Class containing the external function.
'classname' => '\local_treestudyplan\studyplanservice', // Class containing the external function.
'methodname' => 'bulk_course_timing', // External function name.
'description' => 'Change course start/end dates to match that of the studyplan period',
'type' => 'write', // Database rights of the web service function (read, write).

View File

@ -22,7 +22,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->component = 'local_treestudyplan'; // Recommended since 2.0.2 (MDL-26035). Required since 3.0 (MDL-48494).
$plugin->version = 2023121302; // YYYYMMDDHH (year, month, day, iteration).
$plugin->version = 2023121304; // YYYYMMDDHH (year, month, day, iteration).
$plugin->requires = 2021051700; // YYYYMMDDHH (This is the release version for Moodle 3.11).
$plugin->release = "1.1.0";