Skip to content

Commit

Permalink
Merge branch 'master' into hubbub
Browse files Browse the repository at this point in the history
  • Loading branch information
emmachughes committed Jul 14, 2023
2 parents a8836f3 + ab766fe commit 7b24338
Show file tree
Hide file tree
Showing 51 changed files with 712 additions and 457 deletions.
46 changes: 29 additions & 17 deletions sourcecode/apis/contentauthor/app/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Iso639p3;
Expand Down Expand Up @@ -73,27 +75,36 @@ public function render(): string
return self::rewriteUploadUrls($this->content);
}

public function collaborators()
/**
* @return HasMany<ArticleCollaborator>
*/
public function collaborators(): HasMany
{
return $this->hasMany(ArticleCollaborator::class);
}

public function files()
/**
* @return HasMany<File>
*/
public function files(): HasMany
{
return $this->hasMany(File::class);
}

public function rewriteUrls($original, $new)
public function rewriteUrls($original, $new): void
{
$this->content = str_replace($original, $new, $this->content);
}

public function givesScore()
public function givesScore(): int
{
return 0;
}

public function parent()
/**
* @return BelongsTo<Article, self>
*/
public function parent(): BelongsTo
{
return $this->belongsTo(Article::class, 'parent_id');
}
Expand All @@ -108,12 +119,12 @@ public function getOriginalIdAttribute($originalId)
}

// Abstract method implementations
protected function getContentContent()
protected function getContentContent(): string
{
return $this->content;
}

protected function getRequestContent(Request $request)
protected function getRequestContent(Request $request): mixed
{
return $request->get('content');
}
Expand All @@ -128,7 +139,7 @@ public function getISO6393Language(): string
return Iso639p3::code3letters('eng');
}

public function makeCopy($owner = null)
public function makeCopy($owner = null): self
{
$newArticle = $this->replicate();
$newArticle->id = Uuid::uuid4()->toString();
Expand All @@ -154,21 +165,23 @@ public function getContentType(bool $withSubType = false): string
return Content::TYPE_ARTICLE;
}

public function scopeOfBulkCalculated($query, $type)
/**
* @param Builder<self> $query
*/
public function scopeOfBulkCalculated(Builder $query, $type): void
{
$query->where('bulk_calculated', $type);
}

/**
* @param Builder $query
* @param Builder<self> $query
*/
public function scopeNoMaxScore($query)
public function scopeNoMaxScore(Builder $query): void
{
$query->whereNull('max_score');
}


public function getMaxScoreHelper($content, $haltIfNotCalculated = false)
public function getMaxScoreHelper($content, $haltIfNotCalculated = false): int
{
$pattern = '/src=.\/lti\/launch\?url=([^"]+)"?/m';
preg_match_all($pattern, $content, $matches, PREG_SET_ORDER);
Expand Down Expand Up @@ -203,7 +216,7 @@ public function getMaxScoreHelper($content, $haltIfNotCalculated = false)
->sum();
}

public function getMaxScore()
public function getMaxScore(): int
{
return $this->getMaxScoreHelper($this->content);
}
Expand All @@ -228,17 +241,16 @@ public function setParentVersionId(string $parentVersionId): bool
}
}

public function setVersionId(string $versionId)
public function setVersionId(string $versionId): void
{
$this->version_id = $versionId;
}

