2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 * Provides validation classes used by the imscc converters
19 * @package backup-convert
20 * @copyright 2011 Darko Miletic <dmiletic@moodlerooms.com>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 final class error_messages
{
27 * @static error_messages
29 private static $instance = null;
30 private function __construct(){}
31 private function __clone(){}
33 * @return error_messages
35 public static function instance() {
36 if (empty(self
::$instance)) {
38 self
::$instance = new $c();
40 return self
::$instance;
46 private $items = array();
51 public function add($msg) {
53 $this->items
[] = $msg;
60 public function errors() {
65 * Empties the error content
67 public function reset() {
68 $this->items
= array();
75 public function to_string($web = false) {
78 $result .= '<ol>'.PHP_EOL
;
80 foreach ($this->items
as $error) {
85 $result .= $error.PHP_EOL
;
88 $result .= '</li>'.PHP_EOL
;
92 $result .= '</ol>'.PHP_EOL
;
98 * Casting to string method
101 public function __toString() {
102 return $this->to_string(false);
107 final class libxml_errors_mgr
{
111 private $previous = false;
114 * @param boolean $reset
116 public function __construct($reset=false){
118 error_messages
::instance()->reset();
120 $this->previous
= libxml_use_internal_errors(true);
121 libxml_clear_errors();
124 private function collect_errors ($filename=''){
125 $errors = libxml_get_errors();
126 static $error_types = array(
127 LIBXML_ERR_ERROR
=> 'Error'
128 ,LIBXML_ERR_FATAL
=> 'Fatal Error'
129 ,LIBXML_ERR_WARNING
=> 'Warning'
132 foreach($errors as $error){
134 if (!empty($filename)) {
135 $add = " in {$filename}";
136 } elseif (!empty($error->file
)) {
137 $add = " in {$error->file}";
140 if (!empty($error->line
)) {
141 $line = " at line {$error->line}";
143 $err = "{$error_types[$error->level]}{$add}: {$error->message}{$line}";
144 error_messages
::instance()->add($err);
146 libxml_clear_errors();
150 public function __destruct(){
151 $this->collect_errors();
152 if (!$this->previous
) {
153 libxml_use_internal_errors($this->previous
);
157 public function collect() {
158 $this->collect_errors();
163 function validate_xml($xml, $schema){
165 $manifest_file = realpath($xml);
166 $schema_file = realpath($schema);
167 if (empty($manifest_file) ||
empty($schema_file)) {
171 $xml_error = new libxml_errors_mgr();
172 $manifest = new DOMDocument();
173 $doc->validateOnParse
= false;
174 $result = $manifest->load($manifest_file, LIBXML_NONET
) &&
175 $manifest->schemaValidate($schema_file);
180 class cc_validate_type
{
181 const manifest_validator1
= 'cclibxml2validator.xsd' ;
182 const assesment_validator1
= '/domainProfile_4/ims_qtiasiv1p2_localised.xsd';
183 const discussion_validator1
= '/domainProfile_6/imsdt_v1p0_localised.xsd' ;
184 const weblink_validator1
= '/domainProfile_5/imswl_v1p0_localised.xsd' ;
186 const manifest_validator11
= 'cc11libxml2validator.xsd' ;
187 const blti_validator11
= 'imslticc_v1p0p1.xsd' ;
188 const assesment_validator11
= 'ccv1p1_qtiasiv1p2p1_v1p0.xsd';
189 const discussion_validator11
= 'ccv1p1_imsdt_v1p1.xsd' ;
190 const weblink_validator11
= 'ccv1p1_imswl_v1p1.xsd' ;
195 protected $type = null;
200 protected $location = null;
202 public function __construct($type, $location){
204 $this->location
= $location;
209 * @param string $element - File path for the xml
212 public function validate($element) {
213 $this->last_error
= null;
214 $celement = realpath($element);
215 $cvalidator = realpath($this->location
.DIRECTORY_SEPARATOR
.$this->type
);
216 $result = (empty($celement) ||
empty($cvalidator));
218 $xml_error = new libxml_errors_mgr();
219 $doc = new DOMDocument();
220 $doc->validateOnParse
= false;
221 $result = $doc->load($celement, LIBXML_NONET
) &&
222 $doc->schemaValidate($cvalidator);
229 class manifest_validator
extends cc_validate_type
{
230 public function __construct($location){
231 parent
::__construct(self
::manifest_validator11
, $location);
235 class manifest10_validator
extends cc_validate_type
{
236 public function __construct($location){
237 parent
::__construct(self
::manifest_validator1
, $location);
241 class blti_validator
extends cc_validate_type
{
242 public function __construct($location){
243 parent
::__construct(self
::blti_validator11
, $location);
247 class assesment_validator
extends cc_validate_type
{
248 public function __construct($location){
249 parent
::__construct(self
::assesment_validator11
, $location);
253 class discussion_validator
extends cc_validate_type
{
254 public function __construct($location){
255 parent
::__construct(self
::discussion_validator11
, $location);
259 class weblink_validator
extends cc_validate_type
{
260 public function __construct($location){
261 parent
::__construct(self
::weblink_validator11
, $location);