MDL-50015 mod_imscp: New WS mod_imscp_view_imscp
[moodle.git] / mod / imscp / mod_form.php
CommitLineData
2ff648fb 1<?php
2ff648fb 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/**
218ab060 18 * This file contains the forms to create and edit an instance of this module
2ff648fb 19 *
35501c37 20 * @package mod_imscp
3b43615f
PS
21 * @copyright 2009 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2ff648fb 23 */
24
3b43615f 25defined('MOODLE_INTERNAL') || die();
bfebaf64 26
218ab060 27require_once($CFG->dirroot.'/course/moodleform_mod.php');
2ff648fb 28require_once($CFG->libdir.'/filelib.php');
29
218ab060
DB
30/**
31 * IMS CP configuration form
32 *
33 * @package mod_imscp
34 * @copyright 2009 Petr Skoda {@link http://skodak.org}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
2ff648fb 37class mod_imscp_mod_form extends moodleform_mod {
218ab060
DB
38 /**
39 * Define the form - called by parent constructor
40 */
41 public function definition() {
2ff648fb 42 global $CFG, $DB;
43 $mform = $this->_form;
44
45 $config = get_config('imscp');
46
218ab060 47 // Title and description.
2ff648fb 48 $mform->addElement('header', 'general', get_string('general', 'form'));
218ab060 49 $mform->addElement('text', 'name', get_string('name'), array('size' => '48'));
2ff648fb 50 if (!empty($CFG->formatstringstriptags)) {
51 $mform->setType('name', PARAM_TEXT);
52 } else {
b8ea3041 53 $mform->setType('name', PARAM_CLEANHTML);
2ff648fb 54 }
55 $mform->addRule('name', null, 'required', null, 'client');
a74cd331 56 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
6398ff53 57 $this->standard_intro_elements();
2ff648fb 58
218ab060 59 // IMS-CP file upload.
2ff648fb 60 $mform->addElement('header', 'content', get_string('contentheader', 'imscp'));
b7fc9d5a 61 $mform->setExpanded('content', true);
436b1642 62 $mform->addElement('filepicker', 'package', get_string('packagefile', 'imscp'));
2ff648fb 63
218ab060
DB
64 $options = array('-1' => get_string('all'), '0' => get_string('no'),
65 '1' => '1', '2' => '2', '5' => '5', '10' => '10', '20' => '20');
2ff648fb 66 $mform->addElement('select', 'keepold', get_string('keepold', 'imscp'), $options);
67 $mform->setDefault('keepold', $config->keepold);
68 $mform->setAdvanced('keepold', $config->keepold_adv);
69
2ff648fb 70 $this->standard_coursemodule_elements();
71
2ff648fb 72 $this->add_action_buttons();
73 }
74
218ab060
DB
75 /**
76 * Perform minimal validation on the settings form
77 * @param array $data
78 * @param array $files
79 */
80 public function validation($data, $files) {
2ff648fb 81 global $USER;
82
83 if ($errors = parent::validation($data, $files)) {
84 return $errors;
85 }
86
b631c408 87 $usercontext = context_user::instance($USER->id);
2ff648fb 88 $fs = get_file_storage();
436b1642 89
64f93798 90 if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['package'], 'id', false)) {
2ff648fb 91 if (!$this->current->instance) {
92 $errors['package'] = get_string('required');
93 return $errors;
94 }
c6fb4910
PS
95 } else {
96 $file = reset($files);
97 if ($file->get_mimetype() != 'application/zip') {
98 $errors['package'] = get_string('invalidfiletype', 'error', '', $file);
218ab060 99 // Better delete current file, it is not usable anyway.
c6fb4910
PS
100 $fs->delete_area_files($usercontext->id, 'user', 'draft', $data['package']);
101 }
2ff648fb 102 }
103
104 return $errors;
105 }
106}