Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
davidszkiba committed Nov 29, 2024
1 parent aa9e9ad commit 6d74b56
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
10 changes: 10 additions & 0 deletions classes/local/model/model_item_param.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
56 changes: 55 additions & 1 deletion classes/local/model/model_item_param_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -55,7 +56,12 @@
* @copyright 2024 Wunderbyte GmbH <[email protected]>
* @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<model_item_param>
*/
Expand Down Expand Up @@ -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
Expand Down
33 changes: 32 additions & 1 deletion host/calculate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 6d74b56

Please sign in to comment.