/**
* Used by Eloquent to get primary key type.
* UUID Identified as a string.
* @return string
*/
public function getKeyType()
public function getKeyType(): string
{
return 'string';
}
Expand Down
3 changes: 2 additions & 1 deletion sourcecode/apis/contentauthor/app/Collaborator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class Collaborator extends Model
{
use HasFactory;

protected $fillable = ['email'];

public function collaboratable()
public function collaboratable(): MorphTo
{
return $this->morphTo();
}
Expand Down
9 changes: 4 additions & 5 deletions sourcecode/apis/contentauthor/app/CollaboratorContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CollaboratorContext extends Model
public $incrementing = false;
public $timestamps = false;

public static function contextShouldUpdate($systemId, $contextId, $timestamp)
public static function contextShouldUpdate($systemId, $contextId, $timestamp): bool
{
if (!config('feature.context-collaboration', false)) {
return false;
Expand All @@ -27,7 +27,7 @@ public static function contextShouldUpdate($systemId, $contextId, $timestamp)
->doesntExist();
}

public static function deleteContext($systemId, $contextId)
public static function deleteContext($systemId, $contextId): void
{
if (!config('feature.context-collaboration', false)) {
return;
Expand All @@ -38,7 +38,7 @@ public static function deleteContext($systemId, $contextId)
->delete();
}

public static function updateContext($systemId, $contextId, $collaborators, $resources, $timestamp)
public static function updateContext($systemId, $contextId, $collaborators, $resources, $timestamp): void
{
if (!config('feature.context-collaboration', false)) {
return;
Expand Down Expand Up @@ -74,10 +74,9 @@ public static function updateContext($systemId, $contextId, $collaborators, $res
}

/**
* @return bool
* @throws Exception
*/
public static function isUserCollaborator($collaboratorId, $resourceId)
public static function isUserCollaborator($collaboratorId, $resourceId): bool
{
if (!config('feature.context-collaboration', false)) {
return false;
Expand Down
49 changes: 25 additions & 24 deletions sourcecode/apis/contentauthor/app/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
Expand Down Expand Up @@ -99,6 +100,9 @@ public function isOwner($currentUserId): bool

abstract public function getContentType(bool $withSubType = false);

/**
* @return HasOne<NdlaIdMapper>
*/
public function ndlaMapper(): HasOne
{
return $this->hasOne(NdlaIdMapper::class, 'ca_id');
Expand All @@ -109,7 +113,10 @@ public function isCopyable(): bool
return License::isContentCopyable($this->license);
}

public function locks()
/**
* @return HasMany<ContentLock>
*/
public function locks(): HasMany
{
return $this->hasMany(ContentLock::class, 'content_id');
}
Expand Down Expand Up @@ -151,10 +158,9 @@ public function getOwnerName($ownerId): ?string
}

/**
* @return bool
* @throws Exception
*/
public function isExternalCollaborator($currentUserId)
public function isExternalCollaborator($currentUserId): bool
{
if (CollaboratorContext::isUserCollaborator($currentUserId, $this->id)) {
return true;
Expand Down Expand Up @@ -187,7 +193,7 @@ public function canUpdateOriginalResource(mixed $userId): bool
return $this->isOwner($userId) || $this->isCollaborator();
}

public function shouldCreateForkBasedOnSession($username = 'authId')
public function shouldCreateForkBasedOnSession($username = 'authId'): bool
{
return $this->shouldCreateFork(Session::get($username, false));
}
Expand Down Expand Up @@ -290,12 +296,11 @@ public function unlock()
}

/**
* @param Builder $query
* @return Builder
* @param Builder<self> $query
*/
public function scopeUnversioned($query)
public function scopeUnversioned(Builder $query): void
{
return $query->where('version_id', null);
$query->where('version_id', null);
}

/**
Expand Down Expand Up @@ -343,7 +348,7 @@ public function isImported(): bool
return $ndlaMapperCollection->isNotEmpty();
}

private function getVersionedIds(VersionData $version)
private function getVersionedIds(VersionData $version): array
{
$id = [$version->getExternalReference()];
if (!is_null($version->getParent())) {
Expand Down Expand Up @@ -383,40 +388,34 @@ public static function isUserPublishEnabled(): bool
return $adapter->isUserPublishEnabled();
}

public function canList(Request $request)
public function canList(Request $request): bool
{
if (self::isUserPublishEnabled() !== true || $this->exists === false) {
if (!self::isUserPublishEnabled() || !$this->exists) {
return true;
}

$authId = $request->session()->get('authId') ?? false;
return $this->isOwner($authId) || $this->isCollaborator() || $this->isExternalCollaborator($authId);
}

public function canPublish(Request $request)
public function canPublish(Request $request): bool
{
if (self::isUserPublishEnabled() !== true || $this->exists === false || $request->importRequest ?? false === true) {
if (self::isUserPublishEnabled() || !$this->exists || ($request->importRequest ?? false)) {
return true;
}

return $this->canList($request) || $this->isCopyable();
}

/**
* @param boolean $preview
* @return boolean
*/
public function canShow($preview = false)
public function canShow(bool $preview = false): bool
{
return $preview === true || $this->isActuallyPublished();
return $preview || $this->isActuallyPublished();
}

/**
* @return H5PContent|Article|Game|Link|QuestionSet|null
*
* Poor mans morphism...
*/
public static function findContentById($contentId)
public static function findContentById($contentId): H5PContent|Article|Game|Link|QuestionSet|null
{
if ((preg_match('/^\d+$/', $contentId) && ($content = H5PContent::find($contentId))) ||
($content = Article::find($contentId)) ||
Expand All @@ -426,6 +425,8 @@ public static function findContentById($contentId)
) {
return $content;
}

return null;
}

public function getEditUrl($latest = false): ?string
Expand All @@ -447,12 +448,12 @@ public function getEditUrl($latest = false): ?string
return $editUrl;
}

public function getMaxScore()
public function getMaxScore(): int|null
{
return null;
}

public function getAuthorOverwrite()
public function getAuthorOverwrite(): string|null
{
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion sourcecode/apis/contentauthor/app/ContentAttribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function getAttributionAttribute($value): Attribution
/**
* @throws \Exception
*/
public function setAttributionAttribute(Attribution $attribution)
public function setAttributionAttribute(Attribution $attribution): void
{
$serializedAttribution = json_encode($attribution);

Expand Down
2 changes: 1 addition & 1 deletion sourcecode/apis/contentauthor/app/ContentLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ContentLanguage extends Model
/**
* @throws Exception
*/
public function setLanguageCodeAttribute($languageCode)
public function setLanguageCodeAttribute($languageCode): void
{
$languageCode = mb_strtolower($languageCode);

Expand Down
8 changes: 6 additions & 2 deletions sourcecode/apis/contentauthor/app/ContentLanguageLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

namespace App;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class ContentLanguageLink extends Model
{
protected $fillable = ['main_content_id', 'link_content_id', "language_code", "content_type"];

public function scopeOfContentType($query, $contentType)
/**
* @param Builder<self> $query
*/
public function scopeOfContentType(Builder $query, $contentType): void
{
return $query->where("content_type", $contentType);
$query->where("content_type", $contentType);
}
}
8 changes: 6 additions & 2 deletions sourcecode/apis/contentauthor/app/ContentLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\Session;
use Carbon\Carbon;
Expand Down Expand Up @@ -80,8 +81,11 @@ public function getEditor()
return "???";
}

public function scopeActive($query)
/**
* @param Builder<self> $query
*/
public function scopeActive(Builder $query): void
{
return $query->where('updated_at', '>', Carbon::now()->subSeconds(self::EXPIRES));
$query->where('updated_at', '>', Carbon::now()->subSeconds(self::EXPIRES));
}
}
Loading

0 comments on commit 7b24338

Please sign in to comment.