Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-38509 Implement the ZIP package validation page

The script validate.php expects a ZIP file stored in a temporary
location. It extracts the ZIP (optionally renaming the root directory)
and executes the validator. Then it renders the validator log messages
and continue buttons.
  • Loading branch information...
commit cbd125e85d739df4991e4d006947d66ea7fa4930 1 parent 07083b2
David Mudrák authored March 22, 2013
8  admin/tool/installaddon/classes/installfromzip_form.php
@@ -43,6 +43,9 @@ public function definition() {
43 43
         $mform = $this->_form;
44 44
         $installer = $this->_customdata['installer'];
45 45
 
  46
+        $mform->addElement('header', 'general', get_string('installfromzip', 'tool_installaddon'));
  47
+        $mform->addHelpButton('general', 'installfromzip', 'tool_installaddon');
  48
+
46 49
         $options = $installer->get_plugin_types_menu();
47 50
         $mform->addElement('select', 'plugintype', get_string('installfromziptype', 'tool_installaddon'), $options,
48 51
             array('id' => 'tool_installaddon_installfromzip_plugintype'));
@@ -58,6 +61,11 @@ public function definition() {
58 61
         $mform->addHelpButton('zipfile', 'installfromzipfile', 'tool_installaddon');
59 62
         $mform->addRule('zipfile', null, 'required', null, 'client');
60 63
 
  64
+        $mform->addElement('text', 'rootdir', get_string('installfromziprootdir', 'tool_installaddon'));
  65
+        $mform->addHelpButton('rootdir', 'installfromziprootdir', 'tool_installaddon');
  66
+        $mform->setType('rootdir', PARAM_PLUGIN);
  67
+        $mform->setAdvanced('rootdir');
  68
+
61 69
         $mform->addElement('checkbox', 'acknowledgement', get_string('acknowledgement', 'tool_installaddon'),
62 70
             ' '.get_string('acknowledgementtext', 'tool_installaddon'));
63 71
         $mform->addRule('acknowledgement', get_string('acknowledgementmust', 'tool_installaddon'), 'required', null, 'client');
160  admin/tool/installaddon/renderer.php
@@ -37,6 +37,9 @@ class tool_installaddon_renderer extends plugin_renderer_base {
37 37
     /** @var tool_installaddon_installer */
38 38
     protected $installer = null;
39 39
 
  40
+    /** @var tool_installaddon_validator */
  41
+    protected $validator = null;
  42
+
40 43
     /**
41 44
      * Sets the tool_installaddon_installer instance being used.
42 45
      *
@@ -52,12 +55,30 @@ public function set_installer_instance(tool_installaddon_installer $installer) {
52 55
     }
53 56
 
54 57
     /**
  58
+     * Sets the tool_installaddon_validator instance being used.
  59
+     *
  60
+     * @throws coding_exception if the validator has been already set
  61
+     * @param tool_installaddon_validator $validator
  62
+     */
  63
+    public function set_validator_instance(tool_installaddon_validator $validator) {
  64
+        if (is_null($this->validator)) {
  65
+            $this->validator = $validator;
  66
+        } else {
  67
+            throw new coding_exception('Attempting to reset the validator instance.');
  68
+        }
  69
+    }
  70
+
  71
+    /**
55 72
      * Defines the index page layout
56 73
      *
57 74
      * @return string
58 75
      */
59 76
     public function index_page() {
60 77
 
  78
+        if (is_null($this->installer)) {
  79
+            throw new coding_exception('Installer instance has not been set.');
  80
+        }
  81
+
61 82
         $permcheckurl = new moodle_url('/admin/tool/installaddon/permcheck.php');
62 83
         $this->page->requires->yui_module('moodle-tool_installaddon-permcheck', 'M.tool_installaddon.permcheck.init',
63 84
             array(array('permcheckurl' => $permcheckurl->out())));
@@ -74,6 +95,33 @@ public function index_page() {
74 95
     }
75 96
 
76 97
     /**
  98
+     * Defines the validation results page layout
  99
+     *
  100
+     * @return string
  101
+     */
  102
+    public function validation_page() {
  103
+
  104
+        if (is_null($this->installer)) {
  105
+            throw new coding_exception('Installer instance has not been set.');
  106
+        }
  107
+
  108
+        if (is_null($this->validator)) {
  109
+            throw new coding_exception('Validator instance has not been set.');
  110
+        }
  111
+
  112
+        $out = $this->output->header();
  113
+        $out .= $this->validation_page_heading();
  114
+        $out .= $this->validation_page_messages();
  115
+        $out .= $this->validation_page_continue();
  116
+        $out .= $this->output->footer();
  117
+
  118
+        return $out;
  119
+
  120
+    }
  121
+
  122
+    // End of the external API /////////////////////////////////////////////////
  123
+
  124
+    /**
77 125
      * Renders the index page heading
78 126
      *
79 127
      * @return string
@@ -117,4 +165,116 @@ protected function index_page_upload() {
117 165
 
118 166
         return $out;
119 167
     }
  168
+
  169
+    /**
  170
+     * Renders the page title and the overall validation verdict
  171
+     *
  172
+     * @return string
  173
+     */
  174
+    protected function validation_page_heading() {
  175
+
  176
+        $heading = $this->output->heading(get_string('validation', 'tool_installaddon'));
  177
+
  178
+        if ($this->validator->get_result()) {
  179
+            $status = $this->output->container(
  180
+                html_writer::span(get_string('validationresult1', 'tool_installaddon'), 'verdict').
  181
+                    $this->output->help_icon('validationresult1', 'tool_installaddon'),
  182
+                array('validationresult', 'success')
  183
+            );
  184
+        } else {
  185
+            $status = $this->output->container(
  186
+                html_writer::span(get_string('validationresult0', 'tool_installaddon'), 'verdict').
  187
+                    $this->output->help_icon('validationresult0', 'tool_installaddon'),
  188
+                array('validationresult', 'failure')
  189
+            );
  190
+        }
  191
+
  192
+        return $heading . $status;
  193
+    }
  194
+
  195
+    /**
  196
+     * Renders validation log messages.
  197
+     *
  198
+     * @return string
  199
+     */
  200
+    protected function validation_page_messages() {
  201
+
  202
+        $validator = $this->validator; // We need this to be able to use their constants.
  203
+        $messages = $validator->get_messages();
  204
+
  205
+        if (empty($messages)) {
  206
+            return '';
  207
+        }
  208
+
  209
+        $table = new html_table();
  210
+        $table->attributes['class'] = 'validationmessages generaltable';
  211
+        $table->head = array(
  212
+            get_string('validationresultstatus', 'tool_installaddon'),
  213
+            get_string('validationresultmsg', 'tool_installaddon'),
  214
+            get_string('validationresultinfo', 'tool_installaddon')
  215
+        );
  216
+        $table->colclasses = array('msgstatus', 'msgtext', 'msginfo');
  217
+
  218
+        $stringman = get_string_manager();
  219
+
  220
+        foreach ($messages as $message) {
  221
+
  222
+            if ($message->level === $validator::DEBUG and !debugging()) {
  223
+                continue;
  224
+            }
  225
+
  226
+            $msgstatus = get_string('validationmsglevel_'.$message->level, 'tool_installaddon');
  227
+            $msgtext = $msgtext = s($message->msgcode);
  228
+            if (is_null($message->addinfo)) {
  229
+                $msginfo = '';
  230
+            } else {
  231
+                $msginfo = html_writer::tag('pre', s(print_r($message->addinfo, true)));
  232
+            }
  233
+            $msghelp = '';
  234
+
  235
+            // Replace the message code with the string if it is defined.
  236
+            if ($stringman->string_exists('validationmsg_'.$message->msgcode, 'tool_installaddon')) {
  237
+                $msgtext = get_string('validationmsg_'.$message->msgcode, 'tool_installaddon');
  238
+                // And check for the eventual help, too.
  239
+                if ($stringman->string_exists('validationmsg_'.$message->msgcode.'_help', 'tool_installaddon')) {
  240
+                    $msghelp = $this->output->help_icon('validationmsg_'.$message->msgcode, 'tool_installaddon');
  241
+                }
  242
+            }
  243
+
  244
+            // Re-format the message info using a string if it is define.
  245
+            if (!is_null($message->addinfo) and $stringman->string_exists('validationmsg_'.$message->msgcode.'_info', 'tool_installaddon')) {
  246
+                $msginfo = get_string('validationmsg_'.$message->msgcode.'_info', 'tool_installaddon', $message->addinfo);
  247
+            }
  248
+
  249
+            $row = new html_table_row(array($msgstatus, $msgtext.$msghelp, $msginfo));
  250
+            $row->attributes['class'] = 'level-'.$message->level.' '.$message->msgcode;
  251
+
  252
+            $table->data[] = $row;
  253
+        }
  254
+
  255
+        return html_writer::table($table);
  256
+    }
  257
+
  258
+    /**
  259
+     * Renders widgets to continue from the validation results page
  260
+     *
  261
+     * @return string
  262
+     */
  263
+    protected function validation_page_continue() {
  264
+
  265
+        $conturl = $this->validator->get_continue_url();
  266
+        if (is_null($conturl)) {
  267
+            $contbutton = '';
  268
+        } else {
  269
+            $contbutton = $this->output->single_button(
  270
+                $conturl, get_string('installaddon', 'tool_installaddon'), 'post',
  271
+                array('class' => 'singlebutton continuebutton'));
  272
+        }
  273
+
  274
+        $cancelbutton = $this->output->single_button(
  275
+            new moodle_url('/admin/tool/installaddon/index.php'), get_string('cancel', 'core'), 'get',
  276
+            array('class' => 'singlebutton cancelbutton'));
  277
+
  278
+        return $this->output->container($cancelbutton.$contbutton, 'postvalidationbuttons');
  279
+    }
120 280
 }
7  admin/tool/installaddon/settings.php
@@ -29,6 +29,11 @@
29 29
 
30 30
     $ADMIN->add('modules', new admin_externalpage('tool_installaddon_index',
31 31
         get_string('installaddons', 'tool_installaddon'),
32  
-        "$CFG->wwwroot/$CFG->admin/tool/installaddon/"));
  32
+        "$CFG->wwwroot/$CFG->admin/tool/installaddon/index.php"));
33 33
 
  34
+    $ADMIN->add('modules', new admin_externalpage('tool_installaddon_validate',
  35
+        get_string('validation', 'tool_installaddon'),
  36
+        "$CFG->wwwroot/$CFG->admin/tool/installaddon/validate.php",
  37
+        'moodle/site:config',
  38
+        true));
34 39
 }
