Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 15... Línea 15...
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea 16...
16
 
16
 
Línea 17... Línea 17...
17
namespace ltiservice_gradebookservices;
17
namespace ltiservice_gradebookservices;
-
 
18
 
-
 
19
use ltiservice_gradebookservices\local\resources\lineitem;
18
 
20
use ltiservice_gradebookservices\local\resources\results;
Línea 19... Línea 21...
19
use ltiservice_gradebookservices\local\resources\lineitem;
21
use ltiservice_gradebookservices\local\resources\scores;
20
use ltiservice_gradebookservices\local\service\gradebookservices;
22
use ltiservice_gradebookservices\local\service\gradebookservices;
21
 
23
 
Línea 26... Línea 28...
26
 * @category   test
28
 * @category   test
27
 * @copyright  2022 Cengage Group <claude.vervoort@cengage.com>
29
 * @copyright  2022 Cengage Group <claude.vervoort@cengage.com>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 * @coversDefaultClass \mod_lti\service\gradebookservices\local\resources\lineitem
31
 * @coversDefaultClass \mod_lti\service\gradebookservices\local\resources\lineitem
30
 */
32
 */
31
class lineitem_test extends \advanced_testcase {
33
final class lineitem_test extends \advanced_testcase {
Línea 32... Línea 34...
32
 
34
 
33
    /**
35
    /**
34
     * @covers ::execute
36
     * @covers ::execute
35
     *
37
     *
Línea 173... Línea 175...
173
        $this->assertEquals($subreviewurl, $responseitem->submissionReview->url);
175
        $this->assertEquals($subreviewurl, $responseitem->submissionReview->url);
174
        $this->assertFalse(isset($responseitem->submissionReview->custom));
176
        $this->assertFalse(isset($responseitem->submissionReview->custom));
175
    }
177
    }
Línea 176... Línea 178...
176
 
178
 
-
 
179
    /**
-
 
180
     * Test running a series of score updates, highlighting problems with the score posting logic.
-
 
181
     *
-
 
182
     * @covers ::execute
-
 
183
     *
-
 
184
     * @return void
-
 
185
     */
-
 
186
    public function test_sequential_score_posts(): void {
-
 
187
        global $CFG;
-
 
188
        require_once($CFG->dirroot . '/mod/lti/locallib.php');
-
 
189
        $this->resetAfterTest();
-
 
190
        $resourceid = 'test-resource-id';
-
 
191
        $tag = 'tag';
-
 
192
        $course = $this->getDataGenerator()->create_course();
-
 
193
        $typeid = $this->create_type();
-
 
194
        $user = $this->getDataGenerator()->create_and_enrol($course);
-
 
195
 
-
 
196
        // Create mod instance with line item - nothing pushed via services yet.
-
 
197
        $gbservice = new gradebookservices();
-
 
198
        $gbservice->set_type(lti_get_type($typeid));
-
 
199
        $modinstance = $this->create_graded_lti($typeid, $course, $resourceid, $tag);
-
 
200
        $gradeitems = $gbservice->get_lineitems($course->id, null, null, null, null, null, $typeid);
-
 
201
        $this->assertEquals(1, $gradeitems[0]); // The 1st item in the array is the items count.
-
 
202
 
-
 
203
        // Post a score so that there's at least one grade present at the time of lineitem update.
-
 
204
        $score = new scores($gbservice);
-
 
205
        $_SERVER['REQUEST_METHOD'] = \mod_lti\local\ltiservice\resource_base::HTTP_POST;
-
 
206
        $_SERVER['PATH_INFO'] = "/$course->id/lineitems/{$gradeitems[1][0]->id}/lineitem/scores?type_id=$typeid";
-
 
207
        $token = lti_new_access_token($typeid, ['https://purl.imsglobal.org/spec/lti-ags/scope/score']);
-
 
208
        $_SERVER['HTTP_Authorization'] = 'Bearer '.$token->token;
-
 
209
        $_GET['type_id'] = (string)$typeid;
-
 
210
        $requestdata = [
-
 
211
            'scoreGiven' => "8.0",
-
 
212
            'scoreMaximum' => "10.0",
-
 
213
            'activityProgress' => "Completed",
-
 
214
            'timestamp' => "2024-08-07T18:54:36.736+00:00",
-
 
215
            'gradingProgress' => "FullyGraded",
-
 
216
            'userId' => $user->id,
-
 
217
        ];
-
 
218
        $response = new \mod_lti\local\ltiservice\response();
-
 
219
        $response->set_content_type('application/vnd.ims.lis.v1.score+json');
-
 
220
        $response->set_request_data(json_encode($requestdata));
-
 
221
        $score->execute($response);
-
 
222
 
-
 
223
        // The grade in Moodle should reflect the score->timestamp, not the time of the score post.
-
 
224
        $grades = grade_get_grades($course->id, 'mod', 'lti', $modinstance->id, $user->id);
-
 
225
        $studentgrade = array_shift($grades->items[0]->grades);
-
 
226
        $this->assertEquals(strtotime($requestdata['timestamp']), $studentgrade->dategraded);
-
 
227
 
-
 
228
        // Read the results via the service. This should also return the correct dategraded time (timemodified in LTI terms).
-
 
229
        $_SERVER['REQUEST_METHOD'] = \mod_lti\local\ltiservice\resource_base::HTTP_GET;
-
 
230
        $_SERVER['PATH_INFO'] = "/$course->id/lineitems/{$gradeitems[1][0]->id}/lineitem/results?type_id=$typeid";
-
 
231
        $token = lti_new_access_token($typeid, ['https://purl.imsglobal.org/spec/lti-ags/scope/result.readonly']);
-
 
232
        $_SERVER['HTTP_Authorization'] = 'Bearer '.$token->token;
-
 
233
        $_GET['type_id'] = (string)$typeid;
-
 
234
        $result = new results($gbservice);
-
 
235
        $response = new \mod_lti\local\ltiservice\response();
-
 
236
        $response->set_content_type('application/vnd.ims.lis.v2.resultcontainer+json');
-
 
237
        $result->execute($response);
-
 
238
        $body = json_decode($response->get_body());
-
 
239
        $result = array_shift($body);
-
 
240
        $this->assertEquals(strtotime($requestdata['timestamp']), strtotime($result->timestamp));
-
 
241
 
-
 
242
        // Now, try to post a newer score using a timestamp that is greater than the one originally sent, but less than the time at
-
 
243
        // which the score was last posted. This should be valid since the timestamp is greater than the original posted score.
-
 
244
        $_SERVER['REQUEST_METHOD'] = \mod_lti\local\ltiservice\resource_base::HTTP_POST;
-
 
245
        $_SERVER['PATH_INFO'] = "/$course->id/lineitems/{$gradeitems[1][0]->id}/lineitem/scores?type_id=$typeid";
-
 
246
        $token = lti_new_access_token($typeid, ['https://purl.imsglobal.org/spec/lti-ags/scope/score']);
-
 
247
        $_SERVER['HTTP_Authorization'] = 'Bearer '.$token->token;
-
 
248
        $_GET['type_id'] = (string)$typeid;
-
 
249
        $requestdata['scoreGiven'] = "14";
-
 
250
        $requestdata['timestamp'] = "2024-08-08T18:54:36.736+00:00";
-
 
251
        $response = new \mod_lti\local\ltiservice\response();
-
 
252
        $response->set_content_type('application/vnd.ims.lis.v1.score+json');
-
 
253
        $response->set_request_data(json_encode($requestdata));
-
 
254
        $score->execute($response);
-
 
255
        $this->assertEquals(200, json_decode($response->get_code()));
-
 
256
 
-
 
257
        // Finally, post a score that's just been updated (i.e. score->timestamp = now).
-
 
258
        $_SERVER['REQUEST_METHOD'] = \mod_lti\local\ltiservice\resource_base::HTTP_POST;
-
 
259
        $_SERVER['PATH_INFO'] = "/$course->id/lineitems/{$gradeitems[1][0]->id}/lineitem/scores?type_id=$typeid";
-
 
260
        $token = lti_new_access_token($typeid, ['https://purl.imsglobal.org/spec/lti-ags/scope/score']);
-
 
261
        $_SERVER['HTTP_Authorization'] = 'Bearer '.$token->token;
-
 
262
        $_GET['type_id'] = (string)$typeid;
-
 
263
        $requestdata['scoreGiven'] = "15";
-
 
264
        $requestdata['timestamp'] = date('c', time());
-
 
265
        $response = new \mod_lti\local\ltiservice\response();
-
 
266
        $response->set_content_type('application/vnd.ims.lis.v1.score+json');
-
 
267
        $response->set_request_data(json_encode($requestdata));
-
 
268
        $score->execute($response);
-
 
269
        $this->assertEquals(200, json_decode($response->get_code()));
-
 
270
    }
-
 
271
 
177
    /**
272
    /**
178
     * Inserts a graded lti instance, which should create a grade_item and gradebookservices record.
273
     * Inserts a graded lti instance, which should create a grade_item and gradebookservices record.
179
     *
274
     *
180
     * @param int $typeid Type ID of the LTI Tool.
275
     * @param int $typeid Type ID of the LTI Tool.
181
     * @param object $course course where to add the lti instance.
276
     * @param object $course course where to add the lti instance.