1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
import.php
См. документацию.
1<?php
2
3// 2012-04-19 Checked/modified for compatibility with new data model
5{
7 var $LAST_ERROR = "";
8 var $arManifest = Array();
9 var $arSITE_ID = Array();
10 var $COURSE_ID = 0;
12 var $arDraftFields = Array("detail_text", "preview_text", "description");
13 var $arUnsetFields = Array("id", "site_id", "timestamp_x", 'date_create',
14 "chapter_id", "course_id", "lesson_id", "question_id",
15 "created_by", 'created_user_name', 'linked_lesson_id',
16 'childs_cnt', 'is_childs', 'description', 'description_type',
17 'was_chapter_id');
18 var $arPicture = Array("detail_picture", "preview_picture", "file_id");
19 var $arDate = Array("active_from", "active_to");
20 var $arWarnings = Array();
21 protected $arPreventUnsetFieldsForTest = array('description', 'description_type');
22
23
24 // List of fields, writable to unilessons
25 protected $arLessonWritableFields = array('NAME', 'ACTIVE', 'CODE',
26 'PREVIEW_PICTURE', 'PREVIEW_TEXT', 'PREVIEW_TEXT_TYPE',
27 'DETAIL_PICTURE', 'DETAIL_TEXT', 'DETAIL_TEXT_TYPE',
28 'LAUNCH', 'KEYWORDS');
29
30
31 // 2012-04-18 Checked/modified for compatibility with new data model
32 public function __construct($PACKAGE_DIR, $arSITE_ID)
33 {
34 //Cut last slash
35 if (mb_substr($PACKAGE_DIR, -1, 1) == "/")
36 $PACKAGE_DIR = mb_substr($PACKAGE_DIR, 0, -1);
37
38 $this->package_dir = $_SERVER["DOCUMENT_ROOT"].$PACKAGE_DIR;
39
40 //Dir exists?
41 if (!is_dir($this->package_dir))
42 {
43 $this->LAST_ERROR = GetMessage("LEARNING_BAD_PACKAGE")."<br>";
44 return false;
45 }
46
47 //Manifest exists?
48 if (!is_file($this->package_dir."/imsmanifest.xml"))
49 {
50 $this->LAST_ERROR = GetMessage("LEARNING_MANIFEST_NOT_FOUND")."<br>";
51 return false;
52 }
53
54 //Sites check
55 if (!is_array($arSITE_ID) || empty($arSITE_ID))
56 {
57 $this->LAST_ERROR = GetMessage("LEARNING_BAD_SITE_ID")."<br>";
58 return false;
59 }
60
61 $this->arSITE_ID = $arSITE_ID;
62
63 $this->objXML = new CDataXML();
64 if (!$this->objXML->Load($this->package_dir."/imsmanifest.xml"))
65 {
66 $this->LAST_ERROR = GetMessage("LEARNING_MANIFEST_NOT_FOUND")."<br>";
67 return false;
68 }
69
70 return true;
71 }
72
73
74 // 2012-04-18 Checked/modified for compatibility with new data model
75 protected function CreateCourse()
76 {
77 global $APPLICATION;
78
79 if ($this->LAST_ERROR <> '')
80 return false;
81
82 if (!$title = $this->objXML->SelectNodes("/manifest/organizations/organization/item/title"))
83 {
84 $this->LAST_ERROR = GetMessage("LEARNING_BAD_NAME");
85 return false;
86 }
87
88 $arFields = Array(
89 "NAME" => $title->content,
90 "SITE_ID" => $this->arSITE_ID,
91 );
92
93 $course = new CCourse;
94 $this->COURSE_ID = $course->Add($arFields);
96
97 if(!$res)
98 {
99 if($e = $APPLICATION->GetException())
100 $this->LAST_ERROR = $e->GetString();
101 return false;
102 }
103
104 $r = new CDataXML();
105 if (!$r->Load($this->package_dir."/res1.xml"))
106 return false;
107
108 if (!$data = $r->SelectNodes("/coursetoc/"))
109 return false;
110
111 $ar = $data->__toArray();
112 $arFields = $this->_MakeFields($ar);
113
114 $res = $course->Update($this->COURSE_ID, $arFields);
115
116 if(!$res)
117 {
118 if($e = $APPLICATION->GetException())
119 $this->LAST_ERROR = $e->GetString();
120 return false;
121 }
122
123 CheckDirPath($_SERVER["DOCUMENT_ROOT"]."/".(COption::GetOptionString("main", "upload_dir", "upload"))."/learning/".$this->COURSE_ID);
125 $this->package_dir."/resources/res1",
126 $_SERVER["DOCUMENT_ROOT"] . "/" . (COption::GetOptionString("main", "upload_dir", "upload")) . "/learning/" . $this->COURSE_ID . "/res1",
127 true);
128
129 return true;
130 }
131
132
133 // 2012-04-19 Checked/modified for compatibility with new data model
134 protected function CreateContent($arItems = Array(), $PARENT_ID = 0)
135 {
136 if ($this->LAST_ERROR <> '')
137 return false;
138
139 if (empty($arItems))
140 {
141 if ($items = $this->objXML->SelectNodes("/manifest/organizations/organization/item/"))
142 {
143 $arItems = $items->__toArray();
144 $arItems = $arItems["#"]["item"];
145 }
146 }
147
148 foreach ($arItems as $ar)
149 {
150 $type = mb_substr($ar["@"]["identifier"], 0, 3);
151 $res_id = $ar["@"]["identifierref"];
152 $title = $ar["#"]["title"][0]["#"];
153
154 $ID = $this->_MakeItems($title, $type, $res_id, $PARENT_ID);
155
156 if (is_set($ar["#"], "item"))
157 $this->CreateContent($ar["#"]["item"], $ID);
158 }
159 }
160
161
162 // 2012-04-19 Checked/modified for compatibility with new data model
163 protected function _MakeItems($TITLE, $TYPE, $RES_ID, $PARENT_ID)
164 {
165 global $APPLICATION;
166
167 if ($PARENT_ID === 0)
168 $linkToParentLessonId = CCourse::CourseGetLinkedLesson ($this->COURSE_ID);
169 else
170 $linkToParentLessonId = (int) $PARENT_ID;
171
172 $createUnilesson = false;
173
174 if ($TYPE == "LES")
175 {
176 $arFields = Array(
177 'NAME' => $TITLE
178 );
179
180 $createUnilesson = true;
181 }
182 elseif ($TYPE == "CHA")
183 {
184 $arFields = Array(
185 'NAME' => $TITLE
186 );
187
188 $createUnilesson = true;
189 }
190 elseif ($TYPE == "QUE")
191 {
192 $arFields = Array(
193 "NAME" => $TITLE,
194 "LESSON_ID" => $linkToParentLessonId
195 );
196
197 $cl = new CLQuestion;
198 }
199 elseif ($TYPE == "TES")
200 {
201 $arFields = Array(
202 "NAME" => $TITLE,
203 "COURSE_ID" => $this->COURSE_ID
204 );
205
206 $cl = new CTest;
207 }
208 elseif ($TYPE === 'TMK')
209 {
210 $arFields = array();
211
212 $cl = new CLTestMark;
213 }
214 else
215 return $PARENT_ID;
216
217
218 $r = new CDataXML();
219 if (!$r->Load($this->package_dir."/".mb_strtolower($RES_ID).".xml"))
220 $r = false;
221
222 if ($r !== false)
223 {
224 if ($TYPE == "QUE")
225 {
226 if (
227 ($data = $r->SelectNodes("/questestinterop/item/presentation/"))
228 &&
229 ($resp = $r->SelectNodes("/questestinterop/item/resprocessing/"))
230 )
231 {
232 $arQ = Array();
233 $arData = $data->__toArray();
234 $arResp = $resp->__toArray();
235
236 if (is_set($arData["#"]["material"][0]["#"], "mattext"))
237 $arQ["NAME"] = $arData["#"]["material"][0]["#"]["mattext"][0]["#"];
238
239 if (is_set($arData["#"]["material"][0]["#"], "matimage"))
240 {
241 $imageDescription = '';
242 if (is_set($arData["#"]["material"][0]["#"], 'image_description'))
243 $imageDescription = $arData["#"]["material"][0]["#"]['image_description'][0]['#'];
244
245 $arQ["FILE_ID"] = Array(
246 "MODULE_ID" => "learning",
247 "name" =>basename($arData["#"]["material"][0]["#"]["matimage"][0]["@"]["uri"]),
248 "tmp_name" => $this->package_dir."/".$arData["#"]["material"][0]["#"]["matimage"][0]["@"]["uri"],
249 "size" =>@filesize($this->package_dir."/".$arData["#"]["material"][0]["#"]["matimage"][0]["@"]["uri"]),
250 "type" => $arData["#"]["material"][0]["#"]["matimage"][0]["@"]["imagtype"],
251 'description' => $imageDescription
252 );
253 }
254
255 if (is_set($arData["#"]["response_lid"][0]["@"], "rcardinality"))
256 {
257 switch ($arData["#"]["response_lid"][0]["@"]["rcardinality"])
258 {
259 case "Multiple":
260 $arQ["QUESTION_TYPE"] = 'M';
261 break;
262 case "Text":
263 $arQ["QUESTION_TYPE"] = 'T';
264 break;
265 case "Sort":
266 $arQ["QUESTION_TYPE"] = 'R';
267 break;
268 default:
269 $arQ["QUESTION_TYPE"] = 'S';
270 break;
271 }
272 }
273
274 if (is_set($arResp["#"]["respcondition"][0]["#"], "setvar"))
275 $arQ["POINT"] = $arResp["#"]["respcondition"][0]["#"]["setvar"][0]['#'];
276
277 //Additional
278 if ($bx = $r->SelectNodes("/questestinterop/item/bitrix/"))
279 {
280 $arQ = array_merge($arQ, $this->_MakeFields($bx->__toArray(), $TYPE));
281 unset($bx);
282 }
283
284 $arFields = array_merge($arFields,$arQ);
285
286 $cl = new CLQuestion;
287 $ID = $cl->Add($arFields);
288
289 if ($ID > 0)
290 {
291 $PARENT_ID = $ID;
292 $arCorrect = Array();
293 if (
294 is_set($arResp["#"]["respcondition"][0]["#"], "conditionvar")
295 &&
296 is_set($arResp["#"]["respcondition"][0]["#"]["conditionvar"][0]["#"], "varequal")
297 )
298 {
299
300 foreach ($arResp["#"]["respcondition"][0]["#"]["conditionvar"][0]["#"]["varequal"] as $ar)
301 $arCorrect[] = $ar["#"];
302 }
303
304 if (is_set($arData["#"]["response_lid"][0]["#"], "render_choice")
305 &&
306 is_set($arData["#"]["response_lid"][0]["#"]["render_choice"][0]["#"], "response_label")
307 )
308 {
309 $i = 0;
310 foreach ($arData["#"]["response_lid"][0]["#"]["render_choice"][0]["#"]["response_label"] as $ar)
311 {
312 $i +=10;
313 $cl = new CLAnswer;
314 $arFields = Array(
315 "QUESTION_ID" => $PARENT_ID,
316 "SORT" => $i,
317 "CORRECT" => (in_array($ar["@"]["ident"],$arCorrect) ? "Y": "N"),
318 "ANSWER" => $ar["#"]["material"][0]["#"]["mattext"][0]["#"],
319 );
320
321 $AswerID = $cl->Add($arFields);
322 $res = ($AswerID > 0);
323 if (!$res)
324 {
325 if ($e = $APPLICATION->GetException())
326 $this->arWarnings[$TYPE][] = Array("TITLE" => $TITLE, "TEXT" =>$e->GetString());
327 }
328 }
329 }
330 }
331 else
332 {
333 if ($e = $APPLICATION->GetException())
334 $this->arWarnings[$TYPE][] = Array("TITLE" => $TITLE, "TEXT" =>$e->GetString());
335 }
336
337 unset($cl);
338 unset($data);
339 unset($arQ);
340 unset($resp);
341 unset($arData);
342 unset($arResp);
343
344 return $PARENT_ID;
345 }
346 }
347 else
348 {
349 if ($data = $r->SelectNodes("/content/"))
350 {
351 $ar = $data->__toArray();
352 $arFields = array_merge($arFields,$this->_MakeFields($ar, $TYPE));
353 if ($TYPE === 'TMK')
354 $arFields['TEST_ID'] = (int) $PARENT_ID;
355
356 if (is_set($arFields, "COMPLETED_SCORE") && intval($arFields["COMPLETED_SCORE"]) <= 0)
357 unset($arFields["COMPLETED_SCORE"]);
358 if ((is_set($arFields, "PREVIOUS_TEST_ID") && intval($arFields["PREVIOUS_TEST_ID"]) <= 0) || !CTest::GetByID($arFields["PREVIOUS_TEST_ID"])->Fetch())
359 unset($arFields["PREVIOUS_TEST_ID"], $arFields["PREVIOUS_TEST_SCORE"]);
360 }
361 }
362 }
363
364 if ($createUnilesson === false)
365 {
366 $ID = $cl->Add($arFields);
367 unset($cl);
368 }
369 else
370 {
371 $bProhibitPublish = false;
372 // properties (in context of parent) by default
373 $arProperties = array('SORT' => 500);
374
375 // Lesson's sort order in context of parent
376 if (isset($arFields['EDGE_SORT']))
377 {
378 $arFields['SORT'] = (int) $arFields['EDGE_SORT'];
379 unset ($arFields['EDGE_SORT']);
380 }
381
382 if (isset($arFields['SORT']))
383 {
384 $arProperties['SORT'] = (int) $arFields['SORT'];
385
386 // Lessons doesn't have more SORT field
387 unset ($arFields['SORT']);
388 }
389
390 if (isset($arFields['META_PUBLISH_PROHIBITED']))
391 {
392 if ($arFields['META_PUBLISH_PROHIBITED'] === 'Y')
393 $bProhibitPublish = true;
394
395 unset($arFields['META_PUBLISH_PROHIBITED']);
396 }
397
398 // unset fields, that are absent in unilesson
399 $arUnilessonFields = $arFields;
400 $arFieldsNames = array_keys($arUnilessonFields);
401 foreach ($arFieldsNames as $fieldName)
402 {
403 if ( ! in_array(mb_strtoupper($fieldName), $this->arLessonWritableFields) )
404 unset ($arUnilessonFields[$fieldName]);
405 }
406
408 $arUnilessonFields,
409 false, // is it course? - No, it isn't.
410 $linkToParentLessonId,
412 );
413
414 if ($bProhibitPublish && ($ID > 0))
415 CLearnLesson::PublishProhibitionSetTo($ID, $linkToParentLessonId, $bProhibitPublish);
416 }
417
418 if ($ID > 0)
419 return $ID;
420 else
421 {
422 if($e = $APPLICATION->GetException())
423 $this->arWarnings[$TYPE][] = Array("TITLE" => $TITLE, "TEXT" =>$e->GetString());
424 }
425 }
426
427
428 // 2012-04-18 Checked/modified for compatibility with new data model
429 protected function _MakeFields(&$arFields, $itemType = null)
430 {
431 $arRes = Array();
432 $upload_dir = COption::GetOptionString("main", "upload_dir", "upload");
433
434 $arStopList = array();
435 foreach($arFields["#"] as $field => $arValue)
436 {
437 if (in_array($field, $arStopList))
438 continue;
439
440 if (in_array($field, $this->arUnsetFields) && ($itemType !== 'TMK') && ($itemType !== 'QUE'))
441 {
442 if ( ! ($itemType === 'TES' && in_array($field, $this->arPreventUnsetFieldsForTest)) )
443 continue;
444 }
445
446 if (in_array($field, $this->arDraftFields) && ($itemType !== 'TMK'))
447 {
448 if (is_set($arValue[0]["#"], "cdata-section"))
449 {
450 $arRes[mb_strtoupper($field)] = preg_replace(
451 "~([\"'])(cid:resources/(.+?))(\\1)~is",
452 "\\1/".$upload_dir."/learning/".$this->COURSE_ID."/\\3\\1",
453 $arValue[0]["#"]["cdata-section"][0]["#"]);
454 continue;
455 }
456 elseif (isset($arValue[0]["#"]))
457 {
458 $arRes[mb_strtoupper($field)] = preg_replace(
459 "~([\"'])(cid:resources/(.+?))(\\1)~is",
460 "\\1/".$upload_dir."/learning/".$this->COURSE_ID."/\\3\\1",
461 $arValue[0]["#"]);
462 continue;
463 }
464 }
465
466 if (in_array($field, $this->arDate) && $arValue[0]["#"] <> '')
467 {
468 $time = date("His", $arValue[0]["#"]);
469 $arRes[mb_strtoupper($field)] = ConvertTimeStamp($arValue[0]["#"], $time == "000000" ? "SHORT" : "FULL");
470 continue;
471 }
472
473 if (in_array($field, $this->arPicture) && intval($arValue[0]["#"]) > 0)
474 {
475 $file = $this->package_dir."/dbresources/".$arValue[0]["#"];
476
477 if (method_exists('CFile', 'GetImageSize'))
478 {
479 $aImage = @CFile::GetImageSize($file);
480 if($aImage === false)
481 continue;
482
483 if (function_exists("image_type_to_mime_type"))
484 $image_type_to_mime_type = image_type_to_mime_type($aImage[2]);
485 else
486 $image_type_to_mime_type = CCourseImport::ImageTypeToMimeType($aImage[2]);
487 }
488 else
489 $image_type_to_mime_type = self::ImageTypeToMimeTypeByFileName($file);
490
491 $arRes[mb_strtoupper($field)] = array(
492 "MODULE_ID" => "learning",
493 "name" =>$arValue[0]["#"],
494 "tmp_name" => $file,
495 "size" =>@filesize($file),
496 "type" => $image_type_to_mime_type
497 );
498
499 if (isset($arFields["#"][$field . '_description'][0]['#']))
500 {
501 $arRes[mb_strtoupper($field)]['description'] = $arFields["#"][$field . '_description'][0]['#'];
502 $arStopList[] = $field . '_description';
503 }
504
505 continue;
506 }
507
508 $arRes[mb_strtoupper($field)] = $arValue[0]["#"];
509 }
510 unset($arFields);
511 return $arRes;
512 }
513
514
515 // 2012-04-18 Checked/modified for compatibility with new data model
516 public function ImportPackage()
517 {
518 if (!$this->CreateCourse())
519 return false;
520
521 $this->CreateContent();
522
524 $this->package_dir."/resources",
525 $_SERVER["DOCUMENT_ROOT"] . "/" . (COption::GetOptionString("main", "upload_dir", "upload")) . "/learning/" . $this->COURSE_ID,
526 true,
527 true);
528
529 return true;
530 }
531
532
533 protected static function ImageTypeToMimeTypeByFileName ($file)
534 {
535 $ext = mb_strtolower(pathinfo($file, PATHINFO_EXTENSION));
536
537 switch ($ext)
538 {
539 case 'jpg':
540 case 'jpeg':
541 $type = 'image/jpeg';
542 break;
543
544 case 'jp2':
545 $type = 'image/jp2';
546 break;
547
548 case 'gif':
549 $type = 'image/gif';
550 break;
551
552 case 'png':
553 $type = 'image/png';
554 break;
555
556 case 'bmp':
557 $type = 'image/bmp';
558 break;
559
560 default:
561 $type = 'application/octet-stream';
562 break;
563 }
564
565 return ($type);
566 }
567
568
569 // 2012-04-18 Checked/modified for compatibility with new data model
570 public static function ImageTypeToMimeType($type)
571 {
572 $aTypes = array(
573 1 => "image/gif",
574 2 => "image/jpeg",
575 3 => "image/png",
576 4 => "application/x-shockwave-flash",
577 5 => "image/psd",
578 6 => "image/bmp",
579 7 => "image/tiff",
580 8 => "image/tiff",
581 9 => "application/octet-stream",
582 10 => "image/jp2",
583 11 => "application/octet-stream",
584 12 => "application/octet-stream",
585 13 => "application/x-shockwave-flash",
586 14 => "image/iff",
587 15 => "image/vnd.wap.wbmp",
588 16 => "image/xbm"
589 );
590 if(!empty($aTypes[$type]))
591 return $aTypes[$type];
592 else
593 return "application/octet-stream";
594 }
595}
$type
Определения options.php:106
global $APPLICATION
Определения include.php:80
static GetByID($ID)
Определения test.php:264
Определения course.php:4
static CourseGetLinkedLesson($courseId)
Определения course.php:55
Add($arFields)
Определения course.php:136
Определения import.php:5
CreateCourse()
Определения import.php:75
$objXML
Определения import.php:11
_MakeItems($TITLE, $TYPE, $RES_ID, $PARENT_ID)
Определения import.php:163
ImportPackage()
Определения import.php:516
static ImageTypeToMimeTypeByFileName($file)
Определения import.php:533
$arManifest
Определения import.php:8
CreateContent($arItems=Array(), $PARENT_ID=0)
Определения import.php:134
$arUnsetFields
Определения import.php:13
$arWarnings
Определения import.php:20
$arSITE_ID
Определения import.php:9
$arLessonWritableFields
Определения import.php:25
$LAST_ERROR
Определения import.php:7
$COURSE_ID
Определения import.php:10
$arDraftFields
Определения import.php:12
$arPreventUnsetFieldsForTest
Определения import.php:21
_MakeFields(&$arFields, $itemType=null)
Определения import.php:429
$package_dir
Определения import.php:6
__construct($PACKAGE_DIR, $arSITE_ID)
Определения import.php:32
$arDate
Определения import.php:19
$arPicture
Определения import.php:18
static ImageTypeToMimeType($type)
Определения import.php:570
Определения xml.php:396
Определения answer.php:4
Определения question.php:4
Определения testmark.php:5
static CopyDirFiles($path_from, $path_to, $ReWrite=True, $Recursive=False)
Определения clearnhelper.php:432
static Add($arFields, $isCourse=false, $parentLessonId=true, $arProperties=array('SORT'=> 500), $isCheckPermissions=true, $checkPermissionsForUserId=-1)
Определения clearnlesson.php:440
static PublishProhibitionSetTo($in_lessonId, $in_contextCourseLessonId, $in_isProhibited)
Определения clearnlesson.php:2246
Определения test.php:6
$arFields
Определения dblapprove.php:5
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
if($ajaxMode) $ID
Определения get_user.php:27
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
CheckDirPath($path)
Определения tools.php:2707
is_set($a, $k=false)
Определения tools.php:2133
GetMessage($name, $aReplace=null)
Определения tools.php:3397
$TITLE
Определения menu_edit.php:223
$time
Определения payment.php:61
$resp
Определения result_rec.php:20
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$ar
Определения options.php:199
$TYPE
Определения rss.php:27
$i
Определения factura.php:643
$items
Определения template.php:224
$title
Определения pdf.php:123
$arRes
Определения options.php:104
if( $site[ 'SERVER_NAME']==='') if($site['SERVER_NAME']==='') $arProperties
Определения yandex_run.php:644