diff --git a/classes/local/model/model_item_param.php b/classes/local/model/model_item_param.php index 5e3209201..828bf1c87 100644 --- a/classes/local/model/model_item_param.php +++ b/classes/local/model/model_item_param.php @@ -273,6 +273,16 @@ public function get_componentid(): string { return $this->componentid; } + /** + * Sets the component ID (e.g. question id). + * + * @return self + */ + public function set_componentid(string $componentid): self { + $this->componentid = $componentid; + return $this; + } + /** * Return name of model. * diff --git a/classes/local/model/model_item_param_list.php b/classes/local/model/model_item_param_list.php index fe9b23ead..ee4f8ebdf 100644 --- a/classes/local/model/model_item_param_list.php +++ b/classes/local/model/model_item_param_list.php @@ -38,6 +38,7 @@ use local_catquiz\data\catscale_structure; use local_catquiz\data\dataapi; use local_catquiz\event\testiteminscale_added; +use local_catquiz\remote\hash\question_hasher; use moodle_exception; use stdClass; use Traversable; @@ -55,7 +56,12 @@ * @copyright 2024 Wunderbyte GmbH * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class model_item_param_list implements ArrayAccess, IteratorAggregate, Countable { +class model_item_param_list implements ArrayAccess, Countable, IteratorAggregate { + /** + * @var bool Indicates if the list uses question hashes instead of IDs. + */ + private bool $useshashes = false; + /** * @var array */ @@ -601,6 +607,54 @@ public function filter_by_componentids(array $itemids, bool $clone = true): self return $this; } + /** + * Merge another item param list into this one. + * @param model_item_param_list $other The list to merge + * @return self + * @throws coding_exception If lists are incompatible + */ + public function merge(model_item_param_list $other): self { + if ($this->useshashes !== $other->useshashes) { + throw new coding_exception('Cannot merge lists with different identifier types.'); + } + + foreach ($other->itemparams as $itemparam) { + if (!isset($this->itemparams[$itemparam->get_componentid()])) { + $this->add($itemparam); + } + } + return $this; + } + + /** + * Sets the list to use question hashes instead of IDs. + * + * @return self + */ + public function use_hashes(): self { + $this->useshashes = true; + return $this; + } + + /** + * Convert question hashes to IDs. + * @return self + */ + public function convert_hashes_to_ids(): self { + if (!$this->useshashes) { + return $this; + } + + foreach ($this->itemparams as $itemparam) { + $questionid = question_hasher::get_questionid_from_hash($itemparam->get_componentid()); + if ($questionid) { + $itemparam->set_componentid($questionid); + } + } + $this->useshashes = false; + return $this; + } + /** * Gets scaleid and updates scaleid of record. * @param array $newrecord diff --git a/host/calculate.php b/host/calculate.php index ae3c80ba2..3b2c3fb9e 100644 --- a/host/calculate.php +++ b/host/calculate.php @@ -22,6 +22,9 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +use local_catquiz\catscale; +use local_catquiz\data\dataapi; +use local_catquiz\event\calculation_executed; use local_catquiz\local\model\model_responses; use local_catquiz\local\model\model_strategy; @@ -51,10 +54,38 @@ if (optional_param('action', '', PARAM_ALPHA) === 'submit') { $catscaleid = 51; + $catscale = catscale::return_catscale_object($catscaleid); try { $modelresponses = model_responses::create_from_remote_responses($catscaleid); $strategy = new model_strategy($modelresponses); - [$itemdiffs, $personabilities] = $strategy->run_estimation(); + [$itemdifficulties, $personabilities] = $strategy->run_estimation(); + $newcontext = dataapi::create_new_context_for_updated_parameters($catscale); + $updatedmodels = []; + foreach ($itemdifficulties as $modelname => $itemparamlist) { + $itemcounter = 0; + /** @var model_item_param_list $itemparamlist */ + $itemparamlist + ->use_hashes() + ->convert_hashes_to_ids() + ->save_to_db($newcontext->id); + $itemcounter += count($itemparamlist->itemparams); + $model = get_string('pluginname', 'catmodel_' . $modelname); + $updatedmodels[$model] = $itemcounter; + } + + $updatedmodelsjson = json_encode($updatedmodels); + // Trigger event. + $event = calculation_executed::create([ + 'context' => \context_system::instance(), + 'userid' => $userid, + 'other' => [ + 'catscaleid' => $catscaleid, + 'contextid' => $contextid, + 'userid' => $userid, + 'updatedmodelsjson' => $updatedmodelsjson, + ], + ]); + $event->trigger(); echo $OUTPUT->notification( 'Created model_responses',