Exemplo n.º 1
0
 public static final function Delete($lesson_id)
 {
     global $USER_FIELD_MANAGER;
     list($lesson_id, $simulate, $check_permissions, $user_id) = self::_funcDelete_ParseOptions($lesson_id);
     if ($check_permissions) {
         $oAccess = CLearnAccess::GetInstance($user_id);
         if (!$oAccess->IsLessonAccessible($lesson_id, CLearnAccess::OP_LESSON_REMOVE)) {
             throw new LearnException('EA_ACCESS_DENIED', LearnException::EXC_ERR_ALL_ACCESS_DENIED);
         }
     }
     // Parents and childs of the lesson
     $arNeighboursEdges = self::ListImmediateNeighbours($lesson_id);
     // precache rights for lesson
     if ($check_permissions) {
         $IsLessonAccessibleFor_OP_LESSON_UNLINK_DESCENDANTS = $oAccess->IsLessonAccessible($lesson_id, CLearnAccess::OP_LESSON_UNLINK_DESCENDANTS);
         $IsLessonAccessibleFor_OP_LESSON_UNLINK_FROM_PARENTS = $oAccess->IsLessonAccessible($lesson_id, CLearnAccess::OP_LESSON_UNLINK_FROM_PARENTS);
     }
     foreach (GetModuleEvents('learning', 'OnBeforeLessonDelete', true) as $arEvent) {
         ExecuteModuleEventEx($arEvent, array($lesson_id));
     }
     foreach ($arNeighboursEdges as $arEdge) {
         $child_lesson_id = (int) $arEdge['CHILD_LESSON'];
         $parent_lesson_id = (int) $arEdge['PARENT_LESSON'];
         if ($check_permissions) {
             $IsLessonAccessible = false;
             if ($child_lesson_id === $lesson_id) {
                 // if we will be remove edge to parent - use precached rights for OP_LESSON_UNLINK_FROM_PARENTS
                 $IsLessonAccessible = $IsLessonAccessibleFor_OP_LESSON_UNLINK_FROM_PARENTS && $oAccess->IsLessonAccessible($parent_lesson_id, CLearnAccess::OP_LESSON_UNLINK_DESCENDANTS);
             } elseif ($parent_lesson_id === $lesson_id) {
                 // if we will be remove edge to child - use precached rights for OP_LESSON_UNLINK_DESCENDANTS
                 $IsLessonAccessible = $IsLessonAccessibleFor_OP_LESSON_UNLINK_DESCENDANTS && $oAccess->IsLessonAccessible($child_lesson_id, CLearnAccess::OP_LESSON_UNLINK_FROM_PARENTS);
             } else {
                 throw new LearnException('EA_FATAL: $lesson_id (' . $lesson_id . ') not equal to one of: $child_lesson_id (' . $child_lesson_id . '), $parent_lesson_id (' . $parent_lesson_id . ')', LearnException::EXC_ERR_ALL_LOGIC | LearnException::EXC_ERR_ALL_GIVEUP);
             }
             if (!$IsLessonAccessible) {
                 throw new LearnException('EA_ACCESS_DENIED', LearnException::EXC_ERR_ALL_ACCESS_DENIED);
             }
             if ($simulate === false) {
                 self::RelationRemove($parent_lesson_id, $child_lesson_id);
             }
         }
     }
     $linkedCourseId = self::GetLinkedCourse($lesson_id);
     // If lesson is course, remove course
     if ($linkedCourseId !== false) {
         global $DB;
         if ($simulate === false) {
             if (!$DB->Query("DELETE FROM b_learn_course_site WHERE COURSE_ID = " . (int) $linkedCourseId, true)) {
                 throw new LearnException('EA_SQLERROR', LearnException::EXC_ERR_ALL_GIVEUP);
             }
             $rc = self::CourseBecomeLesson($linkedCourseId);
             // if course cannot be converted to lesson - don't remove lesson
             if ($rc === false) {
                 throw new LearnException('EA_OTHER: lesson is unremovable because linked course is in use.', LearnException::EXC_ERR_LL_UNREMOVABLE_CL);
             }
             // reload cache of LINKED_LESSON_ID -> COURSE_ID
             self::GetCourseToLessonMap_ReloadCache();
             if (CModule::IncludeModule("search")) {
                 CSearch::DeleteIndex("learning", false, "C" . $linkedCourseId);
                 CSearch::DeleteIndex("learning", "C" . $linkedCourseId);
             }
         }
     }
     // And remove lesson
     if ($simulate === false) {
         global $DB;
         $r = $DB->Query("SELECT PREVIEW_PICTURE, DETAIL_PICTURE \n\t\t\t\tFROM b_learn_lesson \n\t\t\t\tWHERE ID = " . (int) $lesson_id, true);
         if ($r === false) {
             throw new LearnException('EA_SQLERROR', LearnException::EXC_ERR_ALL_GIVEUP);
         }
         $arRes = $r->Fetch();
         if (!$arRes) {
             throw new LearnException('EA_SQLERROR', LearnException::EXC_ERR_ALL_GIVEUP);
         }
         CFile::Delete($arRes['PREVIEW_PICTURE']);
         CFile::Delete($arRes['DETAIL_PICTURE']);
         // Remove questions
         $q = CLQuestion::GetList(array(), array('LESSON_ID' => $lesson_id));
         while ($arQ = $q->Fetch()) {
             if (!CLQuestion::Delete($arQ['ID'])) {
                 throw new LearnException('EA_QUESTION_NOT_REMOVED', LearnException::EXC_ERR_ALL_GIVEUP);
             }
         }
         CLearnGraphNode::Remove($lesson_id);
         $USER_FIELD_MANAGER->delete('LEARNING_LESSONS', $lesson_id);
         CLearnCacheOfLessonTreeComponent::MarkAsDirty();
         CEventLog::add(array('AUDIT_TYPE_ID' => 'LEARNING_REMOVE_ITEM', 'MODULE_ID' => 'learning', 'ITEM_ID' => 'L #' . $lesson_id, 'DESCRIPTION' => 'lesson removed'));
         if (CModule::IncludeModule('search')) {
             CSearch::DeleteIndex('learning', false, 'L' . $lesson_id);
             CSearch::DeleteIndex('learning', 'L' . $lesson_id);
         }
     }
     if ($simulate === false) {
         foreach (GetModuleEvents('learning', 'OnAfterLessonDelete', true) as $arEvent) {
             ExecuteModuleEventEx($arEvent, array($lesson_id));
         }
     }
 }