| 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 | 
            * Open a given resource in Amanote.
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * If the user doesn't have access to the resource, it will search if there is another
  | 
        
        
            | 
            | 
           21 | 
            * corresponding resource in an other course (from backup/restore, for instance) in
  | 
        
        
            | 
            | 
           22 | 
            * which the current user is enrolled in.
  | 
        
        
            | 
            | 
           23 | 
            *
  | 
        
        
            | 
            | 
           24 | 
            * @package     filter_amanote
  | 
        
        
            | 
            | 
           25 | 
            * @copyright   2021 Amaplex Software
  | 
        
        
            | 
            | 
           26 | 
            * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           27 | 
            */
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
              | 
        
        
            | 
            | 
           30 | 
           require_once('../../config.php');
  | 
        
        
            | 
            | 
           31 | 
           require_once('../../lib/externallib.php');
  | 
        
        
            | 
            | 
           32 | 
           require_once('./amanote.php');
  | 
        
        
            | 
            | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
           // Verify that the current user is logged in.
  | 
        
        
            | 
            | 
           35 | 
           require_login();
  | 
        
        
            | 
            | 
           36 | 
              | 
        
        
            | 
            | 
           37 | 
           // Initialize params.
  | 
        
        
            | 
            | 
           38 | 
           $annotatableid = isset($_GET['annotatableId']) ? $_GET['annotatableId'] : null;
  | 
        
        
            | 
            | 
           39 | 
              | 
        
        
            | 
            | 
           40 | 
           if ($annotatableid == null) {
  | 
        
        
            | 
            | 
           41 | 
               // Backward compatibility.
  | 
        
        
            | 
            | 
           42 | 
               $annotatableid = isset($_GET['amaResourceId']) ? $_GET['amaResourceId'] : null;
  | 
        
        
            | 
            | 
           43 | 
           }
  | 
        
        
            | 
            | 
           44 | 
              | 
        
        
            | 
            | 
           45 | 
           $pagenumber = isset($_GET['pageNumber']) ? $_GET['pageNumber'] : null;
  | 
        
        
            | 
            | 
           46 | 
           $file = null;
  | 
        
        
            | 
            | 
           47 | 
              | 
        
        
            | 
            | 
           48 | 
           if (!validate_annotatableid($annotatableid)) {
  | 
        
        
            | 
            | 
           49 | 
               echo 'Invalid resource id.';
  | 
        
        
            | 
            | 
           50 | 
               exit();
  | 
        
        
            | 
            | 
           51 | 
           }
  | 
        
        
            | 
            | 
           52 | 
              | 
        
        
            | 
            | 
           53 | 
           $explodedid = explode('.', $annotatableid);
  | 
        
        
            | 
            | 
           54 | 
              | 
        
        
            | 
            | 
           55 | 
           // Get the file corresponding to the given id in a course in which the user is enrolled.
  | 
        
        
            | 
            | 
           56 | 
           if (count($explodedid) >= 3) {
  | 
        
        
            | 
            | 
           57 | 
               $courseid = $explodedid[0];
  | 
        
        
            | 
            | 
           58 | 
               $modid = $explodedid[1];
  | 
        
        
            | 
            | 
           59 | 
               $fileid = $explodedid[2];
  | 
        
        
            | 
            | 
           60 | 
              | 
        
        
            | 
            | 
           61 | 
               $sql = "SELECT
  | 
        
        
            | 
            | 
           62 | 
                       {files}.id AS id,
  | 
        
        
            | 
            | 
           63 | 
                       {files}.contextid,
  | 
        
        
            | 
            | 
           64 | 
                       {files}.component,
  | 
        
        
            | 
            | 
           65 | 
                       {files}.filearea,
  | 
        
        
            | 
            | 
           66 | 
                       {files}.filename,
  | 
        
        
            | 
            | 
           67 | 
                       {files}.filepath,
  | 
        
        
            | 
            | 
           68 | 
                       {files}.mimetype,
  | 
        
        
            | 
            | 
           69 | 
                       {course_modules}.instance AS modid,
  | 
        
        
            | 
            | 
           70 | 
                       {course_modules}.course AS courseid
  | 
        
        
            | 
            | 
           71 | 
                       FROM {files}
  | 
        
        
            | 
            | 
           72 | 
                       INNER JOIN {context} ON {context}.id = {files}.contextid
  | 
        
        
            | 
            | 
           73 | 
                       INNER JOIN {course_modules} ON {course_modules}.id = {context}.instanceid
  | 
        
        
            | 
            | 
           74 | 
                       WHERE (contenthash, timecreated)=
  | 
        
        
            | 
            | 
           75 | 
                           (SELECT contenthash,timecreated FROM {files}
  | 
        
        
            | 
            | 
           76 | 
                           WHERE {files}.id=:fileid)
  | 
        
        
            | 
            | 
           77 | 
                       AND {course_modules}.course IN
  | 
        
        
            | 
            | 
           78 | 
                           (SELECT courseId FROM {enrol}
  | 
        
        
            | 
            | 
           79 | 
                           INNER JOIN {user_enrolments} ON {enrol}.id = {user_enrolments}.enrolid
  | 
        
        
            | 
            | 
           80 | 
                           WHERE {user_enrolments}.userid=:userid)
  | 
        
        
            | 
            | 
           81 | 
                       ORDER BY ABS({files}.id - :fileid2) ASC";
  | 
        
        
            | 
            | 
           82 | 
              | 
        
        
            | 
            | 
           83 | 
               $result = $DB->get_records_sql($sql, ['fileid' => $fileid, 'userid' => $USER->id, 'fileid2' => $fileid]);
  | 
        
        
            | 
            | 
           84 | 
              | 
        
        
            | 
            | 
           85 | 
               if (!$result || count($result) <= 0) {
  | 
        
        
            | 
            | 
           86 | 
                   echo 'You don\'t have access to this resource.';
  | 
        
        
            | 
            | 
           87 | 
                   exit();
  | 
        
        
            | 
            | 
           88 | 
               }
  | 
        
        
            | 
            | 
           89 | 
              | 
        
        
            | 
            | 
           90 | 
               $files = array_values($result);
  | 
        
        
            | 
            | 
           91 | 
               $file = array_shift($files);
  | 
        
        
            | 
            | 
           92 | 
              | 
        
        
            | 
            | 
           93 | 
               $annotatableid = $file->courseid . '.' . $file->modid . '.' . $file->id;
  | 
        
        
            | 
            | 
           94 | 
           }
  | 
        
        
            | 
            | 
           95 | 
              | 
        
        
            | 
            | 
           96 | 
           // Generate the Amanote's URL.
  | 
        
        
            | 
            | 
           97 | 
           $url = generate_amanote_url($annotatableid, $file, $pagenumber);
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
           // Redirect to Amanote.
  | 
        
        
            | 
            | 
           100 | 
           echo "If you are not redirected please <a href=\"$url\">click here</a>
  | 
        
        
            | 
            | 
           101 | 
           <script language='JavaScript'>
  | 
        
        
            | 
            | 
           102 | 
           window.location.href='$url';
  | 
        
        
            | 
            | 
           103 | 
           </script>";
  |