Browse Source

Webservice: allow multiple instances in one course (#372)

* [WS] Allow multiple instances in same course

You can now retrieve multiple instances in the one course

* Add test for multiple instances

Also cleaned up a lot of test code… there was a bunch of stuff doing
nothing just sitting in the file. All the stuff with getting the module
and the context… that’s all handled by the data generator.

* satisfy the linter

bloody thing
MOODLE_35_STABLE
Morgan Harris 6 years ago
committed by Dan Marsden
parent
commit
71bc7f15aa
  1. 2
      classes/attendance_webservices_handler.php
  2. 46
      tests/attendance_webservices_test.php

2
classes/attendance_webservices_handler.php

@ -51,7 +51,9 @@ class attendance_handler {
$context = context_course::instance($attendance->course); $context = context_course::instance($attendance->course);
if (has_capability('mod/attendance:takeattendances', $context, $userid)) { if (has_capability('mod/attendance:takeattendances', $context, $userid)) {
$course = $usercourses[$attendance->course]; $course = $usercourses[$attendance->course];
if (!isset($course->attendance_instance)) {
$course->attendance_instance = array(); $course->attendance_instance = array();
}
$att = new stdClass(); $att = new stdClass();
$att->id = $attendance->id; $att->id = $attendance->id;

46
tests/attendance_webservices_test.php

@ -59,20 +59,7 @@ class attendance_webservices_tests extends advanced_testcase {
$this->category = $this->getDataGenerator()->create_category(); $this->category = $this->getDataGenerator()->create_category();
$this->course = $this->getDataGenerator()->create_course(array('category' => $this->category->id)); $this->course = $this->getDataGenerator()->create_course(array('category' => $this->category->id));
$record = new stdClass(); $this->attendance = $this->create_attendance();
$record->course = $this->course->id;
$record->name = "Attendance";
$record->grade = 100;
$DB->insert_record('attendance', $record);
$this->getDataGenerator()->create_module('attendance', array('course' => $this->course->id));
$moduleid = $DB->get_field('modules', 'id', array('name' => 'attendance'));
$cm = $DB->get_record('course_modules', array('course' => $this->course->id, 'module' => $moduleid));
$context = context_course::instance($this->course->id);
$att = $DB->get_record('attendance', array('id' => $cm->instance), '*', MUST_EXIST);
$this->attendance = new mod_attendance_structure($att, $cm, $this->course, $context);
$this->create_and_enrol_users(); $this->create_and_enrol_users();
@ -96,6 +83,14 @@ class attendance_webservices_tests extends advanced_testcase {
$this->attendance->add_sessions($this->sessions); $this->attendance->add_sessions($this->sessions);
} }
private function create_attendance() {
global $DB;
$att = $this->getDataGenerator()->create_module('attendance', array('course' => $this->course->id));
$cm = $DB->get_record('course_modules', array('id' => $att->cmid));
unset($att->cmid);
return new mod_attendance_structure($att, $cm, $this->course);
}
/** Creating 10 students and 1 teacher. */ /** Creating 10 students and 1 teacher. */
protected function create_and_enrol_users() { protected function create_and_enrol_users() {
$this->students = array(); $this->students = array();
@ -125,6 +120,25 @@ class attendance_webservices_tests extends advanced_testcase {
$this->assertEquals(count($attendanceinstance['today_sessions']), 2); $this->assertEquals(count($attendanceinstance['today_sessions']), 2);
} }
public function test_get_courses_with_today_sessions_multiple_instances() {
$this->resetAfterTest(true);
// Make another attendance.
$second = $this->create_attendance();
// Just add the same session.
$secondsession = clone $this->sessions[0];
$secondsession->sessdate += 3600;
$second->add_sessions([$secondsession]);
$courseswithsessions = attendance_handler::get_courses_with_today_sessions($this->teacher->id);
$this->assertTrue(is_array($courseswithsessions));
$this->assertEquals(count($courseswithsessions), 1);
$course = array_pop($courseswithsessions);
$this->assertEquals(count($course->attendance_instances), 2);
}
public function test_get_session() { public function test_get_session() {
$this->resetAfterTest(true); $this->resetAfterTest(true);
@ -157,10 +171,10 @@ class attendance_webservices_tests extends advanced_testcase {
} }
// Add a session that's identical to the first, but with a group. // Add a session that's identical to the first, but with a group.
$session = $this->sessions[0]; $session = clone $this->sessions[0];
$session->groupid = $group->id; $session->groupid = $group->id;
$session->sessdate += 3600; // Make sure it appears second in the list. $session->sessdate += 3600; // Make sure it appears second in the list.
$this->attendance->add_sessions($this->sessions); $this->attendance->add_sessions([$session]);
$courseswithsessions = attendance_handler::get_courses_with_today_sessions($this->teacher->id); $courseswithsessions = attendance_handler::get_courses_with_today_sessions($this->teacher->id);

Loading…
Cancel
Save