Permalink
Browse files

MDL-29956: Common Cartridge should handle HTML content more natively …

…(r17188, r17210:17265)

Also implemented support for LTI icon during import.
  • Loading branch information...
1 parent 91e6ca6 commit 726a770949b701de7f197a258e3df2f9bf23774c @mrmark mrmark committed with stronk7 Oct 26, 2011
@@ -63,8 +63,11 @@ public function __construct($path_to_manifest) {
public function generate_moodle_xml () {
global $CFG;
+ $cdir = static::$path_to_manifest_folder . DIRECTORY_SEPARATOR . 'course_files';
- mkdir(static::$path_to_manifest_folder . DIRECTORY_SEPARATOR . 'course_files');
+ if (!file_exists($cdir)) {
+ mkdir($cdir);
+ }
$sheet_base = static::loadsheet(SHEET_BASE);
@@ -175,13 +178,17 @@ protected function create_code_info_details_mod () {
}
protected function create_node_course_modules_mod () {
+ $labels = new cc_label();
$resources = new cc11_resource();
$forums = new cc11_forum();
$quiz = new cc11_quiz();
$basiclti = new cc11_lti();
static::log_action('Creating node: COURSE/MODULES/MOD');
+ // LABELS
+ $node_course_modules_mod_label = $labels->generate_node();
+
// RESOURCES (WEB CONTENT AND WEB LINK)
$node_course_modules_mod_resource = $resources->generate_node();
@@ -194,7 +201,8 @@ protected function create_node_course_modules_mod () {
//BasicLTI
$node_course_modules_mod_basiclti = $basiclti->generate_node();
- $node_course_modules = $node_course_modules_mod_resource .
+ $node_course_modules = $node_course_modules_mod_label.
+ $node_course_modules_mod_resource .
$node_course_modules_mod_forum .
$node_course_modules_mod_quiz .
$node_course_modules_mod_basiclti;
@@ -141,7 +141,11 @@ public function generate_moodle_xml () {
global $CFG;
- mkdir(static::$path_to_manifest_folder . DIRECTORY_SEPARATOR . 'course_files');
+ $cdir = static::$path_to_manifest_folder . DIRECTORY_SEPARATOR . 'course_files';
+
+ if (!file_exists($cdir)) {
+ mkdir($cdir);
+ }
$sheet_base = static::loadsheet(SHEET_BASE);
@@ -295,7 +299,8 @@ protected function create_node_course_modules_mod () {
// QUIZ
$node_course_modules_mod_quiz = $quiz->generate_node_course_modules_mod();
- $node_course_modules = /*$node_course_modules_mod_label .*/ $node_course_modules_mod_resource . $node_course_modules_mod_forum . $node_course_modules_mod_quiz;
+ //TODO: label
+ $node_course_modules = $node_course_modules_mod_label . $node_course_modules_mod_resource . $node_course_modules_mod_forum . $node_course_modules_mod_quiz;
return $node_course_modules;
}
@@ -527,7 +532,8 @@ protected function create_code_info_details_mod () {
$forum_mod = $count_forum ? $this->create_mod_info_details_mod(MOODLE_TYPE_FORUM, $forum_instance) : '';
$label_mod = $count_label ? $this->create_mod_info_details_mod(MOODLE_TYPE_LABEL, $label_instance) : '';
- return /*$label_mod .*/ $resource_mod . $quiz_mod . $forum_mod;
+ //TODO: label
+ return $label_mod . $resource_mod . $quiz_mod . $forum_mod;
}
@@ -552,6 +558,11 @@ protected function create_mod_info_details_mod_instances_instance ($sheet, $inst
for ($i = 1; $i <= $instances_quantity; $i++) {
$user_info = ($instances[$i - 1]['common_cartriedge_type'] == static::CC_TYPE_FORUM) ? 'true' : 'false';
+ if ($instances[$i - 1]['common_cartriedge_type'] == static::CC_TYPE_EMPTY) {
+ if ($instances[$i - 1]['deep'] <= ROOT_DEEP ) {
+ continue;
+ }
+ }
$replace_values = array($instances[$i - 1]['instance'],
htmlentities($instances[$i - 1]['title']),
@@ -577,7 +588,7 @@ protected function create_instances ($items, $level = 0, &$array_index = 0, $ind
$array_index++;
- if ($item->nodeName == "item") {
+ if ($item->nodeName == "item") {
$identifierref = $xpath->query('@identifierref', $item);
$identifierref = !empty($identifierref->item(0)->nodeValue) ? $identifierref->item(0)->nodeValue : '';
@@ -587,7 +598,6 @@ protected function create_instances ($items, $level = 0, &$array_index = 0, $ind
$cc_type = $this->get_item_cc_type($identifierref);
$moodle_type = $this->convert_to_moodle_type($cc_type);
-
}
elseif ($item->nodeName == "resource") {
@@ -615,8 +625,10 @@ protected function create_instances ($items, $level = 0, &$array_index = 0, $ind
static::$instances['index'][$array_index]['resource_indentifier'] = $identifierref;
static::$instances['instances'][$moodle_type][] = array('title' => $title,
- 'instance' => static::$instances['index'][$array_index]['instance'],
- 'common_cartriedge_type' => $cc_type , 'resource_indentifier' => $identifierref);
+ 'instance' => static::$instances['index'][$array_index]['instance'],
+ 'common_cartriedge_type' => $cc_type,
+ 'resource_indentifier' => $identifierref,
+ 'deep' => $level);
$more_items = $xpath->query('imscc:item', $item);
@@ -677,9 +689,10 @@ public function convert_to_moodle_type ($cc_type) {
if ($cc_type == static::CC_TYPE_QUESTION_BANK) {
$type = MOODLE_TYPE_QUESTION_BANK;
}
- /*if ($cc_type == static::CC_TYPE_EMPTY) {
+ //TODO: label
+ if ($cc_type == static::CC_TYPE_EMPTY) {
$type = MOODLE_TYPE_LABEL;
- }*/
+ }
return $type;
}
@@ -38,4 +38,5 @@
require_once($CFG->dirroot .'/backup/cc/cc_lib/cc_converter_url.php');
require_once($CFG->dirroot .'/backup/cc/cc_lib/cc_converter_resource.php');
require_once($CFG->dirroot .'/backup/cc/cc_lib/cc_converter_quiz.php');
+require_once($CFG->dirroot .'/backup/cc/cc_lib/cc_converter_page.php');
require_once($CFG->dirroot .'/backup/cc/cc_lib/cc_convert_moodle2.php');
@@ -37,6 +37,7 @@ public function convert($outdir) {
$rt = new basicltil1_resurce_file();
$contextid = $this->doc->nodeValue('/activity/@contextid');
$title = $this->doc->nodeValue('/activity/lti/name');
+ $text = $this->doc->nodeValue('/activity/lti/intro');
$rt->set_title($title);
$result = cc_helpers::process_linked_files($text,
$this->manifest,
@@ -0,0 +1,59 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+/**
+ * @package backup-convert
+ * @subpackage cc-library
+ * @copyright 2011 Darko Miletic <dmiletic@moodlerooms.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+require_once 'cc_converters.php';
+require_once 'cc_general.php';
+require_once 'cc_page.php';
+
+class cc_converter_page extends cc_converter {
+ public function __construct(cc_i_item &$item, cc_i_manifest &$manifest, $rootpath, $path){
+ $this->cc_type = cc_version11::webcontent;
+ $this->defaultfile = 'page.xml';
+ $this->defaultname = uniqid().'.html';
+ parent::__construct($item, $manifest, $rootpath, $path);
+ }
+
+ public function convert($outdir) {
+ $rt = new page11_resurce_file();
+ $title = $this->doc->nodeValue('/activity/page/name');
+ $intro = $this->doc->nodeValue('/activity/page/intro');
+ $contextid = $this->doc->nodeValue('/activity/@contextid');
+ $pagecontent = $this->doc->nodeValue('/activity/page/content');
+ $rt->set_title($title);
+ $rawname = str_replace(' ', '_', strtolower(trim(clean_param($title, PARAM_FILE))));
+ if (!empty($rawname)) {
+ $this->defaultname = $rawname.".html";
+ }
+
+ $result = cc_helpers::process_linked_files( $pagecontent,
+ $this->manifest,
+ $this->rootpath,
+ $contextid,
+ $outdir,
+ true);
+ $rt->set_content($result[0]);
+ $rt->set_intro($intro);
+ //store everything
+ $this->store($rt, $outdir, $title, $result[1]);
+ return true;
+ }
+}
@@ -117,13 +117,16 @@ public function __construct(cc_i_item &$item, cc_i_manifest &$manifest, $rootpat
protected function store(general_cc_file $doc, $outdir, $title, $deps = null) {
$rdir = new cc_resource_location($outdir);
$rtp = $rdir->fullpath(true).$this->defaultname;
- $doc->saveTo($rtp);
- $resource = new cc_resource($rdir->rootdir(), $this->defaultname, $rdir->dirname(true));
- $resource->dependency = empty($deps) ? array() : $deps;
- $res = $this->manifest->add_resource($resource, null, $this->cc_type);
- $resitem = new cc_item();
- $resitem->attach_resource($res[0]);
- $resitem->title = $title;
- $this->item->add_child_item($resitem);
+ if ( $doc->saveTo($rtp) ) {
+ $resource = new cc_resource($rdir->rootdir(), $this->defaultname, $rdir->dirname(true));
+ $resource->dependency = empty($deps) ? array() : $deps;
+ $res = $this->manifest->add_resource($resource, null, $this->cc_type);
+ $resitem = new cc_item();
+ $resitem->attach_resource($res[0]);
+ $resitem->title = $title;
+ $this->item->add_child_item($resitem);
+ } else {
+ throw new RuntimeException("Unable to save file {$rtp}!");
+ }
}
}
@@ -60,10 +60,13 @@ protected function on_create() {
$vt = empty($schemaLocation) ? '' : ' ';
$schemaLocation .= $vt.$this->ccnamespaces[$key].' '.$value;
}
- $this->append_new_attribute_ns($rootel,
- $this->ccnamespaces['xsi'],
- 'xsi:schemaLocation',
- $schemaLocation);
+
+ if (!empty($schemaLocation) && isset($this->ccnamespaces['xsi'])) {
+ $this->append_new_attribute_ns($rootel,
+ $this->ccnamespaces['xsi'],
+ 'xsi:schemaLocation',
+ $schemaLocation);
+ }
$this->root = $rootel;
}
@@ -0,0 +1,110 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+/**
+ * @package backup-convert
+ * @copyright 2011 Darko Miletic <dmiletic@moodlerooms.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+require_once 'cc_general.php';
+
+class page11_resurce_file extends general_cc_file {
+ protected $rootns = 'xmlns';
+ protected $rootname = 'html';
+ protected $ccnamespaces = array('xmlns' => 'http://www.w3.org/1999/xhtml');
+
+ protected $content = null;
+ protected $title = null;
+ protected $intro = null;
+
+ public function set_content($value) {
+ $this->content = $value;
+ }
+
+ public function set_title($value) {
+ $this->title = $value;
+ }
+
+ public function set_intro($value) {
+ $this->intro = htmlspecialchars(strip_tags($value), ENT_COMPAT, 'UTF-8', false);
+ }
+
+ protected function on_create() {
+ $impl = new DOMImplementation();
+ $dtd = $impl->createDocumentType( 'html',
+ '-//W3C//DTD XHTML 1.0 Strict//EN',
+ 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
+ $doc = $impl->createDocument($this->ccnamespaces[$this->rootns], null, $dtd);
+ $doc->formatOutput = true;
+ $doc->preserveWhiteSpace = true;
+ $this->doc = $doc;
+ parent::on_create();
+ }
+
+ public function on_save() {
+
+ $rns = $this->ccnamespaces[$this->rootns];
+ //Add the basic tags
+ $head = $this->append_new_element_ns($this->root, $rns, 'head');
+ $this->append_new_attribute_ns($head, $rns, 'profile', 'http://dublincore.org/documents/dc-html/');
+
+ //Linking Dublin Core Metadata 1.1
+ $link_dc = $this->append_new_element_ns($head, $rns, 'link');
+ $this->append_new_attribute_ns($link_dc, $rns, 'rel', 'schema.DC');
+ $this->append_new_attribute_ns($link_dc, $rns, 'href', 'http://purl.org/dc/elements/1.1/');
+ $link_dcterms = $this->append_new_element_ns($head, $rns, 'link');
+ $this->append_new_attribute_ns($link_dcterms, $rns, 'rel', 'schema.DCTERMS');
+ $this->append_new_attribute_ns($link_dcterms, $rns, 'href', 'http://purl.org/dc/terms/');
+ //Content type
+ $meta_type = $this->append_new_element_ns($head, $rns, 'meta');
+ $this->append_new_attribute_ns($meta_type, $rns, 'name' , 'DC.type' );
+ $this->append_new_attribute_ns($meta_type, $rns, 'scheme' , 'DCTERMS.DCMIType');
+ $this->append_new_attribute_ns($meta_type, $rns, 'content', 'Text' );
+
+ //Content description
+ if (!empty($this->intro)) {
+ $meta_description = $this->append_new_element_ns($head, $rns , 'meta' );
+ $this->append_new_attribute_ns($meta_description, $rns, 'name' , 'DC.description');
+ $this->append_new_attribute_ns($meta_description, $rns, 'content', $this->intro );
+ }
+
+ $meta = $this->append_new_element_ns($head, $rns, 'meta');
+ $this->append_new_attribute_ns($meta, $rns, 'http-equiv', 'Content-type');
+ $this->append_new_attribute_ns($meta, $rns, 'content', 'text/html; charset=UTF-8');
+ //set the title
+ $title = $this->append_new_element_ns($head, $rns, 'title', $this->title);
+ $body = $this->append_new_element_ns($this->root, $rns, 'body');
+ //We are unable to use DOM for embedding HTML due to numerous content errors
+ //Therefore we place a dummy tag that will be later replaced with the real content
+ $this->append_new_element_ns($body, $rns, 'div', '##REPLACE##');
+
+ return true;
+ }
+
+ public function saveTo($fname) {
+ $result = $this->on_save();
+ if ($result) {
+ $dret = str_replace('<?xml version="1.0"?>'."\n", '', $this->viewXML());
+ $dret = str_replace('<div>##REPLACE##</div>', $this->content, $dret);
+ $result = (file_put_contents($fname, $dret) !== false);
+ if ($result) {
+ $this->filename = $fname;
+ $this->processPath();
+ }
+ }
+ return $result;
+ }
+}
Oops, something went wrong. Retry.

0 comments on commit 726a770

Please sign in to comment.