| 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 | 
           // This file is part of BasicLTI4Moodle
  | 
        
        
            | 
            | 
           18 | 
           //
  | 
        
        
            | 
            | 
           19 | 
           // BasicLTI4Moodle is an IMS BasicLTI (Basic Learning Tools for Interoperability)
  | 
        
        
            | 
            | 
           20 | 
           // consumer for Moodle 1.9 and Moodle 2.0. BasicLTI is a IMS Standard that allows web
  | 
        
        
            | 
            | 
           21 | 
           // based learning tools to be easily integrated in LMS as native ones. The IMS BasicLTI
  | 
        
        
            | 
            | 
           22 | 
           // specification is part of the IMS standard Common Cartridge 1.1 Sakai and other main LMS
  | 
        
        
            | 
            | 
           23 | 
           // are already supporting or going to support BasicLTI. This project Implements the consumer
  | 
        
        
            | 
            | 
           24 | 
           // for Moodle. Moodle is a Free Open source Learning Management System by Martin Dougiamas.
  | 
        
        
            | 
            | 
           25 | 
           // BasicLTI4Moodle is a project iniciated and leaded by Ludo(Marc Alier) and Jordi Piguillem
  | 
        
        
            | 
            | 
           26 | 
           // at the GESSI research group at UPC.
  | 
        
        
            | 
            | 
           27 | 
           // SimpleLTI consumer for Moodle is an implementation of the early specification of LTI
  | 
        
        
            | 
            | 
           28 | 
           // by Charles Severance (Dr Chuck) htp://dr-chuck.com , developed by Jordi Piguillem in a
  | 
        
        
            | 
            | 
           29 | 
           // Google Summer of Code 2008 project co-mentored by Charles Severance and Marc Alier.
  | 
        
        
            | 
            | 
           30 | 
           //
  | 
        
        
            | 
            | 
           31 | 
           // BasicLTI4Moodle is copyright 2009 by Marc Alier Forment, Jordi Piguillem and Nikolas Galanis
  | 
        
        
            | 
            | 
           32 | 
           // of the Universitat Politecnica de Catalunya http://www.upc.edu
  | 
        
        
            | 
            | 
           33 | 
           // Contact info: Marc Alier Forment granludo @ gmail.com or marc.alier @ upc.edu.
  | 
        
        
            | 
            | 
           34 | 
              | 
        
        
            | 
            | 
           35 | 
           /**
  | 
        
        
            | 
            | 
           36 | 
            * This file contains all the backup steps that will be used
  | 
        
        
            | 
            | 
           37 | 
            * by the backup_lti_activity_task
  | 
        
        
            | 
            | 
           38 | 
            *
  | 
        
        
            | 
            | 
           39 | 
            * @package mod_lti
  | 
        
        
            | 
            | 
           40 | 
            * @copyright  2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
  | 
        
        
            | 
            | 
           41 | 
            *  marc.alier@upc.edu
  | 
        
        
            | 
            | 
           42 | 
            * @copyright  2009 Universitat Politecnica de Catalunya http://www.upc.edu
  | 
        
        
            | 
            | 
           43 | 
            * @author     Marc Alier
  | 
        
        
            | 
            | 
           44 | 
            * @author     Jordi Piguillem
  | 
        
        
            | 
            | 
           45 | 
            * @author     Nikolas Galanis
  | 
        
        
            | 
            | 
           46 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           47 | 
            */
  | 
        
        
            | 
            | 
           48 | 
              | 
        
        
            | 
            | 
           49 | 
           defined('MOODLE_INTERNAL') || die;
  | 
        
        
            | 
            | 
           50 | 
              | 
        
        
            | 
            | 
           51 | 
           /**
  | 
        
        
            | 
            | 
           52 | 
            * Define the complete assignment structure for backup, with file and id annotations
  | 
        
        
            | 
            | 
           53 | 
            */
  | 
        
        
            | 
            | 
           54 | 
           class backup_lti_activity_structure_step extends backup_activity_structure_step {
  | 
        
        
            | 
            | 
           55 | 
              | 
        
        
            | 
            | 
           56 | 
               /**
  | 
        
        
            | 
            | 
           57 | 
                * Defines structure of activity backup
  | 
        
        
            | 
            | 
           58 | 
                * @return backup_nested_element
  | 
        
        
            | 
            | 
           59 | 
                */
  | 
        
        
            | 
            | 
           60 | 
               protected function define_structure() {
  | 
        
        
            | 
            | 
           61 | 
                   global $DB;
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
                   // To know if we are including userinfo.
  | 
        
        
            | 
            | 
           64 | 
                   $userinfo = $this->get_setting_value('userinfo');
  | 
        
        
            | 
            | 
           65 | 
              | 
        
        
            | 
            | 
           66 | 
                   // Define each element separated.
  | 
        
        
            | 
            | 
           67 | 
                   $lti = new backup_nested_element('lti', array('id'), array(
  | 
        
        
            | 
            | 
           68 | 
                       'name',
  | 
        
        
            | 
            | 
           69 | 
                       'intro',
  | 
        
        
            | 
            | 
           70 | 
                       'introformat',
  | 
        
        
            | 
            | 
           71 | 
                       'timecreated',
  | 
        
        
            | 
            | 
           72 | 
                       'timemodified',
  | 
        
        
            | 
            | 
           73 | 
                       'typeid',
  | 
        
        
            | 
            | 
           74 | 
                       'toolurl',
  | 
        
        
            | 
            | 
           75 | 
                       'securetoolurl',
  | 
        
        
            | 
            | 
           76 | 
                       'preferheight',
  | 
        
        
            | 
            | 
           77 | 
                       'launchcontainer',
  | 
        
        
            | 
            | 
           78 | 
                       'instructorchoicesendname',
  | 
        
        
            | 
            | 
           79 | 
                       'instructorchoicesendemailaddr',
  | 
        
        
            | 
            | 
           80 | 
                       'instructorchoiceacceptgrades',
  | 
        
        
            | 
            | 
           81 | 
                       'instructorchoiceallowroster',
  | 
        
        
            | 
            | 
           82 | 
                       'instructorchoiceallowsetting',
  | 
        
        
            | 
            | 
           83 | 
                       'grade',
  | 
        
        
            | 
            | 
           84 | 
                       'instructorcustomparameters',
  | 
        
        
            | 
            | 
           85 | 
                       'debuglaunch',
  | 
        
        
            | 
            | 
           86 | 
                       'showtitlelaunch',
  | 
        
        
            | 
            | 
           87 | 
                       'showdescriptionlaunch',
  | 
        
        
            | 
            | 
           88 | 
                       'icon',
  | 
        
        
            | 
            | 
           89 | 
                       'secureicon',
  | 
        
        
            | 
            | 
           90 | 
                       new encrypted_final_element('resourcekey'),
  | 
        
        
            | 
            | 
           91 | 
                       new encrypted_final_element('password'),
  | 
        
        
            | 
            | 
           92 | 
                       )
  | 
        
        
            | 
            | 
           93 | 
                   );
  | 
        
        
            | 
            | 
           94 | 
              | 
        
        
            | 
            | 
           95 | 
                   $ltitype = new backup_nested_element('ltitype', array('id'), array(
  | 
        
        
            | 
            | 
           96 | 
                       'name',
  | 
        
        
            | 
            | 
           97 | 
                       'baseurl',
  | 
        
        
            | 
            | 
           98 | 
                       'tooldomain',
  | 
        
        
            | 
            | 
           99 | 
                       'state',
  | 
        
        
            | 
            | 
           100 | 
                       'course',
  | 
        
        
            | 
            | 
           101 | 
                       'coursevisible',
  | 
        
        
            | 
            | 
           102 | 
                       'ltiversion',
  | 
        
        
            | 
            | 
           103 | 
                       'clientid',
  | 
        
        
            | 
            | 
           104 | 
                       'toolproxyid',
  | 
        
        
            | 
            | 
           105 | 
                       'enabledcapability',
  | 
        
        
            | 
            | 
           106 | 
                       'parameter',
  | 
        
        
            | 
            | 
           107 | 
                       'icon',
  | 
        
        
            | 
            | 
           108 | 
                       'secureicon',
  | 
        
        
            | 
            | 
           109 | 
                       'createdby',
  | 
        
        
            | 
            | 
           110 | 
                       'timecreated',
  | 
        
        
            | 
            | 
           111 | 
                       'timemodified',
  | 
        
        
            | 
            | 
           112 | 
                       'description'
  | 
        
        
            | 
            | 
           113 | 
                       )
  | 
        
        
            | 
            | 
           114 | 
                   );
  | 
        
        
            | 
            | 
           115 | 
              | 
        
        
            | 
            | 
           116 | 
                   $ltitypesconfigs = new backup_nested_element('ltitypesconfigs');
  | 
        
        
            | 
            | 
           117 | 
                   $ltitypesconfig  = new backup_nested_element('ltitypesconfig', array('id'), array(
  | 
        
        
            | 
            | 
           118 | 
                           'name',
  | 
        
        
            | 
            | 
           119 | 
                           'value',
  | 
        
        
            | 
            | 
           120 | 
                       )
  | 
        
        
            | 
            | 
           121 | 
                   );
  | 
        
        
            | 
            | 
           122 | 
                   $ltitypesconfigencrypted  = new backup_nested_element('ltitypesconfigencrypted', array('id'), array(
  | 
        
        
            | 
            | 
           123 | 
                           'name',
  | 
        
        
            | 
            | 
           124 | 
                           new encrypted_final_element('value'),
  | 
        
        
            | 
            | 
           125 | 
                       )
  | 
        
        
            | 
            | 
           126 | 
                   );
  | 
        
        
            | 
            | 
           127 | 
              | 
        
        
            | 
            | 
           128 | 
                   $ltitoolproxy = new backup_nested_element('ltitoolproxy', array('id'));
  | 
        
        
            | 
            | 
           129 | 
              | 
        
        
            | 
            | 
           130 | 
                   $ltitoolsettings = new backup_nested_element('ltitoolsettings');
  | 
        
        
            | 
            | 
           131 | 
                   $ltitoolsetting  = new backup_nested_element('ltitoolsetting', array('id'), array(
  | 
        
        
            | 
            | 
           132 | 
                           'settings',
  | 
        
        
            | 
            | 
           133 | 
                           'timecreated',
  | 
        
        
            | 
            | 
           134 | 
                           'timemodified',
  | 
        
        
            | 
            | 
           135 | 
                       )
  | 
        
        
            | 
            | 
           136 | 
                   );
  | 
        
        
            | 
            | 
           137 | 
              | 
        
        
            | 
            | 
           138 | 
                   $ltisubmissions = new backup_nested_element('ltisubmissions');
  | 
        
        
            | 
            | 
           139 | 
                   $ltisubmission = new backup_nested_element('ltisubmission', array('id'), array(
  | 
        
        
            | 
            | 
           140 | 
                       'userid',
  | 
        
        
            | 
            | 
           141 | 
                       'datesubmitted',
  | 
        
        
            | 
            | 
           142 | 
                       'dateupdated',
  | 
        
        
            | 
            | 
           143 | 
                       'gradepercent',
  | 
        
        
            | 
            | 
           144 | 
                       'originalgrade',
  | 
        
        
            | 
            | 
           145 | 
                       'launchid',
  | 
        
        
            | 
            | 
           146 | 
                       'state'
  | 
        
        
            | 
            | 
           147 | 
                   ));
  | 
        
        
            | 
            | 
           148 | 
              | 
        
        
            | 
            | 
           149 | 
                   $lticoursevisible = new backup_nested_element('lticoursevisible', ['id'], [
  | 
        
        
            | 
            | 
           150 | 
                       'typeid',
  | 
        
        
            | 
            | 
           151 | 
                       'courseid',
  | 
        
        
            | 
            | 
           152 | 
                       'coursevisible',
  | 
        
        
            | 
            | 
           153 | 
                   ]);
  | 
        
        
            | 
            | 
           154 | 
              | 
        
        
            | 
            | 
           155 | 
                   // Build the tree
  | 
        
        
            | 
            | 
           156 | 
                   $lti->add_child($ltitype);
  | 
        
        
            | 
            | 
           157 | 
                   $ltitype->add_child($ltitypesconfigs);
  | 
        
        
            | 
            | 
           158 | 
                   $ltitypesconfigs->add_child($ltitypesconfig);
  | 
        
        
            | 
            | 
           159 | 
                   $ltitypesconfigs->add_child($ltitypesconfigencrypted);
  | 
        
        
            | 
            | 
           160 | 
                   $ltitype->add_child($ltitoolproxy);
  | 
        
        
            | 
            | 
           161 | 
                   $ltitoolproxy->add_child($ltitoolsettings);
  | 
        
        
            | 
            | 
           162 | 
                   $ltitoolsettings->add_child($ltitoolsetting);
  | 
        
        
            | 
            | 
           163 | 
                   $lti->add_child($ltisubmissions);
  | 
        
        
            | 
            | 
           164 | 
                   $ltisubmissions->add_child($ltisubmission);
  | 
        
        
            | 
            | 
           165 | 
                   $lti->add_child($lticoursevisible);
  | 
        
        
            | 
            | 
           166 | 
              | 
        
        
            | 
            | 
           167 | 
                   // Define sources.
  | 
        
        
            | 
            | 
           168 | 
                   $ltirecord = $DB->get_record('lti', ['id' => $this->task->get_activityid()]);
  | 
        
        
            | 
            | 
           169 | 
                   $lti->set_source_array([$ltirecord]);
  | 
        
        
            | 
            | 
           170 | 
              | 
        
        
            | 
            | 
           171 | 
                   $ltitypedata = $this->retrieve_lti_type($ltirecord);
  | 
        
        
            | 
            | 
           172 | 
                   $ltitype->set_source_array($ltitypedata ? [$ltitypedata] : []);
  | 
        
        
            | 
            | 
           173 | 
              | 
        
        
            | 
            | 
           174 | 
                   if (isset($ltitypedata->baseurl)) {
  | 
        
        
            | 
            | 
           175 | 
                       // Add type config values only if the type was backed up. Encrypt password and resourcekey.
  | 
        
        
            | 
            | 
           176 | 
                       $params = [backup_helper::is_sqlparam($ltitypedata->id),
  | 
        
        
            | 
            | 
           177 | 
                           backup_helper::is_sqlparam('password'),
  | 
        
        
            | 
            | 
           178 | 
                           backup_helper::is_sqlparam('resourcekey')];
  | 
        
        
            | 
            | 
           179 | 
                       $ltitypesconfig->set_source_sql("SELECT id, name, value
  | 
        
        
            | 
            | 
           180 | 
                           FROM {lti_types_config}
  | 
        
        
            | 
            | 
           181 | 
                           WHERE typeid = ? AND name <> ? AND name <> ?", $params);
  | 
        
        
            | 
            | 
           182 | 
                       $ltitypesconfigencrypted->set_source_sql("SELECT id, name, value
  | 
        
        
            | 
            | 
           183 | 
                           FROM {lti_types_config}
  | 
        
        
            | 
            | 
           184 | 
                           WHERE typeid = ? AND (name = ? OR name = ?)", $params);
  | 
        
        
            | 
            | 
           185 | 
                   }
  | 
        
        
            | 
            | 
           186 | 
              | 
        
        
            | 
            | 
           187 | 
                   if (!empty($ltitypedata->toolproxyid)) {
  | 
        
        
            | 
            | 
           188 | 
                       // If this is LTI 2 tool add settings for the current activity.
  | 
        
        
            | 
            | 
           189 | 
                       $ltitoolproxy->set_source_array([['id' => $ltitypedata->toolproxyid]]);
  | 
        
        
            | 
            | 
           190 | 
                       $ltitoolsetting->set_source_sql("SELECT *
  | 
        
        
            | 
            | 
           191 | 
                           FROM {lti_tool_settings}
  | 
        
        
            | 
            | 
           192 | 
                           WHERE toolproxyid = ? AND course = ? AND coursemoduleid = ?",
  | 
        
        
            | 
            | 
           193 | 
                           [backup_helper::is_sqlparam($ltitypedata->toolproxyid), backup::VAR_COURSEID, backup::VAR_MODID]);
  | 
        
        
            | 
            | 
           194 | 
                   } else {
  | 
        
        
            | 
            | 
           195 | 
                       $ltitoolproxy->set_source_array([]);
  | 
        
        
            | 
            | 
           196 | 
                   }
  | 
        
        
            | 
            | 
           197 | 
              | 
        
        
            | 
            | 
           198 | 
                   // All the rest of elements only happen if we are including user info.
  | 
        
        
            | 
            | 
           199 | 
                   if ($userinfo) {
  | 
        
        
            | 
            | 
           200 | 
                       $ltisubmission->set_source_table('lti_submission', array('ltiid' => backup::VAR_ACTIVITYID));
  | 
        
        
            | 
            | 
           201 | 
                   }
  | 
        
        
            | 
            | 
           202 | 
              | 
        
        
            | 
            | 
           203 | 
                   $lticoursevisibledata = $this->retrieve_lti_coursevisible($ltirecord);
  | 
        
        
            | 
            | 
           204 | 
                   $lticoursevisible->set_source_array($lticoursevisibledata ? [$lticoursevisibledata] : []);
  | 
        
        
            | 
            | 
           205 | 
              | 
        
        
            | 
            | 
           206 | 
                   // Define id annotations
  | 
        
        
            | 
            | 
           207 | 
                   $ltitype->annotate_ids('user', 'createdby');
  | 
        
        
            | 
            | 
           208 | 
                   $ltitype->annotate_ids('course', 'course');
  | 
        
        
            | 
            | 
           209 | 
                   $ltisubmission->annotate_ids('user', 'userid');
  | 
        
        
            | 
            | 
           210 | 
              | 
        
        
            | 
            | 
           211 | 
                   // Define file annotations.
  | 
        
        
            | 
            | 
           212 | 
                   $lti->annotate_files('mod_lti', 'intro', null); // This file areas haven't itemid.
  | 
        
        
            | 
            | 
           213 | 
              | 
        
        
            | 
            | 
           214 | 
                   // Add support for subplugin structures.
  | 
        
        
            | 
            | 
           215 | 
                   $this->add_subplugin_structure('ltisource', $lti, true);
  | 
        
        
            | 
            | 
           216 | 
                   $this->add_subplugin_structure('ltiservice', $lti, true);
  | 
        
        
            | 
            | 
           217 | 
              | 
        
        
            | 
            | 
           218 | 
                   // Return the root element (lti), wrapped into standard activity structure.
  | 
        
        
            | 
            | 
           219 | 
                   return $this->prepare_activity_structure($lti);
  | 
        
        
            | 
            | 
           220 | 
               }
  | 
        
        
            | 
            | 
           221 | 
              | 
        
        
            | 
            | 
           222 | 
               /**
  | 
        
        
            | 
            | 
           223 | 
                * Retrieves a record from {lti_type} table associated with the current activity
  | 
        
        
            | 
            | 
           224 | 
                *
  | 
        
        
            | 
            | 
           225 | 
                * Information about site tools is not returned because it is insecure to back it up,
  | 
        
        
            | 
            | 
           226 | 
                * only fields necessary for same-site tool matching are left in the record
  | 
        
        
            | 
            | 
           227 | 
                *
  | 
        
        
            | 
            | 
           228 | 
                * @param stdClass $ltirecord record from {lti} table
  | 
        
        
            | 
            | 
           229 | 
                * @return stdClass|null
  | 
        
        
            | 
            | 
           230 | 
                */
  | 
        
        
            | 
            | 
           231 | 
               protected function retrieve_lti_type($ltirecord) {
  | 
        
        
            | 
            | 
           232 | 
                   global $DB;
  | 
        
        
            | 
            | 
           233 | 
                   if (!$ltirecord->typeid) {
  | 
        
        
            | 
            | 
           234 | 
                       return null;
  | 
        
        
            | 
            | 
           235 | 
                   }
  | 
        
        
            | 
            | 
           236 | 
              | 
        
        
            | 
            | 
           237 | 
                   $record = $DB->get_record('lti_types', ['id' => $ltirecord->typeid]);
  | 
        
        
            | 
            | 
           238 | 
                   if ($record && $record->course == SITEID) {
  | 
        
        
            | 
            | 
           239 | 
                       // Site LTI types or registrations are not backed up except for their name (which is visible).
  | 
        
        
            | 
            | 
           240 | 
                       // Predefined course types can be backed up.
  | 
        
        
            | 
            | 
           241 | 
                       $allowedkeys = ['id', 'course', 'name', 'toolproxyid'];
  | 
        
        
            | 
            | 
           242 | 
                       foreach ($record as $key => $value) {
  | 
        
        
            | 
            | 
           243 | 
                           if (!in_array($key, $allowedkeys)) {
  | 
        
        
            | 
            | 
           244 | 
                               $record->$key = null;
  | 
        
        
            | 
            | 
           245 | 
                           }
  | 
        
        
            | 
            | 
           246 | 
                       }
  | 
        
        
            | 
            | 
           247 | 
                   }
  | 
        
        
            | 
            | 
           248 | 
              | 
        
        
            | 
            | 
           249 | 
                   return $record;
  | 
        
        
            | 
            | 
           250 | 
               }
  | 
        
        
            | 
            | 
           251 | 
              | 
        
        
            | 
            | 
           252 | 
               /**
  | 
        
        
            | 
            | 
           253 | 
                * Retrieves a record from {lti_coursevisible} table associated with the current type
  | 
        
        
            | 
            | 
           254 | 
                *
  | 
        
        
            | 
            | 
           255 | 
                * @param stdClass $ltirecord record from {lti} table
  | 
        
        
            | 
            | 
           256 | 
                * @return mixed
  | 
        
        
            | 
            | 
           257 | 
                */
  | 
        
        
            | 
            | 
           258 | 
               protected function retrieve_lti_coursevisible(stdClass $ltirecord): mixed {
  | 
        
        
            | 
            | 
           259 | 
                   global $DB;
  | 
        
        
            | 
            | 
           260 | 
                   if (!$ltirecord->typeid) {
  | 
        
        
            | 
            | 
           261 | 
                       return null;
  | 
        
        
            | 
            | 
           262 | 
                   }
  | 
        
        
            | 
            | 
           263 | 
                   return $DB->get_record('lti_coursevisible', ['typeid' => $ltirecord->typeid, 'courseid' => $ltirecord->course]);
  | 
        
        
            | 
            | 
           264 | 
               }
  | 
        
        
            | 
            | 
           265 | 
           }
  |