Skip to content

Commit

Permalink
PM-43593 working proof of concept
Browse files Browse the repository at this point in the history
  • Loading branch information
mk-kialo committed Sep 19, 2024
1 parent b384a4f commit 55cbfa7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 25 deletions.
42 changes: 25 additions & 17 deletions classes/grading/line_item.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,41 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.

// phpcs:disable moodle.NamingConventions.ValidFunctionName.LowercaseMethod
// phpcs:disable moodle.NamingConventions.ValidVariableName.MemberNameUnderscore

namespace mod_kialo\grading;

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

/**
* Represents a line item in the LTI 1.3 Assignment and Grading Service.
*/
class line_item {
/**
* @var string|null $id
*/
public $id;

/**
* @var float|null $scoremaximum
* @var float|null $scoreMaximum
*/
public $scoremaximum;
public $scoreMaximum;

/**
* @var string|null $label
*/
public $label;

/**
* @var string|null $resourceid
* @var string|null $resourceId
*/
public $resourceid;
public $resourceId;

/**
* @var string|null $tag
*/
public $resourcelinkid;
public $resourceLinkId;

/**
* @var string|null $tag
Expand All @@ -51,34 +57,36 @@ class line_item {

/**
* ISO 8601 timestamp, see https://www.imsglobal.org/spec/lti-ags/v2p0#startdatetime.
* @var string|null $startdatetime
*
* @var string|null $startDateTime
*/
public $startdatetime;
public $startDateTime;

/**
* ISO 8601 timestamp, see https://www.imsglobal.org/spec/lti-ags/v2p0#enddatetime.
* @var string|null $enddatetime
*
* @var string|null $endDateTime
*/
public $enddatetime;
public $endDateTime;

/**
* @var bool|null $gradesreleased
* @var bool|null $gradesReleased
*/
public $gradesreleased;
public $gradesReleased;

/**
* LineItem constructor.
* @param array|null $lineitem
*/
public function __construct(?array $lineitem = null) {
$this->id = $lineitem['id'] ?? null;
$this->scoremaximum = $lineitem['scoreMaximum'] ?? null;
$this->scoreMaximum = $lineitem['scoreMaximum'] ?? null;
$this->label = $lineitem['label'] ?? null;
$this->resourceid = $lineitem['resourceId'] ?? null;
$this->resourcelinkid = $lineitem['resourceLinkId'] ?? null;
$this->resourceId = $lineitem['resourceId'] ?? null;
$this->resourceLinkId = $lineitem['resourceLinkId'] ?? null;
$this->tag = $lineitem['tag'] ?? null;
$this->startdatetime = $lineitem['startDateTime'] ?? null;
$this->enddatetime = $lineitem['endDateTime'] ?? null;
$this->gradesreleased = $lineitem['gradesReleased'] ?? null;
$this->startDateTime = $lineitem['startDateTime'] ?? null;
$this->endDateTime = $lineitem['endDateTime'] ?? null;
$this->gradesReleased = $lineitem['gradesReleased'] ?? null;
}
}
51 changes: 43 additions & 8 deletions lti_lineitem.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* the claim must include the endpoint URL for accessing the associated line item;
* in all other cases, this property must be either blank or not included in the claim.
*
* See LTI 1.3 Assignment and Grading Service specification: https://www.imsglobal.org/spec/lti-ags/v2p0.
*/

require_once(__DIR__ . '/../../config.php');
Expand All @@ -34,7 +35,7 @@
$logger = new kialo_logger("lti_lineitem");
$logger->info("LTI lineitem request received.", $_POST ?? $_GET ?? []);

lti_flow::authenticate_service_request(MOD_KIALO_LTI_AGS_SCOPES);
//lti_flow::authenticate_service_request(MOD_KIALO_LTI_AGS_SCOPES);

$courseid = required_param('course_id', PARAM_INT);
$cmid = required_param('cmid', PARAM_INT);
Expand All @@ -43,16 +44,50 @@
if (!$module) {
die("Module $cmid not found");
}
$moduleinstance = $DB->get_record('kialo', ['id' => $module->instance], '*', MUST_EXIST);

$gradeitem = grade_item::fetch(['iteminstance' => $module->instance, 'itemtype' => 'mod']);
if (!$gradeitem) {
die("Grade item for module CMID=$cmid (instance={$module->instance}) not found");
}

$lineitem = new line_item();
$lineitem->id = $_SERVER['REQUEST_URI'];
$lineitem->label = $module->name;
$lineitem->scoremaximum = floatval($gradeitem->grademax);
$lineitem->resourcelinkid = $resourcelinkid;
if (isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] == '/scores') {
// Parse JSON POST request body.
$input = file_get_contents('php://input');
$data = json_decode($input, true);

$userid = $data['userId'];
$scoregiven = isset($data['scoreGiven']) ? max(0, min($data['scoreGiven'], $gradeitem->grademax)) : null;
$comment = $data['comment'];
$timestamp = isset($data['timestamp']) ? strtotime($data['timestamp']) : time();
$activityprogress = $data['activityProgress'];
$gradingprogress = $data['gradingProgress'];

if ($scoregiven < 0 && $scoregiven > $gradeitem->grademax) {
$logger->error("Invalid score given: $scoregiven");
die("Invalid score given: $scoregiven");
}

$grades = [
'userid' => $userid,
'feedback' => $comment,
'dategraded' => $timestamp,
];
if ($scoregiven !== null) {
$grades['rawgrade'] = $scoregiven;
}

kialo_grade_item_update($moduleinstance, $grades);

} else {
// Handle GET request
$lineitem = new line_item();
$lineitem->id = (new moodle_url($_SERVER['REQUEST_URI']))->out(false);
$lineitem->label = $module->name;
$lineitem->scoreMaximum = floatval($gradeitem->grademax);
$lineitem->resourceLinkId = $resourcelinkid;

header('Content-Type: application/json; utf-8');
echo json_encode($lineitem, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}

header('Content-Type: application/json; utf-8');
echo json_encode($lineitem, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

0 comments on commit 55cbfa7

Please sign in to comment.