예제 #1
0
 /**
  * Sync sharepoint access for a list of courses and users.
  *
  * @param array $courses The courses to sync.
  * @param array $users The users to sync.
  * @param string $requiredcap The required capability.
  * @param \local\o365\rest\sharepoint $sharepoint Constructed sharepoint API client.
  * @return bool Success/Failure.
  */
 protected function sync_spsiteaccess_for_courses_and_users(array $courses, array $users, $requiredcap, \local_o365\rest\sharepoint $sharepoint)
 {
     global $DB;
     foreach ($courses as $course) {
         $courseid = is_numeric($course) ? $course : $course->id;
         $context = \context_course::instance($courseid);
         $spgroupsql = 'SELECT *
                          FROM {local_o365_coursespsite} site
                          JOIN {local_o365_spgroupdata} grp ON grp.coursespsiteid = site.id
                         WHERE site.courseid = ? AND grp.permtype = ?';
         $spgrouprec = $DB->get_record_sql($spgroupsql, [$courseid, 'contribute']);
         foreach ($users as $user) {
             $userid = is_numeric($user) ? $user : $user->id;
             $userupn = \local_o365\rest\azuread::get_muser_upn($user);
             $hascap = has_capability($requiredcap, $context, $user);
             if ($hascap === true) {
                 // Add to group.
                 $sharepoint->add_user_to_group($userupn, $spgrouprec->groupid, $userid);
             } else {
                 // Remove from group.
                 $sharepoint->remove_user_from_group($userupn, $spgrouprec->groupid, $userid);
             }
         }
     }
     return true;
 }
 /**
  * Sync sharepoint access for a list of courses and users.
  *
  * @param array $courses The courses to sync.
  * @param array $users The users to sync.
  * @param string $requiredcap The required capability.
  * @param \local\o365\rest\sharepoint $sharepoint Constructed sharepoint API client.
  * @return bool Success/Failure.
  */
 protected function sync_spsiteaccess_for_courses_and_users(array $courses, array $users, $requiredcap, \local_o365\rest\sharepoint $sharepoint)
 {
     global $DB;
     foreach ($courses as $course) {
         $courseid = is_numeric($course) ? $course : $course->id;
         $context = \context_course::instance($courseid);
         $spgroupsql = 'SELECT *
                          FROM {local_o365_coursespsite} site
                          JOIN {local_o365_spgroupdata} grp ON grp.coursespsiteid = site.id
                         WHERE site.courseid = ? AND grp.permtype = ?';
         $spgrouprec = $DB->get_record_sql($spgroupsql, [$courseid, 'contribute']);
         if (!empty($spgrouprec)) {
             foreach ($users as $user) {
                 $userid = is_numeric($user) ? $user : $user->id;
                 if (!\local_o365\utils::is_o365_connected($userid)) {
                     continue;
                 }
                 $userupn = \local_o365\rest\azuread::get_muser_upn($user);
                 $hascap = has_capability($requiredcap, $context, $user);
                 if ($hascap === true) {
                     // Add to group.
                     try {
                         mtrace('Adding user #' . $userid . ' to group id ' . $spgrouprec->groupid . '...');
                         $sharepoint->add_user_to_group($userupn, $spgrouprec->groupid, $userid);
                     } catch (\Exception $e) {
                         mtrace('Error: ' . $e->getMessage());
                     }
                 } else {
                     // Remove from group.
                     try {
                         mtrace('Removing user #' . $userid . ' from group id ' . $spgrouprec->groupid . '...');
                         $sharepoint->remove_user_from_group($userupn, $spgrouprec->groupid, $userid);
                     } catch (\Exception $e) {
                         mtrace('Error: ' . $e->getMessage());
                     }
                 }
             }
         }
     }
     return true;
 }