/** * Process a single field. * * @staticvar string $h5peditor_path * @param object $field * @param mixed $params * @param array $files */ private function processField(&$field, &$params, &$files) { switch ($field->type) { case 'file': case 'image': if (isset($params->path)) { $this->processFile($params, $files); // Process original image if (isset($params->originalImage) && isset($params->originalImage->path)) { $this->processFile($params->originalImage, $files); } } break; case 'video': case 'audio': if (is_array($params)) { for ($i = 0, $s = count($params); $i < $s; $i++) { $this->processFile($params[$i], $files); } } break; case 'library': if (isset($params->library) && isset($params->params)) { $library = H5PCore::libraryFromString($params->library); $semantics = $this->h5p->loadLibrarySemantics($library['machineName'], $library['majorVersion'], $library['minorVersion']); // Process parameters for the library. $this->processSemantics($files, $semantics, $params->params); } break; case 'group': if (isset($params)) { if (count($field->fields) == 1) { $params = (object) array($field->fields[0]->name => $params); } $this->processSemantics($files, $field->fields, $params); } break; case 'list': if (is_array($params)) { for ($j = 0, $t = count($params); $j < $t; $j++) { $this->processField($field->field, $params[$j], $files); } } break; } }
/** * Process a single field. * * @staticvar string $h5peditor_path * @param object $field * @param mixed $params * @param array $files * @param array $libraries */ private function processField(&$field, &$params, &$files) { static $h5peditor_path; if (!$h5peditor_path) { $h5peditor_path = $this->editorFilesDir . DIRECTORY_SEPARATOR; } switch ($field->type) { case 'file': case 'image': if (isset($params->path)) { $oldPath = $h5peditor_path . $params->path; $newPath = $this->content_directory . $params->path; if (file_exists($oldPath)) { rename($oldPath, $newPath); $this->storage->keepFile($oldPath, $newPath); } elseif (file_exists($newPath)) { $this->storage->keepFile($newPath, $newPath); } $files[] = $params->path; } break; case 'video': case 'audio': if (is_array($params)) { for ($i = 0, $s = count($params); $i < $s; $i++) { $oldPath = $h5peditor_path . $params[$i]->path; $newPath = $this->content_directory . $params[$i]->path; if (file_exists($oldPath)) { rename($oldPath, $newPath); $this->storage->keepFile($oldPath, $newPath); } elseif (file_exists($newPath)) { $this->storage->keepFile($newPath, $newPath); } $files[] = $params[$i]->path; } } break; case 'library': if (isset($params->library) && isset($params->params)) { $library = H5PCore::libraryFromString($params->library); $semantics = $this->h5p->loadLibrarySemantics($library['machineName'], $library['majorVersion'], $library['minorVersion']); // Process parameters for the library. $this->processSemantics($files, $semantics, $params->params); } break; case 'group': if (isset($params)) { if (count($field->fields) == 1) { $params = (object) array($field->fields[0]->name => $params); } $this->processSemantics($files, $field->fields, $params); } break; case 'list': if (is_array($params)) { for ($j = 0, $t = count($params); $j < $t; $j++) { $this->processField($field->field, $params[$j], $files); } } break; } }
/** * Does the actual process of saving the H5P content that's submitted through * the activity form * * @param stdClass $hvp * @return int Content ID */ function hvp_save_content($hvp) { // Determine disabled content features $hvp->disable = hvp_get_disabled_content_features($hvp); // Determine if we're uploading or creating if ($hvp->h5paction === 'upload') { // Save uploaded package $hvp->uploaded = true; $h5pstorage = \mod_hvp\framework::instance('storage'); $h5pstorage->savePackage((array) $hvp); $hvp->id = $h5pstorage->contentId; } else { // Save newly created or edited content $core = \mod_hvp\framework::instance(); $editor = \mod_hvp\framework::instance('editor'); if (!empty($hvp->id)) { // Load existing content to get old parameters for comparison $content = $core->loadContent($hvp->id); $oldlib = $content['library']; $oldparams = json_decode($content['params']); } // Make params and library available for core to save $hvp->params = $hvp->h5pparams; $hvp->library = H5PCore::libraryFromString($hvp->h5plibrary); $hvp->library['libraryId'] = $core->h5pF->getLibraryId($hvp->library['machineName'], $hvp->library['majorVersion'], $hvp->library['minorVersion']); $hvp->id = $core->saveContent((array) $hvp); // We need to process the parameters to move any images or files and // to determine which dependencies the content has. // Prepare current parameters $params = json_decode($hvp->params); // Move any uploaded images or files. Determine content dependencies. $editor->processParameters($hvp->id, $hvp->library, $params, isset($oldlib) ? $oldlib : NULL, isset($oldparams) ? $oldparams : NULL); } return $hvp->id; }
/** * Validate given library value against library semantics. * Check if provided library is within allowed options. * * Will recurse into validating the library's semantics too. */ public function validateLibrary(&$value, $semantics) { if (!isset($value->library) || !in_array($value->library, $semantics->options)) { $this->h5pF->setErrorMessage($this->h5pF->t('Library used in content is not a valid library according to semantics')); $value = new stdClass(); return; } if (!isset($this->libraries[$value->library])) { $libspec = H5PCore::libraryFromString($value->library); $library = $this->h5pC->loadLibrary($libspec['machineName'], $libspec['majorVersion'], $libspec['minorVersion']); $library['semantics'] = $this->h5pC->loadLibrarySemantics($libspec['machineName'], $libspec['majorVersion'], $libspec['minorVersion']); $this->libraries[$value->library] = $library; } else { $library = $this->libraries[$value->library]; } $this->validateGroup($value->params, (object) array('type' => 'group', 'fields' => $library['semantics']), FALSE); $validkeys = array('library', 'params'); if (isset($semantics->extraAttributes)) { $validkeys = array_merge($validkeys, $semantics->extraAttributes); } $this->filterParams($value, $validkeys); // Find all dependencies for this library $depkey = 'preloaded-' . $library['machineName']; if (!isset($this->dependencies[$depkey])) { $this->dependencies[$depkey] = array('library' => $library, 'type' => 'preloaded'); $this->nextWeight = $this->h5pC->findLibraryDependencies($this->dependencies, $library, $this->nextWeight); $this->dependencies[$depkey]['weight'] = $this->nextWeight++; } }
public function validation($data, $files) { global $CFG; $errors = parent::validation($data, $files); if ($data['h5paction'] === 'upload') { // Validate uploaded H5P file if (empty($data['h5pfile'])) { // Field missing $errors['h5pfile'] = get_string('required'); } else { $files = $this->get_draft_files('h5pfile'); if (count($files) < 1) { // No file uploaded $errors['h5pfile'] = get_string('required'); } else { // Prepare to validate package $file = reset($files); $interface = \mod_hvp\framework::instance('interface'); $path = $CFG->tempdir . uniqid('/hvp-'); $interface->getUploadedH5pFolderPath($path); $path .= '.h5p'; $interface->getUploadedH5pPath($path); $file->copy_content_to($path); $h5pvalidator = \mod_hvp\framework::instance('validator'); if (!$h5pvalidator->isValidPackage()) { // Errors while validating the package $infomessages = implode('<br/>', \mod_hvp\framework::messages('info')); $errormessages = implode('<br/>', \mod_hvp\framework::messages('error')); $errors['h5pfile'] = ($errormessages ? $errormessages . '<br/>' : '') . $infomessages; } } } } else { // Validate library and params used in editor $core = \mod_hvp\framework::instance(); // Get library array from string $library = H5PCore::libraryFromString($data['h5plibrary']); if (!$library) { $errors['h5peditor'] = get_string('invalidlibrary', 'hvp'); } else { // Check that library exists $library['libraryId'] = $core->h5pF->getLibraryId($library['machineName'], $library['majorVersion'], $library['minorVersion']); if (!$library['libraryId']) { $errors['h5peditor'] = get_string('nosuchlibrary', 'hvp'); } else { $data['h5plibrary'] = $library; // Verify that parameters are valid if (empty($data['h5pparams'])) { $errors['h5peditor'] = get_string('noparameters', 'hvp'); } else { $params = json_decode($data['h5pparams']); if ($params === NULL) { $errors['h5peditor'] = get_string('invalidparameters', 'hvp'); } else { $data['h5pparams'] = $params; } } } } } return $errors; }