Skip to content

Commit

Permalink
MDL-77296 core: Record MoodleNet share progress
Browse files Browse the repository at this point in the history
  • Loading branch information
davewoloszyn authored and mickhawkins committed Aug 17, 2023
1 parent dbb3759 commit 54cb80b
Show file tree
Hide file tree
Showing 12 changed files with 684 additions and 11 deletions.
2 changes: 2 additions & 0 deletions lang/en/error.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@
$string['moduleinstancedoesnotexist'] = 'The instance of this module does not exist';
$string['modulemissingcode'] = 'Module {$a} is missing the code needed to perform this function';
$string['moodlenet:invalidshareformat'] = 'Invalid MoodleNet share format';
$string['moodlenet:invalidsharestatus'] = 'Invalid MoodleNet share status';
$string['moodlenet:invalidsharetype'] = 'Invalid MoodleNet share type';
$string['moodlenet:usernotconfigured'] = 'You do not have permission to share content to MoodleNet, or your account is incorrectly configured.';
$string['movecatcontentstoroot'] = 'Moving the category content to root is not allowed. You must move the contents to an existing category!';
$string['movecatcontentstoselected'] = 'Some category content cannot be moved into the selected category.';
Expand Down
8 changes: 8 additions & 0 deletions lang/en/moodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,14 @@
$string['privacy:metadata:log:time'] = 'The time when the action took place';
$string['privacy:metadata:log:url'] = 'The URL related to the event';
$string['privacy:metadata:log:userid'] = 'The ID of the user who performed the action';
$string['privacy:metadata:moodlenet_share_progress'] = 'MoodleNet share progress details';
$string['privacy:metadata:moodlenet_share_progress:type'] = 'The type of share that was performed';
$string['privacy:metadata:moodlenet_share_progress:courseid'] = 'The associated course ID';
$string['privacy:metadata:moodlenet_share_progress:cmid'] = 'The associated course module ID';
$string['privacy:metadata:moodlenet_share_progress:userid'] = 'The user that performed the share';
$string['privacy:metadata:moodlenet_share_progress:timecreated'] = 'The time the share was performed';
$string['privacy:metadata:moodlenet_share_progress:resourceurl'] = 'The returned url from MoodleNet after a successful share';
$string['privacy:metadata:moodlenet_share_progress:status'] = 'The resulting status of the share';
$string['privacy:metadata:oauth2_refresh_token'] = 'Refresh token used in OAuth 2.0 communication';
$string['privacy:metadata:oauth2_refresh_token:issuerid'] = 'The ID of the issuer to which the token corresponds';
$string['privacy:metadata:oauth2_refresh_token:scopehash'] = 'The ID of the user to whom the token corresponds';
Expand Down
12 changes: 12 additions & 0 deletions lib/classes/external/moodlenet_send_activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use core\moodlenet\activity_sender;
use core\moodlenet\moodlenet_client;
use core\moodlenet\utilities;
use core\moodlenet\share_recorder;
use core\oauth2\api;
use core_external\external_api;
use core_external\external_function_parameters;
Expand Down Expand Up @@ -114,17 +115,28 @@ public static function execute(int $issuerid, int $cmid, int $shareformat): arra

// Share activity.
try {
// Record activity share progress.
$shareid = share_recorder::insert_share_progress(share_recorder::TYPE_ACTIVITY, $USER->id, $course->id, $cmid);

$moodlenetclient = new moodlenet_client($client, $oauthclient);
$activitysender = new activity_sender($cmid, $USER->id, $moodlenetclient, $oauthclient, $shareformat);
$result = $activitysender->share_resource();
if (empty($result['drafturl'])) {

share_recorder::update_share_progress($shareid, share_recorder::STATUS_ERROR);

return self::return_errors($result['responsecode'], 'errorsendingactivity',
get_string('moodlenet:cannotconnecttoserver', 'moodle'));
}
} catch (\moodle_exception $e) {

share_recorder::update_share_progress($shareid, share_recorder::STATUS_ERROR);

return self::return_errors(0, 'errorsendingactivity', $e->getMessage());
}

share_recorder::update_share_progress($shareid, share_recorder::STATUS_SENT, $result['drafturl']);

