Skip to content

Commit

Permalink
MDL-55162 mod_assign: Refactor to use new validate_assign helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Jul 12, 2016
1 parent 7336b66 commit b93ea10
Showing 1 changed file with 18 additions and 101 deletions.
119 changes: 18 additions & 101 deletions mod/assign/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -933,11 +933,8 @@ public static function set_user_flags($assignmentid, $userflags = array()) {
'userflags' => $userflags));

// Load assignment if it exists and if the user has the capability.
$cm = get_coursemodule_from_instance('assign', $params['assignmentid'], 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
self::validate_context($context);
list($assign, $course, $cm, $context) = self::validate_assign($params['assignmentid']);
require_capability('mod/assign:grade', $context);
$assign = new assign($context, null, null);

$results = array();
foreach ($params['userflags'] as $userflag) {
Expand Down Expand Up @@ -1363,11 +1360,7 @@ public static function lock_submissions($assignmentid, $userids) {
array('assignmentid' => $assignmentid,
'userids' => $userids));

$cm = get_coursemodule_from_instance('assign', $params['assignmentid'], 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
self::validate_context($context);

$assignment = new assign($context, $cm, null);
list($assignment, $course, $cm, $context) = self::validate_assign($params['assignmentid']);

$warnings = array();
foreach ($params['userids'] as $userid) {
Expand Down Expand Up @@ -1424,11 +1417,7 @@ public static function revert_submissions_to_draft($assignmentid, $userids) {
array('assignmentid' => $assignmentid,
'userids' => $userids));

$cm = get_coursemodule_from_instance('assign', $params['assignmentid'], 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
self::validate_context($context);

$assignment = new assign($context, $cm, null);
list($assignment, $course, $cm, $context) = self::validate_assign($params['assignmentid']);

$warnings = array();
foreach ($params['userids'] as $userid) {
Expand Down Expand Up @@ -1485,11 +1474,7 @@ public static function unlock_submissions($assignmentid, $userids) {
array('assignmentid' => $assignmentid,
'userids' => $userids));

$cm = get_coursemodule_from_instance('assign', $params['assignmentid'], 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
self::validate_context($context);

$assignment = new assign($context, $cm, null);
list($assignment, $course, $cm, $context) = self::validate_assign($params['assignmentid']);

$warnings = array();
foreach ($params['userids'] as $userid) {
Expand Down Expand Up @@ -1551,11 +1536,7 @@ public static function submit_grading_form($assignmentid, $userid, $jsonformdata
'jsonformdata' => $jsonformdata
));

$cm = get_coursemodule_from_instance('assign', $params['assignmentid'], 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
self::validate_context($context);

$assignment = new assign($context, $cm, null);
list($assignment, $course, $cm, $context) = self::validate_assign($params['assignmentid']);

$serialiseddata = json_decode($params['jsonformdata']);

Expand Down Expand Up @@ -1627,11 +1608,7 @@ public static function submit_for_grading($assignmentid, $acceptsubmissionstatem
array('assignmentid' => $assignmentid,
'acceptsubmissionstatement' => $acceptsubmissionstatement));

$cm = get_coursemodule_from_instance('assign', $params['assignmentid'], 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
self::validate_context($context);

$assignment = new assign($context, $cm, null);
list($assignment, $course, $cm, $context) = self::validate_assign($params['assignmentid']);

$warnings = array();
$data = new stdClass();
Expand Down Expand Up @@ -1705,11 +1682,7 @@ public static function save_user_extensions($assignmentid, $userids, $dates) {
return $warnings;
}

$cm = get_coursemodule_from_instance('assign', $params['assignmentid'], 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
self::validate_context($context);

$assignment = new assign($context, $cm, null);
list($assignment, $course, $cm, $context) = self::validate_assign($params['assignmentid']);

$warnings = array();
foreach ($params['userids'] as $idx => $userid) {
Expand Down Expand Up @@ -1761,11 +1734,7 @@ public static function reveal_identities($assignmentid) {
$params = self::validate_parameters(self::reveal_identities_parameters(),
array('assignmentid' => $assignmentid));

$cm = get_coursemodule_from_instance('assign', $params['assignmentid'], 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
self::validate_context($context);

$assignment = new assign($context, $cm, null);
list($assignment, $course, $cm, $context) = self::validate_assign($params['assignmentid']);

$warnings = array();
if (!$assignment->reveal_identities()) {
Expand Down Expand Up @@ -1832,11 +1801,7 @@ public static function save_submission($assignmentid, $plugindata) {
array('assignmentid' => $assignmentid,
'plugindata' => $plugindata));

$cm = get_coursemodule_from_instance('assign', $params['assignmentid'], 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
self::validate_context($context);

$assignment = new assign($context, $cm, null);
list($assignment, $course, $cm, $context) = self::validate_assign($params['assignmentid']);

$notices = array();

Expand Down Expand Up @@ -1963,11 +1928,7 @@ public static function save_grade($assignmentid,
'plugindata' => $plugindata,
'advancedgradingdata' => $advancedgradingdata));

$cm = get_coursemodule_from_instance('assign', $params['assignmentid'], 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
self::validate_context($context);

$assignment = new assign($context, $cm, null);
list($assignment, $course, $cm, $context) = self::validate_assign($params['assignmentid']);

$gradedata = (object)$params['plugindata'];

Expand Down Expand Up @@ -2101,10 +2062,7 @@ public static function save_grades($assignmentid, $applytoall = false, $grades)
'applytoall' => $applytoall,
'grades' => $grades));

$cm = get_coursemodule_from_instance('assign', $params['assignmentid'], 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
self::validate_context($context);
$assignment = new assign($context, $cm, null);
list($assignment, $course, $cm, $context) = self::validate_assign($params['assignmentid']);

if ($assignment->get_instance()->teamsubmission && $params['applytoall']) {
// Check that only 1 user per submission group is provided.
Expand Down Expand Up @@ -2184,11 +2142,7 @@ public static function copy_previous_attempt($assignmentid) {
$params = self::validate_parameters(self::copy_previous_attempt_parameters(),
array('assignmentid' => $assignmentid));

$cm = get_coursemodule_from_instance('assign', $assignmentid, 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
self::validate_context($context);

$assignment = new assign($context, $cm, null);
list($assignment, $course, $cm, $context) = self::validate_assign($params['assignmentid']);

$notices = array();

Expand Down Expand Up @@ -2237,24 +2191,15 @@ public static function view_grading_table_parameters() {
* @throws moodle_exception
*/
public static function view_grading_table($assignid) {
global $DB, $CFG;

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

// 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);
self::validate_context($context);

require_capability('mod/assign:view', $context);
list($assign, $course, $cm, $context) = self::validate_assign($params['assignid']);

$assign = new assign($context, null, null);
$assign->require_view_grades();
\mod_assign\event\grading_table_viewed::create_from_assign($assign)->trigger();

Expand Down Expand Up @@ -2301,23 +2246,15 @@ public static function view_submission_status_parameters() {
* @since Moodle 3.1
*/
public static function view_submission_status($assignid) {
global $DB, $CFG;

$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);
list($assign, $course, $cm, $context) = self::validate_assign($params['assignid']);

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

$result = array();
Expand Down Expand Up @@ -2366,7 +2303,7 @@ public static function get_submission_status_parameters() {
* @throws required_capability_exception
*/
public static function get_submission_status($assignid, $userid = 0) {
global $USER, $DB;
global $USER;

$warnings = array();

Expand All @@ -2376,14 +2313,7 @@ public static function get_submission_status($assignid, $userid = 0) {
);
$params = self::validate_parameters(self::get_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);
self::validate_context($context);

$assign = new assign($context, $cm, $course);
list($assign, $course, $cm, $context) = self::validate_assign($params['assignid']);

// Default value for userid.
if (empty($params['userid'])) {
Expand Down Expand Up @@ -2629,16 +2559,10 @@ public static function list_participants($assignid, $groupid, $filter, $skip, $l
));
$warnings = array();

// 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);
self::validate_context($context);
list($assign, $course, $cm, $context) = self::validate_assign($params['assignid']);

require_capability('mod/assign:view', $context);

$assign = new assign($context, null, null);
$assign->require_view_grades();

$participants = $assign->list_participants_with_filter_status_and_group($params['groupid']);
Expand Down Expand Up @@ -2794,14 +2718,7 @@ public static function get_participant($assignid, $userid, $embeduser) {
'embeduser' => $embeduser
));

// 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);
self::validate_context($context);

$assign = new assign($context, null, null);
list($assign, $course, $cm, $context) = self::validate_assign($params['assignid']);
$assign->require_view_grades();

$participant = $assign->get_participant($params['userid']);
Expand Down

0 comments on commit b93ea10

Please sign in to comment.