From d238016e0d56b36ae4c9b3fed7edace503cdbcd1 Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Fri, 22 Apr 2016 13:03:53 +0800 Subject: [PATCH] MDL-53918 tool_cohortroles: Assign role on non-empty user and cohort IDs --- admin/tool/cohortroles/index.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/admin/tool/cohortroles/index.php b/admin/tool/cohortroles/index.php index a202be65feb..d53af3e9f5c 100644 --- a/admin/tool/cohortroles/index.php +++ b/admin/tool/cohortroles/index.php @@ -57,15 +57,15 @@ if ($removeid) { require_sesskey(); // We must create them all or none. $saved = 0; - foreach ($data->userids as $userid) { - if (empty($data->cohortids)) { - $data->cohortids = array(); - } - foreach ($data->cohortids as $cohortid) { - $params = (object) array('userid' => $userid, 'cohortid' => $cohortid, 'roleid' => $data->roleid); - $result = \tool_cohortroles\api::create_cohort_role_assignment($params); - if ($result) { - $saved++; + // Loop through userids and cohortids only if both of them are not empty. + if (!empty($data->userids) && !empty($data->cohortids)) { + foreach ($data->userids as $userid) { + foreach ($data->cohortids as $cohortid) { + $params = (object) array('userid' => $userid, 'cohortid' => $cohortid, 'roleid' => $data->roleid); + $result = \tool_cohortroles\api::create_cohort_role_assignment($params); + if ($result) { + $saved++; + } } } }