Example #1
0
 /**
  * Remove a list of users given their user id from the cours
  * @param array $userIdList list of user ids to add
  * @param Claro_Class $class execute class unregistration instead of individual registration if given (default:null)
  * @param bool $keepTrackingData tracking data will be deleted if set to false (default:true, i.e. keep data)
  * @param array $moduleDataToPurge list of module_label => (purgeTracking => bool, purgeData => bool)
  * @param bool $unregisterFromSourceIfLastSession remove users that are in no other session course from the source course if any
  * @return boolean
  */
 public function removeUserIdListFromCourse($userIdList, $class = null, $keepTrackingData = true, $moduleDataToPurge = array(), $unregisterFromSourceIfLastSession = true)
 {
     if (!count($userIdList)) {
         return false;
     }
     $classMode = is_null($class) ? false : true;
     $courseCode = $this->course->courseId;
     $sqlCourseCode = $this->database->quote($courseCode);
     if ($classMode && !$class->isRegisteredToCourse($courseCode)) {
         $this->result->addError(get_lang("Class not registered to course"));
         $this->result->setStatus(Claro_BatchRegistrationResult::STATUS_ERROR_NOTHING_TO_DO);
         return false;
     }
     // update user registration counts
     $cntToChange = $classMode ? 'count_class_enrol' : 'count_user_enrol';
     $this->database->exec("\n            UPDATE\n                `{$this->tableNames['rel_course_user']}`\n            SET\n                `{$cntToChange}` = `{$cntToChange}` - 1\n            WHERE\n                `code_cours` = {$sqlCourseCode}\n            AND\n                `{$cntToChange}` > 0\n            AND\n                `user_id` IN (" . implode(',', $userIdList) . ")\n        ");
     // get the user ids to remove
     $userListToRemove = $this->database->query("\n            SELECT \n                `user_id`\n            FROM\n                `{$this->tableNames['rel_course_user']}`\n            WHERE\n                `count_class_enrol` <= 0\n            AND\n                `count_user_enrol` <= 0\n            AND\n                `code_cours` = {$sqlCourseCode}\n        ");
     if ($userListToRemove->numRows()) {
         $userIdListToRemove = array();
         foreach ($userListToRemove as $user) {
             $userIdListToRemove[] = $user['user_id'];
         }
         $sqlList = array();
         $sqlList[] = "DELETE FROM `{$this->tableNames['bb_rel_topic_userstonotify']}` WHERE user_id IN (" . implode(',', $userIdListToRemove) . ")";
         $sqlList[] = "DELETE FROM `{$this->tableNames['userinfo_content']}` WHERE user_id IN (" . implode(',', $userIdListToRemove) . ")";
         $sqlList[] = "UPDATE `{$this->tableNames['group_team']}` SET `tutor` = NULL WHERE `tutor` IN (" . implode(',', $userIdListToRemove) . ")";
         $sqlList[] = "DELETE FROM `{$this->tableNames['group_rel_team_user']}` WHERE user IN (" . implode(',', $userIdListToRemove) . ")";
         if (!$keepTrackingData) {
             $sqlList[] = "DELETE FROM `{$this->tableNames['tracking_event']}` WHERE user_id IN (" . implode(',', $userIdListToRemove) . ")";
         }
         $sqlList[] = "DELETE FROM `{$this->tableNames['rel_course_user']}` WHERE user_id IN (" . implode(',', $userIdListToRemove) . ") AND `code_cours` = {$sqlCourseCode}";
         foreach ($sqlList as $sql) {
             $this->database->exec($sql);
         }
         if (!empty($moduleDataToPurge)) {
             foreach ($moduleDataToPurge as $moduleData) {
                 $connectorPath = get_module_path($moduleData['label']) . '/connector/adminuser.cnr.php';
                 if (file_exists($connectorPath)) {
                     require_once $connectorPath;
                     $connectorClass = $moduleData['label'] . '_AdminUser';
                     if (class_exist($connectorClass)) {
                         $connector = new $connectorClass($this->database);
                         if ($moduleData['purgeTracking']) {
                             $connector->purgeUserListCourseTrackingData($userIdListToRemove, $this->course->courseId);
                         }
                         if ($moduleData['purgeResources']) {
                             $connector->purgeUserListCourseResources($userIdListToRemove, $this->course->courseId);
                         }
                     } else {
                         Console::warning("Class {$connectorClass} not found");
                     }
                 } else {
                     Console::warning("No user delete connector found for module {$moduleData['label']}");
                 }
             }
         }
         $this->result->addDeleted($userIdListToRemove);
         if ($this->course->isSourceCourse()) {
             $sessionCourseIterator = $this->course->getChildren();
             foreach ($sessionCourseIterator as $sessionCourse) {
                 $batchReg = new self($sessionCourse, $this->database);
                 $batchReg->removeUserIdListFromCourse($userIdListToRemove, $class, $keepTrackingData, $moduleDataToPurge, $unregisterFromSourceIfLastSession);
                 $this->result->mergeResult($batchReg->getResult());
             }
         }
         if ($this->course->hasSourceCourse() && $unregisterFromSourceIfLastSession) {
             $sourceCourse = $this->course->getSourceCourse();
             $sessionCourseIterator = $sourceCourse->getChildren();
             $foundSessionWithClass = false;
             if ($classMode) {
                 foreach ($sessionCourseIterator as $sessionCourse) {
                     if ($sessionCourse->courseId != $this->course->courseId && $class->isRegisteredToCourse($sessionCourse->courseId)) {
                         $foundSessionWithClass = true;
                     }
                 }
                 if (!$foundSessionWithClass) {
                     $batchReg = new self($sourceCourse, $this->database);
                     $batchReg->removeUserIdListFromCourse($userIdListToRemove, $class, $keepTrackingData, $moduleDataToPurge, $unregisterFromSourceIfLastSession);
                 }
             } else {
                 // get userids registered in other sessions than the current one
                 $sessionList = $sourceCourse->getChildrenList();
                 if (count($sessionList)) {
                     $userIdListToRemoveFromSource = array();
                     $sessionIdList = array_keys($sessionList);
                     $sqlCourseCode = $this->database->quote($this->course->courseId);
                     $usersInOtherSessions = $this->database->query("\n                            SELECT\n                                user_id\n                            FROM\n                                `{$this->tableNames['rel_course_user']}`\n                            WHERE\n                                user_id IN (" . implode(',', $userIdListToRemove) . ")\n                            AND\n                                code_cours IN ('" . implode("','", $sessionIdList) . "')\n                            AND\n                                code_cours != {$sqlCourseCode}\n                        ");
                     // loop on $userIdList and keep only those who are not in another session and inject them in $userIdListToRemoveFromSource
                     $usersInOtherSessionsList = array();
                     foreach ($usersInOtherSessions as $userNotToRemove) {
                         $usersInOtherSessionsList[$userNotToRemove['user_id']] = $userNotToRemove['user_id'];
                     }
                     foreach ($userListToRemove as $userIdToRemove) {
                         if (!isset($usersInOtherSessionsList[$userIdToRemove['user_id']])) {
                             $userIdListToRemoveFromSource[] = $userIdToRemove['user_id'];
                         }
                     }
                     if (count($userIdListToRemoveFromSource)) {
                         $batchReg = new self($sourceCourse, $this->database);
                         $batchReg->removeUserIdListFromCourse($userIdListToRemoveFromSource, $class, $keepTrackingData, $moduleDataToPurge, $unregisterFromSourceIfLastSession);
                         $this->result->mergeResult($batchReg->getResult());
                     }
                 }
             }
         }
     } else {
         $this->result->setStatus(Claro_BatchRegistrationResult::STATUS_ERROR_NOTHING_TO_DO);
         $this->result->addError(get_lang("No user to delete"));
     }
     return !$this->result->hasError();
 }