return [
'status' => true,
'resourceurl' => $result['drafturl'],
Expand Down
12 changes: 12 additions & 0 deletions lib/classes/external/moodlenet_send_course.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use core\http_client;
use core\moodlenet\course_sender;
use core\moodlenet\moodlenet_client;
use core\moodlenet\share_recorder;
use core\moodlenet\utilities;
use core\oauth2\api;
use core_external\external_api;
Expand Down Expand Up @@ -135,24 +136,35 @@ public static function execute(

// Share course.
try {
// Record course share progress.
$shareid = share_recorder::insert_share_progress(share_recorder::TYPE_COURSE, $USER->id, $course->id);

$moodlenetclient = new moodlenet_client($client, $oauthclient);
$coursesender = new course_sender($course->id, $USER->id, $moodlenetclient, $oauthclient, $shareformat);
$result = $coursesender->share_resource();
if (empty($result['drafturl'])) {

share_recorder::update_share_progress($shareid, share_recorder::STATUS_ERROR);

return self::return_errors(
$result['responsecode'],
'errorsendingcourse',
get_string('moodlenet:cannotconnecttoserver', 'moodle')
);
}
} catch (\moodle_exception | \JsonException $e) {

share_recorder::update_share_progress($shareid, share_recorder::STATUS_ERROR);

return self::return_errors(
0,
'errorsendingcourse',
$e->getMessage()
);
}

share_recorder::update_share_progress($shareid, share_recorder::STATUS_SENT, $result['drafturl']);

return [
'status' => true,
'resourceurl' => $result['drafturl'],
Expand Down
2 changes: 0 additions & 2 deletions lib/classes/moodlenet/activity_sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ public function share_resource(): array {
$responsebody = json_decode($response->getBody());
$resourceurl = $responsebody->homepage ?? '';

// TODO: Store consumable information about completed share - to be completed in MDL-77296.

// Delete the generated file now it is no longer required.
// (It has either been sent, or failed - retries not currently supported).
$filedata->delete();
Expand Down
132 changes: 132 additions & 0 deletions lib/classes/moodlenet/share_recorder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core\moodlenet;

use moodle_exception;
use stdClass;

/**
* Record the sharing of content to MoodleNet.
*
* @package core
* @copyright 2023 David Woloszyn <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class share_recorder {

/**
* @var int The content being shared is a course.
*/
public const TYPE_COURSE = 1;

/**
* @var int The content being shared is an activity.
*/
public const TYPE_ACTIVITY = 2;

/**
* @var int The status of the share is 'sent'.
*/
public const STATUS_SENT = 1;

/**
* @var int The status of the share is 'in progress'.
*/
public const STATUS_IN_PROGRESS = 2;

/**
* @var int The status of the share is 'error'.
*/
public const STATUS_ERROR = 3;

/**
* Get all allowed share types.
*
* @return array
*/
protected static function get_allowed_share_types(): array {

return [
self::TYPE_ACTIVITY,
self::TYPE_COURSE
];
}

/**
* Get all allowed share statuses.
* Note that the particular status values aid in sorting.
*
* @return array
*/
protected static function get_allowed_share_statuses(): array {

return [
self::STATUS_SENT,
self::STATUS_IN_PROGRESS,
self::STATUS_ERROR,
];
}

/**
* Create a new share progress record in the DB.
*
* @param int $sharetype The type of share (e.g. TYPE_COURSE).
* @param int $userid The ID of the user performing the share.
* @param int $courseid The associated course id.
* @param int|null $cmid The associated course module id (when sharing activity).
* @return int Returns the inserted record id.
*/
public static function insert_share_progress(int $sharetype, int $userid, int $courseid, ?int $cmid = null): int {
global $DB, $USER;

if (!in_array($sharetype, self::get_allowed_share_types())) {
throw new moodle_exception('moodlenet:invalidsharetype');
}

$data = new stdClass();
$data->type = $sharetype;
$data->courseid = $courseid;
$data->cmid = $cmid;
$data->userid = $userid;
$data->timecreated = time();
$data->status = self::STATUS_IN_PROGRESS;

return $DB->insert_record('moodlenet_share_progress', $data);
}

/**
* Update the share progress record in the DB.
*
* @param int $shareid The id of the share progress row being updated.
* @param int $status The status of the share progress (e.g. STATUS_SENT).
* @param string|null $resourceurl The resource url returned from MoodleNet.
*/
public static function update_share_progress(int $shareid, int $status, ?string $resourceurl = null): void {
global $DB;

if (!in_array($status, self::get_allowed_share_statuses())) {
throw new moodle_exception('moodlenet:invalidsharestatus');
}

$data = new stdClass();
$data->id = $shareid;
$data->resourceurl = $resourceurl;
$data->status = $status;

$DB->update_record('moodlenet_share_progress', $data);
}
}
Loading

0 comments on commit 54cb80b

Please sign in to comment.