diff options
| author | Jan-Hendrik Willms <tleilax+github@gmail.com> | 2021-07-22 16:07:19 +0200 |
|---|---|---|
| committer | Jan-Hendrik Willms <tleilax+github@gmail.com> | 2021-07-22 16:19:12 +0200 |
| commit | a3da1483a9e689846179159355badfec8073dbec (patch) | |
| tree | 770dcca6bdf5f6f2a11b0e7fcbbeda6919a3fc52 /lib/models/Courseware/UserProgress.php | |
current code from svn, revision 62608
Diffstat (limited to 'lib/models/Courseware/UserProgress.php')
| -rwxr-xr-x | lib/models/Courseware/UserProgress.php | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/lib/models/Courseware/UserProgress.php b/lib/models/Courseware/UserProgress.php new file mode 100755 index 0000000..d4355f5 --- /dev/null +++ b/lib/models/Courseware/UserProgress.php @@ -0,0 +1,66 @@ +<?php + +namespace Courseware; + +/** + * Courseware's user progresses. + * + * @author Marcus Eibrink-Lunzenauer <lunzenauer@elan-ev.de> + * @author Till Glöggler <gloeggler@elan-ev.de> + * @author Ron Lucke <lucke@elan-ev.de> + * @license GPL2 or any later version + * + * @since Stud.IP 5.0 + * + * @property array $id computed column read/write + * @property string $user_id database column + * @property int $block_id database column + * @property string $grade database column + * @property string $mkdate database column + * @property string $chdate database column + * @property \Courseware\Block $block belongs_to Courseware\Block + * @property \User $user belongs_to User + */ +class UserProgress extends \SimpleORMap +{ + protected static function configure($config = []) + { + $config['db_table'] = 'cw_user_progresses'; + + $config['belongs_to']['block'] = [ + 'class_name' => Block::class, + 'foreign_key' => 'block_id', + ]; + + $config['belongs_to']['user'] = [ + 'class_name' => \User::class, + 'foreign_key' => 'user_id', + ]; + + parent::configure($config); + } + + /** + * Returns either an existing UserProgress of this user and block or creates a new one. + * + * @param \User $user the user of the existing or new UserProgress + * @param Block $block the block of the existing or new UserProgress + * + * @return UserProgress the either already existing or new UserProgress + */ + public static function getUserProgress(\User $user, Block $block): UserProgress + { + /** @var ?UserProgress $progress */ + $progress = UserProgress::find([$user->id, $block->id]); + if (!$progress) { + /** @var UserProgress $progress */ + $progress = UserProgress::build([ + 'user_id' => $user->id, + 'block_id' => $block->id, + 'grade' => 0, + ]); + } + + return $progress; + } +} |
