Merge branch 'MDL-56654-master' of git://github.com/damyon/moodle
[moodle.git] / mod / survey / mod_form.php
blobd59d3c41d6671d1a320d97f93ed9f7560fd83100
1 <?php
2 if (!defined('MOODLE_INTERNAL')) {
3 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
6 require_once ($CFG->dirroot.'/course/moodleform_mod.php');
8 class mod_survey_mod_form extends moodleform_mod {
10 function definition() {
11 global $CFG, $DB;
13 $mform =& $this->_form;
15 $strrequired = get_string('required');
17 //-------------------------------------------------------------------------------
18 $mform->addElement('header', 'general', get_string('general', 'form'));
20 $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
21 if (!empty($CFG->formatstringstriptags)) {
22 $mform->setType('name', PARAM_TEXT);
23 } else {
24 $mform->setType('name', PARAM_CLEANHTML);
26 $mform->addRule('name', null, 'required', null, 'client');
28 if (!$options = $DB->get_records_menu("survey", array("template"=>0), "name", "id, name")) {
29 print_error('cannotfindsurveytmpt', 'survey');
32 foreach ($options as $id => $name) {
33 $options[$id] = get_string($name, "survey");
35 $options = array(''=>get_string('choose').'...') + $options;
36 $mform->addElement('select', 'template', get_string("surveytype", "survey"), $options);
37 $mform->addRule('template', $strrequired, 'required', null, 'client');
38 $mform->addHelpButton('template', 'surveytype', 'survey');
40 $this->standard_intro_elements(get_string('customintro', 'survey'));
42 $this->standard_coursemodule_elements();
44 //-------------------------------------------------------------------------------
45 // buttons
46 $this->add_action_buttons();
49 /**
50 * Return submitted data if properly submitted or returns NULL if validation fails or
51 * if there is no submitted data.
53 * @return stdClass submitted data; NULL if not valid or not submitted or cancelled
55 public function get_data() {
56 $data = parent::get_data();
57 if (!$data) {
58 return false;
61 if (!empty($data->completionunlocked)) {
62 // Turn off completion settings if the checkboxes aren't ticked.
63 $autocompletion = !empty($data->completion) &&
64 $data->completion == COMPLETION_TRACKING_AUTOMATIC;
65 if (!$autocompletion || empty($data->completionsubmit)) {
66 $data->completionsubmit = 0;
69 return $data;
72 /**
73 * Add completion rules to form.
74 * @return array
76 public function add_completion_rules() {
77 $mform =& $this->_form;
78 $mform->addElement('checkbox', 'completionsubmit', '', get_string('completionsubmit', 'survey'));
79 return array('completionsubmit');
82 /**
83 * Enable completion rules
84 * @param stdclass $data
85 * @return array
87 public function completion_rule_enabled($data) {
88 return !empty($data['completionsubmit']);