示例#1
0
 public static function OnSearchReindex($NS = array(), $oCallback = null, $callback_method = '')
 {
     global $DB;
     static $arCourseToSiteCache = array();
     $arResult = array();
     $arAllSitesPathes = array();
     $elementStartId = 0;
     $indexElementType = 'C';
     // start reindex from courses
     $by = $order = '';
     $sites = CLang::GetList($by, $order, array('TYPE' => 'C'));
     while ($site = $sites->Fetch()) {
         $arAllSitesPathes[$site['LID']] = array('C' => CCourse::GetSitePathes($site['LID'], 'C'), 'H' => CCourse::GetSitePathes($site['LID'], 'H'), 'L' => CCourse::GetSitePathes($site['LID'], 'L'));
     }
     $arCoursesFilter = array();
     $arLessonsFilter = array('LINKED_LESSON_ID' => '');
     if ($NS['MODULE'] === 'learning' && strlen($NS['ID']) > 0) {
         $indexElementType = substr($NS['ID'], 0, 1);
         $elementStartId = (int) substr($NS['ID'], 1);
         if (strlen($NS['SITE_ID']) > 0) {
             $arCoursesFilter['SITE_ID'] = $NS['SITE_ID'];
         }
     }
     $arCoursesFilter['>ID'] = $elementStartId;
     if ($indexElementType === 'C') {
         $rsCourse = CCourse::GetList(array('ID' => 'ASC'), $arCoursesFilter);
         while ($arCourse = $rsCourse->Fetch()) {
             try {
                 $arCourse["SITE_ID"] = CCourse::GetSiteId($arCourse['ID']);
                 $arPathes = $arAllSitesPathes[$arCourse['SITE_ID']]['C'];
                 $linkedLessonId = CCourse::CourseGetLinkedLesson($arCourse['ID']);
                 if ($linkedLessonId === false) {
                     continue;
                 }
                 $arGroupPermissions = CLearnAccess::GetSymbolsAccessibleToLesson($linkedLessonId, CLearnAccess::OP_LESSON_READ);
             } catch (LearnException $e) {
                 continue;
                 // skip indexation of this item
             }
             $arSiteIds = array();
             foreach ($arPathes as $k => $path) {
                 $arCourse["PATH"] = $path;
                 $Url = str_replace("#COURSE_ID#", $arCourse["ID"], $arCourse["PATH"]);
                 $arSiteIds[$arCourse['SITE_ID']] = $Url;
             }
             if ($arCourse["DETAIL_TEXT_TYPE"] !== 'text') {
                 $detailText = CSearch::KillTags($arCourse['DETAIL_TEXT']);
             } else {
                 $detailText = strip_tags($arCourse['DETAIL_TEXT']);
             }
             if (strlen($detailText) > 0) {
                 $dataBody = $detailText;
             } else {
                 $dataBody = $arCourse['NAME'];
             }
             $Result = array("ID" => "C" . $arCourse["ID"], "LAST_MODIFIED" => $arCourse["TIMESTAMP_X"], "TITLE" => $arCourse["NAME"], "BODY" => $dataBody, "SITE_ID" => $arSiteIds, "PERMISSIONS" => $arGroupPermissions, "COURSE_ID" => "C" . $arCourse["ID"]);
             if ($oCallback) {
                 $res = call_user_func(array($oCallback, $callback_method), $Result);
                 if (!$res) {
                     return "C" . $arCourse["ID"];
                 }
             } else {
                 $arResult[] = $Result;
             }
         }
         // Reindex of courses finished. Let's reindex lessons now.
         $indexElementType = 'U';
         $elementStartId = 0;
     }
     $arLessonsFilter['>LESSON_ID'] = $elementStartId;
     if ($indexElementType === 'U') {
         $rsLessons = CLearnLesson::GetList(array('LESSON_ID' => 'ASC'), $arLessonsFilter);
         while ($arLessonFromDb = $rsLessons->Fetch()) {
             $arLessonsWithCourse = array();
             // list of lessons in context of some course
             $arOParentPathes = CLearnLesson::GetListOfParentPathes($arLessonFromDb['LESSON_ID']);
             foreach ($arOParentPathes as $oParentPath) {
                 $arParentLessons = $oParentPath->GetPathAsArray();
                 foreach ($arParentLessons as $lessonId) {
                     $linkedCourseId = CLearnLesson::GetLinkedCourse($lessonId);
                     if ($linkedCourseId !== false && $linkedCourseId > 0) {
                         $arLessonsWithCourse[] = array_merge($arLessonFromDb, array('PARENT_COURSE_ID' => $linkedCourseId));
                     }
                 }
             }
             foreach ($arLessonsWithCourse as $arLesson) {
                 try {
                     $arGroupPermissions = CLearnAccess::GetSymbolsAccessibleToLesson($arLesson['LESSON_ID'], CLearnAccess::OP_LESSON_READ);
                     $courseId = $arLesson['PARENT_COURSE_ID'];
                     if (!isset($arCourseToSiteCache[$courseId])) {
                         $strSql = "SELECT SITE_ID FROM b_learn_course_site WHERE COURSE_ID=" . (int) $courseId;
                         $rc = $DB->Query($strSql, true);
                         if ($rc === false) {
                             continue;
                         }
                         $arCourseToSiteCache[$courseId] = array();
                         while ($arCourseSite = $rc->fetch()) {
                             $arCourseToSiteCache[$courseId][] = $arCourseSite['SITE_ID'];
                         }
                     }
                     $arAllowedSites = $arCourseToSiteCache[$courseId];
                     if (empty($arAllowedSites)) {
                         continue;
                     }
                     $arSiteIds = array();
                     $lessonType = 'L';
                     if ($arLesson['IS_CHILDS']) {
                         $lessonType = 'H';
                     }
                     foreach ($arAllSitesPathes as $siteId => $arSitePathesByLessonType) {
                         if (!in_array($siteId, $arAllowedSites, true)) {
                             continue;
                         }
                         foreach ($arSitePathesByLessonType as $someLessonType => $arPathes) {
                             // skip wrong types of lessons
                             if ($lessonType !== $someLessonType) {
                                 continue;
                             }
                             foreach ($arPathes as $k => $path) {
                                 if ($lessonType == 'H') {
                                     $Url = str_replace("#CHAPTER_ID#", '0' . $arLesson['LESSON_ID'], $path);
                                 } else {
                                     $Url = str_replace("#LESSON_ID#", $arLesson['LESSON_ID'], $path);
                                 }
                                 $Url = str_replace("#COURSE_ID#", $arLesson['PARENT_COURSE_ID'], $Url);
                                 $arSiteIds[$siteId] = $Url;
                             }
                         }
                     }
                 } catch (LearnException $e) {
                     continue;
                     // skip indexation of this item
                 }
                 if ($arLesson["DETAIL_TEXT_TYPE"] !== 'text') {
                     $detailText = CSearch::KillTags($arLesson['DETAIL_TEXT']);
                 } else {
                     $detailText = strip_tags($arLesson['DETAIL_TEXT']);
                 }
                 if (strlen($detailText) > 0) {
                     $dataBody = $detailText;
                 } else {
                     $dataBody = $arLesson['NAME'];
                 }
                 $Result = array("ID" => 'U' . $arLesson['LESSON_ID'], "LAST_MODIFIED" => $arLesson['TIMESTAMP_X'], "TITLE" => $arLesson['NAME'], "BODY" => $dataBody, "SITE_ID" => $arSiteIds, "PERMISSIONS" => $arGroupPermissions);
                 if ($oCallback) {
                     $res = call_user_func(array($oCallback, $callback_method), $Result);
                     if (!$res) {
                         return 'U' . $arLesson['LESSON_ID'];
                     }
                 } else {
                     $arResult[] = $Result;
                 }
             }
         }
     }
     if ($oCallback) {
         $rc = false;
     } else {
         $rc = $arResult;
     }
     return $rc;
 }