Skip to content

Commit

Permalink
Merge branch 'MDL-52974-master' of git://github.com/jleyva/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Mar 1, 2016
2 parents c116a11 + e3e036e commit 05b0f57
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,7 @@
'core_user_get_users_by_field',
'core_user_add_user_private_files',
'mod_assign_view_grading_table',
'mod_assign_view_submission_status',
'mod_scorm_view_scorm',
'mod_scorm_get_scorm_scoes',
'mod_scorm_get_scorm_user_data',
Expand Down
8 changes: 8 additions & 0 deletions mod/assign/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,12 @@
'capabilities' => 'mod/assign:view, mod/assign:viewgrades'
),

'mod_assign_view_submission_status' => array(
'classname' => 'mod_assign_external',
'methodname' => 'view_submission_status',
'classpath' => 'mod/assign/externallib.php',
'description' => 'Trigger the submission status viewed event.',
'type' => 'write',
'capabilities' => 'mod/assign:view'
),
);
62 changes: 62 additions & 0 deletions mod/assign/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2122,4 +2122,66 @@ public static function view_grading_table_returns() {
)
);
}
/**
* Describes the parameters for view_submission_status.
*
* @return external_external_function_parameters
* @since Moodle 3.1
*/
public static function view_submission_status_parameters() {
return new external_function_parameters (
array(
'assignid' => new external_value(PARAM_INT, 'assign instance id'),
)
);
}

/**
* Trigger the submission status viewed event.
*
* @param int $assignid assign instance id
* @return array of warnings and status result
* @since Moodle 3.1
*/
public static function view_submission_status($assignid) {
global $DB;

$warnings = array();
$params = array(
'assignid' => $assignid,
);
$params = self::validate_parameters(self::view_submission_status_parameters(), $params);

// Request and permission validation.
$assign = $DB->get_record('assign', array('id' => $params['assignid']), 'id', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($assign, 'assign');

$context = context_module::instance($cm->id);
// Please, note that is not required to check mod/assign:view because is done by validate_context->require_login.
self::validate_context($context);

$assign = new assign($context, $cm, $course);
\mod_assign\event\submission_status_viewed::create_from_assign($assign)->trigger();

$result = array();
$result['status'] = true;
$result['warnings'] = $warnings;
return $result;
}

/**
* Describes the view_submission_status return value.
*
* @return external_single_structure
* @since Moodle 3.1
*/
public static function view_submission_status_returns() {
return new external_single_structure(
array(
'status' => new external_value(PARAM_BOOL, 'status: true if success'),
'warnings' => new external_warnings(),
)
);
}

}
69 changes: 69 additions & 0 deletions mod/assign/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1600,4 +1600,73 @@ public function test_subplugins_availability() {
$this->assertEquals(VALUE_OPTIONAL, $parameters->keys['plugindata']->keys['assignfeedbackcomments_editor']->required);
}

/**
* Test test_view_submission_status
*/
public function test_view_submission_status() {
global $DB;

$this->resetAfterTest(true);

$this->setAdminUser();
// Setup test data.
$course = $this->getDataGenerator()->create_course();
$assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
$context = context_module::instance($assign->cmid);
$cm = get_coursemodule_from_instance('assign', $assign->id);

// Test invalid instance id.
try {
mod_assign_external::view_submission_status(0);
$this->fail('Exception expected due to invalid mod_assign instance id.');
} catch (moodle_exception $e) {
$this->assertEquals('invalidrecord', $e->errorcode);
}

// Test not-enrolled user.
$user = self::getDataGenerator()->create_user();
$this->setUser($user);
try {
mod_assign_external::view_submission_status($assign->id);
$this->fail('Exception expected due to not enrolled user.');
} catch (moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
}

// Test user with full capabilities.
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);

// Trigger and capture the event.
$sink = $this->redirectEvents();

$result = mod_assign_external::view_submission_status($assign->id);
$result = external_api::clean_returnvalue(mod_assign_external::view_submission_status_returns(), $result);

$events = $sink->get_events();
$this->assertCount(1, $events);
$event = array_shift($events);

// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_assign\event\submission_status_viewed', $event);
$this->assertEquals($context, $event->get_context());
$moodleurl = new \moodle_url('/mod/assign/view.php', array('id' => $cm->id));
$this->assertEquals($moodleurl, $event->get_url());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());

// Test user with no capabilities.
// We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
assign_capability('mod/assign:view', CAP_PROHIBIT, $studentrole->id, $context->id);
accesslib_clear_all_caches_for_unit_testing();
course_modinfo::clear_instance_cache();

try {
mod_assign_external::view_submission_status($assign->id);
$this->fail('Exception expected due to missing capability.');
} catch (moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
}
}

}
2 changes: 1 addition & 1 deletion mod/assign/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'mod_assign'; // Full name of the plugin (used for diagnostics).
$plugin->version = 2015111600; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2015111601; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2015111000; // Requires this Moodle version.
$plugin->cron = 60;
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

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

$version = 2016030100.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2016030101.00; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.

Expand Down

0 comments on commit 05b0f57

Please sign in to comment.