55  admin/tool/installaddon/styles.css
@@ -11,3 +11,58 @@
11 11
 #page-admin-tool-installaddon-index #installfromrepobox .singlebutton input[type=submit] {
12 12
     padding: 1em;
13 13
 }
  14
+
  15
+#page-admin-tool-installaddon-validate .validationresult {
  16
+    margin: 2em auto;
  17
+    text-align: center;
  18
+}
  19
+
  20
+#page-admin-tool-installaddon-validate .validationresult .verdict {
  21
+    margin: 0em 0.5em;
  22
+    padding: 0.5em;
  23
+    border: 2px solid;
  24
+    -webkit-border-radius: 5px;
  25
+    -moz-border-radius: 5px;
  26
+    border-radius: 5px;
  27
+    font-weight: bold;
  28
+}
  29
+
  30
+#page-admin-tool-installaddon-validate .validationresult.success .verdict {
  31
+    background-color: #e7f1c3;
  32
+    border-color: #aaeeaa;
  33
+}
  34
+
  35
+#page-admin-tool-installaddon-validate .validationresult.failure .verdict {
  36
+    background-color: #ffd3d9;
  37
+    border-color: #eeaaaa;
  38
+}
  39
+
  40
+#page-admin-tool-installaddon-validate .validationmessages {
  41
