Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 * Helper class.
19
 *
20
 * @package    quizaccess_seb
21
 * @author     Dmitrii Metelkin <dmitriim@catalyst-au.net>
22
 * @copyright  2020 Catalyst IT
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace quizaccess_seb;
27
 
28
 
29
use CFPropertyList\CFPropertyList;
30
 
31
defined('MOODLE_INTERNAL') || die();
32
 
33
/**
34
 * Helper class.
35
 *
36
 * @copyright  2020 Catalyst IT
37
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class helper {
40
    /**
41
     * Get a filler icon for display in the actions column of a table.
42
     *
43
     * @param string $url The URL for the icon.
44
     * @param string $icon The icon identifier.
45
     * @param string $alt The alt text for the icon.
46
     * @param string $iconcomponent The icon component.
47
     * @param array $options Display options.
48
     * @return string
49
     */
50
    public static function format_icon_link($url, $icon, $alt, $iconcomponent = 'moodle', $options = []) {
51
        global $OUTPUT;
52
 
53
        return $OUTPUT->action_icon(
54
            $url,
55
            new \pix_icon($icon, $alt, $iconcomponent, [
56
                'title' => $alt,
57
            ]),
58
            null,
59
            $options
60
        );
61
    }
62
 
63
    /**
64
     * Validate seb config string.
65
     *
66
     * @param string $sebconfig
67
     * @return bool
68
     */
69
    public static function is_valid_seb_config(string $sebconfig): bool {
70
        $result = true;
71
 
72
        set_error_handler(function($errno, $errstr, $errfile, $errline ){
73
            throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
74
        });
75
 
76
        $plist = new CFPropertyList();
77
        try {
78
            $plist->parse($sebconfig);
79
        } catch (\ErrorException $e) {
80
            $result = false;
81
        } catch (\Exception $e) {
82
            $result = false;
83
        }
84
 
85
        restore_error_handler();
86
 
87
        return $result;
88
    }
89
 
90
    /**
91
     * A helper function to get a list of seb config file headers.
92
     *
93
     * @param int|null $expiretime  Unix timestamp
94
     * @return array
95
     */
96
    public static function get_seb_file_headers(int $expiretime = null): array {
97
        if (is_null($expiretime)) {
98
            $expiretime = time();
99
        }
100
        $headers = [];
101
        $headers[] = 'Cache-Control: private, max-age=1, no-transform';
102
        $headers[] = 'Expires: '. gmdate('D, d M Y H:i:s', $expiretime) .' GMT';
103
        $headers[] = 'Pragma: no-cache';
104
        $headers[] = 'Content-Disposition: attachment; filename=config.seb';
105
        $headers[] = 'Content-Type: application/seb';
106
 
107
        return $headers;
108
    }
109
 
110
    /**
111
     * Get seb config content for a particular quiz. This method checks caps.
112
     *
113
     * @param string $cmid The course module ID for a quiz with config.
114
     * @return string SEB config string.
115
     */
116
    public static function get_seb_config_content(string $cmid): string {
117
        // Try and get the course module.
118
        $cm = get_coursemodule_from_id('quiz', $cmid, 0, false, MUST_EXIST);
119
 
120
        // Make sure the user is logged in and has access to the module.
121
        require_login($cm->course, false, $cm);
122
 
123
        // Retrieve the config for quiz.
124
        $config = seb_quiz_settings::get_config_by_quiz_id($cm->instance);
125
        if (empty($config)) {
126
            throw new \moodle_exception('noconfigfound', 'quizaccess_seb', '', $cm->id);
127
        }
128
        return $config;
129
    }
130
 
131
    /**
132
     * Serve a file to browser for download.
133
     *
134
     * @param string $contents Contents of file.
135
     */
136
    public static function send_seb_config_file(string $contents) {
137
        // We can now send the file back to the browser.
138
        foreach (self::get_seb_file_headers() as $header) {
139
            header($header);
140
        }
141
 
142
        echo($contents);
143
    }
144
 
145
}
146