-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa9e9ad
commit 6d74b56
Showing
3 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[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> | ||
*/ | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters