Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
davidszkiba committed Nov 22, 2024
1 parent 681a3d0 commit 02da261
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 10 deletions.
16 changes: 16 additions & 0 deletions classes/data/dataapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,22 @@ public static function name_exists(string $name): bool {
}
}

/**
* Check if label of catscale already exsists - must be unique
*
* @param string $label catscale label
*
* @return bool true if label already exists, false if not
*/
public static function label_exists(string $label): bool {
global $DB;
if ($DB->record_exists('local_catquiz_catscales', ['label' => $label])) {
return true;
} else {
return false;
}
}

/**
* Get catscale by ID
* @param int $id catscale id
Expand Down
28 changes: 21 additions & 7 deletions classes/external/client_fetch_parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,13 @@ public static function execute(string $scaleid) {
// If there is no entry yet for the given scale and component:
// insert with new contextid. Otherwise: update with new
// contextid.
$existing = $repo->get_item_with_params(
$localparam = $repo->get_item_with_params(
$questionid,
$param->model,
$currentcontext
);

if (!$existing) {
if (!$localparam) {
// Insert only once per questionid.
if (!self::item_exists($questionid)) {
$itemrecord = new \stdClass();
Expand All @@ -266,15 +266,16 @@ public static function execute(string $scaleid) {
}

// If it did not change, we can skip it.
$changed = $param->difficulty != $existing->difficulty
|| $param->discrimination != $existing->discrimination
|| $param->json != $existing->json
|| $param->status != $existing->status;
$changed = !$localparam // This is a new model param we do not have locally.
|| !self::check_float_equal($param->difficulty, $localparam->difficulty)
|| !self::check_float_equal($param->discrimination, $localparam->discrimination)
|| $param->json != $localparam->json
|| $param->status != $localparam->status;

if (!$changed) {
continue;
}
$changedparams[] = ['old' => $existing, 'new' => $param];
$changedparams[] = ['old' => $localparam, 'new' => $param];

// If we are here, at least one parameter changed and we will
// later have to commit the transaction.
Expand Down Expand Up @@ -353,4 +354,17 @@ private static function item_exists(int $questionid) {
self::$existingitems[$questionid] = $existsforquestion;
return $existsforquestion;
}

/**
* Checks if two float numbers are equal within a given precision.
*
* @param float $num1 First number to compare
* @param float $num2 Second number to compare
* @param int $precision Number of decimal places to check (default: 4)
*
* @return bool True if numbers are equal within precision, false otherwise
*/
private static function check_float_equal(float $num1, float $num2, int $precision = 4) {
return abs($num1 - $num2) < pow(10, -1 * $precision);
}
}
4 changes: 4 additions & 0 deletions classes/form/modal_manage_catscale.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ public function validation($data, $files): array {
$errors['name'] = get_string('catscalesname_exists', 'local_catquiz');
}

if (dataapi::label_exists($data['label']) && $data['id'] === 0) {
$errors['label'] = get_string('catscaleslabel_exists', 'local_catquiz');
}

if (isset($data["catquiz_minscalevalue"]) && (float) $data["catquiz_minscalevalue"] >= 0) {
$errors["catquiz_minscalevalue"] = get_string(
'formelementnegativefloatwithdefault',
Expand Down
1 change: 0 additions & 1 deletion client/submit_responses.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

// Basic security checks.
require_login();
// require_capability('local/catquiz:submit_responses', context_system::instance());

// Setup page.
$PAGE->set_context(context_system::instance());
Expand Down
1 change: 0 additions & 1 deletion db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,5 @@ function xmldb_local_catquiz_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2024111804, 'local', 'catquiz');
}


return true;
}
1 change: 1 addition & 0 deletions lang/de/local_catquiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
$string['catscales'] = 'CAT quiz Dimensionen verwalten';
$string['catscales:information'] = 'Verwalte CAT Test Skalen: {$a->link}';
$string['catscalesheading'] = 'Skalen';
$string['catscaleslabel_exists'] = 'Das Label wird bereits verwendet';
$string['catscalesname_exists'] = 'Der Name wird bereits verwendet';
$string['catscaleupdatedtitle'] = 'Eine Skala wurde aktualisiert';
$string['cattags'] = 'Kurs Tags verwalten';
Expand Down
1 change: 1 addition & 0 deletions lang/en/local_catquiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
$string['catscales'] = 'Define catquiz CAT scales';
$string['catscales:information'] = 'Define CAT scales: {$a->link}';
$string['catscalesheading'] = 'CAT scales';
$string['catscaleslabel_exists'] = 'The label (lowercase name) is already being used';
$string['catscalesname_exists'] = 'The name is already being used';
$string['catscaleupdatedtitle'] = 'A CAT scale was updated';
$string['cattags'] = 'Manage course tags';
Expand Down
3 changes: 2 additions & 1 deletion tests/catcalc_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ public static function ability_can_be_calculated_with_all_models_provider(): arr
$pcmgeneralizedparam = new model_item_param($itemid, 'pcmgeneralized', [], 4, $pcmgeneralizedrecord);
$pcmparam = new model_item_param($itemid, 'pcm', [], 4, $pcmrecord);

$resp = new model_item_response($itemid, 1.0, new model_person_param('1', 1));
$personparam = new model_person_param('1', 1);
$resp = new model_item_response($itemid, 1.0, $personparam);
$responses = [
$itemid => $resp,
];
Expand Down

0 comments on commit 02da261

Please sign in to comment.