+    margin: 0px auto;
  42
+}
  43
+
  44
+#page-admin-tool-installaddon-validate .validationmessages .level-error .msgstatus {
  45
+    background-color: #ffd3d9;
  46
+}
  47
+
  48
+#page-admin-tool-installaddon-validate .validationmessages .level-warning .msgstatus {
  49
+    background-color: #f3f2aa;
  50
+}
  51
+
  52
+#page-admin-tool-installaddon-validate .validationmessages .level-info .msgstatus {
  53
+    background-color: #e7f1c3;
  54
+}
  55
+
  56
+#page-admin-tool-installaddon-validate .validationmessages .level-debug .msgstatus {
  57
+    background-color: #d2ebff;
  58
+}
  59
+
  60
+#page-admin-tool-installaddon-validate .postvalidationbuttons {
  61
+    text-align: center;
  62
+    margin: 1em auto;
  63
+}
  64
+
  65
+#page-admin-tool-installaddon-validate .postvalidationbuttons .singlebutton {
  66
+    display: inline-block;
  67
+    margin: 1em 1em;
  68
+}
80  admin/tool/installaddon/validate.php
... ...
@@ -0,0 +1,80 @@
  1
+<?php
  2
+
  3
+// This file is part of Moodle - http://moodle.org/
  4
+//
  5
+// Moodle is free software: you can redistribute it and/or modify
  6
+// it under the terms of the GNU General Public License as published by
  7
+// the Free Software Foundation, either version 3 of the License, or
  8
+// (at your option) any later version.
  9
+//
  10
+// Moodle is distributed in the hope that it will be useful,
  11
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
  12
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13
+// GNU General Public License for more details.
  14
+//
  15
+// You should have received a copy of the GNU General Public License
  16
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17
+
  18
+/**
  19
+ * The ZIP package validation.
  20
+ *
  21
+ * @package     tool_installaddon
  22
+ * @copyright   2013 David Mudrak <david@moodle.com>
  23
+ * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24
+ */
  25
+
  26
+require(dirname(__FILE__) . '/../../../config.php');
  27
+require_once($CFG->libdir.'/adminlib.php');
  28
+require_once($CFG->libdir.'/filelib.php');
  29
+require_once(dirname(__FILE__).'/classes/installer.php');
  30
+require_once(dirname(__FILE__).'/classes/validator.php');
  31
+
  32
+navigation_node::override_active_url(new moodle_url('/admin/tool/installaddon/index.php'));
  33
+admin_externalpage_setup('tool_installaddon_validate');
  34
+
  35
+if (!empty($CFG->disableonclickaddoninstall)) {
  36
+    notice(get_string('featuredisabled', 'tool_installaddon'));
  37
+}
  38
+
  39
+require_sesskey();
  40
+
  41
+$jobid = required_param('jobid', PARAM_ALPHANUM);
  42
+$zipfilename = required_param('zip', PARAM_FILE);
  43
+$plugintype = required_param('type', PARAM_ALPHANUMEXT);
  44
+$rootdir = optional_param('rootdir', '', PARAM_PLUGIN);
  45
+
  46
+$zipfilepath = $CFG->tempdir.'/tool_installaddon/'.$jobid.'/source/'.$zipfilename;
  47
+if (!file_exists($zipfilepath)) {
  48
+    redirect(new moodle_url('/admin/tool/installaddon/index.php'),
  49
+        get_string('invaliddata', 'core_error'));
  50
+}
  51
+
  52
+$installer = tool_installaddon_installer::instance();
  53
+
  54
+// Extract the ZIP contents.
  55
+fulldelete($CFG->tempdir.'/tool_installaddon/'.$jobid.'/contents');
  56
+$zipcontentpath = make_temp_directory('tool_installaddon/'.$jobid.'/contents');
  57
+$zipcontentfiles = $installer->extract_installfromzip_file($zipfilepath, $zipcontentpath, $rootdir);
  58
+
  59
+// Validate the contents of the plugin ZIP file.
  60
+$validator = tool_installaddon_validator::instance($zipcontentpath, $zipcontentfiles);
  61
+$validator->assert_plugin_type($plugintype);
  62
+$validator->assert_moodle_version($CFG->version);
  63
+$result = $validator->execute();
  64
+
  65
+if ($result) {
  66
+    $validator->set_continue_url(new moodle_url('/admin/tool/installaddon/deploy.php', array(
  67
+        'sesskey' => sesskey(),
  68
+        'jobid' => $jobid,
  69
+        'type' => $plugintype,
  70
+        'name' => $validator->get_rootdir())));
  71
+
  72
+} else {
  73
+    fulldelete($CFG->tempdir.'/tool_installaddon/'.$jobid);
  74
+}
  75
+
  76
+// Display the validation results.
  77
+$output = $PAGE->get_renderer('tool_installaddon');
  78
+$output->set_installer_instance($installer);
  79
+$output->set_validator_instance($validator);
  80
+echo $output->validation_page();

0 notes on commit cbd125e

Please sign in to comment.
Something went wrong with that request. Please try again.