| 1 | 
           efrain | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
           // This file is part of Moodle - http://moodle.org/
  | 
        
        
            | 
            | 
           3 | 
           //
  | 
        
        
            | 
            | 
           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.
  | 
        
        
            | 
            | 
           8 | 
           //
  | 
        
        
            | 
            | 
           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.
  | 
        
        
            | 
            | 
           13 | 
           //
  | 
        
        
            | 
            | 
           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/>.
  | 
        
        
            | 
            | 
           16 | 
              | 
        
        
            | 
            | 
           17 | 
           /**
  | 
        
        
            | 
            | 
           18 | 
            * Class for exporting h5p activity data.
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * @package    mod_h5pactivity
  | 
        
        
            | 
            | 
           21 | 
            * @since      Moodle 3.9
  | 
        
        
            | 
            | 
           22 | 
            * @copyright  2020 Carlos Escobedo <carlos@moodle.com>
  | 
        
        
            | 
            | 
           23 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           24 | 
            */
  | 
        
        
            | 
            | 
           25 | 
           namespace mod_h5pactivity\external;
  | 
        
        
            | 
            | 
           26 | 
              | 
        
        
            | 
            | 
           27 | 
           use core\external\exporter;
  | 
        
        
            | 
            | 
           28 | 
           use renderer_base;
  | 
        
        
            | 
            | 
           29 | 
           use core_external\util as external_util;
  | 
        
        
            | 
            | 
           30 | 
           use core_external\external_files;
  | 
        
        
            | 
            | 
           31 | 
           use core_h5p\api;
  | 
        
        
            | 
            | 
           32 | 
              | 
        
        
            | 
            | 
           33 | 
           /**
  | 
        
        
            | 
            | 
           34 | 
            * Class for exporting h5p activity data.
  | 
        
        
            | 
            | 
           35 | 
            *
  | 
        
        
            | 
            | 
           36 | 
            * @copyright  2020 Carlos Escobedo <carlos@moodle.com>
  | 
        
        
            | 
            | 
           37 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           38 | 
            */
  | 
        
        
            | 
            | 
           39 | 
           class h5pactivity_summary_exporter extends exporter {
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
               /**
  | 
        
        
            | 
            | 
           42 | 
                * Properties definition.
  | 
        
        
            | 
            | 
           43 | 
                *
  | 
        
        
            | 
            | 
           44 | 
                * @return array
  | 
        
        
            | 
            | 
           45 | 
                */
  | 
        
        
            | 
            | 
           46 | 
               protected static function define_properties() {
  | 
        
        
            | 
            | 
           47 | 
              | 
        
        
            | 
            | 
           48 | 
                   return [
  | 
        
        
            | 
            | 
           49 | 
                       'id' => [
  | 
        
        
            | 
            | 
           50 | 
                           'type' => PARAM_INT,
  | 
        
        
            | 
            | 
           51 | 
                           'description' => 'The primary key of the record.',
  | 
        
        
            | 
            | 
           52 | 
                       ],
  | 
        
        
            | 
            | 
           53 | 
                       'course' => [
  | 
        
        
            | 
            | 
           54 | 
                           'type' => PARAM_INT,
  | 
        
        
            | 
            | 
           55 | 
                           'description' => 'Course id this h5p activity is part of.',
  | 
        
        
            | 
            | 
           56 | 
                       ],
  | 
        
        
            | 
            | 
           57 | 
                       'name' => [
  | 
        
        
            | 
            | 
           58 | 
                           'type' => PARAM_TEXT,
  | 
        
        
            | 
            | 
           59 | 
                           'description' => 'The name of the activity module instance.',
  | 
        
        
            | 
            | 
           60 | 
                       ],
  | 
        
        
            | 
            | 
           61 | 
                       'timecreated' => [
  | 
        
        
            | 
            | 
           62 | 
                           'type' => PARAM_INT,
  | 
        
        
            | 
            | 
           63 | 
                           'description' => 'Timestamp of when the instance was added to the course.',
  | 
        
        
            | 
            | 
           64 | 
                           'optional' => true,
  | 
        
        
            | 
            | 
           65 | 
                       ],
  | 
        
        
            | 
            | 
           66 | 
                       'timemodified' => [
  | 
        
        
            | 
            | 
           67 | 
                           'type' => PARAM_INT,
  | 
        
        
            | 
            | 
           68 | 
                           'description' => 'Timestamp of when the instance was last modified.',
  | 
        
        
            | 
            | 
           69 | 
                           'optional' => true,
  | 
        
        
            | 
            | 
           70 | 
                       ],
  | 
        
        
            | 
            | 
           71 | 
                       'intro' => [
  | 
        
        
            | 
            | 
           72 | 
                           'default' => '',
  | 
        
        
            | 
            | 
           73 | 
                           'type' => PARAM_RAW,
  | 
        
        
            | 
            | 
           74 | 
                           'description' => 'H5P activity description.',
  | 
        
        
            | 
            | 
           75 | 
                           'null' => NULL_ALLOWED,
  | 
        
        
            | 
            | 
           76 | 
                       ],
  | 
        
        
            | 
            | 
           77 | 
                       'introformat' => [
  | 
        
        
            | 
            | 
           78 | 
                           'choices' => [FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN],
  | 
        
        
            | 
            | 
           79 | 
                           'type' => PARAM_INT,
  | 
        
        
            | 
            | 
           80 | 
                           'default' => FORMAT_MOODLE,
  | 
        
        
            | 
            | 
           81 | 
                           'description' => 'The format of the intro field.',
  | 
        
        
            | 
            | 
           82 | 
                       ],
  | 
        
        
            | 
            | 
           83 | 
                       'grade' => [
  | 
        
        
            | 
            | 
           84 | 
                           'type' => PARAM_INT,
  | 
        
        
            | 
            | 
           85 | 
                           'default' => 0,
  | 
        
        
            | 
            | 
           86 | 
                           'description' => 'The maximum grade for submission.',
  | 
        
        
            | 
            | 
           87 | 
                           'optional' => true,
  | 
        
        
            | 
            | 
           88 | 
                       ],
  | 
        
        
            | 
            | 
           89 | 
                       'displayoptions' => [
  | 
        
        
            | 
            | 
           90 | 
                           'type' => PARAM_INT,
  | 
        
        
            | 
            | 
           91 | 
                           'default' => 0,
  | 
        
        
            | 
            | 
           92 | 
                           'description' => 'H5P Button display options.',
  | 
        
        
            | 
            | 
           93 | 
                       ],
  | 
        
        
            | 
            | 
           94 | 
                       'enabletracking' => [
  | 
        
        
            | 
            | 
           95 | 
                           'type' => PARAM_INT,
  | 
        
        
            | 
            | 
           96 | 
                           'default' => 1,
  | 
        
        
            | 
            | 
           97 | 
                           'description' => 'Enable xAPI tracking.',
  | 
        
        
            | 
            | 
           98 | 
                       ],
  | 
        
        
            | 
            | 
           99 | 
                       'grademethod' => [
  | 
        
        
            | 
            | 
           100 | 
                           'type' => PARAM_INT,
  | 
        
        
            | 
            | 
           101 | 
                           'default' => 1,
  | 
        
        
            | 
            | 
           102 | 
                           'description' => 'Which H5P attempt is used for grading.',
  | 
        
        
            | 
            | 
           103 | 
                       ],
  | 
        
        
            | 
            | 
           104 | 
                       'contenthash' => [
  | 
        
        
            | 
            | 
           105 | 
                           'type' => PARAM_ALPHANUM,
  | 
        
        
            | 
            | 
           106 | 
                           'description' => 'Sha1 hash of file content.',
  | 
        
        
            | 
            | 
           107 | 
                           'optional' => true,
  | 
        
        
            | 
            | 
           108 | 
                       ],
  | 
        
        
            | 
            | 
           109 | 
                   ];
  | 
        
        
            | 
            | 
           110 | 
               }
  | 
        
        
            | 
            | 
           111 | 
              | 
        
        
            | 
            | 
           112 | 
               /**
  | 
        
        
            | 
            | 
           113 | 
                * Related objects definition.
  | 
        
        
            | 
            | 
           114 | 
                *
  | 
        
        
            | 
            | 
           115 | 
                * @return array
  | 
        
        
            | 
            | 
           116 | 
                */
  | 
        
        
            | 
            | 
           117 | 
               protected static function define_related() {
  | 
        
        
            | 
            | 
           118 | 
                   return [
  | 
        
        
            | 
            | 
           119 | 
                       'context' => 'context',
  | 
        
        
            | 
            | 
           120 | 
                       'factory' => 'core_h5p\\factory'
  | 
        
        
            | 
            | 
           121 | 
                   ];
  | 
        
        
            | 
            | 
           122 | 
               }
  | 
        
        
            | 
            | 
           123 | 
              | 
        
        
            | 
            | 
           124 | 
               /**
  | 
        
        
            | 
            | 
           125 | 
                * Other properties definition.
  | 
        
        
            | 
            | 
           126 | 
                *
  | 
        
        
            | 
            | 
           127 | 
                * @return array
  | 
        
        
            | 
            | 
           128 | 
                */
  | 
        
        
            | 
            | 
           129 | 
               protected static function define_other_properties() {
  | 
        
        
            | 
            | 
           130 | 
                   return [
  | 
        
        
            | 
            | 
           131 | 
                       'coursemodule' => [
  | 
        
        
            | 
            | 
           132 | 
                           'type' => PARAM_INT
  | 
        
        
            | 
            | 
           133 | 
                       ],
  | 
        
        
            | 
            | 
           134 | 
                       'context' => [
  | 
        
        
            | 
            | 
           135 | 
                           'type' => PARAM_INT
  | 
        
        
            | 
            | 
           136 | 
                       ],
  | 
        
        
            | 
            | 
           137 | 
                       'introfiles' => [
  | 
        
        
            | 
            | 
           138 | 
                           'type' => external_files::get_properties_for_exporter(),
  | 
        
        
            | 
            | 
           139 | 
                           'multiple' => true
  | 
        
        
            | 
            | 
           140 | 
                       ],
  | 
        
        
            | 
            | 
           141 | 
                       'package' => [
  | 
        
        
            | 
            | 
           142 | 
                           'type' => external_files::get_properties_for_exporter(),
  | 
        
        
            | 
            | 
           143 | 
                           'multiple' => true
  | 
        
        
            | 
            | 
           144 | 
                       ],
  | 
        
        
            | 
            | 
           145 | 
                       'deployedfile' => [
  | 
        
        
            | 
            | 
           146 | 
                           'optional' => true,
  | 
        
        
            | 
            | 
           147 | 
                           'description' => 'H5P file deployed.',
  | 
        
        
            | 
            | 
           148 | 
                           'type' => [
  | 
        
        
            | 
            | 
           149 | 
                               'filename' => array(
  | 
        
        
            | 
            | 
           150 | 
                                   'type' => PARAM_FILE,
  | 
        
        
            | 
            | 
           151 | 
                                   'description' => 'File name.',
  | 
        
        
            | 
            | 
           152 | 
                                   'optional' => true,
  | 
        
        
            | 
            | 
           153 | 
                                   'null' => NULL_NOT_ALLOWED,
  | 
        
        
            | 
            | 
           154 | 
                               ),
  | 
        
        
            | 
            | 
           155 | 
                               'filepath' => array(
  | 
        
        
            | 
            | 
           156 | 
                                   'type' => PARAM_PATH,
  | 
        
        
            | 
            | 
           157 | 
                                   'description' => 'File path.',
  | 
        
        
            | 
            | 
           158 | 
                                   'optional' => true,
  | 
        
        
            | 
            | 
           159 | 
                                   'null' => NULL_NOT_ALLOWED,
  | 
        
        
            | 
            | 
           160 | 
                               ),
  | 
        
        
            | 
            | 
           161 | 
                               'filesize' => array(
  | 
        
        
            | 
            | 
           162 | 
                                   'type' => PARAM_INT,
  | 
        
        
            | 
            | 
           163 | 
                                   'description' => 'File size.',
  | 
        
        
            | 
            | 
           164 | 
                                   'optional' => true,
  | 
        
        
            | 
            | 
           165 | 
                                   'null' => NULL_NOT_ALLOWED,
  | 
        
        
            | 
            | 
           166 | 
                               ),
  | 
        
        
            | 
            | 
           167 | 
                               'fileurl' => array(
  | 
        
        
            | 
            | 
           168 | 
                                   'type' => PARAM_URL,
  | 
        
        
            | 
            | 
           169 | 
                                   'description' => 'Downloadable file url.',
  | 
        
        
            | 
            | 
           170 | 
                                   'optional' => true,
  | 
        
        
            | 
            | 
           171 | 
                                   'null' => NULL_NOT_ALLOWED,
  | 
        
        
            | 
            | 
           172 | 
                               ),
  | 
        
        
            | 
            | 
           173 | 
                               'timemodified' => array(
  | 
        
        
            | 
            | 
           174 | 
                                   'type' => PARAM_INT,
  | 
        
        
            | 
            | 
           175 | 
                                   'description' => 'Time modified.',
  | 
        
        
            | 
            | 
           176 | 
                                   'optional' => true,
  | 
        
        
            | 
            | 
           177 | 
                                   'null' => NULL_NOT_ALLOWED,
  | 
        
        
            | 
            | 
           178 | 
                               ),
  | 
        
        
            | 
            | 
           179 | 
                               'mimetype' => array(
  | 
        
        
            | 
            | 
           180 | 
                                   'type' => PARAM_RAW,
  | 
        
        
            | 
            | 
           181 | 
                                   'description' => 'File mime type.',
  | 
        
        
            | 
            | 
           182 | 
                                   'optional' => true,
  | 
        
        
            | 
            | 
           183 | 
                                   'null' => NULL_NOT_ALLOWED,
  | 
        
        
            | 
            | 
           184 | 
                               )
  | 
        
        
            | 
            | 
           185 | 
                           ]
  | 
        
        
            | 
            | 
           186 | 
                       ],
  | 
        
        
            | 
            | 
           187 | 
                   ];
  | 
        
        
            | 
            | 
           188 | 
               }
  | 
        
        
            | 
            | 
           189 | 
              | 
        
        
            | 
            | 
           190 | 
               /**
  | 
        
        
            | 
            | 
           191 | 
                * Assign values to the defined other properties.
  | 
        
        
            | 
            | 
           192 | 
                *
  | 
        
        
            | 
            | 
           193 | 
                * @param renderer_base $output The output renderer object.
  | 
        
        
            | 
            | 
           194 | 
                * @return array
  | 
        
        
            | 
            | 
           195 | 
                */
  | 
        
        
            | 
            | 
           196 | 
               protected function get_other_values(renderer_base $output) {
  | 
        
        
            | 
            | 
           197 | 
                   $context = $this->related['context'];
  | 
        
        
            | 
            | 
           198 | 
                   $factory = $this->related['factory'];
  | 
        
        
            | 
            | 
           199 | 
              | 
        
        
            | 
            | 
           200 | 
                   $values = [
  | 
        
        
            | 
            | 
           201 | 
                       'coursemodule' => $context->instanceid,
  | 
        
        
            | 
            | 
           202 | 
                       'context' => $context->id,
  | 
        
        
            | 
            | 
           203 | 
                   ];
  | 
        
        
            | 
            | 
           204 | 
              | 
        
        
            | 
            | 
           205 | 
                   $values['introfiles'] = external_util::get_area_files($context->id, 'mod_h5pactivity', 'intro', false, false);
  | 
        
        
            | 
            | 
           206 | 
              | 
        
        
            | 
            | 
           207 | 
                   $values['package'] = external_util::get_area_files($context->id, 'mod_h5pactivity', 'package', false, true);
  | 
        
        
            | 
            | 
           208 | 
              | 
        
        
            | 
            | 
           209 | 
                   // Only if this H5P activity has been deployed, return the exported file.
  | 
        
        
            | 
            | 
           210 | 
                   $fileh5p = api::get_export_info_from_context_id($context->id, $factory, 'mod_h5pactivity', 'package');
  | 
        
        
            | 
            | 
           211 | 
                   if ($fileh5p) {
  | 
        
        
            | 
            | 
           212 | 
                       $values['deployedfile'] = $fileh5p;
  | 
        
        
            | 
            | 
           213 | 
                   }
  | 
        
        
            | 
            | 
           214 | 
              | 
        
        
            | 
            | 
           215 | 
                   return $values;
  | 
        
        
            | 
            | 
           216 | 
               }
  | 
        
        
            | 
            | 
           217 | 
              | 
        
        
            | 
            | 
           218 | 
               /**
  | 
        
        
            | 
            | 
           219 | 
                * Get the formatting parameters for the intro.
  | 
        
        
            | 
            | 
           220 | 
                *
  | 
        
        
            | 
            | 
           221 | 
                * @return array with the formatting parameters
  | 
        
        
            | 
            | 
           222 | 
                */
  | 
        
        
            | 
            | 
           223 | 
               protected function get_format_parameters_for_intro() {
  | 
        
        
            | 
            | 
           224 | 
                   return [
  | 
        
        
            | 
            | 
           225 | 
                       'component' => 'mod_h5pactivity',
  | 
        
        
            | 
            | 
           226 | 
                       'filearea' => 'intro',
  | 
        
        
            | 
            | 
           227 | 
                       'options' => ['noclean' => true],
  | 
        
        
            | 
            | 
           228 | 
                   ];
  | 
        
        
            | 
            | 
           229 | 
               }
  | 
        
        
            | 
            | 
           230 | 
              | 
        
        
            | 
            | 
           231 | 
               /**
  | 
        
        
            | 
            | 
           232 | 
                * Get the formatting parameters for the package.
  | 
        
        
            | 
            | 
           233 | 
                *
  | 
        
        
            | 
            | 
           234 | 
                * @return array with the formatting parameters
  | 
        
        
            | 
            | 
           235 | 
                */
  | 
        
        
            | 
            | 
           236 | 
               protected function get_format_parameters_for_package() {
  | 
        
        
            | 
            | 
           237 | 
                   return [
  | 
        
        
            | 
            | 
           238 | 
                       'component' => 'mod_h5pactivity',
  | 
        
        
            | 
            | 
           239 | 
                       'filearea' => 'package',
  | 
        
        
            | 
            | 
           240 | 
                       'itemid' => 0,
  | 
        
        
            | 
            | 
           241 | 
                       'options' => ['noclean' => true],
  | 
        
        
            | 
            | 
           242 | 
                   ];
  | 
        
        
            | 
            | 
           243 | 
               }
  | 
        
        
            | 
            | 
           244 | 
           }
  |