Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timespan as a setting #7

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion block_townsquare.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

use block_townsquare\contentcontroller;


/**
* Plugin strings are defined here.
*
Expand Down
17 changes: 8 additions & 9 deletions classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,7 @@ public static function record_usersettings($userid, $timefilterpast, $timefilter
// 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.
if (count($records) > 1) {
try {
foreach ($records as $record) {
$DB->delete_records('block_townsquare_preferences', ['id' => $record->id]);
}
} catch (Exception $e) {
return false;
}
} else {
if (count($records) <= 1) {
// Upgrade the existing record.
$record = reset($records);
$record->timefilterpast = $params['timefilterpast'];
Expand All @@ -161,6 +153,13 @@ public static function record_usersettings($userid, $timefilterpast, $timefilter
$DB->update_record('block_townsquare_preferences', $record);
return true;
}
try {
foreach ($records as $record) {
$DB->delete_records('block_townsquare_preferences', ['id' => $record->id]);
}
} catch (Exception $e) {
return false;
}
}
$record = new stdClass();
$record->userid = $params['userid'];
Expand Down
26 changes: 14 additions & 12 deletions classes/letter/post_letter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/

namespace block_townsquare\letter;
use context_module;
use core_user\fields;
use moodle_url;
use stdClass;

Expand Down Expand Up @@ -146,16 +148,16 @@ private function retrieve_profilepicture() {
global $DB, $OUTPUT;

// Profile picture is only retrieved if the author is visible.
if (!$this->post->anonymous) {
$user = new stdClass();
$picturefields = \core_user\fields::get_picture_fields();
$user = username_load_fields_from_object($user, $DB->get_record('user', ['id' => $this->author->id]),
null, $picturefields);
$user->id = $this->author->id;
$this->author->picture = $OUTPUT->user_picture($user, ['courseid' => $this->courseid, 'link' => false]);
} else {
if ($this->post->anonymous) {
$this->author->picture = '';
return;
}
$user = new stdClass();
$picturefields = fields::get_picture_fields();
$user = username_load_fields_from_object($user, $DB->get_record('user', ['id' => $this->author->id]),
null, $picturefields);
$user->id = $this->author->id;
$this->author->picture = $OUTPUT->user_picture($user, ['courseid' => $this->courseid, 'link' => false]);
}

/**
Expand All @@ -164,11 +166,11 @@ private function retrieve_profilepicture() {
* @return void
*/
private function add_anonymousattribute($postevent): void {
if ($postevent->modulename == 'moodleoverflow') {
$this->post->anonymous = $postevent->anonymous;
} else {
if ($postevent->modulename != 'moodleoverflow') {
$this->post->anonymous = false;
return;
}
$this->post->anonymous = $postevent->anonymous;
}

/**
Expand Down Expand Up @@ -198,7 +200,7 @@ private function add_privatereplyattribute($postevent): void {
* @return string
*/
private function format_post($postevent) {
$context = \context_module::instance($postevent->coursemoduleid);
$context = context_module::instance($postevent->coursemoduleid);
$message = file_rewrite_pluginfile_urls($postevent->content, 'pluginfile.php', $context->id,
'mod_'. $postevent->modulename, 'post', $postevent->postid, ['includetoken' => true]);
$options = new stdClass();
Expand Down
3 changes: 2 additions & 1 deletion classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\contextlist;
use core_privacy\local\request\writer;
use stdClass;


/**
Expand Down Expand Up @@ -113,7 +114,7 @@ public static function export_user_data(approved_contextlist $contextlist): void
$results = $DB->get_records('block_townsquare_preferences', ['userid' => $contextlist->get_user()->id]);

foreach ($results as $result) {
$data = new \stdClass();
$data = new stdClass();
$data->userid = $result->userid;
$data->timefilterpast = $result->timefilterpast;
$data->timefilterfuture = $result->timefilterfuture;
Expand Down
5 changes: 5 additions & 0 deletions lang/en/block_townsquare.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
$string['configcompletionlettercolor'] = 'Configuration for the color of the activity completion letters';
$string['configorientationmarkercolor'] = 'Configuration for the color of the orientation marker';
$string['configpostlettercolor'] = 'Configuration for the color of the post letters';
$string['configtimespan'] = 'Setting, in which time span townsquare will search for notification in the past and future';
$string['coursefilter'] = 'Course filter';
$string['dataclosemessage'] = 'Please submit your entries until {$a->time}. The database closes afterwards';
$string['dataopenmessage'] = 'The database opens today';
Expand Down Expand Up @@ -89,6 +90,10 @@
$string['scormopenmessage'] = 'Scorm Activity opens today';
$string['showmore'] = 'Show more';
$string['timefilter'] = 'Time filter';
$string['timesixmonths'] = 'Six months';
$string['timespan'] = 'Search time span';
$string['timethreemonths'] = 'Three months';
$string['timetwomonths'] = 'Two months';
$string['workshopcloseassessment'] = 'Assessments for the workshop are due until {$a->time}';
$string['workshopclosesubmission'] = 'Please submit your work until {$a->time}. The workshop closes afterwards';
$string['workshopopenassessment'] = 'The assessment phase starts today at {$a->time}';
Expand Down
9 changes: 7 additions & 2 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

// Time constants.
define('TOWNSQUARE_TIME_TWOMONTHS', 5259486); // 60.87 days
define('TOWNSQUARE_TIME_THREEMONTHS', 7889229); // 91.31 days
define('TOWNSQUARE_TIME_SIXMONTHS', 15778463); // 182,62 days

// Color constants from bootstrap.
define('TOWNSQUARE_BASICLETTER_DEFAULTCOLOR', '#0f6cbf');
define('TOWNSQUARE_POSTLETTER_DEFAULTCOLOR', '#f7634d');
Expand Down Expand Up @@ -49,15 +54,15 @@ function townsquare_get_courses(): array {
* @return int
*/
function townsquare_get_timestart(): int {
return time() - 7884000;
return time() - get_config('block_townsquare', 'timespan');
}

/**
* Function for subplugins to get the end time of the search.
* @return int
*/
function townsquare_get_timeend(): int {
return time() + 7884000;
return time() + get_config('block_townsquare', 'timespan');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function townsquare_get_colorsetting($lettertype): string {
case 'orientationmarker':
return get_config('block_townsquare', 'orientationmarkercolor');
default:
throw new \moodle_exception('invalidlettertype', 'block_townsquare');
throw new moodle_exception('invalidlettertype', 'block_townsquare');
}
}

Expand Down
17 changes: 13 additions & 4 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,34 @@
if ($ADMIN->fulltree) {
require_once($CFG->dirroot . '/blocks/townsquare/lib.php');

// Color Setting for the color that will be used on basic letters.
// Time setting for the search span.
$options = [];
$options[TOWNSQUARE_TIME_TWOMONTHS] = get_string('timetwomonths', 'block_townsquare');
$options[TOWNSQUARE_TIME_THREEMONTHS] = get_string('timethreemonths', 'block_townsquare');
$options[TOWNSQUARE_TIME_SIXMONTHS] = get_string('timesixmonths', 'block_townsquare');

$settings->add(new admin_setting_configselect('block_townsquare/timespan', get_string('timespan', 'block_townsquare'),
get_string('configtimespan', 'block_townsquare'), TOWNSQUARE_TIME_THREEMONTHS, $options));

// Color setting for the color that will be used on basic letters.
$settings->add(new admin_setting_configcolourpicker('block_townsquare/basiclettercolor',
get_string('basiclettercolor', 'block_townsquare'),
get_string('configbasiclettercolor', 'block_townsquare'),
TOWNSQUARE_BASICLETTER_DEFAULTCOLOR));

// Color Setting for the color that will be used on post letters.
// Color setting for the color that will be used on post letters.
$settings->add(new admin_setting_configcolourpicker('block_townsquare/postlettercolor',
get_string('postlettercolor', 'block_townsquare'),
get_string('configpostlettercolor', 'block_townsquare'),
TOWNSQUARE_POSTLETTER_DEFAULTCOLOR));

// Color Setting for the color that will be used on completion letters.
// Color setting for the color that will be used on completion letters.
$settings->add(new admin_setting_configcolourpicker('block_townsquare/completionlettercolor',
get_string('completionlettercolor', 'block_townsquare'),
get_string('configcompletionlettercolor', 'block_townsquare'),
TOWNSQUARE_COMPLETIONLETTER_DEFAULTCOLOR));

// Color Setting for the orientation marker.
// Color setting for the orientation marker.
$settings->add(new admin_setting_configcolourpicker('block_townsquare/orientationmarkercolor',
get_string('orientationmarkercolor', 'block_townsquare'),
get_string('configorientationmarkercolor', 'block_townsquare'),
Expand Down
4 changes: 3 additions & 1 deletion tests/contentcontroller_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace block_townsquare;

use stdClass;

/**
* Unit tests for the block_townsquare.
*
Expand Down Expand Up @@ -54,7 +56,7 @@ final class contentcontroller_test extends \advanced_testcase {
// Construct functions.

public function setUp(): void {
$this->testdata = new \stdClass();
$this->testdata = new stdClass();
$this->resetAfterTest();
$this->helper_course_set_up();
}
Expand Down
9 changes: 5 additions & 4 deletions tests/coreevents_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
global $CFG;
require_once($CFG->libdir . '/completionlib.php');

use core_completion_external;
use stdClass;

/**
Expand Down Expand Up @@ -81,11 +82,11 @@ public function test_sortorder(): void {
$timestamp = 9999999999;
$result = true;
foreach ($coreevents as $event) {
if ($timestamp >= $event->timestart) {
$timestamp = $event->timestart;
} else {
if ($timestamp < $event->timestart) {
$result = false;
break;
}
$timestamp = $event->timestart;
}

$this->assertEquals(true, $result);
Expand Down Expand Up @@ -210,7 +211,7 @@ public function test_activitycompletion(): void {
$this->assertEquals(1, $count);

// Test case 2: The student marks the assignment as completed, the activity completion event should disappear.
\core_completion_external::update_activity_completion_status_manually($this->testdata->assignment1->cmid, true);
core_completion_external::update_activity_completion_status_manually($this->testdata->assignment1->cmid, true);

$coreevents = $this->get_coreevents_from_user($this->testdata->student1);
$result = true;
Expand Down
23 changes: 16 additions & 7 deletions tests/external_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

namespace block_townsquare;

use stdClass;

defined('MOODLE_INTERNAL') || die();

global $CFG;
Expand All @@ -36,13 +38,20 @@
* @copyright 2024 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @covers \block_townsquare\external
* @covers \external
* @runTestsInSeparateProcesses
*/
final class external_test extends \advanced_testcase {

/** @var object That that is used for testing
* It contains an instance of the townsquare external class.
* */
private $testdata;

public function setUp(): void {
$this->resetAfterTest();
$this->testdata = new stdClass();
$this->testdata->external = new external();
}

/**
Expand All @@ -52,7 +61,7 @@ public function setUp(): void {
public function test_record_usersettings(): void {
global $DB;

$usersetting = new \stdClass();
$usersetting = new stdClass();
$usersetting->userid = 1;
$usersetting->timefilterpast = 432000;
$usersetting->timefilterfuture = 2592000;
Expand All @@ -67,7 +76,7 @@ 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,
$result = $this->testdata->external->record_usersettings($usersetting->userid,
$usersetting->timefilterpast,
$usersetting->timefilterfuture, $usersetting->basicletter,
$usersetting->completionletter, $usersetting->postletter);
Expand All @@ -90,7 +99,7 @@ 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,
$result = $this->testdata->external->record_usersettings($usersetting->userid, $usersetting->timefilterpast,
$usersetting->timefilterfuture, $usersetting->basicletter,
$usersetting->completionletter, $usersetting->postletter);

Expand All @@ -116,22 +125,22 @@ public function test_reset_usersettings(): void {
$this->assertEquals(1, count($DB->get_records('block_townsquare_preferences', ['userid' => 1])));

// Test case 1: Wrong parameters.
\block_townsquare\external::reset_usersettings(2);
$this->testdata->external->reset_usersettings(2);
$this->assertEquals(1, count($DB->get_records('block_townsquare_preferences', ['userid' => 1])));

// Test case 2: For some reason, many records from the same user exist.
$DB->insert_record('block_townsquare_preferences', ['userid' => 1, 'timefilterpast' => 432000,
'timefilterfuture' => 2592000, 'basicletter' => 1, 'completionletter' => 0, 'postletter' => 0, ]);
$this->assertEquals(2, count($DB->get_records('block_townsquare_preferences', ['userid' => 1])));

external::reset_usersettings(1);
$this->testdata->external->reset_usersettings(1);
$this->assertEquals(0, count($DB->get_records('block_townsquare_preferences', ['userid' => 1])));

// Test case 3: normal case.
$DB->insert_record('block_townsquare_preferences', ['userid' => 1, 'timefilterpast' => 432000,
'timefilterfuture' => 2592000, 'basicletter' => 0, 'completionletter' => 1, 'postletter' => 1, ]);
$this->assertEquals(1, count($DB->get_records('block_townsquare_preferences', ['userid' => 1])));
external::reset_usersettings(1);
$this->testdata->external->reset_usersettings(1);
$this->assertEquals(0, count($DB->get_records('block_townsquare_preferences', ['userid' => 1])));
}
}
18 changes: 8 additions & 10 deletions tests/postevents_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

namespace block_townsquare;

use Exception;
use stdClass;

/**
Expand Down Expand Up @@ -82,11 +83,11 @@ public function test_sortorder(): void {
$timestamp = 9999999999;
$result = true;
foreach ($posts as $post) {
if ($timestamp >= $post->timestart) {
$timestamp = $post->timestart;
} else {
if ($timestamp < $post->timestart) {
$result = false;
break;
}
$timestamp = $post->timestart;
}

$this->assertEquals(true, $result);
Expand Down Expand Up @@ -137,7 +138,7 @@ public function test_module_forum(): void {
$posts = $this->get_postevents_from_user($this->testdata->teacher);

$result = true;
// Check if there are no more moodleoverflow posts.
// Check if there are no more forum posts.
foreach ($posts as $post) {
if ($post->modulename == 'forum') {
$result = false;
Expand Down Expand Up @@ -384,11 +385,8 @@ private function helper_course_set_up(): void {
$this->getDataGenerator()->enrol_user($this->testdata->student2->id, $this->testdata->course2->id, 'student');

// Check if moodleoverflow is available.
if ($DB->get_record('modules', ['name' => 'moodleoverflow', 'visible' => 1])) {
$this->modoverflowinstalled = true;
} else {
$this->modoverflowinstalled = false;
}
$DB->get_record('modules', ['name' => 'moodleoverflow', 'visible' => 1]) ? $this->modoverflowinstalled = true :
$this->modoverflowinstalled = false;
}

/**
Expand Down Expand Up @@ -475,7 +473,7 @@ private function make_anonymous($moodleoverflow, $anonymoussetting): void {
$moodleoverflow->anonymous = $anonymoussetting;
$DB->update_record('moodleoverflow', $moodleoverflow);
} else {
throw new \Exception('invalid parameter, anonymoussetting should be 1 or 2');
throw new Exception('invalid parameter, anonymoussetting should be 1 or 2');
}
}

Expand Down
Loading
Loading