Skip to content

Commit

Permalink
web service fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
TamaroWalter committed May 17, 2024
1 parent 581b5db commit 8fdd5c0
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 35 deletions.
2 changes: 1 addition & 1 deletion amd/build/usersettings_save.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion amd/build/usersettings_save.min.js.map

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions amd/src/usersettings_save.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* This file implements 1 functionality:
* - If the "save settings" button is pressed, store the settings in the database.
*
* @module block_townsquare/timefilter
* @module block_townsquare/usersettings_save
* @copyright 2024 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
Expand Down Expand Up @@ -58,7 +58,7 @@ export function init(userid, settingsfromdb) {
let letterfilter = collectletterfiltersettings();

// Second step: store the usersettings in the database.
saveusersettings(userid, timespans['timepast'], timespans['timefuture'], letterfilter['basicletter'],
await saveusersettings(userid, timespans['timepast'], timespans['timefuture'], letterfilter['basicletter'],
letterfilter['completionletter'], letterfilter['postletter']);
});
}
Expand Down Expand Up @@ -87,11 +87,8 @@ function saveusersettings(userid, timefilterpast, timefilterfuture, basicletter,
postletter: postletter,
},
};
//console.log(data);
result = Ajax.call([data]);

//console.log("The result [0] object ",result[0]);

// Show a success message.
let el = document.getElementById('ts_usersettings_successlabel');
el.style.display = 'block';
Expand Down
36 changes: 23 additions & 13 deletions externallib.php → classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* @copyright 2024 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_townsquare_external extends external_api {
class external extends external_api {

/**
* Returns description of method parameters
Expand Down Expand Up @@ -81,6 +81,17 @@ public static function record_usersettings_returns(): external_value {
public static function record_usersettings($userid, $timefilterpast, $timefilterfuture,
$basicletter, $completionletter, $postletter): bool {
global $DB;

// Parameter validation.
$params = self::validate_parameters(self::record_usersettings_parameters(), [
'userid' => $userid,
'timefilterpast' => $timefilterpast,
'timefilterfuture' => $timefilterfuture,
'basicletter' => $basicletter,
'completionletter' => $completionletter,
'postletter' => $postletter,
]);

// Check if the user already has a record in the database.
if ($records = $DB->get_records('block_townsquare_preferences', ['userid' => $userid])) {
// If there more than a record (it only should be only one), delete all of them and insert the new one.
Expand All @@ -96,24 +107,23 @@ public static function record_usersettings($userid, $timefilterpast, $timefilter
} else {
// Upgrade the existing record.
$record = reset($records);
$record->timefilterpast = $timefilterpast;
$record->timefilterfuture = $timefilterfuture;
$record->basicletter = $basicletter;
$record->completionletter = $completionletter;
$record->postletter = $postletter;
$record->timefilterpast = $params['timefilterpast'];
$record->timefilterfuture = $params['timefilterfuture'];
$record->basicletter = $params['basicletter'];
$record->completionletter = $params['completionletter'];
$record->postletter = $params['postletter'];
$DB->update_record('block_townsquare_preferences', $record);
return true;
}
}
$record = new stdClass();
$record->userid = $userid;
$record->timefilterpast = $timefilterpast;
$record->timefilterfuture = $timefilterfuture;
$record->basicletter = $basicletter;
$record->completionletter = $completionletter;
$record->postletter = $postletter;
$record->userid = $params['userid'];
$record->timefilterpast = $params['timefilterpast'];
$record->timefilterfuture = $params['timefilterfuture'];
$record->basicletter = $params['basicletter'];
$record->completionletter = $params['completionletter'];
$record->postletter = $params['postletter'];
$DB->insert_record('block_townsquare_preferences', $record);
return true;
}

}
6 changes: 3 additions & 3 deletions db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@

$functions = [
'block_townsquare_record_usersettings' => [
'classname' => 'block_townsquare_external',
'classname' => 'block_townsquare\external',
'methodname' => 'record_usersettings',
'classpath' => 'blocks/townsquare/externallib.php',
'classpath' => 'blocks/townsquare/classes/external.php',
'description' => 'Records the user settings for the townsquare block',
'type' => 'write',
'type' => 'read',
'ajax' => true,
],
];
20 changes: 9 additions & 11 deletions tests/externallib_test.php → tests/external_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@
* @copyright 2024 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @covers block_townsquare_external::record_usersettings
* @covers \block_townsquare\external::record_usersettings
*/
class externallib_test extends \advanced_testcase {
class external_test extends \advanced_testcase {

public function setUp(): void {
global $CFG;
require_once($CFG->dirroot . '/blocks/townsquare/externallib.php');
$this->resetAfterTest();
}

Expand All @@ -68,10 +66,10 @@ public function test_record_usersettings(): void {
$this->assertEquals(false, $record);

// Call the function to record the user settings and check, if the record is created.
$result = block_townsquare_external::record_usersettings($usersetting->userid,
$usersetting->timefilterpast,
$usersetting->timefilterfuture, $usersetting->basicletter,
$usersetting->completionletter, $usersetting->postletter);
$result = \block_townsquare\external::record_usersettings($usersetting->userid,
$usersetting->timefilterpast,
$usersetting->timefilterfuture, $usersetting->basicletter,
$usersetting->completionletter, $usersetting->postletter);

$this->assertEquals(true, $result);
$record = $DB->get_record('block_townsquare_preferences', ['userid' => $usersetting->userid]);
Expand All @@ -91,9 +89,9 @@ public function test_record_usersettings(): void {
$usersetting->postletter = 0;

// Call the function to record the user settings and check, if the record is created.
$result = block_townsquare_external::record_usersettings($usersetting->userid, $usersetting->timefilterpast,
$usersetting->timefilterfuture, $usersetting->basicletter,
$usersetting->completionletter, $usersetting->postletter);
$result = \block_townsquare\external::record_usersettings($usersetting->userid, $usersetting->timefilterpast,
$usersetting->timefilterfuture, $usersetting->basicletter,
$usersetting->completionletter, $usersetting->postletter);

$this->assertEquals(true, $result);
$record = $DB->get_record('block_townsquare_preferences', ['userid' => $usersetting->userid]);
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->component = 'block_townsquare';
$plugin->release = '0.1.0';
$plugin->version = 2024032201;
$plugin->version = 2024051701;
$plugin->requires = 2022041900;
$plugin->maturity = MATURITY_ALPHA;

0 comments on commit 8fdd5c0

Please sign in to comment.